From 9e17af7c811e309cc6ef14de61d77e76945506f4 Mon Sep 17 00:00:00 2001 From: Konrad Breitsprecher Date: Fri, 6 Nov 2026 11:30:35 +0100 Subject: [PATCH] lin: send the diagnostic slave response (0x3D) only once A LIN slave with ID 0x3D configured as TxUnconditional answered every 0x3D header with the last data. The LIN specification defines the SlaveResp frame differently: a slave answers only if it has a response pending, and only once. For a non-dynamic slave, SetFrameResponse and UpdateTxBuffer on 0x3D now make the response pending; Init does not. The next 0x3D header sends it once. Without a pending response the slave reports LIN_RX_NO_RESPONSE to the master (trivial simulation), or stays silent if a dynamic node is present. Other IDs, masters and dynamic mode are unchanged. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Konrad Breitsprecher --- SilKit/source/services/lin/LinController.cpp | 21 +++ SilKit/source/services/lin/LinController.hpp | 10 ++ .../services/lin/SimBehaviorTrivial.cpp | 28 +++ .../services/lin/SimBehaviorTrivial.hpp | 1 + .../lin/Test_LinControllerTrivialSim.cpp | 162 ++++++++++++++++++ docs/api/services/lin.rst | 22 +++ docs/changelog/versions/latest.md | 6 + 7 files changed, 250 insertions(+) diff --git a/SilKit/source/services/lin/LinController.cpp b/SilKit/source/services/lin/LinController.cpp index b96ff82c2..8c01ccec1 100644 --- a/SilKit/source/services/lin/LinController.cpp +++ b/SilKit/source/services/lin/LinController.cpp @@ -341,6 +341,7 @@ void LinController::Init(LinControllerConfig config) node.UpdateResponses(config.frameResponses, _logger); _controllerMode = config.controllerMode; + _diagnosticResponsePending = false; _controllerStatus = LinControllerStatus::Operational; SendMsg(to_wire(config)); @@ -493,6 +494,10 @@ void LinController::UpdateTxBuffer(LinFrame frame) // Update the local payload GetThisLinNode().UpdateTxBuffer(frame.id, std::move(frame.data), _logger); + if (IsDiagnosticSlaveResponse(frame.id)) + { + _diagnosticResponsePending = true; + } // Detailed: Send LinFrameResponseUpdate with updated payload to BusSim // Trivial: Nop @@ -533,7 +538,23 @@ void LinController::SetFrameResponse(LinFrameResponse response) return; } + const bool armsDiagnosticResponse = + response.responseMode == LinFrameResponseMode::TxUnconditional && IsDiagnosticSlaveResponse(response.frame.id); UpdateFrameResponse(response); + if (armsDiagnosticResponse) + { + _diagnosticResponsePending = true; + } +} + +bool LinController::IsDiagnosticSlaveResponse(LinId linId) const +{ + return linId == DiagnosticSlaveResponseId && _controllerMode == LinControllerMode::Slave && !_useDynamicResponse; +} + +bool LinController::ConsumePendingDiagnosticResponse() +{ + return _diagnosticResponsePending.exchange(false); } void LinController::UpdateFrameResponse(LinFrameResponse response) diff --git a/SilKit/source/services/lin/LinController.hpp b/SilKit/source/services/lin/LinController.hpp index 2928a3b67..fed52aca8 100644 --- a/SilKit/source/services/lin/LinController.hpp +++ b/SilKit/source/services/lin/LinController.hpp @@ -4,6 +4,8 @@ #pragma once +#include + #include #include @@ -150,6 +152,13 @@ class LinController void ThrowOnSendAttemptWithUndefinedChecksum(const LinFrame& frame) const; void ThrowOnSendAttemptWithUndefinedDataLength(const LinFrame& frame) const; + // LIN diagnostic SlaveResp (0x3D): a slave answers only if a response is pending, and only once. + // SetFrameResponse() and UpdateTxBuffer() make a response pending, Init() does not. + static constexpr LinId DiagnosticSlaveResponseId = 0x3D; + bool IsDiagnosticSlaveResponse(LinId linId) const; + // Returns whether a diagnostic response was pending and clears it + bool ConsumePendingDiagnosticResponse(); + // for ITraceMessageSource inline auto GetTracer() -> Tracer*; @@ -231,6 +240,7 @@ class LinController std::vector _linNodes; std::vector _linIdsRespondedBySlaves{}; // Global view of LinIds with TxUnconditional configured on any node. + std::atomic _diagnosticResponsePending{false}; // see IsDiagnosticSlaveResponse() bool _triggerLinSlaveConfigurationHandlers{false}; std::chrono::nanoseconds _receptionTimeLinSlaveConfiguration{}; diff --git a/SilKit/source/services/lin/SimBehaviorTrivial.cpp b/SilKit/source/services/lin/SimBehaviorTrivial.cpp index 49629420b..8b23f9450 100644 --- a/SilKit/source/services/lin/SimBehaviorTrivial.cpp +++ b/SilKit/source/services/lin/SimBehaviorTrivial.cpp @@ -201,6 +201,14 @@ void SimBehaviorTrivial::ProcessFrameHeaderRequest(const LinSendFrameHeaderReque _parentController->ThrowOnSendAttemptWithUndefinedDataLength(response.frame); } + // LIN diagnostic SlaveResp: answer only if a response is pending, and only once + if (_parentController->IsDiagnosticSlaveResponse(header.id) + && !_parentController->ConsumePendingDiagnosticResponse()) + { + SendNoResponseOnDiagnosticHeader(response.frame); + return; + } + // Dispatch the LIN transmission to all connected nodes LinTransmission transmission{_timeProvider->Now(), response.frame, LinFrameStatus::LIN_RX_OK}; SendMsgImpl(transmission); @@ -217,6 +225,26 @@ void SimBehaviorTrivial::ProcessFrameHeaderRequest(const LinSendFrameHeaderReque LinFrameStatusEvent{transmission.timestamp, transmission.frame, LinFrameStatus::LIN_TX_OK}); } +void SimBehaviorTrivial::SendNoResponseOnDiagnosticHeader(LinFrame frame) +{ + if (_parentController->HasDynamicNode()) + { + // A dynamic node may still answer the header, stay silent as before + return; + } + + // The master only distributes the header if this node is the only responder, so it waits for this node. + // Report the missing response, as the master does if no node is configured to answer. + frame.data = {}; + LinTransmission transmission{_timeProvider->Now(), frame, LinFrameStatus::LIN_RX_NO_RESPONSE}; + SendMsgImpl(transmission); + + _parentController->GetTracer()->Trace(ToTracingDir(transmission.status), transmission.timestamp, + transmission.frame); + _parentController->CallLinFrameStatusEventHandler( + LinFrameStatusEvent{transmission.timestamp, transmission.frame, transmission.status}); +} + void SimBehaviorTrivial::UpdateTxBuffer(const LinFrame& /*frame*/) { // NOP diff --git a/SilKit/source/services/lin/SimBehaviorTrivial.hpp b/SilKit/source/services/lin/SimBehaviorTrivial.hpp index 7c3d28bd1..6432af81b 100644 --- a/SilKit/source/services/lin/SimBehaviorTrivial.hpp +++ b/SilKit/source/services/lin/SimBehaviorTrivial.hpp @@ -47,6 +47,7 @@ class SimBehaviorTrivial : public ISimBehavior void SendMsgImpl(MsgT&& msg); void SendErrorTransmissionOnHeaderRequest(int numResponses, LinFrame frame); + void SendNoResponseOnDiagnosticHeader(LinFrame frame); Core::IParticipantInternal* _participant{nullptr}; LinController* _parentController{nullptr}; diff --git a/SilKit/source/services/lin/Test_LinControllerTrivialSim.cpp b/SilKit/source/services/lin/Test_LinControllerTrivialSim.cpp index 2b0abb498..5d91a37d5 100644 --- a/SilKit/source/services/lin/Test_LinControllerTrivialSim.cpp +++ b/SilKit/source/services/lin/Test_LinControllerTrivialSim.cpp @@ -833,4 +833,166 @@ TEST_F(Test_LinControllerTrivialSim, DISABLED_send_with_tracing) master.SendFrame(frame, LinFrameResponseType::MasterResponse); } +// ------------------------------------------------------------ +// LIN diagnostic SlaveResp (0x3D): a slave answers only if a response is pending, and only once +// ------------------------------------------------------------ + +class Test_LinControllerTrivialSimDiagResponse : public Test_LinControllerTrivialSim +{ +protected: + static constexpr LinId diagId = 0x3D; + + auto MakeDiagFrame(std::array data = {}) -> LinFrame + { + return MakeFrame(diagId, LinChecksumModel::Classic, 8, data); + } + + void InitSlaveWithDiagResponse() + { + LinControllerConfig config = MakeControllerConfig(LinControllerMode::Slave); + LinFrameResponse response; + response.frame = MakeDiagFrame(); + response.responseMode = LinFrameResponseMode::TxUnconditional; + config.frameResponses.push_back(response); + slave1.Init(config); + slave1.AddFrameStatusHandler(frameStatusHandler); + } + + void ExpectResponse(const LinFrame& frame) + { + EXPECT_CALL(participant, SendMsg(&slave1, ATransmissionWith(frame, LinFrameStatus::LIN_RX_OK, 35s))).Times(1); + EXPECT_CALL(callbacks, FrameStatusHandler(&slave1, frame, LinFrameStatus::LIN_TX_OK)).Times(1); + slave1.ReceiveMsg(&master, LinSendFrameHeaderRequest{35s, diagId}); + Mock::VerifyAndClearExpectations(&participant); + Mock::VerifyAndClearExpectations(&callbacks); + } + + void ExpectNoResponse() + { + const auto noData = MakeDiagFrame(); + EXPECT_CALL(participant, SendMsg(&slave1, ATransmissionWith(noData, LinFrameStatus::LIN_RX_NO_RESPONSE, 35s))) + .Times(1); + EXPECT_CALL(callbacks, FrameStatusHandler(&slave1, noData, LinFrameStatus::LIN_RX_NO_RESPONSE)).Times(1); + slave1.ReceiveMsg(&master, LinSendFrameHeaderRequest{35s, diagId}); + Mock::VerifyAndClearExpectations(&participant); + Mock::VerifyAndClearExpectations(&callbacks); + } +}; + +TEST_F(Test_LinControllerTrivialSimDiagResponse, no_response_after_init) +{ + InitSlaveWithDiagResponse(); + ExpectNoResponse(); +} + +TEST_F(Test_LinControllerTrivialSimDiagResponse, update_tx_buffer_answers_once) +{ + InitSlaveWithDiagResponse(); + + const auto frame = MakeDiagFrame({0x01, 0x06, 0xF2, 0x7F, 0x00, 0x01, 0x00, 0x01}); + slave1.UpdateTxBuffer(frame); + + ExpectResponse(frame); + ExpectNoResponse(); +} + +TEST_F(Test_LinControllerTrivialSimDiagResponse, last_update_replaces_pending_response) +{ + InitSlaveWithDiagResponse(); + + slave1.UpdateTxBuffer(MakeDiagFrame({0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA})); + const auto frameB = MakeDiagFrame({0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB}); + slave1.UpdateTxBuffer(frameB); + + ExpectResponse(frameB); + ExpectNoResponse(); +} + +TEST_F(Test_LinControllerTrivialSimDiagResponse, update_after_response_arms_again) +{ + InitSlaveWithDiagResponse(); + + const auto frame = MakeDiagFrame({1, 2, 3, 4, 5, 6, 7, 8}); + slave1.UpdateTxBuffer(frame); + ExpectResponse(frame); + + slave1.UpdateTxBuffer(frame); + ExpectResponse(frame); + ExpectNoResponse(); +} + +TEST_F(Test_LinControllerTrivialSimDiagResponse, set_frame_response_answers_once) +{ + slave1.Init(MakeControllerConfig(LinControllerMode::Slave)); + slave1.AddFrameStatusHandler(frameStatusHandler); + + LinFrameResponse response; + response.frame = MakeDiagFrame({1, 2, 3, 4, 5, 6, 7, 8}); + response.responseMode = LinFrameResponseMode::TxUnconditional; + slave1.SetFrameResponse(response); + + ExpectResponse(response.frame); + ExpectNoResponse(); +} + +TEST_F(Test_LinControllerTrivialSimDiagResponse, silent_with_dynamic_node) +{ + InitSlaveWithDiagResponse(); + + // A dynamic node announces itself + slave1.ReceiveMsg(&slave2, ToWire(MakeControllerConfig(LinControllerMode::Slave), + WireLinControllerConfig::SimulationMode::Dynamic)); + + EXPECT_CALL(participant, SendMsg(&slave1, A())).Times(0); + EXPECT_CALL(callbacks, FrameStatusHandler(&slave1, _, _)).Times(0); + slave1.ReceiveMsg(&master, LinSendFrameHeaderRequest{35s, diagId}); +} + +TEST_F(Test_LinControllerTrivialSimDiagResponse, other_ids_answer_every_header) +{ + LinControllerConfig config = MakeControllerConfig(LinControllerMode::Slave); + LinFrameResponse response; + response.frame = MakeFrame(17, LinChecksumModel::Enhanced, 8, {1, 2, 3, 4, 5, 6, 7, 8}); + response.responseMode = LinFrameResponseMode::TxUnconditional; + config.frameResponses.push_back(response); + slave1.Init(config); + slave1.AddFrameStatusHandler(frameStatusHandler); + + EXPECT_CALL(participant, SendMsg(&slave1, ATransmissionWith(response.frame, LinFrameStatus::LIN_RX_OK, 35s))) + .Times(2); + EXPECT_CALL(callbacks, FrameStatusHandler(&slave1, response.frame, LinFrameStatus::LIN_TX_OK)).Times(2); + slave1.ReceiveMsg(&master, LinSendFrameHeaderRequest{35s, response.frame.id}); + slave1.ReceiveMsg(&master, LinSendFrameHeaderRequest{35s, response.frame.id}); +} + +TEST_F(Test_LinControllerTrivialSimDiagResponse, master_answers_every_header) +{ + // The special case applies to slaves only + LinControllerConfig config = MakeControllerConfig(LinControllerMode::Master); + LinFrameResponse response; + response.frame = MakeDiagFrame({1, 2, 3, 4, 5, 6, 7, 8}); + response.responseMode = LinFrameResponseMode::TxUnconditional; + config.frameResponses.push_back(response); + master.Init(config); + + EXPECT_CALL(participant, SendMsg(&master, ATransmissionWith(response.frame, LinFrameStatus::LIN_RX_OK, 35s))) + .Times(2); + master.ReceiveMsg(&slave1, LinSendFrameHeaderRequest{35s, diagId}); + master.ReceiveMsg(&slave1, LinSendFrameHeaderRequest{35s, diagId}); +} + +TEST_F(Test_LinControllerTrivialSimDiagResponse, master_reports_no_response_from_slave) +{ + LinControllerConfig config = MakeControllerConfig(LinControllerMode::Master); + LinFrameResponse response; + response.frame = MakeDiagFrame(); + response.responseMode = LinFrameResponseMode::Rx; + config.frameResponses.push_back(response); + master.Init(config); + master.AddFrameStatusHandler(frameStatusHandler); + + EXPECT_CALL(callbacks, FrameStatusHandler(&master, response.frame, LinFrameStatus::LIN_RX_NO_RESPONSE)).Times(1); + master.ReceiveMsg(&slave1, LinTransmission{35s, response.frame, LinFrameStatus::LIN_RX_NO_RESPONSE}); +} + } // anonymous namespace diff --git a/docs/api/services/lin.rst b/docs/api/services/lin.rst index f79c98133..8bc82e0ec 100644 --- a/docs/api/services/lin.rst +++ b/docs/api/services/lin.rst @@ -204,6 +204,28 @@ and will configure itself for reception (|LinFrameResponseMode_Rx|). // Transmit the frame header, the response will be provided by the slave. master->SendFrameHeader(0x11); +Diagnostic slave response (ID 0x3D) +___________________________________ + +For the diagnostic slave response (ID 0x3D), a LIN slave configured with |LinFrameResponseMode_Tx| answers a header +only if a response is pending, and only once, as specified by the LIN protocol. Calling |SetFrameResponse| or +|UpdateTxBuffer| for 0x3D makes a response pending, |Init| does not. A new call before the next header replaces the +pending response. If no response is pending, the slave does not answer and the header is reported as +``LIN_RX_NO_RESPONSE``:: + + // Slave: diagnostic request on 0x3C received, prepare the response + LinFrame diagResponse; + diagResponse.id = 0x3D; + diagResponse.dataLength = 8; + diagResponse.data = {0x01, 0x06, 0xF2, 0x7F, 0x00, 0x01, 0x00, 0x01}; + diagResponse.checksumModel = LinChecksumModel::Classic; + slave->UpdateTxBuffer(diagResponse); + + // Master: the first header gets the response, a second header gets LIN_RX_NO_RESPONSE + master->SendFrameHeader(0x3D); + +This applies to LIN slaves only. LIN masters and controllers in dynamic response mode answer as for any other ID. + Transmission acknowledgment ___________________________ diff --git a/docs/changelog/versions/latest.md b/docs/changelog/versions/latest.md index 2ced76608..00a86b4b7 100644 --- a/docs/changelog/versions/latest.md +++ b/docs/changelog/versions/latest.md @@ -2,6 +2,12 @@ ## Changed +- LIN: A slave answers the diagnostic slave response (ID 0x3D) only if a response is pending, and only once, + as specified by the LIN protocol. `SetFrameResponse` and `UpdateTxBuffer` make a response pending, `Init` does not. + A header without a pending response is reported as `LIN_RX_NO_RESPONSE`. Before, a slave configured with + `TxUnconditional` on 0x3D answered every header with its last data. Other LIN IDs, LIN masters and dynamic + response mode are not affected. + - `SilKitVersionMacros.h` is now committed to the source tree instead of being generated at CMake configure time. It is the single source of truth for the version numbers; `SilKitVersion.cmake` reads them from it. `SilKit/ci/bump_version.py` bumps the version and rotates the changelog. See `docs/development/release.md`.