Updated mDNS module

can now be built with latest esp-idf mdns componnent
  some api changes, see the Wiki
Updated _thread module
  some bugfixes, new functions added
Updated esp-idf to the latest commit
This commit is contained in:
Boris Lovosevic
2018-01-27 15:28:49 +01:00
parent 7b97da2889
commit 6935d21745
37 changed files with 266 additions and 155 deletions
+1 -1
View File
@@ -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
+1 -1
View File
@@ -45,7 +45,7 @@
#=======================
TOOLS_VER=ver20180117.id
TOOLS_VER=ver20180127.id
#=======================
# -----------------------------
@@ -35,6 +35,7 @@
#include <stdint.h>
#include <string.h>
#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;
}
@@ -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;
}
}
@@ -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) {
@@ -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);
@@ -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"
@@ -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 = &params->map;
mp_map_elem_t *table = map->table;
if (map->used > 0) {
for (int i=0; i<map->used; 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; i<res; i++) {
const mdns_result_t * r = mdns_result_get(self->mdns, 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; i<res; i++) {
const mdns_result_t * r = mdns_result_get(self->mdns, 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; i<r->txt_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 },
@@ -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);
@@ -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, &not_val, tmo) == pdPASS) {
if (xTaskNotifyWait(0, 0, &not_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) },
@@ -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;
@@ -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();
@@ -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
Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More