Network: use new config
This commit is contained in:
parent
a7f30853ec
commit
3e336213f3
161
main/network.c
161
main/network.c
|
@ -1,4 +1,5 @@
|
||||||
#include "nvs.h"
|
#include "nvs.h"
|
||||||
|
#include "nvs-config.h"
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include <esp_wifi.h>
|
#include <esp_wifi.h>
|
||||||
|
@ -27,18 +28,14 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define WIFI_MAXIMUM_RETRY 3
|
#define WIFI_MAXIMUM_RETRY 3
|
||||||
|
|
||||||
// TODO remove global vars
|
|
||||||
static EventGroupHandle_t wifi_event_group;
|
|
||||||
#define WIFI_CONNECTED_BIT BIT0
|
#define WIFI_CONNECTED_BIT BIT0
|
||||||
#define WIFI_FAIL_BIT BIT1
|
#define WIFI_FAIL_BIT BIT1
|
||||||
|
|
||||||
static WIFIMode wifi_mode = WIFIModeSTA;
|
static WiFiMode wifi_mode = WiFiModeSTA;
|
||||||
static int s_retry_num = 0;
|
|
||||||
|
|
||||||
uint32_t network_get_ip(void) {
|
uint32_t network_get_ip(void) {
|
||||||
tcpip_adapter_ip_info_t ip_info;
|
tcpip_adapter_ip_info_t ip_info;
|
||||||
if(wifi_mode == WIFIModeSTA) {
|
if(wifi_mode == WiFiModeSTA) {
|
||||||
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info);
|
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info);
|
||||||
} else {
|
} else {
|
||||||
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip_info);
|
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip_info);
|
||||||
|
@ -52,19 +49,10 @@ static void
|
||||||
if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||||
esp_wifi_connect();
|
esp_wifi_connect();
|
||||||
} else if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
} else if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||||
if(s_retry_num < WIFI_MAXIMUM_RETRY) {
|
esp_wifi_connect();
|
||||||
esp_wifi_connect();
|
|
||||||
s_retry_num++;
|
|
||||||
ESP_LOGI(TAG, "retry to connect to the AP");
|
|
||||||
} else {
|
|
||||||
xEventGroupSetBits(wifi_event_group, WIFI_FAIL_BIT);
|
|
||||||
}
|
|
||||||
ESP_LOGI(TAG, "connect to the AP fail");
|
|
||||||
} else if(event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
} else if(event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||||
ip_event_got_ip_t* event = (ip_event_got_ip_t*)event_data;
|
ip_event_got_ip_t* event = (ip_event_got_ip_t*)event_data;
|
||||||
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||||
s_retry_num = 0;
|
|
||||||
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +79,7 @@ static void network_start_ap(mstring_t* ap_ssid, mstring_t* ap_pass) {
|
||||||
|
|
||||||
wifi_config_t wifi_config;
|
wifi_config_t wifi_config;
|
||||||
memset(&wifi_config, 0, sizeof(wifi_config_t));
|
memset(&wifi_config, 0, sizeof(wifi_config_t));
|
||||||
wifi_config.ap.ssid_len = strlen("");
|
wifi_config.ap.ssid_len = 0;
|
||||||
wifi_config.ap.channel = ESP_WIFI_CHANNEL;
|
wifi_config.ap.channel = ESP_WIFI_CHANNEL;
|
||||||
wifi_config.ap.max_connection = 4;
|
wifi_config.ap.max_connection = 4;
|
||||||
wifi_config.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;
|
wifi_config.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;
|
||||||
|
@ -130,9 +118,8 @@ static bool network_connect_ap(mstring_t* ap_ssid, mstring_t* ap_pass) {
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
ESP_LOGI(TAG, "init connect to AP");
|
ESP_LOGI(TAG, "init connect to AP");
|
||||||
|
esp_netif_create_default_wifi_sta();
|
||||||
|
|
||||||
wifi_event_group = xEventGroupCreate();
|
|
||||||
esp_netif_t* netif = esp_netif_create_default_wifi_sta();
|
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||||
ESP_ERROR_CHECK(
|
ESP_ERROR_CHECK(
|
||||||
|
@ -161,114 +148,10 @@ static bool network_connect_ap(mstring_t* ap_ssid, mstring_t* ap_pass) {
|
||||||
mstring_get_cstr(ap_ssid),
|
mstring_get_cstr(ap_ssid),
|
||||||
mstring_get_cstr(ap_pass));
|
mstring_get_cstr(ap_pass));
|
||||||
|
|
||||||
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
|
|
||||||
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
|
|
||||||
EventBits_t bits = xEventGroupWaitBits(
|
|
||||||
wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
|
||||||
|
|
||||||
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
|
|
||||||
* happened. */
|
|
||||||
if(bits & WIFI_CONNECTED_BIT) {
|
|
||||||
ESP_LOGI(TAG, "connected to AP");
|
|
||||||
result = true;
|
|
||||||
} else if(bits & WIFI_FAIL_BIT) {
|
|
||||||
ESP_LOGI(TAG, "failed to connect to AP");
|
|
||||||
} else {
|
|
||||||
ESP_LOGE(TAG, "unexpected event while connecting to AP");
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(
|
|
||||||
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &sta_event_handler));
|
|
||||||
ESP_ERROR_CHECK(
|
|
||||||
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &sta_event_handler));
|
|
||||||
vEventGroupDelete(wifi_event_group);
|
|
||||||
|
|
||||||
if(!result) {
|
|
||||||
ESP_LOGI(TAG, "wifi deinit");
|
|
||||||
if(esp_wifi_disconnect() == ESP_FAIL) {
|
|
||||||
ESP_LOGE(TAG, "cannot disconnect wifi, internal WIFI error");
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_stop());
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_deinit());
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(netif));
|
|
||||||
esp_netif_destroy(netif);
|
|
||||||
ESP_LOGI(TAG, "wifi deinit done");
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
WIFIMode network_init(void) {
|
void network_post_init(void) {
|
||||||
mstring_t* ap_mode = mstring_alloc();
|
|
||||||
mstring_t* ap_ssid = mstring_alloc();
|
|
||||||
mstring_t* ap_pass = mstring_alloc();
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
||||||
ESP_ERROR_CHECK(esp_netif_init());
|
|
||||||
|
|
||||||
if(nvs_load_string("ap_mode", ap_mode) != ESP_OK) {
|
|
||||||
ESP_LOGW(TAG, "cannot load [ap_mode]");
|
|
||||||
mstring_set(ap_mode, ESP_WIFI_MODE_AP);
|
|
||||||
nvs_save_string("ap_mode", ap_mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(nvs_load_string("ap_ssid", ap_ssid) != ESP_OK) {
|
|
||||||
ESP_LOGW(TAG, "cannot load [ap_ssid]");
|
|
||||||
mstring_set(ap_ssid, ESP_WIFI_SSID);
|
|
||||||
nvs_save_string("ap_ssid", ap_ssid);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(nvs_load_string("ap_pass", ap_pass) != ESP_OK) {
|
|
||||||
ESP_LOGW(TAG, "cannot load [ap_pass]");
|
|
||||||
mstring_set(ap_pass, ESP_WIFI_PASS);
|
|
||||||
nvs_save_string("ap_pass", ap_pass);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(mstring_size(ap_pass) < 8) {
|
|
||||||
ESP_LOGW(TAG, "too short [ap_pass]");
|
|
||||||
mstring_set(ap_ssid, ESP_WIFI_SSID);
|
|
||||||
nvs_save_string("ap_ssid", ap_ssid);
|
|
||||||
mstring_set(ap_pass, ESP_WIFI_PASS);
|
|
||||||
nvs_save_string("ap_pass", ap_pass);
|
|
||||||
mstring_set(ap_mode, ESP_WIFI_MODE_AP);
|
|
||||||
nvs_save_string("ap_mode", ap_mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(mstring_size(ap_ssid) < 1 || mstring_size(ap_ssid) > 32) {
|
|
||||||
ESP_LOGW(TAG, "too short or too long [ap_ssid]");
|
|
||||||
mstring_set(ap_ssid, ESP_WIFI_SSID);
|
|
||||||
nvs_save_string("ap_ssid", ap_ssid);
|
|
||||||
mstring_set(ap_pass, ESP_WIFI_PASS);
|
|
||||||
nvs_save_string("ap_pass", ap_pass);
|
|
||||||
mstring_set(ap_mode, ESP_WIFI_MODE_AP);
|
|
||||||
nvs_save_string("ap_mode", ap_mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(strcmp(mstring_get_cstr(ap_mode), ESP_WIFI_MODE_STA) == 0) {
|
|
||||||
if(!network_connect_ap(ap_ssid, ap_pass)) {
|
|
||||||
mstring_set(ap_ssid, ESP_WIFI_SSID);
|
|
||||||
nvs_save_string("ap_ssid", ap_ssid);
|
|
||||||
mstring_set(ap_pass, ESP_WIFI_PASS);
|
|
||||||
nvs_save_string("ap_pass", ap_pass);
|
|
||||||
mstring_set(ap_mode, ESP_WIFI_MODE_AP);
|
|
||||||
nvs_save_string("ap_mode", ap_mode);
|
|
||||||
|
|
||||||
ESP_LOGW(TAG, "cannot connect to AP");
|
|
||||||
network_start_ap(ap_ssid, ap_pass);
|
|
||||||
wifi_mode = WIFIModeAP;
|
|
||||||
} else {
|
|
||||||
wifi_mode = WIFIModeSTA;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
network_start_ap(ap_ssid, ap_pass);
|
|
||||||
wifi_mode = WIFIModeAP;
|
|
||||||
}
|
|
||||||
|
|
||||||
mstring_free(ap_ssid);
|
|
||||||
mstring_free(ap_pass);
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "init mdns");
|
ESP_LOGI(TAG, "init mdns");
|
||||||
mdns_init();
|
mdns_init();
|
||||||
mdns_hostname_set(MDNS_HOST_NAME);
|
mdns_hostname_set(MDNS_HOST_NAME);
|
||||||
|
@ -290,10 +173,38 @@ WIFIMode network_init(void) {
|
||||||
netbiosns_init();
|
netbiosns_init();
|
||||||
netbiosns_set_name(MDNS_HOST_NAME);
|
netbiosns_set_name(MDNS_HOST_NAME);
|
||||||
ESP_LOGI(TAG, "init netbios done");
|
ESP_LOGI(TAG, "init netbios done");
|
||||||
|
}
|
||||||
|
|
||||||
|
WiFiMode network_init(void) {
|
||||||
|
mstring_t* ssid = mstring_alloc();
|
||||||
|
mstring_t* pass = mstring_alloc();
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
ESP_ERROR_CHECK(esp_netif_init());
|
||||||
|
|
||||||
|
nvs_config_get_wifi_mode(&wifi_mode);
|
||||||
|
|
||||||
|
switch(wifi_mode) {
|
||||||
|
case WiFiModeAP:
|
||||||
|
nvs_config_get_ap_ssid(ssid);
|
||||||
|
nvs_config_get_ap_pass(pass);
|
||||||
|
network_start_ap(ssid, pass);
|
||||||
|
break;
|
||||||
|
case WiFiModeSTA:
|
||||||
|
nvs_config_get_sta_ssid(ssid);
|
||||||
|
nvs_config_get_sta_pass(pass);
|
||||||
|
network_connect_ap(ssid, pass);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
mstring_free(ssid);
|
||||||
|
mstring_free(pass);
|
||||||
|
|
||||||
|
network_post_init();
|
||||||
|
|
||||||
return wifi_mode;
|
return wifi_mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
WIFIMode network_get_mode(void) {
|
WiFiMode network_get_mode(void) {
|
||||||
return wifi_mode;
|
return wifi_mode;
|
||||||
}
|
}
|
|
@ -8,26 +8,19 @@
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include "nvs-config.h"
|
||||||
#define ESP_WIFI_MODE_STA "STA"
|
|
||||||
#define ESP_WIFI_MODE_AP "AP"
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
WIFIModeSTA,
|
|
||||||
WIFIModeAP,
|
|
||||||
} WIFIMode;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init network module
|
* Init network module
|
||||||
* @return WIFIMode
|
* @return WiFiMode
|
||||||
*/
|
*/
|
||||||
WIFIMode network_init(void);
|
WiFiMode network_init(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns current WIFI mode
|
* Returns current WIFI mode
|
||||||
* @return WIFIMode
|
* @return WiFiMode
|
||||||
*/
|
*/
|
||||||
WIFIMode network_get_mode(void);
|
WiFiMode network_get_mode(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get current IP address
|
* Get current IP address
|
||||||
|
|
Loading…
Reference in New Issue