sysmgr(4255).c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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(
  436. "error: unexpecte error %s.\n",
  437. 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 CarEncryptEcb(int mode)
  608. {
  609. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  610. uint8_t in[] = {'C', 'A', 'R', 1,2,3,4,5,6,7,8,9,10,11,12,13 };
  611. uint16_t tempVal;
  612. if( mode == AES_ENC_CAR )
  613. {
  614. in[0] = 'C';
  615. in[1] = 'A';
  616. in[2] = 'R';
  617. }else if( mode == AES_ENC_EMG )
  618. {
  619. in[0] = 'E';
  620. in[1] = 'M';
  621. in[2] = 'G';
  622. }
  623. tempVal = rand();
  624. in[3] = tempVal&0xff;
  625. in[4] = (tempVal>>8)&0xff;
  626. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  627. NRF_LOG_RAW_INFO("ECB encrypt: %x\n", tempVal);
  628. }
  629. // 0x73:parking 0x72:emg
  630. void CarEncryptEcbACK(char type)
  631. {
  632. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  633. uint8_t in[] = {'C', 'A', 'R', 'A','C','K',1,2,3,4,5,6,7,8,9,0x0A };
  634. if( type == 0x72 ) // emg
  635. {
  636. in[3] = 'E';
  637. in[4] = 'M';
  638. in[5] = 'G';
  639. }
  640. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  641. NRF_LOG_RAW_INFO("ECB ACK\n");
  642. }
  643. void CarDecryptEcb(char* in, char* out)
  644. {
  645. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  646. // uint8_t out[] = {'C', 'A', 'R', 1,2,3,4,5,6,7,8,9,10,11,12,13 };
  647. // uint8_t buffer[16];
  648. AES128_ECB_decrypt(in, key, out);
  649. // NRF_LOG_RAW_INFO("ECB decrypt: ");
  650. }
  651. void AppDecryptCbc(char* in, char* out)
  652. {
  653. uint8_t key[] = {'1', '9', '-', 'H', 'D', 'C', '-', 'I', 'C', 'O', 'N', 'T', 'R', 'O', 'L', 'S'}; // App
  654. uint8_t iv[] = {'H', 'D', 'C', '-', 'I', 'V', '-', 'R', 'N', 'D', 'C', 'E', 'N', 'T', 'E', 'R' };
  655. //uint8_t key[] = "19-HDC-ICONTROLS";
  656. //uint8_t iv[] = "HDC-IV-RNDCENTER";
  657. AES128_CBC_decrypt_buffer(out+0, in+0, 16, key, iv);
  658. //AES128_CBC_decrypt_buffer(out+16, in+16, 8, 0, 0);
  659. }
  660. void AppEnryptCbc(char* in, char* out)
  661. {
  662. uint8_t key[] = {'1', '9', '-', 'H', 'D', 'C', '-', 'T', 'C', 'O', 'N', 'T', 'R', 'O', 'L', 'S'}; // APP
  663. uint8_t iv[] = {'H', 'D', 'C', '-', 'T', 'V', '-', 'R', 'N', 'D', 'C', 'E', 'N', 'T', 'E', 'R' };
  664. // 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 };
  665. // uint8_t buffer[30];
  666. AES128_CBC_encrypt_buffer(out, in, 24, key, iv);
  667. NRF_LOG_RAW_INFO("CBC encrypt: ");
  668. }
  669. static void test_decrypt_cbc(void)
  670. {
  671. // Example "simulating" a smaller buffer...
  672. uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
  673. uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
  674. uint8_t in[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  675. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
  676. 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
  677. 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
  678. uint8_t out[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  679. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  680. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  681. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
  682. uint8_t buffer[64];
  683. AES128_CBC_decrypt_buffer(buffer+0, in+0, 16, key, iv);
  684. AES128_CBC_decrypt_buffer(buffer+16, in+16, 16, 0, 0);
  685. AES128_CBC_decrypt_buffer(buffer+32, in+32, 16, 0, 0);
  686. AES128_CBC_decrypt_buffer(buffer+48, in+48, 16, 0, 0);
  687. NRF_LOG_RAW_INFO("CBC decrypt: ");
  688. if(0 == strncmp((char*) out, (char*) buffer, 64))
  689. {
  690. NRF_LOG_RAW_INFO("SUCCESS!\n");
  691. }
  692. else
  693. {
  694. NRF_LOG_RAW_INFO("FAILURE!\n");
  695. }
  696. }
  697. static void test_encrypt_cbc(void)
  698. {
  699. uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
  700. uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
  701. uint8_t in[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  702. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  703. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  704. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
  705. uint8_t out[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  706. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
  707. 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
  708. 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
  709. uint8_t buffer[64];
  710. AES128_CBC_encrypt_buffer(buffer, in, 64, key, iv);
  711. NRF_LOG_RAW_INFO("CBC encrypt: ");
  712. if(0 == strncmp((char*) out, (char*) buffer, 64))
  713. {
  714. NRF_LOG_RAW_INFO("SUCCESS!\n");
  715. }
  716. else
  717. {
  718. NRF_LOG_RAW_INFO("FAILURE!\n");
  719. }
  720. }
  721. void TestAES()
  722. {
  723. //test_encrypt_cbc();
  724. //test_decrypt_cbc();
  725. //test_encrypt_ecb();
  726. //test_decrypt_ecb();
  727. //CarEncryptEcb();
  728. //CarDecryptEcb();
  729. #if 0
  730. sprintf(SystemManager.TmpBuff2, "%s", "123456789012345678901234");
  731. AppEnryptCbc( SystemManager.TmpBuff2, SystemManager.TmpBuff);
  732. memset(SystemManager.TmpBuff2, 0, 40);
  733. AppDecryptCbc(SystemManager.TmpBuff, SystemManager.TmpBuff2);
  734. #endif
  735. //test_encrypt_ecb_verbose();
  736. }
  737. int AppParkingCheck(char* data)
  738. {
  739. unsigned char i;
  740. char* pData = data;
  741. if( *data != 0x02 )
  742. return 0;
  743. data++;
  744. if( *data != 0x01 )
  745. return 0;
  746. data++;
  747. if( *data != 0x1A )
  748. return 0;
  749. data++;
  750. if( *data != 0x19 )
  751. return 0;
  752. data++;
  753. if( *data != 0xff )
  754. return 0;
  755. AppDecryptCbc(&pData[12], SystemManager.TmpBuff);
  756. NRF_LOG_RAW_INFO("App Park Info ==> ");
  757. for(i = 0; i < 16; ++i)
  758. NRF_LOG_RAW_INFO("%02x ", SystemManager.TmpBuff[i] );
  759. NRF_LOG_RAW_INFO("\n");
  760. SystemManager.Site[0] = SystemManager.TmpBuff[0];
  761. SystemManager.Site[1] = SystemManager.TmpBuff[1];
  762. SystemManager.Site[2] = SystemManager.TmpBuff[2];
  763. SystemManager.Site[3] = SystemManager.TmpBuff[3];
  764. SystemManager.Dong[0] = SystemManager.TmpBuff[4];
  765. SystemManager.Dong[1] = SystemManager.TmpBuff[5];
  766. SystemManager.Ho[0] = SystemManager.TmpBuff[6];
  767. SystemManager.Ho[1] = SystemManager.TmpBuff[7];
  768. if( SystemTimer.SVR_SEND_TIMER > 1000 )
  769. {
  770. SendParkingApp();
  771. }
  772. SystemTimer.SVR_SEND_TIMER = 0;
  773. return 1;
  774. }