#endif
#include "esp_crt_bundle.h"
-https_ota_status_t g_ota_status = HTTPS_OTA_STATUS_IDLE; // 全局OTA状态
+https_ota_status_t g_ota_status = HTTPS_OTA_STATUS_IDLE; // https全局OTA状态
-#define HTTPS_OTA_TIMEOUT_TIME 150//单位s
+#define HTTPS_OTA_TIMEOUT_TIME 150//单位s
-static volatile uint8_t ota_timeout_req = 0; //超时升级失败
-static volatile uint16_t ota_timeout_count = 0;
+#define HTTPS_OTA_MAX_RETRY_CNT 3 // 失败重试次数const uint8_t MAX_RETRY = 3;
+#define HTTPS_OTA_RETRY_TIMES 500//单位ms const int max_retry_times = 500;超时重试时间
-static volatile uint16_t ota_faile_count = 0;
+// static volatile uint8_t ota_timeout_req = 0; //超时升级失败
+// static volatile uint16_t ota_timeout_count = 0;
-static uint8_t https_update_ota_flag = 0;
+// static volatile uint16_t ota_faile_count = 0;
-static uint8_t https_ota_init = 0;
+// static uint8_t https_update_ota_flag = 0; //根据版本号比对 是否需要升级
+
+// static uint8_t https_ota_init = 0;
+
+
+
+
+typedef struct{
+ volatile uint8_t ota_timeout_req;//超时退出标识
+ volatile uint16_t ota_timeout_count; //超时计数器
+ volatile uint16_t ota_faile_count;//失败计数器
+ uint8_t https_update_ota_flag;//根据版本号是否需要升级
+ uint8_t https_ota_init;//ota升级task初始化
+ esp_https_ota_handle_t https_ota_handle; //ota句柄
+}__attribute__((packed)) https_ota_ctx_t;
+
+static https_ota_ctx_t s_ota_ctx = { 0 };
#define HTTPS_OTA_RETRY_NUM 3
#define OTA_URL_SIZE 128
// err = esp_http_client_set_header(http_client, "Custom-Header", "Value");
return err;
}
-esp_https_ota_handle_t https_ota_handle = NULL;
+
#if 0
/*
func:OTA升级回调函数
#else
static void Https_Ota_Task_Cb(void *pvParameter)
{
- const int max_retry_times = 500;
+
Mqtt_Disconnect(); // 关闭MQTTs
vTaskDelay(200 / portTICK_PERIOD_MS);
- https_ota_handle=NULL;
+ s_ota_ctx.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(g_ota_status);
+ Https_Ota_Report_Status(HTTPS_OTA_STATUS_UPGRADING);
vTaskDelay(100 / portTICK_PERIOD_MS);
esp_err_t ota_finish_err = ESP_OK;
bool ota_success = false;
- const uint8_t MAX_RETRY = 3;
- ota_timeout_count = HTTPS_OTA_TIMEOUT_TIME;
+
+ s_ota_ctx.ota_timeout_count = HTTPS_OTA_TIMEOUT_TIME;
// for统一3次重试
- for(uint8_t try_idx = 0; try_idx < MAX_RETRY; try_idx++)
+ uint8_t file_header_check = 0;
+ for(uint8_t try_idx = 0; try_idx < HTTPS_OTA_MAX_RETRY_CNT; try_idx++)
{
+ file_header_check = 0;
ota_success = false;
- ESP_LOGI(HTTPS_OTA,"==== OTA try %d/%d ====",try_idx+1,MAX_RETRY);
+ 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 = 5000,
esp_err_t err = ESP_FAIL;
// 先释放上一轮句柄
- if (https_ota_handle != NULL)
+ if (s_ota_ctx.https_ota_handle != NULL)
{
- esp_https_ota_abort(https_ota_handle);
- https_ota_handle = NULL;
+ esp_https_ota_abort(s_ota_ctx.https_ota_handle);
+ s_ota_ctx.https_ota_handle = NULL;
}
- https_ota_handle = NULL;
- err = esp_https_ota_begin(&ota_config, &https_ota_handle);
+ s_ota_ctx.https_ota_handle = NULL;
+ err = esp_https_ota_begin(&ota_config, &s_ota_ctx.https_ota_handle);
if(err != ESP_OK)
{
ESP_LOGI(HTTPS_OTA, "esp_https_ota_begin failed, try:%d", try_idx+1);
- vTaskDelay(pdMS_TO_TICKS(max_retry_times));
+ 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);
+ err = esp_https_ota_get_img_desc(s_ota_ctx.https_ota_handle, &app_desc);
if (err != ESP_OK) {
ESP_LOGI(HTTPS_OTA, "esp_https_ota_get_img_desc failed");
- vTaskDelay(pdMS_TO_TICKS(max_retry_times));
- continue;
+ vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
+ file_header_check=1;
+ ota_success = 0;
+ break;
}
err = ESP_FAIL;
err = validate_image_header(&app_desc);
if (err != ESP_OK) {
ESP_LOGI(HTTPS_OTA, "image header verification failed");
- vTaskDelay(pdMS_TO_TICKS(max_retry_times));
- continue;
+ vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
+ file_header_check=1;
+ ota_success = 0;
+ break;
}else{
ESP_LOGI(HTTPS_OTA, "image header verification success");
}
// 下载循环
bool download_abort = false;
while (1) {
- if(ota_timeout_req){
+ if(s_ota_ctx.ota_timeout_req){
ESP_LOGI(HTTPS_OTA, "user timeout");
download_abort = true;
break;
}
- err = esp_https_ota_perform(https_ota_handle);
+ if(s_ota_ctx.https_ota_handle == NULL)
+ {
+ download_abort = true;
+ break;
+ }
+ err = esp_https_ota_perform(s_ota_ctx.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),ota_timeout_req);
- if(ota_timeout_req){
+ ESP_LOGI(HTTPS_OTA, "Image bytes read: %d ota_timeout_req:%d", esp_https_ota_get_image_len_read(s_ota_ctx.https_ota_handle),s_ota_ctx.ota_timeout_req);
+ if(s_ota_ctx.ota_timeout_req){
ESP_LOGI(HTTPS_OTA, "user stop!!!!timeout!!!!");
download_abort = true;
}
// 数据没收全
- if (esp_https_ota_is_complete_data_received(https_ota_handle) != true) {
+ if (esp_https_ota_is_complete_data_received(s_ota_ctx.https_ota_handle) != true) {
ESP_LOGI(HTTPS_OTA, "Complete data was not received. continue ota %d",common_setting.wifi_status.wifi_state);
- vTaskDelay(pdMS_TO_TICKS(max_retry_times));
+ vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
continue;
}
// 执行finish
- ota_finish_err = esp_https_ota_finish(https_ota_handle);
+ ota_finish_err = esp_https_ota_finish(s_ota_ctx.https_ota_handle);
if ((err == ESP_OK) && (ota_finish_err == ESP_OK)) {
ESP_LOGI(HTTPS_OTA, "ESP_HTTPS_OTA upgrade successful. Rebooting ...");
g_ota_status = HTTPS_OTA_STATUS_SUCCESS;
- ota_timeout_count=0;
- https_ota_handle = NULL;
+ s_ota_ctx.ota_timeout_count=0;
+ s_ota_ctx.https_ota_handle = NULL;
ota_success = true;
break; //升级成功跳出for,不再重试
} else {
ESP_LOGI(HTTPS_OTA, "Image validation failed, image is corrupted");
}
ESP_LOGI(HTTPS_OTA, "ESP_HTTPS_OTA upgrade failed 0x%x", ota_finish_err);
- vTaskDelay(pdMS_TO_TICKS(max_retry_times));
+ vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
continue;
}
}
+ //文件不对 不是本模组需要升级的bin文件
// 成功分支
if(ota_success)
{
#if (!ENABLE_HTTP_N455_OTA_FUNC)
- uint8_t ret = Https_Ota_Report_Status(g_ota_status);
+ g_ota_status = HTTPS_OTA_STATUS_SUCCESS;
+ uint8_t ret = Https_Ota_Report_Status(HTTPS_OTA_STATUS_SUCCESS);
Https_Ota_Clear_Upgrade_Flag();
- if(!ret)
+ if(1)
{
- esp_err_t ret = esp_ota_mark_app_invalid_rollback();
- if(common_setting.wifi_status.wifi_state == 1){
- xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
- }
+ ret = esp_ota_mark_app_invalid_rollback();
+ xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
}
#else
if(N455_Ota_Get_Update_Flag())
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;
+ if(s_ota_ctx.https_ota_handle != NULL && g_ota_status != HTTPS_OTA_STATUS_SUCCESS){
+ esp_https_ota_abort(s_ota_ctx.https_ota_handle);
+ s_ota_ctx.https_ota_handle = NULL;
}
- ota_timeout_req=0;
+ s_ota_ctx.ota_timeout_req=0;
ESP_LOGI(HTTPS_OTA, "ESP_HTTPS_OTA upgrade failed after 3 retry");
g_ota_status = HTTPS_OTA_STATUS_FAILED;
- ota_timeout_count=0;
- Https_Ota_Report_Status(HTTPS_OTA_STATUS_FAILED);
- Https_Ota_Clear_Upgrade_Flag();
- if(common_setting.wifi_status.wifi_state == 1){
- xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
+ s_ota_ctx.ota_timeout_count=0;
+ 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);
}
}
void Https_Ota_Clear_Upgrade_Flag(void)
{
g_ota_status = HTTPS_OTA_STATUS_IDLE;
- ota_timeout_count = 0;
- https_ota_init=0;
- ota_timeout_req=0;
- https_update_ota_flag = 0; //升级标识清除
+ s_ota_ctx.ota_timeout_count = 0;
+ 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));
}
// url 升级地址
uint8_t Https_Ota_Version_Check(char *version,char *url)
{
- https_update_ota_flag=0;
+ s_ota_ctx.https_update_ota_flag=0;
if(version == NULL || url == NULL)
{
return 0;
ESP_LOGI(HTTPS_OTA, "ver: %s %04x FW_VERSION:%04x", version,new_version,FW_VERSION);
if(new_version != FW_VERSION && new_version != 0xffff)
{
- https_update_ota_flag = 1;
+ s_ota_ctx.https_update_ota_flag = 1;
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");
uint8_t Https_Ota_Get_Update_Flag(void)
{
- return https_update_ota_flag;
+ return s_ota_ctx.https_update_ota_flag;
}
//
void Https_Ota_Clear_Update_Flag(void)
{
- https_update_ota_flag=0;
+ s_ota_ctx.https_update_ota_flag=0;
}
{
if(g_ota_status != HTTPS_OTA_STATUS_SUCCESS && g_ota_status > HTTPS_OTA_STATUS_IDLE)
{
- if(!ota_timeout_req){
- ota_timeout_req = 1;
+ if(!s_ota_ctx.ota_timeout_req){
+ s_ota_ctx.ota_timeout_req = 1;
}
}
void Https_Ota_Task(void)
{
- if(https_ota_init)
+ if(s_ota_ctx.https_ota_init)
{
return;
}
- ota_timeout_req = 0;
- ota_timeout_count = 0;
- https_ota_init=1;
+ s_ota_ctx.ota_timeout_req = 0;
+ s_ota_ctx.ota_timeout_count = 0;
+ s_ota_ctx.https_ota_init=1;
g_ota_status = HTTPS_OTA_STATUS_UPGRADING;
BaseType_t ret = xTaskCreate(Https_Ota_Task_Cb, "https_ota_task", HTTPS_OTA_STACK_SIZE, NULL, 4, NULL);
if(ret !=pdPASS )
{
- https_ota_init=0;
+ s_ota_ctx.https_ota_init=0;
ESP_LOGI(HTTPS_OTA, "create https ota task failed\n");
Https_Ota_Clear_Upgrade_Flag();
g_ota_status = HTTPS_OTA_STATUS_IDLE;
}else{
- https_ota_init=1;
+ s_ota_ctx.https_ota_init=1;
g_ota_status = HTTPS_OTA_STATUS_UPGRADING;
ESP_LOGI(HTTPS_OTA, "task success\n");
{
if(g_ota_status == HTTPS_OTA_STATUS_UPGRADING) //一直没有成功
{
- if(ota_timeout_count > 0)
+ if(s_ota_ctx.ota_timeout_count > 0)
{
- ota_timeout_count--;
- ESP_LOGI(HTTPS_OTA, "OTA ota_timeout_count: %d", ota_timeout_count);
- if(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);
+ if(s_ota_ctx.ota_timeout_count== 0)
{
- ota_timeout_req=1;
+ s_ota_ctx.ota_timeout_req=1;
}
}
}else{
- ota_timeout_count=0;
+ s_ota_ctx.ota_timeout_count=0;
}
- if(ota_faile_count > 0)
+ if(s_ota_ctx.ota_faile_count > 0)
{
- ota_faile_count--;
- if(ota_faile_count == 0)
+ s_ota_ctx.ota_faile_count--;
+ if(s_ota_ctx.ota_faile_count == 0)
{
Https_Ota_Start();
}
}
-
}
/*
*函数功能:OTA事件回调
*无返回值
*/
+
static void ota_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
continue;
}
cJSON_AddStringToObject(pRoot, "serialNumber", (const char*)common_storage.net_info.serial_number);
- #if (!ENABLE_HTTP_N455_OTA_FUNC)
+ #if (ENABLE_HTTPS_OTA_FUNC == 1 && ENABLE_HTTP_N455_OTA_FUNC == 0)
if(ota_status == HTTPS_OTA_STATUS_SUCCESS)
- #else
+ #elif (ENABLE_HTTPS_OTA_FUNC == 1 && ENABLE_HTTP_N455_OTA_FUNC == 1)
if(ota_status == HTTPS_OTA_STATUS_SUCCESS || ota_status == N455_OTA_STATUS_SUCCESS)
#endif
{
cJSON_AddNumberToObject(pRoot,"upgradeStatus", 3);
cJSON_AddStringToObject(pRoot, "detailMsg", "升级成功");
}
- #if (!ENABLE_HTTP_N455_OTA_FUNC)
+ #if (ENABLE_HTTPS_OTA_FUNC == 1 && ENABLE_HTTP_N455_OTA_FUNC == 0)
else if(ota_status == HTTPS_OTA_STATUS_UPGRADING)
- #else
+ #elif (ENABLE_HTTPS_OTA_FUNC == 1 && ENABLE_HTTP_N455_OTA_FUNC == 1)
else if(ota_status == HTTPS_OTA_STATUS_UPGRADING || ota_status == N455_OTA_STATUS_UPGRADING)
#endif
{
cJSON_AddNumberToObject(pRoot,"upgradeStatus", 5);
}
else{
- cJSON_AddStringToObject(pRoot, "detailMsg", "升级失败");
- cJSON_AddNumberToObject(pRoot,"upgradeStatus", 4);
+ #if (ENABLE_HTTPS_OTA_FUNC == 1 && ENABLE_HTTP_N455_OTA_FUNC == 0)
+ if(ota_status == HTTPS_OTA_STATUS_FILE_ERR)
+ #elif (ENABLE_HTTPS_OTA_FUNC == 1 && ENABLE_HTTP_N455_OTA_FUNC == 1)
+ if (ota_status == N455_OTA_STATUS_FILE_ERR || ota_status == HTTPS_OTA_STATUS_FILE_ERR)
+ #endif
+ {
+ cJSON_AddStringToObject(pRoot, "detailMsg", "升级文件不对");
+ cJSON_AddNumberToObject(pRoot,"upgradeStatus", 4);
+ }
+ else{
+ cJSON_AddStringToObject(pRoot, "detailMsg", "升级失败");
+ cJSON_AddNumberToObject(pRoot,"upgradeStatus", 4);
+ }
+
}
char *post_data = cJSON_Print(pRoot);
- if(strlen(post_data) == 0 && post_data == NULL)//数据为空说明cJSON构建失败
+ if(post_data == NULL)
{
cJSON_Delete(pRoot);
vTaskDelay(50);
+ continue;
+ }
+ if(strlen(post_data) <= 0)//数据为空说明cJSON构建失败
+ {
+ cJSON_Delete(pRoot);
+ pRoot = NULL;
+ vTaskDelay(50);
continue;
}
- ESP_LOGI(HTTPS_OTA, "http_config.url = %s", http_config.url);
+ ESP_LOGI(HTTPS_OTA, "http_config.url = %s post_data=%s", http_config.url,post_data);
http_client = esp_http_client_init(&http_config);
if(http_client == NULL)
ESP_LOGI(HTTPS_OTA, "http_config.url = %s", http_config.url);
cJSON_free(post_data);
cJSON_Delete(pRoot);
+ pRoot = NULL;
vTaskDelay(50);
continue;
}
esp_http_client_set_header(http_client, "Content-Type", "application/json");
esp_http_client_set_post_field(http_client, post_data, strlen(post_data));
- esp_err_t err = esp_http_client_open(http_client, 0);
+ // esp_err_t err = esp_http_client_open(http_client, 0);
+ esp_err_t err = esp_http_client_perform(http_client);
if (err == ESP_OK)
{
-
- int header_ret = esp_http_client_fetch_headers(http_client);//获取HTTP响应头,返回值为响应头的总长度,如果返回值小于0表示获取响应头失败
- if(header_ret < 0)
- {
- ESP_LOGE(HTTPS_OTA, "Failed to fetch headers");
- result = 0;
- cJSON_free(post_data);
- cJSON_Delete(pRoot);
- if(http_client != NULL)
- {
- esp_http_client_close(http_client);
- esp_http_client_cleanup(http_client);
- http_client=NULL;
- }
- vTaskDelay(50);
- continue;
- }
int status_code = esp_http_client_get_status_code(http_client);
int read_len = esp_http_client_get_content_length(http_client);
if(status_code == 200)
}
cJSON_free(post_data);
cJSON_Delete(pRoot);
+ pRoot = NULL;
if(http_client != NULL)
{
esp_http_client_close(http_client);
#endif
#include "esp_crt_bundle.h"
-N455_ota_status_t n455_g_ota_status = N455_OTA_STATUS_IDLE; //
+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
// BIN 文件头
#define FW_HEADER_SIZE 256 // 前256字节是头,需要跳过
-// 全局升级应答
-static volatile bool uart_ack_received = false; // UART应答的 1 标识uart收到回复
-static volatile uint8_t uart_ack = 0xFF; //UART应答的 1 成功 2 失败 0xFF未收到应答 0表示收到但失败
+// // 全局升级应答
+// static volatile bool uart_ack_received = false; // UART应答的 1 标识uart收到回复
+// static volatile uint8_t uart_ack = 0xFF; //UART应答的 1 成功 2 失败 0xFF未收到应答 0表示收到但失败
-//task 标识
-static uint8_t n455_ota_init = 0;
-static volatile uint8_t n455_ota_stop = 0;
-static volatile uint16_t n455_ota_timeout = 0;
-static uint8_t n455_ota_update_flag = 0; //ota是否需要升级
+// //task 标识
+// static uint8_t n455_ota_init = 0;
+// static volatile uint8_t n455_ota_stop_flag = 0;
+// static volatile uint16_t n455_ota_timeout_cnt = 0;
+// static uint8_t n455_ota_update_flag = 0; //ota是否需要升级 根据版本号比对
+
+typedef struct{
+ volatile bool uart_ack_received; //收到回复串口的应答 1收到应答
+ volatile uint8_t uart_ack;//应答标识 0xff未收到回复 1成功 2 失败
+ uint8_t n455_ota_init; //ota初始化
+ volatile uint8_t n455_ota_stop_flag;//ota超时停止标识
+ volatile uint16_t n455_ota_timeout_cnt; //ota超时倒计时
+ uint8_t n455_ota_update_flag; //根据版本是否需要Ota
+
+
+}__attribute__((packed)) n455_ota_ctx_t;
+static n455_ota_ctx_t n455_ota_ctx = {0};
/*
*函数功能:接收升级应答
*cmd:命令类型
{
if(cmd == CMD1_N455)
{
- uart_ack = ack;
- uart_ack_received = true;
+ n455_ota_ctx.uart_ack = ack;
+ n455_ota_ctx.uart_ack_received = true;
}
}
*/
static bool ota_wait_ack(uint8_t expect_cmd2)
{
- uart_ack_received = false;
- uart_ack = 0xFF;
+ n455_ota_ctx.uart_ack_received = false;
+ n455_ota_ctx.uart_ack = 0xFF;
uint32_t start = xTaskGetTickCount();
uint32_t ack_outime = OTA_TIMEOUT_MS;
}
while((xTaskGetTickCount() - start) < ack_outime)
{
- if(n455_ota_stop)
+ if(n455_ota_ctx.n455_ota_stop_flag)
{
- uart_ack_received=false;
+ n455_ota_ctx.uart_ack_received=false;
break;
}
- if(uart_ack_received)
+ if(n455_ota_ctx.uart_ack_received)
{
break;
}
vTaskDelay(1 / portTICK_PERIOD_MS);
}
- if(uart_ack_received == false)
+ if(n455_ota_ctx.uart_ack_received == false)
{
ESP_LOGW(N455_OTA, "TIMEOUT %02X", expect_cmd2);
return false;
}
- if(uart_ack == 1) // 1表示成功
+ if(n455_ota_ctx.uart_ack == 1) // 1表示成功
{
return true;
}
ok = ota_wait_ack(cmd2);//等待结果回复
if(!ok)
{
- if(n455_ota_stop)
+ if(n455_ota_ctx.n455_ota_stop_flag)
{
retry=MAX_RETRY_TIMES; // 停止升级,退出重试
return false;
.keep_alive_enable = true,
};
esp_http_client_handle_t client = NULL;
- n455_ota_timeout = OTA_TOTAL_TIMEOUT_S;
+ n455_ota_ctx.n455_ota_timeout_cnt = OTA_TOTAL_TIMEOUT_S;
n455_g_ota_status = N455_OTA_STATUS_UPGRADING;
uint8_t buf[MAX_PACKET_CONTENT];
for (size_t i = 0; i < OAT_CONNECT_RETRY_NUM; i++)
{
- if(n455_ota_stop)
+ if(n455_ota_ctx.n455_ota_stop_flag)
{
ESP_LOGI(N455_OTA, "ota stopped");
- n455_ota_stop=0;
+ n455_ota_ctx.n455_ota_stop_flag=0;
n455_g_ota_status = N455_OTA_STATUS_FAILED;
goto ota_end;
return;
offset_size=0;
while (1)
{
- if(n455_ota_stop)
+ if(n455_ota_ctx.n455_ota_stop_flag)
{
ESP_LOGI(N455_OTA, "ota stopped");
- n455_ota_stop = 0;
+ n455_ota_ctx.n455_ota_stop_flag = 0;
n455_g_ota_status = N455_OTA_STATUS_FAILED;
goto ota_end;
return;
goto ota_end;
return;
}
- if(n455_ota_stop)
+ if(n455_ota_ctx.n455_ota_stop_flag)
{
ESP_LOGI(N455_OTA, "ota stopped");
- n455_ota_stop = 0;
+ n455_ota_ctx.n455_ota_stop_flag = 0;
n455_g_ota_status = N455_OTA_STATUS_FAILED;
goto ota_end;
return;
uint8_t crc_data[8] = {0};
memcpy(crc_data, &real_write_len, 4); // 前4字节是固件大小
memcpy(crc_data+4, &crc_value, 4); // 接着4字节是CRC32
- if(n455_ota_stop)
+ if(n455_ota_ctx.n455_ota_stop_flag)
{
ESP_LOGI(N455_OTA, "ota stopped");
- n455_ota_stop = 0;
+ n455_ota_ctx.n455_ota_stop_flag = 0;
n455_g_ota_status = N455_OTA_STATUS_FAILED;
goto ota_end;
return;
goto ota_end;
return;
}
- if(n455_ota_stop)
+ if(n455_ota_ctx.n455_ota_stop_flag)
{
ESP_LOGI(N455_OTA, "ota stopped");
- n455_ota_stop = 0;
+ n455_ota_ctx.n455_ota_stop_flag = 0;
n455_g_ota_status = N455_OTA_STATUS_FAILED;
goto ota_end;
return;
goto ota_end;
return;
}
- if(n455_ota_stop)
+ if(n455_ota_ctx.n455_ota_stop_flag)
{
ESP_LOGI(N455_OTA, "ota stop");
- n455_ota_stop = 0;
+ n455_ota_ctx.n455_ota_stop_flag = 0;
n455_g_ota_status = N455_OTA_STATUS_FAILED;
goto ota_end;
return;
goto ota_end;
return;
}
- if(n455_ota_stop)
+ if(n455_ota_ctx.n455_ota_stop_flag)
{
ESP_LOGI(N455_OTA, "ota stop");
- n455_ota_stop = 0;
+ n455_ota_ctx.n455_ota_stop_flag = 0;
n455_g_ota_status = N455_OTA_STATUS_FAILED;
goto ota_end;
Wifi_Stop();
Https_Ota_Clear_Upgrade_Flag(); // 清除32升级标识
- n455_ota_init=0;
+ n455_ota_ctx.n455_ota_init=0;
vTaskDelay(10 / portTICK_PERIOD_MS);
esp_restart();
vTaskDelete(NULL);
esp_http_client_cleanup(client);
client = NULL;
}
- n455_ota_init=0;
+ n455_ota_ctx.n455_ota_init=0;
n455_g_ota_status = N455_OTA_STATUS_FAILED;
- n455_ota_stop = 0;
+ n455_ota_ctx.n455_ota_stop_flag = 0;
//如果32ota成功了 455升级失败了 清除32的升级标识 回滚固件
if(Https_Get_Ota_Success() == HTTPS_OTA_STATUS_SUCCESS)
{
Mqtt_Disconnect(); // 关闭MQTTs
vTaskDelay(200 / portTICK_PERIOD_MS);
n455_g_ota_status = N455_OTA_STATUS_UPGRADING;
- Https_Ota_Report_Status(g_ota_status);
+ Https_Ota_Report_Status(N455_OTA_STATUS_UPGRADING);
}
///////////////////////
vTaskDelay(50 / portTICK_PERIOD_MS);
- const int MAX_OTA_RETRY = 3; // 整个OTA只试3次
+
uint8_t buf[MAX_PACKET_CONTENT];
uint32_t real_write_len = 0;
int wlen = 0;
esp_http_client_handle_t client = NULL;
bool failed = false;
- n455_ota_timeout = OTA_TOTAL_TIMEOUT_S;
+ 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;
// ======================
// 只循环3次 → 每次都完整重新下载(正确)
// ======================
- for (int retry = 0; retry < MAX_OTA_RETRY; retry++)
+ for (int retry = 0; retry < OAT_CONNECT_RETRY_NUM; retry++)
{
- ESP_LOGI(N455_OTA, "===== OTA 第 %d/%d 次 =====", retry+1, MAX_OTA_RETRY);
- if(n455_ota_stop)
+ 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)
{
- n455_ota_stop = 0;
+ n455_ota_ctx.n455_ota_stop_flag = 0;
if (client) {
esp_http_client_close(client);
esp_http_client_cleanup(client);
failed = false;
- n455_ota_stop = 0;
- uart_ack_received = false;
- uart_ack = 0xFF;
+ n455_ota_ctx.n455_ota_stop_flag = 0;
+ n455_ota_ctx.uart_ack_received = false;
+ n455_ota_ctx.uart_ack = 0xFF;
n455_g_ota_status = N455_OTA_STATUS_UPGRADING;
// --------------------
{
ESP_LOGE(N455_OTA, "Header check fail");
failed = true;
+ file_header_check = 1;
+ if (client) {
+ esp_http_client_close(client);
+ esp_http_client_cleanup(client);
+ client = NULL;
+ }
+ break;
}
}
memset(write_data,0,sizeof(write_data));
if (!failed) {
while (1) {
- if (n455_ota_stop) { failed = true; break; }
+ 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, "WRITE END:%d",real_write_len);
//超时退出不再继续ota升级
- if(n455_ota_stop)
+ if(n455_ota_ctx.n455_ota_stop_flag)
{
- n455_ota_stop = 0;
+ n455_ota_ctx.n455_ota_stop_flag = 0;
if (client) {
esp_http_client_close(client);
esp_http_client_cleanup(client);
if (!failed) {
n455_g_ota_status = N455_OTA_STATUS_SUCCESS;
- Https_Ota_Report_Status(n455_g_ota_status);
+ Https_Ota_Report_Status(N455_OTA_STATUS_SUCCESS);
Wifi_Stop();
vTaskDelay(10 / portTICK_PERIOD_MS);
Https_Ota_Clear_Upgrade_Flag();
// 失败,等3秒进入下一次重试
ESP_LOGI(N455_OTA, "重试");
- vTaskDelay(pdMS_TO_TICKS(1000));
+ vTaskDelay(pdMS_TO_TICKS(500));
}
- n455_ota_timeout=0;
- n455_ota_stop=0;
+
+ n455_ota_ctx.n455_ota_timeout_cnt=0;
+ n455_ota_ctx.n455_ota_stop_flag=0;
// 3次全部失败
ESP_LOGI(N455_OTA, "全部失败");
n455_g_ota_status = N455_OTA_STATUS_FAILED;
}
Https_Ota_Clear_Upgrade_Flag();
- 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);
{
if(n455_g_ota_status == N455_OTA_STATUS_UPGRADING && n455_g_ota_status > 0) //一直没有成功
{
- if(n455_ota_timeout > 0)
+ if(n455_ota_ctx.n455_ota_timeout_cnt > 0)
{
- n455_ota_timeout--;
- if(n455_ota_timeout == 0)
+ n455_ota_ctx.n455_ota_timeout_cnt--;
+ if(n455_ota_ctx.n455_ota_timeout_cnt == 0)
{
ESP_LOGI(N455_OTA, "ota timeout");
- n455_ota_stop = 1;
+ n455_ota_ctx.n455_ota_stop_flag = 1;
}
}
}else{
- n455_ota_timeout=0;
+ n455_ota_ctx.n455_ota_timeout_cnt=0;
}
}
*/
uint8_t N455_Ota_Version_Check(char *version,char *url)
{
- n455_ota_update_flag=0;
+ n455_ota_ctx.n455_ota_update_flag=0;
if(version == NULL || url == NULL)
{
return 0;
ESP_LOGI(N455_OTA, "ver: %s %04x FW_VERSION:%04x", version,new_version,FW_VERSION);
if(new_version != FW_VERSION && new_version != 0xffff)
{
- n455_ota_update_flag = 1;
+ n455_ota_ctx.n455_ota_update_flag = 1;
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");
*/
uint8_t N455_Ota_Get_Update_Flag(void)
{
- return n455_ota_update_flag;
+ return n455_ota_ctx.n455_ota_update_flag;
}
void N455_Ota_Clear_Update_Flag(void)
{
- n455_ota_update_flag=0;
+ n455_ota_ctx.n455_ota_update_flag=0;
}
/*
*函数功能: 455升级失败清空32升级标识
void N455_Ota_Clear_Upgrade_Flag(void)
{
n455_g_ota_status = N455_OTA_STATUS_IDLE; // 状态重置为空闲
- n455_ota_init=0; //升级初始化标志清零
- n455_ota_timeout =0; //升级超时标志清零
- n455_ota_stop = 0;//升级停止标志清零
- n455_ota_update_flag=0; //升级标识清除
+ n455_ota_ctx.n455_ota_init=0; //升级初始化标志清零
+ 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));
}
/**
}
uint8_t N455_Ota_Task(void)
{
- if(n455_ota_init)
+ if(n455_ota_ctx.n455_ota_init)
{
ESP_LOGI(N455_OTA, "N455 OTA running\n");
return 2;
}
- n455_ota_init=1; //升级初始化标志清零
- n455_ota_timeout =0; //升级超时标志清零
- n455_ota_stop = 0;//升级停止标志清零
+ n455_ota_ctx.n455_ota_init=1; //升级初始化标志清零
+ n455_ota_ctx.n455_ota_timeout_cnt =0; //升级超时标志清零
+ n455_ota_ctx.n455_ota_stop_flag = 0;//升级停止标志清零
n455_g_ota_status = N455_OTA_STATUS_UPGRADING;
BaseType_t ret = xTaskCreate(N455_Ota_Task_Cb, "n455_ota_task", N455_OTA_STACK_SIZE, NULL, 6, NULL);
if(ret !=pdPASS )
{
ESP_LOGI(N455_OTA, "task err\n");
- n455_ota_init=0;
+ n455_ota_ctx.n455_ota_init=0;
n455_g_ota_status = N455_OTA_STATUS_IDLE;
N455_Ota_Clear_Upgrade_Flag();
return 1;
#include "n455_ota.h"
#if ENABLE_MQTT_FUNC
+
+
#define ERR_CODE_OK 200
#define ERR_PID_SERIALNUMBER_ERR_CODE 401
#define ERR_JSON_CODE 402 //不是json字符串
#define REPLY_STATUS_FAIL 0
static const char *MQTT_TAG = "MQTT";
-static esp_mqtt_client_config_t mqtt_cfg = {0};
+// static esp_mqtt_client_config_t mqtt_cfg = {0};
static esp_mqtt_client_handle_t mqtt_client_handle = NULL;
// 发送队列静态内存
uint8_t mqtt_recv_queue_storage[MQTT_RECV_QUEUE_LENGTH * sizeof(mqtt_recv_msg_t)];
QueueHandle_t mqtt_recv_queue_handle=NULL;
StaticQueue_t mqtt_recv_queue_buffer;
-static uint8_t retry_connect_cnt = 0;
+
// MQTT 任务
TaskHandle_t mqtt_connect_task_handler;
mqtt_pulish_data_t publish_data;
//多任务安全标识
-static uint8_t mqtt_init_task_flag = 0;
-static uint8_t mqtt_send_data_count = 0;
-static uint8_t mqtt_init_flag = 0;
-static char cloud_msgID[64]="cloud_0196415f3b1f7e2ab7f72c4e8f91c0a2"; //应答需要用云端下发的msgID
-static uint8_t mqtt_check_connect = 0;
-static uint32_t recv_time = 0;
-static uint32_t get_time = 0;
+// static uint8_t mqtt_send_mcu_info_flag = 0;//发送主控信息标志,1表示已发送过,0表示未发送过
+// static uint8_t mqtt_init_task_flag = 0;
+// static uint8_t mqtt_send_data_count = 0;
+// static uint8_t mqtt_init_flag = 0;
+// static char cloud_msgID[64]="cloud_0196415f3b1f7e2ab7f72c4e8f91c0a2"; //应答需要用云端下发的msgID
+
+
+typedef struct {
+
+ uint8_t mqtt_init_flag; //mqtt 初始化
+ uint8_t mqtt_init_task_flag; //mqtt 任务初始化
+ uint8_t mqtt_send_data_count; //mqtt发送mcu计时器
+ uint8_t mqtt_send_mcu_info_flag; //mqtt发送mcu标识
+ uint8_t connect_flag;//mqtt连接成功的标识
+ uint8_t sub_flag;//Mqtt订阅成功的标识
+ //
+ char cloud_msgID[64]; //保存云端下发的msgid用作应答
+
+ esp_mqtt_client_config_t mqtt_cfg; //mqtt的客户端的配置集合
+
+}__attribute__((packed)) mqtt_ctx_t;
+static mqtt_ctx_t mqtt_ctx = {0};
+
static void log_error_if_nonzero(const char *message, int error_code)
{
if (error_code != 0) {
case TOPIC_FIRMWARE_OTA_UPGRADE: suffix = "firmware/upgrade"; break; //ota报文
+ case TOPIC_FIRMWARE_OTA_ACK: suffix ="firmware/ack";break; //ota回复
default: return NULL;
}
#if PID_A00D
}
}
+
+// ===================== MQTT 事件拆分(原大函数拆分为子函数) =====================
+static void mqtt_event_on_connected(esp_mqtt_event_handle_t event)
+{
+ esp_mqtt_client_handle_t mqtt_client = event->client;
+ int msg_id = 0;
+ mqtt_ctx.mqtt_send_data_count=0;
+
+ mqtt_ctx.connect_flag = 1;
+ mqtt_ctx.sub_flag = 0;
+ ESP_LOGI(MQTT_TAG, "set topic=%s",get_publish_topic(TOPIC_PROPERTY_SET));
+ ESP_LOGI(MQTT_TAG, "MQTT_EVENT_CONNECTED");
+ // /{pid}/{serialNumber}/property/post
+ #if MQTT_USER_DEFAULT_TOPIC
+ snprintf(topic_str,sizeof(topic_str),"%s",MQTT_TOPIC);
+ msg_id = esp_mqtt_client_subscribe(mqtt_client, topic_str, 0);
+ #else
+ ESP_LOGI(MQTT_TAG, "subscribe topic=set");
+ msg_id = esp_mqtt_client_subscribe(mqtt_client,get_publish_topic(TOPIC_PROPERTY_SET), 0);
+
+ ESP_LOGI(MQTT_TAG, "upgarde topic=%s",get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE));
+ msg_id = esp_mqtt_client_subscribe(mqtt_client,get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE), 0);
+
+ if(common_setting.ble_status.is_bind == 1) // 连接mqtt请求一次http绑定接口做绑定 绑定失败配网失败 绑定成功配网成功
+ {
+ common_setting.ble_status.is_bind = 0;
+ xEventGroupSetBits(common_event_group, (EventBits_t)COMMON_HTTP_BIND_BIT);
+ }
+ #endif
+}
+
+static void mqtt_event_on_disconnected(void)
+{
+
+ mqtt_ctx.connect_flag = 0;
+ mqtt_ctx.sub_flag = 0;
+ ESP_LOGI(MQTT_TAG, "MQTT_EVENT_DISCONNECTED");
+}
+
+static void mqtt_event_on_subscribed(void)
+{
+ mqtt_ctx.sub_flag = 1;
+ Mqtt_Send_Connect_Status();
+ ESP_LOGI(MQTT_TAG, "MQTT_EVENT_SUBSCRIBED");
+}
+
+static void mqtt_event_on_data(esp_mqtt_event_handle_t event)
+{
+ Mqtt_Recv_Data(event->topic, event->topic_len, event->data, event->data_len);
+}
+
+static void mqtt_event_on_error(esp_mqtt_event_handle_t event)
+{
+ ESP_LOGI(MQTT_TAG, "MQTT_EVENT_ERROR");
+ if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT)
+ {
+ int esp_tls_err = event->error_handle->esp_tls_last_esp_err;
+ int tls_stack_err = event->error_handle->esp_tls_stack_err;
+ int sock_errno = event->error_handle->esp_transport_sock_errno;
+ int verify_flags = event->error_handle->esp_tls_cert_verify_flags;
+
+ ESP_LOGI(MQTT_TAG, "esp-tls err:0x%04X", esp_tls_err);
+ ESP_LOGI(MQTT_TAG, "tls stack err:0x%04X", tls_stack_err);
+ ESP_LOGI(MQTT_TAG, "socket errno:%d", sock_errno);
+ ESP_LOGI(MQTT_TAG, "cert verify flags:%d", verify_flags);
+
+ if (tls_stack_err == 0x2180)
+ ESP_LOGE(MQTT_TAG, "证书格式非法");
+ if (tls_stack_err == 0x2700)
+ ESP_LOGE(MQTT_TAG, "证书不匹配服务器");
+ if (verify_flags != 0)
+ ESP_LOGE(MQTT_TAG, "SSL 证书校验失败");
+ }
+}
/*
* 函数功能:mqtt回调函数
*/
{
ESP_LOGI(MQTT_TAG, "base=%s, event_id=%" PRIi32, base, event_id);
esp_mqtt_event_handle_t event = event_data;
- esp_mqtt_client_handle_t mqtt_client = event->client;
- int msg_id;
+
switch ((esp_mqtt_event_id_t)event_id) {
case MQTT_EVENT_CONNECTED:
-
- mqtt_check_connect=0;
- retry_connect_cnt = 0;
- mqtt_send_data_count=0;
- common_setting.mqtt_user_cfg.mqtt_sub_flag = 0;
- common_setting.mqtt_user_cfg.mqtt_connect_flag = 1;
- ESP_LOGI(MQTT_TAG, "set topic=%s",get_publish_topic(TOPIC_PROPERTY_SET));
- ESP_LOGI(MQTT_TAG, "MQTT_EVENT_CONNECTED");
- // /{pid}/{serialNumber}/property/post
- #if MQTT_USER_DEFAULT_TOPIC
- snprintf(topic_str,sizeof(topic_str),"%s",MQTT_TOPIC);
- msg_id = esp_mqtt_client_subscribe(mqtt_client, topic_str, 0);
- #else
- ESP_LOGI(MQTT_TAG, "subscribe topic=set");
- msg_id = esp_mqtt_client_subscribe(mqtt_client,get_publish_topic(TOPIC_PROPERTY_SET), 0);
-
- ESP_LOGI(MQTT_TAG, "upgarde topic=%s",get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE));
- msg_id = esp_mqtt_client_subscribe(mqtt_client,get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE), 0);
-
- if(common_setting.ble_status.is_bind == 1) // 连接mqtt请求一次http绑定接口做绑定 绑定失败配网失败 绑定成功配网成功
- {
- common_setting.ble_status.is_bind = 0;
- xEventGroupSetBits(common_event_group, (EventBits_t)COMMON_HTTP_BIND_BIT);
- }
- #endif
-
+ mqtt_event_on_connected(event);
break;
case MQTT_EVENT_DISCONNECTED:
- common_setting.mqtt_user_cfg.mqtt_connect_flag = 0;
- common_setting.mqtt_user_cfg.mqtt_sub_flag = 0;
-
-
- ESP_LOGI(MQTT_TAG, "MQTT_EVENT_DISCONNECTED");
+ mqtt_event_on_disconnected();
break;
case MQTT_EVENT_SUBSCRIBED:
- common_setting.mqtt_user_cfg.mqtt_sub_flag =1;
- Mqtt_Send_Connect_Status();
+ mqtt_event_on_subscribed();
// Mqtt_Send_Mcu_Info();
ESP_LOGI(MQTT_TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d ", event->msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED:
- common_setting.mqtt_user_cfg.mqtt_sub_flag = 0;
+ mqtt_ctx.sub_flag = 0;
ESP_LOGI(MQTT_TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case 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);
- ESP_LOGI(MQTT_TAG, "DATA=%d %d\r\n", event->data_len, event->topic_len);
- Mqtt_Recv_Data(event->topic,event->topic_len,event->data,event->data_len);
+ ESP_LOGI(MQTT_TAG, "DATA=%.*s\r\n", event->data_len, event->data);
+
+ mqtt_event_on_data(event);
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(MQTT_TAG, "MQTT_EVENT_ERROR");
- if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
- // log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
- // log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
- // log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
- // ESP_LOGI(MQTT_TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
- int esp_tls_err = event->error_handle->esp_tls_last_esp_err;
- int tls_stack_err = event->error_handle->esp_tls_stack_err;
- int sock_errno = event->error_handle->esp_transport_sock_errno;
- int verify_flags = event->error_handle->esp_tls_cert_verify_flags;
-
- ESP_LOGI(MQTT_TAG, " esp-tls err: 0x%04X\n", esp_tls_err);
- ESP_LOGI(MQTT_TAG, " tls stack err: 0x%04X\n", tls_stack_err);
- ESP_LOGI(MQTT_TAG, " socket errno: %d\n", sock_errno);
- ESP_LOGI(MQTT_TAG, " verify_flags errno: %d\n", verify_flags);
- if(tls_stack_err == 0x2180)
- {
- ESP_LOGE(MQTT_TAG, "证书格式非法\n");
- }
- if(tls_stack_err == 0x2700)
- {
- ESP_LOGE(MQTT_TAG, "证书合法 但不匹配服务器\n");
- }
- // ==============================================
- // 是不是证书错误!!!
- // ==============================================
- if (event->error_handle->esp_tls_cert_verify_flags != 0) // SSL - 证书不匹配
- {
- ESP_LOGE(MQTT_TAG, "verify_flags errno\n");
- }
- }
- //没意义 有链接才有断开的操作 这个不成立的
+ mqtt_ctx.sub_flag = 0;
+ mqtt_event_on_error(event);
break;
#if ENABLE_HTTPS_OTA_FUNC
if(Https_Get_Ota_Status())
{
- mqtt_check_connect=0;
return;
}
#endif
- // if(common_setting.mqtt_user_cfg.mqtt_start_flag == 1 && common_setting.wifi_status.wifi_state == 1) //首次连接mqtt
- // {
- // if(mqtt_check_connect <= 60) //连续五次连不上就不连 重新配网
- // {
- // if(mqtt_check_connect == 60)
- // {
- // common_setting.mqtt_user_cfg.mqtt_start_flag = 0;
- // //断开mqtt连接
- // //清除配网信息重新开蓝牙
- // xEventGroupSetBits(common_event_group, COMMON_MQTT_INIT_BIT);//初始化失败了 或者连不成功
-
- // }
- // mqtt_check_connect+=1;
- // }
- // }else{
- // mqtt_check_connect=0;
- // }
}
/*
*函数功能:mqtt连接状态发给mcu
*/
static esp_err_t Mqtt_App_Init(void)
{
-
- memset(&publish_data,0x00,sizeof(publish_data));
+
esp_err_t err = ESP_FAIL;
if(common_setting.wifi_status.wifi_state != 1 || common_setting.ble_status.nimble_flag == 0)
{
ESP_LOGI(MQTT_TAG, "mqtt url error");
return ESP_FAIL;
}
- if(mqtt_init_flag)
+ if(mqtt_ctx.mqtt_init_flag)
{
//
ESP_LOGI(MQTT_TAG, "Mqtt initialized.\n");
return ESP_OK;
}
+ strncpy(mqtt_ctx.cloud_msgID, "cloud_0196415f3b1f7e2ab7f72c4e8f91c2331", sizeof(mqtt_ctx.cloud_msgID)-1);
+ memset(&publish_data,0x00,sizeof(mqtt_pulish_data_t));
mqtt_client_handle=NULL;
- memset(&mqtt_cfg, 0, sizeof(mqtt_cfg));
- retry_connect_cnt = 0;
- mqtt_cfg.session.keepalive = 100; //50S
- mqtt_cfg.session.disable_keepalive = 0;
+ 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_cfg.network.disable_auto_reconnect = false;
+ mqtt_ctx.mqtt_cfg.network.disable_auto_reconnect = false;
// common_setting.mqtt_cfg.broker.address.transport = MQTT_TRANSPORT_OVER_TCP;
// common_setting.mqtt_cfg.buffer.out_size = 2048;
// common_setting.mqtt_cfg.buffer.size = 2048;
- ESP_LOGI(MQTT_TAG, "URI: %s %s", common_storage.net_info.mqtt_sub_url,common_setting.net_pairing_info.mqtt_sub_url);
- mqtt_cfg.broker.address.uri = (const char *)common_storage.net_info.mqtt_sub_url;
-
-
-
-
+ ESP_LOGI(MQTT_TAG, "URI: %s", common_storage.net_info.mqtt_sub_url);
+ mqtt_ctx.mqtt_cfg.broker.address.uri = (const char *)common_storage.net_info.mqtt_sub_url;
- mqtt_cfg.broker.verification.certificate =(const char *)common_storage.ssl_cert[SSL_SERVER_CRT].content;
- mqtt_cfg.credentials.authentication.certificate = (const char *)common_storage.ssl_cert[SSL_CLIENT_CRT].content;
- mqtt_cfg.credentials.authentication.key = (const char *)common_storage.ssl_cert[SSL_CLIENT_KEY].content;
+ mqtt_ctx.mqtt_cfg.broker.verification.certificate =(const char *)common_storage.ssl_cert[SSL_SERVER_CRT].content;
+ mqtt_ctx.mqtt_cfg.credentials.authentication.certificate = (const char *)common_storage.ssl_cert[SSL_CLIENT_CRT].content;
+ mqtt_ctx.mqtt_cfg.credentials.authentication.key = (const char *)common_storage.ssl_cert[SSL_CLIENT_KEY].content;
char pid_str[10]={0};
#if PID_A00D
sprintf(pid_str,"0x%04X",common_setting.mcu_info.pid);
#if MQTT_USER_DEFAULT_TOPIC
common_setting.mqtt_cfg.credentials.client_id = "basicPubSub";
#else
- // mqtt_cfg.credentials.client_id = (const char *)pid_str;
- mqtt_cfg.credentials.client_id = (const char *)common_storage.net_info.serial_number;
+ // mqtt_ctx.mqtt_cfg.credentials.client_id = (const char *)pid_str;
+ mqtt_ctx.mqtt_cfg.credentials.client_id = (const char *)common_storage.net_info.serial_number;
#endif
- ESP_LOGI(MQTT_TAG, "Mqtt init %s",pid_str);
- mqtt_cfg.task.priority = 4;
- mqtt_client_handle = esp_mqtt_client_init(&mqtt_cfg);
+ ESP_LOGI(MQTT_TAG, "Mqtt init %s %s",pid_str,common_storage.net_info.serial_number);
+ mqtt_ctx.mqtt_cfg.task.priority = 4;
+ mqtt_client_handle = esp_mqtt_client_init(&mqtt_ctx.mqtt_cfg);
if(mqtt_client_handle == NULL){
ESP_LOGI(MQTT_TAG, "client_handle null\n");
err = ESP_FAIL;
return err;
}
- ESP_LOGI(MQTT_TAG, "Mqtt init");
/* 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(mqtt_client_handle, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
if(err == ESP_OK)
err = esp_mqtt_client_start(mqtt_client_handle);
if(err == ESP_OK)
{
- mqtt_init_flag=1;
+ mqtt_ctx.mqtt_init_flag=1;
ESP_LOGI(MQTT_TAG, "success \n");
}
}
*/
uint8_t Mqtt_Get_Connect_Flag(void)
{
- if(mqtt_client_handle != NULL &&common_setting.mqtt_user_cfg.mqtt_connect_flag == 1 && \
- common_setting.mqtt_user_cfg.mqtt_sub_flag == 1 && \
+ if(mqtt_client_handle != NULL && mqtt_ctx.connect_flag == 1 && \
+ mqtt_ctx.sub_flag == 1 && \
common_setting.ble_status.ble_state == 0 && common_setting.wifi_status.wifi_state == 1)
{
return 1;
}
return 0;
}
-
-/*
- *函数功能:开机连上mqtt订阅成功之后发送主控信息
-*/
-static uint8_t mqtt_send_mcu_info_flag = 0;//发送主控信息标志,1表示已发送过,0表示未发送过
-void Mqtt_Send_Mcu_Info(void)
-{
-
- if(mqtt_send_mcu_info_flag)
- {
- return;
- }
- if(!Mqtt_Get_Connect_Flag())
- {
- return;
- }
- char pid_str[10] = {0};
- char vid_str[10] = {0};
- char wifi_soc[16] = {"Unknown"};
-
-
- // 获取芯片型号
- uint8_t chip_len = 0;
- get_chip_info(wifi_soc, &chip_len);
- if (chip_len == 0) {
- strcpy(wifi_soc, "Unknown");
- }
- // PID、VID 大写十六进制格式
- snprintf(pid_str, sizeof(pid_str), "%04X", common_setting.mcu_info.pid);
- snprintf(vid_str, sizeof(vid_str), "%04X", common_setting.mcu_info.vid);
-
-
-
-
- // 版本号格式化 0x0015 → V0021
- char fw_ver[12] = {0}, wifi_ver[12] = {0};
- snprintf(fw_ver, sizeof(fw_ver), "0x%04X", common_setting.mcu_info.fw_version);
- snprintf(wifi_ver, sizeof(wifi_ver), "0x%04X", (unsigned int)FW_VERSION);
-
- // ===================== 构建正确的 JSON =====================
- cJSON *root = cJSON_CreateObject();
- if (root == NULL) return;
-
-
- // 把设备信息加到里面
- cJSON_AddStringToObject(root, "pid", pid_str);
- cJSON_AddStringToObject(root, "vid", vid_str);
- cJSON_AddStringToObject(root, "wifi_soc", wifi_soc);
- cJSON_AddStringToObject(root, "fw_version", fw_ver);
- 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);
- cJSON_AddNumberToObject(root, "9", common_setting.is_connect_usb);
-
-
- // 生成压缩格式JSON(不带换行,HTTP专用)
- char *post_data = cJSON_Print(root);
- // char *post_data = cJSON_PrintUnformatted(root);
- cJSON_Delete(root);
- root=NULL;
- if (post_data == NULL) return;
-
-
- if(strlen(post_data) >= MQTT_SEND_RECV_DATA_BUF_SIZE)
- {
- ESP_LOGI(MQTT_TAG, "len large %d", strlen(post_data));
- free(post_data);
- post_data=NULL;
- return;
- }
- if(strlen(post_data) == 0 || strlen(get_publish_topic(TOPIC_PROPERTY_ACK_POST)) == 0)
- {
- ESP_LOGI(MQTT_TAG, "len no %d", strlen(get_publish_topic(TOPIC_PROPERTY_ACK_POST)),strlen(post_data));
- free(post_data);
- post_data=NULL;
- return;
- }
- // static char data_buf[64] = {0};
- // static mqtt_send_msg_t send_msg={0};
- // memcpy(data_buf,post_data,strlen(post_data));
- ESP_LOGI(MQTT_TAG, "CJSON:%s", post_data);
-
-
- // memcpy(send_msg.topic,get_publish_topic(TOPIC_PROPERTY_ACK_POST),strlen(get_publish_topic(TOPIC_PROPERTY_ACK_POST)));
-
- if(mqtt_dev_report_queue_handle==NULL)
- {
- ESP_LOGI(MQTT_TAG, "Mqtt_Send_Mcu_Info is null\n");
- return;
- }
- user_esp_mqtt_client_publish(post_data,strlen(post_data),get_publish_topic(TOPIC_PROPERTY_ACK_POST));
- free(post_data);
- post_data = NULL;
- mqtt_send_mcu_info_flag=0;
- return;
-}
-void mqtt_get_dev_test(uint16_t *dpid,uint8_t num);
-/*
- * 函数功能:mcu信息
- *
- */
-void Mqtt_Send_Data(void)
-{
- // return;
- // ESP_LOGI(MQTT_TAG,"HR!!!!!",mqtt_send_data_count);
- // ESP_LOGI(MQTT_TAG,"HR!!!!!:%d",mqtt_send_data_count);
- if(mqtt_send_data_count % 5 == 0)
- {
- // ESP_LOGI(MQTT_TAG,"HR!!!11",mqtt_send_data_count);
- // uint16_t test_dpid[81]= {745,777,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,271,272,273,274,275,276,
- // 277,278,279,280,283,
- // 284,285,286,287,288,289,59,60,62,63,64,65,66,67,68,69,70,71,72,73,
- // 745,777,779,780,781,782,783,784,785,786,745,777,779,780,781,782,783,784,785,786,745,777,779,780,781,782,783,784,785,786};
-
- // // mqtt_get_dev_test(test_dpid,80);
-
- // mqtt_get_dev_test(test_dpid,80);
-
-
- // mqtt_get_dev_test(test_dpid,49);
- }
- if(mqtt_send_data_count >= 30)
- {
- mqtt_send_data_count = 0;
- Mqtt_Send_Mcu_Info();
- }
- mqtt_send_data_count++;
-}
/*
* 函数功能:原始数据传给队列
*/
// 拷贝主题
memcpy(msg.topic, topic, topic_len);
+ msg.topic[topic_len] = '\0';
msg.topic_len = topic_len;
// 拷贝数据
memcpy(msg.data, data, data_len);
+ msg.data[data_len] = '\0';
msg.data_len = data_len;
// ESP_LOGI(MQTT_TAG, "topic=%s data=%s\n",topic,data);
if(mqtt_recv_queue_handle){
void Mqtt_Connect_Status_Check(void)
{
static uint8_t mqtt_init = 0;
- #if (!ENABLE_HTTP_N455_OTA_FUNC)
+ #if (ENABLE_HTTP_N455_OTA_FUNC == 0 && ENABLE_HTTPS_OTA_FUNC == 1)
if((xEventGroupWaitBits(common_event_group, COMMON_MQTT_CONNECT_BIT, pdFALSE, pdFALSE, 0) & COMMON_MQTT_CONNECT_BIT) != 0 && common_setting.ble_status.ble_state == 0\
&&(Https_Get_Ota_Status() == HTTPS_OTA_STATUS_IDLE))
#else
xEventGroupClearBits(common_event_group, COMMON_MQTT_DISCONNECT_BIT);
vTaskDelay(50 / portTICK_PERIOD_MS);
- if(mqtt_init_flag == 1 && mqtt_client_handle != NULL){
+ if(mqtt_ctx.mqtt_init_flag == 1 && mqtt_client_handle != NULL){
if(esp_mqtt_client_stop(mqtt_client_handle) == ESP_OK)
{
}
vTaskDelay(50 / portTICK_PERIOD_MS);
esp_mqtt_client_destroy(mqtt_client_handle);
- mqtt_client_handle=NULL;
- retry_connect_cnt = 0;
- mqtt_init_flag=0;
- common_setting.mqtt_user_cfg.mqtt_connect_flag = 0;
- common_setting.mqtt_user_cfg.mqtt_sub_flag = 0;
+ mqtt_client_handle=NULL;;
+ mqtt_ctx.mqtt_init_flag=0;
+ mqtt_ctx.connect_flag = 0;
+ mqtt_ctx.sub_flag = 0;
}
}
if((xEventGroupWaitBits(common_event_group, COMMON_MQTT_INIT_BIT, pdFALSE, pdFALSE, 0) & COMMON_MQTT_INIT_BIT) != 0 )
{
xEventGroupClearBits(common_event_group, COMMON_MQTT_INIT_BIT);
- vTaskDelay(50 / portTICK_PERIOD_MS);
-
- //断开wifi
- Wifi_Stop();
- vTaskDelay(50 / portTICK_PERIOD_MS);
//断开mqtt释放资源
Mqtt_Disconnect();
vTaskDelay(50 / portTICK_PERIOD_MS);
- User_Erase_NotReset();
- xEventGroupSetBits(ble_event_group, BLE_ON_BIT);
//http请求绑定结果
//
}
}
+uint8_t get_mqtt_init_flag(void)
+{
+ return mqtt_ctx.mqtt_init_flag;
+}
/*
* 函数功能:断开mqtt连接 释放mqtt资源
*/
void Mqtt_Disconnect(void)
{
- if(mqtt_init_flag != 1)
+ if(!mqtt_ctx.mqtt_init_flag)
{
return;
}
if(mqtt_client_handle == NULL)
{
- mqtt_init_flag = 0;
+ mqtt_ctx.mqtt_init_flag = 0;
return;
}
- mqtt_init_flag = 0;
+ mqtt_ctx.mqtt_init_flag = 0;
esp_mqtt_client_stop(mqtt_client_handle);
//
vTaskDelay(pdMS_TO_TICKS(100));
vQueueDelete(mqtt_recv_queue_handle);
mqtt_recv_queue_handle = NULL;
}
- mqtt_init_task_flag=0;
+ mqtt_ctx.mqtt_init_task_flag=0;
mqtt_client_handle = NULL;
- retry_connect_cnt = 0;
- common_setting.mqtt_user_cfg.mqtt_connect_flag = 0;
- common_setting.mqtt_user_cfg.mqtt_sub_flag = 0;
- mqtt_init_flag = 0;
+ mqtt_ctx.connect_flag = 0;
+ mqtt_ctx.sub_flag = 0;
+ mqtt_ctx.mqtt_init_flag = 0;
memset(&publish_data, 0, sizeof(publish_data));
ESP_LOGW(MQTT_TAG, "MQTT STOP");
}
+/*
+ *函数功能:开机连上mqtt订阅成功之后发送主控信息
+*/
+
+void Mqtt_Send_Mcu_Info(void)
+{
+
+ if(mqtt_ctx.mqtt_send_mcu_info_flag)
+ {
+ return;
+ }
+ if(!Mqtt_Get_Connect_Flag())
+ {
+ return;
+ }
+ char pid_str[10] = {0};
+ char vid_str[10] = {0};
+ char wifi_soc[16] = {"Unknown"};
+
+
+ // 获取芯片型号
+ uint8_t chip_len = 0;
+ get_chip_info(wifi_soc, &chip_len);
+ if (chip_len == 0) {
+ strcpy(wifi_soc, "Unknown");
+ }
+ // PID、VID 大写十六进制格式
+ snprintf(pid_str, sizeof(pid_str), "%04X", common_setting.mcu_info.pid);
+ snprintf(vid_str, sizeof(vid_str), "%04X", common_setting.mcu_info.vid);
+
+
+
+
+ // 版本号格式化 0x0015 → V0021
+ char fw_ver[12] = {0}, wifi_ver[12] = {0};
+ snprintf(fw_ver, sizeof(fw_ver), "0x%04X", common_setting.mcu_info.fw_version);
+ snprintf(wifi_ver, sizeof(wifi_ver), "0x%04X", (unsigned int)FW_VERSION);
+
+ // ===================== 构建正确的 JSON =====================
+ cJSON *root = cJSON_CreateObject();
+ if (root == NULL) return;
+
+
+ // 把设备信息加到里面
+ cJSON_AddStringToObject(root, "pid", pid_str);
+ cJSON_AddStringToObject(root, "vid", vid_str);
+ cJSON_AddStringToObject(root, "wifi_soc", wifi_soc);
+ cJSON_AddStringToObject(root, "fw_version", fw_ver);
+ 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);
+ cJSON_AddNumberToObject(root, "9", common_setting.is_connect_usb);
+
+
+ // 生成压缩格式JSON(不带换行,HTTP专用)
+ // char *post_data = cJSON_Print(root);
+ char *post_data = cJSON_PrintUnformatted(root);
+ cJSON_Delete(root);
+ root=NULL;
+ if (post_data == NULL) return;
+
+
+ if(strlen(post_data) >= MQTT_SEND_RECV_DATA_BUF_SIZE)
+ {
+ ESP_LOGI(MQTT_TAG, "len large %d", strlen(post_data));
+ free(post_data);
+ post_data=NULL;
+ return;
+ }
+ if(strlen(post_data) == 0 || strlen(get_publish_topic(TOPIC_PROPERTY_ACK_POST)) == 0)
+ {
+ ESP_LOGI(MQTT_TAG, "len no %d", strlen(get_publish_topic(TOPIC_PROPERTY_ACK_POST)),strlen(post_data));
+ free(post_data);
+ post_data=NULL;
+ return;
+ }
+ // static char data_buf[64] = {0};
+ // static mqtt_send_msg_t send_msg={0};
+ // memcpy(data_buf,post_data,strlen(post_data));
+ ESP_LOGI(MQTT_TAG, "CJSON:%s", post_data);
+
+
+ // memcpy(send_msg.topic,get_publish_topic(TOPIC_PROPERTY_ACK_POST),strlen(get_publish_topic(TOPIC_PROPERTY_ACK_POST)));
+
+ if(mqtt_dev_report_queue_handle==NULL)
+ {
+ ESP_LOGI(MQTT_TAG, "Mqtt_Send_Mcu_Info is null\n");
+ return;
+ }
+ user_esp_mqtt_client_publish(post_data,strlen(post_data),get_publish_topic(TOPIC_PROPERTY_ACK_POST));
+ free(post_data);
+ post_data = NULL;
+ mqtt_ctx.mqtt_send_mcu_info_flag=0;
+ return;
+}
+void mqtt_get_dev_test(uint16_t *dpid,uint8_t num);
+/*
+ * 函数功能:mcu信息
+ *
+ */
+void Mqtt_Send_Data(void)
+{
+ // return;
+ // ESP_LOGI(MQTT_TAG,"HR!!!!!",mqtt_send_data_count);
+ // ESP_LOGI(MQTT_TAG,"HR!!!!!:%d",mqtt_send_data_count);
+ if(mqtt_ctx.mqtt_send_data_count % 5 == 0)
+ {
+ // ESP_LOGI(MQTT_TAG,"HR!!!11",mqtt_send_data_count);
+ // uint16_t test_dpid[81]= {745,777,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,271,272,273,274,275,276,
+ // 277,278,279,280,283,
+ // 284,285,286,287,288,289,59,60,62,63,64,65,66,67,68,69,70,71,72,73,
+ // 745,777,779,780,781,782,783,784,785,786,745,777,779,780,781,782,783,784,785,786,745,777,779,780,781,782,783,784,785,786};
+
+ // // mqtt_get_dev_test(test_dpid,80);
+
+ // mqtt_get_dev_test(test_dpid,80);
+
+
+ // mqtt_get_dev_test(test_dpid,49);
+ }
+ if(mqtt_ctx.mqtt_send_data_count >= 30)
+ {
+ mqtt_ctx.mqtt_send_data_count = 0;
+ Mqtt_Send_Mcu_Info();
+ }
+ mqtt_ctx.mqtt_send_data_count++;
+}
+
// 是否是纯数字 如key value 特殊情况
bool is_pure_number(const char *str)
{
char esp_pid[8] = {0};
snprintf(esp_pid,sizeof(esp_pid)-1,"0x%04X",DEVICE_PID);
- //主控的pid
- if(strcmp(mcu_pid,mcuCode->valuestring) == 0)
+ //esp32 pid
+ if(strcmp(esp_pid,mcuCode->valuestring) == 0)
{
pid_check_flag=1;
#if ENABLE_HTTPS_OTA_FUNC
Https_Ota_Version_Check(version->valuestring,filePath->valuestring);
#endif
}
- //esp32 pid
- else if(strcmp(esp_pid,mcuCode->valuestring) == 0)
+ //主控的pid
+ else if(strcmp(mcu_pid,mcuCode->valuestring) == 0)
{
pid_check_flag=1;
#if ENABLE_HTTP_N455_OTA_FUNC
#endif
}else{
//和本地一个都不匹配就视为无效的报文
+ pid_check_flag=2;
break;
}
}
- if(pid_check_flag){
+ if(pid_check_flag == 1){
//先检查模组是否需要升级 不需要就检查主控是否需要升级
#if ENABLE_HTTPS_OTA_FUNC
if(Https_Ota_Get_Update_Flag())
#endif
}else
{
- ESP_LOGI(MQTT_TAG,"pid is null\n");
+ if(pid_check_flag == 2) //pid和本地不一致
+ {
+ ESP_LOGI(MQTT_TAG,"pid is null\n");
+ }else{
+ ESP_LOGI(MQTT_TAG,"is valide\n");
+ }
+
}
}
// ========== cJSON 解析(安全版)==========
cJSON *root = cJSON_Parse(data);
if (root == NULL) {
- ESP_LOGW(MQTT_TAG, "JSON error");
+ ESP_LOGI(MQTT_TAG, "JSON error");
return;
}
cJSON *version = cJSON_GetObjectItem(root , "version");
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);
if(root != NULL)
{
if(strlen(msgId->valuestring) != 0)
{
- strncpy(publish_data.msgid, msgId->valuestring, sizeof(publish_data.msgid)-1);
+ strncpy(mqtt_ctx.cloud_msgID, msgId->valuestring, sizeof(mqtt_ctx.cloud_msgID)-1);
cJSON *params = cJSON_GetObjectItem(payload, "params");//
if (params == NULL || !cJSON_IsArray(params)) {
// }
int code = ERR_CODE_OK;
-
- recv_time = xTaskGetTickCount();
+
int ret = mqtt_set_dev_property(params);
if(ret == 0)
{
code=ERR_UART_SEND_ERR_CODE;
}
- cJSON *replay_root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
+ cJSON *replay_root = create_publish_ack_json(msgId->valuestring,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
if(replay_root)
{
mqtt_send_publish_json(TOPIC_PROPERTY_ACK_POST,replay_root);
if(strlen(msgId->valuestring) != 0)
{
- strncpy(publish_data.msgid, msgId->valuestring, sizeof(publish_data.msgid)-1);
+ strncpy(mqtt_ctx.cloud_msgID, msgId->valuestring, sizeof(mqtt_ctx.cloud_msgID)-1);
cJSON *params = cJSON_GetObjectItem(payload, "params");//
if (params == NULL || !cJSON_IsArray(params)) {
{
if(strlen(msgId->valuestring) != 0)
{
- strncpy(publish_data.msgid, msgId->valuestring, sizeof(publish_data.msgid)-1);
+ strncpy(mqtt_ctx.cloud_msgID, msgId->valuestring, sizeof(mqtt_ctx.cloud_msgID)-1);
+ mqtt_ctx.cloud_msgID[sizeof(mqtt_ctx.cloud_msgID)-1] = 0;
cJSON *input = cJSON_GetObjectItem(payload, "input");//
if (input == NULL || !cJSON_IsArray(input)) {
cJSON *replay_root = NULL;
cJSON *serviceId = cJSON_GetObjectItem(payload, "serviceId");//
if (serviceId == NULL || !cJSON_IsString(serviceId) || serviceId->valuestring == NULL) {
- replay_root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
+ replay_root = create_publish_ack_json(msgId->valuestring,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
}else{
strncpy(publish_data.serviceId, msgId->valuestring, sizeof(publish_data.serviceId)-1);
- replay_root = create_publish_ack_json(publish_data.msgid,serviceId->valuestring,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
+ replay_root = create_publish_ack_json(msgId->valuestring,serviceId->valuestring,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
}
if(replay_root)
*/
uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len)
{
- strncpy(publish_data.msgid,cloud_msgID,strlen(cloud_msgID));
+ strncpy(publish_data.msgid,mqtt_ctx.cloud_msgID,strlen(mqtt_ctx.cloud_msgID));
+ publish_data.msgid[sizeof(publish_data.msgid)-1] = 0;
uint8_t mode = 0; //0是使用云端生成下发的 属于应答 1 是使用设备端生成的 属于设备主动上报的
//应答
cJSON_Delete(root);
root=NULL;
}
-
- get_time = 0;
root=NULL;
return 1;
}
*/
void Mqtt_Init_Task(void)
{
- if(mqtt_init_task_flag){
+ if(mqtt_ctx.mqtt_init_task_flag){
ESP_LOGI(MQTT_TAG, "Mqtt_Init_Task inited");
return;
}
- mqtt_init_task_flag=1;
+ mqtt_ctx.mqtt_init_task_flag=1;
mqtt_recv_queue_handle = xQueueCreateStatic(MQTT_RECV_QUEUE_LENGTH, MQTT_RECV_QUEUE_ITEM_SIZE, mqtt_recv_queue_storage, &mqtt_recv_queue_buffer);
mqtt_dev_report_queue_handle = xQueueCreateStatic(MQTT_SEND_QUEUE_LENGTH, MQTT_SEND_QUEUE_ITEM_SIZE, mqtt_dev_report_queue_storage, &mqtt_dev_report_queue_buffer);