Skip to content

Commit

Permalink
LEA mDNS v2 (#7540)
Browse files Browse the repository at this point in the history
* LEAmDNSv2
  • Loading branch information
Labor-Et-Ars authored Sep 25, 2020
1 parent faf59f5 commit a3281fe
Show file tree
Hide file tree
Showing 39 changed files with 12,357 additions and 1,844 deletions.
7 changes: 7 additions & 0 deletions cores/esp8266/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
struct ip_addr: ipv4_addr { };
#endif // !LWIP_IPV6

// to display a netif id with printf:
#define NETIFID_STR "%c%c%u"
#define NETIFID_VAL(netif) \
((netif)? (netif)->name[0]: '-'), \
((netif)? (netif)->name[1]: '-'), \
((netif)? netif_get_index(netif): 42)

// A class to make it easier to handle and pass around IP addresses
// IPv6 update:
// IPAddress is now a decorator class for lwIP's ip_addr_t
Expand Down
26 changes: 26 additions & 0 deletions cores/esp8266/LwipIntf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

#ifndef _LWIPINTF_H
#define _LWIPINTF_H

#include <lwip/netif.h>

#include <functional>

class LwipIntf
{
public:

using CBType = std::function <void(netif*)>;

static bool stateUpCB (LwipIntf::CBType&& cb);

private:

LwipIntf () { } // private, cannot be directly allocated

protected:

static bool stateChangeSysCB (LwipIntf::CBType&& cb);
};

#endif // _LWIPINTF_H
42 changes: 42 additions & 0 deletions cores/esp8266/LwipIntfCB.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#include <LwipIntf.h>
#include <Schedule.h>
#include <debug.h>

#define NETIF_STATUS_CB_SIZE 3

static int netifStatusChangeListLength = 0;
LwipIntf::CBType netifStatusChangeList [NETIF_STATUS_CB_SIZE];

extern "C" void netif_status_changed (struct netif* netif)
{
// override the default empty weak function
for (int i = 0; i < netifStatusChangeListLength; i++)
netifStatusChangeList[i](netif);
}

bool LwipIntf::stateChangeSysCB (LwipIntf::CBType&& cb)
{
if (netifStatusChangeListLength >= NETIF_STATUS_CB_SIZE)
{
#if defined(DEBUG_ESP_CORE)
DEBUGV("NETIF_STATUS_CB_SIZE is too low\n");
#endif
return false;
}

netifStatusChangeList[netifStatusChangeListLength++] = cb;
return true;
}

bool LwipIntf::stateUpCB (LwipIntf::CBType&& cb)
{
return stateChangeSysCB([cb](netif* nif)
{
if (netif_is_up(nif))
schedule_function([cb, nif]()
{
cb(nif);
});
});
}
32 changes: 32 additions & 0 deletions cores/esp8266/core_esp8266_noniso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,36 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
return s;
}

/*
strrstr (static)
Backwards search for p_pcPattern in p_pcString
Based on: https://stackoverflow.com/a/1634398/2778898
*/
const char* strrstr(const char*__restrict p_pcString,
const char*__restrict p_pcPattern)
{
const char* pcResult = 0;

size_t stStringLength = (p_pcString ? strlen(p_pcString) : 0);
size_t stPatternLength = (p_pcPattern ? strlen(p_pcPattern) : 0);

if ((stStringLength) &&
(stPatternLength) &&
(stPatternLength <= stStringLength))
{
// Pattern is shorter or has the same length than the string
for (const char* s = (p_pcString + stStringLength - stPatternLength); s >= p_pcString; --s)
{
if (0 == strncmp(s, p_pcPattern, stPatternLength))
{
pcResult = s;
break;
}
}
}
return pcResult;
}

};
3 changes: 3 additions & 0 deletions cores/esp8266/stdlib_noniso.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ char* dtostrf (double val, signed char width, unsigned char prec, char *s);

void reverse(char* begin, char* end);

const char* strrstr(const char*__restrict p_pcString,
const char*__restrict p_pcPattern);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
68 changes: 44 additions & 24 deletions libraries/ESP8266WiFi/src/include/UdpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void esp_schedule();
}

#include <AddrList.h>
#include <PolledTimeout.h>

#define PBUF_ALIGNER_ADJUST 4
#define PBUF_ALIGNER(x) ((void*)((((intptr_t)(x))+3)&~3))
Expand Down Expand Up @@ -390,14 +391,39 @@ class UdpContext
return size;
}

void cancelBuffer ()
{
if (_tx_buf_head)
pbuf_free(_tx_buf_head);
_tx_buf_head = 0;
_tx_buf_cur = 0;
_tx_buf_offset = 0;
}

bool send(const ip_addr_t* addr = 0, uint16_t port = 0)
{
return trySend(addr, port, /* don't keep buffer */false) == ERR_OK;
}

bool sendTimeout(const ip_addr_t* addr, uint16_t port,
esp8266::polledTimeout::oneShotFastMs::timeType timeoutMs)
{
err_t err;
esp8266::polledTimeout::oneShotFastMs timeout(timeoutMs);
while (((err = trySend(addr, port, /* keep buffer on error */true)) != ERR_OK) && !timeout)
delay(0);
if (err != ERR_OK)
cancelBuffer(); // get rid of buffer kept on error after timeout
return err == ERR_OK;
}

private:

err_t trySend(const ip_addr_t* addr, uint16_t port, bool keepBufferOnError)
{
size_t data_size = _tx_buf_offset;
pbuf* tx_copy = pbuf_alloc(PBUF_TRANSPORT, data_size, PBUF_RAM);
if(!tx_copy){
DEBUGV("failed pbuf_alloc");
}
else{
if (tx_copy) {
uint8_t* dst = reinterpret_cast<uint8_t*>(tx_copy->payload);
for (pbuf* p = _tx_buf_head; p; p = p->next) {
size_t will_copy = (data_size < p->len) ? data_size : p->len;
Expand All @@ -406,38 +432,32 @@ class UdpContext
data_size -= will_copy;
}
}
if (_tx_buf_head)
pbuf_free(_tx_buf_head);
_tx_buf_head = 0;
_tx_buf_cur = 0;
_tx_buf_offset = 0;
if(!tx_copy){
return false;
}

if (!keepBufferOnError)
cancelBuffer();

if (!tx_copy){
DEBUGV("failed pbuf_alloc");
return ERR_MEM;
}

if (!addr) {
addr = &_pcb->remote_ip;
port = _pcb->remote_port;
}
#ifdef LWIP_MAYBE_XCC
uint16_t old_ttl = _pcb->ttl;
if (ip_addr_ismulticast(addr)) {
_pcb->ttl = _mcast_ttl;
}
#endif

err_t err = udp_sendto(_pcb, tx_copy, addr, port);
if (err != ERR_OK) {
DEBUGV(":ust rc=%d\r\n", (int) err);
}
#ifdef LWIP_MAYBE_XCC
_pcb->ttl = old_ttl;
#endif

pbuf_free(tx_copy);
return err == ERR_OK;
}

private:
if (err == ERR_OK)
cancelBuffer(); // no error: get rid of buffer

return err;
}

size_t _processSize (const pbuf* pb)
{
Expand Down
16 changes: 2 additions & 14 deletions libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_Clock/mDNS_Clock.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,9 @@
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <time.h>

/*
Include the MDNSResponder (the library needs to be included also)
As LEA MDNSResponder is experimantal in the ESP8266 environment currently, the
legacy MDNSResponder is defaulted in th include file.
There are two ways to access LEA MDNSResponder:
1. Prepend every declaration and call to global declarations or functions with the namespace, like:
'LEAmDNS::MDNSResponder::hMDNSService hMDNSService;'
This way is used in the example. But be careful, if the namespace declaration is missing
somewhere, the call might go to the legacy implementation...
2. Open 'ESP8266mDNS.h' and set LEAmDNS to default.
*/
#include <ESP8266mDNS.h>
#include <PolledTimeout.h>
#include <ESP8266mDNS.h>

/*
Global defines and vars
*/
Expand Down
Loading

0 comments on commit a3281fe

Please sign in to comment.