BC260Y_UDP_Client5.c 12 KB

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