mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor: migrate from Hilt to Koin and expand KMP common modules (#4746)
Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
parent
a5390a80e7
commit
875cf1cff2
440 changed files with 3738 additions and 3508 deletions
|
|
@ -17,9 +17,7 @@
|
|||
package org.meshtastic.feature.firmware
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import co.touchlab.kermit.Logger
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.head
|
||||
|
|
@ -31,14 +29,15 @@ import kotlinx.coroutines.CancellationException
|
|||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.koin.core.annotation.Single
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.common.util.toPlatformUri
|
||||
import org.meshtastic.core.model.DeviceHardware
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipInputStream
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val DOWNLOAD_BUFFER_SIZE = 8192
|
||||
|
||||
|
|
@ -46,15 +45,11 @@ private const val DOWNLOAD_BUFFER_SIZE = 8192
|
|||
* Helper class to handle file operations related to firmware updates, such as downloading, copying from URI, and
|
||||
* extracting specific files from Zip archives.
|
||||
*/
|
||||
class FirmwareFileHandler
|
||||
@Inject
|
||||
constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
private val client: HttpClient,
|
||||
) {
|
||||
@Single
|
||||
class AndroidFirmwareFileHandler(private val context: Context, private val client: HttpClient) : FirmwareFileHandler {
|
||||
private val tempDir = File(context.cacheDir, "firmware_update")
|
||||
|
||||
fun cleanupAllTemporaryFiles() {
|
||||
override fun cleanupAllTemporaryFiles() {
|
||||
runCatching {
|
||||
if (tempDir.exists()) {
|
||||
tempDir.deleteRecursively()
|
||||
|
|
@ -64,7 +59,7 @@ constructor(
|
|||
.onFailure { e -> Logger.w(e) { "Failed to cleanup temp directory" } }
|
||||
}
|
||||
|
||||
suspend fun checkUrlExists(url: String): Boolean = withContext(Dispatchers.IO) {
|
||||
override suspend fun checkUrlExists(url: String): Boolean = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
client.head(url).status.isSuccess()
|
||||
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
|
||||
|
|
@ -73,7 +68,7 @@ constructor(
|
|||
}
|
||||
}
|
||||
|
||||
suspend fun downloadFile(url: String, fileName: String, onProgress: (Float) -> Unit): File? =
|
||||
override suspend fun downloadFile(url: String, fileName: String, onProgress: (Float) -> Unit): String? =
|
||||
withContext(Dispatchers.IO) {
|
||||
val response =
|
||||
try {
|
||||
|
|
@ -93,10 +88,10 @@ constructor(
|
|||
|
||||
if (!tempDir.exists()) tempDir.mkdirs()
|
||||
|
||||
val targetFile = File(tempDir, fileName)
|
||||
val targetFile = java.io.File(tempDir, fileName)
|
||||
|
||||
body.toInputStream().use { input ->
|
||||
FileOutputStream(targetFile).use { output ->
|
||||
java.io.FileOutputStream(targetFile).use { output ->
|
||||
val buffer = ByteArray(DOWNLOAD_BUFFER_SIZE)
|
||||
var bytesRead: Int
|
||||
var totalBytesRead = 0L
|
||||
|
|
@ -116,15 +111,16 @@ constructor(
|
|||
}
|
||||
}
|
||||
}
|
||||
targetFile
|
||||
targetFile.absolutePath
|
||||
}
|
||||
|
||||
suspend fun extractFirmware(
|
||||
zipFile: File,
|
||||
override suspend fun extractFirmwareFromZip(
|
||||
zipFilePath: String,
|
||||
hardware: DeviceHardware,
|
||||
fileExtension: String,
|
||||
preferredFilename: String? = null,
|
||||
): File? = withContext(Dispatchers.IO) {
|
||||
preferredFilename: String?,
|
||||
): String? = withContext(Dispatchers.IO) {
|
||||
val zipFile = java.io.File(zipFilePath)
|
||||
val target = hardware.platformioTarget.ifEmpty { hardware.hwModelSlug }
|
||||
if (target.isEmpty() && preferredFilename == null) return@withContext null
|
||||
|
||||
|
|
@ -153,21 +149,21 @@ constructor(
|
|||
matchingEntries.add(entry to outFile)
|
||||
|
||||
if (preferredFilenameLower != null) {
|
||||
return@withContext outFile
|
||||
return@withContext outFile.absolutePath
|
||||
}
|
||||
}
|
||||
entry = zipInput.nextEntry
|
||||
}
|
||||
}
|
||||
matchingEntries.minByOrNull { it.first.name.length }?.second
|
||||
matchingEntries.minByOrNull { it.first.name.length }?.second?.absolutePath
|
||||
}
|
||||
|
||||
suspend fun extractFirmware(
|
||||
uri: Uri,
|
||||
override suspend fun extractFirmware(
|
||||
uri: CommonUri,
|
||||
hardware: DeviceHardware,
|
||||
fileExtension: String,
|
||||
preferredFilename: String? = null,
|
||||
): File? = withContext(Dispatchers.IO) {
|
||||
preferredFilename: String?,
|
||||
): String? = withContext(Dispatchers.IO) {
|
||||
val target = hardware.platformioTarget.ifEmpty { hardware.hwModelSlug }
|
||||
if (target.isEmpty() && preferredFilename == null) return@withContext null
|
||||
|
||||
|
|
@ -178,7 +174,8 @@ constructor(
|
|||
if (!tempDir.exists()) tempDir.mkdirs()
|
||||
|
||||
try {
|
||||
val inputStream = context.contentResolver.openInputStream(uri) ?: return@withContext null
|
||||
val platformUri = uri.toPlatformUri() as android.net.Uri
|
||||
val inputStream = context.contentResolver.openInputStream(platformUri) ?: return@withContext null
|
||||
ZipInputStream(inputStream).use { zipInput ->
|
||||
var entry = zipInput.nextEntry
|
||||
while (entry != null) {
|
||||
|
|
@ -198,7 +195,7 @@ constructor(
|
|||
matchingEntries.add(entry to outFile)
|
||||
|
||||
if (preferredFilenameLower != null) {
|
||||
return@withContext outFile
|
||||
return@withContext outFile.absolutePath
|
||||
}
|
||||
}
|
||||
entry = zipInput.nextEntry
|
||||
|
|
@ -208,7 +205,17 @@ constructor(
|
|||
Logger.w(e) { "Failed to extract firmware from URI" }
|
||||
return@withContext null
|
||||
}
|
||||
matchingEntries.minByOrNull { it.first.name.length }?.second
|
||||
matchingEntries.minByOrNull { it.first.name.length }?.second?.absolutePath
|
||||
}
|
||||
|
||||
override suspend fun getFileSize(path: String): Long = withContext(Dispatchers.IO) {
|
||||
val file = File(path)
|
||||
if (file.exists()) file.length() else 0L
|
||||
}
|
||||
|
||||
override suspend fun deleteFile(path: String) = withContext(Dispatchers.IO) {
|
||||
val file = File(path)
|
||||
if (file.exists()) file.delete()
|
||||
}
|
||||
|
||||
private fun isValidFirmwareFile(filename: String, target: String, fileExtension: String): Boolean {
|
||||
|
|
@ -218,22 +225,25 @@ constructor(
|
|||
(regex.matches(filename) || filename.startsWith("$target-") || filename.startsWith("$target."))
|
||||
}
|
||||
|
||||
suspend fun copyFileToUri(sourceFile: File, destinationUri: Uri) = withContext(Dispatchers.IO) {
|
||||
val inputStream = FileInputStream(sourceFile)
|
||||
val outputStream =
|
||||
context.contentResolver.openOutputStream(destinationUri)
|
||||
?: throw IOException("Cannot open content URI for writing")
|
||||
override suspend fun copyFileToUri(sourcePath: String, destinationUri: CommonUri): Long =
|
||||
withContext(Dispatchers.IO) {
|
||||
val inputStream = java.io.FileInputStream(java.io.File(sourcePath))
|
||||
val outputStream =
|
||||
context.contentResolver.openOutputStream(destinationUri.toPlatformUri() as android.net.Uri)
|
||||
?: throw IOException("Cannot open content URI for writing")
|
||||
|
||||
inputStream.use { input -> outputStream.use { output -> input.copyTo(output) } }
|
||||
}
|
||||
inputStream.use { input -> outputStream.use { output -> input.copyTo(output) } }
|
||||
}
|
||||
|
||||
suspend fun copyUriToUri(sourceUri: Uri, destinationUri: Uri) = withContext(Dispatchers.IO) {
|
||||
val inputStream =
|
||||
context.contentResolver.openInputStream(sourceUri) ?: throw IOException("Cannot open source URI")
|
||||
val outputStream =
|
||||
context.contentResolver.openOutputStream(destinationUri)
|
||||
?: throw IOException("Cannot open destination URI")
|
||||
override suspend fun copyUriToUri(sourceUri: CommonUri, destinationUri: CommonUri): Long =
|
||||
withContext(Dispatchers.IO) {
|
||||
val inputStream =
|
||||
context.contentResolver.openInputStream(sourceUri.toPlatformUri() as android.net.Uri)
|
||||
?: throw IOException("Cannot open source URI")
|
||||
val outputStream =
|
||||
context.contentResolver.openOutputStream(destinationUri.toPlatformUri() as android.net.Uri)
|
||||
?: throw IOException("Cannot open destination URI")
|
||||
|
||||
inputStream.use { input -> outputStream.use { output -> input.copyTo(output) } }
|
||||
}
|
||||
inputStream.use { input -> outputStream.use { output -> input.copyTo(output) } }
|
||||
}
|
||||
}
|
||||
|
|
@ -16,8 +16,9 @@
|
|||
*/
|
||||
package org.meshtastic.feature.firmware
|
||||
|
||||
import android.net.Uri
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import org.koin.core.annotation.Single
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.database.entity.FirmwareRelease
|
||||
import org.meshtastic.core.model.DeviceHardware
|
||||
import org.meshtastic.core.repository.RadioPrefs
|
||||
|
|
@ -25,29 +26,24 @@ import org.meshtastic.core.repository.isBle
|
|||
import org.meshtastic.core.repository.isSerial
|
||||
import org.meshtastic.core.repository.isTcp
|
||||
import org.meshtastic.feature.firmware.ota.Esp32OtaUpdateHandler
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/** Orchestrates the firmware update process by choosing the correct handler. */
|
||||
@Singleton
|
||||
class FirmwareUpdateManager
|
||||
@Inject
|
||||
constructor(
|
||||
@Single
|
||||
class AndroidFirmwareUpdateManager(
|
||||
private val radioPrefs: RadioPrefs,
|
||||
private val nordicDfuHandler: NordicDfuHandler,
|
||||
private val usbUpdateHandler: UsbUpdateHandler,
|
||||
private val esp32OtaUpdateHandler: Esp32OtaUpdateHandler,
|
||||
) {
|
||||
) : FirmwareUpdateManager {
|
||||
|
||||
/** Start the update process based on the current connection and hardware. */
|
||||
suspend fun startUpdate(
|
||||
override suspend fun startUpdate(
|
||||
release: FirmwareRelease,
|
||||
hardware: DeviceHardware,
|
||||
address: String,
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
firmwareUri: Uri? = null,
|
||||
): File? {
|
||||
firmwareUri: CommonUri?,
|
||||
): String? {
|
||||
val handler = getHandler(hardware)
|
||||
val target = getTarget(address)
|
||||
|
||||
|
|
@ -60,7 +56,7 @@ constructor(
|
|||
)
|
||||
}
|
||||
|
||||
fun dfuProgressFlow(): Flow<DfuInternalState> = nordicDfuHandler.progressFlow()
|
||||
override fun dfuProgressFlow(): Flow<DfuInternalState> = nordicDfuHandler.progressFlow()
|
||||
|
||||
private fun getHandler(hardware: DeviceHardware): FirmwareUpdateHandler = when {
|
||||
radioPrefs.isSerial() -> {
|
||||
|
|
@ -23,18 +23,16 @@ import android.content.IntentFilter
|
|||
import android.hardware.usb.UsbManager
|
||||
import android.os.Build
|
||||
import co.touchlab.kermit.Logger
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import org.koin.core.annotation.Single
|
||||
|
||||
/** Manages USB-related interactions for firmware updates. */
|
||||
@Singleton
|
||||
class UsbManager @Inject constructor(@ApplicationContext private val context: Context) {
|
||||
@Single
|
||||
class AndroidFirmwareUsbManager(private val context: Context) : FirmwareUsbManager {
|
||||
/** Observe when a USB device is detached. */
|
||||
fun deviceDetachFlow(): Flow<Unit> = callbackFlow {
|
||||
override fun deviceDetachFlow(): Flow<Unit> = callbackFlow {
|
||||
val receiver =
|
||||
object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
|
|
@ -50,11 +50,13 @@ class FirmwareDfuService : DfuBaseService() {
|
|||
}
|
||||
|
||||
override fun getNotificationTarget(): Class<out Activity>? = try {
|
||||
// Best effort to find the main activity
|
||||
// Best effort to find the main activity dynamically
|
||||
val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
|
||||
val className = launchIntent?.component?.className ?: "org.meshtastic.app.MainActivity"
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
Class.forName("com.geeksville.mesh.MainActivity") as Class<out Activity>
|
||||
} catch (_: ClassNotFoundException) {
|
||||
null
|
||||
Class.forName(className) as Class<out Activity>
|
||||
} catch (_: Exception) {
|
||||
Activity::class.java
|
||||
}
|
||||
|
||||
override fun isDebug(): Boolean = isDebugFlag
|
||||
|
|
@ -17,18 +17,18 @@
|
|||
package org.meshtastic.feature.firmware
|
||||
|
||||
import co.touchlab.kermit.Logger
|
||||
import org.koin.core.annotation.Single
|
||||
import org.meshtastic.core.database.entity.FirmwareRelease
|
||||
import org.meshtastic.core.model.DeviceHardware
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
|
||||
/** Retrieves firmware files, either by direct download or by extracting from a release asset. */
|
||||
class FirmwareRetriever @Inject constructor(private val fileHandler: FirmwareFileHandler) {
|
||||
@Single
|
||||
class FirmwareRetriever(private val fileHandler: FirmwareFileHandler) {
|
||||
suspend fun retrieveOtaFirmware(
|
||||
release: FirmwareRelease,
|
||||
hardware: DeviceHardware,
|
||||
onProgress: (Float) -> Unit,
|
||||
): File? = retrieve(
|
||||
): String? = retrieve(
|
||||
release = release,
|
||||
hardware = hardware,
|
||||
onProgress = onProgress,
|
||||
|
|
@ -40,7 +40,7 @@ class FirmwareRetriever @Inject constructor(private val fileHandler: FirmwareFil
|
|||
release: FirmwareRelease,
|
||||
hardware: DeviceHardware,
|
||||
onProgress: (Float) -> Unit,
|
||||
): File? = retrieve(
|
||||
): String? = retrieve(
|
||||
release = release,
|
||||
hardware = hardware,
|
||||
onProgress = onProgress,
|
||||
|
|
@ -52,7 +52,7 @@ class FirmwareRetriever @Inject constructor(private val fileHandler: FirmwareFil
|
|||
release: FirmwareRelease,
|
||||
hardware: DeviceHardware,
|
||||
onProgress: (Float) -> Unit,
|
||||
): File? {
|
||||
): String? {
|
||||
val mcu = hardware.architecture.replace("-", "")
|
||||
val otaFilename = "mt-$mcu-ota.bin"
|
||||
retrieve(
|
||||
|
|
@ -84,7 +84,7 @@ class FirmwareRetriever @Inject constructor(private val fileHandler: FirmwareFil
|
|||
fileSuffix: String,
|
||||
internalFileExtension: String,
|
||||
preferredFilename: String? = null,
|
||||
): File? {
|
||||
): String? {
|
||||
val version = release.id.removePrefix("v")
|
||||
val target = hardware.platformioTarget.ifEmpty { hardware.hwModelSlug }
|
||||
val filename = preferredFilename ?: "firmware-$target-$version$fileSuffix"
|
||||
|
|
@ -105,7 +105,7 @@ class FirmwareRetriever @Inject constructor(private val fileHandler: FirmwareFil
|
|||
val zipUrl = getDeviceFirmwareUrl(release.zipUrl, hardware.architecture)
|
||||
val downloadedZip = fileHandler.downloadFile(zipUrl, "firmware_release.zip", onProgress)
|
||||
return downloadedZip?.let {
|
||||
fileHandler.extractFirmware(it, hardware, internalFileExtension, preferredFilename)
|
||||
fileHandler.extractFirmwareFromZip(it, hardware, internalFileExtension, preferredFilename)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -79,15 +79,14 @@ import androidx.compose.ui.platform.LocalView
|
|||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.net.toUri
|
||||
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.navigation.NavController
|
||||
import coil3.compose.AsyncImage
|
||||
import coil3.request.ImageRequest
|
||||
import coil3.request.crossfade
|
||||
import com.mikepenz.markdown.m3.Markdown
|
||||
import kotlinx.coroutines.delay
|
||||
import org.jetbrains.compose.resources.stringResource
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.database.entity.FirmwareRelease
|
||||
import org.meshtastic.core.database.entity.FirmwareReleaseType
|
||||
import org.meshtastic.core.model.DeviceHardware
|
||||
|
|
@ -153,11 +152,7 @@ private const val CYCLE_DELAY_MS = 4500L
|
|||
|
||||
@Composable
|
||||
@Suppress("LongMethod")
|
||||
fun FirmwareUpdateScreen(
|
||||
navController: NavController,
|
||||
modifier: Modifier = Modifier,
|
||||
viewModel: FirmwareUpdateViewModel = hiltViewModel(),
|
||||
) {
|
||||
fun FirmwareUpdateScreen(onNavigateUp: () -> Unit, viewModel: FirmwareUpdateViewModel, modifier: Modifier = Modifier) {
|
||||
val state by viewModel.state.collectAsStateWithLifecycle()
|
||||
val selectedReleaseType by viewModel.selectedReleaseType.collectAsStateWithLifecycle()
|
||||
val deviceHardware by viewModel.deviceHardware.collectAsStateWithLifecycle()
|
||||
|
|
@ -165,21 +160,19 @@ fun FirmwareUpdateScreen(
|
|||
val selectedRelease by viewModel.selectedRelease.collectAsStateWithLifecycle()
|
||||
|
||||
var showExitConfirmation by remember { mutableStateOf(false) }
|
||||
|
||||
val getFileLauncher =
|
||||
val filePickerLauncher =
|
||||
rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
|
||||
uri?.let { viewModel.startUpdateFromFile(it) }
|
||||
uri?.let { viewModel.startUpdateFromFile(CommonUri(it)) }
|
||||
}
|
||||
|
||||
val saveFileLauncher =
|
||||
val createDocumentLauncher =
|
||||
rememberLauncherForActivityResult(
|
||||
ActivityResultContracts.CreateDocument("application/octet-stream"),
|
||||
) { uri: Uri? ->
|
||||
uri?.let { viewModel.saveDfuFile(it) }
|
||||
uri?.let { viewModel.saveDfuFile(CommonUri(it)) }
|
||||
}
|
||||
|
||||
val actions =
|
||||
remember(viewModel, navController, state) {
|
||||
remember(viewModel, onNavigateUp, state) {
|
||||
FirmwareUpdateActions(
|
||||
onReleaseTypeSelect = viewModel::setReleaseType,
|
||||
onStartUpdate = viewModel::startUpdate,
|
||||
|
|
@ -190,16 +183,16 @@ fun FirmwareUpdateScreen(
|
|||
readyState.updateMethod is FirmwareUpdateMethod.Ble ||
|
||||
readyState.updateMethod is FirmwareUpdateMethod.Wifi
|
||||
) {
|
||||
getFileLauncher.launch("*/*")
|
||||
filePickerLauncher.launch("*/*")
|
||||
} else if (readyState.updateMethod is FirmwareUpdateMethod.Usb) {
|
||||
getFileLauncher.launch("*/*")
|
||||
filePickerLauncher.launch("*/*")
|
||||
}
|
||||
}
|
||||
},
|
||||
onSaveFile = { fileName -> saveFileLauncher.launch(fileName) },
|
||||
onSaveFile = { fileName -> createDocumentLauncher.launch(fileName) },
|
||||
onRetry = viewModel::checkForUpdates,
|
||||
onCancel = { showExitConfirmation = true },
|
||||
onDone = { navController.navigateUp() },
|
||||
onDone = { onNavigateUp() },
|
||||
onDismissBootloaderWarning = viewModel::dismissBootloaderWarningForCurrentDevice,
|
||||
)
|
||||
}
|
||||
|
|
@ -217,7 +210,7 @@ fun FirmwareUpdateScreen(
|
|||
onConfirm = {
|
||||
showExitConfirmation = false
|
||||
viewModel.cancelUpdate()
|
||||
navController.navigateUp()
|
||||
onNavigateUp()
|
||||
},
|
||||
dismissText = stringResource(Res.string.back),
|
||||
)
|
||||
|
|
@ -225,7 +218,7 @@ fun FirmwareUpdateScreen(
|
|||
|
||||
FirmwareUpdateScaffold(
|
||||
modifier = modifier,
|
||||
navController = navController,
|
||||
onNavigateUp = onNavigateUp,
|
||||
state = state,
|
||||
selectedReleaseType = selectedReleaseType,
|
||||
actions = actions,
|
||||
|
|
@ -237,7 +230,7 @@ fun FirmwareUpdateScreen(
|
|||
|
||||
@Composable
|
||||
private fun FirmwareUpdateScaffold(
|
||||
navController: NavController,
|
||||
onNavigateUp: () -> Unit,
|
||||
state: FirmwareUpdateState,
|
||||
selectedReleaseType: FirmwareReleaseType,
|
||||
actions: FirmwareUpdateActions,
|
||||
|
|
@ -252,7 +245,7 @@ private fun FirmwareUpdateScaffold(
|
|||
CenterAlignedTopAppBar(
|
||||
title = { Text(stringResource(Res.string.firmware_update_title)) },
|
||||
navigationIcon = {
|
||||
IconButton(onClick = { navController.navigateUp() }) {
|
||||
IconButton(onClick = { onNavigateUp() }) {
|
||||
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = stringResource(Res.string.back))
|
||||
}
|
||||
},
|
||||
|
|
@ -17,10 +17,8 @@
|
|||
package org.meshtastic.feature.firmware
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import co.touchlab.kermit.Logger
|
||||
import co.touchlab.kermit.Severity
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
|
@ -31,6 +29,9 @@ import no.nordicsemi.android.dfu.DfuProgressListenerAdapter
|
|||
import no.nordicsemi.android.dfu.DfuServiceInitiator
|
||||
import no.nordicsemi.android.dfu.DfuServiceListenerHelper
|
||||
import org.jetbrains.compose.resources.getString
|
||||
import org.koin.core.annotation.Single
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.common.util.toPlatformUri
|
||||
import org.meshtastic.core.database.entity.FirmwareRelease
|
||||
import org.meshtastic.core.model.DeviceHardware
|
||||
import org.meshtastic.core.model.RadioController
|
||||
|
|
@ -39,8 +40,6 @@ import org.meshtastic.core.resources.firmware_update_downloading_percent
|
|||
import org.meshtastic.core.resources.firmware_update_nordic_failed
|
||||
import org.meshtastic.core.resources.firmware_update_not_found_in_release
|
||||
import org.meshtastic.core.resources.firmware_update_starting_service
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val SCAN_TIMEOUT = 5000L
|
||||
private const val PACKETS_BEFORE_PRN = 8
|
||||
|
|
@ -48,11 +47,10 @@ private const val PERCENT_MAX = 100
|
|||
private const val PREPARE_DATA_DELAY = 400L
|
||||
|
||||
/** Handles Over-the-Air (OTA) firmware updates for nRF52-based devices using the Nordic DFU library. */
|
||||
class NordicDfuHandler
|
||||
@Inject
|
||||
constructor(
|
||||
@Single
|
||||
class NordicDfuHandler(
|
||||
private val firmwareRetriever: FirmwareRetriever,
|
||||
@ApplicationContext private val context: Context,
|
||||
private val context: Context,
|
||||
private val radioController: RadioController,
|
||||
) : FirmwareUpdateHandler {
|
||||
|
||||
|
|
@ -61,8 +59,8 @@ constructor(
|
|||
hardware: DeviceHardware,
|
||||
target: String, // Bluetooth address
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
firmwareUri: Uri?,
|
||||
): File? =
|
||||
firmwareUri: CommonUri?,
|
||||
): String? =
|
||||
try {
|
||||
val downloadingMsg =
|
||||
getString(Res.string.firmware_update_downloading_percent, 0)
|
||||
|
|
@ -90,7 +88,7 @@ constructor(
|
|||
updateState(FirmwareUpdateState.Error(errorMsg))
|
||||
null
|
||||
} else {
|
||||
initiateDfu(target, hardware, Uri.fromFile(firmwareFile), updateState)
|
||||
initiateDfu(target, hardware, CommonUri.parse("file://$firmwareFile"), updateState)
|
||||
firmwareFile
|
||||
}
|
||||
}
|
||||
|
|
@ -106,7 +104,7 @@ constructor(
|
|||
private suspend fun initiateDfu(
|
||||
address: String,
|
||||
deviceHardware: DeviceHardware,
|
||||
firmwareUri: Uri,
|
||||
firmwareUri: CommonUri,
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
) {
|
||||
val startingMsg = getString(Res.string.firmware_update_starting_service)
|
||||
|
|
@ -127,7 +125,7 @@ constructor(
|
|||
.setPacketsReceiptNotificationsEnabled(true)
|
||||
.setScanTimeout(SCAN_TIMEOUT)
|
||||
.setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(true)
|
||||
.setZip(firmwareUri)
|
||||
.setZip(firmwareUri.toPlatformUri() as android.net.Uri)
|
||||
.start(context, FirmwareDfuService::class.java)
|
||||
}
|
||||
|
||||
|
|
@ -215,36 +213,3 @@ constructor(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
sealed interface DfuInternalState {
|
||||
val address: String
|
||||
|
||||
data class Connecting(override val address: String) : DfuInternalState
|
||||
|
||||
data class Connected(override val address: String) : DfuInternalState
|
||||
|
||||
data class Starting(override val address: String) : DfuInternalState
|
||||
|
||||
data class EnablingDfuMode(override val address: String) : DfuInternalState
|
||||
|
||||
data class Progress(
|
||||
override val address: String,
|
||||
val percent: Int,
|
||||
val speed: Float,
|
||||
val avgSpeed: Float,
|
||||
val currentPart: Int,
|
||||
val partsTotal: Int,
|
||||
) : DfuInternalState
|
||||
|
||||
data class Validating(override val address: String) : DfuInternalState
|
||||
|
||||
data class Disconnecting(override val address: String) : DfuInternalState
|
||||
|
||||
data class Disconnected(override val address: String) : DfuInternalState
|
||||
|
||||
data class Completed(override val address: String) : DfuInternalState
|
||||
|
||||
data class Aborted(override val address: String) : DfuInternalState
|
||||
|
||||
data class Error(override val address: String, val message: String?) : DfuInternalState
|
||||
}
|
||||
|
|
@ -16,11 +16,12 @@
|
|||
*/
|
||||
package org.meshtastic.feature.firmware
|
||||
|
||||
import android.net.Uri
|
||||
import co.touchlab.kermit.Logger
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.delay
|
||||
import org.jetbrains.compose.resources.getString
|
||||
import org.koin.core.annotation.Single
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.database.entity.FirmwareRelease
|
||||
import org.meshtastic.core.model.DeviceHardware
|
||||
import org.meshtastic.core.model.RadioController
|
||||
|
|
@ -30,16 +31,13 @@ import org.meshtastic.core.resources.firmware_update_downloading_percent
|
|||
import org.meshtastic.core.resources.firmware_update_rebooting
|
||||
import org.meshtastic.core.resources.firmware_update_retrieval_failed
|
||||
import org.meshtastic.core.resources.firmware_update_usb_failed
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val REBOOT_DELAY = 5000L
|
||||
private const val PERCENT_MAX = 100
|
||||
|
||||
/** Handles firmware updates via USB Mass Storage (UF2). */
|
||||
class UsbUpdateHandler
|
||||
@Inject
|
||||
constructor(
|
||||
@Single
|
||||
class UsbUpdateHandler(
|
||||
private val firmwareRetriever: FirmwareRetriever,
|
||||
private val radioController: RadioController,
|
||||
private val nodeRepository: NodeRepository,
|
||||
|
|
@ -50,8 +48,8 @@ constructor(
|
|||
hardware: DeviceHardware,
|
||||
target: String, // Unused for USB
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
firmwareUri: Uri?,
|
||||
): File? =
|
||||
firmwareUri: CommonUri?,
|
||||
): String? =
|
||||
try {
|
||||
val downloadingMsg =
|
||||
getString(Res.string.firmware_update_downloading_percent, 0)
|
||||
|
|
@ -91,7 +89,7 @@ constructor(
|
|||
radioController.rebootToDfu(myNodeNum)
|
||||
delay(REBOOT_DELAY)
|
||||
|
||||
updateState(FirmwareUpdateState.AwaitingFileSave(firmwareFile, firmwareFile.name))
|
||||
updateState(FirmwareUpdateState.AwaitingFileSave(firmwareFile, java.io.File(firmwareFile).name))
|
||||
firmwareFile
|
||||
}
|
||||
}
|
||||
|
|
@ -17,9 +17,7 @@
|
|||
package org.meshtastic.feature.firmware.ota
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import co.touchlab.kermit.Logger
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
|
@ -27,9 +25,12 @@ import kotlinx.coroutines.delay
|
|||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.jetbrains.compose.resources.getString
|
||||
import org.koin.core.annotation.Single
|
||||
import org.meshtastic.core.ble.BleConnectionFactory
|
||||
import org.meshtastic.core.ble.BleScanner
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.common.util.nowMillis
|
||||
import org.meshtastic.core.common.util.toPlatformUri
|
||||
import org.meshtastic.core.database.entity.FirmwareRelease
|
||||
import org.meshtastic.core.model.DeviceHardware
|
||||
import org.meshtastic.core.model.RadioController
|
||||
|
|
@ -38,10 +39,10 @@ import org.meshtastic.core.resources.Res
|
|||
import org.meshtastic.core.resources.firmware_update_connecting_attempt
|
||||
import org.meshtastic.core.resources.firmware_update_downloading_percent
|
||||
import org.meshtastic.core.resources.firmware_update_erasing
|
||||
import org.meshtastic.core.resources.firmware_update_extracting
|
||||
import org.meshtastic.core.resources.firmware_update_hash_rejected
|
||||
import org.meshtastic.core.resources.firmware_update_loading
|
||||
import org.meshtastic.core.resources.firmware_update_not_found_in_release
|
||||
import org.meshtastic.core.resources.firmware_update_ota_failed
|
||||
import org.meshtastic.core.resources.firmware_update_retrieval_failed
|
||||
import org.meshtastic.core.resources.firmware_update_starting_ota
|
||||
import org.meshtastic.core.resources.firmware_update_uploading
|
||||
import org.meshtastic.core.resources.firmware_update_waiting_reboot
|
||||
|
|
@ -49,8 +50,6 @@ import org.meshtastic.feature.firmware.FirmwareRetriever
|
|||
import org.meshtastic.feature.firmware.FirmwareUpdateHandler
|
||||
import org.meshtastic.feature.firmware.FirmwareUpdateState
|
||||
import org.meshtastic.feature.firmware.ProgressState
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val RETRY_DELAY = 2000L
|
||||
private const val PERCENT_MAX = 100
|
||||
|
|
@ -68,15 +67,14 @@ private const val GATT_RELEASE_DELAY_MS = 1000L
|
|||
* UnifiedOtaProtocol.
|
||||
*/
|
||||
@Suppress("TooManyFunctions")
|
||||
class Esp32OtaUpdateHandler
|
||||
@Inject
|
||||
constructor(
|
||||
@Single
|
||||
class Esp32OtaUpdateHandler(
|
||||
private val firmwareRetriever: FirmwareRetriever,
|
||||
private val radioController: RadioController,
|
||||
private val nodeRepository: NodeRepository,
|
||||
private val bleScanner: BleScanner,
|
||||
private val bleConnectionFactory: BleConnectionFactory,
|
||||
@ApplicationContext private val context: Context,
|
||||
private val context: Context,
|
||||
) : FirmwareUpdateHandler {
|
||||
|
||||
/** Entry point for FirmwareUpdateHandler interface. Decides between BLE and WiFi based on target format. */
|
||||
|
|
@ -85,8 +83,8 @@ constructor(
|
|||
hardware: DeviceHardware,
|
||||
target: String,
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
firmwareUri: Uri?,
|
||||
): File? = if (target.contains(":")) {
|
||||
firmwareUri: CommonUri?,
|
||||
): String? = if (target.contains(":")) {
|
||||
startBleUpdate(release, hardware, target, updateState, firmwareUri)
|
||||
} else {
|
||||
startWifiUpdate(release, hardware, target, updateState, firmwareUri)
|
||||
|
|
@ -97,8 +95,8 @@ constructor(
|
|||
hardware: DeviceHardware,
|
||||
address: String,
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
firmwareUri: Uri? = null,
|
||||
): File? = performUpdate(
|
||||
firmwareUri: CommonUri? = null,
|
||||
): String? = performUpdate(
|
||||
release = release,
|
||||
hardware = hardware,
|
||||
updateState = updateState,
|
||||
|
|
@ -113,8 +111,8 @@ constructor(
|
|||
hardware: DeviceHardware,
|
||||
deviceIp: String,
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
firmwareUri: Uri? = null,
|
||||
): File? = performUpdate(
|
||||
firmwareUri: CommonUri? = null,
|
||||
): String? = performUpdate(
|
||||
release = release,
|
||||
hardware = hardware,
|
||||
updateState = updateState,
|
||||
|
|
@ -128,18 +126,18 @@ constructor(
|
|||
release: FirmwareRelease,
|
||||
hardware: DeviceHardware,
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
firmwareUri: Uri?,
|
||||
firmwareUri: CommonUri?,
|
||||
transportFactory: () -> UnifiedOtaProtocol,
|
||||
rebootMode: Int,
|
||||
connectionAttempts: Int,
|
||||
): File? = try {
|
||||
): String? = try {
|
||||
withContext(Dispatchers.IO) {
|
||||
// Step 1: Get firmware file
|
||||
val firmwareFile =
|
||||
obtainFirmwareFile(release, hardware, firmwareUri, updateState) ?: return@withContext null
|
||||
|
||||
// Step 2: Calculate Hash and Trigger Reboot
|
||||
val sha256Bytes = FirmwareHashUtil.calculateSha256Bytes(firmwareFile)
|
||||
val sha256Bytes = FirmwareHashUtil.calculateSha256Bytes(java.io.File(firmwareFile))
|
||||
val sha256Hash = FirmwareHashUtil.bytesToHex(sha256Bytes)
|
||||
Logger.i { "ESP32 OTA: Firmware hash: $sha256Hash" }
|
||||
triggerRebootOta(rebootMode, sha256Bytes)
|
||||
|
|
@ -180,11 +178,12 @@ constructor(
|
|||
null
|
||||
}
|
||||
|
||||
@Suppress("UnusedPrivateMember")
|
||||
private suspend fun downloadFirmware(
|
||||
release: FirmwareRelease,
|
||||
hardware: DeviceHardware,
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
): File? {
|
||||
): String? {
|
||||
val downloadingMsg =
|
||||
getString(Res.string.firmware_update_downloading_percent, 0).replace(Regex(":?\\s*%1\\\$d%?"), "").trim()
|
||||
updateState(FirmwareUpdateState.Downloading(ProgressState(message = downloadingMsg, progress = 0f)))
|
||||
|
|
@ -198,12 +197,14 @@ constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private suspend fun getFirmwareFromUri(uri: Uri): File? = withContext(Dispatchers.IO) {
|
||||
val inputStream = context.contentResolver.openInputStream(uri) ?: return@withContext null
|
||||
val tempFile = File(context.cacheDir, "firmware_update/ota_firmware.bin")
|
||||
private suspend fun getFirmwareFromUri(uri: CommonUri): String? = withContext(Dispatchers.IO) {
|
||||
val inputStream =
|
||||
context.contentResolver.openInputStream(uri.toPlatformUri() as android.net.Uri)
|
||||
?: return@withContext null
|
||||
val tempFile = java.io.File(context.cacheDir, "firmware_update/ota_firmware.bin")
|
||||
tempFile.parentFile?.mkdirs()
|
||||
inputStream.use { input -> tempFile.outputStream().use { output -> input.copyTo(output) } }
|
||||
tempFile
|
||||
tempFile.absolutePath
|
||||
}
|
||||
|
||||
private fun triggerRebootOta(mode: Int, hash: ByteArray?) {
|
||||
|
|
@ -227,24 +228,37 @@ constructor(
|
|||
private suspend fun obtainFirmwareFile(
|
||||
release: FirmwareRelease,
|
||||
hardware: DeviceHardware,
|
||||
firmwareUri: Uri?,
|
||||
firmwareUri: CommonUri?,
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
): File? {
|
||||
val firmwareFile =
|
||||
if (firmwareUri != null) {
|
||||
val loadingMsg = getString(Res.string.firmware_update_loading)
|
||||
updateState(FirmwareUpdateState.Processing(ProgressState(loadingMsg)))
|
||||
getFirmwareFromUri(firmwareUri)
|
||||
} else {
|
||||
downloadFirmware(release, hardware, updateState)
|
||||
}
|
||||
): String? {
|
||||
val downloadingMsg =
|
||||
getString(Res.string.firmware_update_downloading_percent, 0).replace(Regex(":?\\s*%1\\\$d%?"), "").trim()
|
||||
|
||||
if (firmwareFile == null) {
|
||||
val retrievalFailedMsg = getString(Res.string.firmware_update_retrieval_failed)
|
||||
updateState(FirmwareUpdateState.Error(retrievalFailedMsg))
|
||||
return null
|
||||
updateState(FirmwareUpdateState.Downloading(ProgressState(message = downloadingMsg, progress = 0f)))
|
||||
|
||||
return if (firmwareUri != null) {
|
||||
val extractingMsg = getString(Res.string.firmware_update_extracting)
|
||||
updateState(FirmwareUpdateState.Processing(ProgressState(message = extractingMsg)))
|
||||
getFirmwareFromUri(firmwareUri)
|
||||
} else {
|
||||
val firmwareFile =
|
||||
firmwareRetriever.retrieveEsp32Firmware(release, hardware) { progress ->
|
||||
val percent = (progress * PERCENT_MAX).toInt()
|
||||
updateState(
|
||||
FirmwareUpdateState.Downloading(
|
||||
ProgressState(message = downloadingMsg, progress = progress, details = "$percent%"),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
if (firmwareFile == null) {
|
||||
val errorMsg = getString(Res.string.firmware_update_not_found_in_release, hardware.displayName)
|
||||
updateState(FirmwareUpdateState.Error(errorMsg))
|
||||
null
|
||||
} else {
|
||||
firmwareFile
|
||||
}
|
||||
}
|
||||
return firmwareFile
|
||||
}
|
||||
|
||||
private suspend fun connectToDevice(
|
||||
|
|
@ -273,16 +287,17 @@ constructor(
|
|||
@Suppress("LongMethod")
|
||||
private suspend fun executeOtaSequence(
|
||||
transport: UnifiedOtaProtocol,
|
||||
firmwareFile: File,
|
||||
firmwareFile: String,
|
||||
sha256Hash: String,
|
||||
rebootMode: Int,
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
) {
|
||||
val file = java.io.File(firmwareFile)
|
||||
// Step 5: Start OTA
|
||||
val startingOtaMsg = getString(Res.string.firmware_update_starting_ota)
|
||||
updateState(FirmwareUpdateState.Processing(ProgressState(startingOtaMsg)))
|
||||
transport
|
||||
.startOta(sizeBytes = firmwareFile.length(), sha256Hash = sha256Hash) { status ->
|
||||
.startOta(sizeBytes = file.length(), sha256Hash = sha256Hash) { status ->
|
||||
when (status) {
|
||||
OtaHandshakeStatus.Erasing -> {
|
||||
val erasingMsg = getString(Res.string.firmware_update_erasing)
|
||||
|
|
@ -295,7 +310,7 @@ constructor(
|
|||
// Step 6: Stream
|
||||
val uploadingMsg = getString(Res.string.firmware_update_uploading)
|
||||
updateState(FirmwareUpdateState.Updating(ProgressState(uploadingMsg, 0f)))
|
||||
val firmwareData = firmwareFile.readBytes()
|
||||
val firmwareData = file.readBytes()
|
||||
val chunkSize =
|
||||
if (rebootMode == 1) {
|
||||
BleOtaTransport.RECOMMENDED_CHUNK_SIZE
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (c) 2026 Meshtastic LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.meshtastic.feature.firmware
|
||||
|
||||
sealed interface DfuInternalState {
|
||||
val address: String
|
||||
|
||||
data class Connecting(override val address: String) : DfuInternalState
|
||||
|
||||
data class Connected(override val address: String) : DfuInternalState
|
||||
|
||||
data class Starting(override val address: String) : DfuInternalState
|
||||
|
||||
data class EnablingDfuMode(override val address: String) : DfuInternalState
|
||||
|
||||
data class Progress(
|
||||
override val address: String,
|
||||
val percent: Int,
|
||||
val speed: Float,
|
||||
val avgSpeed: Float,
|
||||
val currentPart: Int,
|
||||
val partsTotal: Int,
|
||||
) : DfuInternalState
|
||||
|
||||
data class Validating(override val address: String) : DfuInternalState
|
||||
|
||||
data class Disconnecting(override val address: String) : DfuInternalState
|
||||
|
||||
data class Disconnected(override val address: String) : DfuInternalState
|
||||
|
||||
data class Completed(override val address: String) : DfuInternalState
|
||||
|
||||
data class Aborted(override val address: String) : DfuInternalState
|
||||
|
||||
data class Error(override val address: String, val message: String?) : DfuInternalState
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (c) 2026 Meshtastic LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.meshtastic.feature.firmware
|
||||
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.model.DeviceHardware
|
||||
|
||||
interface FirmwareFileHandler {
|
||||
fun cleanupAllTemporaryFiles()
|
||||
|
||||
suspend fun checkUrlExists(url: String): Boolean
|
||||
|
||||
suspend fun downloadFile(url: String, fileName: String, onProgress: (Float) -> Unit): String?
|
||||
|
||||
suspend fun extractFirmware(
|
||||
uri: CommonUri,
|
||||
hardware: DeviceHardware,
|
||||
fileExtension: String,
|
||||
preferredFilename: String? = null,
|
||||
): String?
|
||||
|
||||
suspend fun extractFirmwareFromZip(
|
||||
zipFilePath: String,
|
||||
hardware: DeviceHardware,
|
||||
fileExtension: String,
|
||||
preferredFilename: String? = null,
|
||||
): String?
|
||||
|
||||
suspend fun getFileSize(path: String): Long
|
||||
|
||||
suspend fun deleteFile(path: String)
|
||||
|
||||
suspend fun copyFileToUri(sourcePath: String, destinationUri: CommonUri): Long
|
||||
|
||||
suspend fun copyUriToUri(sourceUri: CommonUri, destinationUri: CommonUri): Long
|
||||
}
|
||||
|
|
@ -16,10 +16,9 @@
|
|||
*/
|
||||
package org.meshtastic.feature.firmware
|
||||
|
||||
import android.net.Uri
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.database.entity.FirmwareRelease
|
||||
import org.meshtastic.core.model.DeviceHardware
|
||||
import java.io.File
|
||||
|
||||
/** Common interface for all firmware update handlers (BLE DFU, ESP32 OTA, USB). */
|
||||
interface FirmwareUpdateHandler {
|
||||
|
|
@ -31,13 +30,13 @@ interface FirmwareUpdateHandler {
|
|||
* @param target The target identifier (e.g., Bluetooth address, IP address, or empty for USB)
|
||||
* @param updateState Callback to report back state changes
|
||||
* @param firmwareUri Optional URI for a local firmware file (bypasses download)
|
||||
* @return The downloaded/extracted firmware file, or null if it was a local file or update finished
|
||||
* @return The downloaded/extracted firmware file path, or null if it was a local file or update finished
|
||||
*/
|
||||
suspend fun startUpdate(
|
||||
release: FirmwareRelease,
|
||||
hardware: DeviceHardware,
|
||||
target: String,
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
firmwareUri: Uri? = null,
|
||||
): File?
|
||||
firmwareUri: CommonUri? = null,
|
||||
): String?
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 2026 Meshtastic LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.meshtastic.feature.firmware
|
||||
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.database.entity.FirmwareRelease
|
||||
import org.meshtastic.core.model.DeviceHardware
|
||||
|
||||
interface FirmwareUpdateManager {
|
||||
suspend fun startUpdate(
|
||||
release: FirmwareRelease,
|
||||
hardware: DeviceHardware,
|
||||
address: String,
|
||||
updateState: (FirmwareUpdateState) -> Unit,
|
||||
firmwareUri: CommonUri? = null,
|
||||
): String?
|
||||
|
||||
fun dfuProgressFlow(): kotlinx.coroutines.flow.Flow<DfuInternalState>
|
||||
}
|
||||
|
|
@ -16,10 +16,9 @@
|
|||
*/
|
||||
package org.meshtastic.feature.firmware
|
||||
|
||||
import android.net.Uri
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.database.entity.FirmwareRelease
|
||||
import org.meshtastic.core.model.DeviceHardware
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* Represents the progress of a long-running firmware update task.
|
||||
|
|
@ -58,6 +57,6 @@ sealed interface FirmwareUpdateState {
|
|||
|
||||
data object Success : FirmwareUpdateState
|
||||
|
||||
data class AwaitingFileSave(val uf2File: File?, val fileName: String, val sourceUri: Uri? = null) :
|
||||
data class AwaitingFileSave(val uf2FilePath: String?, val fileName: String, val sourceUri: CommonUri? = null) :
|
||||
FirmwareUpdateState
|
||||
}
|
||||
|
|
@ -16,11 +16,9 @@
|
|||
*/
|
||||
package org.meshtastic.feature.firmware
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import co.touchlab.kermit.Logger
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
|
|
@ -37,6 +35,7 @@ import kotlinx.coroutines.launch
|
|||
import kotlinx.coroutines.withTimeoutOrNull
|
||||
import org.jetbrains.compose.resources.StringResource
|
||||
import org.jetbrains.compose.resources.getString
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.data.repository.FirmwareReleaseRepository
|
||||
import org.meshtastic.core.database.entity.FirmwareRelease
|
||||
import org.meshtastic.core.database.entity.FirmwareReleaseType
|
||||
|
|
@ -73,8 +72,6 @@ import org.meshtastic.core.resources.firmware_update_unknown_hardware
|
|||
import org.meshtastic.core.resources.firmware_update_updating
|
||||
import org.meshtastic.core.resources.firmware_update_validating
|
||||
import org.meshtastic.core.resources.unknown
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val DFU_RECONNECT_PREFIX = "x"
|
||||
private const val PERCENT_MAX_VALUE = 100f
|
||||
|
|
@ -87,11 +84,8 @@ private const val MILLIS_PER_SECOND = 1000L
|
|||
|
||||
private val BLUETOOTH_ADDRESS_REGEX = Regex("([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
|
||||
|
||||
@HiltViewModel
|
||||
@Suppress("LongParameterList", "TooManyFunctions")
|
||||
class FirmwareUpdateViewModel
|
||||
@Inject
|
||||
constructor(
|
||||
open class FirmwareUpdateViewModel(
|
||||
private val firmwareReleaseRepository: FirmwareReleaseRepository,
|
||||
private val deviceHardwareRepository: DeviceHardwareRepository,
|
||||
private val nodeRepository: NodeRepository,
|
||||
|
|
@ -99,7 +93,7 @@ constructor(
|
|||
private val radioPrefs: RadioPrefs,
|
||||
private val bootloaderWarningDataSource: BootloaderWarningDataSource,
|
||||
private val firmwareUpdateManager: FirmwareUpdateManager,
|
||||
private val usbManager: UsbManager,
|
||||
private val usbManager: FirmwareUsbManager,
|
||||
private val fileHandler: FirmwareFileHandler,
|
||||
) : ViewModel() {
|
||||
|
||||
|
|
@ -121,7 +115,7 @@ constructor(
|
|||
val currentFirmwareVersion = _currentFirmwareVersion.asStateFlow()
|
||||
|
||||
private var updateJob: Job? = null
|
||||
private var tempFirmwareFile: File? = null
|
||||
private var tempFirmwareFile: String? = null
|
||||
private var originalDeviceAddress: String? = null
|
||||
|
||||
init {
|
||||
|
|
@ -135,7 +129,7 @@ constructor(
|
|||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
tempFirmwareFile = cleanupTemporaryFiles(fileHandler, tempFirmwareFile)
|
||||
viewModelScope.launch { tempFirmwareFile = cleanupTemporaryFiles(fileHandler, tempFirmwareFile) }
|
||||
}
|
||||
|
||||
fun setReleaseType(type: FirmwareReleaseType) {
|
||||
|
|
@ -251,9 +245,9 @@ constructor(
|
|||
}
|
||||
}
|
||||
|
||||
fun saveDfuFile(uri: Uri) {
|
||||
fun saveDfuFile(uri: CommonUri) {
|
||||
val currentState = _state.value as? FirmwareUpdateState.AwaitingFileSave ?: return
|
||||
val firmwareFile = currentState.uf2File
|
||||
val firmwareFile = currentState.uf2FilePath
|
||||
val sourceUri = currentState.sourceUri
|
||||
|
||||
viewModelScope.launch {
|
||||
|
|
@ -284,7 +278,7 @@ constructor(
|
|||
}
|
||||
}
|
||||
|
||||
fun startUpdateFromFile(uri: Uri) {
|
||||
fun startUpdateFromFile(uri: CommonUri) {
|
||||
val currentState = _state.value as? FirmwareUpdateState.Ready ?: return
|
||||
if (currentState.updateMethod is FirmwareUpdateMethod.Ble && !isValidBluetoothAddress(currentState.address)) {
|
||||
viewModelScope.launch {
|
||||
|
|
@ -305,7 +299,7 @@ constructor(
|
|||
val extractedFile = fileHandler.extractFirmware(uri, currentState.deviceHardware, extension)
|
||||
|
||||
tempFirmwareFile = extractedFile
|
||||
val firmwareUri = if (extractedFile != null) Uri.fromFile(extractedFile) else uri
|
||||
val firmwareUri = if (extractedFile != null) CommonUri.parse("file://$extractedFile") else uri
|
||||
|
||||
tempFirmwareFile =
|
||||
firmwareUpdateManager.startUpdate(
|
||||
|
|
@ -385,7 +379,7 @@ constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private fun handleDfuProgress(dfuState: DfuInternalState.Progress) {
|
||||
private suspend fun handleDfuProgress(dfuState: DfuInternalState.Progress) {
|
||||
val progress = dfuState.percent / PERCENT_MAX_VALUE
|
||||
val percentText = "${dfuState.percent}%"
|
||||
|
||||
|
|
@ -394,7 +388,7 @@ constructor(
|
|||
val speedKib = speedBytesPerSec / KIB_DIVISOR
|
||||
|
||||
// Calculate ETA
|
||||
val totalBytes = tempFirmwareFile?.length() ?: 0L
|
||||
val totalBytes = tempFirmwareFile?.let { fileHandler.getFileSize(it) } ?: 0L
|
||||
val etaText =
|
||||
if (totalBytes > 0 && speedBytesPerSec > 0 && dfuState.percent > 0) {
|
||||
val remainingBytes = totalBytes * (1f - progress)
|
||||
|
|
@ -483,9 +477,9 @@ constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private fun cleanupTemporaryFiles(fileHandler: FirmwareFileHandler, tempFirmwareFile: File?): File? {
|
||||
private suspend fun cleanupTemporaryFiles(fileHandler: FirmwareFileHandler, tempFirmwareFile: String?): String? {
|
||||
runCatching {
|
||||
tempFirmwareFile?.takeIf { it.exists() }?.delete()
|
||||
tempFirmwareFile?.let { fileHandler.deleteFile(it) }
|
||||
fileHandler.cleanupAllTemporaryFiles()
|
||||
}
|
||||
.onFailure { e -> Logger.w(e) { "Failed to cleanup temp files" } }
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* Copyright (c) 2026 Meshtastic LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.meshtastic.feature.firmware
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface FirmwareUsbManager {
|
||||
fun deviceDetachFlow(): Flow<Unit>
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* Copyright (c) 2026 Meshtastic LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.meshtastic.feature.firmware.di
|
||||
|
||||
import org.koin.core.annotation.ComponentScan
|
||||
import org.koin.core.annotation.Module
|
||||
|
||||
@Module
|
||||
@ComponentScan("org.meshtastic.feature.firmware")
|
||||
class FeatureFirmwareModule
|
||||
Loading…
Add table
Add a link
Reference in a new issue