Skip to content

Commit db7dae0

Browse files
committed
initial: add SocketWrapper library
1 parent 0c1e4f6 commit db7dae0

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <zephyr/net/socket.h>
2+
3+
class ZephyrSocketWrapper {
4+
protected:
5+
int sock_fd;
6+
7+
public:
8+
ZephyrSocketWrapper() : sock_fd(-1) {}
9+
10+
~ZephyrSocketWrapper() {
11+
if (sock_fd != -1) {
12+
::close(sock_fd);
13+
}
14+
}
15+
16+
bool connect(const char* host, uint16_t port) {
17+
struct sockaddr_in addr;
18+
addr.sin_family = AF_INET;
19+
addr.sin_port = htons(port);
20+
inet_pton(AF_INET, host, &addr.sin_addr);
21+
22+
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
23+
if (sock_fd < 0) {
24+
return false;
25+
}
26+
27+
if (::connect(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
28+
::close(sock_fd);
29+
sock_fd = -1;
30+
return false;
31+
}
32+
33+
return true;
34+
}
35+
36+
int available() {
37+
int count = 0;
38+
zsock_ioctl(sock_fd, ZFD_IOCTL_FIONREAD, &count);
39+
return count;
40+
}
41+
42+
int recv(uint8_t* buffer, size_t size) {
43+
if (sock_fd == -1) {
44+
return -1;
45+
}
46+
return ::recv(sock_fd, buffer, size, MSG_DONTWAIT);
47+
}
48+
49+
int send(const uint8_t* buffer, size_t size) {
50+
if (sock_fd == -1) {
51+
return -1;
52+
}
53+
return ::send(sock_fd, buffer, size, 0);
54+
}
55+
56+
void close() {
57+
if (sock_fd != -1) {
58+
::close(sock_fd);
59+
sock_fd = -1;
60+
}
61+
}
62+
63+
friend class ZephyrClient;
64+
};
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "SocketWrapper.h"
2+
#include "api/Client.h"
3+
#include "zephyr/sys/printk.h"
4+
5+
class ZephyrClient : public arduino::Client, ZephyrSocketWrapper {
6+
public:
7+
int connect(const char* host, uint16_t port) override {
8+
return ZephyrSocketWrapper::connect(host, port);
9+
}
10+
int connect(IPAddress ip, uint16_t port) {
11+
return 0;
12+
}
13+
uint8_t connected() override {
14+
return sock_fd != -1;
15+
}
16+
int available() override {
17+
return ZephyrSocketWrapper::available();
18+
}
19+
int read() override {
20+
uint8_t c;
21+
read(&c, 1);
22+
return c;
23+
}
24+
int read(uint8_t* buffer, size_t size) override {
25+
auto received = recv(buffer, size);
26+
27+
if (received == 0) {
28+
return 0;
29+
} else if (received < 0) {
30+
if (errno == EAGAIN || errno == EWOULDBLOCK) {
31+
return 0;
32+
} else {
33+
return 0;
34+
}
35+
}
36+
return received;
37+
}
38+
size_t write(uint8_t c) override {
39+
return write(&c, 1);
40+
}
41+
size_t write(const uint8_t* buffer, size_t size) override {
42+
return send(buffer, size);
43+
}
44+
void flush() override {
45+
// No-op
46+
}
47+
int peek() override {
48+
// No-op
49+
}
50+
void stop() override {
51+
// No-op
52+
}
53+
operator bool() {
54+
return sock_fd != -1;
55+
}
56+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
Blink
3+
4+
Turns an LED on for one second, then off for one second, repeatedly.
5+
6+
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
7+
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
8+
the correct LED pin independent of which board is used.
9+
If you want to know what pin the on-board LED is connected to on your Arduino
10+
model, check the Technical Specs of your board at:
11+
https://www.arduino.cc/en/Main/Products
12+
13+
modified 8 May 2014
14+
by Scott Fitzgerald
15+
modified 2 Sep 2016
16+
by Arturo Guadalupi
17+
modified 8 Sep 2016
18+
by Colby Newman
19+
20+
This example code is in the public domain.
21+
22+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
23+
*/
24+
#include <zephyr/kernel.h>
25+
#include <zephyr/linker/sections.h>
26+
#include <errno.h>
27+
#include <stdio.h>
28+
29+
#include <zephyr/net/net_if.h>
30+
#include <zephyr/net/net_core.h>
31+
#include <zephyr/net/net_context.h>
32+
#include <zephyr/net/net_mgmt.h>
33+
34+
#include <ZephyrClient.h>
35+
36+
#define LOG_INF printk
37+
38+
#define DHCP_OPTION_NTP (42)
39+
40+
static uint8_t ntp_server[4];
41+
42+
static struct net_mgmt_event_callback mgmt_cb;
43+
44+
static struct net_dhcpv4_option_callback dhcp_cb;
45+
46+
static void start_dhcpv4_client(struct net_if *iface, void *user_data)
47+
{
48+
ARG_UNUSED(user_data);
49+
50+
LOG_INF("Start on %s: index=%d", net_if_get_device(iface)->name,
51+
net_if_get_by_iface(iface));
52+
net_dhcpv4_start(iface);
53+
}
54+
55+
static void handler(struct net_mgmt_event_callback *cb,
56+
uint32_t mgmt_event,
57+
struct net_if *iface)
58+
{
59+
int i = 0;
60+
61+
if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
62+
return;
63+
}
64+
65+
for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
66+
char buf[NET_IPV4_ADDR_LEN];
67+
68+
if (iface->config.ip.ipv4->unicast[i].ipv4.addr_type !=
69+
NET_ADDR_DHCP) {
70+
continue;
71+
}
72+
73+
LOG_INF(" Address[%d]: %s", net_if_get_by_iface(iface),
74+
net_addr_ntop(AF_INET,
75+
&iface->config.ip.ipv4->unicast[i].ipv4.address.in_addr,
76+
buf, sizeof(buf)));
77+
LOG_INF(" Subnet[%d]: %s", net_if_get_by_iface(iface),
78+
net_addr_ntop(AF_INET,
79+
&iface->config.ip.ipv4->unicast[i].netmask,
80+
buf, sizeof(buf)));
81+
LOG_INF(" Router[%d]: %s", net_if_get_by_iface(iface),
82+
net_addr_ntop(AF_INET,
83+
&iface->config.ip.ipv4->gw,
84+
buf, sizeof(buf)));
85+
LOG_INF("Lease time[%d]: %u seconds", net_if_get_by_iface(iface),
86+
iface->config.dhcpv4.lease_time);
87+
}
88+
}
89+
90+
static void option_handler(struct net_dhcpv4_option_callback *cb,
91+
size_t length,
92+
enum net_dhcpv4_msg_type msg_type,
93+
struct net_if *iface)
94+
{
95+
char buf[NET_IPV4_ADDR_LEN];
96+
97+
LOG_INF("DHCP Option %d: %s", cb->option,
98+
net_addr_ntop(AF_INET, cb->data, buf, sizeof(buf)));
99+
}
100+
101+
int _main(void)
102+
{
103+
LOG_INF("Run dhcpv4 client");
104+
105+
//net_mgmt_init_event_callback(&mgmt_cb, handler,
106+
// NET_EVENT_IPV4_ADDR_ADD);
107+
//net_mgmt_add_event_callback(&mgmt_cb);
108+
109+
net_dhcpv4_init_option_callback(&dhcp_cb, option_handler,
110+
DHCP_OPTION_NTP, ntp_server,
111+
sizeof(ntp_server));
112+
113+
net_dhcpv4_add_option_callback(&dhcp_cb);
114+
115+
net_if_foreach(start_dhcpv4_client, NULL);
116+
return 0;
117+
}
118+
119+
#define Serial Serial1
120+
121+
ZephyrClient client;
122+
// the setup function runs once when you press reset or power the board
123+
void setup() {
124+
// initialize digital pin LED_BUILTIN as an output.
125+
pinMode(LED_BUILTIN, OUTPUT);
126+
Serial.begin(115200);
127+
_main();
128+
delay(20000);
129+
while (!Serial);
130+
client.connect("93.184.215.14", 80);
131+
client.println("GET / HTTP/1.0");
132+
client.println("Host: example.com");
133+
client.println("Connection: close");
134+
client.println();
135+
}
136+
137+
// the loop function runs over and over again forever
138+
void loop() {
139+
while (client.available()) {
140+
Serial.write(client.read());
141+
}
142+
}

loader/llext_exports.c

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ FORCE_EXPORT_SYM(recv);
8080
FORCE_EXPORT_SYM(open);
8181
FORCE_EXPORT_SYM(close);
8282
EXPORT_SYMBOL(exit);
83+
FORCE_EXPORT_SYM(inet_pton);
8384
#endif
8485

8586
#if defined(CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT)

0 commit comments

Comments
 (0)