BC260Y_UDP_Client5.c 11 KB

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