0
0
Fork 0

feat: Try to connect to wap in the wifi list

This commit is contained in:
windowsair 2022-01-18 20:11:31 +08:00
parent ef18be8c99
commit 79e6de3828
5 changed files with 163 additions and 112 deletions

View File

@ -1,4 +1,6 @@
set(COMPONENT_ADD_INCLUDEDIRS "${PROJECT_PATH}") set(COMPONENT_ADD_INCLUDEDIRS "${PROJECT_PATH}")
set(COMPONENT_SRCS "main.c timer.c tcp_server.c usbip_server.c DAP_handle.c kcp_server.c tcp_netconn.c uart_bridge.c") set(COMPONENT_SRCS
main.c timer.c tcp_server.c usbip_server.c DAP_handle.c
kcp_server.c tcp_netconn.c uart_bridge.c wifi_handle.c)
register_component() register_component()

View File

@ -16,6 +16,7 @@
#include "main/uart_bridge.h" #include "main/uart_bridge.h"
#include "main/timer.h" #include "main/timer.h"
#include "main/wifi_configuration.h" #include "main/wifi_configuration.h"
#include "main/wifi_handle.h"
#include "components/corsacOTA/src/corsacOTA.h" #include "components/corsacOTA/src/corsacOTA.h"
@ -41,110 +42,6 @@ extern void SWO_Thread();
TaskHandle_t kDAPTaskHandle = NULL; TaskHandle_t kDAPTaskHandle = NULL;
/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;
const int IPV4_GOTIP_BIT = BIT0;
#ifdef CONFIG_EXAMPLE_IPV6
const int IPV6_GOTIP_BIT = BIT1;
#endif
static esp_err_t event_handler(void *ctx, system_event_t *event) {
/* For accessing reason codes in case of disconnection */
system_event_info_t *info = &event->event_info;
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
os_printf("SYSTEM_EVENT_STA_START\r\n");
break;
case SYSTEM_EVENT_STA_CONNECTED:
#ifdef CONFIG_EXAMPLE_IPV6
/* enable ipv6 */
tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
#endif
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT);
os_printf("SYSTEM EVENT STA GOT IP : %s\r\n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
os_printf("Disconnect reason : %d\r\n", info->disconnected.reason);
if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) {
/*Switch to 802.11 bgn mode */
esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
}
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT);
#ifdef CONFIG_EXAMPLE_IPV6
xEventGroupClearBits(wifi_event_group, IPV6_GOTIP_BIT);
#endif
#if (USE_UART_BRIDGE == 1)
uart_bridge_close();
#endif
break;
case SYSTEM_EVENT_AP_STA_GOT_IP6:
#ifdef CONFIG_EXAMPLE_IPV6
xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT);
os_printf("SYSTEM_EVENT_STA_GOT_IP6\r\n");
char *ip6 = ip6addr_ntoa(&event->event_info.got_ip6.ip6_info.ip);
os_printf("IPv6: %s\r\n", ip6);
#endif
default:
break;
}
return ESP_OK;
}
static void initialise_wifi(void) {
tcpip_adapter_init();
#if (USE_STATIC_IP == 1)
tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_STA);
tcpip_adapter_ip_info_t ip_info;
#define MY_IP4_ADDR(...) IP4_ADDR(__VA_ARGS__)
MY_IP4_ADDR(&ip_info.ip, DAP_IP_ADDRESS);
MY_IP4_ADDR(&ip_info.gw, DAP_IP_GATEWAY);
MY_IP4_ADDR(&ip_info.netmask, DAP_IP_NETMASK);
#undef MY_IP4_ADDR
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info);
#endif // (USE_STATIC_IP == 1)
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
},
};
os_printf("Setting WiFi configuration SSID %s...\r\n", wifi_config.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
ESP_ERROR_CHECK(esp_wifi_start());
}
static void wait_for_ip() {
#ifdef CONFIG_EXAMPLE_IPV6
uint32_t bits = IPV4_GOTIP_BIT | IPV6_GOTIP_BIT;
#else
uint32_t bits = IPV4_GOTIP_BIT;
#endif
os_printf("Waiting for AP connection...\r\n");
xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY);
os_printf("Connected to AP\r\n");
}
static const char *MDNS_TAG = "server_common"; static const char *MDNS_TAG = "server_common";
@ -197,9 +94,7 @@ void app_main() {
#if (USE_UART_BRIDGE == 1) #if (USE_UART_BRIDGE == 1)
uart_bridge_init(); uart_bridge_init();
#endif #endif
wifi_init();
initialise_wifi();
wait_for_ip();
DAP_Setup(); DAP_Setup();
timer_init(); timer_init();
@ -223,9 +118,7 @@ void app_main() {
#endif #endif
// Specify the usbip server task // Specify the usbip server task
#if (USE_KCP == 1) #if (USE_TCP_NETCONN == 1)
xTaskCreate(kcp_server_task, "kcp_server", 4096, NULL, 7, NULL);
#elif (USE_TCP_NETCONN == 1)
xTaskCreate(tcp_netconn_task, "tcp_server", 4096, NULL, 14, NULL); xTaskCreate(tcp_netconn_task, "tcp_server", 4096, NULL, 14, NULL);
#else // BSD style #else // BSD style
xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 14, NULL); xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 14, NULL);

View File

@ -13,7 +13,19 @@
#define WIFI_SSID "DAP" #define WIFI_SSID "DAP"
#define WIFI_PASS "12345678" #define WIFI_PASS "12345678"
#define USE_MDNS 1 static struct {
const char *ssid;
const char *password;
} wifi_list[] __attribute__((unused)) = {
{.ssid = "OTA", .password = "12345678"},
{.ssid = "DAP", .password = "12345678"},
// Add your WAP like this:
// {.ssid = "your ssid", .password = "your password"},
};
#define WIFI_LIST_SIZE (sizeof(wifi_list) / sizeof(wifi_list[0]))
#define USE_MDNS 1
// Use the address "dap.local" to access the device // Use the address "dap.local" to access the device
#define MDNS_HOSTNAME "dap" #define MDNS_HOSTNAME "dap"
#define MDNS_INSTANCE "DAP mDNS" #define MDNS_INSTANCE "DAP mDNS"

138
main/wifi_handle.c Normal file
View File

@ -0,0 +1,138 @@
#include <string.h>
#include <stdint.h>
#include <sys/param.h>
#include "main/wifi_configuration.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
// #include "nvs_flash.h"
// #include "lwip/err.h"
// #include "lwip/sockets.h"
// #include "lwip/sys.h"
// #include <lwip/netdb.h>
static EventGroupHandle_t wifi_event_group;
static int ssid_index = 0;
const int IPV4_GOTIP_BIT = BIT0;
#ifdef CONFIG_EXAMPLE_IPV6
const int IPV6_GOTIP_BIT = BIT1;
#endif
static void ssid_change();
static esp_err_t event_handler(void *ctx, system_event_t *event) {
/* For accessing reason codes in case of disconnection */
system_event_info_t *info = &event->event_info;
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_CONNECTED:
#ifdef CONFIG_EXAMPLE_IPV6
/* enable ipv6 */
tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
#endif
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT);
os_printf("SYSTEM EVENT STA GOT IP : %s\r\n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
os_printf("Disconnect reason : %d\r\n", (int)info->disconnected.reason);
if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) {
/*Switch to 802.11 bgn mode */
esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
}
ssid_change();
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT);
#ifdef CONFIG_EXAMPLE_IPV6
xEventGroupClearBits(wifi_event_group, IPV6_GOTIP_BIT);
#endif
#if (USE_UART_BRIDGE == 1)
uart_bridge_close();
#endif
break;
case SYSTEM_EVENT_AP_STA_GOT_IP6:
#ifdef CONFIG_EXAMPLE_IPV6
xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT);
os_printf("SYSTEM_EVENT_STA_GOT_IP6\r\n");
char *ip6 = ip6addr_ntoa(&event->event_info.got_ip6.ip6_info.ip);
os_printf("IPv6: %s\r\n", ip6);
#endif
default:
break;
}
return ESP_OK;
}
static void ssid_change() {
wifi_config_t wifi_config;
if (ssid_index > WIFI_LIST_SIZE - 1) {
ssid_index = 0;
}
strcpy((char *)wifi_config.sta.ssid, wifi_list[ssid_index].ssid);
strcpy((char *)wifi_config.sta.password, wifi_list[ssid_index].password);
ssid_index++;
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
}
static void wait_for_ip() {
#ifdef CONFIG_EXAMPLE_IPV6
uint32_t bits = IPV4_GOTIP_BIT | IPV6_GOTIP_BIT;
#else
uint32_t bits = IPV4_GOTIP_BIT;
#endif
os_printf("Waiting for AP connection...\r\n");
xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY);
os_printf("Connected to AP\r\n");
}
void wifi_init(void) {
tcpip_adapter_init();
#if (USE_STATIC_IP == 1)
tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_STA);
tcpip_adapter_ip_info_t ip_info;
#define MY_IP4_ADDR(...) IP4_ADDR(__VA_ARGS__)
MY_IP4_ADDR(&ip_info.ip, DAP_IP_ADDRESS);
MY_IP4_ADDR(&ip_info.gw, DAP_IP_GATEWAY);
MY_IP4_ADDR(&ip_info.netmask, DAP_IP_NETMASK);
#undef MY_IP4_ADDR
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info);
#endif // (USE_STATIC_IP == 1)
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
// os_printf("Setting WiFi configuration SSID %s...\r\n", wifi_config.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
ssid_change();
ESP_ERROR_CHECK(esp_wifi_start());
wait_for_ip();
}

6
main/wifi_handle.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _WIFI_HANDLE_H_
#define _WIFI_HANDLE_H_
void wifi_init();
#endif