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