|
39 | 39 | #include "espconn.h" |
40 | 40 | #include "spi_flash.h" |
41 | 41 | #include "ets_alt_task.h" |
| 42 | +#include "lwip/dns.h" |
42 | 43 |
|
43 | 44 | #define MODNETWORK_INCLUDE_CONSTANTS (1) |
44 | 45 |
|
@@ -210,19 +211,58 @@ STATIC mp_obj_t esp_mac(mp_uint_t n_args, const mp_obj_t *args) { |
210 | 211 | } |
211 | 212 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_mac_obj, 1, 2, esp_mac); |
212 | 213 |
|
213 | | -STATIC mp_obj_t esp_ifconfig(mp_obj_t self_in) { |
214 | | - wlan_if_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 214 | +STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) { |
| 215 | + wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
215 | 216 | struct ip_info info; |
| 217 | + ip_addr_t dns_addr; |
216 | 218 | wifi_get_ip_info(self->if_id, &info); |
217 | | - mp_obj_t ifconfig[4] = { |
| 219 | + if (n_args == 1) { |
| 220 | + // get |
| 221 | + dns_addr = dns_getserver(0); |
| 222 | + mp_obj_t tuple[4] = { |
218 | 223 | netutils_format_ipv4_addr((uint8_t*)&info.ip, NETUTILS_BIG), |
219 | 224 | netutils_format_ipv4_addr((uint8_t*)&info.netmask, NETUTILS_BIG), |
220 | 225 | netutils_format_ipv4_addr((uint8_t*)&info.gw, NETUTILS_BIG), |
221 | | - MP_OBJ_NEW_QSTR(MP_QSTR_), // no DNS server |
222 | | - }; |
223 | | - return mp_obj_new_tuple(4, ifconfig); |
| 226 | + netutils_format_ipv4_addr((uint8_t*)&dns_addr, NETUTILS_BIG), |
| 227 | + }; |
| 228 | + return mp_obj_new_tuple(4, tuple); |
| 229 | + } else { |
| 230 | + // set |
| 231 | + mp_obj_t *items; |
| 232 | + bool restart_dhcp_server = false; |
| 233 | + mp_obj_get_array_fixed_n(args[1], 4, &items); |
| 234 | + netutils_parse_ipv4_addr(items[0], (void*)&info.ip, NETUTILS_BIG); |
| 235 | + if (mp_obj_is_integer(items[1])) { |
| 236 | + // allow numeric netmask, i.e.: |
| 237 | + // 24 -> 255.255.255.0 |
| 238 | + // 16 -> 255.255.0.0 |
| 239 | + // etc... |
| 240 | + uint32_t* m = (uint32_t*)&info.netmask; |
| 241 | + *m = htonl(0xffffffff << (32 - mp_obj_get_int(items[1]))); |
| 242 | + } else { |
| 243 | + netutils_parse_ipv4_addr(items[1], (void*)&info.netmask, NETUTILS_BIG); |
| 244 | + } |
| 245 | + netutils_parse_ipv4_addr(items[2], (void*)&info.gw, NETUTILS_BIG); |
| 246 | + netutils_parse_ipv4_addr(items[3], (void*)&dns_addr, NETUTILS_BIG); |
| 247 | + // To set a static IP we have to disable DHCP first |
| 248 | + if (self->if_id == STATION_IF) { |
| 249 | + wifi_station_dhcpc_stop(); |
| 250 | + } else { |
| 251 | + restart_dhcp_server = wifi_softap_dhcps_status(); |
| 252 | + wifi_softap_dhcps_stop(); |
| 253 | + } |
| 254 | + if (!wifi_set_ip_info(self->if_id, &info)) { |
| 255 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, |
| 256 | + "wifi_set_ip_info() failed")); |
| 257 | + } |
| 258 | + dns_setserver(0, &dns_addr); |
| 259 | + if (restart_dhcp_server) { |
| 260 | + wifi_softap_dhcps_start(); |
| 261 | + } |
| 262 | + return mp_const_none; |
| 263 | + } |
224 | 264 | } |
225 | | -STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_ifconfig_obj, esp_ifconfig); |
| 265 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig); |
226 | 266 |
|
227 | 267 | STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
228 | 268 | if (n_args != 1 && kwargs->used != 0) { |
|
0 commit comments