Ec800m_Regist.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "CONFIG.h"
  2. #if EC800M
  3. /**
  4. ******************************************************************************
  5. * 版 本 :V1.0.0
  6. * 作 者 :liuyanbin
  7. * 版权所有,盗版必究。
  8. * Copyright(C) All rights reserved
  9. ******************************************************************************
  10. * 历史记录
  11. * 序号 版本 变更内容 作者 日期
  12. * 1 V0.0.1 实现4G注册 刘艳斌 2023/12/20
  13. * 2 V0.0.2 实现NB注册 刘艳斌 2023/12/20
  14. * 3 V0.0.3 整合4g和nb 刘艳斌 2024/1/24
  15. *
  16. ******************************************************************************
  17. */
  18. #include "stm32f10x.h"
  19. #include "Regist.h"
  20. #include "ec800m.h"
  21. #include "INflash.h"
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "cJSON.h"
  27. #include "Delay.h"
  28. #include "mbedtls_util.h"
  29. #include "Log_Module.h"
  30. #include "At_Module.h"
  31. #include "Common_Util.h"
  32. #include "Tuoreniot.h"
  33. static uint8_t register_status;//注册成功,向flash存储标志
  34. static Coefficient_Data flashdata;
  35. static uint8_t tcpconnectID = 2;
  36. // 生成环境
  37. static char * REGIST_SERVER = "iot.tuoren.com" ; //注册服务器地址
  38. static uint16_t REGIST_PORT = 8888; //端口号
  39. // 开发环境
  40. //static char * REGIST_SERVER = "z18z017026.51vip.biz" ; //注册服务器地址
  41. //static uint16_t REGIST_PORT = 31396; //端口号
  42. // 注册参数
  43. static struct Regist_Params_Struct regist_params_struct = {
  44. .platform = "aliyun",
  45. .connType = "CoAP",
  46. .deviceId = "123456",
  47. .userId = 4562
  48. };
  49. static uint8_t regist_data[512];
  50. static uint16_t regist_data_length;
  51. //清空flash
  52. void clearflash(void){
  53. flashdata.read_flag=0;
  54. memset(flashdata.deviceSecret, 0, sizeof(flashdata.deviceSecret));
  55. memset(flashdata.productKey, 0, sizeof(flashdata.productKey));
  56. memset(flashdata.deviceName, 0, sizeof(flashdata.deviceName));
  57. delay_ms(10);
  58. STMFLASH_Write(FLASH_SAVE_ADDR,(uint16_t*)&flashdata,sizeof(flashdata));
  59. }
  60. void storedata(void)
  61. {
  62. flashdata.read_flag=1;
  63. strcpy(flashdata.deviceSecret, regist_params_struct.deviceSecret);
  64. strcpy(flashdata.productKey, regist_params_struct.productKey);
  65. strcpy(flashdata.deviceName, regist_params_struct.deviceName);
  66. delay_ms(10);
  67. STMFLASH_Write(FLASH_SAVE_ADDR,(uint16_t*)&flashdata,sizeof(flashdata));
  68. flashdata.read_flag=0;
  69. delay_ms(10);
  70. Log_Printf_Debug("写入数据完成\r\n");
  71. STMFLASH_Read(FLASH_SAVE_ADDR,(uint16_t*)&flashdata,sizeof(flashdata));
  72. Log_Printf_Debug("读取完成read_flag:%d,deviceSecret:%s,productKey:%s,deviceName:%s\r\n",flashdata.read_flag,flashdata.deviceSecret,flashdata.productKey,flashdata.deviceName);
  73. }
  74. //4G模块注册流程
  75. uint8_t regist_Handle()
  76. {
  77. uint8_t mode,stat,regist_flag = 0;
  78. uint16_t lac,err = 0;
  79. uint32_t ci = 0;
  80. enum Result result = Result_None;
  81. ec800m.exit_sleep();//退出休眠
  82. delay_ms(10);
  83. result=ec800m.set_sleep_sync(1);//设置休眠模式
  84. if(result==Result_Success)
  85. {
  86. result=ec800m.set_cfun_sync(1);//设置全功能模式
  87. }
  88. if(result==Result_Success)
  89. {
  90. result=ec800m.set_cgreg_sync(2);//设置网络状态
  91. }
  92. if(result==Result_Success)
  93. {
  94. for(int i=0;i<20;i++){
  95. delay_ms(400);
  96. result=ec800m.query_cgreg_sync(&mode,&stat,&lac,&ci);//循环查询网络状态最多20次
  97. if(stat==1)
  98. {
  99. i=20;
  100. }
  101. }
  102. }
  103. if(result==Result_Success&&stat==1)
  104. {
  105. //tcp连接流程
  106. result=ec800m.open_socket_sync(tcpconnectID,"TCP",REGIST_SERVER,REGIST_PORT,1,&err);//连接tcp
  107. }
  108. if(result==Result_Success)
  109. {
  110. result = ec800m.set_qisde_sync(0);//关闭回显
  111. }
  112. if(result==Result_Success)
  113. {
  114. memset(regist_data, 0, sizeof(regist_data));
  115. byteToHexStr(mcu_id, regist_params_struct.deviceId, sizeof(mcu_id));
  116. packRegistParams(regist_data, &regist_data_length, regist_params_struct);
  117. printf_regist_param_struct(regist_params_struct);
  118. result =ec800m.send_sync(tcpconnectID, regist_data, regist_data_length);//数据发送
  119. }
  120. if(result==Result_Success)
  121. {
  122. memset(regist_data, 0, sizeof(regist_data));
  123. result =ec800m.recv_with_time_sync(tcpconnectID,regist_data,&regist_data_length,10000);
  124. if(result==Result_Failed)
  125. {
  126. return result;
  127. }
  128. // 解析注册数据
  129. regist_flag = analysisRegistData(regist_data, regist_data_length, &regist_params_struct);
  130. printf_regist_param_struct(regist_params_struct);
  131. }
  132. if(result==Result_Success)
  133. {
  134. result=ec800m.close_socket_sync(tcpconnectID);//断开tcp连接
  135. }
  136. ec800m.set_cfun_sync(0);//设置最小功能模式
  137. ec800m.enter_sleep();//进入休眠状态
  138. return regist_flag;
  139. }
  140. uint8_t regist_device_sync(void)
  141. {
  142. flashdata.read_flag=0;
  143. //读取flash
  144. Log_Printf_Debug("读取前read_flag:%d\r\n",flashdata.read_flag);
  145. STMFLASH_Read(FLASH_SAVE_ADDR,(u16*)&flashdata,sizeof(flashdata));//读取flash
  146. delay_ms(10);
  147. //已经注册。读取flash成功
  148. if(flashdata.read_flag == 1)
  149. {
  150. register_status=1;
  151. 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);
  152. return 1;
  153. }
  154. //未注册开始注册流程
  155. register_status=regist_Handle();//注册流程
  156. if (register_status==0)//注册流程失败不储存flash
  157. {
  158. clearflash();
  159. return 0;
  160. }
  161. else
  162. {
  163. storedata();//存储到flash
  164. return 1;
  165. }
  166. }
  167. enum Regist_Result regist_get_result(void)
  168. {
  169. if(register_status == 1){
  170. return Regist_Result_Success;
  171. }
  172. else
  173. {
  174. return Regist_Result_Failure;
  175. }
  176. }
  177. char * regist_get_aliyun_productKey(void)
  178. {
  179. return flashdata.productKey;
  180. }
  181. char * regist_get_aliyun_deviceName(void)
  182. {
  183. return flashdata.deviceName;
  184. }
  185. char * regist_get_aliyun_deviceSecret(void)
  186. {
  187. return flashdata.deviceSecret;
  188. }
  189. #endif