1 //
2 // Copyright (c) 2010-2024 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 
8 using System;
9 using System.Collections.Generic;
10 
11 using Antmicro.Renode.Core;
12 using Antmicro.Renode.Exceptions;
13 using Antmicro.Renode.Logging;
14 using Antmicro.Renode.Peripherals.UART;
15 using Antmicro.Renode.Utilities;
16 
17 namespace Antmicro.Renode.Peripherals.LIN
18 {
19     public class DummyLINPeripheral : LINBase
20     {
DummyLINPeripheral(IMachine machine, byte protectedId, int frameLength = 2)21         public DummyLINPeripheral(IMachine machine, byte protectedId, int frameLength = 2) : base(machine)
22         {
23             linDecoder.ValidateProtectedIdentifier = true;
24             try
25             {
26                 linEntry = RegisterProtectedIdentifier(protectedId, LINMode.Sink, frameLength);
27             }
28             catch(ArgumentException)
29             {
30                 throw new ConstructionException($"{protectedId} is not valid protected identifier");
31             }
32 
33             ProtectedIdentifier = protectedId;
34             internalData = new Queue<byte>();
35         }
36 
EnqueueByte(byte value)37         public void EnqueueByte(byte value)
38         {
39             internalData.Enqueue(value);
40         }
41 
ClearTransmittingData()42         public void ClearTransmittingData()
43         {
44             internalData.Clear();
45         }
46 
FrameReceived(byte privateIdentifier, byte[] data, bool valid)47         public override void FrameReceived(byte privateIdentifier, byte[] data, bool valid)
48         {
49             this.Log(LogLevel.Info, "Received frame (valid={0}): {1}", valid, Misc.PrettyPrintCollectionHex(data));
50         }
51 
StartedTransmission(byte privateIdentifier)52         public override void StartedTransmission(byte privateIdentifier)
53         {
54             linDecoder.TransmitAndFinalize(internalData.ToArray(), TransmitCharacter);
55         }
56 
57         public byte ProtectedIdentifier { get; }
58 
59         public LINMode Mode
60         {
61             get => linEntry.Mode;
62             set => linEntry.Mode = value;
63         }
64 
65         public bool ExtendedFrameChecksum
66         {
67             get => linEntry.ExtendedChecksum;
68             set => linEntry.ExtendedChecksum = value;
69         }
70 
71         public bool ValidateFrame
72         {
73             get => linEntry.ValidateFrame;
74             set => linEntry.ValidateFrame = value;
75         }
76 
77         public int FrameLength
78         {
79             get => linEntry.FrameLength;
80             set => linEntry.FrameLength = value;
81         }
82 
83         public override Bits StopBits => Bits.None;
84         public override Parity ParityBit => Parity.None;
85         public override uint BaudRate => 19200;
86 
CharWritten()87         protected override void CharWritten()
88         {
89         }
90 
QueueEmptied()91         protected override void QueueEmptied()
92         {
93         }
94 
95         private readonly Queue<byte> internalData;
96         private readonly ILINEntry linEntry;
97     }
98 }
99