Home | History | Annotate | Download | only in jni
      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 #else
    116   #define LOG_CALLBACK(...)
    117   #define TRACE(...)
    118 #endif
    119 
    120 struct nfc_jni_native_data
    121 {
    122    /* Thread handle */
    123    pthread_t thread;
    124    int running;
    125 
    126    /* Our VM */
    127    JavaVM *vm;
    128    int env_version;
    129 
    130    /* Reference to the NFCManager instance */
    131    jobject manager;
    132 
    133    /* Cached objects */
    134    jobject cached_NfcTag;
    135    jobject cached_P2pDevice;
    136 
    137    /* Target discovery configuration */
    138    int discovery_modes_state[DISCOVERY_MODE_TABLE_SIZE];
    139    phLibNfc_sADD_Cfg_t discovery_cfg;
    140    phLibNfc_Registry_Info_t registry_info;
    141 
    142    /* Secure Element selected */
    143    int seId;
    144 
    145    /* LLCP params */
    146    int lto;
    147    int miu;
    148    int wks;
    149    int opt;
    150 
    151    /* Tag detected */
    152    jobject tag;
    153 
    154    /* Lib Status */
    155    NFCSTATUS status;
    156 
    157    /* p2p modes */
    158    int p2p_initiator_modes;
    159    int p2p_target_modes;
    160 
    161 };
    162 
    163 typedef struct nfc_jni_native_monitor
    164 {
    165    /* Mutex protecting native library against reentrance */
    166    pthread_mutex_t reentrance_mutex;
    167 
    168    /* Mutex protecting native library against concurrency */
    169    pthread_mutex_t concurrency_mutex;
    170 
    171    /* List used to track pending semaphores waiting for callback */
    172    struct listHead sem_list;
    173 
    174    /* List used to track incoming socket requests (and associated sync variables) */
    175    LIST_HEAD(, nfc_jni_listen_data) incoming_socket_head;
    176    pthread_mutex_t incoming_socket_mutex;
    177    pthread_cond_t  incoming_socket_cond;
    178 
    179 } nfc_jni_native_monitor_t;
    180 
    181 typedef struct nfc_jni_callback_data
    182 {
    183    /* Semaphore used to wait for callback */
    184    sem_t sem;
    185 
    186    /* Used to store the status sent by the callback */
    187    NFCSTATUS status;
    188 
    189    /* Used to provide a local context to the callback */
    190    void* pContext;
    191 
    192 } nfc_jni_callback_data_t;
    193 
    194 typedef struct nfc_jni_listen_data
    195 {
    196    /* LLCP server socket receiving the connection request */
    197    phLibNfc_Handle pServerSocket;
    198 
    199    /* LLCP socket created from the connection request */
    200    phLibNfc_Handle pIncomingSocket;
    201 
    202    /* List entries */
    203    LIST_ENTRY(nfc_jni_listen_data) entries;
    204 
    205 } nfc_jni_listen_data_t;
    206 
    207 /* TODO: treat errors and add traces */
    208 #define REENTRANCE_LOCK()        pthread_mutex_lock(&nfc_jni_get_monitor()->reentrance_mutex)
    209 #define REENTRANCE_UNLOCK()      pthread_mutex_unlock(&nfc_jni_get_monitor()->reentrance_mutex)
    210 #define CONCURRENCY_LOCK()       pthread_mutex_lock(&nfc_jni_get_monitor()->concurrency_mutex)
    211 #define CONCURRENCY_UNLOCK()     pthread_mutex_unlock(&nfc_jni_get_monitor()->concurrency_mutex)
    212 
    213 namespace android {
    214 
    215 extern JavaVM *vm;
    216 
    217 JNIEnv *nfc_get_env();
    218 
    219 bool nfc_cb_data_init(nfc_jni_callback_data* pCallbackData, void* pContext);
    220 void nfc_cb_data_deinit(nfc_jni_callback_data* pCallbackData);
    221 void nfc_cb_data_releaseAll();
    222 
    223 const char* nfc_jni_get_status_name(NFCSTATUS status);
    224 int nfc_jni_cache_object(JNIEnv *e, const char *clsname,
    225    jobject *cached_obj);
    226 struct nfc_jni_native_data* nfc_jni_get_nat(JNIEnv *e, jobject o);
    227 struct nfc_jni_native_data* nfc_jni_get_nat_ext(JNIEnv *e);
    228 nfc_jni_native_monitor_t* nfc_jni_init_monitor(void);
    229 nfc_jni_native_monitor_t* nfc_jni_get_monitor(void);
    230 
    231 int get_technology_type(phNfc_eRemDevType_t type, uint8_t sak);
    232 void nfc_jni_get_technology_tree(JNIEnv* e, phLibNfc_RemoteDevList_t* devList,
    233                         uint8_t count, jintArray* techList, jintArray* handleList,
    234                         jintArray* typeList);
    235 
    236 /* P2P */
    237 phLibNfc_Handle nfc_jni_get_p2p_device_handle(JNIEnv *e, jobject o);
    238 jshort nfc_jni_get_p2p_device_mode(JNIEnv *e, jobject o);
    239 
    240 /* TAG */
    241 jint nfc_jni_get_connected_technology(JNIEnv *e, jobject o);
    242 jint nfc_jni_get_connected_technology_libnfc_type(JNIEnv *e, jobject o);
    243 phLibNfc_Handle nfc_jni_get_connected_handle(JNIEnv *e, jobject o);
    244 jintArray nfc_jni_get_nfc_tag_type(JNIEnv *e, jobject o);
    245 
    246 /* LLCP */
    247 phLibNfc_Handle nfc_jni_get_nfc_socket_handle(JNIEnv *e, jobject o);
    248 
    249 int register_com_android_nfc_NativeNfcManager(JNIEnv *e);
    250 int register_com_android_nfc_NativeNfcTag(JNIEnv *e);
    251 int register_com_android_nfc_NativeP2pDevice(JNIEnv *e);
    252 int register_com_android_nfc_NativeLlcpConnectionlessSocket(JNIEnv *e);
    253 int register_com_android_nfc_NativeLlcpServiceSocket(JNIEnv *e);
    254 int register_com_android_nfc_NativeLlcpSocket(JNIEnv *e);
    255 int register_com_android_nfc_NativeNfcSecureElement(JNIEnv *e);
    256 
    257 } // namespace android
    258 
    259 #endif
    260