Module_RFID.c 18 KB

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