OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
GetPIVData.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
6
8{
12 public class GetPIVData : CommandData
13 {
14 private bool _useSingleByteOffset;
15
22 [Obsolete("Single byte offset no longer supported with future versions of OSDP")]
23 public GetPIVData(ObjectId objectId, byte elementId, byte dataOffset)
24 {
25 ObjectId = objectId switch
26 {
31 _ => throw new ArgumentOutOfRangeException()
32 };
33
34 ElementId = elementId;
35 DataOffset = dataOffset;
36
37 _useSingleByteOffset = true;
38 }
39
46 public GetPIVData(byte[] objectId, byte elementId, ushort dataOffset)
47 {
48 if (objectId.Length != 3)
49 {
50 throw new ArgumentException("Object ID byte length must be 3", nameof(objectId));
51 }
52
53 ObjectId = objectId;
54 ElementId = elementId;
55 DataOffset = dataOffset;
56 }
57
61 public byte[] ObjectId { get; }
62
66 public byte ElementId { get; }
67
71 public ushort DataOffset { get; }
72
76 public static GetPIVData ParseData(ReadOnlySpan<byte> data)
77 {
78 if (data.Length < 5 || data.Length > 6)
79 {
80 throw new ArgumentException("Invalid data length, must either be 5 or 6 bytes", nameof(data));
81 }
82
83 var isSingleByteOffset = data.Length == 5;
84 ushort offset = isSingleByteOffset ? data[4] :
85 Message.ConvertBytesToUnsignedShort(data.Slice(4, 2));
86
87 return new GetPIVData(data.Slice(0, 3).ToArray(), data[3], offset)
88 {
89 _useSingleByteOffset = isSingleByteOffset
90 };
91 }
92
94 public override CommandType CommandType => CommandType.PivData;
95
97 public override byte Code => (byte)CommandType;
98
100 public override ReadOnlySpan<byte> SecurityControlBlock() => SecurityBlock.CommandMessageWithDataSecurity;
101
103 public override byte[] BuildData()
104 {
105 var data = new List<byte>();
106 data.AddRange(ObjectId);
107 data.Add(ElementId);
108
109 if (_useSingleByteOffset) data.Add(Message.ConvertShortToBytes(DataOffset)[0]);
110 else data.AddRange(Message.ConvertShortToBytes(DataOffset));
111
112 return data.ToArray();
113 }
114
116 public override string ToString()
117 {
118 var sb = new StringBuilder();
119 sb.AppendLine($" Object ID: {BitConverter.ToString(ObjectId)}");
120 sb.AppendLine($" Element ID: {ElementId}");
121 sb.AppendLine($"Data Offset: {DataOffset}");
122 return sb.ToString();
123 }
124 }
125
148}
A base message class for sending or replying.
Definition Message.cs:12
Contains standard Security Control Block data that can be used when building messages.
Base class representing a payload of a PD command message.
ushort DataOffset
Gets the data offset.
Definition GetPIVData.cs:71
static GetPIVData ParseData(ReadOnlySpan< byte > data)
Parses the message payload bytes.
Definition GetPIVData.cs:76
GetPIVData(ObjectId objectId, byte elementId, byte dataOffset)
Initializes a new instance of the GetPIVData class.
Definition GetPIVData.cs:23
byte ElementId
Gets the element identifier.
Definition GetPIVData.cs:66
GetPIVData(byte[] objectId, byte elementId, ushort dataOffset)
Initializes a new instance of the GetPIVData class.
Definition GetPIVData.cs:46
byte[] ObjectId
Gets the object identifier.
Definition GetPIVData.cs:61
override ReadOnlySpan< byte > SecurityControlBlock()
@ CertificateForPIVAuthentication
The certificate for piv authentication.
@ CertificateForCardAuthentication
The certificate for card authentication.
@ CardholderUniqueIdentifier
The cardholder unique identifier.
@ CardholderFingerprintTemplate
The cardholder fingerprint template.