Skip to content

Commit a3281fe

Browse files
authored
LEA mDNS v2 (#7540)
* LEAmDNSv2
1 parent faf59f5 commit a3281fe

39 files changed

+12357
-1844
lines changed

cores/esp8266/IPAddress.h

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
struct ip_addr: ipv4_addr { };
3333
#endif // !LWIP_IPV6
3434

35+
// to display a netif id with printf:
36+
#define NETIFID_STR "%c%c%u"
37+
#define NETIFID_VAL(netif) \
38+
((netif)? (netif)->name[0]: '-'), \
39+
((netif)? (netif)->name[1]: '-'), \
40+
((netif)? netif_get_index(netif): 42)
41+
3542
// A class to make it easier to handle and pass around IP addresses
3643
// IPv6 update:
3744
// IPAddress is now a decorator class for lwIP's ip_addr_t

cores/esp8266/LwipIntf.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
#ifndef _LWIPINTF_H
3+
#define _LWIPINTF_H
4+
5+
#include <lwip/netif.h>
6+
7+
#include <functional>
8+
9+
class LwipIntf
10+
{
11+
public:
12+
13+
using CBType = std::function <void(netif*)>;
14+
15+
static bool stateUpCB (LwipIntf::CBType&& cb);
16+
17+
private:
18+
19+
LwipIntf () { } // private, cannot be directly allocated
20+
21+
protected:
22+
23+
static bool stateChangeSysCB (LwipIntf::CBType&& cb);
24+
};
25+
26+
#endif // _LWIPINTF_H

cores/esp8266/LwipIntfCB.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
#include <LwipIntf.h>
3+
#include <Schedule.h>
4+
#include <debug.h>
5+
6+
#define NETIF_STATUS_CB_SIZE 3
7+
8+
static int netifStatusChangeListLength = 0;
9+
LwipIntf::CBType netifStatusChangeList [NETIF_STATUS_CB_SIZE];
10+
11+
extern "C" void netif_status_changed (struct netif* netif)
12+
{
13+
// override the default empty weak function
14+
for (int i = 0; i < netifStatusChangeListLength; i++)
15+
netifStatusChangeList[i](netif);
16+
}
17+
18+
bool LwipIntf::stateChangeSysCB (LwipIntf::CBType&& cb)
19+
{
20+
if (netifStatusChangeListLength >= NETIF_STATUS_CB_SIZE)
21+
{
22+
#if defined(DEBUG_ESP_CORE)
23+
DEBUGV("NETIF_STATUS_CB_SIZE is too low\n");
24+
#endif
25+
return false;
26+
}
27+
28+
netifStatusChangeList[netifStatusChangeListLength++] = cb;
29+
return true;
30+
}
31+
32+
bool LwipIntf::stateUpCB (LwipIntf::CBType&& cb)
33+
{
34+
return stateChangeSysCB([cb](netif* nif)
35+
{
36+
if (netif_is_up(nif))
37+
schedule_function([cb, nif]()
38+
{
39+
cb(nif);
40+
});
41+
});
42+
}

cores/esp8266/core_esp8266_noniso.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,36 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
117117
return s;
118118
}
119119

120+
/*
121+
strrstr (static)
122+
123+
Backwards search for p_pcPattern in p_pcString
124+
Based on: https://stackoverflow.com/a/1634398/2778898
125+
126+
*/
127+
const char* strrstr(const char*__restrict p_pcString,
128+
const char*__restrict p_pcPattern)
129+
{
130+
const char* pcResult = 0;
131+
132+
size_t stStringLength = (p_pcString ? strlen(p_pcString) : 0);
133+
size_t stPatternLength = (p_pcPattern ? strlen(p_pcPattern) : 0);
134+
135+
if ((stStringLength) &&
136+
(stPatternLength) &&
137+
(stPatternLength <= stStringLength))
138+
{
139+
// Pattern is shorter or has the same length than the string
140+
for (const char* s = (p_pcString + stStringLength - stPatternLength); s >= p_pcString; --s)
141+
{
142+
if (0 == strncmp(s, p_pcPattern, stPatternLength))
143+
{
144+
pcResult = s;
145+
break;
146+
}
147+
}
148+
}
149+
return pcResult;
150+
}
151+
120152
};

cores/esp8266/stdlib_noniso.h

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ char* dtostrf (double val, signed char width, unsigned char prec, char *s);
4444

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

47+
const char* strrstr(const char*__restrict p_pcString,
48+
const char*__restrict p_pcPattern);
49+
4750
#ifdef __cplusplus
4851
} // extern "C"
4952
#endif

libraries/ESP8266WiFi/src/include/UdpContext.h

+44-24
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void esp_schedule();
3030
}
3131

3232
#include <AddrList.h>
33+
#include <PolledTimeout.h>
3334

3435
#define PBUF_ALIGNER_ADJUST 4
3536
#define PBUF_ALIGNER(x) ((void*)((((intptr_t)(x))+3)&~3))
@@ -390,14 +391,39 @@ class UdpContext
390391
return size;
391392
}
392393

394+
void cancelBuffer ()
395+
{
396+
if (_tx_buf_head)
397+
pbuf_free(_tx_buf_head);
398+
_tx_buf_head = 0;
399+
_tx_buf_cur = 0;
400+
_tx_buf_offset = 0;
401+
}
402+
393403
bool send(const ip_addr_t* addr = 0, uint16_t port = 0)
404+
{
405+
return trySend(addr, port, /* don't keep buffer */false) == ERR_OK;
406+
}
407+
408+
bool sendTimeout(const ip_addr_t* addr, uint16_t port,
409+
esp8266::polledTimeout::oneShotFastMs::timeType timeoutMs)
410+
{
411+
err_t err;
412+
esp8266::polledTimeout::oneShotFastMs timeout(timeoutMs);
413+
while (((err = trySend(addr, port, /* keep buffer on error */true)) != ERR_OK) && !timeout)
414+
delay(0);
415+
if (err != ERR_OK)
416+
cancelBuffer(); // get rid of buffer kept on error after timeout
417+
return err == ERR_OK;
418+
}
419+
420+
private:
421+
422+
err_t trySend(const ip_addr_t* addr, uint16_t port, bool keepBufferOnError)
394423
{
395424
size_t data_size = _tx_buf_offset;
396425
pbuf* tx_copy = pbuf_alloc(PBUF_TRANSPORT, data_size, PBUF_RAM);
397-
if(!tx_copy){
398-
DEBUGV("failed pbuf_alloc");
399-
}
400-
else{
426+
if (tx_copy) {
401427
uint8_t* dst = reinterpret_cast<uint8_t*>(tx_copy->payload);
402428
for (pbuf* p = _tx_buf_head; p; p = p->next) {
403429
size_t will_copy = (data_size < p->len) ? data_size : p->len;
@@ -406,38 +432,32 @@ class UdpContext
406432
data_size -= will_copy;
407433
}
408434
}
409-
if (_tx_buf_head)
410-
pbuf_free(_tx_buf_head);
411-
_tx_buf_head = 0;
412-
_tx_buf_cur = 0;
413-
_tx_buf_offset = 0;
414-
if(!tx_copy){
415-
return false;
416-
}
417435

436+
if (!keepBufferOnError)
437+
cancelBuffer();
438+
439+
if (!tx_copy){
440+
DEBUGV("failed pbuf_alloc");
441+
return ERR_MEM;
442+
}
418443

419444
if (!addr) {
420445
addr = &_pcb->remote_ip;
421446
port = _pcb->remote_port;
422447
}
423-
#ifdef LWIP_MAYBE_XCC
424-
uint16_t old_ttl = _pcb->ttl;
425-
if (ip_addr_ismulticast(addr)) {
426-
_pcb->ttl = _mcast_ttl;
427-
}
428-
#endif
448+
429449
err_t err = udp_sendto(_pcb, tx_copy, addr, port);
430450
if (err != ERR_OK) {
431451
DEBUGV(":ust rc=%d\r\n", (int) err);
432452
}
433-
#ifdef LWIP_MAYBE_XCC
434-
_pcb->ttl = old_ttl;
435-
#endif
453+
436454
pbuf_free(tx_copy);
437-
return err == ERR_OK;
438-
}
439455

440-
private:
456+
if (err == ERR_OK)
457+
cancelBuffer(); // no error: get rid of buffer
458+
459+
return err;
460+
}
441461

442462
size_t _processSize (const pbuf* pb)
443463
{

libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_Clock/mDNS_Clock.ino

+2-14
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,9 @@
3636
#include <WiFiClient.h>
3737
#include <ESP8266WebServer.h>
3838
#include <time.h>
39-
40-
/*
41-
Include the MDNSResponder (the library needs to be included also)
42-
As LEA MDNSResponder is experimantal in the ESP8266 environment currently, the
43-
legacy MDNSResponder is defaulted in th include file.
44-
There are two ways to access LEA MDNSResponder:
45-
1. Prepend every declaration and call to global declarations or functions with the namespace, like:
46-
'LEAmDNS::MDNSResponder::hMDNSService hMDNSService;'
47-
This way is used in the example. But be careful, if the namespace declaration is missing
48-
somewhere, the call might go to the legacy implementation...
49-
2. Open 'ESP8266mDNS.h' and set LEAmDNS to default.
50-
51-
*/
52-
#include <ESP8266mDNS.h>
5339
#include <PolledTimeout.h>
40+
#include <ESP8266mDNS.h>
41+
5442
/*
5543
Global defines and vars
5644
*/

0 commit comments

Comments
 (0)