#ifndef NL_WIFI_SM_H #define NL_WIFI_SM_H #include #include #define NL_WIFI_FALLBACK_MS 300000u /* 5 min without connection -> AP+STA */ #define NL_WIFI_ONBOARD_TIMEOUT_MS 30000u #define NL_WIFI_AP_CLOSE_ONBOARD_MS 60000u #define NL_WIFI_AP_CLOSE_FALLBACK_MS 10000u #define NL_WIFI_BACKOFF_MIN_MS 1000u #define NL_WIFI_BACKOFF_MAX_MS 60000u #define NL_WIFI_CONNECT_TIMEOUT_MS 30000u /* no event after a connect -> treat as failed */ typedef enum { NL_WIFI_ST_AP_ONBOARDING = 0, NL_WIFI_ST_STA_CONNECTING, NL_WIFI_ST_STA_CONNECTED, NL_WIFI_ST_APSTA_FALLBACK, NL_WIFI_ST_ONBOARD_TEST } nl_wifi_state_t; typedef enum { NL_WIFI_ERR_NONE = 0, NL_WIFI_ERR_AUTH, NL_WIFI_ERR_NOT_FOUND, NL_WIFI_ERR_TIMEOUT } nl_wifi_error_t; typedef enum { NL_WIFI_EV_CONNECTED = 0, NL_WIFI_EV_JOIN_FAILED, NL_WIFI_EV_DISCONNECTED } nl_wifi_event_t; /* What the driver has to do after a state machine call. */ typedef struct { bool start_ap; bool stop_ap; bool connect; /* connect the station ... */ bool use_test_creds; /* ... with the onboarding credentials instead of the stored ones */ bool save_test_creds; /* onboarding succeeded: persist the onboarding credentials */ bool disconnect; /* drop a stray/failed station attempt (before any connect in the same set) */ bool pulse_on; bool pulse_off; } nl_wifi_actions_t; typedef struct { nl_wifi_state_t state; nl_wifi_error_t error; /* result of the last onboarding test */ bool have_creds; bool ap_up; bool pulsing; bool retry_armed; bool ap_stop_armed; bool awaiting; /* waiting for response to a connect */ uint32_t since; /* start of the current connecting phase */ uint32_t retry_at; uint32_t backoff; uint32_t deadline; /* onboarding test */ uint32_t ap_stop_at; uint32_t connect_at; /* when the pending connect was issued */ } nl_wifi_sm_t; nl_wifi_actions_t nl_wifi_sm_start(nl_wifi_sm_t *sm, bool have_creds, uint32_t now); nl_wifi_actions_t nl_wifi_sm_event(nl_wifi_sm_t *sm, nl_wifi_event_t ev, nl_wifi_error_t err, uint32_t now); nl_wifi_actions_t nl_wifi_sm_tick(nl_wifi_sm_t *sm, uint32_t now); nl_wifi_actions_t nl_wifi_sm_onboard(nl_wifi_sm_t *sm, uint32_t now); bool nl_wifi_sm_open_access(const nl_wifi_sm_t *sm); const char *nl_wifi_sm_mode(const nl_wifi_sm_t *sm); const char *nl_wifi_error_name(nl_wifi_error_t e); #endif