fix: fixed race condition in the Bluetooth module (#591)

This commit is contained in:
Serhiy Mytrovtsiy
2021-08-27 18:11:43 +02:00
parent f3632e529d
commit d49eef9020
4 changed files with 36 additions and 5 deletions

View File

@@ -29,6 +29,8 @@ identifier_name:
- NetworkReaders
- SensorsList
- Alignments
- _devices
- _uuidAddress
line_length: 200

View File

@@ -15,13 +15,35 @@ import CoreBluetooth
import IOBluetooth
internal class DevicesReader: Reader<[BLEDevice]>, CBCentralManagerDelegate, CBPeripheralDelegate {
private var devices: [BLEDevice] = []
private let queue = DispatchQueue(label: "eu.exelban.Stats.Bluetooth.reader", attributes: .concurrent)
private var manager: CBCentralManager!
private var uuidAddress: [UUID: String] = [:]
private var _devices: [BLEDevice] = []
private var _uuidAddress: [UUID: String] = [:]
private var peripherals: [CBPeripheral] = []
private var characteristicsDict: [UUID: CBCharacteristic] = [:]
private var devices: [BLEDevice] {
get {
self.queue.sync { self._devices }
}
set {
self.queue.async(flags: .barrier) {
self._devices = newValue
}
}
}
private var uuidAddress: [UUID: String] {
get {
self.queue.sync { self._uuidAddress }
}
set {
self.queue.async(flags: .barrier) {
self._uuidAddress = newValue
}
}
}
private let batteryServiceUUID = CBUUID(string: "0x180F")
private let batteryCharacteristicsUUID = CBUUID(string: "0x2A19")
@@ -89,7 +111,7 @@ internal class DevicesReader: Reader<[BLEDevice]>, CBCentralManagerDelegate, CBP
return
}
if let uuid = UUID(uuidString: key) {
if let uuid = UUID(uuidString: key), self.uuidAddress[uuid] == nil {
self.uuidAddress[uuid] = value
}
}

View File

@@ -89,6 +89,13 @@
isEnabled = "NO">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
<AdditionalOption
key = "NSZombieEnabled"
value = "YES"
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Debug"

View File

@@ -17,7 +17,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>298</string>
<string>299</string>
<key>Description</key>
<string>Simple macOS system monitor in your menu bar</string>
<key>LSApplicationCategoryType</key>