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