瀏覽代碼

lpac_jni: Move HTTP diagnostics to LPA

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

+ 0 - 14
libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/HttpInterface.kt

@@ -26,20 +26,6 @@ interface HttpInterface {
         }
         }
     }
     }
 
 
-    /**
-     * The last HTTP response we have received from the SM-DP+ server.
-     *
-     * This is intended for error diagnosis. However, note that most SM-DP+ servers
-     * respond with 200 even when there is an error. This needs to be taken into
-     * account when designing UI.
-     */
-    val lastHttpResponse: HttpResponse?
-
-    /**
-     * The last exception that has been thrown during a HTTP connection
-     */
-    val lastHttpException: Exception?
-
     fun transmit(url: String, tx: ByteArray, headers: Array<String>): HttpResponse
     fun transmit(url: String, tx: ByteArray, headers: Array<String>): HttpResponse
     // The LPA is supposed to pass in a list of pkIds supported by the eUICC.
     // The LPA is supposed to pass in a list of pkIds supported by the eUICC.
     // HttpInterface is responsible for providing TrustManager implementations that
     // HttpInterface is responsible for providing TrustManager implementations that

+ 1 - 11
libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/impl/HttpInterfaceImpl.kt

@@ -23,9 +23,6 @@ class HttpInterfaceImpl(
 
 
     private lateinit var trustManagers: Array<TrustManager>
     private lateinit var trustManagers: Array<TrustManager>
 
 
-    override var lastHttpResponse: HttpInterface.HttpResponse? = null
-    override var lastHttpException: Exception? = null
-
     override fun transmit(
     override fun transmit(
         url: String,
         url: String,
         tx: ByteArray,
         tx: ByteArray,
@@ -76,16 +73,9 @@ class HttpInterfaceImpl(
                 }
                 }
             }
             }
 
 
-            return HttpInterface.HttpResponse(conn.responseCode, bytes).also {
-                lastHttpResponse = it
-            }
+            return HttpInterface.HttpResponse(conn.responseCode, bytes)
         } catch (e: Exception) {
         } catch (e: Exception) {
             e.printStackTrace()
             e.printStackTrace()
-
-            // Reset response to null because there's no response here
-            lastHttpResponse = null
-            lastHttpException = e
-
             throw e
             throw e
         }
         }
     }
     }

+ 34 - 1
libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/impl/LocalProfileAssistantImpl.kt

@@ -5,6 +5,7 @@ import net.typeblog.lpac_jni.LpacJni
 import net.typeblog.lpac_jni.ApduInterface
 import net.typeblog.lpac_jni.ApduInterface
 import net.typeblog.lpac_jni.EuiccInfo2
 import net.typeblog.lpac_jni.EuiccInfo2
 import net.typeblog.lpac_jni.HttpInterface
 import net.typeblog.lpac_jni.HttpInterface
+import net.typeblog.lpac_jni.HttpInterface.HttpResponse
 import net.typeblog.lpac_jni.LocalProfileAssistant
 import net.typeblog.lpac_jni.LocalProfileAssistant
 import net.typeblog.lpac_jni.LocalProfileInfo
 import net.typeblog.lpac_jni.LocalProfileInfo
 import net.typeblog.lpac_jni.LocalProfileNotification
 import net.typeblog.lpac_jni.LocalProfileNotification
@@ -12,7 +13,7 @@ import net.typeblog.lpac_jni.ProfileDownloadCallback
 
 
 class LocalProfileAssistantImpl(
 class LocalProfileAssistantImpl(
     rawApduInterface: ApduInterface,
     rawApduInterface: ApduInterface,
-    private val httpInterface: HttpInterface
+    rawHttpInterface: HttpInterface
 ): LocalProfileAssistant {
 ): LocalProfileAssistant {
     companion object {
     companion object {
         private const val TAG = "LocalProfileAssistantImpl"
         private const val TAG = "LocalProfileAssistantImpl"
@@ -38,7 +39,39 @@ class LocalProfileAssistantImpl(
             }
             }
     }
     }
 
 
+    /**
+     * Same for HTTP for diagnostics
+     */
+    private class HttpInterfaceWrapper(val httpInterface: HttpInterface) :
+        HttpInterface by httpInterface {
+        /**
+         * The last HTTP response we have received from the SM-DP+ server.
+         *
+         * This is intended for error diagnosis. However, note that most SM-DP+ servers
+         * respond with 200 even when there is an error. This needs to be taken into
+         * account when designing UI.
+         */
+        var lastHttpResponse: HttpResponse? = null
+
+        /**
+         * The last exception that has been thrown during a HTTP connection
+         */
+        var lastHttpException: Exception? = null
+
+        override fun transmit(url: String, tx: ByteArray, headers: Array<String>): HttpResponse =
+            try {
+                httpInterface.transmit(url, tx, headers).also {
+                    lastHttpResponse = it
+                }
+            } catch (e: Exception) {
+                lastHttpResponse = null
+                lastHttpException = e
+                throw e
+            }
+    }
+
     private val apduInterface = ApduInterfaceWrapper(rawApduInterface)
     private val apduInterface = ApduInterfaceWrapper(rawApduInterface)
+    private val httpInterface = HttpInterfaceWrapper(rawHttpInterface)
 
 
     private var finalized = false
     private var finalized = false
     private var contextHandle: Long = LpacJni.createContext(apduInterface, httpInterface)
     private var contextHandle: Long = LpacJni.createContext(apduInterface, httpInterface)