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