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