OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
ExtendedIdEntry.cs
1using System;
2using System.Text;
4
6{
10 public class ExtendedIdEntry
11 {
17 public ExtendedIdEntry(ExtendedIdTag tag, string value)
18 {
19 Tag = tag;
20 Value = value ?? string.Empty;
21 }
22
28 public ExtendedIdEntry(byte tagByte, string value)
29 {
30 TagByte = tagByte;
31 Value = value ?? string.Empty;
32 }
33
38 {
39 get => (ExtendedIdTag)TagByte;
40 private set => TagByte = (byte)value;
41 }
42
46 public byte TagByte { get; private set; }
47
51 public string Value { get; }
52
56 public bool IsKnown => Enum.IsDefined(typeof(ExtendedIdTag), TagByte);
57
62 public byte[] BuildData()
63 {
64 var valueBytes = Encoding.UTF8.GetBytes(Value);
65 var length = (ushort)valueBytes.Length;
66 var result = new byte[3 + length];
67
68 result[0] = TagByte;
69 result[1] = (byte)(length & 0xFF);
70 result[2] = (byte)((length >> 8) & 0xFF);
71 Array.Copy(valueBytes, 0, result, 3, length);
72
73 return result;
74 }
75
82 public static ExtendedIdEntry ParseData(ReadOnlySpan<byte> data, out int bytesConsumed)
83 {
84 bytesConsumed = 0;
85
86 if (data.Length < 3)
87 {
88 return null;
89 }
90
91 byte tag = data[0];
92 ushort length = Message.ConvertBytesToUnsignedShort(data.Slice(1, 2));
93
94 if (data.Length < 3 + length)
95 {
96 return null;
97 }
98
99 string value = Encoding.UTF8.GetString(data.Slice(3, length).ToArray());
100 bytesConsumed = 3 + length;
101
102 return new ExtendedIdEntry(tag, value);
103 }
104
106 public override string ToString()
107 {
108 return IsKnown ? $"{Tag}: {Value}" : $"Unknown Tag {TagByte}: {Value}";
109 }
110 }
111}
A base message class for sending or replying.
Definition Message.cs:12
byte TagByte
Gets the raw tag byte value.
ExtendedIdTag Tag
Gets the tag type for this entry.
byte[] BuildData()
Builds the TLV byte array for this entry.
string Value
Gets the UTF-8 string value for this entry.
static ExtendedIdEntry ParseData(ReadOnlySpan< byte > data, out int bytesConsumed)
Parses a TLV entry from the given data span.
ExtendedIdEntry(byte tagByte, string value)
Creates a new instance of ExtendedIdEntry with a raw tag byte.
ExtendedIdEntry(ExtendedIdTag tag, string value)
Creates a new instance of ExtendedIdEntry.
bool IsKnown
Gets whether this tag is a known/defined tag in the OSDP specification.
ExtendedIdTag
Defines the tag types used in the extended device identification TLV stream.