Skip to content

Commit

Permalink
#25 #32 Working on GATT Server Notifications and Indications
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Apr 17, 2018
1 parent 9b92fa7 commit 0913aa2
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions Sources/GATTServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public final class GATTServer {
fatalError(message, line: line)
}

/// Respond to a client-initiated PDU message.
@inline(__always)
private func respond <T: ATTProtocolDataUnit> (_ response: T) {

Expand All @@ -136,6 +137,28 @@ public final class GATTServer {
else { fatalError("Could not add PDU to queue: \(response)") }
}

/// Send a server-initiated PDU message.
@inline(__always)
private func send (_ indication: ATTHandleValueIndication, response: @escaping (ATTResponse<ATTHandleValueConfirmation>) -> ()) {

log?("Indication: \(indication)")

let callback: (AnyATTResponse) -> () = { response(ATTResponse<ATTHandleValueConfirmation>($0)) }

guard let _ = connection.send(indication, response: (callback, ATTHandleValueIndication.self))
else { fatalError("Could not add PDU to queue: \(indication)") }
}

/// Send a server-initiated PDU message.
@inline(__always)
private func send (_ notification: ATTHandleValueNotification) {

log?("Notification: \(notification)")

guard let _ = connection.send(notification)
else { fatalError("Could not add PDU to queue: \(notification)") }
}

private func checkPermissions(_ permissions: BitMaskOptionSet<ATT.AttributePermission>,
_ attribute: GATTDatabase.Attribute) -> ATT.Error? {

Expand Down Expand Up @@ -236,17 +259,24 @@ public final class GATTServer {
// notify
if descriptor.configuration.contains(.notify) {

let value = attribute.value.prefix(upTo: Int(connection.maximumTransmissionUnit.rawValue))
let value = [UInt8](attribute.value.prefix(upTo: Int(connection.maximumTransmissionUnit.rawValue)))

let notification = ATTHandleValueNotification(handle: attributeHandle, value: value)

connection.send(notification)
send(notification)
}

// indicate
if descriptor.configuration.contains(.indicate) {

let value = [UInt8](attribute.value.prefix(upTo: Int(connection.maximumTransmissionUnit.rawValue)))

let indication = ATTHandleValueIndication(handle: attributeHandle, value: value)

send(indication) { [unowned self] (confirmation) in

self.log?("Confirmation: \(confirmation)")
}
}
}
}
Expand Down

0 comments on commit 0913aa2

Please sign in to comment.