From: d15521469561 <1731678236@qq.com> Date: Tue, 2 Jun 2026 01:14:00 +0000 (+0800) Subject: 1.修改串口缓冲读取字节大小 X-Git-Url: http://git.snicker.com.cn/css/html/static/gitweb.css?a=commitdiff_plain;h=96e75afebbfff719bf5636267f368b643b5561b8;p=mirrors%2Fmqtt_wglt_iot.git 1.修改串口缓冲读取字节大小 2.优化dpid数据 3.优化topic发布和订阅消息 --- diff --git a/main/app_main.c b/main/app_main.c index 57dd49b..ac1402c 100644 --- a/main/app_main.c +++ b/main/app_main.c @@ -43,7 +43,7 @@ static void onesec_timer_callback(void* arg) ESP_LOGI("TIME", "当前时间:%s", buf); #endif - static uint8_t onesec_timer_count = 0; + // static uint8_t onesec_timer_count = 0; // common_setting.tz_offset_min = 8*60; // if(onesec_timer_count < 20){ @@ -60,18 +60,18 @@ static void onesec_timer_callback(void* arg) Get_Time(&local_time); #if ENABLE_HTTP_N455_OTA_FUNC - static uint8_t onesec_timer_count = 0; - if(onesec_timer_count < 65) - { - onesec_timer_count++; - if(onesec_timer_count % 10){ + // static uint8_t onesec_timer_count = 0; + // if(onesec_timer_count < 65) + // { + // onesec_timer_count++; + // if(onesec_timer_count % 10){ // Uart_Reply_Send(COMMON_UART_BLE_CONNECT_STATUS,NULL,0,HOST_TYPE_UART); // Https_Ota_Start(); // xEventGroupSetBits(common_event_group, COMMON_HTTPS_N455_OTA_BIT); - } + // } - } + // } #endif #if ENABLE_HTTPS_OTA_FUNC //===========esp32 ota timeout=================// @@ -83,7 +83,6 @@ static void onesec_timer_callback(void* arg) N455_Ota_Check_TimeOut(); #endif //================mqtt is cnnect===========// - mqtt_check_isconnect(); // 在这里添加每秒执行的代码 #if ENABLE_HTTPS_OTA_FUNC ESP_LOGI(MAIN_TAG, "common_setting.ble_status.ble_state %d ota_cur_status %d %" PRIu32 " bytes",common_setting.ble_status.ble_state,g_ota_status,esp_get_free_heap_size()); @@ -282,7 +281,10 @@ void app_main(void) // esp_log_level_set("transport", ESP_LOG_VERBOSE); // esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_LOGI(MAIN_TAG, "[APP] Free memory start: %" PRIu32 " bytes", esp_get_free_heap_size()); + // Uart_Initialize(); + // uart_task_handler = xTaskCreateStatic(Uart_Task, "Uart_Task", UART_TASK_STACK, NULL, 7, uart_task_stack, &uart_task_tcb); + // return; //chip info esp_chip_info_t chip_info; esp_chip_info(&chip_info); diff --git a/main/ble.h b/main/ble.h index 5131d7d..1ded321 100644 --- a/main/ble.h +++ b/main/ble.h @@ -39,7 +39,7 @@ typedef struct { uint16_t length; uint8_t data[BLE_RECV_BUF_SIZE]; -}BLE_MESSAGE_T; +}__attribute__((packed)) BLE_MESSAGE_T; #define BLE_WRTIE_QUEUE_LENGTH 2 diff --git a/main/https_ota.c b/main/https_ota.c index 7dbb307..8fdae04 100644 --- a/main/https_ota.c +++ b/main/https_ota.c @@ -30,7 +30,7 @@ static const char *HTTPS_OTA = "HTTPS_OTA"; ota_status_t g_ota_status = OTA_STATUS_IDLE; // 全局OTA状态 -#define HTTPS_OTA_TIMEOUT_TIME 300 //单位s +#define HTTPS_OTA_TIMEOUT_TIME 180//单位s static volatile uint8_t ota_timeout_req = 0; //超时升级失败 static volatile uint16_t ota_timeout_count = 0; @@ -40,7 +40,7 @@ static volatile uint16_t ota_faile_count = 0; static uint8_t https_ota_init = 0; #define HTTPS_OTA_RETRY_NUM 0 -#define OTA_URL_SIZE 255 +#define OTA_URL_SIZE 128 #define HTTPS_OTA_STACK_SIZE (4*1024) static char https_ota_url[OTA_URL_SIZE] = "https://pxn-iot.s3.us-east-2.amazonaws.com/iot01/2026/05/22/6a0fb4324db40762648f18b3.bin"; @@ -203,6 +203,7 @@ static void Https_Ota_Task_Cb(void *pvParameter) ESP_LOGI(HTTPS_OTA, "ESP_HTTPS_OTA upgrade successful. Rebooting ..."); g_ota_status = OTA_STATUS_SUCCESS; //成功 ota_timeout_count=0; + https_ota_handle = NULL; //成功不走下面失败的 #if (!ENABLE_HTTP_N455_OTA_FUNC) //n455升级成功后上报状态 esp32升级成功后b不需要上报了 因为n455升级成功后会发一个特定的ack 包给esp32 esp32收到这个ack包就知道n455升级成功了 就不需要esp32再发一次mqtt消息上报了 uint8_t ret = Https_Ota_Report_Status(g_ota_status); @@ -560,7 +561,7 @@ uint8_t Https_Ota_Report_Status(uint8_t ota_status) #if (!ENABLE_HTTP_N455_OTA_FUNC) else if(ota_status == OTA_STATUS_UPGRADING) #else - else if(ota_status == OTA_STATUS_UPGRADING || ota_status == OTA_STATUS_UPGRADING) + else if(ota_status == OTA_STATUS_UPGRADING || ota_status == N455_OTA_STATUS_UPGRADING) #endif { cJSON_AddStringToObject(pRoot, "detailMsg", "升级中"); @@ -639,7 +640,7 @@ uint8_t Https_Ota_Report_Status(uint8_t ota_status) #if (!ENABLE_HTTP_N455_OTA_FUNC) if(ota_status == OTA_STATUS_SUCCESS) #else - if(ota_status == OTA_STATUS_SUCCESS || ota_status == OTA_STATUS_UPGRADING) + if(ota_status == OTA_STATUS_SUCCESS || ota_status == N455_OTA_STATUS_SUCCESS) #endif { Wifi_Stop();//断开wifi diff --git a/main/n455_ota.c b/main/n455_ota.c index 1141bed..3d30d2e 100644 --- a/main/n455_ota.c +++ b/main/n455_ota.c @@ -29,7 +29,7 @@ static const char *N455_OTA = "455_OTA"; #include "esp_crt_bundle.h" N455_ota_status_t n455_g_ota_status = N455_OTA_STATUS_IDLE; // -#define OTA_URL_MAX_LEN 255 +#define OTA_URL_MAX_LEN 128 #define OAT_CONNECT_RETRY_NUM 3 //重连服务器次数 3 #define OTA_TOTAL_TIMEOUT_S 30 //总超时时间 单位s #define HTTPS_OTA_STACK_SIZE (4*1024) diff --git a/main/uart.c b/main/uart.c index da7d228..abfb236 100644 --- a/main/uart.c +++ b/main/uart.c @@ -27,7 +27,7 @@ StaticTask_t uart_task_tcb; // StackType_t uart_send_task_stack[UART_SEND_TASK_STACK]; // StaticTask_t uart_send_task_tcb; /* -func:uart初始化 + * 函数功能:uart初始化 */ void Uart_Initialize(void) { @@ -154,7 +154,11 @@ void Uart_Rx_Queue_Process(void) } -// 直接解析完整帧 +/* + *函数功能:uart接收数据处理 + *data:接收数据 包含协议头和crc + *len:数据长度 包含协议头和crc +*/ void Uart_Recv_Handle(uint8_t *data, uint16_t len) { uint16_t offset = 0; @@ -185,10 +189,14 @@ void Uart_Recv_Handle(uint8_t *data, uint16_t len) } #endif static volatile uint8_t ble_connected_notify_flag = 0; -// static uint8_t ble_connected_first_flag = false; static uint16_t ble_connected_notify_count= 0; #define UART_SEND_BLE_STATUS_RETRY_NUM 3 #define UART_SEND_BLE_STATUS_RETRY_TIME 500 //单位ms +/* + *函数功能:设置蓝牙连接成功标识 + *参数说明:无 + *返回值:无 +*/ void Uart_Set_Ble_Status(void) { ESP_LOGI(UART_TAG,"Uart_Set_Ble_Status!!!!"); @@ -197,6 +205,11 @@ void Uart_Set_Ble_Status(void) // ble_connected_first_flag = 0; } +/* + *函数功能:清空蓝牙连接成功标识 + *参数说明:无 + *返回值:无 +*/ void Uart_Clear_Ble_Status(void) { // ble_connected_first_flag=0; @@ -204,37 +217,30 @@ void Uart_Clear_Ble_Status(void) ble_connected_notify_count =0 ; } -void Uart_Send_Task(void *arg) -{ - while (1) - { - - Uart_Tx_Queue_Process(); - vTaskDelay(5/ portTICK_PERIOD_MS); - } - -} + /* -func:uart接收发送task + * 函数功能: uart接收发送task */ #define READ_BUF_MAX_LEN (256) static uint8_t read_buf[READ_BUF_MAX_LEN]={0}; static uint32_t rx_last_recv_tick = 0; void Uart_Task(void *arg) { - esp_log_level_set(UART_TAG, ESP_LOG_INFO); + esp_log_level_set(UART_TAG, ESP_LOG_ERROR); // static UART_RECV_DATA_T uart_rx_data_struct = {0}; // static uint8_t tx_count = 0; static uint32_t now = 0 ; + static uint32_t i = 0; while (1) { + if(ble_connected_notify_flag) { if(ble_connected_notify_count <= UART_SEND_BLE_STATUS_RETRY_TIME*UART_SEND_BLE_STATUS_RETRY_NUM) { if(ble_connected_notify_count == 0) //第一次发送 { - ESP_LOGI(UART_TAG,"first ble_connected_notify_flag!!!!"); + Uart_Reply_Send(COMMON_UART_BLE_CONNECT_STATUS,NULL,0,HOST_TYPE_UART); } ble_connected_notify_count++; @@ -243,34 +249,27 @@ void Uart_Task(void *arg) Uart_Reply_Send(COMMON_UART_BLE_CONNECT_STATUS,NULL,0,HOST_TYPE_UART); } } + }else{ + ble_connected_notify_count = 0; } - // ESP_LOGI(UART_TAG, "uart %d", rx_cache_len); - const int uart_rx_size = uart_read_bytes(UART_NUM_1, read_buf, READ_BUF_MAX_LEN,2/ portTICK_PERIOD_MS);//2/ portTICK_PERIOD_MS); + const int uart_rx_size = uart_read_bytes(UART_NUM_1, read_buf, READ_BUF_MAX_LEN,0);//2/ portTICK_PERIOD_MS); if (uart_rx_size > 0) { - // ESP_LOGI(UART_TAG, "Received %d bytes: %d\n", uart_rx_size,rx_timeout_cnt); - // uart_rx_data_struct.size = uart_rx_size; - // Uart_Recv_Handle(uart_rx_data_struct.buffer,uart_rx_size); if (rx_cache_len + uart_rx_size < RX_CACHE_MAX_LEN) { memcpy(&rx_cache[rx_cache_len], read_buf, uart_rx_size); rx_cache_len += uart_rx_size; } rx_last_recv_tick = xTaskGetTickCount(); - // ESP_LOGI(UART_TAG, "rx_last_recv_tick: %ld now:%ld", rx_last_recv_tick,now); } if (rx_cache_len > 0) { now = xTaskGetTickCount(); - // ESP_LOGI(UART_TAG, "rx_last_recv_tick %ld bytes: %ld %ld\n", rx_last_recv_tick,now,now - rx_last_recv_tick); if (now - rx_last_recv_tick >= 2) { - // 超时 → 解析 - // ESP_LOGI(UART_TAG, "Frame timeout, parse len: %d", rx_cache_len); - Uart_Recv_Handle(rx_cache, rx_cache_len); rx_cache_len = 0; - memset(rx_cache, 0, sizeof(rx_cache)); + // memset(rx_cache, 0, sizeof(rx_cache)); } } @@ -356,7 +355,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) if (len >= 12 && data[2] >= 9) { common_setting.mcu_info.vid = *(uint16_t*)&data[3]; - #if 0 + #if 1 common_setting.mcu_info.pid = 0xA00D;// #else common_setting.mcu_info.pid = *(uint16_t*)&data[5]; @@ -382,7 +381,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) uart_pairing_reply.content[0]=0x01; ESP_LOGI(UART_RECV, "start nimble 0x%04x 0x%04x 0x%04x 0x%04x %s\n",common_setting.mcu_info.vid,common_setting.mcu_info.pid,\ common_setting.mcu_info.fw_version,common_setting.mcu_info.build_version,common_setting.mcu_info.product_name); - #if !ENABLE_HTTP_N455_OTA_FUNC + #if 1 Ble_Start_Nimble_NetWork();////开启蓝牙配网 #endif @@ -606,31 +605,31 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) // Check_Host_Type(uart_pairing_reply.cmd, uart_pairing_reply.content, uart_pairing_reply.len, host_type); } - ESP_LOGI(UART_RECV, "uart recv %d bytes:cmd %0x", len, data[1]); + // ESP_LOGI(UART_RECV, "uart recv %d bytes:cmd %0x", len, data[1]); } else if(data[0] == FRAMEHEAD_PANEL_TO_IOT) { - ESP_LOGI(UART_RECV, "net pairing crc8 %d bytes:cmd %0x", len, data[1]); + // ESP_LOGI(UART_RECV, "net pairing crc8 %d bytes:cmd %0x", len, data[1]); if(len < 4 || len > UART_BUF_SIZE) { - ESP_LOGE(UART_RECV, "net pairing len err:%d", len); + // ESP_LOGE(UART_RECV, "net pairing len err:%d", len); } else if(Crc8_ROHC_Cal(&data[1], len-2) != data[len-1]) { - ESP_LOGE(UART_RECV, "net pairing crc8 err:%02x %02x", Crc8_ROHC_Cal(&data[1], len-2), data[len-1]); + // ESP_LOGE(UART_RECV, "net pairing crc8 err:%02x %02x", Crc8_ROHC_Cal(&data[1], len-2), data[len-1]); } } } -//0x08048000 + /* -func:uart函数回复send -cmd:协议指令 -pbuf:数据 不包含header crc -len:pbuf的长度 -host_type:预留 -*/ + *函数功能:uart函数回复send + *cmd:协议指令 + *pbuf:数据 不包含header crc + *len:pbuf的长度 + *host_type:预留 + */ //*pub不包含帧头 命令 crcr长度等协议字段,纯数据部分,len为数据部分长度 -uint8_t Uart_Reply_Send(uint8_t cmd, uint8_t* pbuf, uint8_t len, uint8_t host_type) +int Uart_Reply_Send(uint8_t cmd, uint8_t* pbuf, uint8_t len, uint8_t host_type) { if (len > UART_REPLY_MAX_PAYLOAD-4) // 4 = header(1) + cmd(1) + len(1) + crc(1) 暂时len为1个字节 { @@ -656,11 +655,12 @@ uint8_t Uart_Reply_Send(uint8_t cmd, uint8_t* pbuf, uint8_t len, uint8_t host_ty ESP_LOGI(UART_SEND, "uart_tx_queue_handle null"); return 0; } - ESP_LOGI(UART_SEND, "uart_pairing_reply:len %d cmd:%d",uart_pairing_reply.size,cmd); + // ESP_LOGI(UART_SEND, "uart_pairing_reply:len %d cmd:%d",uart_pairing_reply.size,cmd); if (xQueueSend(uart_tx_queue_handle, &uart_pairing_reply, 0) != pdPASS) { - ESP_LOGI(UART_SEND, "UART_SEND ERR\n"); + ESP_LOGI(UART_SEND, "false\n"); return 0; }else{ + ESP_LOGI(UART_SEND, "true\n"); return 1; } diff --git a/main/uart.h b/main/uart.h index ecfd7b4..0babe3f 100644 --- a/main/uart.h +++ b/main/uart.h @@ -128,9 +128,9 @@ void Uart_Initialize(void); void Uart_Tx_Queue_Process(void); void Uart_Rx_Queue_Process(void); -void Uart_Send_Task(void *arg); + void Uart_Task(void *arg); -uint8_t Uart_Reply_Send(uint8_t cmd, uint8_t* pbuf, uint8_t len, uint8_t host_type); +int Uart_Reply_Send(uint8_t cmd, uint8_t* pbuf, uint8_t len, uint8_t host_type); void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type); void Uart_Set_Ble_Status(void); //设置蓝牙连接成功灯闪标识 void Uart_Clear_Ble_Status(void);//清空蓝牙闪灯标识 diff --git a/main/user_http.c b/main/user_http.c index 121118f..05e788b 100644 --- a/main/user_http.c +++ b/main/user_http.c @@ -369,7 +369,6 @@ void Http_Get_Bind_Result(void *pvParameters) } if(result != 1) //绑定结果失败了 重新配网 { - //已经又通过恢复出厂开始广播配网了 Esp32_Factory_Reset(); } @@ -379,7 +378,7 @@ void Http_Get_Bind_Result(void *pvParameters) } //获取绑定结果 void Http_Get_Bing_Result_Task(void){ - ESP_LOGI(HTTP_TAG,"BIND TASK ER\n"); + ESP_LOGI(HTTP_TAG,"BIND TASK init\n"); BaseType_t ret = xTaskCreate(Http_Get_Bind_Result, "http_get_bing_result_task", 2*1024, NULL, 2, NULL); if(ret != pdPASS) { @@ -421,6 +420,7 @@ static void Http_Get_Url_Task_Cb(void *pvParameters) int content_len = 0; int status_code=0; int data_read=0; + char url[HTTP_REQUEST_RUL_LEN] = {0};//预留一个结束符\0 请求地址不能大于255 for(uint8_t retry=0;retryvaluestring) == 0) //"thing.service.property.set" 服务下发属性设备参数 { - + ESP_LOGI(MQTT_TAG, "SET: "); if(strlen(msgId->valuestring) != 0) { @@ -1203,7 +1218,7 @@ void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len) cJSON *replay_root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code); if(replay_root) { - mqtt_send_publish_json(TOPIC_PROPERTY_POST,replay_root); + mqtt_send_publish_json(TOPIC_PROPERTY_ACK_POST,replay_root); cJSON_Delete(replay_root); replay_root = NULL; } @@ -1223,31 +1238,7 @@ void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len) root = NULL; return; } - // mqtt_get_dev_property(params); - // { - // "msgId": "cloud_0196415f3b1f7e2ab7f72c4e8f91c0a1", - // "method": "thing.service.property.set.reply", - // "time": 1777517821, - // "serialNumber": "PXN1LJ1QUR078E", - // "productId": "A00B", - // "code": 0, - // "message": "ok", - - // } - - int ret = mqtt_get_dev_property(params); - // int code = 0; - // if(ret == 0) - // { - // code=ERR_UART_SEND_ERR_CODE; - // } - // cJSON *replay_root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_GET_REPLAY),code); - // if(replay_root) - // { - // mqtt_send_publish_json(TOPIC_PROPERTY_POST,replay_root); - // cJSON_Delete(replay_root); - // replay_root = NULL; - // } + mqtt_get_dev_property(params); } } } @@ -1267,7 +1258,6 @@ void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len) //"thing.service.calibration.invoke", //服务下发 设备应答 else if(strcmp(get_publish_topic(TOPIC_SERVICE_POST),topic) == 0) { - strncpy(topic_compare,topic,sizeof(topic_compare)-1); if(strcmp(get_methon(METHON_SERVICE_CALIBRATION_INVOKE),method->valuestring) == 0) //"thing.service.property.set" 服务下发属性设备参数 { if(strlen(msgId->valuestring) != 0) @@ -1327,7 +1317,7 @@ void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len) func:云端下发设置设备属性值 array:数组 [{"key":value},{"key":value},{"key":value}.............] */ -uint8_t mqtt_set_dev_property(const cJSON *array) +int mqtt_set_dev_property(const cJSON *array) { // { @@ -1344,20 +1334,20 @@ uint8_t mqtt_set_dev_property(const cJSON *array) uint16_t array_size = cJSON_GetArraySize(array); if(array_size <= 0)return 0; - uint8_t total_data_len = 0; + uint16_t total_data_len = 0; uint8_t send_data[UART_REPLY_MAX_PAYLOAD_MQTT]; memset(send_data,0,UART_REPLY_MAX_PAYLOAD_MQTT); ESP_LOGI(MQTT_TAG,"array_size:%d",array_size); - uint8_t ret = 0; - uint8_t send_ok = 0; + int ret = 0; for (uint16_t i = 0; i < array_size; i++) //0-65535 { + ret = 0; // 取数组第i个元素(是一个对象) cJSON *pro_item = cJSON_GetArrayItem(array, i); if (pro_item == NULL || !cJSON_IsObject(pro_item)) { ESP_LOGI(MQTT_TAG,"%d not object\n", i); - ret = 1; + continue; } cJSON *item = pro_item->child; @@ -1384,14 +1374,6 @@ uint8_t mqtt_set_dev_property(const cJSON *array) { continue; } - // if (is_pure_number(key_str)) - // { - // dpid = atoi(key_str);//将key转换成纯数字不能有字母 - // // ESP_LOGI(MQTT_TAG,"dpid %u\n",dpid); - // }else{ - // ESP_LOGI(MQTT_TAG,"dpid is zero\n"); - // continue; - // } }else if(cJSON_IsNumber(item) || (cJSON_IsBool(item))) { @@ -1414,18 +1396,12 @@ uint8_t mqtt_set_dev_property(const cJSON *array) continue; } - // 1. KEY 必须纯数字 - // if (is_pure_number(key_str) ) { - // //item->valueint;//atoi(item->valuestring); //item->valueint; - // dpid = atoi(key_str); - - // // ESP_LOGI(MQTT_TAG,"dpid %u value:%u\n",dpid,value); - // }else{ - // ESP_LOGI(MQTT_TAG,"is number but dpid is zero\n"); - // continue; - // } } + else{ + ESP_LOGI(MQTT_TAG,"%s not string or number\n",key_str); + continue; + } if (!is_pure_number(key_str)) continue; dpid = atoi(key_str); if(dpid <= 0) @@ -1436,26 +1412,25 @@ uint8_t mqtt_set_dev_property(const cJSON *array) //总的长度加上4+当前数据类型的长度 if(total_data_len + 5 + type_len > UART_REPLY_MAX_PAYLOAD_MQTT) //去掉header+cmd+len+crc { - - send_ok = Uart_Reply_Send(COMMON_UART_PROPERTY_SET_CMD,send_data, total_data_len,HOST_TYPE_UART); - if(send_ok==0) + vTaskDelay(5); + ret = Uart_Reply_Send(COMMON_UART_PROPERTY_SET_CMD,send_data, total_data_len,HOST_TYPE_UART); + ESP_LOGI(MQTT_TAG,"set ret %d",ret); + total_data_len=0; + memset(send_data,0,UART_REPLY_MAX_PAYLOAD_MQTT); + if(ret==0) { - ret = 2; break; } - ESP_LOGI(MQTT_TAG,"i:%d UART_REPLY_MAX_PAYLOAD_MQTT-5 %d len:%d send_ok:%d",i,UART_REPLY_MAX_PAYLOAD_MQTT,total_data_len + 5 + type_len,send_ok); - total_data_len=0; - memset(send_data,0,UART_REPLY_MAX_PAYLOAD_MQTT); - vTaskDelay(10); } // 737 10 738 50 739 70 // total_data_len = 0 0 1 2 3 4 5 - send_data[total_data_len+0] = dpid & 0xff; - send_data[total_data_len+1] = (dpid >> 8 )&0xff; - send_data[total_data_len+2] = type;//类型 - send_data[total_data_len+3] = type_len & 0xff; - send_data[total_data_len+4] = (type_len >> 8)& 0xff; + + memcpy(&send_data[total_data_len+0],&dpid,sizeof(uint16_t)); + memcpy(&send_data[total_data_len+2],&type,sizeof(uint8_t)); + memcpy(&send_data[total_data_len+3],&type_len,sizeof(uint16_t)); + + if(type != 5) //不是字符串 copy数字 { memcpy(&send_data[total_data_len+5],&value,type_len); @@ -1467,41 +1442,34 @@ uint8_t mqtt_set_dev_property(const cJSON *array) } if(total_data_len > 0)//发送最后一包 { - if(ret != 1 ) - { - if(ret == 2) //第一包发送失败 - { - return 0; - } - send_ok = Uart_Reply_Send(COMMON_UART_PROPERTY_SET_CMD,send_data, total_data_len,HOST_TYPE_UART); - ESP_LOGI(MQTT_TAG,"send ok:%d\n",send_ok); - return send_ok; - }else{ - ESP_LOGI(MQTT_TAG,"send err\n"); - return 0; - } - - + vTaskDelay(5); + ret = Uart_Reply_Send(COMMON_UART_PROPERTY_SET_CMD,send_data, total_data_len,HOST_TYPE_UART); + ESP_LOGI(MQTT_TAG,"send ok:%d\n",ret); + return ret; + }else{ + ESP_LOGI(MQTT_TAG,"no data to send\n"); + return 0; } - return 1; } -/*云端下发获取设备属性值 -array:数组 ["key","key","key",.............] -*/ -uint8_t mqtt_get_dev_property(const cJSON *array) +/* + *函数功能:云端下发获取设备属性值 + *array:数组 ["key","key","key",.............] + */ +int mqtt_get_dev_property(const cJSON *array) { if(array == NULL || !cJSON_IsArray(array)) return 0; uint16_t array_size = cJSON_GetArraySize(array); if (array_size <= 0) return 0; // 取数组第i个元素(是一字符串数字) + ESP_LOGI(MQTT_TAG, "get array_size:%d",array_size); if(array_size > 0) { - static uint8_t send_dpid_data[UART_REPLY_MAX_PAYLOAD_MQTT]={0}; - memset(send_dpid_data,0,UART_REPLY_MAX_PAYLOAD_MQTT); - uint8_t send_len = 0; + uint8_t send_data[UART_REPLY_MAX_PAYLOAD_MQTT]={0}; + memset(send_data,0,UART_REPLY_MAX_PAYLOAD_MQTT); + uint16_t send_len = 0; // 字段 字节数 说明 // 帧头 1 模组到主控:0xF1 // 命令 1 0x09 @@ -1509,12 +1477,11 @@ uint8_t mqtt_get_dev_property(const cJSON *array) // 数据 N dpid 如:0x010203 表示查询3个dpid,分别为:0x01、0x02、0x03 // CRC校验 1 CRC8-ROHC int ret = 0; - uint8_t send_ok = 1; //["dpid1","dpid2","dpid3"...................] for (int i = 0; i < array_size; i++) { // 取数组第i个元素(是一个对象) - + ret = 0; cJSON *item = cJSON_GetArrayItem(array, i); // ESP_LOGI(MQTT_TAG, " %d =%d | =%s",i,item ? item->type : -1,item ? item->valuestring : "null"); if (item == NULL || !cJSON_IsString(item) || item->valuestring == NULL) { @@ -1536,43 +1503,45 @@ uint8_t mqtt_get_dev_property(const cJSON *array) } if(send_len+sizeof(uint16_t) > UART_REPLY_MAX_PAYLOAD_MQTT) { - vTaskDelay(10); - send_ok = Uart_Reply_Send(COMMON_UART_PROPERTY_BATCH_REPORT_CMD,send_dpid_data, send_len,HOST_TYPE_UART); - if(send_ok == 0) + vTaskDelay(5); + ret = Uart_Reply_Send(COMMON_UART_PROPERTY_BATCH_REPORT_CMD,send_data, send_len,HOST_TYPE_UART); + send_len=0; + memset(send_data,0,UART_REPLY_MAX_PAYLOAD_MQTT); + ESP_LOGI(MQTT_TAG,"ret %d\n",ret); + if(ret == 0) { - ret=1; break; } - send_len=0; - memset(send_dpid_data,0,UART_REPLY_MAX_PAYLOAD_MQTT); - } - send_dpid_data[send_len++] = (dpid >> 8) & 0xFF; - send_dpid_data[send_len++] = dpid & 0xFF; + // send_data[send_len++] = (dpid >> 8) & 0xFF; + // send_data[send_len++] = dpid & 0xFF; + + memcpy(&send_data[send_len],&dpid,sizeof(uint16_t)); + send_len += sizeof(uint16_t); // ESP_LOGI(MQTT_TAG,"dpid:%d send_len %d\n",dpid,send_len); } - if(ret == 1) - { - return 0; - } if(send_len > 0) { - vTaskDelay(10); - send_ok = Uart_Reply_Send(COMMON_UART_PROPERTY_BATCH_REPORT_CMD,send_dpid_data, send_len,HOST_TYPE_UART);\ - - return send_ok; - send_len=0; + vTaskDelay(5); + ret = Uart_Reply_Send(COMMON_UART_PROPERTY_BATCH_REPORT_CMD,send_data, send_len,HOST_TYPE_UART); + ESP_LOGI(MQTT_TAG,"end ret %d\n",ret); + return ret; } - return 1; - + else{ + ESP_LOGI(MQTT_TAG,"no dpid to send\n"); + return 0; + } + }else{ + return 0; } - return 1; } -//测试函数 -// data 是一个数组 ["dpid1","dpid2","dpid3"...........] +/* + * 函数功能:测试函数 + * data 是一个数组 ["dpid1","dpid2","dpid3"...........] + */ void mqtt_get_dev_test(uint16_t *dpid,uint8_t num) { @@ -1613,14 +1582,14 @@ void mqtt_get_dev_test(uint16_t *dpid,uint8_t num) /* -func:统一发送mqtt数据函数 -topic_type:是哪个topic -root:需要发送的json对象 -*/ + * 函数功能:统一发送mqtt数据函数 + * topic_type:是哪个topic + * root:需要发送的json对象 + */ uint8_t mqtt_send_publish_json(TOPIC_TYPE topic_type,cJSON *root) { if (!Mqtt_Get_Connect_Flag() || root == NULL) return 0; - const char *topic = get_publish_topic(topic_type); + char *topic = get_publish_topic(topic_type); if (topic == NULL || strlen(topic) == 0) return 0; // 转成无格式字符串(最小长度) @@ -1628,33 +1597,44 @@ uint8_t mqtt_send_publish_json(TOPIC_TYPE topic_type,cJSON *root) if (!json_str) return 0; // - mqtt_send_msg_t send_msg={0}; - memcpy(send_msg.data,json_str,strlen(json_str)); + // static char data_buf[MQTT_SEND_RECV_DATA_BUF_SIZE] = {0}; + // static mqtt_send_msg_t send_msg={0}; + + // memcpy(data_buf,json_str,strlen(json_str)); + // data_buf[strlen(json_str)]='\0'; ESP_LOGI(MQTT_TAG, "ack CJSON:%s %d topic=%s", json_str,strlen(json_str),topic); - free(json_str); - json_str = NULL; + - memcpy(send_msg.topic,topic,strlen(topic)); + // memcpy(send_msg.topic,topic,strlen(topic)); if(mqtt_dev_report_queue_handle==NULL) { return 0; } - int ret = user_esp_mqtt_client_publish(send_msg.data,strlen(send_msg.data),send_msg.topic); + int js_len = strlen(json_str); + if(js_len > MQTT_SEND_RECV_DATA_BUF_SIZE) + { + ESP_LOGI(MQTT_TAG, "json_str too long:%d\n",js_len); + free(json_str); + json_str = NULL; + return 0; + } + int ret = user_esp_mqtt_client_publish(json_str,strlen(json_str),topic); + free(json_str); + json_str = NULL; if(ret == -1) { - ESP_LOGI(MQTT_TAG, "ack faile \n"); + ESP_LOGI(MQTT_TAG, "publish faile \n"); return 0; }else{ return 1; - ESP_LOGI(MQTT_TAG, "ack OK\n"); + ESP_LOGI(MQTT_TAG, "publish OK\n"); } } - -/* -func:获取实际的json大小 -*/ +/* + * 函数功能:获取实际的json大小 + */ static uint32_t get_json_size(cJSON *root) { if (!root) return 0; @@ -1671,11 +1651,11 @@ static uint32_t get_json_size(cJSON *root) /* -func:设备应答云端公共函数 -msgId:需要区分是cloud_msg 还是dev_msgid -service_id: 传入服务ID 如 "calibration" -method: 传入方法名 如 "thing.service.calibration.invoke.reply" -*/ + * 函数功能:设备应答云端公共函数 + * msgId:需要区分是cloud_msg 还是dev_msgid + * service_id: 传入服务ID 如 "calibration" + * method: 传入方法名 如 "thing.service.calibration.invoke.reply" + */ cJSON* create_publish_ack_json(const char* msgId,const char* service_id, const char* method,uint16_t code) { cJSON *root = cJSON_CreateObject(); @@ -1714,8 +1694,8 @@ cJSON* create_publish_ack_json(const char* msgId,const char* service_id, const c return root; } /* -func:不带code message 的回复 有些需要code 有些不要 -*/ + * 函数功能:不带code message 的回复 有些需要code 有些不要 + */ cJSON* create_publish_post_json(const char* msgId,const char* service_id, const char* method) { cJSON *root = cJSON_CreateObject(); @@ -1756,8 +1736,8 @@ cJSON* create_publish_post_json(const char* msgId,const char* service_id, const } /* -pid serialcNumber和本地不一致 应答服务器 -*/ + * 函数功能:pid serialNumber和本地不一致 应答服务器 + */ cJSON* pid_serialNumber_replay_json(const char* msgId,const char* service_id, const char* method,uint16_t code) { cJSON *root = cJSON_CreateObject(); @@ -1806,8 +1786,8 @@ typedef enum{ }ACK_POST; /* -func:设备生成MSGID规则 dev_设备MAC后四位_时间戳 -*/ + *函数功能:设备生成MSGID规则 dev_设备MAC后四位_时间戳 + */ char *get_devMsgId(void) { static char msgId[64]={0}; @@ -1823,10 +1803,13 @@ char *get_devMsgId(void) } /* -设备属性主动上报 收到0x09之后串口返回的数据通过0x07接收到 发给mqtt服务器 -cmd:预留 上报不需要 -data:真实数据 -len:数据实际大小 + *函数功能:设备属性主动上报 收到0x09之后串口返回的数据通过0x07接收到 发给mqtt服务器 + *参数说明: + *cmd:预留 上报不需要 + *data:真实数据 + *len:数据实际大小 + *返回值:0 发送失败 1发送成功 + */ uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len) { @@ -1834,18 +1817,8 @@ uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len) uint8_t mode = 0; //0是使用云端生成下发的 属于应答 1 是使用设备端生成的 属于设备主动上报的 //应答 - if(strcmp(topic_compare,get_publish_topic(TOPIC_PROPERTY_SET)) == 0) //属性设置需要应答 - { - ESP_LOGI(MQTT_TAG,"TOPIC_PROPERTY_SET:"); - mode=MODE_ACK; - - } - //主动上报 - else if(strcmp(topic_compare,get_methon(METHON_EVENT_PROPERTY_POST)) == 0) - { - mode=MODE_REPORT; - } - ESP_LOGI(MQTT_TAG,"mode:%d %s",mode,topic_compare); + + mode=MODE_ACK; // { // "msgId": "dev_078E_12_1777518245123_0007", // "method": "thing.event.property.post", @@ -1862,9 +1835,6 @@ uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len) } uint16_t offset = 0; - // 循环解析所有的DPID(自动处理可变的长度) -// DF 02 01 01 00 64 E0 02 01 01 00 64 E1 02 01 01 00 64 00 - // ESP_LOGI(MQTT_TAG,"dev_report_dev_property len=%d\n",len); cJSON *root = NULL; switch (mode) { @@ -1890,11 +1860,7 @@ uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len) return 0; } cJSON_AddItemToObject(root, "params", prot_array); - // 把设备信息加到里面 - // { - // "prot":[{"key":value},{"key":value},{"key":value}] - // } - + ESP_LOGI(MQTT_TAG,"offset+5=%d, len=%d\n",offset+5,len); while (1) { if(offset+5 > len) @@ -1903,11 +1869,17 @@ uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len) break; } // 1. 取 DPID (2个字节) // - uint16_t dpid = data[offset + 0] | (data[offset + 1] << 8); + uint16_t dpid = 0; + memcpy(&dpid, &data[offset], sizeof(uint16_t)); + uint8_t data_type = data[offset+2];//type + // 2. 取数据长度(2个字节) - uint16_t data_len = data[offset + 3] | (data[offset + 4] << 8); + uint16_t data_len = 0; + memcpy(&data_len, &data[offset + 3], sizeof(uint16_t)); + + // 3. 检查剩余的长度够不够 if (offset + 5 + data_len > len || data_len > 63 || data_len == 0) { @@ -1927,7 +1899,7 @@ uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len) switch (mode) { case MODE_ACK: - mqtt_send_publish_json(TOPIC_PROPERTY_POST,root); + mqtt_send_publish_json(TOPIC_PROPERTY_ACK_POST,root); break; case MODE_REPORT: @@ -1963,6 +1935,7 @@ uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len) if(!prot_array) { cJSON_Delete(root); + root = NULL; return 0; } cJSON_AddItemToObject(root, "params", prot_array); @@ -2004,7 +1977,7 @@ uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len) switch (mode) { case MODE_ACK: - mqtt_send_publish_json(TOPIC_PROPERTY_POST,root); + mqtt_send_publish_json(TOPIC_PROPERTY_ACK_POST,root); /* code */ break; case MODE_REPORT: @@ -2023,13 +1996,14 @@ uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len) /* -func:uart发送queue给mqtt进行数据解析 -cmd: 命令保留 -data:包含key value -len:总长度 -*/ + *函数功能:uart发送queue给mqtt进行数据解析 + *cmd: 命令保留 + *data:包含key value + *len:总长度 + */ uint8_t uart_send_iot_data(uint8_t cmd,uint8_t *data,uint16_t len) { + ESP_LOGI(MQTT_TAG, "uart_send_iot_data cmd: %d\n",cmd); if(data == NULL || len == 0 || mqtt_dev_report_queue_handle == NULL) { return 0; @@ -2047,8 +2021,8 @@ uint8_t uart_send_iot_data(uint8_t cmd,uint8_t *data,uint16_t len) } /* -func:用户封装mqtt发送函数 -*/ + *函数功能:用户封装mqtt发送函数 + */ int user_esp_mqtt_client_publish(char *data,uint16_t data_len,char *topic) { if(data == NULL || topic == NULL || data_len == 0 || mqtt_client_handle == NULL) @@ -2057,12 +2031,12 @@ int user_esp_mqtt_client_publish(char *data,uint16_t data_len,char *topic) } int ret = esp_mqtt_client_publish(mqtt_client_handle,topic,\ data,data_len,0, 0 ); - ESP_LOGI(MQTT_TAG, "user_esp_mqtt_client_publish ret :%d\n",ret); + ESP_LOGI(MQTT_TAG, "esp_mqtt_client_publish ret :%d\n",ret); return ret; } /* -func:任务cb + *函数功能:任务cb */ static void Mqtt_Init_Task_Cb(void *p) { @@ -2105,8 +2079,8 @@ static void Mqtt_Init_Task_Cb(void *p) } /* -func:mqtt初始化队列 -*/ + *函数功能:mqtt初始化队列 + */ void Mqtt_Init_Task(void) { if(mqtt_init_task_flag){ diff --git a/main/user_mqtt.h b/main/user_mqtt.h index b664be9..6492f5f 100644 --- a/main/user_mqtt.h +++ b/main/user_mqtt.h @@ -20,7 +20,7 @@ typedef enum{ }METHON_MODE; typedef enum { - TOPIC_PROPERTY_POST=0, //属性下发 + TOPIC_PROPERTY_ACK_POST=0, //属性下发 TOPIC_PROPERTY_REPLY, // 属性应答 TOPIC_PROPERTY_SET, @@ -31,16 +31,7 @@ typedef enum { TOPIC_SERVICE_REPLY // 服务应答 } TOPIC_TYPE; -#define MSGID_STR_MAX 64 -#define METHON_STR_MAX 64 -#define SERVICEID_STR_MAX 64 -typedef struct { - char msgid[MSGID_STR_MAX+1]; // msgid - char methon[METHON_STR_MAX+1]; // methon - char serviceId[SERVICEID_STR_MAX+1]; // serviceId - -} mqtt_pulish_data_t; -extern mqtt_pulish_data_t publish_data; + #define MQTT_SEND_RECV_DATA_BUF_SIZE 512 #define MQTT_TOPIC "sdk/test/python" @@ -55,13 +46,24 @@ extern mqtt_pulish_data_t publish_data; #define MQTT_PUT_URL_LEN 255 +#define MSGID_STR_MAX 64 +#define METHON_STR_MAX 64 +#define SERVICEID_STR_MAX 64 +typedef struct { + char msgid[MSGID_STR_MAX+1]; // msgid + char methon[METHON_STR_MAX+1]; // methon + char serviceId[SERVICEID_STR_MAX+1]; // serviceId + +} __attribute__((packed)) mqtt_pulish_data_t; +extern mqtt_pulish_data_t publish_data; + // MQTT 接收消息结构体 typedef struct { char topic[MQTT_TOPIC_LEN+1]; // 主题 char data[MQTT_DATA_BUF_SIZE+1]; // 数据 uint8_t topic_len; // 主题长度 uint16_t data_len; // 数据长度 -} mqtt_recv_msg_t; +} __attribute__((packed)) mqtt_recv_msg_t; // 发送消息结构体 @@ -69,13 +71,13 @@ typedef struct { typedef struct { char topic[MQTT_TOPIC_LEN+1]; char data[MQTT_SEND_RECV_DATA_BUF_SIZE]; -} mqtt_send_msg_t; +} __attribute__((packed)) mqtt_send_msg_t; typedef struct { uint16_t len; uint8_t data[MQTT_SEND_RECV_DATA_BUF_SIZE]; uint8_t cmd; -} mqtt_dev_report_msg_t; +} __attribute__((packed)) mqtt_dev_report_msg_t; #define MQTT_RECV_QUEUE_ITEM_SIZE sizeof(mqtt_recv_msg_t) @@ -102,8 +104,8 @@ void Mqtt_Disconnect(void); void Mqtt_Send_Mcu_Info(void); uint8_t Mqtt_Get_Connect_Flag(void); void Mqtt_Send_Connect_Status(void);//发送mqtt连接状态给主控 -uint8_t mqtt_set_dev_property(const cJSON *array); -uint8_t mqtt_get_dev_property(const cJSON *array); +int mqtt_set_dev_property(const cJSON *array); +int mqtt_get_dev_property(const cJSON *array); uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len); uint8_t uart_send_iot_data(uint8_t cmd,uint8_t *data,uint16_t len); cJSON* create_publish_ack_json(const char* msgId,const char* service_id, const char* method,uint16_t code); diff --git a/main/wifi.h b/main/wifi.h index 2dd0b6a..1e004b6 100644 --- a/main/wifi.h +++ b/main/wifi.h @@ -19,7 +19,7 @@ extern QueueHandle_t wifi_queue_handle; extern StaticQueue_t wifi_queue_buffer; extern uint8_t wifi_queue_write[WIFI_QUEUE_ITEM_SIZE]; -#define WIFI_SCAN_TASK_STACK (2*1024) +#define WIFI_SCAN_TASK_STACK (1*1024+512) extern TaskHandle_t wifi_scan_task_handler; extern StackType_t wifi_scan_task_stack[WIFI_SCAN_TASK_STACK]; extern StaticTask_t wifi_scan_task_tcb;