<commit_before>//
//  ATTProtocolDataUnit.swift
//  BluetoothLinux
//
//  Created by <NAME> on 3/1/16.
//  Copyright © 2016 PureSwift. All rights reserved.
//

import struct SwiftFoundation.UUID

// MARK: - Protocol Definition

/// Data packet for the ATT protocol.
public protocol ATTProtocolDataUnit {
    
    /// The PDU's attribute opcode.
    static var attributeOpcode: ATT.Opcode { get }
    
    /// Converts PDU to raw bytes.
    var byteValue: [UInt8] { get }
    
    /// Initializes PDU from raw bytes.
    init?(byteValue: [UInt8])
}

// MARK: - Error Handling

/// The Error Response is used to state that a given request cannot be performed,
/// and to provide the reason.
///
/// - Note: The Write Command does not generate an Error Response.
public struct ATTErrorResponse: ATTProtocolDataUnit, ErrorType {
    
    /// The request that generated this error response
    public var requestOpcode: ATT.Opcode
    
    /// The attribute handle that generated this error response.
    public var attributeHandle: UInt16
    
    /// The reason why the request has generated an error response.
    public var errorCode: UInt8
    
    public init(requestOpcode: ATT.Opcode, attributeHandle: UInt16, error: ATT.Error) {
        
        self.requestOpcode = requestOpcode
        self.attributeHandle = attributeHandle
        self.errorCode = error.rawValue
    }
    
    public init(requestOpcode: ATT.Opcode, attributeHandle: UInt16, errorCode: UInt8) {
        
        self.requestOpcode = requestOpcode
        self.attributeHandle = attributeHandle
        self.errorCode = errorCode
    }
    
    // ATTProtocolDataUnit
    
    public static let attributeOpcode = ATT.Opcode.ErrorResponse
    
    public static let length = 5
    
    public init?(byteValue: [UInt8]) {
        
        guard byteValue.count == ATTErrorResponse.length else { return nil }
        
        let attributeOpcodeByte     = byteValue[0]
        let requestOpcodeByte       = byteValue[1]
        let attributeHandleByte1    = byteValue[2]
        let attributeHandleByte2    = byteValue[3]
        let errorByte               = byteValue[4]
        
        guard attributeOpcodeByte == ATTErrorResponse.attributeOpcode.rawValue,
            let requestOpcode = ATTOpcode(rawValue: requestOpcodeByte)
            else { return nil }
        
        self.requestOpcode = requestOpcode
        self.errorCode = errorByte
        self.attributeHandle = UInt16(littleEndian: (attributeHandleByte1, attributeHandleByte2))
    }
    
    public var byteValue: [UInt8] {
        
        var bytes = [UInt8](count: ATTErrorResponse.length, repeatedValue: 0)
        
        bytes[0] = ATTErrorResponse.attributeOpcode.rawValue
        bytes[1] = requestOpcode.rawValue
        bytes[2] = attributeHandle.littleEndianBytes.0
        bytes[3] = attributeHandle.littleEndianBytes.1
        bytes[4] = errorCode
        
        return bytes
    }
}

// MARK: - MTU Exchange

/// Exchange MTU Request
///
/// The *Exchange MTU Request* is used by the client to inform the server of the client’s maximum receive MTU
/// size and request the server to respond with its maximum receive MTU size.
///
/// - Note: This request shall only be sent once during a connection by the client. 
/// The *Client Rx MTU* parameter shall be set to the maximum size of the attribute protocol PDU that the client can receive.
public struct ATTMaximumTransmissionUnitRequest: ATTProtocolDataUnit {
    
    public static let attributeOpcode = ATT.Opcode.MaximumTransmissionUnitRequest
    public static let length = 3
    
    /// Client Rx MTU
    var clientMTU: UInt16
    
    public init(clientMTU: UInt16 = 0) {
        
        self.clientMTU = clientMTU
    }
    
    public init?(byteValue: [UInt8]) {
        
        guard byteValue.count == self.dynamicType.length
            else { return nil }
        
        let attributeOpcodeByte = byteValue[0]
        
        guard attributeOpcodeByte == self.dynamicType.attributeOpcode.rawValue
            else { return nil }
        
        self.clientMTU = UInt16(littleEndian: (byteValue[1], byteValue[2]))<commit_msg>fixed non-public property on ATT PDU<commit_after>//
//  ATTProtocolDataUnit.swift
//  BluetoothLinux
//
//  Created by <NAME> on 3/1/16.
//  Copyright © 2016 PureSwift. All rights reserved.
//

import struct SwiftFoundation.UUID

// MARK: - Protocol Definition

/// Data packet for the ATT protocol.
public protocol ATTProtocolDataUnit {
    
    /// The PDU's attribute opcode.
    static var attributeOpcode: ATT.Opcode { get }
    
    /// Converts PDU to raw bytes.
    var byteValue: [UInt8] { get }
    
    /// Initializes PDU from raw bytes.
    init?(byteValue: [UInt8])
}

// MARK: - Error Handling

/// The Error Response is used to state that a given request cannot be performed,
/// and to provide the reason.
///
/// - Note: The Write Command does not generate an Error Response.
public struct ATTErrorResponse: ATTProtocolDataUnit, ErrorType {
    
    /// The request that generated this error response
    public var requestOpcode: ATT.Opcode
    
    /// The attribute handle that generated this error response.
    public var attributeHandle: UInt16
    
    /// The reason why the request has generated an error response.
    public var errorCode: UInt8
    
    public init(requestOpcode: ATT.Opcode, attributeHandle: UInt16, error: ATT.Error) {
        
        self.requestOpcode = requestOpcode
        self.attributeHandle = attributeHandle
        self.errorCode = error.rawValue
    }
    
    public init(requestOpcode: ATT.Opcode, attributeHandle: UInt16, errorCode: UInt8) {
        
        self.requestOpcode = requestOpcode
        self.attributeHandle = attributeHandle
        self.errorCode = errorCode
    }
    
    // ATTProtocolDataUnit
    
    public static let attributeOpcode = ATT.Opcode.ErrorResponse
    
    public static let length = 5
    
    public init?(byteValue: [UInt8]) {
        
        guard byteValue.count == ATTErrorResponse.length else { return nil }
        
        let attributeOpcodeByte     = byteValue[0]
        let requestOpcodeByte       = byteValue[1]
        let attributeHandleByte1    = byteValue[2]
        let attributeHandleByte2    = byteValue[3]
        let errorByte               = byteValue[4]
        
        guard attributeOpcodeByte == ATTErrorResponse.attributeOpcode.rawValue,
            let requestOpcode = ATTOpcode(rawValue: requestOpcodeByte)
            else { return nil }
        
        self.requestOpcode = requestOpcode
        self.errorCode = errorByte
        self.attributeHandle = UInt16(littleEndian: (attributeHandleByte1, attributeHandleByte2))
    }
    
    public var byteValue: [UInt8] {
        
        var bytes = [UInt8](count: ATTErrorResponse.length, repeatedValue: 0)
        
        bytes[0] = ATTErrorResponse.attributeOpcode.rawValue
        bytes[1] = requestOpcode.rawValue
        bytes[2] = attributeHandle.littleEndianBytes.0
        bytes[3] = attributeHandle.littleEndianBytes.1
        bytes[4] = errorCode
        
        return bytes
    }
}

// MARK: - MTU Exchange

/// Exchange MTU Request
///
/// The *Exchange MTU Request* is used by the client to inform the server of the client’s maximum receive MTU
/// size and request the server to respond with its maximum receive MTU size.
///
/// - Note: This request shall only be sent once during a connection by the client. 
/// The *Client Rx MTU* parameter shall be set to the maximum size of the attribute protocol PDU that the client can receive.
public struct ATTMaximumTransmissionUnitRequest: ATTProtocolDataUnit {
    
    public static let attributeOpcode = ATT.Opcode.MaximumTransmissionUnitRequest
    public static let length = 3
    
    /// Client Rx MTU
    public var clientMTU: UInt16
    
    public init(clientMTU: UInt16 = 0) {
        
        self.clientMTU = clientMTU
    }
    
    public init?(byteValue: [UInt8]) {
        
        guard byteValue.count == self.dynamicType.length
            else { return nil }
        
        let attributeOpcodeByte = byteValue[0]
        
        guard attributeOpcodeByte == self.dynamicType.attributeOpcode.rawValue
            else { return nil }
        
        self.clientMTU = UInt16(littleEndian: (byteValue[1], byteValue[2]))
{'fraction_non_alphanumeric': 0.07730105465004794, 'fraction_numerical': 0.012703739213806327, 'mean_word_length': 2.375809061488673, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4497293', 'n_tokens_mistral': 2311, 'n_tokens_neox': 2265, 'n_words': 711}