Fix #2918 crash on hw model (#2927)

pull/2937/head
DaneEvans 2025-08-30 13:00:51 +10:00 zatwierdzone przez GitHub
rodzic e03bd34ae5
commit f8377589aa
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
3 zmienionych plików z 59 dodań i 3 usunięć

Wyświetl plik

@ -47,6 +47,7 @@ import com.geeksville.mesh.repository.api.DeviceHardwareRepository
import com.geeksville.mesh.repository.api.FirmwareReleaseRepository
import com.geeksville.mesh.repository.datastore.RadioConfigRepository
import com.geeksville.mesh.service.ServiceAction
import com.geeksville.mesh.util.safeNumber
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@ -261,7 +262,9 @@ constructor(
// Create a fallback node if not found in database (for hidden clients, etc.)
val actualNode = node ?: createFallbackNode(destNum)
val deviceHardware =
actualNode.user.hwModel.number.let { deviceHardwareRepository.getDeviceHardwareByModel(it) }
actualNode.user.hwModel.safeNumber().let {
deviceHardwareRepository.getDeviceHardwareByModel(it)
}
_state.update { state ->
state.copy(
node = actualNode,

Wyświetl plik

@ -67,6 +67,7 @@ import com.geeksville.mesh.ui.common.components.MainMenuAction
import com.geeksville.mesh.ui.node.components.NodeMenuAction
import com.geeksville.mesh.util.getShortDate
import com.geeksville.mesh.util.positionToMeter
import com.geeksville.mesh.util.safeNumber
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
@ -228,8 +229,8 @@ constructor(
val deviceHardware: StateFlow<DeviceHardware?> =
ourNodeInfo
.mapNotNull { nodeInfo ->
nodeInfo?.user?.hwModel?.let {
deviceHardwareRepository.getDeviceHardwareByModel(it.number).getOrNull()
nodeInfo?.user?.hwModel?.let { hwModel ->
deviceHardwareRepository.getDeviceHardwareByModel(hwModel.safeNumber()).getOrNull()
}
}
.stateIn(scope = viewModelScope, started = SharingStarted.WhileSubscribed(5_000), initialValue = null)

Wyświetl plik

@ -0,0 +1,52 @@
/*
* Copyright (c) 2025 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 com.geeksville.mesh.util
import com.geeksville.mesh.MeshProtos
import com.geeksville.mesh.android.BuildUtils.warn
/**
* Safely extracts the hardware model number from a HardwareModel enum.
*
* This function handles unknown enum values gracefully by catching IllegalArgumentException and returning a fallback
* value. This prevents crashes when the app receives data from devices with hardware models not yet defined in the
* current protobuf version.
*
* @param fallbackValue The value to return if the enum is unknown (defaults to 0 for UNSET)
* @return The hardware model number, or the fallback value if the enum is unknown
*/
@Suppress("detekt:SwallowedException")
fun MeshProtos.HardwareModel.safeNumber(fallbackValue: Int = -1): Int = try {
this.number
} catch (e: IllegalArgumentException) {
warn("Unknown hardware model enum value: $this, using fallback value: $fallbackValue")
fallbackValue
}
/**
* Checks if the hardware model is a known/supported value.
*
* @return true if the hardware model is known and supported, false otherwise
*/
@Suppress("detekt:SwallowedException")
fun MeshProtos.HardwareModel.isKnown(): Boolean = try {
this.number
true
} catch (e: IllegalArgumentException) {
false
}