Skip to content

Commit 571a663

Browse files
committed
Add posix socket implementation
1 parent 43a7900 commit 571a663

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <micro_ros_platformio.h>
2+
3+
#include <uxr/client/util/time.h>
4+
#include <uxr/client/profile/transport/custom/custom_transport.h>
5+
6+
#include <sys/types.h>
7+
#include <sys/socket.h>
8+
#include <netdb.h>
9+
#include <stdlib.h>
10+
#include <unistd.h>
11+
#include <poll.h>
12+
#include <string>
13+
14+
extern "C" {
15+
16+
static int fd;
17+
18+
bool platformio_transport_open(struct uxrCustomTransport * transport)
19+
{
20+
const auto * locator = (const struct micro_ros_agent_locator *) transport->args;
21+
22+
struct addrinfo hints;
23+
24+
memset(&hints, 0, sizeof(hints));
25+
26+
hints.ai_family = AF_UNSPEC;
27+
hints.ai_socktype = SOCK_DGRAM;
28+
hints.ai_flags = AI_NUMERICSERV;
29+
hints.ai_protocol = 0;
30+
31+
const auto port_s = std::to_string(locator->port);
32+
struct addrinfo * p_addrs = nullptr;
33+
34+
if(getaddrinfo(locator->address, port_s.c_str(), &hints, &p_addrs) != 0)
35+
return false;
36+
37+
struct addrinfo * p_addr;
38+
39+
for(p_addr = p_addrs; p_addr != nullptr; p_addr = p_addr->ai_next){
40+
if(0 > (fd = socket(p_addr->ai_family, p_addr->ai_socktype, p_addr->ai_protocol)))
41+
continue;
42+
43+
if(0 == connect(fd, p_addr->ai_addr, p_addr->ai_addrlen))
44+
break;
45+
46+
close(fd);
47+
}
48+
49+
freeaddrinfo(p_addrs);
50+
51+
return p_addr != nullptr;
52+
}
53+
54+
bool platformio_transport_close(struct uxrCustomTransport * transport)
55+
{
56+
close(fd);
57+
return true;
58+
}
59+
60+
size_t platformio_transport_write(struct uxrCustomTransport * transport, const uint8_t *buf, size_t len, uint8_t *)
61+
{
62+
auto ret = send(fd, buf, len, 0);
63+
return ret < 0 ? 0 : (size_t)ret;
64+
}
65+
66+
size_t platformio_transport_read(struct uxrCustomTransport * transport, uint8_t *buf, size_t len, int timeout, uint8_t *)
67+
{
68+
struct pollfd pfd;
69+
int ret;
70+
71+
pfd.fd = fd;
72+
pfd.events = POLLIN;
73+
ret = poll(&pfd, 1, timeout);
74+
switch(ret){
75+
case -1:
76+
case 0:
77+
return 0;
78+
default:
79+
ret = recv(fd, buf, len, 0);
80+
return ret < 0 ? 0 : (size_t)ret;
81+
}
82+
}
83+
84+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <string.h>
2+
3+
struct micro_ros_agent_locator {
4+
char * address;
5+
int port;
6+
};
7+
8+
static inline void set_microros_socket_transports(const char * agent_address, uint16_t agent_port){
9+
static struct micro_ros_agent_locator locator;
10+
locator.address = strdup(agent_address);
11+
locator.port = agent_port;
12+
13+
rmw_uros_set_custom_transport(
14+
false,
15+
(void *) &locator,
16+
platformio_transport_open,
17+
platformio_transport_close,
18+
platformio_transport_write,
19+
platformio_transport_read
20+
);
21+
}

0 commit comments

Comments
 (0)