• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef __COM_ANDROID_NFC_JNI_H__
18 #define __COM_ANDROID_NFC_JNI_H__
19 
20 #define LOG_TAG "NFCJNI"
21 
22 #include <JNIHelp.h>
23 #include <jni.h>
24 
25 #include <pthread.h>
26 #include <sys/queue.h>
27 
28 extern "C" {
29 #include <phNfcStatus.h>
30 #include <phNfcTypes.h>
31 #include <phNfcIoctlCode.h>
32 #include <phLibNfc.h>
33 #include <phDal4Nfc_messageQueueLib.h>
34 #include <phFriNfc_NdefMap.h>
35 #include <cutils/log.h>
36 #include <com_android_nfc_list.h>
37 #include <semaphore.h>
38 
39 }
40 #include <cutils/properties.h> // for property_get
41 
42 
43 /* Discovery modes -- keep in sync with NFCManager.DISCOVERY_MODE_* */
44 #define DISCOVERY_MODE_TAG_READER         0
45 #define DISCOVERY_MODE_NFCIP1             1
46 #define DISCOVERY_MODE_CARD_EMULATION     2
47 
48 #define DISCOVERY_MODE_TABLE_SIZE         3
49 
50 #define DISCOVERY_MODE_DISABLED           0
51 #define DISCOVERY_MODE_ENABLED            1
52 
53 #define MODE_P2P_TARGET                   0
54 #define MODE_P2P_INITIATOR                1
55 
56 /* Properties values */
57 #define PROPERTY_LLCP_LTO                 0
58 #define PROPERTY_LLCP_MIU                 1
59 #define PROPERTY_LLCP_WKS                 2
60 #define PROPERTY_LLCP_OPT                 3
61 #define PROPERTY_NFC_DISCOVERY_A          4
62 #define PROPERTY_NFC_DISCOVERY_B          5
63 #define PROPERTY_NFC_DISCOVERY_F          6
64 #define PROPERTY_NFC_DISCOVERY_15693      7
65 #define PROPERTY_NFC_DISCOVERY_NCFIP      8
66 
67 /* Error codes */
68 #define ERROR_BUFFER_TOO_SMALL            -12
69 #define ERROR_INSUFFICIENT_RESOURCES      -9
70 
71 /* Pre-defined card read/write state values. These must match the values in
72  * Ndef.java in the framework.
73  */
74 
75 #define NDEF_UNKNOWN_TYPE                -1
76 #define NDEF_TYPE1_TAG                   1
77 #define NDEF_TYPE2_TAG                   2
78 #define NDEF_TYPE3_TAG                   3
79 #define NDEF_TYPE4_TAG                   4
80 #define NDEF_MIFARE_CLASSIC_TAG          101
81 #define NDEF_ICODE_SLI_TAG               102
82 
83 /* Pre-defined tag type values. These must match the values in
84  * Ndef.java in the framework.
85  */
86 
87 #define NDEF_MODE_READ_ONLY              1
88 #define NDEF_MODE_READ_WRITE             2
89 #define NDEF_MODE_UNKNOWN                3
90 
91 
92 /* Name strings for target types. These *must* match the values in TagTechnology.java */
93 #define TARGET_TYPE_UNKNOWN               -1
94 #define TARGET_TYPE_ISO14443_3A           1
95 #define TARGET_TYPE_ISO14443_3B           2
96 #define TARGET_TYPE_ISO14443_4            3
97 #define TARGET_TYPE_FELICA                4
98 #define TARGET_TYPE_ISO15693              5
99 #define TARGET_TYPE_NDEF                  6
100 #define TARGET_TYPE_NDEF_FORMATABLE       7
101 #define TARGET_TYPE_MIFARE_CLASSIC        8
102 #define TARGET_TYPE_MIFARE_UL             9
103 
104 #define SMX_SECURE_ELEMENT_ID   11259375
105 
106 /* Maximum byte length of an AID. */
107 #define AID_MAXLEN                        16
108 
109 /* Utility macros for logging */
110 #define GET_LEVEL(status) ((status)==NFCSTATUS_SUCCESS)?ANDROID_LOG_DEBUG:ANDROID_LOG_WARN
111 
112 #if 0
113   #define LOG_CALLBACK(funcName, status)  LOG_PRI(GET_LEVEL(status), LOG_TAG, "Callback: %s() - status=0x%04x[%s]", funcName, status, nfc_jni_get_status_name(status));
114   #define TRACE(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
115   #define TRACE_ENABLED 1
116 #else
117   #define LOG_CALLBACK(...)
118   #define TRACE(...)
119   #define TRACE_ENABLED 0
120 #endif
121 
122 struct nfc_jni_native_data
123 {
124    /* Thread handle */
125    pthread_t thread;
126    int running;
127 
128    /* Our VM */
129    JavaVM *vm;
130    int env_version;
131 
132    /* Reference to the NFCManager instance */
133    jobject manager;
134 
135    /* Cached objects */
136    jobject cached_NfcTag;
137    jobject cached_P2pDevice;
138 
139    /* Target discovery configuration */
140    int discovery_modes_state[DISCOVERY_MODE_TABLE_SIZE];
141    phLibNfc_sADD_Cfg_t discovery_cfg;
142    phLibNfc_Registry_Info_t registry_info;
143 
144    /* Secure Element selected */
145    int seId;
146 
147    /* LLCP params */
148    int lto;
149    int miu;
150    int wks;
151    int opt;
152 
153    /* Tag detected */
154    jobject tag;
155 
156    /* Lib Status */
157    NFCSTATUS status;
158 
159    /* p2p modes */
160    int p2p_initiator_modes;
161    int p2p_target_modes;
162 
163 };
164 
165 typedef struct nfc_jni_native_monitor
166 {
167    /* Mutex protecting native library against reentrance */
168    pthread_mutex_t reentrance_mutex;
169 
170    /* Mutex protecting native library against concurrency */
171    pthread_mutex_t concurrency_mutex;
172 
173    /* List used to track pending semaphores waiting for callback */
174    struct listHead sem_list;
175 
176    /* List used to track incoming socket requests (and associated sync variables) */
177    LIST_HEAD(, nfc_jni_listen_data) incoming_socket_head;
178    pthread_mutex_t incoming_socket_mutex;
179    pthread_cond_t  incoming_socket_cond;
180 
181 } nfc_jni_native_monitor_t;
182 
183 typedef struct nfc_jni_callback_data
184 {
185    /* Semaphore used to wait for callback */
186    sem_t sem;
187 
188    /* Used to store the status sent by the callback */
189    NFCSTATUS status;
190 
191    /* Used to provide a local context to the callback */
192    void* pContext;
193 
194 } nfc_jni_callback_data_t;
195 
196 typedef struct nfc_jni_listen_data
197 {
198    /* LLCP server socket receiving the connection request */
199    phLibNfc_Handle pServerSocket;
200 
201    /* LLCP socket created from the connection request */
202    phLibNfc_Handle pIncomingSocket;
203 
204    /* List entries */
205    LIST_ENTRY(nfc_jni_listen_data) entries;
206 
207 } nfc_jni_listen_data_t;
208 
209 /* TODO: treat errors and add traces */
210 #define REENTRANCE_LOCK()        pthread_mutex_lock(&nfc_jni_get_monitor()->reentrance_mutex)
211 #define REENTRANCE_UNLOCK()      pthread_mutex_unlock(&nfc_jni_get_monitor()->reentrance_mutex)
212 #define CONCURRENCY_LOCK()       pthread_mutex_lock(&nfc_jni_get_monitor()->concurrency_mutex)
213 #define CONCURRENCY_UNLOCK()     pthread_mutex_unlock(&nfc_jni_get_monitor()->concurrency_mutex)
214 
215 namespace android {
216 
217 extern JavaVM *vm;
218 
219 JNIEnv *nfc_get_env();
220 
221 bool nfc_cb_data_init(nfc_jni_callback_data* pCallbackData, void* pContext);
222 void nfc_cb_data_deinit(nfc_jni_callback_data* pCallbackData);
223 void nfc_cb_data_releaseAll();
224 
225 const char* nfc_jni_get_status_name(NFCSTATUS status);
226 int nfc_jni_cache_object(JNIEnv *e, const char *clsname,
227    jobject *cached_obj);
228 struct nfc_jni_native_data* nfc_jni_get_nat(JNIEnv *e, jobject o);
229 struct nfc_jni_native_data* nfc_jni_get_nat_ext(JNIEnv *e);
230 nfc_jni_native_monitor_t* nfc_jni_init_monitor(void);
231 nfc_jni_native_monitor_t* nfc_jni_get_monitor(void);
232 
233 int get_technology_type(phNfc_eRemDevType_t type, uint8_t sak);
234 void nfc_jni_get_technology_tree(JNIEnv* e, phLibNfc_RemoteDevList_t* devList,
235                         uint8_t count, jintArray* techList, jintArray* handleList,
236                         jintArray* typeList);
237 
238 /* P2P */
239 phLibNfc_Handle nfc_jni_get_p2p_device_handle(JNIEnv *e, jobject o);
240 jshort nfc_jni_get_p2p_device_mode(JNIEnv *e, jobject o);
241 
242 /* TAG */
243 jint nfc_jni_get_connected_technology(JNIEnv *e, jobject o);
244 jint nfc_jni_get_connected_technology_libnfc_type(JNIEnv *e, jobject o);
245 phLibNfc_Handle nfc_jni_get_connected_handle(JNIEnv *e, jobject o);
246 jintArray nfc_jni_get_nfc_tag_type(JNIEnv *e, jobject o);
247 
248 /* LLCP */
249 phLibNfc_Handle nfc_jni_get_nfc_socket_handle(JNIEnv *e, jobject o);
250 
251 int register_com_android_nfc_NativeNfcManager(JNIEnv *e);
252 int register_com_android_nfc_NativeNfcTag(JNIEnv *e);
253 int register_com_android_nfc_NativeP2pDevice(JNIEnv *e);
254 int register_com_android_nfc_NativeLlcpConnectionlessSocket(JNIEnv *e);
255 int register_com_android_nfc_NativeLlcpServiceSocket(JNIEnv *e);
256 int register_com_android_nfc_NativeLlcpSocket(JNIEnv *e);
257 int register_com_android_nfc_NativeNfcSecureElement(JNIEnv *e);
258 
259 } // namespace android
260 
261 #endif
262