diff --git a/MatterDotNet/Protocol/Payloads/GeneralCode.cs b/MatterDotNet/Protocol/Payloads/GeneralCode.cs new file mode 100644 index 0000000..4aafaee --- /dev/null +++ b/MatterDotNet/Protocol/Payloads/GeneralCode.cs @@ -0,0 +1,79 @@ + +namespace MatterDotNet.Protocol.Payloads +{ + public enum GeneralCode + { + /// + /// Operation completed successfully. + /// + SUCCESS = 0, + /// + /// Generic failure, additional details may be included in the protocol specific status. + /// + FAILURE = 1, + /// + /// Operation was rejected by the system because the system is in an invalid state. + /// + BAD_PRECONDITION = 2, + /// + /// A value was out of a required range + /// + OUT_OF_RANGE = 3, + /// + /// A request was unrecognized or malformed + /// + BAD_REQUEST = 4, + /// + /// An unrecognized or unsupported request was received + /// + UNSUPPORTED = 5, + /// + /// A request was not expected at this time + /// + UNEXPECTED = 6, + /// + /// Insufficient resources to process the given request + /// + RESOURCE_EXHAUSTED = 7, + /// + /// Device is busy and cannot handle this request at this time + /// + BUSY = 8, + /// + /// A timeout occurred + /// + TIMEOUT = 9, + /// + /// Context-specific signal to proceed + /// + CONTINUE = 10, + /// + /// Failure, may be due to a concurrency error. + /// + ABORTED = 11, + /// + /// An invalid/unsupported argument was provided + /// + INVALID_ARGUMENT = 12, + /// + /// Some requested entity was not found + /// + NOT_FOUND = 13, + /// + /// The sender attempted to create something that already exists + /// + ALREADY_EXISTS = 14, + /// + /// The sender does not have sufficient permissions to execute the requested operations. + /// + PERMISSION_DENIED = 15, + /// + /// Unrecoverable data loss or corruption has occurred. + /// + DATA_LOSS = 16, + /// + /// Message size is larger than the recipient can handle. + /// + MESSAGE_TOO_LARGE = 17, + } +} diff --git a/MatterDotNet/Protocol/Payloads/StatusPayload.cs b/MatterDotNet/Protocol/Payloads/StatusPayload.cs new file mode 100644 index 0000000..8b498c2 --- /dev/null +++ b/MatterDotNet/Protocol/Payloads/StatusPayload.cs @@ -0,0 +1,52 @@ +// MatterDotNet Copyright (C) 2024 +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or any later version. +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY, without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// See the GNU Affero General Public License for more details. +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +using System.Buffers.Binary; + +namespace MatterDotNet.Protocol.Payloads +{ + public class StatusPayload : IPayload + { + /// + /// General status codes conveyed in the GeneralCode field are uniform codes that convey both success and failures. + /// + public ushort GeneralCode { get; set; } + public ushort ProtocolVendor { get; set; } + public ushort ProtocolID { get; set; } + public ushort ProtocolCode { get; set; } + + public StatusPayload(Memory data) + { + GeneralCode = BinaryPrimitives.ReadUInt16LittleEndian(data.Span); + ProtocolVendor = BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(2, 2).Span); + ProtocolID = BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(4, 2).Span); + ProtocolCode = BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(6, 2).Span); + } + + public override string ToString() + { + if (ProtocolVendor == 0) + return $"General Code: {GeneralCode}, Protocol: {(ProtocolType)ProtocolID}, Protocol Code: {ProtocolCode}"; + else + return $"General Code: {GeneralCode}, Vendor: {ProtocolVendor:X2}, Protocol: {ProtocolID:X2}, Protocol Code: {ProtocolCode}"; + } + + public bool Serialize(PayloadWriter stream) + { + stream.Write(GeneralCode); + stream.Write(ProtocolVendor); + stream.Write(ProtocolID); + stream.Write(ProtocolCode); + return true; + } + } +}