This commit is contained in:
2024-06-11 12:54:28 +03:00
parent 482903cecf
commit fecde83747
2 changed files with 110 additions and 59 deletions

View File

@ -8,7 +8,10 @@ void app_main(void)
nvs_flash_init(); nvs_flash_init();
esp_netif_init(); esp_netif_init();
esp_event_loop_create_default(); esp_event_loop_create_default();
#ifdef CONFIG_CONNECTION_TYPE_LAN zh_load_config(gateway_config);
zh_load_status(gateway_config);
if (gateway_config->software_config.is_lan_mode == true)
{
gpio_config_t config = {0}; gpio_config_t config = {0};
config.intr_type = GPIO_INTR_DISABLE; config.intr_type = GPIO_INTR_DISABLE;
config.mode = GPIO_MODE_OUTPUT; config.mode = GPIO_MODE_OUTPUT;
@ -37,16 +40,15 @@ void app_main(void)
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B); esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B);
esp_wifi_start(); esp_wifi_start();
esp_read_mac(gateway_config->self_mac, ESP_MAC_WIFI_STA); esp_read_mac(gateway_config->self_mac, ESP_MAC_WIFI_STA);
#else }
else
{
esp_netif_create_default_wifi_sta(); esp_netif_create_default_wifi_sta();
wifi_init_config_t wifi_init_sta_config = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t wifi_init_sta_config = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&wifi_init_sta_config); esp_wifi_init(&wifi_init_sta_config);
wifi_config_t wifi_config = { wifi_config_t wifi_config;
.sta = { strcpy(wifi_config.sta.ssid, gateway_config->software_config.ssid_name);
.ssid = CONFIG_WIFI_SSID_NAME, strcpy(wifi_config.sta.password, gateway_config->software_config.ssid_password);
.password = CONFIG_WIFI_PASSWORD,
},
};
esp_wifi_set_mode(WIFI_MODE_APSTA); esp_wifi_set_mode(WIFI_MODE_APSTA);
esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_11B); esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_11B);
esp_wifi_set_config(WIFI_IF_STA, &wifi_config); esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
@ -54,21 +56,23 @@ void app_main(void)
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &zh_wifi_event_handler, gateway_config, NULL); esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &zh_wifi_event_handler, gateway_config, NULL);
esp_wifi_start(); esp_wifi_start();
esp_read_mac(gateway_config->self_mac, ESP_MAC_WIFI_SOFTAP); esp_read_mac(gateway_config->self_mac, ESP_MAC_WIFI_SOFTAP);
#endif }
#ifdef CONFIG_NETWORK_TYPE_DIRECT #ifdef CONFIG_NETWORK_TYPE_DIRECT
zh_espnow_init_config_t zh_espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT(); zh_espnow_init_config_t zh_espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT();
zh_espnow_init_config.queue_size = 128; zh_espnow_init_config.queue_size = 128;
#ifdef CONFIG_CONNECTION_TYPE_WIFI if (gateway_config->software_config.is_lan_mode == false)
{
zh_espnow_init_config.wifi_interface = WIFI_IF_AP; zh_espnow_init_config.wifi_interface = WIFI_IF_AP;
#endif }
zh_espnow_init(&zh_espnow_init_config); zh_espnow_init(&zh_espnow_init_config);
esp_event_handler_instance_register(ZH_EVENT, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, gateway_config, NULL); esp_event_handler_instance_register(ZH_EVENT, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, gateway_config, NULL);
#else #else
zh_network_init_config_t zh_network_init_config = ZH_NETWORK_INIT_CONFIG_DEFAULT(); zh_network_init_config_t zh_network_init_config = ZH_NETWORK_INIT_CONFIG_DEFAULT();
zh_network_init_config.queue_size = 128; zh_network_init_config.queue_size = 128;
#ifdef CONFIG_CONNECTION_TYPE_WIFI if (gateway_config->software_config.is_lan_mode == false)
{
zh_network_init_config.wifi_interface = WIFI_IF_AP; zh_network_init_config.wifi_interface = WIFI_IF_AP;
#endif }
zh_network_init(&zh_network_init_config); zh_network_init(&zh_network_init_config);
esp_event_handler_instance_register(ZH_EVENT, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, gateway_config, NULL); esp_event_handler_instance_register(ZH_EVENT, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, gateway_config, NULL);
#endif #endif
@ -87,7 +91,33 @@ void app_main(void)
} }
} }
void zh_load_config(gateway_config_t *gateway_config)
{
nvs_handle_t nvs_handle = 0;
nvs_open("config", NVS_READWRITE, &nvs_handle);
uint8_t config_is_present = 0;
if (nvs_get_u8(nvs_handle, "present", &config_is_present) == ESP_ERR_NVS_NOT_FOUND)
{
nvs_set_u8(nvs_handle, "present", 0xFE);
nvs_close(nvs_handle);
#ifdef CONFIG_CONNECTION_TYPE_LAN #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");
#else
gateway_config->software_config.is_lan_mode = false;
strcpy(gateway_config->software_config.ssid_name, CONFIG_WIFI_SSID_NAME);
strcpy(gateway_config->software_config.ssid_password, CONFIG_WIFI_PASSWORD);
#endif
zh_save_config(gateway_config);
return;
}
nvs_get_u8(nvs_handle, "lan_mode", (uint8_t *)&gateway_config->software_config.is_lan_mode);
nvs_get_str(nvs_handle, "ssid_name", &gateway_config->software_config.ssid_name, 0);
nvs_get_str(nvs_handle, "ssid_password", &gateway_config->software_config.ssid_password, 0);
nvs_close(nvs_handle);
}
void zh_eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) void zh_eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{ {
gateway_config_t *gateway_config = arg; gateway_config_t *gateway_config = arg;
@ -132,9 +162,7 @@ void zh_eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_
break; break;
} }
} }
#endif
#ifdef CONFIG_CONNECTION_TYPE_WIFI
void zh_wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) void zh_wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{ {
gateway_config_t *gateway_config = arg; gateway_config_t *gateway_config = arg;
@ -196,7 +224,6 @@ void zh_wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event
break; break;
} }
} }
#endif
void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{ {

View File

@ -63,6 +63,17 @@
typedef struct // Structure of data exchange between tasks, functions and event handlers. typedef struct // Structure of data exchange between tasks, functions and event handlers.
{ {
struct
{
bool is_lan_mode; // Ethernet work mode flag.
char ssid_name[32]; // WiFi SSID name.
char ssid_password[64]; // WiFi password.
char mqtt_broker_url[64]; // MQTT broker url.
char mqtt_topic_prefix[32]; // MQTT topic prefix.
char ntp_server_url[64]; // NTP server url.
char ntp_time_zone[12]; // NTP time zone.
char firmware_upgrade_url[64]; // Firmware upgrade url.
} software_config;
uint8_t self_mac[6]; // Gateway MAC address. @note Depends at WiFi operation mode. uint8_t self_mac[6]; // Gateway MAC address. @note Depends at WiFi operation mode.
bool sntp_is_enable; // SNTP client operation status flag. @note Used to control the SNTP functions when the network connection is established / lost. bool sntp_is_enable; // SNTP client operation status flag. @note Used to control the SNTP functions when the network connection is established / lost.
bool mqtt_is_enable; // MQTT client operation status flag. @note Used to control the MQTT functions when the network connection is established / lost. bool mqtt_is_enable; // MQTT client operation status flag. @note Used to control the MQTT functions when the network connection is established / lost.
@ -99,21 +110,34 @@ typedef struct // Struct for storing data about available nodes.
extern const uint8_t server_certificate_pem_start[] asm("_binary_certificate_pem_start"); extern const uint8_t server_certificate_pem_start[] asm("_binary_certificate_pem_start");
extern const uint8_t server_certificate_pem_end[] asm("_binary_certificate_pem_end"); extern const uint8_t server_certificate_pem_end[] asm("_binary_certificate_pem_end");
#ifdef CONFIG_CONNECTION_TYPE_LAN /**
* @brief Function for loading the gateway configuration from NVS memory.
*
* @param[out] switch_config Pointer to structure of data exchange between tasks, functions and event handlers.
*/
void zh_load_config(gateway_config_t *gateway_config);
/**
* @brief Function for saving the gateway configuration to NVS memory.
*
* @param[in] switch_config Pointer to structure of data exchange between tasks, functions and event handlers.
*/
void zh_save_config(const gateway_config_t *gateway_config);
/** /**
* @brief Function for LAN event processing. * @brief Function for LAN event processing.
* *
* @param[in,out] arg Pointer to the structure of data exchange between tasks, functions and event handlers. * @param[in,out] arg Pointer to the structure of data exchange between tasks, functions and event handlers.
*/ */
void zh_eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); void zh_eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
#else
/** /**
* @brief Function for WiFi event processing. * @brief Function for WiFi event processing.
* *
* @param[in,out] arg Pointer to the structure of data exchange between tasks, functions and event handlers. * @param[in,out] arg Pointer to the structure of data exchange between tasks, functions and event handlers.
*/ */
void zh_wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); void zh_wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
#endif
/** /**
* @brief Function for ESP-NOW event processing * @brief Function for ESP-NOW event processing
* *