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 extern "C" 25 { 26 #include "nfa_rw_api.h" 27 } 28 29 30 class NfcTag 31 { 32 public: 33 enum ActivationState {Idle, Sleep, Active}; 34 static const int MAX_NUM_TECHNOLOGY = 10; //max number of technologies supported by one or more tags 35 int mTechList [MAX_NUM_TECHNOLOGY]; //array of NFC technologies according to NFC service 36 int mTechHandles [MAX_NUM_TECHNOLOGY]; //array of tag handles according to NFC service 37 int mTechLibNfcTypes [MAX_NUM_TECHNOLOGY]; //array of detailed tag types according to NFC service 38 int mNumTechList; //current number of NFC technologies in the list 39 40 /******************************************************************************* 41 ** 42 ** Function: NfcTag 43 ** 44 ** Description: Initialize member variables. 45 ** 46 ** Returns: None 47 ** 48 *******************************************************************************/ 49 NfcTag (); 50 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 ** 66 ** Function: initialize 67 ** 68 ** Description: Reset member variables. 69 ** native: Native data. 70 ** Returns: None 71 ** 72 *******************************************************************************/ 73 void initialize (nfc_jni_native_data* native); 74 75 76 /******************************************************************************* 77 ** 78 ** Function: abort 79 ** 80 ** Description: Unblock all operations. 81 ** 82 ** Returns: None 83 ** 84 *******************************************************************************/ 85 void abort (); 86 87 88 /******************************************************************************* 89 ** 90 ** Function: connectionEventHandler 91 ** 92 ** Description: Handle connection-related events. 93 ** event: event code. 94 ** data: pointer to event data. 95 ** 96 ** Returns: None 97 ** 98 *******************************************************************************/ 99 void connectionEventHandler (UINT8 event, tNFA_CONN_EVT_DATA* data); 100 101 102 /******************************************************************************* 103 ** 104 ** Function: getActivationState 105 ** 106 ** Description: What is the current state: Idle, Sleep, or Activated. 107 ** 108 ** Returns: Idle, Sleep, or Activated. 109 ** 110 *******************************************************************************/ 111 ActivationState getActivationState (); 112 113 114 /******************************************************************************* 115 ** 116 ** Function: setDeactivationState 117 ** 118 ** Description: Set the current state: Idle or Sleep. 119 ** deactivated: state of deactivation. 120 ** 121 ** Returns: None. 122 ** 123 *******************************************************************************/ 124 void setDeactivationState (tNFA_DEACTIVATED& deactivated); 125 126 127 /******************************************************************************* 128 ** 129 ** Function: setActivationState 130 ** 131 ** Description: Set the current state to Active. 132 ** 133 ** Returns: None. 134 ** 135 *******************************************************************************/ 136 void setActivationState (); 137 138 /******************************************************************************* 139 ** 140 ** Function: getProtocol 141 ** 142 ** Description: Get the protocol of the current tag. 143 ** 144 ** Returns: Protocol number. 145 ** 146 *******************************************************************************/ 147 tNFC_PROTOCOL getProtocol (); 148 149 150 /******************************************************************************* 151 ** 152 ** Function: isP2pDiscovered 153 ** 154 ** Description: Does the peer support P2P? 155 ** 156 ** Returns: True if the peer supports P2P. 157 ** 158 *******************************************************************************/ 159 bool isP2pDiscovered (); 160 161 162 /******************************************************************************* 163 ** 164 ** Function: selectP2p 165 ** 166 ** Description: Select the preferred P2P technology if there is a choice. 167 ** 168 ** Returns: None 169 ** 170 *******************************************************************************/ 171 void selectP2p (); 172 173 174 /******************************************************************************* 175 ** 176 ** Function: selectFirstTag 177 ** 178 ** Description: When multiple tags are discovered, just select the first one to activate. 179 ** 180 ** Returns: None 181 ** 182 *******************************************************************************/ 183 void selectFirstTag (); 184 185 186 /******************************************************************************* 187 ** 188 ** Function: getT1tMaxMessageSize 189 ** 190 ** Description: Get the maximum size (octet) that a T1T can store. 191 ** 192 ** Returns: Maximum size in octets. 193 ** 194 *******************************************************************************/ 195 int getT1tMaxMessageSize (); 196 197 198 /******************************************************************************* 199 ** 200 ** Function: isMifareUltralight 201 ** 202 ** Description: Whether the currently activated tag is Mifare Ultralight. 203 ** 204 ** Returns: True if tag is Mifare Ultralight. 205 ** 206 *******************************************************************************/ 207 bool isMifareUltralight (); 208 209 210 /******************************************************************************* 211 ** 212 ** Function: isT2tNackResponse 213 ** 214 ** Description: Whether the response is a T2T NACK response. 215 ** See NFC Digital Protocol Technical Specification (2010-11-17). 216 ** Chapter 9 (Type 2 Tag Platform), section 9.6 (READ). 217 ** response: buffer contains T2T response. 218 ** responseLen: length of the response. 219 ** 220 ** Returns: True if the response is NACK 221 ** 222 *******************************************************************************/ 223 bool isT2tNackResponse (const UINT8* response, UINT32 responseLen); 224 225 /******************************************************************************* 226 ** 227 ** Function: isNdefDetectionTimedOut 228 ** 229 ** Description: Whether NDEF-detection algorithm has timed out. 230 ** 231 ** Returns: True if NDEF-detection algorithm timed out. 232 ** 233 *******************************************************************************/ 234 bool isNdefDetectionTimedOut (); 235 236 private: 237 nfc_jni_native_data* mNativeData; 238 ActivationState mActivationState; 239 tNFC_PROTOCOL mProtocol; 240 int mtT1tMaxMessageSize; //T1T max NDEF message size 241 tNFA_STATUS mReadCompletedStatus; 242 int mLastKovioUidLen; // len of uid of last Kovio tag activated 243 bool mNdefDetectionTimedOut; // whether NDEF detection algorithm timed out 244 tNFC_RF_TECH_PARAMS mTechParams [MAX_NUM_TECHNOLOGY]; //array of technology parameters 245 SyncEvent mReadCompleteEvent; 246 struct timespec mLastKovioTime; // time of last Kovio tag activation 247 UINT8 mLastKovioUid[NFC_KOVIO_MAX_LEN]; // uid of last Kovio tag activated 248 249 250 /******************************************************************************* 251 ** 252 ** Function: IsSameKovio 253 ** 254 ** Description: Checks if tag activate is the same (UID) Kovio tag previously 255 ** activated. This is needed due to a problem with some Kovio 256 ** tags re-activating multiple times. 257 ** activationData: data from activation. 258 ** 259 ** Returns: true if the activation is from the same tag previously 260 ** activated, false otherwise 261 ** 262 *******************************************************************************/ 263 bool IsSameKovio(tNFA_ACTIVATED& activationData); 264 265 /******************************************************************************* 266 ** 267 ** Function: discoverTechnologies 268 ** 269 ** Description: Discover the technologies that NFC service needs by interpreting 270 ** the data strucutures from the stack. 271 ** activationData: data from activation. 272 ** 273 ** Returns: None 274 ** 275 *******************************************************************************/ 276 void discoverTechnologies (tNFA_ACTIVATED& activationData); 277 278 279 /******************************************************************************* 280 ** 281 ** Function: discoverTechnologies 282 ** 283 ** Description: Discover the technologies that NFC service needs by interpreting 284 ** the data strucutures from the stack. 285 ** discoveryData: data from discovery events(s). 286 ** 287 ** Returns: None 288 ** 289 *******************************************************************************/ 290 void discoverTechnologies (tNFA_DISC_RESULT& discoveryData); 291 292 293 /******************************************************************************* 294 ** 295 ** Function: createNativeNfcTag 296 ** 297 ** Description: Create a brand new Java NativeNfcTag object; 298 ** fill the objects's member variables with data; 299 ** notify NFC service; 300 ** activationData: data from activation. 301 ** 302 ** Returns: None 303 ** 304 *******************************************************************************/ 305 void createNativeNfcTag (tNFA_ACTIVATED& activationData); 306 307 308 /******************************************************************************* 309 ** 310 ** Function: fillNativeNfcTagMembers1 311 ** 312 ** Description: Fill NativeNfcTag's members: mProtocols, mTechList, mTechHandles, mTechLibNfcTypes. 313 ** e: JVM environment. 314 ** tag_cls: Java NativeNfcTag class. 315 ** tag: Java NativeNfcTag object. 316 ** 317 ** Returns: None 318 ** 319 *******************************************************************************/ 320 void fillNativeNfcTagMembers1 (JNIEnv* e, jclass tag_cls, jobject tag); 321 322 323 /******************************************************************************* 324 ** 325 ** Function: fillNativeNfcTagMembers2 326 ** 327 ** Description: Fill NativeNfcTag's members: mConnectedTechIndex or mConnectedTechnology. 328 ** The original Google's implementation is in set_target_pollBytes( 329 ** in com_android_nfc_NativeNfcTag.cpp; 330 ** e: JVM environment. 331 ** tag_cls: Java NativeNfcTag class. 332 ** tag: Java NativeNfcTag object. 333 ** activationData: data from activation. 334 ** 335 ** Returns: None 336 ** 337 *******************************************************************************/ 338 void fillNativeNfcTagMembers2 (JNIEnv* e, jclass tag_cls, jobject tag, tNFA_ACTIVATED& activationData); 339 340 341 /******************************************************************************* 342 ** 343 ** Function: fillNativeNfcTagMembers3 344 ** 345 ** Description: Fill NativeNfcTag's members: mTechPollBytes. 346 ** The original Google's implementation is in set_target_pollBytes( 347 ** in com_android_nfc_NativeNfcTag.cpp; 348 ** e: JVM environment. 349 ** tag_cls: Java NativeNfcTag class. 350 ** tag: Java NativeNfcTag object. 351 ** activationData: data from activation. 352 ** 353 ** Returns: None 354 ** 355 *******************************************************************************/ 356 void fillNativeNfcTagMembers3 (JNIEnv* e, jclass tag_cls, jobject tag, tNFA_ACTIVATED& activationData); 357 358 359 /******************************************************************************* 360 ** 361 ** Function: fillNativeNfcTagMembers4 362 ** 363 ** Description: Fill NativeNfcTag's members: mTechActBytes. 364 ** The original Google's implementation is in set_target_activationBytes() 365 ** in com_android_nfc_NativeNfcTag.cpp; 366 ** e: JVM environment. 367 ** tag_cls: Java NativeNfcTag class. 368 ** tag: Java NativeNfcTag object. 369 ** activationData: data from activation. 370 ** 371 ** Returns: None 372 ** 373 *******************************************************************************/ 374 void fillNativeNfcTagMembers4 (JNIEnv* e, jclass tag_cls, jobject tag, tNFA_ACTIVATED& activationData); 375 376 377 /******************************************************************************* 378 ** 379 ** Function: fillNativeNfcTagMembers5 380 ** 381 ** Description: Fill NativeNfcTag's members: mUid. 382 ** The original Google's implementation is in nfc_jni_Discovery_notification_callback() 383 ** in com_android_nfc_NativeNfcManager.cpp; 384 ** e: JVM environment. 385 ** tag_cls: Java NativeNfcTag class. 386 ** tag: Java NativeNfcTag object. 387 ** activationData: data from activation. 388 ** 389 ** Returns: None 390 ** 391 *******************************************************************************/ 392 void fillNativeNfcTagMembers5 (JNIEnv* e, jclass tag_cls, jobject tag, tNFA_ACTIVATED& activationData); 393 394 395 /******************************************************************************* 396 ** 397 ** Function: resetTechnologies 398 ** 399 ** Description: Clear all data related to the technology, protocol of the tag. 400 ** 401 ** Returns: None 402 ** 403 *******************************************************************************/ 404 void resetTechnologies (); 405 406 407 /******************************************************************************* 408 ** 409 ** Function: calculateT1tMaxMessageSize 410 ** 411 ** Description: Calculate type-1 tag's max message size based on header ROM bytes. 412 ** activate: reference to activation data. 413 ** 414 ** Returns: None 415 ** 416 *******************************************************************************/ 417 void calculateT1tMaxMessageSize (tNFA_ACTIVATED& activate); 418 }; 419 420