sysmgr(1608).c 27 KB

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