Add enice_config protocol support

This commit is contained in:
HuangXin 2019-01-02 17:42:32 +08:00
parent c23e2f9092
commit ce55a490ee
4 changed files with 460 additions and 10 deletions

View File

@ -77,7 +77,6 @@ int hal_load_device_register_info(void)
return 0; return 0;
} }
int hal_get_regist_info(const char* pMsg) int hal_get_regist_info(const char* pMsg)
{ {
int err = 0; int err = 0;

View File

@ -210,12 +210,259 @@ static void* json2RegDev(const char* pJsonS)
return pInfo; 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[] = 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_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) void* Json2Struct(const char* pJsonStr, JSON_ENGINE_TYPE type, int cryptoType, int* pErr)

View File

@ -19,7 +19,6 @@ static UT_string* g_pTopicBroadcase;
static UT_string* g_pShadow; static UT_string* g_pShadow;
static UT_string* g_pUpdate; static UT_string* g_pUpdate;
static void __mqtt_connected_cb(struct mosquitto* pMosq, void* obj, int rc) static void __mqtt_connected_cb(struct mosquitto* pMosq, void* obj, int rc)
{ {
g_isMqttConnected = TRUE; g_isMqttConnected = TRUE;
@ -136,12 +135,21 @@ static int __mqtt_connect_init(void)
LOG_EX(LOG_Debug, "MQTT Connect OK\n"); LOG_EX(LOG_Debug, "MQTT Connect OK\n");
} }
utstring_printf(g_pTopicGet, "/%s/%s/get", NETEASE_PRODUCT_KEY, hal_get_device_name()); 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()); 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_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()); utstring_printf(g_pUpdate, "/%s/%s/update", NETEASE_PRODUCT_KEY, hal_get_device_name());

View File

@ -1,6 +1,8 @@
#ifndef PROTOCOL_H #ifndef PROTOCOL_H
#define PROTOCOL_H #define PROTOCOL_H
#include <uthash/utlist.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -60,12 +62,206 @@ typedef struct
SHADOW_STATE state; SHADOW_STATE state;
} SHADOW_UPDATE, *PSHADOW_UPDATE; } 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 typedef enum
{ {
JE_PROMAIN = 0, JE_PROMAIN = 0,
JE_BYPASS, JE_BYPASS,
JE_SHADOWUP, JE_SHADOWUP,
JE_REGDEVICE, 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, JE_MAX,
} JSON_ENGINE_TYPE; } JSON_ENGINE_TYPE;