156 lines
3.4 KiB
C
156 lines
3.4 KiB
C
//
|
|
// Created by xajhu on 2019/11/19 0019.
|
|
//
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <cjson/cJSON.h>
|
|
|
|
#include "common.h"
|
|
#include "err_code.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;
|
|
|
|
static const char *__ztp_auth_encode(void *pData)
|
|
{
|
|
const char *pJsonS;
|
|
PAUTH_ZTH_REQ p = (PAUTH_ZTH_REQ)pData;
|
|
cJSON *pRoot = cJSON_CreateObject();
|
|
|
|
if(!p) {
|
|
return NULL;
|
|
}
|
|
|
|
if(p->pESN && strlen(p->pESN) > 0) {
|
|
cJSON_AddStringToObject(pRoot, "ESN", p->pESN);
|
|
}
|
|
|
|
pJsonS = cJSON_Print(pRoot);
|
|
cJSON_Delete(pRoot);
|
|
return pJsonS;
|
|
}
|
|
|
|
static int __ztp_auth_decode(const char *pJsonS, void **pStruct)
|
|
{
|
|
cJSON *pRoot, *pVal;
|
|
PAUTH_ZTH_RSP pData;
|
|
*pStruct = malloc(sizeof(AUTH_ZTH_RSP));
|
|
|
|
if(*pStruct == NULL) {
|
|
return -ERR_INPUTERR;
|
|
}
|
|
|
|
pData = (PAUTH_ZTH_RSP) * pStruct;
|
|
pRoot = cJSON_Parse(pJsonS);
|
|
|
|
if(!pRoot) {
|
|
return -ERR_JSONERR;
|
|
}
|
|
|
|
memset(pData, 0, sizeof(AUTH_ZTH_RSP));
|
|
pVal = cJSON_GetObjectItem(pRoot, "status");
|
|
|
|
if(cJSON_IsString(pVal)) {
|
|
pData->status = strdup(pVal->valuestring);
|
|
}
|
|
|
|
pVal = cJSON_GetObjectItem(pRoot, "iptype");
|
|
|
|
if(cJSON_IsString(pVal)) {
|
|
pData->iptype = (int)strtoul(pVal->valuestring, 0, 10);
|
|
}
|
|
|
|
pVal = cJSON_GetObjectItem(pRoot, "ip");
|
|
|
|
if(cJSON_IsString(pVal)) {
|
|
pData->ip = strdup(pVal->valuestring);
|
|
//(unsigned int)strtoul(pVal->valuestring, 0, 10);
|
|
}
|
|
|
|
pVal = cJSON_GetObjectItem(pRoot, "code");
|
|
|
|
if(cJSON_IsString(pVal)) {
|
|
pData->code = strdup(pVal->valuestring);
|
|
}
|
|
|
|
cJSON_Delete(pRoot);
|
|
return ERR_OK;
|
|
}
|
|
|
|
|
|
static JSON_ENGINE g_jSonEngine[] = {
|
|
{JE_AUTH_ZTP, __ztp_auth_encode, __ztp_auth_decode, NULL},
|
|
{JSON_ENGINE_MAX, NULL, NULL, NULL}
|
|
};
|
|
|
|
|
|
int Json2Struct(const char *pJsonStr, void *pData, JSON_ENGINE_TYPE type,
|
|
int enBase64)
|
|
{
|
|
if(pJsonStr == NULL || pData == NULL) {
|
|
return -ERR_INPUTERR;
|
|
}
|
|
|
|
if(type < 0 || type >= JSON_ENGINE_MAX) {
|
|
return -ERR_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 ERR_OK;
|
|
} else {
|
|
return -ERR_NOTSUPPORT;
|
|
}
|
|
}
|
|
|
|
const char *Struct2Json(void *pStruct, JSON_ENGINE_TYPE type, int enBase64,
|
|
int *pErr)
|
|
{
|
|
if(pStruct == NULL || pErr == NULL) {
|
|
if(pErr) {
|
|
*pErr = -ERR_INPUTERR;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
if(type < 0 || type >= JSON_ENGINE_MAX) {
|
|
*pErr = -ERR_INPUTERR;
|
|
return NULL;
|
|
}
|
|
|
|
*pErr = ERR_OK;
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
|
|
|