OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
RawCardData.cs
1using System;
2using System.Collections;
3using System.Linq;
4using System.Text;
7
9
14{
22 public RawCardData(byte readerNumber, FormatCode format, BitArray data)
23 {
24 ReaderNumber = readerNumber;
25 FormatCode = format;
26 Data = data;
27 BitCount = (ushort)data.Count;
28 }
29
33 public byte ReaderNumber { get; }
34
38 public FormatCode FormatCode { get; }
39
43 public ushort BitCount { get; }
44
48 public BitArray Data { get; }
49
51 public override byte Code => (byte)ReplyType.RawReaderData;
52
54 public override ReadOnlySpan<byte> SecurityControlBlock()
55 {
56 return SecurityBlock.ReplyMessageWithDataSecurity;
57 }
58
65 public static RawCardData ParseData(ReadOnlySpan<byte> data)
66 {
67 var dataArray = data.ToArray();
68 if (dataArray.Length < 4)
69 {
70 throw new Exception("Invalid size for the data");
71 }
72
73 ushort bitCount = Message.ConvertBytesToUnsignedShort(new[] {dataArray[2], dataArray[3]});
74 var cardData = new BitArray(dataArray.Skip(4).Take(dataArray.Length - 4).Reverse().ToArray());
75 Reverse(cardData);
76 cardData.Length = bitCount;
77
78 return new RawCardData(dataArray[0], (FormatCode)dataArray[1], cardData);
79 }
80
82 public override string ToString()
83 {
84 var build = new StringBuilder();
85 build.AppendLine($"Reader Number: {ReaderNumber}");
86 build.AppendLine($" Format Code: {Helpers.SplitCamelCase(FormatCode.ToString())}");
87 build.AppendLine($" Bit Count: {BitCount}");
88 build.AppendLine($" Binary Data: {FormatData(Data)}");
89 build.AppendLine($" Hex Data: {FormatHexData(Data)}");
90 return build.ToString();
91 }
92
93 internal static string FormatData(BitArray bitArray)
94 {
95 var builder = new StringBuilder();
96 foreach (bool bit in bitArray)
97 {
98 builder.Append(bit ? "1" : "0");
99 }
100
101 return builder.ToString();
102 }
103
108 internal static string FormatHexData(BitArray bitArray)
109 {
110 var bytes = new byte[(bitArray.Length + 7) / 8];
111 for (int index = 0; index < bitArray.Length; index++)
112 {
113 if (bitArray[index])
114 {
115 bytes[index / 8] |= (byte)(0x80 >> (index % 8));
116 }
117 }
118
119 return BitConverter.ToString(bytes).Replace("-", string.Empty);
120 }
121
122 private static void Reverse(BitArray array)
123 {
124 int length = array.Length;
125 int mid = length / 2;
126
127 for (int index = 0; index < mid; index++)
128 {
129 (array[index], array[length - index - 1]) = (array[length - index - 1], array[index]);
130 }
131 }
132
134 public override byte[] BuildData()
135 {
136 var length = 4 + (Data.Count + 7) / 8;
137 var buffer = new byte[length];
138 buffer[0] = ReaderNumber;
139 buffer[1] = (byte)FormatCode;
140 buffer[2] = (byte)(BitCount & 0xff);
141 buffer[3] = (byte)((BitCount >> 8) & 0xff);
142 Data.CopyTo(buffer, 4);
143
144 for (int i = 4; i < length; i++)
145 {
146 // Source of this nifty little bitwise logic to reverse the bits:
147 // http://igoro.com/archive/programming-job-interview-challenge/
148
149 byte b = buffer[i];
150 int c = (byte)((b >> 4) | ((b & 0xf) << 4));
151 c = ((c & 0xcc) >> 2) | ((c & 0x33) << 2);
152 c = ((c & 0xaa) >> 1) | ((c & 0x55) << 1);
153 buffer[i] = (byte)c;
154 }
155
156 return buffer;
157 }
158}
159
163public enum FormatCode
164{
173}
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 an OSDP message.
Definition PayloadData.cs:9
RawCardData(byte readerNumber, FormatCode format, BitArray data)
Creates a new instance of RawCardData.
FormatCode FormatCode
The card format code.
BitArray Data
The raw card data.
ushort BitCount
The bit count returned by the card.
byte ReaderNumber
The reader number.
override ReadOnlySpan< byte > SecurityControlBlock()
static RawCardData ParseData(ReadOnlySpan< byte > data)
Parses the data.
ReplyType
Identifies the type of OSDP protocol reply (PD -> ACU) message.
Definition ReplyType.cs:7
FormatCode
The raw card format code.
@ NotSpecified
Card format not specified.