Skip to content

How to write single byte #7

Description

@Ricky1966

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:

  • record header;
  • byte 0x01h should be the number of successive byte(s) for the real length of the payload;
  • payload length ;
  • payload type, example 0x54h (T = Text);
    -this byte 0x02h is the UTF8 encoding byte with two bytes for the language;
  • first byte language;
  • second byte language.
    After this list, I put the payload as the second value in the list, and so on until the end of the records.
    Now let's get to the point, if you help me write a function in NFC_PN532.py
    that allows me to directly write the single byte in the memory location that interests me, I would be able to change the value of the payload. always staying within its length. This can be done knowing that the value of the record header contains the length of the payload.
    Very important, the length of the payload starts from the UTF encoding byte, so a dump of a part of the NDEF message like this :
    03 ... 91 01 06 54 02 65 6e 61 61 61
    it means :
  • 03 ISO14333-3A
  • ... length of message ;
  • 91 that the records are more than 1;
  • 01 next byte is payload length;
  • 06 payload length 6 bytes;
  • 54 the payload is of text type;
  • UTF8 encoded (it's also the beginning of the payload);
  • e ;
  • n ;
  • a;
  • a;
  • a.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions