Skip to content

Commit 5a9654f

Browse files
Add ping with defined transport (#159) (#162)
* Add ping with custom transport * Add mising , * Fix ident * Uncrustify (cherry picked from commit 02373a7) Co-authored-by: Antonio Cuadros <49162117+Acuadros95@users.noreply.github.com>
1 parent 8594729 commit 5a9654f

6 files changed

Lines changed: 104 additions & 12 deletions

File tree

rmw_microxrcedds_c/include/rmw_microros/custom_transport.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extern "C"
3737
*/
3838

3939
/**
40-
* \brief Check if micro-ROS Agent answers to micro-ROS client
40+
* \brief Sets micro-ROS default custom transport.
4141
*
4242
* \param[in] framing Enable XRCE framing.
4343
* \param[in] args Arguments for open function.
@@ -56,6 +56,28 @@ rmw_ret_t rmw_uros_set_custom_transport(
5656
write_custom_func write_cb,
5757
read_custom_func read_cb);
5858

59+
/**
60+
* \brief Fills rmw implementation-specific options with the given custom transport.
61+
*
62+
* \param[in] framing Enable XRCE framing.
63+
* \param[in] args Arguments for open function.
64+
* \param[in] open_cb Open transport callback.
65+
* \param[in] close_cb Close transport callback.
66+
* \param[in] write_cb Write transport callback.
67+
* \param[in] read_cb Read transport callback.
68+
* \param[in,out] rmw_options Updated options with updated transport.
69+
* \return RMW_RET_OK If arguments were valid and set in rmw_init_options.
70+
* \return RMW_RET_INVALID_ARGUMENT If rmw_init_options is not valid or unexpected arguments.
71+
*/
72+
rmw_ret_t rmw_uros_options_set_custom_transport(
73+
bool framing,
74+
void * args,
75+
open_custom_func open_cb,
76+
close_custom_func close_cb,
77+
write_custom_func write_cb,
78+
read_custom_func read_cb,
79+
rmw_init_options_t * rmw_options);
80+
5981
/** @}*/
6082

6183
#if defined(__cplusplus)

rmw_microxrcedds_c/include/rmw_microros/ping.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ rmw_ret_t rmw_uros_ping_agent(
5252
const int timeout_ms,
5353
const uint8_t attempts);
5454

55+
/**
56+
* \brief Check if micro-ROS Agent is up and running using the transport set on the given rmw options.
57+
* This function can be called even when the micro-ROS context has not yet been initialized.
58+
* The transport will be initialized and closed once during the ping process.
59+
* \param[in] timeout_ms Timeout in ms (per attempt).
60+
* \param[in] attempts Number of tries before considering the ping as failed.
61+
* \param[in] rmw_options rmw options with populated transport parameters.
62+
* \return RMW_RET_OK If micro-ROS Agent is available.
63+
* \return RMW_RET_ERROR If micro-ROS Agent is not available.
64+
*/
65+
rmw_ret_t rmw_uros_ping_agent_options(
66+
const int timeout_ms,
67+
const uint8_t attempts,
68+
rmw_init_options_t * rmw_options);
69+
5570
/** @}*/
5671

5772
#if defined(__cplusplus)

rmw_microxrcedds_c/src/rmw_microros/custom_transport.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,32 @@ rmw_ret_t rmw_uros_set_custom_transport(
4747
}
4848
return RMW_RET_OK;
4949
}
50+
51+
rmw_ret_t rmw_uros_options_set_custom_transport(
52+
bool framing,
53+
void * args,
54+
open_custom_func open_cb,
55+
close_custom_func close_cb,
56+
write_custom_func write_cb,
57+
read_custom_func read_cb,
58+
rmw_init_options_t * rmw_options)
59+
{
60+
if (NULL != open_cb &&
61+
NULL != close_cb &&
62+
NULL != write_cb &&
63+
NULL != read_cb &&
64+
NULL != rmw_options)
65+
{
66+
rmw_options->impl->transport_params.framing = framing;
67+
rmw_options->impl->transport_params.args = args;
68+
rmw_options->impl->transport_params.open_cb = open_cb;
69+
rmw_options->impl->transport_params.close_cb = close_cb;
70+
rmw_options->impl->transport_params.write_cb = write_cb;
71+
rmw_options->impl->transport_params.read_cb = read_cb;
72+
} else {
73+
RMW_SET_ERROR_MSG("Uninitialised arguments.");
74+
return RMW_RET_INVALID_ARGUMENT;
75+
}
76+
77+
return RMW_RET_OK;
78+
}

rmw_microxrcedds_c/src/rmw_microros/ping.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,35 @@ rmw_ret_t rmw_uros_ping_agent(
6767

6868
return success ? RMW_RET_OK : RMW_RET_ERROR;
6969
}
70+
71+
rmw_ret_t rmw_uros_ping_agent_options(
72+
const int timeout_ms,
73+
const uint8_t attempts,
74+
rmw_init_options_t * rmw_options)
75+
{
76+
bool success = false;
77+
78+
#ifdef RMW_UXRCE_TRANSPORT_SERIAL
79+
uxrSerialTransport transport;
80+
#elif defined(RMW_UXRCE_TRANSPORT_UDP)
81+
uxrUDPTransport transport;
82+
#elif defined(RMW_UXRCE_TRANSPORT_CUSTOM)
83+
uxrCustomTransport transport;
84+
transport.framing = rmw_options->impl->transport_params.framing;
85+
transport.args = rmw_options->impl->transport_params.args;
86+
transport.open = rmw_options->impl->transport_params.open_cb;
87+
transport.close = rmw_options->impl->transport_params.close_cb;
88+
transport.write = rmw_options->impl->transport_params.write_cb;
89+
transport.read = rmw_options->impl->transport_params.read_cb;
90+
#endif /* ifdef RMW_UXRCE_TRANSPORT_SERIAL */
91+
rmw_ret_t ret = rmw_uxrce_transport_init(NULL, rmw_options->impl, (void *)&transport);
92+
93+
if (RMW_RET_OK != ret) {
94+
return ret;
95+
}
96+
97+
success = uxr_ping_agent_attempts(&transport.comm, timeout_ms, attempts);
98+
CLOSE_TRANSPORT(&transport);
99+
100+
return success ? RMW_RET_OK : RMW_RET_ERROR;
101+
}

rmw_microxrcedds_c/src/rmw_uxrce_transports.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ rmw_ret_t rmw_uxrce_transport_init(
2424
void * transport)
2525
{
2626
#ifdef RMW_UXRCE_TRANSPORT_SERIAL
27-
const char * serial_device = (NULL == context_impl) ?
27+
const char * serial_device = (NULL == init_options_impl) ?
2828
RMW_UXRCE_DEFAULT_SERIAL_DEVICE :
2929
init_options_impl->transport_params.serial_device;
3030

@@ -92,9 +92,6 @@ rmw_ret_t rmw_uxrce_transport_init(
9292
RMW_SET_ERROR_MSG("rmw_transport_init SERIAL: invalid serial device file descriptor");
9393
return RMW_RET_ERROR;
9494
}
95-
printf(
96-
"micro-ROS transport: connected using serial mode, dev: '%s'\n",
97-
serial_device);
9895
#elif defined(RMW_UXRCE_TRANSPORT_UDP)
9996
#ifdef RMW_UXRCE_TRANSPORT_IPV4
10097
uxrIpProtocol ip_protocol = UXR_IPv4;
@@ -105,25 +102,22 @@ rmw_ret_t rmw_uxrce_transport_init(
105102
uxrUDPTransport * udp_transport = (NULL == context_impl) ?
106103
(uxrUDPTransport *)transport :
107104
&context_impl->transport;
108-
const char * agent_ip = (NULL == context_impl) ?
105+
const char * agent_ip = (NULL == init_options_impl) ?
109106
RMW_UXRCE_DEFAULT_UDP_IP :
110107
init_options_impl->transport_params.agent_address;
111-
const char * agent_port = (NULL == context_impl) ?
108+
const char * agent_port = (NULL == init_options_impl) ?
112109
RMW_UXRCE_DEFAULT_UDP_PORT :
113110
init_options_impl->transport_params.agent_port;
114111

115112
if (!uxr_init_udp_transport(udp_transport, ip_protocol, agent_ip, agent_port)) {
116113
RMW_SET_ERROR_MSG("rmw_transport_init UDP: cannot init XRCE transport");
117114
return RMW_RET_ERROR;
118115
}
119-
printf(
120-
"micro-ROS transport: connected using UDP mode, ip: '%s', port: '%s'\n",
121-
agent_ip, agent_port);
122116
#elif defined(RMW_UXRCE_TRANSPORT_CUSTOM)
123117
uxrCustomTransport * custom_transport = (NULL == context_impl) ?
124118
(uxrCustomTransport *)transport :
125119
&context_impl->transport;
126-
void * args = (NULL == context_impl) ?
120+
void * args = (NULL == init_options_impl) ?
127121
rmw_uxrce_transport_default_params.args :
128122
init_options_impl->transport_params.args;
129123

rmw_microxrcedds_c/src/rmw_uxrce_transports.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* @param context The RMW context, holding in its implementation the
3636
* XRCE transport entity, if it has been initialized.
3737
* @param init_options Pointer to struct holding the provided init options for
38-
* micro-ROS, if context has been initialized.
38+
* micro-ROS.
3939
* @param transport Älternative opaque transport structure, which will
4040
* must come as NULL and will be given as a handle to the user.
4141
* This structure is filled when the context_impl pointer is NULL.

0 commit comments

Comments
 (0)