Upgrade enice_config parmas to base64 json string

This commit is contained in:
HuangXin 2019-01-17 15:46:06 +08:00
parent daf4e9014c
commit d0678541c9
5 changed files with 193 additions and 27 deletions

View File

@ -268,9 +268,8 @@ static void* __mqtt_msg_process_cb(void* p)
DL_FOREACH_SAFE(g_pMqttMsgList, pApi, pTemp) DL_FOREACH_SAFE(g_pMqttMsgList, pApi, pTemp)
{ {
protocol_runtime(pApi); protocol_runtime(pApi);
__cleanup_mqtt_msg_data(pApi); __cleanup_mqtt_msg_data(pApi);
DL_DELETE(g_pMqttMsgList, pApi);
if(iMaxMsg-- <= 0) break; if(iMaxMsg-- <= 0) break;
} }
pthread_mutex_unlock(&g_mqttMsgLock); pthread_mutex_unlock(&g_mqttMsgLock);

View File

@ -12,54 +12,161 @@
#include "log.h" #include "log.h"
#include "hal_mtk.h" #include "hal_mtk.h"
#include "protocol.h" #include "protocol.h"
#include "crypto.h"
#define ENICE_TOOL_PATH "/root"
const char* get_protocol_name(int cmdId) const char* get_protocol_name(int cmdId)
{ {
switch(cmdId) switch(cmdId)
{ {
case GET_DEV_ID: return "GET_DEV_ID";
case CONFIG_AP: return "CONFIG_AP";
case MSG_BYPASS: return "MSG_BYPASS"; case MSG_BYPASS: return "MSG_BYPASS";
} }
return "UNKNOWN_PROTOCOL"; return "UNKNOWN_PROTOCOL";
} }
static void run_enice_cmd() static void run_enice_cmd(PROUTER_CMD pCmd, char* pTopic)
{ {
int ret = 0;
UT_string *pRunCmd, *pRunRet;
utstring_new(pRunCmd);
if(pCmd->data && strlen(pCmd->data) > 0)
{
#if 0
int outSize = 0;
unsigned char* pDecode = NULL;
pDecode = base64_decode(pCmd->data, &pDecode, &outSize);
if(pDecode == NULL)
{
LOG_EX(LOG_Error, "Base Decode [%s] error\n", pCmd->data);
utstring_free(pRunCmd);
return;
}
if(outSize == 0)
{
LOG_EX(LOG_Error, "Base Decode [%s] size error: %d\n", pCmd->data, outSize);
utstring_free(pRunCmd);
free(pDecode);
return;
}
#endif
utstring_printf(pRunCmd, "%s/enice_config -%s %s", ENICE_TOOL_PATH, pCmd->cmd, pCmd->data);
// free(pDecode);
}
else
{
utstring_printf(pRunCmd, "%s/enice_config -%s", ENICE_TOOL_PATH, pCmd->cmd);
}
ret = hal_get_exec_message_v2(utstring_body(pRunCmd), &pRunRet);
if(ret != 0)
{
LOG_EX(LOG_Error, "Run [%s] error: %d\n", utstring_body(pRunCmd), ret);
utstring_free(pRunCmd);
if(pRunRet)
{
utstring_free(pRunRet);
}
return;
}
if(utstring_len(pRunRet) > 0 && json_is_valid(utstring_body(pRunRet)))
{
struct CLOUND_API api;
char* pMqttRsp = NULL;
char* pRetMsg = (char*)base64_encode(utstring_body(pRunRet), utstring_len(pRunRet), &pRetMsg);
if(pRetMsg == NULL)
{
LOG_EX(LOG_Error, "Base Encode [%s] error\n", utstring_body(pRunRet));
return;
}
memset(&api, 0, sizeof(struct CLOUND_API));
api.cmdId = MSG_BYPASS;
api.cryptoType = CRYPTO_NONE;
api.timeStamp = time(NULL);
api.msgContent = pRetMsg;
pMqttRsp = (char*)Struct2Json(&api, JE_PROMAIN, CRYPTO_NONE, &ret);
free(pRetMsg);
if(ret != ERR_OK || pMqttRsp == NULL)
{
LOG_EX(LOG_Error, "Create Protocol Message Error %p: %d\n", pMqttRsp, ret);
if(pMqttRsp)
{
free(pMqttRsp);
}
return;
}
//mqtt_publish_rrpc_msg(pTopic, (unsigned char*)pMqttRsp, strlen(pMqttRsp));
free(pMqttRsp);
utstring_free(pRunRet);
}
else if(pRunCmd)
{
LOG_EX(LOG_Error, "Json format error: [%s]\n", utstring_body(pRunRet));
utstring_free(pRunRet);
}
else
{
LOG_EX(LOG_Error, "Json format error: Empty\n");
}
utstring_free(pRunCmd);
} }
int protocol_runtime(PCLOUND_API pApi) int protocol_runtime(PCLOUND_API pApi)
{ {
int err = ERR_OK; int err = ERR_OK;
PBYPASS_INFO pInfo = NULL; PROUTER_CMD pCmd = NULL;
switch(pApi->cmdId) switch(pApi->cmdId)
{ {
case MSG_BYPASS: case MSG_BYPASS:
pInfo = (PBYPASS_INFO)Json2Struct(pApi->msgContent, JE_BYPASS, CRYPTO_NONE, &err); pCmd = (PROUTER_CMD)Json2Struct(pApi->msgContent, JE_ROUTERCMD, CRYPTO_NONE, &err);
if(err != ERR_OK || pInfo == NULL) if(err != ERR_OK || pCmd == NULL)
{ {
LOG_EX(LOG_Error, "Process %s Message Error %p: %d\n", get_protocol_name(pApi->cmdId), pInfo, err); LOG_EX(LOG_Error, "Process %s Message Error %p: %d\n", get_protocol_name(pApi->cmdId), pCmd, err);
if(pInfo) if(pCmd)
{ {
free(pInfo); free(pCmd);
} }
} }
else if(pInfo->mcuCmd && strlen(pInfo->mcuCmd) > 0) else if(pCmd->cmd && strlen(pCmd->cmd) > 0)
{ {
run_enice_cmd(pInfo->mcuCmd); run_enice_cmd(pCmd, pApi->pMsgTopic);
} }
if(pInfo->mcuCmd) if(pCmd->cmd)
{ {
free(pInfo->mcuCmd); free(pCmd->cmd);
} }
free(pInfo); if(pCmd->data)
{
free(pCmd->data);
}
free(pCmd);
break; break;
@ -124,7 +231,7 @@ static void* __enice_msg_process_cb(void* p)
SHADOW_UPDATE shInfo; SHADOW_UPDATE shInfo;
char* pShMsg = NULL; char* pShMsg = NULL;
LOG_EX(LOG_Debug, "Receive JSON:\n%s\n", pRecBuf); //LOG_EX(LOG_Debug, "Receive JSON:\n%s\n", pRecBuf);
memset(&shInfo, 0, sizeof(SHADOW_UPDATE)); memset(&shInfo, 0, sizeof(SHADOW_UPDATE));
@ -136,9 +243,15 @@ static void* __enice_msg_process_cb(void* p)
if(pShMsg && strlen(pShMsg) > 0 && ret == ERR_OK) if(pShMsg && strlen(pShMsg) > 0 && ret == ERR_OK)
{ {
LOG_EX(LOG_Debug, "Send Shadow Message:\n%s\n", pShMsg); //LOG_EX(LOG_Debug, "Send Shadow Message:\n%s\n", pShMsg);
mqtt_publish_shadow_msg(pShMsg, strlen(pShMsg)); mqtt_publish_shadow_msg(pShMsg, strlen(pShMsg));
} }
if(pShMsg)
{
free(pShMsg);
}
//ret = ProtocolProcess(pRecBuf); //ret = ProtocolProcess(pRecBuf);
} }

View File

@ -10,12 +10,6 @@ extern "C" {
#define PRO_VERSION (1) #define PRO_VERSION (1)
#define SHADOW_METHOD ("update") #define SHADOW_METHOD ("update")
typedef enum
{
MQTT_RRPC = 0,
MQTT_SHADOW,
} MSG_TYPE;
typedef enum typedef enum
{ {
GET_DEV_ID = 1, GET_DEV_ID = 1,
@ -58,6 +52,17 @@ typedef struct
char* mcuCmd; char* mcuCmd;
} BYPASS_INFO, *PBYPASS_INFO; } BYPASS_INFO, *PBYPASS_INFO;
typedef struct
{
char* cmd;
char* data;
} ROUTER_CMD, *PROUTER_CMD;
typedef struct
{
char* data;
} ROUTER_RSP, *PROUTER_RSP;
typedef struct CLOUND_API typedef struct CLOUND_API
{ {
int cmdId; int cmdId;
@ -65,7 +70,6 @@ typedef struct CLOUND_API
int timeStamp; int timeStamp;
char* msgContent; char* msgContent;
char* pMsgTopic; char* pMsgTopic;
MSG_TYPE msgType;
struct CLOUND_API *next, *prev; struct CLOUND_API *next, *prev;
} *PCLOUND_API; } *PCLOUND_API;
@ -82,6 +86,7 @@ typedef struct
SHADOW_STATE state; SHADOW_STATE state;
} SHADOW_UPDATE, *PSHADOW_UPDATE; } SHADOW_UPDATE, *PSHADOW_UPDATE;
#if 0
/******************************************** /********************************************
Smart router protocol Smart router protocol
********************************************/ ********************************************/
@ -256,6 +261,7 @@ typedef struct
{ {
int time; int time;
} PRO_AUTO_UPGRADE, *PPRO_AUTO_UPGRADE; } PRO_AUTO_UPGRADE, *PPRO_AUTO_UPGRADE;
#endif
typedef enum typedef enum
{ {
@ -263,6 +269,9 @@ typedef enum
JE_BYPASS, JE_BYPASS,
JE_SHADOWUP, JE_SHADOWUP,
JE_REGDEVICE, JE_REGDEVICE,
JE_ROUTERCMD,
JE_ROUTERRSP,
#if 0
JE_PRO_GETVERSION, JE_PRO_GETVERSION,
JE_PRO_ADMINLOGON, JE_PRO_ADMINLOGON,
JE_PRO_GETSPEED, JE_PRO_GETSPEED,
@ -282,6 +291,7 @@ typedef enum
JE_PRO_REBOOT, JE_PRO_REBOOT,
JE_PRO_UPGRADE, JE_PRO_UPGRADE,
JE_PRO_AUTOUPGRADE, JE_PRO_AUTOUPGRADE,
#endif
JE_MAX, JE_MAX,
} JSON_ENGINE_TYPE; } JSON_ENGINE_TYPE;
@ -299,6 +309,7 @@ typedef struct
const char* Struct2Json(void* pStruct, JSON_ENGINE_TYPE type, int cryptoType, int* pErr); const char* Struct2Json(void* pStruct, JSON_ENGINE_TYPE type, int cryptoType, int* pErr);
void* Json2Struct(const char* pJsonStr, JSON_ENGINE_TYPE type, int cryptoType, int* pErr); void* Json2Struct(const char* pJsonStr, JSON_ENGINE_TYPE type, int cryptoType, int* pErr);
int json_is_valid(char* pJsonS);
int protocol_runtime(PCLOUND_API pApi); int protocol_runtime(PCLOUND_API pApi);
int enice_config_init(void); int enice_config_init(void);

View File

@ -60,6 +60,43 @@ int evp_sha1(void)
#endif #endif
static void test_run_cmd(void)
{
int ret = 0;
UT_string *pRunCmd, *pRunRet;
utstring_new(pRunCmd);
utstring_printf(pRunCmd, "/root/enice_config -V");
ret = hal_get_exec_message_v2(utstring_body(pRunCmd), &pRunRet);
if(ret != 0)
{
LOG_EX(LOG_Error, "Run [%s] error: %d\n", utstring_body(pRunCmd), ret);
utstring_free(pRunCmd);
if(pRunRet)
{
utstring_free(pRunRet);
}
return;
}
if(utstring_len(pRunRet) > 0 && json_is_valid(utstring_body(pRunRet)))
{
LOG_EX(LOG_Error, "Get Ret: [%s]\n", utstring_body(pRunRet));
utstring_free(pRunRet);
}
else
{
LOG_EX(LOG_Error, "Json format error: [%s]\n", utstring_body(pRunRet));
}
utstring_free(pRunCmd);
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
int rc; int rc;
@ -92,6 +129,12 @@ int main(int argc, char* argv[])
sleep(5); sleep(5);
} }
#if 0
LOG_EX(LOG_Debug, "+++++++++++++++++++\n");
test_run_cmd();
LOG_EX(LOG_Debug, "-------------------\n");
#endif
mqtt_proxy_setup(); mqtt_proxy_setup();
while(TRUE) while(TRUE)