OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
Nak.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
7
9{
13 public class Nak : PayloadData
14 {
18 private Nak()
19 {
20 }
21
26 public Nak(ErrorCode errorCode)
27 {
28 // TODO: make this guy accept extra data, if/when we need to return that
29
30 ErrorCode = errorCode;
31 ExtraData = Array.Empty<byte>();
32 }
33
37 public ErrorCode ErrorCode { get; private set; }
38
42 public IEnumerable<byte> ExtraData { get; private set; }
43
45 public override byte Code => (byte)ReplyType.Nak;
46
48 public override ReadOnlySpan<byte> SecurityControlBlock()
49 {
50 return SecurityBlock.ReplyMessageWithDataSecurity;
51 }
52
59 internal static Nak ParseData(ReadOnlySpan<byte> data)
60 {
61 var dataArray = data.ToArray();
62 if (dataArray.Length < 1)
63 {
64 throw new Exception("Invalid size for the data");
65 }
66
67 var nak = new Nak
68 {
69 ErrorCode = (ErrorCode)dataArray[0],
70 ExtraData = dataArray.Skip(1).Take(dataArray.Length - 1)
71 };
72
73 return nak;
74 }
75
77 public override byte[] BuildData()
78 {
79 var buffer = new byte[1];
80 buffer[0] = (byte)ErrorCode;
81 return buffer;
82 }
83
85 public override string ToString()
86 {
87 var build = new StringBuilder();
88 build.AppendLine($"Error: {Helpers.SplitCamelCase(ErrorCode.ToString())}");
89 var extraDataArray = ExtraData.ToArray();
90 build.AppendLine($" Data: {(extraDataArray.Length > 0 ? BitConverter.ToString(extraDataArray) : "(none)")}");
91 return build.ToString();
92 }
93 }
94
145}
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
A negative reply.
Definition Nak.cs:14
override string ToString()
Definition Nak.cs:85
override ReadOnlySpan< byte > SecurityControlBlock()
Definition Nak.cs:48
override byte Code
Definition Nak.cs:45
override byte[] BuildData()
Definition Nak.cs:77
Nak(ErrorCode errorCode)
Creates a new instance of Nak class.
Definition Nak.cs:26
ErrorCode ErrorCode
Gets the error code.
Definition Nak.cs:37
IEnumerable< byte > ExtraData
Gets the extra data.
Definition Nak.cs:42
ReplyType
Identifies the type of OSDP protocol reply (PD -> ACU) message.
Definition ReplyType.cs:7
ErrorCode
Error code.
Definition Nak.cs:99
@ BioFormatNotSupported
The Bio format is not supported.
Definition Nak.cs:135
@ UnknownCommandCode
Unknown command code.
Definition Nak.cs:115
@ BioTypeNotSupported
The Bio type is not supported.
Definition Nak.cs:131
@ InvalidCommandLength
Command length error.
Definition Nak.cs:111
@ UnexpectedSequenceNumber
Unexpected sequence number detected in the header.
Definition Nak.cs:119
@ DoesNotSupportSecurityBlock
This PD does not support the security block that was received.
Definition Nak.cs:123
@ GenericError
The generic error.
Definition Nak.cs:143
@ BadChecksumOrCrc
Message check character(s) error (bad checksum/crc).
Definition Nak.cs:107
@ UnableToProcessCommand
Unable to process command record.
Definition Nak.cs:139
@ CommunicationSecurityNotMet
Encrypted communication is required to process this command.
Definition Nak.cs:127