BC260Y_UDP_Client5.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. #include "CONFIG.h"
  2. #if BC260Y
  3. /**
  4. ******************************************************************************
  5. * 版 本 :V1.0.0
  6. * 作 者 :liuyanbin,helomgxiang
  7. * 版权所有,盗版必究。
  8. * Copyright(C) All rights reserved
  9. ******************************************************************************
  10. * 历史记录
  11. * 序号 版本 变更内容 作者 日期
  12. * 1 V0.0.1 实现数据发送功能 何龙翔 2023/12/10
  13. * 2 V0.0.2 实现NB数据发送功能 刘艳斌 2024/1/10
  14. * 3 V0.0.3 整合4g和nb 刘艳斌 2024/1/24
  15. *
  16. ******************************************************************************
  17. */
  18. #include "stm32f10x.h"
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include "UDP_Client.h"
  24. #include "CoAP_Util.h"
  25. #include "mbedtls_util.h"
  26. #include "cJSON.h"
  27. #include "bc260y.h"
  28. #include "Pump_Dicts_Util.h"
  29. #include "INflash.h"
  30. #include "PumpBusiness.h"
  31. #include "aliyuniot.h"
  32. #include "Log_Module.h"
  33. // 流程
  34. enum Step{
  35. STEP_NONE, // 空闲状态,无操作
  36. STEP_START, // 开始
  37. STEP_EXIT_SLEEP, // 退出睡眠
  38. STEP_ENTER_SLEEP, // 进入睡眠
  39. STEP_WAIT, // 等待
  40. STEP_STATE, // 查询状态
  41. STEP_OPEN, // 打开
  42. STEP_OPEN_WAIT_RESULT, // 等待打开结果
  43. STEP_JUDGE_AUTH_OR_DATA, // 判断认证还是发送
  44. STEP_JOIN_AUTH_MESSAGE, // 拼接认证报文
  45. STEP_JOIN_DATA_MESSAGE, // 拼接数据报文
  46. STEP_QUERY_SLEEP, // 查询休眠
  47. STEP_SET_SLEEP, // 开启休眠
  48. STEP_QUERY_QENG_SERVINGCELL, // 查询信号质量
  49. STEP_SET_QISDE_0, // 关闭发送回显
  50. STEP_SEND, // 发送
  51. STEP_RECV, // 等待发送结果
  52. STEP_SET_CFUN_0, // 设置最小功能模式
  53. STEP_WAIT_SET_CFUN_0, // 等待设置最小功能模式
  54. STEP_SET_CFUN_1, // 设置全功能模式
  55. STEP_WAIT_SET_CFUN_1, // 等待设置全功能模式结果
  56. STEP_SET_CGREG_2, // 设置ps域允许上报网络注册和位置信息
  57. STEP_QUERY_CGREG, // 查询网络注册状态
  58. STEP_DATAFORMAT,//配置发送格式
  59. STEP_CLOSE, // 关闭
  60. STEP_SUCCESS, // 成功
  61. STEP_FAILURE, // 失败
  62. STEP_FINISH, // 流程结束
  63. };
  64. // 当前
  65. static enum Step step = STEP_NONE;
  66. // 下一步
  67. static enum Step next_step = STEP_NONE;
  68. // 客户端5
  69. struct COMM_Client_Struct UDP_Client5 = {
  70. .status = Client_Status_None,
  71. };
  72. // 注册后的参数
  73. extern Coefficient_Data flashdata;
  74. // socket ID
  75. static uint8_t connectID = 1;
  76. // coap报文
  77. static uint8_t coap_message[512];
  78. static uint16_t coap_message_length = 0;
  79. // 泵参数
  80. extern struct Pump_Params pump_params;
  81. // 待发送数据的地址和长度
  82. static uint8_t databuff[128];
  83. static uint16_t data_length;
  84. static uint8_t test_flag=0;
  85. extern struct AT_Struct AT;
  86. // 计时相关的变量
  87. uint8_t Client5_time_flag = 0;
  88. uint32_t Client5_timer_ms = 0;
  89. uint32_t Client5_wait_time = 10;
  90. // 信号值
  91. static struct Signal
  92. {
  93. int RSRP;
  94. int RSRQ;
  95. int RSSI;
  96. int SINR;
  97. } signal = {
  98. .RSRP = 99,
  99. .RSRQ = 99,
  100. .RSSI = 99,
  101. .SINR = 99,
  102. };
  103. void Query_Signal(int * RSRP, int * RSRQ, int * RSSI, int * SINR)
  104. {
  105. *RSRP = signal.RSRP;
  106. *RSRQ = signal.RSRQ;
  107. *RSSI = signal.RSSI;
  108. *SINR = signal.SINR;
  109. }
  110. // 初始化
  111. void UDP_Client5_Init(void)
  112. {
  113. aliyuniot_set_device_params(flashdata.productKey,flashdata.deviceName,flashdata.deviceSecret);
  114. // 设置host
  115. }
  116. // 声明函数。步骤跳转
  117. void goto_step(enum Step ns);
  118. // 发送流程
  119. void UDP_Client5_Handle(void);
  120. // 发送数据
  121. void UDP_Client5_Send(uint8_t test_switch)
  122. {
  123. test_flag=test_switch;
  124. if(step == STEP_NONE)
  125. {
  126. // 初始化
  127. UDP_Client5_Init();
  128. // 正在发送
  129. UDP_Client5.status = Client_Status_Sending;
  130. // 流程开始
  131. goto_step(STEP_START);
  132. }
  133. }
  134. // 获取状态
  135. enum Client_Status UDP_Client5_Status(void)
  136. {
  137. if(UDP_Client5.status == Client_Status_None)
  138. {
  139. return UDP_Client5.status;
  140. }
  141. else if(step == STEP_FINISH)
  142. {
  143. return UDP_Client5.status;
  144. }
  145. else
  146. {
  147. return Client_Status_Sending;
  148. }
  149. }
  150. // 清除
  151. void UDP_Client5_Clear(void)
  152. {
  153. // 流程置空
  154. goto_step(STEP_NONE);
  155. // 空闲
  156. UDP_Client5.status = Client_Status_None;
  157. }
  158. // 直接跳转到下一步
  159. static void goto_step(enum Step ns)
  160. {
  161. // 重置bc260y状态
  162. bc260y.reset();
  163. step = ns;
  164. }
  165. // 先等待再跳转到下一步
  166. static void goto_step_wait(enum Step ns, uint32_t t)
  167. {
  168. goto_step(STEP_WAIT); // 等待
  169. Client5_wait_time = t;
  170. next_step = ns; // 等待之后跳转
  171. }
  172. // 只等待,等待之后返回原来的步骤
  173. static void goto_wait(uint32_t t)
  174. {
  175. goto_step_wait(step, t);
  176. }
  177. // 失败
  178. static void goto_failure(char * info)
  179. {
  180. Log_Printf_Debug("STEP: 数据发送失败,%s\r\n", info);
  181. UDP_Client5.status = Client_Status_Failure;
  182. strcpy(UDP_Client5.info, info);
  183. goto_step(STEP_FAILURE);
  184. }
  185. // 成功
  186. static void goto_success(char * info)
  187. {
  188. Log_Printf_Debug("STEP: 数据发送成功,%s\r\n", info);
  189. UDP_Client5.status = Client_Status_Success;
  190. strcpy(UDP_Client5.info, info);
  191. goto_step(STEP_SUCCESS);
  192. }
  193. // 等待
  194. static void wait(void)
  195. {
  196. if(Client5_time_flag == 0)
  197. {
  198. Client5_timer_ms = 0;
  199. Client5_time_flag = 1;
  200. }
  201. else if(Client5_timer_ms > Client5_wait_time)
  202. {
  203. goto_step(next_step); // 进入下一步
  204. Client5_time_flag = 0;
  205. }
  206. }
  207. //// 发送数据的逻辑
  208. static enum Result result = Result_None;
  209. static uint8_t cgreg_n;
  210. static uint8_t cgreg_stat;
  211. static uint16_t cgreg_lac;
  212. static uint32_t cgreg_ci;
  213. static uint8_t query_cgreg_times = 0; // 查询网络状态的次数
  214. static uint8_t auth_times = 0; // 认证次数
  215. static uint16_t socket_err = 0;
  216. static uint8_t auth_or_data = 0;
  217. void UDP_Client5_Handle(void)
  218. {
  219. // 流程
  220. switch(step)
  221. {
  222. case STEP_NONE: // 空闲
  223. break;
  224. case STEP_START: // 开始
  225. query_cgreg_times = 0; // 查询网络状态的次数
  226. auth_times = 0; // 认证次数
  227. goto_step(STEP_EXIT_SLEEP);
  228. break;
  229. case STEP_EXIT_SLEEP: // 退出休眠
  230. result = bc260y.exit_sleep();
  231. if(result == Result_Success)
  232. {
  233. goto_step_wait(STEP_SET_CFUN_1, 5);
  234. }
  235. else if(result == Result_Failed)
  236. {
  237. goto_step_wait(STEP_SET_CFUN_1, 100);
  238. }
  239. break;
  240. case STEP_SET_CFUN_1: // 设置全功能模式
  241. result = bc260y.set_cfun(1);
  242. if(result == Result_Success)
  243. {
  244. goto_step(STEP_SET_CGREG_2);
  245. }
  246. else if(result == Result_Failed)
  247. {
  248. goto_failure("设置全功能模式失败");
  249. }
  250. break;
  251. case STEP_SET_CGREG_2: // 设置ps域
  252. result = bc260y.set_cgreg(2);
  253. if(result == Result_Success)
  254. {
  255. goto_step(STEP_SET_QISDE_0);
  256. }
  257. else if(result == Result_Failed)
  258. {
  259. goto_failure("设置ps域失败");
  260. }
  261. break;
  262. case STEP_SET_QISDE_0: // 关闭回显
  263. result = bc260y.set_qisde(0);
  264. if(result == Result_Success)
  265. {
  266. goto_step(STEP_QUERY_CGREG);
  267. }
  268. else if(result == Result_Failed)
  269. {
  270. goto_failure("关闭回显失败");
  271. }
  272. break;
  273. case STEP_QUERY_CGREG: // 查询ps域
  274. result = bc260y.query_cgreg(&cgreg_n, &cgreg_stat, &cgreg_lac, &cgreg_ci);
  275. if(result == Result_Success)
  276. {
  277. if(cgreg_stat == 1)
  278. {
  279. // 参数赋值
  280. pump_params.lac = cgreg_lac;
  281. pump_params.ci = cgreg_ci;
  282. // 编码
  283. // 下一步
  284. if(test_flag == 0)
  285. {
  286. goto_step(STEP_CLOSE);
  287. }
  288. else
  289. {
  290. goto_step(STEP_QUERY_QENG_SERVINGCELL);
  291. }
  292. }
  293. else if(query_cgreg_times > 10) // 最多查询20次
  294. {
  295. goto_failure("查询ps域次数过多");
  296. }
  297. else
  298. {
  299. goto_wait(400);
  300. query_cgreg_times++;
  301. }
  302. }
  303. else if(result == Result_Failed)
  304. {
  305. goto_failure("查询ps域失败");
  306. }
  307. break;
  308. case STEP_QUERY_QENG_SERVINGCELL: // 查询信号质量
  309. result = bc260y.qeng_servingcell(&signal.RSRP, &signal.RSRQ, &signal.RSSI, &signal.SINR);
  310. if(result == Result_Success)
  311. {
  312. goto_step(STEP_CLOSE);
  313. }
  314. else if(result == Result_Failed)
  315. {
  316. goto_failure("查询信号质量失败");
  317. }
  318. break;
  319. case STEP_CLOSE: // 关闭连接
  320. result = bc260y.close_socket(connectID);
  321. if(result == Result_Success)
  322. {
  323. goto_step(STEP_DATAFORMAT);
  324. }
  325. else if(result == Result_Failed)
  326. {
  327. goto_failure("关闭连接失败");
  328. }
  329. break;
  330. case STEP_DATAFORMAT:
  331. result = bc260y.dataformat(1);
  332. if(result == Result_Success)
  333. {
  334. goto_step(STEP_OPEN);
  335. }
  336. else if(result == Result_Failed)
  337. {
  338. goto_failure("配置发送格式失败");
  339. }
  340. break;
  341. case STEP_OPEN: // 打开客户端
  342. result = bc260y.open_socket(connectID, "UDP",aliyuniot_get_host(), aliyuniot_get_port(), 1, &socket_err);
  343. if(result == Result_Success)
  344. {
  345. if(socket_err == 0)
  346. {
  347. goto_step(STEP_JUDGE_AUTH_OR_DATA);
  348. }
  349. else
  350. {
  351. goto_failure("客户端打开错误");
  352. }
  353. }
  354. else if(result == Result_Failed)
  355. {
  356. goto_failure("打开客户端失败");
  357. }
  358. break;
  359. case STEP_JUDGE_AUTH_OR_DATA: // 判断认证还是发送
  360. auth_or_data = aliyuniot_is_authentication();
  361. if(auth_or_data == 1) // 已认证
  362. {
  363. goto_step(STEP_JOIN_DATA_MESSAGE);
  364. }
  365. else
  366. {
  367. goto_step(STEP_JOIN_AUTH_MESSAGE);
  368. }
  369. break;
  370. case STEP_JOIN_AUTH_MESSAGE: // 拼接认证报文
  371. if(auth_times > 0) // 最多重新认证一次
  372. {
  373. goto_failure("认证次数过多");
  374. }
  375. else
  376. {
  377. aliyuniot_get_auth_message(coap_message, &coap_message_length);
  378. goto_step(STEP_SEND);
  379. auth_times++;
  380. }
  381. break;
  382. case STEP_JOIN_DATA_MESSAGE: // 拼接数据报文
  383. pump_params.lac = cgreg_lac;
  384. pump_params.ci = cgreg_ci;
  385. Pump_Params_Refresh();
  386. // 编码
  387. business_protocol_encode(pump_params, databuff, &data_length);
  388. aliyuniot_get_data_message(databuff, data_length, coap_message, &coap_message_length);
  389. goto_step(STEP_SEND);
  390. break;
  391. case STEP_SEND: // 发送send
  392. result = bc260y.send(connectID, coap_message, coap_message_length);
  393. if(result == Result_Success)
  394. {
  395. goto_step(STEP_RECV);
  396. }
  397. else if(result == Result_Failed)
  398. {
  399. goto_failure("发送send失败");
  400. }
  401. break;
  402. case STEP_RECV: // 等待结果
  403. result = bc260y.recv_with_time(connectID, coap_message, &coap_message_length, 10000);
  404. if(result == Result_Success)
  405. {
  406. uint8_t res = aliyuniot_recv_data_handle(coap_message, coap_message_length);
  407. if(res == 0) // 发送失败
  408. {
  409. goto_step(STEP_JUDGE_AUTH_OR_DATA); // 重新认证
  410. }
  411. else if(res == 1) // 认证成功
  412. {
  413. goto_step(STEP_JOIN_DATA_MESSAGE);
  414. }
  415. else if(res == 2) // 发送成功
  416. {
  417. goto_success("发送成功");
  418. }
  419. }
  420. else if(result == Result_Failed)
  421. {
  422. goto_failure("等待结果失败");
  423. }
  424. break;
  425. case STEP_WAIT: // 等待
  426. wait();
  427. break;
  428. case STEP_SUCCESS: // 成功
  429. goto_step(STEP_SET_SLEEP);
  430. break;
  431. case STEP_FAILURE: // 失败
  432. goto_step(STEP_SET_SLEEP);
  433. break;
  434. case STEP_SET_SLEEP: // 设置休眠模式
  435. result = bc260y.set_sleep(1);
  436. if(result != Result_None)
  437. {
  438. goto_step(STEP_ENTER_SLEEP);
  439. }
  440. break;
  441. case STEP_ENTER_SLEEP: // 进入睡眠
  442. //bc260y.enter_sleep();
  443. goto_step(STEP_FINISH);
  444. break;
  445. case STEP_FINISH: // 结束流程
  446. break;
  447. default:
  448. break;
  449. }
  450. }
  451. #endif