diff --git a/MicroPython_BUILD/.gitignore b/MicroPython_BUILD/.gitignore index 34ebe8c..4d46a24 100644 --- a/MicroPython_BUILD/.gitignore +++ b/MicroPython_BUILD/.gitignore @@ -31,6 +31,6 @@ sdkconfig.fw_esp32_ota sdkconfig.fw_psram sdkconfig.fw_psram_ota sdkconfig.fw_all -sdkconfig.fw_all_psram +sdkconfig.fw_psram_all make_firmwares.sh diff --git a/MicroPython_BUILD/BUILD.sh b/MicroPython_BUILD/BUILD.sh index 509340e..e926857 100755 --- a/MicroPython_BUILD/BUILD.sh +++ b/MicroPython_BUILD/BUILD.sh @@ -45,7 +45,7 @@ #======================= -TOOLS_VER=ver20180117.id +TOOLS_VER=ver20180127.id #======================= # ----------------------------- diff --git a/MicroPython_BUILD/components/micropython/esp32/modnetwork.c b/MicroPython_BUILD/components/micropython/esp32/modnetwork.c index e31c8b7..d501fff 100644 --- a/MicroPython_BUILD/components/micropython/esp32/modnetwork.c +++ b/MicroPython_BUILD/components/micropython/esp32/modnetwork.c @@ -35,6 +35,7 @@ #include #include +#include "sdkconfig.h" #include "py/nlr.h" #include "py/objlist.h" #include "py/runtime.h" @@ -48,6 +49,9 @@ #include "esp_log.h" #include "lwip/dns.h" #include "tcpip_adapter.h" +#ifdef CONFIG_MICROPY_USE_MDNS +#include "mdns.h" +#endif #include "modnetwork.h" @@ -182,6 +186,10 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) { ESP_LOGI("network", "event %d", event->event_id); break; } + + #ifdef CONFIG_MICROPY_USE_MDNS + mdns_handle_system_event(ctx, event); + #endif return ESP_OK; } diff --git a/MicroPython_BUILD/components/micropython/esp32/mphalport.c b/MicroPython_BUILD/components/micropython/esp32/mphalport.c index 89e4337..3284b4e 100644 --- a/MicroPython_BUILD/components/micropython/esp32/mphalport.c +++ b/MicroPython_BUILD/components/micropython/esp32/mphalport.c @@ -317,7 +317,7 @@ int mp_hal_delay_ms(uint32_t ms) ncheck++; if (ncheck >= 50) { ncheck = 0; - if (mp_thread_checknotify()) break; + if (mp_thread_getnotify(1)) break; } } diff --git a/MicroPython_BUILD/components/micropython/esp32/mpthreadport.c b/MicroPython_BUILD/components/micropython/esp32/mpthreadport.c index 0f5467e..33ceccf 100644 --- a/MicroPython_BUILD/components/micropython/esp32/mpthreadport.c +++ b/MicroPython_BUILD/components/micropython/esp32/mpthreadport.c @@ -83,11 +83,12 @@ typedef struct _thread_t { size_t stack_len; // number of words in the stack char name[THREAD_NAME_MAX_SIZE]; // thread name QueueHandle_t threadQueue; // queue used for inter thread communication - int allow_suspend; - int suspended; - int waiting; - int deleted; - uint32_t type; + int8_t allow_suspend; + int8_t suspended; + int8_t waiting; + int8_t deleted; + int16_t notifyed; + uint16_t type; struct _thread_t *next; } thread_t; @@ -138,6 +139,7 @@ void mp_thread_preinit(void *stack, uint32_t stack_len) { thread->suspended = 0; thread->waiting = 0; thread->deleted = 0; + thread->notifyed = 0; thread->type = THREAD_TYPE_MAIN; thread->next = NULL; MainTaskHandle = thread->id; @@ -268,6 +270,7 @@ TaskHandle_t mp_thread_create_ex(void *(*entry)(void*), void *arg, size_t *stack th->suspended = 0; th->waiting = 0; th->deleted = 0; + th->notifyed = 0; th->type = THREAD_TYPE_PYTHON; thread = th; @@ -439,8 +442,9 @@ int mp_thread_notify(TaskHandle_t id, uint32_t value) { int res = 0; mp_thread_mutex_lock(&thread_mutex, 1); for (thread_t *th = thread; th != NULL; th = th->next) { - if ((id == 0) || (th->id == id)) { - res = xTaskNotify(th->id, value, eSetValueWithoutOverwrite); + if ( (th->id != xTaskGetCurrentTaskHandle()) && ( (id == 0) || (th->id == id) ) ) { + res = xTaskNotify(th->id, value, eSetValueWithOverwrite); //eSetValueWithoutOverwrite + th->notifyed = 1; if (id != 0) break; } } @@ -449,15 +453,17 @@ int mp_thread_notify(TaskHandle_t id, uint32_t value) { return res; } -//------------------------------ -uint32_t mp_thread_getnotify() { +//--------------------------------------------- +uint32_t mp_thread_getnotify(bool check_only) { uint32_t value = 0; mp_thread_mutex_lock(&thread_mutex, 1); for (thread_t *th = thread; th != NULL; th = th->next) { if (th->id == xTaskGetCurrentTaskHandle()) { - if (xTaskNotifyWait(0, 0xffffffffUL, &value, 1) != pdPASS) { - value = 0; - } + xTaskNotifyWait(0, 0, &value, 0); + if (!check_only) { + xTaskNotifyWait(ULONG_MAX, ULONG_MAX, NULL, 0); + th->notifyed = 0; + } break; } } @@ -465,18 +471,30 @@ uint32_t mp_thread_getnotify() { return value; } -//-------------------------------- -uint32_t mp_thread_checknotify() { - uint32_t value = 0; +//-------------------------------------------- +int mp_thread_notifyPending(TaskHandle_t id) { + int res = -1; mp_thread_mutex_lock(&thread_mutex, 1); for (thread_t *th = thread; th != NULL; th = th->next) { + if (th->id == id) { + res = th->notifyed; + break; + } + } + mp_thread_mutex_unlock(&thread_mutex); + return res; +} + +//----------------------------- +void mp_thread_resetPending() { + mp_thread_mutex_lock(&thread_mutex, 1); + for (thread_t *th = thread; th != NULL; th = th->next) { if (th->id == xTaskGetCurrentTaskHandle()) { - value = xTaskNotifyWait(0, 0, &value, 0); - break; + th->notifyed = 0; + break; } } mp_thread_mutex_unlock(&thread_mutex); - return value; } //------------------------------ @@ -496,6 +514,8 @@ uint32_t mp_thread_getSelfID() { //------------------------------------- int mp_thread_getSelfname(char *name) { int res = 0; + name[0] = '?'; + name[1] = '\0'; mp_thread_mutex_lock(&thread_mutex, 1); for (thread_t *th = thread; th != NULL; th = th->next) { if (th->id == xTaskGetCurrentTaskHandle()) { @@ -511,6 +531,8 @@ int mp_thread_getSelfname(char *name) { //-------------------------------------------------- int mp_thread_getname(TaskHandle_t id, char *name) { int res = 0; + name[0] = '?'; + name[1] = '\0'; mp_thread_mutex_lock(&thread_mutex, 1); for (thread_t *th = thread; th != NULL; th = th->next) { if (th->id == id) { diff --git a/MicroPython_BUILD/components/micropython/esp32/mpthreadport.h b/MicroPython_BUILD/components/micropython/esp32/mpthreadport.h index cb87953..78b27e0 100644 --- a/MicroPython_BUILD/components/micropython/esp32/mpthreadport.h +++ b/MicroPython_BUILD/components/micropython/esp32/mpthreadport.h @@ -131,8 +131,9 @@ void mp_thread_allowsuspend(int allow); int mp_thread_suspend(TaskHandle_t id); int mp_thread_resume(TaskHandle_t id); int mp_thread_notify(TaskHandle_t id, uint32_t value); -uint32_t mp_thread_getnotify(); -uint32_t mp_thread_checknotify(); +uint32_t mp_thread_getnotify(bool check_only); +int mp_thread_notifyPending(TaskHandle_t id); +void mp_thread_resetPending(); int mp_thread_semdmsg(TaskHandle_t id, int type, uint32_t msg_int, uint8_t *buf, uint32_t buflen); int mp_thread_getmsg(uint32_t *msg_int, uint8_t **buf, uint32_t *buflen, uint32_t *sender); int mp_thread_status(TaskHandle_t id); diff --git a/MicroPython_BUILD/components/micropython/esp32/mpversion.h b/MicroPython_BUILD/components/micropython/esp32/mpversion.h index 0ef8fe3..e9f7d21 100644 --- a/MicroPython_BUILD/components/micropython/esp32/mpversion.h +++ b/MicroPython_BUILD/components/micropython/esp32/mpversion.h @@ -1,8 +1,8 @@ // MicroPython version info -#define MICROPY_GIT_TAG "ESP32_LoBo_v3.1.11" -#define MICROPY_GIT_HASH "g2ddee729" -#define MICROPY_BUILD_DATE "2017-01-24" +#define MICROPY_GIT_TAG "ESP32_LoBo_v3.1.12" +#define MICROPY_GIT_HASH "gdaa8cfa8" +#define MICROPY_BUILD_DATE "2017-01-27" #define MICROPY_VERSION_MAJOR (3) #define MICROPY_VERSION_MINOR (1) -#define MICROPY_VERSION_MICRO (11) -#define MICROPY_VERSION_STRING "3.1.11" +#define MICROPY_VERSION_MICRO (12) +#define MICROPY_VERSION_STRING "3.1.12" diff --git a/MicroPython_BUILD/components/micropython/esp32/network_mdns.c b/MicroPython_BUILD/components/micropython/esp32/network_mdns.c index d3be361..b33e9a9 100644 --- a/MicroPython_BUILD/components/micropython/esp32/network_mdns.c +++ b/MicroPython_BUILD/components/micropython/esp32/network_mdns.c @@ -37,62 +37,47 @@ #include "py/mphal.h" #include "modnetwork.h" +#include "netdb.h" + #define MDNS_NAME_LEN 32 typedef struct _mdns_obj_t { mp_obj_base_t base; - wlan_if_obj_t *if_obj; - mdns_server_t * mdns; + uint8_t is_started; char hostname[MDNS_NAME_LEN+1]; char instance[MDNS_NAME_LEN+1]; } mdns_obj_t; +static const char * if_str[] = {"STA", "AP", "ETH", "MAX"}; +static const char * ip_protocol_str[] = {"V4", "V6", "MAX"}; + const mp_obj_type_t mdns_type; +mdns_obj_t mdns_obj = {0}; + //------------------------------------------------------------------------------------- STATIC void mdns_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { mdns_obj_t *self = self_in; - if (self->mdns == NULL) { + if (!self->is_started) { mp_printf(print, "mDNS( Not started )\n"); return; } - char s_if[8]; - if (self->if_obj->if_id == TCPIP_ADAPTER_IF_STA) sprintf(s_if, "IF_STA"); - else if (self->if_obj->if_id == TCPIP_ADAPTER_IF_AP) sprintf(s_if, "IF_AP"); - else sprintf(s_if, "Unknown"); - mp_printf(print, "mDNS[%s] (Server name: %s, Instance name: %s)\n", s_if, self->hostname, self->instance); + mp_printf(print, "mDNS(Server name: %s, Instance name: %s)\n", self->hostname, self->instance); } //------------------------------------------------------------------------------------------------------------ STATIC mp_obj_t mdns_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - const mp_arg_t mdns_init_allowed_args[] = { - { MP_QSTR_if, MP_ARG_OBJ, {.u_obj = mp_const_none} }, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(mdns_init_allowed_args)]; - mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(mdns_init_allowed_args), mdns_init_allowed_args, args); + // Get the mdns object + mdns_obj_t *self = &mdns_obj; + self->base.type = &mdns_type; - // Setup the mqtt object - mdns_obj_t *self = m_new_obj(mdns_obj_t ); - self->mdns = NULL; - - wlan_if_obj_t *if_obj = (wlan_if_obj_t *)args[0].u_obj; - - if (MP_OBJ_IS_TYPE(if_obj, &wlan_if_type)) { - self->hostname[0] = '\0'; - self->instance[0] = '\0'; - self->if_obj = if_obj; - self->base.type = &mdns_type; - - return MP_OBJ_FROM_PTR(self); - } - mp_raise_msg(&mp_type_OSError, "WLAN STA or AP object expected"); - return mp_const_none; + return MP_OBJ_FROM_PTR(self); } //--------------------------------------------------------------------------------------- @@ -110,27 +95,48 @@ STATIC mp_obj_t mdns_start(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t mdns_obj_t *self = pos_args[0]; esp_err_t err; - snprintf(self->hostname, MDNS_NAME_LEN, mp_obj_str_get_str(args[ARG_name].u_obj)); - snprintf(self->instance, MDNS_NAME_LEN, mp_obj_str_get_str(args[ARG_instance].u_obj)); - self->base.type = &mdns_type; - - if (!self->mdns) { - err = mdns_init(self->if_obj->if_id, &self->mdns); + if (!self->is_started) { + // If not initialized, host name and instance are mandatory + snprintf(self->hostname, MDNS_NAME_LEN, mp_obj_str_get_str(args[ARG_name].u_obj)); + snprintf(self->instance, MDNS_NAME_LEN, mp_obj_str_get_str(args[ARG_instance].u_obj)); + err = mdns_init(); if (err) mp_raise_msg(&mp_type_OSError, "Error initializing mDNS server."); + //set mDNS hostname (required if we want to advertise services) + err = mdns_hostname_set(self->hostname); + if (err) { + mdns_free(); + mp_raise_ValueError("Error setting server name."); + } + err = mdns_instance_name_set(self->instance); + if (err) { + mdns_free(); + mp_raise_ValueError("Error setting server instance."); + } + self->is_started = 1; + return mp_const_none; } - else mp_raise_msg(&mp_type_OSError, "mDNS server already started."); - err = mdns_set_hostname(self->mdns, self->hostname); - if (err) { - mdns_free(self->mdns); - self->mdns = NULL; - mp_raise_ValueError("Error setting server name."); + if (args[ARG_name].u_obj != mp_const_none) { + const char *hostname = mp_obj_str_get_str(args[ARG_name].u_obj); + if (strcmp(self->hostname, hostname) != 0) { + err = mdns_hostname_set(self->hostname); + if (err) { + mdns_free(); + self->is_started = 0; + mp_raise_ValueError("Error setting server name."); + } + } } - err = mdns_set_instance(self->mdns, self->instance); - if (err) { - mdns_free(self->mdns); - self->mdns = NULL; - mp_raise_ValueError("Error setting server instance."); + if (args[ARG_instance].u_obj != mp_const_none) { + const char *instance = mp_obj_str_get_str(args[ARG_instance].u_obj); + if (strcmp(self->instance, instance) != 0) { + err = mdns_instance_name_set(self->instance); + if (err) { + mdns_free(); + self->is_started = 0; + mp_raise_ValueError("Error setting server instance."); + } + } } return mp_const_none; @@ -142,8 +148,8 @@ STATIC mp_obj_t mdns_stop(mp_obj_t self_in) { mdns_obj_t *self = self_in; - if (self->mdns) mdns_free(self->mdns); - self->mdns = NULL; + if (self->is_started) mdns_free(); + self->is_started = 0; return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(mdns_stop_obj, mdns_stop); @@ -165,45 +171,44 @@ STATIC mp_obj_t mdns_add_service(mp_uint_t n_args, const mp_obj_t *pos_args, mp_ mdns_obj_t *self = pos_args[0]; - if (!self->mdns) mp_raise_msg(&mp_type_OSError, "mDNS server not started."); + if (!self->is_started) mp_raise_msg(&mp_type_OSError, "mDNS server not started."); const char *instance = NULL; uint8_t ntxdata = 0; - const char *svctxdata[8] = {NULL}; + mdns_txt_item_t svctxdata[8] = {0}; const char *service = mp_obj_str_get_str(args[ARG_service].u_obj); + if (service[0] != '_') { + mp_raise_ValueError("Service name must start with '_'"); + } const char *protocol = mp_obj_str_get_str(args[ARG_proto].u_obj); + if ((strcmp(protocol, "_tcp") != 0) && (strcmp(protocol, "_udp") != 0)) { + mp_raise_ValueError("Protocol must be '_tcp' or '_udp'"); + } int port = args[ARG_port].u_int; + if ((port < 1) || (port > 0xFFFF)) { + mp_raise_ValueError("Wrong port value"); + } if (MP_OBJ_IS_STR(args[ARG_instance].u_obj)) { instance = mp_obj_str_get_str(args[ARG_instance].u_obj); } - if (MP_OBJ_IS_STR(args[ARG_txdata].u_obj)) { - svctxdata[0] = mp_obj_str_get_str(args[ARG_txdata].u_obj); - ntxdata = 1; - } - else if (MP_OBJ_IS_TYPE(args[ARG_txdata].u_obj, &mp_type_tuple)) { - mp_obj_t *items; - uint len; - mp_obj_tuple_get(args[ARG_txdata].u_obj, &len, &items); - if (len > 8) len = 8; - for (int i = 0; i < len; i++) { - svctxdata[i] = mp_obj_str_get_str(items[i]); - ntxdata++; + + if (MP_OBJ_IS_TYPE(args[ARG_txdata].u_obj, &mp_type_dict)) { + mp_obj_dict_t *params = MP_OBJ_TO_PTR(args[ARG_txdata].u_obj); + mp_map_t *map = ¶ms->map; + mp_map_elem_t *table = map->table; + if (map->used > 0) { + for (int i=0; iused; i++) { + if (i > 7) break; + svctxdata[i].key = (char *)mp_obj_str_get_str(table[i].key); + svctxdata[i].value = (char *)mp_obj_str_get_str(table[i].value); + ntxdata++; + } } } //add the services - if (mdns_service_add(self->mdns, service, protocol, port) != ESP_OK) return mp_const_false; - - if (instance) { - //NOTE: services must be added before their properties can be set - if (mdns_service_instance_set(self->mdns, service, protocol, instance) != ESP_OK) return mp_const_false; - } - - if (ntxdata) { - //set text data for service (will free and replace current data) - if (mdns_service_txt_set(self->mdns, service, protocol, ntxdata, svctxdata) != ESP_OK) return mp_const_false; - } + if (mdns_service_add(instance, service, protocol, port, svctxdata, ntxdata) != ESP_OK) return mp_const_false; return mp_const_true; } @@ -223,13 +228,13 @@ STATIC mp_obj_t mdns_remove_service(mp_uint_t n_args, const mp_obj_t *pos_args, mdns_obj_t *self = pos_args[0]; - if (!self->mdns) mp_raise_msg(&mp_type_OSError, "mDNS server not started."); + if (!self->is_started) mp_raise_msg(&mp_type_OSError, "mDNS server not started."); const char *service = mp_obj_str_get_str(args[ARG_service].u_obj); const char *protocol = mp_obj_str_get_str(args[ARG_proto].u_obj); //remove the services - if (mdns_service_remove(self->mdns, service, protocol) != ESP_OK) return mp_const_false; + if (mdns_service_remove(service, protocol) != ESP_OK) return mp_const_false; return mp_const_true; } @@ -240,102 +245,143 @@ STATIC mp_obj_t mdns_host_query(mp_uint_t n_args, const mp_obj_t *pos_args, mp_m { const mp_arg_t mdns_allowed_args[] = { { MP_QSTR_hostname, MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 2000} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(mdns_allowed_args)]; mp_arg_parse_all(n_args-1, pos_args+1, kw_args, MP_ARRAY_SIZE(mdns_allowed_args), mdns_allowed_args, args); mdns_obj_t *self = pos_args[0]; - if (!self->mdns) mp_raise_msg(&mp_type_OSError, "mDNS server not started."); + if (!self->is_started) mp_raise_msg(&mp_type_OSError, "mDNS server not started."); const char *hostname = mp_obj_str_get_str(args[0].u_obj); + int tmo = args[1].u_int; + if ((tmo < 100) || (tmo > 10000)) tmo = 2000; + struct ip4_addr addr; + addr.addr = 0; - mp_obj_t list = mp_obj_new_list(0, NULL); - uint32_t res; - char tmps[64]; + esp_err_t res; + char tmps[64] = {0}; - mp_obj_tuple_t *t = mp_obj_new_tuple(2, NULL); // Host Lookup - res = mdns_query(self->mdns, hostname, 0, 2000); - if (res) { - size_t i; - for(i=0; imdns, i); - if (r) { - sprintf(tmps, IPSTR, IP2STR(&r->addr)); - t->items[0] = mp_obj_new_str(tmps, strlen(tmps), false); + res = mdns_query_a(hostname, 2000, &addr); - sprintf(tmps, IPV6STR, IPV62STR(r->addrv6)); - t->items[1] = mp_obj_new_str(tmps, strlen(tmps), false); + if (res == ESP_OK) sprintf(tmps, IPSTR, IP2STR(&addr)); + else if (res == ESP_ERR_NOT_FOUND) sprintf(tmps, "Host was not found!"); + else sprintf(tmps, "Query Failed"); - mp_obj_list_append(list, MP_OBJ_FROM_PTR(t)); - } - } - mdns_result_free(self->mdns); - } - - return list; + return mp_obj_new_str(tmps, strlen(tmps), false);; } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mdns_host_query_obj, 2, mdns_host_query); - //----------------------------------------------------------------------------------------------- STATIC mp_obj_t mdns_service_query(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { const mp_arg_t mdns_allowed_args[] = { { MP_QSTR_service, MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_protocol, MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 2000} }, + { MP_QSTR_maxres, MP_ARG_INT, {.u_int = 8} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(mdns_allowed_args)]; mp_arg_parse_all(n_args-1, pos_args+1, kw_args, MP_ARRAY_SIZE(mdns_allowed_args), mdns_allowed_args, args); mdns_obj_t *self = pos_args[0]; - if (!self->mdns) mp_raise_msg(&mp_type_OSError, "mDNS server not started."); + if (!self->is_started) mp_raise_msg(&mp_type_OSError, "mDNS server not started."); const char *service = mp_obj_str_get_str(args[0].u_obj); const char *proto = mp_obj_str_get_str(args[1].u_obj); + if (service[0] != '_') { + mp_raise_ValueError("Service name must start with '_'"); + } + if ((strcmp(proto, "_tcp") != 0) && (strcmp(proto, "_udp") != 0)) { + mp_raise_ValueError("Protocol must be '_tcp' or '_udp'"); + } + + int tmo = args[2].u_int; + if ((tmo < 100) || (tmo > 10000)) tmo = 2000; + + int maxres = args[3].u_int; + if ((maxres < 1) || (maxres > 30)) maxres = 10; mp_obj_t list = mp_obj_new_list(0, NULL); - uint32_t res; - char tmps[128]; + mdns_result_t * results = NULL; + esp_err_t err = mdns_query_ptr(service, proto, tmo, maxres, &results); - mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL); // Service Lookup - res = mdns_query(self->mdns, service, proto, 2000); - if (res) { - size_t i; - for(i=0; imdns, i); - if (r) { - sprintf(tmps, "%s", (r->host) ? r->host : ""); + if (err == ESP_OK) { + if (results) { + mp_obj_tuple_t *t = mp_obj_new_tuple(7, NULL); + char tmps[128]; + mdns_result_t * r = results; + mdns_ip_addr_t * a = NULL; + + while (r) { + // Interface type + sprintf(tmps, "%s", if_str[r->tcpip_if]); t->items[0] = mp_obj_new_str(tmps, strlen(tmps), false); - sprintf(tmps, "%s", (r->instance) ? r->instance : ""); + // Protocol, V4 or V6 + sprintf(tmps, "%s", ip_protocol_str[r->ip_protocol]); t->items[1] = mp_obj_new_str(tmps, strlen(tmps), false); - sprintf(tmps, IPSTR, IP2STR(&r->addr)); + // Instance name + if (r->instance_name) sprintf(tmps, "%s", r->instance_name); + else sprintf(tmps, "?"); t->items[2] = mp_obj_new_str(tmps, strlen(tmps), false); - sprintf(tmps, IPV6STR, IPV62STR(r->addrv6)); - t->items[3] = mp_obj_new_str(tmps, strlen(tmps), false); + // Host name & port + if(r->hostname) { + sprintf(tmps, "%s.local", r->hostname); + t->items[3] = mp_obj_new_str(tmps, strlen(tmps), false); + t->items[4] = mp_obj_new_int(r->port); + } + else { + t->items[3] = mp_const_none; + t->items[4] = mp_const_none; + } - t->items[4] = mp_obj_new_int(r->port); + // IP addresses + a = r->addr; + if (a) { + mp_obj_t addrlist = mp_obj_new_list(0, NULL); + while (a) { + if (a->addr.type == MDNS_IP_PROTOCOL_V6) { + sprintf(tmps, IPV6STR, IPV62STR(a->addr.u_addr.ip6)); + } + else { + sprintf(tmps, IPSTR, IP2STR(&(a->addr.u_addr.ip4))); + } + mp_obj_list_append(addrlist, mp_obj_new_str(tmps, strlen(tmps), false)); + a = a->next; + } + t->items[5] = addrlist; + } + else t->items[5] = mp_const_none; - sprintf(tmps, "%s", (r->txt) ? r->txt : ""); - t->items[5] = mp_obj_new_str(tmps, strlen(tmps), false); + // Text records + if(r->txt_count){ + mp_obj_dict_t *dct = mp_obj_new_dict(0); + for(int i=0; itxt_count; i++){ + mp_obj_dict_store(dct, mp_obj_new_str(r->txt[i].key, strlen(r->txt[i].key), false), mp_obj_new_str(r->txt[i].value, strlen(r->txt[i].key), false)); + } + t->items[6] = dct; + } + else t->items[6] = mp_const_none; - mp_obj_list_append(list, MP_OBJ_FROM_PTR(t)); + mp_obj_list_append(list, MP_OBJ_FROM_PTR(t)); + + r = r->next; } + mdns_query_results_free(results); } - mdns_result_free(self->mdns); } return list; } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mdns_service_query_obj, 3, mdns_service_query); - //========================================================= STATIC const mp_rom_map_elem_t mdns_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR___del__), (mp_obj_t)&mdns_stop_obj }, diff --git a/MicroPython_BUILD/components/micropython/lib/mp-readline/readline.c b/MicroPython_BUILD/components/micropython/lib/mp-readline/readline.c index 3347bb5..074f553 100644 --- a/MicroPython_BUILD/components/micropython/lib/mp-readline/readline.c +++ b/MicroPython_BUILD/components/micropython/lib/mp-readline/readline.c @@ -430,7 +430,7 @@ STATIC void check_notifications(const char *prompt) uint32_t from_th; char th_name[THREAD_NAME_MAX_SIZE]; - uint32_t notify_val = mp_thread_getnotify(); + uint32_t notify_val = mp_thread_getnotify(0); if (notify_val > 0) { mp_printf(&mp_plat_print,"\n[Notification] %u\n", notify_val); //mp_hal_stdout_tx_str(prompt); diff --git a/MicroPython_BUILD/components/micropython/py/modthread.c b/MicroPython_BUILD/components/micropython/py/modthread.c index 4c1968e..98d85c0 100644 --- a/MicroPython_BUILD/components/micropython/py/modthread.c +++ b/MicroPython_BUILD/components/micropython/py/modthread.c @@ -295,6 +295,16 @@ STATIC mp_obj_t mod_thread_notify(mp_obj_t in_id, mp_obj_t in_value) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_thread_notify_obj, mod_thread_notify); +//----------------------------------------------------- +STATIC mp_obj_t mod_thread_isnotified(mp_obj_t in_id) { + uintptr_t thr_id = mp_obj_get_int(in_id); + int notify = mp_thread_notifyPending((void *)thr_id); + + if (notify == 0) return mp_const_true; + return mp_const_false; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_thread_isnotified_obj, mod_thread_isnotified); + //-------------------------------------- STATIC mp_obj_t mod_thread_getREPLId() { return mp_obj_new_int_from_uint((uintptr_t)MainTaskHandle); @@ -304,7 +314,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_getREPLId_obj, mod_thread_getREPLId) //-------------------------------------- STATIC mp_obj_t mod_thread_getnotify() { - uint32_t not_val = mp_thread_getnotify(); + uint32_t not_val = mp_thread_getnotify(0); return MP_OBJ_NEW_SMALL_INT(not_val); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_getnotify_obj, mod_thread_getnotify); @@ -483,7 +493,9 @@ STATIC mp_obj_t mod_thread_waitnotify(mp_uint_t n_args, const mp_obj_t *args) { if (mp_thread_setblocked()) { MP_THREAD_GIL_EXIT(); - if (xTaskNotifyWait(0, 0xffffffffUL, ¬_val, tmo) == pdPASS) { + if (xTaskNotifyWait(0, 0, ¬_val, tmo) == pdPASS) { + xTaskNotifyWait(ULONG_MAX, ULONG_MAX, NULL, 0); + mp_thread_resetPending(); ret = MP_OBJ_NEW_SMALL_INT(not_val); } mp_thread_setnotblocked(); @@ -494,6 +506,22 @@ STATIC mp_obj_t mod_thread_waitnotify(mp_uint_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_waitnotify_obj, 0, 1, mod_thread_waitnotify); +//------------------------------- +STATIC mp_obj_t mod_thread_lock() +{ + MP_STATE_VM(thread_lock) = 1; + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_lock_obj, mod_thread_lock); + +//------------------------------ +STATIC mp_obj_t mod_thread_unlock() +{ + MP_STATE_VM(thread_lock) = 0; + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_unlock_obj, mod_thread_unlock); + //================================================================= STATIC const mp_rom_map_elem_t mp_module_thread_globals_table[] = { @@ -505,9 +533,10 @@ STATIC const mp_rom_map_elem_t mp_module_thread_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_resume), MP_ROM_PTR(&mod_thread_resume_obj) }, { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&mod_thread_stop_obj) }, { MP_ROM_QSTR(MP_QSTR_notify), MP_ROM_PTR(&mod_thread_notify_obj) }, + { MP_ROM_QSTR(MP_QSTR_getnotification), MP_ROM_PTR(&mod_thread_getnotify_obj) }, + { MP_ROM_QSTR(MP_QSTR_isnotified), MP_ROM_PTR(&mod_thread_isnotified_obj) }, { MP_ROM_QSTR(MP_QSTR_getReplID), MP_ROM_PTR(&mod_thread_getREPLId_obj) }, { MP_ROM_QSTR(MP_QSTR_replAcceptMsg), MP_ROM_PTR(&mod_thread_replAcceptMsg_obj) }, - { MP_ROM_QSTR(MP_QSTR_getnotification), MP_ROM_PTR(&mod_thread_getnotify_obj) }, { MP_ROM_QSTR(MP_QSTR_sendmsg), MP_ROM_PTR(&mod_thread_sendmsg_obj) }, { MP_ROM_QSTR(MP_QSTR_getmsg), MP_ROM_PTR(&mod_thread_getmsg_obj) }, { MP_ROM_QSTR(MP_QSTR_list), MP_ROM_PTR(&mod_thread_list_obj) }, @@ -515,6 +544,8 @@ STATIC const mp_rom_map_elem_t mp_module_thread_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_getSelfName), MP_ROM_PTR(&mod_thread_getSelfname_obj) }, { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&mod_thread_status_obj) }, { MP_ROM_QSTR(MP_QSTR_wait), MP_ROM_PTR(&mod_thread_waitnotify_obj) }, + { MP_ROM_QSTR(MP_QSTR_lock), MP_ROM_PTR(&mod_thread_lock_obj) }, + { MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&mod_thread_unlock_obj) }, // Constants { MP_ROM_QSTR(MP_QSTR_PAUSE), MP_ROM_INT(THREAD_NOTIFY_PAUSE) }, diff --git a/MicroPython_BUILD/components/micropython/py/mpstate.h b/MicroPython_BUILD/components/micropython/py/mpstate.h index 6a39ebd..18b6772 100644 --- a/MicroPython_BUILD/components/micropython/py/mpstate.h +++ b/MicroPython_BUILD/components/micropython/py/mpstate.h @@ -206,6 +206,7 @@ typedef struct _mp_state_vm_t { #if MICROPY_PY_THREAD_GIL // This is a global mutex used to make the VM/runtime thread-safe. mp_thread_mutex_t gil_mutex; + volatile int16_t thread_lock; #endif } mp_state_vm_t; diff --git a/MicroPython_BUILD/components/micropython/py/runtime.c b/MicroPython_BUILD/components/micropython/py/runtime.c index 17e5d23..0599f80 100644 --- a/MicroPython_BUILD/components/micropython/py/runtime.c +++ b/MicroPython_BUILD/components/micropython/py/runtime.c @@ -122,6 +122,7 @@ void mp_init(void) { #if MICROPY_PY_THREAD_GIL mp_thread_mutex_init(&MP_STATE_VM(gil_mutex)); + MP_STATE_VM(thread_lock) = 0; #endif MP_THREAD_GIL_ENTER(); diff --git a/MicroPython_BUILD/components/micropython/py/vm.c b/MicroPython_BUILD/components/micropython/py/vm.c index ca9022e..bf6074d 100644 --- a/MicroPython_BUILD/components/micropython/py/vm.c +++ b/MicroPython_BUILD/components/micropython/py/vm.c @@ -1315,15 +1315,16 @@ pending_exception_check: #endif #if MICROPY_ENABLE_SCHEDULER // can only switch threads if the scheduler is unlocked - if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) + if ((MP_STATE_VM(sched_state) == MP_SCHED_IDLE) && !MP_STATE_VM(thread_lock)) #endif { mp_hal_reset_wdt();// LoBo + // *** Switch threads *** MP_THREAD_GIL_EXIT(); MP_THREAD_GIL_ENTER(); } } - #endif + #endif // MICROPY_PY_THREAD_GIL } // for loop diff --git a/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32.zip b/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32.zip index d68889b..a95b601 100644 Binary files a/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32.zip and b/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32.zip differ diff --git a/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_all.zip b/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_all.zip index 8f80385..1d1f8fa 100644 Binary files a/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_all.zip and b/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_all.zip differ diff --git a/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_ota.zip b/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_ota.zip index 1840b87..42c5565 100644 Binary files a/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_ota.zip and b/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_ota.zip differ diff --git a/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_psram.zip b/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_psram.zip index b52ce1e..64e8a68 100644 Binary files a/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_psram.zip and b/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_psram.zip differ diff --git a/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_psram_all.zip b/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_psram_all.zip index e2e50a6..029804a 100644 Binary files a/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_psram_all.zip and b/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_psram_all.zip differ diff --git a/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_psram_ota.zip b/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_psram_ota.zip index 0c0e22a..b75ed29 100644 Binary files a/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_psram_ota.zip and b/MicroPython_BUILD/firmware/MicroPython_LoBo_esp32_psram_ota.zip differ diff --git a/MicroPython_BUILD/firmware/esp32/MicroPython.bin b/MicroPython_BUILD/firmware/esp32/MicroPython.bin index 2298437..55802d2 100644 Binary files a/MicroPython_BUILD/firmware/esp32/MicroPython.bin and b/MicroPython_BUILD/firmware/esp32/MicroPython.bin differ diff --git a/MicroPython_BUILD/firmware/esp32/bootloader/bootloader.bin b/MicroPython_BUILD/firmware/esp32/bootloader/bootloader.bin index a5fb41d..c5a760a 100644 Binary files a/MicroPython_BUILD/firmware/esp32/bootloader/bootloader.bin and b/MicroPython_BUILD/firmware/esp32/bootloader/bootloader.bin differ diff --git a/MicroPython_BUILD/firmware/esp32/partitions_mpy.bin b/MicroPython_BUILD/firmware/esp32/partitions_mpy.bin index 81f7779..279eb6f 100644 Binary files a/MicroPython_BUILD/firmware/esp32/partitions_mpy.bin and b/MicroPython_BUILD/firmware/esp32/partitions_mpy.bin differ diff --git a/MicroPython_BUILD/firmware/esp32/partitions_mpy.csv b/MicroPython_BUILD/firmware/esp32/partitions_mpy.csv index f6cbf02..3259821 100644 --- a/MicroPython_BUILD/firmware/esp32/partitions_mpy.csv +++ b/MicroPython_BUILD/firmware/esp32/partitions_mpy.csv @@ -5,5 +5,5 @@ # ------------------------------------------------------- nvs, data, nvs, 0x9000, 24K, phy_init, data, phy, 0xf000, 4K, -MicroPython, app, factory, 0x10000, 1280K, +MicroPython, app, factory, 0x10000, 1344K, internalfs, data, spiffs, , 1024K, diff --git a/MicroPython_BUILD/firmware/esp32_all/MicroPython.bin b/MicroPython_BUILD/firmware/esp32_all/MicroPython.bin index bd5b08e..93ed540 100644 Binary files a/MicroPython_BUILD/firmware/esp32_all/MicroPython.bin and b/MicroPython_BUILD/firmware/esp32_all/MicroPython.bin differ diff --git a/MicroPython_BUILD/firmware/esp32_all/bootloader/bootloader.bin b/MicroPython_BUILD/firmware/esp32_all/bootloader/bootloader.bin index 05764ec..510ab13 100644 Binary files a/MicroPython_BUILD/firmware/esp32_all/bootloader/bootloader.bin and b/MicroPython_BUILD/firmware/esp32_all/bootloader/bootloader.bin differ diff --git a/MicroPython_BUILD/firmware/esp32_all/partitions_mpy.bin b/MicroPython_BUILD/firmware/esp32_all/partitions_mpy.bin index 0284ac6..a08bf12 100644 Binary files a/MicroPython_BUILD/firmware/esp32_all/partitions_mpy.bin and b/MicroPython_BUILD/firmware/esp32_all/partitions_mpy.bin differ diff --git a/MicroPython_BUILD/firmware/esp32_ota/MicroPython.bin b/MicroPython_BUILD/firmware/esp32_ota/MicroPython.bin index a081562..f11e173 100644 Binary files a/MicroPython_BUILD/firmware/esp32_ota/MicroPython.bin and b/MicroPython_BUILD/firmware/esp32_ota/MicroPython.bin differ diff --git a/MicroPython_BUILD/firmware/esp32_ota/bootloader/bootloader.bin b/MicroPython_BUILD/firmware/esp32_ota/bootloader/bootloader.bin index 0d72f8c..77bcb0f 100644 Binary files a/MicroPython_BUILD/firmware/esp32_ota/bootloader/bootloader.bin and b/MicroPython_BUILD/firmware/esp32_ota/bootloader/bootloader.bin differ diff --git a/MicroPython_BUILD/firmware/esp32_psram/MicroPython.bin b/MicroPython_BUILD/firmware/esp32_psram/MicroPython.bin index efccd5b..34ddf66 100644 Binary files a/MicroPython_BUILD/firmware/esp32_psram/MicroPython.bin and b/MicroPython_BUILD/firmware/esp32_psram/MicroPython.bin differ diff --git a/MicroPython_BUILD/firmware/esp32_psram/bootloader/bootloader.bin b/MicroPython_BUILD/firmware/esp32_psram/bootloader/bootloader.bin index ce6b6c2..b578d7c 100644 Binary files a/MicroPython_BUILD/firmware/esp32_psram/bootloader/bootloader.bin and b/MicroPython_BUILD/firmware/esp32_psram/bootloader/bootloader.bin differ diff --git a/MicroPython_BUILD/firmware/esp32_psram/partitions_mpy.bin b/MicroPython_BUILD/firmware/esp32_psram/partitions_mpy.bin index 72279ef..ab44025 100644 Binary files a/MicroPython_BUILD/firmware/esp32_psram/partitions_mpy.bin and b/MicroPython_BUILD/firmware/esp32_psram/partitions_mpy.bin differ diff --git a/MicroPython_BUILD/firmware/esp32_psram/partitions_mpy.csv b/MicroPython_BUILD/firmware/esp32_psram/partitions_mpy.csv index 4f13ffe..d27de0f 100644 --- a/MicroPython_BUILD/firmware/esp32_psram/partitions_mpy.csv +++ b/MicroPython_BUILD/firmware/esp32_psram/partitions_mpy.csv @@ -5,5 +5,5 @@ # ------------------------------------------------------- nvs, data, nvs, 0x9000, 24K, phy_init, data, phy, 0xf000, 4K, -MicroPython, app, factory, 0x10000, 1408K, +MicroPython, app, factory, 0x10000, 1472K, internalfs, data, spiffs, , 1024K, diff --git a/MicroPython_BUILD/firmware/esp32_psram_all/MicroPython.bin b/MicroPython_BUILD/firmware/esp32_psram_all/MicroPython.bin index 4a02338..d5dcc5a 100644 Binary files a/MicroPython_BUILD/firmware/esp32_psram_all/MicroPython.bin and b/MicroPython_BUILD/firmware/esp32_psram_all/MicroPython.bin differ diff --git a/MicroPython_BUILD/firmware/esp32_psram_all/bootloader/bootloader.bin b/MicroPython_BUILD/firmware/esp32_psram_all/bootloader/bootloader.bin index 3dcb47d..619c972 100644 Binary files a/MicroPython_BUILD/firmware/esp32_psram_all/bootloader/bootloader.bin and b/MicroPython_BUILD/firmware/esp32_psram_all/bootloader/bootloader.bin differ diff --git a/MicroPython_BUILD/firmware/esp32_psram_ota/MicroPython.bin b/MicroPython_BUILD/firmware/esp32_psram_ota/MicroPython.bin index 477b2f9..65a208f 100644 Binary files a/MicroPython_BUILD/firmware/esp32_psram_ota/MicroPython.bin and b/MicroPython_BUILD/firmware/esp32_psram_ota/MicroPython.bin differ diff --git a/MicroPython_BUILD/firmware/esp32_psram_ota/bootloader/bootloader.bin b/MicroPython_BUILD/firmware/esp32_psram_ota/bootloader/bootloader.bin index e34cb3a..0ece3a2 100644 Binary files a/MicroPython_BUILD/firmware/esp32_psram_ota/bootloader/bootloader.bin and b/MicroPython_BUILD/firmware/esp32_psram_ota/bootloader/bootloader.bin differ diff --git a/Tools/esp-idf.tar.xz b/Tools/esp-idf.tar.xz index c626505..2191104 100644 Binary files a/Tools/esp-idf.tar.xz and b/Tools/esp-idf.tar.xz differ