Skip to content
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

Thread sync BA ID troubleshooting updates #3847

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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