Skip to content

Commit 27a2225

Browse files
committed
Adding WiFiSocket class
Nina firmware now exposes plain BSD sockets. Using those provides a powerful and straightforward tool for people familiar with BSD sockets and lets them avoid many long standing issues with simplified WiFiServer/WiFiClient interfaces. This commit adds a WiFiSocket class that exposes all the available socket operations in a convenient and safe wrapper. All operations tested on Nano RP2040 Connect with firmware 1.5.
1 parent 06184eb commit 27a2225

File tree

8 files changed

+1465
-0
lines changed

8 files changed

+1465
-0
lines changed

src/WiFi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern "C" {
3636
#include "WiFiSSLClient.h"
3737
#include "WiFiServer.h"
3838
#include "WiFiStorage.h"
39+
#include "WiFiSocket.h"
3940

4041
typedef void(*FeedHostProcessorWatchdogFuncPointer)();
4142

src/WiFiSocket.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
This file is part of the WiFiNINA library.
3+
Copyright (c) 2018 Arduino SA. All rights 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
/*
21+
Created on: Mar 6, 2024
22+
Author: gershnik
23+
*/
24+
25+
#include "WiFiSocket.h"
26+
27+
28+
void WiFiSocket::close() {
29+
if (m_handle != s_invalidHandle) {
30+
SocketDrv::close(m_handle);
31+
m_handle = s_invalidHandle;
32+
}
33+
}
34+
35+
36+
int32_t WiFiSocket::send(const void * buf, uint16_t size) {
37+
auto ret = SocketDrv::send(m_handle, buf, size);
38+
if (ret == 0 && lastError() != 0)
39+
ret = -1;
40+
return ret;
41+
}
42+
43+
int32_t WiFiSocket::recv(void * buf, uint16_t size) {
44+
auto ret = SocketDrv::recv(m_handle, buf, size);
45+
if (ret == 0 && lastError() != 0)
46+
ret = -1;
47+
return ret;
48+
}
49+
50+
int32_t WiFiSocket::sendTo(const void * buf, uint16_t size,
51+
const arduino::IPAddress & ipAddress, uint16_t port) {
52+
auto ret = SocketDrv::sendTo(m_handle, buf, size, ipAddress, port);
53+
if (ret == 0 && lastError() != 0)
54+
ret = -1;
55+
return ret;
56+
}
57+
58+
int32_t WiFiSocket::recvFrom(void * buf, uint16_t size,
59+
arduino::IPAddress & remoteIpAddress, uint16_t & remotePort) {
60+
auto ret = SocketDrv::recvFrom(m_handle, buf, size, remoteIpAddress, remotePort);
61+
if (ret == 0 && lastError() != 0)
62+
ret = -1;
63+
return ret;
64+
}
65+
66+
bool WiFiSocket::setNonBlocking(bool val) {
67+
uint32_t value = val;
68+
auto size = SocketDrv::ioctl(m_handle, uint32_t(IOControl::NonBlockingIO),
69+
&value, sizeof(value));
70+
return size != 0;
71+
}
72+
73+
int32_t WiFiSocket::availableToRead() const {
74+
int32_t value = 0;
75+
auto size = SocketDrv::ioctl(m_handle, uint32_t(IOControl::NRead),
76+
&value, sizeof(value));
77+
if (size == 0)
78+
return -1;
79+
return value;
80+
}
81+
82+
bool WiFiSocket::poll(State & state) const {
83+
auto res = SocketDrv::poll(m_handle);
84+
if (res == SocketDrv::SocketPollFailed)
85+
return false;
86+
state = State(res);
87+
return true;
88+
}

0 commit comments

Comments
 (0)