diff --git a/CMakeLists.txt b/CMakeLists.txt index fc5c1733..3d62eefa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,4 @@ set(CORE_SRCS - cores/esp32/cbuf.cpp cores/esp32/esp32-hal-adc.c cores/esp32/esp32-hal-bt.c cores/esp32/esp32-hal-cpu.c @@ -36,7 +35,6 @@ set(CORE_SRCS ) set(LIBRARY_SRCS - libraries/AsyncUDP/src/AsyncUDP.cpp libraries/FS/src/FS.cpp libraries/FS/src/vfs_api.cpp libraries/SPIFFS/src/SPIFFS.cpp @@ -47,7 +45,6 @@ set(LIBRARY_SRCS set(includedirs variants/esp32/ cores/esp32/ - libraries/AsyncUDP/src libraries/FS/src libraries/SPIFFS/src libraries/Wire/src diff --git a/Kconfig.projbuild b/Kconfig.projbuild index ca26ee36..fdb4e0a0 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -61,33 +61,6 @@ config ARDUINO_EVENT_RUNNING_CORE default 1 if ARDUINO_EVENT_RUN_CORE1 default -1 if ARDUINO_EVENT_RUN_NO_AFFINITY -choice ARDUINO_UDP_RUNNING_CORE - bool "Core on which Arduino's UDP is running" - default ARDUINO_UDP_RUN_CORE1 - help - Select on which core Arduino's UDP run - - config ARDUINO_UDP_RUN_CORE0 - bool "CORE 0" - config ARDUINO_UDP_RUN_CORE1 - bool "CORE 1" - config ARDUINO_UDP_RUN_NO_AFFINITY - bool "BOTH" - -endchoice - -config ARDUINO_UDP_TASK_PRIORITY - int "Priority of the UDP task" - default 3 - help - Select at what priority you want the UDP task to run. - -config ARDUINO_UDP_RUNNING_CORE - int - default 0 if ARDUINO_UDP_RUN_CORE0 - default 1 if ARDUINO_UDP_RUN_CORE1 - default -1 if ARDUINO_UDP_RUN_NO_AFFINITY - config ARDUINO_ISR_IRAM bool "Run interrupts in IRAM" default "n" @@ -188,11 +161,6 @@ config ARDUINO_SELECTIVE_COMPILATION bool "Include only specific Arduino libraries" default n -config ARDUINO_SELECTIVE_AsyncUDP - bool "Enable AsyncUDP" - depends on ARDUINO_SELECTIVE_COMPILATION - default y - config ARDUINO_SELECTIVE_FS bool "Enable FS" depends on ARDUINO_SELECTIVE_COMPILATION diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index 679e2740..53f07a6f 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -150,9 +150,6 @@ void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); #include "Printable.h" #include "Print.h" #include "IPAddress.h" -#include "Client.h" -#include "Server.h" -#include "Udp.h" #include "HardwareSerial.h" using std::abs; diff --git a/cores/esp32/Client.h b/cores/esp32/Client.h deleted file mode 100644 index 962cacc5..00000000 --- a/cores/esp32/Client.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - Client.h - Base class that provides Client - Copyright (c) 2011 Adrian McEwen. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef client_h -#define client_h -#include "Print.h" -#include "Stream.h" -#include "IPAddress.h" - -class Client: public Stream -{ -public: - virtual int connect(IPAddress ip, uint16_t port) =0; - virtual int connect(const char *host, uint16_t port) =0; - virtual size_t write(uint8_t) =0; - virtual size_t write(const uint8_t *buf, size_t size) =0; - virtual int available() = 0; - virtual int read() = 0; - virtual int read(uint8_t *buf, size_t size) = 0; - virtual int peek() = 0; - virtual void flush() = 0; - virtual void stop() = 0; - virtual uint8_t connected() = 0; - virtual operator bool() = 0; -protected: - uint8_t* rawIPAddress(IPAddress& addr) - { - return addr.raw_address(); - } -}; - -#endif diff --git a/cores/esp32/Server.h b/cores/esp32/Server.h deleted file mode 100644 index 6a940a0b..00000000 --- a/cores/esp32/Server.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - Server.h - Base class that provides Server - Copyright (c) 2011 Adrian McEwen. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef server_h -#define server_h - -#include "Print.h" - -class Server: public Print -{ -public: - virtual void begin(uint16_t port=0) =0; -}; - -#endif diff --git a/cores/esp32/Udp.h b/cores/esp32/Udp.h deleted file mode 100644 index fd79975e..00000000 --- a/cores/esp32/Udp.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Udp.cpp: Library to send/receive UDP packets. - * - * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) - * 1) UDP does not guarantee the order in which assembled UDP packets are received. This - * might not happen often in practice, but in larger network topologies, a UDP - * packet can be received out of sequence. - * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being - * aware of it. Again, this may not be a concern in practice on small local networks. - * For more information, see http://www.cafeaulait.org/course/week12/35.html - * - * MIT License: - * Copyright (c) 2008 Bjoern Hartmann - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * bjoern@cs.stanford.edu 12/30/2008 - */ - -#ifndef udp_h -#define udp_h - -#include -#include - -class UDP: public Stream -{ - -public: - 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 - - // Start building up a packet to send to the remote host specific in ip and port - // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port - virtual int beginPacket(IPAddress ip, uint16_t port) =0; - // Start building up a packet to send to the remote host specific in host and port - // Returns 1 if successful, 0 if there was a problem resolving the hostname or port - virtual int beginPacket(const char *host, uint16_t port) =0; - // Finish off this packet and send it - // Returns 1 if the packet was sent successfully, 0 if there was an error - virtual int endPacket() =0; - // Write a single byte into the packet - virtual size_t write(uint8_t) =0; - // Write size bytes from buffer into the packet - virtual size_t write(const uint8_t *buffer, size_t size) =0; - - // Start processing the next available incoming packet - // Returns the size of the packet in bytes, or 0 if no packets are available - virtual int parsePacket() =0; - // Number of bytes remaining in the current packet - virtual int available() =0; - // Read a single byte from the current packet - virtual int read() =0; - // Read up to len bytes from the current packet and place them into buffer - // Returns the number of bytes read, or 0 if none are available - virtual int read(unsigned char* buffer, size_t len) =0; - // Read up to len characters from the current packet and place them into buffer - // Returns the number of characters read, or 0 if none are available - virtual int read(char* buffer, size_t len) =0; - // Return the next byte from the current packet without moving on to the next byte - virtual int peek() =0; - virtual void flush() =0; // Finish reading the current packet - - // Return the IP address of the host who sent the current incoming packet - virtual IPAddress remoteIP() =0; - // Return the port of the host who sent the current incoming packet - virtual uint16_t remotePort() =0; -protected: - uint8_t* rawIPAddress(IPAddress& addr) - { - return addr.raw_address(); - } -}; - -#endif diff --git a/cores/esp32/cbuf.cpp b/cores/esp32/cbuf.cpp deleted file mode 100644 index ef7370a8..00000000 --- a/cores/esp32/cbuf.cpp +++ /dev/null @@ -1,196 +0,0 @@ -/* - cbuf.cpp - Circular buffer implementation - Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. - This file is part of the esp8266 core for Arduino environment. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "cbuf.h" - -cbuf::cbuf(size_t size) : - next(NULL), _size(size+1), _buf(new char[size+1]), _bufend(_buf + size + 1), _begin(_buf), _end(_begin) -{ -} - -cbuf::~cbuf() -{ - delete[] _buf; -} - -size_t cbuf::resizeAdd(size_t addSize) -{ - return resize(_size + addSize); -} - -size_t cbuf::resize(size_t newSize) -{ - - size_t bytes_available = available(); - newSize += 1; - // not lose any data - // if data can be lost use remove or flush before resize - if((newSize < bytes_available) || (newSize == _size)) { - return _size; - } - - char *newbuf = new char[newSize]; - char *oldbuf = _buf; - - if(!newbuf) { - return _size; - } - - if(_buf) { - read(newbuf, bytes_available); - memset((newbuf + bytes_available), 0x00, (newSize - bytes_available)); - } - - _begin = newbuf; - _end = newbuf + bytes_available; - _bufend = newbuf + newSize; - _size = newSize; - - _buf = newbuf; - delete[] oldbuf; - - return _size; -} - -size_t cbuf::available() const -{ - if(_end >= _begin) { - return _end - _begin; - } - return _size - (_begin - _end); -} - -size_t cbuf::size() -{ - return _size; -} - -size_t cbuf::room() const -{ - if(_end >= _begin) { - return _size - (_end - _begin) - 1; - } - return _begin - _end - 1; -} - -int cbuf::peek() -{ - if(empty()) { - return -1; - } - - return static_cast(*_begin); -} - -size_t cbuf::peek(char *dst, size_t size) -{ - size_t bytes_available = available(); - size_t size_to_read = (size < bytes_available) ? size : bytes_available; - size_t size_read = size_to_read; - char * begin = _begin; - if(_end < _begin && size_to_read > (size_t) (_bufend - _begin)) { - size_t top_size = _bufend - _begin; - memcpy(dst, _begin, top_size); - begin = _buf; - size_to_read -= top_size; - dst += top_size; - } - memcpy(dst, begin, size_to_read); - return size_read; -} - -int cbuf::read() -{ - if(empty()) { - return -1; - } - - char result = *_begin; - _begin = wrap_if_bufend(_begin + 1); - return static_cast(result); -} - -size_t cbuf::read(char* dst, size_t size) -{ - size_t bytes_available = available(); - size_t size_to_read = (size < bytes_available) ? size : bytes_available; - size_t size_read = size_to_read; - if(_end < _begin && size_to_read > (size_t) (_bufend - _begin)) { - size_t top_size = _bufend - _begin; - memcpy(dst, _begin, top_size); - _begin = _buf; - size_to_read -= top_size; - dst += top_size; - } - memcpy(dst, _begin, size_to_read); - _begin = wrap_if_bufend(_begin + size_to_read); - return size_read; -} - -size_t cbuf::write(char c) -{ - if(full()) { - return 0; - } - - *_end = c; - _end = wrap_if_bufend(_end + 1); - return 1; -} - -size_t cbuf::write(const char* src, size_t size) -{ - size_t bytes_available = room(); - size_t size_to_write = (size < bytes_available) ? size : bytes_available; - size_t size_written = size_to_write; - if(_end >= _begin && size_to_write > (size_t) (_bufend - _end)) { - size_t top_size = _bufend - _end; - memcpy(_end, src, top_size); - _end = _buf; - size_to_write -= top_size; - src += top_size; - } - memcpy(_end, src, size_to_write); - _end = wrap_if_bufend(_end + size_to_write); - return size_written; -} - -void cbuf::flush() -{ - _begin = _buf; - _end = _buf; -} - -size_t cbuf::remove(size_t size) -{ - size_t bytes_available = available(); - if(size >= bytes_available) { - flush(); - return 0; - } - size_t size_to_remove = (size < bytes_available) ? size : bytes_available; - if(_end < _begin && size_to_remove > (size_t) (_bufend - _begin)) { - size_t top_size = _bufend - _begin; - _begin = _buf; - size_to_remove -= top_size; - } - _begin = wrap_if_bufend(_begin + size_to_remove); - return available(); -} diff --git a/cores/esp32/cbuf.h b/cores/esp32/cbuf.h deleted file mode 100644 index ca65affc..00000000 --- a/cores/esp32/cbuf.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - cbuf.h - Circular buffer implementation - Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. - This file is part of the esp8266 core for Arduino environment. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef __cbuf_h -#define __cbuf_h - -#include -#include -#include - -class cbuf -{ -public: - cbuf(size_t size); - ~cbuf(); - - size_t resizeAdd(size_t addSize); - size_t resize(size_t newSize); - size_t available() const; - size_t size(); - - size_t room() const; - - inline bool empty() const - { - return _begin == _end; - } - - inline bool full() const - { - return wrap_if_bufend(_end + 1) == _begin; - } - - int peek(); - size_t peek(char *dst, size_t size); - - int read(); - size_t read(char* dst, size_t size); - - size_t write(char c); - size_t write(const char* src, size_t size); - - void flush(); - size_t remove(size_t size); - - cbuf *next; - -private: - inline char* wrap_if_bufend(char* ptr) const - { - return (ptr == _bufend) ? _buf : ptr; - } - - size_t _size; - char* _buf; - const char* _bufend; - char* _begin; - char* _end; - -}; - -#endif//__cbuf_h diff --git a/libraries/AsyncUDP/examples/AsyncUDPClient/AsyncUDPClient.ino b/libraries/AsyncUDP/examples/AsyncUDPClient/AsyncUDPClient.ino deleted file mode 100644 index 3348f8a7..00000000 --- a/libraries/AsyncUDP/examples/AsyncUDPClient/AsyncUDPClient.ino +++ /dev/null @@ -1,51 +0,0 @@ -#include "WiFi.h" -#include "AsyncUDP.h" - -const char * ssid = "***********"; -const char * password = "***********"; - -AsyncUDP udp; - -void setup() -{ - Serial.begin(115200); - WiFi.mode(WIFI_STA); - WiFi.begin(ssid, password); - if (WiFi.waitForConnectResult() != WL_CONNECTED) { - Serial.println("WiFi Failed"); - while(1) { - delay(1000); - } - } - if(udp.connect(IPAddress(192,168,1,100), 1234)) { - Serial.println("UDP connected"); - udp.onPacket([](AsyncUDPPacket packet) { - Serial.print("UDP Packet Type: "); - Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast"); - Serial.print(", From: "); - Serial.print(packet.remoteIP()); - Serial.print(":"); - Serial.print(packet.remotePort()); - Serial.print(", To: "); - Serial.print(packet.localIP()); - Serial.print(":"); - Serial.print(packet.localPort()); - Serial.print(", Length: "); - Serial.print(packet.length()); - Serial.print(", Data: "); - Serial.write(packet.data(), packet.length()); - Serial.println(); - //reply to the client - packet.printf("Got %u bytes of data", packet.length()); - }); - //Send unicast - udp.print("Hello Server!"); - } -} - -void loop() -{ - delay(1000); - //Send broadcast on port 1234 - udp.broadcastTo("Anyone here?", 1234); -} diff --git a/libraries/AsyncUDP/examples/AsyncUDPMulticastServer/AsyncUDPMulticastServer.ino b/libraries/AsyncUDP/examples/AsyncUDPMulticastServer/AsyncUDPMulticastServer.ino deleted file mode 100644 index 2bbbac51..00000000 --- a/libraries/AsyncUDP/examples/AsyncUDPMulticastServer/AsyncUDPMulticastServer.ino +++ /dev/null @@ -1,52 +0,0 @@ -#include "WiFi.h" -#include "AsyncUDP.h" - -const char * ssid = "***********"; -const char * password = "***********"; - -AsyncUDP udp; - -void setup() -{ - Serial.begin(115200); - WiFi.mode(WIFI_STA); - WiFi.begin(ssid, password); - if (WiFi.waitForConnectResult() != WL_CONNECTED) { - Serial.println("WiFi Failed"); - while(1) { - delay(1000); - } - } - if(udp.listenMulticast(IPAddress(239,1,2,3), 1234)) { - Serial.print("UDP Listening on IP: "); - Serial.println(WiFi.localIP()); - udp.onPacket([](AsyncUDPPacket packet) { - Serial.print("UDP Packet Type: "); - Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast"); - Serial.print(", From: "); - Serial.print(packet.remoteIP()); - Serial.print(":"); - Serial.print(packet.remotePort()); - Serial.print(", To: "); - Serial.print(packet.localIP()); - Serial.print(":"); - Serial.print(packet.localPort()); - Serial.print(", Length: "); - Serial.print(packet.length()); - Serial.print(", Data: "); - Serial.write(packet.data(), packet.length()); - Serial.println(); - //reply to the client - packet.printf("Got %u bytes of data", packet.length()); - }); - //Send multicast - udp.print("Hello!"); - } -} - -void loop() -{ - delay(1000); - //Send multicast - udp.print("Anyone here?"); -} diff --git a/libraries/AsyncUDP/examples/AsyncUDPServer/AsyncUDPServer.ino b/libraries/AsyncUDP/examples/AsyncUDPServer/AsyncUDPServer.ino deleted file mode 100644 index 1f8529bd..00000000 --- a/libraries/AsyncUDP/examples/AsyncUDPServer/AsyncUDPServer.ino +++ /dev/null @@ -1,50 +0,0 @@ -#include "WiFi.h" -#include "AsyncUDP.h" - -const char * ssid = "***********"; -const char * password = "***********"; - -AsyncUDP udp; - -void setup() -{ - Serial.begin(115200); - WiFi.mode(WIFI_STA); - WiFi.begin(ssid, password); - if (WiFi.waitForConnectResult() != WL_CONNECTED) { - Serial.println("WiFi Failed"); - while(1) { - delay(1000); - } - } - if(udp.listen(1234)) { - Serial.print("UDP Listening on IP: "); - Serial.println(WiFi.localIP()); - udp.onPacket([](AsyncUDPPacket packet) { - Serial.print("UDP Packet Type: "); - Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast"); - Serial.print(", From: "); - Serial.print(packet.remoteIP()); - Serial.print(":"); - Serial.print(packet.remotePort()); - Serial.print(", To: "); - Serial.print(packet.localIP()); - Serial.print(":"); - Serial.print(packet.localPort()); - Serial.print(", Length: "); - Serial.print(packet.length()); - Serial.print(", Data: "); - Serial.write(packet.data(), packet.length()); - Serial.println(); - //reply to the client - packet.printf("Got %u bytes of data", packet.length()); - }); - } -} - -void loop() -{ - delay(1000); - //Send broadcast - udp.broadcast("Anyone here?"); -} diff --git a/libraries/AsyncUDP/keywords.txt b/libraries/AsyncUDP/keywords.txt deleted file mode 100644 index 67c0b97a..00000000 --- a/libraries/AsyncUDP/keywords.txt +++ /dev/null @@ -1,33 +0,0 @@ -####################################### -# Syntax Coloring Map For Ultrasound -####################################### - -####################################### -# Datatypes (KEYWORD1) -####################################### - -AsyncUDP KEYWORD1 -AsyncUDPPacket KEYWORD1 - -####################################### -# Methods and Functions (KEYWORD2) -####################################### - -connect KEYWORD2 -connected KEYWORD2 -listen KEYWORD2 -listenMulticast KEYWORD2 -close KEYWORD2 -write KEYWORD2 -broadcast KEYWORD2 -onPacket KEYWORD2 -data KEYWORD2 -length KEYWORD2 -localIP KEYWORD2 -localPort KEYWORD2 -remoteIP KEYWORD2 -remotePort KEYWORD2 - -####################################### -# Constants (LITERAL1) -####################################### diff --git a/libraries/AsyncUDP/library.properties b/libraries/AsyncUDP/library.properties deleted file mode 100644 index 95a5e148..00000000 --- a/libraries/AsyncUDP/library.properties +++ /dev/null @@ -1,9 +0,0 @@ -name=ESP32 Async UDP -version=1.0.0 -author=Me-No-Dev -maintainer=Me-No-Dev -sentence=Async UDP Library for ESP32 -paragraph=Async UDP Library for ESP32 -category=Other -url=https://github.com/me-no-dev/ESPAsyncUDP -architectures=* diff --git a/libraries/AsyncUDP/src/AsyncUDP.cpp b/libraries/AsyncUDP/src/AsyncUDP.cpp deleted file mode 100644 index 1007b1a5..00000000 --- a/libraries/AsyncUDP/src/AsyncUDP.cpp +++ /dev/null @@ -1,881 +0,0 @@ -#include "Arduino.h" -#include "AsyncUDP.h" - -extern "C" { -#include "lwip/opt.h" -#include "lwip/inet.h" -#include "lwip/udp.h" -#include "lwip/igmp.h" -#include "lwip/ip_addr.h" -#include "lwip/mld6.h" -#include "lwip/prot/ethernet.h" -#include -#include -} - -#include "lwip/priv/tcpip_priv.h" - -typedef struct { - struct tcpip_api_call_data call; - udp_pcb * pcb; - const ip_addr_t *addr; - uint16_t port; - struct pbuf *pb; - struct netif *netif; - err_t err; -} udp_api_call_t; - -static err_t _udp_connect_api(struct tcpip_api_call_data *api_call_msg){ - udp_api_call_t * msg = (udp_api_call_t *)api_call_msg; - msg->err = udp_connect(msg->pcb, msg->addr, msg->port); - return msg->err; -} - -static err_t _udp_connect(struct udp_pcb *pcb, const ip_addr_t *addr, u16_t port){ - udp_api_call_t msg; - msg.pcb = pcb; - msg.addr = addr; - msg.port = port; - tcpip_api_call(_udp_connect_api, (struct tcpip_api_call_data*)&msg); - return msg.err; -} - -static err_t _udp_disconnect_api(struct tcpip_api_call_data *api_call_msg){ - udp_api_call_t * msg = (udp_api_call_t *)api_call_msg; - msg->err = 0; - udp_disconnect(msg->pcb); - return msg->err; -} - -static void _udp_disconnect(struct udp_pcb *pcb){ - udp_api_call_t msg; - msg.pcb = pcb; - tcpip_api_call(_udp_disconnect_api, (struct tcpip_api_call_data*)&msg); -} - -static err_t _udp_remove_api(struct tcpip_api_call_data *api_call_msg){ - udp_api_call_t * msg = (udp_api_call_t *)api_call_msg; - msg->err = 0; - udp_remove(msg->pcb); - return msg->err; -} - -static void _udp_remove(struct udp_pcb *pcb){ - udp_api_call_t msg; - msg.pcb = pcb; - tcpip_api_call(_udp_remove_api, (struct tcpip_api_call_data*)&msg); -} - -static err_t _udp_bind_api(struct tcpip_api_call_data *api_call_msg){ - udp_api_call_t * msg = (udp_api_call_t *)api_call_msg; - msg->err = udp_bind(msg->pcb, msg->addr, msg->port); - return msg->err; -} - -static err_t _udp_bind(struct udp_pcb *pcb, const ip_addr_t *addr, u16_t port){ - udp_api_call_t msg; - msg.pcb = pcb; - msg.addr = addr; - msg.port = port; - tcpip_api_call(_udp_bind_api, (struct tcpip_api_call_data*)&msg); - return msg.err; -} - -static err_t _udp_sendto_api(struct tcpip_api_call_data *api_call_msg){ - udp_api_call_t * msg = (udp_api_call_t *)api_call_msg; - msg->err = udp_sendto(msg->pcb, msg->pb, msg->addr, msg->port); - return msg->err; -} - -static err_t _udp_sendto(struct udp_pcb *pcb, struct pbuf *pb, const ip_addr_t *addr, u16_t port){ - udp_api_call_t msg; - msg.pcb = pcb; - msg.addr = addr; - msg.port = port; - msg.pb = pb; - tcpip_api_call(_udp_sendto_api, (struct tcpip_api_call_data*)&msg); - return msg.err; -} - -static err_t _udp_sendto_if_api(struct tcpip_api_call_data *api_call_msg){ - udp_api_call_t * msg = (udp_api_call_t *)api_call_msg; - msg->err = udp_sendto_if(msg->pcb, msg->pb, msg->addr, msg->port, msg->netif); - return msg->err; -} - -static err_t _udp_sendto_if(struct udp_pcb *pcb, struct pbuf *pb, const ip_addr_t *addr, u16_t port, struct netif *netif){ - udp_api_call_t msg; - msg.pcb = pcb; - msg.addr = addr; - msg.port = port; - msg.pb = pb; - msg.netif = netif; - tcpip_api_call(_udp_sendto_if_api, (struct tcpip_api_call_data*)&msg); - return msg.err; -} - -typedef struct { - void *arg; - udp_pcb *pcb; - pbuf *pb; - const ip_addr_t *addr; - uint16_t port; - struct netif * netif; -} lwip_event_packet_t; - -static xQueueHandle _udp_queue; -static volatile TaskHandle_t _udp_task_handle = NULL; - -static void _udp_task(void *pvParameters){ - lwip_event_packet_t * e = NULL; - for (;;) { - if(xQueueReceive(_udp_queue, &e, portMAX_DELAY) == pdTRUE){ - if(!e->pb){ - free((void*)(e)); - continue; - } - AsyncUDP::_s_recv(e->arg, e->pcb, e->pb, e->addr, e->port, e->netif); - free((void*)(e)); - } - } - _udp_task_handle = NULL; - vTaskDelete(NULL); -} - -static bool _udp_task_start(){ - if(!_udp_queue){ - _udp_queue = xQueueCreate(32, sizeof(lwip_event_packet_t *)); - if(!_udp_queue){ - return false; - } - } - if(!_udp_task_handle){ - xTaskCreateUniversal(_udp_task, "async_udp", 4096+2048, NULL, CONFIG_ARDUINO_UDP_TASK_PRIORITY, (TaskHandle_t*)&_udp_task_handle, CONFIG_ARDUINO_UDP_RUNNING_CORE); - if(!_udp_task_handle){ - return false; - } - } - return true; -} - -static bool _udp_task_post(void *arg, udp_pcb *pcb, pbuf *pb, const ip_addr_t *addr, uint16_t port, struct netif *netif) -{ - if(!_udp_task_handle || !_udp_queue){ - return false; - } - lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t)); - if(!e){ - return false; - } - e->arg = arg; - e->pcb = pcb; - e->pb = pb; - e->addr = addr; - e->port = port; - e->netif = netif; - if (xQueueSend(_udp_queue, &e, portMAX_DELAY) != pdPASS) { - free((void*)(e)); - return false; - } - return true; -} - -static void _udp_recv(void *arg, udp_pcb *pcb, pbuf *pb, const ip_addr_t *addr, uint16_t port) -{ - while(pb != NULL) { - pbuf * this_pb = pb; - pb = pb->next; - this_pb->next = NULL; - if(!_udp_task_post(arg, pcb, this_pb, addr, port, ip_current_input_netif())){ - pbuf_free(this_pb); - } - } -} -/* -static bool _udp_task_stop(){ - if(!_udp_task_post(NULL, NULL, NULL, NULL, 0, NULL)){ - return false; - } - while(_udp_task_handle){ - vTaskDelay(10); - } - - lwip_event_packet_t * e; - while (xQueueReceive(_udp_queue, &e, 0) == pdTRUE) { - if(e->pb){ - pbuf_free(e->pb); - } - free((void*)(e)); - } - vQueueDelete(_udp_queue); - _udp_queue = NULL; -} -*/ - - - -#define UDP_MUTEX_LOCK() //xSemaphoreTake(_lock, portMAX_DELAY) -#define UDP_MUTEX_UNLOCK() //xSemaphoreGive(_lock) - - -AsyncUDPMessage::AsyncUDPMessage(size_t size) -{ - _index = 0; - if(size > CONFIG_TCP_MSS) { - size = CONFIG_TCP_MSS; - } - _size = size; - _buffer = (uint8_t *)malloc(size); -} - -AsyncUDPMessage::~AsyncUDPMessage() -{ - if(_buffer) { - free(_buffer); - } -} - -size_t AsyncUDPMessage::write(const uint8_t *data, size_t len) -{ - if(_buffer == NULL) { - return 0; - } - size_t s = space(); - if(len > s) { - len = s; - } - memcpy(_buffer + _index, data, len); - _index += len; - return len; -} - -size_t AsyncUDPMessage::write(uint8_t data) -{ - return write(&data, 1); -} - -size_t AsyncUDPMessage::space() -{ - if(_buffer == NULL) { - return 0; - } - return _size - _index; -} - -uint8_t * AsyncUDPMessage::data() -{ - return _buffer; -} - -size_t AsyncUDPMessage::length() -{ - return _index; -} - -void AsyncUDPMessage::flush() -{ - _index = 0; -} - - -AsyncUDPPacket::AsyncUDPPacket(AsyncUDP *udp, pbuf *pb, const ip_addr_t *raddr, uint16_t rport, struct netif * ntif) -{ - _udp = udp; - _pb = pb; - _if = TCPIP_ADAPTER_IF_MAX; - _data = (uint8_t*)(pb->payload); - _len = pb->len; - _index = 0; - - pbuf_ref(_pb); - - //memcpy(&_remoteIp, raddr, sizeof(ip_addr_t)); - _remoteIp.type = raddr->type; - _localIp.type = _remoteIp.type; - - eth_hdr* eth = NULL; - udp_hdr* udphdr = reinterpret_cast(_data - UDP_HLEN); - _localPort = ntohs(udphdr->dest); - _remotePort = ntohs(udphdr->src); - - if (_remoteIp.type == IPADDR_TYPE_V4) { - eth = (eth_hdr *)(((uint8_t *)(pb->payload)) - UDP_HLEN - IP_HLEN - SIZEOF_ETH_HDR); - struct ip_hdr * iphdr = (struct ip_hdr *)(((uint8_t *)(pb->payload)) - UDP_HLEN - IP_HLEN); - _localIp.u_addr.ip4.addr = iphdr->dest.addr; - _remoteIp.u_addr.ip4.addr = iphdr->src.addr; - } else { - eth = (eth_hdr *)(((uint8_t *)(pb->payload)) - UDP_HLEN - IP6_HLEN - SIZEOF_ETH_HDR); - struct ip6_hdr * ip6hdr = (struct ip6_hdr *)(((uint8_t *)(pb->payload)) - UDP_HLEN - IP6_HLEN); - memcpy(&_localIp.u_addr.ip6.addr, (uint8_t *)ip6hdr->dest.addr, 16); - memcpy(&_remoteIp.u_addr.ip6.addr, (uint8_t *)ip6hdr->src.addr, 16); - } - memcpy(_remoteMac, eth->src.addr, 6); - - struct netif * netif = NULL; - void * nif = NULL; - int i; - for (i=0; i a){ - len = a; - } - for(i=0;iwriteTo(data, len, &_remoteIp, _remotePort, _if); -} - -size_t AsyncUDPPacket::write(uint8_t data) -{ - return write(&data, 1); -} - -size_t AsyncUDPPacket::send(AsyncUDPMessage &message) -{ - return write(message.data(), message.length()); -} - -bool AsyncUDP::_init(){ - if(_pcb){ - return true; - } - _pcb = udp_new(); - if(!_pcb){ - return false; - } - //_lock = xSemaphoreCreateMutex(); - udp_recv(_pcb, &_udp_recv, (void *) this); - return true; -} - -AsyncUDP::AsyncUDP() -{ - _pcb = NULL; - _connected = false; - _handler = NULL; -} - -AsyncUDP::~AsyncUDP() -{ - close(); - UDP_MUTEX_LOCK(); - udp_recv(_pcb, NULL, NULL); - _udp_remove(_pcb); - _pcb = NULL; - UDP_MUTEX_UNLOCK(); - //vSemaphoreDelete(_lock); -} - -void AsyncUDP::close() -{ - UDP_MUTEX_LOCK(); - if(_pcb != NULL) { - if(_connected) { - _udp_disconnect(_pcb); - } - _connected = false; - //todo: unjoin multicast group - } - UDP_MUTEX_UNLOCK(); -} - -bool AsyncUDP::connect(const ip_addr_t *addr, uint16_t port) -{ - if(!_udp_task_start()){ - log_e("failed to start task"); - return false; - } - if(!_init()) { - return false; - } - close(); - UDP_MUTEX_LOCK(); - err_t err = _udp_connect(_pcb, addr, port); - if(err != ERR_OK) { - UDP_MUTEX_UNLOCK(); - return false; - } - _connected = true; - UDP_MUTEX_UNLOCK(); - return true; -} - -bool AsyncUDP::listen(const ip_addr_t *addr, uint16_t port) -{ - if(!_udp_task_start()){ - log_e("failed to start task"); - return false; - } - if(!_init()) { - return false; - } - close(); - if(addr){ - IP_SET_TYPE_VAL(_pcb->local_ip, addr->type); - IP_SET_TYPE_VAL(_pcb->remote_ip, addr->type); - } - UDP_MUTEX_LOCK(); - if(_udp_bind(_pcb, addr, port) != ERR_OK) { - UDP_MUTEX_UNLOCK(); - return false; - } - _connected = true; - UDP_MUTEX_UNLOCK(); - return true; -} - -static esp_err_t joinMulticastGroup(const ip_addr_t *addr, bool join, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX) -{ - struct netif * netif = NULL; - if(tcpip_if < TCPIP_ADAPTER_IF_MAX){ - void * nif = NULL; - esp_err_t err = tcpip_adapter_get_netif(tcpip_if, &nif); - if (err) { - return ESP_ERR_INVALID_ARG; - } - netif = (struct netif *)nif; - - if (addr->type == IPADDR_TYPE_V4) { - if(join){ - if (igmp_joingroup_netif(netif, (const ip4_addr *)&(addr->u_addr.ip4))) { - return ESP_ERR_INVALID_STATE; - } - } else { - if (igmp_leavegroup_netif(netif, (const ip4_addr *)&(addr->u_addr.ip4))) { - return ESP_ERR_INVALID_STATE; - } - } - } else { - if(join){ - if (mld6_joingroup_netif(netif, &(addr->u_addr.ip6))) { - return ESP_ERR_INVALID_STATE; - } - } else { - if (mld6_leavegroup_netif(netif, &(addr->u_addr.ip6))) { - return ESP_ERR_INVALID_STATE; - } - } - } - } else { - if (addr->type == IPADDR_TYPE_V4) { - if(join){ - if (igmp_joingroup((const ip4_addr *)IP4_ADDR_ANY, (const ip4_addr *)&(addr->u_addr.ip4))) { - return ESP_ERR_INVALID_STATE; - } - } else { - if (igmp_leavegroup((const ip4_addr *)IP4_ADDR_ANY, (const ip4_addr *)&(addr->u_addr.ip4))) { - return ESP_ERR_INVALID_STATE; - } - } - } else { - if(join){ - if (mld6_joingroup((const ip6_addr *)IP6_ADDR_ANY, &(addr->u_addr.ip6))) { - return ESP_ERR_INVALID_STATE; - } - } else { - if (mld6_leavegroup((const ip6_addr *)IP6_ADDR_ANY, &(addr->u_addr.ip6))) { - return ESP_ERR_INVALID_STATE; - } - } - } - } - return ESP_OK; -} - -bool AsyncUDP::listenMulticast(const ip_addr_t *addr, uint16_t port, uint8_t ttl, tcpip_adapter_if_t tcpip_if) -{ - if(!ip_addr_ismulticast(addr)) { - return false; - } - - if (joinMulticastGroup(addr, true, tcpip_if)!= ERR_OK) { - return false; - } - - if(!listen(NULL, port)) { - return false; - } - - UDP_MUTEX_LOCK(); - _pcb->mcast_ttl = ttl; - _pcb->remote_port = port; - ip_addr_copy(_pcb->remote_ip, *addr); - //ip_addr_copy(_pcb->remote_ip, ip_addr_any_type); - UDP_MUTEX_UNLOCK(); - - return true; -} - -size_t AsyncUDP::writeTo(const uint8_t * data, size_t len, const ip_addr_t * addr, uint16_t port, tcpip_adapter_if_t tcpip_if) -{ - if(!_pcb) { - UDP_MUTEX_LOCK(); - _pcb = udp_new(); - UDP_MUTEX_UNLOCK(); - if(_pcb == NULL) { - return 0; - } - } - if(len > CONFIG_TCP_MSS) { - len = CONFIG_TCP_MSS; - } - err_t err = ERR_OK; - pbuf* pbt = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); - if(pbt != NULL) { - uint8_t* dst = reinterpret_cast(pbt->payload); - memcpy(dst, data, len); - UDP_MUTEX_LOCK(); - if(tcpip_if < TCPIP_ADAPTER_IF_MAX){ - void * nif = NULL; - tcpip_adapter_get_netif((tcpip_adapter_if_t)tcpip_if, &nif); - if(!nif){ - err = _udp_sendto(_pcb, pbt, addr, port); - } else { - err = _udp_sendto_if(_pcb, pbt, addr, port, (struct netif *)nif); - } - } else { - err = _udp_sendto(_pcb, pbt, addr, port); - } - UDP_MUTEX_UNLOCK(); - pbuf_free(pbt); - if(err < ERR_OK) { - return 0; - } - return len; - } - return 0; -} - -void AsyncUDP::_recv(udp_pcb *upcb, pbuf *pb, const ip_addr_t *addr, uint16_t port, struct netif * netif) -{ - while(pb != NULL) { - pbuf * this_pb = pb; - pb = pb->next; - this_pb->next = NULL; - if(_handler) { - AsyncUDPPacket packet(this, this_pb, addr, port, netif); - _handler(packet); - } else { - pbuf_free(this_pb); - } - } -} - -void AsyncUDP::_s_recv(void *arg, udp_pcb *upcb, pbuf *p, const ip_addr_t *addr, uint16_t port, struct netif * netif) -{ - reinterpret_cast(arg)->_recv(upcb, p, addr, port, netif); -} - -bool AsyncUDP::listen(uint16_t port) -{ - return listen(IP_ANY_TYPE, port); -} - -bool AsyncUDP::listen(const IPAddress addr, uint16_t port) -{ - ip_addr_t laddr; - laddr.type = IPADDR_TYPE_V4; - laddr.u_addr.ip4.addr = addr; - return listen(&laddr, port); -} - -bool AsyncUDP::listenMulticast(const IPAddress addr, uint16_t port, uint8_t ttl, tcpip_adapter_if_t tcpip_if) -{ - ip_addr_t laddr; - laddr.type = IPADDR_TYPE_V4; - laddr.u_addr.ip4.addr = addr; - return listenMulticast(&laddr, port, ttl, tcpip_if); -} - -bool AsyncUDP::connect(const IPAddress addr, uint16_t port) -{ - ip_addr_t daddr; - daddr.type = IPADDR_TYPE_V4; - daddr.u_addr.ip4.addr = addr; - return connect(&daddr, port); -} - -size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, const IPAddress addr, uint16_t port, tcpip_adapter_if_t tcpip_if) -{ - ip_addr_t daddr; - daddr.type = IPADDR_TYPE_V4; - daddr.u_addr.ip4.addr = addr; - return writeTo(data, len, &daddr, port, tcpip_if); -} - -IPAddress AsyncUDP::listenIP() -{ - if(!_pcb || _pcb->remote_ip.type != IPADDR_TYPE_V4){ - return IPAddress(); - } - return IPAddress(_pcb->remote_ip.u_addr.ip4.addr); -} - -bool AsyncUDP::listen(const IPv6Address addr, uint16_t port) -{ - ip_addr_t laddr; - laddr.type = IPADDR_TYPE_V6; - memcpy((uint8_t*)(laddr.u_addr.ip6.addr), (const uint8_t*)addr, 16); - return listen(&laddr, port); -} - -bool AsyncUDP::listenMulticast(const IPv6Address addr, uint16_t port, uint8_t ttl, tcpip_adapter_if_t tcpip_if) -{ - ip_addr_t laddr; - laddr.type = IPADDR_TYPE_V6; - memcpy((uint8_t*)(laddr.u_addr.ip6.addr), (const uint8_t*)addr, 16); - return listenMulticast(&laddr, port, ttl, tcpip_if); -} - -bool AsyncUDP::connect(const IPv6Address addr, uint16_t port) -{ - ip_addr_t daddr; - daddr.type = IPADDR_TYPE_V6; - memcpy((uint8_t*)(daddr.u_addr.ip6.addr), (const uint8_t*)addr, 16); - return connect(&daddr, port); -} - -size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, const IPv6Address addr, uint16_t port, tcpip_adapter_if_t tcpip_if) -{ - ip_addr_t daddr; - daddr.type = IPADDR_TYPE_V6; - memcpy((uint8_t*)(daddr.u_addr.ip6.addr), (const uint8_t*)addr, 16); - return writeTo(data, len, &daddr, port, tcpip_if); -} - -IPv6Address AsyncUDP::listenIPv6() -{ - if(!_pcb || _pcb->remote_ip.type != IPADDR_TYPE_V6){ - return IPv6Address(); - } - return IPv6Address(_pcb->remote_ip.u_addr.ip6.addr); -} - -size_t AsyncUDP::write(const uint8_t *data, size_t len) -{ - return writeTo(data, len, &(_pcb->remote_ip), _pcb->remote_port); -} - -size_t AsyncUDP::write(uint8_t data) -{ - return write(&data, 1); -} - -size_t AsyncUDP::broadcastTo(uint8_t *data, size_t len, uint16_t port, tcpip_adapter_if_t tcpip_if) -{ - return writeTo(data, len, IP_ADDR_BROADCAST, port, tcpip_if); -} - -size_t AsyncUDP::broadcastTo(const char * data, uint16_t port, tcpip_adapter_if_t tcpip_if) -{ - return broadcastTo((uint8_t *)data, strlen(data), port, tcpip_if); -} - -size_t AsyncUDP::broadcast(uint8_t *data, size_t len) -{ - if(_pcb->local_port != 0) { - return broadcastTo(data, len, _pcb->local_port); - } - return 0; -} - -size_t AsyncUDP::broadcast(const char * data) -{ - return broadcast((uint8_t *)data, strlen(data)); -} - - -size_t AsyncUDP::sendTo(AsyncUDPMessage &message, const ip_addr_t *addr, uint16_t port, tcpip_adapter_if_t tcpip_if) -{ - if(!message) { - return 0; - } - return writeTo(message.data(), message.length(), addr, port, tcpip_if); -} - -size_t AsyncUDP::sendTo(AsyncUDPMessage &message, const IPAddress addr, uint16_t port, tcpip_adapter_if_t tcpip_if) -{ - if(!message) { - return 0; - } - return writeTo(message.data(), message.length(), addr, port, tcpip_if); -} - -size_t AsyncUDP::sendTo(AsyncUDPMessage &message, const IPv6Address addr, uint16_t port, tcpip_adapter_if_t tcpip_if) -{ - if(!message) { - return 0; - } - return writeTo(message.data(), message.length(), addr, port, tcpip_if); -} - -size_t AsyncUDP::send(AsyncUDPMessage &message) -{ - if(!message) { - return 0; - } - return writeTo(message.data(), message.length(), &(_pcb->remote_ip), _pcb->remote_port); -} - -size_t AsyncUDP::broadcastTo(AsyncUDPMessage &message, uint16_t port, tcpip_adapter_if_t tcpip_if) -{ - if(!message) { - return 0; - } - return broadcastTo(message.data(), message.length(), port, tcpip_if); -} - -size_t AsyncUDP::broadcast(AsyncUDPMessage &message) -{ - if(!message) { - return 0; - } - return broadcast(message.data(), message.length()); -} - -AsyncUDP::operator bool() -{ - return _connected; -} - -bool AsyncUDP::connected() -{ - return _connected; -} - -void AsyncUDP::onPacket(AuPacketHandlerFunctionWithArg cb, void * arg) -{ - onPacket(std::bind(cb, arg, std::placeholders::_1)); -} - -void AsyncUDP::onPacket(AuPacketHandlerFunction cb) -{ - _handler = cb; -} diff --git a/libraries/AsyncUDP/src/AsyncUDP.h b/libraries/AsyncUDP/src/AsyncUDP.h deleted file mode 100644 index bfec5138..00000000 --- a/libraries/AsyncUDP/src/AsyncUDP.h +++ /dev/null @@ -1,152 +0,0 @@ -#ifndef ESPASYNCUDP_H -#define ESPASYNCUDP_H - -#include "IPAddress.h" -#include "IPv6Address.h" -#include "Print.h" -#include -extern "C" { -#include "lwip/ip_addr.h" -#include "esp_netif.h" -#include "freertos/queue.h" -#include "freertos/semphr.h" -} - -class AsyncUDP; -class AsyncUDPPacket; -class AsyncUDPMessage; -struct udp_pcb; -struct pbuf; -struct netif; - -typedef std::function AuPacketHandlerFunction; -typedef std::function AuPacketHandlerFunctionWithArg; - -class AsyncUDPMessage : public Print -{ -protected: - uint8_t *_buffer; - size_t _index; - size_t _size; -public: - AsyncUDPMessage(size_t size=CONFIG_TCP_MSS); - virtual ~AsyncUDPMessage(); - size_t write(const uint8_t *data, size_t len); - size_t write(uint8_t data); - size_t space(); - uint8_t * data(); - size_t length(); - void flush(); - operator bool() - { - return _buffer != NULL; - } -}; - -class AsyncUDPPacket : public Stream -{ -protected: - AsyncUDP *_udp; - pbuf *_pb; - tcpip_adapter_if_t _if; - ip_addr_t _localIp; - uint16_t _localPort; - ip_addr_t _remoteIp; - uint16_t _remotePort; - uint8_t _remoteMac[6]; - uint8_t *_data; - size_t _len; - size_t _index; -public: - AsyncUDPPacket(AsyncUDP *udp, pbuf *pb, const ip_addr_t *addr, uint16_t port, struct netif * netif); - virtual ~AsyncUDPPacket(); - - uint8_t * data(); - size_t length(); - bool isBroadcast(); - bool isMulticast(); - bool isIPv6(); - - tcpip_adapter_if_t interface(); - - IPAddress localIP(); - IPv6Address localIPv6(); - uint16_t localPort(); - IPAddress remoteIP(); - IPv6Address remoteIPv6(); - uint16_t remotePort(); - void remoteMac(uint8_t * mac); - - size_t send(AsyncUDPMessage &message); - - int available(); - size_t read(uint8_t *data, size_t len); - int read(); - int peek(); - void flush(); - - size_t write(const uint8_t *data, size_t len); - size_t write(uint8_t data); -}; - -class AsyncUDP : public Print -{ -protected: - udp_pcb *_pcb; - //xSemaphoreHandle _lock; - bool _connected; - AuPacketHandlerFunction _handler; - - bool _init(); - void _recv(udp_pcb *upcb, pbuf *pb, const ip_addr_t *addr, uint16_t port, struct netif * netif); - -public: - AsyncUDP(); - virtual ~AsyncUDP(); - - void onPacket(AuPacketHandlerFunctionWithArg cb, void * arg=NULL); - void onPacket(AuPacketHandlerFunction cb); - - bool listen(const ip_addr_t *addr, uint16_t port); - bool listen(const IPAddress addr, uint16_t port); - bool listen(const IPv6Address addr, uint16_t port); - bool listen(uint16_t port); - - bool listenMulticast(const ip_addr_t *addr, uint16_t port, uint8_t ttl=1, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX); - bool listenMulticast(const IPAddress addr, uint16_t port, uint8_t ttl=1, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX); - bool listenMulticast(const IPv6Address addr, uint16_t port, uint8_t ttl=1, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX); - - bool connect(const ip_addr_t *addr, uint16_t port); - bool connect(const IPAddress addr, uint16_t port); - bool connect(const IPv6Address addr, uint16_t port); - - void close(); - - size_t writeTo(const uint8_t *data, size_t len, const ip_addr_t *addr, uint16_t port, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX); - size_t writeTo(const uint8_t *data, size_t len, const IPAddress addr, uint16_t port, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX); - size_t writeTo(const uint8_t *data, size_t len, const IPv6Address addr, uint16_t port, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX); - size_t write(const uint8_t *data, size_t len); - size_t write(uint8_t data); - - size_t broadcastTo(uint8_t *data, size_t len, uint16_t port, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX); - size_t broadcastTo(const char * data, uint16_t port, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX); - size_t broadcast(uint8_t *data, size_t len); - size_t broadcast(const char * data); - - size_t sendTo(AsyncUDPMessage &message, const ip_addr_t *addr, uint16_t port, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX); - size_t sendTo(AsyncUDPMessage &message, const IPAddress addr, uint16_t port, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX); - size_t sendTo(AsyncUDPMessage &message, const IPv6Address addr, uint16_t port, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX); - size_t send(AsyncUDPMessage &message); - - size_t broadcastTo(AsyncUDPMessage &message, uint16_t port, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX); - size_t broadcast(AsyncUDPMessage &message); - - IPAddress listenIP(); - IPv6Address listenIPv6(); - bool connected(); - operator bool(); - - static void _s_recv(void *arg, udp_pcb *upcb, pbuf *p, const ip_addr_t *addr, uint16_t port, struct netif * netif); -}; - -#endif