@@ -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-
3121static struct net_if * ap_iface ;
3222static struct net_if * sta_iface ;
3323
@@ -36,16 +26,33 @@ static struct wifi_connect_req_params sta_config;
3626
3727static 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+
3946static 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
7886static 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
113123static 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