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,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)
{