mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-08 20:47:08 +03:00
1307 lines
60 KiB
C
1307 lines
60 KiB
C
#ifdef NETWORK_HANDLERS_LOG_LEVEL
|
|
#define LOG_LOCAL_LEVEL NETWORK_HANDLERS_LOG_LEVEL
|
|
#endif
|
|
#include "network_ethernet.h"
|
|
#include "network_manager.h"
|
|
#include "network_status.h"
|
|
#include "network_wifi.h"
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "dns_server.h"
|
|
#include "esp_log.h"
|
|
#include "esp_system.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "platform_esp32.h"
|
|
|
|
#include "esp_netif.h"
|
|
#include "freertos/task.h"
|
|
|
|
#include "cJSON.h"
|
|
#include "cmd_system.h"
|
|
#include "esp_app_format.h"
|
|
|
|
#include "esp_event.h"
|
|
#include "esp_ota_ops.h"
|
|
#include "esp_wifi.h"
|
|
#include "esp_wifi_types.h"
|
|
#include "lwip/api.h"
|
|
#include "lwip/err.h"
|
|
#include "lwip/ip4_addr.h"
|
|
#include "lwip/netdb.h"
|
|
#include "mdns.h"
|
|
#include "messaging.h"
|
|
|
|
#include "Config.h"
|
|
|
|
#include "trace.h"
|
|
|
|
#include "accessors.h"
|
|
#include "bootstate.h"
|
|
#include "esp_err.h"
|
|
#include "http_server_handlers.h"
|
|
#include "network_manager.h"
|
|
#include "tools.h"
|
|
|
|
static const char TAG[] = "network_handlers";
|
|
|
|
EXT_RAM_ATTR static state_t network_states[TOTAL_NM_STATE];
|
|
EXT_RAM_ATTR static state_t Wifi_Active_State[TOTAL_WIFI_ACTIVE_STATE];
|
|
EXT_RAM_ATTR static state_t Eth_Active_State[TOTAL_ETH_ACTIVE_STATE];
|
|
EXT_RAM_ATTR static state_t Wifi_Configuring_State[TOTAL_WIFI_CONFIGURING_STATE];
|
|
static void network_interface_coexistence(state_machine_t* state_machine);
|
|
static state_machine_result_t local_traverse_state(state_machine_t* const state_machine, const state_t* const target_state, const char* caller);
|
|
static state_machine_result_t local_switch_state(state_machine_t* state_machine, const state_t* const target_state, const char* caller);
|
|
void network_execute_cb(state_machine_t* const state_machine, const char* caller);
|
|
int get_root_id(const state_t* state);
|
|
const state_t* get_root(const state_t* const state);
|
|
|
|
#define ADD_ROOT(name, ...) ADD_ROOT_FORWARD_DECLARATION(name, ...)
|
|
#define ADD_ROOT_LEAF(name, ...) ADD_ROOT_LEAF_FORWARD_DECLARATION(name, ...)
|
|
#define ADD_LEAF(name, ...) ADD_LEAF_FORWARD_DECLARATION(name, ...)
|
|
ALL_NM_STATE
|
|
ALL_ETH_STATE(, )
|
|
ALL_WIFI_STATE(, )
|
|
ALL_WIFI_CONFIGURING_STATE(, )
|
|
#undef ADD_ROOT
|
|
#undef ADD_ROOT_LEAF
|
|
#undef ADD_LEAF
|
|
|
|
/*
|
|
* --------------------- Global variables ---------------------
|
|
*/
|
|
#define ADD_ROOT(NAME, CHILD) ADD_ROOT_(NAME, CHILD)
|
|
#define ADD_LEAF(NAME, PARENT, LEVEL) ADD_LEAF_(NAME, PARENT, LEVEL)
|
|
#define ADD_ROOT_LEAF(NAME) ADD_ROOT_LEAF_(NAME)
|
|
|
|
#define ADD_ROOT_(NAME, CHILD) \
|
|
[NAME] = { \
|
|
.Handler = NAME##_handler, \
|
|
.Entry = NAME##_entry_handler, \
|
|
.Exit = NAME##_exit_handler, \
|
|
.Level = 0, \
|
|
.Parent = NULL, \
|
|
.Id = NAME, \
|
|
.Node = CHILD, \
|
|
},
|
|
|
|
#define ADD_ROOT_LEAF_(NAME) \
|
|
[NAME] = { \
|
|
.Handler = NAME##_handler, \
|
|
.Entry = NAME##_entry_handler, \
|
|
.Exit = NAME##_exit_handler, \
|
|
.Id = NAME, \
|
|
.Parent = NULL, \
|
|
},
|
|
|
|
#define ADD_LEAF_(NAME, PARENT, LEVEL) \
|
|
[NAME] = { \
|
|
.Handler = NAME##_handler, \
|
|
.Entry = NAME##_entry_handler, \
|
|
.Exit = NAME##_exit_handler, \
|
|
.Id = NAME, \
|
|
.Level = LEVEL, \
|
|
.Parent = PARENT, \
|
|
},
|
|
|
|
void network_initialize_state_machine_globals() {
|
|
static state_t loc_network_states[] = {ALL_NM_STATE};
|
|
|
|
static state_t loc_Wifi_Active_State[] = {ALL_WIFI_STATE(&network_states[NETWORK_WIFI_ACTIVE_STATE], 1)};
|
|
static state_t loc_Eth_Active_State[] = {ALL_ETH_STATE(&network_states[NETWORK_ETH_ACTIVE_STATE], 1)
|
|
|
|
};
|
|
static state_t loc_Wifi_Configuring_State[] = {ALL_WIFI_CONFIGURING_STATE(&network_states[NETWORK_WIFI_CONFIGURING_ACTIVE_STATE], 1)};
|
|
memcpy(&network_states, &loc_network_states, sizeof(network_states));
|
|
memcpy(&Eth_Active_State, &loc_Eth_Active_State, sizeof(Eth_Active_State));
|
|
memcpy(&Wifi_Active_State, &loc_Wifi_Active_State, sizeof(Wifi_Active_State));
|
|
memcpy(&Wifi_Configuring_State, &loc_Wifi_Configuring_State, sizeof(Wifi_Configuring_State));
|
|
}
|
|
#undef ADD_ROOT
|
|
#undef ADD_ROOT_LEAF
|
|
#undef ADD_LEAF
|
|
static void network_handle_got_ip(network_event_t trigger, ip_event_got_ip_t* event_data) {
|
|
esp_netif_ip_info_t* ip_info = NULL;
|
|
ip_info = &event_data->ip_info;
|
|
ESP_LOGI(TAG, "Got an IP address from interface %s. IP=" IPSTR ", Gateway=" IPSTR ", NetMask=" IPSTR ", %s",
|
|
trigger == EN_ETH_GOT_IP ? "Eth" : "Wifi", IP2STR(&ip_info->ip), IP2STR(&ip_info->gw), IP2STR(&ip_info->netmask),
|
|
event_data->ip_changed ? "Address was changed" : "Address unchanged");
|
|
}
|
|
#define HANDLE_GLOBAL_EVENT(m) \
|
|
if (handle_global_event(m) == EVENT_HANDLED) return EVENT_HANDLED;
|
|
|
|
static void network_connect_active_ssid(state_machine_t* const State_Machine) {
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
esp_err_t err = network_wifi_connect_active_ssid();
|
|
if (err != ESP_OK) {
|
|
nm->wifi_connected = false;
|
|
|
|
if (err == ESP_ERR_NOT_FOUND) {
|
|
network_async_fail();
|
|
} else {
|
|
ESP_LOGE(TAG, "Oups. Something went wrong while trying to connect to the active ssid!");
|
|
ESP_LOGD(TAG, "Checking if ethernet interface is connected");
|
|
if (network_is_interface_connected(nm->eth_netif)) {
|
|
ESP_LOGD(TAG, "Ethernet connection is found. Try to fallback there");
|
|
network_async(EN_ETHERNET_FALLBACK);
|
|
} else {
|
|
// returning to AP mode
|
|
nm->STA_duration = nm->sta_polling_min_ms;
|
|
ESP_LOGD(TAG, "No ethernet and no wifi configured. Go to configuration mode");
|
|
network_async_configure();
|
|
}
|
|
}
|
|
} else {
|
|
nm->STA_duration = nm->sta_polling_min_ms;
|
|
/* create timer for background STA connection */
|
|
network_set_timer(nm->STA_duration, "Wifi Polling timeout");
|
|
}
|
|
}
|
|
|
|
void initialize_network_handlers(state_machine_t* state_machine) { NETWORK_INSTANTIATED_STATE_handler(state_machine); }
|
|
static state_machine_result_t handle_global_event(state_machine_t* state_machine) {
|
|
network_t* net_sm = ((network_t*)state_machine);
|
|
esp_err_t err = ESP_OK;
|
|
state_machine_result_t result = EVENT_UN_HANDLED;
|
|
ESP_LOGD(TAG, "Processing event %s", network_event_to_string(net_sm->Machine.Event));
|
|
switch (net_sm->Machine.Event) {
|
|
case EN_UPDATE_STATUS:
|
|
// handle the event, but don't swicth
|
|
// todo: fix this
|
|
// network_status_update_basic_info();
|
|
return EVENT_HANDLED;
|
|
/* code */
|
|
break;
|
|
case EN_REMOVE:
|
|
if (net_sm->event_parameters->ctx.credentials.ssid && strlen(net_sm->event_parameters->ctx.credentials.ssid) > 0) {
|
|
ESP_LOGD(TAG, "Deleting ssid credentials for %s", net_sm->event_parameters->ctx.credentials.ssid);
|
|
result = network_wifi_remove_ssid(net_sm->event_parameters->ctx.credentials.ssid) == ESP_OK ? EVENT_HANDLED : EVENT_UN_HANDLED;
|
|
FREE_AND_NULL(net_sm->event_parameters->ctx.credentials.ssid);
|
|
}
|
|
return result;
|
|
break;
|
|
case EN_ADD:
|
|
if (net_sm->event_parameters->ctx.credentials.ssid && strlen(net_sm->event_parameters->ctx.credentials.ssid) > 0) {
|
|
ESP_LOGD(TAG, "Adding ssid credentials for %s, password %s", net_sm->event_parameters->ctx.credentials.ssid,
|
|
STR_OR_BLANK(net_sm->event_parameters->ctx.credentials.password));
|
|
result =
|
|
network_wifi_add_ssid(net_sm->event_parameters->ctx.credentials.ssid, net_sm->event_parameters->ctx.credentials.password) == ESP_OK
|
|
? EVENT_HANDLED
|
|
: EVENT_UN_HANDLED;
|
|
FREE_AND_NULL(net_sm->event_parameters->ctx.credentials.ssid);
|
|
FREE_AND_NULL(net_sm->event_parameters->ctx.credentials.password);
|
|
} else {
|
|
ESP_LOGE(TAG, "Invalid ssid for event EN_ADD");
|
|
}
|
|
return result;
|
|
break;
|
|
case EN_REBOOT:
|
|
ESP_LOGD(TAG, "Called for reboot type %d", net_sm->event_parameters->ctx.rtype);
|
|
switch (net_sm->event_parameters->ctx.rtype) {
|
|
case OTA:
|
|
ESP_LOGD(TAG, " Calling guided_restart_ota.");
|
|
guided_restart_ota();
|
|
break;
|
|
case RECOVERY:
|
|
ESP_LOGD(TAG, " Calling guided_factory.");
|
|
guided_factory();
|
|
break;
|
|
case RESTART:
|
|
ESP_LOGD(TAG, " Calling simple_restart.");
|
|
simple_restart();
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
// If configuratin could not be locked because of pending events,
|
|
// another reboot event will be queued. So in the meantime
|
|
// consider this event as handled
|
|
return EVENT_HANDLED;
|
|
break;
|
|
case EN_REBOOT_URL:
|
|
if (net_sm->event_parameters->ctx.strval) {
|
|
start_ota(net_sm->event_parameters->ctx.strval, NULL, 0);
|
|
FREE_AND_NULL(net_sm->event_parameters->ctx.strval);
|
|
}
|
|
return EVENT_UN_HANDLED;
|
|
break;
|
|
case EN_COMMIT_CHANGES:
|
|
config_commit_protowrapper((void*)net_sm->event_parameters->ctx.strval);
|
|
return EVENT_HANDLED;
|
|
break;
|
|
case EN_EXECUTE_CALLBACK:
|
|
ESP_LOGD(TAG, "Executing Callback");
|
|
if (!net_sm->event_parameters->ctx.cb_ctx.cb && !net_sm->event_parameters->ctx.cb_ctx.ret_cb) {
|
|
ESP_LOGE(TAG, "Invalid callback");
|
|
return EVENT_UN_HANDLED;
|
|
} else if (net_sm->event_parameters->ctx.cb_ctx.cb) {
|
|
ESP_LOGD(TAG, "Executing Callback with no return value");
|
|
net_sm->event_parameters->ctx.cb_ctx.cb(net_sm->event_parameters->ctx.cb_ctx.ctx);
|
|
ESP_LOGD(TAG, "Done executing callback");
|
|
return EVENT_HANDLED;
|
|
} else if (net_sm->event_parameters->ctx.cb_ctx.ret_cb) {
|
|
ESP_LOGD(TAG, "Executing Callback with return value");
|
|
err = net_sm->event_parameters->ctx.cb_ctx.ret_cb(net_sm->event_parameters->ctx.cb_ctx.ctx);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "Error processing event");
|
|
return EVENT_UN_HANDLED;
|
|
}
|
|
ESP_LOGD(TAG, "Done executing callback");
|
|
return EVENT_HANDLED;
|
|
}
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return EVENT_UN_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* INSTANTIATED_STATE
|
|
*/
|
|
static state_machine_result_t NETWORK_INSTANTIATED_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_handler_entry_print(State_Machine, true);
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t NETWORK_INSTANTIATED_STATE_handler(state_machine_t* const State_Machine) {
|
|
network_handler_print(State_Machine, true);
|
|
state_machine_result_t result = EVENT_UN_HANDLED;
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
State_Machine->State = &network_states[NETWORK_INSTANTIATED_STATE];
|
|
State_Machine->Event = EN_START;
|
|
nm->sta_polling_max_ms = (platform && platform->net.sta_polling_max_s > 0 ? platform->net.sta_polling_max_s : 600) * 1000;
|
|
nm->ap_duration_ms = (platform && platform->net.ap_duration_s > 0 ? platform->net.ap_duration_s : 20) * 1000;
|
|
nm->sta_polling_min_ms = (platform && platform->net.sta_polling_min_s > 0 ? platform->net.sta_polling_min_s : 15) * 1000;
|
|
nm->eth_link_down_reboot_ms = (platform && platform->dev.eth.common.ethtmout_s > 0 ? platform->dev.eth.common.ethtmout_s : 30) * 1000;
|
|
nm->dhcp_timeout = (platform && platform->net.dhcp_tmout_s > 0 ? platform->net.dhcp_tmout_s : 30) * 1000;
|
|
ESP_LOGI(TAG,
|
|
"Network manager configuration: polling max %d, polling min %d, ap delay %d, dhcp timeout "
|
|
"%d, eth timeout %d",
|
|
nm->sta_polling_max_ms, nm->sta_polling_min_ms, nm->ap_duration_ms, nm->dhcp_timeout, nm->eth_link_down_reboot_ms);
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
#ifndef TESTPROJECT
|
|
if(!platform){
|
|
ESP_LOGE(TAG,"Configuration not initialized.");
|
|
}
|
|
else
|
|
{
|
|
if (platform && State_Machine->Event == EN_START) {
|
|
result = local_traverse_state(State_Machine, &network_states[NETWORK_INITIALIZING_STATE], __FUNCTION__);
|
|
}
|
|
}
|
|
#else
|
|
ESP_LOGI(TAG,"network message ignored in test mode");
|
|
result= EVENT_HANDLED;
|
|
#endif
|
|
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t NETWORK_INSTANTIATED_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* INITIALIZING_STATE
|
|
*/
|
|
static state_machine_result_t NETWORK_INITIALIZING_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_handler_entry_print(State_Machine, true);
|
|
// Initialize nvs; this is needed by the wifi driver
|
|
ESP_LOGI(TAG, "Initializing flash nvs ");
|
|
esp_err_t err = nvs_flash_init();
|
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
ESP_LOGW(TAG, "%s. Erasing nvs flash", esp_err_to_name(err));
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
err = nvs_flash_init();
|
|
}
|
|
network_wifi_global_init();
|
|
esp_netif_init();
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
init_network_status();
|
|
esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &network_ip_event_handler, NULL);
|
|
// send a message to start the connections
|
|
http_server_start();
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
static state_machine_result_t NETWORK_INITIALIZING_STATE_handler(state_machine_t* const State_Machine) {
|
|
|
|
network_handler_print(State_Machine, true);
|
|
state_machine_result_t result = EVENT_UN_HANDLED;
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
switch (State_Machine->Event) {
|
|
case EN_START:
|
|
if (network_is_wifi_prioritized()) {
|
|
ESP_LOGI(TAG, "WiFi connection is prioritized. Starting WiFi");
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_INITIALIZING_STATE], __FUNCTION__);
|
|
} else if (is_recovery_running) {
|
|
ESP_LOGI(TAG, "Running recovery. Skipping ethernet, starting WiFi");
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_INITIALIZING_STATE], __FUNCTION__);
|
|
} else {
|
|
result = local_traverse_state(State_Machine, &Eth_Active_State[ETH_STARTING_STATE], __FUNCTION__);
|
|
}
|
|
break;
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t NETWORK_INITIALIZING_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* ETH_STARTING_STATE
|
|
*/
|
|
static state_machine_result_t ETH_STARTING_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_handler_entry_print(State_Machine, true);
|
|
ESP_LOGD(TAG, "Looking for ethernet Interface");
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
init_network_ethernet();
|
|
if (!network_ethernet_enabled()) {
|
|
network_async_fail();
|
|
} else {
|
|
nm->eth_netif = network_ethernet_get_interface();
|
|
}
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
static state_machine_result_t ETH_STARTING_STATE_handler(state_machine_t* const State_Machine) {
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
network_handler_print(State_Machine, true);
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
switch (State_Machine->Event) {
|
|
case EN_FAIL:
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_INITIALIZING_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_SUCCESS:
|
|
result = local_traverse_state(State_Machine, &network_states[NETWORK_ETH_ACTIVE_STATE], __FUNCTION__);
|
|
break;
|
|
default:
|
|
ESP_LOGE(TAG, "No handler");
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t ETH_STARTING_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* NETWORK_ETH_ACTIVE_STATE
|
|
*/
|
|
static state_machine_result_t NETWORK_ETH_ACTIVE_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_handler_entry_print(State_Machine, true);
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
ESP_LOGD(TAG,"Entering ETH Active state. Setting timer");
|
|
network_set_timer(nm->eth_link_down_reboot_ms, "Ethernet link not detected");
|
|
ESP_LOGD(TAG,"Entering ETH Active state. Executing callback(s)");
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t NETWORK_ETH_ACTIVE_STATE_handler(state_machine_t* const State_Machine) {
|
|
network_handler_print(State_Machine, true);
|
|
state_machine_result_t result = EVENT_UN_HANDLED;
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
|
|
switch (State_Machine->Event) {
|
|
case EN_CONNECT_NEW:
|
|
result = local_traverse_state(State_Machine, &Eth_Active_State[ETH_CONNECTING_NEW_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_LINK_UP:
|
|
result = local_traverse_state(State_Machine, &Eth_Active_State[ETH_ACTIVE_LINKUP_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_LINK_DOWN:
|
|
result = local_traverse_state(State_Machine, &Eth_Active_State[ETH_ACTIVE_LINKDOWN_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_ETH_GOT_IP:
|
|
network_handle_got_ip(State_Machine->Event, nm->event_parameters->ctx.got_ip_event_data);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.got_ip_event_data);
|
|
result = local_traverse_state(State_Machine, &Eth_Active_State[ETH_ACTIVE_CONNECTED_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_ETHERNET_FALLBACK:
|
|
result = local_traverse_state(State_Machine, &Eth_Active_State[ETH_ACTIVE_CONNECTED_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_TIMER:
|
|
ESP_LOGW(TAG, "Timeout %s. Rebooting to wifi", STR_OR_ALT(nm->timer_tag, "Ethernet link not detected"));
|
|
network_prioritize_wifi(true);
|
|
simple_restart();
|
|
// result= local_traverse_state(State_Machine,
|
|
// &Wifi_Active_State[WIFI_INITIALIZING_STATE],__FUNCTION__);
|
|
break;
|
|
case EN_SCAN:
|
|
ESP_LOGW(TAG, "Wifi scan cannot be executed in this state");
|
|
// todo: fix this!
|
|
// network_wifi_built_known_ap_list();
|
|
result = EVENT_HANDLED;
|
|
break;
|
|
case EN_DELETE: {
|
|
ESP_LOGD(TAG, "WiFi disconnected by user");
|
|
network_wifi_clear_config();
|
|
network_status_update_ip_info(sys_status_reasons_R_USER_DISCONNECT);
|
|
|
|
result = EVENT_HANDLED;
|
|
} break;
|
|
default:
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t NETWORK_ETH_ACTIVE_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_set_timer(0, NULL);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* ETH_CONNECTING_NEW_STATE
|
|
*/
|
|
static state_machine_result_t ETH_CONNECTING_NEW_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_handler_entry_print(State_Machine, true);
|
|
network_start_stop_dhcp_client(nm->wifi_netif, true);
|
|
network_wifi_connect(nm->event_parameters->ctx.credentials.ssid, nm->event_parameters->ctx.credentials.password);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.credentials.ssid);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.credentials.password);
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t ETH_CONNECTING_NEW_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, true);
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
|
|
switch (State_Machine->Event) {
|
|
case EN_GOT_IP:
|
|
network_handle_got_ip(State_Machine->Event, nm->event_parameters->ctx.got_ip_event_data);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.got_ip_event_data);
|
|
result = local_traverse_state(State_Machine, &network_states[WIFI_CONNECTED_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_LOST_CONNECTION:
|
|
network_status_update_ip_info(sys_status_reasons_R_FAILED_ATTEMPT);
|
|
messaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, "Unable to connect to new WiFi access point.");
|
|
// no existing configuration, or wifi wasn't the active connection when connection
|
|
// attempt was made
|
|
network_async(EN_ETHERNET_FALLBACK);
|
|
result = EVENT_HANDLED;
|
|
break;
|
|
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t ETH_CONNECTING_NEW_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_set_timer(0, NULL);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* ETH_ACTIVE_LINKDOWN
|
|
*/
|
|
static state_machine_result_t ETH_ACTIVE_LINKDOWN_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_handler_entry_print(State_Machine, true);
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_set_timer(nm->eth_link_down_reboot_ms, "Ethernet link down");
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "Ethernet link down.");
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t ETH_ACTIVE_LINKDOWN_STATE_handler(state_machine_t* const State_Machine) {
|
|
network_handler_print(State_Machine, true);
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, false);
|
|
return EVENT_UN_HANDLED;
|
|
}
|
|
static state_machine_result_t ETH_ACTIVE_LINKDOWN_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_set_timer(0, NULL);
|
|
network_exit_handler_print(State_Machine, false);
|
|
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* ETH_ACTIVE_LINKUP_STATE
|
|
*/
|
|
static state_machine_result_t ETH_ACTIVE_LINKUP_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_handler_entry_print(State_Machine, true);
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_set_timer(nm->dhcp_timeout, "DHCP timeout");
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
messaging_post_message(MESSAGING_INFO, MESSAGING_CLASS_SYSTEM, "Ethernet link up.");
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t ETH_ACTIVE_LINKUP_STATE_handler(state_machine_t* const State_Machine) {
|
|
state_machine_result_t result = EVENT_UN_HANDLED;
|
|
network_handler_print(State_Machine, true);
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t ETH_ACTIVE_LINKUP_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_UP_STATE
|
|
*/
|
|
static state_machine_result_t NETWORK_WIFI_ACTIVE_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_handler_entry_print(State_Machine, true);
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t NETWORK_WIFI_ACTIVE_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, true);
|
|
state_machine_result_t result = EVENT_UN_HANDLED;
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
switch (State_Machine->Event) {
|
|
case EN_LINK_UP:
|
|
ESP_LOGW(TAG, "Ethernet link up in wifi mode");
|
|
break;
|
|
case EN_ETH_GOT_IP:
|
|
network_handle_got_ip(State_Machine->Event, nm->event_parameters->ctx.got_ip_event_data);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.got_ip_event_data);
|
|
network_interface_coexistence(State_Machine);
|
|
break;
|
|
case EN_GOT_IP:
|
|
network_handle_got_ip(State_Machine->Event, nm->event_parameters->ctx.got_ip_event_data);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.got_ip_event_data);
|
|
|
|
network_status_update_ip_info(sys_status_reasons_R_CONNECTION_OK);
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTED_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_SCAN:
|
|
if (network_wifi_start_scan() == ESP_OK) {
|
|
result = EVENT_HANDLED;
|
|
}
|
|
break;
|
|
case EN_SCAN_DONE:
|
|
if (wifi_scan_done() == ESP_OK) {
|
|
result = EVENT_HANDLED;
|
|
}
|
|
network_async_success();
|
|
break;
|
|
case EN_CONNECT_NEW:
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTING_NEW_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_DELETE:
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_USER_DISCONNECTED_STATE], __FUNCTION__);
|
|
break;
|
|
|
|
case EN_ETHERNET_FALLBACK:
|
|
result = local_traverse_state(State_Machine, &Eth_Active_State[ETH_ACTIVE_CONNECTED_STATE], __FUNCTION__);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t NETWORK_WIFI_ACTIVE_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_INITIALIZING_STATE
|
|
*/
|
|
static state_machine_result_t WIFI_INITIALIZING_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_handler_entry_print(State_Machine, true);
|
|
if (!nm->wifi_netif) {
|
|
nm->wifi_netif = network_wifi_start();
|
|
}
|
|
if (!is_wifi_up()) {
|
|
messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "Wifi not started. Load Configuration");
|
|
return EVENT_UN_HANDLED;
|
|
}
|
|
if (network_wifi_get_known_count() > 0) {
|
|
ESP_LOGI(TAG, "Existing wifi config found. Attempting to connect.");
|
|
network_async_success();
|
|
} else {
|
|
/* no wifi saved: start soft AP! This is what should happen during a first run */
|
|
ESP_LOGW(TAG, "No saved wifi. Starting AP configuration mode.");
|
|
network_async_configure();
|
|
}
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
static state_machine_result_t WIFI_INITIALIZING_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, true);
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
switch (State_Machine->Event) {
|
|
case EN_CONFIGURE:
|
|
result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_SUCCESS:
|
|
ESP_LOGI(TAG, "Traversing to Connecting state");
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTING_STATE], __FUNCTION__);
|
|
break;
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t WIFI_INITIALIZING_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_CONFIGURING_ACTIVE_STATE
|
|
*/
|
|
static state_machine_result_t NETWORK_WIFI_CONFIGURING_ACTIVE_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_handler_entry_print(State_Machine, true);
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
nm->wifi_ap_netif = network_wifi_config_ap();
|
|
dns_server_start(nm->wifi_ap_netif);
|
|
network_wifi_start_scan();
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t NETWORK_WIFI_CONFIGURING_ACTIVE_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, true);
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
switch (State_Machine->Event) {
|
|
case EN_SCAN:
|
|
if (network_wifi_start_scan() == ESP_OK) {
|
|
result = EVENT_HANDLED;
|
|
}
|
|
break;
|
|
case EN_SCAN_DONE:
|
|
ESP_LOGD(TAG, "Network configuration active, wifi scan completed");
|
|
if (wifi_scan_done() == ESP_OK) {
|
|
result = EVENT_HANDLED;
|
|
}
|
|
break;
|
|
case EN_CONNECT_NEW:
|
|
result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_CONNECT_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_LINK_UP:
|
|
ESP_LOGW(TAG, "Ethernet link up in wifi mode");
|
|
break;
|
|
case EN_ETH_GOT_IP:
|
|
network_handle_got_ip(State_Machine->Event, nm->event_parameters->ctx.got_ip_event_data);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.got_ip_event_data);
|
|
network_interface_coexistence(State_Machine);
|
|
break;
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t NETWORK_WIFI_CONFIGURING_ACTIVE_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
/* bring down DNS hijack */
|
|
ESP_LOGD(TAG, " Stopping DNS.");
|
|
dns_server_stop();
|
|
ESP_LOGD(TAG, "Changing wifi mode to STA.");
|
|
network_wifi_set_sta_mode();
|
|
network_exit_handler_print(State_Machine, false);
|
|
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_CONFIGURING_STATE
|
|
*/
|
|
static state_machine_result_t WIFI_CONFIGURING_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_handler_entry_print(State_Machine, true);
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t WIFI_CONFIGURING_STATE_handler(state_machine_t* const State_Machine) {
|
|
network_handler_print(State_Machine, true);
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, false);
|
|
return EVENT_UN_HANDLED;
|
|
}
|
|
static state_machine_result_t WIFI_CONFIGURING_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_CONFIGURING_CONNECT_STATE
|
|
*/
|
|
static state_machine_result_t WIFI_CONFIGURING_CONNECT_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_handler_entry_print(State_Machine, true);
|
|
network_start_stop_dhcp_client(nm->wifi_netif, true);
|
|
network_wifi_connect(nm->event_parameters->ctx.credentials.ssid, nm->event_parameters->ctx.credentials.password);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.credentials.ssid);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.credentials.password);
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t WIFI_CONFIGURING_CONNECT_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, true);
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
switch (State_Machine->Event) {
|
|
case EN_CONNECTED:
|
|
result = EVENT_HANDLED;
|
|
ESP_LOGI(TAG, "Wifi was connected. Waiting for IP address");
|
|
network_set_timer(nm->dhcp_timeout, "DHCP Timeout");
|
|
network_wifi_set_connected(true);
|
|
break;
|
|
case EN_GOT_IP:
|
|
network_handle_got_ip(State_Machine->Event, nm->event_parameters->ctx.got_ip_event_data);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.got_ip_event_data);
|
|
network_status_update_ip_info(sys_status_reasons_R_CONNECTION_OK);
|
|
result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_CONNECT_SUCCESS_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_LOST_CONNECTION:
|
|
if (nm->event_parameters->ctx.disconnected_event->reason == WIFI_REASON_ASSOC_LEAVE) {
|
|
ESP_LOGI(TAG, "Wifi was disconnected from previous access point. Waiting to connect.");
|
|
} else {
|
|
network_status_update_ip_info(sys_status_reasons_R_FAILED_ATTEMPT);
|
|
result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE], __FUNCTION__);
|
|
}
|
|
break;
|
|
case EN_TIMER:
|
|
ESP_LOGW(TAG, "Connection timeout. (%s)", STR_OR_ALT(nm->timer_tag, "Unknown"));
|
|
network_status_update_ip_info(sys_status_reasons_R_FAILED_ATTEMPT);
|
|
result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE], __FUNCTION__);
|
|
break;
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t WIFI_CONFIGURING_CONNECT_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
|
|
FREE_AND_NULL(((network_t*)State_Machine)->event_parameters->ctx.disconnected_event);
|
|
network_set_timer(0, NULL);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_CONFIGURING_CONNECT_SUCCESS_STATE
|
|
*/
|
|
static state_machine_result_t WIFI_CONFIGURING_CONNECT_SUCCESS_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_handler_entry_print(State_Machine, true);
|
|
network_status_update_ip_info(sys_status_reasons_R_CONNECTION_OK);
|
|
ESP_LOGD(TAG, "Saving wifi configuration.");
|
|
// todo: fix this!
|
|
// network_wifi_save_sta_config();
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t WIFI_CONFIGURING_CONNECT_SUCCESS_STATE_handler(state_machine_t* const State_Machine) {
|
|
network_handler_print(State_Machine, true);
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
switch (State_Machine->Event) {
|
|
case EN_UPDATE_STATUS:
|
|
// todo: fix this
|
|
// network_status_update_basic_info();
|
|
network_set_timer(nm->ap_duration_ms, "Access point teardown"); // set a timer to tear down the AP mode
|
|
break;
|
|
case EN_TIMER:
|
|
// todo: fix this
|
|
// network_status_update_basic_info();
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTED_STATE], __FUNCTION__);
|
|
break;
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
// Process global handler at the end, since we want to overwrite
|
|
// UPDATE_STATUS with our own logic above
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t WIFI_CONFIGURING_CONNECT_SUCCESS_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_set_timer(0, NULL);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_CONNECTING_STATE
|
|
*/
|
|
static state_machine_result_t WIFI_CONNECTING_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_handler_entry_print(State_Machine, true);
|
|
nm->initial_retries = 0;
|
|
network_start_stop_dhcp_client(nm->wifi_netif, true);
|
|
network_connect_active_ssid(State_Machine);
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t WIFI_CONNECTING_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_handler_print(State_Machine, true);
|
|
switch (State_Machine->Event) {
|
|
case EN_CONNECTED:
|
|
// nothing to do here. Let's wait for IP address
|
|
break;
|
|
case EN_TIMER:
|
|
if (nm->initial_retries < (platform!=NULL && platform->net.max_initial_wifi_attempt>0?platform->net.max_initial_wifi_attempt:5)) {
|
|
nm->initial_retries++;
|
|
ESP_LOGD(TAG, " Retrying connection connection, %d/%d.", nm->initial_retries, platform->net.max_initial_wifi_attempt);
|
|
ESP_LOGI(TAG, "Timer: %s ", STR_OR_ALT(nm->timer_tag, "Ethernet link not detected"));
|
|
network_connect_active_ssid(State_Machine);
|
|
} else {
|
|
}
|
|
break;
|
|
case EN_LOST_CONNECTION:
|
|
if (nm->event_parameters->ctx.disconnected_event->reason == WIFI_REASON_ASSOC_LEAVE ||
|
|
nm->event_parameters->ctx.disconnected_event->reason == WIFI_REASON_AUTH_EXPIRE ||
|
|
nm->event_parameters->ctx.disconnected_event->reason == WIFI_REASON_ASSOC_EXPIRE) {
|
|
ESP_LOGI(TAG, "Wifi was disconnected from previous access point. Waiting to connect.");
|
|
} else if (nm->event_parameters->ctx.disconnected_event->reason != WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT) {
|
|
network_status_update_ip_info(sys_status_reasons_R_FAILED_ATTEMPT);
|
|
result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE], __FUNCTION__);
|
|
}
|
|
break;
|
|
case EN_FAIL:
|
|
// this would happen if there is no previously connected AP
|
|
ESP_LOGD(TAG, "No active access point. switching to scan");
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTING_SCAN_STATE], __FUNCTION__);
|
|
break;
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t WIFI_CONNECTING_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_CONNECTING_STATE
|
|
*/
|
|
static state_machine_result_t WIFI_CONNECTING_SCAN_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
ESP_LOGD(TAG, "Entering Scan State");
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_handler_entry_print(State_Machine, true);
|
|
network_async_scan();
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t WIFI_CONNECTING_SCAN_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
network_handler_print(State_Machine, true);
|
|
switch (State_Machine->Event) {
|
|
case EN_SUCCESS:
|
|
// scan completed, we can try to connect to a known AP now
|
|
network_wifi_activate_strongest_ssid();
|
|
result = local_switch_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTING_STATE], __FUNCTION__);
|
|
break;
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t WIFI_CONNECTING_SCAN_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_CONNECTING_NEW_STATE
|
|
*/
|
|
static state_machine_result_t WIFI_CONNECTING_NEW_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_handler_entry_print(State_Machine, true);
|
|
network_start_stop_dhcp_client(nm->wifi_netif, true);
|
|
network_wifi_connect(nm->event_parameters->ctx.credentials.ssid, nm->event_parameters->ctx.credentials.password);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.credentials.ssid);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.credentials.password);
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t WIFI_CONNECTING_NEW_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, true);
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
|
|
switch (State_Machine->Event) {
|
|
case EN_GOT_IP:
|
|
network_handle_got_ip(State_Machine->Event, nm->event_parameters->ctx.got_ip_event_data);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.got_ip_event_data);
|
|
|
|
network_status_update_ip_info(sys_status_reasons_R_CONNECTION_OK);
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTED_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_CONNECTED:
|
|
ESP_LOGD(TAG, "Successfully connected to the new access point. Waiting for IP Address");
|
|
result = EVENT_HANDLED;
|
|
break;
|
|
case EN_LOST_CONNECTION:
|
|
if (((network_t*)State_Machine)->event_parameters->ctx.disconnected_event->reason == WIFI_REASON_ASSOC_LEAVE) {
|
|
ESP_LOGD(TAG, "Successfully disconnected from the existing access point. ");
|
|
return EVENT_HANDLED;
|
|
}
|
|
ESP_LOGW(TAG, "Trying to connect failed");
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTING_NEW_FAILED_STATE], __FUNCTION__);
|
|
break;
|
|
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t WIFI_CONNECTING_NEW_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_set_timer(0, NULL);
|
|
FREE_AND_NULL(((network_t*)State_Machine)->event_parameters->ctx.disconnected_event);
|
|
network_exit_handler_print(State_Machine, false);
|
|
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_CONNECTING_NEW_FAILED_STATE
|
|
*/
|
|
static state_machine_result_t WIFI_CONNECTING_NEW_FAILED_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_handler_entry_print(State_Machine, true);
|
|
if (nm->wifi_connected) {
|
|
// Wifi was already connected to an existing access point. Restore connection
|
|
network_connect_active_ssid(State_Machine);
|
|
}
|
|
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t WIFI_CONNECTING_NEW_FAILED_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, true);
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
|
|
switch (State_Machine->Event) {
|
|
case EN_GOT_IP:
|
|
network_handle_got_ip(State_Machine->Event, nm->event_parameters->ctx.got_ip_event_data);
|
|
FREE_AND_NULL(nm->event_parameters->ctx.got_ip_event_data);
|
|
|
|
network_status_update_ip_info(sys_status_reasons_R_FAILED_ATTEMPT_AND_RESTORE);
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTED_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_CONNECTED:
|
|
ESP_LOGD(TAG, "Successfully connected to the previous access point. Waiting for IP Address");
|
|
result = EVENT_HANDLED;
|
|
break;
|
|
case EN_LOST_CONNECTION:
|
|
network_status_update_ip_info(sys_status_reasons_R_FAILED_ATTEMPT);
|
|
messaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, "Unable to fall back to previous access point.");
|
|
result = EVENT_HANDLED;
|
|
break;
|
|
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t WIFI_CONNECTING_NEW_FAILED_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_set_timer(0, NULL);
|
|
FREE_AND_NULL(((network_t*)State_Machine)->event_parameters->ctx.disconnected_event);
|
|
network_exit_handler_print(State_Machine, false);
|
|
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_CONNECTED_STATE
|
|
*/
|
|
static state_machine_result_t WIFI_CONNECTED_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_handler_entry_print(State_Machine, true);
|
|
ESP_LOGD(TAG,"Entered WIFI_CONNECTED_STATE ");
|
|
nm->last_connected = esp_timer_get_time();
|
|
// cancel timeout pulse
|
|
network_set_timer(0, NULL);
|
|
network_interface_coexistence(State_Machine);
|
|
nm->wifi_connected = true;
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t WIFI_CONNECTED_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, true);
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
switch (State_Machine->Event) {
|
|
case EN_LOST_CONNECTION:
|
|
if (is_restarting()) {
|
|
// ignore this as we're restarting
|
|
return EVENT_HANDLED;
|
|
}
|
|
if (nm->event_parameters->ctx.disconnected_event->reason == WIFI_REASON_ASSOC_LEAVE) {
|
|
ESP_LOGD(TAG, "User disconnected from wifi. Re-initializing connection");
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_INITIALIZING_STATE], __FUNCTION__);
|
|
} else {
|
|
ESP_LOGD(TAG, "Lost connection for a reason other than disconnecting. ");
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_LOST_CONNECTION_STATE], __FUNCTION__);
|
|
}
|
|
|
|
break;
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
break;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t WIFI_CONNECTED_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
FREE_AND_NULL(((network_t*)State_Machine)->event_parameters->ctx.disconnected_event);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_USER_DISCONNECTED_STATE
|
|
*/
|
|
static state_machine_result_t WIFI_USER_DISCONNECTED_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_handler_entry_print(State_Machine, true);
|
|
ESP_LOGD(TAG, " WiFi disconnected by user");
|
|
network_wifi_clear_config();
|
|
network_status_update_ip_info(sys_status_reasons_R_USER_DISCONNECT);
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t WIFI_USER_DISCONNECTED_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_handler_print(State_Machine, true);
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
switch (State_Machine->Event) {
|
|
case EN_LOST_CONNECTION:
|
|
// this is a success! we're actually asking to disconnect
|
|
result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE], __FUNCTION__);
|
|
break;
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t WIFI_USER_DISCONNECTED_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* WIFI_LOST_CONNECTION_STATE
|
|
*/
|
|
static state_machine_result_t WIFI_LOST_CONNECTION_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_handler_entry_print(State_Machine, true);
|
|
ESP_LOGE(TAG, " WiFi Connection lost.");
|
|
messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "WiFi Connection lost");
|
|
network_status_update_ip_info(sys_status_reasons_R_LOST_CONNECTION);
|
|
// todo: fix this!
|
|
// network_status_safe_reset_sta_ip_string();
|
|
if (nm->last_connected > 0) nm->total_connected_time += ((esp_timer_get_time() - nm->last_connected) / (1000 * 1000));
|
|
nm->last_connected = 0;
|
|
nm->num_disconnect++;
|
|
ESP_LOGW(TAG, " Wifi disconnected. Number of disconnects: %d, Average time connected: %d", nm->num_disconnect,
|
|
nm->num_disconnect > 0 ? (nm->total_connected_time / nm->num_disconnect) : 0);
|
|
if (nm->retries < (platform && platform->net.max_wifi_retry>0?platform->net.max_wifi_retry:5)) {
|
|
nm->retries++;
|
|
ESP_LOGD(TAG, " Retrying connection connection, %d/%d.", nm->retries, platform->net.max_wifi_retry);
|
|
network_async(EN_CONNECT);
|
|
} else {
|
|
/* In this scenario the connection was lost beyond repair */
|
|
nm->retries = 0;
|
|
ESP_LOGD(TAG, "Checking if ethernet interface is connected");
|
|
if (network_is_interface_connected(nm->eth_netif)) {
|
|
ESP_LOGW(TAG, "Cannot connect to Wifi. Falling back to Ethernet ");
|
|
network_async(EN_ETHERNET_FALLBACK);
|
|
} else {
|
|
network_status_update_ip_info(sys_status_reasons_R_LOST_CONNECTION);
|
|
wifi_mode_t mode;
|
|
ESP_LOGW(TAG, " All connect retry attempts failed.");
|
|
|
|
/* put us in softAP mode first */
|
|
esp_wifi_get_mode(&mode);
|
|
if (WIFI_MODE_APSTA != mode) {
|
|
nm->STA_duration = nm->sta_polling_min_ms;
|
|
network_async_configure();
|
|
} else if (nm->STA_duration < nm->sta_polling_max_ms) {
|
|
nm->STA_duration *= 1.25;
|
|
}
|
|
|
|
/* keep polling for existing connection */
|
|
network_set_timer(nm->STA_duration, "Wifi Polling timeout");
|
|
ESP_LOGD(TAG, " STA search slow polling of %d", nm->STA_duration);
|
|
}
|
|
}
|
|
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t WIFI_LOST_CONNECTION_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
network_handler_print(State_Machine, true);
|
|
switch (State_Machine->Event) {
|
|
case EN_CONFIGURE:
|
|
result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_TIMER:
|
|
ESP_LOGI(TAG, "Timer: %s ", STR_OR_ALT(nm->timer_tag, "Lost connection"));
|
|
result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_LOST_CONNECTION_STATE], __FUNCTION__);
|
|
break;
|
|
case EN_CONNECT:
|
|
result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONNECTING_STATE], __FUNCTION__);
|
|
break;
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t WIFI_LOST_CONNECTION_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
FREE_AND_NULL(((network_t*)State_Machine)->event_parameters->ctx.disconnected_event);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************************************
|
|
* ETH_ACTIVE_CONNECTED_STATE
|
|
*/
|
|
static state_machine_result_t ETH_ACTIVE_CONNECTED_STATE_entry_handler(state_machine_t* const State_Machine) {
|
|
network_t* const nm = (network_t*)State_Machine;
|
|
network_handler_entry_print(State_Machine, true);
|
|
network_status_update_ip_info(sys_status_reasons_R_ETHERNET_CONNECTED);
|
|
nm->ethernet_connected = true;
|
|
NETWORK_EXECUTE_CB(State_Machine);
|
|
network_handler_entry_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
static state_machine_result_t ETH_ACTIVE_CONNECTED_STATE_handler(state_machine_t* const State_Machine) {
|
|
HANDLE_GLOBAL_EVENT(State_Machine);
|
|
state_machine_result_t result = EVENT_HANDLED;
|
|
network_handler_print(State_Machine, true);
|
|
switch (State_Machine->Event) {
|
|
case EN_TIMER:
|
|
ESP_LOGD(TAG, "Ignoring ethernet link up timer check");
|
|
result = EVENT_HANDLED;
|
|
break;
|
|
default:
|
|
result = EVENT_UN_HANDLED;
|
|
}
|
|
network_handler_print(State_Machine, false);
|
|
return result;
|
|
}
|
|
static state_machine_result_t ETH_ACTIVE_CONNECTED_STATE_exit_handler(state_machine_t* const State_Machine) {
|
|
network_exit_handler_print(State_Machine, true);
|
|
network_exit_handler_print(State_Machine, false);
|
|
return EVENT_HANDLED;
|
|
}
|
|
|
|
static state_machine_result_t local_switch_state(state_machine_t* state_machine, const state_t* const target_state, const char* caller) {
|
|
const state_t* source = state_machine->State;
|
|
NETWORK_PRINT_TRANSITION(true, "BEGIN SWITCH", ((network_t*)state_machine)->source_state, target_state, state_machine->Event, true, caller);
|
|
state_machine_result_t result = switch_state(state_machine, target_state);
|
|
NETWORK_PRINT_TRANSITION(false, "BEGIN SWITCH", ((network_t*)state_machine)->source_state, target_state, state_machine->Event, true, caller);
|
|
((network_t*)state_machine)->source_state = source;
|
|
return result;
|
|
}
|
|
static state_machine_result_t local_traverse_state(state_machine_t* const state_machine, const state_t* const target_state, const char* caller) {
|
|
const state_t* source = state_machine->State;
|
|
NETWORK_PRINT_TRANSITION(true, "BEGIN TRAVERSE", ((network_t*)state_machine)->source_state, target_state, state_machine->Event, true, caller);
|
|
state_machine_result_t result = traverse_state(state_machine, target_state);
|
|
NETWORK_PRINT_TRANSITION(false, "END TRAVERSE", ((network_t*)state_machine)->source_state, target_state, state_machine->Event, true, caller);
|
|
((network_t*)state_machine)->source_state = source;
|
|
return result;
|
|
}
|
|
|
|
static void network_interface_coexistence(state_machine_t* state_machine) {
|
|
// this function is called whenever both wifi and ethernet are
|
|
// found to be active at the same time
|
|
network_t* nm = (network_t*)state_machine;
|
|
if (nm->wifi_connected && state_machine->Event == EN_ETH_GOT_IP) {
|
|
network_prioritize_wifi(false);
|
|
if (platform && !platform->net.eth_boot) {
|
|
ESP_LOGW(TAG, "Option eth_reboot set to reboot when ethernet is connected. Rebooting");
|
|
simple_restart();
|
|
} else {
|
|
ESP_LOGW(TAG, "Option eth_reboot set to not reboot when ethernet is connected. Using "
|
|
"Wifi interface until next reboot");
|
|
}
|
|
} else if (get_root(state_machine->State)->Id == NETWORK_ETH_ACTIVE_STATE) {
|
|
messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "Wifi Connected with Ethernet active. System reload needed");
|
|
simple_restart();
|
|
}
|
|
} |