diff --git a/main/zh_gateway.c b/main/zh_gateway.c index 2ffb53f..465cecd 100755 --- a/main/zh_gateway.c +++ b/main/zh_gateway.c @@ -8,67 +8,71 @@ void app_main(void) nvs_flash_init(); esp_netif_init(); esp_event_loop_create_default(); -#ifdef CONFIG_CONNECTION_TYPE_LAN - gpio_config_t config = {0}; - config.intr_type = GPIO_INTR_DISABLE; - config.mode = GPIO_MODE_OUTPUT; - config.pin_bit_mask = (1ULL << ZH_LAN_MODULE_POWER_PIN); - config.pull_down_en = GPIO_PULLDOWN_DISABLE; - config.pull_up_en = GPIO_PULLUP_DISABLE; - gpio_config(&config); - gpio_set_level(ZH_LAN_MODULE_POWER_PIN, 1); - esp_netif_config_t esp_netif_config = ESP_NETIF_DEFAULT_ETH(); - esp_netif_t *esp_netif_eth = esp_netif_new(&esp_netif_config); - eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); - eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); - eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); - esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); - esp_eth_phy_t *phy = ZH_LAN_MODULE_TYPE(&phy_config); - esp_eth_config_t esp_eth_config = ETH_DEFAULT_CONFIG(mac, phy); - esp_eth_handle_t esp_eth_handle = NULL; - esp_eth_driver_install(&esp_eth_config, &esp_eth_handle); - esp_netif_attach(esp_netif_eth, esp_eth_new_netif_glue(esp_eth_handle)); - esp_event_handler_instance_register(ETH_EVENT, ESP_EVENT_ANY_ID, &zh_eth_event_handler, gateway_config, NULL); - esp_event_handler_instance_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &zh_eth_event_handler, gateway_config, NULL); - esp_eth_start(esp_eth_handle); - wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); - esp_wifi_init(&wifi_init_config); - esp_wifi_set_mode(WIFI_MODE_STA); - esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B); - esp_wifi_start(); - esp_read_mac(gateway_config->self_mac, ESP_MAC_WIFI_STA); -#else - esp_netif_create_default_wifi_sta(); - wifi_init_config_t wifi_init_sta_config = WIFI_INIT_CONFIG_DEFAULT(); - esp_wifi_init(&wifi_init_sta_config); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID_NAME, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - esp_wifi_set_mode(WIFI_MODE_APSTA); - esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_11B); - esp_wifi_set_config(WIFI_IF_STA, &wifi_config); - esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &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_read_mac(gateway_config->self_mac, ESP_MAC_WIFI_SOFTAP); -#endif + zh_load_config(gateway_config); + zh_load_status(gateway_config); + if (gateway_config->software_config.is_lan_mode == true) + { + gpio_config_t config = {0}; + config.intr_type = GPIO_INTR_DISABLE; + config.mode = GPIO_MODE_OUTPUT; + config.pin_bit_mask = (1ULL << ZH_LAN_MODULE_POWER_PIN); + config.pull_down_en = GPIO_PULLDOWN_DISABLE; + config.pull_up_en = GPIO_PULLUP_DISABLE; + gpio_config(&config); + gpio_set_level(ZH_LAN_MODULE_POWER_PIN, 1); + esp_netif_config_t esp_netif_config = ESP_NETIF_DEFAULT_ETH(); + esp_netif_t *esp_netif_eth = esp_netif_new(&esp_netif_config); + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); + eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); + esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); + esp_eth_phy_t *phy = ZH_LAN_MODULE_TYPE(&phy_config); + esp_eth_config_t esp_eth_config = ETH_DEFAULT_CONFIG(mac, phy); + esp_eth_handle_t esp_eth_handle = NULL; + esp_eth_driver_install(&esp_eth_config, &esp_eth_handle); + esp_netif_attach(esp_netif_eth, esp_eth_new_netif_glue(esp_eth_handle)); + esp_event_handler_instance_register(ETH_EVENT, ESP_EVENT_ANY_ID, &zh_eth_event_handler, gateway_config, NULL); + esp_event_handler_instance_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &zh_eth_event_handler, gateway_config, NULL); + esp_eth_start(esp_eth_handle); + wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); + esp_wifi_init(&wifi_init_config); + esp_wifi_set_mode(WIFI_MODE_STA); + esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B); + esp_wifi_start(); + esp_read_mac(gateway_config->self_mac, ESP_MAC_WIFI_STA); + } + else + { + esp_netif_create_default_wifi_sta(); + wifi_init_config_t wifi_init_sta_config = WIFI_INIT_CONFIG_DEFAULT(); + esp_wifi_init(&wifi_init_sta_config); + wifi_config_t wifi_config; + strcpy(wifi_config.sta.ssid, gateway_config->software_config.ssid_name); + strcpy(wifi_config.sta.password, gateway_config->software_config.ssid_password); + esp_wifi_set_mode(WIFI_MODE_APSTA); + esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_11B); + esp_wifi_set_config(WIFI_IF_STA, &wifi_config); + esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &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_read_mac(gateway_config->self_mac, ESP_MAC_WIFI_SOFTAP); + } #ifdef CONFIG_NETWORK_TYPE_DIRECT zh_espnow_init_config_t zh_espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT(); zh_espnow_init_config.queue_size = 128; -#ifdef CONFIG_CONNECTION_TYPE_WIFI - zh_espnow_init_config.wifi_interface = WIFI_IF_AP; -#endif + if (gateway_config->software_config.is_lan_mode == false) + { + zh_espnow_init_config.wifi_interface = WIFI_IF_AP; + } 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); #else zh_network_init_config_t zh_network_init_config = ZH_NETWORK_INIT_CONFIG_DEFAULT(); zh_network_init_config.queue_size = 128; -#ifdef CONFIG_CONNECTION_TYPE_WIFI - zh_network_init_config.wifi_interface = WIFI_IF_AP; -#endif + if (gateway_config->software_config.is_lan_mode == false) + { + zh_network_init_config.wifi_interface = WIFI_IF_AP; + } 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); #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 + 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) { 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; } } -#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) { 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; } } -#endif void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { diff --git a/main/zh_gateway.h b/main/zh_gateway.h index 8f378a2..5a07f8b 100755 --- a/main/zh_gateway.h +++ b/main/zh_gateway.h @@ -63,6 +63,17 @@ 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. 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. @@ -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_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. * * @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); -#else + /** * @brief Function for WiFi event processing. * * @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); -#endif + /** * @brief Function for ESP-NOW event processing *