1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.internal.telephony.uicc.euicc.apdu;
18 
19 import android.os.AsyncResult;
20 import android.os.Message;
21 import android.telephony.IccOpenLogicalChannelResponse;
22 
23 import com.android.internal.telephony.CommandException;
24 import com.android.internal.telephony.CommandsInterface;
25 import com.android.internal.telephony.uicc.euicc.async.AsyncMessageInvocation;
26 import com.android.telephony.Rlog;
27 
28 /**
29  * Invokes {@link CommandsInterface#iccOpenLogicalChannel(String, int, Message)}. This takes AID
30  * (String) as the input and return the response of opening a logical channel. Error will be
31  * included in the {@link IccOpenLogicalChannelResponse} result and no exception will be returned to
32  * the result callback.
33  *
34  * @hide
35  */
36 class OpenLogicalChannelInvocation
37         extends AsyncMessageInvocation<String, IccOpenLogicalChannelResponse> {
38     private static final String LOG_TAG = "OpenChan";
39 
40     private final CommandsInterface mCi;
41 
OpenLogicalChannelInvocation(CommandsInterface ci)42     OpenLogicalChannelInvocation(CommandsInterface ci) {
43         mCi = ci;
44     }
45 
46     @Override
sendRequestMessage(String aid, Message msg)47     protected void sendRequestMessage(String aid, Message msg) {
48         mCi.iccOpenLogicalChannel(aid, 0, msg);
49     }
50 
51     @Override
parseResult(AsyncResult ar)52     protected IccOpenLogicalChannelResponse parseResult(AsyncResult ar) {
53         IccOpenLogicalChannelResponse openChannelResp;
54         // The code below is copied from PhoneInterfaceManager.java.
55         // TODO: move this code into IccOpenLogicalChannelResponse so that it can be shared.
56         if (ar.exception == null && ar.result != null) {
57             int[] result = (int[]) ar.result;
58             int channel = result[0];
59             byte[] selectResponse = null;
60             if (result.length > 1) {
61                 selectResponse = new byte[result.length - 1];
62                 for (int i = 1; i < result.length; ++i) {
63                     selectResponse[i - 1] = (byte) result[i];
64                 }
65             }
66             openChannelResp = new IccOpenLogicalChannelResponse(
67                     channel, IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
68         } else {
69             if (ar.result == null) {
70                 Rlog.e(LOG_TAG, "Empty response");
71             }
72             if (ar.exception != null) {
73                 Rlog.e(LOG_TAG, "Exception", ar.exception);
74             }
75 
76             int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
77             if (ar.exception instanceof CommandException) {
78                 CommandException.Error error =
79                         ((CommandException) (ar.exception)).getCommandError();
80                 if (error == CommandException.Error.MISSING_RESOURCE) {
81                     errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
82                 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
83                     errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
84                 }
85             }
86             openChannelResp = new IccOpenLogicalChannelResponse(
87                     IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
88         }
89 
90         Rlog.v(LOG_TAG, "Response: " + openChannelResp);
91         return openChannelResp;
92     }
93 }
94