1. Add protocol json encode/decode

2. Add check string is json string function
This commit is contained in:
HuangXin 2019-01-16 17:26:30 +08:00
parent b139124166
commit 93b1829c5d
3 changed files with 72 additions and 2 deletions

View File

@ -169,7 +169,7 @@ static const char* __shadow2Json(void* pData)
return pJsonS; return pJsonS;
} }
static void* json2RegDev(const char* pJsonS) static void* __json2RegDev(const char* pJsonS)
{ {
PREGIST_DEVICE pInfo = NULL; PREGIST_DEVICE pInfo = NULL;
cJSON *pItem = NULL; cJSON *pItem = NULL;
@ -210,6 +210,7 @@ static void* json2RegDev(const char* pJsonS)
return pInfo; return pInfo;
} }
#if 0
static void* __json2ProRsp(const char* pJsonS) static void* __json2ProRsp(const char* pJsonS)
{ {
PPRO_RESPONSE pRsp = NULL; PPRO_RESPONSE pRsp = NULL;
@ -450,18 +451,68 @@ static void* __json2ProGetDevList(const char* pJsonS)
cJSON_Delete(pRoot); cJSON_Delete(pRoot);
return pRsp; return pRsp;
} }
#endif
static void* __json2RouterCmd(const char* pJsonS)
{
PROUTER_CMD pInfo = NULL;
cJSON *pRoot = cJSON_Parse(pJsonS);
if(pRoot == NULL)
{
LOG_EX(LOG_Error, "Json Format Error: [%s]\n", pJsonS);
return NULL;
}
pInfo = (PROUTER_CMD)malloc(sizeof(ROUTER_CMD));
if(pInfo == NULL)
{
LOG_EX(LOG_Error, "Malloc BYPASS_INFO Error\n");
cJSON_Delete(pRoot);
return NULL;
}
pInfo->cmd = __jsonSafeDecodeStr(pRoot, "cmd");
pInfo->data = __jsonSafeDecodeStr(pRoot, "data");
cJSON_Delete(pRoot);
return pInfo;
}
static const char* __routerRsp2Json(void* pData)
{
PROUTER_RSP pInfo = (PROUTER_RSP)pData;
const char* pJsonS;
s2j_create_json_obj(jObject);
if(pInfo->data)
{
s2j_json_set_basic_element(jObject, pInfo, string, data);
}
pJsonS = cJSON_Print(jObject);
cJSON_Delete(jObject);
return pJsonS;
}
static JSON_ENGINE g_jSonEngine[] = static JSON_ENGINE g_jSonEngine[] =
{ {
{JE_PROMAIN, __pro2Json, __json2pro, NULL}, {JE_PROMAIN, __pro2Json, __json2pro, NULL},
{JE_BYPASS, __bypass2Json, __json2bypass, NULL}, {JE_BYPASS, __bypass2Json, __json2bypass, NULL},
{JE_SHADOWUP, __shadow2Json, NULL, NULL}, {JE_SHADOWUP, __shadow2Json, NULL, NULL},
{JE_REGDEVICE, NULL, json2RegDev, NULL}, {JE_REGDEVICE, NULL, __json2RegDev, NULL},
{JE_ROUTERCMD, NULL, __json2RouterCmd, NULL},
{JE_ROUTERRSP, __routerRsp2Json, NULL, NULL},
#if 0
{JE_PRO_GETVERSION, NULL, __json2ProGetVer, NULL}, {JE_PRO_GETVERSION, NULL, __json2ProGetVer, NULL},
{JE_PRO_ADMINLOGON, __logon2Json, __json2Prologon, NULL}, {JE_PRO_ADMINLOGON, __logon2Json, __json2Prologon, NULL},
{JE_PRO_GETSPEED, NULL, __json2ProGetSpeed, NULL}, {JE_PRO_GETSPEED, NULL, __json2ProGetSpeed, NULL},
{JE_PRO_GETDEVLIST, NULL, __json2ProGetDevList, NULL}, {JE_PRO_GETDEVLIST, NULL, __json2ProGetDevList, NULL},
#endif
// {, , , NULL}, // {, , , NULL},
}; };
@ -539,3 +590,16 @@ const char* Struct2Json(void* pStruct, JSON_ENGINE_TYPE type, int cryptoType, in
return NULL; return NULL;
} }
} }
int json_is_valid(char* pJsonS)
{
cJSON *pRoot = cJSON_Parse(pJsonS);
if(pRoot == NULL)
{
return FALSE;
}
cJSON_Delete(pRoot);
return TRUE;
}

View File

@ -1,6 +1,8 @@
#ifndef HAL_MTK_H #ifndef HAL_MTK_H
#define HAL_MTK_H #define HAL_MTK_H
#include <uthash/utstring.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -24,11 +26,14 @@ const char* hal_get_device_name(void);
int hal_device_name_to_id(void); int hal_device_name_to_id(void);
const char* hal_get_device_secret(void); const char* hal_get_device_secret(void);
int hal_save_device_register_info(unsigned char* pRegInfo, int iSize); int hal_save_device_register_info(unsigned char* pRegInfo, int iSize);
int hal_cleanup_device_register_info(void);
int hal_get_regist_info(const char* pMsg); int hal_get_regist_info(const char* pMsg);
int hal_init(void); int hal_init(void);
int hal_is_device_registed(void); int hal_is_device_registed(void);
int hal_get_exec_message(const char *pCmd, char **pResult); int hal_get_exec_message(const char *pCmd, char **pResult);
int hal_get_exec_message_v2(const char *pCmd, UT_string **pResult);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -7,6 +7,7 @@ extern "C" {
void mqtt_proxy_setup(void); void mqtt_proxy_setup(void);
int mqtt_publish_shadow_msg(unsigned char* pData, int msgSize); int mqtt_publish_shadow_msg(unsigned char* pData, int msgSize);
int mqtt_publish_rrpc_msg(char* pTopic, unsigned char* pData, int msgSize);
#ifdef __cplusplus #ifdef __cplusplus
} }