Some initial mandatory settings converted to optional settings

This commit is contained in:
2024-07-15 22:28:43 +03:00
parent 2f66f01781
commit 243f0ae4ff
4 changed files with 179 additions and 51 deletions

View File

@ -99,10 +99,11 @@ void zh_load_config(gateway_config_t *gateway_config)
{
nvs_set_u8(nvs_handle, "present", 0xFE);
nvs_close(nvs_handle);
SETUP_INITIAL_SETTINGS:
#ifdef CONFIG_CONNECTION_TYPE_LAN
gateway_config->software_config.is_lan_mode = true;
strcpy(gateway_config->software_config.ssid_name, "ssid");
strcpy(gateway_config->software_config.ssid_password, "password");
strcpy(gateway_config->software_config.ssid_name, "NULL");
strcpy(gateway_config->software_config.ssid_password, "NULL");
#else
gateway_config->software_config.is_lan_mode = false;
strcpy(gateway_config->software_config.ssid_name, CONFIG_WIFI_SSID_NAME);
@ -110,29 +111,62 @@ void zh_load_config(gateway_config_t *gateway_config)
#endif
strcpy(gateway_config->software_config.mqtt_broker_url, CONFIG_MQTT_BROKER_URL);
strcpy(gateway_config->software_config.mqtt_topic_prefix, CONFIG_MQTT_TOPIC_PREFIX);
#ifdef CONFIG_SYSLOG_SERVER_USING
gateway_config->software_config.is_syslog_server_usage = true;
strcpy(gateway_config->software_config.syslog_server_ip, CONFIG_SYSLOG_SERVER_IP);
gateway_config->software_config.syslog_server_port = CONFIG_SYSLOG_SERVER_PORT;
#else
gateway_config->software_config.is_syslog_server_usage = false;
strcpy(gateway_config->software_config.syslog_server_ip, "NULL");
gateway_config->software_config.syslog_server_port = 0;
#endif
#ifdef CONFIG_NTP_SERVER_USING
gateway_config->software_config.is_ntp_server_usage = true;
strcpy(gateway_config->software_config.ntp_server_url, CONFIG_NTP_SERVER_URL);
strcpy(gateway_config->software_config.ntp_time_zone, CONFIG_NTP_TIME_ZONE);
#else
gateway_config->software_config.is_ntp_server_usage = false;
strcpy(gateway_config->software_config.ntp_server_url, "NULL");
strcpy(gateway_config->software_config.ntp_time_zone, "NULL");
#endif
#ifdef CONFIG_FIRMWARE_UPGRADE_SERVER_USING
gateway_config->software_config.is_ota_server_usage = true;
strcpy(gateway_config->software_config.firmware_upgrade_url, CONFIG_FIRMWARE_UPGRADE_URL);
#else
gateway_config->software_config.is_ota_server_usage = false;
strcpy(gateway_config->software_config.firmware_upgrade_url, "NULL");
#endif
zh_save_config(gateway_config);
return;
}
nvs_get_u8(nvs_handle, "lan_mode", (uint8_t *)&gateway_config->software_config.is_lan_mode);
esp_err_t err = ESP_OK;
size_t size = 0;
nvs_get_str(nvs_handle, "ssid_name", NULL, &size);
nvs_get_str(nvs_handle, "ssid_name", gateway_config->software_config.ssid_name, &size);
nvs_get_str(nvs_handle, "ssid_password", NULL, &size);
nvs_get_str(nvs_handle, "ssid_password", gateway_config->software_config.ssid_password, &size);
nvs_get_str(nvs_handle, "mqtt_url", NULL, &size);
nvs_get_str(nvs_handle, "mqtt_url", gateway_config->software_config.mqtt_broker_url, &size);
nvs_get_str(nvs_handle, "topic_prefix", NULL, &size);
nvs_get_str(nvs_handle, "topic_prefix", gateway_config->software_config.mqtt_topic_prefix, &size);
nvs_get_str(nvs_handle, "ntp_url", NULL, &size);
nvs_get_str(nvs_handle, "ntp_url", gateway_config->software_config.ntp_server_url, &size);
nvs_get_str(nvs_handle, "time_zone", NULL, &size);
nvs_get_str(nvs_handle, "time_zone", gateway_config->software_config.ntp_time_zone, &size);
nvs_get_str(nvs_handle, "upgrade_url", NULL, &size);
nvs_get_str(nvs_handle, "upgrade_url", gateway_config->software_config.firmware_upgrade_url, &size);
err += nvs_get_u8(nvs_handle, "lan_mode", (uint8_t *)&gateway_config->software_config.is_lan_mode);
err += nvs_get_str(nvs_handle, "ssid_name", NULL, &size);
err += nvs_get_str(nvs_handle, "ssid_name", gateway_config->software_config.ssid_name, &size);
err += nvs_get_str(nvs_handle, "ssid_password", NULL, &size);
err += nvs_get_str(nvs_handle, "ssid_password", gateway_config->software_config.ssid_password, &size);
err += nvs_get_str(nvs_handle, "mqtt_url", NULL, &size);
err += nvs_get_str(nvs_handle, "mqtt_url", gateway_config->software_config.mqtt_broker_url, &size);
err += nvs_get_str(nvs_handle, "topic_prefix", NULL, &size);
err += nvs_get_str(nvs_handle, "topic_prefix", gateway_config->software_config.mqtt_topic_prefix, &size);
err += nvs_get_u8(nvs_handle, "syslog_usage", (uint8_t *)&gateway_config->software_config.is_syslog_server_usage);
err += nvs_get_str(nvs_handle, "syslog_ip", NULL, &size);
err += nvs_get_str(nvs_handle, "syslog_ip", gateway_config->software_config.syslog_server_ip, &size);
err += nvs_get_u32(nvs_handle, "syslog_port", &gateway_config->software_config.syslog_server_port);
err += nvs_get_u8(nvs_handle, "ntp_usage", (uint8_t *)&gateway_config->software_config.is_ntp_server_usage);
err += nvs_get_str(nvs_handle, "ntp_url", NULL, &size);
err += nvs_get_str(nvs_handle, "ntp_url", gateway_config->software_config.ntp_server_url, &size);
err += nvs_get_str(nvs_handle, "time_zone", NULL, &size);
err += nvs_get_str(nvs_handle, "time_zone", gateway_config->software_config.ntp_time_zone, &size);
err += nvs_get_u8(nvs_handle, "ota_usage", (uint8_t *)&gateway_config->software_config.is_ota_server_usage);
err += nvs_get_str(nvs_handle, "upgrade_url", NULL, &size);
err += nvs_get_str(nvs_handle, "upgrade_url", gateway_config->software_config.firmware_upgrade_url, &size);
nvs_close(nvs_handle);
if (err != ESP_OK)
{
goto SETUP_INITIAL_SETTINGS;
}
}
void zh_save_config(const gateway_config_t *gateway_config)
@ -144,8 +178,13 @@ void zh_save_config(const gateway_config_t *gateway_config)
nvs_set_str(nvs_handle, "ssid_password", gateway_config->software_config.ssid_password);
nvs_set_str(nvs_handle, "mqtt_url", gateway_config->software_config.mqtt_broker_url);
nvs_set_str(nvs_handle, "topic_prefix", gateway_config->software_config.mqtt_topic_prefix);
nvs_set_u8(nvs_handle, "syslog_usage", gateway_config->software_config.is_syslog_server_usage);
nvs_set_str(nvs_handle, "syslog_ip", gateway_config->software_config.syslog_server_ip);
nvs_set_u32(nvs_handle, "syslog_port", gateway_config->software_config.syslog_server_port);
nvs_set_u8(nvs_handle, "ntp_usage", gateway_config->software_config.is_ntp_server_usage);
nvs_set_str(nvs_handle, "ntp_url", gateway_config->software_config.ntp_server_url);
nvs_set_str(nvs_handle, "time_zone", gateway_config->software_config.ntp_time_zone);
nvs_set_u8(nvs_handle, "ota_usage", gateway_config->software_config.is_ota_server_usage);
nvs_set_str(nvs_handle, "upgrade_url", gateway_config->software_config.firmware_upgrade_url);
nvs_close(nvs_handle);
}
@ -161,11 +200,22 @@ void zh_eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_
esp_mqtt_client_stop(gateway_config->mqtt_client);
gateway_config->mqtt_is_enable = false;
}
if (gateway_config->sntp_is_enable == true)
if (gateway_config->software_config.is_ntp_server_usage == true)
{
esp_sntp_stop();
gateway_config->sntp_is_enable = false;
vTaskDelete(gateway_config->gateway_current_time_task);
if (gateway_config->sntp_is_enable == true)
{
esp_sntp_stop();
gateway_config->sntp_is_enable = false;
vTaskDelete(gateway_config->gateway_current_time_task);
}
}
if (gateway_config->software_config.is_syslog_server_usage == true)
{
if (gateway_config->syslog_is_enable == true)
{
zh_syslog_deinit();
gateway_config->syslog_is_enable = false;
}
}
break;
case IP_EVENT_ETH_GOT_IP:
@ -179,14 +229,25 @@ void zh_eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_
esp_mqtt_client_start(gateway_config->mqtt_client);
gateway_config->mqtt_is_enable = true;
}
if (gateway_config->sntp_is_enable == false)
if (gateway_config->software_config.is_ntp_server_usage == true)
{
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
esp_sntp_setservername(0, gateway_config->software_config.ntp_server_url);
esp_sntp_init();
gateway_config->sntp_is_enable = true;
xTaskCreatePinnedToCore(&zh_send_espnow_current_time_task, "NULL", ZH_SNTP_STACK_SIZE, gateway_config, ZH_SNTP_TASK_PRIORITY, (TaskHandle_t *)&gateway_config->gateway_current_time_task, tskNO_AFFINITY);
if (gateway_config->sntp_is_enable == false)
{
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
esp_sntp_setservername(0, gateway_config->software_config.ntp_server_url);
esp_sntp_init();
gateway_config->sntp_is_enable = true;
xTaskCreatePinnedToCore(&zh_send_espnow_current_time_task, "NULL", ZH_SNTP_STACK_SIZE, gateway_config, ZH_SNTP_TASK_PRIORITY, (TaskHandle_t *)&gateway_config->gateway_current_time_task, tskNO_AFFINITY);
}
}
if (gateway_config->software_config.is_syslog_server_usage == true)
{
zh_syslog_init_config_t syslog_init_config = ZH_SYSLOG_INIT_CONFIG_DEFAULT();
memcpy(syslog_init_config.syslog_ip, gateway_config->software_config.syslog_server_ip, strlen(gateway_config->software_config.syslog_server_ip));
syslog_init_config.syslog_port = gateway_config->software_config.syslog_server_port;
zh_syslog_init(&syslog_init_config);
gateway_config->syslog_is_enable = true;
}
break;
default:
@ -208,11 +269,22 @@ void zh_wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event
esp_mqtt_client_stop(gateway_config->mqtt_client);
gateway_config->mqtt_is_enable = false;
}
if (gateway_config->sntp_is_enable == true)
if (gateway_config->software_config.is_ntp_server_usage == true)
{
esp_sntp_stop();
gateway_config->sntp_is_enable = false;
vTaskDelete(gateway_config->gateway_current_time_task);
if (gateway_config->sntp_is_enable == true)
{
esp_sntp_stop();
gateway_config->sntp_is_enable = false;
vTaskDelete(gateway_config->gateway_current_time_task);
}
}
if (gateway_config->software_config.is_syslog_server_usage == true)
{
if (gateway_config->syslog_is_enable == true)
{
zh_syslog_deinit();
gateway_config->syslog_is_enable = false;
}
}
if (gateway_config->wifi_reconnect_retry_num < ZH_WIFI_MAXIMUM_RETRY)
{
@ -240,14 +312,25 @@ void zh_wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event
esp_mqtt_client_start(gateway_config->mqtt_client);
gateway_config->mqtt_is_enable = true;
}
if (gateway_config->sntp_is_enable == false)
if (gateway_config->software_config.is_ntp_server_usage == true)
{
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
esp_sntp_setservername(0, gateway_config->software_config.ntp_server_url);
esp_sntp_init();
gateway_config->sntp_is_enable = true;
xTaskCreatePinnedToCore(&zh_send_espnow_current_time_task, "NULL", ZH_SNTP_STACK_SIZE, gateway_config, ZH_SNTP_TASK_PRIORITY, (TaskHandle_t *)&gateway_config->gateway_current_time_task, tskNO_AFFINITY);
if (gateway_config->sntp_is_enable == false)
{
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
esp_sntp_setservername(0, gateway_config->software_config.ntp_server_url);
esp_sntp_init();
gateway_config->sntp_is_enable = true;
xTaskCreatePinnedToCore(&zh_send_espnow_current_time_task, "NULL", ZH_SNTP_STACK_SIZE, gateway_config, ZH_SNTP_TASK_PRIORITY, (TaskHandle_t *)&gateway_config->gateway_current_time_task, tskNO_AFFINITY);
}
}
if (gateway_config->software_config.is_syslog_server_usage == true)
{
zh_syslog_init_config_t syslog_init_config = ZH_SYSLOG_INIT_CONFIG_DEFAULT();
memcpy(syslog_init_config.syslog_ip, gateway_config->software_config.syslog_server_ip, strlen(gateway_config->software_config.syslog_server_ip));
syslog_init_config.syslog_port = gateway_config->software_config.syslog_server_port;
zh_syslog_init(&syslog_init_config);
gateway_config->syslog_is_enable = true;
}
break;
default:
@ -500,7 +583,7 @@ void zh_mqtt_event_handler(void *arg, esp_event_base_t event_base, int32_t event
{
if (memcmp(gateway_config->self_mac, incoming_data_mac, 6) == 0)
{
if (strncmp(incoming_payload, "update", strlen(incoming_payload) + 1) == 0)
if (strncmp(incoming_payload, "update", strlen(incoming_payload) + 1) == 0 && gateway_config->software_config.is_ota_server_usage == true)
{
if (xSemaphoreTake(gateway_config->self_ota_in_progress_mutex, portTICK_PERIOD_MS) == pdTRUE)
{
@ -529,7 +612,7 @@ void zh_mqtt_event_handler(void *arg, esp_event_base_t event_base, int32_t event
switch (incoming_data_payload_type)
{
case ZHPT_NONE:
if (strncmp(incoming_payload, "update", strlen(incoming_payload) + 1) == 0)
if (strncmp(incoming_payload, "update", strlen(incoming_payload) + 1) == 0 && gateway_config->software_config.is_ota_server_usage == true)
{
data.payload_type = ZHPT_UPDATE;
zh_send_message(incoming_data_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
@ -628,7 +711,7 @@ void zh_mqtt_event_handler(void *arg, esp_event_base_t event_base, int32_t event
switch (incoming_data_payload_type)
{
case ZHPT_NONE:
if (strncmp(incoming_payload, "update", strlen(incoming_payload) + 1) == 0)
if (strncmp(incoming_payload, "update", strlen(incoming_payload) + 1) == 0 && gateway_config->software_config.is_ota_server_usage == true)
{
data.payload_type = ZHPT_UPDATE;
zh_send_message(incoming_data_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
@ -735,7 +818,7 @@ void zh_mqtt_event_handler(void *arg, esp_event_base_t event_base, int32_t event
switch (incoming_data_payload_type)
{
case ZHPT_NONE:
if (strncmp(incoming_payload, "update", strlen(incoming_payload) + 1) == 0)
if (strncmp(incoming_payload, "update", strlen(incoming_payload) + 1) == 0 && gateway_config->software_config.is_ota_server_usage == true)
{
data.payload_type = ZHPT_UPDATE;
zh_send_message(incoming_data_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
@ -798,7 +881,7 @@ void zh_mqtt_event_handler(void *arg, esp_event_base_t event_base, int32_t event
switch (incoming_data_payload_type)
{
case ZHPT_NONE:
if (strncmp(incoming_payload, "update", strlen(incoming_payload) + 1) == 0)
if (strncmp(incoming_payload, "update", strlen(incoming_payload) + 1) == 0 && gateway_config->software_config.is_ota_server_usage == true)
{
data.payload_type = ZHPT_UPDATE;
zh_send_message(incoming_data_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));