sysmgr(1384).c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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. nrf_delay_ms(20);
  407. }
  408. memset( &RegMacPool, 0, sizeof(RegMacPool));
  409. }
  410. void LoadRegMac()
  411. {
  412. fds_find_token_t tok = {0};
  413. fds_record_desc_t desc = {0};
  414. uint8_t *data;
  415. char tmpBuf[15];
  416. NRF_LOG_INFO(
  417. "\nrec. id\t"
  418. "\tfile id\t"
  419. "\trec. key"
  420. "\tlength\tmac\n");
  421. while (fds_record_iterate(&desc, &tok) != FDS_ERR_NOT_FOUND)
  422. {
  423. ret_code_t rc;
  424. fds_flash_record_t frec = {0};
  425. rc = fds_record_open(&desc, &frec);
  426. switch (rc)
  427. {
  428. case NRF_SUCCESS:
  429. break;
  430. case FDS_ERR_CRC_CHECK_FAILED:
  431. NRF_LOG_INFO("error: CRC check failed!\n");
  432. continue;
  433. case FDS_ERR_NOT_FOUND:
  434. NRF_LOG_INFO("error: record not found!\n");
  435. continue;
  436. default:
  437. {
  438. NRF_LOG_INFO("error: unexpecte error %s.\n", fds_err_str(rc));
  439. continue;
  440. }
  441. }
  442. uint32_t const len = frec.p_header->length_words * sizeof(uint32_t);
  443. NRF_LOG_RAW_INFO(
  444. " 0x%04x\t"
  445. "\t 0x%04x\t"
  446. "\t 0x%04x\t"
  447. "\t %4u bytes\t",
  448. frec.p_header->record_id,
  449. frec.p_header->file_id,
  450. frec.p_header->record_key,
  451. len);
  452. data = (uint8_t *) frec.p_data;
  453. if( RegMacPool.Count >= 5 )
  454. {
  455. NRF_LOG_RAW_INFO( "Mac Pool Over\n");
  456. return;
  457. }
  458. RegMacPool.Mac[RegMacPool.Count][12] = 0;
  459. strncpy( RegMacPool.Mac[RegMacPool.Count++], data, 12);
  460. //for (uint8_t i=0;i<len;i++)
  461. {
  462. // NRF_LOG_RAW_INFO( "%c",data[i]);
  463. }
  464. //NRF_LOG_RAW_INFO( "\n");
  465. NRF_LOG_RAW_INFO( "%s\n",RegMacPool.Mac[RegMacPool.Count-1]);
  466. rc = fds_record_close(&desc);
  467. APP_ERROR_CHECK(rc);
  468. }
  469. }
  470. void SaveRegMac(char* mac)
  471. {
  472. int i;
  473. if( RegMacPool.Count>= 5 )
  474. return;
  475. for( i=0; i<RegMacPool.Count; i++)
  476. {
  477. if( strncmp( RegMacPool.Mac[i], mac, 12) == 0 )
  478. {
  479. NRF_LOG_RAW_INFO( "Allready Registrated\n");
  480. return;
  481. }
  482. }
  483. RegMacPool.Mac[RegMacPool.Count][12] = 0;
  484. strncpy( RegMacPool.Mac[RegMacPool.Count], mac, 12);
  485. record_write(1, RegMacPool.Count+1, mac, 12);
  486. RegMacPool.Count++;
  487. }
  488. int FindRegMac(char* mac)
  489. {
  490. int i;
  491. for( i=0; i<RegMacPool.Count; i++)
  492. {
  493. if( strncmp( RegMacPool.Mac[i], mac, 12) == 0 )
  494. {
  495. NRF_LOG_RAW_INFO( "Valid Mac\n");
  496. return 1;
  497. }
  498. }
  499. return 0;
  500. }
  501. void DisplayRegMac()
  502. {
  503. print_all_cmd( );
  504. }
  505. void FlashTest()
  506. {
  507. static uint8_t m_data[256];
  508. int len;
  509. sprintf(m_data, "hello-%d", 1);
  510. len = strlen(m_data);
  511. // record_write( 1, 1, m_data, len);
  512. sprintf(m_data, "hello-%d", 2);
  513. //record_write( 1, 2, m_data, len);
  514. print_all_cmd();
  515. }
  516. void SetMacAddress(uint8_t* addr)
  517. {
  518. sd_ble_gap_addr_get(&old_ble_addr);
  519. ble_gap_addr_t dd;
  520. dd.addr_id_peer = 0;
  521. dd.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
  522. dd.addr[0] = addr[0];
  523. dd.addr[1] = addr[1];
  524. //dd.addr[2] = addr[2];
  525. dd.addr[2] = 0x57;
  526. dd.addr[3] = addr[3];
  527. dd.addr[4] = addr[4];
  528. dd.addr[5] = addr[5];
  529. sd_ble_gap_addr_set(&dd);
  530. sd_ble_gap_addr_get(&new_ble_addr);
  531. }
  532. #if 0
  533. // prints string as hex
  534. static void phex(uint8_t* str)
  535. {
  536. unsigned char i;
  537. for(i = 0; i < 16; ++i)
  538. NRF_LOG_RAW_INFO("%.2x", str[i]);
  539. NRF_LOG_RAW_INFO("\n");
  540. }
  541. static void test_encrypt_ecb_verbose(void)
  542. {
  543. // Example of more verbose verification
  544. uint8_t i, buf[64], buf2[64];
  545. // 128bit key
  546. 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 };
  547. // 512bit text
  548. 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,
  549. (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,
  550. (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,
  551. (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 };
  552. memset(buf, 0, 64);
  553. memset(buf2, 0, 64);
  554. // print text to encrypt, key and IV
  555. NRF_LOG_RAW_INFO("ECB encrypt verbose:\n\n");
  556. NRF_LOG_RAW_INFO("plain text:\n");
  557. for(i = (uint8_t) 0; i < (uint8_t) 4; ++i)
  558. {
  559. phex(plain_text + i * (uint8_t) 16);
  560. }
  561. NRF_LOG_RAW_INFO("\n");
  562. NRF_LOG_RAW_INFO("key:\n");
  563. phex(key);
  564. NRF_LOG_RAW_INFO("\n");
  565. // print the resulting cipher as 4 x 16 byte strings
  566. NRF_LOG_RAW_INFO("ciphertext:\n");
  567. for(i = 0; i < 4; ++i)
  568. {
  569. AES128_ECB_encrypt(plain_text + (i*16), key, buf+(i*16));
  570. phex(buf + (i*16));
  571. }
  572. NRF_LOG_RAW_INFO("\n");
  573. }
  574. static void test_encrypt_ecb(void)
  575. {
  576. uint8_t key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
  577. uint8_t in[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
  578. uint8_t out[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
  579. uint8_t buffer[16];
  580. AES128_ECB_encrypt(in, key, buffer);
  581. NRF_LOG_RAW_INFO("ECB encrypt: ");
  582. if(0 == strncmp((char*) out, (char*) buffer, 16))
  583. {
  584. NRF_LOG_RAW_INFO("SUCCESS!\n");
  585. }
  586. else
  587. {
  588. NRF_LOG_RAW_INFO("FAILURE!\n");
  589. }
  590. }
  591. static void test_decrypt_ecb(void)
  592. {
  593. uint8_t key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
  594. uint8_t in[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
  595. uint8_t out[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
  596. uint8_t buffer[16];
  597. AES128_ECB_decrypt(in, key, buffer);
  598. NRF_LOG_RAW_INFO("ECB decrypt: ");
  599. if(0 == strncmp((char*) out, (char*) buffer, 16))
  600. {
  601. NRF_LOG_RAW_INFO("SUCCESS!\n");
  602. }
  603. else
  604. {
  605. NRF_LOG_RAW_INFO("FAILURE!\n");
  606. }
  607. }
  608. #endif
  609. void WesEncryptEcb(int mode)
  610. {
  611. uint8_t key[] = {'I', 'E', 'S', '-', '2', '0', '0', 'B', '-', 'W', 'E', 'S', '-', 'H', 'D', 'C'}; // IES-200B PARTRON
  612. uint8_t in[] = {'W', 'E', 'S', 1,2,3,4,5,6,7,8,9,10,11,12,13 };
  613. uint16_t tempVal;
  614. tempVal = rand();
  615. in[3] = tempVal&0xff;
  616. in[4] = (tempVal>>8)&0xff;
  617. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  618. NRF_LOG_RAW_INFO("WesEncrypEcb Random : 0x%x02, 0x%x02 \n", in[3], in[4]);
  619. }
  620. void WesDecryptEcb(char* in, char* out)
  621. {
  622. uint8_t key[] = {'I', 'E', 'S', '-', '2', '0', '0', 'B', '-', 'W', 'E', 'S', '-', 'H', 'D', 'C'}; // IES-200B PARTRON
  623. AES128_ECB_decrypt(in, key, out);
  624. }
  625. void WesEncryptEcbACK(void)
  626. {
  627. uint8_t key[] = {'I', 'E', 'S', '-', '2', '0', '0', 'B', '-', 'W', 'E', 'S', '-', 'H', 'D', 'C'}; // IES-200B PARTRON
  628. uint8_t in[] = {'W', 'E', 'S', 'A', 'C', 'K', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  629. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  630. NRF_LOG_RAW_INFO("WesEncryptEcb ACK\n");
  631. }
  632. // New iOS OnePass App
  633. void PcaEncryptEcb(int mode)
  634. {
  635. uint8_t key[] = {'P', 'C', 'A', '-', 'O', 'N', 'E', 'P', 'A', 'S', 'S', '-', 'A', 'P', 'P', 'Z'}; // IES-200B PARTRON
  636. uint8_t in[] = {'P', 'C', 'A', 1,2,3,4,5,6,7,8,9,10,11,12,13 };
  637. uint16_t tempVal;
  638. tempVal = rand();
  639. in[3] = tempVal&0xff;
  640. in[4] = (tempVal>>8)&0xff;
  641. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  642. NRF_LOG_RAW_INFO("PcaEncrypEcb Random : 0x%x02, 0x%x02 \n", in[3], in[4]);
  643. }
  644. void PcaDecryptEcb(char* in, char* out)
  645. {
  646. uint8_t key[] = {'P', 'C', 'A', '-', 'O', 'N', 'E', 'P', 'A', 'S', 'S', '-', 'A', 'P', 'P', 'Z'}; // IES-200B PARTRON
  647. AES128_ECB_decrypt(in, key, out);
  648. }
  649. void PcaEncryptEcbACK(void)
  650. {
  651. uint8_t key[] = {'P', 'C', 'A', '-', 'O', 'N', 'E', 'P', 'A', 'S', 'S', '-', 'A', 'P', 'P', 'Z'}; // IES-200B PARTRON
  652. uint8_t in[] = {'P', 'C', 'A', 'A', 'C', 'K', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  653. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  654. NRF_LOG_RAW_INFO("PcaEncryptEcb ACK\n");
  655. }
  656. void CarEncryptEcb(int mode)
  657. {
  658. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  659. uint8_t in[] = {'C', 'A', 'R', 1,2,3,4,5,6,7,8,9,10,11,12,13 };
  660. uint16_t tempVal;
  661. if( mode == AES_ENC_CAR )
  662. {
  663. in[0] = 'C';
  664. in[1] = 'A';
  665. in[2] = 'R';
  666. }else if( mode == AES_ENC_EMG )
  667. {
  668. in[0] = 'E';
  669. in[1] = 'M';
  670. in[2] = 'G';
  671. }else if( mode == AES_ENC_PCA )
  672. {
  673. in[0] = 'P';
  674. in[1] = 'C';
  675. in[2] = 'A';
  676. }
  677. tempVal = rand();
  678. in[3] = (tempVal>>8)&0xff;
  679. in[4] = tempVal&0xff;
  680. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  681. NRF_LOG_RAW_INFO("ECB encrypt: 0x%x04\n", tempVal);
  682. }
  683. // 0x73:parking 0x72:emg
  684. void CarEncryptEcbACK(char type)
  685. {
  686. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  687. uint8_t in[] = {'C', 'A', 'R', 'A','C','K',1,2,3,4,5,6,7,8,9,0x0A };
  688. switch(type){
  689. // BAND EMG
  690. case 0x72:
  691. in[0] = 'E';
  692. in[1] = 'M';
  693. in[2] = 'G';
  694. break;
  695. #if 0
  696. // NEW iOS APP
  697. case 0x75:
  698. in[0] = 'P';
  699. in[1] = 'C';
  700. in[2] = 'A';
  701. break;
  702. }
  703. #endif
  704. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  705. NRF_LOG_RAW_INFO("ECB ACK\n");
  706. }
  707. void CarDecryptEcb(char* in, char* out)
  708. {
  709. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  710. AES128_ECB_decrypt(in, key, out);
  711. }
  712. //
  713. void AppDecryptCbc(char* in, char* out)
  714. {
  715. uint8_t key[] = {'1', '9', '-', 'H', 'D', 'C', '-', 'I', 'C', 'O', 'N', 'T', 'R', 'O', 'L', 'S'}; // App
  716. uint8_t iv[] = {'H', 'D', 'C', '-', 'I', 'V', '-', 'R', 'N', 'D', 'C', 'E', 'N', 'T', 'E', 'R' };
  717. //uint8_t key[] = "19-HDC-ICONTROLS";
  718. //uint8_t iv[] = "HDC-IV-RNDCENTER";
  719. AES128_CBC_decrypt_buffer(out+0, in+0, 16, key, iv);
  720. //AES128_CBC_decrypt_buffer(out+16, in+16, 8, 0, 0);
  721. }
  722. void AppEnryptCbc(char* in, char* out)
  723. {
  724. uint8_t key[] = {'1', '9', '-', 'H', 'D', 'C', '-', 'T', 'C', 'O', 'N', 'T', 'R', 'O', 'L', 'S'}; // APP
  725. uint8_t iv[] = {'H', 'D', 'C', '-', 'T', 'V', '-', 'R', 'N', 'D', 'C', 'E', 'N', 'T', 'E', 'R' };
  726. // 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 };
  727. // uint8_t buffer[30];
  728. AES128_CBC_encrypt_buffer(out, in, 24, key, iv);
  729. NRF_LOG_RAW_INFO("CBC encrypt: ");
  730. }
  731. #if 0
  732. static void test_decrypt_cbc(void)
  733. {
  734. // Example "simulating" a smaller buffer...
  735. uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
  736. uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
  737. uint8_t in[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  738. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
  739. 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
  740. 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
  741. uint8_t out[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  742. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  743. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  744. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
  745. uint8_t buffer[64];
  746. AES128_CBC_decrypt_buffer(buffer+0, in+0, 16, key, iv);
  747. AES128_CBC_decrypt_buffer(buffer+16, in+16, 16, 0, 0);
  748. AES128_CBC_decrypt_buffer(buffer+32, in+32, 16, 0, 0);
  749. AES128_CBC_decrypt_buffer(buffer+48, in+48, 16, 0, 0);
  750. NRF_LOG_RAW_INFO("CBC decrypt: ");
  751. if(0 == strncmp((char*) out, (char*) buffer, 64))
  752. {
  753. NRF_LOG_RAW_INFO("SUCCESS!\n");
  754. }
  755. else
  756. {
  757. NRF_LOG_RAW_INFO("FAILURE!\n");
  758. }
  759. }
  760. static void test_encrypt_cbc(void)
  761. {
  762. uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
  763. uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
  764. uint8_t in[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  765. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  766. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  767. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
  768. uint8_t out[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  769. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
  770. 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
  771. 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
  772. uint8_t buffer[64];
  773. AES128_CBC_encrypt_buffer(buffer, in, 64, key, iv);
  774. NRF_LOG_RAW_INFO("CBC encrypt: ");
  775. if(0 == strncmp((char*) out, (char*) buffer, 64))
  776. {
  777. NRF_LOG_RAW_INFO("SUCCESS!\n");
  778. }
  779. else
  780. {
  781. NRF_LOG_RAW_INFO("FAILURE!\n");
  782. }
  783. }
  784. #endif
  785. int AppParkingCheck(char* data)
  786. {
  787. unsigned char i;
  788. char* pData = data;
  789. if( *data != 0x02 )
  790. return 0;
  791. data++;
  792. // if( *data != 0x01 )
  793. // return 0;
  794. data++;
  795. // if( *data != 0x1A )
  796. // return 0;
  797. data++;
  798. if( *data != 0x19 )
  799. return 0;
  800. data++;
  801. if( *data != 0xff )
  802. return 0;
  803. AppDecryptCbc(&pData[12], SystemManager.TmpBuff);
  804. NRF_LOG_RAW_INFO("App Park Info ==> ");
  805. for(i = 0; i < 16; ++i)
  806. NRF_LOG_RAW_INFO("%02x ", SystemManager.TmpBuff[i] );
  807. NRF_LOG_RAW_INFO("\n");
  808. SystemManager.Site[0] = SystemManager.TmpBuff[0];
  809. SystemManager.Site[1] = SystemManager.TmpBuff[1];
  810. SystemManager.Site[2] = SystemManager.TmpBuff[2];
  811. SystemManager.Site[3] = SystemManager.TmpBuff[3];
  812. SystemManager.Dong[0] = SystemManager.TmpBuff[4];
  813. SystemManager.Dong[1] = SystemManager.TmpBuff[5];
  814. SystemManager.Ho[0] = SystemManager.TmpBuff[6];
  815. SystemManager.Ho[1] = SystemManager.TmpBuff[7];
  816. if( SystemTimer.SVR_SEND_TIMER > 1000 )
  817. {
  818. SendParkingApp();
  819. }
  820. SystemTimer.SVR_SEND_TIMER = 0;
  821. return 1;
  822. }
  823. int AppParkingCheck_IOS(char* data)
  824. {
  825. unsigned char i;
  826. char* pData = data;
  827. AppDecryptCbc(&pData[8], SystemManager.TmpBuff);
  828. NRF_LOG_RAW_INFO("App Park Info ==> ");
  829. for(i = 0; i < 16; ++i)
  830. NRF_LOG_RAW_INFO("%02x ", SystemManager.TmpBuff[i] );
  831. NRF_LOG_RAW_INFO("\n");
  832. SystemManager.Site[0] = SystemManager.TmpBuff[0];
  833. SystemManager.Site[1] = SystemManager.TmpBuff[1];
  834. SystemManager.Site[2] = SystemManager.TmpBuff[2];
  835. SystemManager.Site[3] = SystemManager.TmpBuff[3];
  836. SystemManager.Dong[0] = SystemManager.TmpBuff[4];
  837. SystemManager.Dong[1] = SystemManager.TmpBuff[5];
  838. SystemManager.Ho[0] = SystemManager.TmpBuff[6];
  839. SystemManager.Ho[1] = SystemManager.TmpBuff[7];
  840. if( SystemTimer.SVR_SEND_TIMER > 1000 )
  841. {
  842. SendParkingApp();
  843. }
  844. SystemTimer.SVR_SEND_TIMER = 0;
  845. return 1;
  846. }