diff --git a/cores/esp8266/Udp.h b/cores/esp8266/Udp.h index 6c8ee4b385..37f2fb922b 100644 --- a/cores/esp8266/Udp.h +++ b/cores/esp8266/Udp.h @@ -43,6 +43,7 @@ class UDP: public Stream { public: virtual ~UDP() {}; virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use + virtual uint8_t beginMulticast(IPAddress, uint16_t) { return 0; } // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure virtual void stop() =0; // Finish with the UDP socket // Sending UDP packets diff --git a/doc/esp8266wifi/udp-class.rst b/doc/esp8266wifi/udp-class.rst index 5e02f7b3da..73a9086021 100644 --- a/doc/esp8266wifi/udp-class.rst +++ b/doc/esp8266wifi/udp-class.rst @@ -26,11 +26,11 @@ Multicast UDP .. code:: cpp - uint8_t beginMulticast (IPAddress interfaceAddr, IPAddress multicast, uint16_t port) + uint8_t beginMulticast (IPAddress multicast, uint16_t port) virtual int beginPacketMulticast (IPAddress multicastAddress, uint16_t port, IPAddress interfaceAddress, int ttl=1) IPAddress destinationIP () uint16_t localPort () -The ``WiFiUDP`` class supports sending and receiving multicast packets on STA interface. When sending a multicast packet, replace ``udp.beginPacket(addr, port)`` with ``udp.beginPacketMulticast(addr, port, WiFi.localIP())``. When listening to multicast packets, replace ``udp.begin(port)`` with ``udp.beginMulticast(WiFi.localIP(), multicast_ip_addr, port)``. You can use ``udp.destinationIP()`` to tell whether the packet received was sent to the multicast or unicast address. +The ``WiFiUDP`` class supports sending and receiving multicast packets on STA interface. When sending a multicast packet, replace ``udp.beginPacket(addr, port)`` with ``udp.beginPacketMulticast(addr, port, WiFi.localIP())``. When listening to multicast packets, replace ``udp.begin(port)`` with ``udp.beginMulticast(multicast_ip_addr, port)``. You can use ``udp.destinationIP()`` to tell whether the packet received was sent to the multicast or unicast address. For code samples please refer to separate section with `examples `__ dedicated specifically to the UDP Class. diff --git a/libraries/ESP8266WiFi/src/WiFiUdp.cpp b/libraries/ESP8266WiFi/src/WiFiUdp.cpp index a45659d844..e717746f25 100644 --- a/libraries/ESP8266WiFi/src/WiFiUdp.cpp +++ b/libraries/ESP8266WiFi/src/WiFiUdp.cpp @@ -84,7 +84,18 @@ uint8_t WiFiUDP::begin(uint16_t port) return (_ctx->listen(IPAddress(), port)) ? 1 : 0; } +uint8_t WiFiUDP::beginMulticast(IPAddress multicast, uint16_t port) +{ + IPAddress local = WiFi.localIP(); + return _beginMulticast(local, multicast, port); +} + uint8_t WiFiUDP::beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port) +{ + return _beginMulticast(interfaceAddr, multicast, port); +} + +uint8_t WiFiUDP::_beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port) { if (_ctx) { _ctx->unref(); diff --git a/libraries/ESP8266WiFi/src/WiFiUdp.h b/libraries/ESP8266WiFi/src/WiFiUdp.h index d8fbcbe221..4164e9ff70 100644 --- a/libraries/ESP8266WiFi/src/WiFiUdp.h +++ b/libraries/ESP8266WiFi/src/WiFiUdp.h @@ -32,6 +32,7 @@ class UdpContext; class WiFiUDP : public UDP, public SList { private: UdpContext* _ctx; + uint8_t _beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port); public: WiFiUDP(); // Constructor @@ -47,7 +48,8 @@ class WiFiUDP : public UDP, public SList { // Finish with the UDP connection void stop() override; // join a multicast group and listen on the given port - uint8_t beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port); + virtual uint8_t beginMulticast(IPAddress interfaceAddr, uint16_t port); + uint8_t beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port) __attribute__((deprecated)); // Sending UDP packets