diff --git a/CMakeLists.txt b/CMakeLists.txt index 42ea2f6d5..4c6f1f9e8 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -294,6 +294,7 @@ if(UCLIENT_BUILD_EXAMPLES) add_subdirectory(examples/Deployment) add_subdirectory(examples/Discovery) add_subdirectory(examples/TimeSync) + add_subdirectory(examples/TimeSyncWithCb) endif() ############################################################################### diff --git a/examples/TimeSyncWithCb/CMakeLists.txt b/examples/TimeSyncWithCb/CMakeLists.txt new file mode 100644 index 000000000..d6d5c3720 --- /dev/null +++ b/examples/TimeSyncWithCb/CMakeLists.txt @@ -0,0 +1,51 @@ +# Copyright 2017-present Proyectos y Sistemas de Mantenimiento SL (eProsima). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cmake_minimum_required(VERSION 2.8.12) +if (${CMAKE_VERSION} VERSION_GREATER 3.0) + cmake_policy(SET CMP0048 NEW) +endif() + +project(time-sync-with-cb) + +if(NOT UCLIENT_BUILD_EXAMPLES) + find_package(microxrcedds_client REQUIRED) +endif() + +if(NOT PROFILE_UDP_TRANSPORT) + message(WARNING "Can not compile example: The PROFILE_UDP_TRANSPORT must be enabled.") +else() + add_executable(${PROJECT_NAME} main.c) + if(MSVC OR MSVC_IDE) + target_compile_options(${PROJECT_NAME} PRIVATE /wd4996) + endif() + + set_target_properties(${PROJECT_NAME} PROPERTIES + C_STANDARD 99 + C_STANDARD_REQUIRED YES + ) + + target_link_libraries(${PROJECT_NAME} microxrcedds_client $<$:-Wl,--gc-section,--no-export-dynamic>) + + install(TARGETS ${PROJECT_NAME} + RUNTIME DESTINATION examples/uxr/client/${PROJECT_NAME}/${BIN_INSTALL_DIR} + ) + + install(DIRECTORY ${PROJECT_SOURCE_DIR}/ + DESTINATION examples/uxr/client/${PROJECT_NAME} + FILES_MATCHING PATTERN "*.h" + PATTERN "*.c" + PATTERN "*.idl" + ) +endif() diff --git a/examples/TimeSyncWithCb/main.c b/examples/TimeSyncWithCb/main.c new file mode 100644 index 000000000..286e12a9c --- /dev/null +++ b/examples/TimeSyncWithCb/main.c @@ -0,0 +1,97 @@ +// Copyright 2017-present Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#ifdef WIN32 +#include +#else +#include +#endif + +int64_t avg_time_offset = 0; +size_t sample_id = 0; + +void on_time( + uxrSession* session, + int64_t current_time, + int64_t transmit_timestamp, + int64_t received_timestamp, + int64_t originate_timestamp, + void* args) +{ + (void) args; + int64_t current_time_offset = ((current_time - originate_timestamp) - (transmit_timestamp - received_timestamp)) / 2; + avg_time_offset = (avg_time_offset * sample_id + current_time_offset) / (sample_id + 1); + ++sample_id; + session->time_offset = avg_time_offset; +} + +int main(int args, char** argv) +{ + // CLI + if(3 > args || 0 == atoi(argv[2])) + { + printf("usage: program [-h | --help] | ip port\n"); + return 0; + } + + char* ip = argv[1]; + uint16_t port = (uint16_t)atoi(argv[2]); + + // Transport + uxrUDPTransport transport; + uxrUDPPlatform udp_platform; + if(!uxr_init_udp_transport(&transport, &udp_platform, ip, port)) + { + printf("Error at create transport.\n"); + return 1; + } + + // Session + uxrSession session; + uxr_init_session(&session, &transport.comm, 0xCCCCDDDD); + if(!uxr_create_session(&session)) + { + printf("Error at create session.\n"); + return 1; + } + + // Set time-callback. + uxr_set_time_callback(&session, on_time, NULL); + + // Synchronize with the Agent + bool synchronized = false; + do + { + synchronized = uxr_sync_session(&session, 1000); +#ifdef WIN32 + printf("synchronized with time offset %-5I64d us\n", session.time_offset / 1000); + Sleep(1); +#else + printf("synchronized with time offset %-5" PRId64 "us\n", session.time_offset / 1000); + sleep(1); +#endif + + } while (synchronized); + + // Delete resources + uxr_delete_session(&session); + uxr_close_udp_transport(&transport); + + return 0; +}