Skip to content

Commit 40ae620

Browse files
committed
esp32/ports/modnetwork.c: Implement network.WLAN.ifconfig('dhcp')
1 parent a6566fc commit 40ae620

File tree

5 files changed

+299
-22
lines changed

5 files changed

+299
-22
lines changed

extmod/modussl_mbedtls.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,15 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
141141
const byte seed[] = "upy";
142142
ret = mbedtls_ctr_drbg_seed(&o->ctr_drbg, null_entropy_func/*mbedtls_entropy_func*/, &o->entropy, seed, sizeof(seed));
143143
if (ret != 0) {
144-
printf("ret=%d\n", ret);
145-
assert(0);
144+
goto cleanup;
146145
}
147146

148147
ret = mbedtls_ssl_config_defaults(&o->conf,
149148
args->server_side.u_bool ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
150149
MBEDTLS_SSL_TRANSPORT_STREAM,
151150
MBEDTLS_SSL_PRESET_DEFAULT);
152151
if (ret != 0) {
153-
assert(0);
152+
goto cleanup;
154153
}
155154

156155
mbedtls_ssl_conf_authmode(&o->conf, MBEDTLS_SSL_VERIFY_NONE);
@@ -161,14 +160,14 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
161160

162161
ret = mbedtls_ssl_setup(&o->ssl, &o->conf);
163162
if (ret != 0) {
164-
assert(0);
163+
goto cleanup;
165164
}
166165

167166
if (args->server_hostname.u_obj != mp_const_none) {
168167
const char *sni = mp_obj_str_get_str(args->server_hostname.u_obj);
169168
ret = mbedtls_ssl_set_hostname(&o->ssl, sni);
170169
if (ret != 0) {
171-
assert(0);
170+
goto cleanup;
172171
}
173172
}
174173

@@ -194,13 +193,27 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
194193

195194
while ((ret = mbedtls_ssl_handshake(&o->ssl)) != 0) {
196195
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
197-
//assert(0);
198196
printf("mbedtls_ssl_handshake error: -%x\n", -ret);
199-
mp_raise_OSError(MP_EIO);
197+
goto cleanup;
200198
}
201199
}
202200

203201
return o;
202+
203+
cleanup:
204+
mbedtls_pk_free(&o->pkey);
205+
mbedtls_x509_crt_free(&o->cert);
206+
mbedtls_x509_crt_free(&o->cacert);
207+
mbedtls_ssl_free(&o->ssl);
208+
mbedtls_ssl_config_free(&o->conf);
209+
mbedtls_ctr_drbg_free(&o->ctr_drbg);
210+
mbedtls_entropy_free(&o->entropy);
211+
212+
if (ret == MBEDTLS_ERR_SSL_ALLOC_FAILED) {
213+
mp_raise_OSError(MP_ENOMEM);
214+
} else {
215+
mp_raise_OSError(MP_EIO);
216+
}
204217
}
205218

206219
STATIC mp_obj_t mod_ssl_getpeercert(mp_obj_t o_in, mp_obj_t binary_form) {

ports/esp32/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ SRC_C = \
136136
machine_uart.c \
137137
modmachine.c \
138138
modnetwork.c \
139+
network_lan.c \
139140
modsocket.c \
140141
modesp.c \
141142
moduhashlib.c \
@@ -267,6 +268,9 @@ ESPIDF_CXX_O = $(addprefix $(ESPCOMP)/cxx/,\
267268
ESPIDF_ETHERNET_O = $(addprefix $(ESPCOMP)/ethernet/,\
268269
emac_dev.o \
269270
emac_main.o \
271+
eth_phy/phy_tlk110.o \
272+
eth_phy/phy_lan8720.o \
273+
eth_phy/phy_common.o \
270274
)
271275

272276
$(BUILD)/$(ESPCOMP)/expat/%.o: CFLAGS += -Wno-unused-function

ports/esp32/modnetwork.c

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* The MIT License (MIT)
88
*
99
* Copyright (c) 2016, 2017 Nick Moore @mnemote
10+
* Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com>
1011
*
1112
* Based on esp8266/modnetwork.c which is Copyright (c) 2015 Paul Sokolovsky
1213
* And the ESP IDF example code which is Public Domain / CC0
@@ -48,6 +49,8 @@
4849
#include "lwip/dns.h"
4950
#include "tcpip_adapter.h"
5051

52+
#include "modnetwork.h"
53+
5154
#define MODNETWORK_INCLUDE_CONSTANTS (1)
5255

5356
NORETURN void _esp_exceptions(esp_err_t e) {
@@ -122,7 +125,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) {
122125
ESP_LOGI("wifi", "STA_START");
123126
break;
124127
case SYSTEM_EVENT_STA_GOT_IP:
125-
ESP_LOGI("wifi", "GOT_IP");
128+
ESP_LOGI("network", "GOT_IP");
126129
break;
127130
case SYSTEM_EVENT_STA_DISCONNECTED: {
128131
// This is a workaround as ESP32 WiFi libs don't currently
@@ -161,7 +164,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) {
161164
break;
162165
}
163166
default:
164-
ESP_LOGI("wifi", "event %d", event->event_id);
167+
ESP_LOGI("network", "event %d", event->event_id);
165168
break;
166169
}
167170
return ESP_OK;
@@ -182,6 +185,19 @@ STATIC void require_if(mp_obj_t wlan_if, int if_no) {
182185
}
183186

184187
STATIC mp_obj_t get_wlan(size_t n_args, const mp_obj_t *args) {
188+
static int initialized = 0;
189+
if (!initialized) {
190+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
191+
ESP_LOGD("modnetwork", "Initializing WiFi");
192+
ESP_EXCEPTIONS( esp_wifi_init(&cfg) );
193+
ESP_EXCEPTIONS( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
194+
ESP_LOGD("modnetwork", "Initialized");
195+
ESP_EXCEPTIONS( esp_wifi_set_mode(0) );
196+
ESP_EXCEPTIONS( esp_wifi_start() );
197+
ESP_LOGD("modnetwork", "Started");
198+
initialized = 1;
199+
}
200+
185201
int idx = (n_args > 0) ? mp_obj_get_int(args[0]) : WIFI_IF_STA;
186202
if (idx == WIFI_IF_STA) {
187203
return MP_OBJ_FROM_PTR(&wlan_sta_obj);
@@ -201,14 +217,6 @@ STATIC mp_obj_t esp_initialize() {
201217
ESP_LOGD("modnetwork", "Initializing Event Loop");
202218
ESP_EXCEPTIONS( esp_event_loop_init(event_handler, NULL) );
203219
ESP_LOGD("modnetwork", "esp_event_loop_init done");
204-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
205-
ESP_LOGD("modnetwork", "Initializing WiFi");
206-
ESP_EXCEPTIONS( esp_wifi_init(&cfg) );
207-
ESP_EXCEPTIONS( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
208-
ESP_LOGD("modnetwork", "Initialized");
209-
ESP_EXCEPTIONS( esp_wifi_set_mode(0) );
210-
ESP_EXCEPTIONS( esp_wifi_start() );
211-
ESP_LOGD("modnetwork", "Started");
212220
initialized = 1;
213221
}
214222
return mp_const_none;
@@ -342,6 +350,15 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
342350
return mp_obj_new_tuple(4, tuple);
343351
} else {
344352
// set
353+
if((self->if_id == WIFI_IF_STA || self->if_id == ESP_IF_ETH) &&
354+
n_args == 2 &&
355+
MP_OBJ_IS_STR(args[1]) &&
356+
strcmp(mp_obj_str_get_str(args[1]), "dhcp") == 0) {
357+
esp_err_t e = tcpip_adapter_dhcpc_start(self->if_id);
358+
if ( e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED) _esp_exceptions(e);
359+
return mp_const_none;
360+
}
361+
345362
mp_obj_t *items;
346363
mp_obj_get_array_fixed_n(args[1], 4, &items);
347364
netutils_parse_ipv4_addr(items[0], (void*)&info.ip, NETUTILS_BIG);
@@ -358,11 +375,11 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
358375
netutils_parse_ipv4_addr(items[2], (void*)&info.gw, NETUTILS_BIG);
359376
netutils_parse_ipv4_addr(items[3], (void*)&dns_info.ip, NETUTILS_BIG);
360377
// To set a static IP we have to disable DHCP first
361-
if (self->if_id == WIFI_IF_STA) {
362-
esp_err_t e = tcpip_adapter_dhcpc_stop(WIFI_IF_STA);
378+
if (self->if_id == WIFI_IF_STA || self->if_id == ESP_IF_ETH) {
379+
esp_err_t e = tcpip_adapter_dhcpc_stop(self->if_id);
363380
if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_exceptions(e);
364-
ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(WIFI_IF_STA, &info));
365-
ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(WIFI_IF_STA, TCPIP_ADAPTER_DNS_MAIN, &dns_info));
381+
ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(self->if_id, &info));
382+
ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(self->if_id, TCPIP_ADAPTER_DNS_MAIN, &dns_info));
366383
} else if (self->if_id == WIFI_IF_AP) {
367384
esp_err_t e = tcpip_adapter_dhcps_stop(WIFI_IF_AP);
368385
if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_exceptions(e);
@@ -374,7 +391,7 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
374391
}
375392
}
376393

377-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig);
394+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig);
378395

379396
STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
380397
if (n_args != 1 && kwargs->used != 0) {
@@ -533,6 +550,7 @@ STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
533550
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_network) },
534551
{ MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&esp_initialize_obj },
535552
{ MP_OBJ_NEW_QSTR(MP_QSTR_WLAN), (mp_obj_t)&get_wlan_obj },
553+
{ MP_OBJ_NEW_QSTR(MP_QSTR_LAN), (mp_obj_t)&get_lan_obj },
536554
{ MP_OBJ_NEW_QSTR(MP_QSTR_phy_mode), (mp_obj_t)&esp_phy_mode_obj },
537555

538556
#if MODNETWORK_INCLUDE_CONSTANTS
@@ -560,6 +578,11 @@ STATIC const mp_map_elem_t mp_module_network_globals_table[] = {
560578
MP_OBJ_NEW_SMALL_INT(WIFI_AUTH_WPA_WPA2_PSK) },
561579
{ MP_OBJ_NEW_QSTR(MP_QSTR_AUTH_MAX),
562580
MP_OBJ_NEW_SMALL_INT(WIFI_AUTH_MAX) },
581+
582+
{ MP_OBJ_NEW_QSTR(MP_QSTR_PHY_LAN8720),
583+
MP_OBJ_NEW_SMALL_INT(PHY_LAN8720) },
584+
{ MP_OBJ_NEW_QSTR(MP_QSTR_PHY_TLK110),
585+
MP_OBJ_NEW_SMALL_INT(PHY_TLK110) },
563586
#endif
564587
};
565588

ports/esp32/modnetwork.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_ESP32_MODNETWORK_H
27+
#define MICROPY_INCLUDED_ESP32_MODNETWORK_H
28+
29+
enum { PHY_LAN8720, PHY_TLK110 };
30+
31+
MP_DECLARE_CONST_FUN_OBJ_KW(get_lan_obj);
32+
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj);
33+
34+
#endif

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