Module_RFID.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * Module_RFID.c
  3. *
  4. * Created on: 2019-10-24
  5. * Update: 2020-07-29
  6. * Author: Eason Yang
  7. * Version: D0.02
  8. */
  9. #include "Module_RFID.h"
  10. //==================================
  11. // PRINT OUT LOG FORMAT
  12. //==================================
  13. #define SystemLogMessage
  14. #define DEBUG_INFO_1(format, args...) StoreLogMessage("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  15. #define DEBUG_WARN_1(format, args...) StoreLogMessage("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  16. #define DEBUG_ERROR_1(format, args...) StoreLogMessage("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  17. //==================================
  18. // SYSTEM CONSTANT
  19. //==================================
  20. #define Debug
  21. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  22. #define PASS 1
  23. #define FAIL -1
  24. //==================================
  25. // RFID CMD CONSTANT
  26. //==================================
  27. unsigned int RFID_CMD_ISO1443A_REQUEST = 0x20;
  28. unsigned int RFID_CMD_ISO1443B_REQUEST = 0x60;
  29. unsigned int RFID_CMD_FELICA_POLLING_REQUEST = 0x2F;
  30. unsigned int RFID_CMD_HALT_14443A = 0x28;
  31. unsigned int RFID_CMD_BLOCK_READ = 0x21;
  32. unsigned int RFID_CMD_BLOCK_WRITE = 0x22;
  33. unsigned int RFID_CMD_BUZZER_SET = 0x14;
  34. unsigned int RFID_CMD_RF_OUTPUT_LEVEL_SET = 0x02;
  35. unsigned int RFID_CMD_DEFAULT_AUTO_DETECTING_CARD = 0x1D;
  36. //==================================
  37. // RFID MODE CONSTANT
  38. //==================================
  39. unsigned char WUPA = 0;
  40. unsigned char REQA = 1;
  41. unsigned char AFI = 0;
  42. //==================================
  43. // MIFARE CARD LENGTH
  44. //==================================
  45. unsigned int LENGTH_0 = 0;
  46. unsigned int LENGTH_4 = 4;
  47. unsigned int LENGTH_6 = 6;
  48. unsigned int LENGTH_7 = 7;
  49. unsigned int LENGTH_10 = 10;
  50. //==================================
  51. // RF output Level constant
  52. //==================================
  53. unsigned int RF_STRONGEST = 0x00;
  54. unsigned int RF_STRONGER = 0x01;
  55. unsigned int RF_WEAK = 0x02;
  56. unsigned int RF_WEAKEST = 0x03;
  57. //==================================
  58. // Default of Automatic Detecting constant
  59. //==================================
  60. unsigned int TURN_OFF = 0x00;
  61. unsigned int TURN_ON = 0X01;
  62. unsigned int TURN_TEMPORARILY = 0x11;
  63. //==================================
  64. // MIFARE SECTOR SPACE
  65. //==================================
  66. #define ROW 6
  67. #define COLUMN 16
  68. unsigned char sectorKeyA[COLUMN][ROW] = { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  69. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  70. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  71. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  72. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  73. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  74. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  75. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  76. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  77. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  78. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  79. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  80. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  81. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  82. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  83. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
  84. };
  85. unsigned char serialNumber[32];
  86. unsigned int cardLength;
  87. //==================================
  88. // SystemLog message
  89. //==================================
  90. #ifdef SystemLogMessage
  91. int StoreLogMessage(const char *fmt, ...)
  92. {
  93. char Buf[4096 + 256];
  94. char buffer[4096];
  95. time_t CurrentTime;
  96. struct tm *tm;
  97. va_list args;
  98. va_start(args, fmt);
  99. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  100. va_end(args);
  101. memset(Buf, 0, sizeof(Buf));
  102. CurrentTime = time(NULL);
  103. tm = localtime(&CurrentTime);
  104. sprintf(Buf,
  105. "echo -n \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]RFID_SystemLog",
  106. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
  107. tm->tm_min, tm->tm_sec, buffer, tm->tm_year + 1900, tm->tm_mon + 1);
  108. system(Buf);
  109. #ifdef Debug
  110. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year + 1900,
  111. tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
  112. buffer);
  113. #endif
  114. return rc;
  115. }
  116. #endif
  117. //==========================================
  118. // Module's command send/receive
  119. //==========================================
  120. int system_command(int uart, unsigned char* cmd,int length, unsigned char* rx)
  121. {
  122. int len;
  123. //sleep(2); //required to make flush work, for some reason
  124. tcflush(uart,TCIOFLUSH);
  125. if(write(uart, cmd, length) >= 0)
  126. {
  127. usleep(100000);
  128. len = read(uart, rx, 256);
  129. }
  130. else
  131. {
  132. #ifdef SystemLogMessage
  133. DEBUG_ERROR_1("system command %s response fail.\n", cmd);
  134. #endif
  135. }
  136. return len;
  137. }
  138. //==========================================
  139. // Calculation checksum function
  140. //==========================================
  141. char ClaCheckSum(unsigned char *buffer, int len)
  142. {
  143. int idx;
  144. char chksum = 0;
  145. for(idx = 0 ; idx < len-1 ; idx++)
  146. {
  147. chksum ^= *(buffer+idx);
  148. }
  149. return chksum;
  150. }
  151. bool getRequestCardSN(int Fd, int moduleType, RFID* rfid)
  152. {
  153. bool isSuccess = false;
  154. if(ISO14443A_REQUEST_SN(Fd,moduleType,serialNumber) == true)
  155. {
  156. rfid->cardType = ISO14443A;
  157. if(cardLength == LENGTH_4)
  158. {
  159. rfid->snType = RFID_SN_TYPE_4BYTE;
  160. memcpy(rfid->currentCard, &serialNumber, 32);
  161. }
  162. else if(cardLength == LENGTH_7)
  163. {
  164. rfid->snType = RFID_SN_TYPE_7BYTE;
  165. memcpy(rfid->currentCard, &serialNumber, 32);
  166. }
  167. isSuccess = true;
  168. }
  169. else if(ISO14443B_REQUEST_SN(Fd,moduleType,serialNumber) == true)
  170. {
  171. rfid->cardType = IS014443B;
  172. rfid->snType = RFID_SN_TYPE_4BYTE;
  173. memcpy(rfid->currentCard, &serialNumber, 32);
  174. sleep(2);
  175. isSuccess = true;
  176. }
  177. else if(FELICA_REQUEST_SN(Fd,moduleType,serialNumber) == true)
  178. {
  179. rfid->cardType = FELICA;
  180. rfid->snType = RFID_SN_TYPE_6BYTE;
  181. memcpy(rfid->currentCard, &serialNumber, 32);
  182. sleep(2);
  183. isSuccess = true;
  184. }
  185. else
  186. {}
  187. return isSuccess;
  188. }
  189. //==========================================
  190. // ISO14443A Request SN function
  191. //==========================================
  192. bool ISO14443A_REQUEST_SN(int Fd, int moduleType, unsigned char *data)
  193. {
  194. bool isSuccess = false;
  195. int tx_len = 4;
  196. unsigned char txByte[tx_len];
  197. unsigned char rxByte[254];
  198. unsigned char tmp[254];
  199. switch(moduleType)
  200. {
  201. case MODULE_EWT:
  202. default:
  203. //===============================
  204. // Command
  205. //===============================
  206. txByte[0] = tx_len-1;
  207. txByte[1] = RFID_CMD_ISO1443A_REQUEST;
  208. txByte[2] = REQA;
  209. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  210. if(Fd > 0)
  211. {
  212. memset(rxByte, 0, sizeof (rxByte));
  213. if(system_command(Fd, txByte, tx_len, rxByte) > 0)
  214. {
  215. memset(tmp, 0, sizeof tmp);
  216. memcpy(tmp, rxByte, sizeof(rxByte));
  217. if(tmp[1] == RFID_CMD_ISO1443A_REQUEST)
  218. {
  219. if(tmp[0] == 0x09)
  220. {
  221. #ifdef SystemLogMessage
  222. DEBUG_INFO_1("MIFARE CLASSIC.\n");
  223. #endif
  224. cardLength = LENGTH_4;
  225. memcpy(data, rxByte+2, cardLength);
  226. isSuccess = true;
  227. }
  228. else if(tmp[0] == 0x0C)
  229. {
  230. #ifdef SystemLogMessage
  231. DEBUG_INFO_1("MIFARE PLUS.\n");
  232. #endif
  233. cardLength = LENGTH_7;
  234. memcpy(data, rxByte+2, cardLength);
  235. isSuccess = true;
  236. }
  237. else
  238. {}
  239. }
  240. else if (tmp[1] == 0xDF)
  241. {
  242. cardLength = LENGTH_0;
  243. isSuccess = false;
  244. }
  245. }
  246. else
  247. {}
  248. }
  249. else
  250. {}
  251. break;
  252. }
  253. return isSuccess;
  254. }
  255. //==========================================
  256. // ISO14443B Request SN function
  257. //==========================================
  258. bool ISO14443B_REQUEST_SN(int Fd, int moduleType, unsigned char *data)
  259. {
  260. bool isSuccess = false;
  261. int cardLength;
  262. int tx_len = 5;
  263. unsigned char txByte[tx_len];
  264. unsigned char rxByte[254];
  265. unsigned char tmp[254];
  266. switch(moduleType)
  267. {
  268. case MODULE_EWT:
  269. default:
  270. //===============================
  271. // Command
  272. //===============================
  273. txByte[0] = tx_len-1;
  274. txByte[1] = RFID_CMD_ISO1443B_REQUEST;
  275. txByte[2] = WUPA;
  276. txByte[3] = AFI;
  277. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  278. if(Fd > 0)
  279. {
  280. memset(rxByte, 0, sizeof (rxByte));
  281. if(system_command(Fd, txByte, tx_len, rxByte) > 0)
  282. {
  283. memset(tmp, 0, sizeof tmp);
  284. memcpy(tmp, rxByte, sizeof(rxByte));
  285. if(tmp[1] == RFID_CMD_ISO1443B_REQUEST)
  286. {
  287. #ifdef SystemLogMessage
  288. DEBUG_INFO_1("ISO14443 TYPE B.\n");
  289. #endif
  290. cardLength = LENGTH_4;
  291. memcpy(data, rxByte+3, cardLength);
  292. isSuccess = true;
  293. }
  294. else if(tmp[1] == 0x9F)
  295. {
  296. cardLength = LENGTH_0;
  297. isSuccess = false;
  298. }
  299. }
  300. else
  301. {}
  302. }
  303. else
  304. {}
  305. break;
  306. }
  307. return isSuccess;
  308. }
  309. //==========================================
  310. // FELICA Request SN function
  311. //==========================================
  312. bool FELICA_REQUEST_SN(int Fd, int moduleType, unsigned char *data)
  313. {
  314. bool isSuccess = false;
  315. int cardLength;
  316. int tx_len = 9;
  317. unsigned char txByte[tx_len];
  318. unsigned char rxByte[254];
  319. unsigned char tmp[254];
  320. switch(moduleType)
  321. {
  322. case MODULE_EWT:
  323. default:
  324. //===============================
  325. // Command
  326. //===============================
  327. txByte[0] = tx_len-1;
  328. txByte[1] = RFID_CMD_FELICA_POLLING_REQUEST;
  329. txByte[2] = 0x06;
  330. txByte[3] = 0x00;
  331. txByte[4] = 0xFF;
  332. txByte[5] = 0xFF;
  333. txByte[6] = 0x01;
  334. txByte[7] = 0x00;
  335. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  336. if(Fd > 0)
  337. {
  338. memset(rxByte, 0, sizeof (rxByte));
  339. if(system_command(Fd, txByte, tx_len, rxByte) > 0)
  340. {
  341. memset(tmp, 0, sizeof tmp);
  342. memcpy(tmp, rxByte, sizeof(rxByte));
  343. if(tmp[1] == RFID_CMD_FELICA_POLLING_REQUEST)
  344. {
  345. #ifdef SystemLogMessage
  346. DEBUG_INFO_1("FELICA.\n");
  347. #endif
  348. cardLength = LENGTH_6;
  349. memcpy(data, rxByte+6, cardLength);
  350. isSuccess = true;
  351. }
  352. else if(tmp[1] == 0xD0)
  353. {
  354. cardLength = LENGTH_0;
  355. isSuccess = false;
  356. }
  357. }
  358. else
  359. {}
  360. }
  361. else
  362. {}
  363. break;
  364. }
  365. return isSuccess;
  366. }
  367. //==========================================
  368. // Read block data from RFID card
  369. //==========================================
  370. bool getBlockRead(int Fd, int moduleType, int block, unsigned char keyId, unsigned char *data)
  371. {
  372. bool isSuccess = false;
  373. int i;
  374. int j = 0;
  375. int len = 11;
  376. unsigned char txByte[len];
  377. unsigned char rxByte[254];
  378. unsigned char tmp[254];
  379. switch(moduleType)
  380. {
  381. case MODULE_EWT:
  382. default:
  383. //===============================
  384. // Command
  385. //===============================
  386. txByte[0] = 0x0A;
  387. txByte[1] = RFID_CMD_BLOCK_READ;
  388. txByte[2] = keyId;
  389. txByte[3] = block;
  390. for(i = 4; i < 10; i++)
  391. {
  392. if( j < sizeof (sectorKeyA[0]))
  393. {
  394. txByte[i] = sectorKeyA[(int)(block/4)][j];
  395. j++;
  396. }
  397. }
  398. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  399. if(Fd > 0)
  400. {
  401. memset(rxByte, 0, sizeof(rxByte));
  402. if(system_command(Fd, txByte, len, rxByte) > 0)
  403. {
  404. memset(tmp, 0, sizeof tmp);
  405. memcpy(tmp, rxByte, sizeof (rxByte));
  406. if((tmp[1] == RFID_CMD_BLOCK_READ))
  407. {
  408. #ifdef SystemLogMessage
  409. DEBUG_INFO_1("Read block ok.\n");
  410. #endif
  411. memcpy(data, rxByte+2, 16);
  412. isSuccess = true;
  413. }
  414. else if (tmp[1] == 0xDE)
  415. {
  416. #ifdef SystemLogMessage
  417. DEBUG_INFO_1("Read block failed.\n");
  418. #endif
  419. }
  420. else
  421. {}
  422. }
  423. else
  424. {}
  425. }
  426. else
  427. {}
  428. break;
  429. }
  430. return isSuccess;
  431. }
  432. //==========================================
  433. // Write data into RFID card block
  434. //==========================================
  435. bool setBlockWrite(int Fd, int moduleType, int block, unsigned char keyid, unsigned char *data)
  436. {
  437. bool isSuccess = false;
  438. int i;
  439. int j = 0;
  440. int len = 27;
  441. unsigned char txByte[len];
  442. unsigned char rxByte[254];
  443. unsigned char tmp[254];
  444. switch(moduleType)
  445. {
  446. case MODULE_EWT:
  447. default:
  448. //===============================
  449. // Command
  450. //===============================
  451. txByte[0] = 0x1A;
  452. txByte[1] = RFID_CMD_BLOCK_WRITE;
  453. txByte[2] = keyid;
  454. txByte[3] = block;
  455. for(i = 4; i < 10; i++)
  456. {
  457. if( j < sizeof (sectorKeyA[0]))
  458. {
  459. txByte[i] = sectorKeyA[(int)(block/4)][j];
  460. j++;
  461. }
  462. }
  463. memcpy(txByte+10, data, 16);
  464. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,(ARRAY_SIZE(txByte)));
  465. if(Fd > 0)
  466. {
  467. memset(rxByte, 0, sizeof(rxByte));
  468. if(system_command(Fd, txByte, len, rxByte) > 0)
  469. {
  470. memset(tmp, 0, sizeof tmp);
  471. memcpy(tmp, rxByte, sizeof (rxByte));
  472. if(tmp[1] == RFID_CMD_BLOCK_WRITE)
  473. {
  474. #ifdef SystemLogMessage
  475. DEBUG_INFO_1("Write block ok.\n");
  476. #endif
  477. isSuccess = true;
  478. }
  479. else if(tmp[1] == 0xDD)
  480. {
  481. #ifdef SystemLogMessage
  482. DEBUG_INFO_1("Write block failed.\n");
  483. #endif
  484. isSuccess = false;
  485. }
  486. }
  487. else
  488. {}
  489. }
  490. else
  491. {}
  492. break;
  493. }
  494. return isSuccess;
  495. }
  496. //==========================================
  497. // Buzzer set (EWT Module)
  498. //==========================================
  499. void setBuzzer(int Fd, int moduleType, unsigned char time)
  500. {
  501. int len = 4;
  502. unsigned char txByte[len];
  503. unsigned char rxByte[254];
  504. unsigned char tmp[254];
  505. switch(moduleType)
  506. {
  507. case MODULE_EWT:
  508. default:
  509. //===============================
  510. // Command
  511. //===============================
  512. txByte[0] = 0x03;
  513. txByte[1] = RFID_CMD_BUZZER_SET;
  514. if(time > 0x0A)
  515. {
  516. time = 0x05;
  517. txByte[2] = time;
  518. #ifdef SystemLogMessage
  519. DEBUG_WARN_1("Value is out of range.\n");
  520. #endif
  521. }
  522. else
  523. {
  524. txByte[2] = time;
  525. }
  526. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  527. if(Fd > 0)
  528. {
  529. memset(rxByte, 0, sizeof(rxByte));
  530. if(system_command(Fd, txByte, len, rxByte) > 0)
  531. {
  532. memset(tmp, 0, sizeof tmp);
  533. memcpy(tmp, rxByte, sizeof (rxByte));
  534. if(tmp[1] == 0x14)
  535. {
  536. #ifdef SystemLogMessage
  537. DEBUG_INFO_1("Set Buzzer ok.\n");
  538. DEBUG_INFO_1("Set Buzzer %d ms.\n",time);
  539. #endif
  540. }
  541. else if(tmp[1] == 0xEC)
  542. {
  543. #ifdef SystemLogMessage
  544. DEBUG_INFO_1("Set Buzzer fail.\n");
  545. #endif
  546. }
  547. }
  548. else
  549. {}
  550. }
  551. else
  552. {}
  553. break;
  554. }
  555. }
  556. //==========================================
  557. // Halt RFID card (EWT)
  558. //==========================================
  559. void sethaltCard(int Fd, int moduleType)
  560. {
  561. int len = 3;
  562. unsigned char txByte[len];
  563. unsigned char rxByte[254];
  564. unsigned char tmp[254];
  565. switch(moduleType)
  566. {
  567. case MODULE_EWT:
  568. default:
  569. //===============================
  570. // Command
  571. //===============================
  572. txByte[0] = 0x02;
  573. txByte[1] = RFID_CMD_HALT_14443A;
  574. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  575. if(Fd > 0)
  576. {
  577. memset(rxByte, 0, sizeof(rxByte));
  578. if(system_command(Fd, txByte, len, rxByte) > 0)
  579. {
  580. memset(tmp, 0, sizeof tmp);
  581. memcpy(tmp, rxByte, sizeof (rxByte));
  582. if(tmp[1] == 0x28)
  583. {
  584. #ifdef SystemLogMessage
  585. DEBUG_INFO_1("Halt card pass.\n");
  586. #endif
  587. }
  588. else if(tmp[1] == 0xD7)
  589. {
  590. #ifdef SystemLogMessage
  591. DEBUG_INFO_1("Halt card fail.\n");
  592. #endif
  593. }
  594. else
  595. {}
  596. }
  597. else
  598. {}
  599. }
  600. else
  601. {}
  602. break;
  603. }
  604. }
  605. //==========================================
  606. // Set the RF Output Level
  607. //==========================================
  608. bool setRfOutputLevel(int Fd, int moduleType, int Value)
  609. {
  610. bool isSuccess = false;
  611. int len = 4;
  612. unsigned char txByte[len];
  613. unsigned char rxByte[254];
  614. unsigned char tmp[254];
  615. switch(moduleType)
  616. {
  617. case MODULE_EWT:
  618. default:
  619. //===============================
  620. // Command
  621. //===============================
  622. txByte[0] = 0x03;
  623. txByte[1] = RFID_CMD_RF_OUTPUT_LEVEL_SET;
  624. txByte[2] = Value;
  625. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  626. if(Fd > 0)
  627. {
  628. memset(rxByte, 0, sizeof(rxByte));
  629. if(system_command(Fd, txByte, len, rxByte) > 0)
  630. {
  631. memset(tmp, 0, sizeof tmp);
  632. memcpy(tmp, rxByte, sizeof (rxByte));
  633. if(tmp[1] == RFID_CMD_RF_OUTPUT_LEVEL_SET)
  634. {
  635. #ifdef SystemLogMessage
  636. DEBUG_INFO_1("Set the RF Output Level : Pass...\r\n");
  637. #endif
  638. if(Value == RF_STRONGEST)
  639. {
  640. #ifdef SystemLogMessage
  641. DEBUG_INFO_1("Strongest...\r\n");
  642. #endif
  643. }
  644. else if(Value == RF_STRONGER)
  645. {
  646. #ifdef SystemLogMessage
  647. DEBUG_INFO_1("Strong...\r\n");
  648. #endif
  649. }
  650. else if(Value == RF_WEAK)
  651. {
  652. #ifdef SystemLogMessage
  653. DEBUG_INFO_1("Weak...\r\n");
  654. #endif
  655. }
  656. else if(Value == RF_WEAKEST)
  657. {
  658. #ifdef SystemLogMessage
  659. DEBUG_INFO_1("Weakest...\r\n");
  660. #endif
  661. }
  662. isSuccess = true;
  663. }
  664. else if(tmp[1] == 0xFD)
  665. {
  666. #ifdef SystemLogMessage
  667. DEBUG_INFO_1("Set the RF Output Level : Fail...\r\n");
  668. DEBUG_INFO_1("Unknow value !!! \r\n");
  669. DEBUG_INFO_1("Strongest : SET_RF_STRONGEST...\r\n");
  670. DEBUG_INFO_1("Strong : SET_RF_STRONGER...\r\n");
  671. DEBUG_INFO_1("Weak : SET_RF_WEAK...\r\n");
  672. DEBUG_INFO_1("Weakest : SET_RF_WEAKEST...\r\n");
  673. #endif
  674. isSuccess = false;
  675. }
  676. }
  677. else
  678. {}
  679. }
  680. else
  681. {}
  682. break;
  683. }
  684. return isSuccess;
  685. }
  686. //==========================================
  687. // Set the Default of Automatic Detecting Card
  688. //==========================================
  689. bool setDefaultOfAutomaticDetectingCard(int Fd, int moduleType, int Value)
  690. {
  691. bool isSuccess = false;
  692. int len = 4;
  693. unsigned char txByte[len];
  694. unsigned char rxByte[254];
  695. unsigned char tmp[254];
  696. switch(moduleType)
  697. {
  698. case MODULE_EWT:
  699. default:
  700. //===============================
  701. // Command
  702. //===============================
  703. txByte[0] = 0x03;
  704. txByte[1] = RFID_CMD_DEFAULT_AUTO_DETECTING_CARD;
  705. txByte[2] = Value;
  706. txByte[ARRAY_SIZE(txByte)-1] = ClaCheckSum(txByte,ARRAY_SIZE(txByte));
  707. if(Fd > 0)
  708. {
  709. memset(rxByte, 0, sizeof(rxByte));
  710. if(system_command(Fd, txByte, len, rxByte) > 0)
  711. {
  712. memset(tmp, 0, sizeof tmp);
  713. memcpy(tmp, rxByte, sizeof (rxByte));
  714. if(tmp[1] == RFID_CMD_DEFAULT_AUTO_DETECTING_CARD)
  715. {
  716. if(Value == TURN_OFF)
  717. {
  718. #ifdef SystemLogMessage
  719. DEBUG_INFO_1("Default of Automatic Detecting Card : OFF...\r\n");
  720. #endif
  721. }
  722. else if (Value == TURN_ON)
  723. {
  724. #ifdef SystemLogMessage
  725. DEBUG_INFO_1("Default of Automatic Detecting Card : ON...\r\n");
  726. #endif
  727. }
  728. else if (Value == TURN_TEMPORARILY)
  729. {
  730. #ifdef SystemLogMessage
  731. DEBUG_INFO_1("Default of Automatic Detecting Card : Temporarily open and close automatically detect card...\r\n");
  732. #endif
  733. }
  734. isSuccess = true;
  735. }
  736. else if(tmp[1] == 0xE2)
  737. {
  738. #ifdef SystemLogMessage
  739. DEBUG_INFO_1("Value : SET_DETECTING_OFF = OFF...\r\n");
  740. DEBUG_INFO_1("Value : SET_DETECTING_ON = ON...\r\n");
  741. DEBUG_INFO_1("Value : SET_DETECTING_TEMPORARILY = Temporarily open and close...\r\n");
  742. #endif
  743. isSuccess = false;
  744. }
  745. }
  746. else
  747. {}
  748. }
  749. else
  750. {}
  751. break;
  752. }
  753. return isSuccess;
  754. }