BC260_UDP_Client5.c 10 KB

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