-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathWiFiUdp.cpp
217 lines (175 loc) · 4.3 KB
/
WiFiUdp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
WiFiUdp.cpp - Library for Arduino WiFi shield.
Copyright (c) 2018 Arduino SA. All rights reserved.
Copyright (c) 2011-2014 Arduino LLC. 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
*/
#include <string.h>
extern "C" {
#include "utility/debug.h"
#include "utility/wifi_spi.h"
}
#include "utility/server_drv.h"
#include "utility/wifi_drv.h"
#include "utility/WiFiSocketBuffer.h"
#include "WiFi.h"
#include "WiFiUdp.h"
#include "WiFiClient.h"
#include "WiFiServer.h"
/* Constructor */
WiFiUDP::WiFiUDP() : _sock(NO_SOCKET_AVAIL), _parsed(0) {}
/* Start WiFiUDP socket, listening at local port PORT */
uint8_t WiFiUDP::begin(uint16_t port) {
if (_sock != NO_SOCKET_AVAIL)
{
stop();
}
uint8_t sock = ServerDrv::getSocket();
if (sock != NO_SOCKET_AVAIL)
{
ServerDrv::startServer(port, sock, UDP_MODE);
_sock = sock;
_port = port;
_parsed = 0;
return 1;
}
return 0;
}
uint8_t WiFiUDP::beginMulticast(IPAddress ip, uint16_t port) {
if (_sock != NO_SOCKET_AVAIL)
{
stop();
}
uint8_t sock = ServerDrv::getSocket();
if (sock != NO_SOCKET_AVAIL)
{
ServerDrv::startServer(ip, port, sock, UDP_MULTICAST_MODE);
_sock = sock;
_port = port;
_parsed = 0;
return 1;
}
return 0;
}
/* return number of bytes available in the current packet,
will return zero if parsePacket hasn't been called yet */
int WiFiUDP::available() {
return _parsed;
}
/* Release any resources being used by this WiFiUDP instance */
void WiFiUDP::stop()
{
if (_sock == NO_SOCKET_AVAIL)
return;
ServerDrv::stopClient(_sock);
WiFiSocketBuffer.close(_sock);
_sock = NO_SOCKET_AVAIL;
}
int WiFiUDP::beginPacket(const char *host, uint16_t port)
{
// Look up the host first
int ret = 0;
IPAddress remote_addr;
if (WiFi.hostByName(host, remote_addr))
{
return beginPacket(remote_addr, port);
}
return ret;
}
int WiFiUDP::beginPacket(IPAddress ip, uint16_t port)
{
if (_sock == NO_SOCKET_AVAIL)
_sock = ServerDrv::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startClient(uint32_t(ip), port, _sock, UDP_MODE);
return 1;
}
return 0;
}
int WiFiUDP::endPacket()
{
return ServerDrv::sendUdpData(_sock);
}
size_t WiFiUDP::write(uint8_t byte)
{
return write(&byte, 1);
}
size_t WiFiUDP::write(const uint8_t *buffer, size_t size)
{
ServerDrv::insertDataBuf(_sock, buffer, size);
return size;
}
int WiFiUDP::parsePacket()
{
while (_parsed--)
{
// discard previously parsed packet data
uint8_t b;
WiFiSocketBuffer.read(_sock, &b, sizeof(b));
}
_parsed = ServerDrv::availData(_sock);
return _parsed;
}
int WiFiUDP::read()
{
if (_parsed < 1)
{
return -1;
}
uint8_t b;
WiFiSocketBuffer.read(_sock, &b, sizeof(b));
_parsed--;
return b;
}
int WiFiUDP::read(unsigned char* buffer, size_t len)
{
if (_parsed < 1)
{
return 0;
}
int result = WiFiSocketBuffer.read(_sock, buffer, len);
if (result > 0)
{
_parsed -= result;
}
return result;
}
int WiFiUDP::peek()
{
if (_parsed < 1)
{
return -1;
}
return WiFiSocketBuffer.peek(_sock);
}
void WiFiUDP::flush()
{
// TODO: a real check to ensure transmission has been completed
}
IPAddress WiFiUDP::remoteIP()
{
uint8_t _remoteIp[4] = {0};
uint8_t _remotePort[2] = {0};
WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
IPAddress ip(_remoteIp);
return ip;
}
uint16_t WiFiUDP::remotePort()
{
uint8_t _remoteIp[4] = {0};
uint8_t _remotePort[2] = {0};
WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
uint16_t port = (_remotePort[0]<<8)+_remotePort[1];
return port;
}