Skip to content

Commit 3665c87

Browse files
committed
UDP multicast APIs fix
fix #74, fix #7
1 parent f84f65c commit 3665c87

File tree

7 files changed

+58
-16
lines changed

7 files changed

+58
-16
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ flush KEYWORD2
3333
stop KEYWORD2
3434
connected KEYWORD2
3535
begin KEYWORD2
36+
beginMulticast KEYWORD2
3637
disconnect KEYWORD2
3738
macAddress KEYWORD2
3839
localIP KEYWORD2
@@ -43,6 +44,7 @@ BSSID KEYWORD2
4344
RSSI KEYWORD2
4445
encryptionType KEYWORD2
4546
beginPacket KEYWORD2
47+
beginPacketMulticast KEYWORD2
4648
endPacket KEYWORD2
4749
parsePacket KEYWORD2
4850
remoteIP KEYWORD2

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

+19-8
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,33 @@ int WiFiUDP::beginPacket(IPAddress ip, uint16_t port)
142142
return (_ctx->connect(addr, port)) ? 1 : 0;
143143
}
144144

145-
int WiFiUDP::endPacket()
145+
int WiFiUDP::beginPacketMulticast(IPAddress multicastAddress, uint16_t port,
146+
IPAddress interfaceAddress, int ttl)
146147
{
147-
if (!_ctx)
148-
return 0;
148+
ip_addr_t mcastAddr;
149+
mcastAddr.addr = multicastAddress;
150+
ip_addr_t ifaceAddr;
151+
ifaceAddr.addr = interfaceAddress;
149152

150-
_ctx->send();
153+
if (!_ctx) {
154+
_ctx = new UdpContext;
155+
_ctx->ref();
156+
}
157+
if (!_ctx->connect(mcastAddr, port)) {
158+
return 0;
159+
}
160+
_ctx->setMulticastInterface(ifaceAddr);
161+
_ctx->setMulticastTTL(ttl);
151162
return 1;
152163
}
153164

154-
int WiFiUDP::endPacketMulticast(IPAddress ip, uint16_t port)
165+
int WiFiUDP::endPacket()
155166
{
156167
if (!_ctx)
157168
return 0;
158-
ip_addr_t addr;
159-
addr.addr = (uint32_t) ip;
160-
_ctx->send(&addr, port);
169+
170+
_ctx->send();
171+
_ctx->disconnect();
161172
return 1;
162173
}
163174

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ class WiFiUDP : public UDP {
4040

4141
operator bool() const { return _ctx != 0; }
4242

43-
virtual uint8_t begin(uint16_t port); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
44-
virtual void stop(); // Finish with the UDP socket
45-
uint8_t beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port); // connect to a multicast group and listen on the given port
43+
// initialize, start listening on specified port.
44+
// Returns 1 if successful, 0 if there are no sockets available to use
45+
virtual uint8_t begin(uint16_t port);
46+
// Finish with the UDP connetion
47+
virtual void stop();
48+
// join a multicast group and listen on the given port
49+
uint8_t beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port);
4650

4751
// Sending UDP packets
4852

@@ -52,12 +56,19 @@ class WiFiUDP : public UDP {
5256
// Start building up a packet to send to the remote host specific in host and port
5357
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
5458
virtual int beginPacket(const char *host, uint16_t port);
59+
// Start building up a packet to send to the multicast address
60+
// multicastAddress - muticast address to send to
61+
// interfaceAddress - the local IP address of the interface that should be used
62+
// use WiFi.localIP() or WiFi.softAPIP() depending on the interface you need
63+
// ttl - multicast packet TTL (default is 1)
64+
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
65+
virtual int beginPacketMulticast(IPAddress multicastAddress,
66+
uint16_t port,
67+
IPAddress interfaceAddress,
68+
int ttl = 1);
5569
// Finish off this packet and send it
5670
// Returns 1 if the packet was sent successfully, 0 if there was an error
5771
virtual int endPacket();
58-
// Send the packet to a multicast address
59-
// Returns 1 if the packet was sent successfully, 0 if there was an error
60-
int endPacketMulticast(IPAddress ip, uint16_t port);
6172
// Write a single byte into the packet
6273
virtual size_t write(uint8_t);
6374
// Write size bytes from buffer into the packet

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

+15
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ class UdpContext
9494
udp_disconnect(_pcb);
9595
}
9696

97+
void setMulticastInterface(ip_addr_t addr)
98+
{
99+
// newer versions of lwip have a macro to set the multicast ip
100+
// udp_set_multicast_netif_addr(_pcb, addr);
101+
_pcb->multicast_ip = addr;
102+
}
103+
104+
void setMulticastTTL(int ttl)
105+
{
106+
// newer versions of lwip have an additional field (mcast_ttl) for this purpose
107+
// and a macro to set it instead of direct field access
108+
// udp_set_multicast_ttl(_pcb, ttl);
109+
_pcb->ttl = ttl;
110+
}
111+
97112
size_t getSize() const
98113
{
99114
if (!_rx_buf)

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ MDNSResponder::~MDNSResponder() {
6262

6363
bool MDNSResponder::begin(const char* domain, IPAddress addr, uint32_t ttlSeconds)
6464
{
65+
_localAddr = addr;
6566
// Construct DNS request/response fully qualified domain name of form:
6667
// <domain length>, <domain characters>, 5, "local"
6768
size_t n = strlen(domain);
@@ -190,8 +191,9 @@ void MDNSResponder::update() {
190191
Serial.print("responding, i=");
191192
Serial.println(i);
192193
#endif
194+
_mdnsConn.beginPacketMulticast(IPAddress(224, 0, 0, 251), 5353, _localAddr);
193195
_mdnsConn.write(_response, _responseLen);
194-
_mdnsConn.endPacketMulticast(IPAddress(224, 0, 0, 251), 5353);
196+
_mdnsConn.endPacket();
195197
_index = 0;
196198
}
197199
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class MDNSResponder {
6464
int _responseLen;
6565
// Socket for MDNS communication
6666
WiFiUDP _mdnsConn;
67+
// local IP Address
68+
IPAddress _localAddr;
6769
};
6870

6971
#endif //ESP8266MDNS_H

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

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ void setup(void)
5353
// the fully-qualified domain name is "esp8266.local"
5454
// - second argument is the IP address to advertise
5555
// we send our IP address on the WiFi network
56-
// Note: for AP mode we would use WiFi.softAPIP()!
5756
if (!mdns.begin("esp8266", WiFi.localIP())) {
5857
Serial.println("Error setting up MDNS responder!");
5958
while(1) {

0 commit comments

Comments
 (0)