BC260_UDP_Client5.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /**
  2. ******************************************************************************
  3. * 版 本 :V1.0.0
  4. * 作 者 :liuyanbin,helomgxiang
  5. * 版权所有,盗版必究。
  6. * Copyright(C) All rights reserved
  7. ******************************************************************************
  8. * 历史记录
  9. * 序号 版本 变更内容 作者 日期
  10. * 1 V0.0.1 实现数据发送功能 何龙翔 2023/12/10
  11. * 2 V0.0.2 实现NB数据发送功能 刘艳斌 2024/1/10
  12. * 3 V0.0.3 整合4g和nb 刘艳斌 2024/1/24
  13. *
  14. ******************************************************************************
  15. */
  16. #include "stm32f10x.h"
  17. #include <stdio.h>
  18. #include <stdarg.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include "BC260_UDP_Client5.h"
  22. #include "CoAP.h"
  23. #include "mbedtls_util.h"
  24. #include "cJSON.h"
  25. #include "sys.h"
  26. #include "bc260.h"
  27. #include "pump_dicts.h"
  28. #include "INflash.h"
  29. #include "PumpBusiness.h"
  30. #if NBFLAG
  31. // 流程
  32. enum Step{
  33. STEP_NONE, // 空闲状态,无操作
  34. STEP_START, // 开始
  35. STEP_EXIT_SLEEP, // 退出睡眠
  36. STEP_ENTER_SLEEP, // 进入睡眠
  37. STEP_WAIT, // 等待
  38. STEP_STATE, // 查询状态
  39. STEP_OPEN, // 打开
  40. STEP_OPEN_WAIT_RESULT, // 等待打开结果
  41. STEP_JUDGE_AUTH_OR_DATA, // 判断认证还是发送
  42. STEP_JOIN_AUTH_MESSAGE, // 拼接认证报文
  43. STEP_JOIN_DATA_MESSAGE, // 拼接数据报文
  44. STEP_QUERY_SLEEP, // 查询休眠
  45. STEP_SET_SLEEP, // 开启休眠
  46. STEP_QUERY_QENG_SERVINGCELL, // 查询信号质量
  47. STEP_SET_QISDE_0, // 关闭发送回显
  48. STEP_SEND, // 发送
  49. STEP_RECV, // 等待发送结果
  50. STEP_SET_CFUN_0, // 设置最小功能模式
  51. STEP_WAIT_SET_CFUN_0, // 等待设置最小功能模式
  52. STEP_SET_CFUN_1, // 设置全功能模式
  53. STEP_WAIT_SET_CFUN_1, // 等待设置全功能模式结果
  54. STEP_SET_CGREG_2, // 设置ps域允许上报网络注册和位置信息
  55. STEP_QUERY_CGREG, // 查询网络注册状态
  56. STEP_DATAFORMAT,//配置发送格式
  57. STEP_CLOSE, // 关闭
  58. STEP_SUCCESS, // 成功
  59. STEP_FAILURE, // 失败
  60. STEP_FINISH, // 流程结束
  61. };
  62. // 当前
  63. static enum Step step = STEP_NONE;
  64. // 下一步
  65. static enum Step next_step = STEP_NONE;
  66. // 客户端5
  67. struct BC260Y_Client_Struct UDP_Client5 = {
  68. .status = Client_Status_None,
  69. };
  70. // 注册后的参数
  71. extern Coefficient_Data flashdata;
  72. // socket ID
  73. static uint8_t connectID = 1;
  74. /** 阿里云连接参数 **/
  75. static char ProductKey[20] = "a1t3qlUNDcP";
  76. static char DeviceName[20] = "LTEcat1ceshi001";
  77. static char DeviceSecret[40] = "4929a78860f663ec72e2c313a4eee7ac";
  78. static char ClientID[60];
  79. static char Seq[10] = "666";
  80. static uint16_t Port = 5682;
  81. static char Host[60];
  82. static uint32_t SeqOffset;
  83. static char Random[20];
  84. static char Token[50];
  85. // payload缓存
  86. //static uint8_t payload[256];
  87. //static uint16_t payload_length;
  88. // coap报文
  89. static uint8_t coap_message[512];
  90. static uint16_t coap_message_length = 0;
  91. // aes cbc
  92. static uint8_t aes_key[16] = {0};
  93. static char iv[] = "543yhjy97ae7fyfg";
  94. // 泵参数
  95. extern struct Pump_Params pump_params;
  96. // 待发送数据的地址和长度
  97. static uint8_t data[128];
  98. static uint16_t data_length;
  99. static uint8_t test_flag=0;
  100. // 计时相关的变量
  101. uint8_t Client5_time_flag = 0;
  102. uint32_t Client5_timer_ms = 0;
  103. uint32_t Client5_wait_time = 10;
  104. // 信号值
  105. static struct Signal
  106. {
  107. int RSRP;
  108. int RSRQ;
  109. int RSSI;
  110. int SINR;
  111. } signal = {
  112. .RSRP = 99,
  113. .RSRQ = 99,
  114. .RSSI = 99,
  115. .SINR = 99,
  116. };
  117. void Query_Signal(int * RSRP, int * RSRQ, int * RSSI, int * SINR)
  118. {
  119. *RSRP = signal.RSRP;
  120. *RSRQ = signal.RSRQ;
  121. *RSSI = signal.RSSI;
  122. *SINR = signal.SINR;
  123. }
  124. // 初始化
  125. void UDP_Client5_Init(void)
  126. {
  127. // 赋值三元组
  128. strcpy(ProductKey, flashdata.productKey);
  129. strcpy(DeviceName, flashdata.deviceName);
  130. strcpy(DeviceSecret, flashdata.deviceSecret);
  131. // 设置host
  132. memset(Host, '\0', sizeof(Host));
  133. strcat(Host, ProductKey);
  134. strcat(Host, ".coap.cn-shanghai.link.aliyuncs.com");
  135. // 设置clientid
  136. memset(ClientID, '\0', sizeof(ClientID));
  137. strcat(ClientID, ProductKey);
  138. strcat(ClientID, "&");
  139. strcat(ClientID, DeviceName);
  140. }
  141. // 声明函数。步骤跳转
  142. void goto_step(enum Step ns);
  143. // 发送流程
  144. void UDP_Client5_Handle(void);
  145. // 发送数据
  146. void UDP_Client5_Send(struct Pump_Params params,uint8_t test_switch)
  147. {
  148. test_flag=test_switch;
  149. if(step == STEP_NONE)
  150. {
  151. // 待发送数据的地址和长度
  152. // pump_params = params;
  153. // 初始化
  154. UDP_Client5_Init();
  155. // 正在发送
  156. UDP_Client5.status = Client_Status_Sending;
  157. // 流程开始
  158. goto_step(STEP_START);
  159. }
  160. }
  161. // 获取状态
  162. enum Client_Status UDP_Client5_Status(void)
  163. {
  164. if(UDP_Client5.status == Client_Status_None)
  165. {
  166. return UDP_Client5.status;
  167. }
  168. else if(step == STEP_FINISH)
  169. {
  170. return UDP_Client5.status;
  171. }
  172. else
  173. {
  174. return Client_Status_Sending;
  175. }
  176. }
  177. // 清除
  178. void UDP_Client5_Clear(void)
  179. {
  180. // 流程置空
  181. goto_step(STEP_NONE);
  182. // 空闲
  183. UDP_Client5.status = Client_Status_None;
  184. }
  185. // 直接跳转到下一步
  186. static void goto_step(enum Step ns)
  187. {
  188. // 重置bc260y状态
  189. bc260y.reset();
  190. step = ns;
  191. }
  192. // 先等待再跳转到下一步
  193. static void goto_step_wait(enum Step ns, uint32_t t)
  194. {
  195. goto_step(STEP_WAIT); // 等待
  196. Client5_wait_time = t;
  197. next_step = ns; // 等待之后跳转
  198. }
  199. // 只等待,等待之后返回原来的步骤
  200. static void goto_wait(uint32_t t)
  201. {
  202. goto_step_wait(step, t);
  203. }
  204. // 失败
  205. static void goto_failure(char * info)
  206. {
  207. Log_Printf_Debug("STEP: 数据发送失败,%s\r\n", info);
  208. UDP_Client5.status = Client_Status_Failure;
  209. strcpy(UDP_Client5.info, info);
  210. goto_step(STEP_FAILURE);
  211. }
  212. // 成功
  213. static void goto_success(char * info)
  214. {
  215. Log_Printf_Debug("STEP: 数据发送成功,%s\r\n", info);
  216. UDP_Client5.status = Client_Status_Success;
  217. strcpy(UDP_Client5.info, info);
  218. goto_step(STEP_SUCCESS);
  219. }
  220. // 等待
  221. static void wait(void)
  222. {
  223. if(Client5_time_flag == 0)
  224. {
  225. Client5_timer_ms = 0;
  226. Client5_time_flag = 1;
  227. }
  228. else if(Client5_timer_ms > Client5_wait_time)
  229. {
  230. goto_step(next_step); // 进入下一步
  231. Client5_time_flag = 0;
  232. }
  233. }
  234. // 判断认证还是发送
  235. static uint8_t judge_auth_or_data(void)
  236. {
  237. if(strlen(Token) > 0) // 已认证
  238. {
  239. return 1;
  240. }
  241. else
  242. {
  243. return 0;
  244. }
  245. }
  246. // 拼接认证报文
  247. static void join_auth_message(uint8_t * coap_message, uint16_t * coap_message_length)
  248. {
  249. SeqOffset=65535;
  250. Log_Printf_Debug("STEP: 拼接认证报文,seqOffset=%d\r\n", SeqOffset);
  251. // 先清除
  252. // memset(coap_message, 0, sizeof(coap_message));
  253. // coap_message_length = 0;
  254. // CoAP协议
  255. CoAP_Init();
  256. CoAP_Set_T(0);
  257. CoAP_Set_Code(2);
  258. CoAP_Set_MessageID(SeqOffset);
  259. // option
  260. // host
  261. CoAP_Set_Option_Str(3, Host);
  262. // port
  263. CoAP_Set_Option_Short(7, Port);
  264. // path
  265. CoAP_Set_Option_Str(11, "auth");
  266. // content-format
  267. CoAP_Set_Option_Short(12, 50);
  268. // accept
  269. CoAP_Set_Option_Short(17, 50);
  270. // 待签名数据
  271. // 签名相关
  272. static char StrForSignature[100];
  273. static char sign[50];
  274. memset(StrForSignature, '\0', sizeof(StrForSignature));
  275. sprintf((char * )StrForSignature, "clientId%sdeviceName%sproductKey%sseq%s", ClientID, DeviceName, ProductKey, Seq);
  276. // 签名
  277. memset(sign, '\0', sizeof(sign));
  278. utils_hmac_sha1_str(StrForSignature, strlen((char * )StrForSignature), sign, DeviceSecret, strlen(DeviceSecret));
  279. // payload
  280. // memset(payload, '\0', sizeof(payload));
  281. char payload[256] = {0};
  282. sprintf((char * )payload, "{\"clientId\":\"%s\",\"signmethod\":\"hmacsha1\",\"sign\":\"%s\",\"productKey\":\"%s\",\"deviceName\":\"%s\",\"seq\":\"%s\"}", ClientID, sign, ProductKey, DeviceName, Seq);
  283. // 设置payload
  284. CoAP_Set_Payload_Str(payload);
  285. // 生成报文
  286. * coap_message_length = CoAP_Get_Length();
  287. memcpy(coap_message, CoAP_Get_Message(), * coap_message_length);
  288. }
  289. // 拼接数据报文
  290. static void join_data_message(uint8_t * data, uint8_t data_length, uint8_t * coap_message, uint16_t * coap_message_length)
  291. {
  292. SeqOffset++;
  293. Log_Printf_Debug("STEP: 拼接数据报文,seqOffset=%d\r\n", SeqOffset);
  294. // 先清除
  295. // memset(coap_message, 0, sizeof(coap_message));
  296. // coap_message_length = 0;
  297. char out[80]={0};
  298. char sha256_source[100] = {0};
  299. snprintf(sha256_source, sizeof(sha256_source), "%s,%s" ,DeviceSecret ,Random);
  300. Log_Printf_Debug("sha256_source: %s\r\n", sha256_source);
  301. utils_sha256(sha256_source, strlen(sha256_source), out);
  302. memset(aes_key, 0, sizeof(aes_key));
  303. memcpy(aes_key, out+8, 16); //获取key
  304. // CoAP协议
  305. CoAP_Init();
  306. CoAP_Set_T(0);
  307. CoAP_Set_Code(2);
  308. CoAP_Set_MessageID(SeqOffset);
  309. // option
  310. // host
  311. CoAP_Set_Option_Str(3, Host);
  312. // port
  313. CoAP_Set_Option_Short(7, Port);
  314. // path
  315. CoAP_Set_Option_Str(11, "topic");
  316. CoAP_Set_Option_Str(11, "sys");
  317. CoAP_Set_Option_Str(11, ProductKey);
  318. CoAP_Set_Option_Str(11, DeviceName);
  319. CoAP_Set_Option_Str(11, "thing");
  320. CoAP_Set_Option_Str(11, "model");
  321. CoAP_Set_Option_Str(11, "up_raw");
  322. // content-format
  323. CoAP_Set_Option_Short(12, 50);
  324. // accept
  325. CoAP_Set_Option_Short(17, 50);
  326. // 2088
  327. CoAP_Set_Option_Str(2088, Token);
  328. // 2089
  329. char Rand2089[10] = {'\0'};
  330. char Opt2089[20] = {'\0'};
  331. my_itoa(SeqOffset, Rand2089, 10);
  332. int Opt2089_Length = utils_aes128_cbc_enc((char * )aes_key, iv, Rand2089, Opt2089);
  333. CoAP_Set_Option_Str(2089, Opt2089);
  334. // payload
  335. // memset(payload, '\0', sizeof(payload));
  336. uint8_t payload[256] = {0};
  337. //
  338. int payload_length = utils_aes128_cbc_enc_with_length((char * )aes_key, iv, data, data_length, payload);
  339. // payload_length = utils_aes128_cbc_enc((char * )aes_key, iv, (char * )Data_Buffer, (char * )payload);
  340. Log_Printf_Debug("payload_length: %d\r\n", payload_length);
  341. CoAP_Set_Payload(payload, payload_length);
  342. // 生成报文
  343. * coap_message_length = CoAP_Get_Length();
  344. memcpy(coap_message, CoAP_Get_Message(), * coap_message_length);
  345. }
  346. // 等待数据返回
  347. static uint8_t recv_data_handle(uint8_t * coap_message, uint16_t coap_message_length)
  348. {
  349. uint16_t payload_length = CoAP_Get_Payload_Length(coap_message, coap_message_length);
  350. uint8_t payload[256] = {0};
  351. CoAP_Get_Payload(coap_message, coap_message_length, payload);
  352. Log_Printf_Debug("payload长度: %d\r\n", payload_length);
  353. // 判断是认证返回,还是数据发送返回
  354. if(payload_length == 0) // 无返回
  355. {
  356. // 清空Token,重新认证
  357. memset(Token, '\0', sizeof(Token));
  358. return 0;
  359. }
  360. else if(payload[0] == '{' && payload[payload_length-1] == '}') // 认证返回
  361. {
  362. // 解析json
  363. cJSON * cjson = cJSON_Parse((char * )payload);
  364. Log_Printf_Debug("plaintext(%d): %s\r\n",payload_length, payload);
  365. memset(Random, '\0', sizeof(Random));
  366. memset(Token, '\0', sizeof(Token));
  367. if(cJSON_HasObjectItem(cjson, "random"))
  368. {
  369. char * random = cJSON_GetObjectItem(cjson, "random")->valuestring;
  370. strcpy(Random, random);
  371. }
  372. if(cJSON_HasObjectItem(cjson, "seqOffset"))
  373. {
  374. uint16_t seqOffset = cJSON_GetObjectItem(cjson, "seqOffset")->valueint;
  375. SeqOffset = seqOffset;
  376. }
  377. if(cJSON_HasObjectItem(cjson, "token"))
  378. {
  379. char * token = cJSON_GetObjectItem(cjson, "token")->valuestring;
  380. strcpy(Token, token);
  381. }
  382. Log_Printf_Debug("\r\n(%s, %s, %d)\r\n", Random, Token, SeqOffset);
  383. cJSON_Delete(cjson);//清除结构体
  384. return 1;
  385. }
  386. else
  387. {
  388. // 数据发送成功返回
  389. uint8_t plaintext[256] = {0}; // 明文
  390. uint16_t plaintext_length = utils_aes128_cbc_dec((char * )aes_key, iv, (char * )payload, payload_length, (char * )plaintext);
  391. Log_Printf_Debug("\r\nplaintext(%d): %s\r\n",plaintext_length, (char *)plaintext);
  392. return 2;
  393. }
  394. }
  395. // 发送数据的逻辑
  396. static enum Result result = Result_None;
  397. static uint8_t cgreg_n;
  398. static uint8_t cgreg_stat;
  399. static uint16_t cgreg_lac;
  400. static uint32_t cgreg_ci;
  401. static uint8_t query_cgreg_times = 0; // 查询网络状态的次数
  402. static uint8_t auth_times = 0; // 认证次数
  403. static uint16_t socket_err = 0;
  404. static uint8_t auth_or_data = 0;
  405. void UDP_Client5_Handle(void)
  406. {
  407. // 流程
  408. switch(step)
  409. {
  410. case STEP_NONE: // 空闲
  411. break;
  412. case STEP_START: // 开始
  413. query_cgreg_times = 0; // 查询网络状态的次数
  414. auth_times = 0; // 认证次数
  415. goto_step(STEP_EXIT_SLEEP);
  416. break;
  417. case STEP_EXIT_SLEEP: // 退出休眠
  418. result = bc260y.exit_sleep();
  419. if(result == Result_Success)
  420. {
  421. goto_step_wait(STEP_SET_CFUN_1, 5);
  422. }
  423. else if(result == Result_Failed)
  424. {
  425. goto_step_wait(STEP_SET_CFUN_1, 100);
  426. }
  427. break;
  428. case STEP_SET_CFUN_1: // 设置全功能模式
  429. result = bc260y.set_cfun(1);
  430. if(result == Result_Success)
  431. {
  432. goto_step(STEP_SET_CGREG_2);
  433. }
  434. else if(result == Result_Failed)
  435. {
  436. goto_failure("设置全功能模式失败");
  437. }
  438. break;
  439. case STEP_SET_CGREG_2: // 设置ps域
  440. result = bc260y.set_cgreg(2);
  441. if(result == Result_Success)
  442. {
  443. goto_step(STEP_SET_QISDE_0);
  444. }
  445. else if(result == Result_Failed)
  446. {
  447. goto_failure("设置ps域失败");
  448. }
  449. break;
  450. case STEP_SET_QISDE_0: // 关闭回显
  451. result = bc260y.set_qisde(0);
  452. if(result == Result_Success)
  453. {
  454. goto_step(STEP_QUERY_CGREG);
  455. }
  456. else if(result == Result_Failed)
  457. {
  458. goto_failure("关闭回显失败");
  459. }
  460. break;
  461. case STEP_QUERY_CGREG: // 查询ps域
  462. result = bc260y.query_cgreg(&cgreg_n, &cgreg_stat, &cgreg_lac, &cgreg_ci);
  463. if(result == Result_Success)
  464. {
  465. if(cgreg_stat == 1)
  466. {
  467. // 参数赋值
  468. pump_params.lac = cgreg_lac;
  469. pump_params.ci = cgreg_ci;
  470. // 编码
  471. business_protocol_encode(pump_params, data, &data_length);
  472. // 下一步
  473. if(test_flag == 0)
  474. {
  475. goto_step(STEP_CLOSE);
  476. }
  477. else
  478. {
  479. goto_step(STEP_QUERY_QENG_SERVINGCELL);
  480. }
  481. }
  482. else if(query_cgreg_times > 10) // 最多查询20次
  483. {
  484. goto_failure("查询ps域次数过多");
  485. }
  486. else
  487. {
  488. goto_wait(400);
  489. query_cgreg_times++;
  490. }
  491. }
  492. else if(result == Result_Failed)
  493. {
  494. goto_failure("查询ps域失败");
  495. }
  496. break;
  497. case STEP_QUERY_QENG_SERVINGCELL: // 查询信号质量
  498. result = bc260y.qeng_servingcell(&signal.RSRP, &signal.RSRQ, &signal.RSSI, &signal.SINR);
  499. if(result == Result_Success)
  500. {
  501. goto_step(STEP_CLOSE);
  502. }
  503. else if(result == Result_Failed)
  504. {
  505. goto_failure("查询信号质量失败");
  506. }
  507. break;
  508. case STEP_CLOSE: // 关闭连接
  509. result = bc260y.close_socket(connectID);
  510. if(result == Result_Success)
  511. {
  512. goto_step(STEP_DATAFORMAT);
  513. }
  514. else if(result == Result_Failed)
  515. {
  516. goto_failure("关闭连接失败");
  517. }
  518. break;
  519. case STEP_DATAFORMAT:
  520. result = bc260y.dataformat(1);
  521. if(result == Result_Success)
  522. {
  523. goto_step(STEP_OPEN);
  524. }
  525. else if(result == Result_Failed)
  526. {
  527. goto_failure("配置发送格式失败");
  528. }
  529. break;
  530. case STEP_OPEN: // 打开客户端
  531. result = bc260y.open_socket(connectID, "UDP", Host, Port, 1, &socket_err);
  532. if(result == Result_Success)
  533. {
  534. if(socket_err == 0)
  535. {
  536. goto_step(STEP_JUDGE_AUTH_OR_DATA);
  537. }
  538. else
  539. {
  540. goto_failure("客户端打开错误");
  541. }
  542. }
  543. else if(result == Result_Failed)
  544. {
  545. goto_failure("打开客户端失败");
  546. }
  547. break;
  548. case STEP_JUDGE_AUTH_OR_DATA: // 判断认证还是发送
  549. auth_or_data = judge_auth_or_data();
  550. if(auth_or_data == 1) // 已认证
  551. {
  552. goto_step(STEP_JOIN_DATA_MESSAGE);
  553. }
  554. else
  555. {
  556. goto_step(STEP_JOIN_AUTH_MESSAGE);
  557. }
  558. break;
  559. case STEP_JOIN_AUTH_MESSAGE: // 拼接认证报文
  560. if(auth_times > 0) // 最多重新认证一次
  561. {
  562. goto_failure("认证次数过多");
  563. }
  564. else
  565. {
  566. join_auth_message(coap_message, &coap_message_length);
  567. goto_step(STEP_SEND);
  568. auth_times++;
  569. }
  570. break;
  571. case STEP_JOIN_DATA_MESSAGE: // 拼接数据报文
  572. pump_params.lac = cgreg_lac;
  573. pump_params.ci = cgreg_ci;
  574. Pump_Params_Refresh();
  575. // 编码
  576. business_protocol_encode(pump_params, data, &data_length);
  577. join_data_message(data, data_length, coap_message, &coap_message_length);
  578. goto_step(STEP_SEND);
  579. break;
  580. case STEP_SEND: // 发送send
  581. result = bc260y.send_rai(connectID, coap_message, coap_message_length,2);
  582. if(result == Result_Success)
  583. {
  584. goto_step(STEP_RECV);
  585. }
  586. else if(result == Result_Failed)
  587. {
  588. goto_failure("发送send失败");
  589. }
  590. break;
  591. case STEP_RECV: // 等待结果
  592. result = bc260y.recv_with_time(connectID, coap_message, &coap_message_length, 10000);
  593. if(result == Result_Success)
  594. {
  595. uint8_t res = recv_data_handle(coap_message, coap_message_length);
  596. if(res == 0) // 发送失败
  597. {
  598. goto_step(STEP_JUDGE_AUTH_OR_DATA); // 重新认证
  599. }
  600. else if(res == 1) // 认证成功
  601. {
  602. goto_step(STEP_JOIN_DATA_MESSAGE);
  603. }
  604. else if(res == 2) // 发送成功
  605. {
  606. goto_success("发送成功");
  607. }
  608. }
  609. else if(result == Result_Failed)
  610. {
  611. goto_failure("等待结果失败");
  612. }
  613. break;
  614. case STEP_WAIT: // 等待
  615. wait();
  616. break;
  617. case STEP_SUCCESS: // 成功
  618. goto_step(STEP_SET_SLEEP);
  619. break;
  620. case STEP_FAILURE: // 失败
  621. goto_step(STEP_SET_SLEEP);
  622. break;
  623. case STEP_SET_SLEEP: // 设置休眠模式
  624. result = bc260y.set_sleep(1);
  625. if(result != Result_None)
  626. {
  627. goto_step(STEP_ENTER_SLEEP);
  628. }
  629. break;
  630. case STEP_ENTER_SLEEP: // 进入睡眠
  631. //bc260y.enter_sleep();
  632. goto_step(STEP_FINISH);
  633. break;
  634. case STEP_FINISH: // 结束流程
  635. break;
  636. default:
  637. break;
  638. }
  639. }
  640. #endif