| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- #include "CONFIG.h"
- #if BC260Y
- /**
- ******************************************************************************
- * 版 本 :V1.0.0
- * 作 者 :liuyanbin
- * 版权所有,盗版必究。
- * Copyright(C) All rights reserved
- ******************************************************************************
- * 历史记录
- * 序号 版本 变更内容 作者 日期
- * 1 V0.0.1 实现4G注册 刘艳斌 2023/12/20
- * 2 V0.0.2 实现NB注册 刘艳斌 2023/12/20
- * 3 V0.0.3 整合4g和nb 刘艳斌 2024/1/24
- *
- ******************************************************************************
- */
- #include "stm32f10x.h"
- #include "Regist.h"
- #include "bc260y.h"
- #include "INflash.h"
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
- #include <stdlib.h>
- #include "cJSON.h"
- #include "mbedtls_util.h"
- #include "Log_Module.h"
- #include "At_Module.h"
- #include "Common_Util.h"
- #include "Tuoreniot.h"
- static Coefficient_Data flashdata;
- extern uint8_t mcu_id[8];
- #if PROD_ENV
- // 生产环境
- static char * REGIST_SERVER = "iot.tuoren.com" ; //注册服务器地址
- static uint16_t REGIST_PORT = 8888; //端口号
- #endif
- #if TEST_ENV
- // 开发环境
- static char * REGIST_SERVER = "z18z017026.51vip.biz" ; //注册服务器地址
- static uint16_t REGIST_PORT = 31396; //端口号
- #endif
- static uint8_t tcpconnectID = 2;
- // 注册参数
- static struct Regist_Params_Struct regist_params_struct = {
- .platform = "aliyun",
- .connType = "CoAP",
- .deviceId = "3431228A1936013C",
- .userId = 4562
- };
- static uint8_t regist_data[512];
- static uint16_t regist_data_length;
- // dns
- static char pridnsaddr[20];
- //清空flash
- void clearflash(void){
- 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));
- my_delay_ms(10);
- STMFLASH_Write(FLASH_SAVE_ADDR,(uint16_t*)&flashdata,sizeof(flashdata));
- }
- void storedata(void)
- {
- 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);
-
- my_delay_ms(10);
- STMFLASH_Write(FLASH_SAVE_ADDR,(uint16_t*)&flashdata,sizeof(flashdata));
- flashdata.read_flag=0;
- my_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);
- }
- uint8_t regist_Handle()
- {
- uint8_t cfun_mode, mode,stat,regist_flag = 0;
- uint16_t lac,err = 0;
- uint32_t ci = 0;
- enum Result result = Result_None;
- // GPIO_SetBits(WAKE_GPIO, WAKE_GPIO_Pin);
- Log_Printf_Debug("\r\n>>退出休眠\r\n");
- result = bc260y.exit_sleep_2_sync();//退出休眠
- if(result==Result_Success)
- {
- Log_Printf_Debug("\r\n>>开启回显\r\n");
- result = bc260y.set_qisde_sync(1);//开启回显
- }
- if(result==Result_Success)
- {
- Log_Printf_Debug("\r\n>>查询DNS\r\n");
- memset(pridnsaddr, 0, sizeof(pridnsaddr));
- result = bc260y.query_dns_sync(pridnsaddr); // 查询DNS
- Log_Printf_Debug("DNS: %s, %d\r\n", pridnsaddr, strlen(pridnsaddr));
- }
- if(result==Result_Success)
- {
- if(strlen(pridnsaddr) <= 1)
- {
- Log_Printf_Debug("\r\n>>设置DNS\r\n");
- result = bc260y.set_dns_sync("114.114.114.114", "8.8.8.8"); // 设置DNS
- }
- }
- if(result==Result_Success)
- {
- Log_Printf_Debug("\r\n>>查询功能模式\r\n");
- result=bc260y.query_cfun_sync(&cfun_mode);//查询功能模式
- }
- if(result==Result_Success)
- {
- if(cfun_mode == 1)
- {
- Log_Printf_Debug("\r\n>>设置最小功能模式\r\n");
- result=bc260y.set_cfun_sync(0);//设置最小功能模式
- }
- }
- if(result==Result_Success)
- {
- Log_Printf_Debug("\r\n>>设置休眠模式\r\n");
- result=bc260y.set_sleep_sync(2);//设置休眠模式
- }
- if(result==Result_Success)
- {
- Log_Printf_Debug("\r\n>>设置数据格式\r\n");
- result = bc260y.dataformat_sync(1); // 设置数据格式, 十六进制
- }
-
-
- if(result==Result_Success)
- {
- Log_Printf_Debug("\r\n>>设置全功能模式\r\n");
- result=bc260y.set_cfun_sync(1);//设置全功能模式
- }
-
-
-
-
- if(result==Result_Success)
- {
- Log_Printf_Debug("\r\n>>设置网络状态\r\n");
- result=bc260y.set_cereg_sync(2);//设置网络状态
- }
-
- if(result==Result_Success)
- {
- Log_Printf_Debug("\r\n>>查询网络状态\r\n");
- result=bc260y.query_cereg_sync(&mode,&stat,&lac,&ci);// 查询网络状态
- }
- if(result==Result_Success)
- {
- Log_Printf_Debug("\r\n>>断开tcp连接\r\n");
- result=bc260y.close_socket_sync(tcpconnectID);//防止以前链接没断开,断开tcp链接
- }
-
- if(result==Result_Success)
- {
- Log_Printf_Debug("\r\n>>连接tcp\r\n");
- result=bc260y.open_socket_sync(tcpconnectID,"TCP",REGIST_SERVER,REGIST_PORT,1,&err);//连接tcp
- }
-
- if(result==Result_Success)
- {
- Log_Printf_Debug("\r\n>>发送数据\r\n");
- memset(regist_params_struct.deviceId, 0, sizeof(regist_params_struct.deviceId));
- byteToHexStr(mcu_id, regist_params_struct.deviceId, sizeof(mcu_id));
- memset(regist_data, 0, sizeof(regist_data));
- packRegistParams(regist_data, ®ist_data_length, regist_params_struct);
- printf_regist_param_struct(regist_params_struct);
- result =bc260y.send_rai_sync(tcpconnectID, regist_data, regist_data_length, 0);//数据发送
- }
-
- if(result==Result_Success)
- {
- Log_Printf_Debug("\r\n>>接收数据\r\n");
- memset(regist_data, 0, sizeof(regist_data));
- result =bc260y.recv_with_time_sync(tcpconnectID,regist_data,®ist_data_length,10000);
- // Log_Printf_Debug("解密前:\r\n");
- // Log_SendHex(regist_data, regist_data_length);
- // Log_Printf_Debug("\r\n");
- }
-
-
- if(result == Result_Success)
- {
- Log_Printf_Debug("\r\n>>解析数据\r\n");
- // 解析注册数据
- regist_flag = analysisRegistData(regist_data, regist_data_length, ®ist_params_struct);
- printf_regist_param_struct(regist_params_struct);
-
- Log_Printf_Debug("\r\n>>断开tcp连接\r\n");
- result=bc260y.close_socket_sync(tcpconnectID);//断开tcp连接
- }
-
- // 注册失败,进入最小功能模式,防止耗电
- if(result == Result_Failed)
- {
- Log_Printf_Debug("\r\n>>设置最小功能模式\r\n");
- bc260y.set_cfun_sync(0);//设置最小功能模式
- }
-
- return regist_flag;
- }
- static uint8_t register_status = 0;//注册成功,向flash存储标志
- uint8_t regist_device_sync(void)
- {
- //未注册开始注册流程
- register_status=regist_Handle();//注册流程
- if (register_status==0)//注册流程失败不储存flash
- {
- clearflash();
- }
- else
- {
- storedata();//存储到flash
- }
- return register_status;
- }
- static uint8_t read_flash_flag = 0; // 读取flash的标志
- enum Regist_Result regist_get_result(void)
- {
- if(read_flash_flag == 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
- my_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);
- }
- read_flash_flag = 1;
- }
-
- if(flashdata.read_flag == 1){
- return Regist_Result_Success;
- }
- else
- {
- return Regist_Result_None;
- }
- }
- char * regist_get_aliyun_productKey(void)
- {
- return flashdata.productKey;
- }
- char * regist_get_aliyun_deviceName(void)
- {
- return flashdata.deviceName;
- }
- char * regist_get_aliyun_deviceSecret(void)
- {
- return flashdata.deviceSecret;
- }
- #endif
|