瀏覽代碼

lpac-jni: Cancel es9p/10b sessions on download failure

Peter Cai 1 年之前
父節點
當前提交
c6de599db0

+ 1 - 11
.idea/compiler.xml

@@ -1,16 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="CompilerConfiguration">
-    <bytecodeTargetLevel target="1.7">
-      <module name="OpenEUICC.app" target="17" />
-      <module name="OpenEUICC.app-common" target="17" />
-      <module name="OpenEUICC.app-deps" target="17" />
-      <module name="OpenEUICC.app-unpriv" target="17" />
-      <module name="OpenEUICC.buildSrc" target="17" />
-      <module name="OpenEUICC.buildSrc.main" target="17" />
-      <module name="OpenEUICC.buildSrc.test" target="17" />
-      <module name="OpenEUICC.libs.hidden-apis-shim" target="17" />
-      <module name="OpenEUICC.libs.lpac-jni" target="17" />
-    </bytecodeTargetLevel>
+    <bytecodeTargetLevel target="1.7" />
   </component>
 </project>

+ 2 - 0
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

+ 7 - 1
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
         }
     }
 

+ 11 - 1
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);
+}