1367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly/*
2367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * Copyright (C) 2011 The Android Open Source Project
3367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly *
4367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * Licensed under the Apache License, Version 2.0 (the "License");
5367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * you may not use this file except in compliance with the License.
6367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * You may obtain a copy of the License at
7367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly *
8367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly *      http://www.apache.org/licenses/LICENSE-2.0
9367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly *
10367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * Unless required by applicable law or agreed to in writing, software
11367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * distributed under the License is distributed on an "AS IS" BASIS,
12367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * See the License for the specific language governing permissions and
14367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly * limitations under the License.
15367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly */
16367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
17367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellypackage com.android.nfc_extras;
18367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
19367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.annotation.SdkConstant;
20367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.annotation.SdkConstant.SdkConstantType;
21367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.os.Binder;
22367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.os.Bundle;
23367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.os.RemoteException;
24367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
25bb951c893973691554f49d2e725985125f866b27Jeff Hamiltonimport java.io.IOException;
26bb951c893973691554f49d2e725985125f866b27Jeff Hamilton
27367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellypublic class NfcExecutionEnvironment {
28cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly    private final NfcAdapterExtras mExtras;
29bb951c893973691554f49d2e725985125f866b27Jeff Hamilton    private final Binder mToken;
30367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
31dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen    // Exception types that can be thrown by NfcService
32dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen    // 1:1 mapped to EE_ERROR_ types in NfcService
33dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen    private static final int EE_ERROR_IO = -1;
34dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen    private static final int EE_ERROR_ALREADY_OPEN = -2;
35dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen    private static final int EE_ERROR_INIT = -3;
36dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen    private static final int EE_ERROR_LISTEN_MODE = -4;
37dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen    private static final int EE_ERROR_EXT_FIELD = -5;
38dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen    private static final int EE_ERROR_NFC_DISABLED = -6;
39dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen
40367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
41367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Broadcast Action: An ISO-DEP AID was selected.
42367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
43367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>This happens as the result of a 'SELECT AID' command from an
44367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * external NFC reader/writer.
45367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
46367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>Always contains the extra field {@link #EXTRA_AID}
47367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
48367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p class="note">
49367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission
50367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * to receive.
51367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
52367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
53367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public static final String ACTION_AID_SELECTED =
54367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        "com.android.nfc_extras.action.AID_SELECTED";
55367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
56367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
57367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Mandatory byte array extra field in {@link #ACTION_AID_SELECTED}.
58367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
59367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>Contains the AID selected.
60367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * @hide
61367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
62367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public static final String EXTRA_AID = "com.android.nfc_extras.extra.AID";
63367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
642f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    /**
652f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * Broadcast action: A filtered APDU was received.
662f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
672f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>This happens when an APDU of interest was matched by the Nfc adapter,
682f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * for instance as the result of matching an externally-configured filter.
692f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
702f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>The filter configuration mechanism is not currently defined.
712f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
722f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>Always contains the extra field {@link EXTRA_APDU_BYTES}.
732f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
742f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * @hide
752f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     */
762f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    public static final String ACTION_APDU_RECEIVED =
772f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly        "com.android.nfc_extras.action.APDU_RECEIVED";
782f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly
792f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    /**
802f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * Mandatory byte array extra field in {@link #ACTION_APDU_RECEIVED}.
812f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
822f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>Contains the bytes of the received APDU.
832f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
842f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * @hide
852f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     */
862f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    public static final String EXTRA_APDU_BYTES =
872f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly        "com.android.nfc_extras.extra.APDU_BYTES";
882f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly
892f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    /**
902f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * Broadcast action: An EMV card removal event was detected.
912f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
922f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * @hide
932f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     */
942f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    public static final String ACTION_EMV_CARD_REMOVAL =
952f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly        "com.android.nfc_extras.action.EMV_CARD_REMOVAL";
962f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly
972f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    /**
982f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * Broadcast action: An adapter implementing MIFARE Classic via card
992f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * emulation detected that a block has been accessed.
1002f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
1012f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>This may only be issued for the first block that the reader
1022f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * authenticates to.
1032f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
1042f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>May contain the extra field {@link #EXTRA_MIFARE_BLOCK}.
1052f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
1062f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * @hide
1072f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     */
1082f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    public static final String ACTION_MIFARE_ACCESS_DETECTED =
1092f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly        "com.android.nfc_extras.action.MIFARE_ACCESS_DETECTED";
1102f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly
1112f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    /**
1122f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * Optional integer extra field in {@link #ACTION_MIFARE_ACCESS_DETECTED}.
1132f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
1142f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>Provides the block number being accessed.  If not set, the block
1152f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * number being accessed is unknown.
1162f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
1172f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * @hide
1182f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     */
1192f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    public static final String EXTRA_MIFARE_BLOCK =
1202f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly        "com.android.nfc_extras.extra.MIFARE_BLOCK";
1212f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly
122cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly    NfcExecutionEnvironment(NfcAdapterExtras extras) {
123cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly        mExtras = extras;
124bb951c893973691554f49d2e725985125f866b27Jeff Hamilton        mToken = new Binder();
125367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
126367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
127367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
128367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Open the NFC Execution Environment on its contact interface.
129367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
130dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * <p>Opening a channel to the the secure element may fail
131dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * for a number of reasons:
132dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * <ul>
133dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * <li>NFC must be enabled for the connection to the SE to be opened.
134dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * If it is disabled at the time of this call, an {@link EeNfcDisabledException}
135dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * is thrown.
136dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     *
137dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * <li>Only one process may open the secure element at a time. Additionally,
138dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * this method is not reentrant. If the secure element is already opened,
139dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * either by this process or by a different process, an {@link EeAlreadyOpenException}
140dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * is thrown.
141dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     *
142dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * <li>If the connection to the secure element could not be initialized,
143dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * an {@link EeInitializationException} is thrown.
144dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     *
145dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * <li>If the secure element or the NFC controller is activated in listen
146dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * mode - that is, it is talking over the contactless interface - an
147dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * {@link EeListenModeException} is thrown.
148dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     *
149dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * <li>If the NFC controller is in a field powered by a remote device,
150dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * such as a payment terminal, an {@link EeExternalFieldException} is
151dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * thrown.
152dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * </ul>
153367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>All other NFC functionality is disabled while the NFC-EE is open
154367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * on its contact interface, so make sure to call {@link #close} once complete.
155367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
156367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p class="note">
157367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
158367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
159dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * @throws EeAlreadyOpenException if the NFC-EE is already open
160dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * @throws EeNfcDisabledException if NFC is disabled
161dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * @throws EeInitializationException if the Secure Element could not be initialized
162dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * @throws EeListenModeException if the NFCC or Secure Element is activated in listen mode
163dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * @throws EeExternalFieldException if the NFCC is in the presence of a remote-powered field
164dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen     * @throws EeIoException if an unknown error occurs
165367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
166dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen    public void open() throws EeIOException {
167367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        try {
168bb951c893973691554f49d2e725985125f866b27Jeff Hamilton            Bundle b = mExtras.getService().open(mExtras.mPackageName, mToken);
169367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly            throwBundle(b);
170367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        } catch (RemoteException e) {
171cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            mExtras.attemptDeadServiceRecovery(e);
172dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen            throw new EeIOException("NFC Service was dead, try again");
173367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        }
174367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
175367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
176367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
177367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Close the NFC Execution Environment on its contact interface.
178367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
179367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p class="note">
180367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
181367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
182367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * @throws IOException if the NFC-EE is already open, or some other error occurs
183367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
184367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public void close() throws IOException {
185367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        try {
186bb951c893973691554f49d2e725985125f866b27Jeff Hamilton            throwBundle(mExtras.getService().close(mExtras.mPackageName, mToken));
187367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        } catch (RemoteException e) {
188cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            mExtras.attemptDeadServiceRecovery(e);
189cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            throw new IOException("NFC Service was dead");
190367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        }
191367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
192367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
193367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
194367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Send raw commands to the NFC-EE and receive the response.
195367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
196367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p class="note">
197367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
198367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
199367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * @throws IOException if the NFC-EE is not open, or some other error occurs
200367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
201367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public byte[] transceive(byte[] in) throws IOException {
202367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        Bundle b;
203367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        try {
204bb951c893973691554f49d2e725985125f866b27Jeff Hamilton            b = mExtras.getService().transceive(mExtras.mPackageName, in);
205367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        } catch (RemoteException e) {
206cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            mExtras.attemptDeadServiceRecovery(e);
207cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            throw new IOException("NFC Service was dead, need to re-open");
208367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        }
209367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        throwBundle(b);
210367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        return b.getByteArray("out");
211367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
212367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
213dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen    private static void throwBundle(Bundle b) throws EeIOException {
214dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen        switch (b.getInt("e")) {
215dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen            case EE_ERROR_NFC_DISABLED:
216dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen                throw new EeNfcDisabledException(b.getString("m"));
217dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen            case EE_ERROR_IO:
218dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen                throw new EeIOException(b.getString("m"));
219dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen            case EE_ERROR_INIT:
220dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen                throw new EeInitializationException(b.getString("m"));
221dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen            case EE_ERROR_EXT_FIELD:
222dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen                throw new EeExternalFieldException(b.getString("m"));
223dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen            case EE_ERROR_LISTEN_MODE:
224dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen                throw new EeListenModeException(b.getString("m"));
225dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen            case EE_ERROR_ALREADY_OPEN:
226dd6b959546dfc7b8858fa8e08c4eea080f60b03aMartijn Coenen                throw new EeAlreadyOpenException(b.getString("m"));
227367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        }
228367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
229367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly}
230