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