-
-
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 1 commit
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
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
| // MARK: - FirmwareUpdateNotificationPolicy | ||
|
|
||
| import Foundation | ||
|
|
||
| enum FirmwareUpdateNotificationPolicy { | ||
| static func supportsAppOTA(architecture: String?) -> Bool { | ||
| guard let architecture = architecture.flatMap({ Architecture(rawValue: $0) }) else { | ||
| return false | ||
| } | ||
|
|
||
| switch architecture { | ||
| case .esp32, .esp32C3, .esp32S3, .esp32C6, .nrf52840: | ||
| return true | ||
| case .rp2040: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| 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,159 @@ | ||
| // MARK: - FirmwareUpdateNotifier | ||
|
|
||
| import Foundation | ||
| import OSLog | ||
| import SwiftData | ||
|
|
||
| struct FirmwareUpdateNotificationCandidate { | ||
| let nodeNum: Int64 | ||
| let deviceName: String? | ||
| let platformioTarget: String? | ||
| let supportsAppOTA: Bool | ||
| 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? | ||
| } | ||
|
|
||
| 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, | ||
| supportsAppOTA: FirmwareUpdateNotificationPolicy.supportsAppOTA(architecture: source.architecture), | ||
| currentVersion: source.metadataVersion?.isEmpty == false ? source.metadataVersion : source.connectedVersion, | ||
| latestStableVersion: source.latestStableVersion | ||
| ) | ||
| } | ||
|
|
||
| static func notification( | ||
| for candidate: FirmwareUpdateNotificationCandidate, | ||
| alreadyNotified: Set<String> | ||
| ) -> Notification? { | ||
| guard let platformioTarget = candidate.platformioTarget, | ||
| candidate.supportsAppOTA, | ||
| let currentVersion = candidate.currentVersion, | ||
| let latestStableVersion = candidate.latestStableVersion, | ||
| FirmwareUpdateNotificationPolicy.shouldNotify( | ||
| nodeNum: candidate.nodeNum, | ||
| platformioTarget: platformioTarget, | ||
| currentVersion: currentVersion, | ||
| latestStableVersion: latestStableVersion, | ||
| alreadyNotified: alreadyNotified | ||
| ) 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 Notification( | ||
| id: key, | ||
| title: "Firmware update available", | ||
| subtitle: displayName, | ||
| content: "\(displayName) is running \(current). Stable \(latest) is available.", | ||
| target: target, | ||
| path: path | ||
| ) | ||
| } | ||
|
|
||
| @MainActor | ||
| static func notifyIfNeeded(accessoryManager: AccessoryManager) async { | ||
| await refreshFirmwareDataIfStale() | ||
|
|
||
| guard let nodeNum = accessoryManager.activeDeviceNum, | ||
| let node = getNodeInfo(id: nodeNum, context: accessoryManager.context), | ||
| let platformioTarget = node.myInfo?.pioEnv, | ||
| let hardware = hardwareSupportingAppOTA(platformioTarget: platformioTarget, context: accessoryManager.context) else { | ||
| return | ||
| } | ||
|
|
||
| let candidate = 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) | ||
| )) | ||
|
|
||
| guard let notification = notification( | ||
| for: candidate, | ||
| alreadyNotified: UserDefaults.firmwareUpdateNotificationKeySet | ||
| ) else { | ||
| return | ||
| } | ||
|
|
||
| let localNotificationManager = LocalNotificationManager() | ||
| localNotificationManager.notifications = [notification] | ||
| localNotificationManager.schedule() | ||
| UserDefaults.recordFirmwareUpdateNotificationKey(notification.id) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| @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 hardwareSupportingAppOTA(platformioTarget: String, context: ModelContext) -> DeviceHardwareEntity? { | ||
| var descriptor = FetchDescriptor<DeviceHardwareEntity>( | ||
| predicate: #Predicate { $0.platformioTarget == platformioTarget } | ||
| ) | ||
| descriptor.fetchLimit = 1 | ||
| guard let hardware = try? context.fetch(descriptor).first, | ||
| FirmwareUpdateNotificationPolicy.supportsAppOTA(architecture: hardware.architecture) else { | ||
| return nil | ||
| } | ||
| return hardware | ||
| } | ||
|
|
||
| @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 | ||
| return try? context.fetch(descriptor).first?.versionId | ||
| } | ||
| } | ||
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
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.