sysmgr.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. #include "sysmgr.h"
  2. #include <nrfx.h>
  3. #include <nrf_delay.h>
  4. #include <drv_rtc.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdarg.h>
  9. #include "nrf_spi.h"
  10. #include "nrf_drv_spi.h"
  11. #include "our_service.h"
  12. #include "aes.h"
  13. #define CBC 1
  14. #define ECB 1
  15. #define DATA_STORAGE_INSTANCE_ID 0x0022
  16. #define DATA_STORAGE_TYPE_ID 0x0021
  17. #define SENTINEL_VALUE 0xEE
  18. extern const nrf_drv_spi_t spi; /**< SPI instance. */
  19. extern volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
  20. uint8_t m_tx_buf[64]; /**< TX buffer. */
  21. uint8_t m_rx_buf[64]; /**< RX buffer. */
  22. uint8_t m_length; /**< Transfer length. */
  23. RegMacPool_t RegMacPool;
  24. const char *fds_err_str(ret_code_t ret)
  25. {
  26. /* Array to map FDS return values to strings. */
  27. static char const * err_str[] =
  28. {
  29. "FDS_ERR_OPERATION_TIMEOUT",
  30. "FDS_ERR_NOT_INITIALIZED",
  31. "FDS_ERR_UNALIGNED_ADDR",
  32. "FDS_ERR_INVALID_ARG",
  33. "FDS_ERR_NULL_ARG",
  34. "FDS_ERR_NO_OPEN_RECORDS",
  35. "FDS_ERR_NO_SPACE_IN_FLASH",
  36. "FDS_ERR_NO_SPACE_IN_QUEUES",
  37. "FDS_ERR_RECORD_TOO_LARGE",
  38. "FDS_ERR_NOT_FOUND",
  39. "FDS_ERR_NO_PAGES",
  40. "FDS_ERR_USER_LIMIT_REACHED",
  41. "FDS_ERR_CRC_CHECK_FAILED",
  42. "FDS_ERR_BUSY",
  43. "FDS_ERR_INTERNAL",
  44. };
  45. return err_str[ret - NRF_ERROR_FDS_ERR_BASE];
  46. }
  47. #if 0
  48. void ParkSysInit()
  49. {
  50. SystemTimer.TMR_SYS_OFF = 10;
  51. }
  52. void SC16IS750_FIFOEnable(unsigned char fifo_enable)
  53. {
  54. unsigned char temp_fcr;
  55. temp_fcr = SC16IS750_ReadRegister(SC16IS750_REG_FCR);
  56. if (fifo_enable == 0){
  57. temp_fcr &= 0xFE;
  58. } else {
  59. temp_fcr |= 0x01;
  60. }
  61. SC16IS750_WriteRegister(SC16IS750_REG_FCR,temp_fcr);
  62. return;
  63. }
  64. void SC16IS750_WriteRegister(unsigned char reg_addr, unsigned char val)
  65. {
  66. m_tx_buf[0] = reg_addr<<3;
  67. m_tx_buf[1] = val;
  68. //nrf_gpio_pin_write( SPI_SS_PIN,0);
  69. nrf_delay_us(10);
  70. nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 0);
  71. nrf_delay_us(10);
  72. //nrf_gpio_pin_write( SPI_SS_PIN,1);
  73. return ;
  74. }
  75. unsigned char SC16IS750_ReadRegister(unsigned char reg_addr)
  76. {
  77. unsigned char result;
  78. m_tx_buf[0] = 0x80|(reg_addr<<3);
  79. // nrf_gpio_pin_write( SPI_SS_PIN,0);
  80. nrf_delay_us(10);
  81. nrf_drv_spi_transfer(&spi, m_tx_buf, 1, m_rx_buf, 1);
  82. result = m_rx_buf[0];
  83. nrf_delay_us(10);
  84. // nrf_gpio_pin_write( SPI_SS_PIN,1);
  85. return result;
  86. }
  87. void SC16IS750_ResetDevice(void)
  88. {
  89. unsigned char reg;
  90. reg = SC16IS750_ReadRegister(SC16IS750_REG_IOCONTROL);
  91. reg |= 0x08;
  92. SC16IS750_WriteRegister(SC16IS750_REG_IOCONTROL, reg);
  93. return;
  94. }
  95. int16_t SC16IS750_SetBaudrate(uint32_t baudrate) //return error of baudrate parts per thousand
  96. {
  97. uint16_t divisor;
  98. uint8_t prescaler;
  99. uint32_t actual_baudrate;
  100. int16_t error;
  101. uint8_t temp_lcr;
  102. if ( (SC16IS750_ReadRegister(SC16IS750_REG_MCR)&0x80) == 0) { //if prescaler==1
  103. prescaler = 1;
  104. } else {
  105. prescaler = 4;
  106. }
  107. prescaler = 1;
  108. divisor = (SC16IS750_CRYSTCAL_FREQ/prescaler)/(baudrate*16);
  109. temp_lcr = SC16IS750_ReadRegister(SC16IS750_REG_LCR);
  110. temp_lcr |= 0x80;
  111. SC16IS750_WriteRegister(SC16IS750_REG_LCR,temp_lcr);
  112. //write to DLL
  113. SC16IS750_WriteRegister(SC16IS750_REG_DLL,(uint8_t)divisor);
  114. //write to DLH
  115. SC16IS750_WriteRegister(SC16IS750_REG_DLH,(uint8_t)(divisor>>8));
  116. temp_lcr &= 0x7F;
  117. SC16IS750_WriteRegister(SC16IS750_REG_LCR,temp_lcr);
  118. actual_baudrate = (SC16IS750_CRYSTCAL_FREQ/prescaler)/(16*divisor);
  119. error = ((float)actual_baudrate-baudrate)*1000/baudrate;
  120. return error;
  121. }
  122. void SC16IS750_SetLine(uint8_t data_length, uint8_t parity_select, uint8_t stop_length )
  123. {
  124. uint8_t temp_lcr;
  125. temp_lcr = SC16IS750_ReadRegister(SC16IS750_REG_LCR);
  126. temp_lcr &= 0xC0; //Clear the lower six bit of LCR (LCR[0] to LCR[5]
  127. switch (data_length) { //data length settings
  128. case 5:
  129. break;
  130. case 6:
  131. temp_lcr |= 0x01;
  132. break;
  133. case 7:
  134. temp_lcr |= 0x02;
  135. break;
  136. case 8:
  137. temp_lcr |= 0x03;
  138. break;
  139. default:
  140. temp_lcr |= 0x03;
  141. break;
  142. }
  143. if ( stop_length == 2 ) {
  144. temp_lcr |= 0x04;
  145. }
  146. switch (parity_select) { //parity selection length settings
  147. case 0: //no parity
  148. break;
  149. case 1: //odd parity
  150. temp_lcr |= 0x08;
  151. break;
  152. case 2: //even parity
  153. temp_lcr |= 0x18;
  154. break;
  155. case 3: //force '1' parity
  156. temp_lcr |= 0x03;
  157. break;
  158. case 4: //force '0' parity
  159. break;
  160. default:
  161. break;
  162. }
  163. SC16IS750_WriteRegister(SC16IS750_REG_LCR,temp_lcr);
  164. }
  165. void SC16IS750_WriteByte(uint8_t val)
  166. {
  167. uint8_t tmp_lsr;
  168. do {
  169. tmp_lsr = SC16IS750_ReadRegister(SC16IS750_REG_LSR);
  170. } while ((tmp_lsr&0x20) ==0);
  171. //nrf_delay_ms(1);
  172. SC16IS750_WriteRegister(SC16IS750_REG_THR,val);
  173. }
  174. int DBGPrint(const char *fmt, ...)
  175. {
  176. char buff[128];
  177. va_list args;
  178. int n;
  179. int i;
  180. va_start(args, fmt);
  181. n = vsnprintf(buff, 120, fmt, args);
  182. va_end(args);
  183. //HAL_UART_Transmit(CLIUart, (uint8_t*)buff, n, 500);
  184. for( i=0; i<n; i++)
  185. {
  186. SC16IS750_WriteByte(buff[i]);
  187. }
  188. return n;
  189. }
  190. #endif
  191. /* Flash related functions */
  192. /* Dummy configuration data. */
  193. static configuration_t m_dummy_cfg =
  194. {
  195. .config1_on = false,
  196. .config2_on = true,
  197. .boot_count = 0x0,
  198. .device_name = "dummy",
  199. };
  200. static void record_write(uint32_t fid,
  201. uint32_t key,
  202. void const * p_data,
  203. uint32_t len)
  204. {
  205. fds_record_t const rec =
  206. {
  207. .file_id = fid,
  208. .key = key,
  209. .data.p_data = p_data,
  210. .data.length_words = (len + 3) / sizeof(uint32_t)
  211. };
  212. NRF_LOG_INFO(
  213. "writing record to flash...\n"
  214. "file: 0x%x, key: 0x%x, \"%s\", len: %u bytes\n",
  215. fid, key, p_data, len);
  216. ret_code_t rc = fds_record_write(NULL, &rec);
  217. if (rc != NRF_SUCCESS)
  218. {
  219. NRF_LOG_INFO(
  220. "error: fds_record_write() returned %s.\n",
  221. fds_err_str(rc));
  222. }
  223. }
  224. static void record_update( configuration_t const * p_cfg)
  225. {
  226. fds_record_desc_t desc = {0};
  227. fds_find_token_t ftok = {0};
  228. if (fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &ftok) == NRF_SUCCESS)
  229. {
  230. fds_record_t const rec =
  231. {
  232. .file_id = CONFIG_FILE,
  233. .key = CONFIG_REC_KEY,
  234. .data.p_data = p_cfg,
  235. .data.length_words = (sizeof(configuration_t) + 3) / sizeof(uint32_t)
  236. };
  237. ret_code_t rc = fds_record_update(&desc, &rec);
  238. if (rc != NRF_SUCCESS)
  239. {
  240. NRF_LOG_INFO( "error: fds_record_update() returned %s.\n",
  241. fds_err_str(rc));
  242. }
  243. }
  244. else
  245. {
  246. NRF_LOG_INFO( "error: could not find config file.\n");
  247. }
  248. }
  249. static void record_delete( uint32_t fid, uint32_t key)
  250. {
  251. fds_find_token_t tok = {0};
  252. fds_record_desc_t desc = {0};
  253. NRF_LOG_INFO(
  254. "deleting record...\n"
  255. "file: 0x%x, key: 0x%x\n",
  256. fid,
  257. key);
  258. if (fds_record_find(fid, key, &desc, &tok) == NRF_SUCCESS)
  259. {
  260. ret_code_t rc = fds_record_delete(&desc);
  261. if (rc != NRF_SUCCESS)
  262. {
  263. NRF_LOG_INFO(
  264. "error: fds_record_delete() returned %s.\n", fds_err_str(rc));
  265. return;
  266. }
  267. NRF_LOG_INFO( "record id: 0x%x\n", desc.record_id);
  268. }
  269. else
  270. {
  271. NRF_LOG_INFO( "error: record not found!\n");
  272. }
  273. }
  274. bool record_delete_next(void)
  275. {
  276. fds_find_token_t tok = {0};
  277. fds_record_desc_t desc = {0};
  278. if (fds_record_iterate(&desc, &tok) == NRF_SUCCESS)
  279. {
  280. ret_code_t rc = fds_record_delete(&desc);
  281. if (rc != NRF_SUCCESS)
  282. {
  283. return false;
  284. }
  285. return true;
  286. }
  287. else
  288. {
  289. /* No records left to delete. */
  290. return false;
  291. }
  292. }
  293. static void print_cfg_cmd( size_t argc, char ** argv)
  294. {
  295. fds_record_desc_t desc = {0};
  296. fds_find_token_t tok = {0};
  297. while (fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok) == NRF_SUCCESS)
  298. {
  299. ret_code_t rc;
  300. fds_flash_record_t frec = {0};
  301. rc = fds_record_open(&desc, &frec);
  302. switch (rc)
  303. {
  304. case NRF_SUCCESS:
  305. break;
  306. case FDS_ERR_CRC_CHECK_FAILED:
  307. NRF_LOG_INFO( "error: CRC check failed!\n");
  308. continue;
  309. case FDS_ERR_NOT_FOUND:
  310. NRF_LOG_INFO( "error: record not found!\n");
  311. continue;
  312. default:
  313. {
  314. NRF_LOG_INFO(
  315. "error: unexpecte error %s.\n",
  316. fds_err_str(rc));
  317. continue;
  318. }
  319. }
  320. configuration_t * p_cfg = (configuration_t *)(frec.p_data);
  321. NRF_LOG_INFO(
  322. "config1:\t%s\n"
  323. "config2:\t%s\n"
  324. "boot count:\t%u\n"
  325. "device name:\t%s\n",
  326. p_cfg->config1_on ? "on" : "off",
  327. p_cfg->config2_on ? "on" : "off",
  328. p_cfg->boot_count,
  329. p_cfg->device_name);
  330. rc = fds_record_close(&desc);
  331. APP_ERROR_CHECK(rc);
  332. }
  333. }
  334. static void print_all_cmd( )
  335. {
  336. fds_find_token_t tok = {0};
  337. fds_record_desc_t desc = {0};
  338. uint8_t *data;
  339. NRF_LOG_INFO(
  340. "rec. id\t"
  341. "\tfile id\t"
  342. "\trec. key"
  343. "\tlength\n");
  344. while (fds_record_iterate(&desc, &tok) != FDS_ERR_NOT_FOUND)
  345. {
  346. ret_code_t rc;
  347. fds_flash_record_t frec = {0};
  348. rc = fds_record_open(&desc, &frec);
  349. switch (rc)
  350. {
  351. case NRF_SUCCESS:
  352. break;
  353. case FDS_ERR_CRC_CHECK_FAILED:
  354. NRF_LOG_INFO( "error: CRC check failed!\n");
  355. continue;
  356. case FDS_ERR_NOT_FOUND:
  357. NRF_LOG_INFO( "error: record not found!\n");
  358. continue;
  359. default:
  360. {
  361. NRF_LOG_INFO(
  362. "error: unexpecte error %s.\n",
  363. fds_err_str(rc));
  364. continue;
  365. }
  366. }
  367. uint32_t const len = frec.p_header->length_words * sizeof(uint32_t);
  368. NRF_LOG_INFO(
  369. " 0x%04x\t"
  370. "\t 0x%04x\t"
  371. "\t 0x%04x\t"
  372. "\t %4u bytes\t",
  373. frec.p_header->record_id,
  374. frec.p_header->file_id,
  375. frec.p_header->record_key,
  376. len);
  377. data = (uint8_t *) frec.p_data;
  378. for (uint8_t i=0;i<len;i++)
  379. {
  380. NRF_LOG_RAW_INFO( "%c",data[i]);
  381. }
  382. NRF_LOG_INFO("\n");
  383. rc = fds_record_close(&desc);
  384. APP_ERROR_CHECK(rc);
  385. }
  386. }
  387. void RegMac(char* str)
  388. {
  389. print_all_cmd();
  390. NRF_LOG_RAW_INFO("Registration MAC : %s\n", str);
  391. RegMacPool.Count++;
  392. record_write(1, RegMacPool.Count, str, 12);
  393. print_all_cmd();
  394. }
  395. void RegMacDelteAll()
  396. {
  397. bool next;
  398. while(1)
  399. {
  400. next = record_delete_next();
  401. if (!next)
  402. {
  403. NRF_LOG_INFO("No records left to delete.");
  404. break;
  405. }
  406. }
  407. memset( &RegMacPool, 0, sizeof(RegMacPool));
  408. }
  409. void LoadRegMac()
  410. {
  411. fds_find_token_t tok = {0};
  412. fds_record_desc_t desc = {0};
  413. uint8_t *data;
  414. char tmpBuf[15];
  415. NRF_LOG_INFO(
  416. "\nrec. id\t"
  417. "\tfile id\t"
  418. "\trec. key"
  419. "\tlength\tmac\n");
  420. while (fds_record_iterate(&desc, &tok) != FDS_ERR_NOT_FOUND)
  421. {
  422. ret_code_t rc;
  423. fds_flash_record_t frec = {0};
  424. rc = fds_record_open(&desc, &frec);
  425. switch (rc)
  426. {
  427. case NRF_SUCCESS:
  428. break;
  429. case FDS_ERR_CRC_CHECK_FAILED:
  430. NRF_LOG_INFO("error: CRC check failed!\n");
  431. continue;
  432. case FDS_ERR_NOT_FOUND:
  433. NRF_LOG_INFO("error: record not found!\n");
  434. continue;
  435. default:
  436. {
  437. NRF_LOG_INFO("error: unexpecte error %s.\n", fds_err_str(rc));
  438. continue;
  439. }
  440. }
  441. uint32_t const len = frec.p_header->length_words * sizeof(uint32_t);
  442. NRF_LOG_RAW_INFO(
  443. " 0x%04x\t"
  444. "\t 0x%04x\t"
  445. "\t 0x%04x\t"
  446. "\t %4u bytes\t",
  447. frec.p_header->record_id,
  448. frec.p_header->file_id,
  449. frec.p_header->record_key,
  450. len);
  451. data = (uint8_t *) frec.p_data;
  452. if( RegMacPool.Count >= 5 )
  453. {
  454. NRF_LOG_RAW_INFO( "Mac Pool Over\n");
  455. return;
  456. }
  457. RegMacPool.Mac[RegMacPool.Count][12] = 0;
  458. strncpy( RegMacPool.Mac[RegMacPool.Count++], data, 12);
  459. //for (uint8_t i=0;i<len;i++)
  460. {
  461. // NRF_LOG_RAW_INFO( "%c",data[i]);
  462. }
  463. //NRF_LOG_RAW_INFO( "\n");
  464. NRF_LOG_RAW_INFO( "%s\n",RegMacPool.Mac[RegMacPool.Count-1]);
  465. rc = fds_record_close(&desc);
  466. APP_ERROR_CHECK(rc);
  467. }
  468. }
  469. void SaveRegMac(char* mac)
  470. {
  471. int i;
  472. if( RegMacPool.Count>= 5 )
  473. return;
  474. for( i=0; i<RegMacPool.Count; i++)
  475. {
  476. if( strncmp( RegMacPool.Mac[i], mac, 12) == 0 )
  477. {
  478. NRF_LOG_RAW_INFO( "Allready Registrated\n");
  479. return;
  480. }
  481. }
  482. RegMacPool.Mac[RegMacPool.Count][12] = 0;
  483. strncpy( RegMacPool.Mac[RegMacPool.Count], mac, 12);
  484. record_write(1, RegMacPool.Count+1, mac, 12);
  485. RegMacPool.Count++;
  486. }
  487. int FindRegMac(char* mac)
  488. {
  489. int i;
  490. for( i=0; i<RegMacPool.Count; i++)
  491. {
  492. if( strncmp( RegMacPool.Mac[i], mac, 12) == 0 )
  493. {
  494. NRF_LOG_RAW_INFO( "Valid Mac\n");
  495. return 1;
  496. }
  497. }
  498. return 0;
  499. }
  500. void DisplayRegMac()
  501. {
  502. print_all_cmd( );
  503. }
  504. void FlashTest()
  505. {
  506. static uint8_t m_data[256];
  507. int len;
  508. sprintf(m_data, "hello-%d", 1);
  509. len = strlen(m_data);
  510. // record_write( 1, 1, m_data, len);
  511. sprintf(m_data, "hello-%d", 2);
  512. //record_write( 1, 2, m_data, len);
  513. print_all_cmd();
  514. }
  515. void SetMacAddress(uint8_t* addr)
  516. {
  517. sd_ble_gap_addr_get(&old_ble_addr);
  518. ble_gap_addr_t dd;
  519. dd.addr_id_peer = 0;
  520. dd.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
  521. dd.addr[0] = addr[0];
  522. dd.addr[1] = addr[1];
  523. //dd.addr[2] = addr[2];
  524. dd.addr[2] = 0x57;
  525. dd.addr[3] = addr[3];
  526. dd.addr[4] = addr[4];
  527. dd.addr[5] = addr[5];
  528. sd_ble_gap_addr_set(&dd);
  529. sd_ble_gap_addr_get(&new_ble_addr);
  530. }
  531. // prints string as hex
  532. static void phex(uint8_t* str)
  533. {
  534. unsigned char i;
  535. for(i = 0; i < 16; ++i)
  536. NRF_LOG_RAW_INFO("%.2x", str[i]);
  537. NRF_LOG_RAW_INFO("\n");
  538. }
  539. static void test_encrypt_ecb_verbose(void)
  540. {
  541. // Example of more verbose verification
  542. uint8_t i, buf[64], buf2[64];
  543. // 128bit key
  544. uint8_t key[16] = { (uint8_t) 0x2b, (uint8_t) 0x7e, (uint8_t) 0x15, (uint8_t) 0x16, (uint8_t) 0x28, (uint8_t) 0xae, (uint8_t) 0xd2, (uint8_t) 0xa6, (uint8_t) 0xab, (uint8_t) 0xf7, (uint8_t) 0x15, (uint8_t) 0x88, (uint8_t) 0x09, (uint8_t) 0xcf, (uint8_t) 0x4f, (uint8_t) 0x3c };
  545. // 512bit text
  546. uint8_t plain_text[64] = { (uint8_t) 0x6b, (uint8_t) 0xc1, (uint8_t) 0xbe, (uint8_t) 0xe2, (uint8_t) 0x2e, (uint8_t) 0x40, (uint8_t) 0x9f, (uint8_t) 0x96, (uint8_t) 0xe9, (uint8_t) 0x3d, (uint8_t) 0x7e, (uint8_t) 0x11, (uint8_t) 0x73, (uint8_t) 0x93, (uint8_t) 0x17, (uint8_t) 0x2a,
  547. (uint8_t) 0xae, (uint8_t) 0x2d, (uint8_t) 0x8a, (uint8_t) 0x57, (uint8_t) 0x1e, (uint8_t) 0x03, (uint8_t) 0xac, (uint8_t) 0x9c, (uint8_t) 0x9e, (uint8_t) 0xb7, (uint8_t) 0x6f, (uint8_t) 0xac, (uint8_t) 0x45, (uint8_t) 0xaf, (uint8_t) 0x8e, (uint8_t) 0x51,
  548. (uint8_t) 0x30, (uint8_t) 0xc8, (uint8_t) 0x1c, (uint8_t) 0x46, (uint8_t) 0xa3, (uint8_t) 0x5c, (uint8_t) 0xe4, (uint8_t) 0x11, (uint8_t) 0xe5, (uint8_t) 0xfb, (uint8_t) 0xc1, (uint8_t) 0x19, (uint8_t) 0x1a, (uint8_t) 0x0a, (uint8_t) 0x52, (uint8_t) 0xef,
  549. (uint8_t) 0xf6, (uint8_t) 0x9f, (uint8_t) 0x24, (uint8_t) 0x45, (uint8_t) 0xdf, (uint8_t) 0x4f, (uint8_t) 0x9b, (uint8_t) 0x17, (uint8_t) 0xad, (uint8_t) 0x2b, (uint8_t) 0x41, (uint8_t) 0x7b, (uint8_t) 0xe6, (uint8_t) 0x6c, (uint8_t) 0x37, (uint8_t) 0x10 };
  550. memset(buf, 0, 64);
  551. memset(buf2, 0, 64);
  552. // print text to encrypt, key and IV
  553. NRF_LOG_RAW_INFO("ECB encrypt verbose:\n\n");
  554. NRF_LOG_RAW_INFO("plain text:\n");
  555. for(i = (uint8_t) 0; i < (uint8_t) 4; ++i)
  556. {
  557. phex(plain_text + i * (uint8_t) 16);
  558. }
  559. NRF_LOG_RAW_INFO("\n");
  560. NRF_LOG_RAW_INFO("key:\n");
  561. phex(key);
  562. NRF_LOG_RAW_INFO("\n");
  563. // print the resulting cipher as 4 x 16 byte strings
  564. NRF_LOG_RAW_INFO("ciphertext:\n");
  565. for(i = 0; i < 4; ++i)
  566. {
  567. AES128_ECB_encrypt(plain_text + (i*16), key, buf+(i*16));
  568. phex(buf + (i*16));
  569. }
  570. NRF_LOG_RAW_INFO("\n");
  571. }
  572. static void test_encrypt_ecb(void)
  573. {
  574. uint8_t key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
  575. uint8_t in[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
  576. uint8_t out[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
  577. uint8_t buffer[16];
  578. AES128_ECB_encrypt(in, key, buffer);
  579. NRF_LOG_RAW_INFO("ECB encrypt: ");
  580. if(0 == strncmp((char*) out, (char*) buffer, 16))
  581. {
  582. NRF_LOG_RAW_INFO("SUCCESS!\n");
  583. }
  584. else
  585. {
  586. NRF_LOG_RAW_INFO("FAILURE!\n");
  587. }
  588. }
  589. static void test_decrypt_ecb(void)
  590. {
  591. uint8_t key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
  592. uint8_t in[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
  593. uint8_t out[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
  594. uint8_t buffer[16];
  595. AES128_ECB_decrypt(in, key, buffer);
  596. NRF_LOG_RAW_INFO("ECB decrypt: ");
  597. if(0 == strncmp((char*) out, (char*) buffer, 16))
  598. {
  599. NRF_LOG_RAW_INFO("SUCCESS!\n");
  600. }
  601. else
  602. {
  603. NRF_LOG_RAW_INFO("FAILURE!\n");
  604. }
  605. }
  606. uint8_t AesBuffer[16];
  607. void Wes_Encryp_Ecb()
  608. {
  609. uint8_t key[16] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  610. uint8_t in[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  611. uint16_t tempVal;
  612. in[0] = 'W';
  613. in[1] = 'E';
  614. in[2] = 'S';
  615. tempVal = rand();
  616. in[3] = (tempVal>>8)&0xff;
  617. in[4] = tempVal&0xff;
  618. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  619. NRF_LOG_INFO("==> Wes_Encryp_Ecb : 0x%04x\n", tempVal);
  620. }
  621. void Wes_Encrypt_EcbACK()
  622. {
  623. uint8_t key[16] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  624. uint8_t in[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  625. NRF_LOG_RAW_INFO("Wes_EncryptEcbACK ACK\n");
  626. in[0] = 'W';
  627. in[1] = 'E';
  628. in[2] = 'S';
  629. in[3] = 'A';
  630. in[4] = 'C';
  631. in[5] = 'K';
  632. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  633. }
  634. void Wes_Decrypt_Ecb(char* in, char* out)
  635. {
  636. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  637. AES128_ECB_decrypt(in, key, out);
  638. NRF_LOG_RAW_INFO("Wes_Decrypt_Ecb\n");
  639. }
  640. void CarEncryptEcb(int mode)
  641. {
  642. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  643. uint8_t in[] = {'C', 'A', 'R', 1,2,3,4,5,6,7,8,9,10,11,12,13};
  644. uint16_t tempVal;
  645. if(mode == AES_ENC_WES)
  646. {
  647. in[0] = 'W';
  648. in[1] = 'E';
  649. in[2] = 'S';
  650. }
  651. else if( mode == AES_ENC_CAR )
  652. {
  653. in[0] = 'C';
  654. in[1] = 'A';
  655. in[2] = 'R';
  656. }else if( mode == AES_ENC_EMG )
  657. {
  658. in[0] = 'E';
  659. in[1] = 'M';
  660. in[2] = 'G';
  661. }
  662. tempVal = rand();
  663. in[3] = tempVal&0xff;
  664. in[4] = (tempVal>>8)&0xff;
  665. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  666. NRF_LOG_RAW_INFO("ECB encrypt: %x\n", tempVal);
  667. }
  668. // 0x73:parking 0x72:emg
  669. void CarEncryptEcbACK(char type)
  670. {
  671. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  672. uint8_t in[] = {'C', 'A', 'R', 'A','C','K',1,2,3,4,5,6,7,8,9,0x0A };
  673. switch(type){
  674. // BAND EMG
  675. case 0x72:
  676. in[0] = 'E';
  677. in[1] = 'M';
  678. in[2] = 'G';
  679. break;
  680. // NEW iOS APP
  681. case 0x75:
  682. in[0] = 'P';
  683. in[1] = 'C';
  684. in[2] = 'A';
  685. break;
  686. }
  687. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  688. NRF_LOG_RAW_INFO("ECB ACK\n");
  689. }
  690. void CarDecryptEcb(char* in, char* out)
  691. {
  692. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  693. // uint8_t out[] = {'C', 'A', 'R', 1,2,3,4,5,6,7,8,9,10,11,12,13 };
  694. // uint8_t buffer[16];
  695. AES128_ECB_decrypt(in, key, out);
  696. // NRF_LOG_RAW_INFO("ECB decrypt: ");
  697. }
  698. void PcaEncryptEcb(int mode)
  699. {
  700. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  701. uint8_t in[] = {'C', 'A', 'R', 1,2,3,4,5,6,7,8,9,10,11,12,13 };
  702. uint16_t tempVal;
  703. if( mode == AES_ENC_CAR )
  704. {
  705. in[0] = 'C';
  706. in[1] = 'A';
  707. in[2] = 'R';
  708. }else if( mode == AES_ENC_EMG )
  709. {
  710. in[0] = 'E';
  711. in[1] = 'M';
  712. in[2] = 'G';
  713. }
  714. tempVal = rand();
  715. in[3] = tempVal&0xff;
  716. in[4] = (tempVal>>8)&0xff;
  717. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  718. NRF_LOG_RAW_INFO("ECB encrypt: %x\n", tempVal);
  719. }
  720. // 0x73:parking 0x72:emg
  721. void PcaEncryptEcbACK(char type)
  722. {
  723. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  724. uint8_t in[] = {'C', 'A', 'R', 'A','C','K',1,2,3,4,5,6,7,8,9,0x0A };
  725. switch(type){
  726. // BAND EMG
  727. case 0x72:
  728. in[0] = 'E';
  729. in[1] = 'M';
  730. in[2] = 'G';
  731. break;
  732. // NEW iOS APP
  733. case 0x75:
  734. in[0] = 'P';
  735. in[1] = 'C';
  736. in[2] = 'A';
  737. break;
  738. }
  739. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  740. NRF_LOG_RAW_INFO("ECB ACK\n");
  741. }
  742. void PcaDecryptEcb(char* in, char* out)
  743. {
  744. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  745. AES128_ECB_decrypt(in, key, out);
  746. }
  747. void AppDecryptCbc(char* in, char* out)
  748. {
  749. uint8_t key[] = {'1', '9', '-', 'H', 'D', 'C', '-', 'I', 'C', 'O', 'N', 'T', 'R', 'O', 'L', 'S'}; // App
  750. uint8_t iv[] = {'H', 'D', 'C', '-', 'I', 'V', '-', 'R', 'N', 'D', 'C', 'E', 'N', 'T', 'E', 'R' };
  751. //uint8_t key[] = "19-HDC-ICONTROLS";
  752. //uint8_t iv[] = "HDC-IV-RNDCENTER";
  753. AES128_CBC_decrypt_buffer(out+0, in+0, 16, key, iv);
  754. //AES128_CBC_decrypt_buffer(out+16, in+16, 8, 0, 0);
  755. }
  756. void AppEnryptCbc(char* in, char* out)
  757. {
  758. uint8_t key[] = {'1', '9', '-', 'H', 'D', 'C', '-', 'T', 'C', 'O', 'N', 'T', 'R', 'O', 'L', 'S'}; // APP
  759. uint8_t iv[] = {'H', 'D', 'C', '-', 'T', 'V', '-', 'R', 'N', 'D', 'C', 'E', 'N', 'T', 'E', 'R' };
  760. // uint8_t in[] = { 0x31, 0x01, 0x02, 0xEE, 0x16, 0x00, 0x00, 0xA5, 0xE4, 0x8B, 0x01, 0x16, 0x6C, 0x38, 0xFA, 0x95, 0xDB, 0x5E, 0xFA, 0xF3, 0x82, 0x09, 0x5C, 0x7B };
  761. // uint8_t buffer[30];
  762. AES128_CBC_encrypt_buffer(out, in, 24, key, iv);
  763. NRF_LOG_RAW_INFO("CBC encrypt: ");
  764. }
  765. #if 0
  766. static void test_decrypt_cbc(void)
  767. {
  768. // Example "simulating" a smaller buffer...
  769. uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
  770. uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
  771. uint8_t in[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  772. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
  773. 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
  774. 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
  775. uint8_t out[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  776. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  777. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  778. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
  779. uint8_t buffer[64];
  780. AES128_CBC_decrypt_buffer(buffer+0, in+0, 16, key, iv);
  781. AES128_CBC_decrypt_buffer(buffer+16, in+16, 16, 0, 0);
  782. AES128_CBC_decrypt_buffer(buffer+32, in+32, 16, 0, 0);
  783. AES128_CBC_decrypt_buffer(buffer+48, in+48, 16, 0, 0);
  784. NRF_LOG_RAW_INFO("CBC decrypt: ");
  785. if(0 == strncmp((char*) out, (char*) buffer, 64))
  786. {
  787. NRF_LOG_RAW_INFO("SUCCESS!\n");
  788. }
  789. else
  790. {
  791. NRF_LOG_RAW_INFO("FAILURE!\n");
  792. }
  793. }
  794. static void test_encrypt_cbc(void)
  795. {
  796. uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
  797. uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
  798. uint8_t in[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  799. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  800. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  801. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
  802. uint8_t out[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  803. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
  804. 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
  805. 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
  806. uint8_t buffer[64];
  807. AES128_CBC_encrypt_buffer(buffer, in, 64, key, iv);
  808. NRF_LOG_RAW_INFO("CBC encrypt: ");
  809. if(0 == strncmp((char*) out, (char*) buffer, 64))
  810. {
  811. NRF_LOG_RAW_INFO("SUCCESS!\n");
  812. }
  813. else
  814. {
  815. NRF_LOG_RAW_INFO("FAILURE!\n");
  816. }
  817. }
  818. void TestAES()
  819. {
  820. //test_encrypt_cbc();
  821. //test_decrypt_cbc();
  822. //test_encrypt_ecb();
  823. //test_decrypt_ecb();
  824. //CarEncryptEcb();
  825. //CarDecryptEcb();
  826. #if 0
  827. sprintf(SystemManager.TmpBuff2, "%s", "123456789012345678901234");
  828. AppEnryptCbc( SystemManager.TmpBuff2, SystemManager.TmpBuff);
  829. memset(SystemManager.TmpBuff2, 0, 40);
  830. AppDecryptCbc(SystemManager.TmpBuff, SystemManager.TmpBuff2);
  831. #endif
  832. //test_encrypt_ecb_verbose();
  833. }
  834. #endif
  835. int AppParkingCheck_Android(char *data)
  836. {
  837. uint8_t send_flag = 0;
  838. unsigned char i;
  839. char* pData = data;
  840. AppDecryptCbc(&pData[12], SystemManager.TmpBuff);
  841. NRF_LOG_RAW_INFO("App Park Info Android Low Data ==> ");
  842. for(i = 0; i < 16+12; ++i)
  843. NRF_LOG_RAW_INFO("%02x ", data[i] );
  844. NRF_LOG_RAW_INFO("\n");
  845. NRF_LOG_RAW_INFO("App Park Info Android ==> ");
  846. for(i = 0; i < 16; ++i)
  847. NRF_LOG_RAW_INFO("%02x ", SystemManager.TmpBuff[i] );
  848. NRF_LOG_RAW_INFO("\n");
  849. SystemManager.Site[0] = SystemManager.TmpBuff[0];
  850. SystemManager.Site[1] = SystemManager.TmpBuff[1];
  851. SystemManager.Site[2] = SystemManager.TmpBuff[2];
  852. SystemManager.Site[3] = SystemManager.TmpBuff[3];
  853. SystemManager.Dong[0] = SystemManager.TmpBuff[4];
  854. SystemManager.Dong[1] = SystemManager.TmpBuff[5];
  855. SystemManager.Ho[0] = SystemManager.TmpBuff[6];
  856. SystemManager.Ho[1] = SystemManager.TmpBuff[7];
  857. SystemManager.Key_Num = SystemManager.TmpBuff[8];
  858. SystemManager.Key_Type = SystemManager.TmpBuff[9];
  859. send_flag = 0;
  860. if(SystemTimer.SVR_SEND_TIMER > 3000)
  861. send_flag = 1;
  862. if(SystemManager.Dong_bak[0] != SystemManager.Dong[0])
  863. send_flag = 1;
  864. if(SystemManager.Dong_bak[1] != SystemManager.Dong[1])
  865. send_flag = 1;
  866. if(SystemManager.Ho_bak[0] != SystemManager.Ho[0])
  867. send_flag = 1;
  868. if(SystemManager.Ho_bak[1] != SystemManager.Ho[1])
  869. send_flag = 1;
  870. if(SystemManager.Key_Num_bak != SystemManager.Key_Num)
  871. send_flag = 1;
  872. if(SystemManager.Key_Type_bak != SystemManager.Key_Type)
  873. send_flag = 1;
  874. if(send_flag){
  875. SendParkingApp();
  876. SystemTimer.SVR_SEND_TIMER = 0;
  877. SystemManager.Dong_bak[0] = SystemManager.Dong[0];
  878. SystemManager.Dong_bak[1] = SystemManager.Dong[1];
  879. SystemManager.Ho_bak[0] = SystemManager.Ho[0];
  880. SystemManager.Ho_bak[1] = SystemManager.Ho[1];
  881. SystemManager.Key_Num_bak = SystemManager.Key_Num;
  882. SystemManager.Key_Type_bak = SystemManager.Key_Type;
  883. }
  884. return 1;
  885. }
  886. int AppParkingCheck_IOS(char* data)
  887. {
  888. uint8_t send_flag = 0;
  889. unsigned char i;
  890. char* pData = data;
  891. AppDecryptCbc(&pData[8], SystemManager.TmpBuff);
  892. NRF_LOG_RAW_INFO("App Park Info iOS Low Data ==> ");
  893. for(i = 0; i < 16+8; ++i)
  894. NRF_LOG_RAW_INFO("%02x ", data[i] );
  895. NRF_LOG_RAW_INFO("\n");
  896. NRF_LOG_RAW_INFO("App Park Info iOS ==> ");
  897. for(i = 0; i < 16; ++i)
  898. NRF_LOG_RAW_INFO("%02x ", SystemManager.TmpBuff[i] );
  899. NRF_LOG_RAW_INFO("\n");
  900. SystemManager.Site[0] = SystemManager.TmpBuff[0];
  901. SystemManager.Site[1] = SystemManager.TmpBuff[1];
  902. SystemManager.Site[2] = SystemManager.TmpBuff[2];
  903. SystemManager.Site[3] = SystemManager.TmpBuff[3];
  904. SystemManager.Dong[0] = SystemManager.TmpBuff[4];
  905. SystemManager.Dong[1] = SystemManager.TmpBuff[5];
  906. SystemManager.Ho[0] = SystemManager.TmpBuff[6];
  907. SystemManager.Ho[1] = SystemManager.TmpBuff[7];
  908. SystemManager.Key_Num = SystemManager.TmpBuff[8];
  909. SystemManager.Key_Type = SystemManager.TmpBuff[9];
  910. send_flag = 0;
  911. if(SystemTimer.SVR_SEND_TIMER > 3000)
  912. send_flag = 1;
  913. if(SystemManager.Dong_bak[0] != SystemManager.Dong[0])
  914. send_flag = 1;
  915. if(SystemManager.Dong_bak[1] != SystemManager.Dong[1])
  916. send_flag = 1;
  917. if(SystemManager.Ho_bak[0] != SystemManager.Ho[0])
  918. send_flag = 1;
  919. if(SystemManager.Ho_bak[1] != SystemManager.Ho[1])
  920. send_flag = 1;
  921. if(SystemManager.Key_Num_bak != SystemManager.Key_Num)
  922. send_flag = 1;
  923. if(SystemManager.Key_Type_bak != SystemManager.Key_Type)
  924. send_flag = 1;
  925. if(send_flag){
  926. SendParkingApp();
  927. SystemTimer.SVR_SEND_TIMER = 0;
  928. SystemManager.Dong_bak[0] = SystemManager.Dong[0];
  929. SystemManager.Dong_bak[1] = SystemManager.Dong[1];
  930. SystemManager.Ho_bak[0] = SystemManager.Ho[0];
  931. SystemManager.Ho_bak[1] = SystemManager.Ho[1];
  932. SystemManager.Key_Num_bak = SystemManager.Key_Num;
  933. SystemManager.Key_Type_bak = SystemManager.Key_Type;
  934. }
  935. return 1;
  936. }