BC260_UDP_Client5.c 16 KB

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