NfcExecutionEnvironment.java revision 2f9ad8b499cb150d5cdfe84a9795992c5dd42cff
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 java.io.IOException;
20367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
21367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.annotation.SdkConstant;
22367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.annotation.SdkConstant.SdkConstantType;
23367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.content.Context;
24367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.nfc.INfcAdapterExtras;
25367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.nfc.NfcAdapter;
26367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.os.Binder;
27367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.os.Bundle;
28367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.os.IBinder;
29367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellyimport android.os.RemoteException;
30367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
31367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pellypublic class NfcExecutionEnvironment {
32cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly    private final NfcAdapterExtras mExtras;
33367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
34367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
35367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Broadcast Action: An ISO-DEP AID was selected.
36367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
37367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>This happens as the result of a 'SELECT AID' command from an
38367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * external NFC reader/writer.
39367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
40367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>Always contains the extra field {@link #EXTRA_AID}
41367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
42367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p class="note">
43367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission
44367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * to receive.
45367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
46367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
47367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public static final String ACTION_AID_SELECTED =
48367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        "com.android.nfc_extras.action.AID_SELECTED";
49367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
50367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
51367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Mandatory byte array extra field in {@link #ACTION_AID_SELECTED}.
52367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
53367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>Contains the AID selected.
54367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * @hide
55367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
56367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public static final String EXTRA_AID = "com.android.nfc_extras.extra.AID";
57367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
582f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    /**
592f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * Broadcast action: A filtered APDU was received.
602f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
612f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>This happens when an APDU of interest was matched by the Nfc adapter,
622f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * for instance as the result of matching an externally-configured filter.
632f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
642f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>The filter configuration mechanism is not currently defined.
652f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
662f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>Always contains the extra field {@link EXTRA_APDU_BYTES}.
672f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
682f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * @hide
692f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     */
702f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    public static final String ACTION_APDU_RECEIVED =
712f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly        "com.android.nfc_extras.action.APDU_RECEIVED";
722f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly
732f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    /**
742f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * Mandatory byte array extra field in {@link #ACTION_APDU_RECEIVED}.
752f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
762f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>Contains the bytes of the received APDU.
772f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
782f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * @hide
792f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     */
802f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    public static final String EXTRA_APDU_BYTES =
812f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly        "com.android.nfc_extras.extra.APDU_BYTES";
822f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly
832f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    /**
842f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * Broadcast action: An EMV card removal event was detected.
852f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
862f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * @hide
872f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     */
882f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    public static final String ACTION_EMV_CARD_REMOVAL =
892f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly        "com.android.nfc_extras.action.EMV_CARD_REMOVAL";
902f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly
912f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    /**
922f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * Broadcast action: An adapter implementing MIFARE Classic via card
932f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * emulation detected that a block has been accessed.
942f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
952f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>This may only be issued for the first block that the reader
962f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * authenticates to.
972f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
982f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>May contain the extra field {@link #EXTRA_MIFARE_BLOCK}.
992f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
1002f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * @hide
1012f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     */
1022f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    public static final String ACTION_MIFARE_ACCESS_DETECTED =
1032f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly        "com.android.nfc_extras.action.MIFARE_ACCESS_DETECTED";
1042f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly
1052f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    /**
1062f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * Optional integer extra field in {@link #ACTION_MIFARE_ACCESS_DETECTED}.
1072f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
1082f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * <p>Provides the block number being accessed.  If not set, the block
1092f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * number being accessed is unknown.
1102f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     *
1112f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     * @hide
1122f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly     */
1132f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly    public static final String EXTRA_MIFARE_BLOCK =
1142f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly        "com.android.nfc_extras.extra.MIFARE_BLOCK";
1152f9ad8b499cb150d5cdfe84a9795992c5dd42cffmike wakerly
116cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly    NfcExecutionEnvironment(NfcAdapterExtras extras) {
117cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly        mExtras = extras;
118367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
119367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
120367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
121367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Open the NFC Execution Environment on its contact interface.
122367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
123367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>Only one process may open the secure element at a time. If it is
124367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * already open, an {@link IOException} is thrown.
125367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
126367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p>All other NFC functionality is disabled while the NFC-EE is open
127367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * on its contact interface, so make sure to call {@link #close} once complete.
128367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
129367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p class="note">
130367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
131367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
132367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * @throws IOException if the NFC-EE is already open, or some other error occurs
133367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
134367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public void open() throws IOException {
135367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        try {
136cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            Bundle b = mExtras.getService().open(new Binder());
137367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly            throwBundle(b);
138367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        } catch (RemoteException e) {
139cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            mExtras.attemptDeadServiceRecovery(e);
140cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            throw new IOException("NFC Service was dead, try again");
141367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        }
142367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
143367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
144367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
145367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Close the NFC Execution Environment on its contact interface.
146367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
147367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p class="note">
148367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
149367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
150367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * @throws IOException if the NFC-EE is already open, or some other error occurs
151367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
152367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public void close() throws IOException {
153367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        try {
154cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            throwBundle(mExtras.getService().close());
155367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        } catch (RemoteException e) {
156cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            mExtras.attemptDeadServiceRecovery(e);
157cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            throw new IOException("NFC Service was dead");
158367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        }
159367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
160367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
161367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    /**
162367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Send raw commands to the NFC-EE and receive the response.
163367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
164367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * <p class="note">
165367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
166367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     *
167367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     * @throws IOException if the NFC-EE is not open, or some other error occurs
168367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly     */
169367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    public byte[] transceive(byte[] in) throws IOException {
170367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        Bundle b;
171367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        try {
172cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            b = mExtras.getService().transceive(in);
173367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        } catch (RemoteException e) {
174cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            mExtras.attemptDeadServiceRecovery(e);
175cc9ee72bd42bb40b1852f907f58305adde12ecc2Nick Pelly            throw new IOException("NFC Service was dead, need to re-open");
176367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        }
177367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        throwBundle(b);
178367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        return b.getByteArray("out");
179367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
180367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly
181367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    private static void throwBundle(Bundle b) throws IOException {
182367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        if (b.getInt("e") == -1) {
183367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly            throw new IOException(b.getString("m"));
184367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly        }
185367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly    }
186367f41f8f61126c2ab34a34cc676756a9fc23ac2Nick Pelly}
187