Skip to content

Commit ec4109d

Browse files
facchinmmanchoz
authored andcommitted
Initial: start moving WiFi generic classes into a standalone library
1 parent 4ded56c commit ec4109d

27 files changed

+1248
-432
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SocketWrapper
2+
version=1.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=Wrapper for mbed Socket classes
6+
paragraph=
7+
category=Other
8+
url=http://www.arduino.cc/en/Reference/WiFi
9+
architectures=mbed,ArduinoCore-mbed,mbed_portenta
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
#include "MbedClient.h"
2+
3+
4+
#ifndef SOCKET_TIMEOUT
5+
#define SOCKET_TIMEOUT 1500
6+
#endif
7+
8+
arduino::MbedClient::MbedClient():
9+
_status(false)
10+
{
11+
}
12+
13+
uint8_t arduino::MbedClient::status() {
14+
return _status;
15+
}
16+
17+
void arduino::MbedClient::readSocket() {
18+
while (1) {
19+
event->wait_any(0xFF, 100);
20+
uint8_t data[SOCKET_BUFFER_SIZE];
21+
int ret = NSAPI_ERROR_WOULD_BLOCK;
22+
do {
23+
mutex->lock();
24+
if (rxBuffer.availableForStore() == 0) {
25+
yield();
26+
}
27+
if (sock == nullptr || (closing && borrowed_socket)) {
28+
goto cleanup;
29+
}
30+
ret = sock->recv(data, rxBuffer.availableForStore());
31+
if (ret < 0 && ret != NSAPI_ERROR_WOULD_BLOCK) {
32+
goto cleanup;
33+
}
34+
for (int i = 0; i < ret; i++) {
35+
rxBuffer.store_char(data[i]);
36+
}
37+
_status = true;
38+
mutex->unlock();
39+
} while (ret == NSAPI_ERROR_WOULD_BLOCK || ret > 0);
40+
}
41+
cleanup:
42+
_status = false;
43+
mutex->unlock();
44+
return;
45+
}
46+
47+
void arduino::MbedClient::getStatus() {
48+
event->set(1);
49+
}
50+
51+
void arduino::MbedClient::setSocket(Socket* _sock) {
52+
sock = _sock;
53+
configureSocket(sock);
54+
}
55+
56+
void arduino::MbedClient::configureSocket(Socket* _s) {
57+
_s->set_timeout(0);
58+
_s->set_blocking(false);
59+
60+
if (event == nullptr) {
61+
event = new rtos::EventFlags;
62+
}
63+
if (mutex == nullptr) {
64+
mutex = new rtos::Mutex;
65+
}
66+
mutex->lock();
67+
if (reader_th == nullptr) {
68+
reader_th = new rtos::Thread;
69+
reader_th->start(mbed::callback(this, &MbedClient::readSocket));
70+
}
71+
mutex->unlock();
72+
_s->sigio(mbed::callback(this, &MbedClient::getStatus));
73+
_status = true;
74+
}
75+
76+
int arduino::MbedClient::connect(SocketAddress socketAddress) {
77+
if (sock == nullptr) {
78+
sock = new TCPSocket();
79+
_own_socket = true;
80+
}
81+
if (sock == nullptr) {
82+
return 0;
83+
}
84+
85+
if(static_cast<TCPSocket*>(sock)->open(getNetwork()) != NSAPI_ERROR_OK){
86+
return 0;
87+
}
88+
89+
address = socketAddress;
90+
nsapi_error_t returnCode = static_cast<TCPSocket*>(sock)->connect(socketAddress);
91+
int ret = 0;
92+
93+
switch (returnCode) {
94+
case NSAPI_ERROR_IS_CONNECTED:
95+
case NSAPI_ERROR_OK: {
96+
ret = 1;
97+
break;
98+
}
99+
}
100+
101+
if (ret == 1) {
102+
configureSocket(sock);
103+
_status = true;
104+
} else {
105+
_status = false;
106+
}
107+
108+
return ret;
109+
}
110+
111+
int arduino::MbedClient::connect(IPAddress ip, uint16_t port) {
112+
return connect(SocketHelpers::socketAddressFromIpAddress(ip, port));
113+
}
114+
115+
int arduino::MbedClient::connect(const char *host, uint16_t port) {
116+
SocketAddress socketAddress = SocketAddress();
117+
socketAddress.set_port(port);
118+
getNetwork()->gethostbyname(host, &socketAddress);
119+
return connect(socketAddress);
120+
}
121+
122+
int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
123+
if (sock == nullptr) {
124+
sock = new TLSSocket();
125+
_own_socket = true;
126+
}
127+
if (sock == nullptr) {
128+
return 0;
129+
}
130+
131+
if (beforeConnect) {
132+
beforeConnect();
133+
}
134+
135+
if(static_cast<TLSSocket*>(sock)->open(getNetwork()) != NSAPI_ERROR_OK){
136+
return 0;
137+
}
138+
139+
address = socketAddress;
140+
141+
restart_connect:
142+
nsapi_error_t returnCode = static_cast<TLSSocket*>(sock)->connect(socketAddress);
143+
int ret = 0;
144+
145+
switch (returnCode) {
146+
case NSAPI_ERROR_IS_CONNECTED:
147+
case NSAPI_ERROR_OK: {
148+
ret = 1;
149+
break;
150+
}
151+
case NSAPI_ERROR_IN_PROGRESS:
152+
case NSAPI_ERROR_ALREADY: {
153+
delay(100);
154+
goto restart_connect;
155+
}
156+
}
157+
158+
if (ret == 1) {
159+
configureSocket(sock);
160+
_status = true;
161+
} else {
162+
_status = false;
163+
}
164+
165+
return ret;
166+
}
167+
168+
int arduino::MbedClient::connectSSL(IPAddress ip, uint16_t port) {
169+
return connectSSL(SocketHelpers::socketAddressFromIpAddress(ip, port));
170+
}
171+
172+
int arduino::MbedClient::connectSSL(const char *host, uint16_t port) {
173+
SocketAddress socketAddress = SocketAddress();
174+
socketAddress.set_port(port);
175+
getNetwork()->gethostbyname(host, &socketAddress);
176+
return connectSSL(socketAddress);
177+
}
178+
179+
size_t arduino::MbedClient::write(uint8_t c) {
180+
return write(&c, 1);
181+
}
182+
183+
size_t arduino::MbedClient::write(const uint8_t *buf, size_t size) {
184+
if (sock == nullptr)
185+
return 0;
186+
187+
sock->set_blocking(true);
188+
sock->set_timeout(SOCKET_TIMEOUT);
189+
sock->send(buf, size);
190+
configureSocket(sock);
191+
return size;
192+
}
193+
194+
int arduino::MbedClient::available() {
195+
int ret = rxBuffer.available();
196+
return ret;
197+
}
198+
199+
int arduino::MbedClient::read() {
200+
mutex->lock();
201+
if (!available()) {
202+
return -1;
203+
}
204+
205+
int ret = rxBuffer.read_char();
206+
mutex->unlock();
207+
return ret;
208+
}
209+
210+
int arduino::MbedClient::read(uint8_t *data, size_t len) {
211+
mutex->lock();
212+
int avail = available();
213+
214+
if (!avail) {
215+
return -1;
216+
}
217+
218+
if ((int)len > avail) {
219+
len = avail;
220+
}
221+
222+
for (size_t i = 0; i < len; i++) {
223+
data[i] = rxBuffer.read_char();
224+
}
225+
mutex->unlock();
226+
227+
return len;
228+
}
229+
230+
int arduino::MbedClient::peek() {
231+
return rxBuffer.peek();
232+
}
233+
234+
void arduino::MbedClient::flush() {
235+
236+
}
237+
238+
void arduino::MbedClient::stop() {
239+
if (mutex != nullptr) {
240+
mutex->lock();
241+
}
242+
if (sock != nullptr && borrowed_socket == false) {
243+
if (_own_socket) {
244+
delete sock;
245+
} else {
246+
sock->close();
247+
}
248+
sock = nullptr;
249+
}
250+
closing = true;
251+
if (mutex != nullptr) {
252+
mutex->unlock();
253+
}
254+
if (reader_th != nullptr) {
255+
reader_th->join();
256+
delete reader_th;
257+
reader_th = nullptr;
258+
}
259+
if (event != nullptr) {
260+
delete event;
261+
event = nullptr;
262+
}
263+
if (mutex != nullptr) {
264+
delete mutex;
265+
mutex = nullptr;
266+
}
267+
_status = false;
268+
}
269+
270+
uint8_t arduino::MbedClient::connected() {
271+
return _status;
272+
}
273+
274+
IPAddress arduino::MbedClient::remoteIP() {
275+
return SocketHelpers::ipAddressFromSocketAddress(address);
276+
}
277+
278+
uint16_t arduino::MbedClient::remotePort() {
279+
return 0;
280+
}

0 commit comments

Comments
 (0)