|
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" |
@@ -790,6 +791,9 @@ std::pair<ConnectionResult, Mavsdk::ConnectionHandle> MavsdkImpl::add_any_connec |
790 | 791 | [this, forwarding_option](const CliArg::Serial& serial) { |
791 | 792 | return add_serial_connection( |
792 | 793 | serial.path, serial.baudrate, serial.flow_control_enabled, forwarding_option); |
| 794 | + }, |
| 795 | + [this, forwarding_option](const CliArg::Raw&) { |
| 796 | + return add_raw_connection(forwarding_option); |
793 | 797 | }}, |
794 | 798 | cli_arg.protocol); |
795 | 799 | } |
@@ -930,6 +934,44 @@ std::pair<ConnectionResult, Mavsdk::ConnectionHandle> MavsdkImpl::add_serial_con |
930 | 934 | } |
931 | 935 | } |
932 | 936 |
|
| 937 | +std::pair<ConnectionResult, Mavsdk::ConnectionHandle> |
| 938 | +MavsdkImpl::add_raw_connection(ForwardingOption forwarding_option) |
| 939 | +{ |
| 940 | + // Check if a raw connection already exists |
| 941 | + if (find_raw_connection() != nullptr) { |
| 942 | + LogErr() << "Raw connection already exists. Only one raw connection is allowed."; |
| 943 | + return {ConnectionResult::ConnectionError, Mavsdk::ConnectionHandle{}}; |
| 944 | + } |
| 945 | + |
| 946 | + auto new_conn = std::make_unique<RawConnection>( |
| 947 | + [this](mavlink_message_t& message, Connection* connection) { |
| 948 | + receive_message(message, connection); |
| 949 | + }, |
| 950 | + [this](const Mavsdk::MavlinkMessage& message, Connection* connection) { |
| 951 | + receive_libmav_message(message, connection); |
| 952 | + }, |
| 953 | + *this, |
| 954 | + forwarding_option); |
| 955 | + |
| 956 | + if (!new_conn) { |
| 957 | + return {ConnectionResult::ConnectionError, Mavsdk::ConnectionHandle{}}; |
| 958 | + } |
| 959 | + |
| 960 | + ConnectionResult ret = new_conn->start(); |
| 961 | + if (ret != ConnectionResult::Success) { |
| 962 | + return {ret, Mavsdk::ConnectionHandle{}}; |
| 963 | + } |
| 964 | + |
| 965 | + auto handle = add_connection(std::move(new_conn)); |
| 966 | + |
| 967 | + // Enable heartbeats for raw connection |
| 968 | + auto new_configuration = get_configuration(); |
| 969 | + new_configuration.set_always_send_heartbeats(true); |
| 970 | + set_configuration(new_configuration); |
| 971 | + |
| 972 | + return {ConnectionResult::Success, handle}; |
| 973 | +} |
| 974 | + |
933 | 975 | Mavsdk::ConnectionHandle MavsdkImpl::add_connection(std::unique_ptr<Connection>&& new_connection) |
934 | 976 | { |
935 | 977 | std::lock_guard lock(_mutex); |
@@ -1318,6 +1360,57 @@ void MavsdkImpl::unsubscribe_outgoing_messages_json(Mavsdk::InterceptJsonHandle |
1318 | 1360 | } |
1319 | 1361 | } |
1320 | 1362 |
|
| 1363 | +RawConnection* MavsdkImpl::find_raw_connection() |
| 1364 | +{ |
| 1365 | + std::lock_guard lock(_mutex); |
| 1366 | + |
| 1367 | + for (auto& entry : _connections) { |
| 1368 | + auto* raw_conn = dynamic_cast<RawConnection*>(entry.connection.get()); |
| 1369 | + if (raw_conn != nullptr) { |
| 1370 | + return raw_conn; |
| 1371 | + } |
| 1372 | + } |
| 1373 | + return nullptr; |
| 1374 | +} |
| 1375 | + |
| 1376 | +void MavsdkImpl::pass_received_raw_bytes(const char* bytes, size_t length) |
| 1377 | +{ |
| 1378 | + auto* raw_conn = find_raw_connection(); |
| 1379 | + if (raw_conn == nullptr) { |
| 1380 | + LogErr() |
| 1381 | + << "No raw connection available. Please add one using add_any_connection(\"raw://\")"; |
| 1382 | + return; |
| 1383 | + } |
| 1384 | + |
| 1385 | + raw_conn->receive(bytes, length); |
| 1386 | +} |
| 1387 | + |
| 1388 | +Mavsdk::RawBytesHandle |
| 1389 | +MavsdkImpl::subscribe_raw_bytes_to_be_sent(const Mavsdk::RawBytesCallback& callback) |
| 1390 | +{ |
| 1391 | + if (find_raw_connection() == nullptr) { |
| 1392 | + LogWarn() << "No raw connection available. Subscription will only receive bytes after you " |
| 1393 | + "add a connection using add_any_connection(\"raw://\")"; |
| 1394 | + } |
| 1395 | + return _raw_bytes_subscriptions.subscribe(callback); |
| 1396 | +} |
| 1397 | + |
| 1398 | +void MavsdkImpl::unsubscribe_raw_bytes_to_be_sent(Mavsdk::RawBytesHandle handle) |
| 1399 | +{ |
| 1400 | + _raw_bytes_subscriptions.unsubscribe(handle); |
| 1401 | +} |
| 1402 | + |
| 1403 | +bool MavsdkImpl::notify_raw_bytes_sent(const char* bytes, size_t length) |
| 1404 | +{ |
| 1405 | + if (_raw_bytes_subscriptions.empty()) { |
| 1406 | + return false; |
| 1407 | + } |
| 1408 | + |
| 1409 | + _raw_bytes_subscriptions(bytes, length); |
| 1410 | + |
| 1411 | + return true; |
| 1412 | +} |
| 1413 | + |
1321 | 1414 | Mavsdk::ConnectionErrorHandle |
1322 | 1415 | MavsdkImpl::subscribe_connection_errors(Mavsdk::ConnectionErrorCallback callback) |
1323 | 1416 | { |
|
0 commit comments