|
9 | 9 | #include "tcp_client_connection.h" |
10 | 10 | #include "tcp_server_connection.h" |
11 | 11 | #include "udp_connection.h" |
| 12 | +#include "raw_connection.h" |
12 | 13 | #include "system.h" |
13 | 14 | #include "system_impl.h" |
14 | 15 | #include "serial_connection.h" |
@@ -789,6 +790,9 @@ std::pair<ConnectionResult, Mavsdk::ConnectionHandle> MavsdkImpl::add_any_connec |
789 | 790 | [this, forwarding_option](const CliArg::Serial& serial) { |
790 | 791 | return add_serial_connection( |
791 | 792 | serial.path, serial.baudrate, serial.flow_control_enabled, forwarding_option); |
| 793 | + }, |
| 794 | + [this, forwarding_option](const CliArg::Raw&) { |
| 795 | + return add_raw_connection(forwarding_option); |
792 | 796 | }}, |
793 | 797 | cli_arg.protocol); |
794 | 798 | } |
@@ -929,6 +933,44 @@ std::pair<ConnectionResult, Mavsdk::ConnectionHandle> MavsdkImpl::add_serial_con |
929 | 933 | } |
930 | 934 | } |
931 | 935 |
|
| 936 | +std::pair<ConnectionResult, Mavsdk::ConnectionHandle> |
| 937 | +MavsdkImpl::add_raw_connection(ForwardingOption forwarding_option) |
| 938 | +{ |
| 939 | + // Check if a raw connection already exists |
| 940 | + if (find_raw_connection() != nullptr) { |
| 941 | + LogErr() << "Raw connection already exists. Only one raw connection is allowed."; |
| 942 | + return {ConnectionResult::ConnectionError, Mavsdk::ConnectionHandle{}}; |
| 943 | + } |
| 944 | + |
| 945 | + auto new_conn = std::make_unique<RawConnection>( |
| 946 | + [this](mavlink_message_t& message, Connection* connection) { |
| 947 | + receive_message(message, connection); |
| 948 | + }, |
| 949 | + [this](const Mavsdk::MavlinkMessage& message, Connection* connection) { |
| 950 | + receive_libmav_message(message, connection); |
| 951 | + }, |
| 952 | + *this, |
| 953 | + forwarding_option); |
| 954 | + |
| 955 | + if (!new_conn) { |
| 956 | + return {ConnectionResult::ConnectionError, Mavsdk::ConnectionHandle{}}; |
| 957 | + } |
| 958 | + |
| 959 | + ConnectionResult ret = new_conn->start(); |
| 960 | + if (ret != ConnectionResult::Success) { |
| 961 | + return {ret, Mavsdk::ConnectionHandle{}}; |
| 962 | + } |
| 963 | + |
| 964 | + auto handle = add_connection(std::move(new_conn)); |
| 965 | + |
| 966 | + // Enable heartbeats for raw connection |
| 967 | + auto new_configuration = get_configuration(); |
| 968 | + new_configuration.set_always_send_heartbeats(true); |
| 969 | + set_configuration(new_configuration); |
| 970 | + |
| 971 | + return {ConnectionResult::Success, handle}; |
| 972 | +} |
| 973 | + |
932 | 974 | Mavsdk::ConnectionHandle MavsdkImpl::add_connection(std::unique_ptr<Connection>&& new_connection) |
933 | 975 | { |
934 | 976 | std::lock_guard lock(_mutex); |
@@ -1299,6 +1341,58 @@ void MavsdkImpl::unsubscribe_outgoing_messages_json(Mavsdk::InterceptJsonHandle |
1299 | 1341 | } |
1300 | 1342 | } |
1301 | 1343 |
|
| 1344 | +RawConnection* MavsdkImpl::find_raw_connection() |
| 1345 | +{ |
| 1346 | + std::lock_guard lock(_mutex); |
| 1347 | + |
| 1348 | + for (auto& entry : _connections) { |
| 1349 | + auto* raw_conn = dynamic_cast<RawConnection*>(entry.connection.get()); |
| 1350 | + if (raw_conn != nullptr) { |
| 1351 | + return raw_conn; |
| 1352 | + } |
| 1353 | + } |
| 1354 | + return nullptr; |
| 1355 | +} |
| 1356 | + |
| 1357 | +bool MavsdkImpl::send_raw_bytes(const char* bytes, size_t length) |
| 1358 | +{ |
| 1359 | + auto* raw_conn = find_raw_connection(); |
| 1360 | + if (raw_conn == nullptr) { |
| 1361 | + LogErr() |
| 1362 | + << "No raw connection available. Please add one using add_any_connection(\"raw://\")"; |
| 1363 | + return false; |
| 1364 | + } |
| 1365 | + |
| 1366 | + raw_conn->receive(bytes, length); |
| 1367 | + return true; |
| 1368 | +} |
| 1369 | + |
| 1370 | +Mavsdk::RawBytesHandle MavsdkImpl::subscribe_raw_bytes(const Mavsdk::RawBytesCallback& callback) |
| 1371 | +{ |
| 1372 | + if (find_raw_connection() == nullptr) { |
| 1373 | + LogWarn() << "No raw connection available. Subscription will only receive bytes after you " |
| 1374 | + "add a connection using add_any_connection(\"raw://\")"; |
| 1375 | + } |
| 1376 | + return _raw_bytes_subscriptions.subscribe(callback); |
| 1377 | +} |
| 1378 | + |
| 1379 | +void MavsdkImpl::unsubscribe_raw_bytes(Mavsdk::RawBytesHandle handle) |
| 1380 | +{ |
| 1381 | + _raw_bytes_subscriptions.unsubscribe(handle); |
| 1382 | +} |
| 1383 | + |
| 1384 | +bool MavsdkImpl::notify_raw_bytes_sent(const char* bytes, size_t length) |
| 1385 | +{ |
| 1386 | + if (_raw_bytes_subscriptions.empty()) { |
| 1387 | + return false; |
| 1388 | + } |
| 1389 | + |
| 1390 | + _raw_bytes_subscriptions.queue( |
| 1391 | + bytes, length, [this](const std::function<void()>& func) { call_user_callback(func); }); |
| 1392 | + |
| 1393 | + return true; |
| 1394 | +} |
| 1395 | + |
1302 | 1396 | Mavsdk::ConnectionErrorHandle |
1303 | 1397 | MavsdkImpl::subscribe_connection_errors(Mavsdk::ConnectionErrorCallback callback) |
1304 | 1398 | { |
|
0 commit comments