|
@@ -31,139 +31,64 @@
|
|
|
#include "Log_Module.h"
|
|
#include "Log_Module.h"
|
|
|
#include "At_Module.h"
|
|
#include "At_Module.h"
|
|
|
#include "Common_Util.h"
|
|
#include "Common_Util.h"
|
|
|
-static uint8_t mcu_id[8]="8945809";
|
|
|
|
|
-static uint16_t Version_data = 1032;
|
|
|
|
|
-static uint8_t regist_result =0;
|
|
|
|
|
|
|
+#include "Tuoreniot.h"
|
|
|
|
|
+
|
|
|
static uint8_t register_status;//注册成功,向flash存储标志
|
|
static uint8_t register_status;//注册成功,向flash存储标志
|
|
|
static Coefficient_Data flashdata;
|
|
static Coefficient_Data flashdata;
|
|
|
-//uint8_t registlen=0;
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-unsigned char key[17]="tuorenzhinenghua";
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+static uint8_t mcu_id[8]="ceshi001";
|
|
|
|
|
+// 生成环境
|
|
|
|
|
+static char * REGIST_SERVER = "iot.tuoren.com" ; //注册服务器地址
|
|
|
|
|
+static uint16_t REGIST_PORT = 8888; //端口号
|
|
|
|
|
|
|
|
static uint8_t tcpconnectID = 2;
|
|
static uint8_t tcpconnectID = 2;
|
|
|
-static char * REGIST_SERVER ="8337239yf4.yicp.fun" ; //注册服务器地址
|
|
|
|
|
-static uint16_t REGIST_PORT=22155; //端口号
|
|
|
|
|
-static char qcciddata[31]={0};//
|
|
|
|
|
|
|
+// 开发环境
|
|
|
|
|
+//static char * REGIST_SERVER = "z18z017026.51vip.biz" ; //注册服务器地址
|
|
|
|
|
+//static uint16_t REGIST_PORT = 31396; //端口号
|
|
|
|
|
|
|
|
-static char recvdata[512]={0};//和registData共用
|
|
|
|
|
|
|
+// 注册参数
|
|
|
|
|
+static struct Regist_Params_Struct regist_params_struct = {
|
|
|
|
|
+ .platform = "aliyun",
|
|
|
|
|
+ .connType = "CoAP",
|
|
|
|
|
+ .deviceId = "123456",
|
|
|
|
|
+ .userId = 4562
|
|
|
|
|
+};
|
|
|
|
|
+static uint8_t regist_data[512];
|
|
|
|
|
+static uint16_t regist_data_length;
|
|
|
|
|
|
|
|
|
|
|
|
|
//清空flash
|
|
//清空flash
|
|
|
void clearflash(void){
|
|
void clearflash(void){
|
|
|
-flashdata.read_flag=0;
|
|
|
|
|
-memcpy(flashdata.deviceSecret,"0",40);
|
|
|
|
|
-memcpy(flashdata.productKey,"0",20);
|
|
|
|
|
-memcpy(flashdata.deviceName,"0",20);
|
|
|
|
|
-delay_ms(10);
|
|
|
|
|
-STMFLASH_Write(FLASH_SAVE_ADDR,(uint16_t*)&flashdata,sizeof(flashdata));
|
|
|
|
|
|
|
+ flashdata.read_flag=0;
|
|
|
|
|
+ memset(flashdata.deviceSecret, 0, sizeof(flashdata.deviceSecret));
|
|
|
|
|
+ memset(flashdata.productKey, 0, sizeof(flashdata.productKey));
|
|
|
|
|
+ memset(flashdata.deviceName, 0, sizeof(flashdata.deviceName));
|
|
|
|
|
+ delay_ms(10);
|
|
|
|
|
+ STMFLASH_Write(FLASH_SAVE_ADDR,(uint16_t*)&flashdata,sizeof(flashdata));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void storedata(void){
|
|
|
|
|
-
|
|
|
|
|
-flashdata.read_flag=1;
|
|
|
|
|
- Log_Printf_Debug("###########\r\n");
|
|
|
|
|
- Log_Printf_Debug("deviceSecret:%s,productKey:%s,deviceName:%s,read_flag:%d\r\n",flashdata.deviceSecret,flashdata.productKey,flashdata.deviceName,flashdata.read_flag);
|
|
|
|
|
- Log_Printf_Debug("结构体大小: %d\r\n", sizeof(Coefficient_Data));
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- delay_ms(10);
|
|
|
|
|
- STMFLASH_Write(FLASH_SAVE_ADDR,(uint16_t*)&flashdata,sizeof(flashdata));
|
|
|
|
|
- flashdata.read_flag=0;
|
|
|
|
|
- delay_ms(10);
|
|
|
|
|
- Log_Printf_Debug("写入数据完成\r\n");
|
|
|
|
|
- STMFLASH_Read(FLASH_SAVE_ADDR,(uint16_t*)&flashdata,sizeof(flashdata));
|
|
|
|
|
- Log_Printf_Debug("读取完成read_flag:%d,deviceSecret:%s,productKey:%s,deviceName:%s\r\n",flashdata.read_flag,flashdata.deviceSecret,flashdata.productKey,flashdata.deviceName);
|
|
|
|
|
-
|
|
|
|
|
-}
|
|
|
|
|
-/**
|
|
|
|
|
-*
|
|
|
|
|
-*注册信息打包
|
|
|
|
|
-*/
|
|
|
|
|
-enum Result packRegistDataAnd(uint8_t *length){
|
|
|
|
|
- enum Result result = Result_None;
|
|
|
|
|
-// int i=0;
|
|
|
|
|
- int datalen = 0;
|
|
|
|
|
- memset(qcciddata,'\0',sizeof(qcciddata));
|
|
|
|
|
- memset(recvdata,'\0', sizeof(recvdata));
|
|
|
|
|
- char* dataPtr = recvdata+2; //dataPtr指针指向registData第三个字节
|
|
|
|
|
- //复制muid,并转化为16进制
|
|
|
|
|
- byteToHexStr(mcu_id, dataPtr, sizeof(mcu_id));
|
|
|
|
|
- memcpy(dataPtr+strlen(dataPtr),"&",1);
|
|
|
|
|
- //获取qccid
|
|
|
|
|
- AT_Clear();
|
|
|
|
|
-
|
|
|
|
|
- result=bc260y.query_qccid_sync(qcciddata);
|
|
|
|
|
-
|
|
|
|
|
- if(result==Result_Failed)
|
|
|
|
|
- {
|
|
|
|
|
- return result;
|
|
|
|
|
- }
|
|
|
|
|
- //获取复制qccid
|
|
|
|
|
- memcpy(dataPtr+strlen(dataPtr),qcciddata,31);
|
|
|
|
|
- //复制版本号
|
|
|
|
|
- snprintf(dataPtr+strlen(dataPtr),10,"&%d.%03d",Version_data/1000,Version_data%1000);
|
|
|
|
|
- Log_Printf_Debug("device data:%s,%d\r\n",dataPtr,strlen(dataPtr));//串口打印
|
|
|
|
|
- Log_Printf_Debug("regist data:%s\r\n",recvdata+2);
|
|
|
|
|
- datalen = utils_aes128_ECB_base64_enc_with_length((char *)key,(uint8_t *)dataPtr);
|
|
|
|
|
-
|
|
|
|
|
- recvdata[0] = datalen>>8;//头2个字节赋值数据长度
|
|
|
|
|
- recvdata[1] = datalen;
|
|
|
|
|
- Log_Printf_Debug("strlendata:%d,datalen=%d\r\n",strlen(dataPtr),datalen);
|
|
|
|
|
- *length=datalen+2;
|
|
|
|
|
- return Result_Success;
|
|
|
|
|
-}
|
|
|
|
|
-/**
|
|
|
|
|
-*
|
|
|
|
|
-*注册数据解密
|
|
|
|
|
-*/
|
|
|
|
|
-enum Result analysis_recvdata()
|
|
|
|
|
|
|
+void storedata(void)
|
|
|
{
|
|
{
|
|
|
- enum Result result = Result_None;
|
|
|
|
|
|
|
+ flashdata.read_flag=1;
|
|
|
|
|
+ strcpy(flashdata.deviceSecret, regist_params_struct.deviceSecret);
|
|
|
|
|
+ strcpy(flashdata.productKey, regist_params_struct.productKey);
|
|
|
|
|
+ strcpy(flashdata.deviceName, regist_params_struct.deviceName);
|
|
|
|
|
|
|
|
- uint16_t recvdata_length=0;
|
|
|
|
|
- cJSON *json = NULL,*json_productKey = NULL,*json_deviceName = NULL,*json_deviceSecret = NULL;
|
|
|
|
|
- memset(recvdata, '\0', sizeof(recvdata));
|
|
|
|
|
- result =bc260y.recv_with_time_sync(tcpconnectID,(uint8_t *)recvdata,&recvdata_length,10000);
|
|
|
|
|
-
|
|
|
|
|
- if(result==Result_Failed)
|
|
|
|
|
- {
|
|
|
|
|
- return result;
|
|
|
|
|
- }
|
|
|
|
|
- Log_Printf_Debug("recvdata:%d",strlen(recvdata));
|
|
|
|
|
|
|
+ delay_ms(10);
|
|
|
|
|
+ STMFLASH_Write(FLASH_SAVE_ADDR,(uint16_t*)&flashdata,sizeof(flashdata));
|
|
|
|
|
+ flashdata.read_flag=0;
|
|
|
|
|
+ delay_ms(10);
|
|
|
|
|
+ Log_Printf_Debug("写入数据完成\r\n");
|
|
|
|
|
+ STMFLASH_Read(FLASH_SAVE_ADDR,(uint16_t*)&flashdata,sizeof(flashdata));
|
|
|
|
|
+ Log_Printf_Debug("读取完成read_flag:%d,deviceSecret:%s,productKey:%s,deviceName:%s\r\n",flashdata.read_flag,flashdata.deviceSecret,flashdata.productKey,flashdata.deviceName);
|
|
|
|
|
|
|
|
- utils_aes128_ECB_base64_dec((char *)key,(unsigned char*)recvdata,recvdata_length);
|
|
|
|
|
- Log_Printf_Debug("解密数据recvdata:%s,%d,%d\r\n",recvdata,strlen(recvdata),recvdata_length);
|
|
|
|
|
- json = cJSON_Parse(recvdata);
|
|
|
|
|
- if(!json)
|
|
|
|
|
- {
|
|
|
|
|
- Log_Printf_Debug("json parse error,%s\r\n",cJSON_GetErrorPtr());
|
|
|
|
|
- cJSON_Delete(json);
|
|
|
|
|
- return Result_Failed;
|
|
|
|
|
-
|
|
|
|
|
- }else{
|
|
|
|
|
- json_deviceSecret = cJSON_GetObjectItem(json,"deviceSecret");
|
|
|
|
|
- json_productKey = cJSON_GetObjectItem(json,"productKey");
|
|
|
|
|
- json_deviceName = cJSON_GetObjectItem(json,"deviceName");
|
|
|
|
|
- Log_Printf_Debug("获取数据成功deviceSecret:%s,json_productKey:%s,deviceName:%s\r\n",json_deviceSecret->valuestring,json_productKey->valuestring,json_deviceName->valuestring);
|
|
|
|
|
- strcpy(flashdata.deviceSecret,json_deviceSecret->valuestring);
|
|
|
|
|
- strcpy(flashdata.productKey,json_productKey->valuestring);
|
|
|
|
|
- strcpy(flashdata.deviceName,json_deviceName->valuestring);
|
|
|
|
|
- memset(®ister_status, 1, sizeof(register_status));
|
|
|
|
|
- cJSON_Delete(json);
|
|
|
|
|
-
|
|
|
|
|
- return Result_Success;
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
uint8_t regist_Handle()
|
|
uint8_t regist_Handle()
|
|
|
{
|
|
{
|
|
|
- uint8_t stat,length=0;
|
|
|
|
|
- uint16_t lac,err=0;
|
|
|
|
|
- uint32_t ci=0;
|
|
|
|
|
|
|
+uint8_t mode,stat,regist_flag = 0;
|
|
|
|
|
+ uint16_t lac,err = 0;
|
|
|
|
|
+ uint32_t ci = 0;
|
|
|
enum Result result = Result_None;
|
|
enum Result result = Result_None;
|
|
|
GPIO_SetBits(WAKE_GPIO, WAKE_GPIO_Pin);
|
|
GPIO_SetBits(WAKE_GPIO, WAKE_GPIO_Pin);
|
|
|
bc260y.exit_sleep_sync();//退出休眠
|
|
bc260y.exit_sleep_sync();//退出休眠
|
|
@@ -194,29 +119,27 @@ GPIO_SetBits(WAKE_GPIO, WAKE_GPIO_Pin);
|
|
|
|
|
|
|
|
for(int i=0;i<20;i++){
|
|
for(int i=0;i<20;i++){
|
|
|
delay_ms(400);
|
|
delay_ms(400);
|
|
|
-result=bc260y.query_cgreg_sync(®ist_result,&stat,&lac,&ci);//循环查询网络状态最多20次
|
|
|
|
|
|
|
+ result=bc260y.query_cgreg_sync(&mode,&stat,&lac,&ci);//循环查询网络状态最多20次
|
|
|
if(stat==1)
|
|
if(stat==1)
|
|
|
{i=20;
|
|
{i=20;
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if(result==Result_Failed)
|
|
|
|
|
- {
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
- if(stat==0)
|
|
|
|
|
- {
|
|
|
|
|
- Log_Printf_Debug(" stat=%d\r\n", stat);
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+// if(result==Result_Failed)
|
|
|
|
|
+// {
|
|
|
|
|
+// return 0;
|
|
|
|
|
+// }
|
|
|
|
|
+// if(stat==0)
|
|
|
|
|
+// {
|
|
|
|
|
+// Log_Printf_Debug(" stat=%d\r\n", stat);
|
|
|
|
|
+// return 0;
|
|
|
|
|
+// }
|
|
|
|
|
|
|
|
- result =(enum Result)packRegistDataAnd(&length);//数据打包
|
|
|
|
|
- if(result==Result_Failed)
|
|
|
|
|
- {
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+if(result==Result_Success&&stat==1)
|
|
|
|
|
+ {
|
|
|
result=bc260y.close_socket_sync(tcpconnectID);//防止以前链接没断开,断开tcp链接
|
|
result=bc260y.close_socket_sync(tcpconnectID);//防止以前链接没断开,断开tcp链接
|
|
|
|
|
+ }
|
|
|
if(result==Result_Failed)
|
|
if(result==Result_Failed)
|
|
|
{
|
|
{
|
|
|
Log_Printf_Debug(" result=%d\r\n", result);
|
|
Log_Printf_Debug(" result=%d\r\n", result);
|
|
@@ -229,7 +152,14 @@ result=bc260y.query_cgreg_sync(®ist_result,&stat,&lac,&ci);//循
|
|
|
Log_Printf_Debug(" result=%d\r\n", result);
|
|
Log_Printf_Debug(" result=%d\r\n", result);
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+ if(result==Result_Success)
|
|
|
|
|
+ {
|
|
|
|
|
+ memset(regist_data, 0, sizeof(regist_data));
|
|
|
|
|
+ byteToHexStr(mcu_id, regist_params_struct.deviceId, sizeof(mcu_id));
|
|
|
|
|
+ packRegistParams(regist_data, ®ist_data_length, regist_params_struct);
|
|
|
|
|
+ printf_regist_param_struct(regist_params_struct);
|
|
|
|
|
+ result =bc260y.send_sync(tcpconnectID, regist_data, regist_data_length);//数据发送
|
|
|
|
|
+ }
|
|
|
result = bc260y.dataformat_sync(1);//配置发送数据格式
|
|
result = bc260y.dataformat_sync(1);//配置发送数据格式
|
|
|
if(result==Result_Failed)
|
|
if(result==Result_Failed)
|
|
|
{
|
|
{
|
|
@@ -237,64 +167,55 @@ result=bc260y.query_cgreg_sync(®ist_result,&stat,&lac,&ci);//循
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
- result =bc260y.send_sync(tcpconnectID,(uint8_t *)recvdata,length);//数据发送
|
|
|
|
|
- if(result==Result_Failed)
|
|
|
|
|
- {
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- result =analysis_recvdata();
|
|
|
|
|
- if(result==Result_Failed)
|
|
|
|
|
- {
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- result=bc260y.close_socket_sync(tcpconnectID);//断开tcp连接
|
|
|
|
|
- if(result==Result_Failed)
|
|
|
|
|
|
|
+ if(result==Result_Success)
|
|
|
|
|
+ {
|
|
|
|
|
+ memset(regist_data, 0, sizeof(regist_data));
|
|
|
|
|
+ result =bc260y.recv_with_time_sync(tcpconnectID,regist_data,®ist_data_length,10000);
|
|
|
|
|
+ if(result==Result_Failed)
|
|
|
|
|
+ {
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 解析注册数据
|
|
|
|
|
+ regist_flag = analysisRegistData(regist_data, regist_data_length, ®ist_params_struct);
|
|
|
|
|
+ printf_regist_param_struct(regist_params_struct);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(result==Result_Success)
|
|
|
{
|
|
{
|
|
|
- return 0;
|
|
|
|
|
|
|
+ result=bc260y.close_socket_sync(tcpconnectID);//断开tcp连接
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
bc260y.enter_sleep();//进入休眠状态
|
|
bc260y.enter_sleep();//进入休眠状态
|
|
|
- return 1;
|
|
|
|
|
|
|
+ return regist_flag;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
uint8_t regist_device_sync(void)
|
|
uint8_t regist_device_sync(void)
|
|
|
{
|
|
{
|
|
|
- flashdata.read_flag=0;
|
|
|
|
|
- uint8_t regist_flag=0;
|
|
|
|
|
-//读取flash
|
|
|
|
|
- Log_Printf_Debug("读取前read_flag:%d\r\n",flashdata.read_flag);
|
|
|
|
|
- STMFLASH_Read(FLASH_SAVE_ADDR,(u16*)&flashdata,sizeof(flashdata));//读取flash
|
|
|
|
|
- delay_ms(10);
|
|
|
|
|
-//已经注册。读取flash成功
|
|
|
|
|
- if(flashdata.read_flag==1)
|
|
|
|
|
- {
|
|
|
|
|
- Log_Printf_Debug("读取flash成功read_flag:%d,\ndeviceSecret:%s,\n productKey:%s,\ndeviceName:%s\r\n",flashdata.read_flag,flashdata.deviceSecret,flashdata.productKey,flashdata.deviceName);
|
|
|
|
|
- register_status=1;
|
|
|
|
|
- return 1;
|
|
|
|
|
- }
|
|
|
|
|
- //未注册开始注册流程
|
|
|
|
|
- regist_flag=regist_Handle();//注册流程
|
|
|
|
|
- if(regist_flag==0)//注册流程失败不储存flash
|
|
|
|
|
- {
|
|
|
|
|
- flashdata.read_flag=0;
|
|
|
|
|
- register_status=0;
|
|
|
|
|
- clearflash();
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
- //注册成功向flash储存
|
|
|
|
|
- if(register_status==1){
|
|
|
|
|
- storedata();//存储到flash
|
|
|
|
|
- return 1;
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- Log_Printf_Debug("注册失败。。。regist_flag:%d,register_status:%d\r\n",regist_flag,register_status);
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ flashdata.read_flag=0;
|
|
|
|
|
+ //读取flash
|
|
|
|
|
+ Log_Printf_Debug("读取前read_flag:%d\r\n",flashdata.read_flag);
|
|
|
|
|
+ STMFLASH_Read(FLASH_SAVE_ADDR,(u16*)&flashdata,sizeof(flashdata));//读取flash
|
|
|
|
|
+ delay_ms(10);
|
|
|
|
|
+ //已经注册。读取flash成功
|
|
|
|
|
+ if(flashdata.read_flag == 1)
|
|
|
|
|
+ {
|
|
|
|
|
+ register_status=1;
|
|
|
|
|
+ Log_Printf_Debug("读取flash成功read_flag:%d,\ndeviceSecret:%s,\n productKey:%s,\ndeviceName:%s\r\n",flashdata.read_flag,flashdata.deviceSecret,flashdata.productKey,flashdata.deviceName);
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ //未注册开始注册流程
|
|
|
|
|
+ register_status=regist_Handle();//注册流程
|
|
|
|
|
+ if (register_status==0)//注册流程失败不储存flash
|
|
|
|
|
+ {
|
|
|
|
|
+ clearflash();
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ storedata();//存储到flash
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
enum Regist_Result regist_get_result(void)
|
|
enum Regist_Result regist_get_result(void)
|
|
|
{
|
|
{
|