1 /******************************************************************************
2  *
3  *  Copyright (C) 2024 The Android Open Source Project.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include <android-base/logging.h>
20 #include <android-base/stringprintf.h>
21 #include <nativehelper/ScopedPrimitiveArray.h>
22 
23 #include "JavaClassConstants.h"
24 #include "NativeT4tNfcee.h"
25 #include "NfcJniUtil.h"
26 #include "nfc_config.h"
27 using android::base::StringPrintf;
28 
29 namespace android {
30 /*******************************************************************************
31 **
32 ** Function:        t4tNfceeManager_doClearNdefData
33 **
34 ** Description:     This API will set all the NFCEE NDEF data to zero.
35 **                  This API can be called regardless of NDEF file lock state.
36 **
37 ** Returns:         boolean : Return the Success or fail of the operation.
38 **                  Return "True" when operation is successful. else "False"
39 **
40 *******************************************************************************/
t4tNfceeManager_doClearNdefData(JNIEnv * e,jobject o)41 jint t4tNfceeManager_doClearNdefData(JNIEnv* e, jobject o) {
42   LOG(DEBUG) << StringPrintf("%s: enter", __func__);
43 
44   return NativeT4tNfcee::getInstance().t4tClearData(e, o);
45 }
46 /*******************************************************************************
47 **
48 ** Function:        t4tNfceeManager_isNdefOperationOngoing
49 **
50 ** Description:     This API will get NDEF NFCEE status.
51 **
52 ** Returns:         boolean : Indicates whether NDEF NFCEE Read or write
53 **                            operation is under process
54 **                  Return "True" when operation is in progress. else "False"
55 **
56 *******************************************************************************/
t4tNfceeManager_isNdefOperationOngoing(JNIEnv * e,jobject o)57 jboolean t4tNfceeManager_isNdefOperationOngoing(JNIEnv* e, jobject o) {
58   LOG(DEBUG) << StringPrintf("%s: enter", __func__);
59 
60   return NativeT4tNfcee::getInstance().getT4tStatus(e, o);
61 }
62 /*******************************************************************************
63 **
64 ** Function:        t4tNfceeManager_isNdefNfceeEmulationSupported
65 **
66 ** Description:     This API will tell whether NDEF NFCEE emulation is supported
67 **                  or not.
68 **
69 ** Returns:         boolean : Indicates whether NDEF NFCEE emulation is
70 **                            supported or not
71 **                  Return "True" when feature supported. else "False"
72 **
73 *******************************************************************************/
t4tNfceeManager_isNdefNfceeEmulationSupported(JNIEnv * e,jobject o)74 jboolean t4tNfceeManager_isNdefNfceeEmulationSupported(JNIEnv* e, jobject o) {
75   LOG(DEBUG) << StringPrintf("%s: enter", __func__);
76 
77   return NativeT4tNfcee::getInstance().isT4tNdefNfceeEmulationSupported(e, o);
78 }
79 /*******************************************************************************
80  **
81  ** Function:        nfcManager_doWriteData
82  **
83  ** Description:     Write the data into the NDEF NFCEE file of the specific
84  **                  file ID
85  **
86  ** Returns:         Return the size of data written
87  **                  Return negative number of error code
88  **
89  *******************************************************************************/
t4tNfceeManager_doWriteData(JNIEnv * e,jobject o,jbyteArray fileId,jbyteArray data)90 jint t4tNfceeManager_doWriteData(JNIEnv* e, jobject o, jbyteArray fileId,
91                                  jbyteArray data) {
92   LOG(DEBUG) << StringPrintf("%s: enter", __func__);
93 
94   return NativeT4tNfcee::getInstance().t4tWriteData(e, o, fileId, data);
95 }
96 /*******************************************************************************
97 **
98 ** Function:        nfcManager_doReadData
99 **
100 ** Description:     Read the data from the NDEF NFCEE file of the specific file
101 **                  ID.
102 **
103 ** Returns:         byte[] : all the data previously written to the specific
104 **                  file ID.
105 **                  Return one byte '0xFF' if the data was never written to the
106 **                  specific file ID,
107 **                  Return null if reading fails.
108 **
109 *******************************************************************************/
t4tNfceeManager_doReadData(JNIEnv * e,jobject o,jbyteArray fileId)110 jbyteArray t4tNfceeManager_doReadData(JNIEnv* e, jobject o, jbyteArray fileId) {
111   LOG(DEBUG) << StringPrintf("%s: enter", __func__);
112   return NativeT4tNfcee::getInstance().t4tReadData(e, o, fileId);
113 }
114 /*******************************************************************************
115 **
116 ** Function:        t4tNfceeManager_getNdefNfceeRouteId
117 **
118 ** Description:     Get the NDEF NFCEE Route ID.
119 **
120 ** Returns:         NDEF NFCEE route ID if available
121 **
122 *******************************************************************************/
t4tNfceeManager_getNdefNfceeRouteId()123 jint t4tNfceeManager_getNdefNfceeRouteId() {
124   return NfcConfig::getUnsigned(NAME_DEFAULT_NDEF_NFCEE_ROUTE, 0x10);
125 }
126 /*******************************************************************************
127 **
128 ** Function:        t4tNfceeManager_getT4TNfceePowerState
129 **
130 ** Description:     Get the T4T Nfcee power state supported.
131 **                  e: JVM environment.
132 **                  o: Java object.
133 **                  mode: Not used.
134 **
135 ** Returns:         None
136 **
137 *******************************************************************************/
t4tNfceeManager_getT4TNfceePowerState(JNIEnv * e,jobject o)138 jint t4tNfceeManager_getT4TNfceePowerState(JNIEnv* e, jobject o) {
139   return NfcConfig::getUnsigned(NAME_DEFAULT_T4TNFCEE_AID_POWER_STATE, 0x01);
140 }
141 /*****************************************************************************
142  **
143  ** Description:     JNI functions
144  **
145  *****************************************************************************/
146 static JNINativeMethod gMethods[] = {
147     {"doWriteData", "([B[B)I", (void*)t4tNfceeManager_doWriteData},
148     {"doReadData", "([B)[B", (void*)t4tNfceeManager_doReadData},
149     {"doClearNdefData", "()Z", (void*)t4tNfceeManager_doClearNdefData},
150     {"isNdefOperationOngoing", "()Z",
151      (void*)t4tNfceeManager_isNdefOperationOngoing},
152     {"isNdefNfceeEmulationSupported", "()Z",
153      (void*)t4tNfceeManager_isNdefNfceeEmulationSupported},
154     {"getT4TNfceePowerState", "()I",
155      (void*)t4tNfceeManager_getT4TNfceePowerState},
156     {"getNdefNfceeRouteId", "()I", (void*)t4tNfceeManager_getNdefNfceeRouteId},
157 };
158 
159 /*******************************************************************************
160  **
161  ** Function:        register_com_android_nfc_NativeT4tNfcee
162  **
163  ** Description:     Regisgter JNI functions with Java Virtual Machine.
164  **                  e: Environment of JVM.
165  **
166  ** Returns:         Status of registration.
167  **
168  *******************************************************************************/
register_com_android_nfc_NativeT4tNfcee(JNIEnv * e)169 int register_com_android_nfc_NativeT4tNfcee(JNIEnv* e) {
170   return jniRegisterNativeMethods(e, gNativeT4tNfceeClassName, gMethods,
171                                   NELEM(gMethods));
172 }
173 }  // namespace android
174