summaryrefslogtreecommitdiff
path: root/src/com/android/ons/ONSProfileDownloader.java
blob: affcdf604b7dc17cca71ce511260096fc7d27f67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ons;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.euicc.DownloadableSubscription;
import android.telephony.euicc.EuiccManager;
import android.util.Log;
import android.util.Pair;

import com.android.internal.annotations.VisibleForTesting;

import java.util.Stack;

public class ONSProfileDownloader {

    interface IONSProfileDownloaderListener {
        void onDownloadComplete(int primarySubId);
        void onDownloadError(DownloadRetryOperationCode operationCode, int pSIMSubId);
    }

    private static final String TAG = ONSProfileDownloader.class.getName();
    public static final String ACTION_ONS_ESIM_DOWNLOAD = "com.android.ons.action.ESIM_DOWNLOAD";

    @VisibleForTesting protected static final String PARAM_PRIMARY_SUBID = "PrimarySubscriptionID";
    @VisibleForTesting protected static final String PARAM_REQUEST_TYPE = "REQUEST";
    @VisibleForTesting protected static final int REQUEST_CODE_DOWNLOAD_SUB = 1;

    private final Handler mHandler;
    private final Context mContext;
    private final CarrierConfigManager mCarrierConfigManager;
    private final EuiccManager mEuiccManager;
    private final ONSProfileConfigurator mONSProfileConfig;
    private IONSProfileDownloaderListener mListener;

    // Subscription Id of the CBRS PSIM for which opportunistic eSIM is being downloaded. Used to
    // ignore duplicate download requests when download is in progress.
    private int mDownloadingPSimSubId;

    @VisibleForTesting
    protected enum DownloadRetryOperationCode{
        DOWNLOAD_SUCCESSFUL,
        ERR_UNRESOLVABLE,
        ERR_MEMORY_FULL,
        ERR_INSTALL_ESIM_PROFILE_FAILED,
        ERR_RETRY_DOWNLOAD
    };

    public ONSProfileDownloader(Context context, CarrierConfigManager carrierConfigManager,
                                EuiccManager euiccManager,
                                ONSProfileConfigurator onsProfileConfigurator,
                                IONSProfileDownloaderListener listener) {
        mContext = context;
        mListener = listener;
        mEuiccManager = euiccManager;
        mONSProfileConfig = onsProfileConfigurator;
        mCarrierConfigManager = carrierConfigManager;

        mHandler = new DownloadHandler();
    }

    class DownloadHandler extends Handler {
        DownloadHandler() {
            super(Looper.myLooper());
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // Received Response for download request. REQUEST_CODE_DOWNLOAD_SUB was sent to LPA
                // as part of request intent.
                case REQUEST_CODE_DOWNLOAD_SUB: {
                    Log.d(TAG, "REQUEST_CODE_DOWNLOAD_SUB callback received");

                    //Clear downloading subscription flag. Indicates no download in progress.
                    synchronized (this) {
                        mDownloadingPSimSubId = -1;
                    }

                    int pSIMSubId = ((Intent) msg.obj).getIntExtra(PARAM_PRIMARY_SUBID, 0);
                    int detailedErrCode = ((Intent) msg.obj).getIntExtra(
                            EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE, 0);
                    int operationCode = ((Intent) msg.obj).getIntExtra(
                            EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_OPERATION_CODE, 0);
                    int errorCode = ((Intent) msg.obj).getIntExtra(
                            EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_ERROR_CODE, 0);

                    Log.d(TAG, "Result Code : " + detailedErrCode);
                    Log.d(TAG, "Operation Code : " + operationCode);
                    Log.d(TAG, "Error Code : " + errorCode);

                    DownloadRetryOperationCode opCode = mapDownloaderErrorCode(msg.arg1,
                            detailedErrCode, operationCode, errorCode);
                    Log.d(TAG, "DownloadRetryOperationCode: " + opCode);

                    switch (opCode) {
                        case DOWNLOAD_SUCCESSFUL:
                            mListener.onDownloadComplete(pSIMSubId);
                            break;

                        case ERR_UNRESOLVABLE:
                            mListener.onDownloadError(opCode, pSIMSubId);
                            Log.e(TAG, "Unresolvable download error: "
                                    + getUnresolvableErrorDescription(errorCode));
                            break;

                        default:
                            mListener.onDownloadError(opCode, pSIMSubId);
                            break;
                    }
                }
                break;
            }
        }

        @VisibleForTesting
        protected DownloadRetryOperationCode mapDownloaderErrorCode(int resultCode,
                                                                    int detailedErrCode,
                                                                    int operationCode,
                                                                    int errorCode) {

            if (operationCode == EuiccManager.OPERATION_SMDX_SUBJECT_REASON_CODE) {
                //SMDP Error codes handling
                Pair<String, String> errCode = decodeSmdxSubjectAndReasonCode(detailedErrCode);
                Log.e(TAG, " Subject Code: " + errCode.first + " Reason Code: "
                        + errCode.second);

                //8.1 - eUICC, 4.8 - Insufficient Memory
                // eUICC does not have sufficient space for this Profile.
                if (errCode.equals(Pair.create("8.1.0", "4.8"))) {
                    return DownloadRetryOperationCode.ERR_MEMORY_FULL;
                }

                //8.8.5 - Download order, 4.10 - Time to Live Expired
                //The Download order has expired
                if (errCode.equals(Pair.create("8.8.5", "4.10"))) {
                    return DownloadRetryOperationCode.ERR_RETRY_DOWNLOAD;
                }

                //All other errors are unresolvable or retry after SIM State Change
                return DownloadRetryOperationCode.ERR_UNRESOLVABLE;

            }

            switch (errorCode) {

                //Success Cases
                case EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK: {
                    return DownloadRetryOperationCode.DOWNLOAD_SUCCESSFUL;
                }

                //Low eUICC memory cases
                case EuiccManager.ERROR_EUICC_INSUFFICIENT_MEMORY: {
                    Log.d(TAG, "Download ERR: EUICC_INSUFFICIENT_MEMORY");
                    return DownloadRetryOperationCode.ERR_MEMORY_FULL;
                }

                //Temporary download error cases
                case EuiccManager.ERROR_TIME_OUT:
                case EuiccManager.ERROR_CONNECTION_ERROR:
                case EuiccManager.ERROR_OPERATION_BUSY: {
                    return DownloadRetryOperationCode.ERR_RETRY_DOWNLOAD;
                }

                //Profile installation failure cases
                case EuiccManager.ERROR_INSTALL_PROFILE: {
                    return DownloadRetryOperationCode.ERR_INSTALL_ESIM_PROFILE_FAILED;
                }

                default: {
                    return DownloadRetryOperationCode.ERR_UNRESOLVABLE;
                }
            }
        }
    }

    private String getUnresolvableErrorDescription(int errorCode) {
        switch (errorCode) {
            case EuiccManager.ERROR_INVALID_ACTIVATION_CODE:
                return "ERROR_INVALID_ACTIVATION_CODE";

            case EuiccManager.ERROR_UNSUPPORTED_VERSION:
                return "ERROR_UNSUPPORTED_VERSION";

            case EuiccManager.ERROR_INSTALL_PROFILE:
                return "ERROR_INSTALL_PROFILE";

            case EuiccManager.ERROR_SIM_MISSING:
                return "ERROR_SIM_MISSING";

            case EuiccManager.ERROR_ADDRESS_MISSING:
                return "ERROR_ADDRESS_MISSING";

            case EuiccManager.ERROR_CERTIFICATE_ERROR:
                return "ERROR_CERTIFICATE_ERROR";

            case EuiccManager.ERROR_NO_PROFILES_AVAILABLE:
                return "ERROR_NO_PROFILES_AVAILABLE";

            case EuiccManager.ERROR_CARRIER_LOCKED:
                return "ERROR_CARRIER_LOCKED";
        }

        return "Unknown";
    }

    @VisibleForTesting
    protected enum DownloadProfileResult {
        SUCCESS,
        DUPLICATE_REQUEST,
        INVALID_SMDP_ADDRESS
    }

    @VisibleForTesting
    protected DownloadProfileResult downloadProfile(int primarySubId) {
        Log.d(TAG, "downloadProfile");

        synchronized (this) {
            if (mDownloadingPSimSubId == primarySubId) {
                Log.d(TAG, "Download already in progress.");
                return DownloadProfileResult.DUPLICATE_REQUEST;
            }

            mDownloadingPSimSubId = primarySubId;
        }

        //Get SMDP address from carrier configuration.
        String smdpAddress = getSMDPServerAddress(primarySubId);
        if (smdpAddress == null || smdpAddress.length() <= 0) {
            return DownloadProfileResult.INVALID_SMDP_ADDRESS;
        }

        //Generate Activation code 1${SM-DP+ FQDN}$
        String activationCode = "1$" + smdpAddress + "$";
        Intent intent = new Intent(mContext, ONSProfileResultReceiver.class);
        intent.setAction(ACTION_ONS_ESIM_DOWNLOAD);
        intent.putExtra(PARAM_REQUEST_TYPE, REQUEST_CODE_DOWNLOAD_SUB);
        intent.putExtra(PARAM_PRIMARY_SUBID, primarySubId);
        PendingIntent callbackIntent = PendingIntent.getBroadcast(mContext,
                REQUEST_CODE_DOWNLOAD_SUB, intent, PendingIntent.FLAG_MUTABLE);

        Log.d(TAG, "Download Request sent to EUICC Manager");
        mEuiccManager.downloadSubscription(DownloadableSubscription.forActivationCode(
                activationCode), true, callbackIntent);

        return DownloadProfileResult.SUCCESS;
    }

    /**
     * Retrieves SMDP+ server address of the given subscription from carrier configuration.
     *
     * @param subscriptionId subscription Id of the primary SIM.
     * @return FQDN of SMDP+ server.
     */
    private String getSMDPServerAddress(int subscriptionId) {
        PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subscriptionId);
        return config.getString(CarrierConfigManager.KEY_SMDP_SERVER_ADDRESS_STRING);
    }

    /**
     * Given encoded error code described in
     * {@link android.telephony.euicc.EuiccManager#OPERATION_SMDX_SUBJECT_REASON_CODE} decode it
     * into SubjectCode[5.2.6.1] and ReasonCode[5.2.6.2] from GSMA (SGP.22 v2.2)
     *
     * @param resultCode from
     *               {@link android.telephony.euicc.EuiccManager#OPERATION_SMDX_SUBJECT_REASON_CODE}
     *
     * @return a pair containing SubjectCode[5.2.6.1] and ReasonCode[5.2.6.2] from GSMA (SGP.22
     * v2.2)
     */
    @VisibleForTesting
    protected static Pair<String, String> decodeSmdxSubjectAndReasonCode(int resultCode) {
        final int numOfSections = 6;
        final int bitsPerSection = 4;
        final int sectionMask = 0xF;

        final Stack<Integer> sections = new Stack<>();

        // Extracting each section of digits backwards.
        for (int i = 0; i < numOfSections; ++i) {
            int sectionDigit = resultCode & sectionMask;
            sections.push(sectionDigit);
            resultCode = resultCode >>> bitsPerSection;
        }

        String subjectCode = sections.pop() + "." + sections.pop() + "." + sections.pop();
        String reasonCode = sections.pop() + "." + sections.pop() + "." + sections.pop();

        // drop the leading zeros, e.g. 0.1 -> 1, 0.0.3 -> 3, 0.5.1 -> 5.1
        subjectCode = subjectCode.replaceAll("^(0\\.)*", "");
        reasonCode = reasonCode.replaceAll("^(0\\.)*", "");

        return Pair.create(subjectCode, reasonCode);
    }

    /**
     * Callback to receive result for subscription activate request and process the same.
     * @param intent
     * @param resultCode
     */
    public void onCallbackIntentReceived(Intent intent, int resultCode) {
        Message msg = new Message();
        msg.what = REQUEST_CODE_DOWNLOAD_SUB;
        msg.arg1 = resultCode;
        msg.obj = intent;
        mHandler.sendMessage(msg);
    }
}