Skip to content

Commit df428f6

Browse files
authored
G6 update hotfix (#128)
* Delay control subscription until after auth (#127) * Bump LoopKit rev * Bump version
1 parent b2d483a commit df428f6

File tree

11 files changed

+74
-27
lines changed

11 files changed

+74
-27
lines changed

CGMBLEKit Example/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>APPL</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>3.0</string>
18+
<string>3.1</string>
1919
<key>CFBundleVersion</key>
2020
<string>1</string>
2121
<key>LSRequiresIPhoneOS</key>

CGMBLEKit.xcodeproj/project.pbxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@
13021302
DEFINES_MODULE = YES;
13031303
DEVELOPMENT_TEAM = "";
13041304
DYLIB_COMPATIBILITY_VERSION = 1;
1305-
DYLIB_CURRENT_VERSION = 20;
1305+
DYLIB_CURRENT_VERSION = 21;
13061306
DYLIB_INSTALL_NAME_BASE = "@rpath";
13071307
FRAMEWORK_SEARCH_PATHS = (
13081308
"$(inherited)",
@@ -1335,7 +1335,7 @@
13351335
DEFINES_MODULE = YES;
13361336
DEVELOPMENT_TEAM = "";
13371337
DYLIB_COMPATIBILITY_VERSION = 1;
1338-
DYLIB_CURRENT_VERSION = 20;
1338+
DYLIB_CURRENT_VERSION = 21;
13391339
DYLIB_INSTALL_NAME_BASE = "@rpath";
13401340
FRAMEWORK_SEARCH_PATHS = (
13411341
"$(inherited)",
@@ -1387,7 +1387,7 @@
13871387
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
13881388
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
13891389
COPY_PHASE_STRIP = NO;
1390-
CURRENT_PROJECT_VERSION = 20;
1390+
CURRENT_PROJECT_VERSION = 21;
13911391
DEBUG_INFORMATION_FORMAT = dwarf;
13921392
ENABLE_STRICT_OBJC_MSGSEND = YES;
13931393
ENABLE_TESTABILITY = YES;
@@ -1457,7 +1457,7 @@
14571457
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
14581458
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
14591459
COPY_PHASE_STRIP = NO;
1460-
CURRENT_PROJECT_VERSION = 20;
1460+
CURRENT_PROJECT_VERSION = 21;
14611461
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
14621462
ENABLE_NS_ASSERTIONS = NO;
14631463
ENABLE_STRICT_OBJC_MSGSEND = YES;
@@ -1496,7 +1496,7 @@
14961496
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
14971497
DEFINES_MODULE = YES;
14981498
DYLIB_COMPATIBILITY_VERSION = 1;
1499-
DYLIB_CURRENT_VERSION = 20;
1499+
DYLIB_CURRENT_VERSION = 21;
15001500
DYLIB_INSTALL_NAME_BASE = "@rpath";
15011501
FRAMEWORK_SEARCH_PATHS = (
15021502
"$(inherited)",
@@ -1523,7 +1523,7 @@
15231523
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
15241524
DEFINES_MODULE = YES;
15251525
DYLIB_COMPATIBILITY_VERSION = 1;
1526-
DYLIB_CURRENT_VERSION = 20;
1526+
DYLIB_CURRENT_VERSION = 21;
15271527
DYLIB_INSTALL_NAME_BASE = "@rpath";
15281528
FRAMEWORK_SEARCH_PATHS = (
15291529
"$(inherited)",

CGMBLEKit/BluetoothManager.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ protocol BluetoothManagerDelegate: class {
4545
/// - manager: The bluetooth manager
4646
/// - response: The data received on the backfill characteristic
4747
func bluetoothManager(_ manager: BluetoothManager, didReceiveBackfillResponse response: Data)
48+
49+
/// Informs the delegate that the bluetooth manager received new data in the authentication characteristic
50+
///
51+
/// - Parameters:
52+
/// - manager: The bluetooth manager
53+
/// - response: The data received on the authentication characteristic
54+
func bluetoothManager(_ manager: BluetoothManager, peripheralManager: PeripheralManager, didReceiveAuthenticationResponse response: Data)
4855
}
4956

5057

@@ -318,12 +325,14 @@ extension BluetoothManager: PeripheralManagerDelegate {
318325
}
319326

320327
switch CGMServiceCharacteristicUUID(rawValue: characteristic.uuid.uuidString.uppercased()) {
321-
case .none, .communication?, .authentication?:
328+
case .none, .communication?:
322329
return
323330
case .control?:
324331
self.delegate?.bluetoothManager(self, didReceiveControlResponse: value)
325332
case .backfill?:
326333
self.delegate?.bluetoothManager(self, didReceiveBackfillResponse: value)
334+
case .authentication?:
335+
self.delegate?.bluetoothManager(self, peripheralManager: manager, didReceiveAuthenticationResponse: value)
327336
}
328337
}
329338
}

CGMBLEKit/BluetoothServices.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,24 @@ enum DeviceInfoCharacteristicUUID: String, CBUUIDRawValue {
4040

4141

4242
enum CGMServiceCharacteristicUUID: String, CBUUIDRawValue {
43+
4344
// Read/Notify
4445
case communication = "F8083533-849E-531C-C594-30F1F86A4EA5"
46+
4547
// Write/Indicate
4648
case control = "F8083534-849E-531C-C594-30F1F86A4EA5"
47-
// Read/Write/Indicate
49+
50+
// Write/Indicate
4851
case authentication = "F8083535-849E-531C-C594-30F1F86A4EA5"
4952

5053
// Read/Write/Notify
5154
case backfill = "F8083536-849E-531C-C594-30F1F86A4EA5"
55+
56+
// // Unknown attribute present on older G6 transmitters
57+
// case unknown1 = "F8083537-849E-531C-C594-30F1F86A4EA5"
58+
//
59+
// // Updated G6 characteristic (read/notify)
60+
// case unknown2 = "F8083538-849E-531C-C594-30F1F86A4EA5"
5261
}
5362

5463

CGMBLEKit/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>3.0</string>
18+
<string>3.1</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

CGMBLEKit/Messages/AuthChallengeRxMessage.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import Foundation
1010

1111

1212
struct AuthChallengeRxMessage: TransmitterRxMessage {
13-
let authenticated: UInt8
14-
let bonded: UInt8
13+
let isAuthenticated: Bool
14+
let isBonded: Bool
1515

1616
init?(data: Data) {
1717
guard data.count >= 3 else {
@@ -22,7 +22,7 @@ struct AuthChallengeRxMessage: TransmitterRxMessage {
2222
return nil
2323
}
2424

25-
authenticated = data[1]
26-
bonded = data[2]
25+
isAuthenticated = data[1] == 0x1
26+
isBonded = data[2] == 0x1
2727
}
2828
}

CGMBLEKit/Transmitter.swift

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ public final class Transmitter: BluetoothManagerDelegate {
149149

150150
peripheralManager.perform { (peripheral) in
151151
if self.passiveModeEnabled {
152-
self.log.debug("Listening for control commands in passive mode")
152+
self.log.debug("Listening for authentication responses in passive mode")
153153
do {
154-
try peripheral.listenToControl()
154+
try peripheral.listenToCharacteristic(.authentication)
155155
} catch let error {
156156
self.delegateQueue.async {
157157
self.delegate?.transmitter(self, didError: error)
@@ -162,14 +162,14 @@ public final class Transmitter: BluetoothManagerDelegate {
162162
self.log.debug("Authenticating with transmitter")
163163
let status = try peripheral.authenticate(id: self.id)
164164

165-
if status.bonded != 0x1 {
165+
if !status.isBonded {
166166
self.log.debug("Requesting bond")
167167
try peripheral.requestBond()
168168

169169
self.log.debug("Bonding request sent. Waiting user to respond.")
170170
}
171171

172-
try peripheral.enableNotify(shouldWaitForBond: status.bonded != 0x1)
172+
try peripheral.enableNotify(shouldWaitForBond: !status.isBonded)
173173
defer {
174174
self.log.debug("Initiating a disconnect")
175175
peripheral.disconnect()
@@ -319,6 +319,28 @@ public final class Transmitter: BluetoothManagerDelegate {
319319

320320
self.backfillBuffer?.append(response)
321321
}
322+
323+
func bluetoothManager(_ manager: BluetoothManager, peripheralManager: PeripheralManager, didReceiveAuthenticationResponse response: Data) {
324+
325+
if let message = AuthChallengeRxMessage(data: response), message.isBonded, message.isAuthenticated {
326+
self.log.debug("Observed authenticated session. enabling notifications for control characteristic.")
327+
peripheralManager.perform { (peripheral) in
328+
do {
329+
try peripheral.listenToCharacteristic(.control)
330+
try peripheral.listenToCharacteristic(.backfill)
331+
} catch let error {
332+
self.log.error("Error trying to enable notifications on control characteristic: %{public}@", String(describing: error))
333+
}
334+
do {
335+
try peripheral.stopListeningToCharacteristic(.authentication)
336+
} catch let error {
337+
self.log.error("Error trying to disable notifications on authentication characteristic: %{public}@", String(describing: error))
338+
}
339+
}
340+
} else {
341+
self.log.debug("Ignoring authentication response: %{public}@", response.hexadecimalString)
342+
}
343+
}
322344
}
323345

324346

@@ -390,7 +412,7 @@ fileprivate extension PeripheralManager {
390412
throw TransmitterError.authenticationError("Unable to parse auth status: \(error)")
391413
}
392414

393-
guard challengeResponse.authenticated == 1 else {
415+
guard challengeResponse.isAuthenticated else {
394416
throw TransmitterError.authenticationError("Transmitter rejected auth challenge")
395417
}
396418

@@ -478,12 +500,19 @@ fileprivate extension PeripheralManager {
478500
}
479501
}
480502

481-
fileprivate func listenToControl() throws {
503+
func listenToCharacteristic(_ characteristic: CGMServiceCharacteristicUUID) throws {
482504
do {
483-
try setNotifyValue(true, for: .control)
484-
try setNotifyValue(true, for: .backfill)
505+
try setNotifyValue(true, for: characteristic)
485506
} catch let error {
486-
throw TransmitterError.controlError("Error enabling notification: \(error)")
507+
throw TransmitterError.controlError("Error enabling notification for \(characteristic): \(error)")
508+
}
509+
}
510+
511+
func stopListeningToCharacteristic(_ characteristic: CGMServiceCharacteristicUUID) throws {
512+
do {
513+
try setNotifyValue(false, for: characteristic)
514+
} catch let error {
515+
throw TransmitterError.controlError("Error disabling notification for \(characteristic): \(error)")
487516
}
488517
}
489518
}

CGMBLEKitTests/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>BNDL</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>3.0</string>
18+
<string>3.1</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

CGMBLEKitUI/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>3.0</string>
18+
<string>3.1</string>
1919
<key>CFBundleVersion</key>
2020
<string>$(CURRENT_PROJECT_VERSION)</string>
2121
<key>NSPrincipalClass</key>

Cartfile.resolved

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github "LoopKit/LoopKit" "v2.2.1"
1+
github "LoopKit/LoopKit" "v2.2.2"
22
github "LoopKit/dexcom-share-client-swift" "v1.0"

0 commit comments

Comments
 (0)