From 788d7348b6044b43d4e8581782500a15fe277dfa Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Mon, 31 Mar 2014 19:52:43 +0400 Subject: [PATCH] Linux: Auto-detecting clock adjustment mode --- .../linux/include/uavcan_linux/clock.hpp | 14 ++++++++-- libuavcan_drivers/linux/test/test_clock.cpp | 28 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/libuavcan_drivers/linux/include/uavcan_linux/clock.hpp b/libuavcan_drivers/linux/include/uavcan_linux/clock.hpp index 638b2a6a68..6000ae2120 100644 --- a/libuavcan_drivers/linux/include/uavcan_linux/clock.hpp +++ b/libuavcan_drivers/linux/include/uavcan_linux/clock.hpp @@ -6,8 +6,12 @@ #include #include -#include #include + +#include +#include +#include + #include #include @@ -56,8 +60,14 @@ class SystemClock : public uavcan::ISystemClock return adjtime(&tv, nullptr) == 0; } + static ClockAdjustmentMode detectPreferredClockAdjustmentMode() + { + const bool godmode = geteuid() == 0; + return godmode ? ClockAdjustmentMode::SystemWide : ClockAdjustmentMode::PerDriverPrivate; + } + public: - SystemClock(ClockAdjustmentMode adj_mode = ClockAdjustmentMode::SystemWide) + SystemClock(ClockAdjustmentMode adj_mode = detectPreferredClockAdjustmentMode()) : gradual_adj_limit_(uavcan::UtcDuration::fromMSec(4000)) , adj_mode_(adj_mode) , step_adj_cnt_(0) diff --git a/libuavcan_drivers/linux/test/test_clock.cpp b/libuavcan_drivers/linux/test/test_clock.cpp index 85e91388d3..3566835dcf 100644 --- a/libuavcan_drivers/linux/test/test_clock.cpp +++ b/libuavcan_drivers/linux/test/test_clock.cpp @@ -15,12 +15,36 @@ static std::string systime2str(const std::chrono::system_clock::time_point& tp) int main() { + uavcan_linux::SystemClock clock; + + /* + * Auto-detected clock adjustment mode + */ + std::cout << "Clock adjustment mode: "; + switch (clock.getAdjustmentMode()) + { + case uavcan_linux::ClockAdjustmentMode::SystemWide: + { + std::cout << "SystemWide"; + break; + } + case uavcan_linux::ClockAdjustmentMode::PerDriverPrivate: + { + std::cout << "PerDriverPrivate"; + break; + } + default: + std::abort(); + } + std::cout << std::endl; + + /* + * Test adjustment + */ double sec = 0; std::cout << "Enter system time adjustment in seconds (fractions allowed): " << std::endl; std::cin >> sec; - uavcan_linux::SystemClock clock; - const auto before = std::chrono::system_clock::now(); try {