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 <nativehelper/ScopedLocalRef.h> 20 21 #include <vector> 22 23 #include "NfcJniUtil.h" 24 #include "SyncEvent.h" 25 #include "nfa_api.h" 26 27 typedef enum { OP_READ = 0, OP_WRITE, OP_CLEAR } T4TNFCEE_OPERATIONS_t; 28 29 typedef enum { 30 STATUS_SUCCESS = 0, 31 STATUS_FAILED = -1, 32 ERROR_RF_ACTIVATED = -2, 33 ERROR_NFC_NOT_ON = -3, 34 ERROR_INVALID_FILE_ID = -4, 35 ERROR_INVALID_LENGTH = -5, 36 ERROR_CONNECTION_FAILED = -6, 37 ERROR_EMPTY_PAYLOAD = -7, 38 ERROR_NDEF_VALIDATION_FAILED = -8, 39 ERROR_WRITE_PERMISSION = -9, 40 ERROR_NFC_OFF_TRIGGERED = -10, 41 } T4TNFCEE_STATUS_t; 42 43 class NativeT4tNfcee { 44 public: 45 /***************************************************************************** 46 ** 47 ** Function: getInstance 48 ** 49 ** Description: Get the NativeT4tNfcee singleton object. 50 ** 51 ** Returns: NativeT4tNfcee object. 52 ** 53 *******************************************************************************/ 54 static NativeT4tNfcee& getInstance(); 55 56 /******************************************************************************* 57 ** 58 ** Function: initialize 59 ** 60 ** Description: Initialize all member variables. 61 ** 62 ** Returns: None. 63 ** 64 *******************************************************************************/ 65 void initialize(void); 66 /***************************************************************************** 67 ** 68 ** Function: onNfccShutdown 69 ** 70 ** Description: This api shall be called in NFC OFF case. 71 ** 72 ** Returns: none. 73 ** 74 *******************************************************************************/ 75 void onNfccShutdown(); 76 77 /******************************************************************************* 78 ** 79 ** Function: t4tWriteData 80 ** 81 ** Description: Write the data into the T4T file of the specific file ID 82 ** 83 ** Returns: Return the size of data written 84 ** Return negative number of error code 85 ** 86 *******************************************************************************/ 87 int t4tWriteData(JNIEnv* e, jobject o, jbyteArray fileId, jbyteArray data); 88 /******************************************************************************* 89 ** 90 ** Function: t4tClearData 91 ** 92 ** Description: This API will set all the T4T NFCEE NDEF data to zero. 93 ** This API can be called regardless of NDEF file lock state. 94 ** 95 ** Returns: boolean : Return the Success or fail of the operation. 96 ** Return "True" when operation is successful. else "False" 97 ** 98 *******************************************************************************/ 99 jboolean t4tClearData(JNIEnv* e, jobject o); 100 /******************************************************************************* 101 ** 102 ** Function: performT4tClearData 103 ** 104 ** Description: This api clear the T4T Nfcee data 105 ** 106 ** Returns: boolean : Return the Success or fail of the operation. 107 ** Return "True" when operation is successful. else "False" 108 ** 109 *******************************************************************************/ 110 jboolean performT4tClearData(uint8_t* fileId); 111 /******************************************************************************* 112 ** 113 ** Function: getT4tStatus 114 ** 115 ** Description: This API will get T4T NDEF NFCEE status. 116 ** 117 ** Returns: boolean : Indicates whether T4T NDEF NFCEE Read or write 118 ** operation is under process 119 ** Return "True" when operation is in progress. else "False" 120 ** 121 ** 122 *******************************************************************************/ 123 jboolean getT4tStatus(JNIEnv* e, jobject o); 124 /******************************************************************************* 125 ** 126 ** Function: t4tNfceeManager_isT4tNdefNfceeEmulationSupported 127 ** 128 ** Description: This API will tell whether T4T NDEF NFCEE emulation is 129 ** supported or not. 130 ** 131 ** Returns: boolean : Indicates whether T4T NDEF NFCEE emulation is 132 ** supported or not 133 ** Return "True" when feature supported. else "False" 134 ** 135 *******************************************************************************/ 136 jboolean isT4tNdefNfceeEmulationSupported(JNIEnv* e, jobject o); 137 /******************************************************************************* 138 ** 139 ** Function: t4tReadData 140 ** 141 ** Description: Read the data from the T4T file of the specific file ID. 142 ** 143 ** Returns: byte[] : all the data previously written to the specific 144 ** file ID. 145 ** Return one byte '0xFF' if the data was never written to 146 *the 147 ** specific file ID, 148 ** Return null if reading fails. 149 ** 150 *******************************************************************************/ 151 jbyteArray t4tReadData(JNIEnv* e, jobject o, jbyteArray fileId); 152 153 /******************************************************************************* 154 ** 155 ** Function: t4tReadComplete 156 ** 157 ** Description: Updates read data to the waiting READ API 158 ** 159 ** Returns: none 160 ** 161 *******************************************************************************/ 162 void t4tReadComplete(tNFA_STATUS status, tNFA_RX_DATA data); 163 164 /******************************************************************************* 165 ** 166 ** Function: t4tWriteComplete 167 ** 168 ** Description: Returns write complete information 169 ** 170 ** Returns: none 171 ** 172 *******************************************************************************/ 173 void t4tWriteComplete(tNFA_STATUS status, tNFA_RX_DATA data); 174 /******************************************************************************* 175 ** 176 ** Function: t4tClearComplete 177 ** 178 ** Description: Update T4T clear data status, waiting T4tClearData API. 179 ** 180 ** Returns: none 181 ** 182 *******************************************************************************/ 183 void t4tClearComplete(tNFA_STATUS status); 184 185 /******************************************************************************* 186 ** 187 ** Function: isT4tNfceeBusy 188 ** 189 ** Description: Returns True if T4tNfcee operation is ongoing else false 190 ** 191 ** Returns: true/false 192 ** 193 *******************************************************************************/ 194 bool isT4tNfceeBusy(void); 195 196 /******************************************************************************* 197 ** 198 ** Function: t4tNfceeEventHandler 199 ** 200 ** Description: Handles callback events received from lower layer 201 ** 202 ** Returns: none 203 ** 204 *******************************************************************************/ 205 void eventHandler(uint8_t event, tNFA_CONN_EVT_DATA* eventData); 206 /******************************************************************************* 207 ** 208 ** Function: checkAndUpdateT4TAid 209 ** 210 ** Description: Check and update T4T Ndef Nfcee AID. 211 ** 212 ** Returns: void 213 ** 214 *******************************************************************************/ 215 void checkAndUpdateT4TAid(uint8_t* t4tAid, uint8_t* t4tNdefAidLen); 216 217 private: 218 bool mBusy; 219 static NativeT4tNfcee sNativeT4tNfceeInstance; 220 static bool sIsNfcOffTriggered; 221 SyncEvent mT4tNfcOffEvent; 222 SyncEvent mT4tNfcEeRWCEvent; // Read, Write, & Clear event 223 SyncEvent mT4tNfcEeEvent; 224 tNFA_RX_DATA mReadData; 225 tNFA_STATUS mT4tOpStatus = NFA_STATUS_FAILED; 226 tNFA_STATUS mT4tNfcEeEventStat = NFA_STATUS_FAILED; 227 std::vector<uint8_t> sRxDataBuffer; 228 NativeT4tNfcee(); 229 230 /******************************************************************************* 231 ** 232 ** Function: openConnection 233 ** 234 ** Description: Open T4T Nfcee Connection 235 ** 236 ** Returns: Status 237 ** 238 *******************************************************************************/ 239 tNFA_STATUS openConnection(); 240 241 /******************************************************************************* 242 ** 243 ** Function: closeConnection 244 ** 245 ** Description: Close T4T Nfcee Connection 246 ** 247 ** Returns: Status 248 ** 249 *******************************************************************************/ 250 tNFA_STATUS closeConnection(); 251 252 /******************************************************************************* 253 ** 254 ** Function: setup 255 ** 256 ** Description: stops Discovery and opens T4TNFCEE connection 257 ** 258 ** Returns: Status 259 ** 260 *******************************************************************************/ 261 tNFA_STATUS setup(void); 262 263 /******************************************************************************* 264 ** 265 ** Function: cleanup 266 ** 267 ** Description: closes connection and starts discovery 268 ** 269 ** Returns: Status 270 ** 271 *******************************************************************************/ 272 void cleanup(void); 273 274 /******************************************************************************* 275 ** 276 ** Function: validatePreCondition 277 ** 278 ** Description: Runs precondition checks for requested operation 279 ** 280 ** Returns: Status 281 ** 282 *******************************************************************************/ 283 T4TNFCEE_STATUS_t validatePreCondition(T4TNFCEE_OPERATIONS_t op, 284 jbyteArray fileId, 285 jbyteArray data = nullptr); 286 287 /******************************************************************************* 288 ** 289 ** Function: setBusy 290 ** 291 ** Description: Sets busy flag indicating T4T operation is ongoing 292 ** 293 ** Returns: none 294 ** 295 *******************************************************************************/ 296 void setBusy(); 297 298 /******************************************************************************* 299 ** 300 ** Function: resetBusy 301 ** 302 ** Description: Resets busy flag indicating T4T operation is completed 303 ** 304 ** Returns: none 305 ** 306 *******************************************************************************/ 307 void resetBusy(); 308 /******************************************************************************* 309 ** 310 ** Function: getT4TNfceeAid 311 ** 312 ** Description: Get the T4T Nfcee AID. 313 ** 314 ** Returns: T4T AID: vector<uint8_t> 315 ** 316 *******************************************************************************/ 317 std::vector<uint8_t> getT4TNfceeAid(); 318 }; 319