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

mavlink / MAVSDK / 6568658631

19 Oct 2023 01:31AM UTC coverage: 31.23% (+0.02%) from 31.215%
6568658631

push

github

web-flow
Merge pull request #2155 from mavlink/pr-static-fixes

Threading fixes, MAVLink sequence number cleanup

1386 of 1386 new or added lines in 46 files covered. (100.0%)

7906 of 25315 relevant lines covered (31.23%)

23.54 hits per line

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

0.0
/src/mavsdk/plugins/shell/shell_impl.cpp
1
#include "shell_impl.h"
2
#include "mavlink_address.h"
3
#include "system.h"
4
#include "callback_list.tpp"
5

6
namespace mavsdk {
7

8
template class CallbackList<std::string>;
9

10
void ShellImpl::init()
×
11
{
12
    _system_impl->register_mavlink_message_handler(
×
13
        MAVLINK_MSG_ID_SERIAL_CONTROL,
14
        [this](const mavlink_message_t& message) { process_shell_message(message); },
×
15
        this);
16
}
×
17

18
void ShellImpl::deinit()
×
19
{
20
    _system_impl->unregister_all_mavlink_message_handlers(this);
×
21
}
×
22

23
void ShellImpl::enable() {}
×
24

25
void ShellImpl::disable() {}
×
26

27
ShellImpl::ShellImpl(System& system) : PluginImplBase(system)
×
28
{
29
    _system_impl->register_plugin(this);
×
30
}
×
31

32
ShellImpl::ShellImpl(std::shared_ptr<System> system) : PluginImplBase(std::move(system))
×
33
{
34
    _system_impl->register_plugin(this);
×
35
}
×
36

37
ShellImpl::~ShellImpl()
×
38
{
39
    _system_impl->unregister_plugin(this);
×
40
}
×
41

42
Shell::Result ShellImpl::send(std::string command)
×
43
{
44
    if (!_system_impl->is_connected()) {
×
45
        return Shell::Result::NoSystem;
×
46
    }
47

48
    // In case a newline at the end of the command is missing, we add it here.
49
    if (command.back() != '\n') {
×
50
        command.append(1, '\n');
×
51
    }
52

53
    if (!send_command_message(command)) {
×
54
        return Shell::Result::ConnectionError;
×
55
    }
56

57
    return Shell::Result::Success;
×
58
}
59

60
Shell::ReceiveHandle ShellImpl::subscribe_receive(const Shell::ReceiveCallback& callback)
×
61
{
62
    std::lock_guard<std::mutex> lock(_receive.mutex);
×
63
    return _receive.callbacks.subscribe(callback);
×
64
}
65

66
void ShellImpl::unsubscribe_receive(Shell::ReceiveHandle handle)
×
67
{
68
    std::lock_guard<std::mutex> lock(_receive.mutex);
×
69
    _receive.callbacks.unsubscribe(handle);
×
70
}
×
71

72
bool ShellImpl::send_command_message(std::string command)
×
73
{
74
    mavlink_message_t message;
×
75

76
    while (command.length() > MAVLINK_MSG_SERIAL_CONTROL_FIELD_DATA_LEN) {
×
77
        if (!_system_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
×
78
                mavlink_msg_serial_control_pack_chan(
×
79
                    mavlink_address.system_id,
×
80
                    mavlink_address.component_id,
×
81
                    channel,
82
                    &message,
×
83
                    static_cast<uint8_t>(SERIAL_CONTROL_DEV::SERIAL_CONTROL_DEV_SHELL),
84
                    0,
85
                    timeout_ms,
86
                    0,
87
                    static_cast<uint8_t>(MAVLINK_MSG_SERIAL_CONTROL_FIELD_DATA_LEN),
88
                    reinterpret_cast<const uint8_t*>(command.c_str()),
×
89
                    _system_impl->get_system_id(),
×
90
                    _system_impl->get_autopilot_id());
×
91
                command.erase(0, MAVLINK_MSG_SERIAL_CONTROL_FIELD_DATA_LEN);
×
92
                return message;
×
93
            })) {
94
            return false;
×
95
        }
96
    }
97

98
    uint8_t flags = 0;
×
99
    {
100
        // We only ask for a response if we have subscribed to a response.
101
        std::lock_guard<std::mutex> lock(_receive.mutex);
×
102
        if (!_receive.callbacks.empty()) {
×
103
            flags |= SERIAL_CONTROL_FLAG_RESPOND;
×
104
        }
105
    }
106

107
    uint8_t data[MAVLINK_MSG_SERIAL_CONTROL_FIELD_DATA_LEN]{};
×
108
    memcpy(data, command.c_str(), command.length());
×
109

110
    return _system_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
×
111
        mavlink_msg_serial_control_pack_chan(
×
112
            mavlink_address.system_id,
×
113
            mavlink_address.component_id,
×
114
            channel,
115
            &message,
×
116
            static_cast<uint8_t>(SERIAL_CONTROL_DEV::SERIAL_CONTROL_DEV_SHELL),
117
            flags,
×
118
            timeout_ms,
119
            0,
120
            static_cast<uint8_t>(command.length()),
×
121
            data,
×
122
            _system_impl->get_system_id(),
×
123
            _system_impl->get_autopilot_id());
×
124
        return message;
×
125
    });
×
126
}
127

128
void ShellImpl::process_shell_message(const mavlink_message_t& message)
×
129
{
130
    mavlink_serial_control_t serial_control;
×
131
    mavlink_msg_serial_control_decode(&message, &serial_control);
×
132

133
    // This adds an additional byte for the null termination.
134
    char str_copy[sizeof(serial_control.data) + 1]{0};
×
135

136
    const auto len =
137
        std::min(static_cast<std::size_t>(serial_control.count), sizeof(serial_control.data));
×
138

139
    memcpy(str_copy, serial_control.data, len);
×
140

141
    std::string response(str_copy);
×
142

143
    // For the NuttShell (nsh>) we see these characters being sent but we're not sure
144
    // what they are for, so we're removing them for now.
145
    auto index = response.find({32, 27, '[', 'K'});
×
146
    if (index != std::string::npos) {
×
147
        response.erase(index, 4);
×
148
    }
149

150
    std::lock_guard<std::mutex> lock(_receive.mutex);
×
151
    _receive.callbacks.queue(
×
152
        response, [this](const auto& func) { _system_impl->call_user_callback(func); });
×
153
}
×
154

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