Compare commits

...

2 commits

3 changed files with 24 additions and 12 deletions

View file

@ -12,15 +12,7 @@ abstract class EuiccChannel(
abstract val lpa: LocalProfileAssistant
val valid: Boolean
get() {
try {
// Try to ping the eUICC card by reading the EID
lpa.eID
} catch (e: Exception) {
return false
}
return true
}
get() = lpa.valid
fun close() = lpa.close()
}

View file

@ -9,6 +9,16 @@ interface LocalProfileAssistant {
private const val TAG = "LocalProfileAssistant"
}
val valid: Boolean
get() = try {
// If we can read both eID and profiles properly, we are likely looking at
// a valid LocalProfileAssistant
eID
profiles
true
} catch (e: Exception) {
false
}
val profiles: List<LocalProfileInfo>
val notifications: List<LocalProfileNotification>
val eID: String

View file

@ -33,6 +33,7 @@ class LocalProfileAssistantImpl(
try {
LpacJni.es10xFini(contextHandle)
LpacJni.destroyContext(contextHandle)
contextHandle = -1
} catch (e: Exception) {
// Ignored
}
@ -47,12 +48,21 @@ class LocalProfileAssistantImpl(
try {
apduInterface.connect()
contextHandle = LpacJni.createContext(apduInterface, httpInterface)
val res = LpacJni.es10xInit(contextHandle)
Log.d(TAG, "$res")
check(res >= 0) { "Reconnect attempt failed" }
check(LpacJni.es10xInit(contextHandle) >= 0) { "Reconnect attempt failed" }
// Validate that we can actually use the APDU channel by trying to read eID and profiles
check(valid) { "Reconnected channel is invalid" }
break
} catch (e: Exception) {
e.printStackTrace()
if (contextHandle != -1L) {
try {
LpacJni.es10xFini(contextHandle)
LpacJni.destroyContext(contextHandle)
contextHandle = -1
} catch (e: Exception) {
// Ignored
}
}
// continue retrying
delay(1000)
}