IsoDep.java revision a924973f22aedc580708625e4babb6deabc6b4d3
1/*
2 * Copyright (C) 2010 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 android.nfc.tech;
18
19import android.nfc.Tag;
20import android.os.Bundle;
21import android.os.RemoteException;
22import android.util.Log;
23
24import java.io.IOException;
25
26/**
27 * Provides access to ISO-DEP (ISO 14443-4) properties and I/O operations on a {@link Tag}.
28 *
29 * <p>Acquire an {@link IsoDep} object using {@link #get}.
30 * <p>The primary ISO-DEP I/O operation is {@link #transceive}. Applications must
31 * implement their own protocol stack on top of {@link #transceive}.
32 * <p>Tags that enumerate the {@link IsoDep} technology in {@link Tag#getTechList}
33 * will also enumerate
34 * {@link NfcA} or {@link NfcB} (since IsoDep builds on top of either of these).
35 *
36 * <p class="note"><strong>Note:</strong> Methods that perform I/O operations
37 * require the {@link android.Manifest.permission#NFC} permission.
38 */
39public final class IsoDep extends BasicTagTechnology {
40    private static final String TAG = "NFC";
41
42    /** @hide */
43    public static final String EXTRA_HI_LAYER_RESP = "hiresp";
44    /** @hide */
45    public static final String EXTRA_HIST_BYTES = "histbytes";
46
47    private byte[] mHiLayerResponse = null;
48    private byte[] mHistBytes = null;
49
50    /**
51     * Get an instance of {@link IsoDep} for the given tag.
52     * <p>Does not cause any RF activity and does not block.
53     * <p>Returns null if {@link IsoDep} was not enumerated in {@link Tag#getTechList}.
54     * This indicates the tag does not support ISO-DEP.
55     *
56     * @param tag an ISO-DEP compatible tag
57     * @return ISO-DEP object
58     */
59    public static IsoDep get(Tag tag) {
60        if (!tag.hasTech(TagTechnology.ISO_DEP)) return null;
61        try {
62            return new IsoDep(tag);
63        } catch (RemoteException e) {
64            return null;
65        }
66    }
67
68    /** @hide */
69    public IsoDep(Tag tag)
70            throws RemoteException {
71        super(tag, TagTechnology.ISO_DEP);
72        Bundle extras = tag.getTechExtras(TagTechnology.ISO_DEP);
73        if (extras != null) {
74            mHiLayerResponse = extras.getByteArray(EXTRA_HI_LAYER_RESP);
75            mHistBytes = extras.getByteArray(EXTRA_HIST_BYTES);
76        }
77    }
78
79    /**
80     * Set the timeout of {@link #transceive} in milliseconds.
81     * <p>The timeout only applies to ISO-DEP {@link #transceive}, and is
82     * reset to a default value when {@link #close} is called.
83     * <p>Setting a longer timeout may be useful when performing
84     * transactions that require a long processing time on the tag
85     * such as key generation.
86     *
87     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
88     *
89     * @param timeout timeout value in milliseconds
90     */
91    public void setTimeout(int timeout) {
92        try {
93            mTag.getTagService().setIsoDepTimeout(timeout);
94        } catch (RemoteException e) {
95            Log.e(TAG, "NFC service dead", e);
96        }
97    }
98
99    /**
100     * Return the ISO-DEP historical bytes for {@link NfcA} tags.
101     * <p>Does not cause any RF activity and does not block.
102     * <p>The historical bytes can be used to help identify a tag. They are present
103     * only on {@link IsoDep} tags that are based on {@link NfcA} RF technology.
104     * If this tag is not {@link NfcA} then null is returned.
105     * <p>In ISO 14443-4 terminology, the historical bytes are a subset of the RATS
106     * response.
107     *
108     * @return ISO-DEP historical bytes, or null if this is not a {@link NfcA} tag
109     */
110    public byte[] getHistoricalBytes() {
111        return mHistBytes;
112    }
113
114    /**
115     * Return the higher layer response bytes for {@link NfcB} tags.
116     * <p>Does not cause any RF activity and does not block.
117     * <p>The higher layer response bytes can be used to help identify a tag.
118     * They are present only on {@link IsoDep} tags that are based on {@link NfcB}
119     * RF technology. If this tag is not {@link NfcB} then null is returned.
120     * <p>In ISO 14443-4 terminology, the higher layer bytes are a subset of the
121     * ATTRIB response.
122     *
123     * @return ISO-DEP historical bytes, or null if this is not a {@link NfcB} tag
124     */
125    public byte[] getHiLayerResponse() {
126        return mHiLayerResponse;
127    }
128
129    /**
130     * Send raw ISO-DEP data to the tag and receive the response.
131     *
132     * <p>Applications must only send the INF payload, and not the start of frame and
133     * end of frame indicators. Applications do not need to fragment the payload, it
134     * will be automatically fragmented and defragmented by {@link #transceive} if
135     * it exceeds FSD/FSC limits.
136     *
137     * <p>This is an I/O operation and will block until complete. It must
138     * not be called from the main application thread. A blocked call will be canceled with
139     * {@link IOException} if {@link #close} is called from another thread.
140     *
141     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
142     *
143     * @param data command bytes to send, must not be null
144     * @return response bytes received, will not be null
145     * @throws TagLostException if the tag leaves the field
146     * @throws IOException if there is an I/O failure, or this operation is canceled
147     */
148    public byte[] transceive(byte[] data) throws IOException {
149        return transceive(data, true);
150    }
151}
152