From: d15521469561 <1731678236@qq.com> Date: Wed, 6 May 2026 01:40:21 +0000 (+0800) Subject: idf 5.3.0版本 X-Git-Url: http://git.snicker.com.cn/sitemap.xml?a=commitdiff_plain;ds=sidebyside;p=mirrors%2Fmqtt_wglt.git idf 5.3.0版本 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index e2b915d..d4a1eea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,15 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.2.0) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# 每次编译都强制重新生成时间戳(不清空build也更新) +string(TIMESTAMP COMPILE_TIME "%Y%m%d_%H%M") +set(PROJECT_VER "V0.0.0_${COMPILE_TIME}") +file(WRITE ${CMAKE_CURRENT_LIST_DIR}/version.txt "${PROJECT_VER}\n") +# 关键:强制每次编译都重新运行CMake,刷新时间 +add_custom_target( + force_cmake_refresh ALL + COMMAND ${CMAKE_COMMAND} -E touch_nocreate "${CMAKE_CURRENT_LIST_FILE}" + COMMENT "Forcing CMake refresh to update compile time..." +) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(ssl_mutual_auth) \ No newline at end of file diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 94ba84f..9b24127 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register(SRCS "app_main.c" +idf_component_register(SRCS "https_ota.c" "app_main.c" "iap.c" "mqtt.c" "project_common.c" @@ -11,6 +11,7 @@ idf_component_register(SRCS "app_main.c" "crc.c" "http.c" "uart.c" + "https_ota.c" PRIV_REQUIRES mqtt esp_wifi bt @@ -18,6 +19,7 @@ idf_component_register(SRCS "app_main.c" esp_http_client esp_driver_uart esp_driver_gpio + esp_https_ota json app_update INCLUDE_DIRS ".") diff --git a/main/app_main.c b/main/app_main.c index 8654998..444255f 100644 --- a/main/app_main.c +++ b/main/app_main.c @@ -8,7 +8,7 @@ */ #include "project_common.h" #include "esp_timer.h" - +#include "https_ota.h" static const char *MAIN_TAG = "main"; // 1秒定时器句柄 @@ -17,6 +17,7 @@ static esp_timer_handle_t onesec_timer_handle = NULL; // 1秒定时器回调函数 static void onesec_timer_callback(void* arg) { + Mqtt_Send_Data(); // 在这里添加每秒执行的代码 ESP_LOGI(MAIN_TAG, "onesec_timer_callback memory head size: %" PRIu32 " bytes", esp_get_free_heap_size()); // 例如:检查状态、发送心跳等 @@ -29,8 +30,8 @@ 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"; const uint32_t user_id = 1; -const char* wifi_ssid = "XiaoMing1919810"; -const char* wifi_pwd = "XiaoMing1919810"; +const char* wifi_ssid = "PXN"; +const char* wifi_pwd = "PXN998877"; const uint16_t mcu_vid = 0x36E6; const uint16_t mcu_pid = 0xA00B; @@ -41,25 +42,30 @@ void Main_Loop_Task(void *pvParameters) { common_setting.heart_beat_interval = 1000; common_setting.mcu_info_got_flag = 1; - - // 创建1秒周期性定时器 + common_setting.ble_status.ble_state=0; + // // 创建1秒周期性定时器 const esp_timer_create_args_t onesec_timer_args = { .callback = &onesec_timer_callback, .arg = NULL, .name = "onesec_timer" }; ESP_ERROR_CHECK(esp_timer_create(&onesec_timer_args, &onesec_timer_handle)); - // 启动1秒周期性定时器 (1000000微秒 = 1秒) ESP_ERROR_CHECK(esp_timer_start_periodic(onesec_timer_handle, 1000000)); ESP_LOGI(MAIN_TAG, "1 second periodic timer started"); + #if (IOT_ENABLE) - if(common_storage.net_info.wifi_info_flag == 1 && common_storage.net_info.serial_number_flag == 1 && common_storage.ssl_cert[0].length != 0 && common_storage.ssl_cert[1].length != 0 && common_storage.ssl_cert[2].length != 0) - { - Wifi_Queue_Send(0, 0x03, 0); - } + //直连mqtts + // common_setting.wifi_status.cur_ssid_len=common_storage.net_info.wifi_ssid_len; + // common_setting.wifi_status.cur_pwd_len=common_storage.net_info.wifi_pwd_len; + // memcpy(common_setting.wifi_status.cur_ssid, common_storage.net_info.wifi_ssid,common_storage.net_info.wifi_ssid_len); + // memcpy(common_setting.wifi_status.cur_pwd, common_storage.net_info.wifi_pwd,common_storage.net_info.wifi_pwd_len); + // Wifi_Queue_Send(0, 0x03, 0); + // vTaskDelay(pdMS_TO_TICKS(1000)); + // xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT); + // ESP_LOGI(MAIN_TAG, "[APP] Free memory connect power on: on: %" PRIu32 " bytes", esp_get_free_heap_size()); common_setting.mcu_info.vid = mcu_vid; common_setting.mcu_info.pid = mcu_pid; common_setting.mcu_info.fw_version = 0x0001; @@ -67,8 +73,10 @@ void Main_Loop_Task(void *pvParameters) memcpy(common_setting.mcu_info.product_name, device_name, strlen(device_name)); common_setting.mcu_info.product_name_len = strlen(device_name); common_setting.mcu_info_got_flag = 1; + #if (USER_TEST_ENABLE) // BLE Test + common_setting.mcu_info.vid = mcu_vid; common_setting.mcu_info.pid = mcu_pid; common_setting.mcu_info.fw_version = 0x0001; @@ -78,7 +86,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)); @@ -97,19 +105,27 @@ 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); + common_setting.ble_status.ble_state=0; + common_storage.net_info.wifi_info_flag=1; + common_storage.net_info.serial_number_flag=1; + // Esp_Check_Mcu_info_Flag(); + Wifi_Queue_Send(0, 0x02, 0); #endif #endif + for(;;) { #if (1) Ble_Enable_Status_Check(); - Wifi_Enable_Status_Check(); Http_Connect_Status_Check(); Mqtt_Connect_Status_Check(); + #if OPEN_HTTPS_OTA_FUNC + Https_Ota_Status_Check(); + #endif #endif // heart beat if(common_setting.heart_beat_enable == 1 && common_setting.heart_beat_interval != 0) @@ -141,6 +157,7 @@ void Main_Loop_Task(void *pvParameters) esp_restart(); } } + vTaskDelay(MAIN_LOOP_INTERVAL / portTICK_PERIOD_MS); } } @@ -191,15 +208,21 @@ void app_main(void) ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_LOGI(MAIN_TAG, "[APP] Free memory after lwip: %" PRIu32 " bytes", esp_get_free_heap_size()); - // WIFI MQTT + + // WIFI Wifi_Sta_Init(); - + vTaskDelay(1000); + //等待wifi稳定再去连接wifi ESP_LOGI(MAIN_TAG, "[APP] Free memory after wifi: %" PRIu32 " bytes", esp_get_free_heap_size()); #endif - + common_setting.heart_beat_interval = 1000; + common_setting.mcu_info_got_flag = 1; + main_loop_task_handler = xTaskCreateStatic(Main_Loop_Task, "Main_Loop_Task", MAIN_LOOP_TASK_STACK, NULL, 4, main_loop_task_stack, &main_loop_task_tcb); ESP_LOGI(MAIN_TAG, "[APP] Free memory after Main_Loop: %" PRIu32 " bytes", esp_get_free_heap_size()); - + #if (!USER_TEST_ENABLE) + Esp_Check_Mcu_Info_Flag(); + #endif } diff --git a/main/ble.c b/main/ble.c index 7e90ed8..7a1a349 100644 --- a/main/ble.c +++ b/main/ble.c @@ -118,43 +118,63 @@ void Ble_Nimble_Init(void) // xQueueSend(ble_queue_handle, ble_queue_write, 0); // } //common_setting.mcu_info_got_flag=0 common_storage.net_info.device_id_flag=0 common_storage.net_info.wifi_info_flag=0 common_setting.ble_status.ble_state=0 -void Ble_Enable_Status_Check(void) +//串口接收主控发过来的消息 才开启判断是否需要配网 +void Esp_Check_Mcu_Info_Flag(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) + ESP_LOGI(BLE_TAG, "[APP] Free memory Esp_Check_Mcu_info_Flag on: %d %d %d\n",common_storage.net_info.serial_number_flag,common_storage.net_info.wifi_info_flag,\ + common_setting.ble_status.ble_state); + //只要有一个不为1都需要重新配网 + 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 no serial_number on: %" PRIu32 " bytes", esp_get_free_heap_size()); + ESP_LOGI(BLE_TAG, "[APP] Free memory start BLE_ON_BIT 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) + 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()); + }else//有mqtt地址 密码 名称 证书才能直连mqtt服务器 + { + //直连mqtts + common_setting.wifi_status.cur_ssid_len=common_storage.net_info.wifi_ssid_len; + common_setting.wifi_status.cur_pwd_len=common_storage.net_info.wifi_pwd_len; + if(common_setting.wifi_status.cur_pwd_len !=0 && common_setting.wifi_status.cur_ssid_len != 0) + { + memcpy(common_setting.wifi_status.cur_ssid, common_storage.net_info.wifi_ssid,common_storage.net_info.wifi_ssid_len); + memcpy(common_setting.wifi_status.cur_pwd, common_storage.net_info.wifi_pwd,common_storage.net_info.wifi_pwd_len); + if(common_storage.net_info.mqtt_url_len !=0) + { + Wifi_Queue_Send(0, 3, 0); + + }else{ + ESP_LOGI(BLE_TAG, "[APP] Free memory connect mqtt_sub_url no len"); + } + }else{ + ESP_LOGI(BLE_TAG, "[APP] Free memory connect ssid pwd no len"); + } + ESP_LOGI(BLE_TAG, "[APP] Free memory connect mqtt_sub_url on: on: %" PRIu32 " bytes", esp_get_free_heap_size()); } + }else{ + xEventGroupSetBits(ble_event_group, BLE_ON_BIT);//开启配网模式 + ESP_LOGI(BLE_TAG, "[APP] Free memory have not start BLE_ON_BIT error 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()); - // } } +} + +void Ble_Enable_Status_Check(void) +{ + // static uint8_t ble_queue_read[BLE_QUEUE_ITEM_SIZE] = {0}; // byte0:0:串口 1:蓝牙 0xFF:本地 byte1:1:打开BLE 0xFF:关闭 EventBits_t ble_bits = xEventGroupWaitBits(ble_event_group, BLE_ON_BIT | BLE_OFF_BIT, pdFALSE, pdFALSE, 0); @@ -167,14 +187,13 @@ void Ble_Enable_Status_Check(void) // 开始配网 common_setting.ble_status.ble_state = 1; common_setting.ble_enable_tick = 0; + common_setting.ble_status.nimble_flag=1; 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; ESP_LOGI(BLE_TAG, "[APP] Free memory ble on1: %" PRIu32 " bytes", esp_get_free_heap_size()); Ble_Nimble_Init(); - - ESP_LOGI(BLE_TAG, "[APP] Free memory ble on2: %" PRIu32 " bytes", esp_get_free_heap_size()); } } @@ -195,8 +214,7 @@ void Ble_Enable_Status_Check(void) nimble_port_deinit(); common_setting.ble_status.ble_state = 0; common_setting.ble_enable_tick = 0; - - // xEventGroupSetBits(ble_event_group, BLE_ON_BIT); + common_setting.ble_status.nimble_flag=0; ESP_LOGI(BLE_TAG, "nimble_port_stop\n"); } else diff --git a/main/ble.h b/main/ble.h index c1c67dc..0ad6284 100644 --- a/main/ble.h +++ b/main/ble.h @@ -57,5 +57,5 @@ void Ble_Nimble_Init(void); // void Ble_Queue_Send(uint8_t host_type, uint8_t enable); void Ble_Enable_Status_Check(void); - +void Esp_Check_Mcu_Info_Flag(void); #endif diff --git a/main/crc.c b/main/crc.c index b929ba2..99aea47 100644 --- a/main/crc.c +++ b/main/crc.c @@ -146,7 +146,7 @@ uint8_t Crc8_ROHC_Cal(uint8_t* data, uint32_t len) { crc8 = crc8_table[(crc8 ^ *data++) & 0xff]; } - ESP_LOGI(CHECKCRC_TAG, "Crc8_ROHC_Cal %x bytes:", crc8); + // ESP_LOGI(CHECKCRC_TAG, "Crc8_ROHC_Cal %x bytes:", crc8); return crc8; } #endif diff --git a/main/http.c b/main/http.c index 071307b..c9ad506 100644 --- a/main/http.c +++ b/main/http.c @@ -18,7 +18,7 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt) ESP_LOGI(HTTP_TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value); break; case HTTP_EVENT_ON_DATA: - ESP_LOGI(HTTP_TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len); + // ESP_LOGI(HTTP_TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len); // if (!esp_http_client_is_chunked_response(evt->client)) // { // printf("%.*s", evt->data_len, (char*)evt->data); @@ -46,80 +46,6 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt) #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; -// do -// { -// err = esp_http_client_open(http_client, 0); -// if (err == ESP_OK) -// { -// int header_ret = esp_http_client_fetch_headers(http_client); -// ESP_LOGI(HTTP_TAG, "esp_http_client_fetch_headers = %d", header_ret); - -// int code = esp_http_client_get_status_code(http_client); -// if(code == 200) -// { -// int file_size = esp_http_client_get_content_length(http_client); -// if(file_size <= (sizeof(common_storage.ssl_cert[0].content))) -// { -// 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) -// { -// // 成功 -// 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_TAG, "cert same: %d", cert_type); -// err = ESP_OK; -// break; -// } -// common_storage.ssl_cert[cert_type].length = file_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); -// 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; -// break; -// } -// } -// else -// { -// ESP_LOGE(HTTP_TAG, "http read size low = %d", read_len); -// err = ESP_FAIL; -// break; -// } -// } -// else -// { -// ESP_LOGE(HTTP_TAG, "file_size too large"); -// err = ESP_FAIL; -// break; -// } -// } -// else -// { -// ESP_LOGE(HTTP_TAG, "http req failed"); -// err = ESP_FAIL; -// break; -// } -// } -// else -// { -// err = ESP_FAIL; -// } -// }while(0); - -// return err; -// } #define HTTP_RETRY_NUMBER_MAX (5) //HTTP请求失败重试次数最大值 #define HTTP_GET 0 #define HTTP_POST 0 @@ -128,11 +54,13 @@ static char http_download_cache[sizeof(common_storage.ssl_cert[0].content)] = {0 #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"; - +const esp_partition_t *s_ota_partition = NULL; +esp_ota_handle_t s_ota_handle = 0; void http_download_task(void *pvParameters) { uint8_t success_bit = 0; @@ -144,7 +72,7 @@ void http_download_task(void *pvParameters) } esp_http_client_config_t http_config = { - // .url = "https://e-pxn.com.cn/pms/api/mqtt_ssl_mutual_auth.bin", + // .url = "https://pxn-iot.s3.us-east-2.amazonaws.com/iot01/2026/04/11/ssl_mutual_auth.bin",// "https://e-pxn.com.cn/pms/api/mqtt_ssl_mutual_auth.bin", .url = (const char*)&http_get_cert_task_param.url[cert_type][0], .crt_bundle_attach = esp_crt_bundle_attach, .event_handler = _http_event_handler, @@ -178,6 +106,45 @@ void http_download_task(void *pvParameters) int code = esp_http_client_get_status_code(http_client); int total_size = esp_http_client_get_content_length(http_client); + int data_total_size = 0; + // 擦除OTA分区 + // s_ota_partition = esp_ota_get_next_update_partition(NULL); + // if (esp_ota_begin(s_ota_partition, OTA_SIZE_UNKNOWN, &s_ota_handle) == ESP_OK) { + + + // while (1) //0 + // { + + // // 每次读 512 字节 + // uint8_t flash_buf[DOWNLOAD_BLOCK_SIZE+512] = {0}; + // int read_len = esp_http_client_read(http_client, (char*)flash_buf, DOWNLOAD_BLOCK_SIZE+512); + + // if (read_len <= 0) + // { + // ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "http req failed"); + + // break; + // } + // esp_ota_write(s_ota_handle, flash_buf, read_len); + // data_total_size+=read_len; + // } + // esp_err_t err = esp_ota_end(s_ota_handle); + // if(err == ESP_OK) + // { + // err = esp_ota_set_boot_partition(s_ota_partition); + // if(err == ESP_OK) + // { + // ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "OTA success"); + // vTaskDelay(1000); + // esp_restart(); + // } + // } + // else { + // ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "OTA 下载不完整"); + // } + // } + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "http req data_total_size:%d\n",data_total_size); + if(code == 200 && total_size > 0 && total_size <= (sizeof(common_storage.ssl_cert[cert_type].content))) { ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "esp_http_client_get_status_code"); @@ -189,7 +156,7 @@ void http_download_task(void *pvParameters) 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; @@ -213,7 +180,7 @@ void http_download_task(void *pvParameters) 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); + ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "flash_buf %s",flash_buf); memcpy(common_storage.ssl_cert[cert_type].content + total_read, flash_buf, read_len); total_read += read_len; @@ -233,43 +200,32 @@ void http_download_task(void *pvParameters) 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) + + 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, + &common_storage.ssl_cert[cert_type], + sizeof(SSL_CERT_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; + 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+ 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 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; + 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_LOGI(HTTP_TAG, "device have not ssid or pwd\n"); + else + { + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write cer failed:%d\n",cert_type); + err = ESP_FAIL; } + }else{ ESP_LOGI(HTTP_TAG, "Cert %d download failed, size=%d", cert_type, total_read); err = ESP_FAIL; @@ -292,7 +248,7 @@ void http_download_task(void *pvParameters) 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) + if(0) { ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "cert same: %d", cert_type); err = ESP_OK; @@ -302,6 +258,14 @@ void http_download_task(void *pvParameters) 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, total_size); + common_storage.ssl_cert[cert_type].content[total_size] = '\0'; + for(uint8_t i = 0;i < 10;i++) + { + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG,"common_storage. qian 10 ssl_cert[%d].content 0x%x\n",cert_type,common_storage.ssl_cert[cert_type].content[i]); + } + uint16_t total_size_t = total_size-10; + + ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write cer key server_crt success: %d\n", cert_type); 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) @@ -309,8 +273,11 @@ void http_download_task(void *pvParameters) // 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; + success_bit |= (1 << cert_type); ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write cer key server_crt success: %d\n", cert_type); err = ESP_OK; + } else { @@ -383,21 +350,35 @@ void http_download_task(void *pvParameters) common_storage.net_info.serial_number_flag = 1; common_storage.net_info.wifi_info_flag = 1; #endif + #if (NIMBLE_FLOW_MODE) xEventGroupSetBits(ble_event_group, BLE_OFF_BIT);//关闭蓝牙配网 + #endif ESP_LOGI(HTTP_TAG, "download clientcer clientkey mqtt \n"); + common_setting.mqtt_start_flag = 1; + xEventGroupSetBits(common_event_group, COMMON_HTTPS_OTA_BIT); xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT); + // Mqtt_Stack_init(); } vTaskDelete(NULL); return; } - +#define HTTP_CONTENT_AMX_LEN 2048 +#define USE_MALLOC_DATA_SIZE 0 +#define HTTP_CHUNK_SIZE 512 +#if USE_MALLOC_DATA_SIZE +#define MAX_HTTP_OUTPUT_BUFFER 512 +#else +#define MAX_HTTP_OUTPUT_BUFFER 1024 +#endif +char output_buffer[MAX_HTTP_OUTPUT_BUFFER+1] = {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) { ESP_LOGI(HTTP_TAG, "param wrong:%d %d %d %d %d %d\n", common_setting.net_pairing_info.http_url_flag, common_setting.net_pairing_info.http_token_flag, common_setting.net_pairing_info.user_id_flag, common_setting.mcu_info_got_flag, common_setting.net_pairing_info.http_url_length, common_setting.net_pairing_info.http_token_length); vTaskDelete(NULL); + return; } // @@ -416,6 +397,11 @@ 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; + int content_len = 0; + int status_code=0; + #if (!USE_MALLOC_DATA_SIZE) + int data_read=0; + #endif for(uint8_t retry=0;retry= 0) { - memset(output_buffer, 0x00, sizeof(output_buffer)); - int data_read = esp_http_client_read_response(http_client, output_buffer, MAX_HTTP_OUTPUT_BUFFER);//读取HTTP响应体,output_buffer为存储响应体的缓冲区,MAX_HTTP_OUTPUT_BUFFER为缓冲区的大小,返回值为实际读取的字节数,如果返回值小于0表示读取响应体失败 - if (data_read >= 0) + #if USE_MALLOC_DATA_SIZE + status_code = esp_http_client_get_status_code(http_client);//获取HTTP响应状态码,返回值为状态码的整数值,例如200表示请求成功,404表示资源未找到等 + content_len = esp_http_client_get_content_length(http_client);//获取HTTP响应体的长度,返回值为响应体的字节数,如果返回值小于0表示获取响应体长度失败 + ESP_LOGI(HTTP_TAG, "http_read_buff malloc is status_code %d content_len %d\n",status_code,content_len); + if(status_code == 200) { - int status_code = esp_http_client_get_status_code(http_client);//获取HTTP响应状态码,返回值为状态码的整数值,例如200表示请求成功,404表示资源未找到等 - int read_len = esp_http_client_get_content_length(http_client);//获取HTTP响应体的长度,返回值为响应体的字节数,如果返回值小于0表示获取响应体长度失败 - ESP_LOGI(HTTP_TAG, "HTTP GET Status = %d, content_length = %d", status_code, read_len); + char *json_buf = malloc(HTTP_CONTENT_AMX_LEN+1); + if(json_buf == NULL) + { + result=2; + ESP_LOGI(HTTP_TAG, "http_read_buff malloc is failed\n"); + }else{ + int total_read = 0; + int read_len; + memset(output_buffer, 0, sizeof(output_buffer)); + // 分段读取,拼到一次性申请的 buffer 里 + while ((read_len = esp_http_client_read_response(http_client, output_buffer, HTTP_CHUNK_SIZE)) > 0) + { + if (total_read + read_len > HTTP_CONTENT_AMX_LEN) + { + break; // 防越界 + } + memcpy(json_buf + total_read, output_buffer, read_len); + total_read += read_len; + } + + json_buf[total_read] = '\0'; + + cJSON* root_base = cJSON_Parse(json_buf); + if(root_base != NULL) + { + cJSON* pData = cJSON_GetObjectItem(root_base,"data"); + if(pData != NULL) + { + cJSON* pSerialNumber = cJSON_GetObjectItem(pData,"serialNumber"); + //get the value + if(pSerialNumber != NULL && cJSON_IsString(pSerialNumber)) + { + char* str_serialNumber = pSerialNumber->valuestring; + ESP_LOGI(HTTP_TAG, "serialNumber: %s\n", str_serialNumber); + + + uint8_t serial_number_len = strlen(str_serialNumber); + if(serial_number_len == 0) + { + ESP_LOGI(HTTP_TAG, "serialNumber length is 0\n"); + cJSON_Delete(root_base); + result=2; + }else{ + + if(serial_number_len > SERIAL_NUMBER_MAX_LEN) + { + serial_number_len= SERIAL_NUMBER_MAX_LEN; + } + //临时使用的数据 + memset(common_setting.net_pairing_info.serial_number, 0x00, SERIAL_NUMBER_MAX_LEN+1); + memccpy(common_setting.net_pairing_info.serial_number, str_serialNumber, 0, serial_number_len); + common_setting.net_pairing_info.serial_number[serial_number_len] = '\0'; + common_setting.net_pairing_info.serial_number_length = serial_number_len; + common_setting.net_pairing_info.serial_number_flag = 1; + + //真正需要存储的flash数据 + memset(common_storage.net_info.serial_number, 0x00, SERIAL_NUMBER_MAX_LEN+1); + memccpy(common_storage.net_info.serial_number, str_serialNumber, 0, serial_number_len); + common_storage.net_info.serial_number[serial_number_len] = '\0'; + common_storage.net_info.serial_number_flag=1; + //保存flash后, + 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_LOGI(HTTP_TAG, "flash write user config info success\n"); + if(User_Flash_Read_Check(UF_OFFSET_USER_CONFIG_INFO_ADDR, UF_OFFSET_USER_CONFIG_INFO_FLAG, &common_storage.net_info, sizeof(NET_INFO_T)) == ESP_OK) + { + ESP_LOGI(HTTP_TAG, "read serialNumber: %s\n", common_storage.net_info.serial_number); + } + result = 1; + + }else + { + ESP_LOGE(HTTP_TAG, "flash write error\n"); + result = 2; + + } + } + + } + else + { + result = 2; + } + } + else + { + result = 2; + } + cJSON_Delete(root_base); + } + else + { + result = 2; + ESP_LOGI(HTTP_TAG, "root_base is null\n"); + } + if(json_buf) + { + free(json_buf); + json_buf=NULL; + } + } + } + + #else + memset(output_buffer, 0, sizeof(output_buffer)); + data_read=0; + data_read = esp_http_client_read_response(http_client, output_buffer, MAX_HTTP_OUTPUT_BUFFER);//读取HTTP响应体,output_buffer为存储响应体的缓冲区,MAX_HTTP_OUTPUT_BUFFER为缓冲区的大小,返回值为实际读取的字节数,如果返回值小于0表示读取响应体失败 + status_code = esp_http_client_get_status_code(http_client);//获取HTTP响应状态码,返回值为状态码的整数值,例如200表示请求成功,404表示资源未找到等 + if (data_read > 0 && data_read <= MAX_HTTP_OUTPUT_BUFFER && status_code == 200) + { + + content_len = esp_http_client_get_content_length(http_client);//获取HTTP响应体的长度,返回值为响应体的字节数,如果返回值小于0表示获取响应体长度失败 + ESP_LOGI(HTTP_TAG, "HTTP GET Status = %d, content_length = %d", status_code, content_len); // ESP_LOG_BUFFER_CHAR(HTTP_TAG, output_buffer, data_read); ESP_LOGI(HTTP_TAG, "\nrecv data: %s\n", output_buffer); output_buffer[data_read]='\0'; @@ -554,6 +654,7 @@ void http_get_url_task(void *pvParameters) else { result = 2; + ESP_LOGI(HTTP_TAG, "data len is too large\n"); } } else @@ -561,6 +662,7 @@ void http_get_url_task(void *pvParameters) result = 2; ESP_LOGE(HTTP_TAG, "Failed to read response"); } + #endif } } if(http_client != NULL){ @@ -743,17 +845,148 @@ void http_get_url_task(void *pvParameters) esp_http_client_set_method(http_client, HTTP_METHOD_GET); esp_err_t err = esp_http_client_open(http_client, 0); + if(err == ESP_OK) { + content_len = 0; + status_code=0; int header_ret = esp_http_client_fetch_headers(http_client); ESP_LOGI(HTTP_TAG, "esp_http_client_fetch_headers = %d", header_ret); if (header_ret >= 0) { + #if USE_MALLOC_DATA_SIZE + status_code = esp_http_client_get_status_code(http_client);//获取HTTP响应状态码,返回值为状态码的整数值,例如200表示请求成功,404表示资源未找到等 + content_len = esp_http_client_get_content_length(http_client);//获取HTTP响应体的长度,返回值为响应体的字节数,如果返回值小于0表示获取响应体长度失败 + ESP_LOGI(HTTP_TAG, "http_read_buff malloc is status_code %d content_len %d\n",status_code,content_len); + if(status_code == 200) + { + char *json_buf = malloc(HTTP_CONTENT_AMX_LEN+1); + if(json_buf == NULL) + { + result=2; + ESP_LOGI(HTTP_TAG, "http_read_buff malloc is failed\n"); + }else{ + int total_read = 0; + int read_len; + memset(output_buffer, 0x00, sizeof(output_buffer)); + // 分段读取,拼到一次性申请的 buffer 里 + while ((read_len = esp_http_client_read_response(http_client, output_buffer, HTTP_CHUNK_SIZE)) > 0) + { + if (total_read + read_len > HTTP_CONTENT_AMX_LEN) + { + break; // 防越界 + } + memcpy(json_buf + total_read, output_buffer, read_len); + total_read += read_len; + } + + json_buf[total_read] = '\0'; + ESP_LOGI(HTTP_TAG, "http_read_buff cert data total is failed %d\n",total_read); + cJSON* root_base = cJSON_Parse(json_buf); + if(root_base != NULL) + { + cJSON* pData = cJSON_GetObjectItem(root_base,"data"); + if(pData != NULL) + { + // cJSON* pDevice_id = cJSON_GetObjectItem(pData,"deviceId"); + // { + // "serialNumber":"PXN1LJ1QUR078E", + // "firmwareVersion":1.0, + // "status":4, + // "rssi":-80, + // "clientcrtUrl":"https://pxn-iot.s3.us-east-2.amazonaws.com/iot01/2026/04/02/69ce08f0bfcbe4e5bea9a6a5.crt", + // "clientkeyUrl":"https://pxn-iot.s3.us-east-2.amazonaws.com/iot01/2026/04/02/69ce08b3bfcbe4e5bea9a6a4.key", + // "mosquittoorgcrtUrl":"https://pxn-iot.s3.us-east-2.amazonaws.com/iot01/2026/04/02/69ce0836bfcbe4e5bea9a6a3.crt", + // "mqttUrl":"mqtts://a3l6aw6tvk7fvq-ats.iot.us-east-1.amazonaws.com:8883", + // "macAddress":"1", + // "pid":"0xA00B" + // } + pClientCrt = cJSON_GetObjectItem(pData,"clientcrtUrl"); + pClientkey = cJSON_GetObjectItem(pData,"clientkeyUrl"); + pServerCrt = cJSON_GetObjectItem(pData,"mosquittoorgcrtUrl"); + pMqttSubUrl = cJSON_GetObjectItem(pData,"mqttUrl"); + + memset(&http_get_cert_task_param.url[SSL_CLIENT_CRT][0], 0x00, sizeof(http_get_cert_task_param.url[SSL_CLIENT_CRT])); + memset(&http_get_cert_task_param.url[SSL_CLIENT_KEY][0], 0x00, strlen(http_get_cert_task_param.url[SSL_CLIENT_KEY])); + memset(&http_get_cert_task_param.url[SSL_SERVER_CRT][0], 0x00, strlen(http_get_cert_task_param.url[SSL_SERVER_CRT])); + //get the value + if((pClientCrt != NULL && cJSON_IsString(pClientCrt)) && + (pClientkey != NULL && cJSON_IsString(pClientkey)) && + (pServerCrt != NULL && cJSON_IsString(pServerCrt)) && + (pMqttSubUrl != NULL && cJSON_IsString(pMqttSubUrl))) + { + ESP_LOGI(HTTP_TAG, "get 证书 mqtt 地址\n"); + // common_setting.net_pairing_info.device_id = pDevice_id->valueint; + // common_setting.net_pairing_info.device_id_flag = 1; + result = 1; + + memcpy(&http_get_cert_task_param.url[SSL_CLIENT_CRT][0], pClientCrt->valuestring, strlen((const char*)pClientCrt->valuestring)); + memcpy(&http_get_cert_task_param.url[SSL_CLIENT_KEY][0], pClientkey->valuestring, 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, 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_sub_url[mqtt_url_len]='\0'; + 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 + { + result = 2; + } + } + else + { + result = 3; + } + cJSON_Delete(root_base); + } + else + { + result = 4; + ESP_LOGI(HTTP_TAG, "root_base is null\n"); + } + if(json_buf) + { + free(json_buf); + json_buf=NULL; + } + } + } + + #else memset(output_buffer, 0x00, sizeof(output_buffer)); - int data_read = esp_http_client_read_response(http_client, output_buffer, MAX_HTTP_OUTPUT_BUFFER); - if (data_read >= 0) + data_read=0; + data_read = esp_http_client_read_response(http_client, output_buffer, MAX_HTTP_OUTPUT_BUFFER); + status_code = esp_http_client_get_status_code(http_client); + if (data_read > 0 && status_code == 200) { - int status_code = esp_http_client_get_status_code(http_client); + int read_len = esp_http_client_get_content_length(http_client); ESP_LOGI(HTTP_TAG, "%s HTTP GET Status = %d, content_length = %d data_read = %d\n",__func__, status_code, read_len,data_read); // ESP_LOG_BUFFER_CHAR(HTTP_TAG, output_buffer, data_read); @@ -817,7 +1050,9 @@ void http_get_url_task(void *pvParameters) 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_sub_url[mqtt_url_len]='\0'; 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) @@ -859,6 +1094,7 @@ void http_get_url_task(void *pvParameters) result = 6; ESP_LOGE(HTTP_TAG, "Failed to read response"); } + #endif } } if(http_client != NULL) @@ -920,8 +1156,9 @@ void http_get_url_task(void *pvParameters) if (header_ret >= 0) { memset(output_buffer, 0x00, sizeof(output_buffer)); - int data_read = esp_http_client_read_response(http_client, output_buffer, MAX_HTTP_OUTPUT_BUFFER); - if (data_read >= 0) + data_read=0; + data_read = esp_http_client_read_response(http_client, output_buffer, MAX_HTTP_OUTPUT_BUFFER); + if (data_read > 0) { int status_code = esp_http_client_get_status_code(http_client); int read_len = esp_http_client_get_content_length(http_client); @@ -1202,6 +1439,7 @@ void http_bind_task(void *pvParameters) void Http_Connect_Status_Check(void) { + // ESP_LOGI(HTTP_TAG, "[APP] Free memory http1 on: %" PRIu32 " bytes", esp_get_free_heap_size()); if((xEventGroupWaitBits(common_event_group, COMMON_HTTP_DOWNLOAD_BIT, pdFALSE, pdFALSE, 0) & COMMON_HTTP_DOWNLOAD_BIT) != 0) { xEventGroupClearBits(common_event_group, COMMON_HTTP_DOWNLOAD_BIT); diff --git a/main/http.h b/main/http.h index 6e879f9..d473cc9 100644 --- a/main/http.h +++ b/main/http.h @@ -3,7 +3,7 @@ #include "esp_http_client.h" -#define MAX_HTTP_OUTPUT_BUFFER 2560 + #define CONFIG_EXAMPLE_ENABLE_RESPONSE_BUFFER_DUMP 1 typedef struct diff --git a/main/iap.c b/main/iap.c index 8567e8e..59d8763 100644 --- a/main/iap.c +++ b/main/iap.c @@ -388,7 +388,7 @@ void Iap_Process(uint8_t* data, uint8_t len) ESP_LOGI(IAP_TAG, "unauth\n"); } - Ble_Pairing_Reply(iap_reply.cmd.cmd1, iap_reply.other.buf, sizeof(iap_reply.other.buf), HOST_TYPE_UART); + Uart_Reply(iap_reply.cmd.cmd1, iap_reply.other.buf, sizeof(iap_reply.other.buf), HOST_TYPE_UART); } diff --git a/main/mqtt.c b/main/mqtt.c index b3e8f4c..0e9066b 100644 --- a/main/mqtt.c +++ b/main/mqtt.c @@ -1,15 +1,9 @@ #include "mqtt.h" static const char *MQTT_TAG = "MQTT"; - +#define MQTT_TOPIC "sdk/test/python" #define MQTT_MAX_TRY_CONNECT_TIME 10 static uint8_t retry_connect_cnt = 0; -// extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start"); -// extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end"); -// extern const uint8_t client_key_pem_start[] asm("_binary_client_key_start"); -// extern const uint8_t client_key_pem_end[] asm("_binary_client_key_end"); -// extern const uint8_t server_cert_pem_start[] asm("_binary_mosquitto_org_crt_start"); -// extern const uint8_t server_cert_pem_end[] asm("_binary_mosquitto_org_crt_end"); static void log_error_if_nonzero(const char *message, int error_code) { @@ -28,46 +22,39 @@ static void log_error_if_nonzero(const char *message, int error_code) * @param event_id The id for the received event. * @param event_data The data for the event, esp_mqtt_event_handle_t. */ +static esp_mqtt_client_handle_t mqtt_client; +static uint8_t mqtt_init_flag = 0; +static uint8_t mqtt_send_data_count = 0; +static uint8_t esp_mqtt_init_flag = 0; static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { ESP_LOGD(MQTT_TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id); esp_mqtt_event_handle_t event = event_data; - esp_mqtt_client_handle_t client = event->client; + mqtt_client = event->client; int msg_id; switch ((esp_mqtt_event_id_t)event_id) { case MQTT_EVENT_CONNECTED: retry_connect_cnt = 0; + mqtt_send_data_count=0; + common_setting.mqtt_connect_flag=1; ESP_LOGI(MQTT_TAG, "MQTT_EVENT_CONNECTED"); - // modify - // msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); - // ESP_LOGI(MQTT_TAG, "sent subscribe successful, msg_id=%d", msg_id); - - msg_id = esp_mqtt_client_subscribe(client, "sdk/test/python", 0); + msg_id = esp_mqtt_client_subscribe(mqtt_client, MQTT_TOPIC, 0); ESP_LOGI(MQTT_TAG, "sent subscribe successful, msg_id=%d", msg_id); - - // msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); - // ESP_LOGI(MQTT_TAG, "sent subscribe successful, msg_id=%d", msg_id); - - // msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); - // ESP_LOGI(MQTT_TAG, "sent unsubscribe successful, msg_id=%d", msg_id); - break; case MQTT_EVENT_DISCONNECTED: - if(common_setting.wifi_status.connect_mode != 3 && ++retry_connect_cnt >= MQTT_MAX_TRY_CONNECT_TIME) - { - - xEventGroupSetBits(common_event_group, COMMON_MQTT_DISCONNECT_BIT); - } + mqtt_send_data_count=0; + common_setting.mqtt_connect_flag = 0; + // xEventGroupSetBits(common_event_group, COMMON_MQTT_DISCONNECT_BIT);//不能重复关闭 要不然吃内存最后每次不够 ESP_LOGI(MQTT_TAG, "MQTT_EVENT_DISCONNECTED"); break; case MQTT_EVENT_SUBSCRIBED: + mqtt_send_data_count=0; + common_setting.mqtt_connect_flag=2; ESP_LOGI(MQTT_TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); - // modify - // msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); - msg_id = esp_mqtt_client_publish(client, "sdk/test/python", "good123", 0, 0, 0); - ESP_LOGI(MQTT_TAG, "sent publish successful, msg_id=%d", msg_id); + msg_id = esp_mqtt_client_publish(mqtt_client, "sdk/test/python", "good 456", 0, 0, 0); + // ESP_LOGI(MQTT_TAG, "sent publish successful, msg_id=%d", msg_id); break; case MQTT_EVENT_UNSUBSCRIBED: @@ -82,7 +69,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ ESP_LOGI(MQTT_TAG, "MQTT_EVENT_DATA"); ESP_LOGI(MQTT_TAG, "TOPIC=%.*s\r\n", event->topic_len, event->topic); ESP_LOGI(MQTT_TAG, "DATA=%.*s\r\n", event->data_len, event->data); - + Mqtt_Rec_Data(event->topic,event->topic_len,event->data,event->data_len); // modify // msg_id = esp_mqtt_client_publish(client, "sdk/test/python", "good 456", 0, 0, 0); // ESP_LOGI(MQTT_TAG, "sent publish successful, msg_id=%d", msg_id); @@ -116,7 +103,8 @@ 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; @@ -130,7 +118,7 @@ static esp_err_t Mqtt_App_Init(void) ESP_LOGI(MQTT_TAG, "cert error"); return ESP_FAIL; } - if(common_setting.net_pairing_info.mqtt_sub_url_flag == 0 || common_setting.net_pairing_info.mqtt_sub_url_length == 0) + if(common_storage.net_info.mqtt_url_len == 0) { ESP_LOGI(MQTT_TAG, "mqtt url error"); return ESP_FAIL; @@ -159,6 +147,13 @@ static esp_err_t Mqtt_App_Init(void) ESP_LOGI(MQTT_TAG, "Mqtt init has been initialized.\n"); return ESP_OK; } + if(common_storage.net_info.mqtt_url_len==0) + { + ESP_LOGI(MQTT_TAG, "user falsh mqtt_sub_url == 0"); + return ESP_FAIL; + } + + memset(&common_setting.mqtt_cfg, 0, sizeof(common_setting.mqtt_cfg)); retry_connect_cnt = 0; // common_setting.mqtt_cfg.session.keepalive = 60; common_setting.mqtt_cfg.network.disable_auto_reconnect = false; @@ -166,17 +161,19 @@ static esp_err_t Mqtt_App_Init(void) // common_setting.mqtt_cfg.buffer.out_size = 2048; // common_setting.mqtt_cfg.buffer.size = 2048; - // ESP_LOGI(MQTT_TAG, "URI: %s", common_setting.net_pairing_info.mqtt_sub_url); - - common_setting.mqtt_cfg.broker.address.uri = (const char *)common_setting.net_pairing_info.mqtt_sub_url; + ESP_LOGI(MQTT_TAG, "URI: %s %s", common_storage.net_info.mqtt_sub_url,common_setting.net_pairing_info.mqtt_sub_url); + + common_setting.mqtt_cfg.broker.address.uri = (const char *)common_storage.net_info.mqtt_sub_url; + common_setting.mqtt_cfg.broker.verification.certificate = (const char *)common_storage.ssl_cert[SSL_SERVER_CRT].content; common_setting.mqtt_cfg.credentials.authentication.certificate = (const char *)common_storage.ssl_cert[SSL_CLIENT_CRT].content; common_setting.mqtt_cfg.credentials.authentication.key = (const char *)common_storage.ssl_cert[SSL_CLIENT_KEY].content; - // common_setting.mqtt_cfg.credentials.client_id = (const char *)common_setting.net_pairing_info.serial_number; + common_setting.mqtt_cfg.credentials.client_id = "basicPubSub"; - + ESP_LOGI(MQTT_TAG, "Mqtt reset esp_mqtt_client_init 1"); common_setting.mqtt_client_handle = esp_mqtt_client_init(&common_setting.mqtt_cfg); - + + ESP_LOGI(MQTT_TAG, "Mqtt reset esp_mqtt_client_init 2"); /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ err = esp_mqtt_client_register_event(common_setting.mqtt_client_handle, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); if(err == ESP_OK) @@ -190,9 +187,53 @@ static esp_err_t Mqtt_App_Init(void) return err; } +void Mqtt_Send_Data(void) +{ + return; + int m_id; + static uint8_t i=0; + char data[10] = {0}; + if(common_setting.mqtt_connect_flag == 2) + { + + if(mqtt_send_data_count >= 2) + { + mqtt_send_data_count=0; + + i++; + if(i >= 255) + { + i=0; + } + sprintf(data,"%s%d","good",i); + m_id = esp_mqtt_client_publish(mqtt_client, "sdk/test/python", data, 0, 0, 0); + ESP_LOGI(MQTT_TAG, "sent publish successful, msg_id=%d %s\n", m_id,data); + + } + mqtt_send_data_count++; + } +} +//接收mqtt发过来的数据 +void Mqtt_Rec_Data(char *topic,int topic_len,char *data,int data_len){ + if(memcmp(topic,MQTT_TOPIC,topic_len)!=0 || strlen(MQTT_TOPIC) != topic_len) + { + ESP_LOGI(MQTT_TAG, "主题和订阅的主题不匹配 或者长度不一致 %s %d\n",topic,topic_len); + return; + } + //json数据 + // 2. 安全打印(必须带长度,不能直接当字符串) + ESP_LOGI(MQTT_TAG, "收到合法数据:%.*s", data_len, data); + + + + // 现在 data 就是标准C字符串,可以用 cJSON 解析 + // cJSON_Parse(data); +} + void Mqtt_Connect_Status_Check(void) { - if((xEventGroupWaitBits(common_event_group, COMMON_MQTT_CONNECT_BIT, pdFALSE, pdFALSE, 0) & COMMON_MQTT_CONNECT_BIT) != 0) + if((xEventGroupWaitBits(common_event_group, COMMON_MQTT_CONNECT_BIT, pdFALSE, pdFALSE, 0) & COMMON_MQTT_CONNECT_BIT) != 0 && common_setting.ble_status.ble_state == 0\ + && common_setting.wifi_status.wifi_state == 1 && common_setting.https_ota_cfg.https_ota_start_flag == 0) { xEventGroupClearBits(common_event_group, COMMON_MQTT_CONNECT_BIT); vTaskDelay(50 / portTICK_PERIOD_MS); @@ -210,19 +251,77 @@ void Mqtt_Connect_Status_Check(void) { xEventGroupClearBits(common_event_group, COMMON_MQTT_DISCONNECT_BIT); vTaskDelay(50 / portTICK_PERIOD_MS); - if(esp_mqtt_client_stop(common_setting.mqtt_client_handle) == ESP_OK) + + if(esp_mqtt_init_flag == 1){ + if(common_setting.mqtt_client_handle){ + if(esp_mqtt_client_stop(common_setting.mqtt_client_handle) == ESP_OK) + { + // esp_mqtt_client_destroy(common_setting.mqtt_client_handle); + common_setting.mqtt_client_handle=NULL; + retry_connect_cnt = 0; + esp_mqtt_init_flag=0; + ESP_LOGI(MQTT_TAG, "[APP] Free memory mqtt off: %" PRIu32 " bytes", esp_get_free_heap_size()); + } + else + { + ESP_LOGI(MQTT_TAG, "Mqtt stop fail"); + } + } + } + + } + +} + +void Mqtt_Connect_Task(void *p) +{ + while (1) + { + if((xEventGroupWaitBits(common_event_group, COMMON_MQTT_CONNECT_BIT, pdFALSE, pdFALSE, 0) & COMMON_MQTT_CONNECT_BIT) != 0 && common_setting.wifi_status.wifi_state == 1 \ + && common_setting.ble_status.ble_state == 0) { - retry_connect_cnt = 0; - ESP_LOGI(MQTT_TAG, "[APP] Free memory mqtt off: %" PRIu32 " bytes", esp_get_free_heap_size()); + xEventGroupClearBits(common_event_group, COMMON_MQTT_CONNECT_BIT); + vTaskDelay(50 / portTICK_PERIOD_MS); + if(Mqtt_App_Init() == ESP_OK) + { + ESP_LOGI(MQTT_TAG, "[APP] Free memory mqtt on: %" PRIu32 " bytes", esp_get_free_heap_size()); + } + else + { + ESP_LOGI(MQTT_TAG, "Mqtt init fail"); + } } - else + + if((xEventGroupWaitBits(common_event_group, COMMON_MQTT_DISCONNECT_BIT, pdFALSE, pdFALSE, 0) & COMMON_MQTT_DISCONNECT_BIT) != 0) { - ESP_LOGI(MQTT_TAG, "Mqtt stop fail"); + xEventGroupClearBits(common_event_group, COMMON_MQTT_DISCONNECT_BIT); + vTaskDelay(50 / portTICK_PERIOD_MS); + if(esp_mqtt_client_stop(common_setting.mqtt_client_handle) == ESP_OK) + { + retry_connect_cnt = 0; + ESP_LOGI(MQTT_TAG, "[APP] Free memory mqtt off: %" PRIu32 " bytes", esp_get_free_heap_size()); + } + else + { + ESP_LOGI(MQTT_TAG, "Mqtt stop fail"); + } } + vTaskDelay(10 / portTICK_PERIOD_MS); } + + } - +void Mqtt_Stack_Init(void) +{ + if(mqtt_init_flag){ + return; + } + mqtt_init_flag=1; + ESP_LOGI(MQTT_TAG, "Mqtt_Stack_init 1"); + mqtt_connect_task_handler = xTaskCreateStatic(Mqtt_Connect_Task, "mqtt_connect_task", MQTT_CONNECT_TASK_STACK, NULL, 2, mqtt_connect_task_stack, &mqtt_connect_task_tcb); + ESP_LOGI(MQTT_TAG, "Mqtt_Stack_init 2"); +} diff --git a/main/mqtt.h b/main/mqtt.h index c4cdb0c..d746201 100644 --- a/main/mqtt.h +++ b/main/mqtt.h @@ -8,5 +8,7 @@ void Mqtt_Connect_Status_Check(void); - +void Mqtt_Stack_Init(void); +void Mqtt_Send_Data(void); +void Mqtt_Rec_Data(char *topic,int topic_len,char *data,int data_len); #endif diff --git a/main/project_common.c b/main/project_common.c index 30ee598..93cdd6b 100644 --- a/main/project_common.c +++ b/main/project_common.c @@ -56,6 +56,11 @@ TaskHandle_t wifi_scan_task_handler; StackType_t wifi_scan_task_stack[WIFI_SCAN_TASK_STACK]; StaticTask_t wifi_scan_task_tcb; +// MQTT_LOOP +TaskHandle_t mqtt_connect_task_handler; +StackType_t mqtt_connect_task_stack[MQTT_CONNECT_TASK_STACK]; +StaticTask_t mqtt_connect_task_tcb; + // HTTP // TaskHandle_t http_get_url_task_handler; // StackType_t http_get_url_task_stack[HTTP_GET_URL_TASK_STACK]; @@ -141,31 +146,35 @@ void Check_Host_Type(uint8_t cmd, uint8_t* pbuf, uint16_t len, uint8_t host_type //*pub不包含帧头 命令 crcr长度等协议字段,纯数据部分,len为数据部分长度 void Uart_Reply(uint8_t cmd, uint8_t* pbuf, uint16_t len, uint8_t host_type) { - if (len > UART_REPLY_MAX_PAYLOAD-4) // 5 = header(1) + cmd(1) + len(2) + crc(1) 暂时len为1个字节 + if (len > UART_REPLY_MAX_PAYLOAD-3) // 5 = header(1) + cmd(1) + len(1) + crc(1) 暂时len为1个字节 { ESP_LOGE(COMMON_TAG, "Uart_Reply length too large: %d\n", len); return; } static UART_RECV_DATA_T uart_pairing_reply = { 0 }; - uint8_t header[4] = {FRAMEHEAD_IOT_TO_PANEL, cmd, 0x00, 0x00}; - memcpy(&header[2], &len, 2); - uart_pairing_reply.size = len + 1 + 1 + 2 + 1; // header(1)+cmd(1)+len(2)+crc(1) + uint8_t header[3] = {FRAMEHEAD_IOT_TO_PANEL, cmd, 0x00}; + memcpy(&header[2], &len, 1); + uart_pairing_reply.size = len + 1 + 1 + 1 + 1; // header(1)+cmd(1)+len(1)+crc(1) - memcpy(uart_pairing_reply.buffer, header, 4); + memcpy(uart_pairing_reply.buffer, header, 3); if (len > 0 && pbuf != NULL) { - memcpy(&uart_pairing_reply.buffer[4], pbuf, len); + memcpy(&uart_pairing_reply.buffer[3], pbuf, len); } - uint8_t crc = Crc8_ROHC_Cal(&uart_pairing_reply.buffer[1], len + 3); - uart_pairing_reply.buffer[len + 4] = crc; - ESP_LOGI(COMMON_TAG, "%s crc %02x len %d",__func__, crc, len); + uint8_t crc = Crc8_ROHC_Cal(&uart_pairing_reply.buffer[1], len + 2); + uart_pairing_reply.buffer[len + 3] = crc; + // ESP_LOGI(COMMON_TAG, "%s crc %02x len %d",__func__, crc, len); xQueueSend(uart_tx_queue_handle, &uart_pairing_reply, 0); } void Ble_Pairing_Reply(uint8_t cmd, uint8_t* pbuf, uint16_t len, uint8_t host_type) { + if(common_setting.ble_status.ble_state!=2) + { + return; + } if (len > NET_PAIRING_REPLY_MAX_PAYLOAD) { ESP_LOGE(COMMON_TAG, "Ble_Pairing_Reply length too large: %d\n", len); @@ -268,6 +277,9 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) { memcpy(common_setting.mcu_info.product_name, &data[12], name_len); common_setting.mcu_info.product_name[name_len] = '\0'; // 确保字符串结尾 + common_setting.mcu_info.product_name_len = name_len; + common_setting.mcu_info_got_flag = 1; + Esp_Check_Mcu_Info_Flag();//开启蓝牙配网 } else { @@ -275,8 +287,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) name_len = 0; } } - common_setting.mcu_info.product_name_len = name_len; - common_setting.mcu_info_got_flag = 1; + } } break; @@ -729,9 +740,9 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) { if(data[3] == 1) // 停止配网 { - 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.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; } } @@ -1101,16 +1112,24 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) memcpy(common_setting.wifi_status.cur_pwd, wifi_pwd, wifi_pwd_len); common_setting.wifi_status.cur_pwd[wifi_pwd_len]='\0'; ESP_LOGI(COMMON_TAG, "%s wifi ssid len: %d\n",__func__, wifi_ssid_len); - + memset(common_storage.net_info.wifi_ssid,0,32); common_storage.net_info.wifi_ssid_len=wifi_ssid_len; - memcpy(common_storage.net_info.wifi_ssid, wifi_ssid, wifi_ssid_len); + memset(common_storage.net_info.wifi_pwd,0,64); memcpy(common_storage.net_info.wifi_pwd, wifi_pwd, wifi_pwd_len); common_storage.net_info.wifi_pwd_len=wifi_pwd_len; + common_storage.net_info.wifi_info_flag=1; + //保存flash后, + 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_LOGI(COMMON_TAG, "%s save wifi pwd success: %d\n",__func__, wifi_ssid_len); + }else{ + ESP_LOGI(COMMON_TAG, "%s save wifi pwd failed: %d\n",__func__, wifi_ssid_len); + } ESP_LOG_BUFFER_CHAR(COMMON_TAG, common_setting.wifi_status.cur_ssid, wifi_ssid_len); ESP_LOGI(COMMON_TAG, "%s wifi pwd len: %d\n",__func__, wifi_pwd_len); @@ -1118,7 +1137,9 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) ESP_LOGI(COMMON_TAG, "%s wifi flag: %d %d %d\n",__func__,common_setting.net_pairing_info.http_url_flag, common_setting.net_pairing_info.http_token_flag, common_setting.net_pairing_info.user_id_flag); - if(common_setting.net_pairing_info.http_url_flag && common_setting.net_pairing_info.http_token_flag && common_setting.net_pairing_info.user_id_flag) + //if(common_setting.net_pairing_info.http_url_flag && common_setting.net_pairing_info.http_token_flag && common_setting.net_pairing_info.user_id_flag) + + if(common_setting.net_pairing_info.http_url_flag && common_setting.net_pairing_info.http_token_flag) { if(data[4] == 0) { @@ -1277,6 +1298,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) http_end=1; memset(common_setting.net_pairing_info.http_url, 0x00, NET_PAIRING_INFO_MAX_LEN); net_pairing_reply.content[5] |= 0x20; + common_setting.net_pairing_info.http_url_flag = 0; ESP_LOGI(COMMON_TAG, "%s CRC错误: 计算=%04x 接收=%04x\n",__func__, calc_crc, packet_crc); } } @@ -1345,6 +1367,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) token_end=1; memset(common_setting.net_pairing_info.http_token, 0x00, NET_PAIRING_INFO_MAX_LEN); net_pairing_reply.content[5] |= 0x20; + common_setting.net_pairing_info.http_token_flag = 0; ESP_LOGI(COMMON_TAG, "%s CRC错误: 计算=%04x 接收=%04x",__func__, calc_crc, packet_crc); } } @@ -1387,6 +1410,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) else { common_setting.net_pairing_info.user_id=0; + common_setting.net_pairing_info.user_id_flag = 0; // 校验失败 net_pairing_reply.content[5] |= 0x20; ESP_LOGI(COMMON_TAG, "%s BLE user id crc16 err:%04x %04x\n", __func__,packet_crc, calc_crc); @@ -1428,9 +1452,9 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) if(data[4] == 1) // 停止配网 { // common_setting.net_pairing_info.net_pair_cooldown = 0; - 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.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; net_pairing_reply.content[0] = 1;//0成功 1失败 net_pairing_reply.content[1] = data[4]; //1停止配网 net_pairing_reply.len = 2; @@ -1447,7 +1471,10 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) if (len < 5) break; if(data[4] == 1) // 开启蓝牙配网 { - xEventGroupSetBits(ble_event_group, BLE_ON_BIT); + if(common_setting.ble_status.ble_state == 0){ + xEventGroupSetBits(ble_event_group, BLE_ON_BIT); + } + ESP_LOGI(COMMON_TAG, "%s ble on by app\n",__func__); } else if(data[4] == 0) // 关闭蓝牙配网 @@ -1486,11 +1513,9 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) ESP_LOG_BUFFER_CHAR(COMMON_TAG, common_setting.wifi_status.cur_ssid, 32); ESP_LOGI(COMMON_TAG, "%s wifi test len: %d\n",__func__, 64); ESP_LOG_BUFFER_CHAR(COMMON_TAG, common_setting.wifi_status.cur_pwd, 64); - common_setting.net_pairing_info.http_url_flag=1; - common_setting.net_pairing_info.http_token_flag=1; - common_setting.net_pairing_info.user_id_flag=1; - common_setting.ble_status.ble_state=0;// - xEventGroupSetBits(ble_event_group, BLE_ON_BIT);//打开蓝牙配网 + // common_setting.net_pairing_info.http_url_flag=1; + // common_setting.net_pairing_info.http_token_flag=1; + // common_setting.net_pairing_info.user_id_flag=1; Wifi_Queue_Send(HOST_TYPE_BLE,1,0); break; @@ -1523,53 +1548,85 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type) { //发送调试数据不需要crc校验 ESP_LOGI(COMMON_TAG, "%s no need checkcrc recv %d bytes:cmd %0x",__func__,len, data[1]); - // ESP_LOG_BUFFER_HEX(COMMON_TAG, data, len); - // BLE_SEND_REPLY_T net_pairing_reply = {0}; - // memset(&net_pairing_reply, 0x00, sizeof(BLE_SEND_REPLY_T)); - // char wifi_ssid[10+1] = {0}; - // char wifi_pwd[15+1] = {0}; - // switch (data[1]) - // { - // case 0xFE://fe + cmd+cmd1+dataLen+data+crc8 - // { + ESP_LOG_BUFFER_HEX(COMMON_TAG, data, len); + BLE_SEND_REPLY_T net_pairing_reply = {0}; + memset(&net_pairing_reply, 0x00, sizeof(BLE_SEND_REPLY_T)); + char wifi_ssid[10+1] = {0}; + char wifi_pwd[15+1] = {0}; + switch (data[1]) + { + case 0xFE://fe + cmd+cmd1+dataLen+data+crc8 + { - // switch (data[4]) - // { - // case 0x01://调试app下发wifi名称和密码重新连接新的wifi,不需要加密 - // //调试指令 - // if (len < 20) - // { - // ESP_LOGW(COMMON_TAG, "APP 0xFE len too short %u", len); - // break; - // } - // memcpy(wifi_ssid, &data[5], 10); - // wifi_ssid[10] = '\0'; - // memcpy(wifi_pwd, &data[15], 15); - // wifi_pwd[15] = '\0'; - // common_setting.wifi_status.cur_ssid_len = 10; - // memcpy(common_setting.wifi_status.cur_ssid, wifi_ssid, 10); - // common_setting.wifi_status.cur_pwd_len = 15; - // memcpy(common_setting.wifi_status.cur_pwd, wifi_pwd, 15); - // ESP_LOGI(COMMON_TAG, "wifi ssid len: %d", 10); - // ESP_LOG_BUFFER_CHAR(COMMON_TAG, common_setting.wifi_status.cur_ssid, 10); - // ESP_LOGI(COMMON_TAG, "wifi pwd len: %d", 15); - // ESP_LOG_BUFFER_CHAR(COMMON_TAG, common_setting.wifi_status.cur_pwd, 15); - // ESP_LOGI(COMMON_TAG, "wifi test start"); - // Wifi_Queue_Send(host_type, 1, 0); - // /* code */ - // break; + switch (data[4]) + { + case 0x01://调试app下发wifi名称和密码重新连接新的wifi,不需要加密 + //调试指令 + if (len < 20) + { + ESP_LOGW(COMMON_TAG, "APP 0xFE len too short %u", len); + break; + } + memcpy(wifi_ssid, &data[5], 10); + wifi_ssid[10] = '\0'; + memcpy(wifi_pwd, &data[15], 15); + wifi_pwd[15] = '\0'; + common_setting.wifi_status.cur_ssid_len = 10; + memcpy(common_setting.wifi_status.cur_ssid, wifi_ssid, 10); + common_setting.wifi_status.cur_pwd_len = 15; + memcpy(common_setting.wifi_status.cur_pwd, wifi_pwd, 15); + ESP_LOGI(COMMON_TAG, "wifi ssid len: %d", 10); + ESP_LOG_BUFFER_CHAR(COMMON_TAG, common_setting.wifi_status.cur_ssid, 10); + ESP_LOGI(COMMON_TAG, "wifi pwd len: %d", 15); + ESP_LOG_BUFFER_CHAR(COMMON_TAG, common_setting.wifi_status.cur_pwd, 15); + ESP_LOGI(COMMON_TAG, "wifi test start"); + Wifi_Queue_Send(host_type, 1, 0); + /* code */ + break; - // default: - // break; - // } + default: + break; + } - // } - // break; + } + break; - // default: - // break; - // } + default: + break; + } } } +#include "esp_chip_info.h" +//获取模组的芯片型号 +static const char *CHIP_LOG = "CHIP_TAG"; +void print_chip_info(void) +{ + // 定义 + 获取芯片信息 + esp_chip_info_t chip_info; + esp_chip_info(&chip_info); + + // 打印型号(数字 → 转成文字) + const char *chip_name = "Unknown"; + switch(chip_info.model) { + case CHIP_ESP32: chip_name = "ESP32"; break; + case CHIP_ESP32S2: chip_name = "ESP32-S2"; break; + case CHIP_ESP32S3: chip_name = "ESP32-S3"; break; + case CHIP_ESP32C2: chip_name = "ESP32-C2"; break; + case CHIP_ESP32C3: chip_name = "ESP32-C3"; break; + case CHIP_ESP32C6: chip_name = "ESP32-C6"; break; + case CHIP_ESP32H2: chip_name = "ESP32-H2"; break; + case CHIP_ESP32P4: chip_name = "ESP32-P4"; break; + case CHIP_ESP32C61: chip_name = "ESP32-C61"; break; + case CHIP_POSIX_LINUX: chip_name = "POSIX_LINUX"; break; + default: + break; + } + // 最终清晰打印 + ESP_LOGI(CHIP_LOG, "======================================"); + ESP_LOGI(CHIP_LOG, "芯片型号: %s", chip_name); + ESP_LOGI(CHIP_LOG, "芯片版本: 0x%04x", chip_info.revision); + ESP_LOGI(CHIP_LOG, "核心数量: %d", chip_info.cores); + ESP_LOGI(CHIP_LOG, "======================================"); +} \ No newline at end of file diff --git a/main/project_common.h b/main/project_common.h index 6f7a923..6ba23a1 100644 --- a/main/project_common.h +++ b/main/project_common.h @@ -52,17 +52,18 @@ // Project #define FW_VERSION (0x0001) // 固件版本 -#define BUILD_VERSION (0x0004) // 编译测试版本 +#define BUILD_VERSION (0x0005) // 编译测试版本 #define DEVICE_VID (0x36E6) // 设备厂商代码 #define DEVICE_PID (0xEC01) // 设备型号代码 #define LWIP_LOCAL_HOSTNAME "PXN-Device" // WIFI本地主机名称 #define FW_HEADER_CODE_SIZE 12 #define FW_HEADER_CODE "WGTL_ESP32C2" #define IOT_ENABLE 1 -#define USER_TEST_ENABLE 1 +#define USER_TEST_ENABLE 0 #define BLE_TEST_WIFI_CONNECT_ENABLE 1//BLE下发wifixin不需要做htttp token user_id校验 #define IS_CLOSE_BLE 0 //是否关闭蓝牙广播暂时测试不关闭 一直广播 - +#define NIMBLE_FLOW_MODE 0 //1连上wifi不关闭蓝牙配网 直接请求http 0连上wifi关闭配网 再请求http为了节省空间 +#define OPEN_HTTPS_OTA_FUNC 0 //是否开启HTTPS_OTA升级 #define WIFI_SSID_MAX_LEN 32 #define WIFI_PWD_MAX_LEN 64 /**************** Task ****************/ @@ -103,6 +104,12 @@ extern StaticTask_t wifi_scan_task_tcb; // extern TaskHandle_t http_download_crt_task_handler; // extern StackType_t http_download_crt_task_stack[HTTP_DOWNLOAD_CRT_TASK_STACK]; // extern StaticTask_t http_download_crt_task_tcb; +// WIFI + +#define MQTT_CONNECT_TASK_STACK (0) +extern TaskHandle_t mqtt_connect_task_handler; +extern StackType_t mqtt_connect_task_stack[MQTT_CONNECT_TASK_STACK]; +extern StaticTask_t mqtt_connect_task_tcb; #define NET_PAIRING_INFO_MAX_LEN 256 #define SERIAL_NUMBER_MAX_LEN 32 @@ -145,6 +152,8 @@ typedef enum COMMON_HTTP_DOWNLOAD_BIT = BIT1, COMMON_MQTT_CONNECT_BIT = BIT2, COMMON_MQTT_DISCONNECT_BIT = BIT3, + COMMON_MQTT_INIT_BIT = BIT4, + COMMON_HTTPS_OTA_BIT = BIT5, }COMMON_EVENT_GROUP_BIT_E; typedef enum @@ -233,6 +242,7 @@ typedef struct typedef struct { uint8_t ble_state; // 0:关闭 1:广播 2:已连接 + uint8_t nimble_flag;//1打开配网 0关闭配网 }__attribute__((packed)) BLE_STATUS_T; typedef struct @@ -279,13 +289,19 @@ typedef struct uint32_t user_id; char mqtt_sub_url[MQTT_SUB_URL_MAX_LEN+1]; }__attribute__((packed)) NET_PAIR_INFO_T; - +typedef struct +{ + uint8_t https_ota_start_flag;//需要升级1 0不需要升级 + + +}__attribute__((packed)) HTTPS_OTA_CONFIG_INFO_T; typedef struct { uint8_t heart_beat_enable:1; uint8_t mcu_info_got_flag:1; uint8_t reserved:6; - + uint8_t mqtt_start_flag; + uint8_t mqtt_connect_flag; // UNIVERSAL uint32_t heart_beat_interval; // ms MCU_INFO_T mcu_info; @@ -306,6 +322,8 @@ typedef struct // MQTT esp_mqtt_client_config_t mqtt_cfg; esp_mqtt_client_handle_t mqtt_client_handle; + + HTTPS_OTA_CONFIG_INFO_T https_ota_cfg; }COMMON_SETTING_T; #define WIFI_QUEUE_LENGTH 2 diff --git a/main/user_flash.c b/main/user_flash.c index 40281f7..6a71776 100644 --- a/main/user_flash.c +++ b/main/user_flash.c @@ -15,40 +15,51 @@ void User_Flash_Check(void) //寻找分区表 partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, PARA_FLASH_NAME); assert(partition != NULL); - + // User_Flash_Erase(UF_OFFSET_USER_CONFIG_INFO_ADDR,FLASH_SEC_SIZE); if(User_Flash_Read_Check(UF_OFFSET_USER_CONFIG_INFO_ADDR, UF_OFFSET_USER_CONFIG_INFO_FLAG, &common_storage.net_info, sizeof(NET_INFO_T)) != ESP_OK) { printf("%s read common_storage failed\n",__func__); common_storage.net_info.wifi_info_flag = 0; common_storage.net_info.device_id_flag = 0; common_storage.net_info.serial_number_flag=0; - + memset(&common_storage.net_info,0x00,sizeof(NET_INFO_T)); }else{ - printf("%s read serialNumber %s\n",__func__,common_storage.net_info.serial_number); + printf("%s read serialNumber %s %d wifi_info_flag:%d\n",__func__,common_storage.net_info.serial_number,common_storage.net_info.serial_number_flag,common_storage.net_info.wifi_info_flag); } - - 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) + // User_Flash_Erase(UF_OFFSET_CERT_CLIENT_CRT_ADDR,FLASH_SEC_SIZE); + memset(&common_storage.ssl_cert[SSL_CLIENT_CRT].content,0x00,sizeof(common_storage.ssl_cert[SSL_CLIENT_CRT].content)); + 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) { common_storage.ssl_cert[SSL_CLIENT_CRT].length = 0; - printf("%s read client_ssl_cer failed\n",__func__); + memset(&common_storage.ssl_cert[SSL_CLIENT_CRT].content,0x00,sizeof(common_storage.ssl_cert[SSL_CLIENT_CRT].content)); + printf(" read client_ssl_cer failed\n"); }else{ - printf("%s read client_ssl_cer success %d\n",__func__,common_storage.ssl_cert[SSL_CLIENT_CRT].length); + printf("read client_ssl_cer success 0 %d\n",common_storage.ssl_cert[SSL_CLIENT_CRT].length); + } - 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) + // User_Flash_Erase(UF_OFFSET_CERT_CLIENT_KEY_ADDR,FLASH_SEC_SIZE); + memset(&common_storage.ssl_cert[SSL_CLIENT_KEY].content,0x00,sizeof(common_storage.ssl_cert[SSL_CLIENT_KEY].content)); + 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) { common_storage.ssl_cert[SSL_CLIENT_KEY].length = 0; - printf("%s read client_ssl_cer_key failed\n",__func__); + printf("read client_ssl_cer_key failed\n"); + memset(&common_storage.ssl_cert[SSL_CLIENT_KEY].content,0x00,sizeof(common_storage.ssl_cert[SSL_CLIENT_KEY].content)); } else{ - printf("%s read client_ssl_cer success %d\n",__func__,common_storage.ssl_cert[SSL_CLIENT_KEY].length); + printf("read client_ssl_cer success 1 %d\n",common_storage.ssl_cert[SSL_CLIENT_KEY].length); + } - 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) + // User_Flash_Erase(UF_OFFSET_CERT_SERVER_CRT_ADDR,FLASH_SEC_SIZE); + memset(&common_storage.ssl_cert[SSL_SERVER_CRT].content,0x00,sizeof(common_storage.ssl_cert[SSL_SERVER_CRT].content)); + 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) { common_storage.ssl_cert[SSL_SERVER_CRT].length = 0; - printf("%s read server_ssl_cer failed\n",__func__); + memset(&common_storage.ssl_cert[SSL_SERVER_CRT].content,0x00,sizeof(common_storage.ssl_cert[SSL_SERVER_CRT].content)); + printf("read server_ssl_cer failed\n"); }else{ - printf("%s read client_ssl_cer success %d\n",__func__,common_storage.ssl_cert[SSL_CLIENT_KEY].length); + printf("read client_ssl_cer success 2 %d\n",common_storage.ssl_cert[SSL_SERVER_CRT].length); + } } diff --git a/main/user_flash.h b/main/user_flash.h index fc9b521..ba1019f 100644 --- a/main/user_flash.h +++ b/main/user_flash.h @@ -33,5 +33,5 @@ void User_Flash_Check(void); esp_err_t User_Flash_Write_With_Flag(uint32_t addr, uint32_t flag, void* data, uint16_t size); esp_err_t User_Flash_Read_Check(uint32_t addr, uint32_t flag, void* buf, uint16_t size); - +esp_err_t User_Flash_Erase(size_t offset, size_t size); #endif diff --git a/main/wifi.c b/main/wifi.c index 43e22b5..8e8575e 100644 --- a/main/wifi.c +++ b/main/wifi.c @@ -8,7 +8,8 @@ */ #include #include "project_common.h" - +#include "esp_https_ota.h" +#include "https_ota.h" // #include "freertos/FreeRTOS.h" // #include "freertos/task.h" // #include "freertos/event_groups.h" @@ -45,7 +46,6 @@ static uint32_t wifi_connect_retry_max = 0xFFFFFFFF;//EXAMPLE_ESP_MAXIMUM_RETRY; SemaphoreHandle_t wifi_scan_mutex; - //获取wifi的rssi事件处理函数 static void esp_bss_rssi_low_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { @@ -59,14 +59,14 @@ static void esp_bss_rssi_low_handler(void* arg, esp_event_base_t event_base, int static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { - ESP_LOGE(WIFI_TAG, "event_handler %" PRId32 "\n", event_id); + // ESP_LOGE(WIFI_TAG, "event_handler %" PRId32 "\n", event_id); if(event_base == WIFI_EVENT) { switch(event_id) { case WIFI_EVENT_STA_START: { - if(common_setting.ble_status.ble_state > 0)//只有蓝牙开启才能连接wifi,其他情况不连接wifi,除非重启设备或者app下发wifi信息重新配网 + if(1)// { esp_wifi_connect(); common_setting.wifi_status.wifi_state = 0; @@ -132,12 +132,71 @@ static void event_handler(void* arg, esp_event_base_t event_base, s_retry_num = 0; common_setting.wifi_status.wifi_state = 1; Wifi_Connect_Timerout_Stop();//连接成功了就关闭定时器 + #if OPEN_HTTPS_OTA_FUNC + if(!common_setting.ble_status.nimble_flag && common_setting.mqtt_start_flag == 1) //在配网状态下还不能ota升级只有配完网之后 有证书 wifi密码 + { + xEventGroupSetBits(common_event_group, COMMON_HTTPS_OTA_BIT); + } + + #endif + #if NIMBLE_FLOW_MODE xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + #else + ESP_LOGI(WIFI_TAG, "got common_setting.ble_status.nimble_flag:%d",common_setting.ble_status.nimble_flag); + if(common_setting.ble_status.nimble_flag)//在配网状态不是普通wifi连接 + { + //要先关闭配网再发送ble数据 + ESP_LOGI(WIFI_TAG, "start http get put1 :\n"); + xEventGroupSetBits(ble_event_group, BLE_OFF_BIT); + ESP_LOGI(WIFI_TAG, "start http get put2 :\n"); + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + } + else{ + ESP_LOGI(WIFI_TAG, "start http get common_setting.wifi_status.connect_mode %d\n",common_setting.wifi_status.connect_mode); + if(common_setting.wifi_status.connect_mode == 3 || common_setting.mqtt_start_flag == 1)//已经连过一次了 + { + xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT); + } + } + #endif + + esp_wifi_set_rssi_threshold(WIFI_RSSI_THRESHOLD); } break; } } + else if (event_base == ESP_HTTPS_OTA_EVENT) { + switch (event_id) { + case ESP_HTTPS_OTA_START: + ESP_LOGI(WIFI_TAG, "OTA started"); + break; + case ESP_HTTPS_OTA_CONNECTED: + ESP_LOGI(WIFI_TAG, "Connected to server"); + break; + case ESP_HTTPS_OTA_GET_IMG_DESC: + ESP_LOGI(WIFI_TAG, "Reading Image Description"); + break; + case ESP_HTTPS_OTA_VERIFY_CHIP_ID: + ESP_LOGI(WIFI_TAG, "Verifying chip id of new image: %d", *(esp_chip_id_t *)event_data); + break; + case ESP_HTTPS_OTA_DECRYPT_CB: + ESP_LOGI(WIFI_TAG, "Callback to decrypt function"); + break; + case ESP_HTTPS_OTA_WRITE_FLASH: + ESP_LOGI(WIFI_TAG, "Writing to flash: %d written", *(int *)event_data); + break; + case ESP_HTTPS_OTA_UPDATE_BOOT_PARTITION: + ESP_LOGI(WIFI_TAG, "Boot partition updated. Next Partition: %d", *(esp_partition_subtype_t *)event_data); + break; + case ESP_HTTPS_OTA_FINISH: + ESP_LOGI(WIFI_TAG, "OTA finish"); + break; + case ESP_HTTPS_OTA_ABORT: + ESP_LOGI(WIFI_TAG, "OTA abort"); + break; + } + } } void Wifi_Sta_Init(void) @@ -165,6 +224,8 @@ void Wifi_Sta_Init(void) NULL, &instance_got_ip)); + ESP_ERROR_CHECK(esp_event_handler_register(ESP_HTTPS_OTA_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_BSS_RSSI_LOW, &esp_bss_rssi_low_handler, NULL)); // wifi_config_t wifi_config = { @@ -218,8 +279,6 @@ void Wifi_Connect_Timerout_Stop(void) // Stop and delete the timer if it's still running wifi_one_timer_flag = 0; esp_timer_stop(wifi_one_timer_handle); - esp_timer_delete(wifi_one_timer_handle); - } } void Wifi_Start(void) @@ -232,6 +291,12 @@ void Wifi_Start(void) // memcpy(common_setting.wifi_config.sta.password, "PXN201807", 10); // memcpy(common_setting.wifi_config.sta.ssid, "ChinaNet-LaDe", 13); // memcpy(common_setting.wifi_config.sta.password, "9596w3wq", 9); + ESP_LOGI(WIFI_TAG, "[APP] Free memory wifi len on: %d %d\n",common_setting.wifi_status.cur_ssid_len,common_setting.wifi_status.cur_pwd_len); + if(common_setting.wifi_status.cur_ssid_len == 0 || common_setting.wifi_status.cur_pwd_len == 0) + { + ESP_LOGI(WIFI_TAG, "[APP] Free memory not wifi on: %" PRIu32 " bytes", esp_get_free_heap_size()); + return; + } memcpy(common_setting.wifi_config.sta.ssid, common_setting.wifi_status.cur_ssid, common_setting.wifi_status.cur_ssid_len); memcpy(common_setting.wifi_config.sta.password, common_setting.wifi_status.cur_pwd, common_setting.wifi_status.cur_pwd_len); @@ -258,9 +323,9 @@ void Wifi_Stop(void) xSemaphoreGive(wifi_scan_mutex); common_setting.wifi_status.wifi_mode = 0; common_setting.wifi_status.wifi_state = 0; - 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.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; } } @@ -552,11 +617,11 @@ void Wifi_Enable_Status_Check(void) bits_last = (bits&(WIFI_CONNECTED_BIT | WIFI_FAIL_BIT)); if (bits & WIFI_CONNECTED_BIT) { - ESP_LOGI(WIFI_TAG, "wifi connected\n"); - + ESP_LOGI(WIFI_TAG, "wifi connected %d\n",common_setting.wifi_status.connect_mode); + + #if (!OPEN_HTTPS_OTA_FUNC) if((common_setting.wifi_status.connect_mode == 1 || common_setting.wifi_status.connect_mode == 2)) { - static BLE_SEND_REPLY_T wifi_pairing_reply={0}; wifi_pairing_reply.send_flag = 1; wifi_pairing_reply.cmd = 0x15; @@ -565,6 +630,7 @@ void Wifi_Enable_Status_Check(void) wifi_pairing_reply.content[1] = common_setting.wifi_status.connect_mode-1; wifi_pairing_reply.content[2] = common_setting.wifi_status.net_pair_encrypt_profile; Check_Host_Type(wifi_pairing_reply.cmd, wifi_pairing_reply.content, wifi_pairing_reply.len, common_setting.wifi_status.net_pair_host); + ESP_LOGI(WIFI_TAG, "connect_modeCheck_Host_Type\n"); Wifi_Connect_Timerout_Stop();//关闭定时器 if (USER_TEST_ENABLE) { @@ -591,13 +657,11 @@ void Wifi_Enable_Status_Check(void) // memcpy(&http_get_cert_task_param.url[SSL_SERVER_CRT][0], "https://e-pxn.com.cn/pms/api/mosquitto.org.crt", 47); // http_download_crt_task_handler = xTaskCreateStatic(http_download_task, "http_download_task", HTTP_DOWNLOAD_CRT_TASK_STACK, NULL, 2, http_download_crt_task_stack, &http_download_crt_task_tcb); } + } - - - // if(common_setting.mqtt_client_handle != NULL) - // { - // // esp_mqtt_client_start(common_setting.mqtt_client_handle); - // } + #else + https_ota_task(); + #endif } else if (bits & WIFI_FAIL_BIT) { @@ -625,10 +689,6 @@ void Wifi_Enable_Status_Check(void) wifi_pairing_reply.content[2] = common_setting.wifi_status.net_pair_encrypt_profile; Check_Host_Type(wifi_pairing_reply.cmd, wifi_pairing_reply.content, wifi_pairing_reply.len, common_setting.wifi_status.net_pair_host); } - // if(common_setting.mqtt_client_handle != NULL) - // { - // // esp_mqtt_client_stop(common_setting.mqtt_client_handle); - // } } else {