1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069 |
- #include "sysmgr.h"
- #include <nrfx.h>
- #include <nrf_delay.h>
- #include <drv_rtc.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdarg.h>
- #include "nrf_spi.h"
- #include "nrf_drv_spi.h"
- #include "nrf_pwr_mgmt.h"
- #include "main.h"
- stSystemTimer SystemTimer;
- stSystemManager SystemManager;
- extern const nrf_drv_spi_t spi; /**< SPI instance. */
- extern volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
- uint8_t m_tx_buf[64]; /**< TX buffer. */
- uint8_t m_rx_buf[64]; /**< RX buffer. */
- uint8_t m_length; /**< Transfer length. */
- RegMacPool_t RegMacPool;
- const char *fds_err_str(ret_code_t ret)
- {
- /* Array to map FDS return values to strings. */
- static char const * err_str[] =
- {
- "FDS_ERR_OPERATION_TIMEOUT",
- "FDS_ERR_NOT_INITIALIZED",
- "FDS_ERR_UNALIGNED_ADDR",
- "FDS_ERR_INVALID_ARG",
- "FDS_ERR_NULL_ARG",
- "FDS_ERR_NO_OPEN_RECORDS",
- "FDS_ERR_NO_SPACE_IN_FLASH",
- "FDS_ERR_NO_SPACE_IN_QUEUES",
- "FDS_ERR_RECORD_TOO_LARGE",
- "FDS_ERR_NOT_FOUND",
- "FDS_ERR_NO_PAGES",
- "FDS_ERR_USER_LIMIT_REACHED",
- "FDS_ERR_CRC_CHECK_FAILED",
- "FDS_ERR_BUSY",
- "FDS_ERR_INTERNAL",
- };
- return err_str[ret - NRF_ERROR_FDS_ERR_BASE];
- }
- void ParkSysInit()
- {
- SystemTimer.TMR_SYS_OFF = 3;
- }
- void ParkPowerOff()
- {
- SystemTimer.TMR_SYS_OFF = 0;
- }
- void SC16IS750_FIFOEnable(unsigned char fifo_enable)
- {
- unsigned char temp_fcr;
- temp_fcr = SC16IS750_ReadRegister(SC16IS750_REG_FCR);
- if (fifo_enable == 0){
- temp_fcr &= 0xFE;
- } else {
- temp_fcr |= 0x01;
- }
- SC16IS750_WriteRegister(SC16IS750_REG_FCR,temp_fcr);
- return;
- }
- void SC16IS750_WriteRegister(unsigned char reg_addr, unsigned char val)
- {
- m_tx_buf[0] = reg_addr<<3;
- m_tx_buf[1] = val;
- //nrf_gpio_pin_write( SPI_SS_PIN,0);
- nrf_delay_us(10);
- nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 0);
- nrf_delay_us(10);
- //nrf_gpio_pin_write( SPI_SS_PIN,1);
- return ;
- }
- unsigned char SC16IS750_ReadRegister(unsigned char reg_addr)
- {
- unsigned char result;
- m_tx_buf[0] = 0x80|(reg_addr<<3);
- // nrf_gpio_pin_write( SPI_SS_PIN,0);
- nrf_delay_us(10);
- nrf_drv_spi_transfer(&spi, m_tx_buf, 1, m_rx_buf, 1);
- result = m_rx_buf[0];
- nrf_delay_us(10);
- // nrf_gpio_pin_write( SPI_SS_PIN,1);
- return result;
- }
- void SC16IS750_ResetDevice(void)
- {
- unsigned char reg;
- reg = SC16IS750_ReadRegister(SC16IS750_REG_IOCONTROL);
- reg |= 0x08;
- SC16IS750_WriteRegister(SC16IS750_REG_IOCONTROL, reg);
- return;
- }
- int16_t SC16IS750_SetBaudrate(uint32_t baudrate) //return error of baudrate parts per thousand
- {
- uint16_t divisor;
- uint8_t prescaler;
- uint32_t actual_baudrate;
- int16_t error;
- uint8_t temp_lcr;
- if ( (SC16IS750_ReadRegister(SC16IS750_REG_MCR)&0x80) == 0) { //if prescaler==1
- prescaler = 1;
- } else {
- prescaler = 4;
- }
- prescaler = 1;
- divisor = (SC16IS750_CRYSTCAL_FREQ/prescaler)/(baudrate*16);
- temp_lcr = SC16IS750_ReadRegister(SC16IS750_REG_LCR);
- temp_lcr |= 0x80;
- SC16IS750_WriteRegister(SC16IS750_REG_LCR,temp_lcr);
- //write to DLL
- SC16IS750_WriteRegister(SC16IS750_REG_DLL,(uint8_t)divisor);
- //write to DLH
- SC16IS750_WriteRegister(SC16IS750_REG_DLH,(uint8_t)(divisor>>8));
- temp_lcr &= 0x7F;
- SC16IS750_WriteRegister(SC16IS750_REG_LCR,temp_lcr);
- actual_baudrate = (SC16IS750_CRYSTCAL_FREQ/prescaler)/(16*divisor);
- error = ((float)actual_baudrate-baudrate)*1000/baudrate;
- return error;
- }
- void SC16IS750_SetLine(uint8_t data_length, uint8_t parity_select, uint8_t stop_length )
- {
- uint8_t temp_lcr;
- temp_lcr = SC16IS750_ReadRegister(SC16IS750_REG_LCR);
- temp_lcr &= 0xC0; //Clear the lower six bit of LCR (LCR[0] to LCR[5]
- switch (data_length) { //data length settings
- case 5:
- break;
- case 6:
- temp_lcr |= 0x01;
- break;
- case 7:
- temp_lcr |= 0x02;
- break;
- case 8:
- temp_lcr |= 0x03;
- break;
- default:
- temp_lcr |= 0x03;
- break;
- }
- if ( stop_length == 2 ) {
- temp_lcr |= 0x04;
- }
- switch (parity_select) { //parity selection length settings
- case 0: //no parity
- break;
- case 1: //odd parity
- temp_lcr |= 0x08;
- break;
- case 2: //even parity
- temp_lcr |= 0x18;
- break;
- case 3: //force '1' parity
- temp_lcr |= 0x03;
- break;
- case 4: //force '0' parity
- break;
- default:
- break;
- }
- SC16IS750_WriteRegister(SC16IS750_REG_LCR,temp_lcr);
- }
- void SC16IS750_WriteByte(uint8_t val)
- {
- uint8_t tmp_lsr;
- do {
- tmp_lsr = SC16IS750_ReadRegister(SC16IS750_REG_LSR);
- } while ((tmp_lsr&0x20) ==0);
- //nrf_delay_ms(1);
- SC16IS750_WriteRegister(SC16IS750_REG_THR,val);
- }
- int DBGPrint(const char *fmt, ...)
- {
- char buff[128];
- va_list args;
- int n;
- int i;
- va_start(args, fmt);
- n = vsnprintf(buff, 120, fmt, args);
- va_end(args);
- //HAL_UART_Transmit(CLIUart, (uint8_t*)buff, n, 500);
- for( i=0; i<n; i++)
- {
-
- SC16IS750_WriteByte(buff[i]);
- }
-
- return n;
- }
- /* Dummy configuration data. */
- static configuration_t m_dummy_cfg =
- {
- .config1_on = false,
- .config2_on = true,
- .boot_count = 0x0,
- .device_name = "dummy",
- };
- static void record_write(uint32_t fid,
- uint32_t key,
- void const * p_data,
- uint32_t len)
- {
- fds_record_t const rec =
- {
- .file_id = fid,
- .key = key,
- .data.p_data = p_data,
- .data.length_words = (len + 3) / sizeof(uint32_t)
- };
- NRF_LOG_INFO(
- "writing record to flash...\n"
- "file: 0x%x, key: 0x%x, \"%s\", len: %u bytes\n",
- fid, key, p_data, len);
- fds_record_desc_t desc = {0};
- desc.record_id = 1;
- //ret_code_t rc = fds_record_write(NULL, &rec);
- ret_code_t rc = fds_record_write(&desc, &rec);
- if (rc != NRF_SUCCESS)
- {
- NRF_LOG_INFO(
- "error: fds_record_write() returned %s.\n",
- fds_err_str(rc));
- }
- }
- static void record_update( configuration_t const * p_cfg)
- {
- fds_record_desc_t desc = {0};
- fds_find_token_t ftok = {0};
- if (fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &ftok) == NRF_SUCCESS)
- {
- fds_record_t const rec =
- {
- .file_id = CONFIG_FILE,
- .key = CONFIG_REC_KEY,
- .data.p_data = p_cfg,
- .data.length_words = (sizeof(configuration_t) + 3) / sizeof(uint32_t)
- };
- ret_code_t rc = fds_record_update(&desc, &rec);
- if (rc != NRF_SUCCESS)
- {
- NRF_LOG_INFO( "error: fds_record_update() returned %s.\n",
- fds_err_str(rc));
- }
- }
- else
- {
- NRF_LOG_INFO( "error: could not find config file.\n");
- }
- }
- static void record_delete( uint32_t fid, uint32_t key)
- {
- fds_find_token_t tok = {0};
- fds_record_desc_t desc = {0};
- NRF_LOG_INFO(
- "deleting record...\n"
- "file: 0x%x, key: 0x%x\n",
- fid,
- key);
- if (fds_record_find(fid, key, &desc, &tok) == NRF_SUCCESS)
- {
- ret_code_t rc = fds_record_delete(&desc);
- if (rc != NRF_SUCCESS)
- {
- NRF_LOG_INFO(
- "error: fds_record_delete() returned %s.\n", fds_err_str(rc));
- return;
- }
- NRF_LOG_INFO( "record id: 0x%x\n", desc.record_id);
- }
- else
- {
- NRF_LOG_INFO( "error: record not found!\n");
- }
- }
- bool record_delete_next(void)
- {
- fds_find_token_t tok = {0};
- fds_record_desc_t desc = {0};
- if (fds_record_iterate(&desc, &tok) == NRF_SUCCESS)
- {
- ret_code_t rc = fds_record_delete(&desc);
- if (rc != NRF_SUCCESS)
- {
- return false;
- }
- return true;
- }
- else
- {
- /* No records left to delete. */
- return false;
- }
- }
- static void print_cfg_cmd( size_t argc, char ** argv)
- {
- fds_record_desc_t desc = {0};
- fds_find_token_t tok = {0};
- while (fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok) == NRF_SUCCESS)
- {
- ret_code_t rc;
- fds_flash_record_t frec = {0};
- rc = fds_record_open(&desc, &frec);
- switch (rc)
- {
- case NRF_SUCCESS:
- break;
- case FDS_ERR_CRC_CHECK_FAILED:
- NRF_LOG_INFO( "error: CRC check failed!\n");
- continue;
- case FDS_ERR_NOT_FOUND:
- NRF_LOG_INFO( "error: record not found!\n");
- continue;
- default:
- {
- NRF_LOG_INFO(
- "error: unexpecte error %s.\n",
- fds_err_str(rc));
- continue;
- }
- }
- configuration_t * p_cfg = (configuration_t *)(frec.p_data);
- NRF_LOG_INFO(
- "config1:\t%s\n"
- "config2:\t%s\n"
- "boot count:\t%u\n"
- "device name:\t%s\n",
- p_cfg->config1_on ? "on" : "off",
- p_cfg->config2_on ? "on" : "off",
- p_cfg->boot_count,
- p_cfg->device_name);
- rc = fds_record_close(&desc);
- APP_ERROR_CHECK(rc);
- }
- }
- static void print_all_cmd( )
- {
- fds_find_token_t tok = {0};
- fds_record_desc_t desc = {0};
- uint8_t *data;
- NRF_LOG_INFO("rec. id \t file id \t rec. key \t length");
- while (fds_record_iterate(&desc, &tok) != FDS_ERR_NOT_FOUND)
- {
- ret_code_t rc;
- fds_flash_record_t frec = {0};
- rc = fds_record_open(&desc, &frec);
- switch (rc)
- {
- case NRF_SUCCESS:
- break;
- case FDS_ERR_CRC_CHECK_FAILED:
- NRF_LOG_INFO( "error: CRC check failed!\n");
- continue;
- case FDS_ERR_NOT_FOUND:
- NRF_LOG_INFO( "error: record not found!\n");
- continue;
- default:
- {
- NRF_LOG_INFO("error: unexpecte error %s.\n", fds_err_str(rc));
- continue;
- }
- }
- uint32_t const len = frec.p_header->length_words * sizeof(uint32_t);
- NRF_LOG_INFO( " 0x%04x"
- "\t 0x%04x"
- "\t 0x%04x"
- "\t %4u bytes\t",
- frec.p_header->record_id,
- frec.p_header->file_id,
- frec.p_header->record_key,
- len);
- data = (uint8_t *) frec.p_data;
- for (uint8_t i=0;i<len;i++)
- {
- NRF_LOG_RAW_INFO( "%c",data[i]);
- }
- NRF_LOG_INFO("\n");
- rc = fds_record_close(&desc);
- APP_ERROR_CHECK(rc);
- }
- }
- void RegMac(char* str)
- {
- print_all_cmd();
- NRF_LOG_RAW_INFO("Registration MAC : %s\n", str);
-
- RegMacPool.Count++;
- record_write(1, RegMacPool.Count, str, 12);
- print_all_cmd();
- }
- void RegMacDelteAll()
- {
- bool next;
- #if 1
- while(1)
- {
- next = record_delete_next();
- if (!next)
- {
- NRF_LOG_INFO("No records left to delete.");
- break;
- }
- nrf_delay_ms(20);
- }
- #endif
- memset( &RegMacPool, 0, sizeof(RegMacPool));
- }
- void LoadRegMac()
- {
- fds_find_token_t tok = {0};
- fds_record_desc_t desc = {0};
- uint8_t *data;
- char tmpBuf[15];
- NRF_LOG_INFO(
- "\nrec. id\t"
- "\tfile id\t"
- "\trec. key"
- "\tlength\tmac\n");
- while (fds_record_iterate(&desc, &tok) != FDS_ERR_NOT_FOUND)
- {
- ret_code_t rc;
- fds_flash_record_t frec = {0};
- rc = fds_record_open(&desc, &frec);
-
- switch (rc)
- {
- case NRF_SUCCESS:
- break;
- case FDS_ERR_CRC_CHECK_FAILED:
- NRF_LOG_INFO("error: CRC check failed!\n");
- continue;
- case FDS_ERR_NOT_FOUND:
- NRF_LOG_INFO("error: record not found!\n");
- continue;
- default:
- {
- NRF_LOG_INFO("error: unexpecte error %s.\n", fds_err_str(rc));
- continue;
- }
- }
- uint32_t const len = frec.p_header->length_words * sizeof(uint32_t);
- NRF_LOG_RAW_INFO(
- " 0x%04x\t"
- "\t 0x%04x\t"
- "\t 0x%04x\t"
- "\t %4u bytes\t",
- frec.p_header->record_id,
- frec.p_header->file_id,
- frec.p_header->record_key,
- len);
- data = (uint8_t *) frec.p_data;
- if( RegMacPool.Count >= 5 )
- {
- NRF_LOG_RAW_INFO( "Mac Pool Over\n");
- return;
- }
-
- RegMacPool.Mac[RegMacPool.Count][12] = 0;
- strncpy( RegMacPool.Mac[RegMacPool.Count++], data, 12);
- //for (uint8_t i=0;i<len;i++)
- {
- // NRF_LOG_RAW_INFO( "%c",data[i]);
- }
- //NRF_LOG_RAW_INFO( "\n");
- NRF_LOG_RAW_INFO( "%s\n",RegMacPool.Mac[RegMacPool.Count-1]);
- rc = fds_record_close(&desc);
- APP_ERROR_CHECK(rc);
- }
- }
- void LoadReceiverMac()
- {
- fds_find_token_t tok = {0};
- fds_record_desc_t desc = {0};
- uint8_t *data;
- char tmpBuf[15];
- NRF_LOG_INFO(
- "\nrec. id\t"
- "\tfile id\t"
- "\trec. key"
- "\tlength\tmac\n");
- while (fds_record_iterate(&desc, &tok) != FDS_ERR_NOT_FOUND)
- {
- ret_code_t rc;
- fds_flash_record_t frec = {0};
- rc = fds_record_open(&desc, &frec);
-
- switch (rc)
- {
- case NRF_SUCCESS:
- break;
- case FDS_ERR_CRC_CHECK_FAILED:
- NRF_LOG_INFO("error: CRC check failed!\n");
- continue;
- case FDS_ERR_NOT_FOUND:
- NRF_LOG_INFO("error: record not found!\n");
- continue;
- default:
- {
- NRF_LOG_INFO("error: unexpecte error %s.\n", fds_err_str(rc));
- continue;
- }
- }
- uint32_t const len = frec.p_header->length_words * sizeof(uint32_t);
- NRF_LOG_RAW_INFO(
- " 0x%04x\t"
- "\t 0x%04x\t"
- "\t 0x%04x\t"
- "\t %4u bytes\t",
- frec.p_header->record_id,
- frec.p_header->file_id,
- frec.p_header->record_key,
- len);
- data = (uint8_t *) frec.p_data;
- strncpy( SystemManager.StrRegPeerAddr, data, 12);
- strncpy( tmpBuf, SystemManager.StrRegPeerAddr, 12);
-
- SystemManager.StrRegPeerAddr[12] = 0;
- tmpBuf[12] = 0;
- NRF_LOG_RAW_INFO( "%s\n",SystemManager.StrRegPeerAddr);
- SystemManager.RegPeerAddr[5] = (uint8_t)strtol(&tmpBuf[10], NULL, 16); tmpBuf[10] = 0;
- SystemManager.RegPeerAddr[4] = (uint8_t)strtol(&tmpBuf[8], NULL, 16); tmpBuf[8] = 0;
- SystemManager.RegPeerAddr[3] = (uint8_t)strtol(&tmpBuf[6], NULL, 16); tmpBuf[6] = 0;
- SystemManager.RegPeerAddr[2] = (uint8_t)strtol(&tmpBuf[4], NULL, 16); tmpBuf[4] = 0;
- SystemManager.RegPeerAddr[1] = (uint8_t)strtol(&tmpBuf[2], NULL, 16); tmpBuf[2] = 0;
- SystemManager.RegPeerAddr[0] = (uint8_t)strtol(&tmpBuf[0], NULL, 16);
-
- rc = fds_record_close(&desc);
- APP_ERROR_CHECK(rc);
- }
- }
- void SaveRegMac(char* mac)
- {
- int i;
- if( RegMacPool.Count>= 5 )
- return;
-
- for( i=0; i<RegMacPool.Count; i++)
- {
- if( strncmp( RegMacPool.Mac[i], mac, 12) == 0 )
- {
- NRF_LOG_RAW_INFO( "Allready Registrated\n");
- return;
- }
-
- }
-
-
- RegMacPool.Mac[RegMacPool.Count][12] = 0;
- strncpy( RegMacPool.Mac[RegMacPool.Count], mac, 12);
- record_write(1, RegMacPool.Count+1, mac, 12);
- RegMacPool.Count++;
- }
- void FlashTest()
- {
- static uint8_t m_data[256];
- int len;
- sprintf(m_data, "hello-%d", 1);
- len = strlen(m_data);
-
- record_write( 1, 1, m_data, len);
- sprintf(m_data, "hello-%d", 2);
- record_write( 1, 2, m_data, len);
- print_all_cmd();
- }
- void SaveReceiverMac(char* mac)
- {
- static uint8_t m_data[256];
- mac[12] = 0;
- sprintf(m_data, "%s", mac);
-
- NRF_LOG_RAW_INFO( "===> SaveReceiverMac : %s\n", m_data);
-
- record_write(1,1, m_data, 12);
- print_all_cmd();
- //FlashTest();
- }
- int FindRegMac(char* mac)
- {
- int i;
-
- for( i=0; i<RegMacPool.Count; i++)
- {
- if( strncmp( RegMacPool.Mac[i], mac, 12) == 0 )
- {
- NRF_LOG_RAW_INFO( "Valid Mac\n");
- return 1;
- }
- }
-
- return 0;
- }
- void DisplayRegMac()
- {
- print_all_cmd( );
- }
- void SetMacAddress(uint8_t* addr)
- {
- sd_ble_gap_addr_get(&old_ble_addr);
- ble_gap_addr_t dd;
- dd.addr_id_peer = 0;
- dd.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
- dd.addr[0] = addr[0];
- dd.addr[1] = addr[1];
- //dd.addr[2] = addr[2];
- dd.addr[2] = 0x57;
- dd.addr[3] = addr[3];
- dd.addr[4] = addr[4];
- dd.addr[5] = addr[5];
- sd_ble_gap_addr_set(&dd);
-
- sd_ble_gap_addr_get(&new_ble_addr);
-
- }
- HSKey Key[4];
- void KeyInit(void)
- {
- Key[KEY_SELECT].longPressTimeOne = 2;
- Key[KEY_UP].longPressTimeOne = 2;
- //Key[KEY_COUNTER].longPressTimeKeep = 4;
- }
- //50ms key polling
- void ReadKEY(void)
- {
- static int key_poll_count = 0;
- if( SystemTimer.TIM_KEY < 100 )
- return;
- SystemTimer.TIM_KEY = 0;
-
- if( Key[KEY_SELECT].softRelease == 1 )
- return;
- if( key_poll_count == 2 )
- {
- if( nrf_gpio_pin_read(BSP_BUTTON_1) == true )
- {
- if( SystemManager.EmgSendButton == 10 )
- {
- SystemManager.EmgSendButton = BUTTON_TEST;
- Key[KEY_SELECT].softRelease = 1;
- }
-
- }
- }
- key_poll_count++;
- if( nrf_gpio_pin_read(BSP_BUTTON_1) == false )
- {
- Key[KEY_SELECT].pressTime++;
- nrf_pwr_mgmt_feed();
- if( Key[KEY_SELECT].pressTime>20)
- {
- Key[KEY_SELECT].softRelease = 1;
- KeySelectLongPressedOne();
- }
- }
- }
- void KeySelectLongPressedOne(void)
- {
- NRF_LOG_INFO("KeySelectLongPressedOne\r\n" );
- SystemManager.EmgSendButton = BUTTON_REG;
- }
- void PortInputCfg(void)
- {
- #if 1
- uint32_t i;
- for (i = 17; i <= 20; ++i)
- {
- nrf_gpio_cfg_input(i, NRF_GPIO_PIN_PULLDOWN);
- //nrf_gpio_cfg_input(i, NRF_GPIO_PIN_PULLUP);
- }
- for (i = 12; i <= 15; ++i)
- {
- //nrf_gpio_cfg_input(i, NRF_GPIO_PIN_PULLDOWN);
- //nrf_gpio_cfg_input(i, NRF_GPIO_PIN_PULLUP);
- }
- nrf_gpio_cfg_input(7, NRF_GPIO_PIN_PULLDOWN);
- #endif
- }
- uint8_t AesBuffer[16];
- void CarEncryptEcb(int mode)
- {
- uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
- uint8_t in[] = {'C', 'A', 'R', 1,2,3,4,5,6,7,8,9,10,11,12,13 };
- uint16_t tempVal;
- if( mode == AES_ENC_CAR )
- {
- in[0] = 'C';
- in[1] = 'A';
- in[2] = 'R';
- }else if( mode == AES_ENC_EMG )
- {
- in[0] = 'E';
- in[1] = 'M';
- in[2] = 'G';
- }else if( mode == AES_ENC_WES_EMG )
- {
- in[0] = 'E';
- in[1] = 'M';
- in[2] = 'G';
- }
-
- tempVal = rand();
- in[3] = tempVal&0xff;
- in[4] = (tempVal>>8)&0xff;
- AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
- NRF_LOG_RAW_INFO("ECB encrypt: %x\n", tempVal);
- }
- void WesEncryptEcb(uint8_t* in)
- {
- int i;
- uint8_t* inOrg = in;
- uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
-
- //for( i=0; i<16; i++)
- {
- //*(in+i) = i+1;
- //in++;
- }
-
- AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
- memcpy( inOrg, SystemManager.AesEncData, 16 );
- }
- // 0x73:parking 0x72:emg
- void CarEncryptEcbACK(char type)
- {
- uint8_t key[] = {0x53, 0x6D, 0x61, 0x72, 0x74, 0x42, 0x61, 0x6E, 0x64, 0x50, 0x41, 0x52, 0x54, 0x52, 0x4F, 0x4E}; // SmartBandPARTRON
- uint8_t in[] = {'C', 'A', 'R', 'A','C','K',1,2,3,4,5,6,7,8,9,0x0A };
- switch(type){
- // BAND EMG
- case 0x72:
- in[3] = 'E';
- in[4] = 'M';
- in[5] = 'G';
- break;
- // NEW iOS APP
- case 0x75:
- in[3] = 'P';
- in[4] = 'C';
- in[5] = 'A';
- break;
- }
- AES128_ECB_encrypt(in, key, SystemManager.AesEncData);
- NRF_LOG_RAW_INFO("ECB ACK\n");
- }
|