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

mavlink / MAVSDK / 16482273901

23 Jul 2025 09:32PM UTC coverage: 45.098% (+0.005%) from 45.093%
16482273901

Pull #2621

github

web-flow
Merge 133cd47bb into fbe842a3b
Pull Request #2621: CameraServer improvements

46 of 132 new or added lines in 1 file covered. (34.85%)

2 existing lines in 2 files now uncovered.

15400 of 34148 relevant lines covered (45.1%)

287.93 hits per line

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

36.99
/src/mavsdk/plugins/camera_server/camera_server_impl.cpp
1
#include "camera_server_impl.h"
2
#include "callback_list.tpp"
3

4
namespace mavsdk {
5

6
template class CallbackList<int32_t>;
7
template class CallbackList<CameraServer::TrackPoint>;
8
template class CallbackList<CameraServer::TrackRectangle>;
9

10
CameraServerImpl::CameraServerImpl(std::shared_ptr<ServerComponent> server_component) :
9✔
11
    ServerPluginImplBase(server_component)
9✔
12
{
13
    _server_component_impl->register_plugin(this);
9✔
14
}
9✔
15

16
CameraServerImpl::~CameraServerImpl()
18✔
17
{
18
    _server_component_impl->unregister_plugin(this);
9✔
19
}
18✔
20

21
void CameraServerImpl::init()
9✔
22
{
23
    _server_component_impl->register_mavlink_command_handler(
9✔
24
        MAV_CMD_REQUEST_MESSAGE,
25
        [this](const MavlinkCommandReceiver::CommandLong& command) {
97✔
26
            return process_request_message(command);
97✔
27
        },
28
        this);
29
    _server_component_impl->register_mavlink_command_handler(
9✔
30
        MAV_CMD_REQUEST_CAMERA_INFORMATION,
31
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
32
            return process_camera_information_request(command);
×
33
        },
34
        this);
35
    _server_component_impl->register_mavlink_command_handler(
9✔
36
        MAV_CMD_REQUEST_CAMERA_SETTINGS,
37
        [this](const MavlinkCommandReceiver::CommandLong& command) {
9✔
38
            return process_camera_settings_request(command);
9✔
39
        },
40
        this);
41
    _server_component_impl->register_mavlink_command_handler(
9✔
42
        MAV_CMD_REQUEST_STORAGE_INFORMATION,
43
        [this](const MavlinkCommandReceiver::CommandLong& command) {
16✔
44
            return process_storage_information_request(command);
16✔
45
        },
46
        this);
47
    _server_component_impl->register_mavlink_command_handler(
9✔
48
        MAV_CMD_STORAGE_FORMAT,
49
        [this](const MavlinkCommandReceiver::CommandLong& command) {
1✔
50
            return process_storage_format(command);
1✔
51
        },
52
        this);
53
    _server_component_impl->register_mavlink_command_handler(
9✔
54
        MAV_CMD_REQUEST_CAMERA_CAPTURE_STATUS,
55
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
56
            return process_camera_capture_status_request(command);
×
57
        },
58
        this);
59
    _server_component_impl->register_mavlink_command_handler(
9✔
60
        MAV_CMD_RESET_CAMERA_SETTINGS,
61
        [this](const MavlinkCommandReceiver::CommandLong& command) {
1✔
62
            return process_reset_camera_settings(command);
1✔
63
        },
64
        this);
65
    _server_component_impl->register_mavlink_command_handler(
9✔
66
        MAV_CMD_SET_CAMERA_MODE,
67
        [this](const MavlinkCommandReceiver::CommandLong& command) {
2✔
68
            return process_set_camera_mode(command);
2✔
69
        },
70
        this);
71
    _server_component_impl->register_mavlink_command_handler(
9✔
72
        MAV_CMD_SET_CAMERA_ZOOM,
73
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
74
            return process_set_camera_zoom(command);
×
75
        },
76
        this);
77
    _server_component_impl->register_mavlink_command_handler(
9✔
78
        MAV_CMD_SET_CAMERA_FOCUS,
79
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
80
            return process_set_camera_focus(command);
×
81
        },
82
        this);
83
    _server_component_impl->register_mavlink_command_handler(
9✔
84
        MAV_CMD_SET_STORAGE_USAGE,
85
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
86
            return process_set_storage_usage(command);
×
87
        },
88
        this);
89
    _server_component_impl->register_mavlink_command_handler(
9✔
90
        MAV_CMD_IMAGE_START_CAPTURE,
91
        [this](const MavlinkCommandReceiver::CommandLong& command) {
2✔
92
            return process_image_start_capture(command);
2✔
93
        },
94
        this);
95
    _server_component_impl->register_mavlink_command_handler(
9✔
96
        MAV_CMD_IMAGE_STOP_CAPTURE,
97
        [this](const MavlinkCommandReceiver::CommandLong& command) {
1✔
98
            return process_image_stop_capture(command);
1✔
99
        },
100
        this);
101
    _server_component_impl->register_mavlink_command_handler(
9✔
102
        MAV_CMD_REQUEST_CAMERA_IMAGE_CAPTURE,
103
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
104
            return process_camera_image_capture_request(command);
×
105
        },
106
        this);
107
    _server_component_impl->register_mavlink_command_handler(
9✔
108
        MAV_CMD_VIDEO_START_CAPTURE,
109
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
110
            return process_video_start_capture(command);
×
111
        },
112
        this);
113
    _server_component_impl->register_mavlink_command_handler(
9✔
114
        MAV_CMD_VIDEO_STOP_CAPTURE,
115
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
116
            return process_video_stop_capture(command);
×
117
        },
118
        this);
119
    _server_component_impl->register_mavlink_command_handler(
9✔
120
        MAV_CMD_VIDEO_START_STREAMING,
121
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
122
            return process_video_start_streaming(command);
×
123
        },
124
        this);
125
    _server_component_impl->register_mavlink_command_handler(
9✔
126
        MAV_CMD_VIDEO_STOP_STREAMING,
127
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
128
            return process_video_stop_streaming(command);
×
129
        },
130
        this);
131
    _server_component_impl->register_mavlink_command_handler(
9✔
132
        MAV_CMD_REQUEST_VIDEO_STREAM_INFORMATION,
133
        [this](const MavlinkCommandReceiver::CommandLong& command) {
15✔
134
            return process_video_stream_information_request(command);
15✔
135
        },
136
        this);
137
    _server_component_impl->register_mavlink_command_handler(
9✔
138
        MAV_CMD_REQUEST_VIDEO_STREAM_STATUS,
139
        [this](const MavlinkCommandReceiver::CommandLong& command) {
17✔
140
            return process_video_stream_status_request(command);
17✔
141
        },
142
        this);
143
    _server_component_impl->register_mavlink_command_handler(
9✔
144
        MAV_CMD_CAMERA_TRACK_POINT,
145
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
146
            return process_track_point_command(command);
×
147
        },
148
        this);
149

150
    _server_component_impl->register_mavlink_command_handler(
9✔
151
        MAV_CMD_CAMERA_TRACK_RECTANGLE,
152
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
153
            return process_track_rectangle_command(command);
×
154
        },
155
        this);
156

157
    _server_component_impl->register_mavlink_command_handler(
9✔
158
        MAV_CMD_CAMERA_STOP_TRACKING,
159
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
160
            return process_track_off_command(command);
×
161
        },
162
        this);
163
    _server_component_impl->register_mavlink_command_handler(
9✔
164
        MAV_CMD_SET_MESSAGE_INTERVAL,
165
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
166
            return process_set_message_interval(command);
×
167
        },
168
        this);
169
}
9✔
170

171
void CameraServerImpl::deinit()
9✔
172
{
173
    stop_image_capture_interval();
9✔
174
    stop_sending_capture_status();
9✔
175
    _server_component_impl->unregister_all_mavlink_command_handlers(this);
9✔
176

177
    std::lock_guard lg(_mutex);
9✔
178
    _take_photo_callbacks.clear();
9✔
179
    _start_video_callbacks.clear();
9✔
180
    _stop_video_callbacks.clear();
9✔
181
    _start_video_streaming_callbacks.clear();
9✔
182
    _stop_video_streaming_callbacks.clear();
9✔
183
    _set_mode_callbacks.clear();
9✔
184
    _storage_information_callbacks.clear();
9✔
185
    _capture_status_callbacks.clear();
9✔
186
    _format_storage_callbacks.clear();
9✔
187
    _reset_settings_callbacks.clear();
9✔
188
    _tracking_point_callbacks.clear();
9✔
189
    _tracking_rectangle_callbacks.clear();
9✔
190
    _tracking_off_callbacks.clear();
9✔
191
    _zoom_in_start_callbacks.clear();
9✔
192
    _zoom_out_start_callbacks.clear();
9✔
193
    _zoom_stop_callbacks.clear();
9✔
194
    _zoom_range_callbacks.clear();
9✔
195
}
9✔
196

197
bool CameraServerImpl::is_command_sender_ok(const MavlinkCommandReceiver::CommandLong& command)
×
198
{
199
    if (command.target_system_id != 0 &&
×
200
        command.target_system_id != _server_component_impl->get_own_system_id()) {
×
201
        return false;
×
202
    } else {
203
        return true;
×
204
    }
205
}
206

207
void CameraServerImpl::set_tracking_point_status(CameraServer::TrackPoint tracked_point)
×
208
{
209
    std::lock_guard<std::mutex> lg{_mutex};
×
210
    _tracking_mode = TrackingMode::POINT;
×
211
    _tracked_point = tracked_point;
×
212
}
×
213

214
void CameraServerImpl::set_tracking_rectangle_status(CameraServer::TrackRectangle tracked_rectangle)
×
215
{
216
    std::lock_guard<std::mutex> lg{_mutex};
×
217
    _tracking_mode = TrackingMode::RECTANGLE;
×
218
    _tracked_rectangle = tracked_rectangle;
×
219
}
×
220

221
void CameraServerImpl::set_tracking_off_status()
×
222
{
223
    std::lock_guard<std::mutex> lg{_mutex};
×
224
    _tracking_mode = TrackingMode::NONE;
×
225
}
×
226

227
bool CameraServerImpl::parse_version_string(const std::string& version_str)
9✔
228
{
229
    uint32_t unused;
9✔
230

231
    return parse_version_string(version_str, unused);
9✔
232
}
233

234
bool CameraServerImpl::parse_version_string(const std::string& version_str, uint32_t& version)
18✔
235
{
236
    // empty string means no version
237
    if (version_str.empty()) {
18✔
238
        version = 0;
×
239

240
        return true;
×
241
    }
242

243
    uint8_t major{}, minor{}, patch{}, dev{};
18✔
244

245
    auto ret = sscanf(version_str.c_str(), "%hhu.%hhu.%hhu.%hhu", &major, &minor, &patch, &dev);
18✔
246

247
    if (ret == EOF) {
18✔
248
        return false;
×
249
    }
250

251
    // pack version according to MAVLINK spec
252
    version = dev << 24 | patch << 16 | minor << 8 | major;
18✔
253

254
    return true;
18✔
255
}
256

257
CameraServer::Result CameraServerImpl::set_information(CameraServer::Information information)
9✔
258
{
259
    if (!parse_version_string(information.firmware_version)) {
9✔
260
        LogDebug() << "incorrectly formatted firmware version string: "
×
261
                   << information.firmware_version;
×
262
        return CameraServer::Result::WrongArgument;
×
263
    }
264

265
    std::lock_guard<std::mutex> lg{_mutex};
9✔
266

267
    // TODO: validate information.definition_file_uri
268

269
    _is_information_set = true;
9✔
270
    _information = information;
9✔
271

272
    return CameraServer::Result::Success;
9✔
273
}
9✔
274

275
CameraServer::Result
276
CameraServerImpl::set_video_streaming(CameraServer::VideoStreaming video_streaming)
1✔
277
{
278
    // TODO: validate uri length
279

280
    std::lock_guard<std::mutex> lg{_mutex};
1✔
281

282
    _is_video_streaming_set = true;
1✔
283
    _video_streaming = video_streaming;
1✔
284

285
    return CameraServer::Result::Success;
2✔
286
}
1✔
287

288
CameraServer::Result CameraServerImpl::set_in_progress(bool in_progress)
×
289
{
290
    _is_image_capture_in_progress = in_progress;
×
291

292
    send_capture_status();
×
293
    return CameraServer::Result::Success;
×
294
}
295

296
CameraServer::TakePhotoHandle
297
CameraServerImpl::subscribe_take_photo(const CameraServer::TakePhotoCallback& callback)
2✔
298
{
299
    std::lock_guard<std::mutex> lg{_mutex};
2✔
300
    return _take_photo_callbacks.subscribe(callback);
2✔
301
}
2✔
302

303
void CameraServerImpl::unsubscribe_take_photo(CameraServer::TakePhotoHandle handle)
×
304
{
305
    std::lock_guard<std::mutex> lg{_mutex};
×
306
    _take_photo_callbacks.unsubscribe(handle);
×
307
}
×
308

309
CameraServer::Result CameraServerImpl::respond_take_photo(
4✔
310
    CameraServer::CameraFeedback take_photo_feedback, CameraServer::CaptureInfo capture_info)
311
{
312
    bool should_send_capture_status = false;
4✔
313

314
    {
315
        std::lock_guard<std::mutex> lg{_mutex};
4✔
316

317
        // If capture_info.index == INT32_MIN, it means this was an interval
318
        // capture rather than a single image capture.
319
        if (capture_info.index != INT32_MIN) {
4✔
320
            // We expect each capture to be the next sequential number.
321
            // If _image_capture_count == 0, we ignore since it means that this is
322
            // the first photo since the plugin was initialized.
323
            if (_image_capture_count != 0 && capture_info.index != _image_capture_count + 1) {
4✔
NEW
324
                LogErr() << "unexpected image index, expecting " << +(_image_capture_count + 1)
×
NEW
325
                         << " but was " << +capture_info.index;
×
326
            }
327

328
            _image_capture_count = capture_info.index;
4✔
329
        }
330

331
        // Log the command details to help debug
332
        LogDebug() << "Responding to take photo command: " << "target_system_id: "
8✔
333
                   << static_cast<int>(_last_take_photo_command.target_system_id)
8✔
334
                   << ", target_component_id: "
4✔
335
                   << static_cast<int>(_last_take_photo_command.target_component_id)
8✔
336
                   << ", command: " << _last_take_photo_command.command;
16✔
337

338
        switch (take_photo_feedback) {
4✔
NEW
339
            default:
×
340
                // Fallthrough
341
            case CameraServer::CameraFeedback::Unknown:
NEW
342
                return CameraServer::Result::Error;
×
343
            case CameraServer::CameraFeedback::Ok: {
4✔
344
                // Check for error above
345
                auto command_ack = _server_component_impl->make_command_ack_message(
4✔
346
                    _last_take_photo_command, MAV_RESULT_ACCEPTED);
4✔
347
                _server_component_impl->send_command_ack(command_ack);
4✔
348
                should_send_capture_status = true;
4✔
349
                // Only break and send the captured below.
350
                break;
4✔
351
            }
NEW
352
            case CameraServer::CameraFeedback::Busy: {
×
NEW
353
                auto command_ack = _server_component_impl->make_command_ack_message(
×
NEW
354
                    _last_take_photo_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
NEW
355
                _server_component_impl->send_command_ack(command_ack);
×
NEW
356
                return CameraServer::Result::Success;
×
357
            }
358

NEW
359
            case CameraServer::CameraFeedback::Failed: {
×
NEW
360
                auto command_ack = _server_component_impl->make_command_ack_message(
×
NEW
361
                    _last_take_photo_command, MAV_RESULT_FAILED);
×
NEW
362
                _server_component_impl->send_command_ack(command_ack);
×
NEW
363
                return CameraServer::Result::Success;
×
364
            }
365
        }
366

367
        // REVISIT: Should we cache all CaptureInfo in memory for single image
368
        // captures so that we can respond to requests for lost CAMERA_IMAGE_CAPTURED
369
        // messages without calling back to user code?
370

371
        static const uint8_t camera_id = 0; // deprecated unused field
372

373
        const float attitude_quaternion[] = {
4✔
374
            capture_info.attitude_quaternion.w,
4✔
375
            capture_info.attitude_quaternion.x,
4✔
376
            capture_info.attitude_quaternion.y,
4✔
377
            capture_info.attitude_quaternion.z,
4✔
378
        };
4✔
379

380
        // There needs to be enough data to be copied mavlink internal.
381
        capture_info.file_url.resize(205);
4✔
382

383
        // TODO: this should be a broadcast message
384
        _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
4✔
385
            mavlink_message_t message{};
4✔
386
            mavlink_msg_camera_image_captured_pack_chan(
12✔
387
                mavlink_address.system_id,
4✔
388
                mavlink_address.component_id,
4✔
389
                channel,
390
                &message,
391
                static_cast<uint32_t>(_server_component_impl->get_time().elapsed_s() * 1e3),
4✔
392
                capture_info.time_utc_us,
4✔
393
                camera_id,
394
                static_cast<int32_t>(capture_info.position.latitude_deg * 1e7),
4✔
395
                static_cast<int32_t>(capture_info.position.longitude_deg * 1e7),
4✔
396
                static_cast<int32_t>(capture_info.position.absolute_altitude_m * 1e3f),
4✔
397
                static_cast<int32_t>(capture_info.position.relative_altitude_m * 1e3f),
4✔
398
                attitude_quaternion,
4✔
399
                capture_info.index,
400
                capture_info.is_success,
4✔
401
                capture_info.file_url.c_str());
402
            return message;
4✔
403
        });
404
        LogDebug() << "sent camera image captured msg - index: " << +capture_info.index;
4✔
405

406
    } // Release mutex
4✔
407

408
    if (should_send_capture_status) {
4✔
409
        send_capture_status();
4✔
410
    }
411

412
    return CameraServer::Result::Success;
4✔
413
}
414

415
CameraServer::StartVideoHandle
416
CameraServerImpl::subscribe_start_video(const CameraServer::StartVideoCallback& callback)
×
417
{
418
    std::lock_guard<std::mutex> lg{_mutex};
×
419
    return _start_video_callbacks.subscribe(callback);
×
420
}
×
421

422
void CameraServerImpl::unsubscribe_start_video(CameraServer::StartVideoHandle handle)
×
423
{
424
    std::lock_guard<std::mutex> lg{_mutex};
×
425
    _start_video_callbacks.unsubscribe(handle);
×
426
}
×
427

428
CameraServer::Result
429
CameraServerImpl::respond_start_video(CameraServer::CameraFeedback start_video_feedback)
×
430
{
NEW
431
    bool should_send_capture_status = false;
×
432

433
    {
NEW
434
        std::lock_guard<std::mutex> lg{_mutex};
×
435

NEW
436
        switch (start_video_feedback) {
×
NEW
437
            default:
×
438
                // Fallthrough
439
            case CameraServer::CameraFeedback::Unknown:
NEW
440
                return CameraServer::Result::Error;
×
NEW
441
            case CameraServer::CameraFeedback::Ok: {
×
NEW
442
                auto command_ack = _server_component_impl->make_command_ack_message(
×
NEW
443
                    _last_start_video_command, MAV_RESULT_ACCEPTED);
×
NEW
444
                _server_component_impl->send_command_ack(command_ack);
×
NEW
445
                _capture_status.video_status =
×
446
                    CameraServer::CaptureStatus::VideoStatus::CaptureInProgress;
NEW
447
                should_send_capture_status = true;
×
NEW
448
                break;
×
449
            }
NEW
450
            case CameraServer::CameraFeedback::Busy: {
×
NEW
451
                auto command_ack = _server_component_impl->make_command_ack_message(
×
NEW
452
                    _last_start_video_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
NEW
453
                _server_component_impl->send_command_ack(command_ack);
×
NEW
454
                return CameraServer::Result::Success;
×
455
            }
NEW
456
            case CameraServer::CameraFeedback::Failed: {
×
NEW
457
                auto command_ack = _server_component_impl->make_command_ack_message(
×
NEW
458
                    _last_start_video_command, MAV_RESULT_FAILED);
×
NEW
459
                _server_component_impl->send_command_ack(command_ack);
×
NEW
460
                return CameraServer::Result::Success;
×
461
            }
462
        }
NEW
463
    } // Release mutex
×
464

NEW
465
    if (should_send_capture_status) {
×
NEW
466
        send_capture_status();
×
467
    }
468

NEW
469
    return CameraServer::Result::Success;
×
470
}
471

472
CameraServer::StopVideoHandle
473
CameraServerImpl::subscribe_stop_video(const CameraServer::StopVideoCallback& callback)
×
474
{
475
    std::lock_guard<std::mutex> lg{_mutex};
×
476
    return _stop_video_callbacks.subscribe(callback);
×
477
}
×
478

479
void CameraServerImpl::unsubscribe_stop_video(CameraServer::StopVideoHandle handle)
×
480
{
481
    std::lock_guard<std::mutex> lg{_mutex};
×
482
    return _stop_video_callbacks.unsubscribe(handle);
×
483
}
×
484

485
CameraServer::Result
486
CameraServerImpl::respond_stop_video(CameraServer::CameraFeedback stop_video_feedback)
×
487
{
NEW
488
    bool should_send_capture_status = false;
×
489

490
    {
NEW
491
        std::lock_guard<std::mutex> lg{_mutex};
×
492

NEW
493
        switch (stop_video_feedback) {
×
NEW
494
            default:
×
495
                // Fallthrough
496
            case CameraServer::CameraFeedback::Unknown:
NEW
497
                return CameraServer::Result::Error;
×
NEW
498
            case CameraServer::CameraFeedback::Ok: {
×
NEW
499
                auto command_ack = _server_component_impl->make_command_ack_message(
×
NEW
500
                    _last_stop_video_command, MAV_RESULT_ACCEPTED);
×
NEW
501
                _server_component_impl->send_command_ack(command_ack);
×
NEW
502
                _capture_status.video_status = CameraServer::CaptureStatus::VideoStatus::Idle;
×
NEW
503
                should_send_capture_status = true;
×
NEW
504
                break;
×
505
            }
NEW
506
            case CameraServer::CameraFeedback::Busy: {
×
NEW
507
                auto command_ack = _server_component_impl->make_command_ack_message(
×
NEW
508
                    _last_stop_video_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
NEW
509
                _server_component_impl->send_command_ack(command_ack);
×
NEW
510
                return CameraServer::Result::Success;
×
511
            }
NEW
512
            case CameraServer::CameraFeedback::Failed: {
×
NEW
513
                auto command_ack = _server_component_impl->make_command_ack_message(
×
NEW
514
                    _last_stop_video_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
NEW
515
                _server_component_impl->send_command_ack(command_ack);
×
NEW
516
                return CameraServer::Result::Success;
×
517
            }
518
        }
NEW
519
    } // Release mutex
×
520

NEW
521
    if (should_send_capture_status) {
×
NEW
522
        send_capture_status();
×
523
    }
524

NEW
525
    return CameraServer::Result::Success;
×
526
}
527

528
CameraServer::StartVideoStreamingHandle CameraServerImpl::subscribe_start_video_streaming(
×
529
    const CameraServer::StartVideoStreamingCallback& callback)
530
{
531
    std::lock_guard<std::mutex> lg{_mutex};
×
532
    return _start_video_streaming_callbacks.subscribe(callback);
×
533
}
×
534

535
void CameraServerImpl::unsubscribe_start_video_streaming(
×
536
    CameraServer::StartVideoStreamingHandle handle)
537
{
538
    std::lock_guard<std::mutex> lg{_mutex};
×
539
    return _start_video_streaming_callbacks.unsubscribe(handle);
×
540
}
×
541

542
CameraServer::Result CameraServerImpl::respond_start_video_streaming(
×
543
    CameraServer::CameraFeedback start_video_streaming_feedback)
544
{
545
    std::lock_guard<std::mutex> lg{_mutex};
×
546

547
    switch (start_video_streaming_feedback) {
×
548
        default:
×
549
            // Fallthrough
550
        case CameraServer::CameraFeedback::Unknown:
551
            return CameraServer::Result::Error;
×
552
        case CameraServer::CameraFeedback::Ok: {
×
553
            auto command_ack = _server_component_impl->make_command_ack_message(
×
554
                _last_start_video_streaming_command, MAV_RESULT_ACCEPTED);
×
555
            _server_component_impl->send_command_ack(command_ack);
×
556
            return CameraServer::Result::Success;
×
557
        }
558
        case CameraServer::CameraFeedback::Busy: {
×
559
            auto command_ack = _server_component_impl->make_command_ack_message(
×
560
                _last_start_video_streaming_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
561
            _server_component_impl->send_command_ack(command_ack);
×
562
            return CameraServer::Result::Success;
×
563
        }
564
        case CameraServer::CameraFeedback::Failed: {
×
565
            auto command_ack = _server_component_impl->make_command_ack_message(
×
566
                _last_start_video_streaming_command, MAV_RESULT_FAILED);
×
567
            _server_component_impl->send_command_ack(command_ack);
×
568
            return CameraServer::Result::Success;
×
569
        }
570
    }
571
}
×
572

573
CameraServer::StopVideoStreamingHandle CameraServerImpl::subscribe_stop_video_streaming(
×
574
    const CameraServer::StopVideoStreamingCallback& callback)
575
{
576
    std::lock_guard<std::mutex> lg{_mutex};
×
577
    return _stop_video_streaming_callbacks.subscribe(callback);
×
578
}
×
579

580
void CameraServerImpl::unsubscribe_stop_video_streaming(
×
581
    CameraServer::StopVideoStreamingHandle handle)
582
{
583
    std::lock_guard<std::mutex> lg{_mutex};
×
584
    return _stop_video_streaming_callbacks.unsubscribe(handle);
×
585
}
×
586

587
CameraServer::Result CameraServerImpl::respond_stop_video_streaming(
×
588
    CameraServer::CameraFeedback stop_video_streaming_feedback)
589
{
590
    std::lock_guard<std::mutex> lg{_mutex};
×
591

592
    switch (stop_video_streaming_feedback) {
×
593
        default:
×
594
            // Fallthrough
595
        case CameraServer::CameraFeedback::Unknown:
596
            return CameraServer::Result::Error;
×
597
        case CameraServer::CameraFeedback::Ok: {
×
598
            auto command_ack = _server_component_impl->make_command_ack_message(
×
599
                _last_stop_video_streaming_command, MAV_RESULT_ACCEPTED);
×
600
            _server_component_impl->send_command_ack(command_ack);
×
601
            return CameraServer::Result::Success;
×
602
        }
603
        case CameraServer::CameraFeedback::Busy: {
×
604
            auto command_ack = _server_component_impl->make_command_ack_message(
×
605
                _last_stop_video_streaming_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
606
            _server_component_impl->send_command_ack(command_ack);
×
607
            return CameraServer::Result::Success;
×
608
        }
609
        case CameraServer::CameraFeedback::Failed: {
×
610
            auto command_ack = _server_component_impl->make_command_ack_message(
×
611
                _last_stop_video_streaming_command, MAV_RESULT_FAILED);
×
612
            _server_component_impl->send_command_ack(command_ack);
×
613
            return CameraServer::Result::Success;
×
614
        }
615
    }
616
}
×
617

618
CameraServer::SetModeHandle
619
CameraServerImpl::subscribe_set_mode(const CameraServer::SetModeCallback& callback)
2✔
620
{
621
    std::lock_guard<std::mutex> lg{_mutex};
2✔
622
    return _set_mode_callbacks.subscribe(callback);
2✔
623
}
2✔
624

625
void CameraServerImpl::unsubscribe_set_mode(CameraServer::SetModeHandle handle)
1✔
626
{
627
    std::lock_guard<std::mutex> lg{_mutex};
1✔
628
    _set_mode_callbacks.unsubscribe(handle);
1✔
629
}
1✔
630

631
CameraServer::Result
632
CameraServerImpl::respond_set_mode(CameraServer::CameraFeedback set_mode_feedback)
2✔
633
{
634
    std::lock_guard<std::mutex> lg{_mutex};
2✔
635

636
    switch (set_mode_feedback) {
2✔
637
        default:
×
638
            // Fallthrough
639
        case CameraServer::CameraFeedback::Unknown:
640
            return CameraServer::Result::Error;
×
641
        case CameraServer::CameraFeedback::Ok: {
2✔
642
            auto command_ack = _server_component_impl->make_command_ack_message(
2✔
643
                _last_set_mode_command, MAV_RESULT_ACCEPTED);
2✔
644
            _server_component_impl->send_command_ack(command_ack);
2✔
645
            return CameraServer::Result::Success;
2✔
646
        }
647
        case CameraServer::CameraFeedback::Busy: {
×
648
            auto command_ack = _server_component_impl->make_command_ack_message(
×
649
                _last_set_mode_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
650
            _server_component_impl->send_command_ack(command_ack);
×
651
            return CameraServer::Result::Success;
×
652
        }
653
        case CameraServer::CameraFeedback::Failed: {
×
654
            auto command_ack = _server_component_impl->make_command_ack_message(
×
655
                _last_set_mode_command, MAV_RESULT_FAILED);
×
656
            _server_component_impl->send_command_ack(command_ack);
×
657
            return CameraServer::Result::Success;
×
658
        }
659
    }
660
}
2✔
661

662
CameraServer::StorageInformationHandle CameraServerImpl::subscribe_storage_information(
1✔
663
    const CameraServer::StorageInformationCallback& callback)
664
{
665
    std::lock_guard<std::mutex> lg{_mutex};
1✔
666
    return _storage_information_callbacks.subscribe(callback);
1✔
667
}
1✔
668

669
void CameraServerImpl::unsubscribe_storage_information(
×
670
    CameraServer::StorageInformationHandle handle)
671
{
672
    std::lock_guard<std::mutex> lg{_mutex};
×
673
    _storage_information_callbacks.unsubscribe(handle);
×
674
}
×
675

676
CameraServer::Result CameraServerImpl::respond_storage_information(
2✔
677
    CameraServer::CameraFeedback storage_information_feedback,
678
    CameraServer::StorageInformation storage_information)
679
{
680
    std::lock_guard<std::mutex> lg{_mutex};
2✔
681

682
    switch (storage_information_feedback) {
2✔
683
        default:
×
684
            // Fallthrough
685
        case CameraServer::CameraFeedback::Unknown:
686
            return CameraServer::Result::Error;
×
687
        case CameraServer::CameraFeedback::Ok: {
2✔
688
            auto command_ack = _server_component_impl->make_command_ack_message(
2✔
689
                _last_storage_information_command, MAV_RESULT_ACCEPTED);
2✔
690
            _server_component_impl->send_command_ack(command_ack);
2✔
691
            // break and send storage information
692
            break;
2✔
693
        }
694
        case CameraServer::CameraFeedback::Busy: {
×
695
            auto command_ack = _server_component_impl->make_command_ack_message(
×
696
                _last_storage_information_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
697
            _server_component_impl->send_command_ack(command_ack);
×
698
            return CameraServer::Result::Success;
×
699
        }
700
        case CameraServer::CameraFeedback::Failed: {
×
701
            auto command_ack = _server_component_impl->make_command_ack_message(
×
702
                _last_storage_information_command, MAV_RESULT_FAILED);
×
703
            _server_component_impl->send_command_ack(command_ack);
×
704
            return CameraServer::Result::Success;
×
705
        }
706
    }
707

708
    const uint8_t storage_count = 1;
2✔
709

710
    const float total_capacity = storage_information.total_storage_mib;
2✔
711
    const float used_capacity = storage_information.used_storage_mib;
2✔
712
    const float available_capacity = storage_information.available_storage_mib;
2✔
713
    const float read_speed = storage_information.read_speed_mib_s;
2✔
714
    const float write_speed = storage_information.write_speed_mib_s;
2✔
715

716
    auto status = STORAGE_STATUS::STORAGE_STATUS_NOT_SUPPORTED;
2✔
717
    switch (storage_information.storage_status) {
2✔
718
        case CameraServer::StorageInformation::StorageStatus::NotAvailable:
×
719
            status = STORAGE_STATUS::STORAGE_STATUS_NOT_SUPPORTED;
×
720
            break;
×
721
        case CameraServer::StorageInformation::StorageStatus::Unformatted:
×
722
            status = STORAGE_STATUS::STORAGE_STATUS_UNFORMATTED;
×
723
            break;
×
724
        case CameraServer::StorageInformation::StorageStatus::Formatted:
2✔
725
            status = STORAGE_STATUS::STORAGE_STATUS_READY;
2✔
726
            break;
2✔
727
        case CameraServer::StorageInformation::StorageStatus::NotSupported:
×
728
            status = STORAGE_STATUS::STORAGE_STATUS_NOT_SUPPORTED;
×
729
            break;
×
730
    }
731

732
    auto type = STORAGE_TYPE::STORAGE_TYPE_UNKNOWN;
2✔
733
    switch (storage_information.storage_type) {
2✔
734
        case CameraServer::StorageInformation::StorageType::UsbStick:
×
735
            type = STORAGE_TYPE::STORAGE_TYPE_USB_STICK;
×
736
            break;
×
737
        case CameraServer::StorageInformation::StorageType::Sd:
×
738
            type = STORAGE_TYPE::STORAGE_TYPE_SD;
×
739
            break;
×
740
        case CameraServer::StorageInformation::StorageType::Microsd:
×
741
            type = STORAGE_TYPE::STORAGE_TYPE_MICROSD;
×
742
            break;
×
743
        case CameraServer::StorageInformation::StorageType::Hd:
×
744
            type = STORAGE_TYPE::STORAGE_TYPE_HD;
×
745
            break;
×
746
        case CameraServer::StorageInformation::StorageType::Other:
×
747
            type = STORAGE_TYPE::STORAGE_TYPE_OTHER;
×
748
            break;
×
749
        default:
2✔
750
            break;
2✔
751
    }
752

753
    std::string name("");
4✔
754
    // This needs to be long enough, otherwise the memcpy in mavlink overflows.
755
    name.resize(32);
2✔
756
    const uint8_t storage_usage = 0;
2✔
757

758
    _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
2✔
759
        mavlink_message_t message{};
2✔
760
        mavlink_msg_storage_information_pack_chan(
4✔
761
            mavlink_address.system_id,
2✔
762
            mavlink_address.component_id,
2✔
763
            channel,
764
            &message,
765
            static_cast<uint32_t>(_server_component_impl->get_time().elapsed_s() * 1e3),
2✔
766
            _last_storage_id,
2✔
767
            storage_count,
768
            status,
2✔
769
            total_capacity,
2✔
770
            used_capacity,
2✔
771
            available_capacity,
2✔
772
            read_speed,
2✔
773
            write_speed,
2✔
774
            type,
2✔
775
            name.data(),
2✔
776
            storage_usage);
777
        return message;
2✔
778
    });
779

780
    return CameraServer::Result::Success;
2✔
781
}
2✔
782

783
CameraServer::CaptureStatusHandle
784
CameraServerImpl::subscribe_capture_status(const CameraServer::CaptureStatusCallback& callback)
×
785
{
NEW
786
    CameraServer::CaptureStatusHandle handle;
×
NEW
787
    bool should_start_timer = false;
×
788

789
    {
NEW
790
        std::lock_guard<std::mutex> lg{_mutex};
×
NEW
791
        should_start_timer = _capture_status_callbacks.empty();
×
NEW
792
        handle = _capture_status_callbacks.subscribe(callback);
×
NEW
793
    }
×
794

NEW
795
    if (should_start_timer) {
×
NEW
796
        start_sending_capture_status();
×
797
    }
798

NEW
799
    return handle;
×
800
}
801

802
void CameraServerImpl::unsubscribe_capture_status(CameraServer::CaptureStatusHandle handle)
×
803
{
NEW
804
    bool should_stop_timer = false;
×
805

806
    {
NEW
807
        std::lock_guard<std::mutex> lg{_mutex};
×
NEW
808
        _capture_status_callbacks.unsubscribe(handle);
×
NEW
809
        should_stop_timer = _capture_status_callbacks.empty();
×
NEW
810
    }
×
811

NEW
812
    if (should_stop_timer) {
×
NEW
813
        stop_sending_capture_status();
×
814
    }
UNCOV
815
}
×
816

817
CameraServer::Result CameraServerImpl::respond_capture_status(
×
818
    CameraServer::CameraFeedback capture_status_feedback,
819
    CameraServer::CaptureStatus capture_status)
820
{
821
    {
822
        std::lock_guard<std::mutex> lg{_mutex};
×
823

824
        switch (capture_status_feedback) {
×
825
            default:
×
826
                // Fallthrough
827
            case CameraServer::CameraFeedback::Unknown:
828
                return CameraServer::Result::Error;
×
829
            case CameraServer::CameraFeedback::Ok: {
×
830
                auto command_ack = _server_component_impl->make_command_ack_message(
×
831
                    _last_capture_status_command, MAV_RESULT_ACCEPTED);
×
832
                _server_component_impl->send_command_ack(command_ack);
×
833
                // break and send capture status
834
                break;
×
835
            }
836
            case CameraServer::CameraFeedback::Busy: {
×
837
                auto command_ack = _server_component_impl->make_command_ack_message(
×
838
                    _last_capture_status_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
839
                _server_component_impl->send_command_ack(command_ack);
×
840
                return CameraServer::Result::Success;
×
841
            }
842
            case CameraServer::CameraFeedback::Failed: {
×
843
                auto command_ack = _server_component_impl->make_command_ack_message(
×
844
                    _last_capture_status_command, MAV_RESULT_FAILED);
×
845
                _server_component_impl->send_command_ack(command_ack);
×
846
                return CameraServer::Result::Success;
×
847
            }
848
        }
849

850
        _capture_status = capture_status;
×
851
    }
×
852

853
    send_capture_status();
×
854

855
    return CameraServer::Result::Success;
×
856
}
857

858
CameraServer::FormatStorageHandle
859
CameraServerImpl::subscribe_format_storage(const CameraServer::FormatStorageCallback& callback)
1✔
860
{
861
    std::lock_guard<std::mutex> lg{_mutex};
1✔
862
    return _format_storage_callbacks.subscribe(callback);
1✔
863
}
1✔
864
void CameraServerImpl::unsubscribe_format_storage(CameraServer::FormatStorageHandle handle)
1✔
865
{
866
    std::lock_guard<std::mutex> lg{_mutex};
1✔
867
    _format_storage_callbacks.unsubscribe(handle);
1✔
868
}
1✔
869

870
CameraServer::Result
871
CameraServerImpl::respond_format_storage(CameraServer::CameraFeedback format_storage_feedback)
1✔
872
{
873
    std::lock_guard<std::mutex> lg{_mutex};
1✔
874

875
    switch (format_storage_feedback) {
1✔
876
        default:
×
877
            // Fallthrough
878
        case CameraServer::CameraFeedback::Unknown:
879
            return CameraServer::Result::Error;
×
880
        case CameraServer::CameraFeedback::Ok: {
1✔
881
            auto command_ack = _server_component_impl->make_command_ack_message(
1✔
882
                _last_format_storage_command, MAV_RESULT_ACCEPTED);
1✔
883
            _server_component_impl->send_command_ack(command_ack);
1✔
884
            return CameraServer::Result::Success;
1✔
885
        }
886
        case CameraServer::CameraFeedback::Busy: {
×
887
            auto command_ack = _server_component_impl->make_command_ack_message(
×
888
                _last_format_storage_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
889
            _server_component_impl->send_command_ack(command_ack);
×
890
            return CameraServer::Result::Success;
×
891
        }
892
        case CameraServer::CameraFeedback::Failed: {
×
893
            auto command_ack = _server_component_impl->make_command_ack_message(
×
894
                _last_format_storage_command, MAV_RESULT_FAILED);
×
895
            _server_component_impl->send_command_ack(command_ack);
×
896
            return CameraServer::Result::Success;
×
897
        }
898
    }
899
}
1✔
900

901
CameraServer::ResetSettingsHandle
902
CameraServerImpl::subscribe_reset_settings(const CameraServer::ResetSettingsCallback& callback)
1✔
903
{
904
    std::lock_guard<std::mutex> lg{_mutex};
1✔
905
    return _reset_settings_callbacks.subscribe(callback);
1✔
906
}
1✔
907

908
void CameraServerImpl::unsubscribe_reset_settings(CameraServer::ResetSettingsHandle handle)
×
909
{
910
    std::lock_guard<std::mutex> lg{_mutex};
×
911
    _reset_settings_callbacks.unsubscribe(handle);
×
912
}
×
913

914
CameraServer::Result
915
CameraServerImpl::respond_reset_settings(CameraServer::CameraFeedback reset_settings_feedback)
1✔
916
{
917
    std::lock_guard<std::mutex> lg{_mutex};
1✔
918

919
    switch (reset_settings_feedback) {
1✔
920
        default:
×
921
            // Fallthrough
922
        case CameraServer::CameraFeedback::Unknown:
923
            return CameraServer::Result::Error;
×
924
        case CameraServer::CameraFeedback::Ok: {
1✔
925
            auto command_ack = _server_component_impl->make_command_ack_message(
1✔
926
                _last_reset_settings_command, MAV_RESULT_ACCEPTED);
1✔
927
            _server_component_impl->send_command_ack(command_ack);
1✔
928
            return CameraServer::Result::Success;
1✔
929
        }
930
        case CameraServer::CameraFeedback::Busy: {
×
931
            auto command_ack = _server_component_impl->make_command_ack_message(
×
932
                _last_reset_settings_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
933
            _server_component_impl->send_command_ack(command_ack);
×
934
            return CameraServer::Result::Success;
×
935
        }
936
        case CameraServer::CameraFeedback::Failed: {
×
937
            auto command_ack = _server_component_impl->make_command_ack_message(
×
938
                _last_reset_settings_command, MAV_RESULT_FAILED);
×
939
            _server_component_impl->send_command_ack(command_ack);
×
940
            return CameraServer::Result::Success;
×
941
        }
942
    }
943
}
1✔
944

945
CameraServer::TrackingPointCommandHandle CameraServerImpl::subscribe_tracking_point_command(
×
946
    const CameraServer::TrackingPointCommandCallback& callback)
947
{
948
    std::lock_guard<std::mutex> lg{_mutex};
×
949
    return _tracking_point_callbacks.subscribe(callback);
×
950
}
×
951

952
void CameraServerImpl::unsubscribe_tracking_point_command(
×
953
    CameraServer::TrackingPointCommandHandle handle)
954
{
955
    std::lock_guard<std::mutex> lg{_mutex};
×
956
    _tracking_point_callbacks.unsubscribe(handle);
×
957
}
×
958

959
CameraServer::Result CameraServerImpl::respond_tracking_point_command(
×
960
    CameraServer::CameraFeedback tracking_point_feedback)
961
{
962
    std::lock_guard<std::mutex> lg{_mutex};
×
963

964
    switch (tracking_point_feedback) {
×
965
        default:
×
966
            // Fallthrough
967
        case CameraServer::CameraFeedback::Unknown:
968
            return CameraServer::Result::Error;
×
969
        case CameraServer::CameraFeedback::Ok: {
×
970
            auto command_ack = _server_component_impl->make_command_ack_message(
×
971
                _last_track_point_command, MAV_RESULT_ACCEPTED);
×
972
            _server_component_impl->send_command_ack(command_ack);
×
973
            break;
×
974
        }
975
        case CameraServer::CameraFeedback::Busy: {
×
976
            auto command_ack = _server_component_impl->make_command_ack_message(
×
977
                _last_track_point_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
978
            _server_component_impl->send_command_ack(command_ack);
×
979
            return CameraServer::Result::Success;
×
980
        }
981
        case CameraServer::CameraFeedback::Failed: {
×
982
            auto command_ack = _server_component_impl->make_command_ack_message(
×
983
                _last_track_point_command, MAV_RESULT_FAILED);
×
984
            _server_component_impl->send_command_ack(command_ack);
×
985
            return CameraServer::Result::Success;
×
986
        }
987
    }
988
    return CameraServer::Result::Success;
×
989
}
×
990

991
CameraServer::TrackingRectangleCommandHandle CameraServerImpl::subscribe_tracking_rectangle_command(
×
992
    const CameraServer::TrackingRectangleCommandCallback& callback)
993
{
994
    std::lock_guard<std::mutex> lg{_mutex};
×
995
    return _tracking_rectangle_callbacks.subscribe(callback);
×
996
}
×
997

998
void CameraServerImpl::unsubscribe_tracking_rectangle_command(
×
999
    CameraServer::TrackingRectangleCommandHandle handle)
1000
{
1001
    std::lock_guard<std::mutex> lg{_mutex};
×
1002
    _tracking_rectangle_callbacks.unsubscribe(handle);
×
1003
}
×
1004

1005
CameraServer::Result CameraServerImpl::respond_tracking_rectangle_command(
×
1006
    CameraServer::CameraFeedback tracking_rectangle_feedback)
1007
{
1008
    std::lock_guard<std::mutex> lg{_mutex};
×
1009

1010
    switch (tracking_rectangle_feedback) {
×
1011
        default:
×
1012
            // Fallthrough
1013
        case CameraServer::CameraFeedback::Unknown:
1014
            return CameraServer::Result::Error;
×
1015
        case CameraServer::CameraFeedback::Ok: {
×
1016
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1017
                _last_track_rectangle_command, MAV_RESULT_ACCEPTED);
×
1018
            _server_component_impl->send_command_ack(command_ack);
×
1019
            break;
×
1020
        }
1021
        case CameraServer::CameraFeedback::Busy: {
×
1022
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1023
                _last_track_rectangle_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
1024
            _server_component_impl->send_command_ack(command_ack);
×
1025
            return CameraServer::Result::Success;
×
1026
        }
1027
        case CameraServer::CameraFeedback::Failed: {
×
1028
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1029
                _last_track_rectangle_command, MAV_RESULT_FAILED);
×
1030
            _server_component_impl->send_command_ack(command_ack);
×
1031
            return CameraServer::Result::Success;
×
1032
        }
1033
    }
1034
    return CameraServer::Result::Success;
×
1035
}
×
1036

1037
CameraServer::TrackingOffCommandHandle CameraServerImpl::subscribe_tracking_off_command(
×
1038
    const CameraServer::TrackingOffCommandCallback& callback)
1039
{
1040
    std::lock_guard<std::mutex> lg{_mutex};
×
1041
    return _tracking_off_callbacks.subscribe(callback);
×
1042
}
×
1043

1044
void CameraServerImpl::unsubscribe_tracking_off_command(
×
1045
    CameraServer::TrackingOffCommandHandle handle)
1046
{
1047
    std::lock_guard<std::mutex> lg{_mutex};
×
1048
    _tracking_off_callbacks.unsubscribe(handle);
×
1049
}
×
1050

1051
CameraServer::Result
1052
CameraServerImpl::respond_tracking_off_command(CameraServer::CameraFeedback tracking_off_feedback)
×
1053
{
1054
    std::lock_guard<std::mutex> lg{_mutex};
×
1055

1056
    switch (tracking_off_feedback) {
×
1057
        default:
×
1058
            // Fallthrough
1059
        case CameraServer::CameraFeedback::Unknown:
1060
            return CameraServer::Result::Error;
×
1061
        case CameraServer::CameraFeedback::Ok: {
×
1062
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1063
                _last_tracking_off_command, MAV_RESULT_ACCEPTED);
×
1064
            _server_component_impl->send_command_ack(command_ack);
×
1065
            break;
×
1066
        }
1067
        case CameraServer::CameraFeedback::Busy: {
×
1068
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1069
                _last_tracking_off_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
1070
            _server_component_impl->send_command_ack(command_ack);
×
1071
            return CameraServer::Result::Success;
×
1072
        }
1073
        case CameraServer::CameraFeedback::Failed: {
×
1074
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1075
                _last_tracking_off_command, MAV_RESULT_FAILED);
×
1076
            _server_component_impl->send_command_ack(command_ack);
×
1077
            return CameraServer::Result::Success;
×
1078
        }
1079
    }
1080
    return CameraServer::Result::Success;
×
1081
}
×
1082

1083
void CameraServerImpl::start_image_capture_interval(float interval_s, int32_t count, int32_t index)
1✔
1084
{
1085
    // If count == 0, it means capture "forever" until a stop command is received.
1086
    auto remaining = std::make_shared<int32_t>(count == 0 ? INT32_MAX : count);
1✔
1087

1088
    _last_interval_index = index;
1✔
1089
    _image_capture_timer_cookie = _server_component_impl->add_call_every(
1✔
1090
        [this, remaining]() {
15✔
1091
            LogDebug() << "capture image timer triggered";
3✔
1092

1093
            if (!_take_photo_callbacks.empty()) {
3✔
1094
                _take_photo_callbacks.queue(_last_interval_index++, [this](const auto& func) {
3✔
1095
                    _server_component_impl->call_user_callback(func);
3✔
1096
                });
3✔
1097
                (*remaining)--;
3✔
1098
            }
1099

1100
            if (*remaining == 0) {
3✔
1101
                stop_image_capture_interval();
×
1102
            }
1103
        },
3✔
1104
        interval_s);
1105
    _is_image_capture_interval_set = true;
1✔
1106
    _image_capture_timer_interval_s = interval_s;
1✔
1107
}
1✔
1108

1109
void CameraServerImpl::stop_image_capture_interval()
12✔
1110
{
1111
    _server_component_impl->remove_call_every(_image_capture_timer_cookie);
12✔
1112

1113
    std::lock_guard<std::mutex> lg{_mutex};
12✔
1114
    _is_image_capture_interval_set = false;
12✔
1115
    _image_capture_timer_interval_s = 0;
12✔
1116
}
12✔
1117

1118
std::optional<mavlink_command_ack_t> CameraServerImpl::process_camera_information_request(
×
1119
    const MavlinkCommandReceiver::CommandLong& command)
1120
{
1121
    LogDebug() << "Camera info request";
×
1122

1123
    if (static_cast<int>(command.params.param1) == 0) {
×
1124
        return _server_component_impl->make_command_ack_message(
×
1125
            command, MAV_RESULT::MAV_RESULT_ACCEPTED);
×
1126
    }
1127

1128
    return send_camera_information(command);
×
1129
}
1130

1131
std::optional<mavlink_command_ack_t>
1132
CameraServerImpl::process_request_message(const MavlinkCommandReceiver::CommandLong& command)
97✔
1133
{
1134
    switch (static_cast<int>(command.params.param1)) {
97✔
1135
        case MAVLINK_MSG_ID_CAMERA_INFORMATION:
9✔
1136
            return send_camera_information(command);
9✔
1137

1138
        case MAVLINK_MSG_ID_CAMERA_CAPTURE_STATUS:
9✔
1139
            send_capture_status();
9✔
1140
            return _server_component_impl->make_command_ack_message(
18✔
1141
                command, MAV_RESULT::MAV_RESULT_ACCEPTED);
18✔
1142

1143
        default:
79✔
1144
            LogWarn() << "Got unknown request message!";
79✔
1145
            return _server_component_impl->make_command_ack_message(
158✔
1146
                command, MAV_RESULT::MAV_RESULT_DENIED);
158✔
1147
    }
1148
}
1149

1150
std::optional<mavlink_command_ack_t>
1151
CameraServerImpl::send_camera_information(const MavlinkCommandReceiver::CommandLong& command)
9✔
1152
{
1153
    std::lock_guard<std::mutex> lg{_mutex};
9✔
1154

1155
    if (!_is_information_set) {
9✔
1156
        return _server_component_impl->make_command_ack_message(
×
1157
            command, MAV_RESULT::MAV_RESULT_TEMPORARILY_REJECTED);
×
1158
    }
1159

1160
    // ack needs to be sent before camera information message
1161
    auto command_ack =
9✔
1162
        _server_component_impl->make_command_ack_message(command, MAV_RESULT::MAV_RESULT_ACCEPTED);
9✔
1163
    _server_component_impl->send_command_ack(command_ack);
9✔
1164

1165
    // It is safe to ignore the return value of parse_version_string() here
1166
    // since the string was already validated in set_information().
1167
    uint32_t firmware_version;
9✔
1168
    parse_version_string(_information.firmware_version, firmware_version);
9✔
1169

1170
    // capability flags are determined by subscriptions
1171
    uint32_t capability_flags{};
9✔
1172

1173
    if (!_take_photo_callbacks.empty()) {
9✔
1174
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_CAPTURE_IMAGE;
2✔
1175
    }
1176

1177
    if (!_start_video_callbacks.empty()) {
9✔
1178
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_CAPTURE_VIDEO;
×
1179
    }
1180

1181
    if (!_set_mode_callbacks.empty()) {
9✔
1182
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_HAS_MODES;
2✔
1183
    }
1184

1185
    if (_information.image_in_video_mode_supported) {
9✔
1186
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_CAN_CAPTURE_IMAGE_IN_VIDEO_MODE;
×
1187
    }
1188

1189
    if (_information.video_in_image_mode_supported) {
9✔
1190
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_CAN_CAPTURE_VIDEO_IN_IMAGE_MODE;
×
1191
    }
1192

1193
    if (_is_video_streaming_set) {
9✔
1194
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_HAS_VIDEO_STREAM;
1✔
1195
    }
1196

1197
    if (!_tracking_point_callbacks.empty()) {
9✔
1198
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_HAS_TRACKING_POINT;
×
1199
    }
1200

1201
    if (!_tracking_rectangle_callbacks.empty()) {
9✔
1202
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_HAS_TRACKING_RECTANGLE;
×
1203
    }
1204

1205
    if (!_zoom_range_callbacks.empty() || !_zoom_in_start_callbacks.empty() ||
18✔
1206
        !_zoom_out_start_callbacks.empty()) {
9✔
1207
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_HAS_BASIC_ZOOM;
×
1208
    }
1209

1210
    _information.vendor_name.resize(sizeof(mavlink_camera_information_t::vendor_name));
9✔
1211
    _information.model_name.resize(sizeof(mavlink_camera_information_t::model_name));
9✔
1212
    _information.definition_file_uri.resize(
9✔
1213
        sizeof(mavlink_camera_information_t::cam_definition_uri));
1214

1215
    _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
9✔
1216
        mavlink_message_t message{};
9✔
1217
        mavlink_msg_camera_information_pack_chan(
27✔
1218
            mavlink_address.system_id,
9✔
1219
            mavlink_address.component_id,
9✔
1220
            channel,
1221
            &message,
1222
            static_cast<uint32_t>(_server_component_impl->get_time().elapsed_s() * 1e3),
18✔
1223
            reinterpret_cast<const uint8_t*>(_information.vendor_name.c_str()),
9✔
1224
            reinterpret_cast<const uint8_t*>(_information.model_name.c_str()),
9✔
1225
            firmware_version,
9✔
1226
            _information.focal_length_mm,
1227
            _information.horizontal_sensor_size_mm,
1228
            _information.vertical_sensor_size_mm,
1229
            _information.horizontal_resolution_px,
9✔
1230
            _information.vertical_resolution_px,
9✔
1231
            _information.lens_id,
9✔
1232
            capability_flags,
9✔
1233
            _information.definition_file_version,
9✔
1234
            _information.definition_file_uri.c_str(),
1235
            0,
1236
            0);
1237
        return message;
9✔
1238
    });
1239

1240
    return std::nullopt;
9✔
1241
}
9✔
1242

1243
std::optional<mavlink_command_ack_t> CameraServerImpl::process_camera_settings_request(
9✔
1244
    const MavlinkCommandReceiver::CommandLong& command)
1245
{
1246
    auto settings = static_cast<bool>(command.params.param1);
9✔
1247

1248
    if (!settings) {
9✔
1249
        return _server_component_impl->make_command_ack_message(
×
1250
            command, MAV_RESULT::MAV_RESULT_ACCEPTED);
×
1251
    }
1252

1253
    // ack needs to be sent before camera information message
1254
    auto command_ack =
9✔
1255
        _server_component_impl->make_command_ack_message(command, MAV_RESULT::MAV_RESULT_ACCEPTED);
9✔
1256
    _server_component_impl->send_command_ack(command_ack);
9✔
1257
    LogDebug() << "sent settings ack";
9✔
1258

1259
    // unsupported
1260
    const auto mode_id = CAMERA_MODE::CAMERA_MODE_IMAGE;
9✔
1261
    const float zoom_level = 0;
9✔
1262
    const float focus_level = 0;
9✔
1263

1264
    _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
9✔
1265
        mavlink_message_t message{};
9✔
1266
        mavlink_msg_camera_settings_pack_chan(
18✔
1267
            mavlink_address.system_id,
9✔
1268
            mavlink_address.component_id,
9✔
1269
            channel,
1270
            &message,
1271
            static_cast<uint32_t>(_server_component_impl->get_time().elapsed_s() * 1e3),
9✔
1272
            mode_id,
1273
            zoom_level,
9✔
1274
            focus_level,
9✔
1275
            0);
1276
        return message;
9✔
1277
    });
1278
    LogDebug() << "sent settings msg";
9✔
1279

1280
    // ack was already sent
1281
    return std::nullopt;
9✔
1282
}
1283

1284
std::optional<mavlink_command_ack_t> CameraServerImpl::process_storage_information_request(
16✔
1285
    const MavlinkCommandReceiver::CommandLong& command)
1286
{
1287
    auto storage_id = static_cast<uint8_t>(command.params.param1);
16✔
1288
    auto information = static_cast<bool>(command.params.param2);
16✔
1289

1290
    if (!information) {
16✔
1291
        return _server_component_impl->make_command_ack_message(
×
1292
            command, MAV_RESULT::MAV_RESULT_ACCEPTED);
×
1293
    }
1294

1295
    std::lock_guard<std::mutex> lg{_mutex};
16✔
1296

1297
    if (_storage_information_callbacks.empty()) {
16✔
1298
        LogDebug()
28✔
1299
            << "Get storage information requested with no set storage information subscriber";
28✔
1300
        return _server_component_impl->make_command_ack_message(
28✔
1301
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
28✔
1302
    }
1303

1304
    // TODO may need support multi storage id
1305
    _last_storage_id = storage_id;
2✔
1306

1307
    _last_storage_information_command = command;
2✔
1308

1309
    _storage_information_callbacks.queue(
2✔
1310
        storage_id, [this](const auto& func) { _server_component_impl->call_user_callback(func); });
2✔
1311

1312
    // ack will be sent later in respond_storage_information
1313
    return std::nullopt;
2✔
1314
}
16✔
1315

1316
std::optional<mavlink_command_ack_t>
1317
CameraServerImpl::process_storage_format(const MavlinkCommandReceiver::CommandLong& command)
1✔
1318
{
1319
    auto storage_id = static_cast<uint8_t>(command.params.param1);
1✔
1320
    auto format = static_cast<bool>(command.params.param2);
1✔
1321
    auto reset_image_log = static_cast<bool>(command.params.param3);
1✔
1322

1323
    UNUSED(format);
1✔
1324
    UNUSED(reset_image_log);
1✔
1325

1326
    std::lock_guard<std::mutex> lg{_mutex};
1✔
1327

1328
    if (_format_storage_callbacks.empty()) {
1✔
1329
        LogDebug() << "process storage format requested with no storage format subscriber";
×
1330
        return _server_component_impl->make_command_ack_message(
×
1331
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1332
    }
1333

1334
    _last_format_storage_command = command;
1✔
1335

1336
    _format_storage_callbacks.queue(
1✔
1337
        storage_id, [this](const auto& func) { _server_component_impl->call_user_callback(func); });
1✔
1338

1339
    return std::nullopt;
1✔
1340
}
1✔
1341

1342
std::optional<mavlink_command_ack_t> CameraServerImpl::process_camera_capture_status_request(
×
1343
    const MavlinkCommandReceiver::CommandLong& command)
1344
{
1345
    auto capture_status = static_cast<bool>(command.params.param1);
×
1346

1347
    if (!capture_status) {
×
1348
        return _server_component_impl->make_command_ack_message(
×
1349
            command, MAV_RESULT::MAV_RESULT_ACCEPTED);
×
1350
    }
1351

1352
    std::lock_guard<std::mutex> lg{_mutex};
×
1353
    if (_capture_status_callbacks.empty()) {
×
1354
        LogDebug() << "process camera capture status requested with no capture status subscriber";
×
1355
        return _server_component_impl->make_command_ack_message(
×
1356
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1357
    }
1358

1359
    _last_capture_status_command = command;
×
1360

1361
    // may not need param for now ,just use zero
1362
    _capture_status_callbacks.queue(
×
1363
        0, [this](const auto& func) { _server_component_impl->call_user_callback(func); });
×
1364

1365
    // ack was already sent
1366
    return std::nullopt;
×
1367
}
×
1368

1369
void CameraServerImpl::send_capture_status()
13✔
1370
{
1371
    std::lock_guard<std::mutex> lg{_mutex};
13✔
1372

1373
    uint8_t image_status{};
13✔
1374
    if (_capture_status.image_status ==
13✔
1375
            CameraServer::CaptureStatus::ImageStatus::CaptureInProgress ||
13✔
1376
        _capture_status.image_status ==
13✔
1377
            CameraServer::CaptureStatus::ImageStatus::IntervalInProgress) {
1378
        image_status |= StatusFlags::IN_PROGRESS;
×
1379
    }
1380

1381
    if (_capture_status.image_status == CameraServer::CaptureStatus::ImageStatus::IntervalIdle ||
13✔
1382
        _capture_status.image_status ==
13✔
1383
            CameraServer::CaptureStatus::ImageStatus::IntervalInProgress ||
13✔
1384
        _is_image_capture_interval_set) {
13✔
1385
        image_status |= StatusFlags::INTERVAL_SET;
3✔
1386
    }
1387

1388
    uint8_t video_status = 0;
13✔
1389
    if (_capture_status.video_status == CameraServer::CaptureStatus::VideoStatus::Idle) {
13✔
1390
        video_status = 0;
13✔
1391
    } else if (
×
1392
        _capture_status.video_status ==
×
1393
        CameraServer::CaptureStatus::VideoStatus::CaptureInProgress) {
1394
        video_status = 1;
×
1395
    }
1396

1397
    const uint32_t recording_time_ms =
13✔
1398
        static_cast<uint32_t>(static_cast<double>(_capture_status.recording_time_s) * 1e3);
13✔
1399
    const float available_capacity = _capture_status.available_capacity_mib;
13✔
1400

1401
    _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
13✔
1402
        mavlink_message_t message{};
13✔
1403
        mavlink_msg_camera_capture_status_pack_chan(
26✔
1404
            mavlink_address.system_id,
13✔
1405
            mavlink_address.component_id,
13✔
1406
            channel,
1407
            &message,
1408
            static_cast<uint32_t>(_server_component_impl->get_time().elapsed_s() * 1e3),
13✔
1409
            image_status,
13✔
1410
            video_status,
13✔
1411
            _image_capture_timer_interval_s,
1412
            recording_time_ms,
13✔
1413
            available_capacity,
13✔
1414
            _image_capture_count,
1415
            0);
1416
        return message;
13✔
1417
    });
1418
}
13✔
1419

1420
std::optional<mavlink_command_ack_t>
1421
CameraServerImpl::process_reset_camera_settings(const MavlinkCommandReceiver::CommandLong& command)
1✔
1422
{
1423
    auto reset = static_cast<bool>(command.params.param1);
1✔
1424

1425
    UNUSED(reset);
1✔
1426

1427
    std::lock_guard<std::mutex> lg{_mutex};
1✔
1428

1429
    if (_reset_settings_callbacks.empty()) {
1✔
1430
        LogDebug() << "reset camera settings requested with no camera settings subscriber";
×
1431
        return _server_component_impl->make_command_ack_message(
×
1432
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1433
    }
1434

1435
    _last_reset_settings_command = command;
1✔
1436
    _reset_settings_callbacks.queue(
1✔
1437
        0, [this](const auto& func) { _server_component_impl->call_user_callback(func); });
1✔
1438

1439
    return std::nullopt;
1✔
1440
}
1✔
1441

1442
std::optional<mavlink_command_ack_t>
1443
CameraServerImpl::process_set_camera_mode(const MavlinkCommandReceiver::CommandLong& command)
2✔
1444
{
1445
    auto camera_mode = static_cast<CAMERA_MODE>(command.params.param2);
2✔
1446

1447
    std::lock_guard<std::mutex> lg{_mutex};
2✔
1448

1449
    if (_set_mode_callbacks.empty()) {
2✔
1450
        LogDebug() << "Set mode requested with no set mode subscriber";
×
1451
        return _server_component_impl->make_command_ack_message(
×
1452
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1453
    }
1454

1455
    // convert camera mode enum type
1456
    CameraServer::Mode convert_camera_mode = CameraServer::Mode::Unknown;
2✔
1457
    if (camera_mode == CAMERA_MODE_IMAGE) {
2✔
1458
        convert_camera_mode = CameraServer::Mode::Photo;
1✔
1459
    } else if (camera_mode == CAMERA_MODE_VIDEO) {
1✔
1460
        convert_camera_mode = CameraServer::Mode::Video;
1✔
1461
    }
1462

1463
    if (convert_camera_mode == CameraServer::Mode::Unknown) {
2✔
1464
        return _server_component_impl->make_command_ack_message(
×
1465
            command, MAV_RESULT::MAV_RESULT_DENIED);
×
1466
    }
1467

1468
    _last_set_mode_command = command;
2✔
1469

1470
    _set_mode_callbacks.queue(convert_camera_mode, [this](const auto& func) {
2✔
1471
        _server_component_impl->call_user_callback(func);
2✔
1472
    });
2✔
1473

1474
    return std::nullopt;
2✔
1475
}
2✔
1476

1477
std::optional<mavlink_command_ack_t>
1478
CameraServerImpl::process_set_camera_zoom(const MavlinkCommandReceiver::CommandLong& command)
×
1479
{
1480
    auto zoom_type = static_cast<CAMERA_ZOOM_TYPE>(command.params.param1);
×
1481
    auto zoom_value = command.params.param2;
×
1482

1483
    std::lock_guard<std::mutex> lg{_mutex};
×
1484

1485
    if (_zoom_in_start_callbacks.empty() && _zoom_out_start_callbacks.empty() &&
×
1486
        _zoom_stop_callbacks.empty() && _zoom_range_callbacks.empty()) {
×
1487
        LogWarn() << "No camera zoom is supported";
×
1488
        return _server_component_impl->make_command_ack_message(
×
1489
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1490
    }
1491

1492
    auto unsupported = [&]() {
×
1493
        LogWarn() << "unsupported set camera zoom type (" << (int)zoom_type << ") request";
×
1494
    };
×
1495

1496
    switch (zoom_type) {
×
1497
        case ZOOM_TYPE_CONTINUOUS:
×
1498
            if (zoom_value == -1.f) {
×
1499
                if (_zoom_out_start_callbacks.empty()) {
×
1500
                    unsupported();
×
1501
                    return _server_component_impl->make_command_ack_message(
×
1502
                        command, MAV_RESULT::MAV_RESULT_DENIED);
×
1503
                } else {
1504
                    _last_zoom_out_start_command = command;
×
1505
                    int dummy = 0;
×
1506
                    _zoom_out_start_callbacks.queue(dummy, [this](const auto& func) {
×
1507
                        _server_component_impl->call_user_callback(func);
×
1508
                    });
×
1509
                }
1510
            } else if (zoom_value == 1.f) {
×
1511
                if (_zoom_in_start_callbacks.empty()) {
×
1512
                    unsupported();
×
1513
                    return _server_component_impl->make_command_ack_message(
×
1514
                        command, MAV_RESULT::MAV_RESULT_DENIED);
×
1515
                } else {
1516
                    _last_zoom_in_start_command = command;
×
1517
                    int dummy = 0;
×
1518
                    _zoom_in_start_callbacks.queue(dummy, [this](const auto& func) {
×
1519
                        _server_component_impl->call_user_callback(func);
×
1520
                    });
×
1521
                }
1522
            } else if (zoom_value == 0.f) {
×
1523
                if (_zoom_stop_callbacks.empty()) {
×
1524
                    unsupported();
×
1525
                    return _server_component_impl->make_command_ack_message(
×
1526
                        command, MAV_RESULT::MAV_RESULT_DENIED);
×
1527
                } else {
1528
                    _last_zoom_stop_command = command;
×
1529
                    int dummy = 0;
×
1530
                    _zoom_stop_callbacks.queue(dummy, [this](const auto& func) {
×
1531
                        _server_component_impl->call_user_callback(func);
×
1532
                    });
×
1533
                }
1534
            } else {
1535
                LogWarn() << "Invalid zoom value";
×
1536
                return _server_component_impl->make_command_ack_message(
×
1537
                    command, MAV_RESULT::MAV_RESULT_DENIED);
×
1538
            }
1539
            break;
×
1540
        case ZOOM_TYPE_RANGE:
×
1541
            if (_zoom_range_callbacks.empty()) {
×
1542
                unsupported();
×
1543
                return _server_component_impl->make_command_ack_message(
×
1544
                    command, MAV_RESULT::MAV_RESULT_DENIED);
×
1545

1546
            } else {
1547
                _last_zoom_range_command = command;
×
1548
                _zoom_range_callbacks.queue(zoom_value, [this](const auto& func) {
×
1549
                    _server_component_impl->call_user_callback(func);
×
1550
                });
×
1551
            }
1552
            break;
×
1553
        case ZOOM_TYPE_STEP:
×
1554
        // Fallthrough
1555
        case ZOOM_TYPE_FOCAL_LENGTH:
1556
        // Fallthrough
1557
        case ZOOM_TYPE_HORIZONTAL_FOV:
1558
        // Fallthrough
1559
        default:
1560
            unsupported();
×
1561
            return _server_component_impl->make_command_ack_message(
×
1562
                command, MAV_RESULT::MAV_RESULT_DENIED);
×
1563
            break;
1564
    }
1565

1566
    // For any success so far, we don't ack yet, but later when the respond function is called.
1567
    return std::nullopt;
×
1568
}
×
1569

1570
std::optional<mavlink_command_ack_t>
1571
CameraServerImpl::process_set_camera_focus(const MavlinkCommandReceiver::CommandLong& command)
×
1572
{
1573
    auto focus_type = static_cast<SET_FOCUS_TYPE>(command.params.param1);
×
1574
    auto focus_value = command.params.param2;
×
1575

1576
    UNUSED(focus_type);
×
1577
    UNUSED(focus_value);
×
1578

1579
    LogDebug() << "unsupported set camera focus request";
×
1580

1581
    return _server_component_impl->make_command_ack_message(
×
1582
        command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1583
}
1584

1585
std::optional<mavlink_command_ack_t>
1586
CameraServerImpl::process_set_storage_usage(const MavlinkCommandReceiver::CommandLong& command)
×
1587
{
1588
    auto storage_id = static_cast<uint8_t>(command.params.param1);
×
1589
    auto usage = static_cast<STORAGE_USAGE_FLAG>(command.params.param2);
×
1590

1591
    UNUSED(storage_id);
×
1592
    UNUSED(usage);
×
1593

1594
    LogDebug() << "unsupported set storage usage request";
×
1595

1596
    return _server_component_impl->make_command_ack_message(
×
1597
        command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1598
}
1599

1600
std::optional<mavlink_command_ack_t>
1601
CameraServerImpl::process_image_start_capture(const MavlinkCommandReceiver::CommandLong& command)
2✔
1602
{
1603
    auto interval_s = command.params.param2;
2✔
1604
    auto total_images = static_cast<int32_t>(command.params.param3);
2✔
1605
    auto seq_number = static_cast<int32_t>(command.params.param4);
2✔
1606

1607
    LogDebug() << "received image start capture request - interval: " << +interval_s
6✔
1608
               << " total: " << +total_images << " index: " << +seq_number;
6✔
1609

1610
    // TODO: validate parameters and return MAV_RESULT_DENIED not valid
1611

1612
    stop_image_capture_interval();
2✔
1613

1614
    std::lock_guard<std::mutex> lg{_mutex};
2✔
1615

1616
    if (_take_photo_callbacks.empty()) {
2✔
1617
        LogDebug() << "image capture requested with no take photo subscriber";
×
1618
        return _server_component_impl->make_command_ack_message(
×
1619
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1620
    }
1621

1622
    // single image capture
1623
    if (total_images == 1) {
2✔
1624
        // MAV_RESULT_ACCEPTED must be sent before CAMERA_IMAGE_CAPTURED
1625
        auto command_ack = _server_component_impl->make_command_ack_message(
1✔
1626
            command, MAV_RESULT::MAV_RESULT_IN_PROGRESS);
1627
        _server_component_impl->send_command_ack(command_ack);
1✔
1628

1629
        _last_take_photo_command = command;
1✔
1630

1631
        _take_photo_callbacks.queue(seq_number, [this](const auto& func) {
1✔
1632
            _server_component_impl->call_user_callback(func);
1✔
1633
        });
1✔
1634

1635
        return std::nullopt;
1✔
1636
    }
1637

1638
    start_image_capture_interval(interval_s, total_images, seq_number);
1✔
1639

1640
    return _server_component_impl->make_command_ack_message(
2✔
1641
        command, MAV_RESULT::MAV_RESULT_ACCEPTED);
2✔
1642
}
2✔
1643

1644
std::optional<mavlink_command_ack_t>
1645
CameraServerImpl::process_image_stop_capture(const MavlinkCommandReceiver::CommandLong& command)
1✔
1646
{
1647
    LogDebug() << "received image stop capture request";
1✔
1648

1649
    // REVISIT: should we return something other that MAV_RESULT_ACCEPTED if
1650
    // there is not currently a capture interval active?
1651
    stop_image_capture_interval();
1✔
1652

1653
    return _server_component_impl->make_command_ack_message(
2✔
1654
        command, MAV_RESULT::MAV_RESULT_ACCEPTED);
2✔
1655
}
1656

1657
std::optional<mavlink_command_ack_t> CameraServerImpl::process_camera_image_capture_request(
×
1658
    const MavlinkCommandReceiver::CommandLong& command)
1659
{
1660
    auto seq_number = static_cast<uint32_t>(command.params.param1);
×
1661

1662
    UNUSED(seq_number);
×
1663

1664
    LogDebug() << "unsupported image capture request";
×
1665

1666
    return _server_component_impl->make_command_ack_message(
×
1667
        command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1668
}
1669

1670
std::optional<mavlink_command_ack_t>
1671
CameraServerImpl::process_video_start_capture(const MavlinkCommandReceiver::CommandLong& command)
×
1672
{
1673
    auto stream_id = static_cast<uint8_t>(command.params.param1);
×
1674
    auto status_frequency = command.params.param2;
×
1675

1676
    UNUSED(status_frequency);
×
1677

1678
    std::lock_guard<std::mutex> lg{_mutex};
×
1679

1680
    if (_start_video_callbacks.empty()) {
×
1681
        LogDebug() << "video start capture requested with no video start capture subscriber";
×
1682
        return _server_component_impl->make_command_ack_message(
×
1683
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1684
    }
1685

1686
    _last_start_video_command = command;
×
1687
    _start_video_callbacks.queue(
×
1688
        stream_id, [this](const auto& func) { _server_component_impl->call_user_callback(func); });
×
1689

1690
    return std::nullopt;
×
1691
}
×
1692

1693
std::optional<mavlink_command_ack_t>
1694
CameraServerImpl::process_video_stop_capture(const MavlinkCommandReceiver::CommandLong& command)
×
1695
{
1696
    auto stream_id = static_cast<uint8_t>(command.params.param1);
×
1697

1698
    std::lock_guard<std::mutex> lg{_mutex};
×
1699

1700
    if (_stop_video_callbacks.empty()) {
×
1701
        LogDebug() << "video stop capture requested with no video stop capture subscriber";
×
1702
        return _server_component_impl->make_command_ack_message(
×
1703
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1704
    }
1705

1706
    _last_stop_video_command = command;
×
1707
    _stop_video_callbacks.queue(
×
1708
        stream_id, [this](const auto& func) { _server_component_impl->call_user_callback(func); });
×
1709

1710
    return std::nullopt;
×
1711
}
×
1712

1713
std::optional<mavlink_command_ack_t>
1714
CameraServerImpl::process_video_start_streaming(const MavlinkCommandReceiver::CommandLong& command)
×
1715
{
1716
    auto stream_id = static_cast<uint8_t>(command.params.param1);
×
1717

1718
    std::lock_guard<std::mutex> lg{_mutex};
×
1719

1720
    if (_start_video_streaming_callbacks.empty()) {
×
1721
        LogDebug() << "video start streaming requested with no video start streaming subscriber";
×
1722
        return _server_component_impl->make_command_ack_message(
×
1723
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1724
    }
1725

1726
    _last_start_video_streaming_command = command;
×
1727
    _start_video_streaming_callbacks.queue(
×
1728
        stream_id, [this](const auto& func) { _server_component_impl->call_user_callback(func); });
×
1729

1730
    return std::nullopt;
×
1731
}
×
1732

1733
std::optional<mavlink_command_ack_t>
1734
CameraServerImpl::process_video_stop_streaming(const MavlinkCommandReceiver::CommandLong& command)
×
1735
{
1736
    auto stream_id = static_cast<uint8_t>(command.params.param1);
×
1737

1738
    std::lock_guard<std::mutex> lg{_mutex};
×
1739

1740
    if (_stop_video_streaming_callbacks.empty()) {
×
1741
        LogDebug() << "video stop streaming requested with no video stop streaming subscriber";
×
1742
        return _server_component_impl->make_command_ack_message(
×
1743
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1744
    }
1745

1746
    _last_stop_video_streaming_command = command;
×
1747
    _stop_video_streaming_callbacks.queue(
×
1748
        stream_id, [this](const auto& func) { _server_component_impl->call_user_callback(func); });
×
1749

1750
    return std::nullopt;
×
1751
}
×
1752

1753
std::optional<mavlink_command_ack_t> CameraServerImpl::process_video_stream_information_request(
15✔
1754
    const MavlinkCommandReceiver::CommandLong& command)
1755
{
1756
    auto stream_id = static_cast<uint8_t>(command.params.param1);
15✔
1757

1758
    UNUSED(stream_id);
15✔
1759

1760
    std::lock_guard<std::mutex> lg{_mutex};
15✔
1761

1762
    if (_is_video_streaming_set) {
15✔
1763
        auto command_ack = _server_component_impl->make_command_ack_message(
1✔
1764
            command, MAV_RESULT::MAV_RESULT_ACCEPTED);
1765
        _server_component_impl->send_command_ack(command_ack);
1✔
1766
        LogDebug() << "sent video streaming ack";
1✔
1767

1768
        const char name[32] = "";
1✔
1769

1770
        _video_streaming.rtsp_uri.resize(sizeof(mavlink_video_stream_information_t::uri));
1✔
1771

1772
        mavlink_message_t msg{};
1✔
1773
        mavlink_msg_video_stream_information_pack(
2✔
1774
            _server_component_impl->get_own_system_id(),
1✔
1775
            _server_component_impl->get_own_component_id(),
1✔
1776
            &msg,
1777
            0, // Stream id
1778
            0, // Count
1779
            VIDEO_STREAM_TYPE_RTSP,
1780
            VIDEO_STREAM_STATUS_FLAGS_RUNNING,
1781
            0, // famerate
1782
            0, // resolution horizontal
1783
            0, // resolution vertical
1784
            0, // bitrate
1785
            0, // rotation
1786
            0, // horizontal field of view
1787
            name,
1788
            _video_streaming.rtsp_uri.c_str(),
1789
            0,
1790
            0);
1791

1792
        _server_component_impl->send_message(msg);
1✔
1793

1794
        // Ack already sent.
1795
        return std::nullopt;
1✔
1796

1797
    } else {
1798
        return _server_component_impl->make_command_ack_message(
28✔
1799
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
28✔
1800
    }
1801
}
15✔
1802

1803
std::optional<mavlink_command_ack_t> CameraServerImpl::process_video_stream_status_request(
17✔
1804
    const MavlinkCommandReceiver::CommandLong& command)
1805
{
1806
    auto stream_id = static_cast<uint8_t>(command.params.param1);
17✔
1807

1808
    UNUSED(stream_id);
17✔
1809

1810
    std::lock_guard<std::mutex> lg{_mutex};
17✔
1811

1812
    if (!_is_video_streaming_set) {
17✔
1813
        return _server_component_impl->make_command_ack_message(
32✔
1814
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
32✔
1815
    }
1816

1817
    auto command_ack =
1✔
1818
        _server_component_impl->make_command_ack_message(command, MAV_RESULT::MAV_RESULT_ACCEPTED);
1✔
1819
    _server_component_impl->send_command_ack(command_ack);
1✔
1820
    LogDebug() << "sent video streaming ack";
1✔
1821

1822
    mavlink_message_t msg{};
1✔
1823
    mavlink_msg_video_stream_status_pack(
1✔
1824
        _server_component_impl->get_own_system_id(),
1✔
1825
        _server_component_impl->get_own_component_id(),
1✔
1826
        &msg,
1827
        0, // Stream id
1828
        VIDEO_STREAM_STATUS_FLAGS_RUNNING,
1829
        0, // framerate
1830
        0, // resolution horizontal
1831
        0, // resolution vertical
1832
        0, // bitrate
1833
        0, // rotation
1834
        0, // horizontal field of view
1835
        0);
1836
    _server_component_impl->send_message(msg);
1✔
1837

1838
    // ack was already sent
1839
    return std::nullopt;
1✔
1840
}
17✔
1841

1842
CameraServer::ZoomInStartHandle
1843
CameraServerImpl::subscribe_zoom_in_start(const CameraServer::ZoomInStartCallback& callback)
×
1844
{
1845
    std::lock_guard<std::mutex> lg{_mutex};
×
1846
    return _zoom_in_start_callbacks.subscribe(callback);
×
1847
}
×
1848

1849
void CameraServerImpl::unsubscribe_zoom_in_start(CameraServer::ZoomInStartHandle handle)
×
1850
{
1851
    std::lock_guard<std::mutex> lg{_mutex};
×
1852
    _zoom_in_start_callbacks.unsubscribe(handle);
×
1853
}
×
1854

1855
CameraServer::Result
1856
CameraServerImpl::respond_zoom_in_start(CameraServer::CameraFeedback zoom_in_start_feedback)
×
1857
{
1858
    std::lock_guard<std::mutex> lg{_mutex};
×
1859

1860
    switch (zoom_in_start_feedback) {
×
1861
        default:
×
1862
            // Fallthrough
1863
        case CameraServer::CameraFeedback::Unknown:
1864
            return CameraServer::Result::Error;
×
1865
        case CameraServer::CameraFeedback::Ok: {
×
1866
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1867
                _last_zoom_in_start_command, MAV_RESULT_ACCEPTED);
×
1868
            _server_component_impl->send_command_ack(command_ack);
×
1869
            return CameraServer::Result::Success;
×
1870
        }
1871
        case CameraServer::CameraFeedback::Busy: {
×
1872
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1873
                _last_zoom_in_start_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
1874
            _server_component_impl->send_command_ack(command_ack);
×
1875
            return CameraServer::Result::Success;
×
1876
        }
1877
        case CameraServer::CameraFeedback::Failed: {
×
1878
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1879
                _last_zoom_in_start_command, MAV_RESULT_FAILED);
×
1880
            _server_component_impl->send_command_ack(command_ack);
×
1881
            return CameraServer::Result::Success;
×
1882
        }
1883
    }
1884
}
×
1885

1886
CameraServer::ZoomOutStartHandle
1887
CameraServerImpl::subscribe_zoom_out_start(const CameraServer::ZoomOutStartCallback& callback)
×
1888
{
1889
    std::lock_guard<std::mutex> lg{_mutex};
×
1890
    return _zoom_out_start_callbacks.subscribe(callback);
×
1891
}
×
1892

1893
void CameraServerImpl::unsubscribe_zoom_out_start(CameraServer::ZoomOutStartHandle handle)
×
1894
{
1895
    std::lock_guard<std::mutex> lg{_mutex};
×
1896
    _zoom_out_start_callbacks.unsubscribe(handle);
×
1897
}
×
1898

1899
CameraServer::Result
1900
CameraServerImpl::respond_zoom_out_start(CameraServer::CameraFeedback zoom_out_start_feedback)
×
1901
{
1902
    std::lock_guard<std::mutex> lg{_mutex};
×
1903

1904
    switch (zoom_out_start_feedback) {
×
1905
        default:
×
1906
            // Fallthrough
1907
        case CameraServer::CameraFeedback::Unknown:
1908
            return CameraServer::Result::Error;
×
1909
        case CameraServer::CameraFeedback::Ok: {
×
1910
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1911
                _last_zoom_out_start_command, MAV_RESULT_ACCEPTED);
×
1912
            _server_component_impl->send_command_ack(command_ack);
×
1913
            return CameraServer::Result::Success;
×
1914
        }
1915
        case CameraServer::CameraFeedback::Busy: {
×
1916
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1917
                _last_zoom_out_start_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
1918
            _server_component_impl->send_command_ack(command_ack);
×
1919
            return CameraServer::Result::Success;
×
1920
        }
1921
        case CameraServer::CameraFeedback::Failed: {
×
1922
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1923
                _last_zoom_out_start_command, MAV_RESULT_FAILED);
×
1924
            _server_component_impl->send_command_ack(command_ack);
×
1925
            return CameraServer::Result::Success;
×
1926
        }
1927
    }
1928
}
×
1929

1930
CameraServer::ZoomStopHandle
1931
CameraServerImpl::subscribe_zoom_stop(const CameraServer::ZoomStopCallback& callback)
×
1932
{
1933
    std::lock_guard<std::mutex> lg{_mutex};
×
1934
    return _zoom_stop_callbacks.subscribe(callback);
×
1935
}
×
1936

1937
void CameraServerImpl::unsubscribe_zoom_stop(CameraServer::ZoomStopHandle handle)
×
1938
{
1939
    std::lock_guard<std::mutex> lg{_mutex};
×
1940
    _zoom_stop_callbacks.unsubscribe(handle);
×
1941
}
×
1942

1943
CameraServer::Result
1944
CameraServerImpl::respond_zoom_stop(CameraServer::CameraFeedback zoom_stop_feedback)
×
1945
{
1946
    std::lock_guard<std::mutex> lg{_mutex};
×
1947

1948
    switch (zoom_stop_feedback) {
×
1949
        default:
×
1950
            // Fallthrough
1951
        case CameraServer::CameraFeedback::Unknown:
1952
            return CameraServer::Result::Error;
×
1953
        case CameraServer::CameraFeedback::Ok: {
×
1954
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1955
                _last_zoom_stop_command, MAV_RESULT_ACCEPTED);
×
1956
            _server_component_impl->send_command_ack(command_ack);
×
1957
            return CameraServer::Result::Success;
×
1958
        }
1959
        case CameraServer::CameraFeedback::Busy: {
×
1960
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1961
                _last_zoom_stop_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
1962
            _server_component_impl->send_command_ack(command_ack);
×
1963
            return CameraServer::Result::Success;
×
1964
        }
1965
        case CameraServer::CameraFeedback::Failed: {
×
1966
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1967
                _last_zoom_stop_command, MAV_RESULT_FAILED);
×
1968
            _server_component_impl->send_command_ack(command_ack);
×
1969
            return CameraServer::Result::Success;
×
1970
        }
1971
    }
1972
}
×
1973

1974
CameraServer::ZoomRangeHandle
1975
CameraServerImpl::subscribe_zoom_range(const CameraServer::ZoomRangeCallback& callback)
×
1976
{
1977
    std::lock_guard<std::mutex> lg{_mutex};
×
1978
    return _zoom_range_callbacks.subscribe(callback);
×
1979
}
×
1980

1981
void CameraServerImpl::unsubscribe_zoom_range(CameraServer::ZoomRangeHandle handle)
×
1982
{
1983
    std::lock_guard<std::mutex> lg{_mutex};
×
1984
    _zoom_range_callbacks.unsubscribe(handle);
×
1985
}
×
1986

1987
CameraServer::Result
1988
CameraServerImpl::respond_zoom_range(CameraServer::CameraFeedback zoom_range_feedback)
×
1989
{
1990
    std::lock_guard<std::mutex> lg{_mutex};
×
1991

1992
    switch (zoom_range_feedback) {
×
1993
        case CameraServer::CameraFeedback::Ok: {
×
1994
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1995
                _last_zoom_range_command, MAV_RESULT_ACCEPTED);
×
1996
            _server_component_impl->send_command_ack(command_ack);
×
1997
            return CameraServer::Result::Success;
×
1998
        }
1999
        case CameraServer::CameraFeedback::Busy: {
×
2000
            auto command_ack = _server_component_impl->make_command_ack_message(
×
2001
                _last_zoom_range_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
2002
            _server_component_impl->send_command_ack(command_ack);
×
2003
            return CameraServer::Result::Success;
×
2004
        }
2005
        case CameraServer::CameraFeedback::Failed: {
×
2006
            auto command_ack = _server_component_impl->make_command_ack_message(
×
2007
                _last_zoom_range_command, MAV_RESULT_FAILED);
×
2008
            _server_component_impl->send_command_ack(command_ack);
×
2009
            return CameraServer::Result::Success;
×
2010
        }
2011
        case CameraServer::CameraFeedback::Unknown:
×
2012
            // Fallthrough
2013
        default:
2014
            return CameraServer::Result::Error;
×
2015
    }
2016
}
×
2017

2018
std::optional<mavlink_command_ack_t>
2019
CameraServerImpl::process_track_point_command(const MavlinkCommandReceiver::CommandLong& command)
×
2020
{
2021
    if (!is_command_sender_ok(command)) {
×
2022
        LogWarn() << "Incoming track point command is for target sysid "
×
2023
                  << int(command.target_system_id) << " instead of "
×
2024
                  << int(_server_component_impl->get_own_system_id());
×
2025
        return std::nullopt;
×
2026
    }
2027

2028
    std::lock_guard<std::mutex> lg{_mutex};
×
2029

2030
    if (_tracking_point_callbacks.empty()) {
×
2031
        LogDebug() << "Track point requested with no user callback provided";
×
2032
        return _server_component_impl->make_command_ack_message(
×
2033
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
2034
    }
2035

2036
    CameraServer::TrackPoint track_point{
×
2037
        command.params.param1, command.params.param2, command.params.param3};
×
2038

2039
    _last_track_point_command = command;
×
2040
    _tracking_point_callbacks.queue(track_point, [this](const auto& func) {
×
2041
        _server_component_impl->call_user_callback(func);
×
2042
    });
×
2043
    // We don't send an ack but leave that to the user.
2044
    return std::nullopt;
×
2045
}
×
2046

2047
std::optional<mavlink_command_ack_t> CameraServerImpl::process_track_rectangle_command(
×
2048
    const MavlinkCommandReceiver::CommandLong& command)
2049
{
2050
    if (!is_command_sender_ok(command)) {
×
2051
        LogWarn() << "Incoming track rectangle command is for target sysid "
×
2052
                  << int(command.target_system_id) << " instead of "
×
2053
                  << int(_server_component_impl->get_own_system_id());
×
2054
        return std::nullopt;
×
2055
    }
2056

2057
    std::lock_guard<std::mutex> lg{_mutex};
×
2058

2059
    if (_tracking_rectangle_callbacks.empty()) {
×
2060
        LogDebug() << "Track rectangle requested with no user callback provided";
×
2061
        return _server_component_impl->make_command_ack_message(
×
2062
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
2063
    }
2064

2065
    CameraServer::TrackRectangle track_rectangle{
×
2066
        command.params.param1, command.params.param2, command.params.param3, command.params.param4};
×
2067

2068
    _last_track_rectangle_command = command;
×
2069
    _tracking_rectangle_callbacks.queue(track_rectangle, [this](const auto& func) {
×
2070
        _server_component_impl->call_user_callback(func);
×
2071
    });
×
2072
    // We don't send an ack but leave that to the user.
2073
    return std::nullopt;
×
2074
}
×
2075

2076
std::optional<mavlink_command_ack_t>
2077
CameraServerImpl::process_track_off_command(const MavlinkCommandReceiver::CommandLong& command)
×
2078
{
2079
    if (!is_command_sender_ok(command)) {
×
2080
        LogWarn() << "Incoming track off command is for target sysid "
×
2081
                  << int(command.target_system_id) << " instead of "
×
2082
                  << int(_server_component_impl->get_own_system_id());
×
2083
        return std::nullopt;
×
2084
    }
2085

2086
    std::lock_guard<std::mutex> lg{_mutex};
×
2087

2088
    if (_tracking_off_callbacks.empty()) {
×
2089
        LogDebug() << "Tracking off requested with no user callback provided";
×
2090
        return _server_component_impl->make_command_ack_message(
×
2091
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
2092
    }
2093

2094
    _last_tracking_off_command = command;
×
2095
    _tracking_off_callbacks.queue(
×
2096
        0, [this](const auto& func) { _server_component_impl->call_user_callback(func); });
×
2097
    // We don't send an ack but leave that to the user.
2098
    return std::nullopt;
×
2099
}
×
2100

2101
std::optional<mavlink_command_ack_t>
2102
CameraServerImpl::process_set_message_interval(const MavlinkCommandReceiver::CommandLong& command)
×
2103
{
2104
    if (!is_command_sender_ok(command)) {
×
2105
        LogWarn() << "Incoming track off command is for target sysid "
×
2106
                  << int(command.target_system_id) << " instead of "
×
2107
                  << int(_server_component_impl->get_own_system_id());
×
2108
        return std::nullopt;
×
2109
    }
2110

2111
    auto message_id = static_cast<uint32_t>(command.params.param1);
×
2112
    auto interval_us = static_cast<int32_t>(command.params.param2);
×
2113
    UNUSED(message_id);
×
2114

2115
    // Interval value of -1 means to disable sending messages
2116
    if (interval_us < 0) {
×
2117
        stop_sending_tracking_status();
×
2118
    } else {
2119
        start_sending_tracking_status(interval_us);
×
2120
    }
2121

2122
    // Always send the "Accepted" result
2123
    return _server_component_impl->make_command_ack_message(
×
2124
        command, MAV_RESULT::MAV_RESULT_ACCEPTED);
×
2125
}
2126

2127
void CameraServerImpl::send_tracking_status_with_interval(uint32_t interval_us)
×
2128
{
2129
    while (true) {
2130
        std::this_thread::sleep_for(std::chrono::microseconds{interval_us});
×
2131
        {
2132
            std::scoped_lock lg{_mutex};
×
2133
            if (!_sending_tracking_status) {
×
2134
                return;
×
2135
            }
2136
        }
×
2137
        _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
×
2138
            mavlink_message_t message;
2139
            std::lock_guard<std::mutex> lg{_mutex};
×
2140

2141
            // The message is filled based on current tracking mode
2142
            switch (_tracking_mode) {
×
2143
                default:
×
2144
                    // Fallthrough
2145
                case TrackingMode::NONE:
2146

2147
                    mavlink_msg_camera_tracking_image_status_pack_chan(
×
2148
                        mavlink_address.system_id,
×
2149
                        mavlink_address.component_id,
×
2150
                        channel,
2151
                        &message,
2152
                        CAMERA_TRACKING_STATUS_FLAGS_IDLE,
2153
                        CAMERA_TRACKING_MODE_NONE,
2154
                        CAMERA_TRACKING_TARGET_DATA_NONE,
2155
                        0.0f,
2156
                        0.0f,
2157
                        0.0f,
2158
                        0.0f,
2159
                        0.0f,
2160
                        0.0f,
2161
                        0.0f,
2162
                        0);
2163
                    break;
×
2164
                case TrackingMode::POINT:
×
2165

2166
                    mavlink_msg_camera_tracking_image_status_pack_chan(
×
2167
                        mavlink_address.system_id,
×
2168
                        mavlink_address.component_id,
×
2169
                        channel,
2170
                        &message,
2171
                        CAMERA_TRACKING_STATUS_FLAGS_ACTIVE,
2172
                        CAMERA_TRACKING_MODE_POINT,
2173
                        CAMERA_TRACKING_TARGET_DATA_IN_STATUS,
2174
                        _tracked_point.point_x,
2175
                        _tracked_point.point_y,
2176
                        _tracked_point.radius,
2177
                        0.0f,
2178
                        0.0f,
2179
                        0.0f,
2180
                        0.0f,
2181
                        0);
2182
                    break;
×
2183

2184
                case TrackingMode::RECTANGLE:
×
2185

2186
                    mavlink_msg_camera_tracking_image_status_pack_chan(
×
2187
                        mavlink_address.system_id,
×
2188
                        mavlink_address.component_id,
×
2189
                        channel,
2190
                        &message,
2191
                        CAMERA_TRACKING_STATUS_FLAGS_ACTIVE,
2192
                        CAMERA_TRACKING_MODE_RECTANGLE,
2193
                        CAMERA_TRACKING_TARGET_DATA_IN_STATUS,
2194
                        0.0f,
2195
                        0.0f,
2196
                        0.0f,
2197
                        _tracked_rectangle.top_left_corner_x,
2198
                        _tracked_rectangle.top_left_corner_y,
2199
                        _tracked_rectangle.bottom_right_corner_x,
2200
                        _tracked_rectangle.bottom_right_corner_y,
2201
                        0);
2202
                    break;
×
2203
            }
2204
            return message;
×
2205
        });
×
2206
    }
×
2207
}
2208

2209
void CameraServerImpl::start_sending_tracking_status(uint32_t interval_ms)
×
2210
{
2211
    // Stop sending status with the old interval
2212
    stop_sending_tracking_status();
×
2213

2214
    std::lock_guard<std::mutex> lg{_mutex};
×
2215
    _sending_tracking_status = true;
×
2216
    _tracking_status_sending_thread =
×
2217
        std::thread{&CameraServerImpl::send_tracking_status_with_interval, this, interval_ms};
×
2218
}
×
2219

2220
void CameraServerImpl::stop_sending_tracking_status()
×
2221
{
2222
    // Firstly, ask the other thread to stop sending the status
2223
    {
2224
        std::scoped_lock lg{_mutex};
×
2225
        _sending_tracking_status = false;
×
2226
    }
×
2227
    // If the thread was active, wait for it to finish
2228
    if (_tracking_status_sending_thread.joinable()) {
×
2229
        _tracking_status_sending_thread.join();
×
2230
    }
2231
}
×
2232

NEW
2233
void CameraServerImpl::start_sending_capture_status()
×
2234
{
NEW
2235
    _capture_status_timer_cookie = _server_component_impl->add_call_every(
×
NEW
2236
        [this]() { send_capture_status(); }, CAPTURE_STATUS_INTERVAL_S);
×
NEW
2237
}
×
2238

2239
void CameraServerImpl::stop_sending_capture_status()
9✔
2240
{
2241
    _server_component_impl->remove_call_every(_capture_status_timer_cookie);
9✔
2242
}
9✔
2243

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