From 3df6f958f7faa7936a447f6a57af5e3e122b1c4b Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Sun, 30 Mar 2014 18:58:30 +0400 Subject: [PATCH] Added clock test, superheader uavcan_linux.hpp --- libuavcan_drivers/linux/CMakeLists.txt | 6 +-- .../include/uavcan_linux/uavcan_linux.hpp | 10 +++++ .../linux/test/log_subscriber.cpp | 30 ------------- libuavcan_drivers/linux/test/test_clock.cpp | 43 +++++++++++++++++++ 4 files changed, 56 insertions(+), 33 deletions(-) create mode 100644 libuavcan_drivers/linux/include/uavcan_linux/uavcan_linux.hpp delete mode 100644 libuavcan_drivers/linux/test/log_subscriber.cpp create mode 100644 libuavcan_drivers/linux/test/test_clock.cpp diff --git a/libuavcan_drivers/linux/CMakeLists.txt b/libuavcan_drivers/linux/CMakeLists.txt index bd6a95280e..d4b9c4e567 100644 --- a/libuavcan_drivers/linux/CMakeLists.txt +++ b/libuavcan_drivers/linux/CMakeLists.txt @@ -18,6 +18,6 @@ find_library(UAVCAN_LIB uavcan REQUIRED) include_directories(include) set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -pedantic -std=c++0x -g3 -O1") # GCC or Clang -add_executable(log_subscriber test/log_subscriber.cpp) -target_link_libraries(log_subscriber ${UAVCAN_LIB}) -target_link_libraries(log_subscriber rt) +add_executable(test_clock test/test_clock.cpp) +target_link_libraries(test_clock ${UAVCAN_LIB}) +target_link_libraries(test_clock rt) diff --git a/libuavcan_drivers/linux/include/uavcan_linux/uavcan_linux.hpp b/libuavcan_drivers/linux/include/uavcan_linux/uavcan_linux.hpp new file mode 100644 index 0000000000..32428411f2 --- /dev/null +++ b/libuavcan_drivers/linux/include/uavcan_linux/uavcan_linux.hpp @@ -0,0 +1,10 @@ +/* + * Copyright (C) 2014 Pavel Kirienko + */ + +#pragma once + +#include + +#include +#include diff --git a/libuavcan_drivers/linux/test/log_subscriber.cpp b/libuavcan_drivers/linux/test/log_subscriber.cpp deleted file mode 100644 index 2408265577..0000000000 --- a/libuavcan_drivers/linux/test/log_subscriber.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (C) 2014 Pavel Kirienko - */ - -#include -#include -#include -#include -#include - -int main() -{ - double sec = 0; - std::cout << "Enter system time adjustment in seconds: " << std::endl; - std::cin >> sec; - - uavcan_linux::SystemClockDriver clock; - - try - { - clock.adjustUtc(uavcan::UtcDuration::fromUSec(sec * 1e6)); - } - catch (const uavcan_linux::Exception& ex) - { - std::cout << ex.what() << std::endl; - std::cout << strerror(ex.getErrno()) << std::endl; - } - - return 0; -} diff --git a/libuavcan_drivers/linux/test/test_clock.cpp b/libuavcan_drivers/linux/test/test_clock.cpp new file mode 100644 index 0000000000..b2eca22eae --- /dev/null +++ b/libuavcan_drivers/linux/test/test_clock.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2014 Pavel Kirienko + */ + +#include +#include +#include +#include + +static std::string systime2str(const std::chrono::system_clock::time_point& tp) +{ + const auto tt = std::chrono::system_clock::to_time_t(tp); + return std::ctime(&tt); +} + +int main() +{ + double sec = 0; + std::cout << "Enter system time adjustment in seconds (fractions allowed): " << std::endl; + std::cin >> sec; + + uavcan_linux::SystemClockDriver clock; + + const auto before = std::chrono::system_clock::now(); + try + { + clock.adjustUtc(uavcan::UtcDuration::fromUSec(sec * 1e6)); + } + catch (const uavcan_linux::Exception& ex) + { + std::cout << ex.what() << std::endl; + std::cout << strerror(ex.getErrno()) << std::endl; + return 1; + } + const auto after = std::chrono::system_clock::now(); + + std::cout << "Time before: " << systime2str(before) << "\n" + << "Time after: " << systime2str(after) << "\n" + << "Millisecond diff (after - before): " + << std::chrono::duration_cast(after - before).count() << std::endl; + + return 0; +}