Meshtastic-Apple/Meshtastic/Accessory/Accessory Manager/AccessoryManager+Connect.swift
Garth Vander Houwen 026bb80fba
Transports Interface to Support TCP for all Platforms and Serial on Mac (#1341)
* Initial implementation of transports

* Initial LogRadio implementation

* Fixes for Settings view (caused by debug commenting)

* Refinement of the object and actor model

* Connect view text and tab updates

* Fix mac catalyst and tests

* Warning and logging clean-up

* In progress commit

* Serial Transport and Reconnect draft work

* Serial transport and reconnection draft work

* Quick fix for BLE - still more work to do

* interim commit

* More in progress changes

* Minor improvements

* Pretty good initial implementation

* Bump version beyond the app store

* Fix for disconnection swipeAction

* Tweaks to TCPConnection implementation

* Retry for NONCE_ONLY_DB

* Revert json string change

* Simplified some of the API + "Anti-discovery"

* Tweaks for devices leaving the discovery process

* Bump version

* iOS26 Tweaks

* Tweaks and bug fixes

* Add link with slash sf symbol

* update symbol image on connect view

* BLE disconnect handling

* Log privacy attributes

* Onboarding and minor fixes.

* change database to nodes, add emoji to tcp logs

* Error handling improvements

* More logging emojis

* Suppressed unnecessary errors on disconnect

* Heartbeat emoji

* Add bluetooth symbol

* add privacy attributes to [TCP] logs, add custom bluetooth logo

* Improve routing logs

* Emoji for connect logs

* Heartbeat emoji

* Add CBCentralManagerScanOptionAllowDuplicatesKey options to central for bluetooth

* fix nav errors by switching from observableobject to state

* Update connection indicator icon

* fix for BLE disconnects

* Connection process fixes

* More fixes/tweaks to connection process

* Strict concurrency

* Fix some warnings, remove wifi warning

* delete stale keys

* interim commit

* Update privacy for log, fix wrong space

* fix a couple of linting items

* Switch to targeted

* interim commit

* BLE Signal strenth on connect view

* Remove BLE RSSI from long press menu

* Modem lights

* minor spacing tweak

* Additional BLE logging and a scanning fix.

* Discovery and BLE RSSI improvements

* Background suspension

* Update isConnected to enable UI during db load

* update protobufs

* Replace config if statements with switches, Fix unknown module config logging, make dark mode modem circle stroke color white so they are visible

* Additional logging cleanup

* hast

* Set unmessagable to true if the longname has the unmessagable emoji

* Connect error handling improvements

* Admin popup list icon and activity lights updates

* Revert use of .toolbar back to .navigationBarItems

* More public logging

* Better BLE error handling

* Node DB progress meter

* minor tweak to activity light interaction timing

* Fix comment linting, remove stale keys

* Remove stale keys

* Easy linting fixes

* Two more simple linting fixes

* clean up meshtasticapp

* More public logging

* Replay config

* Logging

* Fix for unselected node on Settings

* Tweak to progress meter based on device idiom

* Update protos

* Session replay redaction of messages

* Serial fix for old devices, and a let statement

* Mask text too

* Fix typo

* BLE poweredOff is now an auto-reconnectable error

* Update logging

* Fix for peerRemovedPairingInformation

* Logging for BLE peripheral:didUpdateValueFor errors.

* Fix for inconsistent swipe disconnect behavior

* periperal:didUpdateValueFor error handling

* Fix for BLEConnection continuation guarding

* BLEConnection actor deadlock on disconnect

* Heartbeat nonce

* Fix for swipe disconnect and task cancellation

* Fix for swipe actions not honoring .disabled()

* Tell BLETransport when BLEConnection is cancelled

* Update navigation logging

* Logging updates

* Bump version to 2.7.0

* Organize into folders and heartbeat stuff

* Minor improvements to manual TCP connection

* Auto-connect toggle

* Possible BLE bug, still waiting to see in logs

* Concurrency tweaks

* Concurrency improvements

* requestDeviceMetadata fix. fixes remote admin

* Minor typo fixes

* "All" button for log filters: category and level

* More robust continuation handling for BLE

* @FetchRequest based ChannelMessageList

* Update info.plist and device hardware file

* Move auto connect toggle to app settings and debug mode, tint properly with the accent color

* Add label to auto connect toggle

* Update log for node info received from ourselves over the mesh

* Remove unused scrollViewProxy

* Update Meshtastic/Views/Onboarding/DeviceOnboarding.swift

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update target for connect view

* Properly Set datadog environment

* Comment out ble manager

* Adjust cyclomatic complexity thresholds in .swiftlint.yml

* Linting fixes, delete ble manager

* Make session replay debug only

---------

Co-authored-by: jake-b <jake-b@users.noreply.github.com>
Co-authored-by: jake <jake@jakes-Mac-mini.local>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-08-27 08:09:02 -07:00

334 lines
11 KiB
Swift

//
// AccessoryManager+Connect.swift
// Meshtastic
//
// Created by Jake Bordens on 7/24/25.
//
import Foundation
import OSLog
import MeshtasticProtobufs
import CoreBluetooth
private let maxRetries = 10
private let retryDelay: Duration = .seconds(1)
extension AccessoryManager {
func connect(to device: Device) async throws {
// Prevent new connection if one is active
if activeConnection != nil {
throw AccessoryError.connectionFailed("Already connected to a device")
}
guard let transport = transportForType(device.transportType) else {
throw AccessoryError.connectionFailed("No transport for type")
}
// Clear any errors from last time
lastConnectionError = nil
packetsSent = 0
packetsReceived = 0
expectedNodeDBSize = nil
// Prepare to connect
self.connectionStepper = SequentialSteps(maxRetries: maxRetries, retryDelay: retryDelay) {
// Step 0
Step { @MainActor retryAttempt in
Logger.transport.info("🔗👟 [Connect] Starting connection to \(device.id)")
if retryAttempt > 0 {
try await self.closeConnection() // clean-up before retries.
self.updateState(.retrying(attempt: retryAttempt + 1))
self.allowDisconnect = true
} else {
self.updateState(.connecting)
}
self.updateDevice(deviceId: device.id, key: \.connectionState, value: .connecting)
}
// Step 1: Setup the connection
Step(timeout: .seconds(2)) { @MainActor _ in
Logger.transport.info("🔗👟[Connect] Step 1: connection to \(device.id)")
do {
let connection = try await transport.connect(to: device)
let eventStream = try await connection.connect()
self.updateState(.communicating)
self.connectionEventTask = Task {
for await event in eventStream {
self.didReceive(event)
}
Logger.transport.info("[Accessory] Event stream closed")
}
self.activeConnection = (device: device, connection: connection)
if UserDefaults.preferredPeripheralId.count < 1 {
UserDefaults.preferredPeripheralId = device.id.uuidString
}
} catch let error as CBError where error.code == .peerRemovedPairingInformation {
await self.connectionStepper?.cancelCurrentlyExecutingStep(withError: error, cancelFullProcess: true)
}
}
// Step 2: Send Heartbeat before wantConfig (config)
Step { @MainActor _ in
Logger.transport.info("💓👟 [Connect] Step 2: Send heartbeat")
try await self.sendHeartbeat()
}
// Step 3: Send WantConfig (config)
Step(timeout: .seconds(30)) { @MainActor _ in
Logger.transport.info("🔗👟 [Connect] Step 3: Send wantConfig (config)")
try await self.sendWantConfig()
}
// Step 4: Send Heartbeat before wantConfig (database)
Step { @MainActor _ in
Logger.transport.info("💓 [Connect] Step 4: Send heartbeat")
try await self.sendHeartbeat()
}
// Step 5: Send WantConfig (database)
Step(timeout: .seconds(3.0), onFailure: .retryStep(attempts: 3)) { @MainActor _ in
Logger.transport.info("🔗👟 [Connect] Step 5: Send wantConfig (database)")
self.updateState(.retrievingDatabase(nodeCount: 0))
self.allowDisconnect = true
try await self.sendWantDatabase()
}
// Step 5a: Wait for end of WantConfig (database)
Step { @MainActor _ in
Logger.transport.info("🔗👟 [Connect] Step 5a: Wait for the final database")
try await self.waitForWantDatabaseResponse()
}
// Step 6: Version check
Step { @MainActor _ in
Logger.transport.info("🔗👟 [Connect] Step 6: Version check")
guard let firmwareVersion = self.activeConnection?.device.firmwareVersion else {
Logger.transport.error("🔗 [Connect] Firmware version not available for device \(device.name, privacy: .public)")
throw AccessoryError.connectionFailed("Firmware version not available")
}
let lastDotIndex = firmwareVersion.lastIndex(of: ".")
if lastDotIndex == nil {
throw AccessoryError.versionMismatch("🚨" + "Update Your Firmware".localized)
}
let version = firmwareVersion[...(lastDotIndex ?? String.Index(utf16Offset: 6, in: firmwareVersion))].dropLast()
// TODO: do we really need to store the firmware version in the UserDefaults?
UserDefaults.firmwareVersion = String(version)
let supportedVersion = self.checkIsVersionSupported(forVersion: self.minimumVersion)
if !supportedVersion {
throw AccessoryError.connectionFailed("🚨" + "Update Your Firmware".localized)
}
}
// Step 7: Update UI and status to connected
Step { @MainActor _ in
Logger.transport.info("🔗👟 [Connect] Step 7: Update UI and status")
// We have an active connection
self.updateDevice(deviceId: device.id, key: \.connectionState, value: .connected)
self.updateState(.subscribed)
}
// Step 8: Update UI and status to connected
Step { @MainActor _ in
Logger.transport.debug("🔗👟 [Connect] Step 8: Initialize MQTT and Location Provider")
self.stopDiscovery()
await self.initializeMqtt()
self.initializeLocationProvider()
if transport.requiresPeriodicHeartbeat {
await self.setupPeriodicHeartbeat()
}
}
}
// Run the connection process
do {
try await connectionStepper?.run()
Logger.transport.debug("🔗 [Connect] ConnectionStepper completed.")
} catch {
Logger.transport.error("🔗 [Connect] Error returned by connectionStepper: \(error)")
try await self.closeConnection()
updateState(.discovering)
self.lastConnectionError = error
}
// All done, one way or another, clean up
self.connectionStepper = nil
}
}
// Sequentially stepped tasks
typealias Step = SequentialSteps.Step
actor SequentialSteps {
typealias StepClosure = @Sendable (_ retryAttempt: Int) async throws -> Void
enum FailureBehavior {
case fail
case retryStep(attempts: Int)
case retryAll
}
struct Step {
let timeout: Duration?
let failureBehavior: FailureBehavior
let operation: StepClosure
init(timeout: Duration? = nil, onFailure: FailureBehavior = .retryAll, operation: @escaping StepClosure) {
self.timeout = timeout
self.failureBehavior = onFailure
self.operation = operation
}
}
private enum SequentialStepError: Error, LocalizedError {
case timeout(stepNumber: Int, afterWaiting: Duration)
var errorDescription: String? {
switch self {
case .timeout(let stepNumber, let afterWaiting):
return "Timeout after \(afterWaiting) waiting for step \(stepNumber)."
}
}
}
let steps: [Step]
var currentlyExecutingStep: Task<Void, any Error>?
var cancelled = false
var maxRetries: Int
var retryDelay: Duration
var isRunning: Bool = false
var externalError: Error?
init(maxRetries: Int = 1, retryDelay: Duration = .seconds(3), @StepsBuilder _ builder: () -> [Step]) {
self.maxRetries = maxRetries
self.retryDelay = retryDelay
self.steps = builder()
}
func run() async throws {
self.isRunning = true
retryLoop: for attempt in 0..<maxRetries {
for stepNumber in 0..<steps.count {
if cancelled {
throw externalError ?? CancellationError()
}
let currentStep = steps[stepNumber]
let isRetry = (attempt > 0)
if isRetry {
try await Task.sleep(for: retryDelay)
}
do {
let stepRetries = if case let .retryStep(attempts) = currentStep.failureBehavior, attempts > 0 { attempts } else { 1 }
stepRetryLoop: for stepRetryAttempt in 0..<stepRetries {
if stepRetryAttempt > 0 {
Logger.transport.info("[Retry Step Loop] Retrying step \(stepNumber + 1) for the \(stepRetryAttempt + 1) time.")
try await Task.sleep(for: retryDelay)
}
do {
// Starting a new attempt for this step.
if let duration = currentStep.timeout {
// Execute this task with a timeout
self.currentlyExecutingStep = executeWithTimeout(stepNumber: stepNumber, timeout: duration) {
try await currentStep.operation(attempt)
}
try await self.currentlyExecutingStep!.value
} else {
// Execute this task without a timeout
self.currentlyExecutingStep = Task {
try await currentStep.operation(attempt)
}
try await self.currentlyExecutingStep!.value
}
break stepRetryLoop // Exit retry loop if successful
} catch {
if stepRetryAttempt == stepRetries - 1 {
// If this is the last retry attempt, we throw the error to the outer loop
throw error
} else {
switch error {
case let SequentialStepError.timeout(stepNumber, afterWaiting):
Logger.transport.info("[Inner Retry Step Loop] Sequential process timed out on step \(stepNumber) of \(stepRetries) after waiting \(afterWaiting)")
case is CancellationError:
if let externalError {
// Something from the outside had an error which caused the cancellation of this step
let errorToThrow = externalError
self.externalError = nil
throw errorToThrow
}
break stepRetryLoop
default:
Logger.transport.error("[Inner Retry Step Loop] Sequential process failed on step \(stepNumber) with error: \(error.localizedDescription, privacy: .public)")
}
}
}
}
} catch {
switch error {
case let SequentialStepError.timeout(stepNumber, afterWaiting):
Logger.transport.info("[Outer Step Retry Loop] Sequential process timed out on step \(stepNumber) after waiting \(afterWaiting)")
default:
Logger.transport.error("[Outer Step Retry Loop] Sequential process failed on step \(stepNumber) with error: \(error.localizedDescription, privacy: .public)")
}
switch currentStep.failureBehavior {
case .retryAll, .retryStep:
// TODO: we could have a .retryStepAndFail and a .retryStepAndContinue instead of just .retryStep to clarify the behavior here
continue retryLoop
case .fail:
isRunning = false
throw error
}
}
}
// We have finished all steps
isRunning = false
return
}
isRunning = false
throw AccessoryError.tooManyRetries
}
func cancel() {
cancelled = true
self.currentlyExecutingStep?.cancel()
}
func cancelCurrentlyExecutingStep(withError: Error?, cancelFullProcess: Bool = false) {
self.externalError = withError
if cancelFullProcess {
cancel()
} else {
self.currentlyExecutingStep?.cancel()
}
}
func executeWithTimeout<ReturnType>(stepNumber: Int, timeout: Duration, operation: @escaping @Sendable () async throws -> ReturnType) -> Task<ReturnType, Error> {
return Task {
try await withThrowingTaskGroup(of: ReturnType.self) { group -> ReturnType in
group.addTask(operation: operation)
group.addTask {
try await _Concurrency.Task.sleep(for: timeout)
throw SequentialStepError.timeout(stepNumber: stepNumber, afterWaiting: timeout)
}
guard let success = try await group.next() else {
throw SequentialStepError.timeout(stepNumber: stepNumber, afterWaiting: timeout)
}
group.cancelAll()
return success
}
}
}
@resultBuilder
struct StepsBuilder {
static func buildBlock(_ components: Step...) -> [Step] {
return components
}
}
}