Skip to content

Commit c29a35d

Browse files
committed
WIP mdns
1 parent 3897ebe commit c29a35d

File tree

10 files changed

+97
-14
lines changed

10 files changed

+97
-14
lines changed

cores/esp8266/spiffs_api.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ class SPIFFSImpl : public FSImpl
221221

222222
if (!_workBuf) {
223223
DEBUGV("SPIFFSImpl: allocating %d+%d+%d=%d bytes\r\n",
224-
workBufSize, fdsBufSize, cacheBufSize,
225-
workBufSize + fdsBufSize + cacheBufSize);
224+
(int)workBufSize, (int)fdsBufSize, (int)cacheBufSize,
225+
(int)(workBufSize + fdsBufSize + cacheBufSize));
226226
_workBuf.reset(new uint8_t[workBufSize]);
227227
_fdsBuf.reset(new uint8_t[fdsBufSize]);
228228
_cacheBuf.reset(new uint8_t[cacheBufSize]);

libraries/ESP8266WiFi/src/WiFiUdp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,15 @@ uint16_t WiFiUDP::localPort()
286286
void WiFiUDP::stopAll()
287287
{
288288
for (WiFiUDP* it = _s_first; it; it = it->_next) {
289-
DEBUGV("%s %08x %08x\n", __func__, (uint32_t) it, (uint32_t) _s_first);
289+
DEBUGV("%s %p %p\n", __func__, it, _s_first);
290290
it->stop();
291291
}
292292
}
293293

294294
void WiFiUDP::stopAllExcept(WiFiUDP * exC) {
295295
for (WiFiUDP* it = _s_first; it; it = it->_next) {
296296
if (it->_ctx != exC->_ctx) {
297-
DEBUGV("%s %08x %08x\n", __func__, (uint32_t) it, (uint32_t) _s_first);
297+
DEBUGV("%s %p %p\n", __func__, it, _s_first);
298298
it->stop();
299299
}
300300
}

tests/host/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ PREINCLUDES := \
8080
-include common/mock.h \
8181
-include common/c_types.h \
8282

83-
CXXFLAGS += -std=c++11 -Wall -Werror -coverage -O0 -fno-common -g
83+
DEBUG += -DDEBUG_ESP_PORT=Serial
84+
DEBUG += -DDEBUG_ESP_SSL -DDEBUG_ESP_TLS_MEM -DDEBUG_ESP_HTTP_CLIENT -DDEBUG_ESP_HTTP_SERVER -DDEBUG_ESP_CORE -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP_UPDATE -DDEBUG_ESP_UPDATER -DDEBUG_ESP_OTA -DDEBUG_ESP_MDNS
85+
86+
CXXFLAGS += $(DEBUG) -std=c++11 -Wall -Werror -coverage -O0 -fno-common -g
8487
CFLAGS += -std=c99 -Wall -Werror -coverage -O0 -fno-common -g
8588
LDFLAGS += -coverage -O0 -g
8689
VALGRINDFLAGS += --leak-check=full --track-origins=yes --error-limit=no --show-leak-kinds=all --error-exitcode=999
@@ -172,9 +175,9 @@ MOCK_ARDUINO_LIBS := \
172175
Parsing.cpp \
173176
detail/mimetable.cpp \
174177
) \
178+
ESP8266mDNS/ESP8266mDNS.cpp \
175179
)
176180

177-
# ESP8266mDNS/ESP8266mDNS.cpp \
178181

179182

180183
MOCK_ARDUINO_LIBS += \

tests/host/common/ArduinoMain.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
11

22
#include <Arduino.h>
33

4+
#include <functional>
5+
#include "lwip/opt.h"
6+
#include "lwip/udp.h"
7+
#include "lwip/inet.h"
8+
#include "lwip/igmp.h"
9+
#include "lwip/mem.h"
10+
#include <include/UdpContext.h>
11+
#include <poll.h>
12+
413
#include <unistd.h> // usleep
14+
#include <map>
15+
16+
std::map<int,UdpContext*> udps;
17+
18+
void register_udp (int sock, UdpContext* udp)
19+
{
20+
if (udp)
21+
udps[sock] = udp;
22+
else
23+
udps.erase(sock);
24+
}
25+
526

627
int main (void)
728
{
829
setup();
930
while (true)
1031
{
1132
usleep(10000); // not 100% cpu
33+
1234
loop();
35+
36+
// check incoming udp
37+
for (auto& udp: udps)
38+
{
39+
pollfd p;
40+
p.fd = udp.first;
41+
p.events = POLLIN;
42+
if (poll(&p, 1, 0) && p.revents == POLLIN)
43+
{
44+
fprintf(stderr, MOCK "UDP poll(%d) -> cb\n", (int)p.fd);
45+
udp.second->mock_cb();
46+
}
47+
}
48+
1349
}
1450
return 0;
1551
}

tests/host/common/MockTools.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
#include <arpa/inet.h>
3+
#include <stdarg.h>
34

45
extern "C"
56
{
@@ -12,4 +13,13 @@ uint16_t lwip_ntohs (uint16_t netshort) { return ntohs(netshort); }
1213
char* ets_strcpy (char* d, const char* s) { return strcpy(d, s); }
1314
size_t ets_strlen (const char* s) { return strlen(s); }
1415

16+
int ets_printf (const char* fmt, ...)
17+
{
18+
va_list ap;
19+
va_start(ap, fmt);
20+
int len = vprintf(fmt, ap);
21+
va_end(ap);
22+
return len;
23+
}
24+
1525
};

tests/host/common/MockUDPSocket.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bool mockUDPListen (int sock, uint32_t dstaddr, uint16_t port, uint32_t mcast)
2626

2727
// Filling server information
2828
servaddr.sin_family = AF_INET;
29-
servaddr.sin_addr.s_addr = htonl(dstaddr);
29+
servaddr.sin_addr.s_addr = dstaddr;
3030
servaddr.sin_port = htons(port);
3131

3232
// Bind the socket with the server address
@@ -42,8 +42,8 @@ bool mockUDPListen (int sock, uint32_t dstaddr, uint16_t port, uint32_t mcast)
4242
{
4343
// https://web.cs.wpi.edu/~claypool/courses/4514-B99/samples/multicast.c
4444
struct ip_mreq mreq;
45-
mreq.imr_multiaddr.s_addr = htonl(mcast);
46-
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
45+
mreq.imr_multiaddr.s_addr = mcast;
46+
mreq.imr_interface.s_addr = 0;//htonl(INADDR_ANY);
4747
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
4848
{
4949
fprintf(stderr, MOCK "can't join multicast group addr %08x\n", (int)mcast);

tests/host/common/MockWiFiServer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include <include/ClientContext.h>
99

10+
extern "C" const ip_addr_t ip_addr_any = IPADDR4_INIT(IPADDR_ANY);
11+
1012
#define int2pcb(x) ((tcp_pcb*)(long)(x))
1113
#define pcb2int(x) ((int)(long)(x))
1214

tests/host/common/include/UdpContext.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,25 @@ class UdpContext
6262

6363
bool listen(ip_addr_t addr, uint16_t port)
6464
{
65-
return mockUDPListen(_sock, addr.addr, port, staticMCastAddr);
65+
bool ret = mockUDPListen(_sock, addr.addr, port, staticMCastAddr);
66+
register_udp(_sock, this);
67+
return ret;
6668
}
6769

6870
void disconnect()
6971
{
7072
if (_sock >= 0)
73+
{
7174
close(_sock);
75+
register_udp(_sock, nullptr);
76+
}
7277
_sock = -1;
7378
}
7479

7580
void setMulticastInterface(const ip_addr_t& addr)
7681
{
77-
fprintf(stderr, MOCK "TODO: UdpContext::setMulticastInterface\n");
82+
// not needed on posix, multicast is associated with
83+
// socket and its address (0.0.0.0) = all interfaces
7884
}
7985

8086
void setMulticastTTL(int ttl)
@@ -165,8 +171,8 @@ class UdpContext
165171

166172
void flush()
167173
{
168-
fprintf(stderr, MOCK "UdpContext::flush() does not follow arduino's flush concept\n");
169-
exit(EXIT_FAILURE);
174+
//fprintf(stderr, MOCK "UdpContext::flush() does not follow arduino's flush concept\n");
175+
//exit(EXIT_FAILURE);
170176
// would be:
171177
_inbufsize = 0;
172178
}
@@ -192,6 +198,11 @@ class UdpContext
192198
_outbufsize = 0;
193199
return ret > 0;
194200
}
201+
202+
void mock_cb (void)
203+
{
204+
if (_on_rx) _on_rx();
205+
}
195206

196207
public:
197208

tests/host/common/mock.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ typedef uint32_t uint32;
3333
//
3434

3535
#ifdef __cplusplus
36+
extern "C" {
37+
#endif
38+
int ets_printf (const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
39+
#define os_printf_plus ets_printf
40+
#ifdef __cplusplus
41+
}
42+
#endif
43+
44+
//
45+
46+
#ifdef __cplusplus
47+
3648
#ifndef CCBUFSIZE
3749
#define CCBUFSIZE 8192
3850
#endif
@@ -53,4 +65,8 @@ size_t mockUDPFillInBuf (int sock, char* ccinbuf, size_t& ccinbufsize, uint8_t&
5365
size_t mockUDPPeekBytes (int sock, char* dst, size_t usersize, int timeout_ms, char* ccinbuf, size_t& ccinbufsize);
5466
size_t mockUDPRead (int sock, char* dst, size_t size, int timeout_ms, char* ccinbuf, size_t& ccinbufsize);
5567
size_t mockUDPWrite (int sock, const uint8_t* data, size_t size, int timeout_ms, uint32_t ipv4, uint16_t port);
68+
69+
class UdpContext;
70+
void register_udp (int sock, UdpContext* udp);
71+
5672
#endif // __cplusplus

tests/host/common/user_interface.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,15 @@ bool wifi_get_ip_info (uint8 if_index, struct ip_info *info)
7979
fprintf(stderr, "we are not AP");
8080

8181
//XXXTODO (give ip address of default route's interface?)
82+
#if 1
83+
info->ip.addr = lwip_htonl(0xc0a80108);
84+
info->netmask.addr = lwip_htonl(0xffffff00);
85+
info->gw.addr = lwip_htonl(0xc0a801fe);
86+
#else
8287
info->ip.addr = lwip_htonl(0x7f000001);
8388
info->netmask.addr = lwip_htonl(0xff000000);
8489
info->gw.addr = lwip_htonl(0x7f000001);
85-
90+
#endif
8691
return true;
8792
}
8893

0 commit comments

Comments
 (0)