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

mavlink / MAVSDK / 20215172078

14 Dec 2025 10:34PM UTC coverage: 47.959% (-0.06%) from 48.021%
20215172078

push

github

web-flow
Merge pull request #2738 from mavlink/pr-set-gps-origin

Add method set to GPS origin

0 of 36 new or added lines in 2 files covered. (0.0%)

12 existing lines in 6 files now uncovered.

17659 of 36821 relevant lines covered (47.96%)

465.45 hits per line

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

88.14
/src/mavsdk/core/connection.cpp
1
#include "connection.h"
2

3
#include <memory>
4
#include <utility>
5
#include "mavsdk_impl.h"
6
#include "log.h"
7

8
#ifdef WINDOWS
9
#include <winsock2.h>
10
#endif
11

12
namespace mavsdk {
13

14
std::atomic<unsigned> Connection::_forwarding_connections_count = 0;
15

16
Connection::Connection(
140✔
17
    ReceiverCallback receiver_callback,
18
    LibmavReceiverCallback libmav_receiver_callback,
19
    MavsdkImpl& mavsdk_impl,
20
    ForwardingOption forwarding_option) :
140✔
21
    _receiver_callback(std::move(receiver_callback)),
140✔
22
    _libmav_receiver_callback(std::move(libmav_receiver_callback)),
140✔
23
    _mavsdk_impl(mavsdk_impl),
140✔
24
    _mavlink_receiver(),
140✔
25
    _libmav_receiver(),
140✔
26
    _forwarding_option(forwarding_option)
280✔
27
{
28
    // Insert system ID 0 in all connections for broadcast.
29
    _system_ids.insert(0);
140✔
30

31
    if (forwarding_option == ForwardingOption::ForwardingOn) {
140✔
32
        _forwarding_connections_count++;
6✔
33
    }
34

35
    if (const char* env_p = std::getenv("MAVSDK_MAVLINK_DIRECT_DEBUGGING")) {
140✔
36
        if (std::string(env_p) == "1") {
×
37
            _debugging = true;
×
38
        }
39
    }
40
}
140✔
41

42
Connection::~Connection()
140✔
43
{
44
    // Just in case a specific connection didn't call it already.
45
    stop_mavlink_receiver();
140✔
46
    stop_libmav_receiver();
140✔
47
    _receiver_callback = {};
140✔
48
    _libmav_receiver_callback = {};
140✔
49
}
140✔
50

51
bool Connection::start_mavlink_receiver()
140✔
52
{
53
    _mavlink_receiver = std::make_unique<MavlinkReceiver>();
140✔
54
    return true;
140✔
55
}
56

57
void Connection::stop_mavlink_receiver()
278✔
58
{
59
    if (_mavlink_receiver) {
278✔
60
        _mavlink_receiver.reset();
140✔
61
    }
62
}
278✔
63

64
bool Connection::start_libmav_receiver()
140✔
65
{
66
    _libmav_receiver = std::make_unique<LibmavReceiver>(_mavsdk_impl);
140✔
67
    return true;
140✔
68
}
69

70
void Connection::stop_libmav_receiver()
140✔
71
{
72
    if (_libmav_receiver) {
140✔
73
        _libmav_receiver.reset();
140✔
74
    }
75
}
140✔
76

77
void Connection::receive_libmav_message(
2,495✔
78
    const Mavsdk::MavlinkMessage& message, Connection* connection)
79
{
80
    // Register system ID when receiving a message from a new system.
81
    if (_system_ids.find(message.system_id) == _system_ids.end()) {
2,495✔
82
        _system_ids.insert(message.system_id);
×
83
    }
84

85
    if (_debugging) {
2,491✔
86
        LogDebug() << "Connection::receive_libmav_message: " << message.message_name
×
87
                   << " from system " << message.system_id;
×
88
    }
89

90
    if (_libmav_receiver_callback) {
2,491✔
91
        if (_debugging) {
2,493✔
92
            LogDebug() << "Calling libmav receiver callback for: " << message.message_name;
×
93
        }
94
        _libmav_receiver_callback(message, connection);
2,493✔
95
    } else {
UNCOV
96
        LogWarn() << "No libmav receiver callback set!";
×
97
    }
98
}
2,492✔
99

100
void Connection::receive_message(
2,497✔
101
    MavlinkReceiver::ParseResult result, mavlink_message_t& message, Connection* connection)
102
{
103
    // Register system ID for valid messages
104
    if (result == MavlinkReceiver::ParseResult::MessageParsed) {
2,497✔
105
        if (_system_ids.find(message.sysid) == _system_ids.end()) {
2,493✔
106
            _system_ids.insert(message.sysid);
142✔
107
        }
108
    }
109
    // Let MavsdkImpl handle the ParseResult (queue for processing or forward-only)
110
    _receiver_callback(result, message, connection);
2,492✔
111
}
2,497✔
112

113
bool Connection::should_forward_messages() const
37✔
114
{
115
    return _forwarding_option == ForwardingOption::ForwardingOn;
37✔
116
}
117

118
unsigned Connection::forwarding_connections_count()
96✔
119
{
120
    return _forwarding_connections_count;
96✔
121
}
122

123
bool Connection::has_system_id(uint8_t system_id)
1,822✔
124
{
125
    return _system_ids.find(system_id) != _system_ids.end();
1,822✔
126
}
127

128
#ifdef WINDOWS
129
std::string get_socket_error_string(int error_code)
130
{
131
    if (error_code == 0) {
132
        return "";
133
    }
134

135
    LPVOID lpMsgBuf = nullptr;
136
    DWORD bufLen = FormatMessage(
137
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
138
        NULL,
139
        error_code,
140
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
141
        (LPTSTR)&lpMsgBuf,
142
        0,
143
        NULL);
144

145
    if (bufLen) {
146
        LPCSTR lpMsgStr = (LPCSTR)lpMsgBuf;
147
        std::string result(lpMsgStr, lpMsgStr + bufLen);
148
        LocalFree(lpMsgBuf);
149

150
        // Remove trailing newline if present
151
        if (!result.empty() && result[result.length() - 1] == '\n') {
152
            result.erase(result.length() - 1);
153
        }
154
        if (!result.empty() && result[result.length() - 1] == '\r') {
155
            result.erase(result.length() - 1);
156
        }
157

158
        return result;
159
    }
160

161
    return std::to_string(error_code);
162
}
163
#endif
164

165
} // 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

© 2026 Coveralls, Inc