OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
ExtendedWrite.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
6
8{
18 {
26 public ExtendedWrite(byte mode, byte pCommand, byte[] pData)
27 {
28 Mode = mode;
29 PCommand = pCommand;
30 PData = pData ?? throw new ArgumentNullException(nameof(pData));
31 }
32
36 public byte Mode { get; }
37
41 public byte PCommand { get; }
42
46 public byte[] PData { get; }
47
49 public override CommandType CommandType => CommandType.ExtendedWrite;
50
52 public override byte Code => (byte)CommandType;
53
55 public override ReadOnlySpan<byte> SecurityControlBlock() => SecurityBlock.CommandMessageWithDataSecurity;
56
58 public override byte[] BuildData()
59 {
60 var data = new List<byte>(2 + PData.Length) { Mode, PCommand };
61 data.AddRange(PData);
62 return data.ToArray();
63 }
64
72 public static ExtendedWrite ParseData(ReadOnlySpan<byte> data)
73 {
74 byte mode = data.Length > 0 ? data[0] : (byte)0;
75 byte pCommand = data.Length > 1 ? data[1] : (byte)0;
76 byte[] pData = data.Length > 2 ? data.Slice(2).ToArray() : [];
77 return new ExtendedWrite(mode, pCommand, pData);
78 }
79
83 public static ExtendedWrite ReadModeSetting() =>
84 new(0, 1, []);
85
90 public static ExtendedWrite ModeZeroConfiguration(bool enabled) =>
91 new(0, 2, [0, (byte)(enabled ? 1 : 0)]);
92
97 new(0, 2, [1, 0]);
98
105 public static ExtendedWrite ModeOnePassAPDUCommand(byte readerNumber, byte[] command)
106 {
107 if (command == null) throw new ArgumentNullException(nameof(command));
108
109 var data = new byte[command.Length + 1];
110 data[0] = readerNumber;
111 Array.Copy(command, 0, data, 1, command.Length);
112 return new ExtendedWrite(1, 1, data);
113 }
114
119 public static ExtendedWrite ModeOneTerminateSmartCardConnection(byte readerNumber) =>
120 new(1, 2, [readerNumber]);
121
126 public static ExtendedWrite ModeOneSmartCardScan(byte readerNumber) =>
127 new(1, 4, [readerNumber]);
128
130 public override string ToString()
131 {
132 var sb = new StringBuilder();
133 sb.AppendLine($" Mode: {Mode}");
134 sb.AppendLine($"PCommand: {PCommand}");
135 sb.AppendLine($" PData: {BitConverter.ToString(PData)}");
136 return sb.ToString();
137 }
138 }
139}
Contains standard Security Control Block data that can be used when building messages.
Base class representing a payload of a PD command message.
byte[] PData
Gets the command-specific data.
byte PCommand
Gets the mode-specific command code.
static ExtendedWrite ModeZeroConfiguration(bool enabled)
Configures Mode 0 (enable or disable transparent mode).
override ReadOnlySpan< byte > SecurityControlBlock()
byte Mode
Gets the extended READ/WRITE mode.
static ExtendedWrite ModeOneConfiguration()
Switches the PD to Mode 1 (transparent APDU).
static ExtendedWrite ModeOneTerminateSmartCardConnection(byte readerNumber)
Instructs the PD to terminate the smart card session on the specified reader.
static ExtendedWrite ReadModeSetting()
Queries the PD for its current extended mode setting.
static ExtendedWrite ModeOnePassAPDUCommand(byte readerNumber, byte[] command)
Passes a raw APDU to the smart card through the specified reader.
static ExtendedWrite ModeOneSmartCardScan(byte readerNumber)
Instructs the PD to scan for a smart card on the specified reader.
static ExtendedWrite ParseData(ReadOnlySpan< byte > data)
Parses the message payload bytes.
ExtendedWrite(byte mode, byte pCommand, byte[] pData)
Initializes a new instance of the ExtendedWrite class.