Skip to content

Commit cebf1f5

Browse files
authored
Add time sync functionality (#117)
* Add time sync functionality * Uncrustified * Add time sync test * Update test assert * Uncrustified Co-authored-by: GitHub Actions Bot <>
1 parent d3ca1be commit cebf1f5

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

rmw_microxrcedds_c/include/rmw_uros/options.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ typedef struct rmw_uxrce_transport_params_t
5757
uint32_t client_key;
5858
} rmw_uxrce_transport_params_t;
5959

60+
/**
61+
* \brief Synchronizes the session time using the NTP protocol.
62+
* \param[in, out] result Variable to update with micro-ROS Agent POSIX timestamp in nanoseconds.
63+
* \param[in] timeout_ms The waiting time in milliseconds.
64+
* \return RMW_RET_OK when success.
65+
* \return RMW_RET_INVALID_ARGUMENT If no session is running or the synchronization fails.
66+
*/
67+
rmw_ret_t rmw_uros_sync_session(
68+
int64_t* result,
69+
const int timeout_ms);
70+
6071
/**
6172
* \brief Parses command line args and fills rmw implementation-specific options.
6273
* `rmw_init_options allocator` is used to allocate the specific rmw options.

rmw_microxrcedds_c/src/rmw_uros_options.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,36 @@
2626

2727
#include <uxr/client/client.h>
2828
#include <uxr/client/util/ping.h>
29+
#include <uxr/client/util/time.h>
30+
31+
rmw_ret_t rmw_uros_sync_session(
32+
int64_t* result,
33+
const int timeout_ms)
34+
{
35+
rmw_ret_t ret = RMW_RET_OK;
36+
37+
// Check session is initialized
38+
if (NULL == session_memory.allocateditems)
39+
{
40+
RMW_SET_ERROR_MSG("Uninitialized session.");
41+
return RMW_RET_ERROR;
42+
}
43+
44+
rmw_uxrce_mempool_item_t* item = session_memory.allocateditems;
45+
rmw_context_impl_t* context = (rmw_context_impl_t*)item->data;
46+
47+
if (uxr_sync_session(&context->session, timeout_ms))
48+
{
49+
*result = uxr_nanos() - context->session.time_offset;
50+
}
51+
else
52+
{
53+
RMW_SET_ERROR_MSG("Time synchronization failed.");
54+
return RMW_RET_ERROR;
55+
}
56+
57+
return ret;
58+
}
2959

3060
rmw_ret_t rmw_uros_init_options(
3161
int argc,

rmw_microxrcedds_c/test/test_rmw.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <gtest/gtest.h>
16+
#include <ctime>
1617

1718
#include <rmw/rmw.h>
1819
#include <rmw/init_options.h>
@@ -64,3 +65,22 @@ TEST(rmw_microxrcedds, agent_ping)
6465

6566
ASSERT_EQ(rmw_uros_ping_agent(100, 1), RMW_RET_OK);
6667
}
68+
69+
/*
70+
* Testing rmw time sync.
71+
*/
72+
TEST(rmw_microxrcedds, sync_session)
73+
{
74+
rmw_context_t test_context = rmw_get_zero_initialized_context();
75+
rmw_init_options_t test_options = rmw_get_zero_initialized_init_options();
76+
int64_t result_ns = 0;
77+
78+
ASSERT_EQ(rmw_init_options_init(&test_options, rcutils_get_default_allocator()), RMW_RET_OK);
79+
ASSERT_EQ(rmw_init(&test_options, &test_context), RMW_RET_OK);
80+
81+
std::time_t timestamp_seconds = std::time(nullptr);
82+
ASSERT_EQ(rmw_uros_sync_session(&result_ns, 500), RMW_RET_OK);
83+
ASSERT_EQ(result_ns / 1000000000, timestamp_seconds);
84+
85+
ASSERT_EQ(rmw_shutdown(&test_context), RMW_RET_OK);
86+
}

0 commit comments

Comments
 (0)