Skip to content

Commit e95703f

Browse files
committed
network.WLAN.ifconfig based on esp8266 version
1 parent 97cbf0d commit e95703f

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

esp32/modnetwork.c

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* The MIT License (MIT)
55
*
66
* Copyright (c) 2016 Nick Moore
7+
* Based on esp8266/modnetwork.c which is Copyright (c) 2015 Paul Sokolovsky
8+
* And the ESP IDF example code which is Public Domain / CC0
79
*
810
* Permission is hereby granted, free of charge, to any person obtaining a copy
911
* of this software and associated documentation files (the "Software"), to deal
@@ -39,6 +41,8 @@
3941
#include "esp_log.h"
4042
#include "esp_event_loop.h"
4143
#include "esp_log.h"
44+
#include "lwip/dns.h"
45+
#include "tcpip_adapter.h"
4246

4347
#define MODNETWORK_INCLUDE_CONSTANTS (1)
4448

@@ -66,13 +70,20 @@ NORETURN void _esp_exceptions(esp_err_t e) {
6670
mp_raise_msg(&mp_type_OSError, "Wifi Invalid Password");
6771
case ESP_ERR_WIFI_NVS:
6872
mp_raise_msg(&mp_type_OSError, "Wifi Internal NVS Error");
73+
case ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS:
74+
mp_raise_msg(&mp_type_OSError, "TCP/IP Invalid Parameters");
75+
case ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY:
76+
mp_raise_msg(&mp_type_OSError, "TCP/IP IF Not Ready");
77+
case ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED:
78+
mp_raise_msg(&mp_type_OSError, "TCP/IP DHCP Client Start Failed");
6979
case ESP_ERR_WIFI_TIMEOUT:
7080
mp_raise_OSError(MP_ETIMEDOUT);
81+
case ESP_ERR_TCPIP_ADAPTER_NO_MEM:
7182
case ESP_ERR_WIFI_NO_MEM:
7283
mp_raise_OSError(MP_ENOMEM);
7384
default:
7485
nlr_raise(mp_obj_new_exception_msg_varg(
75-
&mp_type_RuntimeError, "Wifi Unknown Error %d", e
86+
&mp_type_RuntimeError, "Wifi Unknown Error 0x%04x", e
7687
));
7788
}
7889
}
@@ -256,7 +267,50 @@ STATIC mp_obj_t esp_isconnected(mp_obj_t self_in) {
256267
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_isconnected_obj, esp_isconnected);
257268

258269
STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
259-
return mp_const_none;
270+
wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
271+
tcpip_adapter_ip_info_t info;
272+
ip_addr_t dns_addr;
273+
tcpip_adapter_get_ip_info(self->if_id, &info);
274+
if (n_args == 1) {
275+
// get
276+
dns_addr = dns_getserver(0);
277+
mp_obj_t tuple[4] = {
278+
netutils_format_ipv4_addr((uint8_t*)&info.ip, NETUTILS_BIG),
279+
netutils_format_ipv4_addr((uint8_t*)&info.netmask, NETUTILS_BIG),
280+
netutils_format_ipv4_addr((uint8_t*)&info.gw, NETUTILS_BIG),
281+
netutils_format_ipv4_addr((uint8_t*)&dns_addr, NETUTILS_BIG),
282+
};
283+
return mp_obj_new_tuple(4, tuple);
284+
} else {
285+
// set
286+
mp_obj_t *items;
287+
mp_obj_get_array_fixed_n(args[1], 4, &items);
288+
netutils_parse_ipv4_addr(items[0], (void*)&info.ip, NETUTILS_BIG);
289+
if (mp_obj_is_integer(items[1])) {
290+
// allow numeric netmask, i.e.:
291+
// 24 -> 255.255.255.0
292+
// 16 -> 255.255.0.0
293+
// etc...
294+
uint32_t* m = (uint32_t*)&info.netmask;
295+
*m = htonl(0xffffffff << (32 - mp_obj_get_int(items[1])));
296+
} else {
297+
netutils_parse_ipv4_addr(items[1], (void*)&info.netmask, NETUTILS_BIG);
298+
}
299+
netutils_parse_ipv4_addr(items[2], (void*)&info.gw, NETUTILS_BIG);
300+
netutils_parse_ipv4_addr(items[3], (void*)&dns_addr, NETUTILS_BIG);
301+
// To set a static IP we have to disable DHCP first
302+
if (self->if_id == WIFI_IF_STA) {
303+
esp_err_t e = tcpip_adapter_dhcpc_stop(WIFI_IF_STA);
304+
if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_exceptions(e);
305+
ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(WIFI_IF_STA, &info));
306+
} else if (self->if_id == WIFI_IF_AP) {
307+
esp_err_t e = tcpip_adapter_dhcps_stop(WIFI_IF_AP);
308+
if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_exceptions(e);
309+
ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(WIFI_IF_AP, &info));
310+
ESP_EXCEPTIONS(tcpip_adapter_dhcps_start(WIFI_IF_AP));
311+
}
312+
return mp_const_none;
313+
}
260314
}
261315

262316
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy