Skip to content

Commit

Permalink
Add accessory examples
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Mar 1, 2024
1 parent 3e45309 commit 7f3f737
Show file tree
Hide file tree
Showing 20 changed files with 1,436 additions and 0 deletions.
10 changes: 10 additions & 0 deletions BluetoothAccessory.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
112 changes: 112 additions & 0 deletions Example/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// swift-tools-version:5.7
import PackageDescription

let bluetoothDependencies: [Target.Dependency] = [
"BluetoothAccessory",
.product(
name: "Bluetooth",
package: "Bluetooth"
),
.product(
name: "BluetoothGATT",
package: "Bluetooth",
condition: .when(platforms: [.macOS, .linux])
),
.product(
name: "BluetoothHCI",
package: "Bluetooth",
condition: .when(platforms: [.macOS, .linux])
),
.product(
name: "BluetoothGAP",
package: "Bluetooth",
condition: .when(platforms: [.macOS, .linux])
),
.product(
name: "GATT",
package: "GATT"
),
.product(
name: "DarwinGATT",
package: "GATT",
condition: .when(platforms: [.macOS])
),
.product(
name: "BluetoothLinux",
package: "BluetoothLinux",
condition: .when(platforms: [.linux])
),
.product(
name: "ArgumentParser",
package: "swift-argument-parser"
)
]

let package = Package(
name: "BluetoothAccessory-Sample",
platforms: [
.macOS(.v12)
],
products: [
.executable(
name: "BulbAccessory",
targets: ["BulbAccessory"]
),
.executable(
name: "LockAccessory",
targets: ["LockAccessory"]
),
.executable(
name: "ThermostatAccessory",
targets: ["ThermostatAccessory"]
),
.executable(
name: "SolarAccessory",
targets: ["SolarAccessory"]
)
],
dependencies: [
.package(
name: "BluetoothAccessory",
path: "../"
),
.package(
url: "https://github.com/PureSwift/Bluetooth.git",
.upToNextMajor(from: "6.0.0")
),
.package(
url: "https://github.com/PureSwift/GATT.git",
branch: "master"
),
.package(
url: "https://github.com/PureSwift/BluetoothLinux.git",
branch: "master"
),
.package(
url: "https://github.com/apple/swift-argument-parser",
from: "1.2.0"
),
],
targets: [
.executableTarget(
name: "BulbAccessory",
dependencies: bluetoothDependencies + ["BluetoothAccessoryExample"]
),
.executableTarget(
name: "LockAccessory",
dependencies: bluetoothDependencies + ["BluetoothAccessoryExample"]
),
.executableTarget(
name: "ThermostatAccessory",
dependencies: bluetoothDependencies + ["BluetoothAccessoryExample"]
),
.executableTarget(
name: "SolarAccessory",
dependencies: bluetoothDependencies + ["BluetoothAccessoryExample"]
),
.target(
name: "BluetoothAccessoryExample",
dependencies: bluetoothDependencies
)
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// AuthenticationDelegate.swift
//
//
// Created by Alsey Coleman Miller on 3/1/24.
//

import Foundation
import Bluetooth
import BluetoothAccessory

/// Accessory Authentication Delegate
public protocol AuthenticationDelegate: AnyObject {

var isConfigured: Bool { get async }

var allKeys: [KeysCharacteristic.Item] { get async }

func key(for id: UUID) async -> Key?

func newKey(for id: UUID) async -> NewKey?

func secret(for id: UUID) async -> KeyData?

func setup(_ request: SetupRequest, authenticationMessage: AuthenticationMessage) async -> Bool

func create(_ request: CreateNewKeyRequest, authenticationMessage: AuthenticationMessage) async -> Bool

func confirm(_ request: ConfirmNewKeyRequest, authenticationMessage: AuthenticationMessage) async -> Bool

func remove(_ request: RemoveKeyRequest, authenticationMessage: AuthenticationMessage) async -> Bool
}
40 changes: 40 additions & 0 deletions Example/Sources/BluetoothAccessoryExample/AuthenticationFile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// AuthenticationFile.swift
//
//
// Created by Alsey Coleman Miller on 3/1/24.
//

import Foundation
import Bluetooth
import BluetoothAccessory

/// Bluetooth Accessory Authentication File.
public struct AuthenticationFile: Equatable, Hashable, Codable, JSONFile {

public var keys: [UUID: Key]

public var newKeys: [UUID: NewKey]

public var secretData: [UUID: KeyData]

public init() {
self.keys = [:]
self.newKeys = [:]
self.secretData = [:]
}
}

public extension AuthenticationFile {

var isConfigured: Bool {
keys.contains(where: { $0.value.permission == .owner })
}

init(owner: Key, secret: KeyData) {
assert(owner.permission == .owner)
self.keys = [owner.id : owner]
self.newKeys = [:]
self.secretData = [owner.id : secret]
}
}
Loading

0 comments on commit 7f3f737

Please sign in to comment.