diff --git a/isobus/include/isobus/isobus/can_transport_protocol.hpp b/isobus/include/isobus/isobus/can_transport_protocol.hpp index d4aa89621..45d9c940f 100644 --- a/isobus/include/isobus/isobus/can_transport_protocol.hpp +++ b/isobus/include/isobus/isobus/can_transport_protocol.hpp @@ -63,6 +63,15 @@ namespace isobus AnyOtherError = 250 ///< Any reason not defined in the standard }; + /// @brief The "Role of Sender" of a TP.Conn_Abort, byte 3 bits 1 to 2 (ISO 11783-3:2026 5.9.4.5) + enum class ConnectionAbortSenderRole : std::uint8_t + { + Originator = 0, ///< The abort is sent by the CF that sent the TP.CM_RTS + Receiver = 1, ///< The abort is sent by the CF that sent the TP.CM_CTS + Reserved = 2, ///< Reserved, handled like NotSpecified + NotSpecified = 3 ///< The role is not specified, as sent by devices built to earlier editions that send this byte as FF + }; + /// @brief A storage object to keep track of session information internally class TransportProtocolSession : public TransportProtocolSessionBase { @@ -227,11 +236,13 @@ namespace isobus /// @param[in] receiver The receiver of the abort /// @param[in] parameterGroupNumber The PGN of the TP "session" we're aborting /// @param[in] reason The reason we're aborting the session + /// @param[in] role Our role in the session, Originator if we sent the RTS or Receiver if we sent the CTS /// @returns true if the abort was send OK, false if not sent bool send_abort(std::shared_ptr sender, std::shared_ptr receiver, std::uint32_t parameterGroupNumber, - ConnectionAbortReason reason) const; + ConnectionAbortReason reason, + ConnectionAbortSenderRole role) const; /// @brief Gracefully closes a session to prepare for a new session /// @param[in] session The session to close @@ -297,7 +308,12 @@ namespace isobus /// @param[in] destination The shared pointer to the destination control function. /// @param[in] parameterGroupNumber The PGN (Parameter Group Number) of the message. /// @param[in] reason The reason for the connection abort. - void process_abort(const std::shared_ptr source, const std::shared_ptr destination, std::uint32_t parameterGroupNumber, TransportProtocolManager::ConnectionAbortReason reason); + /// @param[in] role The "Role of Sender" of the abort, which tells if it closes the session the source sends or the one it receives + void process_abort(const std::shared_ptr source, + const std::shared_ptr destination, + std::uint32_t parameterGroupNumber, + TransportProtocolManager::ConnectionAbortReason reason, + ConnectionAbortSenderRole role); /// @brief Processes a connection management message. /// @param[in] message The CAN message to be processed. diff --git a/isobus/src/can_transport_protocol.cpp b/isobus/src/can_transport_protocol.cpp index 651a51e42..9bf726614 100644 --- a/isobus/src/can_transport_protocol.cpp +++ b/isobus/src/can_transport_protocol.cpp @@ -201,7 +201,7 @@ namespace isobus { // TODO: consider using maximum memory instead of maximum number of sessions LOG_WARNING("[TP]: Replying with abort to Request To Send (RTS) for 0x%05X, configured maximum number of sessions reached.", parameterGroupNumber); - send_abort(std::static_pointer_cast(destination), source, parameterGroupNumber, ConnectionAbortReason::AlreadyInCMSession); + send_abort(std::static_pointer_cast(destination), source, parameterGroupNumber, ConnectionAbortReason::AlreadyInCMSession, ConnectionAbortSenderRole::Receiver); } else if ((totalMessageSize <= CAN_DATA_LENGTH) || (totalMessageSize > MAX_PROTOCOL_DATA_LENGTH)) { @@ -209,7 +209,7 @@ namespace isobus parameterGroupNumber, totalMessageSize, MAX_PROTOCOL_DATA_LENGTH); - send_abort(std::static_pointer_cast(destination), source, parameterGroupNumber, ConnectionAbortReason::TotalMessageSizeTooBig); + send_abort(std::static_pointer_cast(destination), source, parameterGroupNumber, ConnectionAbortReason::TotalMessageSizeTooBig, ConnectionAbortSenderRole::Receiver); } else { @@ -281,7 +281,8 @@ namespace isobus { LOG_ERROR("[TP]: Received a Clear To Send (CTS) message for 0x%05X while a session already existed for this source and destination, sending abort for both...", parameterGroupNumber); abort_session(session, ConnectionAbortReason::ClearToSendReceivedWhileTransferInProgress); - send_abort(std::static_pointer_cast(destination), source, parameterGroupNumber, ConnectionAbortReason::ClearToSendReceivedWhileTransferInProgress); + // The CTS was sent to us as the originator of a transfer of this PGN + send_abort(std::static_pointer_cast(destination), source, parameterGroupNumber, ConnectionAbortReason::ClearToSendReceivedWhileTransferInProgress, ConnectionAbortSenderRole::Originator); } else if (nextPacketNumber > session->get_total_number_of_packets()) { @@ -342,12 +343,18 @@ namespace isobus void TransportProtocolManager::process_abort(const std::shared_ptr source, const std::shared_ptr destination, std::uint32_t parameterGroupNumber, - TransportProtocolManager::ConnectionAbortReason reason) + TransportProtocolManager::ConnectionAbortReason reason, + ConnectionAbortSenderRole role) { bool foundSession = false; + // ISO 11783-3:2026 5.9.4.5: the role tells which of two sessions of the same PGN in opposite directions to close. + // A role that is not specified, as from a device built to an earlier edition, closes both. + const bool closesSessionFromSource = (ConnectionAbortSenderRole::Receiver != role); + const bool closesSessionToSource = (ConnectionAbortSenderRole::Originator != role); + auto session = get_session(source, destination); - if ((nullptr != session) && (session->get_parameter_group_number() == parameterGroupNumber)) + if (closesSessionFromSource && (nullptr != session) && (session->get_parameter_group_number() == parameterGroupNumber)) { foundSession = true; LOG_ERROR("[TP]: Received an abort (reason=%hu) for an rx session for parameterGroupNumber 0x%05X", @@ -356,7 +363,7 @@ namespace isobus close_session(session, false); } session = get_session(destination, source); - if ((nullptr != session) && (session->get_parameter_group_number() == parameterGroupNumber)) + if (closesSessionToSource && (nullptr != session) && (session->get_parameter_group_number() == parameterGroupNumber)) { foundSession = true; LOG_ERROR("[TP]: Received an abort (reason=%hu) for a tx session for parameterGroupNumber 0x%05X", @@ -467,10 +474,12 @@ namespace isobus else { const auto reason = static_cast(message.get_uint8_at(1)); + const auto role = static_cast(message.get_uint8_at(2) & 0x03); process_abort(message.get_source_control_function(), message.get_destination_control_function(), parameterGroupNumber, - reason); + reason, + role); } } break; @@ -882,20 +891,23 @@ namespace isobus bool retVal = false; std::shared_ptr myControlFunction; std::shared_ptr partnerControlFunction; + ConnectionAbortSenderRole role; if (TransportProtocolSession::Direction::Transmit == session->get_direction()) { myControlFunction = std::static_pointer_cast(session->get_source()); partnerControlFunction = session->get_destination(); + role = ConnectionAbortSenderRole::Originator; } else { myControlFunction = std::static_pointer_cast(session->get_destination()); partnerControlFunction = session->get_source(); + role = ConnectionAbortSenderRole::Receiver; } if ((nullptr != myControlFunction) && (nullptr != partnerControlFunction)) { - retVal = send_abort(myControlFunction, partnerControlFunction, session->get_parameter_group_number(), reason); + retVal = send_abort(myControlFunction, partnerControlFunction, session->get_parameter_group_number(), reason, role); } close_session(session, false); return retVal; @@ -904,12 +916,14 @@ namespace isobus bool TransportProtocolManager::send_abort(std::shared_ptr sender, std::shared_ptr receiver, std::uint32_t parameterGroupNumber, - ConnectionAbortReason reason) const + ConnectionAbortReason reason, + ConnectionAbortSenderRole role) const { + // ISO 11783-3:2026 5.9.4.5: byte 3 holds the role in bits 1 to 2, and the reserved bits 3 to 8 are sent as 1 const std::array buffer{ CONNECTION_ABORT_MULTIPLEXOR, static_cast(reason), - 0xFF, + static_cast(0xFC | static_cast(role)), 0xFF, 0xFF, static_cast(parameterGroupNumber & 0xFF), diff --git a/test/transport_protocol_tests.cpp b/test/transport_protocol_tests.cpp index 92287f697..4d2148bfc 100644 --- a/test/transport_protocol_tests.cpp +++ b/test/transport_protocol_tests.cpp @@ -909,13 +909,13 @@ TEST_F(TransportProtocolTest, DestinationSpecificMessageReceiving) ASSERT_FALSE(manager.has_session(originator, receiver)); } -void check_abort_message(const CANMessage &message, const std::uint8_t abortReason, const std::uint32_t parameterGroupNumber) +void check_abort_message(const CANMessage &message, const std::uint8_t abortReason, const std::uint32_t parameterGroupNumber, const std::uint8_t roleOfSenderByte) { ASSERT_EQ(message.get_identifier().get_parameter_group_number(), 0xEC00); // Transport Protocol Connection Management ASSERT_EQ(message.get_data_length(), 8); ASSERT_EQ(message.get_data()[0], 255); // Abort control byte ASSERT_EQ(message.get_data()[1], abortReason); - ASSERT_EQ(message.get_data()[2], 0xFF); // Reserved + ASSERT_EQ(message.get_data()[2], roleOfSenderByte); // Role of Sender, with the reserved bits set ASSERT_EQ(message.get_data()[3], 0xFF); // Reserved ASSERT_EQ(message.get_data()[4], 0xFF); // Reserved ASSERT_EQ(message.get_data()[5], parameterGroupNumber & 0xFF); @@ -1019,7 +1019,7 @@ TEST_F(TransportProtocolTest, DestinationSpecificTimeoutInitiation) // For the receiver side, a connection is established as soon as the CTS is sent, hence we do expect an abort message to be sent ASSERT_EQ(receiverQueue.size(), 1); - check_abort_message(receiverQueue.front(), 3, 0xFEEB); // Abort reason 3: Connection timeout + check_abort_message(receiverQueue.front(), 3, 0xFEEB, 0xFD); // Abort reason 3: Connection timeout, sent by the receiver receiverQueue.pop_front(); // Check for correct timeouts, and session removal @@ -1140,7 +1140,8 @@ TEST_F(TransportProtocolTest, DestinationSpecificTimeoutCompletion) // For both sides, a connection should've been established, hence we expect an abort message to be sent from both the originator and receiver ASSERT_EQ(originatorQueue.size(), 1); ASSERT_EQ(receiverQueue.size(), 1); - check_abort_message(receiverQueue.front(), 3, 0xFEEB); // Abort reason 3: Connection timeout + check_abort_message(originatorQueue.front(), 3, 0xFEEB, 0xFC); // Abort reason 3: Connection timeout, sent by the originator + check_abort_message(receiverQueue.front(), 3, 0xFEEB, 0xFD); // Abort reason 3: Connection timeout, sent by the receiver originatorQueue.pop_front(); receiverQueue.pop_front(); @@ -1699,7 +1700,7 @@ TEST_F(TransportProtocolTest, DestinationSpecificMultipleCTS) EXPECT_EQ(parameterGroupNumber, 0xEC00); EXPECT_EQ(data[0], 255); // Abort control byte EXPECT_EQ(data[1], 4); // Abort reason 4: Unexpected CTS - EXPECT_EQ(data[2], 0xFF); // Reserved + EXPECT_EQ(data[2], 0xFC); // Role of Sender: the originator EXPECT_EQ(data[3], 0xFF); // Reserved EXPECT_EQ(data[4], 0xFF); // Reserved EXPECT_EQ(data[5], 0xEB); // PGN LSB @@ -1891,7 +1892,7 @@ TEST_F(TransportProtocolTest, DestinationSpecificRejectForOutOfResources) EXPECT_EQ(parameterGroupNumber, 0xEC00); EXPECT_EQ(data[0], 255); // Abort control byte EXPECT_EQ(data[1], 1); // Abort reason 1: Cannot support another connection - EXPECT_EQ(data[2], 0xFF); // Reserved + EXPECT_EQ(data[2], 0xFD); // Role of Sender: the receiver EXPECT_EQ(data[3], 0xFF); // Reserved EXPECT_EQ(data[4], 0xFF); // Reserved EXPECT_EQ(data[5], 0xEB); // PGN LSB @@ -2254,6 +2255,7 @@ TEST_F(TransportProtocolTest, RequestToSendWithInvalidSizeIsAborted) EXPECT_EQ(parameterGroupNumber, 0xEC00); EXPECT_EQ(destinationControlFunction, originator); EXPECT_EQ(data[0], 255); // Abort control byte + EXPECT_EQ(data[2], 0xFD); // Role of Sender: the receiver rejects the RTS abortReasons.push_back(data[1]); return true; }; @@ -2303,3 +2305,49 @@ TEST_F(TransportProtocolTest, BroadcastWithInvalidSizeIsIgnored) EXPECT_FALSE(manager.has_session(originator, nullptr)) << size; } } + +// ISO 11783-3:2026 5.9.4.5: the Role of Sender picks one of two sessions of the same PGN in opposite directions +TEST_F(TransportProtocolTest, AbortClosesOnlyTheSessionOfItsRoleOfSender) +{ + constexpr std::array dataToSend = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; + constexpr std::uint32_t PGN = 0xFEEB; + + auto internal = test_helpers::create_mock_internal_control_function(0x0B); + auto partner = test_helpers::create_mock_control_function(0x01); + + auto sendFrameCallback = [](std::uint32_t, CANDataSpan, std::shared_ptr, std::shared_ptr, CANIdentifier::CANPriority) { + return true; + }; + + struct Case + { + std::uint8_t roleOfSenderByte; + bool txSessionRemains; + bool rxSessionRemains; + }; + const std::vector cases = { + { 0xFC, true, false }, // The partner aborts as the originator, so only the session it sends to us + { 0xFD, false, true }, // The partner aborts as the receiver, so only the session we send to it + { 0xFE, false, false }, // Reserved, closes both + { 0xFF, false, false } // Not specified, as an earlier edition sends it, closes both + }; + + for (const auto &testCase : cases) + { + CANNetworkConfiguration configuration; + TransportProtocolManager manager(sendFrameCallback, nullptr, &configuration); + + // We send the PGN to the partner + auto data = std::unique_ptr(new CANMessageDataView(dataToSend.data(), dataToSend.size())); + ASSERT_TRUE(manager.protocol_transmit_message(PGN, data, internal, partner, nullptr, nullptr)); + + // And the partner sends the same PGN to us + manager.process_message(test_helpers::create_message(7, 0xEC00, internal, partner, { 16, 9, 0, 2, 16, 0xEB, 0xFE, 0x00 })); + ASSERT_TRUE(manager.has_session(internal, partner)); + ASSERT_TRUE(manager.has_session(partner, internal)); + + manager.process_message(test_helpers::create_message(7, 0xEC00, internal, partner, { 255, 1, testCase.roleOfSenderByte, 0xFF, 0xFF, 0xEB, 0xFE, 0x00 })); + EXPECT_EQ(testCase.txSessionRemains, manager.has_session(internal, partner)) << static_cast(testCase.roleOfSenderByte); + EXPECT_EQ(testCase.rxSessionRemains, manager.has_session(partner, internal)) << static_cast(testCase.roleOfSenderByte); + } +}