From 804ed4cb021c4acc05cdd7cd6ae61fca43fb069f Mon Sep 17 00:00:00 2001 From: Federico Cerutti Date: Sat, 10 Nov 2018 20:56:17 +0100 Subject: [PATCH] Added standard error codes and flags in ISO15693-A.h. Replaced magic numbers in TITagstandard.c and refined the state machine, also @m2otech moved UID flipping to TITagitstandardFlipUid. calloc is not used anymore (to avoid compiler warnings and unnecessary includes). Moved definitions to TITagstandard.h. Changed ISO15693Addressed calls in Vicinity.c and Sl2s2002.c to ISO15693AddressedLegacy. The latter should be removed sooner or later... --- .../Chameleon-Mini/Application/ISO15693-A.h | 30 ++++- .../Chameleon-Mini/Application/Sl2s2002.c | 22 ++-- .../Application/TITagitstandard.c | 118 ++++++++---------- .../Application/TITagitstandard.h | 12 +- .../Chameleon-Mini/Application/Vicinity.c | 13 +- 5 files changed, 102 insertions(+), 93 deletions(-) diff --git a/Firmware/Chameleon-Mini/Application/ISO15693-A.h b/Firmware/Chameleon-Mini/Application/ISO15693-A.h index 1d0996b..3f45a6f 100644 --- a/Firmware/Chameleon-Mini/Application/ISO15693-A.h +++ b/Firmware/Chameleon-Mini/Application/ISO15693-A.h @@ -10,6 +10,14 @@ #include "../Common.h" +/* request and response fields addresses */ +#define ISO15693_ADDR_FLAGS 0x00 +#define ISO15693_REQ_ADDR_CMD 0x01 +#define ISO15693_REQ_ADDR_PARAM 0x02 + +#define ISO15693_RES_ADDR_PARAM 0x01 + +/* command codes */ #define ISO15693_CMD_INVENTORY 0x01 #define ISO15693_CMD_STAY_QUIET 0x02 #define ISO15693_CMD_READ_SINGLE 0x20 @@ -39,6 +47,7 @@ #define ISO15693_REQ_FLAG_AFI 0x10 #define ISO15693_REQ_FLAG_NB_SLOTS 0x20 +#define ISO15693_RES_FLAG_NO_ERROR 0x00 #define ISO15693_RES_FLAG_ERROR 0x01 #define ISO15693_RES_FLAG_PROT_EXT 0x08 @@ -52,6 +61,7 @@ #define ISO15693_RES_ERR_BLK_NOT_PRGR 0x13 #define ISO15693_RES_ERR_BLK_NOT_LKD 0x14 +#define ISO15693_RES_INVENTORY_DSFID 0x00 #define ISO15693_MIN_FRAME_SIZE 5 @@ -96,9 +106,25 @@ void ISO15693CopyUid(uint8_t* DstUid, uint8_t* SrcUid) INLINE bool ISO15693Addressed(uint8_t* Buffer) { - return (Buffer[0] & ISO15693_REQ_FLAG_ADDRESS); /* if the flag is set, the command is addressed */ + return (Buffer[ISO15693_ADDR_FLAGS] & ISO15693_REQ_FLAG_ADDRESS); /* if the flag is set, the command is addressed */ } -// (FrameBuf[0] & ISO15693_REQ_FLAG_ADDRESS) && ISO15693CompareUid(&FrameBuf[2], Uid) + +INLINE +bool ISO15693AddressedLegacy(uint8_t* Buffer, uint8_t* MyUid) { + if (Buffer[0] & ISO15693_REQ_FLAG_ADDRESS) { + /* Addressed mode */ + if ( ISO15693CompareUid(&Buffer[2], MyUid) ) { + /* Our UID addressed */ + return true; + } else { + /* Our UID not addressed */ + return false; + } + } else { + /* Non-Addressed mode */ + return true; + } +} #endif /* ISO15693_3_H_ */ diff --git a/Firmware/Chameleon-Mini/Application/Sl2s2002.c b/Firmware/Chameleon-Mini/Application/Sl2s2002.c index 4010ed4..dbef868 100644 --- a/Firmware/Chameleon-Mini/Application/Sl2s2002.c +++ b/Firmware/Chameleon-Mini/Application/Sl2s2002.c @@ -3,6 +3,10 @@ * * Created on: 01-03-2017 * Author: Phillip Nash + * + * TODO: + * - ISO15693AddressedLegacy should be replaced with ISO15693Addressed and appropriate check + * should be performed (see TITagitstandard.c) - ceres-c */ @@ -62,13 +66,11 @@ uint16_t Sl2s2002AppProcess(uint8_t* FrameBuf, uint16_t FrameBytes) ISO15693CopyUid(&FrameBuf[2], Uid); ResponseByteCount = 10; } else if (Command == ISO15693_CMD_STAY_QUIET) { - /* TODO: check for unaddressed requests */ - if (ISO15693Addressed(FrameBuf) && ISO15693CompareUid(&FrameBuf[2], Uid)) { + if (ISO15693AddressedLegacy(FrameBuf, Uid)) { State = STATE_QUIET; } } else if (Command == ISO15693_CMD_GET_SYS_INFO) { - /* TODO: check for unaddressed requests */ - if (ISO15693Addressed(FrameBuf) && ISO15693CompareUid(&FrameBuf[2], Uid)) { + if (ISO15693AddressedLegacy(FrameBuf, Uid)) { FrameBuf[0] = 0; /* Flags */ FrameBuf[1] = 0x0F; /* InfoFlags */ ISO15693CopyUid(&FrameBuf[2], Uid); @@ -80,8 +82,7 @@ uint16_t Sl2s2002AppProcess(uint8_t* FrameBuf, uint16_t FrameBytes) ResponseByteCount = 15; } } else if (Command == ISO15693_CMD_READ_SINGLE) { - /* TODO: check for unaddressed requests */ - if (ISO15693Addressed(FrameBuf) && ISO15693CompareUid(&FrameBuf[2], Uid)) { + if (ISO15693AddressedLegacy(FrameBuf, Uid)) { uint8_t PageAddress = FrameBuf[10]; if (FrameBuf[0] & ISO15693_REQ_FLAG_OPTION) { @@ -96,8 +97,7 @@ uint16_t Sl2s2002AppProcess(uint8_t* FrameBuf, uint16_t FrameBytes) } } } else if (Command == ISO15693_CMD_READ_MULTIPLE) { - /* TODO: check for unaddressed requests */ - if (ISO15693Addressed(FrameBuf) && ISO15693CompareUid(&FrameBuf[2], Uid)) { + if (ISO15693AddressedLegacy(FrameBuf, Uid)) { uint16_t PageAddress = FrameBuf[10]; uint16_t PageAddressCount = FrameBuf[11] + 1; @@ -120,8 +120,7 @@ uint16_t Sl2s2002AppProcess(uint8_t* FrameBuf, uint16_t FrameBytes) FrameBuf[0] = 0; /* Flags */ } } else if (Command == ISO15693_CMD_GET_BLOCK_SEC) { - /* TODO: check for unaddressed requests */ - if (ISO15693Addressed(FrameBuf) && ISO15693CompareUid(&FrameBuf[2], Uid)) { + if (ISO15693AddressedLegacy(FrameBuf, Uid)) { uint8_t PageAddressStart = FrameBuf[10]; uint8_t PageAddressCount = FrameBuf[11] + 1; FrameBuf[0] = 0; /* Flags */ @@ -138,8 +137,7 @@ uint16_t Sl2s2002AppProcess(uint8_t* FrameBuf, uint16_t FrameBytes) case STATE_QUIET: if (Command == ISO15693_CMD_RESET_TO_READY) { - /* TODO: check for unaddressed requests */ - if (ISO15693Addressed(FrameBuf) && ISO15693CompareUid(&FrameBuf[2], Uid)) { + if (ISO15693AddressedLegacy(FrameBuf, Uid)) { FrameBuf[0] = 0; ResponseByteCount = 1; State = STATE_READY; diff --git a/Firmware/Chameleon-Mini/Application/TITagitstandard.c b/Firmware/Chameleon-Mini/Application/TITagitstandard.c index 0830b20..5c54241 100644 --- a/Firmware/Chameleon-Mini/Application/TITagitstandard.c +++ b/Firmware/Chameleon-Mini/Application/TITagitstandard.c @@ -5,9 +5,10 @@ * Author: Phillip Nash * Modified by rickventura for texas 15693 tag-it STANDARD * Modified by ceres-c to finish things up + * TODO: + * - Selected mode has to be impemented altogether - ceres-c */ - #include "TITagitstandard.h" #include "../Codec/ISO15693.h" #include "../Memory.h" @@ -15,10 +16,6 @@ #include "../Random.h" #include "ISO15693-A.h" -#define BYTES_PER_PAGE 4 -#define NUMBER_OF_SECTORS ( TITAGIT_STD_MEM_SIZE / BYTES_PER_PAGE ) -#define MEM_UID_ADDRESS 0x20 - static enum { STATE_READY, STATE_SELECTED, @@ -49,16 +46,8 @@ void TITagitstandardAppTask(void) void TITagitstandardAppTick(void) { - } -// void sendIntToTermina(uint8_t val) -// { -// char buf[16]; -// snprintf(buf,16, "%02x",val); -// TerminalSendString(buf); -// } - uint16_t TITagitstandardAppProcess(uint8_t* FrameBuf, uint16_t FrameBytes) { if (FrameBytes >= ISO15693_MIN_FRAME_SIZE) { @@ -66,94 +55,87 @@ uint16_t TITagitstandardAppProcess(uint8_t* FrameBuf, uint16_t FrameBytes) // At this point, we have a valid ISO15693 frame uint8_t Command = FrameBuf[1]; uint16_t ResponseByteCount = ISO15693_APP_NO_RESPONSE; - uint8_t Uid[8]; - - //MemoryReadBlock(actualTagIt, 0, 44); // read the whole tag from FRAM + uint8_t Uid[ActiveConfiguration.UidSize]; TITagitstandardGetUid(Uid); - //for (j==0 ; j < ActiveConfiguration.UidSize; j++) Uid[j] = actualTagIt[MEM_UID_ADDRESS + j] ; - switch(State) { case STATE_READY: if (Command == ISO15693_CMD_INVENTORY) { - FrameBuf[0] = 0x00; /* Flags */ - FrameBuf[1] = 0x00; /* DSFID */ - ISO15693CopyUid(&FrameBuf[2], Uid); - + FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; + FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_INVENTORY_DSFID; + ISO15693CopyUid(&FrameBuf[ISO15693_RES_ADDR_PARAM + 0x01], Uid); ResponseByteCount = 10; - } else if (Command == ISO15693_CMD_STAY_QUIET) { - if (ISO15693Addressed(FrameBuf) && ISO15693CompareUid(&FrameBuf[2], Uid)) { + if (ISO15693Addressed(FrameBuf) && ISO15693CompareUid(&FrameBuf[ISO15693_REQ_ADDR_PARAM], Uid)) State = STATE_QUIET; - } } else if (Command == ISO15693_CMD_READ_SINGLE) { uint8_t *FramePtr; uint8_t PageAddress; - if (ISO15693Addressed(FrameBuf)) - if (ISO15693CompareUid(&FrameBuf[2], Uid)) /* read is addressed to us */ - PageAddress = FrameBuf[10]; /* when receiving an addressed request pick block number from the 10th byte in the request*/ - else { /* we are not the addressee of the read command */ - ResponseByteCount = 0; + if (ISO15693Addressed(FrameBuf)) { + if (ISO15693CompareUid(&FrameBuf[ISO15693_REQ_ADDR_PARAM], Uid)) /* read is addressed to us */ + /* pick block 2 + 8 (UID Lenght) */ + PageAddress = FrameBuf[ISO15693_REQ_ADDR_PARAM + 0x08]; + else /* we are not the addressee of the read command */ break; - } - else - PageAddress = FrameBuf[2]; - - if (PageAddress >= NUMBER_OF_SECTORS) { /* the reader is requesting a sector out of bound */ - FrameBuf[0] = ISO15693_RES_FLAG_ERROR; - FrameBuf[1] = ISO15693_RES_ERR_BLK_NOT_AVL; /* real TiTag standard reply with this error */ + } else /* request is not addressed */ + PageAddress = FrameBuf[ISO15693_REQ_ADDR_PARAM]; + + if (PageAddress >= TITAGIT_NUMBER_OF_SECTORS) { /* the reader is requesting a sector out of bound */ + FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR; + FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_BLK_NOT_AVL; /* real TiTag standard reply with this error */ ResponseByteCount = 2; break; } - if (FrameBuf[0] & ISO15693_REQ_FLAG_OPTION) { /* request with option flag set */ - FrameBuf[0] = 0x00; /* Flags */ - FrameBuf[1] = ( PageAddress == 8 || PageAddress == 9) ? 0x02 : 0x00; /* block security status:when request has the option flag set*/ + if (FrameBuf[ISO15693_ADDR_FLAGS] & ISO15693_REQ_FLAG_OPTION) { /* request with option flag set */ + FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; + /* UID is stored in blocks 8 and 9 which are blocked */ + FrameBuf[1] = ( PageAddress == 8 || PageAddress == 9) ? 0x02 : 0x00; /* block security status: when request has the option flag set */ FramePtr = FrameBuf + 2; ResponseByteCount = 6; } else { /* request with option flag not set*/ - FrameBuf[0] = 0x00; /* Flags */ + FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_REQ_FLAG_OPTION; /* Flags */ FramePtr = FrameBuf + 1; ResponseByteCount = 5; } - MemoryReadBlock(FramePtr, PageAddress * BYTES_PER_PAGE, BYTES_PER_PAGE); + MemoryReadBlock(FramePtr, PageAddress * TITAGIT_BYTES_PER_PAGE, TITAGIT_BYTES_PER_PAGE); } - else if (Command == ISO15693_CMD_WRITE_SINGLE){ + else if (Command == ISO15693_CMD_WRITE_SINGLE) { uint8_t* Dataptr; uint8_t PageAddress; - - if (ISO15693Addressed(FrameBuf)) - if (ISO15693CompareUid(&FrameBuf[2], Uid)) {/* write is addressed to us */ - PageAddress = FrameBuf[10]; /*when receiving an addressed request pick block number from 10th byte in the request*/ - Dataptr = &FrameBuf[11]; + if (ISO15693Addressed(FrameBuf)) { + if (ISO15693CompareUid(&FrameBuf[ISO15693_REQ_ADDR_PARAM], Uid)) {/* write is addressed to us */ + /* pick block 2 + 8 (UID Lenght) */ + PageAddress = FrameBuf[ISO15693_REQ_ADDR_PARAM + 0x08]; /*when receiving an addressed request pick block number from 10th byte in the request*/ + /* pick block 2 + 8 (UID Lenght) + 1 (data starts here) */ + Dataptr = &FrameBuf[ISO15693_REQ_ADDR_PARAM + 0x08 + 0x01]; /* addr of sent data to write in memory */ } - else { /* we are not the addressee of the write command */ - ResponseByteCount = 0; + else /* we are not the addressee of the write command */ break; - } - else { - PageAddress = FrameBuf[2]; - Dataptr = &FrameBuf[3]; + } else { /* request is not addressed */ + PageAddress = FrameBuf[ISO15693_REQ_ADDR_PARAM]; + Dataptr = &FrameBuf[ISO15693_REQ_ADDR_PARAM + 0x01]; } - MemoryWriteBlock(Dataptr, PageAddress * BYTES_PER_PAGE, BYTES_PER_PAGE); - FrameBuf[0] = 0x00; + MemoryWriteBlock(Dataptr, PageAddress * TITAGIT_BYTES_PER_PAGE, TITAGIT_BYTES_PER_PAGE); + FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; ResponseByteCount = 1; } break; case STATE_SELECTED: + /* TODO: Selected has to be impemented altogether */ break; case STATE_QUIET: if (Command == ISO15693_CMD_RESET_TO_READY) { if (ISO15693Addressed(FrameBuf)) { - FrameBuf[0] = 0; + FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; ResponseByteCount = 1; State = STATE_READY; } @@ -183,26 +165,26 @@ uint16_t TITagitstandardAppProcess(uint8_t* FrameBuf, uint16_t FrameBytes) void TITagitstandardGetUid(ConfigurationUidType Uid) { - MemoryReadBlock(&Uid[0], MEM_UID_ADDRESS, ActiveConfiguration.UidSize); - + MemoryReadBlock(&Uid[0], TITAGIT_MEM_UID_ADDRESS, ActiveConfiguration.UidSize); + // Reverse UID after reading it - uint8_t* UidTemp = calloc(ActiveConfiguration.UidSize, sizeof(UidTemp)); - int i; - for (i = 0; i < ActiveConfiguration.UidSize; i++) - UidTemp[i] = Uid[i]; - for (i = 0; i < ActiveConfiguration.UidSize; i++) - Uid[i] = UidTemp[ActiveConfiguration.UidSize - i - 1]; + TITagitstandardFlipUid(Uid); } void TITagitstandardSetUid(ConfigurationUidType Uid) { // Reverse UID before writing it - uint8_t* UidTemp = calloc(ActiveConfiguration.UidSize, sizeof(UidTemp)); + TITagitstandardFlipUid(Uid); + + MemoryWriteBlock(Uid, TITAGIT_MEM_UID_ADDRESS, ActiveConfiguration.UidSize); +} + +void TITagitstandardFlipUid(ConfigurationUidType Uid) +{ + uint8_t UidTemp[ActiveConfiguration.UidSize]; int i; for (i = 0; i < ActiveConfiguration.UidSize; i++) UidTemp[i] = Uid[i]; for (i = 0; i < ActiveConfiguration.UidSize; i++) Uid[i] = UidTemp[ActiveConfiguration.UidSize - i - 1]; - - MemoryWriteBlock(Uid, MEM_UID_ADDRESS, ActiveConfiguration.UidSize); } \ No newline at end of file diff --git a/Firmware/Chameleon-Mini/Application/TITagitstandard.h b/Firmware/Chameleon-Mini/Application/TITagitstandard.h index 839506f..bd984c5 100644 --- a/Firmware/Chameleon-Mini/Application/TITagitstandard.h +++ b/Firmware/Chameleon-Mini/Application/TITagitstandard.h @@ -12,8 +12,11 @@ #include "Application.h" #include "ISO15693-A.h" -#define TITAGIT_STD_UID_SIZE ISO15693_GENERIC_UID_SIZE //ISO15693_UID_SIZE -#define TITAGIT_STD_MEM_SIZE 44 //TAG-IT STANDARD MAX MEM SIZE +#define TITAGIT_STD_UID_SIZE ISO15693_GENERIC_UID_SIZE //ISO15693_UID_SIZE +#define TITAGIT_STD_MEM_SIZE 44 //TAG-IT STANDARD MAX MEM SIZE +#define TITAGIT_BYTES_PER_PAGE 4 +#define TITAGIT_NUMBER_OF_SECTORS ( TITAGIT_STD_MEM_SIZE / TITAGIT_BYTES_PER_PAGE ) +#define TITAGIT_MEM_UID_ADDRESS 0x20 void TITagitstandardAppInit(void); void TITagitstandardAppReset(void); @@ -22,7 +25,6 @@ void TITagitstandardAppTick(void); uint16_t TITagitstandardAppProcess(uint8_t* FrameBuf, uint16_t FrameBytes); void TITagitstandardGetUid(ConfigurationUidType Uid); void TITagitstandardSetUid(ConfigurationUidType Uid); +void TITagitstandardFlipUid(ConfigurationUidType Uid); - - -#endif /* VICINITY_H_ */ +#endif /* VICINITY_H_ */ \ No newline at end of file diff --git a/Firmware/Chameleon-Mini/Application/Vicinity.c b/Firmware/Chameleon-Mini/Application/Vicinity.c index ac0e7d4..0b8b9a4 100644 --- a/Firmware/Chameleon-Mini/Application/Vicinity.c +++ b/Firmware/Chameleon-Mini/Application/Vicinity.c @@ -3,6 +3,10 @@ * * Created on: 01-03-2017 * Author: Phillip Nash + * + * TODO: + * - ISO15693AddressedLegacy should be replaced with ISO15693Addressed and appropriate check + * should be performed (see TITagitstandard.c) - ceres-c */ @@ -61,13 +65,11 @@ uint16_t VicinityAppProcess(uint8_t* FrameBuf, uint16_t FrameBytes) ISO15693CopyUid(&FrameBuf[2], Uid); ResponseByteCount = 10; } else if (Command == ISO15693_CMD_STAY_QUIET) { - /* TODO: check for unaddressed requests */ - if (ISO15693Addressed(FrameBuf) && ISO15693CompareUid(&FrameBuf[2], Uid)) { + if (ISO15693AddressedLegacy(FrameBuf, Uid)) { State = STATE_QUIET; } } else if (Command == ISO15693_CMD_GET_SYS_INFO) { - /* TODO: check for unaddressed requests */ - if (ISO15693Addressed(FrameBuf) && ISO15693CompareUid(&FrameBuf[2], Uid)) { + if (ISO15693AddressedLegacy(FrameBuf, Uid)) { FrameBuf[0] = 0; /* Flags */ FrameBuf[1] = 0; /* InfoFlags */ ISO15693CopyUid(&FrameBuf[2], Uid); @@ -83,8 +85,7 @@ uint16_t VicinityAppProcess(uint8_t* FrameBuf, uint16_t FrameBytes) case STATE_QUIET: if (Command == ISO15693_CMD_RESET_TO_READY) { MemoryReadBlock(Uid, MEM_UID_ADDRESS, ActiveConfiguration.UidSize); - /* TODO: check for unaddressed requests */ - if (ISO15693Addressed(FrameBuf) && ISO15693CompareUid(&FrameBuf[2], Uid)) { + if (ISO15693AddressedLegacy(FrameBuf, Uid)) { FrameBuf[0] = 0; ResponseByteCount = 1; State = STATE_READY;