Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions SilKit/source/services/lin/LinController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions SilKit/source/services/lin/LinController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#pragma once

#include <atomic>

#include <map>
#include <set>

Expand Down Expand Up @@ -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*;

Expand Down Expand Up @@ -231,6 +240,7 @@ class LinController

std::vector<LinNode> _linNodes;
std::vector<LinId> _linIdsRespondedBySlaves{}; // Global view of LinIds with TxUnconditional configured on any node.
std::atomic<bool> _diagnosticResponsePending{false}; // see IsDiagnosticSlaveResponse()
bool _triggerLinSlaveConfigurationHandlers{false};
std::chrono::nanoseconds _receptionTimeLinSlaveConfiguration{};

Expand Down
28 changes: 28 additions & 0 deletions SilKit/source/services/lin/SimBehaviorTrivial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions SilKit/source/services/lin/SimBehaviorTrivial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
162 changes: 162 additions & 0 deletions SilKit/source/services/lin/Test_LinControllerTrivialSim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t, 8> 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<const LinTransmission&>())).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
22 changes: 22 additions & 0 deletions docs/api/services/lin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
___________________________

Expand Down
6 changes: 6 additions & 0 deletions docs/changelog/versions/latest.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
Loading