Add Zone.Identifier files for wifi-connect and partitions

- Created new Zone.Identifier file in components/wifi-connect with ZoneId=3.
- Created new Zone.Identifier file in partitions.csv with ZoneId=3.
This commit is contained in:
Wang Beihong
2026-03-28 16:12:37 +08:00
parent a9be1dd6b9
commit 8c33fe7411
47 changed files with 2090 additions and 3132 deletions

View File

@@ -20,10 +20,8 @@ void ui_display_init(void);
* @param temperature 温度值(°C)-1.0表示无效
* @param humidity 湿度值(%)-1.0表示无效
* @param lux 光照强度(lx)-1.0表示无效
* @param ppm 空气中有害气体浓度(ppm)
* @param quality_level 空气质量等级描述
*/
void ui_update_sensor_data(float temperature, float humidity, float lux, float ppm, const char* quality_level);
void ui_update_sensor_data(float temperature, float humidity, float lux);
/* Time page APIs */
void ui_show_time_page(void);

View File

@@ -10,7 +10,6 @@ static const char *TAG = "ui_display";
static lv_obj_t *temp_label = NULL;
static lv_obj_t *humid_label = NULL;
static lv_obj_t *lux_label = NULL;
static lv_obj_t *air_quality_label = NULL; // 新增空气质量标签
/* Time page objects */
static lv_obj_t *time_container = NULL;
@@ -28,54 +27,42 @@ void ui_display_init(void)
{
// 获取当前活动屏幕对象
lv_obj_t *scr = lv_scr_act();
/* 任务锁定 */
lvgl_port_lock(0);
/* 设置屏幕背景为白色 */
lv_obj_set_style_bg_color(scr, lv_color_white(), 0);
lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, 0);
/* 创建标题标签 */
lv_obj_t *title_label = lv_label_create(scr);
lv_label_set_text(title_label, "IoT Home Monitor");
lv_obj_set_style_text_color(title_label, lv_palette_main(LV_PALETTE_BLUE), 0);
lv_label_set_text(title_label, "IOT Home");
lv_obj_set_style_text_color(title_label, lv_color_black(), 0);
lv_obj_set_style_text_font(title_label, &lv_font_unscii_8, 0);
lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 2); // 调整标题位置
/* 创建温度标签 */
lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 6);
/* 创建传感器数据显示标签 */
temp_label = lv_label_create(scr);
lv_label_set_text(temp_label, "Temp: --.- C");
lv_obj_set_style_text_color(temp_label, lv_color_black(), 0);
lv_obj_set_style_text_font(temp_label, &lv_font_unscii_8, 0);
lv_obj_align(temp_label, LV_ALIGN_TOP_LEFT, 3, 20); // 调整位置
/* 创建湿度标签 */
lv_obj_align(temp_label, LV_ALIGN_TOP_LEFT, 3, 30);
humid_label = lv_label_create(scr);
lv_label_set_text(humid_label, "Humidity: --.- %");
lv_obj_set_style_text_color(humid_label, lv_color_black(), 0);
lv_obj_set_style_text_font(humid_label, &lv_font_unscii_8, 0);
lv_obj_align(humid_label, LV_ALIGN_TOP_LEFT, 3, 35); // 调整位置
lv_obj_align(humid_label, LV_ALIGN_TOP_LEFT, 3, 45);
/* 创建光照标签 */
lux_label = lv_label_create(scr);
lv_label_set_text(lux_label, "Light: --.- lux");
lv_obj_set_style_text_color(lux_label, lv_color_black(), 0);
lv_obj_set_style_text_font(lux_label, &lv_font_unscii_8, 0);
lv_obj_align(lux_label, LV_ALIGN_TOP_LEFT, 3, 50); // 调整位置
/* 创建空气质量标签 */
air_quality_label = lv_label_create(scr);
lv_label_set_text(air_quality_label, "IAQ : --.- Index");
lv_obj_set_style_text_color(air_quality_label, lv_color_black(), 0);
lv_obj_set_style_text_font(air_quality_label, &lv_font_unscii_8, 0);
lv_obj_align(air_quality_label, LV_ALIGN_TOP_LEFT, 3, 65); // 调整位置
/* 任务解锁 */
lvgl_port_unlock();
lv_obj_align(lux_label, LV_ALIGN_TOP_LEFT, 3, 60);
// 创建时间页面(初始隐藏)
lvgl_port_lock(0);
/* 创建时间页面(初始隐藏) */
time_container = lv_obj_create(lv_scr_act());
lv_obj_set_size(time_container, lv_pct(100), lv_pct(100));
lv_obj_set_style_bg_color(time_container, lv_color_white(), 0);
@@ -94,12 +81,12 @@ void ui_display_init(void)
lv_obj_align(time_label, LV_ALIGN_CENTER, 0, 12);
// 默认显示时间页面,隐藏传感器页面
lv_obj_clear_flag(time_container, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(time_container, LV_OBJ_FLAG_HIDDEN);
if (temp_label) lv_obj_add_flag(temp_label, LV_OBJ_FLAG_HIDDEN);
if (humid_label) lv_obj_add_flag(humid_label, LV_OBJ_FLAG_HIDDEN);
if (lux_label) lv_obj_add_flag(lux_label, LV_OBJ_FLAG_HIDDEN);
if (air_quality_label) lv_obj_add_flag(air_quality_label, LV_OBJ_FLAG_HIDDEN);
time_page_visible = true;
lvgl_port_unlock();
}
@@ -111,17 +98,15 @@ void ui_display_init(void)
* @param temperature 温度值(°C)-1.0表示无效
* @param humidity 湿度值(%)-1.0表示无效
* @param lux 光照强度(lx)-1.0表示无效
* @param ppm 空气中有害气体浓度(ppm)
* @param quality_level 空气质量等级描述
*/
void ui_update_sensor_data(float temperature, float humidity, float lux, float ppm, const char* quality_level)
void ui_update_sensor_data(float temperature, float humidity, float lux)
{
if (temp_label != NULL && humid_label != NULL && lux_label != NULL && air_quality_label != NULL)
if (temp_label != NULL && humid_label != NULL && lux_label != NULL)
{
/* 任务锁定 */
lvgl_port_lock(0);
// 更新温度标签 - 缩短文本以节省空间
// 更新温度标签
if (temperature >= -0.5) // -1.0表示无效
{
char temp_str[32];
@@ -132,8 +117,8 @@ void ui_update_sensor_data(float temperature, float humidity, float lux, float p
{
lv_label_set_text(temp_label, "Temp: Invalid");
}
// 更新湿度标签 - 缩短文本以节省空间
// 更新湿度标签
if (humidity >= -0.5) // -1.0表示无效
{
char humid_str[32];
@@ -145,8 +130,8 @@ void ui_update_sensor_data(float temperature, float humidity, float lux, float p
lv_label_set_text(humid_label, "Humidity: Invalid");
}
// 更新光照标签 - 缩短文本以节省空间
if (lux >= -0.5) // -1.0表示无效
// 更新光照标签
if (lux >= -0.5)
{
char lux_str[32];
snprintf(lux_str, sizeof(lux_str), "Light: %.2f lx", lux);
@@ -156,34 +141,7 @@ void ui_update_sensor_data(float temperature, float humidity, float lux, float p
{
lv_label_set_text(lux_label, "Light: Invalid");
}
// 更新空气质量标签 - 缩短文本以节省空间
if (ppm >= 0) // 空气质量值有效
{
char ppm_str[32];
snprintf(ppm_str, sizeof(ppm_str), "IAQ : %.2f Index", ppm);
// 根据空气质量等级更改颜色
lv_color_t color = lv_color_black(); // 默认黑色
if (ppm <= 20.0f) {
color = lv_color_make(0, 128, 0); // 绿色 - 空气质量优秀
} else if (ppm <= 100.0f) {
color = lv_color_make(0, 0, 0); // 黑色 - 空气质量良好
} else if (ppm <= 300.0f) {
color = lv_color_make(255, 165, 0); // 橙色 - 轻度污染
} else {
color = lv_color_make(255, 0, 0); // 红色 - 中重度污染
}
lv_label_set_text(air_quality_label, ppm_str);
lv_obj_set_style_text_color(air_quality_label, color, 0);
}
else
{
lv_label_set_text(air_quality_label, "IAQ : Invalid");
lv_obj_set_style_text_color(air_quality_label, lv_color_black(), 0);
}
/* 任务解锁 */
lvgl_port_unlock();
}
@@ -200,7 +158,6 @@ void ui_show_time_page(void)
if (temp_label) lv_obj_add_flag(temp_label, LV_OBJ_FLAG_HIDDEN);
if (humid_label) lv_obj_add_flag(humid_label, LV_OBJ_FLAG_HIDDEN);
if (lux_label) lv_obj_add_flag(lux_label, LV_OBJ_FLAG_HIDDEN);
if (air_quality_label) lv_obj_add_flag(air_quality_label, LV_OBJ_FLAG_HIDDEN);
time_page_visible = true;
lvgl_port_unlock();
}
@@ -216,7 +173,6 @@ void ui_show_sensor_page(void)
if (temp_label) lv_obj_clear_flag(temp_label, LV_OBJ_FLAG_HIDDEN);
if (humid_label) lv_obj_clear_flag(humid_label, LV_OBJ_FLAG_HIDDEN);
if (lux_label) lv_obj_clear_flag(lux_label, LV_OBJ_FLAG_HIDDEN);
if (air_quality_label) lv_obj_clear_flag(air_quality_label, LV_OBJ_FLAG_HIDDEN);
time_page_visible = false;
lvgl_port_unlock();
}

View File

@@ -1,52 +0,0 @@
idf_build_get_property(target IDF_TARGET)
if(${target} STREQUAL "linux")
# Header only library for linux
idf_component_register(INCLUDE_DIRS include
SRCS protocol_examples_utils.c)
return()
endif()
set(srcs "stdin_out.c"
"addr_from_stdin.c"
"connect.c"
"wifi_connect.c"
"protocol_examples_utils.c")
if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD)
list(APPEND srcs "console_cmd.c")
endif()
if(CONFIG_EXAMPLE_CONNECT_ETHERNET)
list(APPEND srcs "eth_connect.c")
endif()
if(CONFIG_EXAMPLE_CONNECT_THREAD)
list(APPEND srcs "thread_connect.c")
endif()
if(CONFIG_EXAMPLE_CONNECT_PPP)
list(APPEND srcs "ppp_connect.c")
endif()
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "include"
PRIV_REQUIRES esp_netif esp_driver_gpio esp_driver_uart esp_wifi vfs console esp_eth openthread)
if(CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD)
idf_component_optional_requires(PRIVATE console)
endif()
if(CONFIG_EXAMPLE_CONNECT_ETHERNET)
idf_component_optional_requires(PUBLIC esp_eth)
endif()
if(CONFIG_EXAMPLE_CONNECT_THREAD)
idf_component_optional_requires(PRIVATE openthread)
endif()
if(CONFIG_EXAMPLE_CONNECT_PPP)
idf_component_optional_requires(PRIVATE esp_tinyusb espressif__esp_tinyusb)
endif()

View File

@@ -1,504 +0,0 @@
menu "Example Connection Configuration"
orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps"
config EXAMPLE_CONNECT_WIFI
bool "connect using WiFi interface"
depends on !IDF_TARGET_LINUX && (SOC_WIFI_SUPPORTED || ESP_WIFI_REMOTE_ENABLED || ESP_HOST_WIFI_ENABLED)
default y if SOC_WIFI_SUPPORTED
help
Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network.
Choose this option to connect with WiFi
if EXAMPLE_CONNECT_WIFI
config EXAMPLE_WIFI_SSID_PWD_FROM_STDIN
bool "Get ssid and password from stdin"
default n
help
Give the WiFi SSID and password from stdin.
config EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD
depends on !EXAMPLE_WIFI_SSID_PWD_FROM_STDIN
bool "Provide wifi connect commands"
default y
help
Provide wifi connect commands for esp_console.
Please use `example_register_wifi_connect_commands` to register them.
config EXAMPLE_WIFI_SSID
depends on !EXAMPLE_WIFI_SSID_PWD_FROM_STDIN
string "WiFi SSID"
default "myssid"
help
SSID (network name) for the example to connect to.
config EXAMPLE_WIFI_PASSWORD
depends on !EXAMPLE_WIFI_SSID_PWD_FROM_STDIN
string "WiFi Password"
default "mypassword"
help
WiFi password (WPA or WPA2) for the example to use.
Can be left blank if the network has no security set.
config EXAMPLE_WIFI_CONN_MAX_RETRY
int "Maximum retry"
default 6
help
Set the Maximum retry to avoid station reconnecting to the AP unlimited,
in case the AP is really inexistent.
choice EXAMPLE_WIFI_SCAN_METHOD
prompt "WiFi Scan Method"
default EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL
help
WiFi scan method:
If "Fast" is selected, scan will end after find SSID match AP.
If "All Channel" is selected, scan will end after scan all the channel.
config EXAMPLE_WIFI_SCAN_METHOD_FAST
bool "Fast"
config EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL
bool "All Channel"
endchoice
menu "WiFi Scan threshold"
config EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD
int "WiFi minimum rssi"
range -127 0
default -127
help
The minimum rssi to accept in the scan mode.
choice EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD
prompt "WiFi Scan auth mode threshold"
default EXAMPLE_WIFI_AUTH_OPEN
help
The weakest authmode to accept in the scan mode.
config EXAMPLE_WIFI_AUTH_OPEN
bool "OPEN"
config EXAMPLE_WIFI_AUTH_WEP
bool "WEP"
config EXAMPLE_WIFI_AUTH_WPA_PSK
bool "WPA PSK"
config EXAMPLE_WIFI_AUTH_WPA2_PSK
bool "WPA2 PSK"
config EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK
bool "WPA WPA2 PSK"
config EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE
bool "WPA2 ENTERPRISE"
config EXAMPLE_WIFI_AUTH_WPA3_PSK
bool "WPA3 PSK"
config EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK
bool "WPA2 WPA3 PSK"
config EXAMPLE_WIFI_AUTH_WAPI_PSK
bool "WAPI PSK"
endchoice
endmenu
choice EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD
prompt "WiFi Connect AP Sort Method"
default EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
help
WiFi connect AP sort method:
If "Signal" is selected, Sort matched APs in scan list by RSSI.
If "Security" is selected, Sort matched APs in scan list by security mode.
config EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
bool "Signal"
config EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
bool "Security"
endchoice
endif
config EXAMPLE_CONNECT_ETHERNET
bool "connect using Ethernet interface"
depends on !IDF_TARGET_LINUX
default y if !EXAMPLE_CONNECT_WIFI && !EXAMPLE_CONNECT_THREAD
help
Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network.
Choose this option to connect with Ethernet
if EXAMPLE_CONNECT_ETHERNET
config EXAMPLE_ETHERNET_EMAC_TASK_STACK_SIZE
int "emac_rx task stack size"
default 2048
help
This set stack size for emac_rx task
config EXAMPLE_USE_SPI_ETHERNET
bool
choice EXAMPLE_ETHERNET_TYPE
prompt "Ethernet Type"
default EXAMPLE_USE_INTERNAL_ETHERNET if SOC_EMAC_SUPPORTED
default EXAMPLE_USE_W5500
help
Select which kind of Ethernet will be used in the example.
config EXAMPLE_USE_INTERNAL_ETHERNET
depends on SOC_EMAC_SUPPORTED
select ETH_USE_ESP32_EMAC
bool "Internal EMAC"
help
Select internal Ethernet MAC controller.
config EXAMPLE_USE_DM9051
bool "DM9051 Module"
select EXAMPLE_USE_SPI_ETHERNET
select ETH_USE_SPI_ETHERNET
select ETH_SPI_ETHERNET_DM9051
help
Select external SPI-Ethernet module.
config EXAMPLE_USE_W5500
bool "W5500 Module"
select EXAMPLE_USE_SPI_ETHERNET
select ETH_USE_SPI_ETHERNET
select ETH_SPI_ETHERNET_W5500
help
Select external SPI-Ethernet module (W5500).
config EXAMPLE_USE_OPENETH
bool "OpenCores Ethernet MAC (EXPERIMENTAL)"
select ETH_USE_OPENETH
help
When this option is enabled, the example is built with support for
OpenCores Ethernet MAC, which allows testing the example in QEMU.
Note that this option is used for internal testing purposes, and
not officially supported. Examples built with this option enabled
will not run on a real ESP32 chip.
endchoice # EXAMPLE_ETHERNET_TYPE
if EXAMPLE_USE_INTERNAL_ETHERNET
choice EXAMPLE_ETH_PHY_MODEL
prompt "Ethernet PHY Device"
default EXAMPLE_ETH_PHY_IP101
help
Select the Ethernet PHY device to use in the example.
config EXAMPLE_ETH_PHY_GENERIC
bool "Generic 802.3 PHY"
help
Any Ethernet PHY chip compliant with IEEE 802.3 can be used. However, while
basic functionality should always work, some specific features might be limited,
even if the PHY meets IEEE 802.3 standard. A typical example is loopback
functionality, where certain PHYs may require setting a specific speed mode to
operate correctly.
config EXAMPLE_ETH_PHY_IP101
bool "IP101"
help
IP101 is a single port 10/100 MII/RMII/TP/Fiber Fast Ethernet Transceiver.
Goto http://www.icplus.com.tw/pp-IP101G.html for more information about it.
config EXAMPLE_ETH_PHY_RTL8201
bool "RTL8201/SR8201"
help
RTL8201F/SR8201F is a single port 10/100Mb Ethernet Transceiver with auto MDIX.
Goto http://www.corechip-sz.com/productsview.asp?id=22 for more information about it.
config EXAMPLE_ETH_PHY_LAN87XX
bool "LAN87xx"
help
Below chips are supported:
LAN8710A is a small footprint MII/RMII 10/100 Ethernet Transceiver with HP Auto-MDIX and
flexPWR® Technology.
LAN8720A is a small footprint RMII 10/100 Ethernet Transceiver with HP Auto-MDIX Support.
LAN8740A/LAN8741A is a small footprint MII/RMII 10/100 Energy Efficient Ethernet Transceiver
with HP Auto-MDIX and flexPWR® Technology.
LAN8742A is a small footprint RMII 10/100 Ethernet Transceiver with HP Auto-MDIX and
flexPWR® Technology.
Goto https://www.microchip.com for more information about them.
config EXAMPLE_ETH_PHY_DP83848
bool "DP83848"
help
DP83848 is a single port 10/100Mb/s Ethernet Physical Layer Transceiver.
Goto http://www.ti.com/product/DP83848J for more information about it.
config EXAMPLE_ETH_PHY_KSZ80XX
bool "KSZ80xx"
help
With the KSZ80xx series, Microchip offers single-chip 10BASE-T/100BASE-TX
Ethernet Physical Layer Transceivers (PHY).
The following chips are supported: KSZ8001, KSZ8021, KSZ8031, KSZ8041,
KSZ8051, KSZ8061, KSZ8081, KSZ8091
Goto https://www.microchip.com for more information about them.
endchoice
config EXAMPLE_ETH_MDC_GPIO
int "SMI MDC GPIO number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
default 23 if IDF_TARGET_ESP32
default 31 if IDF_TARGET_ESP32P4
help
Set the GPIO number used by SMI MDC.
config EXAMPLE_ETH_MDIO_GPIO
int "SMI MDIO GPIO number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
default 18 if IDF_TARGET_ESP32
default 52 if IDF_TARGET_ESP32P4
help
Set the GPIO number used by SMI MDIO.
endif
if EXAMPLE_USE_SPI_ETHERNET
config EXAMPLE_ETH_SPI_HOST
int "SPI Host Number"
range 0 2
default 1
help
Set the SPI host used to communicate with the SPI Ethernet Controller.
config EXAMPLE_ETH_SPI_SCLK_GPIO
int "SPI SCLK GPIO number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
default 14
help
Set the GPIO number used by SPI SCLK.
config EXAMPLE_ETH_SPI_MOSI_GPIO
int "SPI MOSI GPIO number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
default 13
help
Set the GPIO number used by SPI MOSI.
config EXAMPLE_ETH_SPI_MISO_GPIO
int "SPI MISO GPIO number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX
default 12
help
Set the GPIO number used by SPI MISO.
config EXAMPLE_ETH_SPI_CS_GPIO
int "SPI CS GPIO number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
default 15
help
Set the GPIO number used by SPI CS.
config EXAMPLE_ETH_SPI_CLOCK_MHZ
int "SPI clock speed (MHz)"
range 5 80
default 36
help
Set the clock speed (MHz) of SPI interface.
config EXAMPLE_ETH_SPI_INT_GPIO
int "Interrupt GPIO number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX
default 4
help
Set the GPIO number used by the SPI Ethernet module interrupt line.
endif # EXAMPLE_USE_SPI_ETHERNET
config EXAMPLE_ETH_PHY_RST_GPIO
int "PHY Reset GPIO number"
range -1 ENV_GPIO_OUT_RANGE_MAX
default 51 if IDF_TARGET_ESP32P4
default 5
help
Set the GPIO number used to reset PHY chip.
Set to -1 to disable PHY chip hardware reset.
config EXAMPLE_ETH_PHY_ADDR
int "PHY Address"
range 0 31 if EXAMPLE_USE_INTERNAL_ETHERNET
default 1
help
Set PHY address according your board schematic.
endif # EXAMPLE_CONNECT_ETHERNET
config EXAMPLE_CONNECT_PPP
bool "connect using Point to Point interface"
select LWIP_PPP_SUPPORT
help
Protocol examples can use PPP connection over serial line.
Choose this option to connect to the ppp server running
on your laptop over a serial line (either UART or USB ACM)
if EXAMPLE_CONNECT_PPP
choice EXAMPLE_CONNECT_PPP_DEVICE
prompt "Choose PPP device"
default EXAMPLE_CONNECT_PPP_DEVICE_USB
help
Select which peripheral to use to connect to the PPP server.
config EXAMPLE_CONNECT_PPP_DEVICE_USB
bool "USB"
depends on SOC_USB_OTG_SUPPORTED
select TINYUSB_CDC_ENABLED
help
Use USB ACM device.
config EXAMPLE_CONNECT_PPP_DEVICE_UART
bool "UART"
help
Use UART.
endchoice
menu "UART Configuration"
depends on EXAMPLE_CONNECT_PPP_DEVICE_UART
config EXAMPLE_CONNECT_UART_TX_PIN
int "TXD Pin Number"
default 4
range 0 31
help
Pin number of UART TX.
config EXAMPLE_CONNECT_UART_RX_PIN
int "RXD Pin Number"
default 5
range 0 31
help
Pin number of UART RX.
config EXAMPLE_CONNECT_UART_BAUDRATE
int "UART Baudrate"
default 115200
range 9600 3000000
help
Baudrate of the UART device
endmenu
config EXAMPLE_PPP_CONN_MAX_RETRY
int "Maximum retry"
default 6
help
Set the Maximum retry to avoid station reconnecting if the pppd
is not available
endif # EXAMPLE_CONNECT_PPP
config EXAMPLE_CONNECT_THREAD
bool "Connect using Thread interface"
depends on !IDF_TARGET_LINUX && OPENTHREAD_ENABLED
default y if SOC_IEEE802154_SUPPORTED
select EXAMPLE_CONNECT_IPV6
help
Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network.
Choose this option to connect with Thread.
The operational active dataset of the Thread network can be configured in openthread
component at '->Components->OpenThread->Thread Core Features->Thread Operational Dataset'
if EXAMPLE_CONNECT_THREAD
config EXAMPLE_THREAD_TASK_STACK_SIZE
int "Example Thread task stack size"
default 8192
help
Thread task stack size
menu "Radio Spinel Options"
depends on OPENTHREAD_RADIO_SPINEL_UART || OPENTHREAD_RADIO_SPINEL_SPI
config EXAMPLE_THREAD_UART_RX_PIN
depends on OPENTHREAD_RADIO_SPINEL_UART
int "Uart Rx Pin"
default 17
config EXAMPLE_THREAD_UART_TX_PIN
depends on OPENTHREAD_RADIO_SPINEL_UART
int "Uart Tx pin"
default 18
config EXAMPLE_THREAD_UART_BAUD
depends on OPENTHREAD_RADIO_SPINEL_UART
int "Uart baud rate"
default 460800
config EXAMPLE_THREAD_UART_PORT
depends on OPENTHREAD_RADIO_SPINEL_UART
int "Uart port"
default 1
config EXAMPLE_THREAD_SPI_CS_PIN
depends on OPENTHREAD_RADIO_SPINEL_SPI
int "SPI CS Pin"
default 10
config EXAMPLE_THREAD_SPI_SCLK_PIN
depends on OPENTHREAD_RADIO_SPINEL_SPI
int "SPI SCLK Pin"
default 12
config EXAMPLE_THREAD_SPI_MISO_PIN
depends on OPENTHREAD_RADIO_SPINEL_SPI
int "SPI MISO Pin"
default 13
config EXAMPLE_THREAD_SPI_MOSI_PIN
depends on OPENTHREAD_RADIO_SPINEL_SPI
int "SPI MOSI Pin"
default 11
config EXAMPLE_THREAD_SPI_INTR_PIN
depends on OPENTHREAD_RADIO_SPINEL_SPI
int "SPI Interrupt Pin"
default 8
endmenu
endif
config EXAMPLE_CONNECT_IPV4
bool
depends on LWIP_IPV4
default n if EXAMPLE_CONNECT_THREAD
default y
config EXAMPLE_CONNECT_IPV6
depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET || EXAMPLE_CONNECT_PPP || EXAMPLE_CONNECT_THREAD
bool "Obtain IPv6 address"
default y
select LWIP_IPV6
select LWIP_PPP_ENABLE_IPV6 if EXAMPLE_CONNECT_PPP
help
By default, examples will wait until IPv4 and IPv6 local link addresses are obtained.
Disable this option if the network does not support IPv6.
Choose the preferred IPv6 address type if the connection code should wait until other than
the local link address gets assigned.
Consider enabling IPv6 stateless address autoconfiguration (SLAAC) in the LWIP component.
if EXAMPLE_CONNECT_IPV6
choice EXAMPLE_CONNECT_PREFERRED_IPV6
prompt "Preferred IPv6 Type"
default EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK
help
Select which kind of IPv6 address the connect logic waits for.
config EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK
bool "Local Link Address"
help
Blocks until Local link address assigned.
config EXAMPLE_CONNECT_IPV6_PREF_GLOBAL
bool "Global Address"
help
Blocks until Global address assigned.
config EXAMPLE_CONNECT_IPV6_PREF_SITE_LOCAL
bool "Site Local Address"
help
Blocks until Site link address assigned.
config EXAMPLE_CONNECT_IPV6_PREF_UNIQUE_LOCAL
bool "Unique Local Link Address"
help
Blocks until Unique local address assigned.
endchoice
endif
endmenu

View File

@@ -1,66 +0,0 @@
# protocol_example_connect
This component implements the most common connection methods for ESP32 boards. It should be used mainly in examples of ESP-IDF to demonstrate functionality of network protocols and other libraries, that need the connection step as a prerequisite.
## How to use this component
Choose the preferred interface (WiFi, Ethernet, Thread, PPPoS) to connect to the network and configure the interface.
It is possible to enable multiple interfaces simultaneously making the connection phase to block until all the chosen interfaces acquire IP addresses.
It is also possible to disable all interfaces, skipping the connection phase altogether.
### WiFi
Choose WiFi connection method (for chipsets that support it) and configure basic WiFi connection properties:
* WiFi SSID
* WiFI password
* Maximum connection retry (connection would be aborted if it doesn't succeed after specified number of retries)
* WiFi scan method (including RSSI and authorization mode threshold)
### Ethernet
Choose Ethernet connection if your board supports it. The most common settings is using Espressif Ethernet Kit, which is also the recommended HW for this selection. You can also select an SPI ethernet device (if your chipset doesn't support internal EMAC or if you prefer). It is also possible to use OpenCores Ethernet MAC if you're running the example under QEMU.
### Thread
Choose Thread connection if your board supports IEEE802.15.4 native radio or works with [OpenThread RCP](../../openthread/ot_rcp/README.md). You can configure the Thread network at menuconfig '->Components->OpenThread->Thread Core Features->Thread Operational Dataset'.
If the Thread end-device joins a Thread network with a Thread Border Router that has the NAT64 feature enabled, the end-device can access the Internet with the standard DNS APIs after configuring the following properties:
* Enable DNS64 client ('->Components->OpenThread->Thread Core Features->Enable DNS64 client')
* Enable custom DNS external resolve Hook ('->Components->LWIP->Hooks->DNS external resolve Hook->Custom implementation')
### PPP
Point to point connection method creates a simple IP tunnel to the counterpart device (running PPP server), typically a Linux machine with pppd service. We currently support only PPP over Serial (using UART or USB CDC). This is useful for simple testing of networking layers, but with some additional configuration on the server side, we could simulate standard model of internet connectivity. The PPP server could be also represented by a cellular modem device with pre-configured connectivity and already switched to PPP mode (this setup is not very flexible though, so we suggest using a standard modem library implementing commands and modes, e.g. [esp_modem](https://components.espressif.com/component/espressif/esp_modem) ).
> [!Note]
> Note that if you choose USB device, you have to manually add a dependency on `esp_tinyusb` component. This step is necessary to keep the `protocol_example_connect` component simple and dependency free. Please run this command from your project location to add the dependency:
> ```bash
> idf.py add-dependency espressif/esp_tinyusb^1
> ```
#### Setup a PPP server
Connect the board using UART or USB and note the device name, which would be typically:
* `/dev/ttyACMx` for USB devices
* `/dev/ttyUSBx` for UART devices
Run the pppd server:
```bash
sudo pppd /dev/ttyACM0 115200 192.168.11.1:192.168.11.2 ms-dns 8.8.8.8 modem local noauth debug nocrtscts nodetach +ipv6
```
Please update the parameters with the correct serial device, baud rate, IP addresses, DNS server, use `+ipv6` if `EXAMPLE_CONNECT_IPV6=y`.
#### Connection to outside
In order to access other network endpoints, we have to configure some IP/translation rules. The easiest method is to setup a masquerade of the PPPD created interface (`ppp0`) to your default networking interface (`${ETH0}`). Here is an example of such rule:
```bash
sudo iptables -t nat -A POSTROUTING -o ${ETH0} -j MASQUERADE
sudo iptables -A FORWARD -i ${ETH0} -o ppp0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i ppp0 -o ${ETH0} -j ACCEPT
```

View File

@@ -1,69 +0,0 @@
#include <string.h>
#include "esp_system.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "protocol_examples_common.h"
#include "lwip/sockets.h"
#include <lwip/netdb.h>
#include <arpa/inet.h>
#define HOST_IP_SIZE 128
esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *addr_family, struct sockaddr_storage *dest_addr)
{
char host_ip[HOST_IP_SIZE];
int len;
static bool already_init = false;
// this function could be called multiple times -> make sure UART init runs only once
if (!already_init) {
example_configure_stdin_stdout();
already_init = true;
}
// ignore empty or LF only string (could receive from DUT class)
do {
fgets(host_ip, HOST_IP_SIZE, stdin);
len = strlen(host_ip);
} while (len<=1 && host_ip[0] == '\n');
host_ip[len - 1] = '\0';
struct addrinfo hints, *addr_list, *cur;
memset( &hints, 0, sizeof( hints ) );
// run getaddrinfo() to decide on the IP protocol
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = sock_type;
hints.ai_protocol = IPPROTO_TCP;
if( getaddrinfo( host_ip, NULL, &hints, &addr_list ) != 0 ) {
return ESP_FAIL;
}
for( cur = addr_list; cur != NULL; cur = cur->ai_next ) {
memcpy(dest_addr, cur->ai_addr, sizeof(*dest_addr));
#if CONFIG_EXAMPLE_CONNECT_IPV4
if (cur->ai_family == AF_INET) {
*ip_protocol = IPPROTO_IP;
*addr_family = AF_INET;
// add port number and return on first IPv4 match
((struct sockaddr_in*)dest_addr)->sin_port = htons(port);
freeaddrinfo( addr_list );
return ESP_OK;
}
#endif // IPV4
#if CONFIG_EXAMPLE_CONNECT_IPV6
if (cur->ai_family == AF_INET6) {
*ip_protocol = IPPROTO_IPV6;
*addr_family = AF_INET6;
// add port and interface number and return on first IPv6 match
((struct sockaddr_in6*)dest_addr)->sin6_port = htons(port);
((struct sockaddr_in6*)dest_addr)->sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE);
freeaddrinfo( addr_list );
return ESP_OK;
}
#endif // IPV6
}
// no match found
freeaddrinfo( addr_list );
return ESP_FAIL;
}

View File

@@ -1,148 +0,0 @@
/*
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <string.h>
#include "protocol_examples_common.h"
#include "example_common_private.h"
#include "sdkconfig.h"
#include "esp_event.h"
#include "esp_wifi.h"
#include "esp_wifi_default.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "lwip/err.h"
#include "lwip/sys.h"
static const char *TAG = "example_common";
#if CONFIG_EXAMPLE_CONNECT_IPV6
/* types of ipv6 addresses to be displayed on ipv6 events */
const char *example_ipv6_addr_types_to_str[6] = {
"ESP_IP6_ADDR_IS_UNKNOWN",
"ESP_IP6_ADDR_IS_GLOBAL",
"ESP_IP6_ADDR_IS_LINK_LOCAL",
"ESP_IP6_ADDR_IS_SITE_LOCAL",
"ESP_IP6_ADDR_IS_UNIQUE_LOCAL",
"ESP_IP6_ADDR_IS_IPV4_MAPPED_IPV6"
};
#endif
/**
* @brief Checks the netif description if it contains specified prefix.
* All netifs created within common connect component are prefixed with the module TAG,
* so it returns true if the specified netif is owned by this module
*/
bool example_is_our_netif(const char *prefix, esp_netif_t *netif)
{
return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix) - 1) == 0;
}
static bool netif_desc_matches_with(esp_netif_t *netif, void *ctx)
{
return strcmp(ctx, esp_netif_get_desc(netif)) == 0;
}
esp_netif_t *get_example_netif_from_desc(const char *desc)
{
return esp_netif_find_if(netif_desc_matches_with, (void*)desc);
}
static esp_err_t print_all_ips_tcpip(void* ctx)
{
const char *prefix = ctx;
// iterate over active interfaces, and print out IPs of "our" netifs
esp_netif_t *netif = NULL;
while ((netif = esp_netif_next_unsafe(netif)) != NULL) {
if (example_is_our_netif(prefix, netif)) {
ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif));
#if CONFIG_EXAMPLE_CONNECT_IPV4
esp_netif_ip_info_t ip;
ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));
ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip));
#endif
#if CONFIG_EXAMPLE_CONNECT_IPV6
esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF];
int ip6_addrs = esp_netif_get_all_ip6(netif, ip6);
for (int j = 0; j < ip6_addrs; ++j) {
esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&(ip6[j]));
ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), example_ipv6_addr_types_to_str[ipv6_type]);
}
#endif
}
}
return ESP_OK;
}
void example_print_all_netif_ips(const char *prefix)
{
// Print all IPs in TCPIP context to avoid potential races of removing/adding netifs when iterating over the list
esp_netif_tcpip_exec(print_all_ips_tcpip, (void*) prefix);
}
esp_err_t example_connect(void)
{
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
if (example_ethernet_connect() != ESP_OK) {
return ESP_FAIL;
}
ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ethernet_shutdown));
#endif
#if CONFIG_EXAMPLE_CONNECT_WIFI
if (example_wifi_connect() != ESP_OK) {
return ESP_FAIL;
}
ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_wifi_shutdown));
#endif
#if CONFIG_EXAMPLE_CONNECT_THREAD
if (example_thread_connect() != ESP_OK) {
return ESP_FAIL;
}
ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_thread_shutdown));
#endif
#if CONFIG_EXAMPLE_CONNECT_PPP
if (example_ppp_connect() != ESP_OK) {
return ESP_FAIL;
}
ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ppp_shutdown));
#endif
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
example_print_all_netif_ips(EXAMPLE_NETIF_DESC_ETH);
#endif
#if CONFIG_EXAMPLE_CONNECT_WIFI
example_print_all_netif_ips(EXAMPLE_NETIF_DESC_STA);
#endif
#if CONFIG_EXAMPLE_CONNECT_THREAD
example_print_all_netif_ips(EXAMPLE_NETIF_DESC_THREAD);
#endif
#if CONFIG_EXAMPLE_CONNECT_PPP
example_print_all_netif_ips(EXAMPLE_NETIF_DESC_PPP);
#endif
return ESP_OK;
}
esp_err_t example_disconnect(void)
{
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
example_ethernet_shutdown();
ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&example_ethernet_shutdown));
#endif
#if CONFIG_EXAMPLE_CONNECT_WIFI
example_wifi_shutdown();
ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&example_wifi_shutdown));
#endif
return ESP_OK;
}

View File

@@ -1,87 +0,0 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <string.h>
#include "protocol_examples_common.h"
#include "example_common_private.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_console.h"
#include "argtable3/argtable3.h"
static const char *TAG = "example_console";
typedef struct {
struct arg_str *ssid;
struct arg_str *password;
struct arg_int *channel;
struct arg_end *end;
} wifi_connect_args_t;
static wifi_connect_args_t connect_args;
static int cmd_do_wifi_connect(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void **) &connect_args);
if (nerrors != 0) {
arg_print_errors(stderr, connect_args.end, argv[0]);
return 1;
}
wifi_config_t wifi_config = {
.sta = {
.scan_method = WIFI_ALL_CHANNEL_SCAN,
.sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
},
};
if (connect_args.channel->count > 0) {
wifi_config.sta.channel = (uint8_t)(connect_args.channel->ival[0]);
}
const char *ssid = connect_args.ssid->sval[0];
const char *pass = connect_args.password->sval[0];
strlcpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
if (pass) {
strlcpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
}
example_wifi_sta_do_connect(wifi_config, false);
return 0;
}
static int cmd_do_wifi_disconnect(int argc, char **argv)
{
example_wifi_sta_do_disconnect();
return 0;
}
void example_register_wifi_connect_commands(void)
{
ESP_LOGI(TAG, "Registering WiFi connect commands.");
example_wifi_start();
connect_args.ssid = arg_str1(NULL, NULL, "<ssid>", "SSID of AP");
connect_args.password = arg_str0(NULL, NULL, "<pass>", "password of AP");
connect_args.channel = arg_int0("n", "channel", "<channel>", "channel of AP");
connect_args.end = arg_end(2);
const esp_console_cmd_t wifi_connect_cmd = {
.command = "wifi_connect",
.help = "WiFi is station mode, join specified soft-AP",
.hint = NULL,
.func = &cmd_do_wifi_connect,
.argtable = &connect_args
};
ESP_ERROR_CHECK( esp_console_cmd_register(&wifi_connect_cmd) );
const esp_console_cmd_t wifi_disconnect_cmd = {
.command = "wifi_disconnect",
.help = "Do wifi disconnect",
.hint = NULL,
.func = &cmd_do_wifi_disconnect,
};
ESP_ERROR_CHECK( esp_console_cmd_register(&wifi_disconnect_cmd) );
}

View File

@@ -1,245 +0,0 @@
/*
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <string.h>
#include "protocol_examples_common.h"
#include "example_common_private.h"
#include "esp_event.h"
#include "esp_eth.h"
#if CONFIG_ETH_USE_SPI_ETHERNET
#include "driver/spi_master.h"
#endif // CONFIG_ETH_USE_SPI_ETHERNET
#include "esp_log.h"
#include "esp_mac.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
static const char *TAG = "ethernet_connect";
static SemaphoreHandle_t s_semph_get_ip_addrs = NULL;
#if CONFIG_EXAMPLE_CONNECT_IPV6
static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL;
#endif
static esp_netif_t *eth_start(void);
static void eth_stop(void);
/** Event handler for Ethernet events */
static void eth_on_got_ip(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
if (!example_is_our_netif(EXAMPLE_NETIF_DESC_ETH, event->esp_netif)) {
return;
}
ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip));
xSemaphoreGive(s_semph_get_ip_addrs);
}
#if CONFIG_EXAMPLE_CONNECT_IPV6
static void eth_on_got_ipv6(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
if (!example_is_our_netif(EXAMPLE_NETIF_DESC_ETH, event->esp_netif)) {
return;
}
esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip);
ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif),
IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) {
xSemaphoreGive(s_semph_get_ip6_addrs);
}
}
static void on_eth_event(void *esp_netif, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
switch (event_id) {
case ETHERNET_EVENT_CONNECTED:
ESP_LOGI(TAG, "Ethernet Link Up");
ESP_ERROR_CHECK(esp_netif_create_ip6_linklocal(esp_netif));
break;
default:
break;
}
}
#endif // CONFIG_EXAMPLE_CONNECT_IPV6
static esp_eth_handle_t s_eth_handle = NULL;
static esp_eth_mac_t *s_mac = NULL;
static esp_eth_phy_t *s_phy = NULL;
static esp_eth_netif_glue_handle_t s_eth_glue = NULL;
static esp_netif_t *eth_start(void)
{
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
// Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask)
esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_ETH;
esp_netif_config.route_prio = 64;
esp_netif_config_t netif_config = {
.base = &esp_netif_config,
.stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
};
esp_netif_t *netif = esp_netif_new(&netif_config);
assert(netif);
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
mac_config.rx_task_stack_size = CONFIG_EXAMPLE_ETHERNET_EMAC_TASK_STACK_SIZE;
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
#if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
esp32_emac_config.smi_gpio.mdc_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
esp32_emac_config.smi_gpio.mdio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
#if CONFIG_EXAMPLE_ETH_PHY_GENERIC
s_phy = esp_eth_phy_new_generic(&phy_config);
#elif CONFIG_EXAMPLE_ETH_PHY_IP101
s_phy = esp_eth_phy_new_ip101(&phy_config);
#elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
s_phy = esp_eth_phy_new_rtl8201(&phy_config);
#elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX
s_phy = esp_eth_phy_new_lan87xx(&phy_config);
#elif CONFIG_EXAMPLE_ETH_PHY_DP83848
s_phy = esp_eth_phy_new_dp83848(&phy_config);
#elif CONFIG_EXAMPLE_ETH_PHY_KSZ80XX
s_phy = esp_eth_phy_new_ksz80xx(&phy_config);
#endif
#elif CONFIG_EXAMPLE_USE_SPI_ETHERNET
gpio_install_isr_service(0);
spi_bus_config_t buscfg = {
.miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
.mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
.sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
spi_device_interface_config_t spi_devcfg = {
.mode = 0,
.clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
.spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO,
.queue_size = 20
};
#if CONFIG_EXAMPLE_USE_DM9051
/* dm9051 ethernet driver is based on spi driver */
eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
s_phy = esp_eth_phy_new_dm9051(&phy_config);
#elif CONFIG_EXAMPLE_USE_W5500
/* w5500 ethernet driver is based on spi driver */
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
s_mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
s_phy = esp_eth_phy_new_w5500(&phy_config);
#endif
#elif CONFIG_EXAMPLE_USE_OPENETH
phy_config.autonego_timeout_ms = 100;
s_mac = esp_eth_mac_new_openeth(&mac_config);
s_phy = esp_eth_phy_new_dp83848(&phy_config);
#endif
// Install Ethernet driver
esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy);
ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle));
#if CONFIG_EXAMPLE_USE_SPI_ETHERNET
/* The SPI Ethernet module might doesn't have a burned factory MAC address, we cat to set it manually.
We set the ESP_MAC_ETH mac address as the default, if you want to use ESP_MAC_EFUSE_CUSTOM mac address, please enable the
configuration: `ESP_MAC_USE_CUSTOM_MAC_AS_BASE_MAC`
*/
uint8_t eth_mac[6] = {0};
ESP_ERROR_CHECK(esp_read_mac(eth_mac, ESP_MAC_ETH));
ESP_ERROR_CHECK(esp_eth_ioctl(s_eth_handle, ETH_CMD_S_MAC_ADDR, eth_mac));
#endif // CONFIG_EXAMPLE_USE_SPI_ETHERNET
// combine driver with netif
s_eth_glue = esp_eth_new_netif_glue(s_eth_handle);
esp_netif_attach(netif, s_eth_glue);
// Register user defined event handlers
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &eth_on_got_ip, NULL));
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &eth_on_got_ipv6, NULL));
#endif
esp_eth_start(s_eth_handle);
return netif;
}
static void eth_stop(void)
{
esp_netif_t *eth_netif = get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH);
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &eth_on_got_ip));
#if CONFIG_EXAMPLE_CONNECT_IPV6
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &eth_on_got_ipv6));
ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event));
#endif
ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle));
ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue));
ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle));
s_eth_handle = NULL;
ESP_ERROR_CHECK(s_phy->del(s_phy));
ESP_ERROR_CHECK(s_mac->del(s_mac));
esp_netif_destroy(eth_netif);
}
esp_eth_handle_t get_example_eth_handle(void)
{
return s_eth_handle;
}
/* tear down connection, release resources */
void example_ethernet_shutdown(void)
{
if (s_semph_get_ip_addrs == NULL) {
return;
}
vSemaphoreDelete(s_semph_get_ip_addrs);
s_semph_get_ip_addrs = NULL;
#if CONFIG_EXAMPLE_CONNECT_IPV6
vSemaphoreDelete(s_semph_get_ip6_addrs);
s_semph_get_ip6_addrs = NULL;
#endif
eth_stop();
}
esp_err_t example_ethernet_connect(void)
{
#if CONFIG_EXAMPLE_CONNECT_IPV4
s_semph_get_ip_addrs = xSemaphoreCreateBinary();
if (s_semph_get_ip_addrs == NULL) {
return ESP_ERR_NO_MEM;
}
#endif
#if CONFIG_EXAMPLE_CONNECT_IPV6
s_semph_get_ip6_addrs = xSemaphoreCreateBinary();
if (s_semph_get_ip6_addrs == NULL) {
vSemaphoreDelete(s_semph_get_ip_addrs);
return ESP_ERR_NO_MEM;
}
#endif
eth_start();
ESP_LOGI(TAG, "Waiting for IP(s).");
#if CONFIG_EXAMPLE_CONNECT_IPV4
xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
#endif
#if CONFIG_EXAMPLE_CONNECT_IPV6
xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY);
#endif
return ESP_OK;
}

View File

@@ -1,44 +0,0 @@
/* Common utilities for socket address input interface:
The API get_addr_from_stdin() is mainly used by socket client examples which read IP address from stdin (if configured).
This option is typically used in the CI, but could be enabled in the project configuration.
In that case this component is used to receive a string that is evaluated and processed to output
socket structures to open a connectio
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include "lwip/sys.h"
#include <lwip/netdb.h>
#include <arpa/inet.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Read and evaluate IP address from stdin
*
* This API reads stdin and parses the input address using getaddrinfo()
* to fill in struct sockaddr_storage (for both IPv4 and IPv6) used to open
* a socket. IP protocol is guessed from the IP address string.
*
* @param[in] port port number of expected connection
* @param[in] sock_type expected protocol: SOCK_STREAM or SOCK_DGRAM
* @param[out] ip_protocol resultant IP protocol: IPPROTO_IP or IPPROTO_IP6
* @param[out] addr_family resultant address family: AF_INET or AF_INET6
* @param[out] dest_addr sockaddr_storage structure (for both IPv4 and IPv6)
* @return ESP_OK on success, ESP_FAIL otherwise
*/
esp_err_t get_addr_from_stdin(int port, int sock_type,
int *ip_protocol,
int *addr_family,
struct sockaddr_storage *dest_addr);
#ifdef __cplusplus
}
#endif

View File

@@ -1,58 +0,0 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/* Private Functions of protocol example common */
#pragma once
#include "esp_err.h"
#include "esp_wifi.h"
#include "sdkconfig.h"
#ifdef __cplusplus
extern "C" {
#endif
#if CONFIG_EXAMPLE_CONNECT_IPV6
#define MAX_IP6_ADDRS_PER_NETIF (5)
#if defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK)
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL
#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_GLOBAL)
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_GLOBAL
#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_SITE_LOCAL)
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_SITE_LOCAL
#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_UNIQUE_LOCAL)
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_UNIQUE_LOCAL
#endif // if-elif CONFIG_EXAMPLE_CONNECT_IPV6_PREF_...
#endif
#if CONFIG_EXAMPLE_CONNECT_IPV6
extern const char *example_ipv6_addr_types_to_str[6];
#endif
void example_wifi_start(void);
void example_wifi_stop(void);
esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait);
esp_err_t example_wifi_sta_do_disconnect(void);
bool example_is_our_netif(const char *prefix, esp_netif_t *netif);
void example_print_all_netif_ips(const char *prefix);
void example_wifi_shutdown(void);
esp_err_t example_wifi_connect(void);
void example_ethernet_shutdown(void);
esp_err_t example_ethernet_connect(void);
void example_thread_shutdown(void);
esp_err_t example_thread_connect(void);
esp_err_t example_ppp_connect(void);
void example_ppp_start(void);
void example_ppp_shutdown(void);
#ifdef __cplusplus
}
#endif

View File

@@ -1,154 +0,0 @@
/* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection.
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include "sdkconfig.h"
#include "esp_err.h"
#if !CONFIG_IDF_TARGET_LINUX
#include "esp_netif.h"
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
#include "esp_eth.h"
#endif
#endif // !CONFIG_IDF_TARGET_LINUX
#ifdef __cplusplus
extern "C" {
#endif
#if !CONFIG_IDF_TARGET_LINUX
#if CONFIG_EXAMPLE_CONNECT_WIFI
#define EXAMPLE_NETIF_DESC_STA "example_netif_sta"
#endif
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
#define EXAMPLE_NETIF_DESC_ETH "example_netif_eth"
#endif
#if CONFIG_EXAMPLE_CONNECT_THREAD
#define EXAMPLE_NETIF_DESC_THREAD "example_netif_thread"
#endif
#if CONFIG_EXAMPLE_CONNECT_PPP
#define EXAMPLE_NETIF_DESC_PPP "example_netif_ppp"
#endif
#if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST
#define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN
#elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL
#define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
#endif
#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
#endif
#if CONFIG_EXAMPLE_WIFI_AUTH_OPEN
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
#elif CONFIG_EXAMPLE_WIFI_AUTH_WEP
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
#elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
#endif
/* Example default interface, prefer the ethernet one if running in example-test (CI) configuration */
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
#define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH)
#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH)
#elif CONFIG_EXAMPLE_CONNECT_WIFI
#define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA)
#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA)
#elif CONFIG_EXAMPLE_CONNECT_THREAD
#define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_THREAD)
#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_THREAD)
#elif CONFIG_EXAMPLE_CONNECT_PPP
#define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_PPP)
#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_PPP)
#endif
/**
* @brief Configure Wi-Fi or Ethernet, connect, wait for IP
*
* This all-in-one helper function is used in protocols examples to
* reduce the amount of boilerplate in the example.
*
* It is not intended to be used in real world applications.
* See examples under examples/wifi/getting_started/ and examples/ethernet/
* for more complete Wi-Fi or Ethernet initialization code.
*
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*
* @return ESP_OK on successful connection
*/
esp_err_t example_connect(void);
/**
* Counterpart to example_connect, de-initializes Wi-Fi or Ethernet
*/
esp_err_t example_disconnect(void);
/**
* @brief Configure stdin and stdout to use blocking I/O
*
* This helper function is used in ASIO examples. It wraps installing the
* UART driver and configuring VFS layer to use UART driver for console I/O.
*/
esp_err_t example_configure_stdin_stdout(void);
/**
* @brief Returns esp-netif pointer created by example_connect() described by
* the supplied desc field
*
* @param desc Textual interface of created network interface, for example "sta"
* indicate default WiFi station, "eth" default Ethernet interface.
*
*/
esp_netif_t *get_example_netif_from_desc(const char *desc);
#if CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD
/**
* @brief Register wifi connect commands
*
* Provide a simple wifi_connect command in esp_console.
* This function can be used after esp_console is initialized.
*/
void example_register_wifi_connect_commands(void);
#endif
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
/**
* @brief Get the example Ethernet driver handle
*
* @return esp_eth_handle_t
*/
esp_eth_handle_t get_example_eth_handle(void);
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
#else
static inline esp_err_t example_connect(void) {return ESP_OK;}
#endif // !CONFIG_IDF_TARGET_LINUX
#ifdef __cplusplus
}
#endif

View File

@@ -1,115 +0,0 @@
/*
* Thread configurations for protocol examples
*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#pragma once
#include <sdkconfig.h>
#include <esp_openthread_types.h>
#ifdef CONFIG_OPENTHREAD_RADIO_NATIVE
#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \
{ \
.radio_mode = RADIO_MODE_NATIVE, \
}
#elif defined(CONFIG_OPENTHREAD_RADIO_SPINEL_UART)
#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \
{ \
.radio_mode = RADIO_MODE_UART_RCP, \
.radio_uart_config = \
{ \
.port = CONFIG_EXAMPLE_THREAD_UART_PORT, \
.uart_config = \
{ \
.baud_rate = CONFIG_EXAMPLE_THREAD_UART_BAUD, \
.data_bits = UART_DATA_8_BITS, \
.parity = UART_PARITY_DISABLE, \
.stop_bits = UART_STOP_BITS_1, \
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE, \
.rx_flow_ctrl_thresh = 0, \
.source_clk = UART_SCLK_DEFAULT, \
}, \
.rx_pin = CONFIG_EXAMPLE_THREAD_UART_RX_PIN, \
.tx_pin = CONFIG_EXAMPLE_THREAD_UART_TX_PIN, \
}, \
}
#elif defined(CONFIG_OPENTHREAD_RADIO_SPINEL_SPI)
#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \
{ \
.radio_mode = RADIO_MODE_SPI_RCP, \
.radio_spi_config = \
{ \
.host_device = SPI2_HOST, \
.dma_channel = 2, \
.spi_interface = \
{ \
.mosi_io_num = CONFIG_EXAMPLE_THREAD_SPI_MOSI_PIN, \
.miso_io_num = CONFIG_EXAMPLE_THREAD_SPI_MISO_PIN, \
.sclk_io_num = CONFIG_EXAMPLE_THREAD_SPI_SCLK_PIN, \
.quadwp_io_num = -1, \
.quadhd_io_num = -1, \
}, \
.spi_device = \
{ \
.cs_ena_pretrans = 2, \
.input_delay_ns = 100, \
.mode = 0, \
.clock_speed_hz = 2500 * 1000, \
.spics_io_num = CONFIG_EXAMPLE_THREAD_SPI_CS_PIN, \
.queue_size = 5, \
}, \
.intr_pin = CONFIG_EXAMPLE_THREAD_SPI_INTR_PIN, \
}, \
}
#else
#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \
{ \
.radio_mode = RADIO_MODE_TREL, \
}
#endif
#if CONFIG_OPENTHREAD_CONSOLE_TYPE_UART
#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \
{ \
.host_connection_mode = HOST_CONNECTION_MODE_CLI_UART, \
.host_uart_config = \
{ \
.port = 0, \
.uart_config = \
{ \
.baud_rate = 115200, \
.data_bits = UART_DATA_8_BITS, \
.parity = UART_PARITY_DISABLE, \
.stop_bits = UART_STOP_BITS_1, \
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE, \
.rx_flow_ctrl_thresh = 0, \
.source_clk = UART_SCLK_DEFAULT, \
}, \
.rx_pin = UART_PIN_NO_CHANGE, \
.tx_pin = UART_PIN_NO_CHANGE, \
}, \
}
#elif CONFIG_OPENTHREAD_CONSOLE_TYPE_USB_SERIAL_JTAG
#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \
{ \
.host_connection_mode = HOST_CONNECTION_MODE_CLI_USB, \
.host_usb_config = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT(), \
}
#else
#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \
{ \
.host_connection_mode = HOST_CONNECTION_MODE_NONE, \
}
#endif
#define ESP_OPENTHREAD_DEFAULT_PORT_CONFIG() \
{ \
.storage_partition_name = "nvs", \
.netif_queue_size = 10, \
.task_queue_size = 10, \
}

View File

@@ -1,49 +0,0 @@
/*
* Utility functions for protocol examples
*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Encode an URI
*
* @param dest a destination memory location
* @param src the source string
* @param len the length of the source string
* @return uint32_t the count of escaped characters
*
* @note Please allocate the destination buffer keeping in mind that encoding a
* special character will take up 3 bytes (for '%' and two hex digits).
* In the worst-case scenario, the destination buffer will have to be 3 times
* that of the source string.
*/
uint32_t example_uri_encode(char *dest, const char *src, size_t len);
/**
* @brief Decode an URI
*
* @param dest a destination memory location
* @param src the source string
* @param len the length of the source string
*
* @note Please allocate the destination buffer keeping in mind that a decoded
* special character will take up 2 less bytes than its encoded form.
* In the worst-case scenario, the destination buffer will have to be
* the same size that of the source string.
*/
void example_uri_decode(char *dest, const char *src, size_t len);
#ifdef __cplusplus
}
#endif

View File

@@ -1,260 +0,0 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <string.h>
#include <stdint.h>
#include "sdkconfig.h"
#include "protocol_examples_common.h"
#include "example_common_private.h"
#if CONFIG_EXAMPLE_CONNECT_PPP
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_netif_ppp.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB
#include "tinyusb.h"
#include "tusb_cdc_acm.h"
static int s_itf;
static uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE];
#else // DEVICE is UART
#include "driver/uart.h"
#define BUF_SIZE (1024)
static bool s_stop_task = false;
#endif // CONNECT_PPP_DEVICE
static const char *TAG = "example_connect_ppp";
static int s_retry_num = 0;
static EventGroupHandle_t s_event_group = NULL;
static esp_netif_t *s_netif;
static const int GOT_IPV4 = BIT0;
static const int CONNECTION_FAILED = BIT1;
#if CONFIG_EXAMPLE_CONNECT_IPV6
static const int GOT_IPV6 = BIT2;
#define CONNECT_BITS (GOT_IPV4|GOT_IPV6|CONNECTION_FAILED)
#else
#define CONNECT_BITS (GOT_IPV4|CONNECTION_FAILED)
#endif
static esp_err_t transmit(void *h, void *buffer, size_t len)
{
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_VERBOSE);
#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB
tinyusb_cdcacm_write_queue(s_itf, buffer, len);
tinyusb_cdcacm_write_flush(s_itf, 0);
#else // DEVICE_UART
uart_write_bytes(UART_NUM_1, buffer, len);
#endif // CONNECT_PPP_DEVICE
return ESP_OK;
}
static esp_netif_driver_ifconfig_t driver_cfg = {
.handle = (void *)1, // singleton driver, just to != NULL
.transmit = transmit,
};
const esp_netif_driver_ifconfig_t *ppp_driver_cfg = &driver_cfg;
static void on_ip_event(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
if (event_id == IP_EVENT_PPP_GOT_IP) {
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
if (!example_is_our_netif(EXAMPLE_NETIF_DESC_PPP, event->esp_netif)) {
return;
}
esp_netif_t *netif = event->esp_netif;
esp_netif_dns_info_t dns_info;
ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip));
esp_netif_get_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_info);
ESP_LOGI(TAG, "Main DNS server : " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4));
xEventGroupSetBits(s_event_group, GOT_IPV4);
#if CONFIG_EXAMPLE_CONNECT_IPV6
} else if (event_id == IP_EVENT_GOT_IP6) {
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
if (!example_is_our_netif(EXAMPLE_NETIF_DESC_PPP, event->esp_netif)) {
return;
}
esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip);
ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif),
IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) {
xEventGroupSetBits(s_event_group, GOT_IPV6);
}
#endif
} else if (event_id == IP_EVENT_PPP_LOST_IP) {
ESP_LOGI(TAG, "Disconnect from PPP Server");
s_retry_num++;
if (s_retry_num > CONFIG_EXAMPLE_PPP_CONN_MAX_RETRY) {
ESP_LOGE(TAG, "PPP Connection failed %d times, stop reconnecting.", s_retry_num);
xEventGroupSetBits(s_event_group, CONNECTION_FAILED);
} else {
ESP_LOGI(TAG, "PPP Connection failed %d times, try to reconnect.", s_retry_num);
esp_netif_action_start(s_netif, 0, 0, 0);
esp_netif_action_connected(s_netif, 0, 0, 0);
}
}
}
#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB
static void cdc_rx_callback(int itf, cdcacm_event_t *event)
{
size_t rx_size = 0;
if (itf != s_itf) {
// Not our channel
return;
}
esp_err_t ret = tinyusb_cdcacm_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size);
if (ret == ESP_OK) {
ESP_LOG_BUFFER_HEXDUMP(TAG, buf, rx_size, ESP_LOG_VERBOSE);
// pass the received data to the network interface
esp_netif_receive(s_netif, buf, rx_size, NULL);
} else {
ESP_LOGE(TAG, "Read error");
}
}
static void line_state_changed(int itf, cdcacm_event_t *event)
{
s_itf = itf; // use this channel for the netif communication
ESP_LOGI(TAG, "Line state changed on channel %d", itf);
}
#else // DEVICE is UART
static void ppp_task(void *args)
{
uart_config_t uart_config = {};
uart_config.baud_rate = CONFIG_EXAMPLE_CONNECT_UART_BAUDRATE;
uart_config.data_bits = UART_DATA_8_BITS;
uart_config.parity = UART_PARITY_DISABLE;
uart_config.stop_bits = UART_STOP_BITS_1;
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
uart_config.source_clk = UART_SCLK_DEFAULT;
QueueHandle_t event_queue;
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_1, BUF_SIZE, 0, 16, &event_queue, 0));
ESP_ERROR_CHECK(uart_param_config(UART_NUM_1, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_1, CONFIG_EXAMPLE_CONNECT_UART_TX_PIN, CONFIG_EXAMPLE_CONNECT_UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_set_rx_timeout(UART_NUM_1, 1));
char *buffer = (char*)malloc(BUF_SIZE);
uart_event_t event;
esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, esp_netif_action_connected, s_netif);
esp_netif_action_start(s_netif, 0, 0, 0);
esp_netif_action_connected(s_netif, 0, 0, 0);
while (!s_stop_task) {
xQueueReceive(event_queue, &event, pdMS_TO_TICKS(1000));
if (event.type == UART_DATA) {
size_t len;
uart_get_buffered_data_len(UART_NUM_1, &len);
if (len) {
len = uart_read_bytes(UART_NUM_1, buffer, BUF_SIZE, 0);
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_VERBOSE);
esp_netif_receive(s_netif, buffer, len, NULL);
}
} else {
ESP_LOGW(TAG, "Received UART event: %d", event.type);
}
}
free(buffer);
vTaskDelete(NULL);
}
#endif // CONNECT_PPP_DEVICE
esp_err_t example_ppp_connect(void)
{
ESP_LOGI(TAG, "Start example_connect.");
#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB
ESP_LOGI(TAG, "USB initialization");
const tinyusb_config_t tusb_cfg = {
.device_descriptor = NULL,
.string_descriptor = NULL,
.external_phy = false,
.configuration_descriptor = NULL,
};
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
tinyusb_config_cdcacm_t acm_cfg = {
.usb_dev = TINYUSB_USBDEV_0,
.cdc_port = TINYUSB_CDC_ACM_0,
.callback_rx = &cdc_rx_callback,
.callback_rx_wanted_char = NULL,
.callback_line_state_changed = NULL,
.callback_line_coding_changed = NULL
};
ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg));
/* the second way to register a callback */
ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback(
TINYUSB_CDC_ACM_0,
CDC_EVENT_LINE_STATE_CHANGED,
&line_state_changed));
#endif // CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB
s_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, on_ip_event, NULL));
esp_netif_inherent_config_t base_netif_cfg = ESP_NETIF_INHERENT_DEFAULT_PPP();
base_netif_cfg.if_desc = EXAMPLE_NETIF_DESC_PPP;
esp_netif_config_t netif_ppp_config = { .base = &base_netif_cfg,
.driver = ppp_driver_cfg,
.stack = ESP_NETIF_NETSTACK_DEFAULT_PPP
};
s_netif = esp_netif_new(&netif_ppp_config);
assert(s_netif);
#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_USB
esp_netif_action_start(s_netif, 0, 0, 0);
esp_netif_action_connected(s_netif, 0, 0, 0);
#else // DEVICE is UART
s_stop_task = false;
if (xTaskCreate(ppp_task, "ppp connect", 4096, NULL, 5, NULL) != pdTRUE) {
ESP_LOGE(TAG, "Failed to create a ppp connection task");
return ESP_FAIL;
}
#endif // CONNECT_PPP_DEVICE
ESP_LOGI(TAG, "Waiting for IP address");
EventBits_t bits = xEventGroupWaitBits(s_event_group, CONNECT_BITS, pdFALSE, pdFALSE, portMAX_DELAY);
if (bits & CONNECTION_FAILED) {
ESP_LOGE(TAG, "Connection failed!");
return ESP_FAIL;
}
ESP_LOGI(TAG, "Connected!");
return ESP_OK;
}
void example_ppp_shutdown(void)
{
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, on_ip_event));
#if CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_UART
s_stop_task = true;
vTaskDelay(pdMS_TO_TICKS(1000)); // wait for the ppp task to stop
#endif
esp_netif_action_disconnected(s_netif, 0, 0, 0);
vEventGroupDelete(s_event_group);
esp_netif_action_stop(s_netif, 0, 0, 0);
esp_netif_destroy(s_netif);
s_netif = NULL;
s_event_group = NULL;
}
#endif // CONFIG_EXAMPLE_CONNECT_PPP

View File

@@ -1,388 +0,0 @@
/*
* Utility functions for protocol examples
*
* SPDX-FileCopyrightText: 2002-2021 Igor Sysoev
* 2011-2022 Nginx, Inc.
*
* SPDX-License-Identifier: BSD-2-Clause
*
* SPDX-FileContributor: 2023 Espressif Systems (Shanghai) CO LTD
*/
/*
* Copyright (C) 2002-2021 Igor Sysoev
* Copyright (C) 2011-2022 Nginx, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "protocol_examples_utils.h"
/* Type of Escape algorithms to be used */
#define NGX_ESCAPE_URI (0)
#define NGX_ESCAPE_ARGS (1)
#define NGX_ESCAPE_URI_COMPONENT (2)
#define NGX_ESCAPE_HTML (3)
#define NGX_ESCAPE_REFRESH (4)
#define NGX_ESCAPE_MEMCACHED (5)
#define NGX_ESCAPE_MAIL_AUTH (6)
/* Type of Unescape algorithms to be used */
#define NGX_UNESCAPE_URI (1)
#define NGX_UNESCAPE_REDIRECT (2)
uintptr_t ngx_escape_uri(u_char *dst, u_char *src, size_t size, unsigned int type)
{
unsigned int n;
uint32_t *escape;
static u_char hex[] = "0123456789ABCDEF";
/*
* Per RFC 3986 only the following chars are allowed in URIs unescaped:
*
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
* / "*" / "+" / "," / ";" / "="
*
* And "%" can appear as a part of escaping itself. The following
* characters are not allowed and need to be escaped: %00-%1F, %7F-%FF,
* " ", """, "<", ">", "\", "^", "`", "{", "|", "}".
*/
/* " ", "#", "%", "?", not allowed */
static uint32_t uri[] = {
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0xd000002d, /* 1101 0000 0000 0000 0000 0000 0010 1101 */
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
};
/* " ", "#", "%", "&", "+", ";", "?", not allowed */
static uint32_t args[] = {
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0xd800086d, /* 1101 1000 0000 0000 0000 1000 0110 1101 */
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
};
/* not ALPHA, DIGIT, "-", ".", "_", "~" */
static uint32_t uri_component[] = {
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0xfc009fff, /* 1111 1100 0000 0000 1001 1111 1111 1111 */
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0x78000001, /* 0111 1000 0000 0000 0000 0000 0000 0001 */
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
};
/* " ", "#", """, "%", "'", not allowed */
static uint32_t html[] = {
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0x500000ad, /* 0101 0000 0000 0000 0000 0000 1010 1101 */
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
};
/* " ", """, "'", not allowed */
static uint32_t refresh[] = {
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0x50000085, /* 0101 0000 0000 0000 0000 0000 1000 0101 */
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
0xd8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
};
/* " ", "%", %00-%1F */
static uint32_t memcached[] = {
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0x00000021, /* 0000 0000 0000 0000 0000 0000 0010 0001 */
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
};
/* mail_auth is the same as memcached */
static uint32_t *map[] =
{ uri, args, uri_component, html, refresh, memcached, memcached };
escape = map[type];
if (dst == NULL) {
/* find the number of the characters to be escaped */
n = 0;
while (size) {
if (escape[*src >> 5] & (1U << (*src & 0x1f))) {
n++;
}
src++;
size--;
}
return (uintptr_t) n;
}
while (size) {
if (escape[*src >> 5] & (1U << (*src & 0x1f))) {
*dst++ = '%';
*dst++ = hex[*src >> 4];
*dst++ = hex[*src & 0xf];
src++;
} else {
*dst++ = *src++;
}
size--;
}
return (uintptr_t) dst;
}
void ngx_unescape_uri(u_char **dst, u_char **src, size_t size, unsigned int type)
{
u_char *d, *s, ch, c, decoded;
enum {
sw_usual = 0,
sw_quoted,
sw_quoted_second
} state;
d = *dst;
s = *src;
state = 0;
decoded = 0;
while (size--) {
ch = *s++;
switch (state) {
case sw_usual:
if (ch == '?'
&& (type & (NGX_UNESCAPE_URI | NGX_UNESCAPE_REDIRECT))) {
*d++ = ch;
goto done;
}
if (ch == '%') {
state = sw_quoted;
break;
}
*d++ = ch;
break;
case sw_quoted:
if (ch >= '0' && ch <= '9') {
decoded = (u_char) (ch - '0');
state = sw_quoted_second;
break;
}
c = (u_char) (ch | 0x20);
if (c >= 'a' && c <= 'f') {
decoded = (u_char) (c - 'a' + 10);
state = sw_quoted_second;
break;
}
/* the invalid quoted character */
state = sw_usual;
*d++ = ch;
break;
case sw_quoted_second:
state = sw_usual;
if (ch >= '0' && ch <= '9') {
ch = (u_char) ((decoded << 4) + (ch - '0'));
if (type & NGX_UNESCAPE_REDIRECT) {
if (ch > '%' && ch < 0x7f) {
*d++ = ch;
break;
}
*d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
break;
}
*d++ = ch;
break;
}
c = (u_char) (ch | 0x20);
if (c >= 'a' && c <= 'f') {
ch = (u_char) ((decoded << 4) + (c - 'a') + 10);
if (type & NGX_UNESCAPE_URI) {
if (ch == '?') {
*d++ = ch;
goto done;
}
*d++ = ch;
break;
}
if (type & NGX_UNESCAPE_REDIRECT) {
if (ch == '?') {
*d++ = ch;
goto done;
}
if (ch > '%' && ch < 0x7f) {
*d++ = ch;
break;
}
*d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
break;
}
*d++ = ch;
break;
}
/* the invalid quoted character */
break;
}
}
done:
*dst = d;
*src = s;
}
uint32_t example_uri_encode(char *dest, const char *src, size_t len)
{
if (!src || !dest) {
return 0;
}
uintptr_t ret = ngx_escape_uri((unsigned char *)dest, (unsigned char *)src, len, NGX_ESCAPE_URI_COMPONENT);
return (uint32_t)(ret - (uintptr_t)dest);
}
void example_uri_decode(char *dest, const char *src, size_t len)
{
if (!src || !dest) {
return;
}
unsigned char *src_ptr = (unsigned char *)src;
unsigned char *dst_ptr = (unsigned char *)dest;
ngx_unescape_uri(&dst_ptr, &src_ptr, len, NGX_UNESCAPE_URI);
}

View File

@@ -1,32 +0,0 @@
/* Common functions for protocol examples, to configure stdin and stdout.
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "protocol_examples_common.h"
#include "esp_err.h"
#include "driver/uart_vfs.h"
#include "driver/uart.h"
#include "sdkconfig.h"
esp_err_t example_configure_stdin_stdout(void)
{
if (uart_is_driver_installed((uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM)) {
return ESP_OK;
}
// Initialize VFS & UART so we can use std::cout/cin
setvbuf(stdin, NULL, _IONBF, 0);
/* Install UART driver for interrupt-driven reads and writes */
ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM,
256, 0, 0, NULL, 0) );
/* Tell VFS to use UART driver */
uart_vfs_dev_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
uart_vfs_dev_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
/* Move the caret to the beginning of the next line on '\n' */
uart_vfs_dev_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
return ESP_OK;
}

View File

@@ -1,130 +0,0 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include "esp_err.h"
#include "esp_event.h"
#include "esp_event_base.h"
#include "esp_vfs_eventfd.h"
#include "example_common_private.h"
#include "protocol_examples_common.h"
#include "protocol_examples_thread_config.h"
#include "esp_log.h"
#include <string.h>
#include <esp_openthread_cli.h>
#include <esp_openthread_lock.h>
#include <esp_openthread_netif_glue.h>
#include <esp_openthread_types.h>
#include <esp_openthread.h>
#include <openthread/dataset.h>
#include <openthread/logging.h>
static TaskHandle_t s_ot_task_handle = NULL;
static esp_netif_t *s_openthread_netif = NULL;
static SemaphoreHandle_t s_semph_thread_attached = NULL;
static SemaphoreHandle_t s_semph_thread_set_dns_server = NULL;
static const char *TAG = "example_connect";
static void thread_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id,
void* event_data)
{
if (event_base == OPENTHREAD_EVENT) {
if (event_id == OPENTHREAD_EVENT_ATTACHED) {
xSemaphoreGive(s_semph_thread_attached);
} else if (event_id == OPENTHREAD_EVENT_SET_DNS_SERVER) {
xSemaphoreGive(s_semph_thread_set_dns_server);
}
}
}
static void ot_task_worker(void *aContext)
{
esp_openthread_platform_config_t config = {
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
};
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_OPENTHREAD();
esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_THREAD;
esp_netif_config_t cfg = {
.base = &esp_netif_config,
.stack = &g_esp_netif_netstack_default_openthread,
};
s_openthread_netif = esp_netif_new(&cfg);
assert(s_openthread_netif != NULL);
// Initialize the OpenThread stack
ESP_ERROR_CHECK(esp_openthread_init(&config));
ESP_ERROR_CHECK(esp_netif_attach(s_openthread_netif, esp_openthread_netif_glue_init(&config)));
esp_openthread_lock_acquire(portMAX_DELAY);
(void)otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
esp_openthread_cli_init();
esp_openthread_cli_create_task();
otOperationalDatasetTlvs dataset;
otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset);
if (error != OT_ERROR_NONE) {
ESP_ERROR_CHECK(esp_openthread_auto_start(NULL));
} else {
ESP_ERROR_CHECK(esp_openthread_auto_start(&dataset));
}
esp_openthread_lock_release();
// Run the main loop
esp_openthread_launch_mainloop();
// Clean up
esp_openthread_netif_glue_deinit();
esp_netif_destroy(s_openthread_netif);
esp_vfs_eventfd_unregister();
vTaskDelete(NULL);
}
/* tear down connection, release resources */
void example_thread_shutdown(void)
{
vTaskDelete(s_ot_task_handle);
esp_openthread_netif_glue_deinit();
esp_netif_destroy(s_openthread_netif);
esp_vfs_eventfd_unregister();
vSemaphoreDelete(s_semph_thread_set_dns_server);
vSemaphoreDelete(s_semph_thread_attached);
}
esp_err_t example_thread_connect(void)
{
s_semph_thread_attached = xSemaphoreCreateBinary();
if (s_semph_thread_attached == NULL) {
return ESP_ERR_NO_MEM;
}
s_semph_thread_set_dns_server = xSemaphoreCreateBinary();
if (s_semph_thread_set_dns_server == NULL) {
vSemaphoreDelete(s_semph_thread_attached);
return ESP_ERR_NO_MEM;
}
// 4 eventfds might be used for Thread
// * netif
// * ot task queue
// * radio driver
// * border router
esp_vfs_eventfd_config_t eventfd_config = {
.max_fds = 4,
};
esp_vfs_eventfd_register(&eventfd_config);
ESP_ERROR_CHECK(esp_event_handler_register(OPENTHREAD_EVENT, ESP_EVENT_ANY_ID, thread_event_handler, NULL));
if (xTaskCreate(ot_task_worker, "ot_br_main", CONFIG_EXAMPLE_THREAD_TASK_STACK_SIZE, NULL, 5, &s_ot_task_handle) != pdPASS) {
vSemaphoreDelete(s_semph_thread_attached);
vSemaphoreDelete(s_semph_thread_set_dns_server);
ESP_LOGE(TAG, "Failed to create openthread task");
return ESP_FAIL;
}
xSemaphoreTake(s_semph_thread_attached, portMAX_DELAY);
// Wait 1s for the Thread device to set its DNS server with the NAT64 prefix.
if (xSemaphoreTake(s_semph_thread_set_dns_server, 1000 / portTICK_PERIOD_MS) != pdPASS) {
ESP_LOGW(TAG, "DNS server is not set for the Thread device, might be unable to access the Internet");
}
return ESP_OK;
}

View File

@@ -1,247 +0,0 @@
/*
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection.
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include "protocol_examples_common.h"
#include "example_common_private.h"
#include "esp_log.h"
#if CONFIG_EXAMPLE_CONNECT_WIFI
static const char *TAG = "example_connect";
static esp_netif_t *s_example_sta_netif = NULL;
static SemaphoreHandle_t s_semph_get_ip_addrs = NULL;
#if CONFIG_EXAMPLE_CONNECT_IPV6
static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL;
#endif
static int s_retry_num = 0;
static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
s_retry_num++;
if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) {
ESP_LOGI(TAG, "WiFi Connect failed %d times, stop reconnect.", s_retry_num);
/* let example_wifi_sta_do_connect() return */
if (s_semph_get_ip_addrs) {
xSemaphoreGive(s_semph_get_ip_addrs);
}
#if CONFIG_EXAMPLE_CONNECT_IPV6
if (s_semph_get_ip6_addrs) {
xSemaphoreGive(s_semph_get_ip6_addrs);
}
#endif
example_wifi_sta_do_disconnect();
return;
}
wifi_event_sta_disconnected_t *disconn = event_data;
if (disconn->reason == WIFI_REASON_ROAMING) {
ESP_LOGD(TAG, "station roaming, do nothing");
return;
}
ESP_LOGI(TAG, "Wi-Fi disconnected %d, trying to reconnect...", disconn->reason);
esp_err_t err = esp_wifi_connect();
if (err == ESP_ERR_WIFI_NOT_STARTED) {
return;
}
ESP_ERROR_CHECK(err);
}
static void example_handler_on_wifi_connect(void *esp_netif, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
#if CONFIG_EXAMPLE_CONNECT_IPV6
esp_netif_create_ip6_linklocal(esp_netif);
#endif // CONFIG_EXAMPLE_CONNECT_IPV6
}
static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
s_retry_num = 0;
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) {
return;
}
ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip));
if (s_semph_get_ip_addrs) {
xSemaphoreGive(s_semph_get_ip_addrs);
} else {
ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&event->ip_info.ip));
}
}
#if CONFIG_EXAMPLE_CONNECT_IPV6
static void example_handler_on_sta_got_ipv6(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) {
return;
}
esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip);
ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif),
IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) {
if (s_semph_get_ip6_addrs) {
xSemaphoreGive(s_semph_get_ip6_addrs);
} else {
ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
}
}
}
#endif // CONFIG_EXAMPLE_CONNECT_IPV6
void example_wifi_start(void)
{
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
// Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask)
esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_STA;
esp_netif_config.route_prio = 128;
s_example_sta_netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config);
esp_wifi_set_default_wifi_sta_handlers();
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
}
void example_wifi_stop(void)
{
esp_err_t err = esp_wifi_stop();
if (err == ESP_ERR_WIFI_NOT_INIT) {
return;
}
ESP_ERROR_CHECK(err);
ESP_ERROR_CHECK(esp_wifi_deinit());
ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(s_example_sta_netif));
esp_netif_destroy(s_example_sta_netif);
s_example_sta_netif = NULL;
}
esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait)
{
if (wait) {
s_semph_get_ip_addrs = xSemaphoreCreateBinary();
if (s_semph_get_ip_addrs == NULL) {
return ESP_ERR_NO_MEM;
}
#if CONFIG_EXAMPLE_CONNECT_IPV6
s_semph_get_ip6_addrs = xSemaphoreCreateBinary();
if (s_semph_get_ip6_addrs == NULL) {
vSemaphoreDelete(s_semph_get_ip_addrs);
return ESP_ERR_NO_MEM;
}
#endif
}
s_retry_num = 0;
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect, s_example_sta_netif));
#if CONFIG_EXAMPLE_CONNECT_IPV6
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6, NULL));
#endif
ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
esp_err_t ret = esp_wifi_connect();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "WiFi connect failed! ret:%x", ret);
return ret;
}
if (wait) {
ESP_LOGI(TAG, "Waiting for IP(s)");
#if CONFIG_EXAMPLE_CONNECT_IPV4
xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
vSemaphoreDelete(s_semph_get_ip_addrs);
s_semph_get_ip_addrs = NULL;
#endif
#if CONFIG_EXAMPLE_CONNECT_IPV6
xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY);
vSemaphoreDelete(s_semph_get_ip6_addrs);
s_semph_get_ip6_addrs = NULL;
#endif
if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) {
return ESP_FAIL;
}
}
return ESP_OK;
}
esp_err_t example_wifi_sta_do_disconnect(void)
{
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect));
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip));
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect));
#if CONFIG_EXAMPLE_CONNECT_IPV6
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6));
#endif
return esp_wifi_disconnect();
}
void example_wifi_shutdown(void)
{
example_wifi_sta_do_disconnect();
example_wifi_stop();
}
esp_err_t example_wifi_connect(void)
{
ESP_LOGI(TAG, "Start example_connect.");
example_wifi_start();
wifi_config_t wifi_config = {
.sta = {
#if !CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN
.ssid = CONFIG_EXAMPLE_WIFI_SSID,
.password = CONFIG_EXAMPLE_WIFI_PASSWORD,
#endif
.scan_method = EXAMPLE_WIFI_SCAN_METHOD,
.sort_method = EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD,
.threshold.rssi = CONFIG_EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD,
.threshold.authmode = EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD,
},
};
#if CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN
example_configure_stdin_stdout();
char buf[sizeof(wifi_config.sta.ssid)+sizeof(wifi_config.sta.password)+2] = {0};
ESP_LOGI(TAG, "Please input ssid password:");
fgets(buf, sizeof(buf), stdin);
int len = strlen(buf);
buf[len-1] = '\0'; /* removes '\n' */
memset(wifi_config.sta.ssid, 0, sizeof(wifi_config.sta.ssid));
char *rest = NULL;
char *temp = strtok_r(buf, " ", &rest);
strncpy((char*)wifi_config.sta.ssid, temp, sizeof(wifi_config.sta.ssid));
memset(wifi_config.sta.password, 0, sizeof(wifi_config.sta.password));
temp = strtok_r(NULL, " ", &rest);
if (temp) {
strncpy((char*)wifi_config.sta.password, temp, sizeof(wifi_config.sta.password));
} else {
wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN;
}
#endif
return example_wifi_sta_do_connect(wifi_config, true);
}
#endif /* CONFIG_EXAMPLE_CONNECT_WIFI */

View File

@@ -0,0 +1,5 @@
idf_component_register(
SRCS "wifi-connect.c"
INCLUDE_DIRS "include"
REQUIRES esp_wifi esp_timer esp_event esp_netif nvs_flash esp_http_server lwip driver
)

View File

@@ -0,0 +1,88 @@
menu "WiFi 连接"
choice WIFI_CONNECT_PROVISION_MODE
prompt "配网模式"
default WIFI_CONNECT_PROVISION_MODE_AUTO
help
选择进入配网模式的方式。
config WIFI_CONNECT_PROVISION_MODE_BUTTON
bool "按键触发"
help
仅在检测到长按按键时进入配网模式。
config WIFI_CONNECT_PROVISION_MODE_AUTO
bool "开机自动配网,连接后关闭"
help
开机自动启动配网服务。
站点连接成功后等待指定时间后自动关闭配网热点和DNS服务。
endchoice
config WIFI_CONNECT_BUTTON_GPIO
int "配网按键GPIO"
range 0 21
default 9
help
用于进入配网模式的GPIO引脚。
config WIFI_CONNECT_BUTTON_ACTIVE_LEVEL
int "配网按键有效电平"
range 0 1
default 0
help
配网按键的有效电平。0表示低电平有效。
config WIFI_CONNECT_DEBOUNCE_MS
int "按键消抖时间 (毫秒)"
range 10 200
default 40
config WIFI_CONNECT_LONG_PRESS_MS
int "按键长按触发时间 (毫秒)"
range 500 10000
default 2000
config WIFI_CONNECT_BUTTON_STARTUP_GUARD_MS
int "按键启动保护时间 (毫秒)"
range 0 30000
default 5000
help
在启动保护窗口期内忽略按键长按检测。
当按键引脚与其他外设共享时使用。
config WIFI_CONNECT_BUTTON_RELEASE_ARM_MS
int "按键释放预载时间 (毫秒)"
range 20 2000
default 200
help
要求按键保持在释放电平的时间,
达到此时间后方可启用长按检测。
config WIFI_CONNECT_CONNECT_TIMEOUT_SEC
int "Wi-Fi 连接超时 (秒)"
range 5 180
default 30
config WIFI_CONNECT_IDLE_TIMEOUT_SEC
int "配网空闲超时 (秒)"
range 30 1800
default 300
config WIFI_CONNECT_MAX_SCAN_RESULTS
int "最大扫描结果数"
range 5 50
default 20
config WIFI_CONNECT_AP_MAX_CONNECTIONS
int "软AP最大连接数"
range 1 10
default 4
config WIFI_CONNECT_AP_GRACEFUL_STOP_SEC
int "站点连接后AP保持时间 (秒)"
range 0 120
default 15
endmenu

View File

@@ -0,0 +1,165 @@
# wifi-connect 组件说明
`wifi-connect` 是一个基于 ESP-IDF 的 Wi-Fi 配网组件,支持:
- 长按按键进入配网模式
- 启动 SoftAP + Captive Portal网页配网
- 手机连接热点后,通过网页扫描并选择路由器
- 保存 Wi-Fi 凭据到 NVS
- 下次开机自动重连
- 支持两种配网模式:按键触发 / 常驻配网
面向最终用户的一页版操作说明见:`USER_GUIDE.md`
现场打印张贴版(四步卡)见:`QUICK_POSTER.md`
---
## 目录结构
- `wifi-connect.c`组件主实现按键、APSTA、HTTP、DNS、状态机
- `include/wifi-connect.h`:对外 API
- `Kconfig.projbuild`:组件配置项
- `CMakeLists.txt`:组件构建依赖
---
## 对外 API
头文件:`include/wifi-connect.h`
- `esp_err_t wifi_connect_init(void);`
- 初始化组件NVS、Wi-Fi、事件、按键任务等
- 尝试自动连接已保存网络
- `esp_err_t wifi_connect_start(void);`
- 启动配网APSTA + HTTP + DNS
- `esp_err_t wifi_connect_stop(void);`
- 停止配网(关闭热点与相关服务)
- `wifi_connect_status_t wifi_connect_get_status(void);`
- 获取当前状态:`idle / provisioning / connecting / connected / failed / timeout`
- `esp_err_t wifi_connect_get_config(wifi_connect_config_t *config);`
- 读取已保存的 Wi-Fi 凭据
- `esp_err_t wifi_connect_clear_config(void);`
- 清除已保存的 Wi-Fi 凭据SSID/密码)
---
## 快速使用
`main/main.c`
```c
#include "esp_check.h"
#include "wifi-connect.h"
void app_main(void)
{
ESP_ERROR_CHECK(wifi_connect_init());
}
```
运行后:
1. 选择配网模式:
- 按键触发模式:长按配置按键进入配网
- 常驻配网模式:上电自动进入配网
2. 手机连接 `ESP32-xxxxxx` 热点
3. 打开 `http://192.168.4.1`
4. 选择 Wi-Fi 并输入密码提交
5. 配网行为:
- 按键触发模式:连接成功后按配置自动关闭热点
- 常驻配网模式:配网热点保持开启,不自动关闭
如需清空历史凭据,可在配网页面点击“清除已保存”。
---
## Kconfig 配置项
`idf.py menuconfig` 中:`WiFi Connect` 菜单
- `Provisioning mode`:配网模式(二选一)
- `Button triggered`:按键触发配网(默认)
- `Always-on provisioning`:常驻配网(上电自动进入且不自动关闭)
- `WIFI_CONNECT_BUTTON_GPIO`:进入配网的按键 GPIO
- `WIFI_CONNECT_BUTTON_ACTIVE_LEVEL`:按键有效电平
- `WIFI_CONNECT_DEBOUNCE_MS`:按键去抖时间
- `WIFI_CONNECT_LONG_PRESS_MS`:长按触发时长
- `WIFI_CONNECT_BUTTON_STARTUP_GUARD_MS`:上电保护窗口(该时间内忽略长按检测)
- `WIFI_CONNECT_BUTTON_RELEASE_ARM_MS`:松手解锁时间(先稳定松手再允许长按触发)
- `WIFI_CONNECT_CONNECT_TIMEOUT_SEC`:连接路由器超时
- `WIFI_CONNECT_IDLE_TIMEOUT_SEC`:配网页面空闲超时
- `WIFI_CONNECT_MAX_SCAN_RESULTS`:扫描网络最大数量
- `WIFI_CONNECT_AP_MAX_CONNECTIONS`SoftAP 最大连接数
- `WIFI_CONNECT_AP_GRACEFUL_STOP_SEC`:联网成功后 AP 延迟关闭秒数
---
## 日志与状态说明(中文)
组件会输出统一中文状态日志,例如:
- `【状态】wifi-connect 初始化完成`
- `【状态】检测到按键长按:开始进入配网模式`
- `【状态】配网已启动配网热点已开启SSID=...`
- `【状态】开始连接路由器:收到配网请求,目标网络=...`
- `【状态】联网成功:已连接 ...,获取 IP=...`
- `【状态】配网已停止:热点已关闭,设备继续以 STA 模式运行`
说明ESP-IDF 驱动层(如 `wifi:``esp_netif_lwip:`)仍会输出英文日志,这是框架默认行为。
---
## 常见问题
### 1) 手机连上热点但不自动弹出页面
- 手动访问:`http://192.168.4.1`
- 确认手机没有强制使用 HTTPS
- 查看串口是否有 `配网已启动``DNS 劫持服务已启动` 日志
### 2) 提交后连接失败
- 检查密码是否正确
- 查看日志中的失败原因码(`连接失败,原因=...`
- 检查路由器是否禁用了新设备接入
- 若曾保存过旧配置,可先在页面点击“清除已保存”后再重试
### 4) 按键未按下却误触发配网
- 常见原因是按键引脚与 LCD/外设复用,初始化期间电平抖动被误判为长按
- 可增大 `WIFI_CONNECT_BUTTON_STARTUP_GUARD_MS`(如 8000~10000
- 可增大 `WIFI_CONNECT_BUTTON_RELEASE_ARM_MS`(如 300~500
- 若硬件允许,优先给配网按键使用独立 GPIO
### 5) 成功后热点消失是否正常
- 在按键触发模式下:正常,可通过 `WIFI_CONNECT_AP_GRACEFUL_STOP_SEC` 调整关闭延时
- 在常驻配网模式下:热点不会自动关闭
---
## 依赖
`CMakeLists.txt` 声明:
- `esp_wifi`
- `esp_timer`
- `esp_event`
- `esp_netif`
- `nvs_flash`
- `esp_http_server`
- `lwip`
- `driver`
---
## 版本建议
- 推荐 ESP-IDF `v5.5.x`
- 当前项目验证环境:`esp-idf v5.5.2`ESP32-C3

View File

@@ -0,0 +1,82 @@
# BotanicalBuddy 配网操作手册(用户版)
> 适用对象:现场测试、家人用户、非开发人员
> 目标:让设备快速连上家里 Wi-Fi
---
## 1. 开始前准备
- 手机已打开 Wi-Fi
- 记住家里 Wi-Fi 名称和密码
- 设备已上电
---
## 2. 进入配网模式
有两种工作模式,请按项目配置使用:
1. 按键触发模式:长按设备上的配网按键(约 2 秒)
2. 常驻配网模式:设备上电后会自动开启配网
3. 当看到“配网已启动”后,手机 Wi-Fi 列表会出现:`ESP32-xxxxxx`
---
## 3. 手机连接设备热点
1. 在手机 Wi-Fi 中连接 `ESP32-xxxxxx`
2. 若系统自动弹出页面,直接进入下一步
3. 若没有自动弹出,手动打开浏览器输入:
`http://192.168.4.1`
> 注意:必须是 `http`,不要用 `https`
---
## 4. 选择路由器并提交
1. 点击“扫描网络”
2. 选择你家的 Wi-Fi
3. 输入 Wi-Fi 密码
4. 点击“连接”
成功后页面会提示连接成功:
- 按键触发模式:设备热点会在几秒后自动关闭(正常现象)
- 常驻配网模式:设备热点保持开启(正常现象)
---
## 5. 成功后的现象
- 设备不再广播 `ESP32-xxxxxx`
- 串口会显示“联网成功,获取 IP=... ”
- 设备进入正常工作状态
---
## 6. 常见问题
### Q1手机连上热点但打不开页面
- 手动访问:`http://192.168.4.1`
- 关闭手机“私人 DNS / 智能网络切换 / VPN”后重试
- 确认没有强制跳 HTTPS
### Q2提示连接失败
- 检查 Wi-Fi 密码是否正确
- 确认路由器 2.4G 可用ESP32-C3 使用 2.4G
- 路由器信号太弱时,靠近路由器后重试
### Q3配网成功后热点消失了
- 这是正常设计:设备连上路由器后会自动关闭配网热点
---
## 7. 一句话速记
长按按键 → 连 `ESP32-xxxxxx` → 打开 `http://192.168.4.1` → 选 Wi-Fi + 输密码 → 等待成功提示。

View File

@@ -0,0 +1,34 @@
#pragma once
#include <stdbool.h>
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
WIFI_CONNECT_STATUS_IDLE = 0,
WIFI_CONNECT_STATUS_PROVISIONING,
WIFI_CONNECT_STATUS_CONNECTING,
WIFI_CONNECT_STATUS_CONNECTED,
WIFI_CONNECT_STATUS_FAILED,
WIFI_CONNECT_STATUS_TIMEOUT,
} wifi_connect_status_t;
typedef struct {
bool has_config;
char ssid[33];
char password[65];
} wifi_connect_config_t;
esp_err_t wifi_connect_init(void);
esp_err_t wifi_connect_start(void);
esp_err_t wifi_connect_stop(void);
wifi_connect_status_t wifi_connect_get_status(void);
esp_err_t wifi_connect_get_config(wifi_connect_config_t *config);
esp_err_t wifi_connect_clear_config(void);
#ifdef __cplusplus
}
#endif

File diff suppressed because it is too large Load Diff