Carglglz / NFC_PN532_SPI

Partial Port of Adafruit CircuitPython to Micropython of PN532 NFC/RFID control library (SPI)
MIT License
36 stars 8 forks source link

How to write single byte #7

Open Ricky1966 opened 11 months ago

Ricky1966 commented 11 months ago

Hi, I put this post in the issuu, but actually it's not a real problem. I'm implementing a couple of new functions to your library the first one involves reading single, byte given the specific number of bytes to read. I need this because to read a multi-record tag, I need to know certain bytes. But now I find it difficult to write a certain byte. The function : def mifare_classic_read_block(self, block_number): expects that you pass it the block and a 4-byte data array. I need to write the byte directly, is this possible? I attach the lines of code that I wrote to be able to read the single byte `

def ntag2xx_read_byte(self, byte_number):

    """Read a block of data from the card.  Block number should be the block
    to read.  If the block is successfully read a bytearray of length 16 with
    data starting at the specified block will be returned.  If the block is
    not read then None will be returned.
    position = (byte_number-4*(byte_number//4))-1
    block_number = byte_number//4
    """

    posiyion = (byte_number-4*(byte_number//4))
    block_number = (byte_number//4)+4
    return self.mifare_classic_read_block(block_number)[position:1+position]  # only 1 bytes per page``

` As you can see, I pass the position of the byte and the function returns the value. This feature allows me to read a single byte memory dump, which makes things easier for me when parsing the NDEF Message. From my research, an NDEF Message begins with 0x03h (should be a byte indicating the ISO 14333-3A encoding) the second byte indicates the length of the message, the actual message starts from the third byte. The message is divided into records and records have a header and a payload. The header is made up of N bytes, it depends on 8 bits which act as flags. I made a couple of functions to establish the value of the header and then its subsequent bytes, The payload starts with the byte of the UTF8 or UTF16 encoding type, I have always encountered UTF8 which is 0x02h which are then the next two bytes for the encoding , in most cases, "en" for english. finally the real payload starts. in my test, I manage to extrapolate all single records, which I load into a multiple array, where the first value is an array representing the first bytes, in order:

So, if I could write to the 10th byte, say 62, the payload would be UTF8 "en" baa instead of UTF8 "en" aaa Thanks for your attention and I hope you can/want to help me

Carglglz commented 11 months ago

@Ricky1966 I think you may want to check the develop branch at https://github.com/Carglglz/NFC_PN532_SPI/tree/develop, nfctag/utag.py is what you may looking for 👀 or at least it can help . I'm not sure exactly what you are trying to do but I guess reading the NDEF record, modify the payload and rewrite it in the tag should be possible...

Ricky1966 commented 11 months ago

Hi, tks. What I'm looking for is to write only 1 bytes for block and not all entire 4 bytes. If you look inside Adafruit library, there are a couples of functions, called ntag2xx_write_block and ntag2xx_read_block. I've create a second function called ntag2xx_read_byte, who return me a single byte I need. It work fine for my porpuse, but, for rewrite a single byte on EEPROM of the TAG, I have to ways, the first is to rewrite entire block (3 bytes remain the same, 1 byte changes) and this is the more easy way. Other way is to rewrite only one byte, but I don't know how...this is my problem!

Carglglz commented 11 months ago

I have to ways, the first is to rewrite entire block (3 bytes remain the same, 1 byte changes) and this is the more easy way. Other way is to rewrite only one byte, but I don't know how...this is my problem!

Well in that case I don't know if writing a single byte is possible, I think

rewrite entire block (3 bytes remain the same, 1 byte changes)

is the only way, but if you find out let me know 👍🏼

Ricky1966 commented 11 months ago

Rewrite the block is easy, for now I keep this way. But for change only one record in a NDEF Message with more than one record, is a little bit borring way. As you know the 5th block 0x04 have 4 bytes the first in my case is everytime 0x03, somewhere I've read this bytes meens start of NDEF Message and ISO 14443/3A, this is right? The second is the lenght of Message, without first and second bytes and the end 0xfe.Also this is right?

Carglglz commented 11 months ago

somewhere I've read this bytes meens start of NDEF Message and ISO 14443/3A, this is right? The second is the lenght of Message, without first and second bytes and the end 0xfe.Also this is right?

I've just followed https://learn.adafruit.com/adafruit-pn532-rfid-nfc/ndef which I guess is what you're looking for

Ricky1966 commented 11 months ago

I've already read that link, but is not explained how are setting the first bytes of ndef message.

Ricky1966 commented 11 months ago

In the Adafruit link you gave me, some things I found on the net are not explained. For example sector 03 OTP0-3 is the sector where you find what kind of tag you are reading. E1 10 3E 00 is an NTag215. I found these data on the net. if instead of 3E you have another value, which I can't remember now, it's NTag213 or 216. They are constants E1 10. I would like to attach a zip file in which I have put some images. in one there are the two tags that I used to test, the one with the paper is TagA, as you can see they are different. The other images are the dump of the memories of TagA, that of paper, of TagB in one image it is formatted, sector 4 is 03 00 FE and in the last two I wrote two records (hello world with spaces). If you notice, they always start with 03, this byte should be the one that identifies that the next message is an ISO 14443/3A NDEF. Then the next byte is the total length of the message excluding 03 and the length byte. Furthermore, the byte FE closing the message is excluded. I haven't tried using the ntag2xx_write_block function directly, but from what I understand it works, it overwrites 4 bytes in the indicated block. This isn't very practical, because if you notice, the records can be across multiple blocks. This is why, through the analysis of the records, I understood, like you, that an NDEF message is made in the following structure:

Ricky1966 commented 11 months ago

TagB_2 TagB_1 Tag_Empty TagA_2 TagA_1 TagA_TagB

Carglglz commented 11 months ago

I haven't tried using the ntag2xx_write_block function directly, but from what I understand it works, it overwrites 4 bytes in the indicated block. This isn't very practical, because if you notice, the records can be across multiple blocks.

The thing is it doesn't look like writing less than a block is possible, i.e. 4. bytes (in case of ntags) is the minimum amount but if you find out that you can write a single byte (maybe in pn532 data sheet?) you could send a pull request and I may be able to test it 👍🏼

Ricky1966 commented 11 months ago

I'll try to write to NXP directly. Maybe they could help me for understand if is possible, if I don't wrong the PN532 chip comes from they.

Ricky1966 commented 11 months ago

If you want my code, I'll gladly give it to you, write me at the email I put above and I'll send it to you.