1+ #if defined(TARGET_STM32F4)
2+
3+ #include < Arduino.h>
4+ #include < EthernetUdp.h>
5+ #include < STM32Ethernet.h>
6+ #include < micro_ros_arduino.h>
7+
8+ extern " C" {
9+
10+ #include < stdbool.h>
11+ #include < stdio.h>
12+ #include < sys/time.h>
13+
14+ static EthernetUDP udp_client;
15+
16+ bool arduino_native_ethernet_udp_transport_open (
17+ struct uxrCustomTransport *transport) {
18+ struct micro_ros_agent_locator *locator =
19+ (struct micro_ros_agent_locator *)transport->args ;
20+ udp_client.begin (locator->port );
21+ return true ;
22+ }
23+
24+ bool arduino_native_ethernet_udp_transport_close (
25+ struct uxrCustomTransport *transport) {
26+ udp_client.stop ();
27+ return true ;
28+ }
29+
30+ size_t arduino_native_ethernet_udp_transport_write (
31+ struct uxrCustomTransport *transport, const uint8_t *buf, size_t len,
32+ uint8_t *errcode) {
33+ (void )errcode;
34+ struct micro_ros_agent_locator *locator =
35+ (struct micro_ros_agent_locator *)transport->args ;
36+
37+ udp_client.beginPacket (locator->address , locator->port );
38+ size_t sent = udp_client.write (buf, len);
39+ udp_client.endPacket ();
40+ udp_client.flush ();
41+
42+ return sent;
43+ }
44+
45+ size_t arduino_native_ethernet_udp_transport_read (
46+ struct uxrCustomTransport *transport, uint8_t *buf, size_t len, int timeout,
47+ uint8_t *errcode) {
48+ (void )errcode;
49+ uint32_t start_time = millis ();
50+
51+ while (millis () - start_time < ((uint32_t )timeout) &&
52+ udp_client.parsePacket () == 0 ) {
53+ delay (1 );
54+ }
55+
56+ size_t readed = udp_client.read (buf, len);
57+ return (readed < 0 ) ? 0 : readed;
58+ }
59+
60+ #define micro_rollover_useconds 4294967295
61+
62+ int clock_gettime (clockid_t unused, struct timespec *tp) {
63+ (void )unused;
64+ static uint32_t rollover = 0 ;
65+ static uint32_t last_measure = 0 ;
66+
67+ uint32_t m = micros ();
68+ rollover += (m < last_measure) ? 1 : 0 ;
69+
70+ uint64_t real_us = (uint64_t )(m + rollover * micro_rollover_useconds);
71+ tp->tv_sec = real_us / 1000000 ;
72+ tp->tv_nsec = (real_us % 1000000 ) * 1000 ;
73+ last_measure = m;
74+
75+ return 0 ;
76+ }
77+
78+ }
79+
80+ #endif
0 commit comments