sysmgr(1422).c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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 "nrf_pwr_mgmt.h"
  12. #include "main.h"
  13. stSystemTimer SystemTimer;
  14. stSystemManager SystemManager;
  15. extern const nrf_drv_spi_t spi; /**< SPI instance. */
  16. extern volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
  17. uint8_t m_tx_buf[64]; /**< TX buffer. */
  18. uint8_t m_rx_buf[64]; /**< RX buffer. */
  19. uint8_t m_length; /**< Transfer length. */
  20. RegMacPool_t RegMacPool;
  21. const char *fds_err_str(ret_code_t ret)
  22. {
  23. /* Array to map FDS return values to strings. */
  24. static char const * err_str[] =
  25. {
  26. "FDS_ERR_OPERATION_TIMEOUT",
  27. "FDS_ERR_NOT_INITIALIZED",
  28. "FDS_ERR_UNALIGNED_ADDR",
  29. "FDS_ERR_INVALID_ARG",
  30. "FDS_ERR_NULL_ARG",
  31. "FDS_ERR_NO_OPEN_RECORDS",
  32. "FDS_ERR_NO_SPACE_IN_FLASH",
  33. "FDS_ERR_NO_SPACE_IN_QUEUES",
  34. "FDS_ERR_RECORD_TOO_LARGE",
  35. "FDS_ERR_NOT_FOUND",
  36. "FDS_ERR_NO_PAGES",
  37. "FDS_ERR_USER_LIMIT_REACHED",
  38. "FDS_ERR_CRC_CHECK_FAILED",
  39. "FDS_ERR_BUSY",
  40. "FDS_ERR_INTERNAL",
  41. };
  42. return err_str[ret - NRF_ERROR_FDS_ERR_BASE];
  43. }
  44. void ParkSysInit()
  45. {
  46. SystemTimer.TMR_SYS_OFF = 3;
  47. }
  48. void ParkPowerOff()
  49. {
  50. SystemTimer.TMR_SYS_OFF = 0;
  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. /* 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. fds_record_desc_t desc = {0};
  215. desc.record_id = 1;
  216. //ret_code_t rc = fds_record_write(NULL, &rec);
  217. ret_code_t rc = fds_record_write(&desc, &rec);
  218. if (rc != NRF_SUCCESS)
  219. {
  220. NRF_LOG_INFO(
  221. "error: fds_record_write() returned %s.\n",
  222. fds_err_str(rc));
  223. }
  224. }
  225. static void record_update( configuration_t const * p_cfg)
  226. {
  227. fds_record_desc_t desc = {0};
  228. fds_find_token_t ftok = {0};
  229. if (fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &ftok) == NRF_SUCCESS)
  230. {
  231. fds_record_t const rec =
  232. {
  233. .file_id = CONFIG_FILE,
  234. .key = CONFIG_REC_KEY,
  235. .data.p_data = p_cfg,
  236. .data.length_words = (sizeof(configuration_t) + 3) / sizeof(uint32_t)
  237. };
  238. ret_code_t rc = fds_record_update(&desc, &rec);
  239. if (rc != NRF_SUCCESS)
  240. {
  241. NRF_LOG_INFO( "error: fds_record_update() returned %s.\n",
  242. fds_err_str(rc));
  243. }
  244. }
  245. else
  246. {
  247. NRF_LOG_INFO( "error: could not find config file.\n");
  248. }
  249. }
  250. static void record_delete( uint32_t fid, uint32_t key)
  251. {
  252. fds_find_token_t tok = {0};
  253. fds_record_desc_t desc = {0};
  254. NRF_LOG_INFO(
  255. "deleting record...\n"
  256. "file: 0x%x, key: 0x%x\n",
  257. fid,
  258. key);
  259. if (fds_record_find(fid, key, &desc, &tok) == NRF_SUCCESS)
  260. {
  261. ret_code_t rc = fds_record_delete(&desc);
  262. if (rc != NRF_SUCCESS)
  263. {
  264. NRF_LOG_INFO(
  265. "error: fds_record_delete() returned %s.\n", fds_err_str(rc));
  266. return;
  267. }
  268. NRF_LOG_INFO( "record id: 0x%x\n", desc.record_id);
  269. }
  270. else
  271. {
  272. NRF_LOG_INFO( "error: record not found!\n");
  273. }
  274. }
  275. bool record_delete_next(void)
  276. {
  277. fds_find_token_t tok = {0};
  278. fds_record_desc_t desc = {0};
  279. if (fds_record_iterate(&desc, &tok) == NRF_SUCCESS)
  280. {
  281. ret_code_t rc = fds_record_delete(&desc);
  282. if (rc != NRF_SUCCESS)
  283. {
  284. return false;
  285. }
  286. return true;
  287. }
  288. else
  289. {
  290. /* No records left to delete. */
  291. return false;
  292. }
  293. }
  294. static void print_cfg_cmd( size_t argc, char ** argv)
  295. {
  296. fds_record_desc_t desc = {0};
  297. fds_find_token_t tok = {0};
  298. while (fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok) == NRF_SUCCESS)
  299. {
  300. ret_code_t rc;
  301. fds_flash_record_t frec = {0};
  302. rc = fds_record_open(&desc, &frec);
  303. switch (rc)
  304. {
  305. case NRF_SUCCESS:
  306. break;
  307. case FDS_ERR_CRC_CHECK_FAILED:
  308. NRF_LOG_INFO( "error: CRC check failed!\n");
  309. continue;
  310. case FDS_ERR_NOT_FOUND:
  311. NRF_LOG_INFO( "error: record not found!\n");
  312. continue;
  313. default:
  314. {
  315. NRF_LOG_INFO(
  316. "error: unexpecte error %s.\n",
  317. fds_err_str(rc));
  318. continue;
  319. }
  320. }
  321. configuration_t * p_cfg = (configuration_t *)(frec.p_data);
  322. NRF_LOG_INFO(
  323. "config1:\t%s\n"
  324. "config2:\t%s\n"
  325. "boot count:\t%u\n"
  326. "device name:\t%s\n",
  327. p_cfg->config1_on ? "on" : "off",
  328. p_cfg->config2_on ? "on" : "off",
  329. p_cfg->boot_count,
  330. p_cfg->device_name);
  331. rc = fds_record_close(&desc);
  332. APP_ERROR_CHECK(rc);
  333. }
  334. }
  335. static void print_all_cmd( )
  336. {
  337. fds_find_token_t tok = {0};
  338. fds_record_desc_t desc = {0};
  339. uint8_t *data;
  340. NRF_LOG_INFO("rec. id \t file id \t rec. key \t length");
  341. while (fds_record_iterate(&desc, &tok) != FDS_ERR_NOT_FOUND)
  342. {
  343. ret_code_t rc;
  344. fds_flash_record_t frec = {0};
  345. rc = fds_record_open(&desc, &frec);
  346. switch (rc)
  347. {
  348. case NRF_SUCCESS:
  349. break;
  350. case FDS_ERR_CRC_CHECK_FAILED:
  351. NRF_LOG_INFO( "error: CRC check failed!\n");
  352. continue;
  353. case FDS_ERR_NOT_FOUND:
  354. NRF_LOG_INFO( "error: record not found!\n");
  355. continue;
  356. default:
  357. {
  358. NRF_LOG_INFO("error: unexpecte error %s.\n", fds_err_str(rc));
  359. continue;
  360. }
  361. }
  362. uint32_t const len = frec.p_header->length_words * sizeof(uint32_t);
  363. NRF_LOG_INFO( " 0x%04x"
  364. "\t 0x%04x"
  365. "\t 0x%04x"
  366. "\t %4u bytes\t",
  367. frec.p_header->record_id,
  368. frec.p_header->file_id,
  369. frec.p_header->record_key,
  370. len);
  371. data = (uint8_t *) frec.p_data;
  372. for (uint8_t i=0;i<len;i++)
  373. {
  374. NRF_LOG_RAW_INFO( "%c",data[i]);
  375. }
  376. NRF_LOG_INFO("\n");
  377. rc = fds_record_close(&desc);
  378. APP_ERROR_CHECK(rc);
  379. }
  380. }
  381. void RegMac(char* str)
  382. {
  383. print_all_cmd();
  384. NRF_LOG_RAW_INFO("Registration MAC : %s\n", str);
  385. RegMacPool.Count++;
  386. record_write(1, RegMacPool.Count, str, 12);
  387. print_all_cmd();
  388. }
  389. void RegMacDelteAll()
  390. {
  391. bool next;
  392. #if 1
  393. while(1)
  394. {
  395. next = record_delete_next();
  396. if (!next)
  397. {
  398. NRF_LOG_INFO("No records left to delete.");
  399. break;
  400. }
  401. nrf_delay_ms(20);
  402. }
  403. #endif
  404. memset( &RegMacPool, 0, sizeof(RegMacPool));
  405. }
  406. void LoadRegMac()
  407. {
  408. fds_find_token_t tok = {0};
  409. fds_record_desc_t desc = {0};
  410. uint8_t *data;
  411. char tmpBuf[15];
  412. NRF_LOG_INFO(
  413. "\nrec. id\t"
  414. "\tfile id\t"
  415. "\trec. key"
  416. "\tlength\tmac\n");
  417. while (fds_record_iterate(&desc, &tok) != FDS_ERR_NOT_FOUND)
  418. {
  419. ret_code_t rc;
  420. fds_flash_record_t frec = {0};
  421. rc = fds_record_open(&desc, &frec);
  422. switch (rc)
  423. {
  424. case NRF_SUCCESS:
  425. break;
  426. case FDS_ERR_CRC_CHECK_FAILED:
  427. NRF_LOG_INFO("error: CRC check failed!\n");
  428. continue;
  429. case FDS_ERR_NOT_FOUND:
  430. NRF_LOG_INFO("error: record not found!\n");
  431. continue;
  432. default:
  433. {
  434. NRF_LOG_INFO("error: unexpecte error %s.\n", fds_err_str(rc));
  435. continue;
  436. }
  437. }
  438. uint32_t const len = frec.p_header->length_words * sizeof(uint32_t);
  439. NRF_LOG_RAW_INFO(
  440. " 0x%04x\t"
  441. "\t 0x%04x\t"
  442. "\t 0x%04x\t"
  443. "\t %4u bytes\t",
  444. frec.p_header->record_id,
  445. frec.p_header->file_id,
  446. frec.p_header->record_key,
  447. len);
  448. data = (uint8_t *) frec.p_data;
  449. if( RegMacPool.Count >= 5 )
  450. {
  451. NRF_LOG_RAW_INFO( "Mac Pool Over\n");
  452. return;
  453. }
  454. RegMacPool.Mac[RegMacPool.Count][12] = 0;
  455. strncpy( RegMacPool.Mac[RegMacPool.Count++], data, 12);
  456. //for (uint8_t i=0;i<len;i++)
  457. {
  458. // NRF_LOG_RAW_INFO( "%c",data[i]);
  459. }
  460. //NRF_LOG_RAW_INFO( "\n");
  461. NRF_LOG_RAW_INFO( "%s\n",RegMacPool.Mac[RegMacPool.Count-1]);
  462. rc = fds_record_close(&desc);
  463. APP_ERROR_CHECK(rc);
  464. }
  465. }
  466. void LoadReceiverMac()
  467. {
  468. fds_find_token_t tok = {0};
  469. fds_record_desc_t desc = {0};
  470. uint8_t *data;
  471. char tmpBuf[15];
  472. NRF_LOG_INFO(
  473. "\nrec. id\t"
  474. "\tfile id\t"
  475. "\trec. key"
  476. "\tlength\tmac\n");
  477. while (fds_record_iterate(&desc, &tok) != FDS_ERR_NOT_FOUND)
  478. {
  479. ret_code_t rc;
  480. fds_flash_record_t frec = {0};
  481. rc = fds_record_open(&desc, &frec);
  482. switch (rc)
  483. {
  484. case NRF_SUCCESS:
  485. break;
  486. case FDS_ERR_CRC_CHECK_FAILED:
  487. NRF_LOG_INFO("error: CRC check failed!\n");
  488. continue;
  489. case FDS_ERR_NOT_FOUND:
  490. NRF_LOG_INFO("error: record not found!\n");
  491. continue;
  492. default:
  493. {
  494. NRF_LOG_INFO("error: unexpecte error %s.\n", fds_err_str(rc));
  495. continue;
  496. }
  497. }
  498. uint32_t const len = frec.p_header->length_words * sizeof(uint32_t);
  499. NRF_LOG_RAW_INFO(
  500. " 0x%04x\t"
  501. "\t 0x%04x\t"
  502. "\t 0x%04x\t"
  503. "\t %4u bytes\t",
  504. frec.p_header->record_id,
  505. frec.p_header->file_id,
  506. frec.p_header->record_key,
  507. len);
  508. data = (uint8_t *) frec.p_data;
  509. strncpy( SystemManager.StrRegPeerAddr, data, 12);
  510. strncpy( tmpBuf, SystemManager.StrRegPeerAddr, 12);
  511. SystemManager.StrRegPeerAddr[12] = 0;
  512. tmpBuf[12] = 0;
  513. NRF_LOG_RAW_INFO( "%s\n",SystemManager.StrRegPeerAddr);
  514. SystemManager.RegPeerAddr[5] = (uint8_t)strtol(&tmpBuf[10], NULL, 16); tmpBuf[10] = 0;
  515. SystemManager.RegPeerAddr[4] = (uint8_t)strtol(&tmpBuf[8], NULL, 16); tmpBuf[8] = 0;
  516. SystemManager.RegPeerAddr[3] = (uint8_t)strtol(&tmpBuf[6], NULL, 16); tmpBuf[6] = 0;
  517. SystemManager.RegPeerAddr[2] = (uint8_t)strtol(&tmpBuf[4], NULL, 16); tmpBuf[4] = 0;
  518. SystemManager.RegPeerAddr[1] = (uint8_t)strtol(&tmpBuf[2], NULL, 16); tmpBuf[2] = 0;
  519. SystemManager.RegPeerAddr[0] = (uint8_t)strtol(&tmpBuf[0], NULL, 16);
  520. rc = fds_record_close(&desc);
  521. APP_ERROR_CHECK(rc);
  522. }
  523. }
  524. void SaveRegMac(char* mac)
  525. {
  526. int i;
  527. if( RegMacPool.Count>= 5 )
  528. return;
  529. for( i=0; i<RegMacPool.Count; i++)
  530. {
  531. if( strncmp( RegMacPool.Mac[i], mac, 12) == 0 )
  532. {
  533. NRF_LOG_RAW_INFO( "Allready Registrated\n");
  534. return;
  535. }
  536. }
  537. RegMacPool.Mac[RegMacPool.Count][12] = 0;
  538. strncpy( RegMacPool.Mac[RegMacPool.Count], mac, 12);
  539. record_write(1, RegMacPool.Count+1, mac, 12);
  540. RegMacPool.Count++;
  541. }
  542. void FlashTest()
  543. {
  544. static uint8_t m_data[256];
  545. int len;
  546. sprintf(m_data, "hello-%d", 1);
  547. len = strlen(m_data);
  548. record_write( 1, 1, m_data, len);
  549. sprintf(m_data, "hello-%d", 2);
  550. record_write( 1, 2, m_data, len);
  551. print_all_cmd();
  552. }
  553. void SaveReceiverMac(char* mac)
  554. {
  555. static uint8_t m_data[256];
  556. mac[12] = 0;
  557. sprintf(m_data, "%s", mac);
  558. NRF_LOG_RAW_INFO( "===> SaveReceiverMac : %s\n", m_data);
  559. record_write(1,1, m_data, 12);
  560. print_all_cmd();
  561. //FlashTest();
  562. }
  563. int FindRegMac(char* mac)
  564. {
  565. int i;
  566. for( i=0; i<RegMacPool.Count; i++)
  567. {
  568. if( strncmp( RegMacPool.Mac[i], mac, 12) == 0 )
  569. {
  570. NRF_LOG_RAW_INFO( "Valid Mac\n");
  571. return 1;
  572. }
  573. }
  574. return 0;
  575. }
  576. void DisplayRegMac()
  577. {
  578. print_all_cmd( );
  579. }
  580. void SetMacAddress(uint8_t* addr)
  581. {
  582. sd_ble_gap_addr_get(&old_ble_addr);
  583. ble_gap_addr_t dd;
  584. dd.addr_id_peer = 0;
  585. dd.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
  586. dd.addr[0] = addr[0];
  587. dd.addr[1] = addr[1];
  588. //dd.addr[2] = addr[2];
  589. dd.addr[2] = 0x57;
  590. dd.addr[3] = addr[3];
  591. dd.addr[4] = addr[4];
  592. dd.addr[5] = addr[5];
  593. sd_ble_gap_addr_set(&dd);
  594. sd_ble_gap_addr_get(&new_ble_addr);
  595. }
  596. HSKey Key[4];
  597. void KeyInit(void)
  598. {
  599. Key[KEY_SELECT].longPressTimeOne = 2;
  600. Key[KEY_UP].longPressTimeOne = 2;
  601. //Key[KEY_COUNTER].longPressTimeKeep = 4;
  602. }
  603. //50ms key polling
  604. void ReadKEY(void)
  605. {
  606. static int key_poll_count = 0;
  607. if( SystemTimer.TIM_KEY < 100 )
  608. return;
  609. SystemTimer.TIM_KEY = 0;
  610. if( Key[KEY_SELECT].softRelease == 1 )
  611. return;
  612. if( key_poll_count == 2 )
  613. {
  614. if( nrf_gpio_pin_read(BSP_BUTTON_1) == true )
  615. {
  616. if( SystemManager.EmgSendButton == 10 )
  617. {
  618. SystemManager.EmgSendButton = BUTTON_TEST;
  619. Key[KEY_SELECT].softRelease = 1;
  620. }
  621. }
  622. }
  623. key_poll_count++;
  624. if( nrf_gpio_pin_read(BSP_BUTTON_1) == false )
  625. {
  626. Key[KEY_SELECT].pressTime++;
  627. nrf_pwr_mgmt_feed();
  628. if( Key[KEY_SELECT].pressTime>20)
  629. {
  630. Key[KEY_SELECT].softRelease = 1;
  631. KeySelectLongPressedOne();
  632. }
  633. }
  634. }
  635. void KeySelectLongPressedOne(void)
  636. {
  637. NRF_LOG_INFO("KeySelectLongPressedOne\r\n" );
  638. SystemManager.EmgSendButton = BUTTON_REG;
  639. }
  640. void PortInputCfg(void)
  641. {
  642. #if 1
  643. uint32_t i;
  644. for (i = 17; i <= 20; ++i)
  645. {
  646. nrf_gpio_cfg_input(i, NRF_GPIO_PIN_PULLDOWN);
  647. //nrf_gpio_cfg_input(i, NRF_GPIO_PIN_PULLUP);
  648. }
  649. for (i = 12; i <= 15; ++i)
  650. {
  651. //nrf_gpio_cfg_input(i, NRF_GPIO_PIN_PULLDOWN);
  652. //nrf_gpio_cfg_input(i, NRF_GPIO_PIN_PULLUP);
  653. }
  654. nrf_gpio_cfg_input(7, NRF_GPIO_PIN_PULLDOWN);
  655. #endif
  656. }
  657. uint8_t AesBuffer[16];
  658. void CarEncryptEcb(int mode)
  659. {
  660. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  661. uint8_t in[] = {'C', 'A', 'R', 1,2,3,4,5,6,7,8,9,10,11,12,13 };
  662. uint16_t tempVal;
  663. if( mode == AES_ENC_CAR )
  664. {
  665. in[0] = 'C';
  666. in[1] = 'A';
  667. in[2] = 'R';
  668. }else if( mode == AES_ENC_EMG )
  669. {
  670. in[0] = 'E';
  671. in[1] = 'M';
  672. in[2] = 'G';
  673. }else if( mode == AES_ENC_WES_EMG )
  674. {
  675. in[0] = 'E';
  676. in[1] = 'M';
  677. in[2] = 'G';
  678. }
  679. tempVal = rand();
  680. in[3] = tempVal&0xff;
  681. in[4] = (tempVal>>8)&0xff;
  682. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  683. NRF_LOG_RAW_INFO("ECB encrypt: %x\n", tempVal);
  684. }
  685. void WesEncryptEcb(uint8_t* in)
  686. {
  687. int i;
  688. uint8_t* inOrg = in;
  689. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  690. //for( i=0; i<16; i++)
  691. {
  692. //*(in+i) = i+1;
  693. //in++;
  694. }
  695. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  696. memcpy( inOrg, SystemManager.AesEncData, 16 );
  697. }
  698. // 0x73:parking 0x72:emg
  699. void CarEncryptEcbACK(char type)
  700. {
  701. uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
  702. uint8_t in[] = {'C', 'A', 'R', 'A','C','K',1,2,3,4,5,6,7,8,9,0x0A };
  703. switch(type){
  704. // BAND EMG
  705. case 0x72:
  706. in[3] = 'E';
  707. in[4] = 'M';
  708. in[5] = 'G';
  709. break;
  710. // NEW iOS APP
  711. case 0x75:
  712. in[3] = 'P';
  713. in[4] = 'C';
  714. in[5] = 'A';
  715. break;
  716. }
  717. AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
  718. NRF_LOG_RAW_INFO("ECB ACK\n");
  719. }