Skip to content

Commit

Permalink
Merge pull request #20 from PureSwift/GATTClient
Browse files Browse the repository at this point in the history
Implemented `GATTClient.discoverAllCharacteristics()`
  • Loading branch information
colemancda authored Dec 8, 2017
2 parents 4bba411 + d90f5c2 commit 5de29e2
Show file tree
Hide file tree
Showing 17 changed files with 504 additions and 184 deletions.
2 changes: 1 addition & 1 deletion Package.pins
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"package": "Bluetooth",
"reason": null,
"repositoryURL": "https://github.com/PureSwift/Bluetooth.git",
"version": "1.4.2"
"version": "1.5.0"
}
],
"version": 1
Expand Down
56 changes: 28 additions & 28 deletions Sources/BluetoothLinux/ATTConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ internal final class ATTConnection {
// Act on the received PDU based on the opcode type
switch opcode.type {

case .Response:
case .response:

try handle(response: recievedData, opcode: opcode)

case .Confirmation:
case .confirmation:

try handle(confirmation: recievedData, opcode: opcode)

case .Request:
case .request:

try handle(request: recievedData, opcode: opcode)

case .Command, .Notification, .Indication:
case .command, .notification, .indication:

// For all other opcodes notify the upper layer of the PDU and let them act on it.
try handle(notify: recievedData, opcode: opcode)
Expand Down Expand Up @@ -142,23 +142,23 @@ internal final class ATTConnection {
*/
switch opcode.type {

case .Request:
case .request:

pendingRequest = sendOperation

case .Indication:
case .indication:

pendingRequest = sendOperation

case .Response:
case .response:

// Set `incomingRequest` to false to indicate that no request is pending
incomingRequest = false

// Fall through to the next case
fallthrough

case .Command, .Notification, .Confirmation:
case .command, .notification, .confirmation:

break
}
Expand Down Expand Up @@ -225,7 +225,7 @@ internal final class ATTConnection {
// Only request and indication PDUs should have response callbacks.
switch type {

case .Request, .Indication: // Indication handles confirmation
case .request, .indication: // Indication handles confirmation

guard response != nil
else { return nil }
Expand Down Expand Up @@ -253,15 +253,15 @@ internal final class ATTConnection {
// Add the op to the correct queue based on its type
switch type {

case .Request:
case .request:

requestQueue.append(sendOpcode)

case .Indication:
case .indication:

indicationQueue.append(sendOpcode)

case .Command, .Notification, .Response, .Confirmation:
case .command, .notification, .response, .confirmation:

writeQueue.append(sendOpcode)
}
Expand Down Expand Up @@ -314,7 +314,7 @@ internal final class ATTConnection {
let requestOpcode: ATTOpcode

// Retry for error response
if opcode == .ErrorResponse {
if opcode == .errorResponse {

guard let errorResponse = ATTErrorResponse(byteValue: [UInt8](data))
else { throw Error.GarbageResponse(data) }
Expand Down Expand Up @@ -406,9 +406,9 @@ internal final class ATTConnection {
}

// If this was a request and no handler was registered for it, respond with "Not Supported"
if foundPDU == nil && opcode.type == .Request {
if foundPDU == nil && opcode.type == .request {

let errorResponse = ATTErrorResponse(requestOpcode: opcode, attributeHandle: 0x00, error: .RequestNotSupported)
let errorResponse = ATTErrorResponse(requestOpcode: opcode, attributeHandle: 0x00, error: .requestNotSupported)

let _ = send(errorResponse)
}
Expand Down Expand Up @@ -470,25 +470,25 @@ internal final class ATTConnection {
private func changeSecurity(for error: ATTError) -> Bool {

// only change if security is Auto
guard self.socket.securityLevel == .SDP
guard self.socket.securityLevel == .sdp
else { return false }

// get security from IO
var security = self.socket.securityLevel

if error == .InsufficientEncryption,
security < .Medium {
if error == .insufficientEncryption,
security < .medium {

security = .Medium
security = .medium

} else if error == .Authentication {
} else if error == .authentication {

if (security < .Medium) {
security = .Medium
} else if (security < .High) {
security = .High
} else if (security < .Fips) {
security = .Fips
if (security < .medium) {
security = .medium
} else if (security < .high) {
security = .high
} else if (security < .fips) {
security = .fips
} else {
return false
}
Expand Down Expand Up @@ -541,7 +541,7 @@ public enum ATTResponse <Value: ATTProtocolDataUnit> {

// validate types
assert(Value.self != ATTErrorResponse.self)
assert(Value.attributeOpcode.type == .Response)
assert(Value.attributeOpcode.type == .response)

switch anyResponse {
case let .error(error):
Expand Down Expand Up @@ -590,7 +590,7 @@ fileprivate final class ATTSendOperation {
guard let opcode = data.first
else { throw ATTConnectionError.GarbageResponse(data) }

if opcode == ATT.Opcode.ErrorResponse.rawValue {
if opcode == ATT.Opcode.errorResponse.rawValue {

guard let errorResponse = ATTErrorResponse(byteValue: [UInt8](data))
else { throw ATTConnectionError.GarbageResponse(data) }
Expand Down
10 changes: 5 additions & 5 deletions Sources/BluetoothLinux/AddressType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
public enum AddressType: UInt8 {

/// Bluetooth Basic Rate/Enhanced Data Rate
case BREDR = 0x00
case LowEnergyRandom = 0x01
case LowEnergyPublic = 0x02
case bredr = 0x00
case lowEnergyRandom = 0x01
case lowEnergyPublic = 0x02

public init() { self = .BREDR }
public init() { self = .bredr }

/// Whether the Bluetooth address type is LE.
public var isLowEnergy: Bool {

switch self {

case .LowEnergyPublic, .LowEnergyRandom:
case .lowEnergyPublic, .lowEnergyRandom:
return true

default:
Expand Down
16 changes: 8 additions & 8 deletions Sources/BluetoothLinux/DeviceRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ internal func HCISendRequest(_ deviceDescriptor: CInt,
newFilter.clear()
newFilter.typeMask = 16
//newFilter.setPacketType(.Event)
newFilter.setEvent(HCIGeneralEvent.CommandStatus.rawValue)
newFilter.setEvent(HCIGeneralEvent.CommandComplete.rawValue)
newFilter.setEvent(HCIGeneralEvent.LowEnergyMeta.rawValue)
newFilter.setEvent(HCIGeneralEvent.commandStatus.rawValue)
newFilter.setEvent(HCIGeneralEvent.commandComplete.rawValue)
newFilter.setEvent(HCIGeneralEvent.lowEnergyMeta.rawValue)
newFilter.setEvent(event)
//newFilter.setEvent(HCIGeneralEvent.CommandStatus.rawValue, HCIGeneralEvent.CommandComplete.rawValue, HCIGeneralEvent.LowEnergyMeta.rawValue, event)
newFilter.opcode = opcodePacked
Expand Down Expand Up @@ -304,7 +304,7 @@ internal func HCISendRequest(_ deviceDescriptor: CInt,

switch eventHeader.event {

case HCIGeneralEvent.CommandStatus.rawValue:
case HCIGeneralEvent.commandStatus.rawValue:

let parameterData = Array(eventData.prefix(min(eventData.count, HCIGeneralEvent.CommandStatusParameter.length)))

Expand All @@ -315,7 +315,7 @@ internal func HCISendRequest(_ deviceDescriptor: CInt,
guard parameter.opcode == opcodePacked else { continue }

///
guard event == HCIGeneralEvent.CommandStatus.rawValue else {
guard event == HCIGeneralEvent.commandStatus.rawValue else {

guard parameter.status == 0
else { throw restoreFilter(HCIError(rawValue: parameter.status) ?? POSIXError(code: .EIO)) }
Expand All @@ -328,7 +328,7 @@ internal func HCISendRequest(_ deviceDescriptor: CInt,
let dataLength = min(eventData.count, eventParameterLength)
return Array(eventData.suffix(dataLength))

case HCIGeneralEvent.CommandComplete.rawValue:
case HCIGeneralEvent.commandComplete.rawValue:

let parameterData = Array(eventData.prefix(min(eventData.count, HCIGeneralEvent.CommandCompleteParameter.length)))

Expand All @@ -346,7 +346,7 @@ internal func HCISendRequest(_ deviceDescriptor: CInt,
let dataLength = min(data.count, eventParameterLength)
return Array(data.suffix(dataLength))

case HCIGeneralEvent.RemoteNameRequestComplete.rawValue:
case HCIGeneralEvent.remoteNameRequestComplete.rawValue:

guard eventHeader.event == event else { break }

Expand All @@ -369,7 +369,7 @@ internal func HCISendRequest(_ deviceDescriptor: CInt,
let dataLength = min(eventData.count - 1, eventParameterLength)
return Array(eventData.suffix(dataLength))

case HCIGeneralEvent.LowEnergyMeta.rawValue:
case HCIGeneralEvent.lowEnergyMeta.rawValue:

let parameterData = eventData

Expand Down
Loading

0 comments on commit 5de29e2

Please sign in to comment.