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
17package com.android.internal.telephony.uicc.euicc.apdu;
18
19import android.telephony.IccOpenLogicalChannelResponse;
20
21/**
22 * The exception of failing to execute an APDU command. It can be caused by an error happening on
23 * opening the basic or logical channel, or the response of the APDU command is not success
24 * ({@link ApduSender#STATUS_NO_ERROR}).
25 *
26 * @hide
27 */
28public class ApduException extends Exception {
29    private final int mApduStatus;
30
31    /** Creates an exception with the apduStatus code of the response of an APDU command. */
32    public ApduException(int apduStatus) {
33        super();
34        mApduStatus = apduStatus;
35    }
36
37    public ApduException(String message) {
38        super(message);
39        mApduStatus = 0;
40    }
41
42    /**
43     * @return The error status of the response of an APDU command. An error status can be any
44     *         positive 16-bit integer (i.e., SW1 & SW2) other than
45     *         {@link ApduSender#STATUS_NO_ERROR} which means no error. For an error encountered
46     *         when opening a logical channel before the APDU command gets sent, this is not the
47     *         status defined in {@link IccOpenLogicalChannelResponse}. In this caes, 0 will be
48     *         returned and the message of this exception will have the detailed error information.
49     */
50    public int getApduStatus() {
51        return mApduStatus;
52    }
53
54    /** @return The hex string of the error status. */
55    public String getStatusHex() {
56        return Integer.toHexString(mApduStatus);
57    }
58
59    @Override
60    public String getMessage() {
61        return super.getMessage() + " (apduStatus=" + getStatusHex() + ")";
62    }
63}
64