Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions OmnipodKit/Bluetooth/BlePodComms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class BlePodComms: PodComms {

var manager: PeripheralManager? {
didSet {
if oldValue !== manager {
oldValue?.delegate = nil
}
manager?.delegate = self
}
}
Expand Down Expand Up @@ -56,6 +59,7 @@ class BlePodComms: PodComms {
log.default("Removing %{public}@ from auto-connect ids", manager.peripheral)
bluetoothManager.disconnectFromDevice(uuidString: manager.peripheral.identifier.uuidString)
}
manager = nil
super.forgetPod()
}

Expand Down Expand Up @@ -225,7 +229,7 @@ class BlePodComms: PodComms {

log.info("Establish an Eap Session")
let msgSeq = Int(response.msgSeq)
guard let bleMessageTransportState = try establishSession(ltk: ltk, eapSeq: eapSeq, msgSeq: msgSeq) else {
guard let bleMessageTransportState = try establishSession(on: manager, ltk: ltk, eapSeq: eapSeq, msgSeq: msgSeq) else {
log.error("@@@ pairPod: failed to establish session!")
throw PodCommsError.noPodPaired
}
Expand Down Expand Up @@ -278,12 +282,10 @@ class BlePodComms: PodComms {
log.bleDebug("@@@ pairPod: podState transport state is %{public}@", self.podState!.bleMessageTransportState.inlineDescription)
}

private func establishSession(ltk: Data, eapSeq: Int, msgSeq: Int = 1) throws -> BleMessageTransportState? {
private func establishSession(on manager: PeripheralManager, ltk: Data, eapSeq: Int, msgSeq: Int = 1) throws -> BleMessageTransportState? {
// We should already be holding podStateLock during calls to this function, so try() should fail
assert(!podStateLock.try(), "\(#function) should be invoked while holding podStateLock")

guard let manager = manager else { throw PodCommsError.noPodPaired }

// PRIMARY mode (controller initiates challenge).
// SECONDARY mode was tested for O5 post-pairing reconnections (tests #24, #25)
// but the pod never initiates an EAP-AKA challenge — it expects PRIMARY always.
Expand Down Expand Up @@ -328,17 +330,17 @@ class BlePodComms: PodComms {
}
}

private func establishNewSession() throws {
private func establishNewSession(on manager: PeripheralManager) throws {
// We should already be holding podStateLock during calls to this function, so try() should fail
assert(!podStateLock.try(), "\(#function) should be invoked while holding podStateLock")

guard let ltk = podState?.ltk else {
throw PodCommsError.noPodPaired
}

let mts = try establishSession(ltk: ltk, eapSeq: podState!.incrementEapSeq())
let mts = try establishSession(on: manager, ltk: ltk, eapSeq: podState!.incrementEapSeq())
if mts == nil {
let mts = try establishSession(ltk: ltk, eapSeq: podState!.incrementEapSeq())
let mts = try establishSession(on: manager, ltk: ltk, eapSeq: podState!.incrementEapSeq())
if mts == nil {
throw PodCommsError.diagnosticMessage(str: "Received resynchronization SQN for the second time")
}
Expand Down Expand Up @@ -617,7 +619,7 @@ class BlePodComms: PodComms {
// Paired but no active session: re-establish
try manager.sendHello(myId: myId)
try manager.enableNotifications()
try establishNewSession()
try establishNewSession(on: manager)
}

guard podState != nil else {
Expand Down Expand Up @@ -801,11 +803,17 @@ extension BlePodComms: PeripheralManagerDelegate {
podStateLock.unlock()
}

guard manager === self.manager else {
log.default("completeConfiguration for a manager that is no longer the pod's, skipping login")
manager.noteSetupStep("not the pod's current manager, login skipped")
return
}

do {
try manager.setupStep("hello") { try manager.sendHello(myId: myId) }
// Seemingly this cannot be done before the hello command, or the pod disconnects
try manager.setupStep("notifications") { try manager.enableNotifications() }
try manager.setupStep("login") { try establishNewSession() }
try manager.setupStep("login") { try establishNewSession(on: manager) }
needsSessionEstablishment = false
delegate?.podCommsDidEstablishSession(self)
} catch {
Expand Down
2 changes: 2 additions & 0 deletions OmnipodKit/Bluetooth/PeripheralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ extension PeripheralManager {
if let delegate = self.delegate {
try delegate.completeConfiguration(for: self)
self.log.bleDebug("Delegate configuration notified")
} else {
self.noteSetupStep("no delegate, login skipped")
}

self.log.bleDebug("Peripheral configuration completed")
Expand Down