@@ -214,60 +214,68 @@ swap(IPAddress &lhs, IPAddress &rhs)
214
214
* @param dns1 Static DNS server 1
215
215
* @param dns2 Static DNS server 2
216
216
*/
217
- bool ESP8266WiFiSTAClass::config (IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) {
218
-
219
- if (!WiFi.enableSTA (true )) {
220
- return false ;
221
- }
222
-
223
- // Arduino has a different arg order: ip, dns, gateway, subnet. To allow compatibility, check first octet of 3rd arg. If 255, interpret as ESP order, otherwise Arduino order.
224
- if (subnet[0 ] != 255 )
225
- {
226
- // octet is not 255 => interpret as Arduino order
227
-
228
- if (dns1[0 ] == 0 )
229
- {
230
- // arg order is arduino and 4th arg not given => assign it arduino default
231
- dns1 = IPAddress (255 ,255 ,255 ,0 );
232
- }
233
-
234
- // current order is arduino: ip-dns-gway-subnet
235
- swap (gateway, subnet); // after this, order is ip-gway-dns-subnet
236
- swap (subnet, dns1); // after this, order is ip-gway-subnet-dns (correct ESP order)
237
- }
238
-
239
- struct ip_info info;
240
- info.ip .addr = static_cast <uint32_t >(local_ip);
241
- info.gw .addr = static_cast <uint32_t >(gateway);
242
- info.netmask .addr = static_cast <uint32_t >(subnet);
243
-
244
- if (local_ip == 0U && gateway == 0U && subnet == 0U ) {
245
- _useStaticIp = false ;
246
- wifi_station_dhcpc_start ();
247
- return true ;
248
- }
249
-
250
- wifi_station_dhcpc_stop ();
251
- if (wifi_set_ip_info (STATION_IF, &info)) {
252
- _useStaticIp = true ;
253
- } else {
254
- return false ;
255
- }
256
- ip_addr_t d;
257
-
258
- if (dns1 != (uint32_t )0x00000000 ) {
259
- // Set DNS1-Server
260
- d.addr = static_cast <uint32_t >(dns1);
261
- dns_setserver (0 , &d);
262
- }
263
-
264
- if (dns2 != (uint32_t )0x00000000 ) {
265
- // Set DNS2-Server
266
- d.addr = static_cast <uint32_t >(dns2);
267
- dns_setserver (1 , &d);
268
- }
269
-
270
- return true ;
217
+ bool ESP8266WiFiSTAClass::config (IPAddress local_ip, IPAddress arg1, IPAddress arg2, IPAddress arg3, IPAddress dns2) {
218
+
219
+ if (!WiFi.enableSTA (true )) {
220
+ return false ;
221
+ }
222
+
223
+ // ESP argument order is: ip, gateway, subnet, dns1
224
+ // Arduino arg order is: ip, dns, gateway, subnet.
225
+
226
+ // first, check whether dhcp should be used, which is when ip == 0 && gateway == 0 && subnet == 0.
227
+ bool espOrderUseDHCP = (local_ip == 0U && arg1 == 0U && arg2 == 0U );
228
+ bool arduinoOrderUseDHCP = (local_ip == 0U && arg2 == 0U && arg3 == 0U );
229
+ if (espOrderUseDHCP || arduinoOrderUseDHCP) {
230
+ _useStaticIp = false ;
231
+ wifi_station_dhcpc_start ();
232
+ return true ;
233
+ }
234
+
235
+ // To allow compatibility, check first octet of 3rd arg. If 255, interpret as ESP order, otherwise Arduino order.
236
+ IPAddress gateway = arg1;
237
+ IPAddress subnet = arg2;
238
+ IPAddress dns1 = arg3;
239
+
240
+ if (subnet[0 ] != 255 )
241
+ {
242
+ // octet is not 255 => interpret as Arduino order
243
+ gateway = arg2;
244
+ subnet = arg3[0 ] == 0 ? IPAddress (255 ,255 ,255 ,0 ) : arg3; // arg order is arduino and 4th arg not given => assign it arduino default
245
+ dns1 = arg1;
246
+ }
247
+
248
+ // ip and gateway must be in the same subnet
249
+ if ((local_ip & subnet) != (gateway & subnet)) {
250
+ return false ;
251
+ }
252
+
253
+ struct ip_info info;
254
+ info.ip .addr = static_cast <uint32_t >(local_ip);
255
+ info.gw .addr = static_cast <uint32_t >(gateway);
256
+ info.netmask .addr = static_cast <uint32_t >(subnet);
257
+
258
+ wifi_station_dhcpc_stop ();
259
+ if (wifi_set_ip_info (STATION_IF, &info)) {
260
+ _useStaticIp = true ;
261
+ } else {
262
+ return false ;
263
+ }
264
+ ip_addr_t d;
265
+
266
+ if (dns1 != (uint32_t )0x00000000 ) {
267
+ // Set DNS1-Server
268
+ d.addr = static_cast <uint32_t >(dns1);
269
+ dns_setserver (0 , &d);
270
+ }
271
+
272
+ if (dns2 != (uint32_t )0x00000000 ) {
273
+ // Set DNS2-Server
274
+ d.addr = static_cast <uint32_t >(dns2);
275
+ dns_setserver (1 , &d);
276
+ }
277
+
278
+ return true ;
271
279
}
272
280
273
281
/* *
0 commit comments