|
| 1 | +#include <rcl/rcl.h> |
| 2 | +#include <rcl/error_handling.h> |
| 3 | +#include <rclc/rclc.h> |
| 4 | +#include <rclc/executor.h> |
| 5 | + |
| 6 | +#include <std_msgs/msg/int32.h> |
| 7 | + |
| 8 | +#include <sys/types.h> |
| 9 | +#include <sys/socket.h> |
| 10 | +#include <poll.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <stdint.h> |
| 13 | +#include <unistd.h> |
| 14 | +#include <fcntl.h> |
| 15 | +#include <sys/socket.h> |
| 16 | +#include <arpa/inet.h> |
| 17 | +#include <string.h> |
| 18 | +#include <errno.h> |
| 19 | +#include <netdb.h> |
| 20 | + |
| 21 | +#include <rmw_uros/options.h> |
| 22 | + |
| 23 | +#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Aborting.\n",__LINE__,(int)temp_rc); return 1;}} |
| 24 | +#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Continuing.\n",__LINE__,(int)temp_rc);}} |
| 25 | + |
| 26 | +// --- micro-ROS Transports --- |
| 27 | + |
| 28 | +typedef struct { |
| 29 | + struct pollfd poll_fd; |
| 30 | +} custom_transport_data_t; |
| 31 | + |
| 32 | +bool custom_transport_open(struct uxrCustomTransport * transport){ |
| 33 | + custom_transport_data_t * transport_data = (custom_transport_data_t*) transport->args; |
| 34 | + |
| 35 | + bool rv = false; |
| 36 | + transport_data->poll_fd.fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 37 | + |
| 38 | + if (-1 != transport_data->poll_fd.fd) |
| 39 | + { |
| 40 | + struct addrinfo hints; |
| 41 | + struct addrinfo* result; |
| 42 | + struct addrinfo* ptr; |
| 43 | + |
| 44 | + memset(&hints, 0, sizeof(hints)); |
| 45 | + hints.ai_family = AF_INET; |
| 46 | + hints.ai_socktype = SOCK_DGRAM; |
| 47 | + |
| 48 | + if (0 == getaddrinfo("localhost", "8888", &hints, &result)) |
| 49 | + { |
| 50 | + for (ptr = result; ptr != NULL; ptr = ptr->ai_next) |
| 51 | + { |
| 52 | + if (0 == connect(transport_data->poll_fd.fd, ptr->ai_addr, ptr->ai_addrlen)) |
| 53 | + { |
| 54 | + transport_data->poll_fd.events = POLLIN; |
| 55 | + rv = true; |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + freeaddrinfo(result); |
| 61 | + } |
| 62 | + return rcutils_vsnprintf; |
| 63 | +} |
| 64 | + |
| 65 | +bool custom_transport_close(struct uxrCustomTransport * transport){ |
| 66 | + custom_transport_data_t * transport_data = (custom_transport_data_t*) transport->args; |
| 67 | + return (-1 == transport_data->poll_fd.fd) ? true : (0 == close(transport_data->poll_fd.fd)); |
| 68 | +} |
| 69 | + |
| 70 | +size_t custom_transport_write(struct uxrCustomTransport* transport, const uint8_t * buf, size_t len, uint8_t * errcode){ |
| 71 | + custom_transport_data_t * transport_data = (custom_transport_data_t*) transport->args; |
| 72 | + size_t rv = 0; |
| 73 | + ssize_t bytes_sent = send(transport_data->poll_fd.fd, (void*)buf, len, 0); |
| 74 | + if (-1 != bytes_sent) |
| 75 | + { |
| 76 | + rv = (size_t)bytes_sent; |
| 77 | + *errcode = 0; |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + *errcode = 1; |
| 82 | + } |
| 83 | + return rv; |
| 84 | +} |
| 85 | + |
| 86 | +size_t custom_transport_read(struct uxrCustomTransport* transport, uint8_t* buf, size_t len, int timeout, uint8_t* errcode){ |
| 87 | + custom_transport_data_t * transport_data = (custom_transport_data_t*) transport->args; |
| 88 | + size_t rv = 0; |
| 89 | + int poll_rv = poll(&transport_data->poll_fd, 1, timeout); |
| 90 | + if (0 < poll_rv) |
| 91 | + { |
| 92 | + ssize_t bytes_received = recv(transport_data->poll_fd.fd, (void*)buf, len, 0); |
| 93 | + if (-1 != bytes_received) |
| 94 | + { |
| 95 | + rv = (size_t)bytes_received; |
| 96 | + *errcode = 0; |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + *errcode = 1; |
| 101 | + } |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + *errcode = (0 == poll_rv) ? 0 : 1; |
| 106 | + } |
| 107 | + return rv; |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +// --- micro-ROS App --- |
| 112 | + |
| 113 | +rcl_publisher_t publisher; |
| 114 | +std_msgs__msg__Int32 msg; |
| 115 | + |
| 116 | +void timer_callback(rcl_timer_t * timer, int64_t last_call_time) |
| 117 | +{ |
| 118 | + (void) last_call_time; |
| 119 | + if (timer != NULL) { |
| 120 | + RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL)); |
| 121 | + printf("Sent: %d\n", msg.data); |
| 122 | + msg.data++; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +int main(int argc, char * const argv[]) |
| 127 | +{ |
| 128 | + rcl_allocator_t allocator = rcl_get_default_allocator(); |
| 129 | + rclc_support_t support; |
| 130 | + rcl_init_options_t init_options = rcl_get_zero_initialized_init_options(); |
| 131 | + |
| 132 | + RCCHECK(rcl_init_options_init(&init_options, allocator)); |
| 133 | + rmw_init_options_t* rmw_options = rcl_init_options_get_rmw_init_options(&init_options); |
| 134 | + RCCHECK(rmw_uros_options_set_client_key(0xCAFEBABA, rmw_options)) |
| 135 | + |
| 136 | + custom_transport_data_t custom_transport_data; |
| 137 | + |
| 138 | + RCCHECK(rmw_uros_options_set_custom_transport( |
| 139 | + false, |
| 140 | + (void*) &custom_transport_data, |
| 141 | + custom_transport_open, |
| 142 | + custom_transport_close, |
| 143 | + custom_transport_write, |
| 144 | + custom_transport_read, |
| 145 | + rmw_options |
| 146 | + )) |
| 147 | + |
| 148 | + // create init_options |
| 149 | + RCCHECK(rclc_support_init_with_options(&support, 0, NULL, &init_options, &allocator)); |
| 150 | + |
| 151 | + // create node |
| 152 | + rcl_node_t node = rcl_get_zero_initialized_node(); |
| 153 | + RCCHECK(rclc_node_init_default(&node, "custom_transport_node", "", &support)); |
| 154 | + |
| 155 | + // create publisher |
| 156 | + RCCHECK(rclc_publisher_init_default( |
| 157 | + &publisher, |
| 158 | + &node, |
| 159 | + ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), |
| 160 | + "std_msgs_msg_Int32")); |
| 161 | + |
| 162 | + // create timer, |
| 163 | + rcl_timer_t timer = rcl_get_zero_initialized_timer(); |
| 164 | + const unsigned int timer_timeout = 1000; |
| 165 | + RCCHECK(rclc_timer_init_default( |
| 166 | + &timer, |
| 167 | + &support, |
| 168 | + RCL_MS_TO_NS(timer_timeout), |
| 169 | + timer_callback)); |
| 170 | + |
| 171 | + // create executor |
| 172 | + rclc_executor_t executor = rclc_executor_get_zero_initialized_executor(); |
| 173 | + RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator)); |
| 174 | + RCCHECK(rclc_executor_add_timer(&executor, &timer)); |
| 175 | + |
| 176 | + msg.data = 0; |
| 177 | + |
| 178 | + rclc_executor_spin(&executor); |
| 179 | + |
| 180 | + RCCHECK(rcl_publisher_fini(&publisher, &node)); |
| 181 | + RCCHECK(rcl_node_fini(&node)); |
| 182 | + |
| 183 | + return 0; |
| 184 | +} |
0 commit comments