OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
EchoRequest.cs
1using System;
5
7{
12 public class EchoRequest : CommandData
13 {
22 public EchoRequest(byte sequenceNumber, TestPattern pattern, int payloadLength)
23 : this(sequenceNumber, pattern, LineQualityProtocol.GeneratePattern(pattern, payloadLength),
24 payloadLength)
25 {
26 }
27
28 private EchoRequest(byte sequenceNumber, TestPattern pattern, byte[] data, int declaredPayloadLength)
29 {
30 SequenceNumber = sequenceNumber;
31 Pattern = pattern;
32 Data = data ?? throw new ArgumentNullException(nameof(data));
33 DeclaredPayloadLength = declaredPayloadLength;
34 }
35
37 public byte SequenceNumber { get; }
38
40 public TestPattern Pattern { get; }
41
43 public byte[] Data { get; }
44
49 public int DeclaredPayloadLength { get; }
50
55
57 public override CommandType CommandType => CommandType.ManufacturerSpecific;
58
60 public override byte Code => (byte)CommandType;
61
63 public override ReadOnlySpan<byte> SecurityControlBlock() => SecurityBlock.CommandMessageWithDataSecurity;
64
66 public override byte[] BuildData()
67 {
68 var data = new byte[LineQualityProtocol.EchoHeaderLength + Data.Length];
69 LineQualityProtocol.VendorCodeBytes.CopyTo(data, 0);
71 data[4] = SequenceNumber;
72 data[5] = (byte)Pattern;
73 data[6] = (byte)DeclaredPayloadLength;
75 return data;
76 }
77
90 public static bool TryParse(ReadOnlySpan<byte> payload, out EchoRequest request)
91 {
92 request = null;
93
94 if (payload.Length < LineQualityProtocol.EchoHeaderLength) return false;
95 if (!LineQualityProtocol.IsLineQualityVendorCode(payload)) return false;
96 if (payload[3] != LineQualityProtocol.EchoCommandId) return false;
97
98 int available = payload.Length - LineQualityProtocol.EchoHeaderLength;
99 int declared = payload[6];
100 var data = payload.Slice(LineQualityProtocol.EchoHeaderLength,
101 Math.Min(declared, available)).ToArray();
102
103 request = new EchoRequest(payload[4], (TestPattern)payload[5], data, declared);
104 return true;
105 }
106 }
107}
byte SequenceNumber
Gets the test sequence number, echoed back by the responder.
override ReadOnlySpan< byte > SecurityControlBlock()
int DeclaredPayloadLength
Gets the payload length declared in the header, which may differ from the number of bytes actually pr...
EchoRequest(byte sequenceNumber, TestPattern pattern, int payloadLength)
Initializes an Echo Request carrying freshly generated pattern data.
static bool TryParse(ReadOnlySpan< byte > payload, out EchoRequest request)
Attempts to parse an Echo Request from an osdp_MFG payload.
TestPattern Pattern
Gets the requested test pattern.
byte[] Data
Gets the test data carried by the request.
override CommandType CommandType
bool IsLengthConsistent
Gets a value indicating whether the declared payload length matches the bytes present.
Constants and helpers for the OSDP Line Quality Test Procedure.
static bool IsLineQualityVendorCode(ReadOnlySpan< byte > candidate)
Determines whether the supplied bytes are the reserved line quality vendor code.
const int EchoHeaderLength
Number of bytes preceding the test data in an echo request or response: three vendor code bytes,...
const byte EchoCommandId
Command and reply identifier for the echo exchange.
Contains standard Security Control Block data that can be used when building messages.
TestPattern
Test pattern identifiers defined by the OSDP Line Quality Test Procedure, section 3....