From d0678541c9e7c81fd038172505ef4498682f911a Mon Sep 17 00:00:00 2001 From: HuangXin Date: Thu, 17 Jan 2019 15:46:06 +0800 Subject: [PATCH] Upgrade enice_config parmas to base64 json string --- src/Framework/mqtt/mqtt.c | 3 +- src/Framework/protocol/protocol.c | 145 ++++++++++++++++++++++++++---- src/Makefile.cross | 2 +- src/include/protocol.h | 27 ++++-- src/main.c | 43 +++++++++ 5 files changed, 193 insertions(+), 27 deletions(-) diff --git a/src/Framework/mqtt/mqtt.c b/src/Framework/mqtt/mqtt.c index b7f26ce..14cee02 100644 --- a/src/Framework/mqtt/mqtt.c +++ b/src/Framework/mqtt/mqtt.c @@ -268,9 +268,8 @@ static void* __mqtt_msg_process_cb(void* p) DL_FOREACH_SAFE(g_pMqttMsgList, pApi, pTemp) { protocol_runtime(pApi); - __cleanup_mqtt_msg_data(pApi); - + DL_DELETE(g_pMqttMsgList, pApi); if(iMaxMsg-- <= 0) break; } pthread_mutex_unlock(&g_mqttMsgLock); diff --git a/src/Framework/protocol/protocol.c b/src/Framework/protocol/protocol.c index 060e72e..85e0105 100644 --- a/src/Framework/protocol/protocol.c +++ b/src/Framework/protocol/protocol.c @@ -12,54 +12,161 @@ #include "log.h" #include "hal_mtk.h" #include "protocol.h" +#include "crypto.h" + +#define ENICE_TOOL_PATH "/root" const char* get_protocol_name(int cmdId) { switch(cmdId) { - case GET_DEV_ID: return "GET_DEV_ID"; - case CONFIG_AP: return "CONFIG_AP"; case MSG_BYPASS: return "MSG_BYPASS"; } 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 err = ERR_OK; - PBYPASS_INFO pInfo = NULL; + PROUTER_CMD pCmd = NULL; switch(pApi->cmdId) { 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; @@ -124,7 +231,7 @@ static void* __enice_msg_process_cb(void* p) SHADOW_UPDATE shInfo; 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)); @@ -136,9 +243,15 @@ static void* __enice_msg_process_cb(void* p) 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)); } + + if(pShMsg) + { + free(pShMsg); + } + //ret = ProtocolProcess(pRecBuf); } diff --git a/src/Makefile.cross b/src/Makefile.cross index 8c04a8d..820ace1 100644 --- a/src/Makefile.cross +++ b/src/Makefile.cross @@ -23,7 +23,7 @@ endif .PHONY : mqtt_proxy enice -all: mqtt_proxy enice +all: mqtt_proxy enice mqtt_proxy: diff --git a/src/include/protocol.h b/src/include/protocol.h index 54ebfca..bd45d53 100644 --- a/src/include/protocol.h +++ b/src/include/protocol.h @@ -10,12 +10,6 @@ extern "C" { #define PRO_VERSION (1) #define SHADOW_METHOD ("update") -typedef enum -{ - MQTT_RRPC = 0, - MQTT_SHADOW, -} MSG_TYPE; - typedef enum { GET_DEV_ID = 1, @@ -55,9 +49,20 @@ typedef struct typedef struct { - char* mcuCmd; + char* mcuCmd; } 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 { int cmdId; @@ -65,7 +70,6 @@ typedef struct CLOUND_API int timeStamp; char* msgContent; char* pMsgTopic; - MSG_TYPE msgType; struct CLOUND_API *next, *prev; } *PCLOUND_API; @@ -82,6 +86,7 @@ typedef struct SHADOW_STATE state; } SHADOW_UPDATE, *PSHADOW_UPDATE; +#if 0 /******************************************** Smart router protocol ********************************************/ @@ -256,6 +261,7 @@ typedef struct { int time; } PRO_AUTO_UPGRADE, *PPRO_AUTO_UPGRADE; +#endif typedef enum { @@ -263,6 +269,9 @@ typedef enum JE_BYPASS, JE_SHADOWUP, JE_REGDEVICE, + JE_ROUTERCMD, + JE_ROUTERRSP, +#if 0 JE_PRO_GETVERSION, JE_PRO_ADMINLOGON, JE_PRO_GETSPEED, @@ -282,6 +291,7 @@ typedef enum JE_PRO_REBOOT, JE_PRO_UPGRADE, JE_PRO_AUTOUPGRADE, +#endif JE_MAX, } JSON_ENGINE_TYPE; @@ -299,6 +309,7 @@ typedef struct 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); +int json_is_valid(char* pJsonS); int protocol_runtime(PCLOUND_API pApi); int enice_config_init(void); diff --git a/src/main.c b/src/main.c index a8b91c5..76359aa 100644 --- a/src/main.c +++ b/src/main.c @@ -60,6 +60,43 @@ int evp_sha1(void) #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 rc; @@ -92,6 +129,12 @@ int main(int argc, char* argv[]) sleep(5); } +#if 0 + LOG_EX(LOG_Debug, "+++++++++++++++++++\n"); + test_run_cmd(); + LOG_EX(LOG_Debug, "-------------------\n"); +#endif + mqtt_proxy_setup(); while(TRUE)