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