Module_RFID.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * Module_RFID.c
  3. *
  4. * Created on: 2019|~10?e28?e
  5. * Author: Eason Yang
  6. * Version: D0.01
  7. */
  8. #include "Module_RFID.h"
  9. //==================================
  10. // PRINT OUT LOG FORMAT
  11. //==================================
  12. #define SystemLogMessage
  13. #define DEBUG_INFO_1(format, args...) StoreLogMessage("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  14. #define DEBUG_WARN_1(format, args...) StoreLogMessage("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  15. #define DEBUG_ERROR_1(format, args...) StoreLogMessage("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  16. //==================================
  17. // SYSTEM CONSTANT
  18. //==================================
  19. #define Debug
  20. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  21. #define PASS 1
  22. #define FAIL -1
  23. //==================================
  24. // RFID CMD CONSTANT
  25. //==================================
  26. unsigned int RFID_CMD_ISO1443A_REQUEST = 0x20;
  27. unsigned int RFID_CMD_ISO1443B_REQUEST = 0x60;
  28. unsigned int RFID_CMD_FELICA_POLLING_REQUEST = 0x2F;
  29. unsigned int RFID_CMD_HALT_14443A = 0x28;
  30. unsigned int RFID_CMD_BLOCK_READ = 0x21;
  31. unsigned int RFID_CMD_BLOCK_WRITE = 0X22;
  32. unsigned int RFID_CMD_BUZZER_SET = 0X14;
  33. //==================================
  34. // RFID MODE CONSTANT
  35. //==================================
  36. unsigned char WUPA = 0;
  37. unsigned char REQA = 1;
  38. unsigned char AFI = 0;
  39. //==================================
  40. // MIFARE CARD LENGTH
  41. //==================================
  42. unsigned int LENGTH_0 = 0;
  43. unsigned int LENGTH_4 = 4;
  44. unsigned int LENGTH_6 = 6;
  45. unsigned int LENGTH_7 = 7;
  46. unsigned int LENGTH_10 = 10;
  47. //==================================
  48. // MIFARE SECTOR SPACE
  49. //==================================
  50. #define ROW 6
  51. #define COLUMN 16
  52. unsigned char sectorKeyA[COLUMN][ROW] = { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  53. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  54. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  55. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  56. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  57. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  58. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  59. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  60. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  61. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  62. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  63. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  64. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  65. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  66. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  67. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
  68. };
  69. unsigned char serialNumber[32];
  70. unsigned int cardLength;
  71. //==================================
  72. // SystemLog message
  73. //==================================
  74. #ifdef SystemLogMessage
  75. int StoreLogMessage(const char *fmt, ...)
  76. {
  77. char Buf[4096 + 256];
  78. char buffer[4096];
  79. time_t CurrentTime;
  80. struct tm *tm;
  81. va_list args;
  82. va_start(args, fmt);
  83. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  84. va_end(args);
  85. memset(Buf, 0, sizeof(Buf));
  86. CurrentTime = time(NULL);
  87. tm = localtime(&CurrentTime);
  88. sprintf(Buf,
  89. "echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]RFID_SystemLog",
  90. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
  91. tm->tm_min, tm->tm_sec, buffer, tm->tm_year + 1900, tm->tm_mon + 1);
  92. system(Buf);
  93. #ifdef Debug
  94. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year + 1900,
  95. tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
  96. buffer);
  97. #endif
  98. return rc;
  99. }
  100. #endif
  101. //==========================================
  102. // Module's command send/receive
  103. //==========================================
  104. int system_command(int uart, unsigned char* cmd,int length, unsigned char* rx)
  105. {
  106. int len;
  107. //sleep(2); //required to make flush work, for some reason
  108. tcflush(uart,TCIOFLUSH);
  109. if(write(uart, cmd, length) >= 0)
  110. {
  111. usleep(100000);
  112. len = read(uart, rx, 256);
  113. }
  114. else
  115. {
  116. #ifdef SystemLogMessage
  117. DEBUG_ERROR_1("system command %s response fail.\n", cmd);
  118. #endif
  119. }
  120. return len;
  121. }
  122. //==========================================
  123. // Calculation checksum function
  124. //==========================================
  125. char ClaCheckSum(unsigned char *buffer, int len)
  126. {
  127. int idx;
  128. char chksum = 0;
  129. for(idx = 0 ; idx < len-1 ; idx++)
  130. {
  131. chksum ^= *(buffer+idx);
  132. }
  133. return chksum;
  134. }
  135. bool getRequestCardSN(int Fd, int moduleType, RFID* rfid)
  136. {
  137. bool isSuccess = false;
  138. if(ISO14443A_REQUEST_SN(Fd,moduleType,serialNumber) == true)
  139. {
  140. rfid->cardType = ISO14443A;
  141. if(cardLength == LENGTH_4)
  142. {
  143. rfid->snType = RFID_SN_TYPE_4BYTE;
  144. memcpy(rfid->currentCard, &serialNumber, 32);
  145. }
  146. else if(cardLength == LENGTH_7)
  147. {
  148. rfid->snType = RFID_SN_TYPE_7BYTE;
  149. memcpy(rfid->currentCard, &serialNumber, 32);
  150. }
  151. sethaltCard(Fd,moduleType);
  152. isSuccess = true;
  153. }
  154. else if(ISO14443B_REQUEST_SN(Fd,moduleType,serialNumber) == true)
  155. {
  156. rfid->cardType = IS014443B;
  157. rfid->snType = RFID_SN_TYPE_4BYTE;
  158. memcpy(rfid->currentCard, &serialNumber, 32);
  159. sleep(2);
  160. isSuccess = true;
  161. }
  162. else if(FELICA_REQUEST_SN(Fd,moduleType,serialNumber) == true)
  163. {
  164. rfid->cardType = FELICA;
  165. rfid->snType = RFID_SN_TYPE_6BYTE;
  166. memcpy(rfid->currentCard, &serialNumber, 32);
  167. sleep(2);
  168. isSuccess = true;
  169. }
  170. else
  171. {}
  172. return isSuccess;
  173. }
  174. //==========================================
  175. // ISO14443A Request SN function
  176. //==========================================
  177. bool ISO14443A_REQUEST_SN(int Fd, int moduleType, unsigned char *data)
  178. {
  179. bool isSuccess = false;
  180. int tx_len = 4;
  181. unsigned char txByte[tx_len];
  182. unsigned char rxByte[254];
  183. unsigned char tmp[254];
  184. switch(moduleType)
  185. {
  186. case MODULE_EWT:
  187. default:
  188. //===============================
  189. // Command
  190. //===============================
  191. txByte[0] = tx_len-1;
  192. txByte[1] = RFID_CMD_ISO1443A_REQUEST;
  193. txByte[2] = REQA;
  194. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  195. if(Fd > 0)
  196. {
  197. memset(rxByte, 0, sizeof (rxByte));
  198. if(system_command(Fd, txByte, tx_len, rxByte) > 0)
  199. {
  200. memset(tmp, 0, sizeof tmp);
  201. memcpy(tmp, rxByte, sizeof(rxByte));
  202. if(tmp[1] == RFID_CMD_ISO1443A_REQUEST)
  203. {
  204. if(tmp[0] == 0x09)
  205. {
  206. #ifdef SystemLogMessage
  207. DEBUG_INFO_1("MIFARE CLASSIC.\n");
  208. #endif
  209. cardLength = LENGTH_4;
  210. memcpy(data, rxByte+2, cardLength);
  211. isSuccess = true;
  212. }
  213. else if(tmp[0] == 0x0C)
  214. {
  215. #ifdef SystemLogMessage
  216. DEBUG_INFO_1("MIFARE PLUS.\n");
  217. #endif
  218. cardLength = LENGTH_7;
  219. memcpy(data, rxByte+2, cardLength);
  220. isSuccess = true;
  221. }
  222. else
  223. {}
  224. }
  225. else if (tmp[1] == 0xDF)
  226. {
  227. cardLength = LENGTH_0;
  228. isSuccess = false;
  229. }
  230. }
  231. else
  232. {}
  233. }
  234. else
  235. {}
  236. break;
  237. }
  238. return isSuccess;
  239. }
  240. //==========================================
  241. // ISO14443B Request SN function
  242. //==========================================
  243. bool ISO14443B_REQUEST_SN(int Fd, int moduleType, unsigned char *data)
  244. {
  245. bool isSuccess = false;
  246. int cardLength;
  247. int tx_len = 5;
  248. unsigned char txByte[tx_len];
  249. unsigned char rxByte[254];
  250. unsigned char tmp[254];
  251. switch(moduleType)
  252. {
  253. case MODULE_EWT:
  254. default:
  255. //===============================
  256. // Command
  257. //===============================
  258. txByte[0] = tx_len-1;
  259. txByte[1] = RFID_CMD_ISO1443B_REQUEST;
  260. txByte[2] = WUPA;
  261. txByte[3] = AFI;
  262. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  263. if(Fd > 0)
  264. {
  265. memset(rxByte, 0, sizeof (rxByte));
  266. if(system_command(Fd, txByte, tx_len, rxByte) > 0)
  267. {
  268. memset(tmp, 0, sizeof tmp);
  269. memcpy(tmp, rxByte, sizeof(rxByte));
  270. if(tmp[1] == RFID_CMD_ISO1443B_REQUEST)
  271. {
  272. #ifdef SystemLogMessage
  273. DEBUG_INFO_1("ISO14443 TYPE B.\n");
  274. #endif
  275. cardLength = LENGTH_4;
  276. memcpy(data, rxByte+3, cardLength);
  277. isSuccess = true;
  278. }
  279. else if(tmp[1] == 0x9F)
  280. {
  281. cardLength = LENGTH_0;
  282. isSuccess = false;
  283. }
  284. }
  285. else
  286. {}
  287. }
  288. else
  289. {}
  290. break;
  291. }
  292. return isSuccess;
  293. }
  294. //==========================================
  295. // FELICA Request SN function
  296. //==========================================
  297. bool FELICA_REQUEST_SN(int Fd, int moduleType, unsigned char *data)
  298. {
  299. bool isSuccess = false;
  300. int cardLength;
  301. int tx_len = 9;
  302. unsigned char txByte[tx_len];
  303. unsigned char rxByte[254];
  304. unsigned char tmp[254];
  305. switch(moduleType)
  306. {
  307. case MODULE_EWT:
  308. default:
  309. //===============================
  310. // Command
  311. //===============================
  312. txByte[0] = tx_len-1;
  313. txByte[1] = RFID_CMD_FELICA_POLLING_REQUEST;
  314. txByte[2] = 0x06;
  315. txByte[3] = 0x00;
  316. txByte[4] = 0xFF;
  317. txByte[5] = 0xFF;
  318. txByte[6] = 0x01;
  319. txByte[7] = 0x00;
  320. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  321. if(Fd > 0)
  322. {
  323. memset(rxByte, 0, sizeof (rxByte));
  324. if(system_command(Fd, txByte, tx_len, rxByte) > 0)
  325. {
  326. memset(tmp, 0, sizeof tmp);
  327. memcpy(tmp, rxByte, sizeof(rxByte));
  328. if(tmp[1] == RFID_CMD_FELICA_POLLING_REQUEST)
  329. {
  330. #ifdef SystemLogMessage
  331. DEBUG_INFO_1("FELICA.\n");
  332. #endif
  333. cardLength = LENGTH_6;
  334. memcpy(data, rxByte+6, cardLength);
  335. isSuccess = true;
  336. }
  337. else if(tmp[1] == 0xD0)
  338. {
  339. cardLength = LENGTH_0;
  340. isSuccess = false;
  341. }
  342. }
  343. else
  344. {}
  345. }
  346. else
  347. {}
  348. break;
  349. }
  350. return isSuccess;
  351. }
  352. //==========================================
  353. // Read block data from RFID card
  354. //==========================================
  355. bool getBlockRead(int Fd, int moduleType, int block, unsigned char keyId, unsigned char *data)
  356. {
  357. bool isSuccess = false;
  358. int i;
  359. int j = 0;
  360. int len = 11;
  361. unsigned char txByte[len];
  362. unsigned char rxByte[254];
  363. unsigned char tmp[254];
  364. switch(moduleType)
  365. {
  366. case MODULE_EWT:
  367. default:
  368. //===============================
  369. // Command
  370. //===============================
  371. txByte[0] = 0x0A;
  372. txByte[1] = RFID_CMD_BLOCK_READ;
  373. txByte[2] = keyId;
  374. txByte[3] = block;
  375. for(i = 4; i < 10; i++)
  376. {
  377. if( j < sizeof (sectorKeyA[0]))
  378. {
  379. txByte[i] = sectorKeyA[(int)(block/4)][j];
  380. j++;
  381. }
  382. }
  383. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  384. if(Fd > 0)
  385. {
  386. memset(rxByte, 0, sizeof(rxByte));
  387. if(system_command(Fd, txByte, len, rxByte) > 0)
  388. {
  389. memset(tmp, 0, sizeof tmp);
  390. memcpy(tmp, rxByte, sizeof (rxByte));
  391. if((tmp[1] == RFID_CMD_BLOCK_READ))
  392. {
  393. #ifdef SystemLogMessage
  394. DEBUG_INFO_1("Read block ok.\n");
  395. #endif
  396. memcpy(data, rxByte+2, 16);
  397. isSuccess = true;
  398. }
  399. else if (tmp[1] == 0xDE)
  400. {
  401. #ifdef SystemLogMessage
  402. DEBUG_INFO_1("Read block failed.\n");
  403. #endif
  404. }
  405. else
  406. {}
  407. }
  408. else
  409. {}
  410. }
  411. else
  412. {}
  413. break;
  414. }
  415. return isSuccess;
  416. }
  417. //==========================================
  418. // Write data into RFID card block
  419. //==========================================
  420. bool setBlockWrite(int Fd, int moduleType, int block, unsigned char keyid, unsigned char *data)
  421. {
  422. bool isSuccess = false;
  423. int i;
  424. int j = 0;
  425. int len = 27;
  426. unsigned char txByte[len];
  427. unsigned char rxByte[254];
  428. unsigned char tmp[254];
  429. switch(moduleType)
  430. {
  431. case MODULE_EWT:
  432. default:
  433. //===============================
  434. // Command
  435. //===============================
  436. txByte[0] = 0x1A;
  437. txByte[1] = RFID_CMD_BLOCK_WRITE;
  438. txByte[2] = keyid;
  439. txByte[3] = block;
  440. for(i = 4; i < 10; i++)
  441. {
  442. if( j < sizeof (sectorKeyA[0]))
  443. {
  444. txByte[i] = sectorKeyA[(int)(block/4)][j];
  445. j++;
  446. }
  447. }
  448. memcpy(txByte+10, data, 16);
  449. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,(ARRAY_SIZE(txByte)));
  450. if(Fd > 0)
  451. {
  452. memset(rxByte, 0, sizeof(rxByte));
  453. if(system_command(Fd, txByte, len, rxByte) > 0)
  454. {
  455. memset(tmp, 0, sizeof tmp);
  456. memcpy(tmp, rxByte, sizeof (rxByte));
  457. if(tmp[1] == RFID_CMD_BLOCK_WRITE)
  458. {
  459. #ifdef SystemLogMessage
  460. DEBUG_INFO_1("Write block ok.\n");
  461. #endif
  462. isSuccess = true;
  463. }
  464. else if(tmp[1] == 0xDD)
  465. {
  466. #ifdef SystemLogMessage
  467. DEBUG_INFO_1("Write block failed.\n");
  468. #endif
  469. isSuccess = false;
  470. }
  471. }
  472. else
  473. {}
  474. }
  475. else
  476. {}
  477. break;
  478. }
  479. return isSuccess;
  480. }
  481. //==========================================
  482. // Buzzer set (EWT Module)
  483. //==========================================
  484. void setBuzzer(int Fd, int moduleType, unsigned char time)
  485. {
  486. int len = 4;
  487. unsigned char txByte[len];
  488. unsigned char rxByte[254];
  489. unsigned char tmp[254];
  490. switch(moduleType)
  491. {
  492. case MODULE_EWT:
  493. default:
  494. //===============================
  495. // Command
  496. //===============================
  497. txByte[0] = 0x03;
  498. txByte[1] = RFID_CMD_BUZZER_SET;
  499. if(time > 0x0A)
  500. {
  501. time = 0x05;
  502. txByte[2] = time;
  503. #ifdef SystemLogMessage
  504. DEBUG_WARN_1("Value is out of range.\n");
  505. #endif
  506. }
  507. else
  508. {
  509. txByte[2] = time;
  510. }
  511. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  512. if(Fd > 0)
  513. {
  514. memset(rxByte, 0, sizeof(rxByte));
  515. if(system_command(Fd, txByte, len, rxByte) > 0)
  516. {
  517. memset(tmp, 0, sizeof tmp);
  518. memcpy(tmp, rxByte, sizeof (rxByte));
  519. if(tmp[1] == 0x14)
  520. {
  521. #ifdef SystemLogMessage
  522. DEBUG_INFO_1("Set Buzzer ok.\n");
  523. DEBUG_INFO_1("Set Buzzer %d ms.\n",time);
  524. #endif
  525. }
  526. else if(tmp[1] == 0xEC)
  527. {
  528. #ifdef SystemLogMessage
  529. DEBUG_INFO_1("Set Buzzer fail.\n");
  530. #endif
  531. }
  532. }
  533. else
  534. {}
  535. }
  536. else
  537. {}
  538. break;
  539. }
  540. }
  541. //==========================================
  542. // Halt RFID card (EWT)
  543. //==========================================
  544. void sethaltCard(int Fd, int moduleType)
  545. {
  546. int len = 3;
  547. unsigned char txByte[len];
  548. unsigned char rxByte[254];
  549. unsigned char tmp[254];
  550. switch(moduleType)
  551. {
  552. case MODULE_EWT:
  553. default:
  554. //===============================
  555. // Command
  556. //===============================
  557. txByte[0] = 0x02;
  558. txByte[1] = RFID_CMD_HALT_14443A;
  559. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  560. if(Fd > 0)
  561. {
  562. memset(rxByte, 0, sizeof(rxByte));
  563. if(system_command(Fd, txByte, len, rxByte) > 0)
  564. {
  565. memset(tmp, 0, sizeof tmp);
  566. memcpy(tmp, rxByte, sizeof (rxByte));
  567. if(tmp[1] == 0x28)
  568. {
  569. #ifdef SystemLogMessage
  570. DEBUG_INFO_1("Halt card pass.\n");
  571. #endif
  572. }
  573. else if(tmp[1] == 0xD7)
  574. {
  575. #ifdef SystemLogMessage
  576. DEBUG_INFO_1("Halt card fail.\n");
  577. #endif
  578. }
  579. else
  580. {}
  581. }
  582. else
  583. {}
  584. }
  585. else
  586. {}
  587. break;
  588. }
  589. }