nfc-types.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Free/Libre Near Field Communication (NFC) library
  3. *
  4. * Libnfc historical contributors:
  5. * Copyright (C) 2009 Roel Verdult
  6. * Copyright (C) 2009-2013 Romuald Conty
  7. * Copyright (C) 2010-2012 Romain Tartière
  8. * Copyright (C) 2010-2013 Philippe Teuwen
  9. * Copyright (C) 2012-2013 Ludovic Rousseau
  10. * See AUTHORS file for a more comprehensive list of contributors.
  11. * Additional contributors of this file:
  12. *
  13. * This program is free software: you can redistribute it and/or modify it
  14. * under the terms of the GNU Lesser General Public License as published by the
  15. * Free Software Foundation, either version 3 of the License, or (at your
  16. * option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful, but WITHOUT
  19. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  20. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  21. * more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. */
  26. /**
  27. * @file nfc-types.h
  28. * @brief Define NFC types
  29. */
  30. #ifndef __NFC_TYPES_H__
  31. #define __NFC_TYPES_H__
  32. #include <stddef.h>
  33. #include <stdint.h>
  34. #include <stdbool.h>
  35. #include <stdio.h>
  36. #ifndef NFC_BUFSIZE_CONNSTRING
  37. #define NFC_BUFSIZE_CONNSTRING 1024
  38. #endif
  39. /**
  40. * NFC context
  41. */
  42. typedef struct nfc_context nfc_context;
  43. /**
  44. * NFC device
  45. */
  46. typedef struct nfc_device nfc_device;
  47. /**
  48. * NFC device driver
  49. */
  50. typedef struct nfc_driver nfc_driver;
  51. /**
  52. * Connection string
  53. */
  54. typedef char nfc_connstring[NFC_BUFSIZE_CONNSTRING];
  55. /**
  56. * Properties
  57. */
  58. typedef enum {
  59. /**
  60. * Default command processing timeout
  61. * Property value's (duration) unit is ms and 0 means no timeout (infinite).
  62. * Default value is set by driver layer
  63. */
  64. NP_TIMEOUT_COMMAND,
  65. /**
  66. * Timeout between ATR_REQ and ATR_RES
  67. * When the device is in initiator mode, a target is considered as mute if no
  68. * valid ATR_RES is received within this timeout value.
  69. * Default value for this property is 103 ms on PN53x based devices.
  70. */
  71. NP_TIMEOUT_ATR,
  72. /**
  73. * Timeout value to give up reception from the target in case of no answer.
  74. * Default value for this property is 52 ms).
  75. */
  76. NP_TIMEOUT_COM,
  77. /** Let the PN53X chip handle the CRC bytes. This means that the chip appends
  78. * the CRC bytes to the frames that are transmitted. It will parse the last
  79. * bytes from received frames as incoming CRC bytes. They will be verified
  80. * against the used modulation and protocol. If an frame is expected with
  81. * incorrect CRC bytes this option should be disabled. Example frames where
  82. * this is useful are the ATQA and UID+BCC that are transmitted without CRC
  83. * bytes during the anti-collision phase of the ISO14443-A protocol. */
  84. NP_HANDLE_CRC,
  85. /** Parity bits in the network layer of ISO14443-A are by default generated and
  86. * validated in the PN53X chip. This is a very convenient feature. On certain
  87. * times though it is useful to get full control of the transmitted data. The
  88. * proprietary MIFARE Classic protocol uses for example custom (encrypted)
  89. * parity bits. For interoperability it is required to be completely
  90. * compatible, including the arbitrary parity bits. When this option is
  91. * disabled, the functions to communicating bits should be used. */
  92. NP_HANDLE_PARITY,
  93. /** This option can be used to enable or disable the electronic field of the
  94. * NFC device. */
  95. NP_ACTIVATE_FIELD,
  96. /** The internal CRYPTO1 co-processor can be used to transmit messages
  97. * encrypted. This option is automatically activated after a successful MIFARE
  98. * Classic authentication. */
  99. NP_ACTIVATE_CRYPTO1,
  100. /** The default configuration defines that the PN53X chip will try indefinitely
  101. * to invite a tag in the field to respond. This could be desired when it is
  102. * certain a tag will enter the field. On the other hand, when this is
  103. * uncertain, it will block the application. This option could best be compared
  104. * to the (NON)BLOCKING option used by (socket)network programming. */
  105. NP_INFINITE_SELECT,
  106. /** If this option is enabled, frames that carry less than 4 bits are allowed.
  107. * According to the standards these frames should normally be handles as
  108. * invalid frames. */
  109. NP_ACCEPT_INVALID_FRAMES,
  110. /** If the NFC device should only listen to frames, it could be useful to let
  111. * it gather multiple frames in a sequence. They will be stored in the internal
  112. * FIFO of the PN53X chip. This could be retrieved by using the receive data
  113. * functions. Note that if the chip runs out of bytes (FIFO = 64 bytes long),
  114. * it will overwrite the first received frames, so quick retrieving of the
  115. * received data is desirable. */
  116. NP_ACCEPT_MULTIPLE_FRAMES,
  117. /** This option can be used to enable or disable the auto-switching mode to
  118. * ISO14443-4 is device is compliant.
  119. * In initiator mode, it means that NFC chip will send RATS automatically when
  120. * select and it will automatically poll for ISO14443-4 card when ISO14443A is
  121. * requested.
  122. * In target mode, with a NFC chip compliant (ie. PN532), the chip will
  123. * emulate a 14443-4 PICC using hardware capability */
  124. NP_AUTO_ISO14443_4,
  125. /** Use automatic frames encapsulation and chaining. */
  126. NP_EASY_FRAMING,
  127. /** Force the chip to switch in ISO14443-A */
  128. NP_FORCE_ISO14443_A,
  129. /** Force the chip to switch in ISO14443-B */
  130. NP_FORCE_ISO14443_B,
  131. /** Force the chip to run at 106 kbps */
  132. NP_FORCE_SPEED_106,
  133. } nfc_property;
  134. // Compiler directive, set struct alignment to 1 uint8_t for compatibility
  135. # pragma pack(1)
  136. /**
  137. * @enum nfc_dep_mode
  138. * @brief NFC D.E.P. (Data Exchange Protocol) active/passive mode
  139. */
  140. typedef enum {
  141. NDM_UNDEFINED = 0,
  142. NDM_PASSIVE,
  143. NDM_ACTIVE,
  144. } nfc_dep_mode;
  145. /**
  146. * @struct nfc_dep_info
  147. * @brief NFC target information in D.E.P. (Data Exchange Protocol) see ISO/IEC 18092 (NFCIP-1)
  148. */
  149. typedef struct {
  150. /** NFCID3 */
  151. uint8_t abtNFCID3[10];
  152. /** DID */
  153. uint8_t btDID;
  154. /** Supported send-bit rate */
  155. uint8_t btBS;
  156. /** Supported receive-bit rate */
  157. uint8_t btBR;
  158. /** Timeout value */
  159. uint8_t btTO;
  160. /** PP Parameters */
  161. uint8_t btPP;
  162. /** General Bytes */
  163. uint8_t abtGB[48];
  164. size_t szGB;
  165. /** DEP mode */
  166. nfc_dep_mode ndm;
  167. } nfc_dep_info;
  168. /**
  169. * @struct nfc_iso14443a_info
  170. * @brief NFC ISO14443A tag (MIFARE) information
  171. */
  172. typedef struct {
  173. uint8_t abtAtqa[2];
  174. uint8_t btSak;
  175. size_t szUidLen;
  176. uint8_t abtUid[10];
  177. size_t szAtsLen;
  178. uint8_t abtAts[254]; // Maximal theoretical ATS is FSD-2, FSD=256 for FSDI=8 in RATS
  179. } nfc_iso14443a_info;
  180. /**
  181. * @struct nfc_felica_info
  182. * @brief NFC FeLiCa tag information
  183. */
  184. typedef struct {
  185. size_t szLen;
  186. uint8_t btResCode;
  187. uint8_t abtId[8];
  188. uint8_t abtPad[8];
  189. uint8_t abtSysCode[2];
  190. } nfc_felica_info;
  191. /**
  192. * @struct nfc_iso14443b_info
  193. * @brief NFC ISO14443B tag information
  194. */
  195. typedef struct {
  196. /** abtPupi store PUPI contained in ATQB (Answer To reQuest of type B) (see ISO14443-3) */
  197. uint8_t abtPupi[4];
  198. /** abtApplicationData store Application Data contained in ATQB (see ISO14443-3) */
  199. uint8_t abtApplicationData[4];
  200. /** abtProtocolInfo store Protocol Info contained in ATQB (see ISO14443-3) */
  201. uint8_t abtProtocolInfo[3];
  202. /** ui8CardIdentifier store CID (Card Identifier) attributted by PCD to the PICC */
  203. uint8_t ui8CardIdentifier;
  204. } nfc_iso14443b_info;
  205. /**
  206. * @struct nfc_iso14443bi_info
  207. * @brief NFC ISO14443B' tag information
  208. */
  209. typedef struct {
  210. /** DIV: 4 LSBytes of tag serial number */
  211. uint8_t abtDIV[4];
  212. /** Software version & type of REPGEN */
  213. uint8_t btVerLog;
  214. /** Config Byte, present if long REPGEN */
  215. uint8_t btConfig;
  216. /** ATR, if any */
  217. size_t szAtrLen;
  218. uint8_t abtAtr[33];
  219. } nfc_iso14443bi_info;
  220. /**
  221. * @struct nfc_iso14443b2sr_info
  222. * @brief NFC ISO14443-2B ST SRx tag information
  223. */
  224. typedef struct {
  225. uint8_t abtUID[8];
  226. } nfc_iso14443b2sr_info;
  227. /**
  228. * @struct nfc_iso14443b2ct_info
  229. * @brief NFC ISO14443-2B ASK CTx tag information
  230. */
  231. typedef struct {
  232. uint8_t abtUID[4];
  233. uint8_t btProdCode;
  234. uint8_t btFabCode;
  235. } nfc_iso14443b2ct_info;
  236. /**
  237. * @struct nfc_jewel_info
  238. * @brief NFC Jewel tag information
  239. */
  240. typedef struct {
  241. uint8_t btSensRes[2];
  242. uint8_t btId[4];
  243. } nfc_jewel_info;
  244. /**
  245. * @union nfc_target_info
  246. * @brief Union between all kind of tags information structures.
  247. */
  248. typedef union {
  249. nfc_iso14443a_info nai;
  250. nfc_felica_info nfi;
  251. nfc_iso14443b_info nbi;
  252. nfc_iso14443bi_info nii;
  253. nfc_iso14443b2sr_info nsi;
  254. nfc_iso14443b2ct_info nci;
  255. nfc_jewel_info nji;
  256. nfc_dep_info ndi;
  257. } nfc_target_info;
  258. /**
  259. * @enum nfc_baud_rate
  260. * @brief NFC baud rate enumeration
  261. */
  262. typedef enum {
  263. NBR_UNDEFINED = 0,
  264. NBR_106,
  265. NBR_212,
  266. NBR_424,
  267. NBR_847,
  268. } nfc_baud_rate;
  269. /**
  270. * @enum nfc_modulation_type
  271. * @brief NFC modulation type enumeration
  272. */
  273. typedef enum {
  274. NMT_ISO14443A = 1,
  275. NMT_JEWEL,
  276. NMT_ISO14443B,
  277. NMT_ISO14443BI, // pre-ISO14443B aka ISO/IEC 14443 B' or Type B'
  278. NMT_ISO14443B2SR, // ISO14443-2B ST SRx
  279. NMT_ISO14443B2CT, // ISO14443-2B ASK CTx
  280. NMT_FELICA,
  281. NMT_DEP,
  282. } nfc_modulation_type;
  283. /**
  284. * @enum nfc_mode
  285. * @brief NFC mode type enumeration
  286. */
  287. typedef enum {
  288. N_TARGET,
  289. N_INITIATOR,
  290. } nfc_mode;
  291. /**
  292. * @struct nfc_modulation
  293. * @brief NFC modulation structure
  294. */
  295. typedef struct {
  296. nfc_modulation_type nmt;
  297. nfc_baud_rate nbr;
  298. } nfc_modulation;
  299. /**
  300. * @struct nfc_target
  301. * @brief NFC target structure
  302. */
  303. typedef struct {
  304. nfc_target_info nti;
  305. nfc_modulation nm;
  306. } nfc_target;
  307. // Reset struct alignment to default
  308. # pragma pack()
  309. #endif // _LIBNFC_TYPES_H_