@@ -85,22 +85,24 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
85
85
86
86
if (!WiFi.enableAP (true )) {
87
87
// enable AP failed
88
- DEBUG_WIFI (" [AP] enableAP failed!" );
88
+ DEBUG_WIFI (" [AP] enableAP failed!\n " );
89
89
return false ;
90
90
}
91
91
92
92
if (!ssid || *ssid == 0 || strlen (ssid) > 31 ) {
93
93
// fail SSID too long or missing!
94
- DEBUG_WIFI (" [AP] SSID too long or missing!" );
94
+ DEBUG_WIFI (" [AP] SSID too long or missing!\n " );
95
95
return false ;
96
96
}
97
97
98
98
if (passphrase && (strlen (passphrase) > 63 || strlen (passphrase) < 8 )) {
99
99
// fail passphrase to long or short!
100
- DEBUG_WIFI (" [AP] fail passphrase to long or short!" );
100
+ DEBUG_WIFI (" [AP] fail passphrase to long or short!\n " );
101
101
return false ;
102
102
}
103
103
104
+ bool ret = false ;
105
+
104
106
struct softap_config conf;
105
107
strcpy (reinterpret_cast <char *>(conf.ssid ), ssid);
106
108
conf.channel = channel;
@@ -119,24 +121,51 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
119
121
120
122
struct softap_config conf_current;
121
123
wifi_softap_get_config (&conf_current);
122
- if (softap_config_equal (conf, conf_current)) {
123
- DEBUG_WIFI (" [AP] softap config unchanged" );
124
- return true ;
125
- }
124
+ if (!softap_config_equal (conf, conf_current)) {
126
125
127
- bool ret;
126
+ ETS_UART_INTR_DISABLE ();
127
+ if (WiFi._persistent ) {
128
+ ret = wifi_softap_set_config (&conf);
129
+ } else {
130
+ ret = wifi_softap_set_config_current (&conf);
131
+ }
132
+ ETS_UART_INTR_ENABLE ();
133
+
134
+ if (!ret) {
135
+ DEBUG_WIFI (" [AP] set_config failed!\n " );
136
+ return false ;
137
+ }
128
138
129
- ETS_UART_INTR_DISABLE ();
130
- if (WiFi._persistent ) {
131
- ret = wifi_softap_set_config (&conf);
132
139
} else {
133
- ret = wifi_softap_set_config_current (&conf );
140
+ DEBUG_WIFI ( " [AP] softap config unchanged \n " );
134
141
}
135
- ETS_UART_INTR_ENABLE ();
136
142
137
- if (!ret) {
138
- DEBUG_WIFI (" [AP] set_config faild!" );
143
+ if (wifi_softap_dhcps_status () != DHCP_STARTED) {
144
+ DEBUG_WIFI (" [AP] DHCP not started, starting...\n " );
145
+ if (!wifi_softap_dhcps_start ()) {
146
+ DEBUG_WIFI (" [AP] wifi_softap_dhcps_start failed!\n " );
147
+ ret = false ;
148
+ }
149
+ }
150
+
151
+ // check IP config
152
+ struct ip_info ip;
153
+ if (wifi_get_ip_info (SOFTAP_IF, &ip)) {
154
+ if (ip.ip .addr == 0x00000000 ) {
155
+ // Invalid config
156
+ DEBUG_WIFI (" [AP] IP config Invalid resetting...\n " );
157
+ // 192.168.244.1 , 192.168.244.1 , 255.255.255.0
158
+ ret = softAPConfig (0x01F4A8C0 , 0x01F4A8C0 , 0x00FFFFFF );
159
+ if (!ret) {
160
+ DEBUG_WIFI (" [AP] softAPConfig failed!\n " );
161
+ ret = false ;
162
+ }
163
+ }
164
+ } else {
165
+ DEBUG_WIFI (" [AP] wifi_get_ip_info failed!\n " );
166
+ ret = false ;
139
167
}
168
+
140
169
return ret;
141
170
}
142
171
@@ -148,24 +177,76 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
148
177
* @param subnet subnet mask
149
178
*/
150
179
bool ESP8266WiFiAPClass::softAPConfig (IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
151
-
180
+ DEBUG_WIFI ( " [APConfig] local_ip: %s gateway: %s subnet: %s \n " , local_ip. toString (). c_str (), gateway. toString (). c_str (), subnet. toString (). c_str ());
152
181
if (!WiFi.enableAP (true )) {
153
182
// enable AP failed
154
- DEBUG_WIFI (" [AP ] enableAP failed!" );
183
+ DEBUG_WIFI (" [APConfig ] enableAP failed!\n " );
155
184
return false ;
156
185
}
186
+ bool ret = true ;
157
187
158
188
struct ip_info info;
159
189
info.ip .addr = static_cast <uint32_t >(local_ip);
160
190
info.gw .addr = static_cast <uint32_t >(gateway);
161
191
info.netmask .addr = static_cast <uint32_t >(subnet);
162
- wifi_softap_dhcps_stop ();
163
- if (wifi_set_ip_info (SOFTAP_IF, &info)) {
164
- return wifi_softap_dhcps_start ();
192
+
193
+ if (!wifi_softap_dhcps_stop ()) {
194
+ DEBUG_WIFI (" [APConfig] wifi_softap_dhcps_stop failed!\n " );
195
+ }
196
+
197
+ if (!wifi_set_ip_info (SOFTAP_IF, &info)) {
198
+ DEBUG_WIFI (" [APConfig] wifi_set_ip_info failed!\n " );
199
+ ret = false ;
200
+ }
201
+
202
+ struct dhcps_lease dhcp_lease;
203
+ IPAddress ip = local_ip;
204
+ ip[3 ] += 99 ;
205
+ dhcp_lease.start_ip .addr = static_cast <uint32_t >(ip);
206
+ DEBUG_WIFI (" [APConfig] DHCP IP start: %s\n " , ip.toString ().c_str ());
207
+
208
+ ip[3 ] += 100 ;
209
+ dhcp_lease.end_ip .addr = static_cast <uint32_t >(ip);
210
+ DEBUG_WIFI (" [APConfig] DHCP IP end: %s\n " , ip.toString ().c_str ());
211
+
212
+ if (!wifi_softap_set_dhcps_lease (&dhcp_lease)) {
213
+ DEBUG_WIFI (" [APConfig] wifi_set_ip_info failed!\n " );
214
+ ret = false ;
215
+ }
216
+
217
+ // set lease time to 720min --> 12h
218
+ if (!wifi_softap_set_dhcps_lease_time (720 )) {
219
+ DEBUG_WIFI (" [APConfig] wifi_softap_set_dhcps_lease_time failed!\n " );
220
+ ret = false ;
221
+ }
222
+
223
+ uint8 mode = 1 ;
224
+ if (!wifi_softap_set_dhcps_offer_option (OFFER_ROUTER, &mode)) {
225
+ DEBUG_WIFI (" [APConfig] wifi_softap_set_dhcps_offer_option failed!\n " );
226
+ ret = false ;
227
+ }
228
+
229
+ if (!wifi_softap_dhcps_start ()) {
230
+ DEBUG_WIFI (" [APConfig] wifi_softap_dhcps_start failed!\n " );
231
+ ret = false ;
232
+ }
233
+
234
+ // check config
235
+ if (wifi_get_ip_info (SOFTAP_IF, &info)) {
236
+ if (info.ip .addr == 0x00000000 ) {
237
+ DEBUG_WIFI (" [AP] IP config Invalid?!\n " );
238
+ ret = false ;
239
+ } else if (local_ip != info.ip .addr ) {
240
+ ip = info.ip .addr ;
241
+ DEBUG_WIFI (" [AP] IP config not set correct?! new IP: %s\n " , ip.toString ().c_str ());
242
+ ret = false ;
243
+ }
165
244
} else {
166
- DEBUG_WIFI (" [AP] wifi_set_ip_info failed!" );
245
+ DEBUG_WIFI (" [AP] wifi_get_ip_info failed!\n " );
246
+ ret = false ;
167
247
}
168
- return false ;
248
+
249
+ return ret;
169
250
}
170
251
171
252
@@ -188,6 +269,10 @@ bool ESP8266WiFiAPClass::softAPdisconnect(bool wifioff) {
188
269
}
189
270
ETS_UART_INTR_ENABLE ();
190
271
272
+ if (!ret) {
273
+ DEBUG_WIFI (" [APdisconnect] set_config failed!\n " );
274
+ }
275
+
191
276
if (wifioff) {
192
277
ret = WiFi.enableAP (false );
193
278
}
0 commit comments