From ce55a490ee56fa5ced34f05640298889e9f032c9 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Wed, 2 Jan 2019 17:42:32 +0800 Subject: [PATCH] Add enice_config protocol support --- src/Framework/hal/hal_mtk.c | 1 - src/Framework/json_utils/json_struct.c | 255 ++++++++++++++++++++++++- src/Framework/mqtt/mqtt.c | 18 +- src/include/protocol.h | 196 +++++++++++++++++++ 4 files changed, 460 insertions(+), 10 deletions(-) diff --git a/src/Framework/hal/hal_mtk.c b/src/Framework/hal/hal_mtk.c index be47074..187c84c 100644 --- a/src/Framework/hal/hal_mtk.c +++ b/src/Framework/hal/hal_mtk.c @@ -77,7 +77,6 @@ int hal_load_device_register_info(void) return 0; } - int hal_get_regist_info(const char* pMsg) { int err = 0; diff --git a/src/Framework/json_utils/json_struct.c b/src/Framework/json_utils/json_struct.c index 8f832c3..d01122d 100644 --- a/src/Framework/json_utils/json_struct.c +++ b/src/Framework/json_utils/json_struct.c @@ -210,12 +210,259 @@ static void* json2RegDev(const char* pJsonS) return pInfo; } +static void* __json2ProRsp(const char* pJsonS) +{ + PPRO_RESPONSE pRsp = NULL; + cJSON *pRoot = cJSON_Parse(pJsonS); + + if(pRoot == NULL) + { + LOG_EX(LOG_Error, "Json Format Error: [%s]\n", pJsonS); + return NULL; + } + + pRsp = (PPRO_RESPONSE)malloc(sizeof(PRO_RESPONSE)); + + if(pRsp == NULL) + { + LOG_EX(LOG_Error, "Malloc PPRO_RESPONSE Error\n"); + cJSON_Delete(pRoot); + return NULL; + } + + memset(pRsp, 0, sizeof(PRO_RESPONSE)); + + pRsp->code = cJSON_GetObjectItem(pRoot, "code")->valueint; + pRsp->ostime = cJSON_GetObjectItem(pRoot, "ostime")->valueint; + + cJSON_Delete(pRoot); + return pRsp; +} + +static void* __json2ProGetVer(const char* pJsonS) +{ + PPRO_RESPONSE pVer = NULL; + cJSON *pItem = NULL; + cJSON *pRoot = cJSON_Parse(pJsonS); + + if(pRoot == NULL) + { + LOG_EX(LOG_Error, "Json Format Error: [%s]\n", pJsonS); + return NULL; + } + + pVer = (PPRO_RESPONSE)malloc(sizeof(PRO_RESPONSE)); + + if(pVer == NULL) + { + LOG_EX(LOG_Error, "Malloc PPRO_RESPONSE Error\n"); + cJSON_Delete(pRoot); + return NULL; + } + + memset(pVer, 0, sizeof(PRO_RESPONSE)); + + pVer->code = cJSON_GetObjectItem(pRoot, "code")->valueint; + pVer->ostime = cJSON_GetObjectItem(pRoot, "ostime")->valueint; + + pItem = cJSON_GetObjectItem(pRoot, "data"); + + if(pItem && pVer->code == 0) + { + pVer->data.ver.apiVersion = __jsonSafeDecodeStr(pItem, "apiVersion"); + pVer->data.ver.firmwareVersion = __jsonSafeDecodeStr(pItem, "firmwareVersion"); + pVer->data.ver.sn = __jsonSafeDecodeStr(pItem, "sn"); + } + + cJSON_Delete(pRoot); + return pVer; +} + +static const char* __logon2Json(void* pData) +{ + PPRO_ADMIN_LOGON pInfo = (PPRO_ADMIN_LOGON)pData; + const char* pJsonS; + + s2j_create_json_obj(jObject); + + if(pInfo->account) + { + s2j_json_set_basic_element(jObject, pInfo, string, account); + } + + if(pInfo->password) + { + s2j_json_set_basic_element(jObject, pInfo, string, password); + } + + pJsonS = cJSON_Print(jObject); + + cJSON_Delete(jObject); + + return pJsonS; +} + +static void* __json2Prologon(const char* pJsonS) +{ + PPRO_RESPONSE pLogon = NULL; + cJSON *pItem = NULL; + cJSON *pRoot = cJSON_Parse(pJsonS); + + if(pRoot == NULL) + { + LOG_EX(LOG_Error, "Json Format Error: [%s]\n", pJsonS); + return NULL; + } + + pLogon = (PPRO_RESPONSE)malloc(sizeof(PRO_RESPONSE)); + + if(pLogon == NULL) + { + LOG_EX(LOG_Error, "Malloc PPRO_RESPONSE Error\n"); + cJSON_Delete(pRoot); + return NULL; + } + + memset(pLogon, 0, sizeof(PRO_RESPONSE)); + + pLogon->code = cJSON_GetObjectItem(pRoot, "code")->valueint; + pLogon->ostime = cJSON_GetObjectItem(pRoot, "ostime")->valueint; + + pItem = cJSON_GetObjectItem(pRoot, "data"); + + if(pItem && pLogon->code == 0) + { + pLogon->data.logon.token = __jsonSafeDecodeStr(pItem, "token"); + } + + cJSON_Delete(pRoot); + return pLogon; +} + +static void* __json2ProGetSpeed(const char* pJsonS) +{ + PPRO_RESPONSE pRsp = NULL; + cJSON *pItem = NULL; + cJSON *pRoot = cJSON_Parse(pJsonS); + + if(pRoot == NULL) + { + LOG_EX(LOG_Error, "Json Format Error: [%s]\n", pJsonS); + return NULL; + } + + pRsp = (PPRO_RESPONSE)malloc(sizeof(PRO_RESPONSE)); + + if(pRsp == NULL) + { + LOG_EX(LOG_Error, "Malloc PPRO_RESPONSE Error\n"); + cJSON_Delete(pRoot); + return NULL; + } + + memset(pRsp, 0, sizeof(PRO_RESPONSE)); + + pRsp->code = cJSON_GetObjectItem(pRoot, "code")->valueint; + pRsp->ostime = cJSON_GetObjectItem(pRoot, "ostime")->valueint; + + pItem = cJSON_GetObjectItem(pRoot, "data"); + + if(pItem && pRsp->code == 0) + { + pRsp->data.speed.downloadSpeed = cJSON_GetObjectItem(pItem, "downloadSpeed")->valueint; + pRsp->data.speed.uploadSpeed = cJSON_GetObjectItem(pItem, "uploadSpeed")->valueint; + } + + cJSON_Delete(pRoot); + return pRsp; +} + +static void* __json2ProGetDevList(const char* pJsonS) +{ + PPRO_RESPONSE pRsp = NULL; + cJSON *pList = NULL; + cJSON *pRoot = cJSON_Parse(pJsonS); + + if(pRoot == NULL) + { + LOG_EX(LOG_Error, "Json Format Error: [%s]\n", pJsonS); + return NULL; + } + + pRsp = (PPRO_RESPONSE)malloc(sizeof(PRO_RESPONSE)); + + if(pRsp == NULL) + { + LOG_EX(LOG_Error, "Malloc PPRO_RESPONSE Error\n"); + cJSON_Delete(pRoot); + return NULL; + } + + memset(pRsp, 0, sizeof(PRO_RESPONSE)); + + pRsp->code = cJSON_GetObjectItem(pRoot, "code")->valueint; + pRsp->ostime = cJSON_GetObjectItem(pRoot, "ostime")->valueint; + + pList = cJSON_GetObjectItem(pRoot, "data"); + + if(pList && pRsp->code == 0) + { + int i = 0; + int arraySize = cJSON_GetArraySize(pList); + pRsp->data.pDevList = NULL; + + for(i = 0; (i < arraySize) && pList, i++) + { + PDEVLIST_DATA pDevItem = (PDEVLIST_DATA)malloc(sizeof(DEVLIST_DATA)); + + if(pDevItem == NULL) + { + LOG_EX(LOG_Error, "Malloc Memory Error: %d\n", sizeof(DEVLIST_DATA)); + continue; + } + + pDevItem->type = __jsonSafeDecodeStr(pList, "type"); + pDevItem->macAddress = __jsonSafeDecodeStr(pList, "macAddress"); + pDevItem->name = __jsonSafeDecodeStr(name, "macAddress"); + + pDevItem->connectEnable = cJSON_GetObjectItem(pList, "connectEnable")->valueint; + pDevItem->downloadSpeed = cJSON_GetObjectItem(pList, "downloadSpeed")->valueint; + pDevItem->banCount = cJSON_GetObjectItem(pList, "banCount")->valueint; + pDevItem->banEndTime = cJSON_GetObjectItem(pList, "banEndTime")->valueint; + pDevItem->wifiStrengthLevel = cJSON_GetObjectItem(pList, "wifiStrengthLevel")->valueint; + pDevItem->banStartTime = cJSON_GetObjectItem(pList, "banStartTime")->valueint; + pDevItem->banDate = cJSON_GetObjectItem(pList, "banDate")->valueint; + + pDevItem->speedLimit = cJSON_GetObjectItem(pList, "speedLimit")->valueint; + pDevItem->uploadSpeed = cJSON_GetObjectItem(pList, "uploadSpeed")->valueint; + pDevItem->connectTime = cJSON_GetObjectItem(pList, "connectTime")->valueint; + pDevItem->disconnectTime = cJSON_GetObjectItem(pList, "disconnectTime")->valueint; + pDevItem->network = cJSON_GetObjectItem(pList, "network")->valueint; + pDevItem->isOnline = cJSON_GetObjectItem(pList, "isOnline")->valueint; + pDevItem->isBan = cJSON_GetObjectItem(pList, "isBan")->valueint; + + LL_APPEND(pRsp->data.pDevList, pDevItem); + pList = pList->next; + } + + } + + cJSON_Delete(pRoot); + return pRsp; +} + + static JSON_ENGINE g_jSonEngine[] = { - {JE_PROMAIN, __pro2Json, __json2pro, NULL}, - {JE_BYPASS, __bypass2Json, __json2bypass, NULL}, - {JE_SHADOWUP, __shadow2Json, NULL, NULL}, - {JE_REGDEVICE, NULL, json2RegDev, NULL}, + {JE_PROMAIN, __pro2Json, __json2pro, NULL}, + {JE_BYPASS, __bypass2Json, __json2bypass, NULL}, + {JE_SHADOWUP, __shadow2Json, NULL, NULL}, + {JE_REGDEVICE, NULL, json2RegDev, NULL}, + {JE_PRO_GETVERSION, NULL, __json2ProGetVer, NULL}, + {JE_PRO_ADMINLOGON, __logon2Json, __json2Prologon, NULL}, + {JE_PRO_GETSPEED, NULL, __json2ProGetSpeed, NULL}, + {JE_PRO_GETDEVLIST, NULL, __json2ProGetDevList, NULL}, +// {, , , NULL}, }; void* Json2Struct(const char* pJsonStr, JSON_ENGINE_TYPE type, int cryptoType, int* pErr) diff --git a/src/Framework/mqtt/mqtt.c b/src/Framework/mqtt/mqtt.c index 605f9e9..e272f4f 100644 --- a/src/Framework/mqtt/mqtt.c +++ b/src/Framework/mqtt/mqtt.c @@ -19,7 +19,6 @@ static UT_string* g_pTopicBroadcase; static UT_string* g_pShadow; static UT_string* g_pUpdate; - static void __mqtt_connected_cb(struct mosquitto* pMosq, void* obj, int rc) { g_isMqttConnected = TRUE; @@ -134,15 +133,24 @@ static int __mqtt_connect_init(void) else { LOG_EX(LOG_Debug, "MQTT Connect OK\n"); - } - + } utstring_printf(g_pTopicGet, "/%s/%s/get", NETEASE_PRODUCT_KEY, hal_get_device_name()); - mosquitto_subscribe(pMosq, NULL, utstring_body(g_pTopicGet), 1); + rc = mosquitto_subscribe(pMosq, NULL, utstring_body(g_pTopicGet), 1); + + if(rc != MOSQ_ERR_SUCCESS) + { + LOG_EX(LOG_Error, "MQTT Subscribe %s error: %d\n", utstring_body(g_pTopicGet), rc); + } utstring_printf(g_pTopicBroadcase, "/broadcast/%s/%d/get", NETEASE_PRODUCT_KEY, hal_device_name_to_id()); - mosquitto_subscribe(pMosq, NULL, utstring_body(g_pTopicBroadcase), 1); + rc = mosquitto_subscribe(pMosq, NULL, utstring_body(g_pTopicBroadcase), 1); + if(rc != MOSQ_ERR_SUCCESS) + { + LOG_EX(LOG_Error, "MQTT Subscribe %s error: %d\n", utstring_body(g_pTopicBroadcase), rc); + } + utstring_printf(g_pShadow, "/shadow/update/%s/%s", NETEASE_PRODUCT_KEY, hal_get_device_name()); utstring_printf(g_pUpdate, "/%s/%s/update", NETEASE_PRODUCT_KEY, hal_get_device_name()); diff --git a/src/include/protocol.h b/src/include/protocol.h index 7b18068..94b1634 100644 --- a/src/include/protocol.h +++ b/src/include/protocol.h @@ -1,6 +1,8 @@ #ifndef PROTOCOL_H #define PROTOCOL_H +#include + #ifdef __cplusplus extern "C" { #endif @@ -60,12 +62,206 @@ typedef struct SHADOW_STATE state; } SHADOW_UPDATE, *PSHADOW_UPDATE; +/******************************************** + Smart router protocol +********************************************/ +typedef struct +{ + char* sn; + char* firmwareVersion; + char* apiVersion; +} VERSION_DATA, *PVERSION_DATA; + +typedef struct +{ + char* token; +} ADMIN_LOGON_DATA, *PADMIN_LOGON_DATA; + +typedef struct +{ + char* downloadSpeed; + char* uploadSpeed; +} GET_SPEED_DATA, *PGET_SPEED_DATA; + +typedef struct DEVLIST_DATA +{ + char* type; + char* macAddress; + char* name; + int connectEnable; + int downloadSpeed; + int banCount; + int banEndTime; + int wifiStrengthLevel; + int banStartTime; + int banDate; + int speedLimit; + int uploadSpeed; + int connectTime; + int disconnectTime; + int traffic; + int network; + int isOnline; + int isBan; + + struct DEVLIST_DATA *next, *prev; +}*PDEVLIST_DATA; + +typedef struct +{ + char password[64]; + char wifiName[128]; + int isVisible; + int secretMethod; + int wifiStrengthLevel; + int enabled; +} WIFI_CFG, *PWIFI_CFG; + +typedef struct WIFI_STATUS +{ + int autoFrekvensBandet; + int MUMINOEnable; + + WIFI_CFG wifi_5G; + WIFI_CFG wifi_24G; + WIFI_CFG wifi_guest; + WIFI_CFG wifi_sameFrequency; +} WIFI_STATUS_DATA, *PWIFI_STATUS_DATA; + +typedef struct +{ + int autoLink; + int linkType; + char gateway[20]; + char mask[20]; + char dns1[20]; + char dns2[20]; + char ip[20]; + char account[128]; + char password[128]; +} NET_SETTING_DATA, *PNET_SETTING_DATA; + +typedef struct +{ + int ostime; + union { + VERSION_DATA ver; + ADMIN_LOGON_DATA logon; + GET_SPEED_DATA speed; + PDEVLIST_DATA pDevList; + WIFI_STATUS_DATA wifiStatus; + NET_SETTING_DATA netSettings; + } data; + int code; +} PRO_RESPONSE, *PPRO_RESPONSE; + +typedef struct +{ + char* account; + char* password; +} PRO_ADMIN_LOGON, *PPRO_ADMIN_LOGON; + +typedef struct +{ + char* macAddress; + char* name; +} PRO_SET_DEVNAME, *PPRO_SET_DEVNAME; + +typedef struct +{ + char* macAddress; + char* uploadSpeed; + char* downloadSpeed; +} PRO_SPEED_LIMITS, *PPRO_SPEED_LIMITS; + +typedef struct +{ + char* macAddress; + int banDate; + int banStartTime; + int banEndTime; + int banCount; +} PRO_DISCONNECT_ONTIME, *PPRO_DISCONNECT_ONTIME; + +typedef struct +{ + char* macAddress; + int connectEnable; +} PRO_CONNECT_SWITCH, *PPRO_CONNECT_SWITCH; + +typedef struct +{ + int autoFrekvensBandet; +} PRO_WIFI_SAMEFREQ, *PPRO_WIFI_SAMEFREQ; + +typedef struct +{ + int wifiOption; +} PRO_WIFI_24AND5G_SWITCH, *PPRO_WIFI_24AND5G_SWITCH; + +typedef struct +{ + int MUMIMOEnable; +} PRO_MIMO_SWITCH, *PPRO_MIMO_SWITCH; + +typedef struct +{ + int wifiOption; + char* wifiName; + char* wifiPassword; + int isVisible; + int secretMethod; +} PRO_WIFI_CONNECT, *PPRO_WIFI_CONNECT; + +typedef struct +{ + int autoLink; + char* linkType; + char gateway[20]; + char mask[20]; + char dns1[20]; + char dns2[20]; + char ip[20]; + char account[128]; + char password[128]; +} PRO_SET_CONNECT, *PPRO_SET_CONNECT; + +typedef struct +{ + char* oldPassword; + char* newPassword; +} PRO_SET_ADMINPASSWD, *PPRO_SET_ADMINPASSWD; + +typedef struct +{ + int time; +} PRO_AUTO_UPGRADE, *PPRO_AUTO_UPGRADE; + typedef enum { JE_PROMAIN = 0, JE_BYPASS, JE_SHADOWUP, JE_REGDEVICE, + JE_PRO_GETVERSION, + JE_PRO_ADMINLOGON, + JE_PRO_GETSPEED, + JE_PRO_GETDEVLIST, + JE_PRO_SETDEVNAME, + JE_PRO_SPEEDLIMITS, + JE_PRO_DISONTIME, + JE_PRO_CONNSWITCH, + JE_PRO_GETWIFIINFO, + JE_PRO_SETSMAEFREQ, + JE_PRO_24AND5GKEY, + JE_PRO_MIMOKEY, + JE_PRO_WIFICONNECT, + JE_PRO_GETNETSETS, + JE_PRO_SETCONNECT, + JE_PRO_SETPASSWD, + JE_PRO_REBOOT, + JE_PRO_UPGRADE, + JE_PRO_AUTOUPGRADE, JE_MAX, } JSON_ENGINE_TYPE;