mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-22 18:13:34 +08:00
Linux: Auto-detecting clock adjustment mode
This commit is contained in:
@@ -6,8 +6,12 @@
|
|||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <sys/time.h>
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include <uavcan/driver/system_clock.hpp>
|
#include <uavcan/driver/system_clock.hpp>
|
||||||
#include <uavcan_linux/exception.hpp>
|
#include <uavcan_linux/exception.hpp>
|
||||||
|
|
||||||
@@ -56,8 +60,14 @@ class SystemClock : public uavcan::ISystemClock
|
|||||||
return adjtime(&tv, nullptr) == 0;
|
return adjtime(&tv, nullptr) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ClockAdjustmentMode detectPreferredClockAdjustmentMode()
|
||||||
|
{
|
||||||
|
const bool godmode = geteuid() == 0;
|
||||||
|
return godmode ? ClockAdjustmentMode::SystemWide : ClockAdjustmentMode::PerDriverPrivate;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SystemClock(ClockAdjustmentMode adj_mode = ClockAdjustmentMode::SystemWide)
|
SystemClock(ClockAdjustmentMode adj_mode = detectPreferredClockAdjustmentMode())
|
||||||
: gradual_adj_limit_(uavcan::UtcDuration::fromMSec(4000))
|
: gradual_adj_limit_(uavcan::UtcDuration::fromMSec(4000))
|
||||||
, adj_mode_(adj_mode)
|
, adj_mode_(adj_mode)
|
||||||
, step_adj_cnt_(0)
|
, step_adj_cnt_(0)
|
||||||
|
|||||||
@@ -15,12 +15,36 @@ static std::string systime2str(const std::chrono::system_clock::time_point& tp)
|
|||||||
|
|
||||||
int main()
|
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;
|
double sec = 0;
|
||||||
std::cout << "Enter system time adjustment in seconds (fractions allowed): " << std::endl;
|
std::cout << "Enter system time adjustment in seconds (fractions allowed): " << std::endl;
|
||||||
std::cin >> sec;
|
std::cin >> sec;
|
||||||
|
|
||||||
uavcan_linux::SystemClock clock;
|
|
||||||
|
|
||||||
const auto before = std::chrono::system_clock::now();
|
const auto before = std::chrono::system_clock::now();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user