Skip to content

Commit a5139ba

Browse files
author
ficeto
committed
Merge pull request #17 from esp8266/esp8266
pull latest changes
2 parents fdbd40d + 1e94865 commit a5139ba

File tree

16 files changed

+728
-387
lines changed

16 files changed

+728
-387
lines changed

hardware/esp8266com/esp8266/cores/esp8266/libc_replacements.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ char* ICACHE_FLASH_ATTR strtok_r(char * str, const char * delimiters, char ** te
175175
uint32_t size = 0;
176176

177177
if(str == NULL) {
178+
if(temp == NULL) {
179+
return NULL;
180+
}
178181
start = *temp;
179182
} else {
180183
start = str;
@@ -184,6 +187,10 @@ char* ICACHE_FLASH_ATTR strtok_r(char * str, const char * delimiters, char ** te
184187
return NULL;
185188
}
186189

190+
if(delimiters == NULL) {
191+
return NULL;
192+
}
193+
187194
end = start;
188195

189196
while(1) {
@@ -211,7 +218,9 @@ char* ICACHE_FLASH_ATTR strtok_r(char * str, const char * delimiters, char ** te
211218
}
212219

213220
char* ICACHE_FLASH_ATTR strtok(char * str, const char * delimiters) {
214-
return strtok_r(str, delimiters, NULL);
221+
static char * ret = NULL;
222+
ret = strtok_r(str, delimiters, &ret);
223+
return ret;
215224
}
216225

217226
int strcasecmp(const char * str1, const char * str2) {

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino

-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,5 @@ void setup(void){
6969
}
7070

7171
void loop(void){
72-
mdns.update();
7372
server.handleClient();
7473
}

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino

-1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,5 @@ void setup(void){
308308
}
309309

310310
void loop(void){
311-
//mdns.update();
312311
server.handleClient();
313312
}

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/WiFiUdp.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
*/
2222

2323
#define LWIP_INTERNAL
24-
24+
#include <functional>
25+
2526
extern "C"
2627
{
2728
#include "include/wl_definitions.h"
@@ -168,7 +169,6 @@ int WiFiUDP::endPacket()
168169
return 0;
169170

170171
_ctx->send();
171-
_ctx->disconnect();
172172
return 1;
173173
}
174174

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/include/UdpContext.h

+46-13
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ extern "C" void esp_schedule();
2828

2929
#define GET_IP_HDR(pb) reinterpret_cast<ip_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN - IP_HLEN);
3030
#define GET_UDP_HDR(pb) reinterpret_cast<udp_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN);
31+
3132
class UdpContext
3233
{
3334
public:
35+
36+
typedef std::function<void(void)> rxhandler_t;
37+
3438
UdpContext()
3539
: _pcb(0)
3640
, _rx_buf(0)
@@ -40,8 +44,11 @@ class UdpContext
4044
, _tx_buf_head(0)
4145
, _tx_buf_cur(0)
4246
, _tx_buf_offset(0)
47+
, _multicast_ttl(1)
48+
, _dest_port(0)
4349
{
4450
_pcb = udp_new();
51+
_dest_addr.addr = 0;
4552
}
4653

4754
~UdpContext()
@@ -79,8 +86,9 @@ class UdpContext
7986

8087
bool connect(ip_addr_t addr, uint16_t port)
8188
{
82-
err_t err = udp_connect(_pcb, &addr, port);
83-
return err == ERR_OK;
89+
_dest_addr = addr;
90+
_dest_port = port;
91+
return true;
8492
}
8593

8694
bool listen(ip_addr_t addr, uint16_t port)
@@ -107,7 +115,13 @@ class UdpContext
107115
// newer versions of lwip have an additional field (mcast_ttl) for this purpose
108116
// and a macro to set it instead of direct field access
109117
// udp_set_multicast_ttl(_pcb, ttl);
110-
_pcb->ttl = ttl;
118+
_multicast_ttl = ttl;
119+
}
120+
121+
// warning: handler is called from tcp stack context
122+
// esp_yield and non-reentrant functions which depend on it will fail
123+
void onRx(rxhandler_t handler) {
124+
_on_rx = handler;
111125
}
112126

113127
size_t getSize() const
@@ -173,10 +187,10 @@ class UdpContext
173187
return _rx_buf != 0;
174188
}
175189

176-
char read()
190+
int read()
177191
{
178192
if (!_rx_buf || _rx_buf->len == _rx_buf_offset)
179-
return 0;
193+
return -1;
180194

181195
char c = reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset];
182196
_consume(1);
@@ -190,7 +204,7 @@ class UdpContext
190204

191205
size_t max_size = _rx_buf->len - _rx_buf_offset;
192206
size = (size < max_size) ? size : max_size;
193-
DEBUGV(":rd %d, %d, %d\r\n", size, _rx_buf->len, _rx_buf_offset);
207+
DEBUGV(":urd %d, %d, %d\r\n", size, _rx_buf->len, _rx_buf_offset);
194208

195209
os_memcpy(dst, reinterpret_cast<char*>(_rx_buf->payload) + _rx_buf_offset, size);
196210
_consume(size);
@@ -257,10 +271,19 @@ class UdpContext
257271
}
258272
}
259273

260-
if (addr)
261-
udp_sendto(_pcb, _tx_buf_head, addr, port);
262-
else
263-
udp_send(_pcb, _tx_buf_head);
274+
if (!addr) {
275+
addr = &_dest_addr;
276+
port = _dest_port;
277+
}
278+
279+
uint16_t old_ttl = _pcb->ttl;
280+
if (ip_addr_ismulticast(addr)) {
281+
_pcb->ttl = _multicast_ttl;
282+
}
283+
284+
udp_sendto(_pcb, _tx_buf_head, addr, port);
285+
286+
_pcb->ttl = old_ttl;
264287

265288
for (pbuf* p = _tx_buf_head; p; p = p->next)
266289
{
@@ -281,7 +304,7 @@ class UdpContext
281304

282305
void _reserve(size_t size)
283306
{
284-
const size_t pbuf_unit_size = 1024;
307+
const size_t pbuf_unit_size = 512;
285308
if (!_tx_buf_head)
286309
{
287310
_tx_buf_head = pbuf_alloc(PBUF_TRANSPORT, pbuf_unit_size, PBUF_RAM);
@@ -317,16 +340,19 @@ class UdpContext
317340
{
318341
// there is some unread data
319342
// chain the new pbuf to the existing one
320-
DEBUGV(":rch %d, %d\r\n", _rx_buf->tot_len, pb->tot_len);
343+
DEBUGV(":urch %d, %d\r\n", _rx_buf->tot_len, pb->tot_len);
321344
pbuf_cat(_rx_buf, pb);
322345
}
323346
else
324347
{
325-
DEBUGV(":rn %d\r\n", pb->tot_len);
348+
DEBUGV(":urn %d\r\n", pb->tot_len);
326349
_first_buf_taken = false;
327350
_rx_buf = pb;
328351
_rx_buf_offset = 0;
329352
}
353+
if (_on_rx) {
354+
_on_rx();
355+
}
330356
}
331357

332358

@@ -341,13 +367,20 @@ class UdpContext
341367
int _refcnt;
342368
udp_pcb* _pcb;
343369

370+
ip_addr_t _dest_addr;
371+
uint16_t _dest_port;
372+
373+
uint16_t _multicast_ttl;
374+
344375
bool _first_buf_taken;
345376
pbuf* _rx_buf;
346377
size_t _rx_buf_offset;
347378

348379
pbuf* _tx_buf_head;
349380
pbuf* _tx_buf_cur;
350381
size_t _tx_buf_offset;
382+
383+
rxhandler_t _on_rx;
351384
};
352385

353386

hardware/esp8266com/esp8266/libraries/ESP8266mDNS/ESP8266mDNS.cpp

+34-10
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ License (MIT license):
4242
#define TTL_OFFSET 4
4343
#define IP_OFFSET 10
4444

45+
static const IPAddress MDNS_MULTICAST_ADDR(224, 0, 0, 251);
46+
static const int MDNS_MULTICAST_TTL = 1;
47+
static const int MDNS_PORT = 5353;
48+
4549

4650
MDNSResponder::MDNSResponder()
4751
: _expected(NULL)
4852
, _expectedLen(0)
4953
, _response(NULL)
5054
, _responseLen(0)
5155
, _index(0)
56+
, _conn(0)
5257
{ }
5358

5459
MDNSResponder::~MDNSResponder() {
@@ -149,21 +154,37 @@ bool MDNSResponder::begin(const char* domain, IPAddress addr, uint32_t ttlSecond
149154
records[IP_OFFSET + 0] = (uint8_t) ipAddress;
150155

151156
// Open the MDNS socket if it isn't already open.
152-
if (!_mdnsConn) {
153-
if (!_mdnsConn.beginMulticast(addr, IPAddress(224, 0, 0, 251), 5353)) {
157+
if (!_conn) {
158+
ip_addr_t ifaddr;
159+
ifaddr.addr = (uint32_t) addr;
160+
ip_addr_t multicast_addr;
161+
multicast_addr.addr = (uint32_t) MDNS_MULTICAST_ADDR;
162+
163+
if (igmp_joingroup(&ifaddr, &multicast_addr)!= ERR_OK) {
154164
return false;
155165
}
156-
}
157166

167+
_conn = new UdpContext;
168+
_conn->ref();
169+
170+
if (!_conn->listen(*IP_ADDR_ANY, MDNS_PORT)) {
171+
return false;
172+
}
173+
_conn->setMulticastInterface(ifaddr);
174+
_conn->setMulticastTTL(MDNS_MULTICAST_TTL);
175+
_conn->onRx(std::bind(&MDNSResponder::update, this));
176+
_conn->connect(multicast_addr, MDNS_PORT);
177+
}
158178
return true;
159179
}
160180

161181
void MDNSResponder::update() {
162-
if (!_mdnsConn.parsePacket())
163-
return;
182+
if (!_conn->next()) {
183+
return;
184+
}
164185

165186
// Read available data.
166-
int n = _mdnsConn.available();
187+
int n = _conn->getSize();
167188

168189
_index = 0;
169190

@@ -172,7 +193,7 @@ void MDNSResponder::update() {
172193
#endif
173194
// Look for domain name in request and respond with canned response if found.
174195
for (int i = 0; i < n; ++i) {
175-
uint8_t ch = tolower(_mdnsConn.read());
196+
uint8_t ch = tolower(_conn->read());
176197

177198
#ifdef MDNS_DEBUG
178199
String str(ch, 16);
@@ -191,9 +212,12 @@ void MDNSResponder::update() {
191212
Serial.print("responding, i=");
192213
Serial.println(i);
193214
#endif
194-
_mdnsConn.beginPacketMulticast(IPAddress(224, 0, 0, 251), 5353, _localAddr);
195-
_mdnsConn.write(_response, _responseLen);
196-
_mdnsConn.endPacket();
215+
ip_addr_t multicast_addr;
216+
multicast_addr.addr = (uint32_t) MDNS_MULTICAST_ADDR;
217+
218+
_conn->append(reinterpret_cast<const char*>(_response), _responseLen);
219+
_conn->send();
220+
197221
_index = 0;
198222
}
199223
}

hardware/esp8266com/esp8266/libraries/ESP8266mDNS/ESP8266mDNS.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ License (MIT license):
4646
#include "ESP8266WiFi.h"
4747
#include "WiFiUdp.h"
4848

49+
50+
class UdpContext;
51+
4952
class MDNSResponder {
5053
public:
5154
MDNSResponder();
@@ -63,7 +66,7 @@ class MDNSResponder {
6366
uint8_t* _response;
6467
int _responseLen;
6568
// Socket for MDNS communication
66-
WiFiUDP _mdnsConn;
69+
UdpContext* _conn;
6770
// local IP Address
6871
IPAddress _localAddr;
6972
};

hardware/esp8266com/esp8266/libraries/ESP8266mDNS/examples/mDNS_Web_Server/mDNS_Web_Server.ino

-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ void setup(void)
6868

6969
void loop(void)
7070
{
71-
// Check for any mDNS queries and send responses
72-
mdns.update();
73-
7471
// Check if a client has connected
7572
WiFiClient client = server.available();
7673
if (!client) {

0 commit comments

Comments
 (0)