Compare commits

..

1 commit

Author SHA1 Message Date
f507dae186
feat: cancellable download 2025-03-10 20:53:47 +08:00

View file

@ -60,19 +60,14 @@ void lpac_download_init() {
set_cancelled = (*env)->GetMethodID(env, download_callback_class, "setCancelled", "(Z)V"); set_cancelled = (*env)->GetMethodID(env, download_callback_class, "setCancelled", "(Z)V");
} }
#define IS_CANCELLED (*env)->CallBooleanMethod(env, callback, is_cancelled) #define IS_CANCELLED (*env)->CallBooleanMethod(env, callback, is_cancelled) == JNI_TRUE
#define SET_CANCELLED(VALUE) (*env)->CallVoidMethod(env, callback, set_cancelled, VALUE)
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile( Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(JNIEnv *env, jobject thiz, jlong handle,
JNIEnv *env, jstring smdp, jstring matching_id,
__attribute__((unused)) jobject thiz, jstring imei, jstring confirmation_code,
jlong handle, jobject callback) {
jstring smdp,
jstring matching_id,
jstring imei,
jstring confirmation_code,
jobject callback
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
struct es10b_load_bound_profile_package_result es10b_load_bound_profile_package_result; struct es10b_load_bound_profile_package_result es10b_load_bound_profile_package_result;
const char *_confirmation_code = NULL; const char *_confirmation_code = NULL;
@ -91,11 +86,10 @@ Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(
ctx->http.server_address = _smdp; ctx->http.server_address = _smdp;
if (IS_CANCELLED) { // callback.isCancelled() if (IS_CANCELLED) {
ret = -ES10B_ERROR_REASON_UNDEFINED; ret = -ES10B_ERROR_REASON_UNDEFINED;
goto out; goto out;
} }
// callback.onStateUpdate(DownloadState.Preparing)
(*env)->CallVoidMethod(env, callback, on_state_update, download_state_preparing); (*env)->CallVoidMethod(env, callback, on_state_update, download_state_preparing);
ret = es10b_get_euicc_challenge_and_info(ctx); ret = es10b_get_euicc_challenge_and_info(ctx);
syslog(LOG_INFO, "es10b_get_euicc_challenge_and_info %d", ret); syslog(LOG_INFO, "es10b_get_euicc_challenge_and_info %d", ret);
@ -104,12 +98,10 @@ Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(
goto out; goto out;
} }
if (IS_CANCELLED) {
if (IS_CANCELLED) { // callback.isCancelled()
ret = -ES10B_ERROR_REASON_UNDEFINED; ret = -ES10B_ERROR_REASON_UNDEFINED;
goto out; goto out;
} }
// callback.onStateUpdate(DownloadState.Connecting)
(*env)->CallVoidMethod(env, callback, on_state_update, download_state_connecting); (*env)->CallVoidMethod(env, callback, on_state_update, download_state_connecting);
ret = es9p_initiate_authentication(ctx); ret = es9p_initiate_authentication(ctx);
syslog(LOG_INFO, "es9p_initiate_authentication %d", ret); syslog(LOG_INFO, "es9p_initiate_authentication %d", ret);
@ -118,11 +110,10 @@ Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(
goto out; goto out;
} }
if (IS_CANCELLED) { // callback.isCancelled() if (IS_CANCELLED) {
ret = -ES10B_ERROR_REASON_UNDEFINED; ret = -ES10B_ERROR_REASON_UNDEFINED;
goto out; goto out;
} }
// callback.onStateUpdate(DownloadState.Authenticating)
(*env)->CallVoidMethod(env, callback, on_state_update, download_state_authenticating); (*env)->CallVoidMethod(env, callback, on_state_update, download_state_authenticating);
ret = es10b_authenticate_server(ctx, _matching_id, _imei); ret = es10b_authenticate_server(ctx, _matching_id, _imei);
syslog(LOG_INFO, "es10b_authenticate_server %d", ret); syslog(LOG_INFO, "es10b_authenticate_server %d", ret);
@ -131,7 +122,7 @@ Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(
goto out; goto out;
} }
if (IS_CANCELLED) { // callback.isCancelled() if (IS_CANCELLED) {
ret = -ES10B_ERROR_REASON_UNDEFINED; ret = -ES10B_ERROR_REASON_UNDEFINED;
goto out; goto out;
} }
@ -142,11 +133,10 @@ Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(
goto out; goto out;
} }
if (IS_CANCELLED) { // callback.isCancelled() if (IS_CANCELLED) {
ret = -ES10B_ERROR_REASON_UNDEFINED; ret = -ES10B_ERROR_REASON_UNDEFINED;
goto out; goto out;
} }
// callback.onStateUpdate(DownloadState.Downloading)
(*env)->CallVoidMethod(env, callback, on_state_update, download_state_downloading); (*env)->CallVoidMethod(env, callback, on_state_update, download_state_downloading);
ret = es10b_prepare_download(ctx, _confirmation_code); ret = es10b_prepare_download(ctx, _confirmation_code);
syslog(LOG_INFO, "es10b_prepare_download %d", ret); syslog(LOG_INFO, "es10b_prepare_download %d", ret);
@ -155,7 +145,7 @@ Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(
goto out; goto out;
} }
if (IS_CANCELLED) { // callback.isCancelled() if (IS_CANCELLED) {
ret = -ES10B_ERROR_REASON_UNDEFINED; ret = -ES10B_ERROR_REASON_UNDEFINED;
goto out; goto out;
} }
@ -164,11 +154,10 @@ Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(
if (ret < 0) if (ret < 0)
goto out; goto out;
if (IS_CANCELLED) { // callback.isCancelled() if (IS_CANCELLED) {
ret = -ES10B_ERROR_REASON_UNDEFINED; ret = -ES10B_ERROR_REASON_UNDEFINED;
goto out; goto out;
} }
// callback.onStateUpdate(DownloadState.Finalizing)
(*env)->CallVoidMethod(env, callback, on_state_update, download_state_finalizing); (*env)->CallVoidMethod(env, callback, on_state_update, download_state_finalizing);
ret = es10b_load_bound_profile_package(ctx, &es10b_load_bound_profile_package_result); ret = es10b_load_bound_profile_package(ctx, &es10b_load_bound_profile_package_result);
syslog(LOG_INFO, "es10b_load_bound_profile_package %d, reason %d", ret, es10b_load_bound_profile_package_result.errorReason); syslog(LOG_INFO, "es10b_load_bound_profile_package %d, reason %d", ret, es10b_load_bound_profile_package_result.errorReason);
@ -181,7 +170,7 @@ Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(
out: out:
if (IS_CANCELLED == 0 && ret == -ES10B_ERROR_REASON_UNDEFINED) { if (IS_CANCELLED == 0 && ret == -ES10B_ERROR_REASON_UNDEFINED) {
(*env)->CallVoidMethod(env, callback, set_cancelled, JNI_TRUE); SET_CANCELLED(JNI_TRUE);
} }
// We expect Java side to call cancelSessions after any error -- thus, `euicc_http_cleanup` is done there // 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. // This is so that Java side can access the last HTTP and/or APDU errors when we return.