json



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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值