mirror of
https://github.com/Lurkars/esp-ena.git
synced 2026-05-08 20:10:37 +02:00
support for ena-eke-proxy, improved interface
This commit is contained in:
@@ -33,9 +33,41 @@
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
static bool initialized = false;
|
||||
static bool connecting = false;
|
||||
|
||||
static wifi_ap_record_t current_wifi_ap;
|
||||
|
||||
static wifi_connected_callback wifi_callback;
|
||||
|
||||
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
||||
{
|
||||
esp_wifi_connect();
|
||||
}
|
||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
||||
{
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
connecting = false;
|
||||
}
|
||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
||||
{
|
||||
|
||||
ESP_LOGD(WIFI_LOG, "Got Ip!");
|
||||
if (wifi_callback != NULL)
|
||||
{
|
||||
(*wifi_callback)();
|
||||
}
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
connecting = false;
|
||||
heap_caps_check_integrity_all(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGD(WIFI_LOG, "other wifi event: event base %s, event id %d", event_base, event_id);
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_controller_init(void)
|
||||
{
|
||||
// init NVS for WIFI
|
||||
@@ -69,6 +101,7 @@ void wifi_controller_scan(wifi_ap_record_t *ap_info, uint16_t *ap_count)
|
||||
|
||||
uint16_t number = 10;
|
||||
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_stop());
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true));
|
||||
@@ -77,34 +110,24 @@ void wifi_controller_scan(wifi_ap_record_t *ap_info, uint16_t *ap_count)
|
||||
ESP_ERROR_CHECK(esp_wifi_stop());
|
||||
}
|
||||
|
||||
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||
esp_err_t wifi_controller_connect(wifi_config_t wifi_config, wifi_connected_callback callback)
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
||||
if (connecting)
|
||||
{
|
||||
esp_wifi_connect();
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
||||
{
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
}
|
||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
||||
{
|
||||
ESP_LOGD(WIFI_LOG, "got IP");
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
heap_caps_check_integrity_all(true);
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t wifi_controller_connect(wifi_config_t wifi_config)
|
||||
{
|
||||
connecting = true;
|
||||
wifi_callback = callback;
|
||||
if (!initialized)
|
||||
{
|
||||
wifi_controller_init();
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_stop());
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
@@ -116,6 +139,7 @@ esp_err_t wifi_controller_connect(wifi_config_t wifi_config)
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
|
||||
vEventGroupDelete(s_wifi_event_group);
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT)
|
||||
@@ -135,16 +159,25 @@ esp_err_t wifi_controller_connect(wifi_config_t wifi_config)
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t wifi_controller_reconnect(void)
|
||||
esp_err_t wifi_controller_reconnect(wifi_connected_callback callback)
|
||||
{
|
||||
if (connecting)
|
||||
{
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
connecting = true;
|
||||
|
||||
wifi_callback = callback;
|
||||
if (!initialized)
|
||||
{
|
||||
wifi_controller_init();
|
||||
}
|
||||
|
||||
esp_wifi_stop();
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
@@ -155,6 +188,7 @@ esp_err_t wifi_controller_reconnect(void)
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
|
||||
vEventGroupDelete(s_wifi_event_group);
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT)
|
||||
@@ -174,6 +208,15 @@ esp_err_t wifi_controller_reconnect(void)
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t wifi_controller_disconnect(void)
|
||||
{
|
||||
if (esp_wifi_sta_get_ap_info(¤t_wifi_ap) == ESP_OK)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_wifi_disconnect());
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
wifi_ap_record_t *wifi_controller_connection(void)
|
||||
{
|
||||
if (esp_wifi_sta_get_ap_info(¤t_wifi_ap) == ESP_OK)
|
||||
|
||||
@@ -19,6 +19,11 @@
|
||||
|
||||
#define WIFI_LOG "wifi-controller" // TAG for Logging
|
||||
|
||||
/**
|
||||
* @brief callback function after successfull wifi connect
|
||||
*/
|
||||
typedef void (*wifi_connected_callback)(void);
|
||||
|
||||
/**
|
||||
* @brief scan for WiFis
|
||||
*
|
||||
@@ -31,22 +36,31 @@ void wifi_controller_scan(wifi_ap_record_t ap_info[], uint16_t *ap_count);
|
||||
* @brief connect to wifi ap
|
||||
*
|
||||
* @param[in] wifi_config config of wifi to connect
|
||||
* @param[in] callback callback function after connection
|
||||
*
|
||||
* @return
|
||||
* esp_err_t connection status
|
||||
*/
|
||||
esp_err_t wifi_controller_connect(wifi_config_t wifi_config);
|
||||
esp_err_t wifi_controller_connect(wifi_config_t wifi_config, wifi_connected_callback callback);
|
||||
|
||||
/**
|
||||
* @brief reconnect to previous wifi
|
||||
*
|
||||
* @param[in] callback callback function after connection
|
||||
*
|
||||
* @return
|
||||
* esp_err_t connection status
|
||||
*/
|
||||
esp_err_t wifi_controller_reconnect(void);
|
||||
esp_err_t wifi_controller_reconnect(wifi_connected_callback callback);
|
||||
|
||||
/**
|
||||
* @brief reconnect to previous wifi
|
||||
* @brief disconnect wifi
|
||||
*
|
||||
*/
|
||||
esp_err_t wifi_controller_disconnect(void);
|
||||
|
||||
/**
|
||||
* @brief get current wifi connection
|
||||
*
|
||||
* @return
|
||||
* wifi_ap_record_t pointer to current wifi connection, NULL if not connected
|
||||
|
||||
Reference in New Issue
Block a user