• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mavlink / MAVSDK / 19749270208

27 Nov 2025 10:42PM UTC coverage: 48.21% (-0.01%) from 48.224%
19749270208

push

github

web-flow
Merge pull request #2719 from OwenCochell/cmake-pathfix

CMake custom command fails in certain build configurations

17631 of 36571 relevant lines covered (48.21%)

466.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

12.73
/src/mavsdk/core/timesync.cpp
1
#include "timesync.h"
2
#include "log.h"
3
#include "mavlink_address.h"
4
#include "system_impl.h"
5

6
// Partially based on: https://github.com/mavlink/mavros/blob/master/mavros/src/plugins/sys_time.cpp
7

8
namespace mavsdk {
9

10
Timesync::Timesync(SystemImpl& parent) : _system_impl(parent) {}
138✔
11

12
Timesync::~Timesync()
138✔
13
{
14
    _system_impl.unregister_all_mavlink_message_handlers(this);
138✔
15
}
138✔
16

17
void Timesync::enable()
×
18
{
19
    _is_enabled = true;
×
20
    _system_impl.register_mavlink_message_handler(
×
21
        MAVLINK_MSG_ID_TIMESYNC,
22
        [this](const mavlink_message_t& message) { process_timesync(message); },
×
23
        this);
24
}
×
25

26
void Timesync::do_work()
18,134✔
27
{
28
    if (!_is_enabled) {
18,134✔
29
        return;
18,134✔
30
    }
31

32
    if (_system_impl.get_time().elapsed_since_s(_last_time) >= TIMESYNC_SEND_INTERVAL_S) {
×
33
        if (_system_impl.is_connected()) {
×
34
            uint64_t now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
×
35
                                  _system_impl.get_autopilot_time().now().time_since_epoch())
×
36
                                  .count();
×
37
            send_timesync(0, now_ns);
×
38
        } else {
39
            _autopilot_timesync_acquired = false;
×
40
        }
41
        _last_time = _system_impl.get_time().steady_time();
×
42
    }
43
}
44

45
void Timesync::process_timesync(const mavlink_message_t& message)
×
46
{
47
    mavlink_timesync_t timesync{};
×
48

49
    mavlink_msg_timesync_decode(&message, &timesync);
×
50

51
    int64_t now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
×
52
                         _system_impl.get_autopilot_time().now().time_since_epoch())
×
53
                         .count();
×
54

55
    if (timesync.tc1 == 0 && _autopilot_timesync_acquired) {
×
56
        // Send synced time to remote system
57
        send_timesync(now_ns, timesync.ts1);
×
58
    } else if (timesync.tc1 > 0) {
×
59
        // Time offset between this system and the remote system is calculated assuming RTT for
60
        // the timesync packet is roughly equal both ways.
61
        set_timesync_offset((timesync.tc1 * 2 - (timesync.ts1 + now_ns)) / 2, timesync.ts1);
×
62
    }
63
}
×
64

65
void Timesync::send_timesync(uint64_t tc1, uint64_t ts1)
×
66
{
67
    _system_impl.queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
×
68
        mavlink_message_t message;
69
        mavlink_msg_timesync_pack_chan(
×
70
            mavlink_address.system_id,
×
71
            mavlink_address.component_id,
×
72
            channel,
73
            &message,
74
            static_cast<int64_t>(tc1),
×
75
            static_cast<int64_t>(ts1),
×
76
            0,
77
            0);
78
        return message;
×
79
    });
80
}
×
81

82
void Timesync::set_timesync_offset(int64_t offset_ns, uint64_t start_transfer_local_time_ns)
×
83
{
84
    uint64_t now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
×
85
                          _system_impl.get_autopilot_time().now().time_since_epoch())
×
86
                          .count();
×
87

88
    // Calculate the round trip time (RTT) it took the timesync packet to bounce back to us from
89
    // remote system
90
    uint64_t rtt_ns = now_ns - start_transfer_local_time_ns;
×
91

92
    if (rtt_ns < MAX_RTT_SAMPLE_MS * 1000000ULL) { // Only use samples with low RTT
×
93

94
        // Save time offset for other components to use
95
        _system_impl.get_autopilot_time().shift_time_by(std::chrono::nanoseconds(offset_ns));
×
96
        _autopilot_timesync_acquired = true;
×
97

98
        // Reset high RTT count after filter update
99
        _high_rtt_count = 0;
×
100
    } else {
101
        // Increment counter if round trip time is too high for accurate timesync
102
        _high_rtt_count++;
×
103

104
        if (_high_rtt_count > MAX_CONS_HIGH_RTT) {
×
105
            // Issue a warning to the user if the RTT is constantly high
106
            LogWarn() << "RTT too high for timesync: " << static_cast<double>(rtt_ns) / 1000000.0
×
107
                      << " ms.";
×
108

109
            // Reset counter
110
            _high_rtt_count = 0;
×
111
        }
112
    }
113
}
×
114

115
} // namespace mavsdk
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc