| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673 |
- #include "CONFIG.h"
- #if BC260Y
- /**
- ******************************************************************************
- * 版 本 :V1.0.0
- * 作 者 :liuyanbin,helomgxiang
- * 版权所有,盗版必究。
- * Copyright(C) All rights reserved
- ******************************************************************************
- * 历史记录
- * 序号 版本 变更内容 作者 日期
- * 1 V0.0.1 实现数据发送功能 何龙翔 2023/12/10
- * 2 V0.0.2 实现NB数据发送功能 刘艳斌 2024/1/10
- * 3 V0.0.3 整合4g和nb 刘艳斌 2024/1/24
- *
- ******************************************************************************
- */
- #include "stm32f10x.h"
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
- #include <stdlib.h>
- #include "UDP_Client.h"
- #include "Timer_Module.h"
- #include "Log_Module.h"
- #include "mbedtls_util.h"
- #include "cJSON.h"
- #include "bc260y.h"
- #include "Pump_Dicts_Util.h"
- #include "Regist.h"
- #include "PumpBusiness.h"
- #include "aliyuniot.h"
- #include "Initialize.h"
- // 流程
- enum Step{
- STEP_NONE, // 空闲状态,无操作
- STEP_START, // 开始
- STEP_EXIT_SLEEP, // 退出睡眠
- STEP_ENTER_SLEEP, // 进入睡眠
- STEP_WAIT, // 等待
-
- STEP_STATE, // 查询状态
- STEP_OPEN, // 打开
- STEP_OPEN_WAIT_RESULT, // 等待打开结果
- STEP_JUDGE_AUTH_OR_DATA, // 判断认证还是发送
- STEP_JOIN_AUTH_MESSAGE, // 拼接认证报文
- STEP_JOIN_DATA_MESSAGE, // 拼接数据报文
- STEP_QUERY_SLEEP, // 查询休眠
- STEP_SET_SLEEP, // 开启休眠
- STEP_QUERY_QENG_SERVINGCELL, // 查询信号质量
-
- STEP_SET_QISDE_0, // 关闭发送回显
- STEP_SET_QISDE_1,
- STEP_SEND, // 发送
- STEP_RECV, // 等待发送结果
-
- STEP_QUERY_CFUN,
- STEP_SET_CFUN_0, // 设置最小功能模式
- STEP_WAIT_SET_CFUN_0, // 等待设置最小功能模式
- STEP_SET_CFUN_1, // 设置全功能模式
- STEP_WAIT_SET_CFUN_1, // 等待设置全功能模式结果
-
- STEP_SET_CGREG_2, // 设置ps域允许上报网络注册和位置信息
- STEP_QUERY_CGREG, // 查询网络注册状态
- STEP_DATAFORMAT,//配置发送格式
-
- STEP_CLOSE, // 关闭
-
- STEP_SUCCESS, // 成功
- STEP_FAILURE, // 失败
- STEP_FINISH, // 流程结束
- STEP_VERIFY_REGIST,//验证注册
- STEP_VERIFY_INITIALIZE, // 验证初始化
- STEP_QUERY_SOCKET, // 查询连接
- STEP_QUERY_DNS, // 查询DNS
- STEP_SET_DNS, // 设置DNS
-
- STEP_QUERY_CFUN_DNS, // 设置完dns后,查询功能模式
- STEP_SET_CFUN_0_DNS, // 设置完dns后,设置最小功能模式
- };
- // 当前
- static enum Step step = STEP_NONE;
- // 下一步
- static enum Step next_step = STEP_NONE;
- // 客户端5
- struct UDP_Client_Struct client = {
- .status = Client_Status_None,
- };
- // socket ID
- static uint8_t connectID = 1;
- // coap报文
- static uint8_t coap_message[512];
- static uint16_t coap_message_length = 0;
- // 泵参数
- extern struct Pump_Params pump_params;
- // 待发送数据的地址和长度
- static uint8_t databuff[128];
- static uint16_t data_length;
- static uint8_t test_flag=0;
- // 计时相关的变量
- static struct TIMER_Struct timer;
- uint32_t wait_time = 10;
- // 信号值
- static struct Signal
- {
- int RSRP;
- int RSRQ;
- int RSSI;
- int SINR;
- } signal = {
- .RSRP = 99,
- .RSRQ = 99,
- .RSSI = 99,
- .SINR = 99,
- };
- void Query_Signal(int * RSRP, int * RSRQ, int * RSSI, int * SINR)
- {
- *RSRP = signal.RSRP;
- *RSRQ = signal.RSRQ;
- *RSSI = signal.RSSI;
- *SINR = signal.SINR;
- }
- // 初始化
- void UDP_Client_Init(void)
- {
- // aliyuniot_set_device_params("he1fAihB9M9", "123456", "76ef28342ee0760d781af138095b8fe4");
- aliyuniot_set_device_params(regist_get_aliyun_productKey(),regist_get_aliyun_deviceName(),regist_get_aliyun_deviceSecret());
- // aliyuniot_set_device_params(flashdata.productKey,flashdata.deviceName,flashdata.deviceSecret);
- // 设置host
- }
- // 声明函数。步骤跳转
- void goto_step(enum Step ns);
- // 发送流程
- void UDP_Client_Handle(void);
- // 发送数据
- void UDP_Client_Send(uint8_t test_switch)
- {
- test_flag=test_switch;
- if(step == STEP_NONE)
- {
- // 初始化
- UDP_Client_Init();
- // 正在发送
- client.status = Client_Status_Sending;
- // 流程开始
- goto_step(STEP_START);
- }
- }
- // 获取状态
- enum Client_Status UDP_Client_Status(void)
- {
- if(client.status == Client_Status_None)
- {
- return client.status;
- }
- else if(step == STEP_FINISH)
- {
- return client.status;
- }
- else
- {
- return Client_Status_Sending;
- }
- }
- // 获取备注
- char * UDP_Client_Get_Info(void)
- {
- return client.info;
- }
- // 清除
- void UDP_Client_Clear(void)
- {
- // 流程置空
- goto_step(STEP_NONE);
- // 空闲
- client.status = Client_Status_None;
- }
- // 直接跳转到下一步
- static void goto_step(enum Step ns)
- {
- // 重置bc260y状态
- bc260y.reset();
- step = ns;
- }
- // 先等待再跳转到下一步
- static void goto_step_wait(enum Step ns, uint32_t t)
- {
- goto_step(STEP_WAIT); // 等待
- wait_time = t;
- next_step = ns; // 等待之后跳转
- }
- // 只等待,等待之后返回原来的步骤
- static void goto_wait(uint32_t t)
- {
- goto_step_wait(step, t);
- }
- // 失败
- static void goto_failure(char * info)
- {
- Log_Printf_Debug("STEP: 数据发送失败,%s\r\n", info);
- client.status = Client_Status_Failure;
- strcpy(client.info, info);
- goto_step(STEP_FAILURE);
- }
- // 成功
- static void goto_success(char * info)
- {
- Log_Printf_Debug("STEP: 数据发送成功,%s\r\n", info);
- client.status = Client_Status_Success;
- strcpy(client.info, info);
- goto_step(STEP_SUCCESS);
- }
- // 等待
- static void wait(void)
- {
- if(time_get_delay(&timer) > wait_time)
- {
- goto_step(next_step); // 进入下一步
- time_clear(&timer);
- }
- }
- //// 发送数据的逻辑
- static enum Result result = Result_None;
- static uint8_t cgreg_n;
- static uint8_t cgreg_stat;
- static uint16_t cgreg_lac;
- static uint32_t cgreg_ci;
- //static uint8_t query_cgreg_times = 0; // 查询网络状态的次数
- static uint8_t auth_times = 0; // 认证次数
- static uint16_t socket_err = 0;
- static uint8_t auth_or_data = 0;
- static enum Initialize_Result InitializeResult;
- // dns
- static char pridnsaddr[20];
- static uint8_t resend_counter = 0;//nb失败重发标志
- static uint8_t cfun_mode = 0;
- static uint8_t connectstate = 0;
- void UDP_Client_Handle(void)
- {
- // 如果模块正在初始化,直接返回发送失败
- InitializeResult = get_initialize_status();
- if(InitializeResult==Initialize_Result_Busy)
- {
- goto_failure("模块正在初始化");
- goto_step(STEP_FINISH);
- }
- // 流程
- switch(step)
- {
- case STEP_NONE: // 空闲
- break;
- case STEP_START: // 开始
- Log_Printf_Debug("\r\n>>发送流程开始\r\n");
- auth_times = 0; // 认证次数
- goto_step(STEP_EXIT_SLEEP);
- Log_Printf_Debug("\r\n>>退出休眠\r\n");
- break;
- case STEP_EXIT_SLEEP: // 退出休眠
- result = bc260y.exit_sleep_2();
- if(result == Result_Success)
- {
- goto_step(STEP_SET_QISDE_1);
- Log_Printf_Debug("\r\n>>开启回显\r\n");
- }
- else if(result == Result_Failed)
- {
- goto_failure("退出休眠失败");
- }
- break;
- case STEP_SET_QISDE_1: // 开启回显
- result = bc260y.set_qisde(1);
- if(result == Result_Success)
- {
- goto_step(STEP_SET_SLEEP);
- Log_Printf_Debug("\r\n>>设置休眠模式\r\n");
- }
- else if(result == Result_Failed)
- {
- goto_failure("开启回显失败");
- }
- break;
- case STEP_SET_SLEEP: // 设置休眠模式
- result = bc260y.set_sleep(2);
- if(result == Result_Success)
- {
- goto_step(STEP_DATAFORMAT);
- Log_Printf_Debug("\r\n>>配置发送格式\r\n");
- }
- else if(result == Result_Failed)
- {
- goto_failure("设置休眠模式失败");
- }
- break;
- case STEP_DATAFORMAT: // 配置发送格式
- result = bc260y.dataformat(1);
- if(result == Result_Success)
- {
- goto_step(STEP_QUERY_DNS);
- Log_Printf_Debug("\r\n>>查询DNS\r\n");
- memset(pridnsaddr, 0, sizeof(pridnsaddr));
- }
- else if(result == Result_Failed)
- {
- goto_failure("配置发送格式失败");
- }
- break;
- case STEP_QUERY_DNS: // 查询DNS
- result = bc260y.query_dns(pridnsaddr);
- if(result == Result_Success)
- {
- if(strlen(pridnsaddr) <= 1)
- {
- goto_step(STEP_SET_DNS);
- Log_Printf_Debug("\r\n>>设置DNS\r\n");
- }
- else
- {
- goto_step(STEP_QUERY_CFUN);
- Log_Printf_Debug("\r\n>>查询功能模式\r\n");
- }
- }
- else if(result == Result_Failed)
- {
- goto_failure("查询DNS失败");
- }
- break;
- case STEP_SET_DNS:
- result = bc260y.set_dns("114.114.114.114", "8.8.8.8"); // 设置DNS
- if(result == Result_Success)
- {
- goto_step(STEP_QUERY_CFUN_DNS);
- Log_Printf_Debug("\r\n>>设置完dns,查询功能模式\r\n");
- }
- else if(result == Result_Failed)
- {
- goto_failure("查询功能模式失败");
- }
- break;
- case STEP_QUERY_CFUN_DNS: // 设置完dns,查询功能模式
- result = bc260y.query_cfun(&cfun_mode);
- if(result == Result_Success)
- {
- if(cfun_mode == 1)
- {
- goto_step(STEP_SET_CFUN_0_DNS);
- Log_Printf_Debug("\r\n>>设置完dns,先设置最小功能模式\r\n");
- }
- else
- {
- goto_step(STEP_SET_CFUN_1);
- Log_Printf_Debug("\r\n>>设置完dns,设置全功能模式\r\n");
- }
- }
- else if(result == Result_Failed)
- {
- goto_failure("查询功能模式失败");
- }
- break;
- case STEP_SET_CFUN_0_DNS: // 设置完dns,设置最小功能模式
- result = bc260y.set_cfun(0);
- if(result == Result_Success)
- {
- goto_step(STEP_SET_CFUN_1);
- Log_Printf_Debug("\r\n>>设置完dns,设置全功能模式\r\n");
- }
- else if(result == Result_Failed)
- {
- goto_failure("设置完dns,设置最小模式失败");
- }
- break;
- case STEP_QUERY_CFUN: // 查询功能模式
- result = bc260y.query_cfun(&cfun_mode);
- if(result == Result_Success)
- {
- if(cfun_mode == 0)
- {
- goto_step(STEP_SET_CFUN_1);
- Log_Printf_Debug("\r\n>>设置全功能模式\r\n");
- }
- else
- {
- goto_step(STEP_SET_CGREG_2);
- Log_Printf_Debug("\r\n>>设置ps域\r\n");
- }
- }
- else if(result == Result_Failed)
- {
- goto_failure("查询功能模式失败");
- }
- break;
- case STEP_SET_CFUN_1: // 设置全功能模式
- result = bc260y.set_cfun(1);
- if(result == Result_Success)
- {
- resend_counter++;
- goto_step(STEP_SET_CGREG_2);
- Log_Printf_Debug("\r\n>>设置ps域\r\n");
- }
- else if(result == Result_Failed)
- {
- resend_counter++;
- goto_failure("设置全功能模式失败");
- }
- break;
- case STEP_SET_CGREG_2: // 设置ps域
- result = bc260y.set_cereg(2);
- if(result == Result_Success)
- {
- // goto_step_wait(STEP_QUERY_CGREG, 400);
- goto_step(STEP_QUERY_CGREG);
- Log_Printf_Debug("\r\n>>查询ps域\r\n");
- }
- else if(result == Result_Failed)
- {
- goto_failure("设置ps域失败");
- }
- break;
- case STEP_QUERY_CGREG: // 查询ps域
- result = bc260y.query_cereg(&cgreg_n, &cgreg_stat, &cgreg_lac, &cgreg_ci);
- if(result == Result_Success)
- {
- if(cgreg_stat == 1)
- {
- // 参数赋值
- pump_params.lac = cgreg_lac;
- pump_params.ci = cgreg_ci;
- // 编码
- // 下一步
- if(test_flag == 0)
- {
- goto_step(STEP_QUERY_SOCKET);
- Log_Printf_Debug("\r\n>>查询连接\r\n");
- }
- else
- {
- goto_step(STEP_QUERY_QENG_SERVINGCELL);
- Log_Printf_Debug("\r\n>>查询信号质量\r\n");
- }
- }
- else
- {
- goto_failure("网络注册失败");
- }
- }
- else if(result == Result_Failed)
- {
- goto_failure("查询ps域失败");
- }
- break;
- case STEP_QUERY_QENG_SERVINGCELL: // 查询信号质量
- result = bc260y.qeng_servingcell(&signal.RSRP, &signal.RSRQ, &signal.RSSI, &signal.SINR);
- if(result == Result_Success)
- {
- goto_step(STEP_QUERY_SOCKET);
- Log_Printf_Debug("\r\n>>查询连接\r\n");
- }
- else if(result == Result_Failed)
- {
- goto_failure("查询信号质量失败");
- }
- break;
-
- case STEP_QUERY_SOCKET: // 查询连接
- result =bc260y.query_socket_state(connectID,&connectstate);
- if(result == Result_Success)
- {
- if(connectstate==2)
- {
- goto_step(STEP_JUDGE_AUTH_OR_DATA);
- Log_Printf_Debug("\r\n>>判断认证还是发送\r\n");
- }
- else
- {
- goto_step(STEP_CLOSE);
- Log_Printf_Debug("\r\n>>关闭连接\r\n");
- }
- }
- else if(result == Result_Failed)
- {
- goto_failure("查询UDP连接失败");
- }
- break;
- case STEP_CLOSE: // 关闭连接
- result = bc260y.close_socket(connectID);
- if(result == Result_Success)
- {
- goto_step(STEP_OPEN);
- Log_Printf_Debug("\r\n>>打开客户端\r\n");
- }
- else if(result == Result_Failed)
- {
- goto_failure("关闭连接失败");
- }
- break;
- case STEP_OPEN: // 打开客户端
- result = bc260y.open_socket(connectID, "UDP",aliyuniot_get_host(), aliyuniot_get_port(), 1, &socket_err);
- if(result == Result_Success)
- {
- if(socket_err == 0)
- {
- goto_step(STEP_JUDGE_AUTH_OR_DATA);
- Log_Printf_Debug("\r\n>>判断认证还是发送\r\n");
- }
- else
- {
- goto_failure("客户端打开错误");
- }
- }
- else if(result == Result_Failed)
- {
- goto_failure("打开客户端失败");
- }
- break;
- case STEP_JUDGE_AUTH_OR_DATA: // 判断认证还是发送
- auth_or_data = aliyuniot_is_authentication();
- if(auth_or_data == 1) // 已认证
- {
- goto_step(STEP_JOIN_DATA_MESSAGE);
- Log_Printf_Debug("\r\n>>拼接数据报文\r\n");
- }
- else
- {
- goto_step(STEP_JOIN_AUTH_MESSAGE);
- Log_Printf_Debug("\r\n>>拼接认证报文\r\n");
- }
- break;
- case STEP_JOIN_AUTH_MESSAGE: // 拼接认证报文
- if(auth_times > 0) // 最多重新认证一次
- {
- goto_failure("认证次数过多");
- }
- else
- {
- memset(coap_message, 0, sizeof(coap_message));
- aliyuniot_get_auth_message(coap_message, &coap_message_length);
- // Log_Printf_Debug("认证报文:%d\r\n", coap_message_length);
- // Log_SendHex(coap_message, coap_message_length);
- // Log_Printf_Debug("\r\n");
- goto_step(STEP_SEND);
- Log_Printf_Debug("\r\n>>发送认证报文\r\n");
- auth_times++;
- }
- break;
- case STEP_JOIN_DATA_MESSAGE: // 拼接数据报文
- pump_params.lac = cgreg_lac;
- pump_params.ci = cgreg_ci;
- Pump_Params_Refresh();
- // 编码
- memset(databuff, 0, sizeof(databuff));
- business_protocol_encode(pump_params, databuff, &data_length);
- memset(coap_message, 0, sizeof(coap_message));
- aliyuniot_get_data_message(databuff, data_length, coap_message, &coap_message_length);
- // Log_Printf_Debug("数据报文:%d\r\n", coap_message_length);
- // Log_SendHex(coap_message, coap_message_length);
- // Log_Printf_Debug("\r\n");
- goto_step(STEP_SEND);
- Log_Printf_Debug("\r\n>>发送数据报文\r\n");
- break;
-
- case STEP_SEND: // 发送send
- result = bc260y.send_rai(connectID, coap_message, coap_message_length, 2);
- if(result == Result_Success)
- {
- memset(coap_message, 0, sizeof(coap_message));
- goto_step(STEP_RECV);
- Log_Printf_Debug("\r\n>>等待接收数据\r\n");
- }
- else if(result == Result_Failed)
- {
- goto_failure("发送send失败");
- }
- break;
- case STEP_RECV: // 等待结果
- result = bc260y.recv_with_time(connectID, coap_message, &coap_message_length, 4000);
- if(result == Result_Success)
- {
- uint8_t res = aliyuniot_recv_data_handle(coap_message, coap_message_length);
- if(res == 0) // 发送失败
- {
- goto_step(STEP_JUDGE_AUTH_OR_DATA); // 重新认证
- Log_Printf_Debug("\r\n>>重新认证\r\n");
- }
- else if(res == 1) // 认证成功
- {
- goto_step(STEP_JOIN_DATA_MESSAGE);
- Log_Printf_Debug("\r\n>>拼接数据报文\r\n");
- }
- else if(res == 2) // 发送成功
- {
- goto_success("发送成功");
- }
- }
- else if(result == Result_Failed)
- {
- goto_failure("等待结果失败");
- }
- break;
- case STEP_WAIT: // 等待
- wait();
- break;
- case STEP_SUCCESS: // 成功
- goto_step(STEP_ENTER_SLEEP);
- Log_Printf_Debug("\r\n>>成功,进入睡眠\r\n");
- break;
- case STEP_FAILURE: // 失败
- resend_counter++;
- goto_step(STEP_SET_CFUN_0);
- Log_Printf_Debug("\r\n>>失败,设置最小功能模式\r\n");
- break;
- case STEP_SET_CFUN_0: // 设置最小功能模式
- result = bc260y.set_cfun(0);
- if(result == Result_Success)
- {
- if(resend_counter > 1) // 重发次数
- {
- goto_step(STEP_ENTER_SLEEP);
- Log_Printf_Debug("\r\n>>重发次数%d > 1,结束发送,进入睡眠\r\n", resend_counter);
- }
- else
- {
- goto_step(STEP_START);
- Log_Printf_Debug("\r\n>>重发次数%d <= 1,重新发送\r\n", resend_counter);
- }
- }
- else if(result == Result_Failed)
- {
- goto_step(STEP_ENTER_SLEEP);
- Log_Printf_Debug("\r\n>>进入睡眠\r\n");
- }
- break;
- case STEP_ENTER_SLEEP: // 进入睡眠
- resend_counter=0;//失败重发置0
- goto_step(STEP_FINISH);
- Log_Printf_Debug("\r\n>>发送流程结束\r\n");
- break;
- case STEP_FINISH: // 结束流程
- break;
- default:
- break;
- }
-
- }
- #endif
|