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 /* 18 * Tag-reading, tag-writing operations. 19 */ 20 21 #pragma once 22 #include <vector> 23 #include "NfcJniUtil.h" 24 #include "SyncEvent.h" 25 26 #include "nfa_rw_api.h" 27 28 class NfcTag { 29 public: 30 enum ActivationState { Idle, Sleep, Active }; 31 static const int MAX_NUM_TECHNOLOGY = 32 11; // max number of technologies supported by one or more tags 33 int mTechList[MAX_NUM_TECHNOLOGY]; // array of NFC technologies according to 34 // NFC service 35 int mTechHandles[MAX_NUM_TECHNOLOGY]; // array of tag handles according to 36 // NFC service 37 int mTechLibNfcTypes[MAX_NUM_TECHNOLOGY]; // array of detailed tag types 38 // according to NFC service 39 int mNumTechList; // current number of NFC technologies in the list 40 41 /******************************************************************************* 42 ** 43 ** Function: NfcTag 44 ** 45 ** Description: Initialize member variables. 46 ** 47 ** Returns: None 48 ** 49 *******************************************************************************/ 50 NfcTag(); 51 52 /******************************************************************************* 53 ** 54 ** Function: getInstance 55 ** 56 ** Description: Get a reference to the singleton NfcTag object. 57 ** 58 ** Returns: Reference to NfcTag object. 59 ** 60 *******************************************************************************/ 61 static NfcTag& getInstance(); 62 63 /******************************************************************************* 64 ** 65 ** Function: initialize 66 ** 67 ** Description: Reset member variables. 68 ** native: Native data. 69 ** Returns: None 70 ** 71 *******************************************************************************/ 72 void initialize(nfc_jni_native_data* native); 73 74 /******************************************************************************* 75 ** 76 ** Function: abort 77 ** 78 ** Description: Unblock all operations. 79 ** 80 ** Returns: None 81 ** 82 *******************************************************************************/ 83 void abort(); 84 85 /******************************************************************************* 86 ** 87 ** Function: connectionEventHandler 88 ** 89 ** Description: Handle connection-related events. 90 ** event: event code. 91 ** data: pointer to event data. 92 ** 93 ** Returns: None 94 ** 95 *******************************************************************************/ 96 void connectionEventHandler(uint8_t event, tNFA_CONN_EVT_DATA* data); 97 98 /******************************************************************************* 99 ** 100 ** Function: isActivated 101 ** 102 ** Description: Is tag activated? 103 ** 104 ** Returns: True if tag is activated. 105 ** 106 *******************************************************************************/ 107 bool isActivated(); 108 109 /******************************************************************************* 110 ** 111 ** Function: getActivationState 112 ** 113 ** Description: What is the current state: Idle, Sleep, or Activated. 114 ** 115 ** Returns: Idle, Sleep, or Activated. 116 ** 117 *******************************************************************************/ 118 ActivationState getActivationState(); 119 120 /******************************************************************************* 121 ** 122 ** Function: setDeactivationState 123 ** 124 ** Description: Set the current state: Idle or Sleep. 125 ** deactivated: state of deactivation. 126 ** 127 ** Returns: None. 128 ** 129 *******************************************************************************/ 130 void setDeactivationState(tNFA_DEACTIVATED& deactivated); 131 132 /******************************************************************************* 133 ** 134 ** Function: setActivationState 135 ** 136 ** Description: Set the current state to Active. 137 ** 138 ** Returns: None. 139 ** 140 *******************************************************************************/ 141 void setActivationState(); 142 143 /******************************************************************************* 144 ** 145 ** Function: getProtocol 146 ** 147 ** Description: Get the protocol of the current tag. 148 ** 149 ** Returns: Protocol number. 150 ** 151 *******************************************************************************/ 152 tNFC_PROTOCOL getProtocol(); 153 154 /******************************************************************************* 155 ** 156 ** Function: isP2pDiscovered 157 ** 158 ** Description: Does the peer support P2P? 159 ** 160 ** Returns: True if the peer supports P2P. 161 ** 162 *******************************************************************************/ 163 bool isP2pDiscovered(); 164 165 /******************************************************************************* 166 ** 167 ** Function: selectP2p 168 ** 169 ** Description: Select the preferred P2P technology if there is a choice. 170 ** 171 ** Returns: None 172 ** 173 *******************************************************************************/ 174 void selectP2p(); 175 176 /******************************************************************************* 177 ** 178 ** Function: selectFirstTag 179 ** 180 ** Description: When multiple tags are discovered, just select the first 181 *one to activate. 182 ** 183 ** Returns: None 184 ** 185 *******************************************************************************/ 186 void selectFirstTag(); 187 188 /******************************************************************************* 189 ** 190 ** Function: getT1tMaxMessageSize 191 ** 192 ** Description: Get the maximum size (octet) that a T1T can store. 193 ** 194 ** Returns: Maximum size in octets. 195 ** 196 *******************************************************************************/ 197 int getT1tMaxMessageSize(); 198 199 /******************************************************************************* 200 ** 201 ** Function: isMifareUltralight 202 ** 203 ** Description: Whether the currently activated tag is Mifare Ultralight. 204 ** 205 ** Returns: True if tag is Mifare Ultralight. 206 ** 207 *******************************************************************************/ 208 bool isMifareUltralight(); 209 210 /******************************************************************************* 211 ** 212 ** Function: isMifareDESFire 213 ** 214 ** Description: Whether the currently activated tag is Mifare DESFire. 215 ** 216 ** Returns: True if tag is Mifare DESFire. 217 ** 218 *******************************************************************************/ 219 bool isMifareDESFire(); 220 221 /******************************************************************************* 222 ** 223 ** Function: isFelicaLite 224 ** 225 ** Description: Whether the currently activated tag is Felica Lite. 226 ** 227 ** Returns: True if tag is Felica Lite. 228 ** 229 *******************************************************************************/ 230 bool isFelicaLite(); 231 232 /******************************************************************************* 233 ** 234 ** Function: isT2tNackResponse 235 ** 236 ** Description: Whether the response is a T2T NACK response. 237 ** See NFC Digital Protocol Technical Specification 238 *(2010-11-17). 239 ** Chapter 9 (Type 2 Tag Platform), section 9.6 (READ). 240 ** response: buffer contains T2T response. 241 ** responseLen: length of the response. 242 ** 243 ** Returns: True if the response is NACK 244 ** 245 *******************************************************************************/ 246 bool isT2tNackResponse(const uint8_t* response, uint32_t responseLen); 247 248 /******************************************************************************* 249 ** 250 ** Function: isNdefDetectionTimedOut 251 ** 252 ** Description: Whether NDEF-detection algorithm has timed out. 253 ** 254 ** Returns: True if NDEF-detection algorithm timed out. 255 ** 256 *******************************************************************************/ 257 bool isNdefDetectionTimedOut(); 258 259 /******************************************************************************* 260 ** 261 ** Function setActive 262 ** 263 ** Description Sets the active state for the object 264 ** 265 ** Returns None. 266 ** 267 *******************************************************************************/ 268 void setActive(bool active); 269 270 /******************************************************************************* 271 ** 272 ** Function: isDynamicTagId 273 ** 274 ** Description: Whether a tag has a dynamic tag ID. 275 ** 276 ** Returns: True if ID is dynamic. 277 ** 278 *******************************************************************************/ 279 bool isDynamicTagId(); 280 281 /******************************************************************************* 282 ** 283 ** Function: resetAllTransceiveTimeouts 284 ** 285 ** Description: Reset all timeouts for all technologies to default values. 286 ** 287 ** Returns: none 288 ** 289 *******************************************************************************/ 290 void resetAllTransceiveTimeouts(); 291 292 /******************************************************************************* 293 ** 294 ** Function: isDefaultTransceiveTimeout 295 ** 296 ** Description: Is the timeout value for a technology the default value? 297 ** techId: one of the values in TARGET_TYPE_* defined in 298 *NfcJniUtil.h. 299 ** timeout: Check this value against the default value. 300 ** 301 ** Returns: True if timeout is equal to the default value. 302 ** 303 *******************************************************************************/ 304 bool isDefaultTransceiveTimeout(int techId, int timeout); 305 306 /******************************************************************************* 307 ** 308 ** Function: getTransceiveTimeout 309 ** 310 ** Description: Get the timeout value for one technology. 311 ** techId: one of the values in TARGET_TYPE_* defined in 312 *NfcJniUtil.h 313 ** 314 ** Returns: Timeout value in millisecond. 315 ** 316 *******************************************************************************/ 317 int getTransceiveTimeout(int techId); 318 319 /******************************************************************************* 320 ** 321 ** Function: setTransceiveTimeout 322 ** 323 ** Description: Set the timeout value for one technology. 324 ** techId: one of the values in TARGET_TYPE_* defined in 325 *NfcJniUtil.h 326 ** timeout: timeout value in millisecond. 327 ** 328 ** Returns: Timeout value. 329 ** 330 *******************************************************************************/ 331 void setTransceiveTimeout(int techId, int timeout); 332 333 /******************************************************************************* 334 ** 335 ** Function: getPresenceCheckAlgorithm 336 ** 337 ** Description: Get presence-check algorithm from .conf file. 338 ** 339 ** Returns: Presence-check algorithm. 340 ** 341 *******************************************************************************/ 342 tNFA_RW_PRES_CHK_OPTION getPresenceCheckAlgorithm(); 343 344 /******************************************************************************* 345 ** 346 ** Function: isInfineonMyDMove 347 ** 348 ** Description: Whether the currently activated tag is Infineon My-D Move. 349 ** 350 ** Returns: True if tag is Infineon My-D Move. 351 ** 352 *******************************************************************************/ 353 bool isInfineonMyDMove(); 354 355 /******************************************************************************* 356 ** 357 ** Function: isKovioType2Tag 358 ** 359 ** Description: Whether the currently activated tag is Kovio 2Kb RFID tag. 360 ** It is a NFC Forum type-2 tag. 361 ** 362 ** Returns: True if tag is Kovio 2Kb RFID tag. 363 ** 364 *******************************************************************************/ 365 bool isKovioType2Tag(); 366 367 private: 368 std::vector<int> mTechnologyTimeoutsTable; 369 std::vector<int> mTechnologyDefaultTimeoutsTable; 370 nfc_jni_native_data* mNativeData; 371 bool mIsActivated; 372 ActivationState mActivationState; 373 tNFC_PROTOCOL mProtocol; 374 int mtT1tMaxMessageSize; // T1T max NDEF message size 375 tNFA_STATUS mReadCompletedStatus; 376 int mLastKovioUidLen; // len of uid of last Kovio tag activated 377 bool mNdefDetectionTimedOut; // whether NDEF detection algorithm timed out 378 tNFC_RF_TECH_PARAMS 379 mTechParams[MAX_NUM_TECHNOLOGY]; // array of technology parameters 380 SyncEvent mReadCompleteEvent; 381 struct timespec mLastKovioTime; // time of last Kovio tag activation 382 uint8_t mLastKovioUid[NFC_KOVIO_MAX_LEN]; // uid of last Kovio tag activated 383 bool mIsDynamicTagId; // whether the tag has dynamic tag ID 384 tNFA_RW_PRES_CHK_OPTION mPresenceCheckAlgorithm; 385 bool mIsFelicaLite; 386 387 /******************************************************************************* 388 ** 389 ** Function: IsSameKovio 390 ** 391 ** Description: Checks if tag activate is the same (UID) Kovio tag 392 *previously 393 ** activated. This is needed due to a problem with some 394 *Kovio 395 ** tags re-activating multiple times. 396 ** activationData: data from activation. 397 ** 398 ** Returns: true if the activation is from the same tag previously 399 ** activated, false otherwise 400 ** 401 *******************************************************************************/ 402 bool IsSameKovio(tNFA_ACTIVATED& activationData); 403 404 /******************************************************************************* 405 ** 406 ** Function: discoverTechnologies 407 ** 408 ** Description: Discover the technologies that NFC service needs by 409 *interpreting 410 ** the data strucutures from the stack. 411 ** activationData: data from activation. 412 ** 413 ** Returns: None 414 ** 415 *******************************************************************************/ 416 void discoverTechnologies(tNFA_ACTIVATED& activationData); 417 418 /******************************************************************************* 419 ** 420 ** Function: discoverTechnologies 421 ** 422 ** Description: Discover the technologies that NFC service needs by 423 *interpreting 424 ** the data strucutures from the stack. 425 ** discoveryData: data from discovery events(s). 426 ** 427 ** Returns: None 428 ** 429 *******************************************************************************/ 430 void discoverTechnologies(tNFA_DISC_RESULT& discoveryData); 431 432 /******************************************************************************* 433 ** 434 ** Function: createNativeNfcTag 435 ** 436 ** Description: Create a brand new Java NativeNfcTag object; 437 ** fill the objects's member variables with data; 438 ** notify NFC service; 439 ** activationData: data from activation. 440 ** 441 ** Returns: None 442 ** 443 *******************************************************************************/ 444 void createNativeNfcTag(tNFA_ACTIVATED& activationData); 445 446 /******************************************************************************* 447 ** 448 ** Function: fillNativeNfcTagMembers1 449 ** 450 ** Description: Fill NativeNfcTag's members: mProtocols, mTechList, 451 *mTechHandles, mTechLibNfcTypes. 452 ** e: JVM environment. 453 ** tag_cls: Java NativeNfcTag class. 454 ** tag: Java NativeNfcTag object. 455 ** 456 ** Returns: None 457 ** 458 *******************************************************************************/ 459 void fillNativeNfcTagMembers1(JNIEnv* e, jclass tag_cls, jobject tag); 460 461 /******************************************************************************* 462 ** 463 ** Function: fillNativeNfcTagMembers2 464 ** 465 ** Description: Fill NativeNfcTag's members: mConnectedTechIndex or 466 *mConnectedTechnology. 467 ** The original Google's implementation is in 468 *set_target_pollBytes( 469 ** in com_android_nfc_NativeNfcTag.cpp; 470 ** e: JVM environment. 471 ** tag_cls: Java NativeNfcTag class. 472 ** tag: Java NativeNfcTag object. 473 ** activationData: data from activation. 474 ** 475 ** Returns: None 476 ** 477 *******************************************************************************/ 478 void fillNativeNfcTagMembers2(JNIEnv* e, jclass tag_cls, jobject tag, 479 tNFA_ACTIVATED& activationData); 480 481 /******************************************************************************* 482 ** 483 ** Function: fillNativeNfcTagMembers3 484 ** 485 ** Description: Fill NativeNfcTag's members: mTechPollBytes. 486 ** The original Google's implementation is in 487 *set_target_pollBytes( 488 ** in com_android_nfc_NativeNfcTag.cpp; 489 ** e: JVM environment. 490 ** tag_cls: Java NativeNfcTag class. 491 ** tag: Java NativeNfcTag object. 492 ** activationData: data from activation. 493 ** 494 ** Returns: None 495 ** 496 *******************************************************************************/ 497 void fillNativeNfcTagMembers3(JNIEnv* e, jclass tag_cls, jobject tag, 498 tNFA_ACTIVATED& activationData); 499 500 /******************************************************************************* 501 ** 502 ** Function: fillNativeNfcTagMembers4 503 ** 504 ** Description: Fill NativeNfcTag's members: mTechActBytes. 505 ** The original Google's implementation is in 506 *set_target_activationBytes() 507 ** in com_android_nfc_NativeNfcTag.cpp; 508 ** e: JVM environment. 509 ** tag_cls: Java NativeNfcTag class. 510 ** tag: Java NativeNfcTag object. 511 ** activationData: data from activation. 512 ** 513 ** Returns: None 514 ** 515 *******************************************************************************/ 516 void fillNativeNfcTagMembers4(JNIEnv* e, jclass tag_cls, jobject tag, 517 tNFA_ACTIVATED& activationData); 518 519 /******************************************************************************* 520 ** 521 ** Function: fillNativeNfcTagMembers5 522 ** 523 ** Description: Fill NativeNfcTag's members: mUid. 524 ** The original Google's implementation is in 525 *nfc_jni_Discovery_notification_callback() 526 ** in com_android_nfc_NativeNfcManager.cpp; 527 ** e: JVM environment. 528 ** tag_cls: Java NativeNfcTag class. 529 ** tag: Java NativeNfcTag object. 530 ** activationData: data from activation. 531 ** 532 ** Returns: None 533 ** 534 *******************************************************************************/ 535 void fillNativeNfcTagMembers5(JNIEnv* e, jclass tag_cls, jobject tag, 536 tNFA_ACTIVATED& activationData); 537 538 /******************************************************************************* 539 ** 540 ** Function: resetTechnologies 541 ** 542 ** Description: Clear all data related to the technology, protocol of the 543 *tag. 544 ** 545 ** Returns: None 546 ** 547 *******************************************************************************/ 548 void resetTechnologies(); 549 550 /******************************************************************************* 551 ** 552 ** Function: calculateT1tMaxMessageSize 553 ** 554 ** Description: Calculate type-1 tag's max message size based on header 555 *ROM bytes. 556 ** activate: reference to activation data. 557 ** 558 ** Returns: None 559 ** 560 *******************************************************************************/ 561 void calculateT1tMaxMessageSize(tNFA_ACTIVATED& activate); 562 }; 563