From 32b4c8c81a4c15d6a3128ee381c2e5baad6add69 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 9 Aug 2023 16:26:43 +0200 Subject: [PATCH] Update API --- Sesame/API/DeviceResponse.swift | 8 ++++---- Sesame/API/MessageResult.swift | 21 ++++++++++++++++++--- Sesame/API/ServerMessage.swift | 25 ------------------------- 3 files changed, 22 insertions(+), 32 deletions(-) diff --git a/Sesame/API/DeviceResponse.swift b/Sesame/API/DeviceResponse.swift index b5157bc..0b71b81 100644 --- a/Sesame/API/DeviceResponse.swift +++ b/Sesame/API/DeviceResponse.swift @@ -27,8 +27,8 @@ struct DeviceResponse { } /// Shorthand property for an invalid message. - static var invalidMessageData: DeviceResponse { - .init(event: .invalidMessageData) + static var invalidMessageSize: DeviceResponse { + .init(event: .invalidMessageSize) } /// Shorthand property for missing body data. @@ -83,8 +83,8 @@ struct DeviceResponse { /// Get the reponse encoded in bytes. var encoded: Data { guard let message = response else { - return Data([event.rawValue]) + return event.encoded } - return Data([event.rawValue]) + message.encoded + return event.encoded + message.encoded } } diff --git a/Sesame/API/MessageResult.swift b/Sesame/API/MessageResult.swift index 7f58703..a115ca5 100644 --- a/Sesame/API/MessageResult.swift +++ b/Sesame/API/MessageResult.swift @@ -11,8 +11,8 @@ enum MessageResult: UInt8 { /// A socket event on the device was unexpected (not binary data) case unexpectedSocketEvent = 2 - /// The size of the payload (i.e. message) was invalid, or the data could not be read - case invalidMessageData = 3 + /// The size of the payload (i.e. message) was invalid + case invalidMessageSize = 3 /// The transmitted message could not be authenticated using the key case messageAuthenticationFailed = 4 @@ -44,6 +44,10 @@ enum MessageResult: UInt8 { /// The device is connected case deviceConnected = 15 + + case invalidUrlParameter = 20 + + case invalidResponseAuthentication = 21 } extension MessageResult: CustomStringConvertible { @@ -54,7 +58,7 @@ extension MessageResult: CustomStringConvertible { return "The device received unexpected text" case .unexpectedSocketEvent: return "Unexpected socket event for the device" - case .invalidMessageData: + case .invalidMessageSize: return "Invalid message data" case .messageAuthenticationFailed: return "Message authentication failed" @@ -76,6 +80,17 @@ extension MessageResult: CustomStringConvertible { return "Another operation is in progress" case .deviceConnected: return "The device is connected" + case .invalidUrlParameter: + return "The url parameter could not be found" + case .invalidResponseAuthentication: + return "The response could not be authenticated" } } } + +extension MessageResult { + + var encoded: Data { + Data([rawValue]) + } +} diff --git a/Sesame/API/ServerMessage.swift b/Sesame/API/ServerMessage.swift index 1e4dfb6..783e453 100644 --- a/Sesame/API/ServerMessage.swift +++ b/Sesame/API/ServerMessage.swift @@ -11,8 +11,6 @@ struct ServerMessage { static let authTokenSize = SHA256.byteCount - static let length = authTokenSize + Message.length - let authToken: Data let message: Message @@ -21,31 +19,8 @@ struct ServerMessage { self.authToken = authToken self.message = message } - - /** - Decode a message from a byte buffer. - The buffer must contain at least `ServerMessage.length` bytes, or it will return `nil`. - - Parameter buffer: The buffer containing the bytes. - */ - init?(decodeFrom buffer: ByteBuffer) { - guard let data = buffer.getBytes(at: 0, length: ServerMessage.length) else { - return nil - } - self.authToken = Data(data.prefix(ServerMessage.authTokenSize)) - self.message = Message(decodeFrom: Data(data.dropFirst(ServerMessage.authTokenSize))) - } var encoded: Data { authToken + message.encoded } - - static func token(from buffer: ByteBuffer) -> Data? { - guard buffer.readableBytes == authTokenSize else { - return nil - } - guard let bytes = buffer.getBytes(at: 0, length: authTokenSize) else { - return nil - } - return Data(bytes) - } }