-
-
Notifications
You must be signed in to change notification settings - Fork 262
Add firmware update notifications #2033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
garthvh
merged 5 commits into
meshtastic:main
from
RCGV1:codex/update-firmware-notifications
Jul 16, 2026
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3c58157
Add firmware update notifications
RCGV1 bbc464f
Refine firmware update notifications
RCGV1 61d8dee
Address firmware notification review
RCGV1 74e5ecf
Route flasher update notices externally
RCGV1 d745b84
Merge remote-tracking branch 'origin/main' into pr2033-merge
garthvh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
Meshtastic/Model/Firmware/FirmwareUpdateNotificationPolicy.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // MARK: - FirmwareUpdateNotificationPolicy | ||
|
|
||
| import Foundation | ||
|
|
||
| enum FirmwareUpdateInstallMethod: Equatable { | ||
| case appOTA | ||
| case flasher | ||
| } | ||
|
|
||
| enum FirmwareUpdateNotificationPolicy { | ||
| static func installMethod(architecture: String?) -> FirmwareUpdateInstallMethod { | ||
| guard let architecture = architecture.flatMap({ Architecture(rawValue: $0) }) else { | ||
| return .flasher | ||
| } | ||
|
|
||
| switch architecture { | ||
| case .esp32, .esp32C3, .esp32S3, .esp32C6, .nrf52840: | ||
| return .appOTA | ||
| case .rp2040: | ||
| return .flasher | ||
| } | ||
| } | ||
|
|
||
| static func normalizedVersion(_ version: String) -> String { | ||
| let trimmedVersion = version.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| let cleanVersion = trimmedVersion.hasPrefix("v") ? String(trimmedVersion.dropFirst()) : trimmedVersion | ||
| let parts = cleanVersion.split(separator: ".") | ||
| guard parts.count >= 3 else { return cleanVersion } | ||
| return parts.prefix(3).joined(separator: ".") | ||
| } | ||
|
|
||
| static func isUpdateAvailable(current: String, latestStable: String) -> Bool { | ||
| let currentVersion = normalizedVersion(current) | ||
| let latestStableVersion = normalizedVersion(latestStable) | ||
| return currentVersion.compare(latestStableVersion, options: .numeric) == .orderedAscending | ||
| } | ||
|
|
||
| static func notificationKey( | ||
| nodeNum: Int64, | ||
| platformioTarget: String, | ||
| latestStableVersion: String | ||
| ) -> String { | ||
| "firmware-update-notified:\(nodeNum):\(platformioTarget):\(normalizedVersion(latestStableVersion))" | ||
| } | ||
|
|
||
| static func shouldNotify( | ||
| nodeNum: Int64, | ||
| platformioTarget: String, | ||
| currentVersion: String, | ||
| latestStableVersion: String, | ||
| alreadyNotified: Set<String> | ||
| ) -> Bool { | ||
| guard isUpdateAvailable(current: currentVersion, latestStable: latestStableVersion) else { | ||
| return false | ||
| } | ||
|
|
||
| let key = notificationKey( | ||
| nodeNum: nodeNum, | ||
| platformioTarget: platformioTarget, | ||
| latestStableVersion: latestStableVersion | ||
| ) | ||
| return !alreadyNotified.contains(key) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| // MARK: - FirmwareUpdateNotifier | ||
|
|
||
| import Foundation | ||
| import OSLog | ||
| import SwiftData | ||
|
|
||
| struct FirmwareUpdateNotificationCandidate { | ||
| let nodeNum: Int64 | ||
| let deviceName: String? | ||
| let platformioTarget: String? | ||
| let installMethod: FirmwareUpdateInstallMethod | ||
| let currentVersion: String? | ||
| let latestStableVersion: String? | ||
| } | ||
|
|
||
| struct FirmwareUpdateNotificationSource { | ||
| let nodeNum: Int64 | ||
| let deviceName: String? | ||
| let platformioTarget: String? | ||
| let architecture: String? | ||
| let metadataVersion: String? | ||
| let connectedVersion: String? | ||
| let latestStableVersion: String? | ||
| } | ||
|
|
||
| struct FirmwareUpdateNotice: Equatable { | ||
| let notificationKey: String | ||
| let deviceName: String | ||
| let currentVersion: String | ||
| let latestStableVersion: String | ||
| let installMethod: FirmwareUpdateInstallMethod | ||
|
|
||
| var notificationContent: String { | ||
| switch installMethod { | ||
| case .appOTA: | ||
| return "\(deviceName) is running \(currentVersion). Stable \(latestStableVersion) is available in Firmware Updates." | ||
| case .flasher: | ||
| return "\(deviceName) is running \(currentVersion). Stable \(latestStableVersion) is available. Use Meshtastic Flasher to update this hardware." | ||
| } | ||
| } | ||
|
|
||
| var connectMessage: String { | ||
| switch installMethod { | ||
| case .appOTA: | ||
| return "\(currentVersion) is behind stable \(latestStableVersion). Open Firmware Updates when you're ready." | ||
| case .flasher: | ||
| return "\(currentVersion) is behind stable \(latestStableVersion). Use Meshtastic Flasher for this hardware." | ||
| } | ||
| } | ||
| } | ||
|
|
||
| enum FirmwareUpdateNotifier { | ||
| static let target = "firmwareUpdates" | ||
| static let path = "meshtastic:///settings/firmwareUpdates" | ||
| private static let staleFirmwareAPIInterval: TimeInterval = 24 * 60 * 60 | ||
|
|
||
| static func candidate(from source: FirmwareUpdateNotificationSource) -> FirmwareUpdateNotificationCandidate { | ||
| FirmwareUpdateNotificationCandidate( | ||
| nodeNum: source.nodeNum, | ||
| deviceName: source.deviceName, | ||
| platformioTarget: source.platformioTarget, | ||
| installMethod: FirmwareUpdateNotificationPolicy.installMethod(architecture: source.architecture), | ||
| currentVersion: source.metadataVersion?.isEmpty == false ? source.metadataVersion : source.connectedVersion, | ||
| latestStableVersion: source.latestStableVersion | ||
| ) | ||
| } | ||
|
|
||
| static func notice(for candidate: FirmwareUpdateNotificationCandidate) -> FirmwareUpdateNotice? { | ||
| guard let platformioTarget = candidate.platformioTarget, | ||
| let currentVersion = candidate.currentVersion, | ||
| let latestStableVersion = candidate.latestStableVersion, | ||
| FirmwareUpdateNotificationPolicy.isUpdateAvailable(current: currentVersion, latestStable: latestStableVersion) else { | ||
| return nil | ||
| } | ||
|
|
||
| let key = FirmwareUpdateNotificationPolicy.notificationKey( | ||
| nodeNum: candidate.nodeNum, | ||
| platformioTarget: platformioTarget, | ||
| latestStableVersion: latestStableVersion | ||
| ) | ||
| let displayName: String | ||
| if let trimmedName = candidate.deviceName?.trimmingCharacters(in: .whitespacesAndNewlines), !trimmedName.isEmpty { | ||
| displayName = trimmedName | ||
| } else { | ||
| displayName = "Connected node" | ||
| } | ||
| let current = FirmwareUpdateNotificationPolicy.normalizedVersion(currentVersion) | ||
| let latest = FirmwareUpdateNotificationPolicy.normalizedVersion(latestStableVersion) | ||
|
|
||
| return FirmwareUpdateNotice( | ||
| notificationKey: key, | ||
| deviceName: displayName, | ||
| currentVersion: current, | ||
| latestStableVersion: latest, | ||
| installMethod: candidate.installMethod | ||
| ) | ||
| } | ||
|
|
||
| static func notification( | ||
| for candidate: FirmwareUpdateNotificationCandidate, | ||
| alreadyNotified: Set<String> | ||
| ) -> Notification? { | ||
| guard let notice = notice(for: candidate), | ||
| !alreadyNotified.contains(notice.notificationKey) else { | ||
| return nil | ||
| } | ||
|
|
||
| return Notification( | ||
| id: notice.notificationKey, | ||
| title: "Firmware update available", | ||
| subtitle: notice.deviceName, | ||
| content: notice.notificationContent, | ||
| target: target, | ||
| path: path | ||
| ) | ||
| } | ||
|
|
||
| @MainActor | ||
| static func notifyIfNeeded(accessoryManager: AccessoryManager) async throws { | ||
| await refreshFirmwareDataIfStale() | ||
| try Task.checkCancellation() | ||
|
|
||
| guard let candidate = candidate(accessoryManager: accessoryManager), | ||
| let notification = notification( | ||
| for: candidate, | ||
| alreadyNotified: UserDefaults.firmwareUpdateNotificationKeySet | ||
| ) else { | ||
| return | ||
| } | ||
| try Task.checkCancellation() | ||
|
|
||
| let localNotificationManager = LocalNotificationManager() | ||
| localNotificationManager.notifications = [notification] | ||
| localNotificationManager.schedule() | ||
| UserDefaults.recordFirmwareUpdateNotificationKey(notification.id) | ||
| } | ||
|
|
||
| @MainActor | ||
| static func notice(accessoryManager: AccessoryManager) -> FirmwareUpdateNotice? { | ||
| guard let candidate = candidate(accessoryManager: accessoryManager) else { return nil } | ||
| return notice(for: candidate) | ||
| } | ||
|
|
||
| @MainActor | ||
| private static func refreshFirmwareDataIfStale() async { | ||
| guard UserDefaults.lastFirmwareAPIUpdate == .distantPast | ||
| || abs(UserDefaults.lastFirmwareAPIUpdate.timeIntervalSinceNow) > staleFirmwareAPIInterval else { | ||
| return | ||
| } | ||
|
|
||
| do { | ||
| try await MeshtasticAPI.shared.refreshFirmwareAPIData() | ||
| } catch { | ||
| Logger.services.warning("Failed to refresh firmware data before update notification check: \(error.localizedDescription, privacy: .public)") | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| private static func candidate(accessoryManager: AccessoryManager) -> FirmwareUpdateNotificationCandidate? { | ||
| guard let nodeNum = accessoryManager.activeDeviceNum, | ||
| let node = getNodeInfo(id: nodeNum, context: accessoryManager.context), | ||
| let platformioTarget = node.myInfo?.pioEnv, | ||
| let hardware = hardware(platformioTarget: platformioTarget, context: accessoryManager.context) else { | ||
| return nil | ||
| } | ||
|
|
||
| return candidate(from: FirmwareUpdateNotificationSource( | ||
| nodeNum: node.num, | ||
| deviceName: node.user?.longName ?? accessoryManager.activeConnection?.device.longName ?? accessoryManager.activeConnection?.device.name, | ||
| platformioTarget: platformioTarget, | ||
| architecture: hardware.architecture, | ||
| metadataVersion: node.metadata?.firmwareVersion, | ||
| connectedVersion: accessoryManager.connectedVersion, | ||
| latestStableVersion: latestStableFirmwareVersion(context: accessoryManager.context) | ||
| )) | ||
| } | ||
|
|
||
| @MainActor | ||
| private static func hardware(platformioTarget: String, context: ModelContext) -> DeviceHardwareEntity? { | ||
| var descriptor = FetchDescriptor<DeviceHardwareEntity>( | ||
| predicate: #Predicate { $0.platformioTarget == platformioTarget } | ||
| ) | ||
| descriptor.fetchLimit = 1 | ||
| do { | ||
| return try context.fetch(descriptor).first | ||
| } catch { | ||
| Logger.services.warning("Failed to fetch hardware for firmware update notification target \(platformioTarget, privacy: .public): \(error.localizedDescription, privacy: .public)") | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| private static func latestStableFirmwareVersion(context: ModelContext) -> String? { | ||
| let stableRawValue = ReleaseType.stable.rawValue | ||
| var descriptor = FetchDescriptor<FirmwareReleaseEntity>( | ||
| predicate: #Predicate { $0.releaseType == stableRawValue }, | ||
| sortBy: [ | ||
| SortDescriptor(\.versionMajor, order: .reverse), | ||
| SortDescriptor(\.versionMinor, order: .reverse), | ||
| SortDescriptor(\.versionPatch, order: .reverse) | ||
| ] | ||
| ) | ||
| descriptor.fetchLimit = 1 | ||
| do { | ||
| return try context.fetch(descriptor).first?.versionId | ||
| } catch { | ||
| Logger.services.warning("Failed to fetch latest stable firmware release for update notification: \(error.localizedDescription, privacy: .public)") | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.