Skip to content

Commit

Permalink
Working on LinuxPeripheral
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Jun 29, 2018
1 parent b470a1e commit b645faf
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions Sources/GATT/LinuxPeripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

public let controller: HostController

public var willRead: ((_ central: Central, _ uuid: BluetoothUUID, _ value: Data, _ offset: Int) -> ATT.Error?)?
public var willRead: ((_ central: Central, _ uuid: BluetoothUUID, _ handle: UInt16, _ value: Data, _ offset: Int) -> ATT.Error?)?

public var willWrite: ((_ central: Central, _ uuid: BluetoothUUID, _ value: Data, _ newValue: Data) -> ATT.Error?)?
public var willWrite: ((_ central: Central, _ uuid: BluetoothUUID, _ handle: UInt16, _ value: Data, _ newValue: Data) -> ATT.Error?)?

public var didWrite: ((_ central: Central, _ uuid: BluetoothUUID, _ value: Data, _ newValue: Data) -> ())?
public var didWrite: ((_ central: Central, _ uuid: BluetoothUUID, _ handle: UInt16, _ value: Data, _ newValue: Data) -> ())?

// MARK: - Private Properties

Expand Down Expand Up @@ -94,20 +94,20 @@

do {

var didWriteValues: (central: Central, uuid: BluetoothUUID, value: Data, newValue: Data)?
var didWriteValues: (central: Central, uuid: BluetoothUUID, handle: UInt16, value: Data, newValue: Data)?

server.willRead = { (uuid, handle, value, offset) in
peripheral.willRead?(Central(socket: newSocket), uuid, value, offset)
peripheral.willRead?(Central(socket: newSocket), uuid, handle, value, offset)
}

server.willWrite = { (uuid, handle, value, newValue) in

if let error = peripheral.willWrite?(Central(socket: newSocket), uuid, value, newValue) {
if let error = peripheral.willWrite?(Central(socket: newSocket), uuid, handle, value, newValue) {

return error
}

didWriteValues = (central: Central(socket: newSocket), uuid: uuid, value: value, newValue: newValue)
didWriteValues = (central: Central(socket: newSocket), uuid: uuid, handle: handle, value: value, newValue: newValue)

return nil
}
Expand All @@ -122,7 +122,7 @@

if let writtenValues = didWriteValues {

peripheral.didWrite?(writtenValues.central, writtenValues.uuid, writtenValues.value, writtenValues.newValue)
peripheral.didWrite?(writtenValues.central, writtenValues.uuid, writtenValues.handle, writtenValues.value, writtenValues.newValue)
}
}

Expand Down Expand Up @@ -173,29 +173,18 @@
database.remove(service: handle)
}

public func clear() {
public func removeAllServices() {

database.removeAll()
}

// MARK: Subscript

public subscript(characteristic uuid: BluetoothUUID) -> Data {
public subscript(characteristic handle: UInt16) -> Data {

get { return database.filter({ $0.uuid == uuid}).first!.value }
get { return database[handle: handle].value }

set {

let matchingAttributes = database.filter({ $0.uuid == uuid })

assert(matchingAttributes.count == 1, "\(matchingAttributes.count) Attributes with UUID \(uuid)")

let attribute = matchingAttributes.first!

database.write(newValue, forAttribute: attribute.handle)

//assert(self[characteristic: UUID] == newValue, "New Characteristic value \(UUID) could not be written.")
}
set { database.write(newValue, forAttribute: handle) }
}
}

Expand Down

0 comments on commit b645faf

Please sign in to comment.