|
|
@@ -15,6 +15,9 @@
|
|
|
#include "Timer_Module.h"
|
|
|
#include "CoAP_Util.h"
|
|
|
|
|
|
+// 泵号
|
|
|
+extern uint8_t mcu_id[8];
|
|
|
+
|
|
|
// 注册时用的密钥
|
|
|
char regist_key[17]="tuorenzhinenghua";
|
|
|
|
|
|
@@ -29,13 +32,14 @@ uint16_t REGIST_PORT = 8888; //
|
|
|
#endif
|
|
|
|
|
|
#if TEST_ENV
|
|
|
+
|
|
|
// 开发环境
|
|
|
-//char * REGIST_SERVER = "b21759p174.iok.la" ; //注册服务器地址
|
|
|
-//uint16_t REGIST_PORT = 43097; //端口号
|
|
|
+//char * REGIST_SERVER = "192.168.103.172" ; //注册服务器地址
|
|
|
+//uint16_t REGIST_PORT = 8888; //端口号
|
|
|
|
|
|
+char * REGIST_SERVER = "18017zi0ec26.vicp.fun" ; //注册服务器地址
|
|
|
+uint16_t REGIST_PORT = 58562; //端口号
|
|
|
|
|
|
-char * REGIST_SERVER = "frp-fun.top" ; //注册服务器地址
|
|
|
-uint16_t REGIST_PORT = 27827; //端口号
|
|
|
|
|
|
#endif
|
|
|
|
|
|
@@ -46,10 +50,6 @@ static void ReadRegistResponseFromFlash(void);
|
|
|
// 将注册响应写入到flash中
|
|
|
static void WriteRegistResponseToFlash(void);
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
// 注册结果
|
|
|
struct TUORENIOT_RegistResponseStruct regist_response = {
|
|
|
.version = 1
|
|
|
@@ -66,15 +66,33 @@ static void InitClientId(void)
|
|
|
strcat(ClientID, regist_response.deviceId);
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
-*
|
|
|
-*注册数据解密
|
|
|
-*/
|
|
|
+/*************************************** 注册信息打包函数 ***************************************/
|
|
|
+void TUORENIOT_PackRegistRequest(uint8_t * pack_data, uint16_t * pack_data_length, struct TUORENIOT_RegistRequestStruct * registParams)
|
|
|
+{
|
|
|
+
|
|
|
+ Log_Printf_Debug("mcu_id: %s\r\n", mcu_id);
|
|
|
+ byteToHexStr(mcu_id, registParams->deviceId, sizeof(mcu_id));
|
|
|
+
|
|
|
+ sprintf((char *)(pack_data + 2), "{\"version\":\"%d\",\"networkType\":\"%d\",\"networkProtocol\":\"%d\",\"productId\":\"%s\",\"deviceId\":\"%s\",\"userId\":\"%s\"}",
|
|
|
+ registParams->version, registParams->networkType, registParams->networkProtocol, registParams->productId, registParams->deviceId, registParams->userId); // 拼接AT指令
|
|
|
+ Log_Printf_Debug("%s\r\n", (char *)(pack_data + 2));
|
|
|
+
|
|
|
+ uint16_t datalen = utils_aes128_ECB_base64_enc_with_length(regist_key, (pack_data + 2));
|
|
|
+
|
|
|
+ pack_data[0] = datalen>>8;//头2个字节赋值数据长度
|
|
|
+ pack_data[1] = datalen;
|
|
|
+
|
|
|
+ * pack_data_length = datalen + 2;
|
|
|
+}
|
|
|
+
|
|
|
+/*************************************** 注册数据解密函数 ***************************************/
|
|
|
+
|
|
|
+static int wifi_count=0; //WiFi信息计数变量
|
|
|
uint8_t TUORENIOT_AnalysisRegistData(uint8_t * regist_data, uint16_t regist_data_length)
|
|
|
{
|
|
|
-// Log_Printf_Debug("regist_data: \r\n");
|
|
|
-// Log_SendHex(regist_data, regist_data_length);
|
|
|
-// Log_Printf_Debug("\r\n");
|
|
|
+ Log_Printf_Debug("regist_data: \r\n");
|
|
|
+ Log_SendHex(regist_data, regist_data_length);
|
|
|
+ Log_Printf_Debug("\r\n");
|
|
|
|
|
|
uint8_t result;
|
|
|
utils_aes128_ECB_base64_dec(regist_key,regist_data,regist_data_length);
|
|
|
@@ -90,19 +108,56 @@ uint8_t TUORENIOT_AnalysisRegistData(uint8_t * regist_data, uint16_t regist_data
|
|
|
{
|
|
|
regist_response.code = cJSON_GetObjectItem(json, "code")->valueint;
|
|
|
regist_response.version = cJSON_GetObjectItem(json, "version")->valueint;
|
|
|
- regist_response.userId = cJSON_GetObjectItem(json, "userId")->valueint;
|
|
|
- strcpy(regist_response.networkProtocol, cJSON_GetObjectItem(json, "networkProtocol")->valuestring);
|
|
|
- strcpy(regist_response.platform, cJSON_GetObjectItem(json, "platform")->valuestring);
|
|
|
- strcpy(regist_response.deviceSecret, cJSON_GetObjectItem(json, "deviceSecret")->valuestring);
|
|
|
+ regist_response.platform = cJSON_GetObjectItem(json, "platform")->valueint;
|
|
|
+ regist_response.networkType = cJSON_GetObjectItem(json, "networkType")->valueint;
|
|
|
+ regist_response.networkProtocol = cJSON_GetObjectItem(json, "networkProtocol")->valueint;
|
|
|
strcpy(regist_response.productId, cJSON_GetObjectItem(json, "productId")->valuestring);
|
|
|
strcpy(regist_response.deviceId, cJSON_GetObjectItem(json, "deviceId")->valuestring);
|
|
|
+ strcpy(regist_response.userId, cJSON_GetObjectItem(json, "userId")->valuestring);
|
|
|
+ strcpy(regist_response.deviceSecret, cJSON_GetObjectItem(json, "deviceSecret")->valuestring);
|
|
|
strcpy(regist_response.remoteAddress, cJSON_GetObjectItem(json, "remoteAddress")->valuestring);
|
|
|
regist_response.remotePort = cJSON_GetObjectItem(json, "remotePort")->valueint;
|
|
|
|
|
|
- regist_response.wifiNumber = 3;
|
|
|
- // 解析wifi
|
|
|
-
|
|
|
-
|
|
|
+ // 判断是否解析WiFi信息
|
|
|
+ if(regist_response.networkType == 3)
|
|
|
+ {
|
|
|
+ Log_Printf_Debug("---------------------解析WiFi信息----------------------\r\n");
|
|
|
+ cJSON *wifi_Array = cJSON_GetObjectItem(json, "wifi");
|
|
|
+ regist_response.wifiNumber = cJSON_GetArraySize(wifi_Array);
|
|
|
+ Log_Printf_Debug("wifiNumber=%d\r\n",regist_response.wifiNumber);
|
|
|
+ if (cJSON_IsArray(wifi_Array))
|
|
|
+ {
|
|
|
+ cJSON *wifiElement = NULL;
|
|
|
+ cJSON_ArrayForEach(wifiElement, wifi_Array)
|
|
|
+ {
|
|
|
+ // 提取wifiName
|
|
|
+ cJSON *wifiName = cJSON_GetObjectItem(wifiElement, "wifiName");
|
|
|
+ strcpy(regist_response.wifi[wifi_count].wifiName,wifiName->valuestring);
|
|
|
+ // 提取wifiPassword
|
|
|
+ cJSON *wifiPassword = cJSON_GetObjectItem(wifiElement, "wifiPassword");
|
|
|
+ strcpy(regist_response.wifi[wifi_count].wifiPassword,wifiPassword->valuestring);
|
|
|
+ //解析WiFi信息计数
|
|
|
+ wifi_count++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //校验解析的WiFi信息数目是否正确
|
|
|
+ if(wifi_count != regist_response.wifiNumber)
|
|
|
+ {
|
|
|
+ Log_Printf_Debug("wifi_count=%d\r\n",wifi_count);
|
|
|
+ Log_Printf_Debug("解析wifi信息ERROR!!!\r\n");
|
|
|
+ result = 0;
|
|
|
+ cJSON_Delete(json);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ //打印解析的WiFi信息
|
|
|
+ Log_Printf_Debug("解析出%d组wifi信息如下:\r\n",wifi_count);
|
|
|
+ for(int i=0;i<wifi_count;i++)
|
|
|
+ {
|
|
|
+ Log_Printf_Debug("wifi[%d]->wifiName: %s\r\n", i,regist_response.wifi[i].wifiName);
|
|
|
+ Log_Printf_Debug("wifi[%d]->wifiPassword: %s\r\n", i,regist_response.wifi[i].wifiPassword);
|
|
|
+ }
|
|
|
+ Log_Printf_Debug("-------------------------结束--------------------------\r\n");
|
|
|
+ }
|
|
|
result = 1;
|
|
|
WriteRegistResponseToFlash();
|
|
|
}
|
|
|
@@ -357,34 +412,33 @@ void TUORENIOT_PrintfRegistRequestStruct(struct TUORENIOT_RegistRequestStruct *
|
|
|
{
|
|
|
Log_Printf("------print regist params start------\r\n");
|
|
|
Log_Printf("version=%d\r\n", regist_params_struct->version);
|
|
|
- Log_Printf("networkType=%s\r\n", regist_params_struct->networkType);
|
|
|
- Log_Printf("networkProtocol=%s\r\n", regist_params_struct->networkProtocol);
|
|
|
+ Log_Printf("networkType=%d\r\n", regist_params_struct->networkType);
|
|
|
+ Log_Printf("networkProtocol=%d\r\n", regist_params_struct->networkProtocol);
|
|
|
+ Log_Printf("productId=%s\r\n", regist_params_struct->productId);
|
|
|
Log_Printf("deviceId=%s\r\n", regist_params_struct->deviceId);
|
|
|
- Log_Printf("platform=%s\r\n", regist_params_struct->platform);
|
|
|
- Log_Printf("userId=%d\r\n", regist_params_struct->userId);
|
|
|
+ Log_Printf("userId=%s\r\n", regist_params_struct->userId);
|
|
|
Log_Printf("------print regist params end------\r\n");
|
|
|
}
|
|
|
|
|
|
-
|
|
|
void TUORENIOT_PrintfRegistResponseStruct(void)
|
|
|
{
|
|
|
Log_Printf("------print regist result start------\r\n");
|
|
|
Log_Printf("code=%d\r\n", regist_response.code);
|
|
|
Log_Printf("version=%d\r\n", regist_response.version);
|
|
|
- Log_Printf("networkProtocol=%s\r\n", regist_response.networkProtocol);
|
|
|
- Log_Printf("platform=%s\r\n", regist_response.platform);
|
|
|
+ Log_Printf("platform=%d\r\n", regist_response.platform);
|
|
|
+ Log_Printf("networkType=%d\r\n", regist_response.networkType);
|
|
|
+ Log_Printf("networkProtocol=%d\r\n", regist_response.networkProtocol);
|
|
|
+ Log_Printf("productId=%s\r\n", regist_response.productId);
|
|
|
Log_Printf("deviceId=%s\r\n", regist_response.deviceId);
|
|
|
+ Log_Printf("userId=%s\r\n", regist_response.userId);
|
|
|
Log_Printf("deviceSecret=%s\r\n", regist_response.deviceSecret);
|
|
|
Log_Printf("remoteAddress=%s\r\n", regist_response.remoteAddress);
|
|
|
Log_Printf("remotePort=%d\r\n", regist_response.remotePort);
|
|
|
- Log_Printf("productId=%s\r\n", regist_response.productId);
|
|
|
- Log_Printf("userId=%d\r\n", regist_response.userId);
|
|
|
Log_Printf("wifiNumber=%d\r\n", regist_response.wifiNumber);
|
|
|
Log_Printf("------print regist result end------\r\n");
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
// 初始化
|
|
|
void TUORENIOT_Init(void)
|
|
|
{
|