EC800M_UDP_Client5.c 10.0 KB

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