-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathLinController.cpp
More file actions
988 lines (834 loc) · 32 KB
/
LinController.cpp
File metadata and controls
988 lines (834 loc) · 32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
// SPDX-FileCopyrightText: 2022 Vector Informatik GmbH
//
// SPDX-License-Identifier: MIT
#include "LinController.hpp"
#include <iostream>
#include <chrono>
#include "silkit/services/lin/string_utils.hpp"
#include "IServiceDiscovery.hpp"
#include "ServiceDatatypes.hpp"
#include "Tracing.hpp"
#include "LoggerMessage.hpp"
#include "WireLinMessages.hpp"
namespace {
using namespace SilKit::Services::Lin;
auto to_wire(const LinControllerConfig& config) -> WireLinControllerConfig
{
WireLinControllerConfig result{};
result.baudRate = config.baudRate;
result.controllerMode = config.controllerMode;
result.frameResponses = config.frameResponses;
return result;
}
auto to_wire(const SilKit::Experimental::Services::Lin::LinControllerDynamicConfig& config) -> WireLinControllerConfig
{
WireLinControllerConfig result{};
result.baudRate = config.baudRate;
result.controllerMode = config.controllerMode;
result.simulationMode = WireLinControllerConfig::SimulationMode::Dynamic;
return result;
}
} // namespace
namespace SilKit {
namespace Services {
namespace Lin {
LinController::LinController(Core::IParticipantInternal* participant, SilKit::Config::LinController config,
Services::Orchestration::ITimeProvider* timeProvider)
: _participant{participant}
, _config{config}
, _logger{participant->GetLogger()}
, _simulationBehavior{participant, this, timeProvider}
, _timeProvider{timeProvider}
, _replayActive{Tracing::IsValidReplayConfig(_config.replay)}
{
}
//------------------------
// Trivial or detailed
//------------------------
void LinController::RegisterServiceDiscovery()
{
_participant->GetServiceDiscovery()->RegisterServiceDiscoveryHandler(
[this](Core::Discovery::ServiceDiscoveryEvent::Type discoveryType,
const Core::ServiceDescriptor& remoteServiceDescriptor) {
// check if discovered service is a network simulator (if none is known)
if (_simulationBehavior.IsTrivial())
{
// check if received descriptor has a matching simulated link
if (discoveryType == Core::Discovery::ServiceDiscoveryEvent::Type::ServiceCreated
&& IsRelevantNetwork(remoteServiceDescriptor))
{
Logging::Info(_logger,
"Controller '{}' is using the simulated network '{}' and will route all messages to "
"the network simulator '{}'",
_config.name, remoteServiceDescriptor.GetNetworkName(),
remoteServiceDescriptor.GetParticipantName());
SetDetailedBehavior(remoteServiceDescriptor);
}
}
else
{
if (discoveryType == Core::Discovery::ServiceDiscoveryEvent::Type::ServiceRemoved
&& IsRelevantNetwork(remoteServiceDescriptor))
{
Logging::Warn(_logger,
"The network simulator for controller '{}' left the simulation. The controller is no "
"longer simulated.",
_config.name);
SetTrivialBehavior();
}
}
});
}
void LinController::SetDetailedBehavior(const Core::ServiceDescriptor& remoteServiceDescriptor)
{
_simulationBehavior.SetDetailedBehavior(remoteServiceDescriptor);
}
void LinController::SetTrivialBehavior()
{
_simulationBehavior.SetTrivialBehavior();
}
auto LinController::AllowReception(const IServiceEndpoint* from) const -> bool
{
return _simulationBehavior.AllowReception(from);
}
auto LinController::IsRelevantNetwork(const Core::ServiceDescriptor& remoteServiceDescriptor) const -> bool
{
return remoteServiceDescriptor.GetServiceType() == SilKit::Core::ServiceType::Link
&& remoteServiceDescriptor.GetNetworkName() == _serviceDescriptor.GetNetworkName();
}
template <typename MsgT>
void LinController::SendMsg(MsgT&& msg)
{
_simulationBehavior.SendMsg(std::move(msg));
}
//------------------------
// Error handling
//------------------------
void LinController::ThrowIfUninitialized(const std::string& callingMethodName) const
{
if (_controllerStatus == LinControllerStatus::Unknown)
{
std::string errorMsg = callingMethodName
+ " must only be called when the controller is initialized! Check "
"whether a call to LinController::Init is missing.";
_logger->Error(errorMsg);
throw SilKit::StateError{errorMsg};
}
}
void LinController::ThrowIfNotMaster(const std::string& callingMethodName) const
{
if (_controllerMode != LinControllerMode::Master)
{
std::string errorMsg = callingMethodName + " must only be called in master mode!";
_logger->Error(errorMsg);
throw SilKitError{errorMsg};
}
}
void LinController::ThrowIfDynamic(const std::string& callingMethodName) const
{
if (_useDynamicResponse)
{
std::string errorMsg = callingMethodName + " can not be called if the node was initialized using InitDynamic!";
_logger->Error(errorMsg);
throw SilKitError{errorMsg};
}
}
void LinController::ThrowIfNotDynamic(const std::string& callingMethodName) const
{
if (_useDynamicResponse)
{
std::string errorMsg = callingMethodName + " can only be called if the node was initialized using InitDynamic!";
_logger->Error(errorMsg);
throw SilKitError{errorMsg};
}
}
void LinController::ThrowIfNotConfiguredTxUnconditional(LinId linId)
{
if (GetThisLinNode().responses[linId].responseMode != LinFrameResponseMode::TxUnconditional)
{
std::string errorMsg = fmt::format("This node must be configured with LinFrameResponseMode::TxUnconditional to "
"update the TxBuffer for ID {}",
static_cast<uint16_t>(linId));
_logger->Error(errorMsg);
throw SilKit::ConfigurationError{errorMsg};
}
}
void LinController::WarnOnWrongDataLength(const LinFrame& receivedFrame, const LinFrame& configuredFrame) const
{
std::string errorMsg =
fmt::format("Mismatch between configured ({}) and received ({}) LinDataLength in LinFrame with ID {}",
configuredFrame.dataLength, receivedFrame.dataLength, static_cast<uint16_t>(receivedFrame.id));
_logger->Warn(errorMsg);
}
void LinController::WarnOnWrongChecksum(const LinFrame& receivedFrame, const LinFrame& configuredFrame) const
{
std::string errorMsg = fmt::format(
"Mismatch between configured ({}) and received ({}) LinChecksumModel in LinFrame with ID {}",
configuredFrame.checksumModel, receivedFrame.checksumModel, static_cast<uint16_t>(receivedFrame.id));
_logger->Warn(errorMsg);
}
void LinController::WarnOnReceptionWithInvalidDataLength(LinDataLength invalidDataLength,
const std::string& fromParticipantName,
const std::string& fromServiceName) const
{
std::string errorMsg =
fmt::format("LinController received transmission with invalid payload length {} from {{{}, {}}}. This "
"tranmission is ignored.",
static_cast<uint16_t>(invalidDataLength), fromParticipantName, fromServiceName);
_logger->Warn(errorMsg);
}
void LinController::WarnOnReceptionWithInvalidLinId(LinId invalidLinId, const std::string& fromParticipantName,
const std::string& fromServiceName) const
{
std::string errorMsg = fmt::format(
"LinController received transmission with invalid LIN ID {} from {{{}, {}}}. This transmission is ignored.",
static_cast<uint16_t>(invalidLinId), fromParticipantName, fromServiceName);
_logger->Warn(errorMsg);
}
void LinController::WarnOnReceptionWhileInactive() const
{
std::string errorMsg = fmt::format("Inactive LinController received a transmission. This transmission is ignored.");
_logger->Warn(errorMsg);
}
void LinController::WarnOnUnneededStatusChange(LinControllerStatus status) const
{
std::string errorMsg =
fmt::format("Invalid LinController status change: controller is already in {} mode.", status);
_logger->Warn(errorMsg);
}
void LinController::WarnOnInvalidLinId(LinId invalidLinId, const std::string& callingMethodName) const
{
std::string errorMsg =
fmt::format("Invalid ID={} in call to '{}'", static_cast<uint16_t>(invalidLinId), callingMethodName);
_logger->Warn(errorMsg);
}
void LinController::WarnOnUnusedResponseMode(const std::string& callingMethodName) const
{
std::string errorMsg =
fmt::format("LinFrameResponseMode::Unused is not allowed in call to '{}'.", callingMethodName);
_logger->Warn(errorMsg);
}
void LinController::WarnOnResponseModeReconfiguration(LinId id, LinFrameResponseMode currentResponseMode) const
{
std::string errorMsg =
fmt::format("Can't set response mode for ID={}. Mode is already configured to {}.", id, currentResponseMode);
_logger->Warn(errorMsg);
}
void LinController::WarnOnUnconfiguredSlaveResponse(LinId id) const
{
std::string errorMsg = fmt::format("No slave has configured a response for ID={}. Use Init() or SetFrameResponse() "
"on the slave node to configure responses.",
id);
_logger->Warn(errorMsg);
}
void LinController::WarnOnSendFrameSlaveResponseWithMasterTx(LinId id) const
{
std::string errorMsg =
fmt::format("Master has already configured a response on ID={}. Ignoring this call to SendFrame()", id);
_logger->Warn(errorMsg);
}
void LinController::ThrowOnSendAttemptWithUndefinedChecksum(const LinFrame& frame) const
{
std::string errorMsg =
fmt::format("LinFrame with ID {} has an undefined checksum model.", static_cast<uint16_t>(frame.id));
_logger->Error(errorMsg);
throw SilKit::StateError{errorMsg};
}
void LinController::ThrowOnSendAttemptWithUndefinedDataLength(const LinFrame& frame) const
{
std::string errorMsg =
fmt::format("LinFrame with ID {} has an undefined data length.", static_cast<uint16_t>(frame.id));
_logger->Error(errorMsg);
throw SilKit::StateError{errorMsg};
}
void LinController::ThrowOnErroneousInitialization() const
{
std::string errorMsg{"A LinController can't be initialized with LinControllerMode::Inactive!"};
_logger->Error(errorMsg);
throw SilKit::StateError{errorMsg};
}
void LinController::ThrowOnDuplicateInitialization() const
{
std::string errorMsg{"LinController::Init() must only be called once!"};
_logger->Error(errorMsg);
throw SilKit::StateError{errorMsg};
}
//------------------------
// Public API
//------------------------
void LinController::Init(LinControllerConfig config)
{
if (config.controllerMode == LinControllerMode::Inactive)
{
ThrowOnErroneousInitialization();
}
if (_controllerStatus != LinControllerStatus::Unknown)
{
ThrowOnDuplicateInitialization();
}
auto& node = GetThisLinNode();
node.controllerMode = config.controllerMode;
node.controllerStatus = LinControllerStatus::Operational;
node.UpdateResponses(config.frameResponses, _logger);
_controllerMode = config.controllerMode;
_controllerStatus = LinControllerStatus::Operational;
SendMsg(to_wire(config));
}
void LinController::InitDynamic(const SilKit::Experimental::Services::Lin::LinControllerDynamicConfig& config)
{
if (config.controllerMode == LinControllerMode::Inactive)
{
ThrowOnErroneousInitialization();
}
if (_controllerStatus != LinControllerStatus::Unknown)
{
ThrowOnDuplicateInitialization();
}
auto& node = GetThisLinNode();
node.controllerMode = config.controllerMode;
node.controllerStatus = LinControllerStatus::Operational;
node.simulationMode = WireLinControllerConfig::SimulationMode::Dynamic;
_controllerMode = config.controllerMode;
_controllerStatus = LinControllerStatus::Operational;
_useDynamicResponse = true;
SendMsg(to_wire(config));
}
void LinController::SendDynamicResponse(const LinFrame& frame)
{
if (!_useDynamicResponse)
{
ThrowIfNotDynamic(__FUNCTION__);
}
// prepare the response update
LinFrameResponse response{};
response.frame = frame;
response.responseMode = LinFrameResponseMode::TxUnconditional;
// distribute the response update
UpdateFrameResponse(response);
// invoke actual response
LinSendFrameHeaderRequest request{};
request.id = response.frame.id;
request.timestamp = _timeProvider->Now();
_simulationBehavior.ProcessFrameHeaderRequest(request);
}
auto LinController::Mode() const noexcept -> LinControllerMode
{
return _controllerMode;
}
auto LinController::Status() const noexcept -> LinControllerStatus
{
return _controllerStatus;
}
void LinController::SendFrameInternal(LinFrame frame, LinFrameResponseType responseType)
{
if (responseType == LinFrameResponseType::MasterResponse)
{
// Update local response reconfiguration
LinFrameResponse response{};
response.frame = frame;
response.responseMode = LinFrameResponseMode::TxUnconditional;
UpdateFrameResponse(response);
}
else
{
// Only allow SendFrame of unconfigured LIN Ids for LinFrameResponseType::MasterResponse
// that LinSlaveConfigurationHandler and GetSlaveConfiguration stays valid.
if (!HasRespondingSlave(frame.id) && !HasDynamicNode())
{
WarnOnUnconfiguredSlaveResponse(frame.id);
CallLinFrameStatusEventHandler(
LinFrameStatusEvent{_timeProvider->Now(), frame, LinFrameStatus::LIN_RX_NO_RESPONSE});
return;
}
if (responseType == LinFrameResponseType::SlaveResponse)
{
// As the master, we configure for RX in case of unconfigured SlaveResponse
auto currentResponseMode = GetThisLinNode().responses[frame.id].responseMode;
if (currentResponseMode == LinFrameResponseMode::Unused)
{
std::vector<LinFrameResponse> responseUpdate;
LinFrameResponse response{};
response.frame = frame;
response.responseMode = LinFrameResponseMode::Rx;
UpdateFrameResponse(response);
}
else if (currentResponseMode == LinFrameResponseMode::TxUnconditional)
{
WarnOnSendFrameSlaveResponseWithMasterTx(frame.id);
return;
}
}
else if (responseType == LinFrameResponseType::SlaveToSlave)
{
CallLinFrameStatusEventHandler(LinFrameStatusEvent{_timeProvider->Now(), frame, LinFrameStatus::LIN_TX_OK});
}
}
// Detailed: Send LinSendFrameRequest to BusSim
// Trivial: SendFrameHeader
SendMsg(LinSendFrameRequest{frame, responseType});
}
void LinController::SendFrame(LinFrame frame, LinFrameResponseType responseType)
{
ThrowIfUninitialized(__FUNCTION__);
ThrowIfNotMaster(__FUNCTION__);
ThrowIfDynamic(__FUNCTION__);
if (Tracing::IsReplayEnabledFor(_config.replay, Config::Replay::Direction::Send))
{
Logging::Debug(_logger, _logOnce, "LinController: Ignoring SendFrame API call due to Replay config on {}",
_config.name);
return;
}
SendFrameInternal(std::move(frame), responseType);
}
void LinController::SendFrameHeader(LinId linId)
{
ThrowIfUninitialized(__FUNCTION__);
ThrowIfNotMaster(__FUNCTION__);
// Detailed: Send LinSendFrameHeaderRequest to BusSim
// Trivial: Good case (numResponses == 1): Distribute LinSendFrameHeaderRequest, the receiving Tx-Node will generate the LinTransmission.
// Error case: Generate the LinTransmission and trigger a FrameStatusUpdate with
// LIN_RX_NO_RESPONSE (numResponses == 0) or LIN_RX_ERROR (numResponses > 1).
SendMsg(LinSendFrameHeaderRequest{_timeProvider->Now(), linId});
}
void LinController::UpdateTxBuffer(LinFrame frame)
{
ThrowIfUninitialized(__FUNCTION__);
ThrowIfDynamic(__FUNCTION__);
ThrowIfNotConfiguredTxUnconditional(frame.id);
// Update the local payload
GetThisLinNode().UpdateTxBuffer(frame.id, std::move(frame.data), _logger);
// Detailed: Send LinFrameResponseUpdate with updated payload to BusSim
// Trivial: Nop
_simulationBehavior.UpdateTxBuffer(frame);
}
void LinController::SetFrameResponse(LinFrameResponse response)
{
ThrowIfUninitialized(__FUNCTION__);
ThrowIfDynamic(__FUNCTION__);
if (response.frame.id >= _maxLinId)
{
WarnOnInvalidLinId(response.frame.id, __FUNCTION__);
return;
}
if (response.responseMode == LinFrameResponseMode::Unused)
{
WarnOnUnusedResponseMode(__FUNCTION__);
return;
}
// Don't allow reconfiguration
auto currentResponseMode = GetThisLinNode().responses[response.frame.id].responseMode;
if (currentResponseMode != LinFrameResponseMode::Unused)
{
WarnOnResponseModeReconfiguration(response.frame.id, currentResponseMode);
return;
}
if (Tracing::IsReplayEnabledFor(_config.replay, Config::Replay::Direction::Send))
{
Logging::Debug(_logger, _logOnce,
"LinController: Ignoring SetFrameResponse API call due to Replay config on {}", _config.name);
return;
}
UpdateFrameResponse(response);
}
void LinController::UpdateFrameResponse(LinFrameResponse response)
{
// Local update
GetThisLinNode().UpdateResponses({response}, _logger);
// Distribute update
LinFrameResponseUpdate responseUpdate{};
responseUpdate.frameResponses.push_back(response);
SendMsg(responseUpdate);
}
void LinController::GoToSleep()
{
ThrowIfUninitialized(__FUNCTION__);
ThrowIfNotMaster(__FUNCTION__);
if (Tracing::IsReplayEnabledFor(_config.replay, Config::Replay::Direction::Send))
{
Logging::Debug(_logger, _logOnce, "LinController: Ignoring GoToSleep API call due to Replay config on {}",
_config.name);
return;
}
// Detailed: Send LinSendFrameRequest with GoToSleep-Frame and set LinControllerStatus::SleepPending. BusSim will trigger LinTransmission.
// Trivial: Directly send LinTransmission with GoToSleep-Frame and call GoToSleepInternal() on this controller.
_simulationBehavior.GoToSleep();
_controllerStatus = LinControllerStatus::Sleep;
}
void LinController::GoToSleepInternal()
{
ThrowIfUninitialized(__FUNCTION__);
SetControllerStatusInternal(LinControllerStatus::Sleep);
}
void LinController::Wakeup()
{
ThrowIfUninitialized(__FUNCTION__);
// Detailed: Send LinWakeupPulse and call WakeupInternal()
// Trivial: Send LinWakeupPulse and call WakeupInternal(), self-deliver LinWakeupPulse with TX
_simulationBehavior.Wakeup();
}
void LinController::WakeupInternal()
{
ThrowIfUninitialized(__FUNCTION__);
SetControllerStatusInternal(LinControllerStatus::Operational);
}
Experimental::Services::Lin::LinSlaveConfiguration LinController::GetSlaveConfiguration()
{
ThrowIfNotMaster(__FUNCTION__);
return Experimental::Services::Lin::LinSlaveConfiguration{_linIdsRespondedBySlaves};
}
//------------------------
// Helpers
//------------------------
bool LinController::HasRespondingSlave(LinId id)
{
auto it = std::find(_linIdsRespondedBySlaves.begin(), _linIdsRespondedBySlaves.end(), id);
const bool result = it != _linIdsRespondedBySlaves.end();
return result;
}
bool LinController::HasDynamicNode()
{
const auto it = std::find_if(_linNodes.begin(), _linNodes.end(), [](const LinNode& node) {
return node.simulationMode == WireLinControllerConfig::SimulationMode::Dynamic;
});
const bool result = it != _linNodes.end();
return result;
}
void LinController::UpdateLinIdsRespondedBySlaves(const std::vector<LinFrameResponse>& responsesUpdate)
{
for (auto&& response : responsesUpdate)
{
if (response.responseMode == LinFrameResponseMode::TxUnconditional)
{
if (!HasRespondingSlave(response.frame.id))
{
_linIdsRespondedBySlaves.push_back(response.frame.id);
}
}
}
}
void LinController::SetControllerStatusInternal(LinControllerStatus status)
{
if (_controllerStatus == status)
{
WarnOnUnneededStatusChange(status);
}
_controllerStatus = status;
LinControllerStatusUpdate msg{};
msg.status = status;
msg.timestamp = _timeProvider->Now();
SendMsg(msg);
}
void LinController::HandleResponsesUpdate(const IServiceEndpoint* from,
const std::vector<LinFrameResponse>& responsesToUpdate)
{
auto& linNode = GetLinNode(from->GetServiceDescriptor().to_endpointAddress());
linNode.UpdateResponses(responsesToUpdate, _logger);
if (linNode.controllerMode == LinControllerMode::Slave)
{
UpdateLinIdsRespondedBySlaves(responsesToUpdate);
}
auto& callbacks = std::get<CallbacksT<Experimental::Services::Lin::LinSlaveConfigurationEvent>>(_callbacks);
auto receptionTime = _timeProvider->Now();
if (callbacks.Size() == 0)
{
// No handlers yet, but received a LinSlaveConfiguration -> trigger upon handler addition
_triggerLinSlaveConfigurationHandlers = true;
_receptionTimeLinSlaveConfiguration = receptionTime;
}
else
{
CallHandlers(Experimental::Services::Lin::LinSlaveConfigurationEvent{receptionTime});
}
}
//------------------------
// Node bookkeeping
//------------------------
void LinController::LinNode::UpdateResponses(std::vector<LinFrameResponse> responsesToUpdate,
Services::Logging::ILogger* logger)
{
for (auto&& response : responsesToUpdate)
{
auto linId = response.frame.id;
if (linId >= responses.size())
{
Logging::Warn(logger, "Ignoring LinFrameResponse update for invalid ID={}", static_cast<uint16_t>(linId));
continue;
}
responses[linId] = std::move(response);
}
}
void LinController::LinNode::UpdateTxBuffer(LinId linId, std::array<uint8_t, 8> data,
Services::Logging::ILogger* logger)
{
if (linId >= responses.size())
{
Logging::Warn(logger, "Ignoring LinFrameResponse update for invalid ID={}", static_cast<uint16_t>(linId));
return;
}
responses[linId].frame.data = data;
}
auto LinController::GetThisLinNode() -> LinNode&
{
return GetLinNode(_serviceDescriptor.to_endpointAddress());
}
auto LinController::GetLinNode(Core::EndpointAddress addr) -> LinNode&
{
auto iter = std::lower_bound(
_linNodes.begin(), _linNodes.end(), addr,
[](const LinNode& lhs, const Core::EndpointAddress& address) { return lhs.address < address; });
if (iter == _linNodes.end() || iter->address != addr)
{
LinNode node;
node.address = addr;
iter = _linNodes.insert(iter, node);
}
return *iter;
}
void LinController::CallLinFrameStatusEventHandler(const LinFrameStatusEvent& msg)
{
// Trivial: Used to dispatch the LinFrameStatusEvent locally
CallHandlers(msg);
}
auto LinController::GetResponse(LinId id) -> std::pair<int, LinFrame>
{
LinFrame responseFrame;
responseFrame.id = id;
auto numResponses = 0;
for (auto&& node : _linNodes)
{
if (node.controllerMode == LinControllerMode::Inactive)
continue;
if (node.controllerStatus != LinControllerStatus::Operational)
continue;
auto& response = node.responses[id];
if (response.responseMode == LinFrameResponseMode::TxUnconditional)
{
responseFrame = response.frame;
numResponses++;
}
}
return {numResponses, responseFrame};
}
//------------------------
// ReceiveMsg
//------------------------
void LinController::ReceiveMsg(const IServiceEndpoint* from, const LinSendFrameHeaderRequest& msg)
{
if (!AllowReception(from))
{
return;
}
if (_useDynamicResponse)
{
SilKit::Experimental::Services::Lin::LinFrameHeaderEvent headerEvent{};
headerEvent.id = msg.id;
headerEvent.timestamp = msg.timestamp;
CallHandlers(headerEvent);
return;
}
// Detailed: Depends on how LinSendFrameHeaderRequest will work with BusSim, currently NOP
// Trivial: Generate LinTransmission
// In future: Also Trigger OnHeaderCallback
_simulationBehavior.ProcessFrameHeaderRequest(msg);
}
void LinController::ReceiveMsg(const IServiceEndpoint* from, const LinTransmission& msg)
{
if (!AllowReception(from))
{
return;
}
if (_controllerMode == LinControllerMode::Inactive)
{
WarnOnReceptionWhileInactive();
return;
}
const auto& frame = msg.frame;
bool isGoToSleepFrame = frame.id == GoToSleepFrame().id && frame.data == GoToSleepFrame().data;
if (frame.dataLength != LinDataLengthUnknown && frame.dataLength > _maxDataLength)
{
WarnOnReceptionWithInvalidDataLength(frame.dataLength, from->GetServiceDescriptor().GetParticipantName(),
from->GetServiceDescriptor().GetServiceName());
return;
}
if (frame.id >= _maxLinId)
{
WarnOnReceptionWithInvalidLinId(frame.id, from->GetServiceDescriptor().GetParticipantName(),
from->GetServiceDescriptor().GetServiceName());
return;
}
_tracer.Trace(SilKit::Services::TransmitDirection::RX, msg.timestamp, frame);
// Detailed: Just use msg.status
// Trivial: Evaluate status using cached response
const LinFrameStatus msgStatus = _simulationBehavior.CalcFrameStatus(msg, isGoToSleepFrame);
if (msgStatus != LinFrameStatus::NOT_OK)
{
// Dispatch frame to handlers
CallHandlers(LinFrameStatusEvent{msg.timestamp, frame, msgStatus});
}
// Dispatch GoToSleep frames to dedicated handlers
if (isGoToSleepFrame)
{
// only call GoToSleepHandlers for slaves, i.e., not for the master that issued the GoToSleep command.
if (_controllerMode == LinControllerMode::Slave)
{
CallHandlers(LinGoToSleepEvent{msg.timestamp});
}
}
}
void LinController::ReceiveMsg(const IServiceEndpoint* from, const LinWakeupPulse& msg)
{
if (!AllowReception(from))
{
return;
}
CallHandlers(LinWakeupEvent{msg.timestamp, msg.direction});
}
void LinController::ReceiveMsg(const IServiceEndpoint* from, const LinFrameResponseUpdate& msg)
{
// Self-delivered messages are rejected
if (from->GetServiceDescriptor() == _serviceDescriptor)
return;
HandleResponsesUpdate(from, msg.frameResponses);
}
void LinController::ReceiveMsg(const IServiceEndpoint* from, const WireLinControllerConfig& msg)
{
// Self-delivered messages are rejected
if (from->GetServiceDescriptor() == _serviceDescriptor)
return;
auto& linNode = GetLinNode(from->GetServiceDescriptor().to_endpointAddress());
linNode.controllerMode = msg.controllerMode;
linNode.controllerStatus = LinControllerStatus::Operational;
linNode.simulationMode = msg.simulationMode;
HandleResponsesUpdate(from, msg.frameResponses);
}
void LinController::ReceiveMsg(const IServiceEndpoint* from, const LinControllerStatusUpdate& msg)
{
auto& linNode = GetLinNode(from->GetServiceDescriptor().to_endpointAddress());
linNode.controllerStatus = msg.status;
}
//------------------------
// Handlers
//------------------------
HandlerId LinController::AddFrameStatusHandler(FrameStatusHandler handler)
{
return AddHandler(std::move(handler));
}
void LinController::RemoveFrameStatusHandler(HandlerId handlerId)
{
if (!RemoveHandler<LinFrameStatusEvent>(handlerId))
{
_participant->GetLogger()->Warn("RemoveFrameStatusHandler failed: Unknown HandlerId.");
}
}
HandlerId LinController::AddGoToSleepHandler(GoToSleepHandler handler)
{
return AddHandler(std::move(handler));
}
void LinController::RemoveGoToSleepHandler(HandlerId handlerId)
{
if (!RemoveHandler<LinGoToSleepEvent>(handlerId))
{
_participant->GetLogger()->Warn("RemoveGoToSleepHandler failed: Unknown HandlerId.");
}
}
HandlerId LinController::AddWakeupHandler(WakeupHandler handler)
{
return AddHandler(std::move(handler));
}
auto LinController::AddFrameHeaderHandler(SilKit::Experimental::Services::Lin::LinFrameHeaderHandler handler)
-> HandlerId
{
return AddHandler(std::move(handler));
}
void LinController::RemoveWakeupHandler(HandlerId handlerId)
{
if (!RemoveHandler<LinWakeupEvent>(handlerId))
{
_participant->GetLogger()->Warn("RemoveWakeupHandler failed: Unknown HandlerId.");
}
}
void LinController::RemoveFrameHeaderHandler(HandlerId handlerId)
{
if (!RemoveHandler<SilKit::Experimental::Services::Lin::LinFrameHeaderEvent>(handlerId))
{
_participant->GetLogger()->Warn("RemoveFrameHeaderHandler failed: Unknown HandlerId.");
}
}
HandlerId LinController::AddLinSlaveConfigurationHandler(
Experimental::Services::Lin::LinSlaveConfigurationHandler handler)
{
auto handlerId = AddHandler(std::move(handler));
// Trigger handler if a LinSlaveConfigurations was received before adding a handler
// No need to cache the LinSlaveConfigs (just the reception time),
// as the user has to actively call GetSlaveConfiguration in the callback
if (_triggerLinSlaveConfigurationHandlers)
{
_triggerLinSlaveConfigurationHandlers = false;
CallHandlers(Experimental::Services::Lin::LinSlaveConfigurationEvent{_receptionTimeLinSlaveConfiguration});
}
return handlerId;
}
void LinController::RemoveLinSlaveConfigurationHandler(HandlerId handlerId)
{
if (!RemoveHandler<Experimental::Services::Lin::LinSlaveConfigurationEvent>(handlerId))
{
_participant->GetLogger()->Warn("RemoveLinSlaveConfigurationHandler failed: Unknown HandlerId.");
}
}
template <typename MsgT>
HandlerId LinController::AddHandler(CallbackT<MsgT> handler)
{
auto& callbacks = std::get<CallbacksT<MsgT>>(_callbacks);
return callbacks.Add(std::move(handler));
}
template <typename MsgT>
auto LinController::RemoveHandler(HandlerId handlerId) -> bool
{
auto& callbacks = std::get<CallbacksT<MsgT>>(_callbacks);
return callbacks.Remove(handlerId);
}
template <typename MsgT>
void LinController::CallHandlers(const MsgT& msg)
{
auto& callbacks = std::get<CallbacksT<MsgT>>(_callbacks);
callbacks.InvokeAll(this, msg);
}
// IReplayDataProvider
void LinController::ReplayMessage(const IReplayMessage* replayMessage)
{
if (!_replayActive)
{
return;
}
if (_controllerMode != LinControllerMode::Master)
{
Logging::Debug(_logger, "ReplayMessage: skipping, because controller mode is {}", _controllerMode);
return;
}
// The LinFrame Response Updates ensures that all controllers have the same notion of the
// response that is going to be generated by a slave.
auto&& frame = dynamic_cast<const LinFrame&>(*replayMessage);
const auto isSleepFrame = (frame.id == GoToSleepFrame().id && frame.data == GoToSleepFrame().data);
const auto isReceive = replayMessage->GetDirection() == TransmitDirection::RX;
LinTransmission tm{};
tm.timestamp = replayMessage->Timestamp();
tm.frame = std::move(frame);
tm.status = isReceive ? LinFrameStatus::LIN_RX_OK : LinFrameStatus::LIN_TX_OK;
// ensure slave responses are updated locally
LinFrameResponse response;
response.frame = tm.frame;
response.responseMode = isReceive ? LinFrameResponseMode::Rx : LinFrameResponseMode::TxUnconditional;
UpdateFrameResponse(response);
if (isSleepFrame)
{
_simulationBehavior.GoToSleep();
_controllerStatus = LinControllerStatus::Sleep;
return;
}
//broadcast to slaves
auto responseType = isReceive ? LinFrameResponseType::SlaveResponse : LinFrameResponseType::MasterResponse;
SendFrameInternal(tm.frame, responseType);
}
} // namespace Lin
} // namespace Services
} // namespace SilKit