Skip to content

Commit 70fec2a

Browse files
Refactor UDP transports (#739) (#742)
Signed-off-by: Pablo Garrido <[email protected]> (cherry picked from commit 6fe2fd4) Co-authored-by: Pablo Garrido <[email protected]>
1 parent 6d8337b commit 70fec2a

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

src/native_ethernet_transport.cpp

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ bool arduino_native_ethernet_udp_transport_open(
2424
struct uxrCustomTransport *transport) {
2525
struct micro_ros_agent_locator *locator =
2626
(struct micro_ros_agent_locator *)transport->args;
27-
udp_client.begin(locator->port);
28-
return true;
27+
return 1 == udp_client.begin(locator->port);
2928
}
3029

3130
bool arduino_native_ethernet_udp_transport_close(
@@ -41,9 +40,12 @@ size_t arduino_native_ethernet_udp_transport_write(
4140
struct micro_ros_agent_locator *locator =
4241
(struct micro_ros_agent_locator *)transport->args;
4342

44-
udp_client.beginPacket(locator->address, locator->port);
45-
size_t sent = udp_client.write(buf, len);
46-
udp_client.endPacket();
43+
size_t sent = 0;
44+
if(1 == udp_client.beginPacket(locator->address, locator->port)){
45+
sent = udp_client.write(buf, len);
46+
sent = 1 == udp_client.endPacket() ? sent : 0;
47+
}
48+
4749
udp_client.flush();
4850

4951
return sent;
@@ -53,33 +55,18 @@ size_t arduino_native_ethernet_udp_transport_read(
5355
struct uxrCustomTransport *transport, uint8_t *buf, size_t len, int timeout,
5456
uint8_t *errcode) {
5557
(void)errcode;
56-
uint32_t start_time = millis();
5758

58-
while (millis() - start_time < ((uint32_t)timeout) &&
59-
udp_client.parsePacket() == 0) {
59+
int64_t start_time = uxr_millis();
60+
61+
while ((uxr_millis() - start_time) < ((int64_t)timeout) && udp_client.parsePacket() == 0) {
6062
delay(1);
6163
}
6264

63-
size_t readed = udp_client.read(buf, len);
64-
return (readed < 0) ? 0 : readed;
65-
}
66-
67-
#define micro_rollover_useconds 4294967295
68-
69-
int clock_gettime(clockid_t unused, struct timespec *tp) {
70-
(void)unused;
71-
static uint32_t rollover = 0;
72-
static uint32_t last_measure = 0;
73-
74-
uint32_t m = micros();
75-
rollover += (m < last_measure) ? 1 : 0;
76-
77-
uint64_t real_us = (uint64_t)(m + rollover * micro_rollover_useconds);
78-
tp->tv_sec = real_us / 1000000;
79-
tp->tv_nsec = (real_us % 1000000) * 1000;
80-
last_measure = m;
81-
82-
return 0;
65+
size_t available = 0;
66+
if(udp_client.available()){
67+
available = udp_client.read(buf, len);
68+
}
69+
return (available < 0) ? 0 : available;
8370
}
8471
}
8572

0 commit comments

Comments
 (0)