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 int mNumRfDiscId; 50 51 /******************************************************************************* 52 ** 53 ** Function: NfcTag 54 ** 55 ** Description: Initialize member variables. 56 ** 57 ** Returns: None 58 ** 59 *******************************************************************************/ 60 NfcTag(); 61 62 /******************************************************************************* 63 ** 64 ** Function: getInstance 65 ** 66 ** Description: Get a reference to the singleton NfcTag object. 67 ** 68 ** Returns: Reference to NfcTag object. 69 ** 70 *******************************************************************************/ 71 static NfcTag& getInstance(); 72 73 /******************************************************************************* 74 ** 75 ** Function: initialize 76 ** 77 ** Description: Reset member variables. 78 ** native: Native data. 79 ** Returns: None 80 ** 81 *******************************************************************************/ 82 void initialize(nfc_jni_native_data* native); 83 84 /******************************************************************************* 85 ** 86 ** Function: abort 87 ** 88 ** Description: Unblock all operations. 89 ** 90 ** Returns: None 91 ** 92 *******************************************************************************/ 93 void abort(); 94 95 /******************************************************************************* 96 ** 97 ** Function: connectionEventHandler 98 ** 99 ** Description: Handle connection-related events. 100 ** event: event code. 101 ** data: pointer to event data. 102 ** 103 ** Returns: None 104 ** 105 *******************************************************************************/ 106 void connectionEventHandler(uint8_t event, tNFA_CONN_EVT_DATA* data); 107 108 /******************************************************************************* 109 ** 110 ** Function: isActivated 111 ** 112 ** Description: Is tag activated? 113 ** 114 ** Returns: True if tag is activated. 115 ** 116 *******************************************************************************/ 117 bool isActivated(); 118 119 /******************************************************************************* 120 ** 121 ** Function: getActivationState 122 ** 123 ** Description: What is the current state: Idle, Sleep, or Activated. 124 ** 125 ** Returns: Idle, Sleep, or Activated. 126 ** 127 *******************************************************************************/ 128 ActivationState getActivationState(); 129 130 /******************************************************************************* 131 ** 132 ** Function: setDeactivationState 133 ** 134 ** Description: Set the current state: Idle or Sleep. 135 ** deactivated: state of deactivation. 136 ** 137 ** Returns: None. 138 ** 139 *******************************************************************************/ 140 void setDeactivationState(tNFA_DEACTIVATED& deactivated); 141 142 /******************************************************************************* 143 ** 144 ** Function: setActivationState 145 ** 146 ** Description: Set the current state to Active. 147 ** 148 ** Returns: None. 149 ** 150 *******************************************************************************/ 151 void setActivationState(); 152 153 /******************************************************************************* 154 ** 155 ** Function: getProtocol 156 ** 157 ** Description: Get the protocol of the current tag. 158 ** 159 ** Returns: Protocol number. 160 ** 161 *******************************************************************************/ 162 tNFC_PROTOCOL getProtocol(); 163 164 /******************************************************************************* 165 ** 166 ** Function: selectFirstTag 167 ** 168 ** Description: When multiple tags are discovered, just select the first 169 *one to activate. 170 ** 171 ** Returns: None 172 ** 173 *******************************************************************************/ 174 void selectFirstTag(); 175 176 /******************************************************************************* 177 ** 178 ** Function: selectNextTagIfExists 179 ** 180 ** Description: When multiple tags are discovered, selects the Next one to 181 ** activate. 182 ** 183 ** Returns: None 184 ** 185 *******************************************************************************/ 186 void selectNextTagIfExists(); 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 /******************************************************************************* 368 ** 369 ** Function: setMultiProtocolTagSupport 370 ** 371 ** Description: Update mIsMultiProtocolTag 372 ** 373 ** Returns: None 374 ** 375 *******************************************************************************/ 376 void setMultiProtocolTagSupport(bool isMultiProtocolSupported); 377 378 /******************************************************************************* 379 ** 380 ** Function: setNumDiscNtf 381 ** 382 ** Description: Update mNumDiscNtf 383 ** 384 ** Returns: None 385 ** 386 *******************************************************************************/ 387 void setNumDiscNtf(int numDiscNtfValue); 388 389 /******************************************************************************* 390 ** 391 ** Function: getNumDiscNtf 392 ** 393 ** Description: number of discovery notifications received from NFCC after 394 ** last RF DISCOVERY state 395 ** 396 ** Returns: number of discovery notifications received from NFCC 397 ** 398 *******************************************************************************/ 399 int getNumDiscNtf(); 400 401 private: 402 std::vector<int> mTechnologyTimeoutsTable; 403 std::vector<int> mTechnologyDefaultTimeoutsTable; 404 nfc_jni_native_data* mNativeData; 405 bool mIsActivated; 406 ActivationState mActivationState; 407 tNFC_PROTOCOL mProtocol; 408 int mtT1tMaxMessageSize; // T1T max NDEF message size 409 tNFA_STATUS mReadCompletedStatus; 410 int mLastKovioUidLen; // len of uid of last Kovio tag activated 411 bool mNdefDetectionTimedOut; // whether NDEF detection algorithm timed out 412 tNFC_RF_TECH_PARAMS 413 mTechParams[MAX_NUM_TECHNOLOGY]; // array of technology parameters 414 SyncEvent mReadCompleteEvent; 415 struct timespec mLastKovioTime; // time of last Kovio tag activation 416 uint8_t mLastKovioUid[NFC_KOVIO_MAX_LEN]; // uid of last Kovio tag activated 417 bool mIsDynamicTagId; // whether the tag has dynamic tag ID 418 tNFA_RW_PRES_CHK_OPTION mPresenceCheckAlgorithm; 419 bool mIsFelicaLite; 420 int mTechHandlesDiscData[MAX_NUM_TECHNOLOGY]; // array of tag handles (RF 421 // DISC ID) received from 422 // RF_DISC_NTF 423 int mTechLibNfcTypesDiscData[MAX_NUM_TECHNOLOGY]; // array of detailed tag 424 // types ( RF Protocol) 425 // received from 426 // RF_DISC_NTF 427 int mNumDiscNtf; 428 int mNumDiscTechList; 429 int mTechListTail; // Index of Last added entry in mTechList 430 bool mIsMultiProtocolTag; 431 NfcStatsUtil* mNfcStatsUtil; 432 433 /******************************************************************************* 434 ** 435 ** Function: IsSameKovio 436 ** 437 ** Description: Checks if tag activate is the same (UID) Kovio tag 438 *previously 439 ** activated. This is needed due to a problem with some 440 *Kovio 441 ** tags re-activating multiple times. 442 ** activationData: data from activation. 443 ** 444 ** Returns: true if the activation is from the same tag previously 445 ** activated, false otherwise 446 ** 447 *******************************************************************************/ 448 bool IsSameKovio(tNFA_ACTIVATED& activationData); 449 450 /******************************************************************************* 451 ** 452 ** Function: discoverTechnologies 453 ** 454 ** Description: Discover the technologies that NFC service needs by 455 *interpreting 456 ** the data strucutures from the stack. 457 ** activationData: data from activation. 458 ** 459 ** Returns: None 460 ** 461 *******************************************************************************/ 462 void discoverTechnologies(tNFA_ACTIVATED& activationData); 463 464 /******************************************************************************* 465 ** 466 ** Function: discoverTechnologies 467 ** 468 ** Description: Discover the technologies that NFC service needs by 469 *interpreting 470 ** the data strucutures from the stack. 471 ** discoveryData: data from discovery events(s). 472 ** 473 ** Returns: None 474 ** 475 *******************************************************************************/ 476 void discoverTechnologies(tNFA_DISC_RESULT& discoveryData); 477 478 /******************************************************************************* 479 ** 480 ** Function: createNativeNfcTag 481 ** 482 ** Description: Create a brand new Java NativeNfcTag object; 483 ** fill the objects's member variables with data; 484 ** notify NFC service; 485 ** activationData: data from activation. 486 ** 487 ** Returns: None 488 ** 489 *******************************************************************************/ 490 void createNativeNfcTag(tNFA_ACTIVATED& activationData); 491 492 /******************************************************************************* 493 ** 494 ** Function: fillNativeNfcTagMembers1 495 ** 496 ** Description: Fill NativeNfcTag's members: mProtocols, mTechList, 497 *mTechHandles, mTechLibNfcTypes. 498 ** e: JVM environment. 499 ** tag_cls: Java NativeNfcTag class. 500 ** tag: Java NativeNfcTag object. 501 ** 502 ** Returns: None 503 ** 504 *******************************************************************************/ 505 void fillNativeNfcTagMembers1(JNIEnv* e, jclass tag_cls, jobject tag); 506 507 /******************************************************************************* 508 ** 509 ** Function: fillNativeNfcTagMembers2 510 ** 511 ** Description: Fill NativeNfcTag's members: mConnectedTechIndex or 512 *mConnectedTechnology. 513 ** The original Google's implementation is in 514 *set_target_pollBytes( 515 ** in com_android_nfc_NativeNfcTag.cpp; 516 ** e: JVM environment. 517 ** tag_cls: Java NativeNfcTag class. 518 ** tag: Java NativeNfcTag object. 519 ** activationData: data from activation. 520 ** 521 ** Returns: None 522 ** 523 *******************************************************************************/ 524 void fillNativeNfcTagMembers2(JNIEnv* e, jclass tag_cls, jobject tag, 525 tNFA_ACTIVATED& activationData); 526 527 /******************************************************************************* 528 ** 529 ** Function: fillNativeNfcTagMembers3 530 ** 531 ** Description: Fill NativeNfcTag's members: mTechPollBytes. 532 ** The original Google's implementation is in 533 *set_target_pollBytes( 534 ** in com_android_nfc_NativeNfcTag.cpp; 535 ** e: JVM environment. 536 ** tag_cls: Java NativeNfcTag class. 537 ** tag: Java NativeNfcTag object. 538 ** activationData: data from activation. 539 ** 540 ** Returns: None 541 ** 542 *******************************************************************************/ 543 void fillNativeNfcTagMembers3(JNIEnv* e, jclass tag_cls, jobject tag, 544 tNFA_ACTIVATED& activationData); 545 546 /******************************************************************************* 547 ** 548 ** Function: fillNativeNfcTagMembers4 549 ** 550 ** Description: Fill NativeNfcTag's members: mTechActBytes. 551 ** The original Google's implementation is in 552 *set_target_activationBytes() 553 ** in com_android_nfc_NativeNfcTag.cpp; 554 ** e: JVM environment. 555 ** tag_cls: Java NativeNfcTag class. 556 ** tag: Java NativeNfcTag object. 557 ** activationData: data from activation. 558 ** 559 ** Returns: None 560 ** 561 *******************************************************************************/ 562 void fillNativeNfcTagMembers4(JNIEnv* e, jclass tag_cls, jobject tag, 563 tNFA_ACTIVATED& activationData); 564 565 /******************************************************************************* 566 ** 567 ** Function: fillNativeNfcTagMembers5 568 ** 569 ** Description: Fill NativeNfcTag's members: mUid. 570 ** The original Google's implementation is in 571 *nfc_jni_Discovery_notification_callback() 572 ** in com_android_nfc_NativeNfcManager.cpp; 573 ** e: JVM environment. 574 ** tag_cls: Java NativeNfcTag class. 575 ** tag: Java NativeNfcTag object. 576 ** activationData: data from activation. 577 ** 578 ** Returns: None 579 ** 580 *******************************************************************************/ 581 void fillNativeNfcTagMembers5(JNIEnv* e, jclass tag_cls, jobject tag, 582 tNFA_ACTIVATED& activationData); 583 584 /******************************************************************************* 585 ** 586 ** Function: resetTechnologies 587 ** 588 ** Description: Clear all data related to the technology, protocol of the 589 *tag. 590 ** 591 ** Returns: None 592 ** 593 *******************************************************************************/ 594 void resetTechnologies(); 595 596 /******************************************************************************* 597 ** 598 ** Function: calculateT1tMaxMessageSize 599 ** 600 ** Description: Calculate type-1 tag's max message size based on header 601 *ROM bytes. 602 ** activate: reference to activation data. 603 ** 604 ** Returns: None 605 ** 606 *******************************************************************************/ 607 void calculateT1tMaxMessageSize(tNFA_ACTIVATED& activate); 608 }; 609