Modbus Vault v1.0
Loading...
Searching...
No Matches
wifi_manager.c File Reference

Implementation of the wifi manager. More...

#include "wifi_manager.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_timer.h"
#include <string.h>
Include dependency graph for wifi_manager.c:

Macros

#define WIFI_MANAGE_MAXIMUM_RECONNECT_TIMEOUT_US    (60000000UL)
 
#define WIFI_MANAGE_MINIMUM_RECONNECT_TIMEOUT_US    (1000000)
 

Functions

static void wifi_manager_set_state_helper (const wifi_state_t state)
 Set current WiFi state.
 
static void wifi_event_handler (void *args_void_ptr, esp_event_base_t event_base, int32_t event_id, void *event_data_void_ptr)
 WiFi event handler.
 
static void reconnect_timer_callback (void *arg_void_ptr)
 Callback to reconnect on one-shot timer.
 
esp_err_t wifi_manager_init (wifi_config_t *wifi_config_ptr)
 Initialize Wi-Fi manager.
 
wifi_state_t wifi_manager_get_state (void)
 Get Wi-Fi current connection state.
 
void wifi_manager_trigger_reconnect (void)
 Trigger Wi-Fi re-connect.
 
void wifi_manager_deinit (void)
 De-Initialize Wi-Fi manager.
 

Variables

static const char * TAG = "WIFI_MANAGER"
 
static SemaphoreHandle_t state_lock = NULL
 
static wifi_state_t wifi_manager_state = WIFI_STATE_IDLE
 
static esp_timer_handle_t reconnect_timer = NULL
 
static uint64_t reconnect_timeout_us = (1000000)
 
static esp_event_handler_instance_t wifi_event_ctx = NULL
 
static esp_event_handler_instance_t ip_event_ctx = NULL
 

Detailed Description

Implementation of the wifi manager.

  • Initializes WiFi with provided configurations as in station mode
  • Registers to events to manage WiFi connection state with a lock for mutual access
  • Creates one-shot timer to reconnect with an exponential backoff timing

Macro Definition Documentation

◆ WIFI_MANAGE_MAXIMUM_RECONNECT_TIMEOUT_US

#define WIFI_MANAGE_MAXIMUM_RECONNECT_TIMEOUT_US    (60000000UL)

Maximum reconnect timeout in micro seconds

◆ WIFI_MANAGE_MINIMUM_RECONNECT_TIMEOUT_US

#define WIFI_MANAGE_MINIMUM_RECONNECT_TIMEOUT_US    (1000000)

Minimum reconnect timeout in micro seconds \

Function Documentation

◆ reconnect_timer_callback()

static void reconnect_timer_callback ( void *  arg_void_ptr)
static

Callback to reconnect on one-shot timer.

Parameters
arg_void_ptrVoid pointer to callback argument
Here is the call graph for this function:
Here is the caller graph for this function:

◆ wifi_event_handler()

static void wifi_event_handler ( void *  args_void_ptr,
esp_event_base_t  event_base,
int32_t  event_id,
void *  event_data_void_ptr 
)
static

WiFi event handler.

  • Handles
    • WIFI_EVENT_STA_START
    • WIFI_EVENT_STA_DISCONNECTED
    • IP_EVENT_STA_GOT_IP
Parameters
args_void_ptrVoid pointer to handler arguments
event_baseESP event base
event_idEvent id
event_data_void_ptrVoid pointer to event data

< Minimum reconnect timeout in micro seconds

Here is the call graph for this function:
Here is the caller graph for this function:

◆ wifi_manager_deinit()

void wifi_manager_deinit ( void  )

De-Initialize Wi-Fi manager.

Stops and deletes timer and stops Wi-Fi then deinitialize Wi-Fi unregister events

Here is the caller graph for this function:

◆ wifi_manager_get_state()

wifi_state_t wifi_manager_get_state ( void  )

Get Wi-Fi current connection state.

Returns
wifi_state_t Current connection state
Here is the caller graph for this function:

◆ wifi_manager_init()

esp_err_t wifi_manager_init ( wifi_config_t *  wifi_config_ptr)

Initialize Wi-Fi manager.

Initialize different components needed by Wi-Fi and register events to manage Wi-Fi connection state and setup a one-shot timer to trigger on disconnect event

Parameters
wifi_config_ptrPointer to Wi-Fi configuration structure
Returns
esp_err_t
Return values
ESP_OKInitialize success
BLACKBOX_FAILInitialize fail
ESP_ERR_INVALID_ARGProvided invalid argument(s)
ESP_ERR_NO_MEMNo available memory for resource allocation
anyAny error from down layers propagates upward
Here is the call graph for this function:
Here is the caller graph for this function:

◆ wifi_manager_set_state_helper()

static void wifi_manager_set_state_helper ( const wifi_state_t  state)
static

Set current WiFi state.

Parameters
stateWiFi state
Here is the caller graph for this function:

◆ wifi_manager_trigger_reconnect()

void wifi_manager_trigger_reconnect ( void  )

Trigger Wi-Fi re-connect.

Trigger Wi-Fi re-connect using one-shot timer with an exponential back-off increment to avoid network congestion

< Maximum reconnect timeout in micro seconds

< Maximum reconnect timeout in micro seconds

Here is the caller graph for this function:

Variable Documentation

◆ ip_event_ctx

esp_event_handler_instance_t ip_event_ctx = NULL
static

WiFi event handler instance

◆ reconnect_timeout_us

uint64_t reconnect_timeout_us = (1000000)
static

WiFi Manager reconnect current timeout Minimum reconnect timeout in micro seconds

◆ reconnect_timer

esp_timer_handle_t reconnect_timer = NULL
static

WiFi Manager reconnect timer handle

◆ state_lock

SemaphoreHandle_t state_lock = NULL
static

WiFi Manager state lock

◆ TAG

const char* TAG = "WIFI_MANAGER"
static

WiFi Manager TAG name

◆ wifi_event_ctx

esp_event_handler_instance_t wifi_event_ctx = NULL
static

WiFi event context instance

◆ wifi_manager_state

wifi_state_t wifi_manager_state = WIFI_STATE_IDLE
static

WiFi Manager state