From: d15521469561 <1731678236@qq.com> Date: Wed, 8 Apr 2026 11:13:08 +0000 (+0800) Subject: 完善流程还差mqtt X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=37c602b3eadb20c386859be83d50034584fd95d3;p=mirrors%2Fmqtt_wglt.git 完善流程还差mqtt --- diff --git a/main/app_main.c b/main/app_main.c index e663ead..8654998 100644 --- a/main/app_main.c +++ b/main/app_main.c @@ -24,6 +24,7 @@ static void onesec_timer_callback(void* arg) #if (IOT_ENABLE) +#if (1) const char* device_name = "PXN-W GTL"; const char* http_url = "http://192.168.66.85:8080";//"http://107.23.202.137:8080";//"http://192.168.66.80:8080";//http://107.23.202.137:8080 const char* http_token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImJhZWRiZjUyLTUzMDgtNDRlOS04MWI3LTE1MmU4NGYwYzk5ZCJ9.fyxIGmmYD-pKd7VQ02DrL6FwBKVkqXoIu4iKSVMOUj8WubDvuSdbX6j8YzQw8K_t6UTRVZsRDTlO5me2KKhs7A";//"eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjRhNjBlZDdiLTExMDUtNDU5Ny1iNDBhLTg5ZDRiMzMzNTIyYSJ9.CUxkgU2F6BoxzE3HHVdXAin2OEDD1-D_iI0zh7VWTnlNsjsHHulvZVMq2hQLXF4cAQIJpLWwiMZJQO6Bvy9vwg"; @@ -33,7 +34,7 @@ const char* wifi_pwd = "XiaoMing1919810"; const uint16_t mcu_vid = 0x36E6; const uint16_t mcu_pid = 0xA00B; - +#endif #endif void Main_Loop_Task(void *pvParameters) @@ -77,7 +78,7 @@ void Main_Loop_Task(void *pvParameters) common_setting.mcu_info_got_flag = 1; common_setting.net_pairing_info.serial_number_flag =1;//等同于device_id // Ble_Queue_Send(0xFF, 0x01); - // xEventGroupSetBits(ble_event_group, BLE_ON_BIT); + xEventGroupSetBits(ble_event_group, BLE_ON_BIT); memset(common_setting.net_pairing_info.http_url, 0x00, sizeof(common_setting.net_pairing_info.http_url)); memset(common_setting.net_pairing_info.http_token, 0x00, sizeof(common_setting.net_pairing_info.http_token)); @@ -96,7 +97,7 @@ void Main_Loop_Task(void *pvParameters) memcpy(common_setting.wifi_status.cur_pwd, wifi_pwd, strlen(wifi_pwd)); common_setting.wifi_status.cur_ssid_len = strlen(wifi_ssid); common_setting.wifi_status.cur_pwd_len = strlen(wifi_pwd); - // Wifi_Queue_Send(0, 0x02, 0); + Wifi_Queue_Send(0, 0x02, 0); #endif #endif for(;;) diff --git a/main/ble.c b/main/ble.c index b320fd8..7e90ed8 100644 --- a/main/ble.c +++ b/main/ble.c @@ -122,18 +122,38 @@ void Ble_Enable_Status_Check(void) { if(common_setting.mcu_info_got_flag) { + //无serial_number wifi info 并且蓝牙关闭状态开启蓝牙配网功能 if(common_storage.net_info.serial_number_flag == 0 && common_storage.net_info.wifi_info_flag == 0 && common_setting.ble_status.ble_state == 0) { // Ble_Queue_Send(0xFF, 0x01); xEventGroupSetBits(ble_event_group, BLE_ON_BIT); - ESP_LOGI(BLE_TAG, "[APP] Free memory device_id_flag1 on: %" PRIu32 " bytes", esp_get_free_heap_size()); + ESP_LOGI(BLE_TAG, "[APP] Free memory no serial_number on: %" PRIu32 " bytes", esp_get_free_heap_size()); } - else if(common_storage.net_info.serial_number_flag && common_storage.net_info.wifi_info_flag && common_setting.ble_status.ble_state != 0) - { - // Ble_Queue_Send(0xFF, 0xFF); - xEventGroupSetBits(ble_event_group, BLE_OFF_BIT); - ESP_LOGI(BLE_TAG, "[APP] Free memory device_id_flag2 on: %" PRIu32 " bytes", esp_get_free_heap_size()); + else if(common_storage.net_info.serial_number_flag == 1 && common_storage.net_info.wifi_info_flag == 1){ + //有serial_number wifi信息 + ESP_LOGI(BLE_TAG, "[APP] Free memory have serial_number on: %" PRIu32 " bytes", esp_get_free_heap_size()); + //还要查询是否有订阅地址和证书 没有订阅地址 或者 没有密码 没有wifi名称 没有证书长度 + if(common_storage.net_info.mqtt_url_len != 0 || common_storage.net_info.wifi_ssid_len != 0 || common_storage.net_info.wifi_pwd_len != 0 || \ + common_storage.ssl_cert[SSL_CLIENT_CRT].length != 0 || common_storage.ssl_cert[SSL_CLIENT_KEY].length != 0 || common_storage.ssl_cert[SSL_SERVER_CRT].length != 0) + { + ESP_LOGI(BLE_TAG, "[APP] Free memory have mqtt_sub_url on: %d %d %d %d %d %d\n", common_storage.net_info.mqtt_url_len,common_storage.net_info.wifi_ssid_len,\ + common_storage.net_info.wifi_pwd_len,common_storage.ssl_cert[SSL_SERVER_CRT].length,common_storage.ssl_cert[SSL_CLIENT_KEY].length,\ + common_storage.ssl_cert[SSL_CLIENT_CRT].length); + //直连mqtts + xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT); + }else + { + xEventGroupSetBits(ble_event_group, BLE_ON_BIT);//开启配网模式 + ESP_LOGI(BLE_TAG, "[APP] Free memory have not mqtt_sub_url on: on: %" PRIu32 " bytes", esp_get_free_heap_size()); + } } + //只有配网后设备主动关闭蓝牙配网 或者开始配网之后30分钟后没有进行配网则关闭 其他条件无需关闭配网 + // else if(common_storage.net_info.serial_number_flag && common_storage.net_info.wifi_info_flag && common_setting.ble_status.ble_state != 0) + // { + // // Ble_Queue_Send(0xFF, 0xFF); + // xEventGroupSetBits(ble_event_group, BLE_OFF_BIT); + // ESP_LOGI(BLE_TAG, "[APP] Free memory device_id_flag2 on: %" PRIu32 " bytes", esp_get_free_heap_size()); + // } } // static uint8_t ble_queue_read[BLE_QUEUE_ITEM_SIZE] = {0}; // byte0:0:串口 1:蓝牙 0xFF:本地 byte1:1:打开BLE 0xFF:关闭 @@ -164,11 +184,6 @@ void Ble_Enable_Status_Check(void) xEventGroupClearBits(ble_event_group, BLE_OFF_BIT); if(common_setting.ble_status.ble_state > 0) { - #if (IS_CLOSE_BLE) - common_setting.ble_status.ble_state = 0; - common_setting.ble_enable_tick = 0; - stop_advertising();//停止广播 - #endif // 停止配网 int ret = nimble_port_stop(); if (ret == 0) @@ -178,10 +193,10 @@ void Ble_Enable_Status_Check(void) vTaskDelete(ble_task_handler); } nimble_port_deinit(); - common_setting.ble_status.ble_state = 0; common_setting.ble_enable_tick = 0; - + + // xEventGroupSetBits(ble_event_group, BLE_ON_BIT); ESP_LOGI(BLE_TAG, "nimble_port_stop\n"); } else diff --git a/main/crc.c b/main/crc.c index 217cc0b..b929ba2 100644 --- a/main/crc.c +++ b/main/crc.c @@ -285,5 +285,13 @@ uint32_t Crc32_JAMCRC_Cal(uint8_t* data, uint32_t len) } return crc32; } +uint32_t Crc32_JAMCRC_Cal_Continue(uint32_t crc, const uint8_t *data, uint32_t len) +{ + for (uint32_t i = 0; i < len; i++) + { + crc = (crc >> 8) ^ crc32_table[(crc ^ data[i]) & 0xFF]; + } + return crc; +} #endif diff --git a/main/crc.h b/main/crc.h index 83bcebe..9d4fdfe 100644 --- a/main/crc.h +++ b/main/crc.h @@ -61,5 +61,5 @@ void CRC32_Update(CRC32_CTX_T *ctx, uint8_t* data, uint32_t len); uint32_t CRC32_Final(CRC32_CTX_T *ctx); uint32_t Crc32_JAMCRC_Cal(uint8_t* data, uint32_t len); - +uint32_t Crc32_JAMCRC_Cal_Continue(uint32_t crc, const uint8_t *data, uint32_t len); #endif diff --git a/main/gap.c b/main/gap.c index 7ba57a0..e9d6df3 100644 --- a/main/gap.c +++ b/main/gap.c @@ -58,6 +58,7 @@ static void print_conn_desc(struct ble_gap_conn_desc *desc) { void start_advertising(void) { + /* Local variables */ int rc = 0; // const char *name; @@ -75,8 +76,15 @@ void start_advertising(void) *(uint16_t*)&adv_vendor_data[2] = common_setting.mcu_info.vid; adv_vendor_len+=2; *(uint16_t*)&adv_vendor_data[4] = common_setting.mcu_info.pid; - adv_vendor_len=adv_vendor_len+common_setting.mcu_info.product_name_len; - memcpy(&adv_vendor_data[6], common_setting.mcu_info.product_name, common_setting.mcu_info.product_name_len); + + uint8_t product_name_len = 0; + product_name_len = common_setting.mcu_info.product_name_len; + if(common_setting.mcu_info.product_name_len > PRODUCT_NAME_MAX_LEN) + { + product_name_len =PRODUCT_NAME_MAX_LEN; + } + adv_vendor_len=adv_vendor_len+product_name_len; + memcpy(&adv_vendor_data[6], common_setting.mcu_info.product_name, product_name_len); adv_fields.mfg_data = adv_vendor_data; ESP_LOGE(GAP_TAG, "advertising data,len: %d", adv_vendor_len); @@ -246,7 +254,7 @@ static int gap_event_handler(struct ble_gap_event *event, void *arg) { /* Disconnect event */ case BLE_GAP_EVENT_DISCONNECT: /* A connection was terminated, print connection descriptor */ - ESP_LOGI(GAP_TAG, "disconnected from peer; reason=%d", + ESP_LOGI(GAP_TAG, " reason=%d", event->disconnect.reason); /* Restart advertising if allowed */ diff --git a/main/gatt_svc.c b/main/gatt_svc.c index e0dfb56..29b8384 100644 --- a/main/gatt_svc.c +++ b/main/gatt_svc.c @@ -107,6 +107,15 @@ static int gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle, printf("%02x ", ctxt->om->om_data[i]); } printf("\n"); + + // memset(&ble_notify,0x00,sizeof(BLE_MESSAGE_T)); + // if(ctxt->om->om_len <= sizeof(ble_notify.data)) + // { + // ble_notify.length = ctxt->om->om_len; + // memcpy(ble_notify.data, ctxt->om->om_data, ctxt->om->om_len); + // // xQueueSend(ble_notify_queue_handle, &ble_message_temp, 0); // 回传测试 + // xQueueSend(ble_write_queue_handle, &ble_notify, 0); + // } static BLE_MESSAGE_T ble_message_temp = {0}; memset(&ble_message_temp,0x00,sizeof(BLE_MESSAGE_T)); if(ctxt->om->om_len <= sizeof(ble_message_temp.data)) @@ -116,14 +125,6 @@ static int gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle, // xQueueSend(ble_notify_queue_handle, &ble_message_temp, 0); // 回传测试 xQueueSend(ble_write_queue_handle, &ble_message_temp, 0); } - // memset(&ble_notify,0x00,sizeof(BLE_MESSAGE_T)); - // if(ctxt->om->om_len <= sizeof(ble_notify.data)) - // { - // ble_notify.length = ctxt->om->om_len; - // memcpy(ble_notify.data, ctxt->om->om_data, ctxt->om->om_len); - // // xQueueSend(ble_notify_queue_handle, &ble_message_temp, 0); // 回传测试 - // xQueueSend(ble_write_queue_handle, &ble_notify, 0); - // } return rc; } goto unknown; diff --git a/main/http.c b/main/http.c index 6d95ee2..071307b 100644 --- a/main/http.c +++ b/main/http.c @@ -42,8 +42,10 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt) } return ESP_OK; } - +#define HTTP_DOWNLOAD_CER_FUNC_ENABLE 0 +#if HTTP_DOWNLOAD_CER_FUNC_ENABLE static char http_download_cache[sizeof(common_storage.ssl_cert[0].content)] = {0}; +#endif // esp_err_t http_get_cert(uint8_t cert_type, esp_http_client_handle_t http_client) // { // esp_err_t err = 0; @@ -118,8 +120,19 @@ static char http_download_cache[sizeof(common_storage.ssl_cert[0].content)] = {0 // return err; // } - +#define HTTP_RETRY_NUMBER_MAX (5) //HTTP请求失败重试次数最大值 +#define HTTP_GET 0 +#define HTTP_POST 0 +#define HTTP_GET_MQTT_URL_CER_ENABLE 1 //设备请求下发mqtt地址 证书 +#define HTTP_GET_DEVICEID_ENBALE 0 //设备上报serialNumber 服务器返回deviceID等信息 本产品暂时不用deviceID,所以这个功能先关闭,后续如果需要可以打开 +#define HTTP_PUT_REPORT_MAC_RSSI_ENABLE 1 //PUT请求上报mac和rssi信息 +#define HTTP_GET_SERIAL_NUMBER_ENBALE 1 //GET请求获取serialNumber,服务器根据设备token mac pid等信息返回对应的serialNumber=deviceID等信息 +#define HTTP_GET_URL_MAX_SIZE NET_PAIRING_INFO_MAX_LEN+128 +char output_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; +#define DOWNLOAD_BLOCK_SIZE 512 HTTP_GET_CERT_TASK_PARAM_T http_get_cert_task_param; +static const char *HTTP_DOWNLOAD_TASK_TAG = "http_download_task"; + void http_download_task(void *pvParameters) { uint8_t success_bit = 0; @@ -139,24 +152,24 @@ void http_download_task(void *pvParameters) // .disable_auto_redirect = true, }; // mbedtls_ssl_conf_authmode(&ssl_config, MBEDTLS_SSL_VERIFY_NONE); - ESP_LOGI(HTTP_TAG, "downloading %d", cert_type); + ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "downloading %d", cert_type); esp_err_t err = ESP_FAIL; esp_http_client_handle_t http_client = NULL; // 提前定义 - for(uint8_t retry=0;retry<3;retry++) + for(uint8_t retry=0;retry 0 && total_size <= (sizeof(common_storage.ssl_cert[cert_type].content))) { - ESP_LOGI(HTTP_TAG, "esp_http_client_get_status_code"); - int file_size = esp_http_client_get_content_length(http_client); - if(file_size <= (sizeof(common_storage.ssl_cert[0].content))) + ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "esp_http_client_get_status_code"); + + if(total_size <= (sizeof(common_storage.ssl_cert[cert_type].content))) { + #if (!HTTP_DOWNLOAD_CER_FUNC_ENABLE) + int total_read = 0; + uint8_t flash_buf[DOWNLOAD_BLOCK_SIZE] = {0}; + uint8_t download_ok = 1; + + // 先清空对应证书存储区 + memset(common_storage.ssl_cert[cert_type].content, 0, sizeof(common_storage.ssl_cert[cert_type].content)); + common_storage.ssl_cert[cert_type].length = total_size; + + memset(flash_buf,0,512); + + // 0 890 + // 512 + // 512+ + uint32_t crc_download =0xffffffff; + while (total_read < total_size) //0 + { + + // 每次读 512 字节 + int read_len = esp_http_client_read(http_client, (char*)flash_buf, DOWNLOAD_BLOCK_SIZE); + + if (read_len <= 0) + { + download_ok = false; + break; + } + + crc_download =Crc32_JAMCRC_Cal_Continue(crc_download,flash_buf,read_len);//读出来的数据 和实际读出来的size_len计算crc32校验 + // 直接写入 RAM 结构体(分段写) + printf(HTTP_DOWNLOAD_TASK_TAG, "flash_buf %s crc_download:%08X",flash_buf,crc_download); + memcpy(common_storage.ssl_cert[cert_type].content + total_read, flash_buf, read_len); + + total_read += read_len; + + // 防止溢出 + if (total_read >= sizeof(common_storage.ssl_cert[cert_type].content)) + break; + } + + // 下载完成校验 + if (download_ok && total_read == total_size) + { + //写完之后校验 crc + uint32_t crc_value =0xffffffff; + + crc_value =Crc32_JAMCRC_Cal_Continue(crc_value,common_storage.ssl_cert[cert_type].content,total_size); + ESP_LOGI(HTTP_TAG, "Cert %d download success, size=%d", cert_type, total_read); + if(crc_value == crc_download) + { + if(common_storage.net_info.wifi_pwd_len != 0 && common_storage.net_info.wifi_ssid_len != 0) + { + if(User_Flash_Write_With_Flag(UF_OFFSET_USER_CONFIG_INFO_ADDR, UF_OFFSET_USER_CONFIG_INFO_FLAG, &common_storage.net_info, sizeof(NET_INFO_T)) == ESP_OK) + { + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write wifi pwd ssid success: \n"); + // 写入 Flash(一次性写整个证书,因为已经在 RAM 里了) + if (User_Flash_Write_With_Flag( + UF_OFFSET_CERT_CLIENT_CRT_ADDR + FLASH_SEC_SIZE * cert_type, + UF_OFFSET_CERT_CERT_FLAG + cert_type, + &common_storage.ssl_cert[cert_type], + sizeof(SSL_CERT_T)) == ESP_OK) + { + err = ESP_OK; + success_bit |= (1 << cert_type); + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write cer success: %d\n",common_storage.ssl_cert[cert_type].length); + if(User_Flash_Read_Check(UF_OFFSET_CERT_CLIENT_CRT_ADDR, UF_OFFSET_CERT_CERT_FLAG+cert_type, &common_storage.ssl_cert[cert_type], sizeof(SSL_CERT_T)) != ESP_OK) + { + common_storage.ssl_cert[cert_type].length = 0; + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG,"read cer failed"); + }else{ + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG,"read client_ssl_cer success"); + } + } + else + { + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write cer failed:%d\n",cert_type); + err = ESP_FAIL; + } + } + else{ + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write wifi pwd ssid failed: \n"); + err = ESP_FAIL; + } + } + else{ + ESP_LOGI(HTTP_TAG, "device have not ssid or pwd\n"); + } + }else{ + ESP_LOGI(HTTP_TAG, "Cert %d download failed, size=%d", cert_type, total_read); + err = ESP_FAIL; + } + + } + else + { + err = ESP_FAIL; + } + #endif + #if HTTP_DOWNLOAD_CER_FUNC_ENABLE int read_len = 0; memset(http_download_cache, 0x00, sizeof(http_download_cache)); - read_len = esp_http_client_read(http_client, http_download_cache, file_size); - if(read_len == file_size) + read_len = esp_http_client_read(http_client, http_download_cache, total_size); + if(read_len == total_size) { // 成功 - ESP_LOGI(HTTP_TAG, "\nrecv data len%d: %s\n", file_size, http_download_cache); - if(common_storage.ssl_cert[cert_type].length == file_size && memcmp(http_download_cache, common_storage.ssl_cert[cert_type].content, file_size) == 0) + + ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "\nrecv data len%d: %s\n", total_size, http_download_cache); + + if(common_storage.ssl_cert[cert_type].length == total_size && memcmp(http_download_cache, common_storage.ssl_cert[cert_type].content, total_size) == 0) { - ESP_LOGI(HTTP_TAG, "cert same: %d", cert_type); + ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "cert same: %d", cert_type); err = ESP_OK; success_bit |= (1 << cert_type); } else{ - common_storage.ssl_cert[cert_type].length = file_size; + common_storage.ssl_cert[cert_type].length = total_size; memset(common_storage.ssl_cert[cert_type].content, 0x00, sizeof(common_storage.ssl_cert[0].content)); - memcpy(common_storage.ssl_cert[cert_type].content, http_download_cache, file_size); + memcpy(common_storage.ssl_cert[cert_type].content, http_download_cache, total_size); if(common_storage.net_info.wifi_pwd_len != 0 && common_storage.net_info.wifi_ssid_len != 0) { - if(User_Flash_Write_With_Flag(UF_OFFSET_USER_CONFIG_INFO_ADDR, UF_OFFSET_USER_CONFIG_INFO_FLAG, &common_storage.net_info.wifi_ssid, sizeof(NET_INFO_T)) == ESP_OK) + if(User_Flash_Write_With_Flag(UF_OFFSET_USER_CONFIG_INFO_ADDR, UF_OFFSET_USER_CONFIG_INFO_FLAG, &common_storage.net_info, sizeof(NET_INFO_T)) == ESP_OK) { - err = ESP_OK; - ESP_LOGE(HTTP_TAG, "flash write wifi ssid success: \n"); - } - else - { - ESP_LOGE(HTTP_TAG, "flash write wifi ssid failed: \n"); - err = ESP_FAIL; - - } - - if(User_Flash_Write_With_Flag(UF_OFFSET_USER_CONFIG_INFO_ADDR, UF_OFFSET_USER_CONFIG_INFO_FLAG, &common_storage.net_info.wifi_pwd, sizeof(NET_INFO_T)) == ESP_OK) - { - err = ESP_OK; - ESP_LOGE(HTTP_TAG, "flash write wifi pwd success: \n"); + // + if(User_Flash_Write_With_Flag(UF_OFFSET_CERT_CLIENT_CRT_ADDR+FLASH_SEC_SIZE*cert_type, UF_OFFSET_CERT_CERT_FLAG, &common_storage.ssl_cert[cert_type], sizeof(SSL_CERT_T)) == ESP_OK) + { + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write cer key server_crt success: %d\n", cert_type); + err = ESP_OK; + } + else + { + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write cer key server_crt error: %d\n", cert_type); + err = ESP_FAIL; + } + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write wifi pwd ssid success: \n"); } else { - ESP_LOGE(HTTP_TAG, "flash write wifi pwd failed: \n"); + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write wifi pwd ssid failed: \n"); err = ESP_FAIL; - } } - if(User_Flash_Write_With_Flag(UF_OFFSET_CERT_CLIENT_CRT_ADDR+FLASH_SEC_SIZE*cert_type, UF_OFFSET_CERT_CERT_FLAG, &common_storage.ssl_cert[cert_type], sizeof(SSL_CERT_T)) == ESP_OK) - { - err = ESP_OK; - } - else - { - ESP_LOGE(HTTP_TAG, "flash write error: %d\n", cert_type); - err = ESP_FAIL; - } + } } else { - ESP_LOGE(HTTP_TAG, "http read size low = %d", read_len); + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "http read size low = %d", read_len); err = ESP_FAIL; } + #endif } - else - { - ESP_LOGE(HTTP_TAG, "file_size too large"); + else{ err = ESP_FAIL; + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "http filed size too large\n"); } } else { - ESP_LOGE(HTTP_TAG, "http req failed"); + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "http req failed"); err = ESP_FAIL; } @@ -265,7 +368,7 @@ void http_download_task(void *pvParameters) if(err != ESP_OK) { - ESP_LOGI(HTTP_TAG, "download fail %d", cert_type); + ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "download fail %d", cert_type); break; } vTaskDelay(50 / portTICK_PERIOD_MS); @@ -276,23 +379,19 @@ void http_download_task(void *pvParameters) { //成功 // Ble_Queue_Send(0xFF, 0xFF); + #if (USER_TEST_ENABLE) //开机直连测试使用 + common_storage.net_info.serial_number_flag = 1; + common_storage.net_info.wifi_info_flag = 1; + #endif xEventGroupSetBits(ble_event_group, BLE_OFF_BIT);//关闭蓝牙配网 ESP_LOGI(HTTP_TAG, "download clientcer clientkey mqtt \n"); - xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);// + xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT); } vTaskDelete(NULL); return; } -#define HTTP_RETRY_NUMBER_MAX (3) //HTTP请求失败重试次数最大值 -#define HTTP_GET 0 -#define HTTP_POST 0 -#define HTTP_GET_MQTT_URL_CER_ENABLE 1 //设备请求下发mqtt地址 证书 -#define HTTP_GET_DEVICEID_ENBALE 0 //设备上报serialNumber 服务器返回deviceID等信息 本产品暂时不用deviceID,所以这个功能先关闭,后续如果需要可以打开 -#define HTTP_PUT_REPORT_MAC_RSSI_ENABLE 1 //PUT请求上报mac和rssi信息 -#define HTTP_GET_SERIAL_NUMBER_ENBALE 1 //GET请求获取serialNumber,服务器根据设备token mac pid等信息返回对应的serialNumber=deviceID等信息 -#define HTTP_GET_URL_MAX_SIZE NET_PAIRING_INFO_MAX_LEN+128 -char output_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; + void http_get_url_task(void *pvParameters) { if(common_setting.net_pairing_info.http_url_flag == 0 || common_setting.net_pairing_info.http_token_flag == 0 || common_setting.net_pairing_info.user_id_flag == 0 || common_setting.mcu_info_got_flag == 0 || common_setting.net_pairing_info.http_url_length == 0 || common_setting.net_pairing_info.http_token_length == 0 || common_setting.net_pairing_info.http_url_length > NET_PAIRING_INFO_MAX_LEN || common_setting.net_pairing_info.http_token_length > NET_PAIRING_INFO_MAX_LEN) @@ -317,7 +416,7 @@ void http_get_url_task(void *pvParameters) // Get(Send:PID/Get:serialNumber) deviceID ESP_LOGI(HTTP_TAG, "\nHTTP STEP 1.......GET serialNumber=deviceID.................\n"); esp_http_client_handle_t http_client = NULL; - for(uint8_t retry=0;retry<3;retry++) + for(uint8_t retry=0;retryvaluestring, strlen((const char*)pClientkey->valuestring)); memcpy(&http_get_cert_task_param.url[SSL_SERVER_CRT][0], pServerCrt->valuestring, strlen((const char*)pServerCrt->valuestring)); + uint16_t mqtt_url_len = strlen((const char*)pMqttSubUrl->valuestring); + if(mqtt_url_len > MQTT_SUB_URL_MAX_LEN) + { + mqtt_url_len=MQTT_SUB_URL_MAX_LEN; + } + memset(common_setting.net_pairing_info.mqtt_sub_url, 0x00, sizeof(common_setting.net_pairing_info.mqtt_sub_url)); - memcpy(common_setting.net_pairing_info.mqtt_sub_url, pMqttSubUrl->valuestring, strlen((const char*)pMqttSubUrl->valuestring)); - common_setting.net_pairing_info.mqtt_sub_url_length = strlen((const char*)pMqttSubUrl->valuestring); + memcpy(common_setting.net_pairing_info.mqtt_sub_url, pMqttSubUrl->valuestring, mqtt_url_len); + common_setting.net_pairing_info.mqtt_sub_url_length = mqtt_url_len; common_setting.net_pairing_info.mqtt_sub_url_flag = 1; + + + memset(common_storage.net_info.mqtt_sub_url, 0x00, sizeof(common_storage.net_info.mqtt_sub_url)); + memcpy(common_storage.net_info.mqtt_sub_url, pMqttSubUrl->valuestring,mqtt_url_len); + common_storage.net_info.mqtt_url_len=mqtt_url_len; + //保存mqtt 地址 + //UF_OFFSET_USER_CONFIG_INFO_ADDR, UF_OFFSET_USER_CONFIG_INFO_FLAG, &common_storage.net_info, sizeof(NET_INFO_T)) != ESP_OK + if(User_Flash_Write_With_Flag(UF_OFFSET_USER_CONFIG_INFO_ADDR, UF_OFFSET_USER_CONFIG_INFO_FLAG, &common_storage.net_info, sizeof(NET_INFO_T)) == ESP_OK) + { + ESP_LOGE(HTTP_TAG, "flash write mqtt_sub_url success: %s\n",common_storage.net_info.mqtt_sub_url); + err = ESP_OK; + } + else + { + ESP_LOGE(HTTP_TAG, "flash write mqtt_sub_url fail: %s\n",common_storage.net_info.mqtt_sub_url); + err = ESP_FAIL; + } + } else { diff --git a/main/mqtt.c b/main/mqtt.c index d6b2e9a..b3e8f4c 100644 --- a/main/mqtt.c +++ b/main/mqtt.c @@ -116,7 +116,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ break; } } - +static uint8_t esp_mqtt_init_flag = 0; static esp_err_t Mqtt_App_Init(void) { esp_err_t err = ESP_FAIL; @@ -153,7 +153,12 @@ static esp_err_t Mqtt_App_Init(void) // .client_id = "basicPubSub", // } // }; - + if(esp_mqtt_init_flag) + { + // + ESP_LOGI(MQTT_TAG, "Mqtt init has been initialized.\n"); + return ESP_OK; + } retry_connect_cnt = 0; // common_setting.mqtt_cfg.session.keepalive = 60; common_setting.mqtt_cfg.network.disable_auto_reconnect = false; @@ -177,6 +182,10 @@ static esp_err_t Mqtt_App_Init(void) if(err == ESP_OK) { err = esp_mqtt_client_start(common_setting.mqtt_client_handle); + if(err == ESP_OK) + { + esp_mqtt_init_flag=1; + } } return err; } diff --git a/main/project_common.c b/main/project_common.c index aa7cbed..30ee598 100644 --- a/main/project_common.c +++ b/main/project_common.c @@ -656,6 +656,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) else { uart_pairing_reply.content[5] |= 0x20; + common_setting.net_pairing_info.http_url_flag=0; ESP_LOGI(COMMON_TAG, "%s http_url crc16 err:%04x %04x\n",__func__, Crc16_Modbus_Cal((uint8_t*)common_setting.net_pairing_info.http_url, total_len), *(uint16_t*)&data[6]); } } @@ -678,6 +679,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) else { // 校验失败 + common_setting.net_pairing_info.http_token_flag=0; uart_pairing_reply.content[5] |= 0x20; ESP_LOGI(COMMON_TAG, "%s http_token crc16 err:%04x %04x\n",__func__, Crc16_Modbus_Cal((uint8_t*)common_setting.net_pairing_info.http_token, total_len), *(uint16_t*)&data[6]); } @@ -699,6 +701,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) } else { + common_setting.net_pairing_info.user_id_flag=0; // 校验失败 uart_pairing_reply.content[5] |= 0x20; ESP_LOGI(COMMON_TAG, "%s user id crc16 err:%04x %04x\n",__func__, Crc16_Modbus_Cal((uint8_t*)&common_setting.net_pairing_info.user_id, total_len), *(uint16_t*)&data[6]); @@ -1248,7 +1251,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) uint16_t calc_crc = Crc16_Modbus_Cal((uint8_t*)common_setting.net_pairing_info.http_url, total_len); uint16_t packet_crc = (data[7] << 8) | data[8]; - ESP_LOGI(COMMON_TAG, "%s 正确 http url: %s (len=%u)\n",__func__, common_setting.net_pairing_info.http_url, total_len); + ESP_LOGI(COMMON_TAG, "%s http url: %s (len=%u)\n",__func__, common_setting.net_pairing_info.http_url, total_len); //本次结束接收后数据校验出错了,然后又下发最后一包不完整的数据,结束了上一次的完整包 但是后面又下发了最后一包数据不完整 if(http_end == 1 || http_end_flag == 0) @@ -1442,12 +1445,12 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) case 0x21: { if (len < 5) break; - if(data[4] == 1) // 开启蓝牙 + if(data[4] == 1) // 开启蓝牙配网 { xEventGroupSetBits(ble_event_group, BLE_ON_BIT); ESP_LOGI(COMMON_TAG, "%s ble on by app\n",__func__); } - else if(data[4] == 0) // 关闭蓝牙 + else if(data[4] == 0) // 关闭蓝牙配网 { xEventGroupSetBits(ble_event_group, BLE_OFF_BIT); ESP_LOGI(COMMON_TAG, "%s ble off by app\n",__func__); diff --git a/main/project_common.h b/main/project_common.h index 9476d8c..6f7a923 100644 --- a/main/project_common.h +++ b/main/project_common.h @@ -59,7 +59,7 @@ #define FW_HEADER_CODE_SIZE 12 #define FW_HEADER_CODE "WGTL_ESP32C2" #define IOT_ENABLE 1 -#define USER_TEST_ENABLE 0 +#define USER_TEST_ENABLE 1 #define BLE_TEST_WIFI_CONNECT_ENABLE 1//BLE下发wifixin不需要做htttp token user_id校验 #define IS_CLOSE_BLE 0 //是否关闭蓝牙广播暂时测试不关闭 一直广播 @@ -73,7 +73,7 @@ extern StackType_t main_loop_task_stack[MAIN_LOOP_TASK_STACK]; extern StaticTask_t main_loop_task_tcb; // UART -#define UART_TASK_STACK (2*512) +#define UART_TASK_STACK (2*1024) extern TaskHandle_t uart_task_handler; extern StackType_t uart_task_stack[UART_TASK_STACK]; extern StaticTask_t uart_task_tcb; @@ -83,7 +83,7 @@ extern StaticTask_t uart_task_tcb; extern TaskHandle_t nimble_task_handler; extern StackType_t nimble_task_stack[NIMBLE_TASK_STACK]; extern StaticTask_t nimble_task_tcb; -#define BLE_TASK_STACK (1*1024) +#define BLE_TASK_STACK (2*1024) extern TaskHandle_t ble_task_handler; extern StackType_t ble_task_stack[BLE_TASK_STACK]; extern StaticTask_t ble_task_tcb; @@ -106,7 +106,7 @@ extern StaticTask_t wifi_scan_task_tcb; #define NET_PAIRING_INFO_MAX_LEN 256 #define SERIAL_NUMBER_MAX_LEN 32 - +#define MQTT_SUB_URL_MAX_LEN 128 // WIFI #if CONFIG_ESP_STATION_EXAMPLE_WPA3_SAE_PWE_HUNT_AND_PECK #define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_HUNT_AND_PECK @@ -163,15 +163,15 @@ typedef enum SSL_CLIENT_KEY, SSL_SERVER_CRT, }CERT_TYPE_E; - +#define SSL_CERT_BUFFER_SIZE 2560 typedef union { struct { uint16_t length; - uint8_t content[2560-2]; + uint8_t content[SSL_CERT_BUFFER_SIZE-2]; }; - uint8_t buffer[2560]; + uint8_t buffer[SSL_CERT_BUFFER_SIZE]; }__attribute__((packed)) SSL_CERT_T; typedef union @@ -191,6 +191,8 @@ typedef union uint8_t serial_number[SERIAL_NUMBER_MAX_LEN+1]; uint8_t device_id_len; uint8_t device_id[32]; + uint8_t mqtt_url_len; + char mqtt_sub_url[MQTT_SUB_URL_MAX_LEN+1]; }; }__attribute__((packed)) NET_INFO_T; @@ -275,7 +277,7 @@ typedef struct char http_url[NET_PAIRING_INFO_MAX_LEN+1]; char http_token[NET_PAIRING_INFO_MAX_LEN+1]; uint32_t user_id; - char mqtt_sub_url[128]; + char mqtt_sub_url[MQTT_SUB_URL_MAX_LEN+1]; }__attribute__((packed)) NET_PAIR_INFO_T; typedef struct diff --git a/main/user_flash.c b/main/user_flash.c index 5cb6ef9..40281f7 100644 --- a/main/user_flash.c +++ b/main/user_flash.c @@ -23,18 +23,19 @@ void User_Flash_Check(void) common_storage.net_info.device_id_flag = 0; common_storage.net_info.serial_number_flag=0; + }else{ printf("%s read serialNumber %s\n",__func__,common_storage.net_info.serial_number); } - if(User_Flash_Read_Check(UF_OFFSET_CERT_CLIENT_CRT_ADDR, UF_OFFSET_CERT_CERT_FLAG, &common_storage.ssl_cert[SSL_CLIENT_CRT], sizeof(SSL_CERT_T)) != ESP_OK) + if(User_Flash_Read_Check(UF_OFFSET_CERT_CLIENT_CRT_ADDR, UF_OFFSET_CERT_CERT_FLAG+SSL_CLIENT_CRT, &common_storage.ssl_cert[SSL_CLIENT_CRT], sizeof(SSL_CERT_T)) != ESP_OK) { common_storage.ssl_cert[SSL_CLIENT_CRT].length = 0; printf("%s read client_ssl_cer failed\n",__func__); }else{ printf("%s read client_ssl_cer success %d\n",__func__,common_storage.ssl_cert[SSL_CLIENT_CRT].length); } - if(User_Flash_Read_Check(UF_OFFSET_CERT_CLIENT_KEY_ADDR, UF_OFFSET_CERT_CERT_FLAG, &common_storage.ssl_cert[SSL_CLIENT_KEY], sizeof(SSL_CERT_T)) != ESP_OK) + if(User_Flash_Read_Check(UF_OFFSET_CERT_CLIENT_KEY_ADDR, UF_OFFSET_CERT_CERT_FLAG+SSL_CLIENT_KEY, &common_storage.ssl_cert[SSL_CLIENT_KEY], sizeof(SSL_CERT_T)) != ESP_OK) { common_storage.ssl_cert[SSL_CLIENT_KEY].length = 0; printf("%s read client_ssl_cer_key failed\n",__func__); @@ -42,7 +43,7 @@ void User_Flash_Check(void) else{ printf("%s read client_ssl_cer success %d\n",__func__,common_storage.ssl_cert[SSL_CLIENT_KEY].length); } - if(User_Flash_Read_Check(UF_OFFSET_CERT_SERVER_CRT_ADDR, UF_OFFSET_CERT_CERT_FLAG, &common_storage.ssl_cert[SSL_SERVER_CRT], sizeof(SSL_CERT_T)) != ESP_OK) + if(User_Flash_Read_Check(UF_OFFSET_CERT_SERVER_CRT_ADDR, UF_OFFSET_CERT_CERT_FLAG+SSL_CLIENT_KEY, &common_storage.ssl_cert[SSL_SERVER_CRT], sizeof(SSL_CERT_T)) != ESP_OK) { common_storage.ssl_cert[SSL_SERVER_CRT].length = 0; printf("%s read server_ssl_cer failed\n",__func__);