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 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: isT2tNackResponse 226 ** 227 ** Description: Whether the response is a T2T NACK response. 228 ** See NFC Digital Protocol Technical Specification (2010-11-17). 229 ** Chapter 9 (Type 2 Tag Platform), section 9.6 (READ). 230 ** response: buffer contains T2T response. 231 ** responseLen: length of the response. 232 ** 233 ** Returns: True if the response is NACK 234 ** 235 *******************************************************************************/ 236 bool isT2tNackResponse (const UINT8* response, UINT32 responseLen); 237 238 /******************************************************************************* 239 ** 240 ** Function: isNdefDetectionTimedOut 241 ** 242 ** Description: Whether NDEF-detection algorithm has timed out. 243 ** 244 ** Returns: True if NDEF-detection algorithm timed out. 245 ** 246 *******************************************************************************/ 247 bool isNdefDetectionTimedOut (); 248 249 250 /******************************************************************************* 251 ** 252 ** Function setActive 253 ** 254 ** Description Sets the active state for the object 255 ** 256 ** Returns None. 257 ** 258 *******************************************************************************/ 259 void setActive(bool active); 260 261 262 /******************************************************************************* 263 ** 264 ** Function: resetAllTransceiveTimeouts 265 ** 266 ** Description: Reset all timeouts for all technologies to default values. 267 ** 268 ** Returns: none 269 ** 270 *******************************************************************************/ 271 void resetAllTransceiveTimeouts (); 272 273 274 /******************************************************************************* 275 ** 276 ** Function: getTransceiveTimeout 277 ** 278 ** Description: Get the timeout value for one technology. 279 ** techId: one of the values in TARGET_TYPE_* defined in NfcJniUtil.h 280 ** 281 ** Returns: Timeout value in millisecond. 282 ** 283 *******************************************************************************/ 284 int getTransceiveTimeout (int techId); 285 286 287 /******************************************************************************* 288 ** 289 ** Function: setTransceiveTimeout 290 ** 291 ** Description: Set the timeout value for one technology. 292 ** techId: one of the values in TARGET_TYPE_* defined in NfcJniUtil.h 293 ** timeout: timeout value in millisecond. 294 ** 295 ** Returns: Timeout value. 296 ** 297 *******************************************************************************/ 298 void setTransceiveTimeout (int techId, int timeout); 299 300 301 private: 302 std::vector<int> mTechnologyTimeoutsTable; 303 nfc_jni_native_data* mNativeData; 304 bool mIsActivated; 305 ActivationState mActivationState; 306 tNFC_PROTOCOL mProtocol; 307 int mtT1tMaxMessageSize; //T1T max NDEF message size 308 tNFA_STATUS mReadCompletedStatus; 309 int mLastKovioUidLen; // len of uid of last Kovio tag activated 310 bool mNdefDetectionTimedOut; // whether NDEF detection algorithm timed out 311 tNFC_RF_TECH_PARAMS mTechParams [MAX_NUM_TECHNOLOGY]; //array of technology parameters 312 SyncEvent mReadCompleteEvent; 313 struct timespec mLastKovioTime; // time of last Kovio tag activation 314 UINT8 mLastKovioUid[NFC_KOVIO_MAX_LEN]; // uid of last Kovio tag activated 315 316 317 /******************************************************************************* 318 ** 319 ** Function: IsSameKovio 320 ** 321 ** Description: Checks if tag activate is the same (UID) Kovio tag previously 322 ** activated. This is needed due to a problem with some Kovio 323 ** tags re-activating multiple times. 324 ** activationData: data from activation. 325 ** 326 ** Returns: true if the activation is from the same tag previously 327 ** activated, false otherwise 328 ** 329 *******************************************************************************/ 330 bool IsSameKovio(tNFA_ACTIVATED& activationData); 331 332 /******************************************************************************* 333 ** 334 ** Function: discoverTechnologies 335 ** 336 ** Description: Discover the technologies that NFC service needs by interpreting 337 ** the data strucutures from the stack. 338 ** activationData: data from activation. 339 ** 340 ** Returns: None 341 ** 342 *******************************************************************************/ 343 void discoverTechnologies (tNFA_ACTIVATED& activationData); 344 345 346 /******************************************************************************* 347 ** 348 ** Function: discoverTechnologies 349 ** 350 ** Description: Discover the technologies that NFC service needs by interpreting 351 ** the data strucutures from the stack. 352 ** discoveryData: data from discovery events(s). 353 ** 354 ** Returns: None 355 ** 356 *******************************************************************************/ 357 void discoverTechnologies (tNFA_DISC_RESULT& discoveryData); 358 359 360 /******************************************************************************* 361 ** 362 ** Function: createNativeNfcTag 363 ** 364 ** Description: Create a brand new Java NativeNfcTag object; 365 ** fill the objects's member variables with data; 366 ** notify NFC service; 367 ** activationData: data from activation. 368 ** 369 ** Returns: None 370 ** 371 *******************************************************************************/ 372 void createNativeNfcTag (tNFA_ACTIVATED& activationData); 373 374 375 /******************************************************************************* 376 ** 377 ** Function: fillNativeNfcTagMembers1 378 ** 379 ** Description: Fill NativeNfcTag's members: mProtocols, mTechList, mTechHandles, mTechLibNfcTypes. 380 ** e: JVM environment. 381 ** tag_cls: Java NativeNfcTag class. 382 ** tag: Java NativeNfcTag object. 383 ** 384 ** Returns: None 385 ** 386 *******************************************************************************/ 387 void fillNativeNfcTagMembers1 (JNIEnv* e, jclass tag_cls, jobject tag); 388 389 390 /******************************************************************************* 391 ** 392 ** Function: fillNativeNfcTagMembers2 393 ** 394 ** Description: Fill NativeNfcTag's members: mConnectedTechIndex or mConnectedTechnology. 395 ** The original Google's implementation is in set_target_pollBytes( 396 ** in com_android_nfc_NativeNfcTag.cpp; 397 ** e: JVM environment. 398 ** tag_cls: Java NativeNfcTag class. 399 ** tag: Java NativeNfcTag object. 400 ** activationData: data from activation. 401 ** 402 ** Returns: None 403 ** 404 *******************************************************************************/ 405 void fillNativeNfcTagMembers2 (JNIEnv* e, jclass tag_cls, jobject tag, tNFA_ACTIVATED& activationData); 406 407 408 /******************************************************************************* 409 ** 410 ** Function: fillNativeNfcTagMembers3 411 ** 412 ** Description: Fill NativeNfcTag's members: mTechPollBytes. 413 ** The original Google's implementation is in set_target_pollBytes( 414 ** in com_android_nfc_NativeNfcTag.cpp; 415 ** e: JVM environment. 416 ** tag_cls: Java NativeNfcTag class. 417 ** tag: Java NativeNfcTag object. 418 ** activationData: data from activation. 419 ** 420 ** Returns: None 421 ** 422 *******************************************************************************/ 423 void fillNativeNfcTagMembers3 (JNIEnv* e, jclass tag_cls, jobject tag, tNFA_ACTIVATED& activationData); 424 425 426 /******************************************************************************* 427 ** 428 ** Function: fillNativeNfcTagMembers4 429 ** 430 ** Description: Fill NativeNfcTag's members: mTechActBytes. 431 ** The original Google's implementation is in set_target_activationBytes() 432 ** in com_android_nfc_NativeNfcTag.cpp; 433 ** e: JVM environment. 434 ** tag_cls: Java NativeNfcTag class. 435 ** tag: Java NativeNfcTag object. 436 ** activationData: data from activation. 437 ** 438 ** Returns: None 439 ** 440 *******************************************************************************/ 441 void fillNativeNfcTagMembers4 (JNIEnv* e, jclass tag_cls, jobject tag, tNFA_ACTIVATED& activationData); 442 443 444 /******************************************************************************* 445 ** 446 ** Function: fillNativeNfcTagMembers5 447 ** 448 ** Description: Fill NativeNfcTag's members: mUid. 449 ** The original Google's implementation is in nfc_jni_Discovery_notification_callback() 450 ** in com_android_nfc_NativeNfcManager.cpp; 451 ** e: JVM environment. 452 ** tag_cls: Java NativeNfcTag class. 453 ** tag: Java NativeNfcTag object. 454 ** activationData: data from activation. 455 ** 456 ** Returns: None 457 ** 458 *******************************************************************************/ 459 void fillNativeNfcTagMembers5 (JNIEnv* e, jclass tag_cls, jobject tag, tNFA_ACTIVATED& activationData); 460 461 462 /******************************************************************************* 463 ** 464 ** Function: resetTechnologies 465 ** 466 ** Description: Clear all data related to the technology, protocol of the tag. 467 ** 468 ** Returns: None 469 ** 470 *******************************************************************************/ 471 void resetTechnologies (); 472 473 474 /******************************************************************************* 475 ** 476 ** Function: calculateT1tMaxMessageSize 477 ** 478 ** Description: Calculate type-1 tag's max message size based on header ROM bytes. 479 ** activate: reference to activation data. 480 ** 481 ** Returns: None 482 ** 483 *******************************************************************************/ 484 void calculateT1tMaxMessageSize (tNFA_ACTIVATED& activate); 485 }; 486 487