diff --git a/OmniBLE/PumpManager/PodCommsSession.swift b/OmniBLE/PumpManager/PodCommsSession.swift index aa17c2c..75e20f4 100644 --- a/OmniBLE/PumpManager/PodCommsSession.swift +++ b/OmniBLE/PumpManager/PodCommsSession.swift @@ -11,6 +11,10 @@ import Foundation import LoopKit import os.log +// configure with low percentage of failure, should always recover +fileprivate var fakeUncertainBeforeSendPercent: Double = 0.01 +fileprivate var fakeUncertainAfterSendPercent: Double = 0.01 + public enum PodCommsError: Error { case noPodPaired case invalidData @@ -325,8 +329,36 @@ public class PodCommsSession { // Clear the lastDeliveryStatusReceived variable which is used to guard against possible 0x31 pod faults podState.lastDeliveryStatusReceived = nil + // Toggle the fakeUncertainXXX values here (before they are used), but only if set to 0.0 or 1.0 + // No change if set to a fraction + if fakeUncertainBeforeSendPercent == 1.0 { + fakeUncertainBeforeSendPercent = 0.0 + } else if fakeUncertainBeforeSendPercent == 0.0 { + fakeUncertainBeforeSendPercent = 1.0 + } + if fakeUncertainAfterSendPercent == 1.0 { + fakeUncertainAfterSendPercent = 0.0 + } else if fakeUncertainAfterSendPercent == 0.0 { + fakeUncertainAfterSendPercent = 1.0 + } + // enable breakpoint here to configure fakeUncertainXXX for testing + + if Double.random(in: 0...1) < fakeUncertainBeforeSendPercent { + // fake an uncertain message delivery that was never received by the pod by failing right before sending + let mess = "Fake uncertain command error not received by pod" + print(Date(), "@@@ \(mess) for command message \(String(describing: message))") + throw PodCommsError.unacknowledgedMessage(sequenceNumber: messageNumber, error: PodCommsError.diagnosticMessage(str: mess)) + } + let response = try transport.sendMessage(message) - + + if Double.random(in: 0...1) < fakeUncertainAfterSendPercent { + // fake an uncertain message delivery that was actually received by the pod by failing right after sending + let mess = "Fake uncertain command error received by pod" + print(Date(), "@@@ \(mess) for command message \(String(describing: message))") + throw PodCommsError.unacknowledgedMessage(sequenceNumber: messageNumber, error: PodCommsError.diagnosticMessage(str: mess)) + } + // Simulate fault //let podInfoResponse = try PodInfoResponse(encodedData: Data(hexadecimalString: "0216020d0000000000ab6a038403ff03860000285708030d0000")!) //let response = Message(address: podState.address, messageBlocks: [podInfoResponse], sequenceNum: message.sequenceNum) @@ -440,7 +472,10 @@ public class PodCommsSession { } do { - _ = try getStatus() // should resolve the pending unacknowledged command if successful + let statusResponse = try getStatus() + if podState.unacknowledgedCommand != nil { // XXX added debug code to handle getStatus not handlling for testing + recoverUnacknowledgedCommand(using: statusResponse) + } // XXX end added debug code } catch let error { log.error("GetStatus failed trying to resolve pending unacknowledged command: %{public}@", String(describing: error)) throw PodCommsError.unacknowledgedCommandPending @@ -932,6 +967,7 @@ public class PodCommsSession { let statusResponse: StatusResponse = try send([GetStatusCommand(podInfoType: statusType)], beepBlock: beepBlock) if podState.unacknowledgedCommand != nil { + print(Date(), "@@@ func getStatus calling recoverUnacknowledgedCommand to allow for recovery testing") recoverUnacknowledgedCommand(using: statusResponse) } podState.updateFromStatusResponse(statusResponse, at: currentDate)