Skip to content

Commit 6a6de68

Browse files
committed
SocketWrapper: Client: properly implement connect
1 parent 75e4700 commit 6a6de68

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

libraries/SocketWrapper/SocketWrapper.h

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,51 @@ class ZephyrSocketWrapper {
1414
}
1515

1616
bool connect(const char* host, uint16_t port) {
17+
18+
// Resolve address
19+
struct addrinfo hints;
20+
struct addrinfo *res;
21+
22+
hints.ai_family = AF_INET;
23+
hints.ai_socktype = SOCK_STREAM;
24+
25+
int resolve_attempts = 10;
26+
int ret;
27+
28+
while (resolve_attempts--) {
29+
ret = getaddrinfo(host, String(port).c_str(), &hints, &res);
30+
31+
if (ret == 0) {
32+
break;
33+
}
34+
}
35+
36+
if (ret != 0) {
37+
return false;
38+
}
39+
40+
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
41+
if (sock_fd < 0) {
42+
return false;
43+
}
44+
45+
if (::connect(sock_fd, res->ai_addr, res->ai_addrlen) < 0) {
46+
::close(sock_fd);
47+
sock_fd = -1;
48+
return false;
49+
}
50+
51+
return true;
52+
}
53+
54+
bool connect(IPAddress host, uint16_t port) {
55+
56+
const char* _host = host.toString().c_str();
57+
1758
struct sockaddr_in addr;
1859
addr.sin_family = AF_INET;
1960
addr.sin_port = htons(port);
20-
inet_pton(AF_INET, host, &addr.sin_addr);
61+
inet_pton(AF_INET, _host, &addr.sin_addr);
2162

2263
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2364
if (sock_fd < 0) {

libraries/SocketWrapper/ZephyrClient.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#include "SocketWrapper.h"
22
#include "api/Client.h"
3+
#include "unistd.h"
34
#include "zephyr/sys/printk.h"
45

56
class ZephyrClient : public arduino::Client, ZephyrSocketWrapper {
67
public:
78
int connect(const char* host, uint16_t port) override {
8-
return ZephyrSocketWrapper::connect(host, port);
9+
return ZephyrSocketWrapper::connect((char*)host, port);
910
}
1011
int connect(IPAddress ip, uint16_t port) {
11-
return 0;
12+
return ZephyrSocketWrapper::connect(ip, port);
1213
}
1314
uint8_t connected() override {
1415
return sock_fd != -1;
@@ -48,7 +49,7 @@ class ZephyrClient : public arduino::Client, ZephyrSocketWrapper {
4849
// No-op
4950
}
5051
void stop() override {
51-
// No-op
52+
ZephyrSocketWrapper::close();
5253
}
5354
operator bool() {
5455
return sock_fd != -1;

libraries/SocketWrapper/examples/TestClient/TestClient.ino

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,22 @@ int _main(void)
118118

119119
#define Serial Serial1
120120

121+
121122
ZephyrClient client;
123+
IPAddress addr(93,184,215,14);
122124
// the setup function runs once when you press reset or power the board
123125
void setup() {
124126
// initialize digital pin LED_BUILTIN as an output.
125127
pinMode(LED_BUILTIN, OUTPUT);
126128
Serial.begin(115200);
127129
_main();
128-
delay(20000);
130+
delay(5000);
129131
while (!Serial);
130-
client.connect("93.184.215.14", 80);
131-
client.println("GET / HTTP/1.0");
132+
//auto res = client.connect(addr, 80);
133+
auto res = client.connect("example.com", 80);
134+
Serial.println(res);
135+
res = client.println("GET / HTTP/1.0");
136+
Serial.println(res);
132137
client.println("Host: example.com");
133138
client.println("Connection: close");
134139
client.println();

0 commit comments

Comments
 (0)