EC800M_UDP_Client5.c 9.6 KB

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