| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- #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 "Delay.h"
- #include "mbedtls_util.h"
- #include "Log_Module.h"
- #include "At_Module.h"
- #include "Common_Util.h"
- #include "Tuoreniot.h"
- static uint8_t register_status;//注册成功,向flash存储标志
- static Coefficient_Data flashdata;
- 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 char * REGIST_SERVER = "z18z017026.51vip.biz" ; //注册服务器地址
- //static uint16_t REGIST_PORT = 31396; //端口号
- // 注册参数
- 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
- 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));
- 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);
-
- 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);
- }
- uint8_t regist_Handle()
- {
- uint8_t 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);
- bc260y.exit_sleep_sync();//退出休眠
- delay_ms(100);
- bc260y.reset();
- result = bc260y.set_qisde_sync(0);//关闭回显
- if(result==Result_Failed)
- {
- return 0;
- }
- result=bc260y.set_sleep_sync(1);//设置休眠模式
- if(result==Result_Failed)
- {
- return 0;
- }
- result=bc260y.set_cfun_sync(1);//设置全功能模式
-
- if(result==Result_Failed)
- {
- return 0;
- }
- result=bc260y.set_cgreg_sync(2);//设置网络状态
- if(result==Result_Failed)
- {
- return 0;
- }
-
- for(int i=0;i<20;i++){
- delay_ms(400);
- result=bc260y.query_cgreg_sync(&mode,&stat,&lac,&ci);//循环查询网络状态最多20次
- if(stat==1)
- {i=20;
-
- }
- }
- // if(result==Result_Failed)
- // {
- // return 0;
- // }
- // if(stat==0)
- // {
- // Log_Printf_Debug(" stat=%d\r\n", stat);
- // return 0;
- // }
-
- if(result==Result_Success&&stat==1)
- {
- result=bc260y.close_socket_sync(tcpconnectID);//防止以前链接没断开,断开tcp链接
- }
- if(result==Result_Failed)
- {
- Log_Printf_Debug(" result=%d\r\n", result);
- return 0;
- }
- //tcp连接流程
- result=bc260y.open_socket_sync(tcpconnectID,"TCP",REGIST_SERVER,REGIST_PORT,1,&err);//连接tcp
- if(result==Result_Failed)
- {
- Log_Printf_Debug(" result=%d\r\n", result);
- 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);//配置发送数据格式
- if(result==Result_Failed)
- {
- return 0;
- }
- 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)
- {
- result=bc260y.close_socket_sync(tcpconnectID);//断开tcp连接
- }
- bc260y.enter_sleep();//进入休眠状态
- return regist_flag;
- }
- uint8_t regist_device_sync(void)
- {
- 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)
- {
- if(register_status==1){
- return Regist_Result_Success;
- }
- else
- {
- return Regist_Result_Failure;
- }
- }
- 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
|