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

mavlink / MAVSDK / 12119959322

02 Dec 2024 01:05PM UTC coverage: 43.572% (+4.9%) from 38.69%
12119959322

Pull #2386

github

web-flow
Merge 674dc60ab into 68856f27a
Pull Request #2386: camera: support multiple cameras within one instance

1368 of 2039 new or added lines in 47 files covered. (67.09%)

50 existing lines in 8 files now uncovered.

13871 of 31835 relevant lines covered (43.57%)

293.43 hits per line

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

35.68
/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) {
40✔
26
            return process_request_message(command);
40✔
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) {
5✔
38
            return process_camera_settings_request(command);
5✔
39
        },
40
        this);
41
    _server_component_impl->register_mavlink_command_handler(
9✔
42
        MAV_CMD_REQUEST_STORAGE_INFORMATION,
43
        [this](const MavlinkCommandReceiver::CommandLong& command) {
9✔
44
            return process_storage_information_request(command);
9✔
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) {
1✔
134
            return process_video_stream_information_request(command);
1✔
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) {
6✔
140
            return process_video_stream_status_request(command);
6✔
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
    _server_component_impl->unregister_all_mavlink_command_handlers(this);
9✔
175
}
9✔
176

177
bool CameraServerImpl::is_command_sender_ok(const MavlinkCommandReceiver::CommandLong& command)
×
178
{
179
    if (command.target_system_id != 0 &&
×
180
        command.target_system_id != _server_component_impl->get_own_system_id()) {
×
181
        return false;
×
182
    } else {
183
        return true;
×
184
    }
185
}
186

187
void CameraServerImpl::set_tracking_point_status(CameraServer::TrackPoint tracked_point)
×
188
{
189
    std::lock_guard<std::mutex> lg{_tracking_status_mutex};
×
190
    _tracking_mode = TrackingMode::POINT;
×
191
    _tracked_point = tracked_point;
×
192
}
×
193

194
void CameraServerImpl::set_tracking_rectangle_status(CameraServer::TrackRectangle tracked_rectangle)
×
195
{
196
    std::lock_guard<std::mutex> lg{_tracking_status_mutex};
×
197
    _tracking_mode = TrackingMode::RECTANGLE;
×
198
    _tracked_rectangle = tracked_rectangle;
×
199
}
×
200

201
void CameraServerImpl::set_tracking_off_status()
×
202
{
203
    std::lock_guard<std::mutex> lg{_tracking_status_mutex};
×
204
    _tracking_mode = TrackingMode::NONE;
×
205
}
×
206

207
bool CameraServerImpl::parse_version_string(const std::string& version_str)
9✔
208
{
209
    uint32_t unused;
9✔
210

211
    return parse_version_string(version_str, unused);
9✔
212
}
213

214
bool CameraServerImpl::parse_version_string(const std::string& version_str, uint32_t& version)
18✔
215
{
216
    // empty string means no version
217
    if (version_str.empty()) {
18✔
218
        version = 0;
×
219

220
        return true;
×
221
    }
222

223
    uint8_t major{}, minor{}, patch{}, dev{};
18✔
224

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

227
    if (ret == EOF) {
18✔
228
        return false;
×
229
    }
230

231
    // pack version according to MAVLINK spec
232
    version = dev << 24 | patch << 16 | minor << 8 | major;
18✔
233

234
    return true;
18✔
235
}
236

237
CameraServer::Result CameraServerImpl::set_information(CameraServer::Information information)
9✔
238
{
239
    if (!parse_version_string(information.firmware_version)) {
9✔
240
        LogDebug() << "incorrectly formatted firmware version string: "
×
241
                   << information.firmware_version;
×
242
        return CameraServer::Result::WrongArgument;
×
243
    }
244

245
    // TODO: validate information.definition_file_uri
246

247
    _is_information_set = true;
9✔
248
    _information = information;
9✔
249

250
    return CameraServer::Result::Success;
9✔
251
}
252

253
CameraServer::Result
254
CameraServerImpl::set_video_streaming(CameraServer::VideoStreaming video_streaming)
1✔
255
{
256
    // TODO: validate uri length
257

258
    _is_video_streaming_set = true;
1✔
259
    _video_streaming = video_streaming;
1✔
260

261
    return CameraServer::Result::Success;
1✔
262
}
263

264
CameraServer::Result CameraServerImpl::set_in_progress(bool in_progress)
×
265
{
266
    _is_image_capture_in_progress = in_progress;
×
267

268
    send_capture_status();
×
269
    return CameraServer::Result::Success;
×
270
}
271

272
CameraServer::TakePhotoHandle
273
CameraServerImpl::subscribe_take_photo(const CameraServer::TakePhotoCallback& callback)
2✔
274
{
275
    return _take_photo_callbacks.subscribe(callback);
2✔
276
}
277

278
void CameraServerImpl::unsubscribe_take_photo(CameraServer::TakePhotoHandle handle)
×
279
{
280
    _take_photo_callbacks.unsubscribe(handle);
×
281
}
×
282

283
CameraServer::Result CameraServerImpl::respond_take_photo(
4✔
284
    CameraServer::CameraFeedback take_photo_feedback, CameraServer::CaptureInfo capture_info)
285
{
286
    // If capture_info.index == INT32_MIN, it means this was an interval
287
    // capture rather than a single image capture.
288
    if (capture_info.index != INT32_MIN) {
4✔
289
        // We expect each capture to be the next sequential number.
290
        // If _image_capture_count == 0, we ignore since it means that this is
291
        // the first photo since the plugin was initialized.
292
        if (_image_capture_count != 0 && capture_info.index != _image_capture_count + 1) {
4✔
293
            LogErr() << "unexpected image index, expecting " << +(_image_capture_count + 1)
×
294
                     << " but was " << +capture_info.index;
×
295
        }
296

297
        _image_capture_count = capture_info.index;
4✔
298
    }
299

300
    switch (take_photo_feedback) {
4✔
301
        default:
×
302
            // Fallthrough
303
        case CameraServer::CameraFeedback::Unknown:
304
            return CameraServer::Result::Error;
×
305
        case CameraServer::CameraFeedback::Ok: {
4✔
306
            // Check for error above
307
            auto command_ack = _server_component_impl->make_command_ack_message(
4✔
308
                _last_take_photo_command, MAV_RESULT_ACCEPTED);
4✔
309
            _server_component_impl->send_command_ack(command_ack);
4✔
310
            // Only break and send the captured below.
311
            break;
4✔
312
        }
313
        case CameraServer::CameraFeedback::Busy: {
×
314
            auto command_ack = _server_component_impl->make_command_ack_message(
×
315
                _last_take_photo_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
316
            _server_component_impl->send_command_ack(command_ack);
×
317
            return CameraServer::Result::Success;
×
318
        }
319

320
        case CameraServer::CameraFeedback::Failed: {
×
321
            auto command_ack = _server_component_impl->make_command_ack_message(
×
322
                _last_take_photo_command, MAV_RESULT_FAILED);
×
323
            _server_component_impl->send_command_ack(command_ack);
×
324
            return CameraServer::Result::Success;
×
325
        }
326
    }
327

328
    // REVISIT: Should we cache all CaptureInfo in memory for single image
329
    // captures so that we can respond to requests for lost CAMERA_IMAGE_CAPTURED
330
    // messages without calling back to user code?
331

332
    static const uint8_t camera_id = 0; // deprecated unused field
333

334
    const float attitude_quaternion[] = {
4✔
335
        capture_info.attitude_quaternion.w,
4✔
336
        capture_info.attitude_quaternion.x,
4✔
337
        capture_info.attitude_quaternion.y,
4✔
338
        capture_info.attitude_quaternion.z,
4✔
339
    };
4✔
340

341
    // There needs to be enough data to be copied mavlink internal.
342
    capture_info.file_url.resize(205);
4✔
343

344
    // TODO: this should be a broadcast message
345
    _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
4✔
346
        mavlink_message_t message{};
4✔
347
        mavlink_msg_camera_image_captured_pack_chan(
12✔
348
            mavlink_address.system_id,
4✔
349
            mavlink_address.component_id,
4✔
350
            channel,
351
            &message,
352
            static_cast<uint32_t>(_server_component_impl->get_time().elapsed_s() * 1e3),
4✔
353
            capture_info.time_utc_us,
4✔
354
            camera_id,
355
            static_cast<int32_t>(capture_info.position.latitude_deg * 1e7),
4✔
356
            static_cast<int32_t>(capture_info.position.longitude_deg * 1e7),
4✔
357
            static_cast<int32_t>(capture_info.position.absolute_altitude_m * 1e3f),
4✔
358
            static_cast<int32_t>(capture_info.position.relative_altitude_m * 1e3f),
4✔
359
            attitude_quaternion,
4✔
360
            capture_info.index,
361
            capture_info.is_success,
4✔
362
            capture_info.file_url.c_str());
363
        return message;
4✔
364
    });
365
    LogDebug() << "sent camera image captured msg - index: " << +capture_info.index;
4✔
366

367
    return CameraServer::Result::Success;
4✔
368
}
369

370
CameraServer::StartVideoHandle
371
CameraServerImpl::subscribe_start_video(const CameraServer::StartVideoCallback& callback)
×
372
{
373
    return _start_video_callbacks.subscribe(callback);
×
374
}
375

376
void CameraServerImpl::unsubscribe_start_video(CameraServer::StartVideoHandle handle)
×
377
{
378
    _start_video_callbacks.unsubscribe(handle);
×
379
}
×
380

381
CameraServer::Result
382
CameraServerImpl::respond_start_video(CameraServer::CameraFeedback start_video_feedback)
×
383
{
384
    switch (start_video_feedback) {
×
385
        default:
×
386
            // Fallthrough
387
        case CameraServer::CameraFeedback::Unknown:
388
            return CameraServer::Result::Error;
×
389
        case CameraServer::CameraFeedback::Ok: {
×
390
            auto command_ack = _server_component_impl->make_command_ack_message(
×
391
                _last_start_video_command, MAV_RESULT_ACCEPTED);
×
392
            _server_component_impl->send_command_ack(command_ack);
×
393
            return CameraServer::Result::Success;
×
394
        }
395
        case CameraServer::CameraFeedback::Busy: {
×
396
            auto command_ack = _server_component_impl->make_command_ack_message(
×
397
                _last_start_video_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
398
            _server_component_impl->send_command_ack(command_ack);
×
399
            return CameraServer::Result::Success;
×
400
        }
401
        case CameraServer::CameraFeedback::Failed: {
×
402
            auto command_ack = _server_component_impl->make_command_ack_message(
×
403
                _last_start_video_command, MAV_RESULT_FAILED);
×
404
            _server_component_impl->send_command_ack(command_ack);
×
405
            return CameraServer::Result::Success;
×
406
        }
407
    }
408
}
409

410
CameraServer::StopVideoHandle
411
CameraServerImpl::subscribe_stop_video(const CameraServer::StopVideoCallback& callback)
×
412
{
413
    return _stop_video_callbacks.subscribe(callback);
×
414
}
415

416
void CameraServerImpl::unsubscribe_stop_video(CameraServer::StopVideoHandle handle)
×
417
{
418
    return _stop_video_callbacks.unsubscribe(handle);
×
419
}
420

421
CameraServer::Result
422
CameraServerImpl::respond_stop_video(CameraServer::CameraFeedback stop_video_feedback)
×
423
{
424
    switch (stop_video_feedback) {
×
425
        default:
×
426
            // Fallthrough
427
        case CameraServer::CameraFeedback::Unknown:
428
            return CameraServer::Result::Error;
×
429
        case CameraServer::CameraFeedback::Ok: {
×
430
            auto command_ack = _server_component_impl->make_command_ack_message(
×
431
                _last_stop_video_command, MAV_RESULT_ACCEPTED);
×
432
            _server_component_impl->send_command_ack(command_ack);
×
433
            return CameraServer::Result::Success;
×
434
        }
435
        case CameraServer::CameraFeedback::Busy: {
×
436
            auto command_ack = _server_component_impl->make_command_ack_message(
×
437
                _last_stop_video_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
438
            _server_component_impl->send_command_ack(command_ack);
×
439
            return CameraServer::Result::Success;
×
440
        }
441
        case CameraServer::CameraFeedback::Failed: {
×
442
            auto command_ack = _server_component_impl->make_command_ack_message(
×
443
                _last_stop_video_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
444
            _server_component_impl->send_command_ack(command_ack);
×
445
            return CameraServer::Result::Success;
×
446
        }
447
    }
448
}
449

450
CameraServer::StartVideoStreamingHandle CameraServerImpl::subscribe_start_video_streaming(
×
451
    const CameraServer::StartVideoStreamingCallback& callback)
452
{
453
    return _start_video_streaming_callbacks.subscribe(callback);
×
454
}
455

456
void CameraServerImpl::unsubscribe_start_video_streaming(
×
457
    CameraServer::StartVideoStreamingHandle handle)
458
{
459
    return _start_video_streaming_callbacks.unsubscribe(handle);
×
460
}
461

462
CameraServer::Result CameraServerImpl::respond_start_video_streaming(
×
463
    CameraServer::CameraFeedback start_video_streaming_feedback)
464
{
465
    switch (start_video_streaming_feedback) {
×
466
        default:
×
467
            // Fallthrough
468
        case CameraServer::CameraFeedback::Unknown:
469
            return CameraServer::Result::Error;
×
470
        case CameraServer::CameraFeedback::Ok: {
×
471
            auto command_ack = _server_component_impl->make_command_ack_message(
×
472
                _last_start_video_streaming_command, MAV_RESULT_ACCEPTED);
×
473
            _server_component_impl->send_command_ack(command_ack);
×
474
            return CameraServer::Result::Success;
×
475
        }
476
        case CameraServer::CameraFeedback::Busy: {
×
477
            auto command_ack = _server_component_impl->make_command_ack_message(
×
478
                _last_start_video_streaming_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
479
            _server_component_impl->send_command_ack(command_ack);
×
480
            return CameraServer::Result::Success;
×
481
        }
482
        case CameraServer::CameraFeedback::Failed: {
×
483
            auto command_ack = _server_component_impl->make_command_ack_message(
×
484
                _last_start_video_streaming_command, MAV_RESULT_FAILED);
×
485
            _server_component_impl->send_command_ack(command_ack);
×
486
            return CameraServer::Result::Success;
×
487
        }
488
    }
489
}
490

491
CameraServer::StopVideoStreamingHandle CameraServerImpl::subscribe_stop_video_streaming(
×
492
    const CameraServer::StopVideoStreamingCallback& callback)
493
{
494
    return _stop_video_streaming_callbacks.subscribe(callback);
×
495
}
496

497
void CameraServerImpl::unsubscribe_stop_video_streaming(
×
498
    CameraServer::StopVideoStreamingHandle handle)
499
{
500
    return _stop_video_streaming_callbacks.unsubscribe(handle);
×
501
}
502

503
CameraServer::Result CameraServerImpl::respond_stop_video_streaming(
×
504
    CameraServer::CameraFeedback stop_video_streaming_feedback)
505
{
506
    switch (stop_video_streaming_feedback) {
×
507
        default:
×
508
            // Fallthrough
509
        case CameraServer::CameraFeedback::Unknown:
510
            return CameraServer::Result::Error;
×
511
        case CameraServer::CameraFeedback::Ok: {
×
512
            auto command_ack = _server_component_impl->make_command_ack_message(
×
513
                _last_stop_video_streaming_command, MAV_RESULT_ACCEPTED);
×
514
            _server_component_impl->send_command_ack(command_ack);
×
515
            return CameraServer::Result::Success;
×
516
        }
517
        case CameraServer::CameraFeedback::Busy: {
×
518
            auto command_ack = _server_component_impl->make_command_ack_message(
×
519
                _last_stop_video_streaming_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
520
            _server_component_impl->send_command_ack(command_ack);
×
521
            return CameraServer::Result::Success;
×
522
        }
523
        case CameraServer::CameraFeedback::Failed: {
×
524
            auto command_ack = _server_component_impl->make_command_ack_message(
×
525
                _last_stop_video_streaming_command, MAV_RESULT_FAILED);
×
526
            _server_component_impl->send_command_ack(command_ack);
×
527
            return CameraServer::Result::Success;
×
528
        }
529
    }
530
}
531

532
CameraServer::SetModeHandle
533
CameraServerImpl::subscribe_set_mode(const CameraServer::SetModeCallback& callback)
2✔
534
{
535
    return _set_mode_callbacks.subscribe(callback);
2✔
536
}
537

538
void CameraServerImpl::unsubscribe_set_mode(CameraServer::SetModeHandle handle)
×
539
{
540
    _set_mode_callbacks.unsubscribe(handle);
×
541
}
×
542

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

572
CameraServer::StorageInformationHandle CameraServerImpl::subscribe_storage_information(
1✔
573
    const CameraServer::StorageInformationCallback& callback)
574
{
575
    return _storage_information_callbacks.subscribe(callback);
1✔
576
}
577

578
void CameraServerImpl::unsubscribe_storage_information(
×
579
    CameraServer::StorageInformationHandle handle)
580
{
581
    _storage_information_callbacks.unsubscribe(handle);
×
582
}
×
583

584
CameraServer::Result CameraServerImpl::respond_storage_information(
1✔
585
    CameraServer::CameraFeedback storage_information_feedback,
586
    CameraServer::StorageInformation storage_information)
587
{
588
    switch (storage_information_feedback) {
1✔
589
        default:
×
590
            // Fallthrough
591
        case CameraServer::CameraFeedback::Unknown:
592
            return CameraServer::Result::Error;
×
593
        case CameraServer::CameraFeedback::Ok: {
1✔
594
            auto command_ack = _server_component_impl->make_command_ack_message(
1✔
595
                _last_storage_information_command, MAV_RESULT_ACCEPTED);
1✔
596
            _server_component_impl->send_command_ack(command_ack);
1✔
597
            // break and send storage information
598
            break;
1✔
599
        }
600
        case CameraServer::CameraFeedback::Busy: {
×
601
            auto command_ack = _server_component_impl->make_command_ack_message(
×
602
                _last_storage_information_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
603
            _server_component_impl->send_command_ack(command_ack);
×
604
            return CameraServer::Result::Success;
×
605
        }
606
        case CameraServer::CameraFeedback::Failed: {
×
607
            auto command_ack = _server_component_impl->make_command_ack_message(
×
608
                _last_storage_information_command, MAV_RESULT_FAILED);
×
609
            _server_component_impl->send_command_ack(command_ack);
×
610
            return CameraServer::Result::Success;
×
611
        }
612
    }
613

614
    const uint8_t storage_count = 1;
1✔
615

616
    const float total_capacity = storage_information.total_storage_mib;
1✔
617
    const float used_capacity = storage_information.used_storage_mib;
1✔
618
    const float available_capacity = storage_information.available_storage_mib;
1✔
619
    const float read_speed = storage_information.read_speed_mib_s;
1✔
620
    const float write_speed = storage_information.write_speed_mib_s;
1✔
621

622
    auto status = STORAGE_STATUS::STORAGE_STATUS_NOT_SUPPORTED;
1✔
623
    switch (storage_information.storage_status) {
1✔
624
        case CameraServer::StorageInformation::StorageStatus::NotAvailable:
×
625
            status = STORAGE_STATUS::STORAGE_STATUS_NOT_SUPPORTED;
×
626
            break;
×
627
        case CameraServer::StorageInformation::StorageStatus::Unformatted:
×
628
            status = STORAGE_STATUS::STORAGE_STATUS_UNFORMATTED;
×
629
            break;
×
630
        case CameraServer::StorageInformation::StorageStatus::Formatted:
1✔
631
            status = STORAGE_STATUS::STORAGE_STATUS_READY;
1✔
632
            break;
1✔
633
        case CameraServer::StorageInformation::StorageStatus::NotSupported:
×
634
            status = STORAGE_STATUS::STORAGE_STATUS_NOT_SUPPORTED;
×
635
            break;
×
636
    }
637

638
    auto type = STORAGE_TYPE::STORAGE_TYPE_UNKNOWN;
1✔
639
    switch (storage_information.storage_type) {
1✔
640
        case CameraServer::StorageInformation::StorageType::UsbStick:
×
641
            type = STORAGE_TYPE::STORAGE_TYPE_USB_STICK;
×
642
            break;
×
643
        case CameraServer::StorageInformation::StorageType::Sd:
×
644
            type = STORAGE_TYPE::STORAGE_TYPE_SD;
×
645
            break;
×
646
        case CameraServer::StorageInformation::StorageType::Microsd:
×
647
            type = STORAGE_TYPE::STORAGE_TYPE_MICROSD;
×
648
            break;
×
649
        case CameraServer::StorageInformation::StorageType::Hd:
×
650
            type = STORAGE_TYPE::STORAGE_TYPE_HD;
×
651
            break;
×
652
        case CameraServer::StorageInformation::StorageType::Other:
×
653
            type = STORAGE_TYPE::STORAGE_TYPE_OTHER;
×
654
            break;
×
655
        default:
1✔
656
            break;
1✔
657
    }
658

659
    std::string name("");
2✔
660
    // This needs to be long enough, otherwise the memcpy in mavlink overflows.
661
    name.resize(32);
1✔
662
    const uint8_t storage_usage = 0;
1✔
663

664
    _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
1✔
665
        mavlink_message_t message{};
1✔
666
        mavlink_msg_storage_information_pack_chan(
2✔
667
            mavlink_address.system_id,
1✔
668
            mavlink_address.component_id,
1✔
669
            channel,
670
            &message,
671
            static_cast<uint32_t>(_server_component_impl->get_time().elapsed_s() * 1e3),
1✔
672
            _last_storage_id,
1✔
673
            storage_count,
674
            status,
1✔
675
            total_capacity,
1✔
676
            used_capacity,
1✔
677
            available_capacity,
1✔
678
            read_speed,
1✔
679
            write_speed,
1✔
680
            type,
1✔
681
            name.data(),
1✔
682
            storage_usage);
683
        return message;
1✔
684
    });
685

686
    return CameraServer::Result::Success;
1✔
687
}
688

689
CameraServer::CaptureStatusHandle
690
CameraServerImpl::subscribe_capture_status(const CameraServer::CaptureStatusCallback& callback)
×
691
{
692
    return _capture_status_callbacks.subscribe(callback);
×
693
}
694

695
void CameraServerImpl::unsubscribe_capture_status(CameraServer::CaptureStatusHandle handle)
×
696
{
697
    _capture_status_callbacks.unsubscribe(handle);
×
698
}
×
699

700
CameraServer::Result CameraServerImpl::respond_capture_status(
×
701
    CameraServer::CameraFeedback capture_status_feedback,
702
    CameraServer::CaptureStatus capture_status)
703
{
704
    switch (capture_status_feedback) {
×
705
        default:
×
706
            // Fallthrough
707
        case CameraServer::CameraFeedback::Unknown:
708
            return CameraServer::Result::Error;
×
709
        case CameraServer::CameraFeedback::Ok: {
×
710
            auto command_ack = _server_component_impl->make_command_ack_message(
×
711
                _last_capture_status_command, MAV_RESULT_ACCEPTED);
×
712
            _server_component_impl->send_command_ack(command_ack);
×
713
            // break and send capture status
714
            break;
×
715
        }
716
        case CameraServer::CameraFeedback::Busy: {
×
717
            auto command_ack = _server_component_impl->make_command_ack_message(
×
718
                _last_capture_status_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
719
            _server_component_impl->send_command_ack(command_ack);
×
720
            return CameraServer::Result::Success;
×
721
        }
722
        case CameraServer::CameraFeedback::Failed: {
×
723
            auto command_ack = _server_component_impl->make_command_ack_message(
×
724
                _last_capture_status_command, MAV_RESULT_FAILED);
×
725
            _server_component_impl->send_command_ack(command_ack);
×
726
            return CameraServer::Result::Success;
×
727
        }
728
    }
729

730
    _capture_status = capture_status;
×
731

732
    send_capture_status();
×
733

734
    return CameraServer::Result::Success;
×
735
}
736

737
CameraServer::FormatStorageHandle
738
CameraServerImpl::subscribe_format_storage(const CameraServer::FormatStorageCallback& callback)
1✔
739
{
740
    return _format_storage_callbacks.subscribe(callback);
1✔
741
}
742
void CameraServerImpl::unsubscribe_format_storage(CameraServer::FormatStorageHandle handle)
×
743
{
744
    _format_storage_callbacks.unsubscribe(handle);
×
745
}
×
746

747
CameraServer::Result
748
CameraServerImpl::respond_format_storage(CameraServer::CameraFeedback format_storage_feedback)
1✔
749
{
750
    switch (format_storage_feedback) {
1✔
751
        default:
×
752
            // Fallthrough
753
        case CameraServer::CameraFeedback::Unknown:
754
            return CameraServer::Result::Error;
×
755
        case CameraServer::CameraFeedback::Ok: {
1✔
756
            auto command_ack = _server_component_impl->make_command_ack_message(
1✔
757
                _last_format_storage_command, MAV_RESULT_ACCEPTED);
1✔
758
            _server_component_impl->send_command_ack(command_ack);
1✔
759
            return CameraServer::Result::Success;
1✔
760
        }
761
        case CameraServer::CameraFeedback::Busy: {
×
762
            auto command_ack = _server_component_impl->make_command_ack_message(
×
763
                _last_format_storage_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
764
            _server_component_impl->send_command_ack(command_ack);
×
765
            return CameraServer::Result::Success;
×
766
        }
767
        case CameraServer::CameraFeedback::Failed: {
×
768
            auto command_ack = _server_component_impl->make_command_ack_message(
×
769
                _last_format_storage_command, MAV_RESULT_FAILED);
×
770
            _server_component_impl->send_command_ack(command_ack);
×
771
            return CameraServer::Result::Success;
×
772
        }
773
    }
774
}
775

776
CameraServer::ResetSettingsHandle
777
CameraServerImpl::subscribe_reset_settings(const CameraServer::ResetSettingsCallback& callback)
1✔
778
{
779
    return _reset_settings_callbacks.subscribe(callback);
1✔
780
}
781

782
void CameraServerImpl::unsubscribe_reset_settings(CameraServer::ResetSettingsHandle handle)
×
783
{
784
    _reset_settings_callbacks.unsubscribe(handle);
×
785
}
×
786

787
CameraServer::Result
788
CameraServerImpl::respond_reset_settings(CameraServer::CameraFeedback reset_settings_feedback)
1✔
789
{
790
    switch (reset_settings_feedback) {
1✔
791
        default:
×
792
            // Fallthrough
793
        case CameraServer::CameraFeedback::Unknown:
794
            return CameraServer::Result::Error;
×
795
        case CameraServer::CameraFeedback::Ok: {
1✔
796
            auto command_ack = _server_component_impl->make_command_ack_message(
1✔
797
                _last_reset_settings_command, MAV_RESULT_ACCEPTED);
1✔
798
            _server_component_impl->send_command_ack(command_ack);
1✔
799
            return CameraServer::Result::Success;
1✔
800
        }
801
        case CameraServer::CameraFeedback::Busy: {
×
802
            auto command_ack = _server_component_impl->make_command_ack_message(
×
803
                _last_reset_settings_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
804
            _server_component_impl->send_command_ack(command_ack);
×
805
            return CameraServer::Result::Success;
×
806
        }
807
        case CameraServer::CameraFeedback::Failed: {
×
808
            auto command_ack = _server_component_impl->make_command_ack_message(
×
809
                _last_reset_settings_command, MAV_RESULT_FAILED);
×
810
            _server_component_impl->send_command_ack(command_ack);
×
811
            return CameraServer::Result::Success;
×
812
        }
813
    }
814
}
815

816
CameraServer::TrackingPointCommandHandle CameraServerImpl::subscribe_tracking_point_command(
×
817
    const CameraServer::TrackingPointCommandCallback& callback)
818
{
819
    return _tracking_point_callbacks.subscribe(callback);
×
820
}
821

822
void CameraServerImpl::unsubscribe_tracking_point_command(
×
823
    CameraServer::TrackingPointCommandHandle handle)
824
{
825
    _tracking_point_callbacks.unsubscribe(handle);
×
826
}
×
827

828
CameraServer::Result CameraServerImpl::respond_tracking_point_command(
×
829
    CameraServer::CameraFeedback tracking_point_feedback)
830
{
831
    switch (tracking_point_feedback) {
×
832
        default:
×
833
            // Fallthrough
834
        case CameraServer::CameraFeedback::Unknown:
835
            return CameraServer::Result::Error;
×
836
        case CameraServer::CameraFeedback::Ok: {
×
837
            auto command_ack = _server_component_impl->make_command_ack_message(
×
838
                _last_track_point_command, MAV_RESULT_ACCEPTED);
×
839
            _server_component_impl->send_command_ack(command_ack);
×
840
            break;
×
841
        }
842
        case CameraServer::CameraFeedback::Busy: {
×
843
            auto command_ack = _server_component_impl->make_command_ack_message(
×
844
                _last_track_point_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
845
            _server_component_impl->send_command_ack(command_ack);
×
846
            return CameraServer::Result::Success;
×
847
        }
848
        case CameraServer::CameraFeedback::Failed: {
×
849
            auto command_ack = _server_component_impl->make_command_ack_message(
×
850
                _last_track_point_command, MAV_RESULT_FAILED);
×
851
            _server_component_impl->send_command_ack(command_ack);
×
852
            return CameraServer::Result::Success;
×
853
        }
854
    }
855
    return CameraServer::Result::Success;
×
856
}
857

858
CameraServer::TrackingRectangleCommandHandle CameraServerImpl::subscribe_tracking_rectangle_command(
×
859
    const CameraServer::TrackingRectangleCommandCallback& callback)
860
{
861
    return _tracking_rectangle_callbacks.subscribe(callback);
×
862
}
863

864
void CameraServerImpl::unsubscribe_tracking_rectangle_command(
×
865
    CameraServer::TrackingRectangleCommandHandle handle)
866
{
867
    _tracking_rectangle_callbacks.unsubscribe(handle);
×
868
}
×
869

870
CameraServer::Result CameraServerImpl::respond_tracking_rectangle_command(
×
871
    CameraServer::CameraFeedback tracking_rectangle_feedback)
872
{
873
    switch (tracking_rectangle_feedback) {
×
874
        default:
×
875
            // Fallthrough
876
        case CameraServer::CameraFeedback::Unknown:
877
            return CameraServer::Result::Error;
×
878
        case CameraServer::CameraFeedback::Ok: {
×
879
            auto command_ack = _server_component_impl->make_command_ack_message(
×
880
                _last_track_rectangle_command, MAV_RESULT_ACCEPTED);
×
881
            _server_component_impl->send_command_ack(command_ack);
×
882
            break;
×
883
        }
884
        case CameraServer::CameraFeedback::Busy: {
×
885
            auto command_ack = _server_component_impl->make_command_ack_message(
×
886
                _last_track_rectangle_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
887
            _server_component_impl->send_command_ack(command_ack);
×
888
            return CameraServer::Result::Success;
×
889
        }
890
        case CameraServer::CameraFeedback::Failed: {
×
891
            auto command_ack = _server_component_impl->make_command_ack_message(
×
892
                _last_track_rectangle_command, MAV_RESULT_FAILED);
×
893
            _server_component_impl->send_command_ack(command_ack);
×
894
            return CameraServer::Result::Success;
×
895
        }
896
    }
897
    return CameraServer::Result::Success;
×
898
}
899

900
CameraServer::TrackingOffCommandHandle CameraServerImpl::subscribe_tracking_off_command(
×
901
    const CameraServer::TrackingOffCommandCallback& callback)
902
{
903
    return _tracking_off_callbacks.subscribe(callback);
×
904
}
905

906
void CameraServerImpl::unsubscribe_tracking_off_command(
×
907
    CameraServer::TrackingOffCommandHandle handle)
908
{
909
    _tracking_off_callbacks.unsubscribe(handle);
×
910
}
×
911

912
CameraServer::Result
913
CameraServerImpl::respond_tracking_off_command(CameraServer::CameraFeedback tracking_off_feedback)
×
914
{
915
    switch (tracking_off_feedback) {
×
916
        default:
×
917
            // Fallthrough
918
        case CameraServer::CameraFeedback::Unknown:
919
            return CameraServer::Result::Error;
×
920
        case CameraServer::CameraFeedback::Ok: {
×
921
            auto command_ack = _server_component_impl->make_command_ack_message(
×
922
                _last_tracking_off_command, MAV_RESULT_ACCEPTED);
×
923
            _server_component_impl->send_command_ack(command_ack);
×
924
            break;
×
925
        }
926
        case CameraServer::CameraFeedback::Busy: {
×
927
            auto command_ack = _server_component_impl->make_command_ack_message(
×
928
                _last_tracking_off_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
929
            _server_component_impl->send_command_ack(command_ack);
×
930
            return CameraServer::Result::Success;
×
931
        }
932
        case CameraServer::CameraFeedback::Failed: {
×
933
            auto command_ack = _server_component_impl->make_command_ack_message(
×
934
                _last_tracking_off_command, MAV_RESULT_FAILED);
×
935
            _server_component_impl->send_command_ack(command_ack);
×
936
            return CameraServer::Result::Success;
×
937
        }
938
    }
939
    return CameraServer::Result::Success;
×
940
}
941

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

947
    _image_capture_timer_cookie = _server_component_impl->add_call_every(
1✔
948
        [this, remaining, index]() {
9✔
949
            LogDebug() << "capture image timer triggered";
3✔
950

951
            if (!_take_photo_callbacks.empty()) {
3✔
952
                _take_photo_callbacks(index);
3✔
953
                (*remaining)--;
3✔
954
            }
955

956
            if (*remaining == 0) {
3✔
957
                stop_image_capture_interval();
×
958
            }
959
        },
3✔
960
        interval_s);
961

962
    _is_image_capture_interval_set = true;
1✔
963
    _image_capture_timer_interval_s = interval_s;
1✔
964
}
1✔
965

966
void CameraServerImpl::stop_image_capture_interval()
12✔
967
{
968
    _server_component_impl->remove_call_every(_image_capture_timer_cookie);
12✔
969

970
    _is_image_capture_interval_set = false;
12✔
971
    _image_capture_timer_interval_s = 0;
12✔
972
}
12✔
973

974
std::optional<mavlink_command_ack_t> CameraServerImpl::process_camera_information_request(
×
975
    const MavlinkCommandReceiver::CommandLong& command)
976
{
NEW
977
    LogDebug() << "Camera info request";
×
978

NEW
979
    if (static_cast<int>(command.params.param1) == 0) {
×
980
        return _server_component_impl->make_command_ack_message(
×
981
            command, MAV_RESULT::MAV_RESULT_ACCEPTED);
×
982
    }
983

NEW
984
    return send_camera_information(command);
×
985
}
986

987
std::optional<mavlink_command_ack_t>
988
CameraServerImpl::process_request_message(const MavlinkCommandReceiver::CommandLong& command)
40✔
989
{
990
    switch (static_cast<int>(command.params.param1)) {
40✔
991
        case MAVLINK_MSG_ID_CAMERA_INFORMATION:
9✔
992
            return send_camera_information(command);
9✔
993

994
        case MAVLINK_MSG_ID_CAMERA_CAPTURE_STATUS:
5✔
995
            send_capture_status();
5✔
996
            return _server_component_impl->make_command_ack_message(
10✔
997
                command, MAV_RESULT::MAV_RESULT_ACCEPTED);
10✔
998

999
        default:
26✔
1000
            LogWarn() << "Got unknown request message!";
26✔
1001
            return _server_component_impl->make_command_ack_message(
52✔
1002
                command, MAV_RESULT::MAV_RESULT_DENIED);
52✔
1003
    }
1004
}
1005

1006
std::optional<mavlink_command_ack_t>
1007
CameraServerImpl::send_camera_information(const MavlinkCommandReceiver::CommandLong& command)
9✔
1008
{
1009
    if (!_is_information_set) {
9✔
1010
        return _server_component_impl->make_command_ack_message(
×
1011
            command, MAV_RESULT::MAV_RESULT_TEMPORARILY_REJECTED);
×
1012
    }
1013

1014
    // ack needs to be sent before camera information message
1015
    auto command_ack =
9✔
1016
        _server_component_impl->make_command_ack_message(command, MAV_RESULT::MAV_RESULT_ACCEPTED);
9✔
1017
    _server_component_impl->send_command_ack(command_ack);
9✔
1018

1019
    // It is safe to ignore the return value of parse_version_string() here
1020
    // since the string was already validated in set_information().
1021
    uint32_t firmware_version;
9✔
1022
    parse_version_string(_information.firmware_version, firmware_version);
9✔
1023

1024
    // capability flags are determined by subscriptions
1025
    uint32_t capability_flags{};
9✔
1026

1027
    if (!_take_photo_callbacks.empty()) {
9✔
1028
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_CAPTURE_IMAGE;
2✔
1029
    }
1030

1031
    if (!_start_video_callbacks.empty()) {
9✔
1032
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_CAPTURE_VIDEO;
×
1033
    }
1034

1035
    if (!_set_mode_callbacks.empty()) {
9✔
1036
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_HAS_MODES;
2✔
1037
    }
1038

1039
    if (_information.image_in_video_mode_supported) {
9✔
1040
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_CAN_CAPTURE_IMAGE_IN_VIDEO_MODE;
×
1041
    }
1042

1043
    if (_information.video_in_image_mode_supported) {
9✔
1044
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_CAN_CAPTURE_VIDEO_IN_IMAGE_MODE;
×
1045
    }
1046

1047
    if (_is_video_streaming_set) {
9✔
1048
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_HAS_VIDEO_STREAM;
1✔
1049
    }
1050

1051
    if (!_tracking_point_callbacks.empty()) {
9✔
1052
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_HAS_TRACKING_POINT;
×
1053
    }
1054

1055
    if (!_tracking_rectangle_callbacks.empty()) {
9✔
1056
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_HAS_TRACKING_RECTANGLE;
×
1057
    }
1058

1059
    if (!_zoom_range_callbacks.empty() || !_zoom_in_start_callbacks.empty() ||
18✔
1060
        !_zoom_out_start_callbacks.empty()) {
9✔
1061
        capability_flags |= CAMERA_CAP_FLAGS::CAMERA_CAP_FLAGS_HAS_BASIC_ZOOM;
×
1062
    }
1063

1064
    _information.vendor_name.resize(sizeof(mavlink_camera_information_t::vendor_name));
9✔
1065
    _information.model_name.resize(sizeof(mavlink_camera_information_t::model_name));
9✔
1066
    _information.definition_file_uri.resize(
9✔
1067
        sizeof(mavlink_camera_information_t::cam_definition_uri));
1068

1069
    _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
9✔
1070
        mavlink_message_t message{};
9✔
1071
        mavlink_msg_camera_information_pack_chan(
27✔
1072
            mavlink_address.system_id,
9✔
1073
            mavlink_address.component_id,
9✔
1074
            channel,
1075
            &message,
1076
            static_cast<uint32_t>(_server_component_impl->get_time().elapsed_s() * 1e3),
18✔
1077
            reinterpret_cast<const uint8_t*>(_information.vendor_name.c_str()),
9✔
1078
            reinterpret_cast<const uint8_t*>(_information.model_name.c_str()),
9✔
1079
            firmware_version,
9✔
1080
            _information.focal_length_mm,
1081
            _information.horizontal_sensor_size_mm,
1082
            _information.vertical_sensor_size_mm,
1083
            _information.horizontal_resolution_px,
9✔
1084
            _information.vertical_resolution_px,
9✔
1085
            _information.lens_id,
9✔
1086
            capability_flags,
9✔
1087
            _information.definition_file_version,
9✔
1088
            _information.definition_file_uri.c_str(),
1089
            0);
1090
        return message;
9✔
1091
    });
1092

1093
    return std::nullopt;
9✔
1094
}
1095

1096
std::optional<mavlink_command_ack_t> CameraServerImpl::process_camera_settings_request(
5✔
1097
    const MavlinkCommandReceiver::CommandLong& command)
1098
{
1099
    auto settings = static_cast<bool>(command.params.param1);
5✔
1100

1101
    if (!settings) {
5✔
1102
        return _server_component_impl->make_command_ack_message(
×
1103
            command, MAV_RESULT::MAV_RESULT_ACCEPTED);
×
1104
    }
1105

1106
    // ack needs to be sent before camera information message
1107
    auto command_ack =
5✔
1108
        _server_component_impl->make_command_ack_message(command, MAV_RESULT::MAV_RESULT_ACCEPTED);
5✔
1109
    _server_component_impl->send_command_ack(command_ack);
5✔
1110
    LogDebug() << "sent settings ack";
5✔
1111

1112
    // unsupported
1113
    const auto mode_id = CAMERA_MODE::CAMERA_MODE_IMAGE;
5✔
1114
    const float zoom_level = 0;
5✔
1115
    const float focus_level = 0;
5✔
1116

1117
    _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
5✔
1118
        mavlink_message_t message{};
5✔
1119
        mavlink_msg_camera_settings_pack_chan(
10✔
1120
            mavlink_address.system_id,
5✔
1121
            mavlink_address.component_id,
5✔
1122
            channel,
1123
            &message,
1124
            static_cast<uint32_t>(_server_component_impl->get_time().elapsed_s() * 1e3),
5✔
1125
            mode_id,
1126
            zoom_level,
5✔
1127
            focus_level);
5✔
1128
        return message;
5✔
1129
    });
1130
    LogDebug() << "sent settings msg";
5✔
1131

1132
    // ack was already sent
1133
    return std::nullopt;
5✔
1134
}
1135

1136
std::optional<mavlink_command_ack_t> CameraServerImpl::process_storage_information_request(
9✔
1137
    const MavlinkCommandReceiver::CommandLong& command)
1138
{
1139
    auto storage_id = static_cast<uint8_t>(command.params.param1);
9✔
1140
    auto information = static_cast<bool>(command.params.param2);
9✔
1141

1142
    if (!information) {
9✔
1143
        return _server_component_impl->make_command_ack_message(
×
1144
            command, MAV_RESULT::MAV_RESULT_ACCEPTED);
×
1145
    }
1146

1147
    if (_storage_information_callbacks.empty()) {
9✔
1148
        LogDebug()
16✔
1149
            << "Get storage information requested with no set storage information subscriber";
16✔
1150
        return _server_component_impl->make_command_ack_message(
16✔
1151
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
16✔
1152
    }
1153

1154
    // TODO may need support multi storage id
1155
    _last_storage_id = storage_id;
1✔
1156

1157
    _last_storage_information_command = command;
1✔
1158
    _storage_information_callbacks(storage_id);
1✔
1159

1160
    // ack will be sent later in respond_storage_information
1161
    return std::nullopt;
1✔
1162
}
1163

1164
std::optional<mavlink_command_ack_t>
1165
CameraServerImpl::process_storage_format(const MavlinkCommandReceiver::CommandLong& command)
1✔
1166
{
1167
    auto storage_id = static_cast<uint8_t>(command.params.param1);
1✔
1168
    auto format = static_cast<bool>(command.params.param2);
1✔
1169
    auto reset_image_log = static_cast<bool>(command.params.param3);
1✔
1170

1171
    UNUSED(format);
1✔
1172
    UNUSED(reset_image_log);
1✔
1173
    if (_format_storage_callbacks.empty()) {
1✔
1174
        LogDebug() << "process storage format requested with no storage format subscriber";
×
1175
        return _server_component_impl->make_command_ack_message(
×
1176
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1177
    }
1178

1179
    _last_format_storage_command = command;
1✔
1180
    _format_storage_callbacks(storage_id);
1✔
1181

1182
    return std::nullopt;
1✔
1183
}
1184

1185
std::optional<mavlink_command_ack_t> CameraServerImpl::process_camera_capture_status_request(
×
1186
    const MavlinkCommandReceiver::CommandLong& command)
1187
{
1188
    auto capture_status = static_cast<bool>(command.params.param1);
×
1189

1190
    if (!capture_status) {
×
1191
        return _server_component_impl->make_command_ack_message(
×
1192
            command, MAV_RESULT::MAV_RESULT_ACCEPTED);
×
1193
    }
1194

1195
    if (_capture_status_callbacks.empty()) {
×
1196
        LogDebug() << "process camera capture status requested with no capture status subscriber";
×
1197
        return _server_component_impl->make_command_ack_message(
×
1198
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1199
    }
1200

1201
    _last_capture_status_command = command;
×
1202

1203
    // may not need param for now ,just use zero
1204
    _capture_status_callbacks(0);
×
1205

1206
    // ack was already sent
1207
    return std::nullopt;
×
1208
}
1209

1210
void CameraServerImpl::send_capture_status()
5✔
1211
{
1212
    uint8_t image_status{};
5✔
1213
    if (_capture_status.image_status ==
5✔
1214
            CameraServer::CaptureStatus::ImageStatus::CaptureInProgress ||
5✔
1215
        _capture_status.image_status ==
5✔
1216
            CameraServer::CaptureStatus::ImageStatus::IntervalInProgress) {
1217
        image_status |= StatusFlags::IN_PROGRESS;
×
1218
    }
1219

1220
    if (_capture_status.image_status == CameraServer::CaptureStatus::ImageStatus::IntervalIdle ||
5✔
1221
        _capture_status.image_status ==
5✔
1222
            CameraServer::CaptureStatus::ImageStatus::IntervalInProgress ||
5✔
1223
        _is_image_capture_interval_set) {
5✔
1224
        image_status |= StatusFlags::INTERVAL_SET;
×
1225
    }
1226

1227
    uint8_t video_status = 0;
5✔
1228
    if (_capture_status.video_status == CameraServer::CaptureStatus::VideoStatus::Idle) {
5✔
1229
        video_status = 0;
5✔
1230
    } else if (
×
1231
        _capture_status.video_status ==
×
1232
        CameraServer::CaptureStatus::VideoStatus::CaptureInProgress) {
1233
        video_status = 1;
×
1234
    }
1235

1236
    const uint32_t recording_time_ms =
5✔
1237
        static_cast<uint32_t>(static_cast<double>(_capture_status.recording_time_s) * 1e3);
5✔
1238
    const float available_capacity = _capture_status.available_capacity_mib;
5✔
1239

1240
    _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
5✔
1241
        mavlink_message_t message{};
5✔
1242
        mavlink_msg_camera_capture_status_pack_chan(
10✔
1243
            mavlink_address.system_id,
5✔
1244
            mavlink_address.component_id,
5✔
1245
            channel,
1246
            &message,
1247
            static_cast<uint32_t>(_server_component_impl->get_time().elapsed_s() * 1e3),
5✔
1248
            image_status,
5✔
1249
            video_status,
5✔
1250
            _image_capture_timer_interval_s,
1251
            recording_time_ms,
5✔
1252
            available_capacity,
5✔
1253
            _image_capture_count);
1254
        return message;
5✔
1255
    });
1256
}
5✔
1257

1258
std::optional<mavlink_command_ack_t>
1259
CameraServerImpl::process_reset_camera_settings(const MavlinkCommandReceiver::CommandLong& command)
1✔
1260
{
1261
    auto reset = static_cast<bool>(command.params.param1);
1✔
1262

1263
    UNUSED(reset);
1✔
1264

1265
    if (_reset_settings_callbacks.empty()) {
1✔
1266
        LogDebug() << "reset camera settings requested with no camera settings subscriber";
×
1267
        return _server_component_impl->make_command_ack_message(
×
1268
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1269
    }
1270

1271
    _last_reset_settings_command = command;
1✔
1272
    _reset_settings_callbacks(0);
1✔
1273

1274
    return std::nullopt;
1✔
1275
}
1276

1277
std::optional<mavlink_command_ack_t>
1278
CameraServerImpl::process_set_camera_mode(const MavlinkCommandReceiver::CommandLong& command)
2✔
1279
{
1280
    auto camera_mode = static_cast<CAMERA_MODE>(command.params.param2);
2✔
1281

1282
    if (_set_mode_callbacks.empty()) {
2✔
1283
        LogDebug() << "Set mode requested with no set mode subscriber";
×
1284
        return _server_component_impl->make_command_ack_message(
×
1285
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1286
    }
1287

1288
    // convert camera mode enum type
1289
    CameraServer::Mode convert_camera_mode = CameraServer::Mode::Unknown;
2✔
1290
    if (camera_mode == CAMERA_MODE_IMAGE) {
2✔
1291
        convert_camera_mode = CameraServer::Mode::Photo;
1✔
1292
    } else if (camera_mode == CAMERA_MODE_VIDEO) {
1✔
1293
        convert_camera_mode = CameraServer::Mode::Video;
1✔
1294
    }
1295

1296
    if (convert_camera_mode == CameraServer::Mode::Unknown) {
2✔
1297
        return _server_component_impl->make_command_ack_message(
×
1298
            command, MAV_RESULT::MAV_RESULT_DENIED);
×
1299
    }
1300

1301
    _last_set_mode_command = command;
2✔
1302
    _set_mode_callbacks(convert_camera_mode);
2✔
1303

1304
    return std::nullopt;
2✔
1305
}
1306

1307
std::optional<mavlink_command_ack_t>
1308
CameraServerImpl::process_set_camera_zoom(const MavlinkCommandReceiver::CommandLong& command)
×
1309
{
1310
    auto zoom_type = static_cast<CAMERA_ZOOM_TYPE>(command.params.param1);
×
1311
    auto zoom_value = command.params.param2;
×
1312

1313
    if (_zoom_in_start_callbacks.empty() && _zoom_out_start_callbacks.empty() &&
×
1314
        _zoom_stop_callbacks.empty() && _zoom_range_callbacks.empty()) {
×
1315
        LogWarn() << "No camera zoom is supported";
×
1316
        return _server_component_impl->make_command_ack_message(
×
1317
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1318
    }
1319

1320
    auto unsupported = [&]() {
×
1321
        LogWarn() << "unsupported set camera zoom type (" << (int)zoom_type << ") request";
×
1322
    };
×
1323

1324
    switch (zoom_type) {
×
1325
        case ZOOM_TYPE_CONTINUOUS:
×
1326
            if (zoom_value == -1.f) {
×
1327
                if (_zoom_out_start_callbacks.empty()) {
×
1328
                    unsupported();
×
1329
                    return _server_component_impl->make_command_ack_message(
×
1330
                        command, MAV_RESULT::MAV_RESULT_DENIED);
×
1331
                } else {
1332
                    _last_zoom_out_start_command = command;
×
1333
                    int dummy = 0;
×
1334
                    _zoom_out_start_callbacks(dummy);
×
1335
                }
1336
            } else if (zoom_value == 1.f) {
×
1337
                if (_zoom_in_start_callbacks.empty()) {
×
1338
                    unsupported();
×
1339
                    return _server_component_impl->make_command_ack_message(
×
1340
                        command, MAV_RESULT::MAV_RESULT_DENIED);
×
1341
                } else {
1342
                    _last_zoom_in_start_command = command;
×
1343
                    int dummy = 0;
×
1344
                    _zoom_in_start_callbacks(dummy);
×
1345
                }
1346
            } else if (zoom_value == 0.f) {
×
1347
                if (_zoom_stop_callbacks.empty()) {
×
1348
                    unsupported();
×
1349
                    return _server_component_impl->make_command_ack_message(
×
1350
                        command, MAV_RESULT::MAV_RESULT_DENIED);
×
1351
                } else {
1352
                    _last_zoom_stop_command = command;
×
1353
                    int dummy = 0;
×
1354
                    _zoom_stop_callbacks(dummy);
×
1355
                }
1356
            } else {
1357
                LogWarn() << "Invalid zoom value";
×
1358
                return _server_component_impl->make_command_ack_message(
×
1359
                    command, MAV_RESULT::MAV_RESULT_DENIED);
×
1360
            }
1361
            break;
×
1362
        case ZOOM_TYPE_RANGE:
×
1363
            if (_zoom_range_callbacks.empty()) {
×
1364
                unsupported();
×
1365
                return _server_component_impl->make_command_ack_message(
×
1366
                    command, MAV_RESULT::MAV_RESULT_DENIED);
×
1367

1368
            } else {
1369
                _last_zoom_range_command = command;
×
1370
                _zoom_range_callbacks(zoom_value);
×
1371
            }
1372
            break;
×
1373
        case ZOOM_TYPE_STEP:
×
1374
        // Fallthrough
1375
        case ZOOM_TYPE_FOCAL_LENGTH:
1376
        // Fallthrough
1377
        case ZOOM_TYPE_HORIZONTAL_FOV:
1378
        // Fallthrough
1379
        default:
1380
            unsupported();
×
1381
            return _server_component_impl->make_command_ack_message(
×
1382
                command, MAV_RESULT::MAV_RESULT_DENIED);
×
1383
            break;
1384
    }
1385

1386
    // For any success so far, we don't ack yet, but later when the respond function is called.
1387
    return std::nullopt;
×
1388
}
1389

1390
std::optional<mavlink_command_ack_t>
1391
CameraServerImpl::process_set_camera_focus(const MavlinkCommandReceiver::CommandLong& command)
×
1392
{
1393
    auto focus_type = static_cast<SET_FOCUS_TYPE>(command.params.param1);
×
1394
    auto focus_value = command.params.param2;
×
1395

1396
    UNUSED(focus_type);
×
1397
    UNUSED(focus_value);
×
1398

1399
    LogDebug() << "unsupported set camera focus request";
×
1400

1401
    return _server_component_impl->make_command_ack_message(
×
1402
        command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1403
}
1404

1405
std::optional<mavlink_command_ack_t>
1406
CameraServerImpl::process_set_storage_usage(const MavlinkCommandReceiver::CommandLong& command)
×
1407
{
1408
    auto storage_id = static_cast<uint8_t>(command.params.param1);
×
1409
    auto usage = static_cast<STORAGE_USAGE_FLAG>(command.params.param2);
×
1410

1411
    UNUSED(storage_id);
×
1412
    UNUSED(usage);
×
1413

1414
    LogDebug() << "unsupported set storage usage request";
×
1415

1416
    return _server_component_impl->make_command_ack_message(
×
1417
        command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1418
}
1419

1420
std::optional<mavlink_command_ack_t>
1421
CameraServerImpl::process_image_start_capture(const MavlinkCommandReceiver::CommandLong& command)
2✔
1422
{
1423
    auto interval_s = command.params.param2;
2✔
1424
    auto total_images = static_cast<int32_t>(command.params.param3);
2✔
1425
    auto seq_number = static_cast<int32_t>(command.params.param4);
2✔
1426

1427
    LogDebug() << "received image start capture request - interval: " << +interval_s
6✔
1428
               << " total: " << +total_images << " index: " << +seq_number;
6✔
1429

1430
    // TODO: validate parameters and return MAV_RESULT_DENIED not valid
1431

1432
    stop_image_capture_interval();
2✔
1433

1434
    if (_take_photo_callbacks.empty()) {
2✔
1435
        LogDebug() << "image capture requested with no take photo subscriber";
×
1436
        return _server_component_impl->make_command_ack_message(
×
1437
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1438
    }
1439

1440
    // single image capture
1441
    if (total_images == 1) {
2✔
1442
        // MAV_RESULT_ACCEPTED must be sent before CAMERA_IMAGE_CAPTURED
1443
        auto command_ack = _server_component_impl->make_command_ack_message(
1✔
1444
            command, MAV_RESULT::MAV_RESULT_IN_PROGRESS);
1445
        _server_component_impl->send_command_ack(command_ack);
1✔
1446

1447
        _last_take_photo_command = command;
1✔
1448

1449
        _take_photo_callbacks(seq_number);
1✔
1450

1451
        return std::nullopt;
1✔
1452
    }
1453

1454
    start_image_capture_interval(interval_s, total_images, seq_number);
1✔
1455

1456
    return _server_component_impl->make_command_ack_message(
2✔
1457
        command, MAV_RESULT::MAV_RESULT_ACCEPTED);
2✔
1458
}
1459

1460
std::optional<mavlink_command_ack_t>
1461
CameraServerImpl::process_image_stop_capture(const MavlinkCommandReceiver::CommandLong& command)
1✔
1462
{
1463
    LogDebug() << "received image stop capture request";
1✔
1464

1465
    // REVISIT: should we return something other that MAV_RESULT_ACCEPTED if
1466
    // there is not currently a capture interval active?
1467
    stop_image_capture_interval();
1✔
1468

1469
    return _server_component_impl->make_command_ack_message(
2✔
1470
        command, MAV_RESULT::MAV_RESULT_ACCEPTED);
2✔
1471
}
1472

1473
std::optional<mavlink_command_ack_t> CameraServerImpl::process_camera_image_capture_request(
×
1474
    const MavlinkCommandReceiver::CommandLong& command)
1475
{
1476
    auto seq_number = static_cast<uint32_t>(command.params.param1);
×
1477

1478
    UNUSED(seq_number);
×
1479

1480
    LogDebug() << "unsupported image capture request";
×
1481

1482
    return _server_component_impl->make_command_ack_message(
×
1483
        command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1484
}
1485

1486
std::optional<mavlink_command_ack_t>
1487
CameraServerImpl::process_video_start_capture(const MavlinkCommandReceiver::CommandLong& command)
×
1488
{
1489
    auto stream_id = static_cast<uint8_t>(command.params.param1);
×
1490
    auto status_frequency = command.params.param2;
×
1491

1492
    UNUSED(status_frequency);
×
1493

1494
    if (_start_video_callbacks.empty()) {
×
1495
        LogDebug() << "video start capture requested with no video start capture subscriber";
×
1496
        return _server_component_impl->make_command_ack_message(
×
1497
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1498
    }
1499

1500
    _last_start_video_command = command;
×
1501
    _start_video_callbacks(stream_id);
×
1502

1503
    return std::nullopt;
×
1504
}
1505

1506
std::optional<mavlink_command_ack_t>
1507
CameraServerImpl::process_video_stop_capture(const MavlinkCommandReceiver::CommandLong& command)
×
1508
{
1509
    auto stream_id = static_cast<uint8_t>(command.params.param1);
×
1510

1511
    if (_stop_video_callbacks.empty()) {
×
1512
        LogDebug() << "video stop capture requested with no video stop capture subscriber";
×
1513
        return _server_component_impl->make_command_ack_message(
×
1514
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1515
    }
1516

1517
    _last_stop_video_command = command;
×
1518
    _stop_video_callbacks(stream_id);
×
1519

1520
    return std::nullopt;
×
1521
}
1522

1523
std::optional<mavlink_command_ack_t>
1524
CameraServerImpl::process_video_start_streaming(const MavlinkCommandReceiver::CommandLong& command)
×
1525
{
1526
    auto stream_id = static_cast<uint8_t>(command.params.param1);
×
1527

1528
    if (_start_video_streaming_callbacks.empty()) {
×
1529
        LogDebug() << "video start streaming requested with no video start streaming subscriber";
×
1530
        return _server_component_impl->make_command_ack_message(
×
1531
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1532
    }
1533

1534
    _last_start_video_streaming_command = command;
×
1535
    _start_video_streaming_callbacks(stream_id);
×
1536

1537
    return std::nullopt;
×
1538
}
1539

1540
std::optional<mavlink_command_ack_t>
1541
CameraServerImpl::process_video_stop_streaming(const MavlinkCommandReceiver::CommandLong& command)
×
1542
{
1543
    auto stream_id = static_cast<uint8_t>(command.params.param1);
×
1544

1545
    if (_stop_video_streaming_callbacks.empty()) {
×
1546
        LogDebug() << "video stop streaming requested with no video stop streaming subscriber";
×
1547
        return _server_component_impl->make_command_ack_message(
×
1548
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1549
    }
1550

1551
    _last_stop_video_streaming_command = command;
×
1552
    _stop_video_streaming_callbacks(stream_id);
×
1553

1554
    return std::nullopt;
×
1555
}
1556

1557
std::optional<mavlink_command_ack_t> CameraServerImpl::process_video_stream_information_request(
1✔
1558
    const MavlinkCommandReceiver::CommandLong& command)
1559
{
1560
    auto stream_id = static_cast<uint8_t>(command.params.param1);
1✔
1561

1562
    UNUSED(stream_id);
1✔
1563

1564
    if (_is_video_streaming_set) {
1✔
1565
        auto command_ack = _server_component_impl->make_command_ack_message(
1✔
1566
            command, MAV_RESULT::MAV_RESULT_ACCEPTED);
1567
        _server_component_impl->send_command_ack(command_ack);
1✔
1568
        LogDebug() << "sent video streaming ack";
1✔
1569

1570
        const char name[32] = "";
1✔
1571

1572
        _video_streaming.rtsp_uri.resize(sizeof(mavlink_video_stream_information_t::uri));
1✔
1573

1574
        mavlink_message_t msg{};
1✔
1575
        mavlink_msg_video_stream_information_pack(
2✔
1576
            _server_component_impl->get_own_system_id(),
1✔
1577
            _server_component_impl->get_own_component_id(),
1✔
1578
            &msg,
1579
            0, // Stream id
1580
            0, // Count
1581
            VIDEO_STREAM_TYPE_RTSP,
1582
            VIDEO_STREAM_STATUS_FLAGS_RUNNING,
1583
            0, // famerate
1584
            0, // resolution horizontal
1585
            0, // resolution vertical
1586
            0, // bitrate
1587
            0, // rotation
1588
            0, // horizontal field of view
1589
            name,
1590
            _video_streaming.rtsp_uri.c_str());
1591

1592
        _server_component_impl->send_message(msg);
1✔
1593

1594
        // Ack already sent.
1595
        return std::nullopt;
1✔
1596

1597
    } else {
1598
        return _server_component_impl->make_command_ack_message(
×
1599
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1600
    }
1601
}
1602

1603
std::optional<mavlink_command_ack_t> CameraServerImpl::process_video_stream_status_request(
6✔
1604
    const MavlinkCommandReceiver::CommandLong& command)
1605
{
1606
    auto stream_id = static_cast<uint8_t>(command.params.param1);
6✔
1607

1608
    UNUSED(stream_id);
6✔
1609

1610
    if (!_is_video_streaming_set) {
6✔
1611
        return _server_component_impl->make_command_ack_message(
4✔
1612
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
4✔
1613
    }
1614

1615
    auto command_ack =
4✔
1616
        _server_component_impl->make_command_ack_message(command, MAV_RESULT::MAV_RESULT_ACCEPTED);
4✔
1617
    _server_component_impl->send_command_ack(command_ack);
4✔
1618
    LogDebug() << "sent video streaming ack";
4✔
1619

1620
    mavlink_message_t msg{};
4✔
1621
    mavlink_msg_video_stream_status_pack(
4✔
1622
        _server_component_impl->get_own_system_id(),
4✔
1623
        _server_component_impl->get_own_component_id(),
4✔
1624
        &msg,
1625
        0, // Stream id
1626
        VIDEO_STREAM_STATUS_FLAGS_RUNNING,
1627
        0, // framerate
1628
        0, // resolution horizontal
1629
        0, // resolution vertical
1630
        0, // bitrate
1631
        0, // rotation
1632
        0 // horizontal field of view
1633
    );
1634
    _server_component_impl->send_message(msg);
4✔
1635

1636
    // ack was already sent
1637
    return std::nullopt;
4✔
1638
}
1639

1640
CameraServer::ZoomInStartHandle
1641
CameraServerImpl::subscribe_zoom_in_start(const CameraServer::ZoomInStartCallback& callback)
×
1642
{
1643
    return _zoom_in_start_callbacks.subscribe(callback);
×
1644
}
1645

1646
void CameraServerImpl::unsubscribe_zoom_in_start(CameraServer::ZoomInStartHandle handle)
×
1647
{
1648
    _zoom_in_start_callbacks.unsubscribe(handle);
×
1649
}
×
1650

1651
CameraServer::Result
1652
CameraServerImpl::respond_zoom_in_start(CameraServer::CameraFeedback zoom_in_start_feedback)
×
1653
{
1654
    switch (zoom_in_start_feedback) {
×
1655
        default:
×
1656
            // Fallthrough
1657
        case CameraServer::CameraFeedback::Unknown:
1658
            return CameraServer::Result::Error;
×
1659
        case CameraServer::CameraFeedback::Ok: {
×
1660
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1661
                _last_zoom_in_start_command, MAV_RESULT_ACCEPTED);
×
1662
            _server_component_impl->send_command_ack(command_ack);
×
1663
            return CameraServer::Result::Success;
×
1664
        }
1665
        case CameraServer::CameraFeedback::Busy: {
×
1666
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1667
                _last_zoom_in_start_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
1668
            _server_component_impl->send_command_ack(command_ack);
×
1669
            return CameraServer::Result::Success;
×
1670
        }
1671
        case CameraServer::CameraFeedback::Failed: {
×
1672
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1673
                _last_zoom_in_start_command, MAV_RESULT_FAILED);
×
1674
            _server_component_impl->send_command_ack(command_ack);
×
1675
            return CameraServer::Result::Success;
×
1676
        }
1677
    }
1678
}
1679

1680
CameraServer::ZoomOutStartHandle
1681
CameraServerImpl::subscribe_zoom_out_start(const CameraServer::ZoomOutStartCallback& callback)
×
1682
{
1683
    return _zoom_out_start_callbacks.subscribe(callback);
×
1684
}
1685

1686
void CameraServerImpl::unsubscribe_zoom_out_start(CameraServer::ZoomOutStartHandle handle)
×
1687
{
1688
    _zoom_out_start_callbacks.unsubscribe(handle);
×
1689
}
×
1690

1691
CameraServer::Result
1692
CameraServerImpl::respond_zoom_out_start(CameraServer::CameraFeedback zoom_out_start_feedback)
×
1693
{
1694
    switch (zoom_out_start_feedback) {
×
1695
        default:
×
1696
            // Fallthrough
1697
        case CameraServer::CameraFeedback::Unknown:
1698
            return CameraServer::Result::Error;
×
1699
        case CameraServer::CameraFeedback::Ok: {
×
1700
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1701
                _last_zoom_out_start_command, MAV_RESULT_ACCEPTED);
×
1702
            _server_component_impl->send_command_ack(command_ack);
×
1703
            return CameraServer::Result::Success;
×
1704
        }
1705
        case CameraServer::CameraFeedback::Busy: {
×
1706
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1707
                _last_zoom_out_start_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
1708
            _server_component_impl->send_command_ack(command_ack);
×
1709
            return CameraServer::Result::Success;
×
1710
        }
1711
        case CameraServer::CameraFeedback::Failed: {
×
1712
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1713
                _last_zoom_out_start_command, MAV_RESULT_FAILED);
×
1714
            _server_component_impl->send_command_ack(command_ack);
×
1715
            return CameraServer::Result::Success;
×
1716
        }
1717
    }
1718
}
1719

1720
CameraServer::ZoomStopHandle
1721
CameraServerImpl::subscribe_zoom_stop(const CameraServer::ZoomStopCallback& callback)
×
1722
{
1723
    return _zoom_stop_callbacks.subscribe(callback);
×
1724
}
1725

1726
void CameraServerImpl::unsubscribe_zoom_stop(CameraServer::ZoomStopHandle handle)
×
1727
{
1728
    _zoom_stop_callbacks.unsubscribe(handle);
×
1729
}
×
1730

1731
CameraServer::Result
1732
CameraServerImpl::respond_zoom_stop(CameraServer::CameraFeedback zoom_stop_feedback)
×
1733
{
1734
    switch (zoom_stop_feedback) {
×
1735
        default:
×
1736
            // Fallthrough
1737
        case CameraServer::CameraFeedback::Unknown:
1738
            return CameraServer::Result::Error;
×
1739
        case CameraServer::CameraFeedback::Ok: {
×
1740
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1741
                _last_zoom_stop_command, MAV_RESULT_ACCEPTED);
×
1742
            _server_component_impl->send_command_ack(command_ack);
×
1743
            return CameraServer::Result::Success;
×
1744
        }
1745
        case CameraServer::CameraFeedback::Busy: {
×
1746
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1747
                _last_zoom_stop_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
1748
            _server_component_impl->send_command_ack(command_ack);
×
1749
            return CameraServer::Result::Success;
×
1750
        }
1751
        case CameraServer::CameraFeedback::Failed: {
×
1752
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1753
                _last_zoom_stop_command, MAV_RESULT_FAILED);
×
1754
            _server_component_impl->send_command_ack(command_ack);
×
1755
            return CameraServer::Result::Success;
×
1756
        }
1757
    }
1758
}
1759

1760
CameraServer::ZoomRangeHandle
1761
CameraServerImpl::subscribe_zoom_range(const CameraServer::ZoomRangeCallback& callback)
×
1762
{
1763
    return _zoom_range_callbacks.subscribe(callback);
×
1764
}
1765

1766
void CameraServerImpl::unsubscribe_zoom_range(CameraServer::ZoomRangeHandle handle)
×
1767
{
1768
    _zoom_range_callbacks.unsubscribe(handle);
×
1769
}
×
1770

1771
CameraServer::Result
1772
CameraServerImpl::respond_zoom_range(CameraServer::CameraFeedback zoom_range_feedback)
×
1773
{
1774
    switch (zoom_range_feedback) {
×
1775
        case CameraServer::CameraFeedback::Ok: {
×
1776
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1777
                _last_zoom_range_command, MAV_RESULT_ACCEPTED);
×
1778
            _server_component_impl->send_command_ack(command_ack);
×
1779
            return CameraServer::Result::Success;
×
1780
        }
1781
        case CameraServer::CameraFeedback::Busy: {
×
1782
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1783
                _last_zoom_range_command, MAV_RESULT_TEMPORARILY_REJECTED);
×
1784
            _server_component_impl->send_command_ack(command_ack);
×
1785
            return CameraServer::Result::Success;
×
1786
        }
1787
        case CameraServer::CameraFeedback::Failed: {
×
1788
            auto command_ack = _server_component_impl->make_command_ack_message(
×
1789
                _last_zoom_range_command, MAV_RESULT_FAILED);
×
1790
            _server_component_impl->send_command_ack(command_ack);
×
1791
            return CameraServer::Result::Success;
×
1792
        }
1793
        case CameraServer::CameraFeedback::Unknown:
×
1794
            // Fallthrough
1795
        default:
1796
            return CameraServer::Result::Error;
×
1797
    }
1798
}
1799

1800
std::optional<mavlink_command_ack_t>
1801
CameraServerImpl::process_track_point_command(const MavlinkCommandReceiver::CommandLong& command)
×
1802
{
1803
    if (!is_command_sender_ok(command)) {
×
1804
        LogWarn() << "Incoming track point command is for target sysid "
×
1805
                  << int(command.target_system_id) << " instead of "
×
1806
                  << int(_server_component_impl->get_own_system_id());
×
1807
        return std::nullopt;
×
1808
    }
1809

1810
    if (_tracking_point_callbacks.empty()) {
×
1811
        LogDebug() << "Track point requested with no user callback provided";
×
1812
        return _server_component_impl->make_command_ack_message(
×
1813
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1814
    }
1815

1816
    CameraServer::TrackPoint track_point{
×
1817
        command.params.param1, command.params.param2, command.params.param3};
×
1818

1819
    _last_track_point_command = command;
×
1820
    _tracking_point_callbacks(track_point);
×
1821
    // We don't send an ack but leave that to the user.
1822
    return std::nullopt;
×
1823
}
1824

1825
std::optional<mavlink_command_ack_t> CameraServerImpl::process_track_rectangle_command(
×
1826
    const MavlinkCommandReceiver::CommandLong& command)
1827
{
1828
    if (!is_command_sender_ok(command)) {
×
1829
        LogWarn() << "Incoming track rectangle command is for target sysid "
×
1830
                  << int(command.target_system_id) << " instead of "
×
1831
                  << int(_server_component_impl->get_own_system_id());
×
1832
        return std::nullopt;
×
1833
    }
1834

1835
    if (_tracking_rectangle_callbacks.empty()) {
×
1836
        LogDebug() << "Track rectangle requested with no user callback provided";
×
1837
        return _server_component_impl->make_command_ack_message(
×
1838
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1839
    }
1840

1841
    CameraServer::TrackRectangle track_rectangle{
×
1842
        command.params.param1, command.params.param2, command.params.param3, command.params.param4};
×
1843

1844
    _last_track_rectangle_command = command;
×
1845
    _tracking_rectangle_callbacks(track_rectangle);
×
1846
    // We don't send an ack but leave that to the user.
1847
    return std::nullopt;
×
1848
}
1849

1850
std::optional<mavlink_command_ack_t>
1851
CameraServerImpl::process_track_off_command(const MavlinkCommandReceiver::CommandLong& command)
×
1852
{
1853
    if (!is_command_sender_ok(command)) {
×
1854
        LogWarn() << "Incoming track off command is for target sysid "
×
1855
                  << int(command.target_system_id) << " instead of "
×
1856
                  << int(_server_component_impl->get_own_system_id());
×
1857
        return std::nullopt;
×
1858
    }
1859

1860
    if (_tracking_off_callbacks.empty()) {
×
1861
        LogDebug() << "Tracking off requested with no user callback provided";
×
1862
        return _server_component_impl->make_command_ack_message(
×
1863
            command, MAV_RESULT::MAV_RESULT_UNSUPPORTED);
×
1864
    }
1865

1866
    _last_tracking_off_command = command;
×
1867
    _tracking_off_callbacks(0);
×
1868
    // We don't send an ack but leave that to the user.
1869
    return std::nullopt;
×
1870
}
1871

1872
std::optional<mavlink_command_ack_t>
1873
CameraServerImpl::process_set_message_interval(const MavlinkCommandReceiver::CommandLong& command)
×
1874
{
1875
    if (!is_command_sender_ok(command)) {
×
1876
        LogWarn() << "Incoming track off command is for target sysid "
×
1877
                  << int(command.target_system_id) << " instead of "
×
1878
                  << int(_server_component_impl->get_own_system_id());
×
1879
        return std::nullopt;
×
1880
    }
1881

1882
    auto message_id = static_cast<uint32_t>(command.params.param1);
×
1883
    auto interval_us = static_cast<int32_t>(command.params.param2);
×
1884
    UNUSED(message_id);
×
1885

1886
    // Interval value of -1 means to disable sending messages
1887
    if (interval_us < 0) {
×
1888
        stop_sending_tracking_status();
×
1889
    } else {
1890
        start_sending_tracking_status(interval_us);
×
1891
    }
1892

1893
    // Always send the "Accepted" result
1894
    return _server_component_impl->make_command_ack_message(
×
1895
        command, MAV_RESULT::MAV_RESULT_ACCEPTED);
×
1896
}
1897

1898
void CameraServerImpl::send_tracking_status_with_interval(uint32_t interval_us)
×
1899
{
1900
    while (true) {
1901
        std::this_thread::sleep_for(std::chrono::microseconds{interval_us});
×
1902
        {
1903
            std::scoped_lock lg{_tracking_status_mutex};
×
1904
            if (!_sending_tracking_status) {
×
1905
                return;
×
1906
            }
1907
        }
×
1908
        _server_component_impl->queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
×
1909
            mavlink_message_t message;
1910
            std::lock_guard<std::mutex> lg{_tracking_status_mutex};
×
1911

1912
            // The message is filled based on current tracking mode
1913
            switch (_tracking_mode) {
×
1914
                default:
×
1915
                    // Fallthrough
1916
                case TrackingMode::NONE:
1917

1918
                    mavlink_msg_camera_tracking_image_status_pack_chan(
×
1919
                        mavlink_address.system_id,
×
1920
                        mavlink_address.component_id,
×
1921
                        channel,
1922
                        &message,
1923
                        CAMERA_TRACKING_STATUS_FLAGS_IDLE,
1924
                        CAMERA_TRACKING_MODE_NONE,
1925
                        CAMERA_TRACKING_TARGET_DATA_NONE,
1926
                        0.0f,
1927
                        0.0f,
1928
                        0.0f,
1929
                        0.0f,
1930
                        0.0f,
1931
                        0.0f,
1932
                        0.0f);
1933
                    break;
×
1934
                case TrackingMode::POINT:
×
1935

1936
                    mavlink_msg_camera_tracking_image_status_pack_chan(
×
1937
                        mavlink_address.system_id,
×
1938
                        mavlink_address.component_id,
×
1939
                        channel,
1940
                        &message,
1941
                        CAMERA_TRACKING_STATUS_FLAGS_ACTIVE,
1942
                        CAMERA_TRACKING_MODE_POINT,
1943
                        CAMERA_TRACKING_TARGET_DATA_IN_STATUS,
1944
                        _tracked_point.point_x,
1945
                        _tracked_point.point_y,
1946
                        _tracked_point.radius,
1947
                        0.0f,
1948
                        0.0f,
1949
                        0.0f,
1950
                        0.0f);
1951
                    break;
×
1952

1953
                case TrackingMode::RECTANGLE:
×
1954

1955
                    mavlink_msg_camera_tracking_image_status_pack_chan(
×
1956
                        mavlink_address.system_id,
×
1957
                        mavlink_address.component_id,
×
1958
                        channel,
1959
                        &message,
1960
                        CAMERA_TRACKING_STATUS_FLAGS_ACTIVE,
1961
                        CAMERA_TRACKING_MODE_RECTANGLE,
1962
                        CAMERA_TRACKING_TARGET_DATA_IN_STATUS,
1963
                        0.0f,
1964
                        0.0f,
1965
                        0.0f,
1966
                        _tracked_rectangle.top_left_corner_x,
1967
                        _tracked_rectangle.top_left_corner_y,
1968
                        _tracked_rectangle.bottom_right_corner_x,
1969
                        _tracked_rectangle.bottom_right_corner_y);
1970
                    break;
×
1971
            }
1972
            return message;
×
1973
        });
×
1974
    }
×
1975
}
1976

1977
void CameraServerImpl::start_sending_tracking_status(uint32_t interval_ms)
×
1978
{
1979
    // Stop sending status with the old interval
1980
    stop_sending_tracking_status();
×
1981
    _sending_tracking_status = true;
×
1982
    _tracking_status_sending_thread =
×
1983
        std::thread{&CameraServerImpl::send_tracking_status_with_interval, this, interval_ms};
×
1984
}
×
1985

1986
void CameraServerImpl::stop_sending_tracking_status()
×
1987
{
1988
    // Firstly, ask the other thread to stop sending the status
1989
    {
1990
        std::scoped_lock lg{_tracking_status_mutex};
×
1991
        _sending_tracking_status = false;
×
1992
    }
×
1993
    // If the thread was active, wait for it to finish
1994
    if (_tracking_status_sending_thread.joinable()) {
×
1995
        _tracking_status_sending_thread.join();
×
1996
    }
1997
}
×
1998

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