OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
KeypadData.cs
1using System;
2using System.Text;
5
7{
11 public class KeypadData : PayloadData
12 {
13 private const int DataStartIndex = 2;
14
20 public KeypadData(byte readerNumber, string data)
21 {
22 ReaderNumber = readerNumber;
23 Data = ConvertKeypadStringToBytes(data);
24 DigitCount = (ushort)Data.Length;
25 }
26
30 public byte ReaderNumber { get; private set; }
31
35 public ushort DigitCount { get; private set; }
36
79 public byte[] Data { get; private set; }
80
82 public override byte Code => (byte)ReplyType.KeypadData;
83
85 public override ReadOnlySpan<byte> SecurityControlBlock()
86 {
87 return SecurityBlock.ReplyMessageWithDataSecurity;
88 }
89
91 public override byte[] BuildData()
92 {
93 var length = 2 + Data.Length;
94 var buffer = new byte[length];
95 buffer[0] = ReaderNumber;
96 buffer[1] = (byte)DigitCount;
97 Data.CopyTo(buffer, 2);
98
99 return buffer;
100 }
101
105 public static KeypadData ParseData(ReadOnlySpan<byte> data)
106 {
107 if (data.Length < DataStartIndex)
108 {
109 throw new Exception("Invalid size for the data");
110 }
111
112 var dataBytes = data.Slice(DataStartIndex, data.Length - DataStartIndex).ToArray();
113
114 // Convert encoded bytes to human-readable string
115 var keypadString = ConvertBytesToKeypadString(dataBytes);
116
117 return new KeypadData(data[0], keypadString);
118 }
119
121 public override string ToString()
122 {
123 var build = new StringBuilder();
124 build.AppendLine($"Reader Number: {ReaderNumber}");
125 build.AppendLine($" Digit Count: {DigitCount}");
126 build.AppendLine($" Data: {ConvertBytesToKeypadString(Data)}");
127 return build.ToString();
128 }
129
130 private static byte[] ConvertKeypadStringToBytes(string data)
131 {
132 var bytes = new byte[data.Length];
133 for (int index = 0; index < data.Length; index++)
134 {
135 bytes[index] = data[index] switch
136 {
137 '#' => 0x0D, // Enter/'#' key as ASCII return
138 '*' => 0x7F, // Clear/delete/'*' key as ASCII DELETE
139 _ => (byte)data[index] // All other characters as-is
140 };
141 }
142 return bytes;
143 }
144
145 private static string ConvertBytesToKeypadString(byte[] data)
146 {
147 var build = new StringBuilder();
148 foreach (var digit in data)
149 {
150 switch (digit)
151 {
152 case 0x0D:
153 build.Append('#');
154 break;
155 case 0x7F:
156 build.Append('*');
157 break;
158 default:
159 build.Append(Convert.ToChar(digit));
160 break;
161 }
162 }
163 return build.ToString();
164 }
165 }
166}
Contains standard Security Control Block data that can be used when building messages.
Base class representing a payload an OSDP message.
Definition PayloadData.cs:9
override ReadOnlySpan< byte > SecurityControlBlock()
Definition KeypadData.cs:85
ushort DigitCount
Number of digits in the return data.
Definition KeypadData.cs:35
static KeypadData ParseData(ReadOnlySpan< byte > data)
Parses the message payload bytes.
KeypadData(byte readerNumber, string data)
Creates a new instance of KeypadData.
Definition KeypadData.cs:20
byte ReaderNumber
Reader number 0=First Reader 1=Second Reader.
Definition KeypadData.cs:30
ReplyType
Identifies the type of OSDP protocol reply (PD -> ACU) message.
Definition ReplyType.cs:7