Skip to content

Commit fbbcfe1

Browse files
committed
SocketWrapper: implement Server
1 parent 280a5ca commit fbbcfe1

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

libraries/SocketWrapper/SocketWrapper.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include <zephyr/net/socket.h>
24

35
class ZephyrSocketWrapper {
@@ -6,6 +8,7 @@ class ZephyrSocketWrapper {
68

79
public:
810
ZephyrSocketWrapper() : sock_fd(-1) {}
11+
ZephyrSocketWrapper(int sock_fd) : sock_fd(sock_fd) {}
912

1013
~ZephyrSocketWrapper() {
1114
if (sock_fd != -1) {
@@ -101,5 +104,47 @@ class ZephyrSocketWrapper {
101104
}
102105
}
103106

107+
bool bind(uint16_t port) {
108+
struct sockaddr_in addr;
109+
addr.sin_family = AF_INET;
110+
addr.sin_port = htons(port);
111+
addr.sin_addr.s_addr = INADDR_ANY;
112+
113+
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
114+
if (sock_fd < 0) {
115+
return false;
116+
}
117+
118+
if (::bind(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
119+
::close(sock_fd);
120+
sock_fd = -1;
121+
return false;
122+
}
123+
124+
return true;
125+
}
126+
127+
bool listen(int backlog = 5) {
128+
if (sock_fd == -1) {
129+
return false;
130+
}
131+
132+
if (::listen(sock_fd, backlog) < 0) {
133+
::close(sock_fd);
134+
sock_fd = -1;
135+
return false;
136+
}
137+
138+
return true;
139+
}
140+
141+
int accept() {
142+
if (sock_fd == -1) {
143+
return -1;
144+
}
145+
146+
return ::accept(sock_fd, nullptr, nullptr);
147+
}
148+
104149
friend class ZephyrClient;
105150
};

libraries/SocketWrapper/ZephyrClient.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ class ZephyrClient : public arduino::Client, ZephyrSocketWrapper {
77
private:
88
bool _connected = false;
99

10+
protected:
11+
void setSocket(int sock) {
12+
sock_fd = sock;
13+
_connected = true;
14+
}
15+
1016
public:
1117
int connect(const char* host, uint16_t port) override {
1218
auto ret = ZephyrSocketWrapper::connect((char*)host, port);
@@ -68,4 +74,5 @@ class ZephyrClient : public arduino::Client, ZephyrSocketWrapper {
6874
operator bool() {
6975
return sock_fd != -1;
7076
}
77+
friend class ZephyrServer;
7178
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma once
2+
3+
#include "SocketWrapper.h"
4+
#include "ZephyrClient.h"
5+
#include "api/Server.h"
6+
#include "unistd.h"
7+
8+
class ZephyrServer : public arduino::Server, ZephyrSocketWrapper {
9+
private:
10+
int _port;
11+
12+
public:
13+
ZephyrServer() : _port(80){};
14+
ZephyrServer(uint16_t port) : _port(port){};
15+
16+
virtual ~ZephyrServer() {
17+
end();
18+
}
19+
void end() {
20+
ZephyrSocketWrapper::close();
21+
}
22+
void begin(uint16_t port) {
23+
_port = port;
24+
begin();
25+
}
26+
void begin() {
27+
ZephyrSocketWrapper::bind(_port);
28+
ZephyrSocketWrapper::listen(5);
29+
}
30+
uint8_t status() {
31+
return 0;
32+
}
33+
explicit operator bool() {
34+
return sock_fd != -1;
35+
}
36+
37+
ZephyrClient accept(uint8_t* status = nullptr) {
38+
ZephyrClient client;
39+
int sock = ZephyrSocketWrapper::accept();
40+
client.setSocket(sock);
41+
return client;
42+
}
43+
44+
ZephyrClient available(uint8_t* status = nullptr) __attribute__((deprecated("Use accept()."))) {
45+
return accept(status);
46+
}
47+
48+
size_t write(uint8_t c) override {
49+
return write(&c, 1);
50+
}
51+
size_t write(const uint8_t* buffer, size_t size) override {
52+
return send(buffer, size);
53+
}
54+
55+
friend class ZephyrClient;
56+
};

loader/llext_exports.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ FORCE_EXPORT_SYM(send);
7979
FORCE_EXPORT_SYM(recv);
8080
FORCE_EXPORT_SYM(open);
8181
FORCE_EXPORT_SYM(close);
82+
FORCE_EXPORT_SYM(accept);
83+
FORCE_EXPORT_SYM(bind);
84+
FORCE_EXPORT_SYM(listen);
8285
EXPORT_SYMBOL(exit);
8386
FORCE_EXPORT_SYM(inet_pton);
8487
#endif

0 commit comments

Comments
 (0)