Skip to content

Commit a4000c6

Browse files
Liam-qkkartben
authored andcommitted
samples: net: wifi: Add new Kconfig for apsta_mode
Introduce a dedicated Kconfig option for apsta_mode example to allow more flexible configuration in the application layer. This change helps make apsta_mode more generic and easier to integrate across different vendor platforms. Add compile-time assertions for the following macros: CONFIG_WIFI_SAMPLE_AP_SSID CONFIG_WIFI_SAMPLE_SSID CONFIG_WIFI_SAMPLE_AP_IP_ADDRESS CONFIG_WIFI_SAMPLE_AP_NETMASK Default to empty string ("") if the following two macros are not defined: CONFIG_WIFI_SAMPLE_AP_PSK CONFIG_WIFI_SAMPLE_PSK Signed-off-by: Qiankun Li <[email protected]>
1 parent e045e39 commit a4000c6

File tree

2 files changed

+102
-27
lines changed

2 files changed

+102
-27
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2025 NXP
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
mainmenu "Network Wi-Fi apsta_mode application"
5+
6+
config WIFI_SAMPLE_AP_SSID
7+
string "SSID of soft AP"
8+
help
9+
SSID of the soft AP created by the sample.
10+
Fox example: "SOFT-AP"
11+
12+
config WIFI_SAMPLE_AP_PSK
13+
string "Pre-shared key for soft AP"
14+
help
15+
WPA2 password used by the soft AP for client authentication.
16+
For example: "PSK"
17+
18+
config WIFI_SAMPLE_SSID
19+
string "SSID of the target AP"
20+
help
21+
SSID of the access point to connect to in station mode.
22+
For example: "TARGET-AP"
23+
24+
config WIFI_SAMPLE_PSK
25+
string "Pre-shared key for target AP"
26+
help
27+
WPA2 password used to connect to the target AP.
28+
For example: "PSK"
29+
30+
config WIFI_SAMPLE_DHCPV4_START
31+
bool "Start DHCPv4 server in application layer."
32+
default y
33+
help
34+
Enable this option to start the DHCPv4 server from the application layer,
35+
typically before the Soft AP is initialized.
36+
37+
When disabled, the DHCPv4 server startup timing is determined by the vendor's
38+
implementation. In some cases, the server may be started after the AP is up,
39+
using the vendor's default IP address, gateway, subnet mask.
40+
41+
42+
if WIFI_SAMPLE_DHCPV4_START
43+
44+
# Some vendors start the DHCPv4 server after Soft AP initialization,
45+
# using default IP address and subnet mask. In such cases, manual
46+
# configuration of gateway and netmask may not be necessary.
47+
# The following options are used only if the DHCPv4 server is started
48+
# by the application.
49+
50+
config WIFI_SAMPLE_AP_IP_ADDRESS
51+
string "IP address of soft AP"
52+
help
53+
IP address assigned to the soft AP interface.
54+
For example: "192.168.4.1"
55+
56+
config WIFI_SAMPLE_AP_NETMASK
57+
string "Netmask of soft AP"
58+
help
59+
Subnet mask for the soft AP interface.
60+
For example: "255.255.255.0"
61+
endif # WIFI_SAMPLE_DHCPV4_START
62+
63+
source "Kconfig.zephyr"

samples/net/wifi/apsta_mode/src/main.c

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ LOG_MODULE_REGISTER(MAIN);
1818
NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \
1919
NET_EVENT_WIFI_AP_STA_CONNECTED | NET_EVENT_WIFI_AP_STA_DISCONNECTED)
2020

21-
/* AP Mode Configuration */
22-
#define WIFI_AP_SSID "ESP32-AP"
23-
#define WIFI_AP_PSK ""
24-
#define WIFI_AP_IP_ADDRESS "192.168.4.1"
25-
#define WIFI_AP_NETMASK "255.255.255.0"
26-
27-
/* STA Mode Configuration */
28-
#define WIFI_SSID "SSID" /* Replace `SSID` with WiFi ssid. */
29-
#define WIFI_PSK "PASSWORD" /* Replace `PASSWORD` with Router password. */
30-
3121
static struct net_if *ap_iface;
3222
static struct net_if *sta_iface;
3323

@@ -36,16 +26,33 @@ static struct wifi_connect_req_params sta_config;
3626

3727
static struct net_mgmt_event_callback cb;
3828

29+
/* Check necessary definitions */
30+
31+
BUILD_ASSERT(sizeof(CONFIG_WIFI_SAMPLE_AP_SSID) > 1,
32+
"CONFIG_WIFI_SAMPLE_AP_SSID is empty. Please set it in conf file.");
33+
34+
BUILD_ASSERT(sizeof(CONFIG_WIFI_SAMPLE_SSID) > 1,
35+
"CONFIG_WIFI_SAMPLE_SSID is empty. Please set it in conf file.");
36+
37+
#if WIFI_SAMPLE_DHCPV4_START
38+
BUILD_ASSERT(sizeof(CONFIG_WIFI_SAMPLE_AP_IP_ADDRESS) > 1,
39+
"CONFIG_WIFI_SAMPLE_AP_IP_ADDRESS is empty. Please set it in conf file.");
40+
41+
BUILD_ASSERT(sizeof(CONFIG_WIFI_SAMPLE_AP_NETMASK) > 1,
42+
"CONFIG_WIFI_SAMPLE_AP_NETMASK is empty. Please set it in conf file.");
43+
44+
#endif
45+
3946
static void wifi_event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_event,
4047
struct net_if *iface)
4148
{
4249
switch (mgmt_event) {
4350
case NET_EVENT_WIFI_CONNECT_RESULT: {
44-
LOG_INF("Connected to %s", WIFI_SSID);
51+
LOG_INF("Connected to %s", CONFIG_WIFI_SAMPLE_SSID);
4552
break;
4653
}
4754
case NET_EVENT_WIFI_DISCONNECT_RESULT: {
48-
LOG_INF("Disconnected from %s", WIFI_SSID);
55+
LOG_INF("Disconnected from %s", CONFIG_WIFI_SAMPLE_SSID);
4956
break;
5057
}
5158
case NET_EVENT_WIFI_AP_ENABLE_RESULT: {
@@ -75,18 +82,19 @@ static void wifi_event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt
7582
}
7683
}
7784

85+
#if CONFIG_WIFI_SAMPLE_DHCPV4_START
7886
static void enable_dhcpv4_server(void)
7987
{
8088
static struct in_addr addr;
8189
static struct in_addr netmaskAddr;
8290

83-
if (net_addr_pton(AF_INET, WIFI_AP_IP_ADDRESS, &addr)) {
84-
LOG_ERR("Invalid address: %s", WIFI_AP_IP_ADDRESS);
91+
if (net_addr_pton(AF_INET, CONFIG_WIFI_SAMPLE_AP_IP_ADDRESS, &addr)) {
92+
LOG_ERR("Invalid address: %s", CONFIG_WIFI_SAMPLE_AP_IP_ADDRESS);
8593
return;
8694
}
8795

88-
if (net_addr_pton(AF_INET, WIFI_AP_NETMASK, &netmaskAddr)) {
89-
LOG_ERR("Invalid netmask: %s", WIFI_AP_NETMASK);
96+
if (net_addr_pton(AF_INET, CONFIG_WIFI_SAMPLE_AP_NETMASK, &netmaskAddr)) {
97+
LOG_ERR("Invalid netmask: %s", CONFIG_WIFI_SAMPLE_AP_NETMASK);
9098
return;
9199
}
92100

@@ -97,7 +105,8 @@ static void enable_dhcpv4_server(void)
97105
}
98106

99107
if (!net_if_ipv4_set_netmask_by_addr(ap_iface, &addr, &netmaskAddr)) {
100-
LOG_ERR("Unable to set netmask for AP interface: %s", WIFI_AP_NETMASK);
108+
LOG_ERR("Unable to set netmask for AP interface: %s",
109+
CONFIG_WIFI_SAMPLE_AP_NETMASK);
101110
}
102111

103112
addr.s4_addr[3] += 10; /* Starting IPv4 address for DHCPv4 address pool. */
@@ -109,6 +118,7 @@ static void enable_dhcpv4_server(void)
109118

110119
LOG_INF("DHCPv4 server started...\n");
111120
}
121+
#endif
112122

113123
static int enable_ap_mode(void)
114124
{
@@ -118,21 +128,23 @@ static int enable_ap_mode(void)
118128
}
119129

120130
LOG_INF("Turning on AP Mode");
121-
ap_config.ssid = (const uint8_t *)WIFI_AP_SSID;
122-
ap_config.ssid_length = strlen(WIFI_AP_SSID);
123-
ap_config.psk = (const uint8_t *)WIFI_AP_PSK;
124-
ap_config.psk_length = strlen(WIFI_AP_PSK);
131+
ap_config.ssid = (const uint8_t *)CONFIG_WIFI_SAMPLE_AP_SSID;
132+
ap_config.ssid_length = sizeof(CONFIG_WIFI_SAMPLE_AP_SSID) - 1;
133+
ap_config.psk = (const uint8_t *)CONFIG_WIFI_SAMPLE_AP_PSK;
134+
ap_config.psk_length = sizeof(CONFIG_WIFI_SAMPLE_AP_PSK) - 1;
125135
ap_config.channel = WIFI_CHANNEL_ANY;
126136
ap_config.band = WIFI_FREQ_BAND_2_4_GHZ;
127137

128-
if (strlen(WIFI_AP_PSK) == 0) {
138+
if (sizeof(CONFIG_WIFI_SAMPLE_AP_PSK) == 1) {
129139
ap_config.security = WIFI_SECURITY_TYPE_NONE;
130140
} else {
131141

132142
ap_config.security = WIFI_SECURITY_TYPE_PSK;
133143
}
134144

145+
#if CONFIG_WIFI_SAMPLE_DHCPV4_START
135146
enable_dhcpv4_server();
147+
#endif
136148

137149
int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config,
138150
sizeof(struct wifi_connect_req_params));
@@ -150,10 +162,10 @@ static int connect_to_wifi(void)
150162
return -EIO;
151163
}
152164

153-
sta_config.ssid = (const uint8_t *)WIFI_SSID;
154-
sta_config.ssid_length = strlen(WIFI_SSID);
155-
sta_config.psk = (const uint8_t *)WIFI_PSK;
156-
sta_config.psk_length = strlen(WIFI_PSK);
165+
sta_config.ssid = (const uint8_t *)CONFIG_WIFI_SAMPLE_SSID;
166+
sta_config.ssid_length = sizeof(CONFIG_WIFI_SAMPLE_SSID) - 1;
167+
sta_config.psk = (const uint8_t *)CONFIG_WIFI_SAMPLE_PSK;
168+
sta_config.psk_length = sizeof(CONFIG_WIFI_SAMPLE_PSK) - 1;
157169
sta_config.security = WIFI_SECURITY_TYPE_PSK;
158170
sta_config.channel = WIFI_CHANNEL_ANY;
159171
sta_config.band = WIFI_FREQ_BAND_2_4_GHZ;
@@ -163,7 +175,7 @@ static int connect_to_wifi(void)
163175
int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config,
164176
sizeof(struct wifi_connect_req_params));
165177
if (ret) {
166-
LOG_ERR("Unable to Connect to (%s)", WIFI_SSID);
178+
LOG_ERR("Unable to Connect to (%s)", CONFIG_WIFI_SAMPLE_SSID);
167179
}
168180

169181
return ret;

0 commit comments

Comments
 (0)