reformat wifi
This commit is contained in:
parent
24aa3bed7f
commit
ea55a6f576
|
@ -2,4 +2,6 @@
|
||||||
build/
|
build/
|
||||||
tmp/
|
tmp/
|
||||||
.history/
|
.history/
|
||||||
sdkconfig.old
|
sdkconfig.old
|
||||||
|
.idea/
|
||||||
|
dependencies.lock
|
|
@ -1,29 +0,0 @@
|
||||||
language: c
|
|
||||||
sudo: required
|
|
||||||
before_install:
|
|
||||||
- sudo apt update
|
|
||||||
- sudo apt install -y gcc git wget make libncurses-dev flex bison python python-serial ninja-build
|
|
||||||
- wget https://dl.espressif.com/dl/xtensa-lx106-elf-linux64-1.22.0-100-ge567ec7-5.2.0.tar.gz
|
|
||||||
|
|
||||||
install:
|
|
||||||
- tar -xzf ./xtensa-lx106-elf-linux64-1.22.0-100-ge567ec7-5.2.0.tar.gz
|
|
||||||
- wget https://github.com/espressif/ESP8266_RTOS_SDK/releases/download/v3.3-rc1/ESP8266_RTOS_SDK-v3.3-rc1.zip
|
|
||||||
- unzip ESP8266_RTOS_SDK-v3.3-rc1.zip
|
|
||||||
- python -m pip install --user -r ./ESP8266_RTOS_SDK/requirements.txt
|
|
||||||
|
|
||||||
before_script:
|
|
||||||
- export IDF_PATH=$PWD/ESP8266_RTOS_SDK
|
|
||||||
- export PATH="$PATH:$PWD/xtensa-lx106-elf/bin"
|
|
||||||
|
|
||||||
script:
|
|
||||||
- python ./idf.py fullclean
|
|
||||||
- python ./idf.py build
|
|
||||||
|
|
||||||
deploy:
|
|
||||||
provider: releases
|
|
||||||
api_key: ${GITHUB_TOKEN}
|
|
||||||
file:
|
|
||||||
- ./build/esp8266_dap.bin
|
|
||||||
skip_cleanup: true
|
|
||||||
on:
|
|
||||||
tags: true
|
|
|
@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.5)
|
||||||
add_compile_options (-fdiagnostics-color=always)
|
add_compile_options (-fdiagnostics-color=always)
|
||||||
|
|
||||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
set(EXTRA_COMPONENT_DIRS project_components)
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
project(wireless_esp_dap)
|
project(wireless_tools_esp32)
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
Subproject commit ed19d9cccf880d88d34004607472dd8977dcb1b6
|
|
|
@ -32,7 +32,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "main/dap_configuration.h"
|
#include "main/dap_configuration.h"
|
||||||
#include "main/wifi_configuration.h"
|
|
||||||
|
|
||||||
#include "cmsis-dap/include/cmsis_compiler.h"
|
#include "cmsis-dap/include/cmsis_compiler.h"
|
||||||
#include "cmsis-dap/include/gpio_op.h"
|
#include "cmsis-dap/include/gpio_op.h"
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
Subproject commit eecc603dc5db13aecd4253804d57f9a0a285bffa
|
|
60
main/main.c
60
main/main.c
|
@ -1,57 +1,25 @@
|
||||||
#include <string.h>
|
#include <freertos/FreeRTOS.h>
|
||||||
#include <stdint.h>
|
#include <freertos/task.h>
|
||||||
|
#include <esp_event.h>
|
||||||
|
#include <esp_netif.h>
|
||||||
|
|
||||||
#include "tcp_server.h"
|
#include "tcp_server.h"
|
||||||
#include "main/wifi_configuration.h"
|
|
||||||
#include "main/wifi_handle.h"
|
|
||||||
#include "cmsis-dap/include/DAP.h"
|
#include "cmsis-dap/include/DAP.h"
|
||||||
#include "DAP_handle.h"
|
#include "DAP_handle.h"
|
||||||
|
#include "wt_mdns_config.h"
|
||||||
|
#include "wt_storage.h"
|
||||||
|
#include "wifi_manager.h"
|
||||||
|
|
||||||
#include "freertos/FreeRTOS.h"
|
void app_main()
|
||||||
#include "freertos/task.h"
|
{
|
||||||
#include "esp_system.h"
|
wt_storage_init();
|
||||||
#include "esp_log.h"
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
#include "nvs_flash.h"
|
ESP_ERROR_CHECK(esp_netif_init());
|
||||||
|
|
||||||
#include "mdns.h"
|
wifi_manager_init();
|
||||||
|
|
||||||
static const char *MDNS_TAG = "server_common";
|
|
||||||
|
|
||||||
void mdns_setup() {
|
|
||||||
// initialize mDNS
|
|
||||||
int ret;
|
|
||||||
ret = mdns_init();
|
|
||||||
if (ret != ESP_OK) {
|
|
||||||
ESP_LOGW(MDNS_TAG, "mDNS initialize failed:%d", ret);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// set mDNS hostname
|
|
||||||
ret = mdns_hostname_set(MDNS_HOSTNAME);
|
|
||||||
if (ret != ESP_OK) {
|
|
||||||
ESP_LOGW(MDNS_TAG, "mDNS set hostname failed:%d", ret);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ESP_LOGI(MDNS_TAG, "mDNS hostname set to: [%s]", MDNS_HOSTNAME);
|
|
||||||
|
|
||||||
// set default mDNS instance name
|
|
||||||
ret = mdns_instance_name_set(MDNS_INSTANCE);
|
|
||||||
if (ret != ESP_OK) {
|
|
||||||
ESP_LOGW(MDNS_TAG, "mDNS set instance name failed:%d", ret);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ESP_LOGI(MDNS_TAG, "mDNS instance name set to: [%s]", MDNS_INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void app_main() {
|
|
||||||
ESP_ERROR_CHECK(nvs_flash_init());
|
|
||||||
|
|
||||||
wifi_init();
|
|
||||||
DAP_Setup();
|
DAP_Setup();
|
||||||
|
|
||||||
#if (USE_MDNS == 1)
|
wt_mdns_init();
|
||||||
mdns_setup();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 14, NULL);
|
xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 14, NULL);
|
||||||
|
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
/**
|
|
||||||
* @file wifi_configuration.h
|
|
||||||
* @brief Fill in your wifi configuration information here.
|
|
||||||
* @version 0.1
|
|
||||||
* @date 2020-01-22
|
|
||||||
*
|
|
||||||
* @copyright Copyright (c) 2020
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
#ifndef __WIFI_CONFIGURATION__
|
|
||||||
#define __WIFI_CONFIGURATION__
|
|
||||||
|
|
||||||
|
|
||||||
static struct {
|
|
||||||
const char *ssid;
|
|
||||||
const char *password;
|
|
||||||
} wifi_list[] __attribute__((unused)) = {
|
|
||||||
{.ssid = "MOWiFi_2.4G_A7F0", .password = "00112288"},
|
|
||||||
{.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
|
|
||||||
#define MDNS_HOSTNAME "dap"
|
|
||||||
#define MDNS_INSTANCE "DAP mDNS"
|
|
||||||
//
|
|
||||||
|
|
||||||
#define USE_STATIC_IP 0
|
|
||||||
// If you don't want to specify the ip configuration, then ignore the following items.
|
|
||||||
#define DAP_IP_ADDRESS 192, 168, 137, 123
|
|
||||||
#define DAP_IP_GATEWAY 192, 168, 137, 1
|
|
||||||
#define DAP_IP_NETMASK 255, 255, 255, 0
|
|
||||||
|
|
||||||
|
|
||||||
#define PORT 3240
|
|
||||||
#define CONFIG_EXAMPLE_IPV4 1
|
|
||||||
#define MTU_SIZE 1500
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,165 +0,0 @@
|
||||||
#include <string.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <sys/param.h>
|
|
||||||
#include <lwip/ip4_addr.h>
|
|
||||||
|
|
||||||
#ifdef CONFIG_EXAMPLE_IPV6
|
|
||||||
#include <lwip/ip6_addr.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#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.h"
|
|
||||||
#include "esp_log.h"
|
|
||||||
|
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
|
||||||
#define PIN_LED_WIFI_STATUS 15
|
|
||||||
#elif defined CONFIG_IDF_TARGET_ESP32
|
|
||||||
#define PIN_LED_WIFI_STATUS 27
|
|
||||||
#elif defined CONFIG_IDF_TARGET_ESP32C3
|
|
||||||
#define PIN_LED_WIFI_STATUS 10
|
|
||||||
#else
|
|
||||||
#error unknown hardware
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static EventGroupHandle_t wifi_event_group;
|
|
||||||
static esp_netif_t *sta_netif;
|
|
||||||
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 void event_handler(void *handler_arg __attribute__((unused)),
|
|
||||||
esp_event_base_t event_base __attribute__((unused)),
|
|
||||||
int32_t event_id,
|
|
||||||
void *event_data)
|
|
||||||
{
|
|
||||||
switch (event_id) {
|
|
||||||
case WIFI_EVENT_STA_START:
|
|
||||||
esp_wifi_connect();
|
|
||||||
break;
|
|
||||||
case WIFI_EVENT_STA_CONNECTED:
|
|
||||||
#ifdef CONFIG_EXAMPLE_IPV6
|
|
||||||
/* enable ipv6 */
|
|
||||||
esp_netif_create_ip6_linklocal(sta_netif);
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
case IP_EVENT_STA_GOT_IP: {
|
|
||||||
ip_event_got_ip_t *event = event_data;
|
|
||||||
// GPIO_SET_LEVEL_HIGH(PIN_LED_WIFI_STATUS);
|
|
||||||
xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT);
|
|
||||||
printf("SYSTEM EVENT STA GOT IP : %s\r\n", ip4addr_ntoa((const ip4_addr_t *) &event->ip_info.ip));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case WIFI_EVENT_STA_DISCONNECTED: {
|
|
||||||
wifi_event_sta_disconnected_t *event = event_data;
|
|
||||||
// GPIO_SET_LEVEL_LOW(PIN_LED_WIFI_STATUS);
|
|
||||||
printf("Disconnect reason : %d\r\n", event->reason);
|
|
||||||
|
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
ssid_change();
|
|
||||||
esp_wifi_connect();
|
|
||||||
xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT);
|
|
||||||
#ifdef CONFIG_EXAMPLE_IPV6
|
|
||||||
xEventGroupClearBits(wifi_event_group, IPV6_GOTIP_BIT);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case IP_EVENT_GOT_IP6: {
|
|
||||||
#ifdef CONFIG_EXAMPLE_IPV6
|
|
||||||
ip_event_got_ip6_t *event = event_data;
|
|
||||||
xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT);
|
|
||||||
os_printf("SYSTEM_EVENT_STA_GOT_IP6\r\n");
|
|
||||||
|
|
||||||
char *ip6 = ip6addr_ntoa((ip6_addr_t *) &event->ip6_info);
|
|
||||||
os_printf("IPv6: %s\r\n", ip6);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ssid_change() {
|
|
||||||
if (ssid_index > WIFI_LIST_SIZE - 1) {
|
|
||||||
ssid_index = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
wifi_config_t wifi_config = {
|
|
||||||
.sta = {
|
|
||||||
.ssid = "",
|
|
||||||
.password = "",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
printf("Waiting for AP connection...\r\n");
|
|
||||||
xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY);
|
|
||||||
printf("Connected to AP\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void wifi_init(void) {
|
|
||||||
// GPIO_FUNCTION_SET(PIN_LED_WIFI_STATUS);
|
|
||||||
// GPIO_SET_DIRECTION_NORMAL_OUT(PIN_LED_WIFI_STATUS);
|
|
||||||
|
|
||||||
esp_netif_init();
|
|
||||||
wifi_event_group = xEventGroupCreate();
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
||||||
sta_netif = esp_netif_create_default_wifi_sta();
|
|
||||||
assert(sta_netif);
|
|
||||||
|
|
||||||
#if (USE_STATIC_IP == 1)
|
|
||||||
esp_netif_dhcpc_stop(sta_netif);
|
|
||||||
|
|
||||||
esp_netif_ip_info_t ip_info;
|
|
||||||
esp_netif_set_ip4_addr(&ip_info.ip, DAP_IP_ADDRESS);
|
|
||||||
esp_netif_set_ip4_addr(&ip_info.gw, DAP_IP_GATEWAY);
|
|
||||||
esp_netif_set_ip4_addr(&ip_info.netmask, DAP_IP_NETMASK);
|
|
||||||
esp_netif_set_ip_info(sta_netif, &ip_info);
|
|
||||||
#endif // (USE_STATIC_IP == 1)
|
|
||||||
|
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
||||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
|
||||||
WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL));
|
|
||||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
|
||||||
IP_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL));
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
|
@ -1,6 +0,0 @@
|
||||||
#ifndef _WIFI_HANDLE_H_
|
|
||||||
#define _WIFI_HANDLE_H_
|
|
||||||
|
|
||||||
void wifi_init();
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
file(GLOB SOURCES *.c)
|
||||||
|
|
||||||
|
idf_component_register(
|
||||||
|
SRCS ${SOURCES}
|
||||||
|
INCLUDE_DIRS "."
|
||||||
|
PRIV_REQUIRES mdns esp_wifi esp_event
|
||||||
|
)
|
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef WIFI_CONFIGURATION_H_GUARD
|
||||||
|
#define WIFI_CONFIGURATION_H_GUARD
|
||||||
|
|
||||||
|
#define WIFI_DEFAULT_AP_SSID "无线DAP"
|
||||||
|
#define WIFI_DEFAULT_AP_PASS "12345678"
|
||||||
|
#define WIFI_DEFAULT_HOSTNAME "无线DAP"
|
||||||
|
|
||||||
|
#define WIFI_DEFAULT_STA_SSID "example_ssid"
|
||||||
|
#define WIFI_DEFAULT_STA_PASS "12345678"
|
||||||
|
|
||||||
|
#endif //WIFI_CONFIGURATION_H_GUARD
|
|
@ -0,0 +1,99 @@
|
||||||
|
#include "wifi_event_handler.h"
|
||||||
|
|
||||||
|
#include <esp_netif.h>
|
||||||
|
#include <lwip/ip4_addr.h>
|
||||||
|
#include <esp_wifi.h>
|
||||||
|
|
||||||
|
void ip_event_handler(void *handler_arg __attribute__((unused)),
|
||||||
|
esp_event_base_t event_base __attribute__((unused)),
|
||||||
|
int32_t event_id,
|
||||||
|
void *event_data)
|
||||||
|
{
|
||||||
|
switch (event_id) {
|
||||||
|
case IP_EVENT_STA_GOT_IP: {
|
||||||
|
ip_event_got_ip_t *event = event_data;
|
||||||
|
printf("STA GOT IP : %s\n",
|
||||||
|
ip4addr_ntoa((const ip4_addr_t *) &event->ip_info.ip));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case IP_EVENT_STA_LOST_IP:
|
||||||
|
printf("sta lost ip\n");
|
||||||
|
break;
|
||||||
|
case IP_EVENT_AP_STAIPASSIGNED:
|
||||||
|
printf("event STAIPASSIGNED\n");
|
||||||
|
break;
|
||||||
|
case IP_EVENT_GOT_IP6:
|
||||||
|
#ifdef CONFIG_EXAMPLE_IPV6
|
||||||
|
xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT);
|
||||||
|
printf("SYSTEM_EVENT_STA_GOT_IP6\r\n");
|
||||||
|
|
||||||
|
char *ip6 = ip6addr_ntoa(&event->event_info.got_ip6.ip6_info.ip);
|
||||||
|
printf("IPv6: %s\r\n", ip6);
|
||||||
|
#endif
|
||||||
|
case IP_EVENT_ETH_GOT_IP:
|
||||||
|
case IP_EVENT_ETH_LOST_IP:
|
||||||
|
case IP_EVENT_PPP_GOT_IP:
|
||||||
|
case IP_EVENT_PPP_LOST_IP:
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wifi_event_handler(void *handler_arg __attribute__((unused)),
|
||||||
|
esp_event_base_t event_base __attribute__((unused)),
|
||||||
|
int32_t event_id,
|
||||||
|
void *event_data)
|
||||||
|
{
|
||||||
|
switch (event_id) {
|
||||||
|
case WIFI_EVENT_WIFI_READY:
|
||||||
|
printf("event: WIFI_EVENT_WIFI_READY\n");
|
||||||
|
break;
|
||||||
|
case WIFI_EVENT_SCAN_DONE:
|
||||||
|
break;
|
||||||
|
case WIFI_EVENT_STA_START:
|
||||||
|
printf("event: WIFI_EVENT_STA_START\n");
|
||||||
|
esp_wifi_connect();
|
||||||
|
break;
|
||||||
|
case WIFI_EVENT_STA_CONNECTED: {
|
||||||
|
wifi_event_sta_connected_t *event = event_data;
|
||||||
|
uint8_t *m = event->bssid;
|
||||||
|
printf("event: WIFI_EVENT_STA_CONNECTED\n");
|
||||||
|
printf("connected to %2X:%2X:%2X:%2X:%2X:%2X\n", m[0], m[1], m[2], m[3], m[4], m[5]);
|
||||||
|
#ifdef CONFIG_EXAMPLE_IPV6
|
||||||
|
tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case WIFI_EVENT_STA_DISCONNECTED: {
|
||||||
|
wifi_event_sta_disconnected_t *event = event_data;
|
||||||
|
uint8_t *m = event->bssid;
|
||||||
|
printf("event: WIFI_EVENT_STA_DISCONNECTED\n");
|
||||||
|
printf("sta %2X:%2X:%2X:%2X:%2X:%2X disconnect reason %d\n",
|
||||||
|
m[0], m[1], m[2], m[3], m[4], m[5], event->reason);
|
||||||
|
/* auto reconnect after disconnection */
|
||||||
|
esp_wifi_connect();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case WIFI_EVENT_AP_START:
|
||||||
|
printf("event: WIFI_EVENT_AP_START\n");
|
||||||
|
break;
|
||||||
|
case WIFI_EVENT_AP_STACONNECTED: {
|
||||||
|
wifi_event_ap_staconnected_t *event = event_data;
|
||||||
|
uint8_t *m = event->mac;
|
||||||
|
printf("event: WIFI_EVENT_AP_STACONNECTED\n");
|
||||||
|
printf("%2X:%2X:%2X:%2X:%2X:%2X is connected\n",
|
||||||
|
m[0], m[1], m[2], m[3], m[4], m[5]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case WIFI_EVENT_AP_STADISCONNECTED: {
|
||||||
|
wifi_event_ap_staconnected_t *event = event_data;
|
||||||
|
uint8_t *m = event->mac;
|
||||||
|
printf("event: WIFI_EVENT_AP_STADISCONNECTED\n");
|
||||||
|
printf("%2X:%2X:%2X:%2X:%2X:%2X is connected\n",
|
||||||
|
m[0], m[1], m[2], m[3], m[4], m[5]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef WIFI_EVENT_HANDLER_H_GUARD
|
||||||
|
#define WIFI_EVENT_HANDLER_H_GUARD
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <esp_event_base.h>
|
||||||
|
|
||||||
|
void ip_event_handler(void *handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||||
|
|
||||||
|
void wifi_event_handler(void *handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||||
|
|
||||||
|
|
||||||
|
#endif //WIFI_EVENT_HANDLER_H_GUARD
|
|
@ -0,0 +1,85 @@
|
||||||
|
#include "wifi_manager.h"
|
||||||
|
#include "wifi_configuration.h"
|
||||||
|
#include "wifi_event_handler.h"
|
||||||
|
|
||||||
|
#include <esp_err.h>
|
||||||
|
#include <esp_netif.h>
|
||||||
|
#include <esp_wifi.h>
|
||||||
|
#include <esp_event.h>
|
||||||
|
#include <esp_log.h>
|
||||||
|
|
||||||
|
#include <lwip/ip4_addr.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define TAG __FILENAME__
|
||||||
|
|
||||||
|
static esp_netif_t *ap_netif;
|
||||||
|
static esp_netif_t *sta_netif;
|
||||||
|
|
||||||
|
void wifi_manager_init(void)
|
||||||
|
{
|
||||||
|
esp_err_t err;
|
||||||
|
|
||||||
|
ap_netif = esp_netif_create_default_wifi_ap();
|
||||||
|
assert(ap_netif);
|
||||||
|
sta_netif = esp_netif_create_default_wifi_sta();
|
||||||
|
assert(sta_netif);
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(esp_netif_set_hostname(ap_netif, WIFI_DEFAULT_HOSTNAME));
|
||||||
|
ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, WIFI_DEFAULT_HOSTNAME));
|
||||||
|
|
||||||
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL));
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler, NULL, NULL));
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
|
||||||
|
|
||||||
|
wifi_config_t ap_config = {0};
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
len = 32;
|
||||||
|
strncpy((char *) ap_config.ap.ssid, WIFI_DEFAULT_AP_SSID, len);
|
||||||
|
len = 64;
|
||||||
|
strncpy((char *) ap_config.ap.password, WIFI_DEFAULT_AP_PASS, len);
|
||||||
|
ap_config.ap.authmode = WIFI_AUTH_WPA2_WPA3_PSK;
|
||||||
|
ap_config.ap.max_connection = 4;
|
||||||
|
ap_config.ap.channel = 6;
|
||||||
|
ap_config.ap.ssid_hidden = 0;
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));
|
||||||
|
|
||||||
|
// TODO: scan once and connect to known wifi
|
||||||
|
|
||||||
|
wifi_config_t wifi_config = {
|
||||||
|
.sta = {
|
||||||
|
.ssid = WIFI_DEFAULT_STA_SSID,
|
||||||
|
.password = WIFI_DEFAULT_STA_PASS,
|
||||||
|
.sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||||
|
|
||||||
|
/* TODO: Read from nvs */
|
||||||
|
esp_netif_ip_info_t ip_info;
|
||||||
|
IP4_ADDR(&ip_info.ip, 192, 168, 1, 1);
|
||||||
|
IP4_ADDR(&ip_info.gw, 192, 168, 1, 1);
|
||||||
|
IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);
|
||||||
|
|
||||||
|
err = esp_netif_dhcps_stop(ap_netif);
|
||||||
|
if (err) {
|
||||||
|
ESP_LOGE(TAG, "dhcps stop %s", esp_err_to_name(err));
|
||||||
|
}
|
||||||
|
err = esp_netif_set_ip_info(ap_netif, &ip_info);
|
||||||
|
if (err) {
|
||||||
|
ESP_LOGE(TAG, "net if set info %s", esp_err_to_name(err));
|
||||||
|
}
|
||||||
|
err = esp_netif_dhcps_start(ap_netif);
|
||||||
|
if (err) {
|
||||||
|
ESP_LOGE(TAG, "dhcps start %s", esp_err_to_name(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_start());
|
||||||
|
ESP_LOGI(TAG, "wifi started");
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
#ifndef WIFI_MANAGER_H_GUARD
|
||||||
|
#define WIFI_MANAGER_H_GUARD
|
||||||
|
|
||||||
|
void wifi_manager_init();
|
||||||
|
|
||||||
|
int wifi_set_ap_cred(const char *ssid, const char *password);
|
||||||
|
|
||||||
|
int wifi_set_sta_cred(const char *ssid, const char *password);
|
||||||
|
|
||||||
|
#endif //WIFI_MANAGER_H_GUARD
|
|
@ -0,0 +1,7 @@
|
||||||
|
file(GLOB SOURCES *.c)
|
||||||
|
|
||||||
|
idf_component_register(
|
||||||
|
SRCS ${SOURCES}
|
||||||
|
INCLUDE_DIRS "."
|
||||||
|
PRIV_REQUIRES mdns
|
||||||
|
)
|
|
@ -0,0 +1,25 @@
|
||||||
|
#include "wt_mdns_config.h"
|
||||||
|
|
||||||
|
#include <mdns.h>
|
||||||
|
#include <esp_log.h>
|
||||||
|
|
||||||
|
#define MDNS_TAG "wt_mdns"
|
||||||
|
|
||||||
|
void wt_mdns_init()
|
||||||
|
{
|
||||||
|
ESP_ERROR_CHECK(mdns_init());
|
||||||
|
|
||||||
|
/* TODO: read hostname from NVS */
|
||||||
|
ESP_ERROR_CHECK(mdns_hostname_set(MDSN_DEFAULT_HOSTNAME));
|
||||||
|
|
||||||
|
/* TODO: read instance description from NVS */
|
||||||
|
ESP_ERROR_CHECK(mdns_instance_name_set(MDSN_INSTANCE_DESC));
|
||||||
|
}
|
||||||
|
|
||||||
|
int wt_mdns_set_hostname(const char *hostname)
|
||||||
|
{
|
||||||
|
mdns_hostname_set(hostname);
|
||||||
|
|
||||||
|
/* TODO save to NVS */
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef WT_MDNS_CONFIG_H_GUARD
|
||||||
|
#define WT_MDNS_CONFIG_H_GUARD
|
||||||
|
|
||||||
|
#define MDSN_DEFAULT_HOSTNAME "dap" // + serial number (4 char)
|
||||||
|
#define MDSN_INSTANCE_DESC "无线STM32调试器 by 允斯工作室"
|
||||||
|
|
||||||
|
void wt_mdns_init();
|
||||||
|
|
||||||
|
int wt_mdns_set_hostname(const char* hostname);
|
||||||
|
|
||||||
|
|
||||||
|
#endif //WT_MDNS_CONFIG_H_GUARD
|
|
@ -0,0 +1,7 @@
|
||||||
|
file(GLOB SOURCES wt_nvs.c wt_storage.c)
|
||||||
|
|
||||||
|
idf_component_register(
|
||||||
|
SRCS ${SOURCES}
|
||||||
|
INCLUDE_DIRS "."
|
||||||
|
PRIV_REQUIRES nvs_flash
|
||||||
|
)
|
|
@ -0,0 +1,16 @@
|
||||||
|
#include "wt_nvs.h"
|
||||||
|
|
||||||
|
#include <nvs_flash.h>
|
||||||
|
|
||||||
|
void wt_nvs_init()
|
||||||
|
{
|
||||||
|
// Initialize default NVS
|
||||||
|
esp_err_t err = nvs_flash_init();
|
||||||
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
|
// NVS partition was truncated and needs to be erased
|
||||||
|
// Retry nvs_flash_init
|
||||||
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||||
|
err = nvs_flash_init();
|
||||||
|
}
|
||||||
|
ESP_ERROR_CHECK(err);
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef WT_NVS_H_GUARD
|
||||||
|
#define WT_NVS_H_GUARD
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void wt_nvs_init();
|
||||||
|
|
||||||
|
#endif //WT_NVS_H_GUARD
|
|
@ -0,0 +1,7 @@
|
||||||
|
#include "wt_nvs.h"
|
||||||
|
#include "wt_storage.h"
|
||||||
|
|
||||||
|
void wt_storage_init()
|
||||||
|
{
|
||||||
|
wt_nvs_init();
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef WT_STORAGE_H_GUARD
|
||||||
|
#define WT_STORAGE_H_GUARD
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Init all storage, including ESP-NVS, SDIO...
|
||||||
|
*/
|
||||||
|
void wt_storage_init();
|
||||||
|
|
||||||
|
#endif //WT_STORAGE_H_GUARD
|
Loading…
Reference in New Issue