357 lines
9.7 KiB
C
357 lines
9.7 KiB
C
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <cjson/cJSON.h>
|
|
|
|
#include "log.h"
|
|
#include "json_interface.h"
|
|
|
|
typedef const char *(*StructToJsonCb)(void *pStruct);
|
|
typedef int (*JsonToStructCb)(const char *pJsonStr, void **pStruct);
|
|
typedef void (*Base64Code)(void *pStruct, int enCode);
|
|
|
|
typedef struct {
|
|
JSON_ENGINE_TYPE typeId;
|
|
StructToJsonCb s2jCb;
|
|
JsonToStructCb j2sCb;
|
|
Base64Code base64Cb;
|
|
} JSON_ENGINE, *PJSON_ENGINE;
|
|
|
|
static const char *__interface_encode(void *pData)
|
|
{
|
|
const char *pJsonS;
|
|
PJSON_INTERFACE p = (PJSON_INTERFACE)pData;
|
|
cJSON *pRoot = cJSON_CreateObject();
|
|
|
|
if(!p) {
|
|
return NULL;
|
|
}
|
|
|
|
cJSON_AddNumberToObject(pRoot, "cmdId", p->cmdId);
|
|
cJSON_AddNumberToObject(pRoot, "ver", INTERFACE_VERSINO);
|
|
cJSON_AddNumberToObject(pRoot, "cryptoType", p->cryptoType);
|
|
cJSON_AddNumberToObject(pRoot, "timeStamp", time((time_t *)NULL));
|
|
|
|
if(p->msgContent) {
|
|
cJSON_AddStringToObject(pRoot, "msgContent", p->msgContent);
|
|
}
|
|
|
|
pJsonS = cJSON_Print(pRoot);
|
|
cJSON_Delete(pRoot);
|
|
|
|
return pJsonS;
|
|
}
|
|
|
|
static int __interface_decode(const char *pJsonS, void **pStruct)
|
|
{
|
|
cJSON *pRoot;
|
|
PJSON_INTERFACE pData;
|
|
|
|
*pStruct = malloc(sizeof(JSON_INTERFACE));
|
|
|
|
if(*pStruct == NULL) {
|
|
return -RET_INPUTERR;
|
|
}
|
|
|
|
pData = (PJSON_INTERFACE) * pStruct;
|
|
|
|
pRoot = cJSON_Parse(pJsonS);
|
|
|
|
if(!pRoot) {
|
|
return -RET_SYSERR;
|
|
}
|
|
|
|
memset(pData, 0, sizeof(JSON_INTERFACE));
|
|
|
|
pData->cmdId = cJSON_GetObjectItem(pRoot, "cmdId")->valueint;
|
|
pData->ver = cJSON_GetObjectItem(pRoot, "ver")->valueint;
|
|
pData->cryptoType = cJSON_GetObjectItem(pRoot, "cryptoType")->valueint;
|
|
pData->timeStamp = cJSON_GetObjectItem(pRoot, "timeStamp")->valueint;
|
|
|
|
if(cJSON_HasObjectItem(pRoot, "msgContent")) {
|
|
pData->msgContent = strdup(cJSON_GetObjectItem(pRoot,
|
|
"msgContent")->valuestring);
|
|
}
|
|
|
|
cJSON_Delete(pRoot);
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
static const char *__obj_add_encode(void *pData)
|
|
{
|
|
const char *pJsonS;
|
|
PIFC_RET_MSG p = (PIFC_RET_MSG)pData;
|
|
cJSON *pRoot = cJSON_CreateObject();
|
|
|
|
if(!p) {
|
|
return NULL;
|
|
}
|
|
|
|
cJSON_AddNumberToObject(pRoot, "retCode", p->ret_code);
|
|
|
|
if(p->mesg) {
|
|
cJSON_AddStringToObject(pRoot, "mesg", p->mesg);
|
|
}
|
|
|
|
if(p->n_items > 0) {
|
|
int i;
|
|
cJSON *pArray = cJSON_AddArrayToObject(pRoot, "data");
|
|
|
|
if(pArray) {
|
|
for(i = 0; i < p->n_items; i++) {
|
|
cJSON *pItem = cJSON_CreateObject();
|
|
|
|
if(!pItem) {
|
|
continue;
|
|
}
|
|
|
|
cJSON_AddStringToObject(pItem, "name", p->data[i].name);
|
|
cJSON_AddNumberToObject(pItem, "ret_code", p->data[i].ret_code);
|
|
|
|
if(p->data[i].mesg) {
|
|
cJSON_AddStringToObject(pItem, "mesg", p->data[i].mesg);
|
|
}
|
|
|
|
cJSON_AddItemToArray(pArray, pItem);
|
|
}
|
|
}
|
|
}
|
|
|
|
pJsonS = cJSON_Print(pRoot);
|
|
cJSON_Delete(pRoot);
|
|
|
|
return pJsonS;
|
|
}
|
|
|
|
static int __obj_add_decode(const char *pJsonS, void **pStruct)
|
|
{
|
|
cJSON *pRoot;
|
|
PIFACE_ADD_OBJ pData;
|
|
int arraySize;
|
|
int i, j;
|
|
|
|
*pStruct = malloc(sizeof(IFACE_ADD_OBJ));
|
|
|
|
if(*pStruct == NULL) {
|
|
return -RET_INPUTERR;
|
|
}
|
|
|
|
pData = (PIFACE_ADD_OBJ) * pStruct;
|
|
|
|
pRoot = cJSON_Parse(pJsonS);
|
|
|
|
if(!pRoot) {
|
|
return -RET_SYSERR;
|
|
}
|
|
|
|
memset(pData, 0, sizeof(IFACE_ADD_OBJ));
|
|
|
|
arraySize = cJSON_GetArraySize(pRoot);
|
|
|
|
pData->pCtx = (PIFC_ADD_CTX)malloc(sizeof(IFC_ADD_CTX) * arraySize);
|
|
|
|
if(pData->pCtx == NULL) {
|
|
LOG_EX(LOG_Error, "Malloc memory %d error\n", sizeof(IFC_ADD_CTX) * arraySize);
|
|
cJSON_Delete(pRoot);
|
|
return -RET_NOMEM;
|
|
}
|
|
|
|
memset(pData->pCtx, 0, sizeof(IFC_ADD_CTX) * arraySize);
|
|
|
|
for(i = 0; i < arraySize; i++) {
|
|
pData->pCtx[i].pObj = malloc(sizeof(CMHI_OBJECT));
|
|
|
|
if(pData->pCtx[i].pObj == NULL) {
|
|
for(j = 0; j < i; j++) {
|
|
if(pData->pCtx[j].pObj) {
|
|
free(pData->pCtx[j].pObj);
|
|
}
|
|
}
|
|
LOG_EX(LOG_Error, "Malloc memory (%d):%d error\n", i,
|
|
sizeof(CMHI_OBJECT) * arraySize);
|
|
cJSON_Delete(pRoot);
|
|
free(pData->pCtx);
|
|
return -RET_NOMEM;
|
|
}
|
|
|
|
memset(pData->pCtx[i].pObj, 0, sizeof(CMHI_OBJECT));
|
|
}
|
|
|
|
pData->n_obj = arraySize;
|
|
|
|
for(i = 0; i < arraySize; i++) {
|
|
PCMHI_OBJECT pObj = pData->pCtx[i].pObj;
|
|
POBJECT_K pObjK = &pData->pCtx[i].objk;
|
|
cJSON *pSub = cJSON_GetArrayItem(pRoot, i);
|
|
cJSON *pSubContent = cJSON_GetObjectItem(pSub, "objContent");
|
|
|
|
if(!pSubContent) {
|
|
LOG_EX(LOG_Error, "Don't found (objContent) content\n");
|
|
cJSON_Delete(pRoot);
|
|
for(j = 0; j < arraySize; j++) {
|
|
if(pData->pCtx[j].pObj) {
|
|
free(pData->pCtx[j].pObj);
|
|
}
|
|
}
|
|
free(pData->pCtx);
|
|
return -RET_INPUTERR;
|
|
}
|
|
|
|
pObjK->ctx_num = cJSON_GetArraySize(pSubContent);
|
|
|
|
if(pObjK->ctx_num <= 0) {
|
|
LOG_EX(LOG_Error, "The objContent has no member\n");
|
|
for(j = 0; j < arraySize; j++) {
|
|
if(pData->pCtx[j].pObj) {
|
|
free(pData->pCtx[j].pObj);
|
|
}
|
|
}
|
|
free(pData->pCtx);
|
|
cJSON_Delete(pRoot);
|
|
return -RET_INPUTERR;
|
|
}
|
|
|
|
pObj->prio = cJSON_GetObjectItem(pSub, "prio")->valueint;
|
|
pObjK->type = cJSON_GetObjectItem(pSub, "type")->valueint;
|
|
strncpy(pObj->name, cJSON_GetObjectItem(pSub, "name")->valuestring,
|
|
MAX_NAME_LEN - 1);
|
|
strncpy(pObj->desc, cJSON_GetObjectItem(pSub, "desc")->valuestring,
|
|
MAX_DESC - 1);
|
|
|
|
for(j = 0; j < pObjK->ctx_num; j++) {
|
|
cJSON *pItem = cJSON_GetArrayItem(pSubContent, j);
|
|
|
|
switch(pObjK->type / 10) {
|
|
case OBJ_TYPE_SERVER:
|
|
pObjK->objs.svr_obj[j].pro_type = cJSON_GetObjectItem(pItem,
|
|
"proType")->valueint;
|
|
pObjK->objs.svr_obj[j].str_val = strdup(cJSON_GetObjectItem(pItem,
|
|
"porPort")->valuestring);
|
|
break;
|
|
|
|
case OBJ_TYPE_ADDR:
|
|
pObjK->objs.addr_obj[j].str_val = strdup(cJSON_GetObjectItem(pItem,
|
|
"ipAddr")->valuestring);
|
|
break;
|
|
|
|
case OBJ_TYPE_DATETIME:
|
|
pObjK->objs.dt_obj[j].rep_mode = cJSON_GetObjectItem(pItem,
|
|
"repType")->valueint;
|
|
pObjK->objs.dt_obj[j].str_val = strdup(cJSON_GetObjectItem(pItem,
|
|
"timeValue")->valuestring);
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
cJSON_Delete(pRoot);
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
static int __obj_del_decode(const char *pJsonS, void **pStruct)
|
|
{
|
|
cJSON *pRoot;
|
|
PIFACE_DEL_OBJ pData;
|
|
int arraySize;
|
|
int i, j;
|
|
|
|
*pStruct = malloc(sizeof(IFACE_DEL_OBJ));
|
|
|
|
if(*pStruct == NULL) {
|
|
return -RET_INPUTERR;
|
|
}
|
|
|
|
pData = (PIFACE_DEL_OBJ) * pStruct;
|
|
|
|
pRoot = cJSON_Parse(pJsonS);
|
|
|
|
if(!pRoot) {
|
|
return -RET_SYSERR;
|
|
}
|
|
|
|
memset(pData, 0, sizeof(IFACE_ADD_OBJ));
|
|
|
|
arraySize = cJSON_GetArraySize(pRoot);
|
|
|
|
pData->n_obj = arraySize;
|
|
|
|
for(i = 0; i < arraySize; i++) {
|
|
cJSON *pSub = cJSON_GetArrayItem(pRoot, i);
|
|
strncpy(pData->name[i], cJSON_GetObjectItem(pSub, "name")->valuestring,
|
|
MAX_NAME_LEN - 1);
|
|
}
|
|
|
|
cJSON_Delete(pRoot);
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
static JSON_ENGINE g_jSonEngine[] = {
|
|
{JE_INTERFACE, __interface_encode, __interface_decode, NULL},
|
|
{OBJ_CMD_ADD, __obj_add_encode, __obj_add_decode, NULL},
|
|
{JE_OBJ_MOD, NULL, NULL, NULL},
|
|
{JE_OBJ_DEL, NULL, __obj_del_decode, NULL}
|
|
};
|
|
|
|
int Json2Struct(const char *pJsonStr, void *pData, JSON_ENGINE_TYPE type,
|
|
int enBase64)
|
|
{
|
|
if(pJsonStr == NULL || pData == NULL) {
|
|
return -RET_INPUTERR;
|
|
}
|
|
|
|
if(type < 0 || type >= JSON_ENGINE_MAX) {
|
|
return -RET_INPUTERR;
|
|
}
|
|
|
|
//LOG_EX(LOG_Debug, "Json:\n%s\n", pJsonStr);
|
|
|
|
if(g_jSonEngine[type].j2sCb) {
|
|
g_jSonEngine[type].j2sCb(pJsonStr, pData);
|
|
|
|
if(enBase64 && g_jSonEngine[type].base64Cb) {
|
|
g_jSonEngine[type].base64Cb(pData, FALSE);
|
|
}
|
|
|
|
return RET_OK;
|
|
} else {
|
|
return -RET_NOTSUPPORT;
|
|
}
|
|
}
|
|
|
|
const char *Struct2Json(void *pStruct, JSON_ENGINE_TYPE type, int enBase64,
|
|
int *pErr)
|
|
{
|
|
if(pStruct == NULL || pErr == NULL) {
|
|
if(pErr) {
|
|
*pErr = -RET_INPUTERR;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
if(type < 0 || type >= JSON_ENGINE_MAX) {
|
|
*pErr = -RET_INPUTERR;
|
|
return NULL;
|
|
}
|
|
|
|
*pErr = 0;
|
|
|
|
if(enBase64 && g_jSonEngine[type].base64Cb) {
|
|
g_jSonEngine[type].base64Cb(pStruct, TRUE);
|
|
}
|
|
|
|
if(g_jSonEngine[type].s2jCb) {
|
|
const char *pJsongStr = g_jSonEngine[type].s2jCb(pStruct);
|
|
|
|
//LOG_EX(LOG_Debug, "Json: \n%s\n", pJsongStr);
|
|
return (pJsongStr);
|
|
} else {
|
|
return NULL;
|
|
}
|
|
}
|