From c6de599db0825b0ed15c4ce6702639ecd6cbba7f Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Sun, 1 Dec 2024 17:08:09 -0500 Subject: [PATCH] lpac-jni: Cancel es9p/10b sessions on download failure --- .idea/compiler.xml | 12 +----------- .../src/main/java/net/typeblog/lpac_jni/LpacJni.kt | 2 ++ .../lpac_jni/impl/LocalProfileAssistantImpl.kt | 8 +++++++- libs/lpac-jni/src/main/jni/lpac-jni/lpac-download.c | 12 +++++++++++- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 61f4db45..1c5ab058 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,16 +1,6 @@ - - - - - - - - - - - + \ No newline at end of file diff --git a/libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/LpacJni.kt b/libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/LpacJni.kt index f9a2f901..1e64c69d 100644 --- a/libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/LpacJni.kt +++ b/libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/LpacJni.kt @@ -30,6 +30,8 @@ internal object LpacJni { external fun downloadProfile(handle: Long, smdp: String, matchingId: String?, imei: String?, confirmationCode: String?, callback: ProfileDownloadCallback): Int external fun handleNotification(handle: Long, seqNumber: Long): Int + // Cancel any ongoing es9p and/or es10b sessions + external fun cancelSessions(handle: Long) // es10cex (actually part of es10b) external fun es10cexGetEuiccInfo2(handle: Long): Long diff --git a/libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/impl/LocalProfileAssistantImpl.kt b/libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/impl/LocalProfileAssistantImpl.kt index 70606d99..4dbb1813 100644 --- a/libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/impl/LocalProfileAssistantImpl.kt +++ b/libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/impl/LocalProfileAssistantImpl.kt @@ -212,12 +212,18 @@ class LocalProfileAssistantImpl( ) if (res != 0) { - throw LocalProfileAssistant.ProfileDownloadException( + // Construct the error now to store any error information we _can_ access + val err = LocalProfileAssistant.ProfileDownloadException( httpInterface.lastHttpResponse, httpInterface.lastHttpException, apduInterface.lastApduResponse, apduInterface.lastApduException, ) + + // Cancel sessions if possible. This will overwrite recorded errors from HTTP and APDU interfaces. + LpacJni.cancelSessions(contextHandle) + + throw err } } diff --git a/libs/lpac-jni/src/main/jni/lpac-jni/lpac-download.c b/libs/lpac-jni/src/main/jni/lpac-jni/lpac-download.c index 91676daf..89101707 100644 --- a/libs/lpac-jni/src/main/jni/lpac-jni/lpac-download.c +++ b/libs/lpac-jni/src/main/jni/lpac-jni/lpac-download.c @@ -117,7 +117,8 @@ Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(JNIEnv *env, jobject thiz, j syslog(LOG_INFO, "es10b_load_bound_profile_package %d, reason %d", ret, es10b_load_bound_profile_package_result.errorReason); out: - euicc_http_cleanup(ctx); + // We expect Java side to call cancelSessions after any error -- thus, `euicc_http_cleanup` is done there + // This is so that Java side can access the last HTTP and/or APDU errors when we return. if (_confirmation_code != NULL) (*env)->ReleaseStringUTFChars(env, confirmation_code, _confirmation_code); if (_matching_id != NULL) @@ -127,3 +128,12 @@ Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(JNIEnv *env, jobject thiz, j (*env)->ReleaseStringUTFChars(env, imei, _imei); return ret; } + + +JNIEXPORT void JNICALL +Java_net_typeblog_lpac_1jni_LpacJni_cancelSessions(JNIEnv *env, jobject thiz, jlong handle) { + struct euicc_ctx *ctx = (struct euicc_ctx *) handle; + es9p_cancel_session(ctx); + es10b_cancel_session(ctx, ES10B_CANCEL_SESSION_REASON_UNDEFINED); + euicc_http_cleanup(ctx); +} \ No newline at end of file