Skip to content

Commit

Permalink
Update PeripheralManager.add()
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Mar 22, 2023
1 parent 73bf8e3 commit e96654a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
23 changes: 11 additions & 12 deletions Sources/DarwinGATT/DarwinPeripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public final class DarwinPeripheral: PeripheralManager {
}
}

public func add(service: GATTAttribute.Service) async throws -> UInt16 {
public func add(service: GATTAttribute.Service) async throws -> (UInt16, [UInt16]) {
let serviceObject = service.toCoreBluetooth()
// add service
try await withCheckedThrowingContinuation { [unowned self] (continuation: CheckedContinuation<(), Error>) in
Expand Down Expand Up @@ -574,24 +574,23 @@ private extension DarwinPeripheral {
return lastHandle
}

mutating func add(service: GATTAttribute.Service, _ coreService: CBMutableService) -> UInt16 {
mutating func add(service: GATTAttribute.Service, _ coreService: CBMutableService) -> (UInt16, [UInt16]) {

let serviceHandle = newHandle()

var characteristicHandles = [UInt16]()
characteristicHandles.reserveCapacity((coreService.characteristics ?? []).count)
services[coreService] = Service(handle: serviceHandle)

for (index, characteristic) in ((coreService.characteristics ?? []) as! [CBMutableCharacteristic]).enumerated() {

let data = service.characteristics[index].value

let characteristicHandle = newHandle()

characteristics[characteristic] = Characteristic(handle: characteristicHandle,
serviceHandle: serviceHandle,
value: data)
characteristics[characteristic] = Characteristic(
handle: characteristicHandle,
serviceHandle: serviceHandle,
value: data
)
characteristicHandles.append(characteristicHandle)
}

return serviceHandle
return (serviceHandle, characteristicHandles)
}

mutating func remove(service handle: UInt16) {
Expand Down
21 changes: 16 additions & 5 deletions Sources/GATT/GATTPeripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public final class GATTPeripheral <HostController: BluetoothHostControllerInterf
let isAdvertising = await self.isAdvertising
assert(isAdvertising == false)

// read address
// use public or random address
let address: BluetoothAddress
if let randomAddress = options.randomAddress {
address = randomAddress
Expand All @@ -91,7 +91,7 @@ public final class GATTPeripheral <HostController: BluetoothHostControllerInterf
address = try await hostController.readDeviceAddress()
}

// set advertising data
// set advertising data and scan response
if options.advertisingData != nil || options.scanResponse != nil {
do { try await hostController.enableLowEnergyAdvertising(false) }
catch HCIError.commandDisallowed { /* ignore */ }
Expand Down Expand Up @@ -137,7 +137,7 @@ public final class GATTPeripheral <HostController: BluetoothHostControllerInterf
log?("Stopped GATT Server")
}

public func add(service: BluetoothGATT.GATTAttribute.Service) async throws -> UInt16 {
public func add(service: BluetoothGATT.GATTAttribute.Service) async throws -> (UInt16, [UInt16]) {
return await storage.add(service: service)
}

Expand Down Expand Up @@ -294,8 +294,19 @@ internal extension GATTPeripheral {
self.task = task
}

func add(service: BluetoothGATT.GATTAttribute.Service) -> UInt16 {
return database.add(service: service)
func add(service: GATTAttribute.Service) -> (UInt16, [UInt16]) {
var includedServicesHandles = [UInt16]()
var characteristicDeclarationHandles = [UInt16]()
var characteristicValueHandles = [UInt16]()
var descriptorHandles = [[UInt16]]()
let serviceHandle = database.add(
service: service,
includedServicesHandles: &includedServicesHandles,
characteristicDeclarationHandles: &characteristicDeclarationHandles,
characteristicValueHandles: &characteristicValueHandles,
descriptorHandles: &descriptorHandles
)
return (serviceHandle, characteristicValueHandles)
}

func remove(service handle: UInt16) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/GATT/PeripheralProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public protocol PeripheralManager: AnyObject {

/// Attempts to add the specified service to the GATT database.
///
/// - Returns: Attribute handle.
func add(service: BluetoothGATT.GATTAttribute.Service) async throws -> UInt16
/// - Returns: Handle for service declaration and handles for characteristic value handles.
func add(service: BluetoothGATT.GATTAttribute.Service) async throws -> (UInt16, [UInt16])

/// Removes the service with the specified handle.
func remove(service: UInt16) async
Expand Down
13 changes: 7 additions & 6 deletions Tests/GATTTests/GATTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,10 @@ final class GATTTests: XCTestCase {

try await connect(
server: { peripheral in
serviceAttribute = try await peripheral.add(service: service)
let (serviceAttributeHandle, characteristicValueHandles) = try await peripheral.add(service: service)
serviceAttribute = serviceAttributeHandle
XCTAssertEqual(serviceAttribute, 1)
characteristicValueHandle = await peripheral.characteristics(for: .batteryLevel)[0]
characteristicValueHandle = characteristicValueHandles[0]
peripheralDatabaseValue = { await peripheral[characteristic: characteristicValueHandle] }
let currentValue = await peripheralDatabaseValue()
XCTAssertEqual(currentValue, characteristics[0].value)
Expand Down Expand Up @@ -336,9 +337,9 @@ final class GATTTests: XCTestCase {
serverOptions: .init(maximumTransmissionUnit: .default, maximumPreparedWrites: 1000),
clientOptions: .init(maximumTransmissionUnit: .max),
server: { peripheral in
let serviceAttribute = try await peripheral.add(service: service)
let (serviceAttribute, characteristicValueHandles) = try await peripheral.add(service: service)
XCTAssertEqual(serviceAttribute, 1)
let characteristicValueHandle = await peripheral.characteristics(for: .batteryLevel)[0]
let characteristicValueHandle = characteristicValueHandles[0]
Task {
try await Task.sleep(nanoseconds: 1_000_000_000)
await peripheral.write(newValue.data, forCharacteristic: characteristicValueHandle)
Expand Down Expand Up @@ -401,9 +402,9 @@ final class GATTTests: XCTestCase {
serverOptions: .init(maximumTransmissionUnit: .default, maximumPreparedWrites: 1000),
clientOptions: .init(maximumTransmissionUnit: .max),
server: { peripheral in
let serviceAttribute = try await peripheral.add(service: service)
let (serviceAttribute, characteristicValueHandles) = try await peripheral.add(service: service)
XCTAssertEqual(serviceAttribute, 1)
let characteristicValueHandle = await peripheral.characteristics(for: .batteryLevel)[0]
let characteristicValueHandle = characteristicValueHandles[0]
Task {
try await Task.sleep(nanoseconds: 1_000_000_000)
await peripheral.write(newValue.data, forCharacteristic: characteristicValueHandle)
Expand Down

0 comments on commit e96654a

Please sign in to comment.