static int save_seting(const char *p_data)
{
int nRet = 0;
assert(NULL != g_setting);
assert(NULL != p_data);
if (NULL != p_data)
{
cJSON *pSetup = cJSON_Parse(p_data);
cJSON *pType = cJSON_GetObjectItem(pSetup, "type");
cJSON *pSetting = cJSON_GetObjectItem(pSetup, "setting");
assert((NULL != pType) && (cJSON_Number == pType->type));
assert((NULL != pSetting) && (cJSON_Object == pSetting->type));
if ((NULL != pSetting) && (cJSON_Object == pSetting->type))
{
nRet = check_cfg_privilege(pType->valueint, g_UiPrivilegeSessionid);
if (nRet != SN_SUCCESS)
return nRet;
//只有修改合法才写入文件
if (0 == setup(pType->valueint, pSetting))
{
sys_mutex_lock(g_set_lock);
if (NULL != g_setting)
{
cJSON_Delete(g_setting);
g_setting = NULL;
}
assert(NULL == g_setting);
//这里必须复制
g_setting = cJSON_Duplicate(pSetting, 1);
config_json_write(SETTING_PATH, g_setting);
sys_mutex_unlock(g_set_lock);
}
}
//删除才是王道
cJSON_Delete(pSetup);
}
return SN_SUCCESS;
}static int save_seting(const char *p_data)
{
int nRet = 0;
assert(NULL != g_setting);
assert(NULL != p_data);
if (NULL != p_data)
{
cJSON *pSetup = cJSON_Parse(p_data);
cJSON *pType = cJSON_GetObjectItem(pSetup, "type");
cJSON *pSetting = cJSON_GetObjectItem(pSetup, "setting");
assert((NULL != pType) && (cJSON_Number == pType->type));
assert((NULL != pSetting) && (cJSON_Object == pSetting->type));
if ((NULL != pSetting) && (cJSON_Object == pSetting->type))
{
nRet = check_cfg_privilege(pType->valueint, g_UiPrivilegeSessionid);
if (nRet != SN_SUCCESS)
return nRet;
//只有修改合法才写入文件
if (0 == setup(pType->valueint, pSetting))
{
sys_mutex_lock(g_set_lock);
if (NULL != g_setting)
{
cJSON_Delete(g_setting);
g_setting = NULL;
}
assert(NULL == g_setting);
//这里必须复制
g_setting = cJSON_Duplicate(pSetting, 1);
config_json_write(SETTING_PATH, g_setting);
sys_mutex_unlock(g_set_lock);
}
}
//删除才是王道
cJSON_Delete(pSetup);
}
return SN_SUCCESS;
}
导出
static char *ExportSetting(const char *p_data)
{
if (0 != mount_mov_usb())
return NULL;
//组装导出参数
cJSON* p_export = cJSON_CreateObject();
cJSON* p_setting = NULL;
if (0 < gp_nvr_cmd->max_cam_num)
{
sys_mutex_lock(g_set_lock);
if (NULL == g_setting)
{
unmount_mov_usb();
return NULL;
}
p_setting = cJSON_Duplicate(g_setting, 1);
sys_mutex_unlock(g_set_lock);
}
else
{
p_setting = cJSON_CreateObject();
cJSON* p_device = cJSON_CreateObject();
cJSON_AddItemToObject(p_setting, "device", p_device);
#if defined(__ODM_JY__)
cJSON_AddStringToObject(p_device, "define", "window/define_jy");
#else
cJSON_AddStringToObject(p_device, "define", "window/define");
#endif
cJSON_AddStringToObject(p_device, "vo", "SXGA");
cJSON_AddStringToObject(p_device, "language", "english");
}
cJSON_AddStringToObject(p_export, "serial_num", SERIAL_NUM);
cJSON_AddItemToObject(p_export, "setting", p_setting);
//写文件
char *file_path = USB_MOUNT_PATH"/export_setting.json";
if (0 != config_json_write(file_path, p_export))
{
cJSON_Delete(p_export);
unmount_mov_usb();
return NULL;
}
cJSON_Delete(p_export);
unmount_mov_usb();
pack_ok();
return api_json;
}
int config_json_write(const char *p_file_path,cJSON *p_config)
{
FILE *p_config_file = fopen(p_file_path,"w");
if (NULL == p_config_file)
{
return 1;
}
char *p_config_str = cJSON_PrintUnformatted(p_config);
assert(NULL != p_config_str);
int config_len = strlen(p_config_str);
assert(MAX_CONFIG_SIZE > config_len);
fwrite(p_config_str,1,config_len,p_config_file);
free(p_config_str);
fclose(p_config_file);
return 0;
}