|
|
|
#include "JZsdkLib.h"
|
|
|
|
#include <string.h> // 确保包含头文件
|
|
|
|
|
|
|
|
#include "./JZsdk_Hal.h"
|
|
|
|
#include "BaseConfig.h"
|
|
|
|
|
|
|
|
#define JZSDK_HAL_CONFIG_FILE "/root/sdcard/config.ini"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 读取INI配置文件中的指定键值对
|
|
|
|
* @param config_file 配置文件路径
|
|
|
|
* @param section 配置段名称
|
|
|
|
* @param key 键名称
|
|
|
|
* @return 成功返回动态分配的字符串(需调用者 free),失败返回 NULL
|
|
|
|
*/
|
|
|
|
char* JZsdk_HalConfig_Read(const char* config_file, const char* section, const char* key) {
|
|
|
|
FILE* fp = fopen(config_file, "r");
|
|
|
|
if (!fp) return NULL;
|
|
|
|
|
|
|
|
char line[1024];
|
|
|
|
char section_buf[256];
|
|
|
|
int in_target_section = 0;
|
|
|
|
char* result = NULL;
|
|
|
|
|
|
|
|
while (fgets(line, sizeof(line), fp)) {
|
|
|
|
// 去除行尾换行符
|
|
|
|
size_t len = strlen(line);
|
|
|
|
while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) {
|
|
|
|
line[--len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line[0] == '[') {
|
|
|
|
if (sscanf(line, "[%[^]]]", section_buf) == 1) {
|
|
|
|
// 去除空格
|
|
|
|
char* start = section_buf;
|
|
|
|
char* end = section_buf + strlen(section_buf) - 1;
|
|
|
|
while (start <= end && (*start == ' ' || *start == '\t')) start++;
|
|
|
|
while (end >= start && (*end == ' ' || *end == '\t')) end--;
|
|
|
|
if (start <= end) {
|
|
|
|
*(end + 1) = '\0';
|
|
|
|
in_target_section = (strcmp(start, section) == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_target_section) {
|
|
|
|
char current_key[256], current_value[256];
|
|
|
|
if (sscanf(line, "%[^=]=%[^\n]", current_key, current_value) == 2) {
|
|
|
|
// 去除key两端空格
|
|
|
|
char* k_start = current_key;
|
|
|
|
char* k_end = current_key + strlen(current_key) - 1;
|
|
|
|
while (k_start <= k_end && (*k_start == ' ' || *k_start == '\t')) k_start++;
|
|
|
|
while (k_end >= k_start && (*k_end == ' ' || *k_end == '\t')) k_end--;
|
|
|
|
if (k_start <= k_end) {
|
|
|
|
*(k_end + 1) = '\0';
|
|
|
|
if (strcmp(k_start, key) == 0) {
|
|
|
|
// 去除value两端空格和行尾注释
|
|
|
|
char* v_start = current_value;
|
|
|
|
char* v_end = current_value + strlen(current_value) - 1;
|
|
|
|
while (v_start <= v_end && (*v_start == ' ' || *v_start == '\t')) v_start++;
|
|
|
|
while (v_end >= v_start) {
|
|
|
|
if (*v_end == ' ' || *v_end == '\t' || *v_end == '#') {
|
|
|
|
v_end--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (v_start <= v_end) {
|
|
|
|
size_t val_len = v_end - v_start + 1;
|
|
|
|
result = (char*)malloc(val_len + 1);
|
|
|
|
if (result) {
|
|
|
|
strncpy(result, v_start, val_len);
|
|
|
|
result[val_len] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 修改INI配置文件中的指定键值对
|
|
|
|
* @param config_file 配置文件路径
|
|
|
|
* @param section 配置段名称(如 "APP_RUN_TYPE")
|
|
|
|
* @param key 键名称(如 "appmode")
|
|
|
|
* @param new_value 新的值(字符串形式)
|
|
|
|
* @return 0成功,-1失败
|
|
|
|
*/
|
|
|
|
T_JZsdkReturnCode JZsdk_HalConfig_Write(const char* config_file, const char* section,
|
|
|
|
const char* key, const char* new_value)
|
|
|
|
{
|
|
|
|
FILE* fp, * fp_temp;
|
|
|
|
char temp_file[512];
|
|
|
|
char line[1024];
|
|
|
|
char section_buf[256];
|
|
|
|
int in_target_section = 0;
|
|
|
|
int key_found = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
// 参数检查
|
|
|
|
if (!config_file || !section || !key || !new_value) {
|
|
|
|
printf("错误:参数不能为空\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 检查原文件是否存在
|
|
|
|
fp = fopen(config_file, "r");
|
|
|
|
if (!fp) {
|
|
|
|
JZSDK_LOG_ERROR("错误:无法打开配置文件 %s\n", config_file);
|
|
|
|
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建临时文件
|
|
|
|
snprintf(temp_file, sizeof(temp_file), "%s.tmp", config_file);
|
|
|
|
fp_temp = fopen(temp_file, "w");
|
|
|
|
if (!fp_temp) {
|
|
|
|
JZSDK_LOG_ERROR("错误:无法创建临时文件\n");
|
|
|
|
fclose(fp);
|
|
|
|
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 逐行读取原文件并处理
|
|
|
|
while (fgets(line, sizeof(line), fp)) {
|
|
|
|
// 去除行尾的换行符(保留用于写入)
|
|
|
|
size_t len = strlen(line);
|
|
|
|
if (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) {
|
|
|
|
line[len - 1] = '\0';
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
if (len > 0 && line[len - 1] == '\r') {
|
|
|
|
line[len - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
// 检查是否是section行
|
|
|
|
if (line[0] == '[') {
|
|
|
|
// 解析section名称
|
|
|
|
if (sscanf(line, "[%[^]]]", section_buf) == 1) {
|
|
|
|
// 去除section名称两端的空格
|
|
|
|
char* start = section_buf;
|
|
|
|
char* end = section_buf + strlen(section_buf) - 1;
|
|
|
|
while (start <= end && (*start == ' ' || *start == '\t')) start++;
|
|
|
|
while (end >= start && (*end == ' ' || *end == '\t')) end--;
|
|
|
|
if (start <= end) {
|
|
|
|
*(end + 1) = '\0';
|
|
|
|
// 判断是否是目标section
|
|
|
|
if (strcmp(start, section) == 0) {
|
|
|
|
in_target_section = 1;
|
|
|
|
printf("进入目标段: [%s]\n", section);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
in_target_section = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 写入原section行
|
|
|
|
fprintf(fp_temp, "%s\n", line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 如果在目标section内,查找key
|
|
|
|
if (in_target_section && !key_found) {
|
|
|
|
char current_key[256];
|
|
|
|
char current_value[256];
|
|
|
|
|
|
|
|
// 尝试解析key=value格式
|
|
|
|
if (sscanf(line, "%[^=]=%[^\n]", current_key, current_value) == 2) {
|
|
|
|
// 去除key两端的空格
|
|
|
|
char* key_start = current_key;
|
|
|
|
char* key_end = current_key + strlen(current_key) - 1;
|
|
|
|
while (key_start <= key_end && (*key_start == ' ' || *key_start == '\t')) key_start++;
|
|
|
|
while (key_end >= key_start && (*key_end == ' ' || *key_end == '\t')) key_end--;
|
|
|
|
if (key_start <= key_end) {
|
|
|
|
*(key_end + 1) = '\0';
|
|
|
|
|
|
|
|
// 检查是否是目标key
|
|
|
|
if (strcmp(key_start, key) == 0) {
|
|
|
|
printf("找到目标键: %s,原值: %s,修改为新值: %s\n",
|
|
|
|
key, current_value, new_value);
|
|
|
|
|
|
|
|
// 保留原行的缩进和格式
|
|
|
|
// 找到等号的位置
|
|
|
|
char* eq_pos = strchr(line, '=');
|
|
|
|
if (eq_pos) {
|
|
|
|
// 计算等号前的部分(包括空格)
|
|
|
|
int prefix_len = eq_pos - line + 1;
|
|
|
|
// 写入key部分(保持原格式)
|
|
|
|
fprintf(fp_temp, "%.*s%s\n", prefix_len, line, new_value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// 如果找不到等号(不应该发生),简单写入
|
|
|
|
fprintf(fp_temp, "%s=%s\n", key, new_value);
|
|
|
|
}
|
|
|
|
key_found = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 如果不是目标key,写入原行
|
|
|
|
fprintf(fp_temp, "%s\n", line);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 如果在目标section内没有找到目标key,则在section末尾添加
|
|
|
|
if (in_target_section && !key_found) {
|
|
|
|
printf("未找到键 %s,在段末添加: %s=%s\n", key, key, new_value);
|
|
|
|
fprintf(fp_temp, "%s=%s\n", key, new_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 关闭文件
|
|
|
|
fclose(fp);
|
|
|
|
fclose(fp_temp);
|
|
|
|
|
|
|
|
// 用临时文件替换原文件
|
|
|
|
if (remove(config_file) != 0) {
|
|
|
|
printf("警告:无法删除原文件 %s\n", config_file);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rename(temp_file, config_file) != 0) {
|
|
|
|
printf("错误:无法重命名临时文件为 %s\n", config_file);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
printf("成功修改配置文件: %s\n", config_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
T_JZsdkReturnCode JZsdk_Hal_config_use_uart1(int num)
|
|
|
|
{
|
|
|
|
// const char* config_file = JZSDK_HAL_CONFIG_FILE;
|
|
|
|
// const char* target_value = NULL;
|
|
|
|
// char* current_value = NULL;
|
|
|
|
// int need_write = 0;
|
|
|
|
//
|
|
|
|
// // 确定目标值
|
|
|
|
// if (num == 0) {
|
|
|
|
// target_value = "0";
|
|
|
|
// }
|
|
|
|
// else if (num == 1) {
|
|
|
|
// target_value = "1";
|
|
|
|
// }
|
|
|
|
// else if (num == 3) {
|
|
|
|
//#if DEVICE_VERSION == JZ_H150A
|
|
|
|
// T_JZsdkHalHandle* handle = JZsdk_Platform_GetHalHandle();
|
|
|
|
// if (handle != NULL) {
|
|
|
|
// T_JZsdk_HalInfo HalInfo;
|
|
|
|
// handle->GetHalInfo(&HalInfo);
|
|
|
|
// for (int i = 0; i < HalInfo.UartNum; i++) {
|
|
|
|
// if (strcmp(HalInfo.UartInfo[i].DevicePath, "/dev/ttyS2") == 0) {
|
|
|
|
// target_value = "0";
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// else if (strcmp(HalInfo.UartInfo[i].DevicePath, "/dev/ttyGS0") == 0) {
|
|
|
|
// target_value = "1";
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//#endif
|
|
|
|
// if (!target_value) {
|
|
|
|
// JZSDK_LOG_ERROR("无法确定目标设备类型");
|
|
|
|
// return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS; // 其他num不处理
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // 读取当前配置值
|
|
|
|
// current_value = JZsdk_HalConfig_Read(config_file, "UART1", "device");
|
|
|
|
// if (current_value) {
|
|
|
|
// if (strcmp(current_value, target_value) != 0) {
|
|
|
|
// need_write = 1;
|
|
|
|
// }
|
|
|
|
// free(current_value);
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// // 读取失败(如文件不存在或键不存在),需要写入
|
|
|
|
// need_write = 1;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (need_write) {
|
|
|
|
// if (JZsdk_HalConfig_Write(config_file, "UART1", "device", target_value) == 0) {
|
|
|
|
// JZSDK_LOG_INFO("成功修改device为 %s\n", target_value);
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// JZSDK_LOG_ERROR("修改device失败");
|
|
|
|
// return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// JZSDK_LOG_INFO("device已经是 %s,无需修改\n", target_value);
|
|
|
|
// }
|
|
|
|
|
|
|
|
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
T_JZsdkReturnCode JZsdk_HalConfigInit()
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
...
|
...
|
|