Files
firmware-test/project/ble_peripheral/ble_app_bladder_patch/cmd_parse.c
jh.Chun 590922638c Add new parser commands and FDS reliability fix
- HW Number Read/Write (mwh?, mrh?)
- Serial Number Read/Write (mws?, mrs?)
- FW Version Read (mfv?)
- Piezo TX/RX Deactivate (mpb?)
- Fix config_save() to wait for previous FDS operation instead of skipping
- Disable legacy s-prefix commands (ssz, srz, siz, shz, ssv) in cmd_parse.c

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 15:05:58 +09:00

1166 lines
30 KiB
C
Raw Blame History

/***********************************************
EEPROM ADDRESS
HW_NO[12]
SERIAL_NO[12]
v1.17 p1,p2 parssurer
***********************************************/
#include <cmd_parse.h>
#include "debug_print.h"
#include "fstorage.h"
#define DEVICE_VERSION "FW25LIT2B102"
#define DEVICE_NAME "MEDIDEV_2004"
#define err_code1 65535 //length
#define err_code2 65534 //activate
#define err_code3 65533 //param
#define err_code4 65532 //? missing
#define err_code5 65531 //CMD wrong
#define err_code6 65530 //CRC wrong
#define SERIAL_NO_LENGTH 12
#include "i2c_manager.h"
#include "app_timer.h"
#include "parser.h"
// 0x0060 bonding address.....
extern uint8_t m_reset_status;
char SERIAL_NO[12]; //for eeprom r/write 30
bool bond_data_delete; //for eeprom r/write 50
char m_static_passkey[6] = "123456";//uint8_t static_passkey[6]; eeprom r/write 20
uint32_t m_life_cycle; //for eeprom r/write 90
uint8_t resetCount = 0; //cj add
char HW_NO[12]; //for eeprom r/write 10
bool info4;
extern bool motion_raw_data_enabled;
extern char ble_tx_buffer[BLE_NUS_MAX_DATA_LEN];
extern bool device_status; /* true : Device has been activated, false : Device entered sleep mode */
extern bool device_reset;
//static uint32_t processing_start_time=0;
static uint32_t processing_start_tick = 0;
//ble_status_t ble_connection_st = BLE_DISCONNECTED_ST;
extern volatile bool ble_connection_st;
extern volatile bool data_tx_in_progress;
uint8_t ble_bin_buffer[BLE_NUS_MAX_DATA_LEN] = {0};
extern bool go_device_power_off;
extern bool go_sleep_mode_enter;
extern bool go_NVIC_SystemReset;
extern bool go_temp;
extern bool go_batt;
extern bool ble_got_new_data;
extern bool motion_data_once;
extern dr_platform_if_t g_plat;
extern bool g_log_enable;
extern int dr_cmd_parser(const uint8_t *buf, uint8_t len);
uint16_t imsi_value;
ParsedCmd scmd;
/**@brief Function for handling app_uart events.
*
* @details This function will receive a single character from the app_uart module and append it to
* a string. The string will be be sent over BLE when the last character received was a
* 'new line' '\n' (hex 0x0A) or if the string has reached the maximum data length.
*/
/**@snippet [Handling the data received over UART & BLE] */
bool is_valid_serial_no(const char *serial)
{
// Check for all same characters (e.g., all 'F' or all '0')
bool all_same = true;
for (int i = 1; i < SERIAL_NO_LENGTH; i++) {
if (serial[i] != serial[0]) {
all_same = false;
break;
}
}
if (all_same) {
return false; // reject "FFFFFFFF..." or "00000000..."
}
// Check all characters are alphanumeric
for (int i = 0; i < SERIAL_NO_LENGTH; i++) {
char c = serial[i];
if (c == '\0' || c == (char)0xFF || !isalnum((unsigned char)c)) {
return false;
}
}
return true;
}
bool is_valid_passkey(const char *passkey)
{
for(uint8_t i = 0 ; i<6 ;i++)
{
DBG_PRINTF("check passkey :%2X \r\n",passkey[i]);
}
// Check for NULL pointer
if (passkey == NULL) {
return false;
}
// Check that the length is exactly 6 and all characters are digits
for (int i = 0; i < 6; i++) {
if (passkey[i] == '\0' || !isdigit((unsigned char)passkey[i])) {
return false;
}
}
// Ensure the 7th character is null terminator
// if (passkey[6] != '\0') {
// return false;
// }
return true;
}
bool crc16_check(uint8_t const * p_data, uint32_t data_len, uint16_t expected_crc)
{
uint16_t computed_crc = crc16_compute(p_data, data_len, NULL);
return (computed_crc == expected_crc);
}
bool crc16_check_packet(uint8_t const * packet, uint32_t packet_len)
{
if (packet_len < 2) return false;
uint32_t data_len = packet_len - 2;
uint16_t expected_crc = (packet[packet_len - 1] << 8) | packet[packet_len - 2]; // Big endian
//DBG_PRINTF("\r\n expecterrrd_data_delete :%02X \n", expected_crc);
return crc16_check(packet, data_len, expected_crc);
}
bool parse_cmd(const uint8_t *buffer, ParsedCmd *cmd_out, uint8_t length) {
// Extract 4-character command
for (int i = 0; i < 4; i++) {
cmd_out->tag[i] = (char)buffer[i];
}
cmd_out->tag[4] = '\0'; // Null-terminate
// Extract 16-bit value (little-endian)
cmd_out->value0 = (uint16_t)buffer[5] | ((uint16_t)buffer[4] << 8);
cmd_out->value1 = (uint16_t)buffer[7] | ((uint16_t)buffer[6] << 8);
cmd_out->value2 = (uint16_t)buffer[9] | ((uint16_t)buffer[8] << 8);
cmd_out->value3 = (uint16_t)buffer[11] | ((uint16_t)buffer[10] << 8);
cmd_out->value4 = (uint16_t)buffer[13] | ((uint16_t)buffer[12] << 8);
for (int i = 0; i < length; i++) {
cmd_out->values[i] = buffer[i];
}
for (int i = 0; i < 12; i++) {
cmd_out->value_ascii[i] = (char)buffer[i+4];
}
cmd_out->value_ascii[12]= '\0'; // Null-terminate
return true; // You could return false on validation failure
}
bool length_error(const char *cmd , uint8_t target_length, uint8_t length)
{
if (target_length == length)
{
return true;
}
else
{
char resp_error[4];
resp_error[0] = 'r';
resp_error[1] = cmd[1]; // 2nd letter (index 1)
resp_error[2] = cmd[2]; // 3rd letter (index 2)
resp_error[3] = '!';
single_format_data(ble_bin_buffer, resp_error, err_code1);
binary_tx_handler(ble_bin_buffer,3);
return false;
}
}
bool activate_error(const char *cmd , bool device_status)
{
if (device_status == true)
{
return true;
}
else
{
char resp_error[4];
resp_error[0] = 'r';
resp_error[1] = cmd[1]; // 2nd letter (index 1)
resp_error[2] = cmd[2]; // 3rd letter (index 2)
resp_error[3] = '!';
single_format_data(ble_bin_buffer, resp_error, err_code2);
binary_tx_handler(ble_bin_buffer,3);
return false;
}
}
void param_error(const char *cmd )
{
char resp_error[4];
resp_error[0] = 'r';
resp_error[1] = cmd[1]; // 2nd letter (index 1)
resp_error[2] = cmd[2]; // 3rd letter (index 2)
resp_error[3] = '!';
single_format_data(ble_bin_buffer, resp_error, err_code3);
binary_tx_handler(ble_bin_buffer,3);
}
void quest_error(const char *cmd )
{
char resp_error[4];
const char pass_init[6] = "123456";
if( (cmd[0] == '*') && (cmd[1] == '*') && (cmd[2] == '*') && (cmd[3] == '*')){
if(eeprom_write_encrypted(0x0020, (uint8_t *)pass_init, 6)!= NRF_SUCCESS)
{
DBG_PRINTF("ERR!!! EEP_passkey 6\r\n\r\n");;
}
resp_error[0] = '*';
resp_error[1] = cmd[1]; // 2nd letter (index 1)
resp_error[2] = cmd[2]; // 3rd letter (index 2)
resp_error[3] = '*';
single_format_data(ble_bin_buffer, resp_error, err_code4);
binary_tx_handler(ble_bin_buffer,3);
}
else{
resp_error[0] = 'r';
resp_error[1] = cmd[1]; // 2nd letter (index 1)
resp_error[2] = cmd[2]; // 3rd letter (index 2)
resp_error[3] = '!';
single_format_data(ble_bin_buffer, resp_error, err_code4);
binary_tx_handler(ble_bin_buffer,3);
}
}
//uint32_t serial_to_passkey_hash(const char *input)
//{
// uint32_t hash = 1026;
// while (*input)
// {
// hash = ((hash << 5) + hash) + (uint8_t)(*input++); // hash * 33 + c
// }
// return hash % 1000000; // 6-digit range
//}
//void test_eeprom_page_rw(void)
//{
// uint8_t tx_data[EEPROM_PAGE_SIZE];
// uint8_t rx_data[EEPROM_PAGE_SIZE];
// // Fill tx_data with example values
// for (int i = 0; i < EEPROM_PAGE_SIZE; i++) {
// tx_data[i] = i*10;
// }
// uint16_t test_address = 0x0000;
// if (eeprom_write_page(test_address, tx_data) == NRF_SUCCESS) {
// DBG_PRINTF("Write OK\r\n");
// }
// if (eeprom_read_page(test_address, rx_data) == NRF_SUCCESS) {
// DBG_PRINTF("Read OK\n");
// }
// // Verify
// for (int i = 0; i < EEPROM_PAGE_SIZE; i++) {
// if (rx_data[i] != tx_data[i]) {
// DBG_PRINTF("Mismatch at %d: wrote %02X, read %02X\n", i, tx_data[i], rx_data[i]);
// }
// }
//}
ret_code_t eeprom_read_bool(uint16_t mem_address, bool *value_out)
{
uint8_t raw;
ret_code_t ret = eeprom_read_byte(mem_address, &raw);
if (ret != NRF_SUCCESS) {
return ret;
}
*value_out = (raw != 0);
return NRF_SUCCESS;
}
ret_code_t eeprom_init_values_read(void)
{
ret_code_t err_code;
//uint8_t *data_bond;
// Read 11 bytes from EEPROM
err_code = eeprom_read_decrypted(0x0030, (uint8_t *)SERIAL_NO, 12);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
else
{
DBG_PRINTF("\r\n SN:%s \r\n", SERIAL_NO);
}
err_code = eeprom_read_decrypted(0x0020, (uint8_t *)m_static_passkey, 6);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
else
{
DBG_PRINTF("\r\n passkey-0 :%s \n", m_static_passkey);
}
//err_code = eeprom_read_bytes(0x0060, data_bond,16);
err_code = eeprom_read_bool(0x0060, &bond_data_delete);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
else
{
// bond_data_delete = data_bond[0];
DBG_PRINTF("\r\n bond_data_delete :%d \n", bond_data_delete);
}
err_code = eeprom_read_byte(0x0065, &m_reset_status);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
else
{
if(m_reset_status){
//DBG_PRINTF("\r\n status :%d \n", m_reset_status);
}
}
DBG_PRINTF("m_life_cycle eprom read\n"); //ad cj
err_code = eeprom_read_uint32(0x0090, &m_life_cycle);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
else
{
if(m_life_cycle){
DBG_PRINTF("\r\n m_life_cycle :%1u \r\n", m_life_cycle);
}
}
return NRF_SUCCESS;
}
static void log_printf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
void received_command_process (uint8_t const *data_array, which_cmd_t cmd_t, uint8_t length )
{
uint8_t r_data[BLE_NUS_MAX_DATA_LEN]={0,};
uint16_t result_data[BLE_NUS_MAX_DATA_LEN];
int parser_result; // ? ??
ble_got_new_data = true;
memset( ble_tx_buffer, 0, BLE_NUS_MAX_DATA_LEN);
for( uint16_t i = 0; i < length ; i++ ) {
r_data[i] = data_array[i];
}
DBG_PRINTF("data : %s\r\n",r_data);
DBG_PRINTF("Length : %d\r\n",length);
// ========================================
// Initialize parser (once)
// ========================================
static bool parser_initialized = false;
if (!parser_initialized) {
// Setup platform interface
g_plat.log = log_printf; // printf
g_plat.tx_bin = binary_tx_handler;
g_plat.crc_check = true; // CRC
g_log_enable = true;
parser_initialized = true;
DBG_PRINTF(">>> NEW parser initialized\r\n");
}
// ========================================
// NEW PARSER: Try first
// ========================================
parser_result = dr_cmd_parser(r_data, length);
if (parser_result > 0) {
// Success - handled by new parser
DBG_PRINTF(">>> Handled by NEW parser\r\n");
return;
}
//
parse_cmd(r_data, &scmd,length);
DBG_PRINTF("parsed.tag ? %s,%d,%d,%d\r\n",scmd.tag,scmd.value0,scmd.value1,scmd.value2);
if(scmd.tag[3] != '?' )
{
quest_error(scmd.tag );
return;
}
else if(!crc16_check_packet(data_array , length)){
char resp_error[4];
resp_error[0] = 'c';
resp_error[1] = 'r'; // 2nd letter (index 1)
resp_error[2] = 'c'; // 3rd letter (index 2)
resp_error[3] = '!';
single_format_data(ble_bin_buffer, resp_error, err_code6);
binary_tx_handler(ble_bin_buffer,3);
return ;
}
else if(processing == true)
{
// DBG_PRINTF("mmm....\r\n");
uint32_t now = app_timer_cnt_get();
if (processing_start_tick == 0)
{
processing_start_tick = now;
}
//
if (app_timer_cnt_diff_compute(now, processing_start_tick) > APP_TIMER_TICKS(5000)) //add cj
{
processing = false;
processing_start_tick = 0;
DBG_PRINTF("processing timeout -> force reset to false\r\n");
}
else
{
DBG_PRINTF("mmm....\r\n");
}
return;
}
else
{
#if 0 //spas
// if((data_array[0] == 'p')&&(data_array[1] == 'a')&&(data_array[2] == 's')&&(data_array[3] == 's')&&(data_array[4] == 'k')&&(data_array[5] == 'e')&&(data_array[6] == 'y')) { // Write, Passkey
//
// if(data_array[7] == '='){
//
//
//
// if ((data_array[8] >=0x30 && data_array[8] <=0x39) //only number is available
// &&(data_array[9] >=0x30 && data_array[9] <=0x39)
// &&(data_array[10] >=0x30 && data_array[10] <=0x39)
// &&(data_array[11] >=0x30 && data_array[11] <=0x39)
// &&(data_array[12] >=0x30 && data_array[12] <=0x39)
// &&(data_array[13] >=0x30 && data_array[13] <=0x39))
//
// {
// memcpy(m_config.static_passkey, data_array+8, 6);
//
// DBG_PRINTF("m_config.static_passkey = %s\r\n", m_config.static_passkey);
//
// if(cmd_t == CMD_UART) {
// DBG_PRINTF("Passkey Ok\r\n");
// } else if(cmd_t == CMD_BLE) {
// sprintf(ble_tx_buffer, "Passkey Ok\r\n");
// data_tx_handler(ble_tx_buffer);
// }
// }
// else{ DBG_PRINTF("Nondigit!! \r\n");
//
// }
//
// }else if(data_array[7] == '?'){
// if(cmd_t == CMD_UART) {
// DBG_PRINTF("Passkey: %s\r\n", m_config.static_passkey);
// } else if(cmd_t == CMD_BLE) {
// sprintf(ble_tx_buffer, "Passkey: %s\r\n", m_config.static_passkey);
// data_tx_handler(ble_tx_buffer);
// }
// }else {
// if(cmd_t == CMD_UART) {
// DBG_PRINTF("ERR!!! Passkey failed!\r\n\r\n");
// } else if(cmd_t == CMD_BLE) {
// sprintf(ble_tx_buffer, "ERR!!! Passkey failed!\r\n\r\n");
// data_tx_handler(ble_tx_buffer);
// }
// }
// } else
//if((data_array[0] == 's')&&(data_array[1] == 't')&&(data_array[2] == 'a')&&(data_array[3] == '?')) {
#endif //sta sta `
// sta
if((scmd.tag[0] == 's')&&(scmd.tag[1] == 't')&&(scmd.tag[2] == 'a')&&(scmd.tag[3] == '?')) {
//sta sta sta sta
resetCount=0; //reset hear cj chun
if(length_error(scmd.tag,8,length)==false)
{
return;
}
if(scmd.value0 == 1) {
if(device_activated() == 0) {
device_status = true;
}
}else if(scmd.value0 == 0) {
if(device_status == true) {
if(device_sleep_mode() == 0) {
device_status = false;
}
}
}else if(data_array[6] == '?') {
/* Nothing to do */
}else {
if(cmd_t == CMD_UART) {
DBG_PRINTF("ERR!!! Status failed!\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
param_error(scmd.tag );
}
}
if(cmd_t == CMD_UART) {
DBG_PRINTF("Return%d\r\n\r\n", device_status);
} else if(cmd_t == CMD_BLE) {
single_format_data(ble_bin_buffer, "rta:", scmd.value0);
binary_tx_handler(ble_bin_buffer,3);
}
}
// str
else if((scmd.tag[0] == 's')&&(scmd.tag[1] == 't')&&(scmd.tag[2] == 'r')&&(scmd.tag[3] == '?')) {
if(length_error(scmd.tag,6,length)==false)
{
return;
}
// else {
// if(cmd_t == CMD_UART) {
// DBG_PRINTF("ERR!!! Status failed!\r\n\r\n");
// } else if(cmd_t == CMD_BLE) {
// param_error(scmd.tag );
// }
// }
if(cmd_t == CMD_UART) {
DBG_PRINTF("Return%d\r\n\r\n", device_status);
} else if(cmd_t == CMD_BLE) {
single_format_data(ble_bin_buffer, "rtr:", (uint8_t)device_status);
binary_tx_handler(ble_bin_buffer,3);
}
}
else if(scmd.tag[0] == 's') {
// ssn
if((scmd.tag[1] == 's') && (scmd.tag[2] == 'n')){ // Measuring the Battery level
battery_level_meas();
}
// spn
else if((scmd.tag[1] == 'p') && (scmd.tag[2] == 'n')){ // Measuring the pressure1 level read
pressure_all_level_meas(); // pressure1 + pressure2
}
// sso
else if((scmd.tag[1] == 's') && (scmd.tag[2] == 'o')){ // Measuring the Temperature of LED
if(length_error(scmd.tag,6,length)==false)
{
return;
}
else if(activate_error(scmd.tag,device_status)==false)
{
return;
}
else{
tmp235_voltage_level_meas();
}
}
// ssp
else if((scmd.tag[1] == 's') && (scmd.tag[2] == 'p')){ // Measuring the raw data of 6-axis motion sensor
hw_i2c_init_once();
motion_raw_data_enabled = true;
hw_i2c_init_once();
ble_got_new_data =false;
if(data_array[2] == 'c')
{
motion_data_once = false;
}
else
{
motion_data_once = true;
}
main_timer_start();
// }else if((scmd.tag[1] == 'P') && (scmd.tag[2] == 'p')){ // Measuring the raw data of 6-axis motion sensor
// motion_raw_data_enabled = true;
// ble_got_new_data =false;
//
//
// motion_data_once = false;
//
// main_timer_start();
}
// ssq
else if((scmd.tag[1] == 's') && (scmd.tag[2] == 'q')){ // Device power off
if(cmd_t == CMD_UART) {
DBG_PRINTF("Tq\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
single_format_data(ble_bin_buffer, "rsq:", scmd.value0);
binary_tx_handler(ble_bin_buffer,2);
// sprintf(ble_tx_buffer, "Tq\r\n");
// data_tx_handler(ble_tx_buffer);
}
go_device_power_off = true;
main_timer_start();
#if FEATURE_SECURE_CONNECTION
}
// ssr
else if((scmd.tag[1] == 's') && (scmd.tag[2] == 'r')){ // Bond Info Delete
// ret_code_t err_code;
if(cmd_t == CMD_UART) {
DBG_PRINTF("Tr\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
single_format_data(ble_bin_buffer, "rsr:", scmd.value0);
binary_tx_handler(ble_bin_buffer,2);
}
bond_data_delete = true;
eeprom_write_byte(0x0060, (uint8_t)bond_data_delete);
//m_config.reset_status = 2;
m_reset_status = 2;
eeprom_write_byte(0x0065, m_reset_status);
//config_save();
nrf_delay_ms(5);
go_NVIC_SystemReset = true;
main_timer_start();
#endif
}
// sss
else if((scmd.tag[1] == 's') && (scmd.tag[2] == 's')){ //Device reset(Reboot)
// ret_code_t err_code;
// err_code = eeprom_write_byte(0x0060, 0x00);
//ret_code_t err_code;
if(cmd_t == CMD_UART) {
DBG_PRINTF("Ts\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
single_format_data(ble_bin_buffer, "rss:", scmd.value0);
binary_tx_handler(ble_bin_buffer,2);
}
go_NVIC_SystemReset = true;
//m_config.reset_status = 2;
m_reset_status = 2;
eeprom_write_byte(0x0065, m_reset_status);
// config_save();
nrf_delay_ms(5);
go_NVIC_SystemReset = true;
main_timer_start();
}
// sst
else if((scmd.tag[1] == 's') && (scmd.tag[2] == 't')){ // Auto Gain Control
if(cmd_t == CMD_UART) {
DBG_PRINTF("Ready\r\n\r\nREADY\r\n");
} else if(cmd_t == CMD_BLE) {
single_format_data(ble_bin_buffer, "rst:", scmd.value0);
binary_tx_handler(ble_bin_buffer,2);
}
}
// ssv — DISABLED: use ssv? in pc_firm/parser.c
else if(0 && (scmd.tag[1] == 's') && (scmd.tag[2] == 'v')){ // Auto Gain Control
if(cmd_t == CMD_UART) {
DBG_PRINTF("%s\r\n",DEVICE_VERSION);
} else if(cmd_t == CMD_BLE) {
ascii_format_data(ble_bin_buffer, "rsv:", DEVICE_VERSION,12);
binary_tx_handler(ble_bin_buffer,8);
//test_eeprom_page_rw();
}
}
// ssz - Write Serial Number (FDS only) — DISABLED: use mws? in pc_firm/parser.c
else if(0 && (scmd.tag[1] == 's') && (scmd.tag[2] == 'z')){
if(length_error(scmd.tag,18,length)==false)
{
return;
}
if(cmd_t == CMD_UART) {
DBG_PRINTF("Tz0\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
memcpy(SERIAL_NO, scmd.value_ascii, 12);
memcpy(m_config.serial_no, scmd.value_ascii, 12);
config_save();
DBG_PRINTF("[ssz] S/N=%s saved to FDS\r\n", m_config.serial_no);
ascii_format_data(ble_bin_buffer, "rsz:", scmd.value_ascii,12);
binary_tx_handler(ble_bin_buffer,8);
}
else{
DBG_PRINTF("ERR!!! Serial_number 12\r\n\r\n");
if(cmd_t == CMD_UART) {
DBG_PRINTF("Tz0FF\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
param_error(scmd.tag );
}
}
}
// spz
else if((scmd.tag[1] == 'p') && (scmd.tag[2] == 'z')){ //Write, passkey
if(length_error(scmd.tag,12,length)==false)
{
return;
}
uint8_t tx_data[EEPROM_PAGE_SIZE];
uint8_t rx_data[EEPROM_PAGE_SIZE];
uint8_t raw_data[EEPROM_PAGE_SIZE];
if(cmd_t == CMD_UART) {
DBG_PRINTF("Tz0\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
//memcpy(SERIAL_NO, scmd.value_ascii, 6);
//eeprom_write_bytes(uint16_t mem_address, const uint8_t *data, size_t length)
for (uint8_t i=0 ; i<6 ;i++)
{
tx_data[i] = (uint8_t)(scmd.value_ascii[i]);
}
if(eeprom_write_encrypted(0x0020, tx_data, 6)!= NRF_SUCCESS)
{
DBG_PRINTF("ERR!!! EEP_passkey 6\r\n\r\n");;
}
if(eeprom_read_bytes(0x0020, raw_data, 6)!= NRF_SUCCESS)
{
DBG_PRINTF("ERR!!! EEP_passkey 6\r\n\r\n");;
}
nrf_delay_ms(10);
DBG_PRINTF("encrypted: %s\n", raw_data);
if(eeprom_read_decrypted(0x0020, rx_data, 6)!= NRF_SUCCESS)
{
DBG_PRINTF("ERR!!! EEP_passkey 6\r\n\r\n");;
}
DBG_PRINTF("Decrypted: %s\n", rx_data);
ascii_format_data(ble_bin_buffer, "rpz:", scmd.value_ascii,6);
binary_tx_handler(ble_bin_buffer,5);
}
else{
DBG_PRINTF("ERR!!! passkey 6\r\n\r\n");
if(cmd_t == CMD_UART) {
DBG_PRINTF("Tpz0FF\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
param_error(scmd.tag );
}
}
}
// sqz
else if((scmd.tag[1] == 'q') && (scmd.tag[2] == 'z')){ // Read, Serial Number
uint8_t rx_data[EEPROM_PAGE_SIZE];
if(cmd_t == CMD_UART) {
DBG_PRINTF("Tz1,%s\r\n\r\n", SERIAL_NO);
} else if(cmd_t == CMD_BLE) {
hw_i2c_init_once();
if(eeprom_read_decrypted(0x0020, rx_data, 6)!= NRF_SUCCESS)
{
DBG_PRINTF("ERR!!! EEP_Serial_number 12\r\n\r\n");;
}
DBG_PRINTF("Decrypted: %s\n", rx_data);
// if (eeprom_read_bytes(0x0030 , rx_data ,12) !=NRF_SUCCESS)
// {
// DBG_PRINTF("ERR!!! EEP_Serial_number 12\r\n\r\n");
// }
// sprintf(ble_tx_buffer, "Tz1,%s\r\n", m_config.serial_number);
// data_tx_handler(ble_tx_buffer);
for (uint8_t i=0 ; i<6 ;i++)
{
(m_static_passkey[i]) = (char)rx_data[i] ;
}
ascii_format_data(ble_bin_buffer, "rqz:", m_static_passkey,6);
binary_tx_handler(ble_bin_buffer,5);
}
}
// srz - Read Serial Number (FDS only) — DISABLED: use mrs? in pc_firm/parser.c
else if(0 && (scmd.tag[1] == 'r') && (scmd.tag[2] == 'z')){
if(cmd_t == CMD_UART) {
DBG_PRINTF("Tz1,%s\r\n\r\n", SERIAL_NO);
} else if(cmd_t == CMD_BLE) {
memcpy(SERIAL_NO, m_config.serial_no, 12);
ascii_format_data(ble_bin_buffer, "rrz:", SERIAL_NO, 12);
binary_tx_handler(ble_bin_buffer, 8);
}
#if 0 // ===== EEPROM start block =====
// }else if((scmd.tag[1] == 'a') && (scmd.tag[2] == 'z')){ // write bytes
//
//
// uint8_t write_data[64];
//
// memcpy(write_data,scmd.value_ascii,12);
// eeprom_write_bytes(scmd.value0,write_data,12);
// if(cmd_t == CMD_UART) {
// DBG_PRINTF("Tz2,%s\r\n\r\n",write_data);
// } else if(cmd_t == CMD_BLE) {
// sprintf(ble_tx_buffer, "Tz22,%s\r\n", write_data);
// data_tx_handler(ble_tx_buffer);
// }
// }else if((scmd.tag[1] == 'b') && (scmd.tag[2] == 'z')){
//
// unsigned char *read_data;
//
// eeprom_read_bytes(scmd.value0,read_data,12);
//
// if(cmd_t == CMD_UART) {
// DBG_PRINTF("Tz3,%s,%s\r\n\r\n", read_data,read_data);
// } else if(cmd_t == CMD_BLE) {
// sprintf(ble_tx_buffer, "Tz3,%s\r\n",read_data);
// data_tx_handler(ble_tx_buffer);
// }
// }else if((scmd.tag[1] == 'c') && (scmd.tag[2] == 'z')){ // write page
//
// //unsigned char read_data[EEPROM_PAGE_SIZE];
// uint8_t write_data[64];
// for(uint8_t i=0 ; i<64 ;i++){
// write_data[i] = scmd.values[i+6];
// }
//
// //memcpy(write_data,scmd.value_ascii,12);
// eeprom_write_page (scmd.value0,write_data);
//
// if(cmd_t == CMD_UART) {
// DBG_PRINTF("Tz3,%s\r\n\r\n", write_data);
// } else if(cmd_t == CMD_BLE) {
//// sprintf(ble_tx_buffer, "Tz3,%s\r\n",read_data
// ascii_format_data(ble_bin_buffer, "rcz:", "ok",3);
// binary_tx_handler(ble_bin_buffer,5);
//// data_tx_handler(ble_tx_buffer);
// }
//
//
//
//
//
// }else if((scmd.tag[1] == 'd') && (scmd.tag[2] == 'z')){ // Read page
//
// uint8_t read_data[64];
// eeprom_read_page (scmd.value0,read_data);
//
// if(cmd_t == CMD_UART) {
// // DBG_PRINTF("Tz3,%s,%s\r\n\r\n", read_data,read_data);
// } else if(cmd_t == CMD_BLE) {
// for (int i = 0; i < EEPROM_PAGE_SIZE; i++) {
//
// DBG_PRINTF("%02X,", read_data[i]);
// }
//
//
// format_data_byte(ble_bin_buffer, "rdz:", read_data,64);
// binary_tx_handler(ble_bin_buffer,34);
// }
//ret_code_t eeprom_write_uint16_array(uint16_t start_address, const uint16_t *data, size_t count)
#endif // ===== EEPROM block End =====
}
// sez
else if((scmd.tag[1] == 'e') && (scmd.tag[2] == 'z')){ // Read, Serial Number
hw_i2c_init_once();
uint16_t write_data[64];
for (int i = 0; i < 48; i++) {
write_data[i]= (uint16_t)(scmd.values[i*2+7] | (uint16_t)(scmd.values[i*2+6] << 8));
}
eeprom_write_uint16_array(scmd.value0,write_data,48);
if(cmd_t == CMD_UART) {
DBG_PRINTF("rez,%s\r\n\r\n", scmd.value_ascii);
} else if(cmd_t == CMD_BLE) {
format_data(ble_bin_buffer, "rez:", write_data,48);
binary_tx_handler(ble_bin_buffer,50);
}
}
// sfz
else if((scmd.tag[1] == 'f') && (scmd.tag[2] == 'z')){ // Read, Serial Number sfz arry eeprom read
if(length_error(scmd.tag,8,length)==false)
{
return;
}
// else if(activate_error(scmd.tag,device_status)==false)
// {
// return;
// }
else{
hw_i2c_init_once();
uint16_t read_data[64];
//uint8_t EEPread_data[64];
eeprom_read_uint16_array(scmd.value0,read_data,48); //int16(48ea) DP Preset 0~6
// for (int i = 0; i < 48; i++) {
// EEPread_data[i]= (uint8_t)(read_data[i]);
// }
if(cmd_t == CMD_UART) {
DBG_PRINTF("ref,%s\r\n\r\n", scmd.value_ascii);
} else if(cmd_t == CMD_BLE) {
format_data(ble_bin_buffer, "rfz:", read_data,48);
binary_tx_handler(ble_bin_buffer,50);
}
}
}
// siz - Read HW Number (FDS only) — DISABLED: use mrh? in pc_firm/parser.c
else if(0 && (scmd.tag[1] == 'i') && (scmd.tag[2] == 'z')){
if(cmd_t == CMD_UART) {
DBG_PRINTF("Tz1,%s\r\n\r\n", HW_NO);
} else if(cmd_t == CMD_BLE) {
memcpy(HW_NO, m_config.hw_no, 12);
ascii_format_data(ble_bin_buffer, "riz:", HW_NO, 12);
binary_tx_handler(ble_bin_buffer, 8);
}
}
// shz - Write HW Number (FDS only) — DISABLED: use mwh? in pc_firm/parser.c
else if(0 && (scmd.tag[1] == 'h') && (scmd.tag[2] == 'z')){
if(length_error(scmd.tag,18,length)==false)
{
return;
}
if(cmd_t == CMD_UART) {
DBG_PRINTF("Tz0\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
memcpy(HW_NO, scmd.value_ascii, 12);
memcpy(m_config.hw_no, scmd.value_ascii, 12);
config_save();
DBG_PRINTF("[shz] HW=%s saved to FDS\r\n", m_config.hw_no);
ascii_format_data(ble_bin_buffer, "rhz:", scmd.value_ascii,12);
binary_tx_handler(ble_bin_buffer,8);
}
else{
DBG_PRINTF("ERR!!! HW_NO 12\r\n\r\n");
if(cmd_t == CMD_UART) {
DBG_PRINTF("Tpz0FF\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
param_error(scmd.tag );
}
}
}
// sxz
else if((scmd.tag[1] == 'x') && (scmd.tag[2] == 'z')){ // Set the delay for PD stabilization. 0<>s ~ FFFF(65535)<29>s
m_life_cycle = ((uint16_t)scmd.value0 << 16) | ((uint16_t)scmd.value1 & 0xFFFF);
result_data[0] = scmd.value0;
result_data[1] = scmd.value1;
if(length_error(scmd.tag,10,length)==false)
{
return;
}
else if((m_life_cycle > 0)||(m_life_cycle <= 99999999)) {
if(cmd_t == CMD_UART) {
DBG_PRINTF("rxz\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
hw_i2c_init_once();
eeprom_write_uint32(0x0090, m_life_cycle);
format_data(ble_bin_buffer, "rxz:", result_data,2);
binary_tx_handler(ble_bin_buffer,4);
}
}else{
if(cmd_t == CMD_UART) {
DBG_PRINTF("ERR!!! pd_delay_us failed!\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
param_error(scmd.tag );
}
}
}
// syz
else if((scmd.tag[1] == 'y') && (scmd.tag[2] == 'z')){ //
if(length_error(scmd.tag,6,length)==false)
{
return;
}
else {
eeprom_read_uint32(0x0090, &m_life_cycle); //thnk cj
result_data[0] = (uint16_t)(m_life_cycle >> 16);
result_data[1] = (uint16_t)(m_life_cycle & 0xFFFF);
if(cmd_t == CMD_UART) {
DBG_PRINTF("ryz:\r\n\r\n");
} else if(cmd_t == CMD_BLE) {
format_data(ble_bin_buffer, "ryz:", result_data,4);
binary_tx_handler(ble_bin_buffer,4);
}
}
//ret_code_t eeprom_write_uint32(uint16_t mem_address, uint32_t data);
}else {
if(cmd_t == CMD_UART) {
DBG_PRINTF("ERR!!! UART Command Failed, %c%c%c%c\r\n",data_array[0], data_array[1], data_array[2], data_array[3]);
} else if(cmd_t == CMD_BLE) {
single_format_data(ble_bin_buffer, "err!", err_code5);
binary_tx_handler(ble_bin_buffer,3);
}
}
}
}
}
/**
* @}
*/
//0x0A