Skip to content

Commit 115fe2f

Browse files
authored
Add Windows support and native PlatformIO test coverage (#24)
* Add Windows port * Fix sd file system * Remove unity lib dep * Fix errno with udp * Fix extraneous def * Fix bug in RingBuffer where 0x00 data is treated as empty. * Address code review issues
1 parent 7113603 commit 115fe2f

37 files changed

Lines changed: 1147 additions & 271 deletions

‎.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
build/
22
.vscode
33
.ipynb_checkpoints/
4-
4+
.pio/

‎.vscode/extensions.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"platformio.platformio-ide"
6+
],
7+
"unwantedRecommendations": [
8+
"ms-vscode.cpptools-extension-pack"
9+
]
10+
}

‎ArduinoCore-Linux/cores/arduino/Arduino.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
#pragma once
2020

21+
#include "Platform.h"
22+
2123
/// do not use exithandler atexit
2224
#ifndef HOST
2325
# define HOST
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
#include "Platform.h"
3+
4+
#if ARDUINO_EMULATOR_WINDOWS
5+
#include "DesktopSocketWindows.h"
6+
#else
7+
#include "DesktopSocketPosix.h"
8+
#endif
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
#include <arpa/inet.h>
4+
// Collide with existing definitions
5+
#undef INADDR_NONE
6+
7+
#include <errno.h>
8+
#include <fcntl.h>
9+
#include <netdb.h>
10+
#include <poll.h>
11+
#include <sys/ioctl.h>
12+
#include <sys/socket.h>
13+
#include <sys/types.h>
14+
#include <unistd.h>
15+
16+
namespace arduino {
17+
18+
using SocketHandle = int;
19+
constexpr SocketHandle INVALID_SOCKET_HANDLE = -1;
20+
using SocketLength = socklen_t;
21+
22+
class SocketRuntime {
23+
public:
24+
SocketRuntime() = default;
25+
~SocketRuntime() = default;
26+
};
27+
28+
inline void ensureSocketRuntime() {
29+
static SocketRuntime runtime;
30+
(void)runtime;
31+
}
32+
33+
inline void closeSocket(SocketHandle socket) { ::close(socket); }
34+
inline int socketLastError() { return errno; }
35+
36+
inline bool socketWouldBlock(int error) {
37+
return error == EWOULDBLOCK || error == EAGAIN;
38+
}
39+
inline bool socketConnectionReset(int error) { return error == ECONNRESET; }
40+
41+
inline bool setSocketNonBlocking(SocketHandle socket, bool enabled = true) {
42+
int flags = fcntl(socket, F_GETFL, 0);
43+
return flags >= 0 && fcntl(socket, F_SETFL, enabled ? flags | O_NONBLOCK
44+
: flags & ~O_NONBLOCK) == 0;
45+
}
46+
47+
} // namespace arduino
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
3+
#ifndef WIN32_LEAN_AND_MEAN
4+
#define WIN32_LEAN_AND_MEAN
5+
#endif
6+
#define INPUT ARDUINO_WINDOWS_INPUT
7+
#include <winsock2.h>
8+
#include <ws2tcpip.h>
9+
#undef INPUT
10+
#undef INADDR_NONE
11+
12+
#ifndef SHUT_RDWR
13+
#define SHUT_RDWR SD_BOTH
14+
#endif
15+
16+
namespace arduino {
17+
18+
using SocketHandle = SOCKET;
19+
constexpr SocketHandle INVALID_SOCKET_HANDLE = INVALID_SOCKET;
20+
using SocketLength = int;
21+
22+
class SocketRuntime {
23+
public:
24+
SocketRuntime() {
25+
WSADATA data;
26+
WSAStartup(MAKEWORD(2, 2), &data);
27+
}
28+
~SocketRuntime() { WSACleanup(); }
29+
};
30+
31+
inline void ensureSocketRuntime() {
32+
static SocketRuntime runtime;
33+
(void)runtime;
34+
}
35+
36+
inline void closeSocket(SocketHandle socket) { closesocket(socket); }
37+
inline int socketLastError() { return WSAGetLastError(); }
38+
inline bool socketWouldBlock(int error) { return error == WSAEWOULDBLOCK; }
39+
inline bool socketConnectionReset(int error) { return error == WSAECONNRESET; }
40+
41+
inline bool setSocketNonBlocking(SocketHandle socket, bool enabled = true) {
42+
u_long mode = enabled ? 1UL : 0UL;
43+
return ioctlsocket(socket, FIONBIO, &mode) == 0;
44+
}
45+
46+
} // namespace arduino

‎ArduinoCore-Linux/cores/arduino/Ethernet.h‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
*/
2020
#pragma once
2121

22-
#include <arpa/inet.h> // for inet_pton
23-
#include <netdb.h> // for gethostbyname, struct hostent
24-
#include <unistd.h> // for close
22+
#include "DesktopSocket.h"
23+
#include <cstring>
2524
#include <memory> // This is the include you need
2625

2726
#include "ArduinoLogger.h"
@@ -351,12 +350,17 @@ class EthernetClient : public Client {
351350
serv_addr4.sin_port = htons(port);
352351
if (::inet_pton(AF_INET, address, &serv_addr4.sin_addr) <= 0) {
353352
// Not an IP, try to resolve hostname
354-
struct hostent* he = ::gethostbyname(address);
355-
if (he == nullptr || he->h_addr_list[0] == nullptr) {
353+
addrinfo hints{};
354+
hints.ai_family = AF_INET;
355+
addrinfo* info = nullptr;
356+
int rc = ::getaddrinfo(address, nullptr, &hints, &info);
357+
if (rc != 0 || info == nullptr) {
356358
Logger.error(WIFICLIENT, "Hostname resolution failed");
357359
serv_addr4.sin_addr.s_addr = 0;
358360
} else {
359-
memcpy(&serv_addr4.sin_addr, he->h_addr_list[0], he->h_length);
361+
auto* resolved = reinterpret_cast<sockaddr_in*>(info->ai_addr);
362+
memcpy(&serv_addr4.sin_addr, &resolved->sin_addr, sizeof(serv_addr4.sin_addr));
363+
freeaddrinfo(info);
360364
}
361365
}
362366
return IPAddress(serv_addr4.sin_addr.s_addr);

‎ArduinoCore-Linux/cores/arduino/EthernetServer.h‎

Lines changed: 6 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -19,194 +19,10 @@
1919
*/
2020
#pragma once
2121

22-
#include <poll.h>
23-
#include <unistd.h>
22+
#include "Platform.h"
2423

25-
#include "Ethernet.h"
26-
#include "api/Server.h"
27-
#include "SignalHandler.h"
28-
29-
namespace arduino {
30-
31-
/**
32-
* A minimal ethernet server
33-
*/
34-
35-
class EthernetServer : public Server {
36-
private:
37-
uint16_t _port;
38-
int server_fd = 0;
39-
struct sockaddr_in server_addr;
40-
int _status = wl_status_t::WL_DISCONNECTED;
41-
bool is_blocking = false;
42-
bool _noDelay = false;
43-
44-
static std::vector<EthernetServer*>& active_servers() {
45-
static std::vector<EthernetServer*> servers;
46-
return servers;
47-
}
48-
static void cleanupAll(int sig) {
49-
for (auto* server : active_servers()) {
50-
if (server && server->server_fd > 0) {
51-
shutdown(server->server_fd, SHUT_RDWR);
52-
close(server->server_fd);
53-
server->server_fd = 0;
54-
}
55-
}
56-
}
57-
58-
public:
59-
EthernetServer(int port = 80) {
60-
_port = port;
61-
// Register signal handler only once
62-
static bool signal_registered = false;
63-
if (!signal_registered) {
64-
SignalHandler::registerHandler(SIGINT, cleanupAll);
65-
SignalHandler::registerHandler(SIGTERM, cleanupAll);
66-
signal_registered = true;
67-
}
68-
}
69-
70-
~EthernetServer() {
71-
stop();
72-
// Remove from active servers list
73-
auto& servers = active_servers();
74-
auto it = std::find(servers.begin(), servers.end(), this);
75-
if (it != servers.end()) {
76-
servers.erase(it);
77-
}
78-
}
79-
void begin() { begin(_port); }
80-
void begin(int port) { begin_(port); }
81-
82-
void stop() {
83-
if (server_fd > 0) {
84-
// Set SO_LINGER to force immediate close
85-
struct linger linger_opt = {1, 0};
86-
setsockopt(server_fd, SOL_SOCKET, SO_LINGER, &linger_opt,
87-
sizeof(linger_opt));
88-
89-
shutdown(server_fd, SHUT_RDWR);
90-
close(server_fd);
91-
}
92-
server_fd = 0;
93-
_status = wl_status_t::WL_DISCONNECTED;
94-
}
95-
WiFiClient accept() { return available_(); }
96-
WiFiClient available(uint8_t* status = NULL) { return available_(); }
97-
virtual size_t write(uint8_t ch) { return write(&ch, 1); }
98-
virtual size_t write(const uint8_t* buf, size_t size) {
99-
int rc = ::write(server_fd, buf, size);
100-
if (rc < 0) {
101-
rc = 0;
102-
}
103-
return rc;
104-
}
105-
int status() { return _status; }
106-
107-
// The following are for compatibility with ESP32 WiFiServer
108-
// get/setNoDelay maintain the state variable, but otherwise
109-
// have no effect on the code.
110-
void setNoDelay(bool nodelay) { _noDelay = nodelay; }
111-
bool getNoDelay() { return _noDelay; }
112-
bool hasClient() {
113-
if (server_fd <= 0) return false;
114-
struct pollfd pfd;
115-
pfd.fd = server_fd;
116-
pfd.events = POLLIN;
117-
return ::poll(&pfd, 1, 0) > 0 && (pfd.revents & POLLIN);
118-
}
119-
120-
using Print::write;
121-
122-
protected:
123-
124-
bool begin_(int port = 0) {
125-
if (port > 0) _port = port;
126-
_status = wl_status_t::WL_DISCONNECTED;
127-
128-
// create server socket
129-
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
130-
// error("socket failed");
131-
_status = wl_status_t::WL_CONNECT_FAILED;
132-
return false;
133-
}
134-
135-
// Reuse address after restart
136-
int iSetOption = 1;
137-
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&iSetOption,
138-
sizeof(iSetOption));
139-
140-
// Set SO_REUSEPORT for better port reuse
141-
setsockopt(server_fd, SOL_SOCKET, SO_REUSEPORT, (char*)&iSetOption,
142-
sizeof(iSetOption));
143-
144-
// config socket
145-
server_addr.sin_family = AF_INET;
146-
server_addr.sin_addr.s_addr = INADDR_ANY;
147-
server_addr.sin_port = htons(_port);
148-
149-
// bind socket to port
150-
while (::bind(server_fd, (struct sockaddr*)&server_addr,
151-
sizeof(server_addr)) < 0) {
152-
// error("bind failed");
153-
//_status = wl_status_t::WL_CONNECT_FAILED;
154-
Logger.error("bind failed");
155-
// return false;
156-
delay(1000);
157-
}
158-
159-
// listen for connections
160-
if (::listen(server_fd, 10) < 0) {
161-
// error("listen failed");
162-
_status = wl_status_t::WL_CONNECT_FAILED;
163-
Logger.error("listen failed");
164-
return false;
165-
}
166-
167-
_noDelay = false;
168-
169-
// Add to active servers list for signal handling
170-
active_servers().push_back(this);
171-
_status = wl_status_t::WL_CONNECTED;
172-
return true;
173-
}
174-
175-
void setBlocking(bool flag) { is_blocking = flag; }
176-
177-
EthernetClient available_() {
178-
struct sockaddr_in client_addr;
179-
socklen_t client_addr_len = sizeof(client_addr);
180-
int client_fd;
181-
182-
if (_status == wl_status_t::WL_CONNECT_FAILED) {
183-
begin(_port);
184-
}
185-
186-
struct pollfd pfd;
187-
pfd.fd = server_fd;
188-
pfd.events = POLLIN;
189-
int poll_rc = ::poll(&pfd, 1, 200);
190-
191-
// non blocking check if we have any request to accept
192-
if (!is_blocking) {
193-
if (poll_rc <= 0 || !(pfd.revents & POLLIN)) {
194-
EthernetClient result(nullptr);
195-
return result;
196-
}
197-
}
198-
199-
// accept client connection (blocking call)
200-
if ((client_fd = ::accept(server_fd, (struct sockaddr*)&client_addr,
201-
&client_addr_len)) < 0) {
202-
EthernetClient result(nullptr);
203-
Logger.error("accept failed");
204-
return result;
205-
}
206-
std::shared_ptr<SocketImpl> sock_impl = std::make_shared<SocketImpl>(client_fd, (struct sockaddr_in*)&client_addr);
207-
EthernetClient result{sock_impl};
208-
return result;
209-
}
210-
};
211-
212-
} // namespace arduino
24+
#if ARDUINO_EMULATOR_WINDOWS
25+
#include "EthernetServerWindows.h"
26+
#else
27+
#include "EthernetServerPosix.h"
28+
#endif

0 commit comments

Comments
 (0)