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 /* Properties values */
33 #define PROPERTY_NFC_DISCOVERY_A 4
34 #define PROPERTY_NFC_DISCOVERY_B 5
35 #define PROPERTY_NFC_DISCOVERY_F 6
36 #define PROPERTY_NFC_DISCOVERY_15693 7
37 #define PROPERTY_NFC_DISCOVERY_NCFIP 8
38 
39 /* Error codes */
40 #define ERROR_BUFFER_TOO_SMALL (-12)
41 #define ERROR_INSUFFICIENT_RESOURCES (-9)
42 
43 /* Pre-defined tag type values. These must match the values in
44  * Ndef.java in the framework.
45  */
46 #define NDEF_UNKNOWN_TYPE (-1)
47 #define NDEF_TYPE1_TAG 1
48 #define NDEF_TYPE2_TAG 2
49 #define NDEF_TYPE3_TAG 3
50 #define NDEF_TYPE4_TAG 4
51 #define NDEF_MIFARE_CLASSIC_TAG 101
52 
53 /* Pre-defined card read/write state values. These must match the values in
54  * Ndef.java in the framework.
55  */
56 #define NDEF_MODE_READ_ONLY 1
57 #define NDEF_MODE_READ_WRITE 2
58 #define NDEF_MODE_UNKNOWN 3
59 
60 /* Name strings for target types. These *must* match the values in
61  * TagTechnology.java */
62 #define TARGET_TYPE_UNKNOWN (-1)
63 #define TARGET_TYPE_ISO14443_3A 1
64 #define TARGET_TYPE_ISO14443_3B 2
65 #define TARGET_TYPE_ISO14443_4 3
66 #define TARGET_TYPE_FELICA 4
67 #define TARGET_TYPE_V 5
68 #define TARGET_TYPE_NDEF 6
69 #define TARGET_TYPE_NDEF_FORMATABLE 7
70 #define TARGET_TYPE_MIFARE_CLASSIC 8
71 #define TARGET_TYPE_MIFARE_UL 9
72 #define TARGET_TYPE_KOVIO_BARCODE 10
73 
74 // define a few NXP error codes that NFC service expects;
75 // see external/libnfc-nxp/src/phLibNfcStatus.h;
76 // see external/libnfc-nxp/inc/phNfcStatus.h
77 #define NFCSTATUS_SUCCESS (0x0000)
78 #define NFCSTATUS_FAILED (0x00FF)
79 
80 /* Pre-defined route type values. These must match the values in
81  * RoutingManager.cpp.
82  */
83 #define ROUTE_TYPE_TECH 0x01
84 #define ROUTE_TYPE_PROTO 0x02
85 #define ROUTE_TYPE_AID 0x04
86 
87 #define ROUTE_SWITCH_ON 0x01
88 #define ROUTE_SWITCH_OFF 0x02
89 #define ROUTE_BATTERY_OFF 0x04
90 #define ROUTE_SCREEN_OFF_UNLOCKED 0x08
91 #define ROUTE_SCREEN_ON_LOCKED 0x10
92 #define ROUTE_SCREEN_OFF_LOCKED 0x20
93 
94 struct nfc_jni_native_data {
95   /* Thread handle */
96   pthread_t thread;
97   int running;
98 
99   /* Our VM */
100   JavaVM* vm;
101   int env_version;
102 
103   /* Reference to the NFCManager instance */
104   jobject manager;
105 
106   /* Cached objects */
107   jobject cached_NfcTag;
108 
109   /* Secure Element selected */
110   int seId;
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 }  // namespace android
146