8000 esp32: Add support for mDNS queries and responder. by dpgeorge · Pull Request #4951 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

esp32: Add support for mDNS queries and responder. #4951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ports/esp32/Makefile
< 10000 td class="blob-num blob-num-expandable" colspan="2"> Expand Down Expand Up
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ INC += -I$(BUILD)

INC_ESPCOMP += -I$(ESPCOMP)/bootloader_support/include
INC_ESPCOMP += -I$(ESPCOMP)/bootloader_support/include_bootloader
INC_ESPCOMP += -I$(ESPCOMP)/console
INC_ESPCOMP += -I$(ESPCOMP)/driver/include
INC_ESPCOMP += -I$(ESPCOMP)/driver/include/driver
INC_ESPCOMP += -I$(ESPCOMP)/nghttp/port/include
Expand Down Expand Up @@ -103,6 +104,8 @@ INC_ESPCOMP += -I$(ESPCOMP)/lwip/port/esp32/include
INC_ESPCOMP += -I$(ESPCOMP)/lwip/include/apps
INC_ESPCOMP += -I$(ESPCOMP)/mbedtls/mbedtls/include
INC_ESPCOMP += -I$(ESPCOMP)/mbedtls/port/include
INC_ESPCOMP += -I$(ESPCOMP)/mdns/include
INC_ESPCOMP += -I$(ESPCOMP)/mdns/private_include
INC_ESPCOMP += -I$(ESPCOMP)/spi_flash/include
INC_ESPCOMP += -I$(ESPCOMP)/ulp/include
INC_ESPCOMP += -I$(ESPCOMP)/vfs/include
@@ -346,6 +349,8 @@ ESPIDF_MBEDTLS_O = $(patsubst %.c,%.o,\
$(wildcard $(ESPCOMP)/mbedtls/port/*.c) \
)

ESPIDF_MDNS_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/mdns/*.c))

$(BUILD)/$(ESPCOMP)/wpa_supplicant/%.o: CFLAGS += -DEMBEDDED_SUPP -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_MSCHAPv2 -DEAP_TTLS -DEAP_TLS -DEAP_PEAP -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DALLOW_EVEN_MOD -D__ets__ -Wno-strict-aliasing
ESPIDF_WPA_SUPPLICANT_O = $(patsubst %.c,%.o,\
$(wildcard $(ESPCOMP)/wpa_supplicant/port/*.c) \
Expand Down Expand Up @@ -390,6 +395,7 @@ $(eval $(call gen_espidf_lib_rule,spi_flash,$(ESPIDF_SPI_FLASH_O)))
$(eval $(call gen_espidf_lib_rule,ulp,$(ESPIDF_ULP_O)))
$(eval $(call gen_espidf_lib_rule,lwip,$(ESPIDF_LWIP_O)))
$(eval $(call gen_espidf_lib_rule,mbedtls,$(ESPIDF_MBEDTLS_O)))
$(eval $(call gen_espidf_lib_rule,mdns,$(ESPIDF_MDNS_O)))
$(eval $(call gen_espidf_lib_rule,wpa_supplicant,$(ESPIDF_WPA_SUPPLICANT_O)))
$(eval $(call gen_espidf_lib_rule,sdmmc,$(ESPIDF_SDMMC_O)))

Expand Down
10 changes: 10 additions & 0 deletions ports/esp32/modnetwork.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "esp_event_loop.h"
#include "lwip/dns.h"
#include "tcpip_adapter.h"
#include "mdns.h"

#include "modnetwork.h"

Expand Down Expand Up @@ -128,6 +129,9 @@ static bool wifi_sta_connected = false;
// Store the current status. 0 means None here, safe to do so as first enum value is WIFI_REASON_UNSPECIFIED=1.
static uint8_t wifi_sta_disconn_reason = 0;

// Whether mDNS has been initialised or not
static bool mdns_initialised = false;

// This function is called by the system-event task and so runs in a different
// thread to the main MicroPython task. It must not raise any Python exceptions.
static esp_err_t event_handler(void *ctx, system_event_t *event) {
Expand All @@ -142,6 +146,12 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) {
ESP_LOGI("network", "GOT_IP");
wifi_sta_connected = true;
wifi_sta_disconn_reason = 0; // Success so clear error. (in case of new error will be replaced anyway)
if (!mdns_initialised) {
mdns_init();
mdns_hostname_set("esp32");
mdns_instance_name_set("esp32");
mdns_initialised = true;
}
break;
case SYSTEM_EVENT_STA_DISCONNECTED: {
// This is a workaround as ESP32 WiFi libs don't currently
Expand Down
54 changes: 53 additions & 1 deletion ports/esp32/modsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "py/mperrno.h"
#include "lib/netutils/netutils.h"
#include "tcpip_adapter.h"
#include "mdns.h"
#include "modnetwork.h"

#include "lwip/sockets.h"
Expand All @@ -56,6 +57,8 @@
#include "esp_log.h"

#define SOCKET_POLL_US (100000)
#define MDNS_QUERY_TIMEOUT_MS (5000)
#define MDNS_LOCAL_SUFFIX ".local"

typedef struct _socket_obj_t {
mp_obj_base_t base;
Expand Down Expand Up @@ -150,6 +153,55 @@ static inline void check_for_exceptions(void) {
mp_handle_pending();
}

// This function mimics lwip_getaddrinfo, with added support for mDNS queries
static int _socket_getaddrinfo3(const char *nodename, const char *servname,
const struct addrinfo *hints, struct addrinfo **res) {
int nodename_len = strlen(nodename);
const int local_len = sizeof(MDNS_LOCAL_SUFFIX) - 1;
if (nodename_len > local_len
&& strcasecmp(nodename + nodename_len - local_len, MDNS_LOCAL_SUFFIX) == 0) {
// mDNS query
char nodename_no_local[nodename_len - local_len + 1];
memcpy(nodename_no_local, nodename, nodename_len - local_len);
nodename_no_local[nodename_len - local_len] = '\0';

struct ip4_addr addr = {0};
esp_err_t err = mdns_query_a(nodename_no_local, MDNS_QUERY_TIMEOUT_MS, &addr);
if (err != ESP_OK) {
if (err == ESP_ERR_NOT_FOUND){
*res = NULL;
return 0;
}
*res = NULL;
return err;
}

struct addrinfo *ai = memp_malloc(MEMP_NETDB);
if (ai == NULL) {
*res = NULL;
return EAI_MEMORY;
}
memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));

struct sockaddr_in *sa = (struct sockaddr_in*)((uint8_t*)ai + sizeof(struct addrinfo));
inet_addr_from_ip4addr(&sa->sin_addr, &addr);
sa->sin_family = AF_INET;
sa->sin_len = sizeof(struct sockaddr_in);
sa->sin_port = lwip_htons((u16_t)atoi(servname));
ai->ai_family = AF_INET;
ai->ai_canonname = ((char*)sa + sizeof(struct sockaddr_storage));
memcpy(ai->ai_canonname, nodename, nodename_len + 1);
ai->ai_addrlen = sizeof(struct sockaddr_storage);
ai->ai_addr = (struct sockaddr*)sa;

*res = ai;
return 0;
} else {
// Normal query
return lwip_getaddrinfo(nodename, servname, hints, res);
}
}

static int _socket_getaddrinfo2(const mp_obj_t host, const mp_obj_t portx, struct addrinfo **resp) {
const struct addrinfo hints = {
.ai_family = AF_INET,
Expand All @@ -172,7 +224,7 @@ static int _socket_getaddrinfo2(const mp_obj_t host, const mp_obj_t portx, struc
}

MP_THREAD_GIL_EXIT();
int res = lwip_getaddrinfo(host_str, port_str, &hints, resp);
int res = _socket_getaddrinfo3(host_str, port_str, &hints, resp);
MP_THREAD_GIL_ENTER();

return res;
Expand Down
0