forked from chunlinhan/WiFiRM04
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiFiRM04Udp.cpp
163 lines (136 loc) · 2.99 KB
/
WiFiRM04Udp.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
extern "C" {
#include "utility/debug.h"
#include "utility/wifi_spi.h"
}
#include <string.h>
#include "server_drv.h"
#include "wifi_drv.h"
#include "WiFiRM04.h"
#include "WiFiRM04Udp.h"
#include "WiFiRM04Client.h"
#include "WiFiRM04Server.h"
/* Constructor */
WiFiRM04UDP::WiFiRM04UDP() : _sock(NO_SOCKET_AVAIL) {}
/* Start WiFiRM04UDP socket, listening at local port PORT */
uint8_t WiFiRM04UDP::begin(uint16_t port) {
uint8_t sock = WiFiRM04Class::getSocket();
if (sock != NO_SOCKET_AVAIL)
{
ServerDrv::startServer(port, sock, UDP_MODE);
WiFiRM04Class::_server_port[sock] = port;
_sock = sock;
_port = port;
return 1;
}
return 0;
}
/* return number of bytes available in the current packet,
will return zero if parsePacket hasn't been called yet */
int WiFiRM04UDP::available() {
if (_sock != NO_SOCKET_AVAIL)
{
return ServerDrv::availData(_sock);
}
return 0;
}
/* Release any resources being used by this WiFiRM04UDP instance */
void WiFiRM04UDP::stop()
{
if (_sock == NO_SOCKET_AVAIL)
return;
ServerDrv::stopClient(_sock);
_sock = NO_SOCKET_AVAIL;
}
int WiFiRM04UDP::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 WiFiRM04UDP::beginPacket(IPAddress ip, uint16_t port)
{
if (_sock == NO_SOCKET_AVAIL)
_sock = WiFiRM04Class::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startClient(uint32_t(ip), port, _sock, UDP_MODE);
WiFiRM04Class::_state[_sock] = _sock;
return 1;
}
return 0;
}
int WiFiRM04UDP::endPacket()
{
return ServerDrv::sendUdpData(_sock);
}
size_t WiFiRM04UDP::write(uint8_t byte)
{
return write(&byte, 1);
}
size_t WiFiRM04UDP::write(const uint8_t *buffer, size_t size)
{
ServerDrv::insertDataBuf(_sock, buffer, size);
return size;
}
int WiFiRM04UDP::parsePacket()
{
return available();
}
int WiFiRM04UDP::read()
{
uint8_t b;
if (available())
{
ServerDrv::getData(_sock, &b);
return b;
}else{
return -1;
}
}
int WiFiRM04UDP::read(unsigned char* buffer, size_t len)
{
if (available())
{
size_t size = 0;
if (!ServerDrv::getDataBuf(_sock, buffer, &size))
return -1;
// TODO check if the buffer is too smal respect to buffer size
return size;
}else{
return -1;
}
}
int WiFiRM04UDP::peek()
{
uint8_t b;
if (!available())
return -1;
ServerDrv::getData(_sock, &b, 1);
return b;
}
void WiFiRM04UDP::flush()
{
while (available())
read();
}
IPAddress WiFiRM04UDP::remoteIP()
{
uint8_t _remoteIp[4] = {0};
uint8_t _remotePort[2] = {0};
WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
IPAddress ip(_remoteIp);
return ip;
}
uint16_t WiFiRM04UDP::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;
}