if(onesec_timer_count == 20){
// N455_Ota_Start();
+
+ // Https_Ota_Start();
}
}
if(common_setting.heart_beat_enable == 1 && common_setting.heart_beat_interval != 0)
{
static uint32_t heart_beat_tick = 0;
+
heart_beat_tick += MAIN_LOOP_INTERVAL;
if(heart_beat_tick >= common_setting.heart_beat_interval)
{
Uart_Reply_Send(COMMON_UART_HEART_CMD, NULL, 0, HOST_TYPE_UART);//(cmd, pbuf, len, host_type);
#endif
// Check_Host_Type(0xFF, heart_beat_data, sizeof(heart
-
ESP_LOGI(MAIN_TAG, "send heart :%d\n",common_setting.mcu_info_got_flag);
if(common_setting.mcu_info_got_flag == 0)//没有收到主控发过来的mcu信息 一开机获取一次通过0xff 主控收到0xff就发送0x01 0x0A 0X0C对应的指令
{
static uint32_t poweron_count = 0;
poweron_count += MAIN_LOOP_INTERVAL;
- if(poweron_count >= 5000)
+ if(poweron_count >= 2000)
{
poweron_count=0;
if(common_setting.mcu_info_got_flag == 0)//没有收到主控发过来的mcu信息 一开机获取一次通过0xff 主控收到0xff就发送0x01 0x0A 0X0C对应的指令
return;
}
check_mcu_init=1;
- common_setting.ble_status.nimble_flag=0; //代表当前配网成功
+
if(common_setting.mcu_info_got_flag == 1) //flag=2uart发送的信息 并且没有连接wifi
{
//无serial_number wifi info 并且蓝牙关闭状态开启蓝牙配网功能
// 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)
{
}
}
}
+int Ble_Start(void)
+{
+ if(common_setting.ble_status.ble_adv_flag == 0)//蓝牙开始广播并打开蓝牙开始配网
+ {
+ // 开始配网
+ common_setting.ble_status.ble_adv_flag = 1;
+ common_setting.ble_status.ble_state = 1;
+ common_setting.ble_enable_tick = 0;
+ common_setting.ble_status.nimble_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;
+ // 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());
+ }else{
+ return 0;
+ }
+ return 1;
+}
+
+int Ble_Stop(void)
+{
+ if (common_setting.ble_status.ble_adv_flag > 0) {
+
+ int ret = nimble_port_stop();
+ if (ret == 0)
+ {
+ if(ble_task_handler != NULL)
+ {
+ vTaskDelete(ble_task_handler);
+ ble_task_handler = NULL;
+ }
+ nimble_port_deinit();
+ common_setting.ble_status.ble_state = 0;
+ common_setting.ble_status.ble_adv_flag = 0;
+ ESP_LOGI(BLE_TAG, "BLE stopped ok");
+ }
+ else
+ {
+ return -1;
+ }
+ }else{
+ ESP_LOGE(BLE_TAG, "BLE stop fail");
+ }
+ return 1;
+}
/*
func:ble开关状态检查
BLE_ON_BIT:开广播
EventBits_t ble_bits = xEventGroupWaitBits(ble_event_group, BLE_ON_BIT | BLE_OFF_BIT, pdFALSE, pdFALSE, 0);
if((ble_bits & BLE_ON_BIT) != 0)
{
- ESP_LOGI(BLE_TAG, "BLE_ON_BIT: %d", common_setting.ble_status.ble_adv_flag);
+ ESP_LOGI(BLE_TAG, "start_ble: %d", common_setting.ble_status.ble_adv_flag);
xEventGroupClearBits(ble_event_group, BLE_ON_BIT);
- if(common_setting.ble_status.ble_adv_flag == 0)//蓝牙开始广播并打开蓝牙开始配网
- {
- // 开始配网
- common_setting.ble_status.nimble_flag = 0; //清除配网成功标识
- common_setting.ble_status.ble_adv_flag = 1;
- common_setting.ble_status.ble_state = 1;
- common_setting.ble_enable_tick = 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;
- // 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());
- }
+ Ble_Start();
}
else if((ble_bits & BLE_OFF_BIT) != 0)
{
ESP_LOGI(BLE_TAG, "stop_ble: %d", common_setting.ble_status.ble_adv_flag);
xEventGroupClearBits(ble_event_group, BLE_OFF_BIT);
- if(common_setting.ble_status.ble_adv_flag > 0) //关闭蓝牙广播
- {
- // 停止配网
- int ret = nimble_port_stop();
- if (ret == 0)
- {
- if(ble_task_handler != NULL)
- {
- vTaskDelete(ble_task_handler);
- }
- nimble_port_deinit();
- common_setting.ble_status.ble_state = 0;
- common_setting.ble_enable_tick = 0;
- common_setting.ble_status.ble_adv_flag=0;
- ESP_LOGI(BLE_TAG, "nimble_port_stop\n");
- }
- else
- {
- ESP_LOGI(BLE_TAG, "nimble_port_stop fail\n");
- }
- // ESP_LOGI(BLE_TAG, "[APP] Free memory ble off: %" PRIu32 " bytes", esp_get_free_heap_size());
- }
+ Ble_Stop();
}
// 30分钟无配网动作则关闭蓝牙
if(common_setting.ble_status.ble_adv_flag > 0)
{
common_setting.ble_enable_tick += MAIN_LOOP_INTERVAL;
- if(common_setting.ble_enable_tick > (30*60*1000))
+ if(common_setting.ble_enable_tick > 30*60*1000)
{
- // Ble_Queue_Send(0xFF, 0xFF);
- xEventGroupSetBits(ble_event_group, BLE_OFF_BIT);
+ int ret = Ble_Stop();
+ ESP_LOGI(BLE_TAG,"BLE_TAG timout=%d",ret);
+ 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 && \
+ common_storage.net_info.serial_number_flag == 1 && common_storage.net_info.wifi_info_flag == 1)
+ {
+ 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);
+ common_setting.ble_status.nimble_flag = 1;
+ Wifi_Queue_Send(0, 3, 0);
+ }
common_setting.ble_enable_tick = 0;
}
}
-
}
#endif
// void Ble_Queue_Send(uint8_t host_type, uint8_t enable);
void Ble_Enable_Status_Check(void);
void Ble_Start_Nimble_NetWork(void);
+int Ble_Stop(void);
+int Ble_Start(void);
#endif
#endif
}
if(!ble_connected_executed){
ble_connected_executed=1;
- common_setting.ble_enable_tick = 0;
common_setting.ble_status.ble_state = 2;
Uart_Set_Ble_Status();
}
event->conn_update.status);
if(!ble_connected_executed){
ble_connected_executed=1;
- common_setting.ble_enable_tick = 0;
common_setting.ble_status.ble_state = 2;
Uart_Set_Ble_Status();
}
volatile uint16_t ota_faile_count;//失败计数器
uint8_t https_update_ota_flag;//根据版本号是否需要升级
uint8_t https_ota_init;//ota升级task初始化
+ uint8_t ota_auto;//是否自动升级
}__attribute__((packed)) https_ota_ctx_t;
static https_ota_ctx_t s_ota_ctx = { 0 };
-
-#define HTTPS_OTA_RETRY_NUM 3
+static uint8_t ota_retry_cnt = 0;
+#define HTTPS_OTA_RETRY_NUM 2
#define OTA_URL_SIZE 128
// OTA 分片大小,建议 4KB / 8KB,兼顾效率与栈/内存
#define OTA_DOWNLOAD_BUF_SIZE (4096)
// HTTP 单次超时(毫秒),核心防阻塞
-#define OTA_HTTP_TIMEOUT_MS (6000)
-// 连续无数据最大次数(判定网络卡死)
-#define OTA_STUCK_MAX_CNT (8)
-// OTA 最大重试次数
-#define OTA_SELF_MAX_RETRY (3)
+#define OTA_HTTP_TIMEOUT_MS (1500)
// 下载缓冲区
+#define HTTP_MALLOC_DOWNLOAD 0
+static uint8_t auto_upgrae = 0;
+#if HTTP_MALLOC_DOWNLOAD
+static uint8_t *ota_download_buf=NULL;
+#else
+
static uint8_t ota_download_buf[OTA_DOWNLOAD_BUF_SIZE];
+#endif
static esp_err_t self_ota_download_and_write(const char *url)
{
esp_err_t ret = ESP_FAIL;
goto clean_exit;
}
+ #if HTTP_MALLOC_DOWNLOAD
+ const int BUF_SIZE = 8192;
+ if(ota_download_buf != NULL)
+ {
+ free(ota_download_buf);
+ ota_download_buf = NULL;
+ }
+ ota_download_buf = NULL;
+ ota_download_buf = malloc(BUF_SIZE);
+ if(ota_download_buf == NULL)
+ {
+ ret=ESP_FAIL;
+ goto clean_exit;
+ }
+ #endif
// s_self_ota_state = SELF_OTA_DOWNLOADING;
ESP_LOGI(HTTPS_OTA, "Start self ota download");
-
+ uint8_t err_retry = 0;
// 5. 循环分片读取 + 写入 Flash(核心防阻塞循环)
+ s_ota_ctx.ota_timeout_count = HTTPS_OTA_TIMEOUT_TIME;
while (1)
{
// 上层主动停止标记(对应你原来的 ota_timeout_req)
}
// 单次读取分片数据
+ #if HTTP_MALLOC_DOWNLOAD
+ int read_len = esp_http_client_read(http_client, (char *)ota_download_buf, BUF_SIZE);
+ #else
int read_len = esp_http_client_read(http_client, (char *)ota_download_buf, OTA_DOWNLOAD_BUF_SIZE);
+ #endif
if (read_len < 0) {
- ESP_LOGE(HTTPS_OTA, "http read error");
+ if(err_retry < 5)
+ {
+ err_retry++;
+ vTaskDelay(10);
+ continue;
+ }
ret = ESP_FAIL;
break;
}
}
break;
}
+ err_retry = 0;
total_read += read_len;
// 第一次读到数据,校验固件头部
goto clean_exit;
} else {
// 切换启动分区
- ret = esp_ota_set_boot_partition(ota_part);
- if (ret != ESP_OK) {
- // s_self_ota_state = SELF_OTA_SUCCESS;
- ESP_LOGI(HTTPS_OTA, "OTA set boot partition ok");
- goto clean_exit;
+ if(!s_ota_ctx.ota_auto)
+ {
+ ret = esp_ota_set_boot_partition(ota_part);
+ if (ret != ESP_OK) {
+ // s_self_ota_state = SELF_OTA_SUCCESS;
+ ESP_LOGI(HTTPS_OTA, "OTA set boot partition ok");
+ goto clean_exit;
+ }
}
ota_handle=0;
}
static void Https_Ota_Task_Cb(void *pvParameter)
{
Mqtt_Disconnect();
- vTaskDelay(200 / portTICK_PERIOD_MS);
+ vTaskDelay(100 / portTICK_PERIOD_MS);
ESP_LOGI(HTTPS_OTA, "Self OTA start, url: %s", https_ota_url);
- g_ota_status = HTTPS_OTA_STATUS_UPGRADING;
- Https_Ota_Report_Status(HTTPS_OTA_STATUS_UPGRADING);
+ int ret = Https_Ota_Report_Status(HTTPS_OTA_STATUS_UPGRADING);
+ if(!ret) //上报升级中的状态失败不再继续ota升级
+ {
+ Https_Ota_Clear_Upgrade_Flag();
+ xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
+ vTaskDelete(NULL);
+ return;
+ }
esp_err_t ota_ret = ESP_FAIL;
uint8_t try_idx = 0;
bool ota_success = false;
- s_ota_ctx.ota_timeout_count = HTTPS_OTA_TIMEOUT_TIME;
+
// 多层重试
for (try_idx = 0; try_idx < HTTPS_OTA_MAX_RETRY_CNT; try_idx++)
{
+ s_ota_ctx.ota_timeout_count = HTTPS_OTA_TIMEOUT_TIME;
ESP_LOGI(HTTPS_OTA, "==== Self OTA retry %d/%d ====", try_idx+1, HTTPS_OTA_MAX_RETRY_CNT);
if (s_ota_ctx.ota_timeout_req) {
ota_success = false;
}
vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
}
-
+ #if HTTP_MALLOC_DOWNLOAD
+ if(ota_download_buf != NULL)
+ {
+ free(ota_download_buf);
+ ota_download_buf = NULL;
+ }
+ #endif
// ========== 分支处理:成功 / 失败 ==========
if (ota_success)
{
Https_Ota_Clear_Upgrade_Flag();
esp_restart(); // 正常重启
#else
- // 保留你原有 N455 联动逻辑
+ // N455 联动逻辑
if(N455_Ota_Get_Update_Flag())
{
if(N455_Ota_Task() == 1)
}else{
Https_Ota_Report_Status(HTTPS_OTA_STATUS_SUCCESS);
Https_Ota_Clear_Upgrade_Flag();
- esp_restart();
+ if(!s_ota_ctx.ota_auto){ //手动更新ota
+ Uart_Reply_Send(COMMON_UART_OTA_RESTET_N455,NULL, 0, HOST_TYPE_UART);
+ vTaskDelay(pdMS_TO_TICKS(100));
+ esp_restart();
+ }
}
#endif
vTaskDelete(NULL);
vTaskDelete(NULL);
}
}
+
+
#endif
#ifndef HTTPS_OTA_USER_CILENT
#ifndef HTTPS_OTA_IS_RETRY_FUNC
https_ota_handle=NULL;
ESP_LOGI(HTTPS_OTA, "Starting Advanced OTA example:%s\n",https_ota_url);
g_ota_status = HTTPS_OTA_STATUS_UPGRADING;
- Https_Ota_Report_Status(HTTPS_OTA_STATUS_UPGRADING);
-
+ if(!s_ota_ctx.ota_auto)
+ {
+ int ret = Https_Ota_Report_Status(HTTPS_OTA_STATUS_UPGRADING);
+ if(!ret)
+ {
+ Https_Ota_Clear_Upgrade_Flag();
+ xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
+ vTaskDelete(NULL);
+ return;
+ }
+ }
+
vTaskDelay(50 / portTICK_PERIOD_MS);
esp_err_t ota_finish_err = ESP_OK;
bool ota_success = false;
- s_ota_ctx.ota_timeout_count = HTTPS_OTA_TIMEOUT_TIME;
+
// for统一3次重试
uint8_t file_header_check = 0;
- for(uint8_t try_idx = 0; try_idx < HTTPS_OTA_MAX_RETRY_CNT; try_idx++)
+ for(uint8_t try_idx = 0; try_idx < HTTPS_OTA_RETRY_NUM; try_idx++)
{
+ s_ota_ctx.ota_timeout_count = HTTPS_OTA_TIMEOUT_TIME;
+ if (https_ota_url[0] == '\0')
+ {
+ ESP_LOGE(HTTPS_OTA, "OTA URL is empty, abort this retry");
+
+ continue;
+ }
+ s_ota_ctx.ota_timeout_req = 0;
file_header_check = 0;
ota_success = false;
ESP_LOGI(HTTPS_OTA,"==== OTA try %d/%d ====",try_idx+1,HTTPS_OTA_MAX_RETRY_CNT);
esp_http_client_config_t config = {
.url = https_ota_url,
- .timeout_ms = 3000,
+ .timeout_ms = 1500,
.keep_alive_enable = false,
};
config.crt_bundle_attach = esp_crt_bundle_attach;
esp_err_t err = ESP_FAIL;
// 先释放上一轮句柄
- if (https_ota_handle != NULL)
+ for(uint8_t i = 0;i < 3;i++)
{
- esp_https_ota_abort(https_ota_handle);
+ if (https_ota_handle != NULL)
+ {
+ esp_https_ota_abort(https_ota_handle);
+ https_ota_handle = NULL;
+ }
https_ota_handle = NULL;
- }
- https_ota_handle = NULL;
- err = esp_https_ota_begin(&ota_config, &https_ota_handle);
+
+ err = esp_https_ota_begin(&ota_config, &https_ota_handle);
+ if(err == ESP_OK)
+ {
+ ESP_LOGE(HTTPS_OTA, "connect ok");
+ break;
+ }else{
+ ESP_LOGE(HTTPS_OTA, "connect failed");
+ vTaskDelay(pdMS_TO_TICKS(100));
+ }
+ }
if(err != ESP_OK)
{
- ESP_LOGI(HTTPS_OTA, "esp_https_ota_begin failed, try:%d", try_idx+1);
+ ESP_LOGE(HTTPS_OTA, "esp_https_ota_begin failed");
vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
continue;
}
-
err = ESP_FAIL;
esp_app_desc_t app_desc;
err = esp_https_ota_get_img_desc(https_ota_handle, &app_desc);
ESP_LOGI(HTTPS_OTA, "image header verification success");
}
+ s_ota_ctx.ota_timeout_count = HTTPS_OTA_TIMEOUT_TIME;
+ s_ota_ctx.ota_timeout_req = 0;
// 下载循环
bool download_abort = false;
while (1) {
break;
}
err = esp_https_ota_perform(https_ota_handle);
- if (err == ESP_ERR_HTTPS_OTA_IN_PROGRESS) {
- ESP_LOGI(HTTPS_OTA, "Image bytes read: %d ota_timeout_req:%d", esp_https_ota_get_image_len_read(https_ota_handle),s_ota_ctx.ota_timeout_req);
- if(s_ota_ctx.ota_timeout_req){
- ESP_LOGI(HTTPS_OTA, "user stop!!!!timeout!!!!");
+ if (err != ESP_ERR_HTTPS_OTA_IN_PROGRESS) {
+ ESP_LOGI(HTTPS_OTA, "ota timout");
+
+ break;
+ }
+ if(s_ota_ctx.ota_timeout_req){
+ ESP_LOGI(HTTPS_OTA, "user stop!!!!timeout!!!!");
- download_abort = true;
- break;
- }
- continue;
+ download_abort = true;
+ break;
}
- break;
+ ESP_LOGI(HTTPS_OTA, "Image bytes read: %d", esp_https_ota_get_image_len_read(https_ota_handle));
+ vTaskDelay(1);
}
if(download_abort){
- break;
+ vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
+ continue;
}
// 数据没收全
vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
continue;
}
-
// 执行finish
ota_finish_err = esp_https_ota_finish(https_ota_handle);
if ((err == ESP_OK) && (ota_finish_err == ESP_OK)) {
{
esp_err_t ret = esp_ota_mark_app_invalid_rollback();
ESP_LOGI(HTTPS_OTA, "esp32 restart: %d", ret);
- Https_Ota_Report_Status(HTTPS_OTA_STATUS_FAILED);
+ if(!s_ota_ctx.ota_auto){
+ Https_Ota_Report_Status(HTTPS_OTA_STATUS_FAILED);
+ }
N455_Ota_Clear_Upgrade_Flag();
Https_Ota_Clear_Upgrade_Flag();
xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
}else{
//不需要升级455 直接重启
g_ota_status = HTTPS_OTA_STATUS_SUCCESS;
- Https_Ota_Report_Status(HTTPS_OTA_STATUS_SUCCESS);
- Wifi_Stop();
- vTaskDelay(10 / portTICK_PERIOD_MS);
- Https_Ota_Clear_Upgrade_Flag();
- esp_restart();
+ if(!s_ota_ctx.ota_auto){
+ Https_Ota_Report_Status(HTTPS_OTA_STATUS_SUCCESS);
+ }
+ if(s_ota_ctx.ota_auto) //如果是自动升级 不重启 还是一样重连mqtt
+ {
+ ESP_LOGI(HTTPS_OTA, "ota not reset");
+ Https_Ota_Clear_Upgrade_Flag();
+ N455_Ota_Clear_Upgrade_Flag();
+ // xEventGroupSetBits(common_event_group, COMMON_HTTPS_OTA_BIT);
+ xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
+ }else
+ {
+ ESP_LOGI(HTTPS_OTA, "ota reset");
+ Wifi_Stop();
+ vTaskDelay(10 / portTICK_PERIOD_MS);
+ Https_Ota_Clear_Upgrade_Flag();
+ N455_Ota_Clear_Upgrade_Flag();
+ Uart_Reply_Send(COMMON_UART_OTA_RESTET_N455,NULL, 0, HOST_TYPE_UART); //让主控重启给用户感知
+ vTaskDelay(100 / portTICK_PERIOD_MS);
+ esp_restart();
+ }
}
-
#endif
vTaskDelete(NULL);
return;
}else{
+
if(https_ota_handle != NULL && g_ota_status != HTTPS_OTA_STATUS_SUCCESS){
esp_https_ota_abort(https_ota_handle);
https_ota_handle = NULL;
ESP_LOGI(HTTPS_OTA, "ESP_HTTPS_OTA upgrade failed after 3 retry");
g_ota_status = HTTPS_OTA_STATUS_FAILED;
s_ota_ctx.ota_timeout_count=0;
- if(file_header_check)
- {
+ if(s_ota_ctx.ota_auto){
file_header_check=0;
- Https_Ota_Report_Status(HTTPS_OTA_STATUS_FILE_ERR);
- }else{
- Https_Ota_Report_Status(HTTPS_OTA_STATUS_FAILED);
+ }else
+ {
+ if(file_header_check)
+ {
+ file_header_check=0;
+ Https_Ota_Report_Status(HTTPS_OTA_STATUS_FILE_ERR);
+ }else{
+ Https_Ota_Report_Status(HTTPS_OTA_STATUS_FAILED);
+ }
}
Https_Ota_Clear_Upgrade_Flag();
xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
+
vTaskDelete(NULL);
}
}
s_ota_ctx.https_ota_init=0;
s_ota_ctx.ota_timeout_req=0;
s_ota_ctx.https_update_ota_flag = 0; //升级标识清除
- memset(https_ota_url,0,sizeof(https_ota_url));
+ s_ota_ctx.ota_auto = 0;
+ // memset(https_ota_url,0,sizeof(https_ota_url));
}
-// OTA版本检查
-//versin字符串转16进制版本号 0x0103
-// url 升级地址
-uint8_t Https_Ota_Version_Check(char *version,char *url)
+/**
+ * 函数功能
+ * @brief OTA版本检查
+ * @param version: 版本号字符串
+ * @param url: 升级地址
+ * @param ota_auto: 自动升级标志
+ * @return uint8_t: 1需要升级 0不需要升级
+ */
+uint8_t Https_Ota_Version_Check(char *version,char *url,uint8_t ota_auto)
{
s_ota_ctx.https_update_ota_flag=0;
+ s_ota_ctx.ota_auto = 0;
if(version == NULL || url == NULL)
{
return 0;
}
uint16_t new_version = version_str_to_hex(version);
// 核心:把 0x0A15 转成浮点数 10.21
- ESP_LOGI(HTTPS_OTA, "ver: %s %04x FW_VERSION:%04x", version,new_version,FW_VERSION);
+ ESP_LOGI(HTTPS_OTA, "ver: %s %04x ESP32_VERSION:%04x", version,new_version,FW_VERSION);
if(new_version != FW_VERSION && new_version != 0xffff)
{
s_ota_ctx.https_update_ota_flag = 1;
+ s_ota_ctx.ota_auto = ota_auto;
strncpy(https_ota_url, url, OTA_URL_SIZE - 1);
https_ota_url[OTA_URL_SIZE - 1] = '\0'; // 确保字符串以null结尾
ESP_LOGI(HTTPS_OTA, "need upgrade\n");
-
return 1;
}else{
ESP_LOGI(HTTPS_OTA, "no need upgrade\n");
}
//
-void Https_Ota_Clear_Update_Flag(void)
-{
- s_ota_ctx.https_update_ota_flag=0;
-}
/*
if(s_ota_ctx.ota_timeout_count > 0)
{
s_ota_ctx.ota_timeout_count--;
- ESP_LOGI(HTTPS_OTA, "OTA ota_timeout_count: %d", s_ota_ctx.ota_timeout_count);
+ ESP_LOGI(HTTPS_OTA, "ota_timeout_count: %d", s_ota_ctx.ota_timeout_count);
if(s_ota_ctx.ota_timeout_count== 0)
{
s_ota_ctx.ota_timeout_req=1;
*函数功能:OTA升级状态上报 http请求
*返回值: 0表示失败,1表示成功
*/
-uint8_t Https_Ota_Report_Status(uint8_t ota_status)
+int Https_Ota_Report_Status(uint8_t ota_status)
{
// http://192.168.66.85:8089/iot/firmware/task/detailadd {
// "serialNumber": "PXN1LJ1QUR078E",
{
.event_handler = _http_ota_status_event_handler,
.disable_auto_redirect = true,
+ .timeout_ms = 5000, // 短超时防阻塞
};
int result = 0;
esp_http_client_handle_t http_client = NULL;
vTaskDelay(50);
}
- #if (!ENABLE_HTTP_N455_OTA_FUNC)
- if(ota_status == HTTPS_OTA_STATUS_SUCCESS)
- #else
- if(ota_status == HTTPS_OTA_STATUS_SUCCESS || ota_status == N455_OTA_STATUS_SUCCESS)
- #endif
- {
- // Wifi_Stop();//断开wifi
- // vTaskDelay(50);
- // esp_restart();
- return 1;
- }
- return 0;
+
+ return result;
}
void Https_Ota_Task(void);//ota升级任务
void Https_Ota_Status_Check(void);//是否需要ota升级状态
-void Https_Ota_Clear_Update_Flag(void);
uint8_t Https_Ota_Get_Update_Flag(void);//版本检查结果标识
/*
ota版本检查
*/
-uint8_t Https_Ota_Version_Check(char *version,char *url);
+uint8_t Https_Ota_Version_Check(char *version,char *url,uint8_t ota_auto);
void Https_Ota_Start(void); //开始ota升级
void Https_Ota_Stop(void);
void Https_Ota_Handler_Init(void); //ota注册回调
-uint8_t Https_Ota_Report_Status(uint8_t ota_status);//report_ota_status(https_ota_status_t ota_status);
+int Https_Ota_Report_Status(uint8_t ota_status);//report_ota_status(https_ota_status_t ota_status);
void Https_Ota_Check_TimeOut(void); //ota超时检查
uint8_t Https_Get_Ota_Status(void);
void Https_Ota_Clear_Upgrade_Flag(void);//清除升级标识
N455_ota_status_t n455_g_ota_status = N455_OTA_STATUS_IDLE; // 455升级状态
#define OTA_URL_MAX_LEN 128
-#define OAT_CONNECT_RETRY_NUM 3 //重连服务器次数 3
-#define OTA_TOTAL_TIMEOUT_S 60 //总超时时间 单位s
+#define OAT_CONNECT_RETRY_NUM 2 //重连服务器次数 1
+#define OTA_TOTAL_TIMEOUT_S 60 //总超时时间 单位s
#define N455_OTA_STACK_SIZE (4*1024)
static char n455_ota_url[OTA_URL_MAX_LEN] = "https://pxn-iot.s3-accelerate.amazonaws.com/iot01/2026/06/03/6a1f95e5bfcb3c271127dd8b.bin";
volatile uint8_t n455_ota_stop_flag;//ota超时停止标识
volatile uint16_t n455_ota_timeout_cnt; //ota超时倒计时
uint8_t n455_ota_update_flag; //根据版本是否需要Ota
+ uint8_t ota_auto; //是否自动升级 1自动升级 0手动升级
}__attribute__((packed)) n455_ota_ctx_t;
cmd.end.protocol_version = 0x01;
cmd.end.replay = 0x00;
cmd.end.sub_cmd = CMD2_FINISH;
+
+ memcpy(&cmd.end.ota_auto_flag,data,1);
Uart_Reply_Send(COMMON_UART_MCU_FIRMWARE_UPDATE_CMD, (uint8_t*)&cmd.end, sizeof(cmd.end), HOST_TYPE_UART);
break;
case CMD2_MOVE:
switch (evt->event_id) {
case HTTP_EVENT_ERROR:
ESP_LOGI(N455_OTA, "HTTP_EVENT_ERROR");
- n455_g_ota_status = N455_OTA_STATUS_FAILED;
+
break;
case HTTP_EVENT_ON_CONNECTED:
ESP_LOGI(N455_OTA, "HTTP_EVENT_ON_CONNECTED");
*函数功能:N455 ota升级任务
*返回值: 无
*/
-
void N455_Ota_Task_Cb(void *param)
{
esp_http_client_config_t config = {
.url = n455_ota_url,
.crt_bundle_attach = esp_crt_bundle_attach,
.event_handler = _http_ota_event_handler,
- .timeout_ms = 5000,
+ .timeout_ms = 1500,
.keep_alive_enable = false, //true 会占用很多内存建议是false 不需要保持连接
};
/*
Mqtt_Disconnect(); // 关闭MQTTs
vTaskDelay(50 / portTICK_PERIOD_MS);
n455_g_ota_status = N455_OTA_STATUS_UPGRADING;
- Https_Ota_Report_Status(N455_OTA_STATUS_UPGRADING);
+ if(!n455_ota_ctx.ota_auto) //自动升级不需要上报状态
+ {
+ int ret = Https_Ota_Report_Status(N455_OTA_STATUS_UPGRADING);
+ if(!ret)
+ {
+ N455_Ota_Clear_Upgrade_Flag();
+ xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
+ vTaskDelete(NULL);
+ return;
+ }
+ }
+
}
///////////////////////
vTaskDelay(50 / portTICK_PERIOD_MS);
int wlen = 0;
esp_http_client_handle_t client = NULL;
bool failed = false;
- n455_ota_ctx.n455_ota_timeout_cnt = OTA_TOTAL_TIMEOUT_S;
+
uint8_t write_data[sizeof(FW_FILE_HEADER_T)] = {0};
uint8_t file_header_check = 0;
// ======================
// ======================
for (int retry = 0; retry < OAT_CONNECT_RETRY_NUM; retry++)
{
+ n455_ota_ctx.n455_ota_timeout_cnt = OTA_TOTAL_TIMEOUT_S;
+ n455_ota_ctx.n455_ota_stop_flag = 0;
+
file_header_check = 0;
ESP_LOGI(N455_OTA, "===== OTA 第 %d/%d 次 =====", retry+1, OAT_CONNECT_RETRY_NUM);
if(n455_ota_ctx.n455_ota_stop_flag)
esp_http_client_cleanup(client);
client = NULL;
}
- break;
+ vTaskDelay(pdMS_TO_TICKS(100));
+ continue;
}
memset(buf, 0, sizeof(buf));
real_write_len = 0;
// memset(header_buf,0,sizeof(header_buf));
wlen = 0;
//防止没有关闭http连接就进入下一次重试,导致资源泄露
- if(client)
- {
- esp_http_client_close(client);
- esp_http_client_cleanup(client);
- }
- client = NULL;
+
failed = false;
n455_ota_ctx.uart_ack_received = false;
n455_ota_ctx.uart_ack = 0xFF;
n455_g_ota_status = N455_OTA_STATUS_UPGRADING;
-
+ if (n455_ota_url[0] == '\0')
+ {
+ ESP_LOGE(N455_OTA, "OTA URL is empty, abort this retry");
+ failed = true;
+ continue;
+ }
// --------------------
// 1. HTTP 下载(每次重试都必须重新下载)
// --------------------
- client = esp_http_client_init(&config);
- if (!client) {
- ESP_LOGW(N455_OTA, "HTTP init fail");
- failed = true;
- }
+
+ if(!failed)
+ {
+ for(uint8_t i = 0;i < 3;i++) //连接重试三次
+ {
+ failed = true;
+ client = NULL;
+ client = esp_http_client_init(&config);
+ if (!client) {
+ ESP_LOGW(N455_OTA, "HTTP init fail");
+ failed = true;
+ vTaskDelay(100);
+ continue;
+ }
+ // 2. 初始化成功,再尝试连接
+ int err = esp_http_client_open(client, 0);
+ if (err == ESP_OK)
+ {
+ ESP_LOGI(N455_OTA, "http connect success");
+ failed = false;
+ break;
+ }
- if (!failed && esp_http_client_open(client, 0) != ESP_OK) {
- ESP_LOGW(N455_OTA, "HTTP connect fail");
- failed = true;
+ // 3. open 失败:必须完整释放资源
+ ESP_LOGW(N455_OTA, "http open failed, retry %d", i + 1);
+ esp_http_client_close(client);
+ esp_http_client_cleanup(client);
+ client = NULL;
+ vTaskDelay(pdMS_TO_TICKS(100));
+ }
+ if(failed)
+ {
+ vTaskDelay(pdMS_TO_TICKS(100));
+ continue;
+ }
}
- int ret = esp_http_client_fetch_headers(client);
- int code = esp_http_client_get_status_code(client);
- if (!failed && (code != 200 || ret < 0)) {
- ESP_LOGW(N455_OTA, "HTTP code error");
- failed = true;
+
+
+
+ if (!failed) {
+ int ret = esp_http_client_fetch_headers(client);
+ int code = esp_http_client_get_status_code(client);
+ if(code != 200 || ret < 0){
+ ESP_LOGW(N455_OTA, "HTTP code error");
+ failed = true;
+ }
}
- int64_t total_len = esp_http_client_get_content_length(client);
- if (!failed && total_len <= 0) {
- ESP_LOGW(N455_OTA, "File length error");
- failed = true;
+ int64_t total_len= 0;
+ if (!failed ) {
+ total_len = esp_http_client_get_content_length(client);
+ if(total_len <= 0)
+ {
+ ESP_LOGW(N455_OTA, "File length error");
+ failed = true;
+ }
}
-
+ n455_ota_ctx.n455_ota_timeout_cnt = OTA_TOTAL_TIMEOUT_S;
+ n455_ota_ctx.n455_ota_stop_flag = 0;
// 读头部256字节
-
+ uint8_t err_retry = 0;
+ int total_read = 0;
+ TickType_t header_start_tick = xTaskGetTickCount();
+ TickType_t header_timeout = pdMS_TO_TICKS(5000);
if (!failed) {
+ while (total_read < sizeof(FW_FILE_HEADER_T))
+ {
+ if ((xTaskGetTickCount() - header_start_tick) >= header_timeout)
+ {
+ break;
+ }
+ int r = esp_http_client_read(client, (char *)write_data+total_read, sizeof(FW_FILE_HEADER_T)-total_read);
+ ESP_LOGE(N455_OTA, "r=%d %d",r,err_retry);
+ if(r == 0)
+ {
+ break;
+ }else if(r < 0)
+ {
+ if(err_retry < 5)
+ {
+ err_retry++;
+ vTaskDelay(10);
+ continue;
+ }else{
+ break;
+ }
+ }
+ err_retry=0;
+ total_read+=r;
+
+ }
- int r = esp_http_client_read(client, (char *)write_data, sizeof(FW_FILE_HEADER_T));
- if (r < sizeof(FW_FILE_HEADER_T) ||
+
+ ESP_LOGE(N455_OTA, "total_read %d %02x %02x %02x %02x %02x %02x %02x %02x",total_read, write_data[0], write_data[1], write_data[2], write_data[3], write_data[4], write_data[5], write_data[6], write_data[7]);
+ if (total_read < sizeof(FW_FILE_HEADER_T) ||
write_data[0] != 0xFE || write_data[1] != 0xCA ||
write_data[2] != 0x00 || write_data[3] != 0xEE ||
write_data[4] != 0xFF || write_data[5] != 0xC0 ||
// --------------------
if (!failed && !ota_send_with_retry(CMD2_READY, NULL, 0)) failed = true;
if (!failed && !ota_send_with_retry(CMD2_ERASE, NULL, 0)) failed = true;
-
+ err_retry=0;
if (!failed) {
while (1) {
if (n455_ota_ctx.n455_ota_stop_flag) { failed = true; break; }
int r = esp_http_client_read(client, (char *)buf, MAX_PACKET_CONTENT);
- if (r <= 0) break;
+ ESP_LOGE(N455_OTA, "wr=%d %d",r,err_retry);
+ if (r == 0)
+ {
+ err_retry= 0;
+ break;
+ }
+ else if(r < 0)
+ {
+ if(err_retry < 5)
+ {
+ err_retry++;
+ vTaskDelay(10);
+ continue;
+ }else{
+ break;
+ }
+ }else{
+ err_retry=0;
+ }
+
wlen = 0;
CRC32_Update(&crc_ctx, buf, r);
- write_data[wlen++] = real_write_len & 0xFF;
- write_data[wlen++] = (real_write_len >> 8) & 0xFF;
- write_data[wlen++] = (real_write_len >> 16) & 0xFF;
- write_data[wlen++] = (real_write_len >> 24) & 0xFF;
- write_data[wlen++] = r & 0xFF;
- write_data[wlen++] = (r >> 8) & 0xFF;
+
+ memcpy(&write_data[wlen],&real_write_len,4);
+ wlen+=4;
+
+ memcpy(&write_data[wlen],&r,2);
+ wlen+=2;
+
memcpy(&write_data[wlen], buf, r);
wlen += r;
// offset_size += r;
real_write_len += r;
- ESP_LOGE(N455_OTA, "W:%d",real_write_len);
+
+ vTaskDelay(1);
}
ESP_LOGE(N455_OTA, "WRITE END:%d",real_write_len);
//超时退出不再继续ota升级
esp_http_client_cleanup(client);
client = NULL;
}
- break;
+ vTaskDelay(pdMS_TO_TICKS(500));
+ continue;
}
}
{
failed = true;
}
- if (!failed && !ota_send_with_retry(CMD2_FINISH, NULL, 0))
+ uint8_t finish_data[1] = {0};
+ finish_data[0] = n455_ota_ctx.ota_auto;
+ if (!failed && !ota_send_with_retry(CMD2_FINISH, finish_data, 1))
{
failed = true;
}
if (!failed) {
n455_g_ota_status = N455_OTA_STATUS_SUCCESS;
- Https_Ota_Report_Status(N455_OTA_STATUS_SUCCESS);
- Wifi_Stop();
- vTaskDelay(10 / portTICK_PERIOD_MS);
- Https_Ota_Clear_Upgrade_Flag();
- N455_Ota_Clear_Upgrade_Flag();
-
- esp_restart();
+ if(!n455_ota_ctx.ota_auto){
+
+ Https_Ota_Report_Status(N455_OTA_STATUS_SUCCESS);
+ }
+
+
+ if(!n455_ota_ctx.ota_auto){
+ ESP_LOGI(N455_OTA, "ota reset");
+ Wifi_Stop();
+ vTaskDelay(10 / portTICK_PERIOD_MS);
+ Https_Ota_Clear_Upgrade_Flag();
+ N455_Ota_Clear_Upgrade_Flag();
+ esp_restart();
+ }else{
+ ESP_LOGI(N455_OTA, "ota not reset");
+ Https_Ota_Clear_Upgrade_Flag();
+ N455_Ota_Clear_Upgrade_Flag();
+ vTaskDelay(pdMS_TO_TICKS(10));
+ // xEventGroupSetBits(common_event_group, COMMON_HTTPS_N455_OTA_BIT);
+ xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
+ }
vTaskDelete(NULL);
return;
}
}
Https_Ota_Clear_Upgrade_Flag();
- if(file_header_check)
- {
+ if(n455_ota_ctx.ota_auto){
file_header_check=0;
- Https_Ota_Report_Status(N455_OTA_STATUS_FILE_ERR);
}else{
- Https_Ota_Report_Status(N455_OTA_STATUS_FAILED);
+ if(file_header_check)
+ {
+ file_header_check=0;
+ Https_Ota_Report_Status(N455_OTA_STATUS_FILE_ERR);
+ }else{
+ Https_Ota_Report_Status(N455_OTA_STATUS_FAILED);
+ }
}
+
N455_Ota_Clear_Upgrade_Flag();
xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
vTaskDelete(NULL);
}
#endif
-/*
-函数功能:OTA任务开始函数
-*/
-
-
/*
*函数功能:OTA超时检查
*/
}
}
-/*
- *函数功能: OTA版本检查
- *参数: version 版本字符串,url 升级地址 versin字符串转16进制版本号 0x0103
- *返回值: 1需要升级 0不需要升级
-*/
-uint8_t N455_Ota_Version_Check(char *version,char *url)
+/**
+ * 函数功能
+ * @brief OTA版本检查
+ * @param version: 版本号字符串
+ * @param url: 升级地址
+ * @param ota_auto: 自动升级标志
+ * @return uint8_t: 1需要升级 0不需要升级
+ */
+uint8_t N455_Ota_Version_Check(char *version,char *url,uint8_t ota_auto)
{
n455_ota_ctx.n455_ota_update_flag=0;
+ n455_ota_ctx.ota_auto = 0;
if(version == NULL || url == NULL)
{
return 0;
}
uint16_t new_version = version_str_to_hex(version);
// 核心:把 0x0A15 转成浮点数 10.21
- ESP_LOGI(N455_OTA, "ver: %s %04x FW_VERSION:%04x", version,new_version,FW_VERSION);
- if(new_version != FW_VERSION && new_version != 0xffff)
+ ESP_LOGI(N455_OTA, "ver: %s %04x MCU_VERSION:%04x", version,new_version,common_setting.mcu_info.fw_version);
+ ESP_LOGI(N455_OTA, "url:%s", url);
+ if(new_version != common_setting.mcu_info.fw_version && new_version != 0xffff)
{
n455_ota_ctx.n455_ota_update_flag = 1;
+ n455_ota_ctx.ota_auto= ota_auto;
strncpy(n455_ota_url, url, OTA_URL_MAX_LEN - 1);
n455_ota_url[OTA_URL_MAX_LEN - 1] = '\0'; // 确保字符串以null结尾
ESP_LOGI(N455_OTA, "need upgrade\n");
}
return 0;
}
-/*
- *函数功能: 获取455升级标识
- *返回值: 1需要升级 0不需要升级
-*/
+/**
+ * 函数功能
+ * @brief 获取455升级标识
+ * @return uint8_t: 1需要升级 0不需要升级
+ */
uint8_t N455_Ota_Get_Update_Flag(void)
{
return n455_ota_ctx.n455_ota_update_flag;
}
-void N455_Ota_Clear_Update_Flag(void)
-{
- n455_ota_ctx.n455_ota_update_flag=0;
-}
-/*
- *函数功能: 455升级失败清空32升级标识
+/**
+ * 函数功能
+ * @brief 455升级失败清空32升级标识
*/
+
void N455_Ota_Clear_Upgrade_Flag(void)
{
n455_g_ota_status = N455_OTA_STATUS_IDLE; // 状态重置为空闲
n455_ota_ctx.n455_ota_timeout_cnt =0; //升级超时标志清零
n455_ota_ctx.n455_ota_stop_flag = 0;//升级停止标志清零
n455_ota_ctx.n455_ota_update_flag=0; //升级标识清除
- memset(n455_ota_url,0,sizeof(n455_ota_url));
+ n455_ota_ctx.ota_auto = 0; //自动升级标志清零
+ // memset(n455_ota_url,0,sizeof(n455_ota_url));
}
/**
*函数功能: 获取455升级状态
uint8_t protocol_version;
uint8_t replay;
uint8_t sub_cmd;
+
+ uint8_t ota_auto_flag;
}__attribute__((packed))end;
void N455_Ota_Status_Check(void); //455 ota升级状态检查
void N455_Ota_Capture_Ack(uint8_t cmd, uint8_t ack); //捕获455升级ack
uint8_t N455_Ota_Get_Update_Flag(void); //版本检查结果标识
-uint8_t N455_Ota_Version_Check(char *version,char *url); //版本检查
+uint8_t N455_Ota_Version_Check(char *version,char *url,uint8_t ota_auto);
void N455_Ota_Clear_Upgrade_Flag(void); //清除升级标识
void N455_Ota_Check_TimeOut(void); //ota超时检查
uint8_t N455_Get_Ota_Status(void);
-void N455_Ota_Clear_Update_Flag(void);
void N455_Ota_Start(void);
#endif
#endif
\ No newline at end of file
//data[4~(4+len-1)] = payload
//data[len-1] = crc
net_pairing_reply.cmd = data[1];
- common_setting.ble_enable_tick = 0; // 刷新蓝牙关闭倒计时
switch(data[1])
{
// 心跳检测
// 输出结果
ESP_LOGI(COMMON_TAG,"major:%d\n", major); // 1
ESP_LOGI(COMMON_TAG,"minor:%d\n", minor); // 3
- ESP_LOGI(COMMON_TAG,"total version:%04x\n", (major << 8) | minor);
+ ESP_LOGI(COMMON_TAG,"total version:0x%04x\n", (major << 8) | minor);
+ uint16_t version_total = (major << 8) | minor;
// 合成 0x0103
- return (major << 8) | minor;
+ return version_total;
}
/*
* 函数功能:清除配网信息
Mqtt_Disconnect();
vTaskDelay(50 / portTICK_PERIOD_MS);
//擦除flash
- User_Erase_NotReset();
+ // User_Erase_NotReset(); //不删除配网信息
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.ble_status.nimble_flag=0; //配网成功 开机置1
common_setting.mqtt_user_cfg.mqtt_start_flag=0; //开始连接mqtt
common_setting.ble_status.is_bind = 0; //绑定失败
xEventGroupSetBits(ble_event_group, BLE_ON_BIT);
// Process
uart_pairing_reply.cmd = data[1];
- common_setting.ble_enable_tick = 0; // 刷新蓝牙关闭倒计时
switch(data[1])
{
// 心跳检测
if (len >= 12 && data[2] >= 9)
{
common_setting.mcu_info.vid = *(uint16_t*)&data[3];
- #if 1
- common_setting.mcu_info.pid = 0xA00C;//S
+ #if 0
+ common_setting.mcu_info.pid = 0xA00B;//S
#else
common_setting.mcu_info.pid = *(uint16_t*)&data[5];
#endif
//
case COMMON_UART_FACTORY_RESET:
{
-
- xEventGroupSetBits(common_event_group, COMMON_FACTORY_SET_BIT);
- uart_pairing_reply.send_flag=1;
- uart_pairing_reply.len = 0;
+ uart_pairing_reply.send_flag=1;
+ uart_pairing_reply.len = 0;
+ xEventGroupSetBits(common_event_group, COMMON_FACTORY_SET_BIT);
+
}
break;
case COMMON_UART_BLE_CONNECT_STATUS:
COMMON_UART_FACTORY_RESET = 0x13, //恢复出厂设置
COMMON_UART_BLE_CONNECT_STATUS = 0x14, //蓝牙成功
+
+ COMMON_UART_OTA_RESTET_N455 = 0x15, //模组升级主控重启
//0x15-0xB0
COMMON_UART_MCU_FIRMWARE_UPDATE_CMD = 0xE0,//主控固件升级
#if (NIMBLE_NETWORK_MODE)
//回复配网状态
+
+
+ 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, common_setting.net_pairing_info.mqtt_sub_url,common_setting.net_pairing_info.mqtt_sub_url_length);
+ common_storage.net_info.mqtt_sub_url[common_setting.net_pairing_info.mqtt_sub_url_length]='\0';
+ common_storage.net_info.mqtt_url_len=common_setting.net_pairing_info.mqtt_sub_url_length;
+
+
+ //真正需要存储的flash数据
+ memset(common_storage.net_info.serial_number, 0x00, SERIAL_NUMBER_MAX_LEN+1);
+ memcpy(common_storage.net_info.serial_number, common_setting.net_pairing_info.serial_number, common_setting.net_pairing_info.serial_number_length);
+ common_storage.net_info.serial_number[common_setting.net_pairing_info.serial_number_length] = '\0';
+ common_storage.net_info.serial_number_flag=1;
+ common_storage.net_info.serial_number_len=common_setting.net_pairing_info.serial_number_length;
+
+ //保存mqtt 地址
+ int result = 0;
+ //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_DOWNLOAD_TASK_TAG, "network write\n");
+ result = 1;
+ }else{
+ //失败重新读
+ 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_DOWNLOAD_TASK_TAG,"common_storage ok\n");
+ }
+ 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;
+ network_request_replay(NETWORK_HTTPS_DOWNLOAD_CERTIFICATE_FAILED);
+ vTaskDelay(200);
+ vTaskDelete(NULL);
+ return;
+ }
network_request_replay(NETWORK_HTTPS_DOWNLOAD_CERTIFICATE_OK);
vTaskDelay(200);
+
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;
- xEventGroupSetBits(ble_event_group, BLE_OFF_BIT);//关闭蓝牙配网
- vTaskDelay(100);
+ int stop_ret = Ble_Stop();
+ if(stop_ret < 0)
+ {
+ ESP_LOGW(HTTP_DOWNLOAD_TASK_TAG, "Ble stop failed, continue MQTT");
+ }
+ else
+ {
+ ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "Ble stop success");
+ }
+ vTaskDelay(10);
ESP_LOGI(HTTP_TAG, "download mqtt \n");
common_setting.ble_status.nimble_flag=1; //配网成功
common_setting.mqtt_user_cfg.mqtt_start_flag=1; //开始连接mqtt
break;
}
-
- const char path[] = "/iot/device/getClientTimeByIp?pid=";// "/iot/device/getDeviceByPid/";//;//;
+ ///iot/device/generateSerialNumberByPid
+ const char path[] = "/iot/device/generateSerialNumberByPid?pid=";// "/iot/device/getDeviceByPid/";//;//;
memset(url, 0x00, sizeof(url));
http_config.url = url;
ESP_LOGI(HTTP_TAG, "http_config.url = %s", http_config.url);
- // ESP_LOGI(HTTP_TAG, "\nHTTP STEP 1..666........%d %" PRIu32 " bytes",result,esp_get_free_heap_size());
http_client = esp_http_client_init(&http_config);
if(http_client == NULL)
{
ESP_LOGI(HTTP_TAG, "\nHTTP STEP %s",(const char*)common_setting.net_pairing_info.http_token);
esp_http_client_set_header(http_client, "Authorization", (const char*)common_setting.net_pairing_info.http_token);//设置header,key为Authorization,value为http_token
esp_http_client_set_method(http_client, HTTP_METHOD_GET);//设置请求方法为GET
- // ESP_LOGI(HTTP_TAG, "%s esp_http_client_open...1.....%d %" PRIu32 " bytes",__func__,result,esp_get_free_heap_size());
esp_err_t err = esp_http_client_open(http_client, 0);//开始连接服务器并发送HTTP请求,write_len参数为0表示没有要发送的请求体
- // ESP_LOGI(HTTP_TAG, "%s esp_http_client_open....2....%d %" PRIu32 " bytes",__func__,result,esp_get_free_heap_size());
content_len = 0;
status_code=0;
if(err == ESP_OK)
}
//临时使用的数据
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);
+ memcpy(common_setting.net_pairing_info.serial_number, str_serialNumber, 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;
- common_storage.net_info.serial_number_len=serial_number_len;
- //保存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, "write usersuccess\n");
+ result = 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;
+ // common_storage.net_info.serial_number_len=serial_number_len;
+ // //保存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, "write usersuccess\n");
- result = 1;
+ // result = 1;
- }else
- {
- ESP_LOGE(HTTP_TAG, "flash write error\n");
- result = 2;
+ // }else
+ // {
+ // ESP_LOGE(HTTP_TAG, "flash write error\n");
+ // result = 2;
- }
+ // }
}
}
common_setting.net_pairing_info.mqtt_sub_url_length = mqtt_url_len;
common_setting.net_pairing_info.mqtt_sub_url_flag = 1;
+ result = 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;
- 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, "mqtt_sub_url success: %s\n",common_storage.net_info.mqtt_sub_url);
+ // result = 1;
+ // }
+ // else
+ // {
+ // ESP_LOGE(HTTP_TAG, "mqtt_sub_url fail: %s\n",common_storage.net_info.mqtt_sub_url);
+ // result = 2;
+ // }
- //保存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, "mqtt_sub_url success: %s\n",common_storage.net_info.mqtt_sub_url);
- result = 1;
- }
- else
- {
- ESP_LOGE(HTTP_TAG, "mqtt_sub_url fail: %s\n",common_storage.net_info.mqtt_sub_url);
- result = 2;
- }
}
}
memset(&publish_data,0x00,sizeof(mqtt_pulish_data_t));
mqtt_client_handle=NULL;
memset(&mqtt_ctx.mqtt_cfg, 0, sizeof(mqtt_ctx.mqtt_cfg));
- mqtt_ctx.mqtt_cfg.session.keepalive = 100; //50S
mqtt_ctx.mqtt_cfg.session.disable_keepalive = 0;
// common_setting.mqtt_cfg.session.keepalive = 60;
mqtt_ctx.mqtt_cfg.network.disable_auto_reconnect = false;
uint8_t Mqtt_Send_Mcu_Info(void)
{
-
+ static uint8_t power_on_flag = 0;
if(mqtt_ctx.mqtt_send_mcu_info_flag)
{
return 0;
cJSON_AddStringToObject(root, "wifi_version", wifi_ver);
cJSON_AddStringToObject(root, "serial_id", (char *)common_storage.net_info.serial_number);
cJSON_AddNumberToObject(root, "connected_platform", common_setting.mcu_platform_mode);
- cJSON_AddNumberToObject(root, "power_on", 1);
+ if(!power_on_flag)
+ {
+ power_on_flag=1;
+ cJSON_AddNumberToObject(root, "power_on", 1);
+ }
+
cJSON_AddNumberToObject(root, "9", common_setting.is_connect_usb);
return 0;
}
int ret = user_esp_mqtt_client_publish(post_data,strlen(post_data),get_publish_topic(TOPIC_PROPERTY_ACK_POST));
+ if(ret == -1) //发送失败
+ {
+ power_on_flag=0;
+ }
free(post_data);
post_data = NULL;
mqtt_ctx.mqtt_send_mcu_info_flag=0;
// mqtt_get_dev_test(test_dpid,49);
}
- ESP_LOGI(MQTT_TAG,"%d %d",mqtt_ctx.sub_flag,mqtt_ctx.connect_flag);
+ // ESP_LOGI(MQTT_TAG,"%d %d",mqtt_ctx.sub_flag,mqtt_ctx.connect_flag);
if(mqtt_ctx.sub_flag == 1 && mqtt_ctx.connect_flag == 1)
{
static uint8_t send_flag_first = 0;
- if(send_flag_first == 0 && mqtt_ctx.mqtt_send_data_count >= 2)
+ if(send_flag_first == 0)
{
if(Mqtt_Send_Mcu_Info())
{
send_flag_first=1;
}
}
- if(mqtt_ctx.mqtt_send_data_count >= 30)
+ if(mqtt_ctx.mqtt_send_data_count >= 60)
{
mqtt_ctx.mqtt_send_data_count = 0;
Mqtt_Send_Mcu_Info();
ESP_LOGI(MQTT_TAG,"not arrary szie:%d",array_size);
return;
}
- #if ENABLE_HTTPS_OTA_FUNC
- Https_Ota_Clear_Update_Flag();
- #endif
- #if ENABLE_HTTP_N455_OTA_FUNC
- N455_Ota_Clear_Update_Flag();
- #endif
//只要有一个pid不对就不需要升级
uint8_t pid_check_flag = 0;
for (uint16_t i = 0; i < array_size; i++) //0-65535
continue;
}
+ cJSON *ota_auto = cJSON_GetObjectItem(item, "ota_auto");//
+ if(ota_auto == NULL || !cJSON_IsNumber(ota_auto))
+ {
+ continue;
+ }
cJSON *version = cJSON_GetObjectItem(item, "version");//
if(version == NULL || version->valuestring == NULL || !cJSON_IsString(version))
{
{
pid_check_flag=1;
#if ENABLE_HTTPS_OTA_FUNC
- Https_Ota_Version_Check(version->valuestring,filePath->valuestring);
+ Https_Ota_Version_Check(version->valuestring,filePath->valuestring,ota_auto->valueint);
#endif
}
//主控的pid
{
pid_check_flag=1;
#if ENABLE_HTTP_N455_OTA_FUNC
- N455_Ota_Version_Check(version->valuestring,filePath->valuestring);
+ N455_Ota_Version_Check(version->valuestring,filePath->valuestring,ota_auto->valueint);
#endif
}else{
//和本地一个都不匹配就视为无效的报文
}
if(pid_check_flag == 1){
- //先检查模组是否需要升级 不需要就检查主控是否需要升级
+ //先检查模组是否需要升级 不需要就检查主控是否需要升级 如果两个都要升级 优先升级模组再升级主控
#if ENABLE_HTTPS_OTA_FUNC
if(Https_Ota_Get_Update_Flag())
{
return;
}
#endif
+
+ //不需要升级
}else
{
if(pid_check_flag == 2) //pid和本地不一致
if ((version != NULL && cJSON_IsString(version) && version->valuestring != NULL) )
{
ESP_LOGI(MQTT_TAG,"ota file:%s",filePath->valuestring);
- Https_Ota_Version_Check(version->valuestring, filePath->valuestring);
+ Https_Ota_Version_Check(version->valuestring, filePath->valuestring,0);
if(root != NULL)
{
cJSON_Delete(root);
// }
// }
+ //ota topic
if(strcmp(get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE),topic) == 0)
{
cJSON *firmwareList = cJSON_GetObjectItem(payload, "firmwareList");//
#include "user_http.h"
#include "esp_timer.h"
#include "gap.h"
-// #include "freertos/FreeRTOS.h"
-// #include "freertos/task.h"
-// #include "freertos/event_groups.h"
-// #include "esp_system.h"
-// #include "esp_wifi.h"
-// #include "esp_event.h"
-// #include "esp_log.h"
-// #include "nvs_flash.h"
-
-// #include "lwip/err.h"
-// #include "lwip/sys.h"
-
-// #include "wifi.h"
-// #include "mqtt_client.h"
-// #include "user_flash.h"
+
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
static void esp_bss_rssi_low_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
wifi_event_bss_rssi_low_t *event = event_data;
-
- // ESP_LOGI(WIFI_TAG, "%s:bss rssi is=%ld", __func__, event->rssi);
common_setting.wifi_status.rssi = event->rssi;
}
common_setting.wifi_status.wifi_state = 1;
Wifi_Connect_Timerout_Stop();//连接成功了就关闭定时器
wifi_connect_timeout_flag = 0;//连接成功了就把超时标志清零
- #define USER_TMP 0
- #if USER_TMP
-
- #else
- #if 0
- // if(1) //在配网状态下还不能ota升级只有配完网之后 有证书 wifi密码
- {
- xEventGroupSetBits(ble_event_group, BLE_OFF_BIT);
- xEventGroupSetBits(common_event_group, COMMON_HTTPS_OTA_BIT);
- }
-
- #else
- #if NIMBLE_NETWORK_MODE
- if(common_setting.ble_status.ble_adv_flag) //在广播状态下 不允许连接mqtt
+ if(common_setting.ble_status.ble_adv_flag != 0) //在广播状态下 不允许连接mqtt
{
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
- }else{
- if(common_setting.wifi_status.connect_mode == 3 || common_setting.ble_status.nimble_flag == 1)//已经连过一次了
- {
- xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_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连接
+ else
{
- //要先关闭配网再发送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_user_cfg.mqtt_start_flag == 1)//已经连过一次了
+ if(common_setting.wifi_status.connect_mode == 3 || common_setting.ble_status.nimble_flag == 1)//已经连过一次了
{
xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
}
}
- #endif
- #endif
- #endif
-
-
esp_wifi_set_rssi_threshold(WIFI_RSSI_THRESHOLD);
}
break;