1/*
2 * Copyright (C) 2006 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;
18
19
20/**
21 * {@hide}
22 */
23public class
24IccIoResult {
25
26    private static final String UNKNOWN_ERROR = "unknown";
27
28    private String getErrorString() {
29        // Errors from 3gpp 11.11 9.4.1
30        // Additional Errors from ETSI 102.221
31        //
32        // All error codes below are copied directly from their respective specification
33        // without modification except in cases where necessary string formatting has been omitted.
34        switch(sw1) {
35            case 0x62:
36                switch(sw2) {
37                    case 0x00: return "No information given,"
38                               + " state of non volatile memory unchanged";
39                    case 0x81: return "Part of returned data may be corrupted";
40                    case 0x82: return "End of file/record reached before reading Le bytes";
41                    case 0x83: return "Selected file invalidated";
42                    case 0x84: return "Selected file in termination state";
43                    case 0xF1: return "More data available";
44                    case 0xF2: return "More data available and proactive command pending";
45                    case 0xF3: return "Response data available";
46                }
47                break;
48            case 0x63:
49                if (sw2 >> 4 == 0x0C) {
50                    return "Command successful but after using an internal"
51                        + "update retry routine but Verification failed";
52                }
53                switch(sw2) {
54                    case 0xF1: return "More data expected";
55                    case 0xF2: return "More data expected and proactive command pending";
56                }
57                break;
58            case 0x64:
59                switch(sw2) {
60                    case 0x00: return "No information given,"
61                               + " state of non-volatile memory unchanged";
62                }
63                break;
64            case 0x65:
65                switch(sw2) {
66                    case 0x00: return "No information given, state of non-volatile memory changed";
67                    case 0x81: return "Memory problem";
68                }
69                break;
70            case 0x67:
71                switch(sw2) {
72                    case 0x00: return "incorrect parameter P3";
73                    default: return "The interpretation of this status word is command dependent";
74                }
75                // break;
76            case 0x6B: return "incorrect parameter P1 or P2";
77            case 0x6D: return "unknown instruction code given in the command";
78            case 0x6E: return "wrong instruction class given in the command";
79            case 0x6F:
80                switch(sw2) {
81                    case 0x00: return "technical problem with no diagnostic given";
82                    default: return "The interpretation of this status word is command dependent";
83                }
84                // break;
85            case 0x68:
86                switch(sw2) {
87                    case 0x00: return "No information given";
88                    case 0x81: return "Logical channel not supported";
89                    case 0x82: return "Secure messaging not supported";
90                }
91                break;
92            case 0x69:
93                switch(sw2) {
94                    case 0x00: return "No information given";
95                    case 0x81: return "Command incompatible with file structure";
96                    case 0x82: return "Security status not satisfied";
97                    case 0x83: return "Authentication/PIN method blocked";
98                    case 0x84: return "Referenced data invalidated";
99                    case 0x85: return "Conditions of use not satisfied";
100                    case 0x86: return "Command not allowed (no EF selected)";
101                    case 0x89: return "Command not allowed - secure channel -"
102                               + " security not satisfied";
103                }
104                break;
105            case 0x6A:
106                switch(sw2) {
107                    case 0x80: return "Incorrect parameters in the data field";
108                    case 0x81: return "Function not supported";
109                    case 0x82: return "File not found";
110                    case 0x83: return "Record not found";
111                    case 0x84: return "Not enough memory space";
112                    case 0x86: return "Incorrect parameters P1 to P2";
113                    case 0x87: return "Lc inconsistent with P1 to P2";
114                    case 0x88: return "Referenced data not found";
115                }
116                break;
117            case 0x90: return null; // success
118            case 0x91: return null; // success
119            //Status Code 0x92 has contradictory meanings from 11.11 and 102.221 10.2.1.1
120            case 0x92:
121                if (sw2 >> 4 == 0) {
122                    return "command successful but after using an internal update retry routine";
123                }
124                switch(sw2) {
125                    case 0x40: return "memory problem";
126                }
127                break;
128            case 0x93:
129                switch(sw2) {
130                    case 0x00:
131                        return "SIM Application Toolkit is busy. Command cannot be executed"
132                            + " at present, further normal commands are allowed.";
133                }
134                break;
135            case 0x94:
136                switch(sw2) {
137                    case 0x00: return "no EF selected";
138                    case 0x02: return "out f range (invalid address)";
139                    case 0x04: return "file ID not found/pattern not found";
140                    case 0x08: return "file is inconsistent with the command";
141                }
142                break;
143            case 0x98:
144                switch(sw2) {
145                    case 0x02: return "no CHV initialized";
146                    case 0x04: return "access condition not fulfilled/"
147                            + "unsuccessful CHV verification, at least one attempt left/"
148                            + "unsuccessful UNBLOCK CHV verification, at least one attempt left/"
149                            + "authentication failed";
150                    case 0x08: return "in contradiction with CHV status";
151                    case 0x10: return "in contradiction with invalidation status";
152                    case 0x40: return "unsuccessful CHV verification, no attempt left/"
153                            + "unsuccessful UNBLOCK CHV verification, no attempt left/"
154                            + "CHV blocked"
155                            + "UNBLOCK CHV blocked";
156                    case 0x50: return "increase cannot be performed, Max value reached";
157                }
158                break;
159            case 0x9E: return null; // success
160            case 0x9F: return null; // success
161        }
162        return UNKNOWN_ERROR;
163    }
164
165
166    public int sw1;
167    public int sw2;
168
169    public byte[] payload;
170
171    public IccIoResult(int sw1, int sw2, byte[] payload) {
172        this.sw1 = sw1;
173        this.sw2 = sw2;
174        this.payload = payload;
175    }
176
177    public IccIoResult(int sw1, int sw2, String hexString) {
178        this(sw1, sw2, IccUtils.hexStringToBytes(hexString));
179    }
180
181    @Override
182    public String toString() {
183        return "IccIoResult sw1:0x" + Integer.toHexString(sw1) + " sw2:0x"
184                + Integer.toHexString(sw2) + ((!success()) ? " Error: " + getErrorString() : "");
185    }
186
187    /**
188     * true if this operation was successful
189     * See GSM 11.11 Section 9.4
190     * (the fun stuff is absent in 51.011)
191     */
192    public boolean success() {
193        return sw1 == 0x90 || sw1 == 0x91 || sw1 == 0x9e || sw1 == 0x9f;
194    }
195
196    /**
197     * Returns exception on error or null if success
198     */
199    public IccException getException() {
200        if (success()) return null;
201
202        switch (sw1) {
203            case 0x94:
204                if (sw2 == 0x08) {
205                    return new IccFileTypeMismatch();
206                } else {
207                    return new IccFileNotFound();
208                }
209            default:
210                return new IccException("sw1:" + sw1 + " sw2:" + sw2);
211        }
212    }
213}
214