Chameleon-Mini
ISO15693-A.h
1 /*
2  * ISO15693-3.h
3  *
4  * Created on: 24.08.2013
5  * Author: skuser
6  */
7 
8 #ifndef ISO15693_3_H_
9 #define ISO15693_3_H_
10 
11 #include "../Common.h"
12 
13 /* request and response fields addresses */
14 #define ISO15693_ADDR_FLAGS 0x00
15 #define ISO15693_REQ_ADDR_CMD 0x01
16 #define ISO15693_REQ_ADDR_PARAM 0x02
17 
18 #define ISO15693_RES_ADDR_PARAM 0x01
19 
20 /* command codes */
21 #define ISO15693_CMD_INVENTORY 0x01
22 #define ISO15693_CMD_STAY_QUIET 0x02
23 #define ISO15693_CMD_READ_SINGLE 0x20
24 #define ISO15693_CMD_WRITE_SINGLE 0x21
25 #define ISO15693_CMD_LOCK_BLOCK 0x22
26 #define ISO15693_CMD_READ_MULTIPLE 0x23
27 #define ISO15693_CMD_WRITE_MULTIPLE 0x24
28 #define ISO15693_CMD_SELECT 0x25
29 #define ISO15693_CMD_RESET_TO_READY 0x26
30 #define ISO15693_CMD_WRITE_AFI 0x27
31 #define ISO15693_CMD_LOCK_AFI 0x28
32 #define ISO15693_CMD_WRITE_DSFID 0x29
33 #define ISO15693_CMD_LOCK_DSFID 0x2A
34 #define ISO15693_CMD_GET_SYS_INFO 0x2B
35 #define ISO15693_CMD_GET_BLOCK_SEC 0x2C
36 
37 #define ISO15693_REQ_FLAG_SUBCARRIER 0x01
38 #define ISO15693_REQ_FLAG_DATARATE 0x02
39 #define ISO15693_REQ_FLAG_INVENTORY 0x04
40 #define ISO15693_REQ_FLAG_PROT_EXT 0x08
41 #define ISO15693_REQ_FLAG_OPTION 0x40
42 #define ISO15693_REQ_FLAG_RFU 0x80
43 /* When INVENTORY flag is not set: */
44 #define ISO15693_REQ_FLAG_SELECT 0x10
45 #define ISO15693_REQ_FLAG_ADDRESS 0x20
46 /* When INVENTORY flag is set: */
47 #define ISO15693_REQ_FLAG_AFI 0x10
48 #define ISO15693_REQ_FLAG_NB_SLOTS 0x20
49 
50 #define ISO15693_RES_FLAG_NO_ERROR 0x00
51 #define ISO15693_RES_FLAG_ERROR 0x01
52 #define ISO15693_RES_FLAG_PROT_EXT 0x08
53 
54 #define ISO15693_RES_ERR_NOT_SUPP 0x01
55 #define ISO15693_RES_ERR_NOT_REC 0x02
56 #define ISO15693_RES_ERR_OPT_NOT_SUPP 0x03
57 #define ISO15693_RES_ERR_GENERIC 0x0F
58 #define ISO15693_RES_ERR_BLK_NOT_AVL 0x10
59 #define ISO15693_RES_ERR_BLK_ALRD_LKD 0x11
60 #define ISO15693_RES_ERR_BLK_CHG_LKD 0x12
61 #define ISO15693_RES_ERR_BLK_NOT_PRGR 0x13
62 #define ISO15693_RES_ERR_BLK_NOT_LKD 0x14
63 
64 #define ISO15693_RES_INVENTORY_DSFID 0x00
65 
66 #define ISO15693_MIN_FRAME_SIZE 0x04
67 
68 #define ISO15693_GENERIC_UID_SIZE 0x08
69 #define ISO15693_GENERIC_MEM_SIZE 8192
70 
71 #define ISO15693_CRC16_SIZE 0x2 /* Bytes */
72 #define ISO15693_CRC16_POLYNORMAL 0x8408
73 #define ISO15693_CRC16_PRESET 0xFFFF
74 
75 /* The lock status byte has bits assigned as follow */
76 #define ISO15693_MASK_UNLOCKED ( 0 << 0 )
77 #define ISO15693_MASK_USER_LOCK ( 1 << 0 )
78 #define ISO15693_MASK_FACTORY_LOCK ( 1 << 1 )
79 
80 typedef struct {
81  uint8_t *Flags;
82  uint8_t *Command;
83  uint8_t *Parameters;
84  uint8_t ParamLen;
85  bool Addressed;
86  bool Selected;
87 } CurrentFrame;
88 extern CurrentFrame FrameInfo; /* Holds current frame information */
89 extern uint8_t Uid[];
90 extern uint8_t MyAFI; /* Holds current tag's AFI, used during inventory */
91 extern uint16_t ResponseByteCount; /* Length of response, used when building response frames */
92 
93 void ISO15693AppendCRC(uint8_t *FrameBuf, uint16_t FrameBufSize);
94 bool ISO15693CheckCRC(void *FrameBuf, uint16_t FrameBufSize);
95 bool ISO15693PrepareFrame(uint8_t *FrameBuf, uint16_t FrameBytes, CurrentFrame *FrameStruct, uint8_t IsSelected, uint8_t *MyUid, uint8_t MyAFI);
96 bool ISO15693AntiColl(uint8_t *FrameBuf, uint16_t FrameBytes, CurrentFrame *FrameStruct, uint8_t *MyUid);
97 
98 INLINE
99 bool ISO15693CompareUid(uint8_t *Uid1, uint8_t *Uid2) {
100  if ((Uid1[0] == Uid2[7])
101  && (Uid1[1] == Uid2[6])
102  && (Uid1[2] == Uid2[5])
103  && (Uid1[3] == Uid2[4])
104  && (Uid1[4] == Uid2[3])
105  && (Uid1[5] == Uid2[2])
106  && (Uid1[6] == Uid2[1])
107  && (Uid1[7] == Uid2[0])) {
108  return true;
109  } else {
110  return false;
111  }
112 }
113 
114 INLINE
115 void ISO15693CopyUid(uint8_t *DstUid, uint8_t *SrcUid) {
116  DstUid[0] = SrcUid[7];
117  DstUid[1] = SrcUid[6];
118  DstUid[2] = SrcUid[5];
119  DstUid[3] = SrcUid[4];
120  DstUid[4] = SrcUid[3];
121  DstUid[5] = SrcUid[2];
122  DstUid[6] = SrcUid[1];
123  DstUid[7] = SrcUid[0];
124 }
125 
126 INLINE
127 bool ISO15693Addressed(uint8_t *Buffer) {
128  return (Buffer[ISO15693_ADDR_FLAGS] & ISO15693_REQ_FLAG_ADDRESS); /* if the flag is set, the command is addressed */
129 }
130 
131 
132 INLINE
133 bool ISO15693AddressedLegacy(uint8_t *Buffer, uint8_t *MyUid) {
134  if (Buffer[0] & ISO15693_REQ_FLAG_ADDRESS) {
135  /* Addressed mode */
136  if (ISO15693CompareUid(&Buffer[2], MyUid)) {
137  /* Our UID addressed */
138  return true;
139  } else {
140  /* Our UID not addressed */
141  return false;
142  }
143  } else {
144  /* Non-Addressed mode */
145  return true;
146  }
147 }
148 
149 #endif /* ISO15693_3_H_ */