Bc260y_Regist.c 10 KB

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