#include #include #include "esp_common.h" #include "user_config.h" #include "cfg.h" #include "log.h" #include "s2j.h" #include "c_types.h" #include "protocol.h" #include "user_main.h" #include "jsprase.h" #include "cfg.h" #include "gpio_map.h" S2jHook s2jHook = { .malloc_fn = malloc, .free_fn = free, }; static char* __jsonSafeDecodeStr(cJSON* pJson, const char* pTags) { cJSON* pItem = cJSON_GetObjectItem(pJson, pTags); if(pItem) { return strdup(pItem->valuestring); } else { return strdup(""); } } static const char* __pro2Json(void* pData) { PCLOUND_API pPro = (PCLOUND_API)pData; const char* pJsonS; s2j_create_json_obj(jObject); s2j_json_set_basic_element(jObject, pPro, int, cmdId); //s2j_json_set_basic_element(jObject, pPro, int, ver); s2j_json_set_basic_element(jObject, pPro, int, cryptoType); s2j_json_set_basic_element(jObject, pPro, int, timeStamp); if(pPro->msgContent) { s2j_json_set_basic_element(jObject, pPro, string, msgContent); } pJsonS = cJSON_Print(jObject); cJSON_Delete(jObject); return pJsonS; } static void* __json2pro(const char* pJsonS) { cJSON* pItem = NULL; PCLOUND_API pApi = NULL; cJSON *pRoot = cJSON_Parse(pJsonS); if(pRoot == NULL) { LOG_EX(LOG_Error, "Json Format Error: [%s]\n", pJsonS); return NULL; } pApi = (PCLOUND_API)malloc(sizeof(CLOUND_API)); if(pApi == NULL) { LOG_EX(LOG_Error, "Malloc CLOUND_API Error\n"); cJSON_Delete(pRoot); return NULL; } pApi->cmdId = cJSON_GetObjectItem(pRoot, "cmdId")->valueint; //pApi->cmdId = cJSON_GetObjectItem(pRoot, "ver")->valueint; pApi->cryptoType = cJSON_GetObjectItem(pRoot, "cryptoType")->valueint; pApi->timeStamp = cJSON_GetObjectItem(pRoot, "timeStamp")->valueint; pItem = cJSON_GetObjectItem(pRoot, "msgContent"); if(pItem) { char* pMsgContent = cJSON_GetObjectItem(pRoot, "msgContent")->valuestring; pApi->msgContent = (char *)malloc(strlen(pMsgContent) + 1); if(pApi->msgContent == NULL) { LOG_EX(LOG_Error, "Malloc MsgContent(%u) Memory Error\n", strlen(pMsgContent) + 1); cJSON_Delete(pRoot); free(pApi); return NULL; } memset(pApi->msgContent, 0, strlen(pMsgContent) + 1); strcpy(pApi->msgContent, pMsgContent); } else { pApi->msgContent = NULL; } cJSON_Delete(pRoot); return pApi; } static const char* __devId2Json(void* pData) { PRSP_GET_DEVID_PRO pDevId = (PRSP_GET_DEVID_PRO)pData; const char* pJsonS; s2j_create_json_obj(jObject); s2j_json_set_basic_element(jObject, pDevId, string, cpuId); s2j_json_set_basic_element(jObject, pDevId, string, macAddr); s2j_json_set_basic_element(jObject, pDevId, string, productSign); pJsonS = cJSON_Print(jObject); cJSON_Delete(jObject); return pJsonS; } static void* __json2CfgAp(const char* pJsonS) { PCFG_AP_INFO pApInfo = NULL; cJSON *pRoot = cJSON_Parse(pJsonS); if(pRoot == NULL) { LOG_EX(LOG_Error, "Json Format Error: [%s]\n", pJsonS); return NULL; } pApInfo = (PCFG_AP_INFO)malloc(sizeof(CFG_AP_INFO)); if(pApInfo == NULL) { LOG_EX(LOG_Error, "Malloc CFG_AP_INFO Error\n"); cJSON_Delete(pRoot); return NULL; } memset(pApInfo, 0, sizeof(CFG_AP_INFO)); pApInfo->bssid = __jsonSafeDecodeStr(pRoot, "bssid"); pApInfo->ssid = __jsonSafeDecodeStr(pRoot, "ssid"); pApInfo->passwd = __jsonSafeDecodeStr(pRoot, "passwd"); pApInfo->gbkssid = __jsonSafeDecodeStr(pRoot, "gbkssid"); pApInfo->gbkpasswd = __jsonSafeDecodeStr(pRoot, "gbkpasswd"); cJSON_Delete(pRoot); return pApInfo; } static const char* __errno2Json(void* pData) { PCLOUND_API_RSP_ERR pInfo = (PCLOUND_API_RSP_ERR)pData; const char* pJsonS; s2j_create_json_obj(jObject); s2j_json_set_basic_element(jObject, pInfo, int, errNo); if(pInfo->errMsg) { s2j_json_set_basic_element(jObject, pInfo, string, errMsg); } pJsonS = cJSON_Print(jObject); cJSON_Delete(jObject); return pJsonS; } static const char* __bypass2Json(void* pData) { PBYPASS_INFO pInfo = (PBYPASS_INFO)pData; const char* pJsonS; s2j_create_json_obj(jObject); if(pInfo->mcuCmd) { s2j_json_set_basic_element(jObject, pInfo, string, mcuCmd); } pJsonS = cJSON_Print(jObject); cJSON_Delete(jObject); return pJsonS; } static void* __json2bypass(const char* pJsonS) { PBYPASS_INFO pInfo = NULL; cJSON *pRoot = cJSON_Parse(pJsonS); if(pRoot == NULL) { LOG_EX(LOG_Error, "Json Format Error: [%s]\n", pJsonS); return NULL; } pInfo = (PBYPASS_INFO)malloc(sizeof(BYPASS_INFO)); if(pInfo == NULL) { LOG_EX(LOG_Error, "Malloc BYPASS_INFO Error\n"); cJSON_Delete(pRoot); return NULL; } pInfo->mcuCmd = __jsonSafeDecodeStr(pRoot, "mcuCmd"); cJSON_Delete(pRoot); return pInfo; } static const char* __gblCfg2Json(void* pData) { PGLOGAL_CONFIG pReq = (PGLOGAL_CONFIG)pData; const char* pJsonS; s2j_create_json_obj(jObject); s2j_json_set_basic_element(jObject, pReq, string, pCfgTagsInfo); s2j_json_set_struct_element(json_sta, jObject, struct_sta, pReq, STA_WIFI_CONFIG, staWifiCfg); s2j_json_set_basic_element(json_sta, struct_sta, string, pSSID); s2j_json_set_basic_element(json_sta, struct_sta, string, pPassword); s2j_json_set_struct_element(json_softap, jObject, struct_softap, pReq, SOFTAP_WIFI_CONFIG, softapWifiCfg); s2j_json_set_basic_element(json_softap, struct_softap, int, authMode); s2j_json_set_basic_element(json_softap, struct_softap, int, svrPort); s2j_json_set_basic_element(json_softap, struct_softap, string, pPassword); pJsonS = cJSON_Print(jObject); cJSON_Delete(jObject); return pJsonS; } static void* __json2GblCfg(const char* pJsonS) { cJSON* pJson = cJSON_Parse(pJsonS); if(pJson == NULL) { return NULL; } s2j_create_struct_obj(sObject, GLOGAL_CONFIG); memset(sObject, 0, sizeof(GLOGAL_CONFIG)); s2j_struct_get_basic_element(sObject, pJson, string, pCfgTagsInfo); s2j_struct_get_struct_element(struct_sta, sObject, json_sta, pJson, STA_WIFI_CONFIG, staWifiCfg); s2j_struct_get_basic_element(struct_sta, json_sta, string, pSSID); s2j_struct_get_basic_element(struct_sta, json_sta, string, pPassword); s2j_struct_get_struct_element(struct_softap, sObject, json_softap, pJson, SOFTAP_WIFI_CONFIG, softapWifiCfg); s2j_struct_get_basic_element(struct_softap, json_softap, int, authMode); s2j_struct_get_basic_element(struct_softap, json_softap, int, svrPort); s2j_struct_get_basic_element(struct_softap, json_softap, string, pPassword); cJSON_Delete(pJson); return sObject; } static const char* __shadow2Json(void* pData) { PSHADOW_UPDATE pInfo = (PSHADOW_UPDATE)pData; cJSON *pRoot, *pStatus, *pReports; const char* pJsonS; pRoot = cJSON_CreateObject(); cJSON_AddNumberToObject(pRoot, "version", pInfo->version); cJSON_AddItemToObject(pRoot, "method", cJSON_CreateString(pInfo->method)); pStatus = cJSON_CreateObject(); pReports = cJSON_CreateObject(); cJSON_AddItemToObject(pRoot, "state", pStatus); cJSON_AddItemToObject(pStatus, "reported", pReports); cJSON_AddItemToObject(pReports, "mcuCmd", cJSON_CreateString(pInfo->state.reported.mcuCmd)); pJsonS = cJSON_Print(pRoot); cJSON_Delete(pRoot); return pJsonS; } static int __getIntValue(cJSON *pRoot, const char* pKey, int defVal) { cJSON* pRet = cJSON_GetObjectItem(pRoot, pKey); if(pRet == NULL) { return defVal; } else { return pRet->valueint; } } static int __getArrayIntValue(cJSON *pRoot, int idx, int defVal) { cJSON* pRet = cJSON_GetArrayItem(pRoot, idx); if(pRet == NULL) { return defVal; } else { return pRet->valueint; } } static char* __getStringValue(cJSON *pRoot, const char* pKey, const char* defVal) { cJSON* pRet = cJSON_GetObjectItem(pRoot, pKey); if(pRet == NULL) { return strdup(defVal); } else { return strdup(pRet->valuestring); } } static const char* __userCfg2Json(void* pData) { int i; const char* pJsonS; cJSON *pSvrCfg, *pSoftApCfg, *pWifiApCfg, *pUartCfg, *pDhcpCfg, *pGPIOCfg; PUSER_CONFIG pCfg = (PUSER_CONFIG)pData; cJSON* pRoot = cJSON_CreateObject(); if(pRoot == NULL) { LOG_EX(LOG_Error, "Create cJSON Error\n"); return NULL; } pSvrCfg = cJSON_CreateObject(); if(pSvrCfg == NULL) { LOG_EX(LOG_Error, "Create cJSON Error\n"); cJSON_Delete(pRoot); return NULL; } pSoftApCfg = cJSON_CreateObject(); if(pSoftApCfg == NULL) { LOG_EX(LOG_Error, "Create cJSON Error\n"); cJSON_Delete(pRoot); cJSON_Delete(pSvrCfg); return NULL; } pWifiApCfg = cJSON_CreateObject(); if(pWifiApCfg == NULL) { LOG_EX(LOG_Error, "Create cJSON Error\n"); cJSON_Delete(pRoot); cJSON_Delete(pSvrCfg); cJSON_Delete(pSoftApCfg); return NULL; } pUartCfg = cJSON_CreateArray(); if(pUartCfg == NULL) { LOG_EX(LOG_Error, "Create cJSON Error\n"); cJSON_Delete(pRoot); cJSON_Delete(pSvrCfg); cJSON_Delete(pSoftApCfg); cJSON_Delete(pWifiApCfg); return NULL; } pGPIOCfg = cJSON_CreateArray(); if(pGPIOCfg == NULL) { LOG_EX(LOG_Error, "Create cJSON Error\n"); cJSON_Delete(pRoot); cJSON_Delete(pSvrCfg); cJSON_Delete(pSoftApCfg); cJSON_Delete(pWifiApCfg); cJSON_Delete(pUartCfg); return NULL; } pDhcpCfg = cJSON_CreateObject(); if(pDhcpCfg == NULL) { LOG_EX(LOG_Error, "Create cJSON Error\n"); cJSON_Delete(pRoot); cJSON_Delete(pSvrCfg); cJSON_Delete(pSoftApCfg); cJSON_Delete(pUartCfg); cJSON_Delete(pGPIOCfg); cJSON_Delete(pWifiApCfg); return NULL; } cJSON_AddItemToObject(pRoot, "UsedDefault", cJSON_CreateNumber(pCfg->UsedDefault)); cJSON_AddItemToObject(pRoot, "Version", cJSON_CreateNumber(pCfg->Version)); cJSON_AddItemToObject(pRoot, "FactoryMode", cJSON_CreateNumber(pCfg->FactoryMode)); cJSON_AddItemToObject(pRoot, "DeviceType", cJSON_CreateNumber(pCfg->DeviceType)); cJSON_AddItemToObject(pRoot, "ServerCfg", pSvrCfg); cJSON_AddItemToObject(pRoot, "WIFIApConfig", pWifiApCfg); cJSON_AddItemToObject(pRoot, "SoftAP", pSoftApCfg); cJSON_AddItemToObject(pRoot, "UART", pUartCfg); cJSON_AddItemToObject(pRoot, "GPIO", pGPIOCfg); cJSON_AddItemToObject(pSvrCfg, "ProductKey", cJSON_CreateString(pCfg->ServerCfg.ProductKey)); cJSON_AddItemToObject(pSvrCfg, "ProductSecury", cJSON_CreateString(pCfg->ServerCfg.ProductSecury)); cJSON_AddItemToObject(pSvrCfg, "MQTTServer", cJSON_CreateString(pCfg->ServerCfg.MQTTServer)); cJSON_AddItemToObject(pSvrCfg, "MQTTPort", cJSON_CreateNumber(pCfg->ServerCfg.MQTTPort)); cJSON_AddItemToObject(pSvrCfg, "HTTPSServer", cJSON_CreateString(pCfg->ServerCfg.HTTPSServer)); cJSON_AddItemToObject(pWifiApCfg, "SSID", cJSON_CreateString(pCfg->WifiAP.pSSID)); cJSON_AddItemToObject(pWifiApCfg, "Password", cJSON_CreateString(pCfg->WifiAP.pPassword)); cJSON_AddItemToObject(pSoftApCfg, "softAPDHCP", pDhcpCfg); cJSON_AddItemToObject(pSoftApCfg, "UDPSvrPort", cJSON_CreateNumber(pCfg->SoftAP.UDPSvrPort)); cJSON_AddItemToObject(pSoftApCfg, "SoftAPFormat", cJSON_CreateString(pCfg->SoftAP.SoftAPFormat)); cJSON_AddItemToObject(pDhcpCfg, "IPAddr", cJSON_CreateString(pCfg->SoftAP.softAPDHCP.IPAddr)); cJSON_AddItemToObject(pDhcpCfg, "Netmask", cJSON_CreateString(pCfg->SoftAP.softAPDHCP.Netmask)); cJSON_AddItemToObject(pDhcpCfg, "Gatway", cJSON_CreateString(pCfg->SoftAP.softAPDHCP.Gatway)); cJSON_AddItemToObject(pDhcpCfg, "DHCPStartIP", cJSON_CreateString(pCfg->SoftAP.softAPDHCP.DHCPStartIP)); cJSON_AddItemToObject(pDhcpCfg, "DHCPEndIP", cJSON_CreateString(pCfg->SoftAP.softAPDHCP.DHCPEndIP)); for(i = 0; i < 2; i++) { cJSON* pUartItem = cJSON_CreateObject(); if(pUartItem == NULL) { LOG_EX(LOG_Error, "Create cJSON Object Error\n"); cJSON_Delete(pRoot); return NULL; } cJSON_AddItemToObject(pUartItem, "Baud", cJSON_CreateNumber(pCfg->UART[i].Baud)); cJSON_AddItemToObject(pUartItem, "DataBits", cJSON_CreateNumber(pCfg->UART[i].DataBits)); cJSON_AddItemToObject(pUartItem, "StopBits", cJSON_CreateNumber(pCfg->UART[i].StopBits)); cJSON_AddItemToObject(pUartItem, "Parity", cJSON_CreateNumber(pCfg->UART[i].Parity)); cJSON_AddItemToObject(pUartItem, "FlowCtrl", cJSON_CreateNumber(pCfg->UART[i].FlowCtrl)); cJSON_AddItemToArray(pUartCfg, pUartItem); } for(i = 0; i < pCfg->GPIO.iSize && pCfg->GPIO.pGpioCfg; i++) { cJSON* pGpioItem = cJSON_CreateObject(); if(pGpioItem == NULL) { LOG_EX(LOG_Error, "Create cJSON Object Error\n"); cJSON_Delete(pRoot); return NULL; } cJSON_AddItemToObject(pGpioItem, "Id", cJSON_CreateNumber(pCfg->GPIO.pGpioCfg[i].Id)); cJSON_AddItemToObject(pGpioItem, "Func", cJSON_CreateNumber(pCfg->GPIO.pGpioCfg[i].Func)); cJSON_AddItemToObject(pGpioItem, "Dir", cJSON_CreateNumber(pCfg->GPIO.pGpioCfg[i].Dir)); cJSON_AddItemToObject(pGpioItem, "PullUp", cJSON_CreateNumber(pCfg->GPIO.pGpioCfg[i].PullUp)); cJSON_AddItemToObject(pGpioItem, "Interrupt", cJSON_CreateNumber(pCfg->GPIO.pGpioCfg[i].Interrupt)); cJSON_AddItemToArray(pGPIOCfg, pGpioItem); } pJsonS = cJSON_Print(pRoot); cJSON_Delete(pRoot); return pJsonS; } static void* __json2UserCfg(const char* pJsonS) { cJSON* pItem = NULL; PUSER_CONFIG pCfg = NULL; cJSON *pRoot = cJSON_Parse(pJsonS); if(pRoot == NULL) { LOG_EX(LOG_Error, "Json Format Error: [%s]\n", pJsonS); return NULL; } pCfg = (PUSER_CONFIG)malloc(sizeof(USER_CONFIG)); if(pCfg == NULL) { LOG_EX(LOG_Error, "Malloc CLOUND_API Error\n"); cJSON_Delete(pRoot); return NULL; } memset(pCfg, 0, sizeof(USER_CONFIG)); pCfg->Version = __getIntValue(pRoot, "Version", USER_CFG_VERSION); pCfg->FactoryMode = __getIntValue(pRoot, "FactoryMode", 0); pCfg->DeviceType = __getIntValue(pRoot, "DeviceType", 1); pItem = cJSON_GetObjectItem(pRoot, "ServerCfg"); if(pItem) { pCfg->ServerCfg.ProductKey = __getStringValue(pItem, "ProductKey", "3cfb69e088527ef73941b9b74ba1cd1b"); pCfg->ServerCfg.ProductSecury = __getStringValue(pItem, "ProductSecury", "ce3f33a99a86"); pCfg->ServerCfg.MQTTServer = __getStringValue(pItem, "MQTTServer", "link-iot.netease.im"); pCfg->ServerCfg.MQTTPort = __getIntValue(pItem, "MQTTPort", 8680); pCfg->ServerCfg.HTTPSServer = __getStringValue(pItem, "HTTPSServer", "https://api-iot.netease.im/iot/RegisterDevice"); } pItem = cJSON_GetObjectItem(pRoot, "WIFIApConfig"); if(pItem) { pCfg->WifiAP.pSSID = __getStringValue(pItem, "SSID", ""); pCfg->WifiAP.pPassword = __getStringValue(pItem, "Password", ""); } pItem = cJSON_GetObjectItem(pRoot, "SoftAP"); if(pItem) { pCfg->SoftAP.UDPSvrPort = __getIntValue(pItem, "UDPSvrPort", 10086); pCfg->SoftAP.SoftAPFormat = __getStringValue(pItem, "SoftAPFormat", "NetEase_%%02x%%02x"); cJSON* pObj = cJSON_GetObjectItem(pItem, "SoftAPDHCP"); if(pObj) { pCfg->SoftAP.softAPDHCP.IPAddr = __getStringValue(pObj, "IPAddr", "192.168.5.1"); pCfg->SoftAP.softAPDHCP.Netmask = __getStringValue(pObj, "Netmask", "255.255.255.0"); pCfg->SoftAP.softAPDHCP.Gatway = __getStringValue(pObj, "Gatway", "192.168.5.1"); pCfg->SoftAP.softAPDHCP.DHCPStartIP = __getStringValue(pObj, "DHCPStartIP", "192.168.5.100"); pCfg->SoftAP.softAPDHCP.DHCPEndIP = __getStringValue(pObj, "DHCPEndIP", "192.168.5.105"); } } pItem = cJSON_GetObjectItem(pRoot, "UART"); if(pItem) { int i = 0; int arraySize = cJSON_GetArraySize(pItem); cJSON *pList = pItem->child; for(i = 0; i < arraySize; i++) { pCfg->UART[i].Baud = __getIntValue(pList, "Baud", 115200); pCfg->UART[i].DataBits = __getIntValue(pList, "DataBits", 8); pCfg->UART[i].StopBits = __getIntValue(pList, "StopBits", 1); pCfg->UART[i].Parity = __getIntValue(pList, "Parity", 2); pCfg->UART[i].FlowCtrl = __getIntValue(pList, "FlowCtrl", 0); pList = pList->next; } } pItem = cJSON_GetObjectItem(pRoot, "GPIO"); if(pItem) { int i = 0; int arraySize = cJSON_GetArraySize(pItem); cJSON *pList = pItem->child; if(arraySize > 0 && arraySize < USER_GPIO_MAX) { pCfg->GPIO.pGpioCfg = (PGPIO_ITEM)malloc(sizeof(GPIO_ITEM) * arraySize); if(pCfg->GPIO.pGpioCfg == NULL) { LOG_EX(LOG_Error, "Malloc Error Error: %d\n", arraySize); } } pCfg->GPIO.iSize = 0; for(i = 0; i < arraySize && pCfg->GPIO.pGpioCfg; i++) { LOG_EX(LOG_Debug, "i = %d, arraySize = %d, item = %d\n", i, arraySize, pCfg->GPIO.iSize); pCfg->GPIO.pGpioCfg[i].Id = __getIntValue(pList, "Id", -1); pCfg->GPIO.pGpioCfg[i].Func = __getIntValue(pList, "Func", -1); pCfg->GPIO.pGpioCfg[i].Dir = __getIntValue(pList, "Dir", -1); pCfg->GPIO.pGpioCfg[i].PullUp = __getIntValue(pList, "PullUp", -1); pCfg->GPIO.pGpioCfg[i].Interrupt = __getIntValue(pList, "Interrupt", -1); pCfg->GPIO.iSize++; pList = pList->next; } } cJSON_Delete(pRoot); return pCfg; } static const char* __gpio2Json(void* pData) { int i; const char* pJsonS; PGPIO_INFO pInfo = (PGPIO_INFO)pData; cJSON* pGpio = NULL; cJSON* pRoot = cJSON_CreateObject(); if(pRoot == NULL) { LOG_EX(LOG_Error, "Create cJSON Error\n"); return NULL; } pGpio = cJSON_CreateArray(); if(pGpio == NULL) { LOG_EX(LOG_Error, "Create cJSON Error\n"); cJSON_Delete(pRoot); return NULL; } cJSON_AddItemToObject(pRoot, "gpio", pGpio); for(i = 0; i < pInfo->iSize && pInfo->pGpio; i++) { int id = pInfo->pGpio[i].id; int cmd = pInfo->pGpio[i].cmd; cJSON* pGpioItem = cJSON_CreateObject(); if(pGpioItem == NULL) { LOG_EX(LOG_Error, "Create cJSON Object Error\n"); continue; } if(id < USER_GPIO_4 || id >= USER_GPIO_MAX) { LOG_EX(LOG_Error, "Bad GPIO id [%d]\n", id); continue; } if(cmd < GPIO_CMD_CFG || cmd > GPIO_CMD_MAX) { LOG_EX(LOG_Error, "Bad GPIO cmd [%d --> %d]\n", id, cmd); continue; } cJSON_AddItemToObject(pGpioItem, "id", cJSON_CreateNumber(id)); cJSON_AddItemToObject(pGpioItem, "cmd", cJSON_CreateNumber(cmd)); cJSON_AddItemToObject(pGpioItem, "err", cJSON_CreateNumber(pInfo->pGpio[i].err)); if(cmd == GPIO_CMD_INPUT) { cJSON_AddItemToObject(pGpioItem, "val", cJSON_CreateNumber(pInfo->pGpio[i].val)); } } pJsonS = cJSON_Print(pRoot); cJSON_Delete(pRoot); return pJsonS; } static void* __json2Gpio(const char* pJsonS) { int i, arraySize = 0; cJSON* pItem = NULL; cJSON* pList = NULL; PGPIO_INFO pInfo = NULL; cJSON *pRoot = cJSON_Parse(pJsonS); if(pRoot == NULL) { LOG_EX(LOG_Error, "Json Format Error: [%s]\n", pJsonS); return NULL; } pInfo = (PGPIO_INFO)malloc(sizeof(GPIO_INFO)); if(pInfo == NULL) { LOG_EX(LOG_Error, "Malloc GPIO_INFO Error\n"); cJSON_Delete(pRoot); return NULL; } pItem = cJSON_GetObjectItem(pRoot, "gpio"); if(pItem == NULL) { LOG_EX(LOG_Error, "Items is empty:[%s]\n", pJsonS); cJSON_Delete(pRoot); return NULL; } arraySize = cJSON_GetArraySize(pItem); if(arraySize <= 0) { LOG_EX(LOG_Error, "Items is empty(%d):[%s]\n", arraySize, pJsonS); cJSON_Delete(pRoot); return NULL; } pInfo->pGpio = (PGPIO_VALUE)malloc(sizeof(GPIO_VALUE) * arraySize); if(pInfo->pGpio == NULL) { LOG_EX(LOG_Error, "Malloc GPIO_VALUE Error(%d)\n", arraySize); cJSON_Delete(pRoot); free(pInfo); return NULL; } pList = pItem->child; pInfo->iSize = arraySize; for(i = 0; i < arraySize; i++) { pInfo->pGpio[i].id = __getIntValue(pList, "id", USER_GPIO_MAX); pInfo->pGpio[i].cmd = __getIntValue(pList, "cmd", -1); if(pInfo->pGpio[i].cmd == 0) { cJSON* pArray = cJSON_GetObjectItem(pList, "cfgParams"); int nItem = cJSON_GetArraySize(pArray); if(nItem == 4 && pArray) { pInfo->pGpio[i].cfgParams[0] = __getArrayIntValue(pArray, 0, 0); pInfo->pGpio[i].cfgParams[1] = __getArrayIntValue(pArray, 1, 0); pInfo->pGpio[i].cfgParams[2] = __getArrayIntValue(pArray, 2, 0); pInfo->pGpio[i].cfgParams[3] = __getArrayIntValue(pArray, 3, 0); } else { LOG_EX(LOG_Error, "Decode Array Error: nItem = %d, pArray = %p\n", nItem, pArray); } } else if(pInfo->pGpio[i].cmd == 1) { pInfo->pGpio[i].val = __getIntValue(pList, "val", -1); } pList = pList->next; } cJSON_Delete(pRoot); return pInfo; } static JSON_ENGINE g_jSonEngine[] = { {JE_PROMAIN, __pro2Json, __json2pro, NULL}, {JE_BYPASS, __bypass2Json, __json2bypass, NULL}, {JE_SHADOWUP, __shadow2Json, NULL, NULL}, {JE_GETID, __devId2Json, NULL, NULL}, {JE_CFG_AP, NULL, __json2CfgAp, NULL}, {JE_RSP_STATUS, __errno2Json, NULL, NULL}, {JE_GLOBAL_CFG, __gblCfg2Json, __json2GblCfg, NULL}, {JE_USER_CFG, __userCfg2Json, __json2UserCfg, NULL}, {JE_USER_CFG, __gpio2Json, __json2Gpio, NULL}, }; void* Json2Struct(const char* pJsonStr, JSON_ENGINE_TYPE type, int cryptoType, int* pErr) { if(pJsonStr == NULL || pErr == NULL) { if(pErr) { *pErr = -ERR_INPUT_PARAMS; } return NULL; } if(type < 0 || type >= JE_MAX) { *pErr = -ERR_INPUT_PARAMS; return NULL; } *pErr = 0; //LOG_EX(LOG_Debug, "Json:\n%s\n", pJsonStr); if(g_jSonEngine[type].jsonDecodeCb) { void *pStruct = g_jSonEngine[type].jsonDecodeCb(pJsonStr); if(CRYPTO_NONE != cryptoType && g_jSonEngine[type].cryptoCb) { g_jSonEngine[type].cryptoCb(pStruct, cryptoType, CRYPTO_ENCODE); } return (pStruct); } else { return (NULL); } } const char* Struct2Json(void* pStruct, JSON_ENGINE_TYPE type, int cryptoType, int* pErr) { if(pStruct == NULL || pErr == NULL) { if(pErr) { *pErr = -ERR_INPUT_PARAMS; } return NULL; } if(type < 0 || type >= JE_MAX) { *pErr = -ERR_INPUT_PARAMS; return NULL; } *pErr = 0; if(CRYPTO_NONE != cryptoType && g_jSonEngine[type].cryptoCb) { g_jSonEngine[type].cryptoCb(pStruct, cryptoType, CRYPTO_DECODE); } if(g_jSonEngine[type].jsonEncodeCb) { const char *pJsongStr = g_jSonEngine[type].jsonEncodeCb(pStruct); //LOG_EX(LOG_Debug, "Json: \n%s\n", pJsongStr); return (pJsongStr); } else { return NULL; } }