-
Notifications
You must be signed in to change notification settings - Fork 28
/
WiFiRM04Client.cpp
200 lines (161 loc) · 3.65 KB
/
WiFiRM04Client.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
extern "C" {
#include "utility/wl_definitions.h"
#include "utility/wl_types.h"
#include "socket.h"
#include "string.h"
#include "utility/debug.h"
}
#include "WiFiRM04.h"
#include "WiFiRM04Client.h"
#include "WiFiRM04Server.h"
#include "server_drv.h"
uint16_t WiFiRM04Client::_srcport = 1024;
WiFiRM04Client::WiFiRM04Client() : _sock(MAX_SOCK_NUM) {
}
WiFiRM04Client::WiFiRM04Client(uint8_t sock) : _sock(sock) {
if(sock >= MAX_SOCK_NUM) {
_sock = 255;
}
}
int WiFiRM04Client::connect(const char* host, uint16_t port) {
if(_sock >= MAX_SOCK_NUM)
_sock = getFirstSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startClient(host, port, _sock);
WiFiRM04Class::_state[_sock] = _sock;
unsigned long start = millis();
// wait 4 second for the connection to close
while (!connected() && millis() - start < 10000)
delay(1);
if (!connected())
{
return 0;
}
}else{
Serial.println("No Socket available");
return 0;
}
return 1;
}
int WiFiRM04Client::connect(IPAddress ip, uint16_t port) {
if(_sock == MAX_SOCK_NUM)
_sock = getFirstSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startClient(uint32_t(ip), port, _sock);
WiFiRM04Class::_state[_sock] = _sock;
unsigned long start = millis();
// wait 4 second for the connection to close
while (!connected() && millis() - start < 10000)
delay(1);
if (!connected())
{
return 0;
}
}else{
Serial.println("No Socket available");
return 0;
}
return 1;
}
size_t WiFiRM04Client::write(uint8_t b) {
return write(&b, 1);
}
size_t WiFiRM04Client::write(const uint8_t *buf, size_t size) {
if (_sock >= MAX_SOCK_NUM)
{
setWriteError();
return 0;
}
if (size==0)
{
setWriteError();
return 0;
}
if (!ServerDrv::sendData(_sock, buf, size))
{
setWriteError();
return 0;
}
if (!ServerDrv::checkDataSent(_sock))
{
setWriteError();
return 0;
}
return size;
}
int WiFiRM04Client::available() {
if (_sock != 255)
{
return ServerDrv::availData(_sock);
}
return 0;
}
int WiFiRM04Client::read() {
uint8_t b;
if (!available())
return -1;
ServerDrv::getData(_sock, &b);
return b;
}
int WiFiRM04Client::read(uint8_t* buf, size_t size) {
if (!ServerDrv::getDataBuf(_sock, buf, &size))
return -1;
return 0;
}
int WiFiRM04Client::peek() {
uint8_t b;
if (!available())
return -1;
ServerDrv::getData(_sock, &b, 1);
return b;
}
void WiFiRM04Client::flush() {
while (available())
read();
}
void WiFiRM04Client::stop() {
if (_sock == 255)
return;
ServerDrv::stopClient(_sock);
WiFiRM04Class::_state[_sock] = NA_STATE;
int count = 0;
// wait maximum 5 secs for the connection to close
// Don't wait since RM04 can't disconnect the connetion by itself
//while (status() != CLOSED && ++count < 50)
// delay(100);
_sock = 255;
}
uint8_t WiFiRM04Client::connected() {
if (_sock == 255) {
return 0;
} else {
uint8_t s = status();
return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 ||
s == FIN_WAIT_2 || s == TIME_WAIT ||
s == SYN_SENT || s== SYN_RCVD ||
(s == CLOSE_WAIT));
}
}
uint8_t WiFiRM04Client::status() {
if (_sock == 255) {
return CLOSED;
} else {
return ServerDrv::getClientState(_sock);
}
}
WiFiRM04Client::operator bool() {
return _sock != 255;
}
// Private Methods
uint8_t WiFiRM04Client::getFirstSocket()
{
for (int i = 0; i < MAX_SOCK_NUM; i++) {
if (WiFiRM04Class::_state[i] == NA_STATE)
{
return i;
}
}
return SOCK_NOT_AVAIL;
}