EC600U_esp32_iap_uart/EC600U_t2n/cfg.c

370 lines
9.6 KiB
C
Raw Normal View History

2024-02-05 17:39:56 +08:00
#include "cfg.h"
#include <string.h>
#include <stdlib.h>
#include "ql_api_osi.h"
#include "ql_log.h"
#include "cJSON.h"
#include "ql_fs.h"
#include "ql_api_dev.h"
// #include "hx_device.h"
// #include "can_device.h"
#define MAGIC_CFG_OFFSET 0
#define METER_CFG_OFFSET 16
#define SERIAL1_CFG_OFFSET 32
#define SERIAL2_CFG_OFFSET 48
#define DSC_CFG_OFFSET 64
// #define _USE_CAN_ 0
#define LOGD(msg, ...) QL_LOG(QL_LOG_LEVEL_DEBUG, "t2n_cfg", msg, ##__VA_ARGS__)
#define LOGI(msg, ...) QL_LOG(QL_LOG_LEVEL_INFO, "t2n_cfg", msg, ##__VA_ARGS__)
#define LOGW(msg, ...) QL_LOG(QL_LOG_LEVEL_WARN, "t2n_cfg", msg, ##__VA_ARGS__)
#define LOGE(msg, ...) QL_LOG(QL_LOG_LEVEL_ERROR, "t2n_cfg", msg, ##__VA_ARGS__)
#define LOG_TAG "cfg"
extern uint16_t gWordVar[];
// dsc_cfg_t dsc_cf;// = {{0,0,0,0},0,"123456"};
// meter_cfg_t meter_cfg = {DEV_KMS_GENARATOR,3,1,0x2222,10,10,10,10};
const char hw_ver[32] = "MCGPS_V1.0";
const char sw_ver[32] = "HXGPS20170122";
void save_cfg_file(cJSON *json, char *name)
{
int n;
// uint16_t fname[32];
int fd;
char *buff_p;
UINT writedLen;
fd = ql_fopen(name, "w");
if (fd < 0)
{
LOGE("ql_fopen():Create File Fail,and Return Error is %x ", fd);
return;
}
buff_p = cJSON_PrintUnformatted(json);
if (buff_p == NULL)
{
LOGE("mem alloc fail!");
ql_fclose(fd);
return;
}
n = strlen(buff_p);
writedLen = ql_fwrite(buff_p, n, 1, fd);
if (n < 0)
{
LOGE("ql_fwrite():Write File Fail,and Return Error is %d", writedLen);
}
ql_fclose(fd);
free(buff_p);
}
cJSON *load_cfg_file(char *name)
{
// uint16_t fname[32];
cJSON *json;
// char *buff_p;
// char *p;
int32_t fd;
int readLen, filesize;
fd = ql_fopen(name, "r");
if (fd < 0)
{
LOGE("ql_fopen():Open [%s] Fail,and Return Error is %x ", name, fd);
return NULL;
}
filesize = ql_fseek(fd, 0, SEEK_END); // 读取文件大小
if (filesize > 0 && filesize < 1024 * 16)
{
char *buff_p = malloc(filesize);
if (buff_p == NULL)
{
LOGE("malloc fail!");
ql_fclose(fd);
return NULL;
}
ql_fseek(fd, 0, SEEK_SET);
readLen = ql_fread(buff_p, filesize,1, fd);
if (readLen < 0)
{
LOGE("ql_fread():Read File Fail,and Return Error is %d", readLen);
free(buff_p);
ql_fclose(fd);
return NULL;
}
buff_p[filesize] = '\0';
ql_fclose(fd);
json = cJSON_Parse(buff_p);
free(buff_p);
if (json == NULL)
{
LOGE("JSON_Parse Err ret%s", cJSON_GetErrorPtr());
}
}
return NULL;
}
void load_hw_cfg(void)
{
}
// void load_gps_cfg(gps_cfg_t *gps_cfg)
// {
// cJSON *node, *child;
// cJSON *cfg_json = load_cfg_file("gps_cfg.json");
// // char *out = cJSON_Print(cfg_json);
// // eat_trace(out);
// // eat_mem_free(out);
// if (cfg_json != NULL)
// {
// node = cJSON_GetObjectItem(cfg_json, "baud");
// if (node != NULL)
// {
// gps_cfg->baud = node->valueint;
// }
// node = cJSON_GetObjectItem(cfg_json, "pwr_pin");
// if (node != NULL)
// {
// gps_cfg->pwr_pin = node->valueint;
// }
// cJSON_Delete(cfg_json);
// }
// }
// void save_gps_cfg(gps_cfg_t *gps_cfg)
// {
// // cJSON *node,*child;
// cJSON *cfg_json = cJSON_CreateObject();
// if (cfg_json != NULL)
// {
// cJSON_AddNumberToObject(cfg_json, "baud", gps_cfg->baud);
// cJSON_AddNumberToObject(cfg_json, "pwr_pin", gps_cfg->pwr_pin);
// save_cfg_file(cfg_json, "gps_cfg.json");
// cJSON_Delete(cfg_json);
// }
// }
void load_dsc_cfg(dsc_cfg_t *dsc_cfg)
{
cJSON *node;//cJSON *child;
cJSON *cfg_json = load_cfg_file("dsc_cfg.json");
if (cfg_json != NULL)
{
node = cJSON_GetObjectItem(cfg_json, "host1");
if (node != NULL)
{
strncpy(dsc_cfg->host[0], node->valuestring, 24);
LOGD("host1=%s", node->valuestring);
}
node = cJSON_GetObjectItem(cfg_json, "port1");
if (node != NULL)
{
dsc_cfg->port[0] = node->valueint;
LOGD("port1=%d", node->valueint);
}
node = cJSON_GetObjectItem(cfg_json, "host2");
if (node != NULL)
{
strncpy(dsc_cfg->host[1], node->valuestring, 24);
LOGD("host2=%s", node->valuestring);
}
node = cJSON_GetObjectItem(cfg_json, "port2");
if (node != NULL)
{
dsc_cfg->port[1] = node->valueint;
LOGD("port2=%d", node->valueint);
}
cJSON_Delete(cfg_json);
}
}
void save_dsc_cfg(dsc_cfg_t *dsc_cfg)
{
cJSON *cfg_json = cJSON_CreateObject();
if (cfg_json != NULL)
{
cJSON_AddStringToObject(cfg_json, "host1", dsc_cfg->host[0]);
cJSON_AddStringToObject(cfg_json, "host2", dsc_cfg->host[1]);
cJSON_AddNumberToObject(cfg_json, "port1", dsc_cfg->port[0]);
cJSON_AddNumberToObject(cfg_json, "port2", dsc_cfg->port[1]);
save_cfg_file(cfg_json, "dsc_cfg.json");
cJSON_Delete(cfg_json);
}
}
// void load_uart_cfg(uart_cfg_t *uart_cfg)
// {
// cJSON *node, *child;
// cJSON *cfg_json = load_cfg_file("uart_cfg.json");
// if (cfg_json != NULL)
// {
// child = cfg_json->child;
// while (child != NULL)
// {
// int index;
// if (strcmp(child->string, "uart1") == 0)
// {
// index = 0;
// }
// else if (strcmp(child->string, "uart2") == 0)
// {
// index = 1;
// }
// else
// {
// child = child->next;
// continue;
// }
// node = cJSON_GetObjectItem(child, "baud");
// if (node != NULL)
// {
// (uart_cfg + index)->baud = node->valueint;
// }
// node = cJSON_GetObjectItem(child, "data_bits");
// if (node != NULL)
// {
// (uart_cfg + index)->data_bits = node->valueint;
// }
// node = cJSON_GetObjectItem(child, "parity");
// if (node != NULL)
// {
// (uart_cfg + index)->parity = *node->valuestring;
// }
// node = cJSON_GetObjectItem(child, "stop_bits");
// if (node != NULL)
// {
// (uart_cfg + index)->stop_bits = node->valueint;
// }
// node = cJSON_GetObjectItem(child, "enable");
// if (node != NULL)
// {
// (uart_cfg + index)->enable = node->valueint;
// }
// child = child->next;
// }
// cJSON_Delete(cfg_json);
// }
// }
// void save_last_fault(char *dtc, uint32_t ts)
// {
// cJSON *cfg_json = cJSON_CreateObject();
// if (cfg_json != NULL)
// {
// char str_dtc[5] = {0};
// strncpy(str_dtc, dtc, 4);
// cJSON_AddStringToObject(cfg_json, "dtc", str_dtc);
// cJSON_AddNumberToObject(cfg_json, "ts", ts);
// save_cfg_file(cfg_json, "fault.json");
// cJSON_Delete(cfg_json);
// }
// }
// void load_last_fault(char *dtc, uint32_t *ts)
// {
// cJSON *node, *child;
// cJSON *cfg_json = load_cfg_file("fault.json");
// if (cfg_json != NULL)
// {
// node = cJSON_GetObjectItem(cfg_json, "dtc");
// if (node != NULL)
// {
// strncpy(dtc, node->valuestring, 4);
// }
// node = cJSON_GetObjectItem(cfg_json, "ts");
// if (node != NULL)
// {
// *ts = atoi(node->valuestring);
// }
// }
// cJSON_Delete(cfg_json);
// // meter_cfg.trans_time = 0;
// }
// void load_adc_cal(void)
// {
// extern adc_cal_t *p_adc_cal;
// cJSON *node, *child;
// cJSON *cfg_json = load_cfg_file("adc_cal.json");
// if (cfg_json != NULL)
// {
// node = cJSON_GetObjectItem(cfg_json, "ad");
// if (node != NULL)
// {
// p_adc_cal->ad = node->valueint;
// }
// node = cJSON_GetObjectItem(cfg_json, "voltage");
// if (node != NULL)
// {
// p_adc_cal->voltage = node->valueint;
// }
// cJSON_Delete(cfg_json);
// eat_trace("adc_cal load ok");
// }
// else
// {
// uint32_t ad;
// // eat_get_adc_sync(EAT_PIN38_ADC,&ad);
// if (p_adc_cal->ad > 1000)
// {
// p_adc_cal->ad = ad;
// p_adc_cal->voltage = 2400;
// save_adc_cal();
// }
// else
// {
// p_adc_cal->ad = 1200;
// p_adc_cal->voltage = 2400;
// }
// eat_trace("adc_cal load err");
// }
// }
// int save_adc_cal(void)
// {
// extern adc_cal_t *p_adc_cal;
// cJSON *cfg_json = cJSON_CreateObject();
// if (cfg_json != NULL)
// {
// cJSON_AddNumberToObject(cfg_json, "ad", p_adc_cal->ad);
// cJSON_AddNumberToObject(cfg_json, "voltage", p_adc_cal->voltage);
// save_cfg_file(cfg_json, "adc_cal.json");
// cJSON_Delete(cfg_json);
// return 0;
// }
// return -1;
// }
// int save_working_time(void)
// {
// cJSON *cfg_json = cJSON_CreateObject();
// if (cfg_json != NULL)
// {
// cJSON_AddNumberToObject(cfg_json, "dev_working_time", p_time_cfg->working_time);
// cJSON_AddNumberToObject(cfg_json, "working_time_offset", p_time_cfg->time_offset);
// save_cfg_file(cfg_json, "working_time.json");
// cJSON_Delete(cfg_json);
// return 0;
// }
// return -1;
// }
// int load_working_time(void)
// {
// cJSON *node;
// cJSON *cfg_json = load_cfg_file("working_time.json");
// if (cfg_json != NULL)
// {
// node = cJSON_GetObjectItem(cfg_json, "dev_working_time");
// if (node != NULL)
// {
// p_time_cfg->working_time = node->valueint;
// }
// node = cJSON_GetObjectItem(cfg_json, "working_time_offset");
// if (node != NULL)
// {
// p_time_cfg->time_offset = node->valueint;
// }
// cJSON_Delete(cfg_json);
// return 0;
// }
// return -1;
// }