Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f6297a5

Browse files
committed
MicroPython core updated
commit: 59dda71 (Apr 10, 2018) fullSHA: 59dda7103855e2f85822f83ecbb835d66f12183d 'network' module update see Wiki for changes 'microWebServer' frozen module updated Some fixes related 'ticks_*' functions returning 64-bit values better compatibiliy with 'uasyncio' module Other minor changes and bugfixes
1 parent 7df5b49 commit f6297a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+726
-1990
lines changed

‎MicroPython_BUILD/components/micropython/esp32/modmachine.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,7 @@ void prepareSleepReset(uint8_t hrst, char *msg)
278278
internalUmount();
279279

280280
if (!hrst) {
281-
mp_thread_deinit();
282-
283281
if (msg) mp_hal_stdout_tx_str(msg);
284-
285282
// deinitialise peripherals
286283
//ToDo: deinitialize other peripherals, threads, services, ...
287284
machine_pins_deinit();
@@ -462,7 +459,7 @@ STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *pos_args, mp_ma
462459
}
463460

464461
if (machine_rtc_config.ext0_pin >= 0) {
465-
printf("EXT0=%d\n", machine_rtc_config.ext0_pin);
462+
ESP_LOGD("DEEP SLEEP", "EXT0=%d\n", machine_rtc_config.ext0_pin);
466463
esp_sleep_enable_ext0_wakeup((gpio_num_t)machine_rtc_config.ext0_pin, machine_rtc_config.ext0_level ? 1 : 0);
467464
esp_set_deep_sleep_wake_stub(&wake_stub);
468465
}
@@ -475,7 +472,7 @@ STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *pos_args, mp_ma
475472
}
476473
}
477474
if (ext1_pins != 0) {
478-
printf("EXT1 = [%llx]\n", ext1_pins);
475+
ESP_LOGD("DEEP SLEEP", "EXT1 = [%llx]\n", ext1_pins);
479476
//esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
480477
uint8_t ext1_level = machine_rtc_config.ext1_level;
481478
if (machine_rtc_config.ext1_level == EXT1_WAKEUP_ALL_HIGH) ext1_level = ESP_EXT1_WAKEUP_ANY_HIGH;
@@ -487,9 +484,9 @@ STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *pos_args, mp_ma
487484
esp_sleep_enable_touchpad_wakeup();
488485
}
489486

490-
printf("Sleep time: time=%d, interval=%d, pin=%d, level=%d, wait=%llu\n",
487+
ESP_LOGD("DEEP SLEEP", "Sleep time: time=%d, interval=%d, pin=%d, level=%d, wait=%llu\n",
491488
sleep_time, stub_sleep, led_pin, args[ARG_stub_ledlevel].u_bool, wait_in_stub);
492-
prepareSleepReset(0, "ESP32: DEEP SLEEP\n");
489+
prepareSleepReset(0, NULL);
493490

494491
if ((stub_sleep) || (led_pin >= 0)) {
495492
if (led_pin >= 0) {

‎MicroPython_BUILD/components/micropython/esp32/modnetwork.c‎

Lines changed: 107 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
#include "py/runtime.h"
4646
#include "py/mphal.h"
4747
#include "py/mperrno.h"
48-
//#include "py/obj.h"
4948
#include "netutils.h"
5049
#include "esp_wifi.h"
5150
#include "esp_wifi_types.h"
@@ -147,6 +146,7 @@ static const char* const wifi_events[] = {
147146
"Ethernet got IP from connected AP",
148147
};
149148

149+
/*
150150
static const char* const wifi_cyphers[] = {
151151
"NONE",
152152
"WEP40",
@@ -156,6 +156,7 @@ static const char* const wifi_cyphers[] = {
156156
"TKIP_CCMP",
157157
"UNKNOWN",
158158
};
159+
*/
159160

160161
static const char* const wifi_auth_modes[] = {
161162
"OPEN",
@@ -378,8 +379,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
378379
_isConnected = 1;
379380
break;
380381
case SYSTEM_EVENT_STA_DISCONNECTED: {
381-
// This is a workaround as ESP32 WiFi libs don't currently
382-
// auto-reassociate.
382+
// This is a workaround as ESP32 WiFi library doesn't currently auto-reconnect.
383383
_isConnected = 0;
384384
system_event_sta_disconnected_t *disconn = &event->event_info.disconnected;
385385
ESP_LOGI("wifi", "STA_DISCONNECTED, reason:%d", disconn->reason);
@@ -407,8 +407,9 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
407407
// STA is active so attempt to reconnect.
408408
esp_err_t e = esp_wifi_connect();
409409
if (e != ESP_OK) {
410-
ESP_LOGD("wifi", "error attempting to reconnect: 0x%04x", e);
410+
ESP_LOGD("wifi", "error attempting to reconnect: (%d)", e);
411411
}
412+
else wifi_sta_connected = true;
412413
}
413414
}
414415
}
@@ -430,13 +431,6 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
430431
return ESP_OK;
431432
}
432433

433-
/*void error_check(bool status, const char *msg) {
434-
if (!status) {
435-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, msg));
436-
}
437-
}
438-
*/
439-
440434
//---------------------------------------------------
441435
STATIC void require_if(mp_obj_t wlan_if, int if_no) {
442436
wlan_if_obj_t *self = MP_OBJ_TO_PTR(wlan_if);
@@ -473,7 +467,7 @@ static void _wifi_init(wifi_mode_t mode)
473467
ESP_LOGE("modnetwork", "Error starting WiFi(%d)", ret);
474468
mp_raise_OSError(ret);
475469
}
476-
ESP_LOGD("modnetwork", "Started");
470+
ESP_LOGD("modnetwork", "Started, mode %d", mode);
477471
}
478472

479473
//-------------------------------------------------------------
@@ -482,28 +476,72 @@ STATIC mp_obj_t get_wlan(size_t n_args, const mp_obj_t *args) {
482476
mp_raise_ValueError("TCT/IP Adapter not initialized");
483477
}
484478

485-
// Get required WiFi mode
486-
int if_id = (n_args > 0) ? mp_obj_get_int(args[0]) : WIFI_IF_STA;
487-
if ((if_id != WIFI_IF_STA) && (if_id != WIFI_IF_AP)) {
488-
mp_raise_ValueError("invalid WLAN interface identifier");
489-
}
479+
int if_id = 0;
480+
wifi_mode_t wifi_mode = WIFI_MODE_NULL;
490481

491-
if (wifi_network_state < 2) {
492-
wifi_mode_t mode;
493-
if (if_id == WIFI_IF_STA) mode = WIFI_MODE_STA;
494-
else mode = WIFI_MODE_AP;
495-
_wifi_init(mode);
496-
wifi_network_state = 2;
482+
if (wifi_network_state == 2) {
483+
// wifi started, get mode
484+
esp_err_t ret = esp_wifi_get_mode(&wifi_mode);
485+
if (ret != ESP_OK) {
486+
mp_raise_ValueError("Cannot get current wifi mode");
487+
}
488+
if (wifi_mode == WIFI_MODE_STA) if_id = WIFI_IF_STA;
489+
else if_id = WIFI_IF_AP;
490+
if (n_args > 0) {
491+
// Get required WiFi mode
492+
if_id = mp_obj_get_int(args[0]);
493+
if ((if_id != WIFI_IF_STA) && (if_id != WIFI_IF_AP)) {
494+
mp_raise_ValueError("invalid WLAN interface identifier");
495+
}
496+
// check required mode
497+
if (((if_id == WIFI_IF_STA) && (wifi_mode != WIFI_MODE_STA)) ||
498+
((if_id == WIFI_IF_AP) && (wifi_mode != WIFI_MODE_AP))) {
499+
mp_raise_ValueError("WiFi already started in different mode");
500+
}
501+
}
502+
}
503+
else {
504+
// wifi not started
505+
if (n_args > 0) {
506+
// Get required WiFi mode
507+
if_id = mp_obj_get_int(args[0]);
508+
if ((if_id != WIFI_IF_STA) && (if_id != WIFI_IF_AP)) {
509+
mp_raise_ValueError("invalid WLAN interface identifier");
510+
}
511+
// set required mode mode
512+
if (if_id == WIFI_IF_STA) wifi_mode = WIFI_MODE_STA;
513+
else wifi_mode = WIFI_MODE_AP;
514+
}
515+
else {
516+
// set default mode: STA
517+
if_id = WIFI_IF_STA;
518+
wifi_mode = WIFI_MODE_STA;
519+
}
520+
wifi_network_state = 1;
521+
if (n_args > 1) {
522+
// we have the 2nd argument: active
523+
bool active = mp_obj_is_true(args[1]);
524+
if (active) {
525+
// Start wifi in requested mode
526+
wifi_mode_t mode;
527+
if (if_id == WIFI_IF_STA) mode = WIFI_MODE_STA;
528+
else mode = WIFI_MODE_AP;
529+
_wifi_init(mode);
530+
wifi_network_state = 2;
531+
}
532+
}
497533
}
498534

535+
// Return the WLAN object
499536
if (if_id == WIFI_IF_STA) return MP_OBJ_FROM_PTR(&wlan_sta_obj);
500-
elsereturn MP_OBJ_FROM_PTR(&wlan_ap_obj);
537+
return MP_OBJ_FROM_PTR(&wlan_ap_obj);
501538
}
502-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(get_wlan_obj, 0, 1, get_wlan);
539+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(get_wlan_obj, 0, 2, get_wlan);
503540

504541
//--------------------------------
505542
STATIC mp_obj_t esp_initialize() {
506543
if (wifi_network_state < 0) {
544+
// This is executed only once
507545
ESP_LOGD("modnetwork", "Initializing TCP/IP");
508546
tcpip_adapter_init();
509547
ESP_LOGD("modnetwork", "Initializing Event Loop");
@@ -526,6 +564,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_initialize_obj, esp_initialize);
526564
#error WIFI_MODE_STA and WIFI_MODE_AP are supposed to be bitfields!
527565
#endif
528566

567+
// Activate (start) Wifi
529568
//-------------------------------------------------------------
530569
STATIC mp_obj_t esp_active(size_t n_args, const mp_obj_t *args)
531570
{
@@ -534,15 +573,18 @@ STATIC mp_obj_t esp_active(size_t n_args, const mp_obj_t *args)
534573
return mp_const_false;
535574
}
536575
if (n_args < 2) {
576+
// Return wifi status (started/not started)
537577
if (wifi_network_state == 2) return mp_const_true;
538578
else return mp_const_false;
539579
}
540580

541581
wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
542582

583+
// Get requested action
543584
bool active = mp_obj_is_true(args[1]);
585+
544586
if ((!active) && (wifi_network_state == 2)) {
545-
// was active, Deactivate WiFi
587+
// == wifi started, deactivation requested ==
546588
wifi_network_state = 1;
547589
wifi_sta_isconnected = false;
548590
wifi_sta_has_ipaddress = false;
@@ -553,18 +595,20 @@ STATIC mp_obj_t esp_active(size_t n_args, const mp_obj_t *args)
553595
esp_wifi_deinit();
554596
}
555597
else if ((active) && (wifi_network_state == 1)) {
556-
// Was inactive, Activate WiFi
598+
// == wifi NOT started, activation requested ==
557599
wifi_mode_t mode;
558600
if (self->if_id == WIFI_IF_STA) mode = WIFI_MODE_STA;
559601
else mode = WIFI_MODE_AP;
560602
_wifi_init(mode);
561603
wifi_network_state = 2;
562604
}
605+
// Return new wifi status
563606
if (wifi_network_state == 2) return mp_const_true;
564607
else return mp_const_false;
565608
}
566609
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_active_obj, 1, 2, esp_active);
567610

611+
// Connect to access point (only in STA mode)
568612
//----------------------------------------------------------------
569613
STATIC mp_obj_t esp_connect(size_t n_args, const mp_obj_t *args) {
570614

@@ -578,18 +622,21 @@ STATIC mp_obj_t esp_connect(size_t n_args, const mp_obj_t *args) {
578622
ESP_LOGE("modnetwork", "Error getting WiFi mode (%d)", ret);
579623
return mp_const_none;
580624
}
625+
// Only connect if in STA mode
581626
if ((mode & WIFI_MODE_STA) == 0) return mp_const_none;
582627

583628
mp_uint_t len;
584629
const char *p;
585630
if (n_args > 1) {
631+
// Get SSID
586632
memset(&wifi_sta_config, 0, sizeof(wifi_sta_config));
587633
p = mp_obj_str_get_data(args[1], &len);
588634
memcpy(wifi_sta_config.sta.ssid, p, MIN(len, sizeof(wifi_sta_config.sta.ssid)));
635+
// Get password (optional)
589636
p = (n_args > 2) ? mp_obj_str_get_data(args[2], &len) : "";
590637
memcpy(wifi_sta_config.sta.password, p, MIN(len, sizeof(wifi_sta_config.sta.password)));
591638
if ((n_args > 3)) {
592-
// Get channel
639+
// Get channel (optional
593640
int chan = mp_obj_get_int(args[3]);
594641
if ((chan >= 1) && (chan <= 13)) wifi_sta_config.sta.channel = chan;
595642
}
@@ -682,11 +729,12 @@ STATIC mp_obj_t esp_scan(size_t n_args, const mp_obj_t *args) {
682729

683730
mp_obj_t list = mp_obj_new_list(0, NULL);
684731
wifi_scan_config_t config = { 0 };
685-
// XXX how do we scan hidden APs (and if we can scan them, are they really hidden?)
686732
if (n_args > 1) config.show_hidden = mp_obj_is_true(args[1]);
733+
687734
MP_THREAD_GIL_EXIT();
688735
esp_err_t status = esp_wifi_scan_start(&config, 1);
689736
MP_THREAD_GIL_ENTER();
737+
690738
if (status == 0) {
691739
uint16_t count = 0;
692740
ESP_EXCEPTIONS( esp_wifi_scan_get_ap_num(&count) );
@@ -732,6 +780,22 @@ STATIC mp_obj_t esp_isconnected(mp_obj_t self_in) {
732780
}
733781
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_isconnected_obj, esp_isconnected);
734782

783+
//----------------------------------------------
784+
STATIC mp_obj_t esp_isactive(mp_obj_t self_in) {
785+
if (wifi_network_state < 2) {
786+
return mp_obj_new_bool(false);
787+
}
788+
789+
wifi_mode_t wifi_mode;
790+
esp_err_t ret = esp_wifi_get_mode(&wifi_mode);
791+
if (ret != ESP_OK) return mp_obj_new_bool(false);
792+
793+
if (wifi_mode == WIFI_MODE_STA) return mp_obj_new_bool(((wifi_sta_isconnected) && (wifi_sta_has_ipaddress)));
794+
else if (wifi_mode == WIFI_MODE_AP) return mp_obj_new_bool(wifi_ap_isconnected);
795+
return mp_obj_new_bool(false);
796+
}
797+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_isactive_obj, esp_isactive);
798+
735799
//-----------------------------------------------------------------
736800
STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
737801
if (wifi_network_state < 2) {
@@ -743,18 +807,21 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
743807
tcpip_adapter_get_ip_info(self->if_id, &info);
744808
tcpip_adapter_get_dns_info(self->if_id, TCPIP_ADAPTER_DNS_MAIN, &dns_info);
745809
if (n_args == 1) {
746-
// get
810+
// get configuration
747811
mp_obj_t tuple[4] = {
748812
netutils_format_ipv4_addr((uint8_t*)&info.ip, NETUTILS_BIG),
749813
netutils_format_ipv4_addr((uint8_t*)&info.netmask, NETUTILS_BIG),
750814
netutils_format_ipv4_addr((uint8_t*)&info.gw, NETUTILS_BIG),
751815
netutils_format_ipv4_addr((uint8_t*)&dns_info.ip, NETUTILS_BIG),
752816
};
753817
return mp_obj_new_tuple(4, tuple);
754-
} else {
755-
// set
818+
}
819+
else {
820+
// set configuration parameters
756821
mp_obj_t *items;
757822
mp_obj_get_array_fixed_n(args[1], 4, &items);
823+
824+
// Static IP
758825
netutils_parse_ipv4_addr(items[0], (void*)&info.ip, NETUTILS_BIG);
759826
if (mp_obj_is_integer(items[1])) {
760827
// allow numeric netmask, i.e.:
@@ -767,8 +834,11 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
767834
else {
768835
netutils_parse_ipv4_addr(items[1], (void*)&info.netmask, NETUTILS_BIG);
769836
}
837+
// net mask
770838
netutils_parse_ipv4_addr(items[2], (void*)&info.gw, NETUTILS_BIG);
839+
// gateway
771840
netutils_parse_ipv4_addr(items[3], (void*)&dns_info.ip, NETUTILS_BIG);
841+
772842
// To set a static IP we have to disable DHCP first
773843
if ((self->if_id == WIFI_IF_STA) || (self->if_id == ESP_IF_ETH)) {
774844
esp_err_t e = tcpip_adapter_dhcpc_stop(self->if_id);
@@ -788,6 +858,7 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
788858
}
789859
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig);
790860

861+
// Set or get wifi configuration parameters
791862
//---------------------------------------------------------------------------------
792863
STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
793864
if (wifi_network_state < 2) {
@@ -989,6 +1060,7 @@ STATIC const mp_map_elem_t wlan_if_locals_dict_table[] = {
9891060
{ MP_OBJ_NEW_QSTR(MP_QSTR_status), (mp_obj_t)&esp_status_obj },
9901061
{ MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&esp_scan_obj },
9911062
{ MP_OBJ_NEW_QSTR(MP_QSTR_isconnected), (mp_obj_t)&esp_isconnected_obj },
1063+
{ MP_OBJ_NEW_QSTR(MP_QSTR_isactive), (mp_obj_t)&esp_isactive_obj },
9921064
{ MP_OBJ_NEW_QSTR(MP_QSTR_config), (mp_obj_t)&esp_config_obj },
9931065
{ MP_OBJ_NEW_QSTR(MP_QSTR_ifconfig), (mp_obj_t)&esp_ifconfig_obj },
9941066
{ MP_OBJ_NEW_QSTR(MP_QSTR_eventCB), (mp_obj_t)&esp_callback_obj },
@@ -1014,6 +1086,9 @@ extern const mp_obj_type_t mqtt_type;
10141086
#endif
10151087

10161088

1089+
// ==============================
1090+
// ==== FTP & Telnet services ===
1091+
10171092
#if defined(CONFIG_MICROPY_USE_TELNET) || defined(CONFIG_MICROPY_USE_FTPSERVER)
10181093
#include "mpthreadport.h"
10191094
#endif

‎MicroPython_BUILD/components/micropython/esp32/modsocket.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,25 @@ STATIC mp_uint_t socket_stream_ioctl(mp_obj_t self_in, mp_uint_t request, uintpt
510510
if (FD_ISSET(socket->fd, &wfds)) ret |= MP_STREAM_POLL_WR;
511511
if (FD_ISSET(socket->fd, &efds)) ret |= MP_STREAM_POLL_HUP;
512512
return ret;
513+
} else if (request == MP_STREAM_CLOSE) {
514+
if (socket->fd >= 0) {
515+
int ret = lwip_close_r(socket->fd);
516+
if (ret != 0) {
517+
*errcode = errno;
518+
return MP_STREAM_ERROR;
519+
}
520+
socket->fd = -1;
521+
}
522+
return 0;
513523
}
514524

515525
*errcode = MP_EINVAL;
516526
return MP_STREAM_ERROR;
517527
}
518528

519529
STATIC const mp_map_elem_t socket_locals_dict_table[] = {
520-
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&socket_close_obj },
521-
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj },
530+
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&mp_stream_close_obj },
531+
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&mp_stream_close_obj },
522532
{ MP_OBJ_NEW_QSTR(MP_QSTR_bind), (mp_obj_t)&socket_bind_obj },
523533
{ MP_OBJ_NEW_QSTR(MP_QSTR_listen), (mp_obj_t)&socket_listen_obj },
524534
{ MP_OBJ_NEW_QSTR(MP_QSTR_accept), (mp_obj_t)&socket_accept_obj },

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /