Skip to content

Commit

Permalink
Thread sync BA ID troubleshooting updates (#3847)
Browse files Browse the repository at this point in the history
* Fix deleting orphaned Thread credentials

 - Deleting orphaned Thread datasets (= datasets for servers that have been deleted for the app) actually deleted current datasets. No real harm except it's deleting stuff and adding it again.

* Log app added preferred Thread network details for debugging

* Always delete Thread datasets using placeholder on >=2023.9

* More detailed log for update app added dataset action
  • Loading branch information
jpelgrom authored Sep 6, 2023
1 parent 4c87dce commit 21bfda0
Showing 1 changed file with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ class ThreadManagerImpl @Inject constructor(
return if (deviceThreadIntent == null && coreThreadDataset != null) {
try {
importDatasetFromServer(context, coreThreadDataset.datasetId, coreThreadDataset.preferredBorderAgentId, serverId)
serverManager.integrationRepository(serverId).setThreadBorderAgentIds(listOf((coreThreadDataset.preferredBorderAgentId ?: BORDER_AGENT_ID)))
coreThreadDataset.preferredBorderAgentId?.let {
serverManager.integrationRepository(serverId).setThreadBorderAgentIds(listOf(it))
} // else added using placeholder, will be removed when core is updated
Log.d(TAG, "Thread import to device completed")
ThreadManager.SyncResult.OnlyOnServer(imported = true)
} catch (e: Exception) {
Expand Down Expand Up @@ -93,11 +95,7 @@ class ThreadManagerImpl @Inject constructor(
try {
val localIds = serverManager.defaultServers.flatMap {
serverManager.integrationRepository(it.id).getThreadBorderAgentIds()
}.toMutableList()
if (localIds.isEmpty()) { // Prefers something from HA, must've been added before BA ID logic
localIds += BORDER_AGENT_ID
}

updated = if (coreThreadDataset.source != "Google") { // Credential from HA, update
localIds.filter { it != coreThreadDataset.preferredBorderAgentId }.forEach { baId ->
try {
Expand All @@ -109,13 +107,14 @@ class ThreadManagerImpl @Inject constructor(
importDatasetFromServer(context, coreThreadDataset.datasetId, coreThreadDataset.preferredBorderAgentId, serverId)
serverManager.defaultServers.forEach {
serverManager.integrationRepository(it.id).setThreadBorderAgentIds(
if (it.id == serverId) {
listOf(coreThreadDataset.preferredBorderAgentId ?: BORDER_AGENT_ID)
if (it.id == serverId && coreThreadDataset.preferredBorderAgentId != null) {
listOf(coreThreadDataset.preferredBorderAgentId!!)
} else {
emptyList()
}
)
}
Log.d(TAG, "Thread update device completed: deleted ${localIds.size} datasets, updated 1")
true
} else { // Core prefers imported from other app, this shouldn't be managed by HA
localIds.forEach { baId ->
Expand All @@ -128,9 +127,9 @@ class ThreadManagerImpl @Inject constructor(
serverManager.defaultServers.forEach {
serverManager.integrationRepository(it.id).setThreadBorderAgentIds(emptyList())
}
Log.d(TAG, "Thread update device completed: deleted ${localIds.size} datasets")
false
}
Log.d(TAG, "Thread update device completed")
} catch (e: Exception) {
Log.e(TAG, "Thread update device failed", e)
}
Expand Down Expand Up @@ -211,7 +210,13 @@ class ThreadManagerImpl @Inject constructor(
.addOnFailureListener { cont.resume(null) }
}
return try {
appCredentials?.any { isPreferredCredentials(context, it) } ?: false
appCredentials?.any {
val isPreferred = isPreferredCredentials(context, it)
if (isPreferred) {
Log.d(TAG, "Thread device prefers app added dataset: ${it.networkName} (PAN ${it.panId}, EXTPAN ${it.extendedPanId})")
}
isPreferred
} ?: false
} catch (e: Exception) {
Log.e(TAG, "Thread app added credentials preferred check failed", e)
false
Expand Down Expand Up @@ -239,7 +244,15 @@ class ThreadManagerImpl @Inject constructor(
}

private suspend fun deleteOrphanedThreadCredentials(context: Context, serverId: Int) {
val orphanedCredentials = serverManager.integrationRepository(serverId).getThreadBorderAgentIds()
if (serverManager.defaultServers.all { it.version?.isAtLeast(2023, 9) == true }) {
try {
deleteThreadCredential(context, BORDER_AGENT_ID)
} catch (e: Exception) {
// Expected, it may not exist
}
}

val orphanedCredentials = serverManager.integrationRepository(serverId).getOrphanedThreadBorderAgentIds()
if (orphanedCredentials.isEmpty()) return

orphanedCredentials.forEach {
Expand Down

0 comments on commit 21bfda0

Please sign in to comment.