From e459f763ed8b213ee2ee30ab4c1acd5d4d11c14f Mon Sep 17 00:00:00 2001 From: Olivier Martin Date: Sat, 18 May 2024 23:44:23 +0200 Subject: [PATCH] Initial Swift support --- .gitignore | 4 + Package.swift | 23 ++++++ swift/Gattlib/Gattlib.swift | 125 ++++++++++++++++++++++++++++++++ swift/GattlibExample/main.swift | 1 + 4 files changed, 153 insertions(+) create mode 100644 Package.swift create mode 100644 swift/Gattlib/Gattlib.swift create mode 100644 swift/GattlibExample/main.swift diff --git a/.gitignore b/.gitignore index e67b1b9..898f0bc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,12 @@ .vscode/ # Generated files +.build build __pycache__ MANIFEST.in dist gattlib-py/gattlib_py.egg-info/ + +# Python +/venv* \ No newline at end of file diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..f1b1bee --- /dev/null +++ b/Package.swift @@ -0,0 +1,23 @@ +// swift-tools-version: 5.10 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "Gattlib", + products: [ + // Products define the executables and libraries a package produces, making them visible to other packages. + .library( + name: "Gattlib", + targets: ["Gattlib"]), + ], + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. + .target(name: "Gattlib", path: "swift/Gattlib"), + .executableTarget( + name: "GattlibExample", + dependencies: [ "Gattlib" ], + path: "swift/GattlibExample"), + ] +) diff --git a/swift/Gattlib/Gattlib.swift b/swift/Gattlib/Gattlib.swift new file mode 100644 index 0000000..fc8e2d3 --- /dev/null +++ b/swift/Gattlib/Gattlib.swift @@ -0,0 +1,125 @@ +import Foundation + +public typealias dispatch_queue_t = Any + +public let CBCentralManagerScanOptionAllowDuplicatesKey = "CBCentralManagerScanOptionAllowDuplicatesKey" +public let CBAdvertisementDataLocalNameKey = "CBAdvertisementDataLocalNameKey" + +public enum CBCharacteristicWriteType { + case withResponse + case withoutResponse +} + +public class CBUUID : Equatable { + let uuid: UUID? + + public init(string: String) { + self.uuid = UUID(uuidString: string) + } + + public static func == (left: CBUUID, right: CBUUID) -> Bool { + return left.uuid == right.uuid + } +} + +public class CBManager { + public enum CBManagerState { + case poweredOn + case poweredOff + case unsupported + case resetting + case unauthorized + case unknown + } + public var state: CBManagerState + + init(state: CBManagerState) { + self.state = state + } +} + +public class CBPeer : Equatable { + public var identifier: UUID + + init(identifier: UUID) { + self.identifier = identifier + } + + public static func == (left: CBPeer, right: CBPeer) -> Bool { + return left.identifier == right.identifier + } +} + +public class CBAttribute { + public var uuid: CBUUID + + init(uuid: CBUUID) { + self.uuid = uuid + } +} + +public class CBMutableService { + +} + +public class CBCentralManager : CBManager { + init(delegate: (any CBCentralManagerDelegate)?, queue: dispatch_queue_t?, options: [String : Any]?) { + super.init(state: .unknown) + } + + func scanForPeripherals(withServices serviceUUIDs: [CBUUID]?, options: [String : Any]? = nil) { + + } + + func stopScan() { + + } + + func connect(_ peripheral: CBPeripheral, options: [String : Any]? = nil) { + + } +} + +public protocol CBCentralManagerDelegate { + +} + +public class CBService : CBAttribute { + var characteristics: [CBCharacteristic]? +} + +public protocol CBPeripheralDelegate { + +} + +public class CBCharacteristic : CBAttribute { + var service: CBService? + var value: Data? +} + +public class CBPeripheral : CBPeer { + var name: String? + var delegate: (any CBPeripheralDelegate)? + var services: [CBService]? + + func discoverServices(_ serviceUUIDs: [CBUUID]?) { + + } + + func discoverCharacteristics(_ characteristicUUIDs: [CBUUID]?, for service: CBService) { + + } + + func setNotifyValue(_ enabled: Bool, for characteristic: CBCharacteristic) { + + } + + func writeValue(_ data: Data, for: CBCharacteristic, type: CBCharacteristicWriteType) { + + } + + func maximumWriteValueLength(for type: CBCharacteristicWriteType) -> Int { + assert(false) + return -1 + } +} diff --git a/swift/GattlibExample/main.swift b/swift/GattlibExample/main.swift new file mode 100644 index 0000000..f301245 --- /dev/null +++ b/swift/GattlibExample/main.swift @@ -0,0 +1 @@ +print("Hello World!")