BC260Y_UDP_Client5.c 12 KB

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