Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2012 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 #pragma once
     18 #undef LOG_TAG
     19 #define LOG_TAG "BrcmNfcJni"
     20 #include <JNIHelp.h>
     21 #include <jni.h>
     22 #include <pthread.h>
     23 #include <sys/queue.h>
     24 #include <semaphore.h>
     25 
     26 
     27 /* Discovery modes -- keep in sync with NFCManager.DISCOVERY_MODE_* */
     28 #define DISCOVERY_MODE_TAG_READER         0
     29 #define DISCOVERY_MODE_NFCIP1             1
     30 #define DISCOVERY_MODE_CARD_EMULATION     2
     31 #define DISCOVERY_MODE_TABLE_SIZE         3
     32 
     33 #define DISCOVERY_MODE_DISABLED           0
     34 #define DISCOVERY_MODE_ENABLED            1
     35 
     36 #define MODE_P2P_TARGET                   0
     37 #define MODE_P2P_INITIATOR                1
     38 
     39 
     40 /* Properties values */
     41 #define PROPERTY_LLCP_LTO                 0
     42 #define PROPERTY_LLCP_MIU                 1
     43 #define PROPERTY_LLCP_WKS                 2
     44 #define PROPERTY_LLCP_OPT                 3
     45 #define PROPERTY_NFC_DISCOVERY_A          4
     46 #define PROPERTY_NFC_DISCOVERY_B          5
     47 #define PROPERTY_NFC_DISCOVERY_F          6
     48 #define PROPERTY_NFC_DISCOVERY_15693      7
     49 #define PROPERTY_NFC_DISCOVERY_NCFIP      8
     50 
     51 
     52 /* Error codes */
     53 #define ERROR_BUFFER_TOO_SMALL            -12
     54 #define ERROR_INSUFFICIENT_RESOURCES      -9
     55 
     56 
     57 /* Pre-defined tag type values. These must match the values in
     58  * Ndef.java in the framework.
     59  */
     60 #define NDEF_UNKNOWN_TYPE                -1
     61 #define NDEF_TYPE1_TAG                   1
     62 #define NDEF_TYPE2_TAG                   2
     63 #define NDEF_TYPE3_TAG                   3
     64 #define NDEF_TYPE4_TAG                   4
     65 #define NDEF_MIFARE_CLASSIC_TAG          101
     66 
     67 
     68 /* Pre-defined card read/write state values. These must match the values in
     69  * Ndef.java in the framework.
     70  */
     71 #define NDEF_MODE_READ_ONLY              1
     72 #define NDEF_MODE_READ_WRITE             2
     73 #define NDEF_MODE_UNKNOWN                3
     74 
     75 
     76 /* Name strings for target types. These *must* match the values in TagTechnology.java */
     77 #define TARGET_TYPE_UNKNOWN               -1
     78 #define TARGET_TYPE_ISO14443_3A           1
     79 #define TARGET_TYPE_ISO14443_3B           2
     80 #define TARGET_TYPE_ISO14443_4            3
     81 #define TARGET_TYPE_FELICA                4
     82 #define TARGET_TYPE_ISO15693              5
     83 #define TARGET_TYPE_NDEF                  6
     84 #define TARGET_TYPE_NDEF_FORMATABLE       7
     85 #define TARGET_TYPE_MIFARE_CLASSIC        8
     86 #define TARGET_TYPE_MIFARE_UL             9
     87 #define TARGET_TYPE_KOVIO_BARCODE         10
     88 
     89 
     90 //define a few NXP error codes that NFC service expects;
     91 //see external/libnfc-nxp/src/phLibNfcStatus.h;
     92 //see external/libnfc-nxp/inc/phNfcStatus.h
     93 #define NFCSTATUS_SUCCESS (0x0000)
     94 #define NFCSTATUS_FAILED (0x00FF)
     95 
     96 //default general trasceive timeout in millisecond
     97 #define DEFAULT_GENERAL_TRANS_TIMEOUT  1000
     98 
     99 struct nfc_jni_native_data
    100 {
    101    /* Thread handle */
    102    pthread_t thread;
    103    int running;
    104 
    105    /* Our VM */
    106    JavaVM *vm;
    107    int env_version;
    108 
    109    /* Reference to the NFCManager instance */
    110    jobject manager;
    111 
    112    /* Cached objects */
    113    jobject cached_NfcTag;
    114    jobject cached_P2pDevice;
    115 
    116    /* Secure Element selected */
    117    int seId;
    118 
    119    /* LLCP params */
    120    int lto;
    121    int miu;
    122    int wks;
    123    int opt;
    124 
    125    int tech_mask;
    126 
    127    /* Tag detected */
    128    jobject tag;
    129 
    130    int tHandle;
    131    int tProtocols[16];
    132    int handles[16];
    133 };
    134 
    135 
    136 class ScopedAttach {
    137  public:
    138   ScopedAttach(JavaVM* vm, JNIEnv** env) : vm_(vm) {
    139     vm_->AttachCurrentThread(env, NULL);
    140   }
    141 
    142   ~ScopedAttach() {
    143     vm_->DetachCurrentThread();
    144   }
    145 
    146  private:
    147   JavaVM* vm_;
    148 };
    149 
    150 
    151 extern "C"
    152 {
    153     jint JNI_OnLoad(JavaVM *jvm, void *reserved);
    154 }
    155 
    156 
    157 namespace android
    158 {
    159     int nfc_jni_cache_object (JNIEnv *e, const char *clsname, jobject *cached_obj);
    160     int nfc_jni_get_nfc_socket_handle (JNIEnv *e, jobject o);
    161     struct nfc_jni_native_data* nfc_jni_get_nat (JNIEnv *e, jobject o);
    162     int register_com_android_nfc_NativeNfcManager (JNIEnv *e);
    163     int register_com_android_nfc_NativeNfcTag (JNIEnv *e);
    164     int register_com_android_nfc_NativeP2pDevice (JNIEnv *e);
    165     int register_com_android_nfc_NativeLlcpConnectionlessSocket (JNIEnv *e);
    166     int register_com_android_nfc_NativeLlcpServiceSocket (JNIEnv *e);
    167     int register_com_android_nfc_NativeLlcpSocket (JNIEnv *e);
    168     int register_com_android_nfc_NativeNfcSecureElement (JNIEnv *e);
    169 } // namespace android
    170