-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3830dc
commit f3c1ccb
Showing
608 changed files
with
244,262 additions
and
574 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
SiliconLabsApp/BluetoothControllers/SILPeripheralDelegate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// | ||
// SILPeripheralDelegate.swift | ||
// BlueGecko | ||
// | ||
// Created by Kamil Czajka on 29.3.2021. | ||
// Copyright © 2021 SiliconLabs. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
enum SILPeripheralDelegateStatus { | ||
case successForServices(_ services: [CBService]) | ||
case successForCharacteristics(_ characteristics: [CBCharacteristic]) | ||
case successGetValue(value: Data?, characteristic: CBCharacteristic) | ||
case successWrite(characteristic: CBCharacteristic) | ||
case updateNotificationState(characteristic: CBCharacteristic, state: Bool) | ||
case servicesModified(peripheral: CBPeripheral) | ||
case failure(error: Error) | ||
case unknown | ||
} | ||
|
||
class SILPeripheralDelegate: NSObject, CBPeripheralDelegate { | ||
private var peripheral: CBPeripheral | ||
|
||
var status: SILObservable<SILPeripheralDelegateStatus> = SILObservable(initialValue: .unknown) | ||
|
||
init(peripheral: CBPeripheral) { | ||
self.peripheral = peripheral | ||
super.init() | ||
self.peripheral.delegate = self | ||
} | ||
|
||
func updatePeripheral(peripheral: CBPeripheral) { | ||
self.peripheral = peripheral | ||
self.peripheral.delegate = self | ||
} | ||
|
||
func newStatus() -> SILObservable<SILPeripheralDelegateStatus> { | ||
status = SILObservable(initialValue: .unknown) | ||
return status | ||
} | ||
|
||
func discoverServices(services: [CBUUID]?) { | ||
self.peripheral.discoverServices(services) | ||
} | ||
|
||
func discoverCharacteristics(characteristics: [CBUUID]?, for service: CBService) { | ||
self.peripheral.discoverCharacteristics(characteristics, for: service) | ||
} | ||
|
||
func readCharacteristic(characteristic: CBCharacteristic) { | ||
self.peripheral.readValue(for: characteristic) | ||
} | ||
|
||
func writeToCharacteristic(data: Data, characteristic: CBCharacteristic, writeType: CBCharacteristicWriteType) { | ||
self.peripheral.writeValue(data, for: characteristic, type: writeType) | ||
} | ||
|
||
func notifyCharacteristic(characteristic: CBCharacteristic, enabled: Bool = true) { | ||
self.peripheral.setNotifyValue(enabled, for: characteristic) | ||
} | ||
|
||
func getMTUValue(for writeType: CBCharacteristicWriteType) -> Int { | ||
return self.peripheral.maximumWriteValueLength(for: writeType) | ||
} | ||
|
||
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { | ||
debugPrint("didDiscoverServices**********") | ||
if let error = error { | ||
status.value = .failure(error: error) | ||
} else { | ||
status.value = .successForServices(peripheral.services!) | ||
} | ||
} | ||
|
||
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { | ||
debugPrint("didDiscoverCharacteristics*********") | ||
if let error = error { | ||
status.value = .failure(error: error) | ||
} else { | ||
status.value = .successForCharacteristics(service.characteristics!) | ||
} | ||
} | ||
|
||
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { | ||
debugPrint("didUpdateValueForCharacteristic*********") | ||
if let error = error { | ||
status.value = .failure(error: error) | ||
} else { | ||
status.value = .successGetValue(value: characteristic.value, characteristic: characteristic) | ||
} | ||
} | ||
|
||
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) { | ||
debugPrint("didWriteValueForCharacteristic*********") | ||
if let error = error { | ||
status.value = .failure(error: error) | ||
} else { | ||
status.value = .successWrite(characteristic: characteristic) | ||
} | ||
} | ||
|
||
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) { | ||
debugPrint("didUpdateNotificationStateFor*********") | ||
if let error = error { | ||
status.value = .failure(error: error) | ||
} else { | ||
status.value = .updateNotificationState(characteristic: characteristic, state: characteristic.isNotifying) | ||
} | ||
} | ||
|
||
func peripheral(_ peripheral: CBPeripheral, didModifyServices invalidatedServices: [CBService]) { | ||
debugPrint("didMofifyServices*********") | ||
status.value = .servicesModified(peripheral: peripheral) | ||
} | ||
|
||
func findService(with serviceUUID: CBUUID, in peripheral: CBPeripheral) -> CBService? { | ||
return peripheral.services?.first(where: { service in service.uuid == serviceUUID}) | ||
} | ||
|
||
func findCharacteristic(with characteristicUUID: CBUUID, in characteristics: [CBCharacteristic]) -> CBCharacteristic? { | ||
return characteristics.first(where: { characteristic in characteristic.uuid == characteristicUUID }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// String+Subscript.swift | ||
// BlueGecko | ||
// | ||
// Created by Grzegorz Janosz on 19/11/2021. | ||
// Copyright © 2021 SiliconLabs. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension String { | ||
subscript(_ i: Int) -> String { | ||
let idx1 = index(startIndex, offsetBy: i) | ||
let idx2 = index(idx1, offsetBy: 1) | ||
return String(self[idx1..<idx2]) | ||
} | ||
|
||
subscript (r: Range<Int>) -> String { | ||
let start = index(startIndex, offsetBy: r.lowerBound) | ||
let end = index(startIndex, offsetBy: r.upperBound) | ||
return String(self[start ..< end]) | ||
} | ||
|
||
subscript (r: CountableClosedRange<Int>) -> String { | ||
let startIndex = self.index(self.startIndex, offsetBy: r.lowerBound) | ||
let endIndex = self.index(startIndex, offsetBy: r.upperBound - r.lowerBound) | ||
return String(self[startIndex...endIndex]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// String+VersionCompare.swift | ||
// BlueGecko | ||
// | ||
// Created by Grzegorz Janosz on 10/12/2021. | ||
// Copyright © 2021 SiliconLabs. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension String { | ||
func versionCompare(_ otherVersion: String) -> ComparisonResult { | ||
return self.compare(otherVersion, options: .numeric) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.