EC800M_UDP_Client5.c 9.3 KB

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