Module_RFID.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. isSuccess = true;
  152. }
  153. else if(ISO14443B_REQUEST_SN(Fd,moduleType,serialNumber) == true)
  154. {
  155. rfid->cardType = IS014443B;
  156. rfid->snType = RFID_SN_TYPE_4BYTE;
  157. memcpy(rfid->currentCard, &serialNumber, 32);
  158. sleep(2);
  159. isSuccess = true;
  160. }
  161. else if(FELICA_REQUEST_SN(Fd,moduleType,serialNumber) == true)
  162. {
  163. rfid->cardType = FELICA;
  164. rfid->snType = RFID_SN_TYPE_6BYTE;
  165. memcpy(rfid->currentCard, &serialNumber, 32);
  166. sleep(2);
  167. isSuccess = true;
  168. }
  169. else
  170. {}
  171. return isSuccess;
  172. }
  173. //==========================================
  174. // ISO14443A Request SN function
  175. //==========================================
  176. bool ISO14443A_REQUEST_SN(int Fd, int moduleType, unsigned char *data)
  177. {
  178. bool isSuccess = false;
  179. int tx_len = 4;
  180. unsigned char txByte[tx_len];
  181. unsigned char rxByte[254];
  182. unsigned char tmp[254];
  183. switch(moduleType)
  184. {
  185. case MODULE_EWT:
  186. default:
  187. //===============================
  188. // Command
  189. //===============================
  190. txByte[0] = tx_len-1;
  191. txByte[1] = RFID_CMD_ISO1443A_REQUEST;
  192. txByte[2] = REQA;
  193. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  194. if(Fd > 0)
  195. {
  196. memset(rxByte, 0, sizeof (rxByte));
  197. if(system_command(Fd, txByte, tx_len, rxByte) > 0)
  198. {
  199. memset(tmp, 0, sizeof tmp);
  200. memcpy(tmp, rxByte, sizeof(rxByte));
  201. if(tmp[1] == RFID_CMD_ISO1443A_REQUEST)
  202. {
  203. if(tmp[0] == 0x09)
  204. {
  205. #ifdef SystemLogMessage
  206. DEBUG_INFO_1("MIFARE CLASSIC.\n");
  207. #endif
  208. cardLength = LENGTH_4;
  209. memcpy(data, rxByte+2, cardLength);
  210. isSuccess = true;
  211. }
  212. else if(tmp[0] == 0x0C)
  213. {
  214. #ifdef SystemLogMessage
  215. DEBUG_INFO_1("MIFARE PLUS.\n");
  216. #endif
  217. cardLength = LENGTH_7;
  218. memcpy(data, rxByte+2, cardLength);
  219. isSuccess = true;
  220. }
  221. else
  222. {}
  223. }
  224. else if (tmp[1] == 0xDF)
  225. {
  226. cardLength = LENGTH_0;
  227. isSuccess = false;
  228. }
  229. }
  230. else
  231. {}
  232. }
  233. else
  234. {}
  235. break;
  236. }
  237. return isSuccess;
  238. }
  239. //==========================================
  240. // ISO14443B Request SN function
  241. //==========================================
  242. bool ISO14443B_REQUEST_SN(int Fd, int moduleType, unsigned char *data)
  243. {
  244. bool isSuccess = false;
  245. int cardLength;
  246. int tx_len = 5;
  247. unsigned char txByte[tx_len];
  248. unsigned char rxByte[254];
  249. unsigned char tmp[254];
  250. switch(moduleType)
  251. {
  252. case MODULE_EWT:
  253. default:
  254. //===============================
  255. // Command
  256. //===============================
  257. txByte[0] = tx_len-1;
  258. txByte[1] = RFID_CMD_ISO1443B_REQUEST;
  259. txByte[2] = WUPA;
  260. txByte[3] = AFI;
  261. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  262. if(Fd > 0)
  263. {
  264. memset(rxByte, 0, sizeof (rxByte));
  265. if(system_command(Fd, txByte, tx_len, rxByte) > 0)
  266. {
  267. memset(tmp, 0, sizeof tmp);
  268. memcpy(tmp, rxByte, sizeof(rxByte));
  269. if(tmp[1] == RFID_CMD_ISO1443B_REQUEST)
  270. {
  271. #ifdef SystemLogMessage
  272. DEBUG_INFO_1("ISO14443 TYPE B.\n");
  273. #endif
  274. cardLength = LENGTH_4;
  275. memcpy(data, rxByte+3, cardLength);
  276. isSuccess = true;
  277. }
  278. else if(tmp[1] == 0x9F)
  279. {
  280. cardLength = LENGTH_0;
  281. isSuccess = false;
  282. }
  283. }
  284. else
  285. {}
  286. }
  287. else
  288. {}
  289. break;
  290. }
  291. return isSuccess;
  292. }
  293. //==========================================
  294. // FELICA Request SN function
  295. //==========================================
  296. bool FELICA_REQUEST_SN(int Fd, int moduleType, unsigned char *data)
  297. {
  298. bool isSuccess = false;
  299. int cardLength;
  300. int tx_len = 9;
  301. unsigned char txByte[tx_len];
  302. unsigned char rxByte[254];
  303. unsigned char tmp[254];
  304. switch(moduleType)
  305. {
  306. case MODULE_EWT:
  307. default:
  308. //===============================
  309. // Command
  310. //===============================
  311. txByte[0] = tx_len-1;
  312. txByte[1] = RFID_CMD_FELICA_POLLING_REQUEST;
  313. txByte[2] = 0x06;
  314. txByte[3] = 0x00;
  315. txByte[4] = 0xFF;
  316. txByte[5] = 0xFF;
  317. txByte[6] = 0x01;
  318. txByte[7] = 0x00;
  319. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  320. if(Fd > 0)
  321. {
  322. memset(rxByte, 0, sizeof (rxByte));
  323. if(system_command(Fd, txByte, tx_len, rxByte) > 0)
  324. {
  325. memset(tmp, 0, sizeof tmp);
  326. memcpy(tmp, rxByte, sizeof(rxByte));
  327. if(tmp[1] == RFID_CMD_FELICA_POLLING_REQUEST)
  328. {
  329. #ifdef SystemLogMessage
  330. DEBUG_INFO_1("FELICA.\n");
  331. #endif
  332. cardLength = LENGTH_6;
  333. memcpy(data, rxByte+6, cardLength);
  334. isSuccess = true;
  335. }
  336. else if(tmp[1] == 0xD0)
  337. {
  338. cardLength = LENGTH_0;
  339. isSuccess = false;
  340. }
  341. }
  342. else
  343. {}
  344. }
  345. else
  346. {}
  347. break;
  348. }
  349. return isSuccess;
  350. }
  351. //==========================================
  352. // Read block data from RFID card
  353. //==========================================
  354. bool getBlockRead(int Fd, int moduleType, int block, unsigned char keyId, unsigned char *data)
  355. {
  356. bool isSuccess = false;
  357. int i;
  358. int j = 0;
  359. int len = 11;
  360. unsigned char txByte[len];
  361. unsigned char rxByte[254];
  362. unsigned char tmp[254];
  363. switch(moduleType)
  364. {
  365. case MODULE_EWT:
  366. default:
  367. //===============================
  368. // Command
  369. //===============================
  370. txByte[0] = 0x0A;
  371. txByte[1] = RFID_CMD_BLOCK_READ;
  372. txByte[2] = keyId;
  373. txByte[3] = block;
  374. for(i = 4; i < 10; i++)
  375. {
  376. if( j < sizeof (sectorKeyA[0]))
  377. {
  378. txByte[i] = sectorKeyA[(int)(block/4)][j];
  379. j++;
  380. }
  381. }
  382. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  383. if(Fd > 0)
  384. {
  385. memset(rxByte, 0, sizeof(rxByte));
  386. if(system_command(Fd, txByte, len, rxByte) > 0)
  387. {
  388. memset(tmp, 0, sizeof tmp);
  389. memcpy(tmp, rxByte, sizeof (rxByte));
  390. if((tmp[1] == RFID_CMD_BLOCK_READ))
  391. {
  392. #ifdef SystemLogMessage
  393. DEBUG_INFO_1("Read block ok.\n");
  394. #endif
  395. memcpy(data, rxByte+2, 16);
  396. isSuccess = true;
  397. }
  398. else if (tmp[1] == 0xDE)
  399. {
  400. #ifdef SystemLogMessage
  401. DEBUG_INFO_1("Read block failed.\n");
  402. #endif
  403. }
  404. else
  405. {}
  406. }
  407. else
  408. {}
  409. }
  410. else
  411. {}
  412. break;
  413. }
  414. return isSuccess;
  415. }
  416. //==========================================
  417. // Write data into RFID card block
  418. //==========================================
  419. bool setBlockWrite(int Fd, int moduleType, int block, unsigned char keyid, unsigned char *data)
  420. {
  421. bool isSuccess = false;
  422. int i;
  423. int j = 0;
  424. int len = 27;
  425. unsigned char txByte[len];
  426. unsigned char rxByte[254];
  427. unsigned char tmp[254];
  428. switch(moduleType)
  429. {
  430. case MODULE_EWT:
  431. default:
  432. //===============================
  433. // Command
  434. //===============================
  435. txByte[0] = 0x1A;
  436. txByte[1] = RFID_CMD_BLOCK_WRITE;
  437. txByte[2] = keyid;
  438. txByte[3] = block;
  439. for(i = 4; i < 10; i++)
  440. {
  441. if( j < sizeof (sectorKeyA[0]))
  442. {
  443. txByte[i] = sectorKeyA[(int)(block/4)][j];
  444. j++;
  445. }
  446. }
  447. memcpy(txByte+10, data, 16);
  448. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,(ARRAY_SIZE(txByte)));
  449. if(Fd > 0)
  450. {
  451. memset(rxByte, 0, sizeof(rxByte));
  452. if(system_command(Fd, txByte, len, rxByte) > 0)
  453. {
  454. memset(tmp, 0, sizeof tmp);
  455. memcpy(tmp, rxByte, sizeof (rxByte));
  456. if(tmp[1] == RFID_CMD_BLOCK_WRITE)
  457. {
  458. #ifdef SystemLogMessage
  459. DEBUG_INFO_1("Write block ok.\n");
  460. #endif
  461. isSuccess = true;
  462. }
  463. else if(tmp[1] == 0xDD)
  464. {
  465. #ifdef SystemLogMessage
  466. DEBUG_INFO_1("Write block failed.\n");
  467. #endif
  468. isSuccess = false;
  469. }
  470. }
  471. else
  472. {}
  473. }
  474. else
  475. {}
  476. break;
  477. }
  478. return isSuccess;
  479. }
  480. //==========================================
  481. // Buzzer set (EWT Module)
  482. //==========================================
  483. void setBuzzer(int Fd, int moduleType, unsigned char time)
  484. {
  485. int len = 4;
  486. unsigned char txByte[len];
  487. unsigned char rxByte[254];
  488. unsigned char tmp[254];
  489. switch(moduleType)
  490. {
  491. case MODULE_EWT:
  492. default:
  493. //===============================
  494. // Command
  495. //===============================
  496. txByte[0] = 0x03;
  497. txByte[1] = RFID_CMD_BUZZER_SET;
  498. if(time > 0x0A)
  499. {
  500. time = 0x05;
  501. txByte[2] = time;
  502. #ifdef SystemLogMessage
  503. DEBUG_WARN_1("Value is out of range.\n");
  504. #endif
  505. }
  506. else
  507. {
  508. txByte[2] = time;
  509. }
  510. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  511. if(Fd > 0)
  512. {
  513. memset(rxByte, 0, sizeof(rxByte));
  514. if(system_command(Fd, txByte, len, rxByte) > 0)
  515. {
  516. memset(tmp, 0, sizeof tmp);
  517. memcpy(tmp, rxByte, sizeof (rxByte));
  518. if(tmp[1] == 0x14)
  519. {
  520. #ifdef SystemLogMessage
  521. DEBUG_INFO_1("Set Buzzer ok.\n");
  522. DEBUG_INFO_1("Set Buzzer %d ms.\n",time);
  523. #endif
  524. }
  525. else if(tmp[1] == 0xEC)
  526. {
  527. #ifdef SystemLogMessage
  528. DEBUG_INFO_1("Set Buzzer fail.\n");
  529. #endif
  530. }
  531. }
  532. else
  533. {}
  534. }
  535. else
  536. {}
  537. break;
  538. }
  539. }
  540. //==========================================
  541. // Halt RFID card (EWT)
  542. //==========================================
  543. void sethaltCard(int Fd, int moduleType)
  544. {
  545. int len = 3;
  546. unsigned char txByte[len];
  547. unsigned char rxByte[254];
  548. unsigned char tmp[254];
  549. switch(moduleType)
  550. {
  551. case MODULE_EWT:
  552. default:
  553. //===============================
  554. // Command
  555. //===============================
  556. txByte[0] = 0x02;
  557. txByte[1] = RFID_CMD_HALT_14443A;
  558. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  559. if(Fd > 0)
  560. {
  561. memset(rxByte, 0, sizeof(rxByte));
  562. if(system_command(Fd, txByte, len, rxByte) > 0)
  563. {
  564. memset(tmp, 0, sizeof tmp);
  565. memcpy(tmp, rxByte, sizeof (rxByte));
  566. if(tmp[1] == 0x28)
  567. {
  568. #ifdef SystemLogMessage
  569. DEBUG_INFO_1("Halt card pass.\n");
  570. #endif
  571. }
  572. else if(tmp[1] == 0xD7)
  573. {
  574. #ifdef SystemLogMessage
  575. DEBUG_INFO_1("Halt card fail.\n");
  576. #endif
  577. }
  578. else
  579. {}
  580. }
  581. else
  582. {}
  583. }
  584. else
  585. {}
  586. break;
  587. }
  588. }