Bc260y_Regist.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include "CONFIG.h"
  2. #if BC260Y
  3. #include "stm32f10x.h"
  4. #include "Regist.h"
  5. #include "FlashSetup.h"
  6. #include <stdio.h>
  7. #include <stdarg.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include "cJSON.h"
  11. #include "mbedtls_util.h"
  12. #include "Log_Module.h"
  13. #include "At_Module.h"
  14. #include "Common_Util.h"
  15. #include "Tuoreniot.h"
  16. #include "bc260y.h"
  17. // socket ID
  18. static uint8_t connectID = 2;
  19. // 注册参数
  20. static struct TUORENIOT_RegistRequestStruct regist_request = {
  21. .version = 1,
  22. .networkType = 1,
  23. .networkProtocol = 1,
  24. .deviceId = "3431228A1936013C",
  25. .userId = "1000"
  26. };
  27. // dns
  28. static char pridnsaddr[20];
  29. // 函数声明
  30. static void REGIST_Process(void); // 注册流程
  31. static void REGIST_SetStatus(enum REGIST_StatusEnum status); // 设置注册状态
  32. static void REGIST_SetResult(enum REGIST_ResultEnum result); // 设置注册结果
  33. static void goto_start(void); // 开始
  34. static void goto_success(void); // 成功
  35. static void goto_failure(char * _info); // 失败
  36. static void goto_finish(void); // 结束
  37. // 流程
  38. enum Step{
  39. STEP_FINISH, // 结束
  40. STEP_START, // 开始
  41. STEP_EXIT_SLEEP, // 退出睡眠
  42. STEP_WAIT, // 等待
  43. STEP_STATE, // 查询状态
  44. STEP_OPEN, // 打开
  45. STEP_SET_SLEEP, // 设置休眠
  46. STEP_SET_QISDE_0, // 关闭发送回显
  47. STEP_SET_QISDE_1, // 打开发送回显
  48. STEP_SEND, // 发送
  49. STEP_RECV, // 等待发送结果
  50. STEP_QUERY_CFUN, // 查询功能模式
  51. STEP_SET_CFUN_0, // 设置最小功能模式
  52. STEP_SET_CFUN_1, // 设置全功能模式
  53. STEP_SET_CEREG_2, // 设置ps域允许上报网络注册和位置信息
  54. STEP_QUERY_CEREG, // 查询网络注册状态
  55. STEP_DATAFORMAT, // 配置发送格式
  56. STEP_CLOSE, // 关闭
  57. STEP_SUCCESS, // 成功
  58. STEP_FAILURE, // 失败
  59. STEP_QUERY_DNS, // 查询DNS
  60. STEP_SET_DNS, // 设置DNS
  61. };
  62. // 步骤跳转时的监听
  63. static uint8_t STEP_SET_CFUN_1_times = 0; // 全功能模式的次数
  64. static uint16_t goto_step_event(uint16_t ns)
  65. {
  66. // 重置bc260y状态
  67. bc260y.reset();
  68. // 流程控制
  69. uint16_t step = ns;
  70. if(ns == STEP_SET_CFUN_1 && STEP_SET_CFUN_1_times++ >= 1)
  71. {
  72. goto_failure("重新联网次数过多");
  73. step = STEP_FAILURE;
  74. }
  75. return step;
  76. }
  77. // 流程控制
  78. static struct PCTRL_Struct pctrl = {
  79. .current_step = STEP_FINISH,
  80. .next_step = STEP_FINISH,
  81. .step_wait = STEP_WAIT,
  82. .goto_step_listener = goto_step_event,
  83. };
  84. // 备注
  85. static char info[40];
  86. // 开始
  87. static void goto_start(void)
  88. {
  89. PCTRL_GotoStep(&pctrl, STEP_START, "开始");
  90. }
  91. // 成功
  92. static void goto_success(void)
  93. {
  94. REGIST_SetResult(REGIST_Result_Success);
  95. PCTRL_GotoStep(&pctrl, STEP_SUCCESS, "成功");
  96. }
  97. // 失败
  98. static void goto_failure(char * _info)
  99. {
  100. REGIST_SetResult(REGIST_Result_Failure);
  101. memset(info, 0, sizeof(info));
  102. strcpy(info, _info);
  103. PCTRL_GotoStep(&pctrl, STEP_FAILURE, "失败");
  104. Log_Printf_Debug("info:%s!\r\n", info);
  105. }
  106. // 结束
  107. static void goto_finish(void)
  108. {
  109. REGIST_SetStatus(REGIST_Status_Done);
  110. PCTRL_GotoStep(&pctrl, STEP_FINISH, "结束");
  111. }
  112. // 注册状态
  113. static enum REGIST_StatusEnum regist_status = REGIST_Status_None;
  114. // 注册结果
  115. static enum REGIST_ResultEnum regist_result = REGIST_Result_None;
  116. // 设置注册状态
  117. static void REGIST_SetStatus(enum REGIST_StatusEnum status)
  118. {
  119. regist_status = status;
  120. }
  121. // 获取注册状态
  122. enum REGIST_StatusEnum REGIST_GetStatus(void)
  123. {
  124. return regist_status;
  125. }
  126. // 设置注册结果
  127. static void REGIST_SetResult(enum REGIST_ResultEnum result)
  128. {
  129. regist_result = result;
  130. }
  131. // 获取注册结果
  132. enum REGIST_ResultEnum REGIST_GetResult(void)
  133. {
  134. return regist_result;
  135. }
  136. // 重置注册
  137. enum EXECUTE_ResultEnum REGIST_Reset(void)
  138. {
  139. if(REGIST_GetStatus() == REGIST_Status_Being)
  140. {
  141. Log_Printf_Debug("注册流程重置错误!\r\n");
  142. return EXECUTE_Result_Failure;
  143. }
  144. REGIST_SetStatus(REGIST_Status_None);
  145. REGIST_SetResult(REGIST_Result_None);
  146. return EXECUTE_Result_Success;
  147. }
  148. // 开始注册
  149. enum EXECUTE_ResultEnum REGIST_Start(void)
  150. {
  151. REGIST_Reset();
  152. if(REGIST_GetStatus() != REGIST_Status_None)
  153. {
  154. Log_Printf_Debug("注册流程启动错误!\r\n");
  155. return EXECUTE_Result_Failure;
  156. }
  157. goto_start();
  158. REGIST_SetStatus(REGIST_Status_Being);
  159. return EXECUTE_Result_Success;
  160. }
  161. // 注册流程处理,放到大循环里
  162. void REGIST_ProcessHandle(void)
  163. {
  164. if(REGIST_GetStatus() == REGIST_Status_Being)
  165. {
  166. REGIST_Process();
  167. }
  168. }
  169. // 注册流程
  170. static enum Result result = Result_None;
  171. static uint8_t cfun_mode, mode, stat = 0;
  172. static uint16_t lac, err = 0;
  173. static uint32_t ci = 0;
  174. static void REGIST_Process()
  175. {
  176. switch(pctrl.current_step)
  177. {
  178. case STEP_START: // 开始
  179. PCTRL_GotoStep(&pctrl, STEP_EXIT_SLEEP, "退出休眠");
  180. break;
  181. case STEP_EXIT_SLEEP:
  182. result = bc260y.exit_sleep_2();//退出休眠
  183. if(result == Result_Success)
  184. {
  185. PCTRL_GotoStep(&pctrl, STEP_SET_QISDE_1, "开启回显");
  186. }
  187. else if(result == Result_Failed)
  188. {
  189. goto_failure("退出休眠失败");
  190. }
  191. break;
  192. case STEP_SET_QISDE_1:
  193. result = bc260y.set_qisde(1);//开启回显
  194. if(result == Result_Success)
  195. {
  196. PCTRL_GotoStep(&pctrl, STEP_SET_SLEEP, "设置休眠模式");
  197. }
  198. else if(result == Result_Failed)
  199. {
  200. goto_failure("开启回显失败");
  201. }
  202. break;
  203. case STEP_SET_SLEEP:
  204. result=bc260y.set_sleep(2);//设置休眠模式
  205. if(result == Result_Success)
  206. {
  207. PCTRL_GotoStep(&pctrl, STEP_DATAFORMAT, "设置数据格式");
  208. }
  209. else if(result == Result_Failed)
  210. {
  211. goto_failure("设置休眠模式失败");
  212. }
  213. break;
  214. case STEP_DATAFORMAT:
  215. result = bc260y.dataformat(1); // 设置数据格式, 十六进制
  216. if(result == Result_Success)
  217. {
  218. PCTRL_GotoStep(&pctrl, STEP_QUERY_CFUN, "查询功能模式");
  219. }
  220. else if(result == Result_Failed)
  221. {
  222. goto_failure("设置数据格式失败");
  223. }
  224. break;
  225. case STEP_QUERY_CFUN:
  226. result = bc260y.query_cfun(&cfun_mode); // 查询功能模式
  227. if(result == Result_Success)
  228. {
  229. if(cfun_mode == 1)
  230. {
  231. PCTRL_GotoStep(&pctrl, STEP_SET_CFUN_0, "设置最小功能模式");
  232. }
  233. else
  234. {
  235. PCTRL_GotoStep(&pctrl, STEP_SET_CFUN_1, "设置全功能模式");
  236. }
  237. }
  238. else if(result == Result_Failed)
  239. {
  240. goto_failure("查询功能模式失败");
  241. }
  242. break;
  243. case STEP_SET_CFUN_0:
  244. result=bc260y.set_cfun(0);//设置最小功能模式
  245. if(result == Result_Success)
  246. {
  247. if(REGIST_GetResult() != REGIST_Result_None)
  248. {
  249. goto_finish();
  250. }
  251. else
  252. {
  253. PCTRL_GotoStep(&pctrl, STEP_SET_CFUN_1, "设置全功能模式");
  254. }
  255. }
  256. else if(result == Result_Failed)
  257. {
  258. goto_failure("设置最小功能模式失败");
  259. goto_finish();
  260. }
  261. break;
  262. case STEP_SET_CFUN_1:
  263. result=bc260y.set_cfun(1);//设置全功能模式
  264. if(result == Result_Success)
  265. {
  266. PCTRL_GotoStep(&pctrl, STEP_QUERY_DNS, "查询DNS");
  267. memset(pridnsaddr, 0, sizeof(pridnsaddr));
  268. }
  269. else if(result == Result_Failed)
  270. {
  271. goto_failure("设置全功能模式失败");
  272. }
  273. break;
  274. case STEP_QUERY_DNS:
  275. result = bc260y.query_dns(pridnsaddr); // 查询DNS
  276. if(result == Result_Success)
  277. {
  278. Log_Printf_Debug("DNS: %s, %d\r\n", pridnsaddr, strlen(pridnsaddr));
  279. if(strlen(pridnsaddr) <= 1)
  280. {
  281. PCTRL_GotoStep(&pctrl, STEP_SET_DNS, "设置DNS");
  282. }
  283. else
  284. {
  285. PCTRL_GotoStep(&pctrl, STEP_SET_CEREG_2, "设置网络状态");
  286. }
  287. }
  288. else if(result == Result_Failed) goto_failure("查询DNS失败");
  289. break;
  290. case STEP_SET_DNS:
  291. result = bc260y.set_dns("114.114.114.114", "8.8.8.8"); // 设置DNS
  292. if(result == Result_Success)
  293. {
  294. PCTRL_GotoStep(&pctrl, STEP_SET_CFUN_0, "设置最小功能模式");
  295. }
  296. else if(result == Result_Failed)
  297. {
  298. goto_failure("设置DNS失败");
  299. }
  300. break;
  301. case STEP_SET_CEREG_2:
  302. result=bc260y.set_cereg(2); // 设置网络状态
  303. if(result == Result_Success)
  304. {
  305. PCTRL_GotoStep(&pctrl, STEP_QUERY_CEREG, "查询网络状态");
  306. }
  307. else if(result == Result_Failed)
  308. {
  309. goto_failure("设置网络状态失败");
  310. }
  311. break;
  312. case STEP_QUERY_CEREG:
  313. result=bc260y.query_cereg(&mode, &stat, &lac, &ci);// 查询网络状态
  314. if(result == Result_Success)
  315. {
  316. PCTRL_GotoStep(&pctrl, STEP_CLOSE, "断开tcp连接");
  317. }
  318. else if(result == Result_Failed)
  319. {
  320. goto_failure("查询网络状态失败");
  321. }
  322. break;
  323. case STEP_CLOSE:
  324. result=bc260y.close_socket(connectID);//防止以前链接没断开,断开tcp链接
  325. if(result == Result_Success)
  326. {
  327. if(REGIST_GetResult() != REGIST_Result_None)
  328. {
  329. PCTRL_GotoStep(&pctrl, STEP_SET_CFUN_0, "设置最小功能模式");
  330. }
  331. else
  332. {
  333. PCTRL_GotoStep(&pctrl, STEP_OPEN, "连接tcp");
  334. }
  335. }
  336. else if(result == Result_Failed)
  337. {
  338. goto_failure("断开tcp连接失败");
  339. }
  340. break;
  341. case STEP_OPEN:
  342. result=bc260y.open_socket(connectID, "TCP", REGIST_SERVER, REGIST_PORT, 1, &err); // 连接tcp
  343. if(result == Result_Success)
  344. {
  345. PCTRL_GotoStep(&pctrl, STEP_SEND, "发送数据");
  346. // 打包注册请求
  347. memset(regist_data, 0, sizeof(regist_data));
  348. TUORENIOT_PackRegistRequest(regist_data, &regist_data_length, &regist_request);
  349. TUORENIOT_PrintfRegistRequestStruct(&regist_request);
  350. }
  351. else if(result == Result_Failed)
  352. {
  353. goto_failure("连接tcp失败");
  354. }
  355. break;
  356. case STEP_SEND: // 发送数据
  357. result =bc260y.send_rai(connectID, regist_data, regist_data_length, 0);//数据发送
  358. if(result == Result_Success)
  359. {
  360. PCTRL_GotoStep(&pctrl, STEP_RECV, "等待结果");
  361. memset(regist_data, 0, sizeof(regist_data));
  362. }
  363. else if(result == Result_Failed)
  364. {
  365. goto_failure("发送数据失败");
  366. }
  367. break;
  368. case STEP_RECV: // 等待结果
  369. result =bc260y.recv_with_time(connectID, regist_data, &regist_data_length, 10000);
  370. if(result == Result_Success)
  371. {
  372. uint8_t res = TUORENIOT_AnalysisRegistData(regist_data, regist_data_length);
  373. if(res == 1) // 成功
  374. {
  375. goto_success();
  376. }
  377. else // 失败
  378. {
  379. goto_failure("解析失败");
  380. }
  381. PCTRL_GotoStep(&pctrl, STEP_CLOSE, "断开tcp连接");
  382. }
  383. else if(result == Result_Failed)
  384. {
  385. goto_failure("等待结果失败");
  386. }
  387. break;
  388. case STEP_WAIT: // 等待
  389. PCTRL_Wait(&pctrl);
  390. break;
  391. case STEP_SUCCESS: // 成功
  392. goto_finish();
  393. break;
  394. case STEP_FAILURE: // 失败
  395. PCTRL_GotoStep(&pctrl, STEP_SET_CFUN_0, "失败,设置最小功能模式");
  396. break;
  397. case STEP_FINISH: // 结束
  398. break;
  399. default: // 默认
  400. goto_failure("步骤不存在");
  401. break;
  402. }
  403. }
  404. #endif