From 86b0f39e69148f149fbd508c3fb89313fca87029 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Tue, 16 Jul 2024 09:54:58 +0300 Subject: [PATCH] WIP --- README.md | 2 +- components/zh_config | 2 +- main/zh_gateway.c | 94 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 95 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4d01a2e..3ebe60f 100755 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Gateway for ESP32 ESP-IDF for data exchange between ESP-NOW devices and MQTT bro 2. ESP-NOW mesh network based on the [zh_network](https://github.com/aZholtikov/zh_network). 3. For initial settings use "menuconfig -> ZH Gateway Configuration". After first boot all settings (except work mode) will be stored in NVS memory for prevente change during OTA firmware update. But it is highly recommended to set up the configuration via menuconfig before updating. 4. To restart the gateway, send the "restart" command to the root topic of the gateway (example - "homeassistant/gateway/70-03-9F-44-BE-F7"). -5. To update the gateway firmware, send the "update" command to the root topic of the gateway (example - "homeassistant/gateway/70-03-9F-44-BE-F7"). The update path should be like as "https://your_server/zh_gateway_esp32.bin". The online status of the update is displayed in the root gateway topic. +5. To update the gateway firmware, send the "update" command to the root topic of the gateway (example - "homeassistant/gateway/70-03-9F-44-BE-F7"). The update path should be like as "https://your_server/zh_gateway_esp32.bin". The online status of the update is displayed in the root gateway topic and Syslog server (if enabled). ## Build and flash diff --git a/components/zh_config b/components/zh_config index 49a4564..db717bf 160000 --- a/components/zh_config +++ b/components/zh_config @@ -1 +1 @@ -Subproject commit 49a4564b99119d9713bb60790bbf341782c03647 +Subproject commit db717bf6eec149759082c00ecf2fca81faf94adf diff --git a/main/zh_gateway.c b/main/zh_gateway.c index 91d2c39..fc3c231 100755 --- a/main/zh_gateway.c +++ b/main/zh_gateway.c @@ -475,9 +475,35 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve break; case ZHPT_UPDATE_FAIL: esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_fail", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + char *mac = (char *)heap_caps_malloc(18, MALLOC_CAP_8BIT); + memset(mac, 0, 18); + sprintf(mac, "" MAC_STR "", MAC2STR(recv_data->mac_addr)); + zh_syslog_send(ZH_USER, ZH_ERR, mac, zh_get_device_type_value_name(data->device_type), "Firmware update fail. Incorrect bin file."); + heap_caps_free(mac); + } break; case ZHPT_UPDATE_SUCCESS: esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_success", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + char *mac = (char *)heap_caps_malloc(18, MALLOC_CAP_8BIT); + memset(mac, 0, 18); + sprintf(mac, "" MAC_STR "", MAC2STR(recv_data->mac_addr)); + zh_syslog_send(ZH_USER, ZH_INFO, mac, zh_get_device_type_value_name(data->device_type), "Firmware update success."); + heap_caps_free(mac); + } + break; + case ZHPT_ERROR: + if (gateway_config->syslog_is_enable == true) + { + char *mac = (char *)heap_caps_malloc(18, MALLOC_CAP_8BIT); + memset(mac, 0, 18); + sprintf(mac, "" MAC_STR "", MAC2STR(recv_data->mac_addr)); + zh_syslog_send(ZH_USER, ZH_ERR, mac, zh_get_device_type_value_name(data->device_type), data->payload_data.status_message.error_message.message); + heap_caps_free(mac); + } break; default: break; @@ -940,6 +966,9 @@ void zh_mqtt_event_handler(void *arg, esp_event_base_t event_base, int32_t event void zh_self_ota_update_task(void *pvParameter) { gateway_config_t *gateway_config = pvParameter; + char *mac = (char *)heap_caps_malloc(18, MALLOC_CAP_8BIT); + memset(mac, 0, 18); + sprintf(mac, "" MAC_STR "", MAC2STR(gateway_config->self_mac)); xSemaphoreTake(gateway_config->self_ota_in_progress_mutex, portMAX_DELAY); char *topic = (char *)heap_caps_malloc(strlen(gateway_config->software_config.mqtt_topic_prefix) + strlen(zh_get_device_type_value_name(ZHDT_GATEWAY)) + 20, MALLOC_CAP_8BIT); memset(topic, 0, strlen(gateway_config->software_config.mqtt_topic_prefix) + strlen(zh_get_device_type_value_name(ZHDT_GATEWAY)) + 20); @@ -948,6 +977,10 @@ void zh_self_ota_update_task(void *pvParameter) esp_ota_handle_t update_handle = {0}; const esp_partition_t *update_partition = NULL; esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_begin", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + zh_syslog_send(ZH_USER, ZH_INFO, mac, zh_get_device_type_value_name(ZHDT_GATEWAY), "Firmware update begin."); + } const esp_app_desc_t *app_info = esp_app_get_description(); char *app_name = (char *)heap_caps_malloc(strlen(gateway_config->software_config.firmware_upgrade_url) + strlen(app_info->project_name) + 6, MALLOC_CAP_8BIT); memset(app_name, 0, strlen(gateway_config->software_config.firmware_upgrade_url) + strlen(app_info->project_name) + 6); @@ -965,6 +998,10 @@ void zh_self_ota_update_task(void *pvParameter) esp_http_client_fetch_headers(https_client); update_partition = esp_ota_get_next_update_partition(NULL); esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_progress", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + zh_syslog_send(ZH_USER, ZH_INFO, mac, zh_get_device_type_value_name(ZHDT_GATEWAY), "Firmware update progress."); + } esp_ota_begin(update_partition, OTA_WITH_SEQUENTIAL_WRITES, &update_handle); for (;;) { @@ -974,7 +1011,12 @@ void zh_self_ota_update_task(void *pvParameter) esp_http_client_close(https_client); esp_http_client_cleanup(https_client); esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_error_data_size", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + zh_syslog_send(ZH_USER, ZH_ERR, mac, zh_get_device_type_value_name(ZHDT_GATEWAY), "Firmware update error. Incorrect size of read data."); + } heap_caps_free(topic); + heap_caps_free(mac); xSemaphoreGive(gateway_config->self_ota_in_progress_mutex); vTaskDelete(NULL); } @@ -992,7 +1034,12 @@ void zh_self_ota_update_task(void *pvParameter) esp_http_client_close(https_client); esp_http_client_cleanup(https_client); esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_fail", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + zh_syslog_send(ZH_USER, ZH_ERR, mac, zh_get_device_type_value_name(ZHDT_GATEWAY), "Firmware update fail. Incorrect bin file."); + } heap_caps_free(topic); + heap_caps_free(mac); xSemaphoreGive(gateway_config->self_ota_in_progress_mutex); vTaskDelete(NULL); } @@ -1000,7 +1047,12 @@ void zh_self_ota_update_task(void *pvParameter) esp_http_client_close(https_client); esp_http_client_cleanup(https_client); esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_success", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + zh_syslog_send(ZH_USER, ZH_INFO, mac, zh_get_device_type_value_name(ZHDT_GATEWAY), "Firmware update success."); + } heap_caps_free(topic); + heap_caps_free(mac); zh_espnow_data_t data = {0}; data.device_type = ZHDT_GATEWAY; data.payload_type = ZHPT_KEEP_ALIVE; @@ -1013,6 +1065,10 @@ void zh_self_ota_update_task(void *pvParameter) void zh_espnow_ota_update_task(void *pvParameter) { gateway_config_t *gateway_config = pvParameter; + char *mac = (char *)heap_caps_malloc(18, MALLOC_CAP_8BIT); + memset(mac, 0, 18); + sprintf(mac, "" MAC_STR "", MAC2STR(gateway_config->espnow_ota_data.mac_addr)); + // heap_caps_free(mac); xSemaphoreTake(gateway_config->espnow_ota_in_progress_mutex, portMAX_DELAY); zh_espnow_data_t data = {0}; data.device_type = ZHDT_GATEWAY; @@ -1020,6 +1076,10 @@ void zh_espnow_ota_update_task(void *pvParameter) memset(topic, 0, strlen(gateway_config->software_config.mqtt_topic_prefix) + strlen(zh_get_device_type_value_name(gateway_config->espnow_ota_data.device_type)) + 20); sprintf(topic, "%s/%s/" MAC_STR, gateway_config->software_config.mqtt_topic_prefix, zh_get_device_type_value_name(gateway_config->espnow_ota_data.device_type), MAC2STR(gateway_config->espnow_ota_data.mac_addr)); esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_begin", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + zh_syslog_send(ZH_USER, ZH_INFO, mac, zh_get_device_type_value_name(gateway_config->espnow_ota_data.device_type), "Firmware update begin."); + } char espnow_ota_write_data[sizeof(data.payload_data.ota_message.espnow_ota_message.data) + 1] = {0}; char *app_name = (char *)heap_caps_malloc(strlen(gateway_config->software_config.firmware_upgrade_url) + strlen(gateway_config->espnow_ota_data.app_name) + 6, MALLOC_CAP_8BIT); memset(app_name, 0, strlen(gateway_config->software_config.firmware_upgrade_url) + strlen(gateway_config->espnow_ota_data.app_name) + 6); @@ -1044,11 +1104,20 @@ void zh_espnow_ota_update_task(void *pvParameter) data.payload_type = ZHPT_UPDATE_ERROR; zh_send_message(gateway_config->espnow_ota_data.mac_addr, (uint8_t *)&data, sizeof(zh_espnow_data_t)); esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_error_begin_timeout", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + zh_syslog_send(ZH_USER, ZH_ERR, mac, zh_get_device_type_value_name(gateway_config->espnow_ota_data.device_type), "Firmware update error. Timeout exceed."); + } + heap_caps_free(mac); heap_caps_free(topic); xSemaphoreGive(gateway_config->espnow_ota_in_progress_mutex); vTaskDelete(NULL); } esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_progress", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + zh_syslog_send(ZH_USER, ZH_INFO, mac, zh_get_device_type_value_name(gateway_config->espnow_ota_data.device_type), "Firmware update progress."); + } for (;;) { int data_read_size = esp_http_client_read(https_client, espnow_ota_write_data, sizeof(data.payload_data.ota_message.espnow_ota_message.data)); @@ -1057,6 +1126,11 @@ void zh_espnow_ota_update_task(void *pvParameter) esp_http_client_close(https_client); esp_http_client_cleanup(https_client); esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_error_data_size", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + zh_syslog_send(ZH_USER, ZH_ERR, mac, zh_get_device_type_value_name(gateway_config->espnow_ota_data.device_type), "Firmware update error. Incorrect size of read data."); + } + heap_caps_free(mac); heap_caps_free(topic); xSemaphoreGive(gateway_config->espnow_ota_in_progress_mutex); vTaskDelete(NULL); @@ -1075,6 +1149,11 @@ void zh_espnow_ota_update_task(void *pvParameter) data.payload_type = ZHPT_UPDATE_ERROR; zh_send_message(gateway_config->espnow_ota_data.mac_addr, (uint8_t *)&data, sizeof(zh_espnow_data_t)); esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_error_progress_timeout", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + zh_syslog_send(ZH_USER, ZH_ERR, mac, zh_get_device_type_value_name(gateway_config->espnow_ota_data.device_type), "Firmware update error. Timeout exceed."); + } + heap_caps_free(mac); heap_caps_free(topic); xSemaphoreGive(gateway_config->espnow_ota_in_progress_mutex); vTaskDelete(NULL); @@ -1087,6 +1166,11 @@ void zh_espnow_ota_update_task(void *pvParameter) data.payload_type = ZHPT_UPDATE_END; zh_send_message(gateway_config->espnow_ota_data.mac_addr, (uint8_t *)&data, sizeof(zh_espnow_data_t)); esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "update_end", 0, 2, true); + if (gateway_config->syslog_is_enable == true) + { + zh_syslog_send(ZH_USER, ZH_INFO, mac, zh_get_device_type_value_name(gateway_config->espnow_ota_data.device_type), "Firmware update end."); + } + heap_caps_free(mac); heap_caps_free(topic); xSemaphoreGive(gateway_config->espnow_ota_in_progress_mutex); vTaskDelete(NULL); @@ -1101,6 +1185,14 @@ void zh_send_espnow_current_time_task(void *pvParameter) { vTaskDelay(5000 / portTICK_PERIOD_MS); } + if (gateway_config->syslog_is_enable == true) + { + char *mac = (char *)heap_caps_malloc(18, MALLOC_CAP_8BIT); + memset(mac, 0, 18); + sprintf(mac, "" MAC_STR "", MAC2STR(gateway_config->self_mac)); + zh_syslog_send(ZH_USER, ZH_INFO, mac, zh_get_device_type_value_name(ZHDT_GATEWAY), "Connected to NTP."); + heap_caps_free(mac); + } time_t now; setenv("TZ", gateway_config->software_config.ntp_time_zone, 1); tzset(); @@ -1139,7 +1231,7 @@ void zh_device_availability_check_task(void *pvParameter) char *mac = (char *)heap_caps_malloc(18, MALLOC_CAP_8BIT); memset(mac, 0, 18); sprintf(mac, "" MAC_STR "", MAC2STR(available_device->mac_addr)); - zh_syslog_send(ZH_USER, ZH_INFO, mac, zh_get_device_type_value_name(available_device->device_type), "Disconnected from gateway."); + zh_syslog_send(ZH_USER, ZH_WARNING, mac, zh_get_device_type_value_name(available_device->device_type), "Disconnected from gateway."); heap_caps_free(mac); } zh_vector_delete_item(&gateway_config->available_device_vector, i);