arduino-emulator
Loading...
Searching...
No Matches
WiFiUdpStream.h
1/*
2 WiFiUdpStream.h
3 Copyright (c) 2025 Phil Schatzmann. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#pragma once
21#include <string.h>
22#include <thread>
23#include "ArduinoLogger.h"
24#include "WiFiUdp.h"
25#include "api/ArduinoAPI.h"
26#include "api/IPAddress.h"
27
28namespace arduino {
29
54class WiFiUDPStream : public WiFiUDP {
55 public:
56 // unknown target -> we wait for a hallo until we get one
57 WiFiUDPStream() = default;
58
59 // known target
61 setTarget(targetAdress, port);
62 }
63
64 void flush() {
65 WiFiUDP::flush();
66 endPacket();
67 if (!targetDefined()) {
68 target_adress = remoteIP();
69 }
70 beginPacket(target_adress, port);
71 }
72
73 void stop() {
74 if (active) {
75 endPacket();
76 WiFiUDP::stop();
77 active = false;
78 }
79 }
80
81 bool targetDefined() { return ((uint32_t)target_adress) != 0; }
82
83 void setTarget(IPAddress targetAdress, int port) {
84 this->target_adress = targetAdress;
85 this->port = port;
86 beginPacket(targetAdress, port);
87 }
88
89 size_t readBytes(uint8_t* values, size_t len) {
90 if (this->available() == 0) {
91 // we need to receive the next packet
92 this->parsePacket();
93 Logger.debug("parsePacket");
94 } else {
95 Logger.debug("no parsePacket()");
96 }
97 size_t result = read(values, len);
98
99 char msg[50];
100 sprintf(msg, "->len %ld", result);
101 Logger.debug(msg);
102
103 return result;
104 }
105
106 bool isActive() { return active; }
107
108 protected:
109 bool active = false;
110 IPAddress target_adress{0, 0, 0, 0};
111 int port = 0;
112};
113
114// Define a global function which will be used to start a thread
115
116} // namespace arduino
Definition DMAPool.h:103
Definition UDP.h:30
Definition IPAddress.h:43
UDP Stream implementation for single-target communication.
Definition WiFiUdpStream.h:54
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31