From 573fc1b778dd701b134e1c8ee664350cfb85409b Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Tue, 5 Apr 2011 21:05:27 +0000 Subject: [PATCH] Implement mifare_ultralight_write() using macros. Update issue 58 I can't reproduce this bug, but saw that the mifare_ultralight_write() was implemented in an old-school way. I updated the code so that you should have better traces when compiling the library in debug mode and running: romain@marvin ~/Projects/libfreefare % cutter -n test_mifare_ultralight_write test ===> 0000 30 07 |0. | <=== 0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| ===> 0000 a2 07 12 34 56 78 |...4Vx | ===> 0000 30 07 |0. | <=== 0000 12 34 56 78 00 00 00 00 00 00 00 00 00 00 00 00 |.4Vx............| ===> 0000 a2 07 aa 55 00 ff |...U.. | ===> 0000 30 07 |0. | <=== 0000 aa 55 00 ff 00 00 00 00 00 00 00 00 00 00 00 00 |.U..............| ===> 0000 a2 07 00 00 00 00 |...... | ===> 0000 30 07 |0. | <=== 0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| . Finished in 0,548238 seconds (total: 0,069079 seconds) 1 test(s), 12 assertion(s), 0 failure(s), 0 error(s), 0 pending(s), 0 omission(s), 0 notification(s) 100% passed --- libfreefare/mifare_ultralight.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/libfreefare/mifare_ultralight.c b/libfreefare/mifare_ultralight.c index fea1406..45703b6 100644 --- a/libfreefare/mifare_ultralight.c +++ b/libfreefare/mifare_ultralight.c @@ -221,16 +221,14 @@ mifare_ultralight_write (MifareTag tag, const MifareUltralightPageNumber page, c ASSERT_MIFARE_ULTRALIGHT (tag); ASSERT_VALID_PAGE (tag, page, true); - uint8_t cmd[6]; - cmd[0] = 0xA2; - cmd[1] = page; - memcpy (cmd + 2, data, sizeof (MifareUltralightPage)); + BUFFER_INIT (cmd, 6); + BUFFER_INIT (res, 1); - size_t n; - if (!(nfc_initiator_transceive_bytes (tag->device, cmd, sizeof (cmd), NULL, &n))) { - errno = EIO; - return -1; - } + BUFFER_APPEND (cmd, 0xA2); + BUFFER_APPEND (cmd, page); + BUFFER_APPEND_BYTES (cmd, data, sizeof (MifareUltralightPage)); + + ULTRALIGHT_TRANSCEIVE (tag, cmd, res); /* Invalidate page in cache */ MIFARE_ULTRALIGHT(tag)->cached_pages[page] = 0;