|
| 1 | +#pragma once |
| 2 | +#include <WiFi.h> |
| 3 | +#include <WiFiUdp.h> |
| 4 | +#if defined(ESP32) |
| 5 | +#include <esp_wifi.h> |
| 6 | +#endif |
| 7 | +#include "ArduinoLogger.h" |
| 8 | + |
| 9 | +namespace arduino { |
| 10 | + |
| 11 | +/** |
| 12 | + * @brief A UDP class which makes sure that we can use UDP as |
| 13 | + * simple Stream w/o additional complexity. |
| 14 | + * When we did not specify a target IP address we send the data |
| 15 | + * to the address we received the data. |
| 16 | + * Single character writes will be buffered up to the defined write |
| 17 | + * buffer size (default 256 bytes) |
| 18 | + * @author Phil Schatzmann |
| 19 | + * @copyright GPLv3 |
| 20 | + */ |
| 21 | + |
| 22 | +class UDPStream : public Stream { |
| 23 | + public: |
| 24 | + /// Default Constructor |
| 25 | + UDPStream() = default; |
| 26 | + |
| 27 | + /// Short hand constructor to start sending data to the indicated address / |
| 28 | + /// port |
| 29 | + UDPStream(IPAddress a, uint16_t port) { begin(a, port); } |
| 30 | + |
| 31 | + /// @brief Defines an alternative UDP object. By default we use WiFiUDP |
| 32 | + /// @param udp |
| 33 | + void setUDP(UDP& udp) { p_udp = &udp; }; |
| 34 | + |
| 35 | + /// Always return 1492 (MTU 1500 - 8 byte header) as UDP packet available to |
| 36 | + /// write |
| 37 | + int availableForWrite() override { return 1492; } |
| 38 | + |
| 39 | + /** |
| 40 | + * Provides the available size of the current package and if this is used up |
| 41 | + * of the next package |
| 42 | + */ |
| 43 | + int available() override { |
| 44 | + int size = p_udp->available(); |
| 45 | + // if the curren package is used up we prvide the info for the next |
| 46 | + if (size == 0) { |
| 47 | + size = p_udp->parsePacket(); |
| 48 | + } |
| 49 | + return size; |
| 50 | + } |
| 51 | + |
| 52 | + /// Starts to send data to the indicated address / port |
| 53 | + bool begin(IPAddress a, uint16_t port) { |
| 54 | + remote_address_ext = a; |
| 55 | + remote_port_ext = port; |
| 56 | + return p_udp->begin(port); |
| 57 | + } |
| 58 | + |
| 59 | + /// Starts to receive data from/with the indicated port |
| 60 | + bool begin(uint16_t port, uint16_t port_ext = 0) { |
| 61 | + remote_address_ext = IPAddress((uint32_t)0); |
| 62 | + remote_port_ext = port_ext != 0 ? port_ext : port; |
| 63 | + return p_udp->begin(port); |
| 64 | + } |
| 65 | + |
| 66 | + /// Starts to receive data in multicast from/with the indicated address / port |
| 67 | + bool beginMulticast(IPAddress address, uint16_t port) { |
| 68 | + return p_udp->beginMulticast(address, port); |
| 69 | + } |
| 70 | + |
| 71 | + /// Alternative to begin(IPAddress a, uint16_t port) |
| 72 | + bool setTarget(IPAddress a, uint16_t port) { return begin(a, port); } |
| 73 | + |
| 74 | + /// We use the same remote port as defined in begin for write |
| 75 | + uint16_t remotePort() { |
| 76 | + uint16_t result = p_udp->remotePort(); |
| 77 | + return result != 0 ? result : remote_port_ext; |
| 78 | + } |
| 79 | + |
| 80 | + /// We use the same remote ip as defined in begin for write |
| 81 | + IPAddress remoteIP() { |
| 82 | + // Determine address if it has not been specified |
| 83 | + if ((uint32_t)remote_address_ext == 0) { |
| 84 | + remote_address_ext = p_udp->remoteIP(); |
| 85 | + } |
| 86 | + // EmulatorLogger.log(EmulatorLogger::INFO, "ip: %u", remote_address_ext); |
| 87 | + return remote_address_ext; |
| 88 | + } |
| 89 | + |
| 90 | + /// Replys will be sent to the initial remote caller |
| 91 | + size_t write(const uint8_t* data, size_t len) override { |
| 92 | + IPAddress remote = remoteIP(); |
| 93 | + if ((uint32_t)remote == 0) { |
| 94 | + Logger.error("UDPStream", "no remote address defined"); |
| 95 | + return 0; |
| 96 | + } |
| 97 | + p_udp->beginPacket(remote, remotePort()); |
| 98 | + size_t result = p_udp->write(data, len); |
| 99 | + p_udp->endPacket(); |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | + /// Reads bytes using WiFi::readBytes |
| 104 | + size_t readBytes(uint8_t* data, size_t len) { |
| 105 | + size_t avail = available(); |
| 106 | + size_t bytes_read = 0; |
| 107 | + if (avail > 0) { |
| 108 | + // get the data now |
| 109 | + bytes_read = p_udp->readBytes((uint8_t*)data, len); |
| 110 | + } |
| 111 | + return bytes_read; |
| 112 | + } |
| 113 | + |
| 114 | + void stop() { p_udp->stop(); } |
| 115 | + |
| 116 | + int peek() override { return p_udp->peek(); } |
| 117 | + |
| 118 | + int read() override { return p_udp->read(); } |
| 119 | + |
| 120 | + size_t write(uint8_t data) override { |
| 121 | + if (write_buffer.capacity() < write_buffer_size) { |
| 122 | + write_buffer.reserve(write_buffer_size); |
| 123 | + } |
| 124 | + write_buffer.push_back(data); |
| 125 | + if (write_buffer.size() >= write_buffer_size) { |
| 126 | + flush(); |
| 127 | + } |
| 128 | + return 1; |
| 129 | + } |
| 130 | + |
| 131 | + void flush() override { |
| 132 | + if (write_buffer.empty()) { |
| 133 | + return; |
| 134 | + } |
| 135 | + write(write_buffer.data(), write_buffer.size()); |
| 136 | + write_buffer.clear(); |
| 137 | + } |
| 138 | + |
| 139 | + void setWriteBufferSize(size_t size) { write_buffer_size = size; } |
| 140 | + |
| 141 | + protected: |
| 142 | + WiFiUDP default_udp; |
| 143 | + UDP* p_udp = &default_udp; |
| 144 | + uint16_t remote_port_ext = 0; |
| 145 | + IPAddress remote_address_ext; |
| 146 | + int write_buffer_size = 256; |
| 147 | + std::vector<uint8_t> write_buffer; |
| 148 | +}; |
| 149 | + |
| 150 | +} // namespace arduino |
| 151 | + |
| 152 | +using WiFiUDPStream = arduino::UDPStream; |
0 commit comments