|
4 | 4 | * The MIT License (MIT)
|
5 | 5 | *
|
6 | 6 | * 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 |
7 | 9 | *
|
8 | 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 | 11 | * of this software and associated documentation files (the "Software"), to deal
|
|
39 | 41 | #include "esp_log.h"
|
40 | 42 | #include "esp_event_loop.h"
|
41 | 43 | #include "esp_log.h"
|
| 44 | +#include "lwip/dns.h" |
| 45 | +#include "tcpip_adapter.h" |
42 | 46 |
|
43 | 47 | #define MODNETWORK_INCLUDE_CONSTANTS (1)
|
44 | 48 |
|
@@ -66,13 +70,20 @@ NORETURN void _esp_exceptions(esp_err_t e) {
|
66 | 70 | mp_raise_msg(&mp_type_OSError, "Wifi Invalid Password");
|
67 | 71 | case ESP_ERR_WIFI_NVS:
|
68 | 72 | 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"); |
69 | 79 | case ESP_ERR_WIFI_TIMEOUT:
|
70 | 80 | mp_raise_OSError(MP_ETIMEDOUT);
|
| 81 | + case ESP_ERR_TCPIP_ADAPTER_NO_MEM: |
71 | 82 | case ESP_ERR_WIFI_NO_MEM:
|
72 | 83 | mp_raise_OSError(MP_ENOMEM);
|
73 | 84 | default:
|
74 | 85 | 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 |
76 | 87 | ));
|
77 | 88 | }
|
78 | 89 | }
|
@@ -256,7 +267,50 @@ STATIC mp_obj_t esp_isconnected(mp_obj_t self_in) {
|
256 | 267 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_isconnected_obj, esp_isconnected);
|
257 | 268 |
|
258 | 269 | 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 | + } |
260 | 314 | }
|
261 | 315 |
|
262 | 316 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig);
|
|
0 commit comments