• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   bool mIsReselecting;
51 
52   /*******************************************************************************
53   **
54   ** Function:        NfcTag
55   **
56   ** Description:     Initialize member variables.
57   **
58   ** Returns:         None
59   **
60   *******************************************************************************/
61   NfcTag();
62 
63   /*******************************************************************************
64   **
65   ** Function:        getInstance
66   **
67   ** Description:     Get a reference to the singleton NfcTag object.
68   **
69   ** Returns:         Reference to NfcTag object.
70   **
71   *******************************************************************************/
72   static NfcTag& getInstance();
73 
74   /*******************************************************************************
75   **
76   ** Function:        initialize
77   **
78   ** Description:     Reset member variables.
79   **                  native: Native data.
80   ** Returns:         None
81   **
82   *******************************************************************************/
83   void initialize(nfc_jni_native_data* native);
84 
85   /*******************************************************************************
86   **
87   ** Function:        abort
88   **
89   ** Description:     Unblock all operations.
90   **
91   ** Returns:         None
92   **
93   *******************************************************************************/
94   void abort();
95 
96   /*******************************************************************************
97   **
98   ** Function:        connectionEventHandler
99   **
100   ** Description:     Handle connection-related events.
101   **                  event: event code.
102   **                  data: pointer to event data.
103   **
104   ** Returns:         None
105   **
106   *******************************************************************************/
107   void connectionEventHandler(uint8_t event, tNFA_CONN_EVT_DATA* data);
108 
109   /*******************************************************************************
110   **
111   ** Function:        notifyTagDiscovered
112   **
113   ** Description:     Notify NFC service about tag discovery.
114   **                  discovered: true if tag is discovered, false if tag is lost.
115   **
116   ** Returns:         None
117   **
118   *******************************************************************************/
119   void notifyTagDiscovered(bool discovered);
120 
121   /*******************************************************************************
122   **
123   ** Function:        isActivated
124   **
125   ** Description:     Is tag activated?
126   **
127   ** Returns:         True if tag is activated.
128   **
129   *******************************************************************************/
130   bool isActivated();
131 
132   /*******************************************************************************
133   **
134   ** Function:        getActivationState
135   **
136   ** Description:     What is the current state: Idle, Sleep, or Activated.
137   **
138   ** Returns:         Idle, Sleep, or Activated.
139   **
140   *******************************************************************************/
141   ActivationState getActivationState();
142 
143   /*******************************************************************************
144   **
145   ** Function:        setDeactivationState
146   **
147   ** Description:     Set the current state: Idle or Sleep.
148   **                  deactivated: state of deactivation.
149   **
150   ** Returns:         None.
151   **
152   *******************************************************************************/
153   void setDeactivationState(tNFA_DEACTIVATED& deactivated);
154 
155   /*******************************************************************************
156   **
157   ** Function:        setActivationState
158   **
159   ** Description:     Set the current state to Active.
160   **
161   ** Returns:         None.
162   **
163   *******************************************************************************/
164   void setActivationState();
165 
166   /*******************************************************************************
167   **
168   ** Function:        getProtocol
169   **
170   ** Description:     Get the protocol of the current tag.
171   **
172   ** Returns:         Protocol number.
173   **
174   *******************************************************************************/
175   tNFC_PROTOCOL getProtocol();
176 
177   /*******************************************************************************
178   **
179   ** Function:        selectFirstTag
180   **
181   ** Description:     When multiple tags are discovered, just select the first
182   *one to activate.
183   **
184   ** Returns:         None
185   **
186   *******************************************************************************/
187   void selectFirstTag();
188 
189   /*******************************************************************************
190   **
191   ** Function:        selectNextTagIfExists
192   **
193   ** Description:     When multiple tags are discovered, selects the Next one to
194   **                  activate.
195   **
196   ** Returns:         None
197   **
198   *******************************************************************************/
199   void selectNextTagIfExists();
200 
201   /*******************************************************************************
202   **
203   ** Function:        setLastSelectedTag
204   **
205   ** Description:     Set the last selected tag in case of multiprotocol tag
206   **
207   ** Returns:         NFA_STATUS_FAILED if tag is not found.
208   **
209   *******************************************************************************/
210   tNFA_STATUS setLastSelectedTag(int targetHandle, int nfcType);
211 
212   /*******************************************************************************
213   **
214   ** Function:        retrySelect
215   **
216   ** Description:     Retry select last tag in case of multiprotocol tag
217   **
218   ** Returns:         NFA_STATUS_FAILED if it is not a multiprotocol tag or
219   **                  retry is already done. Otherwise it returns Select status.
220   **
221   *******************************************************************************/
222   tNFA_STATUS retrySelect();
223 
224   /*******************************************************************************
225   **
226   ** Function:        clearSelectRetryCount
227   **
228   ** Description:     Clear select retry count.
229   **
230   ** Returns:         None.
231   **
232   *******************************************************************************/
233   void clearSelectRetryCount();
234 
235   /*******************************************************************************
236   **
237   ** Function:        selectTagAtIndex
238   **
239   ** Description:     When multiple tags are discovered, selects a tag at
240   **                  specified index
241   **
242   ** Returns:         Select result
243   **
244   *******************************************************************************/
245   tNFA_STATUS selectTagAtIndex(int index);
246 
247   /*******************************************************************************
248   **
249   ** Function:        getT1tMaxMessageSize
250   **
251   ** Description:     Get the maximum size (octet) that a T1T can store.
252   **
253   ** Returns:         Maximum size in octets.
254   **
255   *******************************************************************************/
256   int getT1tMaxMessageSize();
257 
258   /*******************************************************************************
259   **
260   ** Function:        isNfcForumT2T
261   **
262   ** Description:     Whether tag is Nfc-Forum based and uses read command for
263   **                  presence check.
264   **
265   ** Returns:         True if tag is isNfcForumT2T.
266   **
267   *******************************************************************************/
268   bool isNfcForumT2T();
269 
270   /*******************************************************************************
271   **
272   ** Function:        isMifareUltralight
273   **
274   ** Description:     Whether the currently activated tag is Mifare Ultralight.
275   **
276   ** Returns:         True if tag is Mifare Ultralight.
277   **
278   *******************************************************************************/
279   bool isMifareUltralight();
280 
281   /*******************************************************************************
282   **
283   ** Function:        isMifareDESFire
284   **
285   ** Description:     Whether the currently activated tag is Mifare DESFire.
286   **
287   ** Returns:         True if tag is Mifare DESFire.
288   **
289   *******************************************************************************/
290   bool isMifareDESFire();
291 
292   /*******************************************************************************
293   **
294   ** Function:        isFelicaLite
295   **
296   ** Description:     Whether the currently activated tag is Felica Lite.
297   **
298   ** Returns:         True if tag is Felica Lite.
299   **
300   *******************************************************************************/
301   bool isFelicaLite();
302 
303   /*******************************************************************************
304   **
305   ** Function:        isT2tNackResponse
306   **
307   ** Description:     Whether the response is a T2T NACK response.
308   **                  See NFC Digital Protocol Technical Specification
309   *(2010-11-17).
310   **                  Chapter 9 (Type 2 Tag Platform), section 9.6 (READ).
311   **                  response: buffer contains T2T response.
312   **                  responseLen: length of the response.
313   **
314   ** Returns:         True if the response is NACK
315   **
316   *******************************************************************************/
317   bool isT2tNackResponse(const uint8_t* response, uint32_t responseLen);
318 
319   /*******************************************************************************
320   **
321   ** Function:        isNdefDetectionTimedOut
322   **
323   ** Description:     Whether NDEF-detection algorithm has timed out.
324   **
325   ** Returns:         True if NDEF-detection algorithm timed out.
326   **
327   *******************************************************************************/
328   bool isNdefDetectionTimedOut();
329 
330   /*******************************************************************************
331   **
332   ** Function         setActive
333   **
334   ** Description      Sets the active state for the object
335   **
336   ** Returns          None.
337   **
338   *******************************************************************************/
339   void setActive(bool active);
340 
341   /*******************************************************************************
342   **
343   ** Function:        isDynamicTagId
344   **
345   ** Description:     Whether a tag has a dynamic tag ID.
346   **
347   ** Returns:         True if ID is dynamic.
348   **
349   *******************************************************************************/
350   bool isDynamicTagId();
351 
352   /*******************************************************************************
353   **
354   ** Function:        resetAllTransceiveTimeouts
355   **
356   ** Description:     Reset all timeouts for all technologies to default values.
357   **
358   ** Returns:         none
359   **
360   *******************************************************************************/
361   void resetAllTransceiveTimeouts(bool includeType4);
362 
363   /*******************************************************************************
364   **
365   ** Function:        isDefaultTransceiveTimeout
366   **
367   ** Description:     Is the timeout value for a technology the default value?
368   **                  techId: one of the values in TARGET_TYPE_* defined in
369   *NfcJniUtil.h.
370   **                  timeout: Check this value against the default value.
371   **
372   ** Returns:         True if timeout is equal to the default value.
373   **
374   *******************************************************************************/
375   bool isDefaultTransceiveTimeout(int techId, int timeout);
376 
377   /*******************************************************************************
378   **
379   ** Function:        getTransceiveTimeout
380   **
381   ** Description:     Get the timeout value for one technology.
382   **                  techId: one of the values in TARGET_TYPE_* defined in
383   **                  NfcJniUtil.h
384   **
385   ** Returns:         Timeout value in millisecond.
386   **
387   *******************************************************************************/
388   int getTransceiveTimeout(int techId);
389 
390   /*******************************************************************************
391   **
392   ** Function:        setTransceiveTimeout
393   **
394   ** Description:     Set the timeout value for one technology.
395   **                  techId: one of the values in TARGET_TYPE_* defined in
396   *NfcJniUtil.h
397   **                  timeout: timeout value in millisecond.
398   **
399   ** Returns:         Timeout value.
400   **
401   *******************************************************************************/
402   void setTransceiveTimeout(int techId, int timeout);
403 
404   /*******************************************************************************
405   **
406   ** Function:        getPresenceCheckAlgorithm
407   **
408   ** Description:     Get presence-check algorithm from .conf file.
409   **
410   ** Returns:         Presence-check algorithm.
411   **
412   *******************************************************************************/
413   tNFA_RW_PRES_CHK_OPTION getPresenceCheckAlgorithm();
414 
415   /*******************************************************************************
416   **
417   ** Function:        isInfineonMyDMove
418   **
419   ** Description:     Whether the currently activated tag is Infineon My-D Move.
420   **
421   ** Returns:         True if tag is Infineon My-D Move.
422   **
423   *******************************************************************************/
424   bool isInfineonMyDMove();
425 
426   /*******************************************************************************
427   **
428   ** Function:        isKovioType2Tag
429   **
430   ** Description:     Whether the currently activated tag is Kovio 2Kb RFID tag.
431   **                  It is a NFC Forum type-2 tag.
432   **
433   ** Returns:         True if tag is Kovio 2Kb RFID tag.
434   **
435   *******************************************************************************/
436   bool isKovioType2Tag();
437 
438   /*******************************************************************************
439   **
440   ** Function:        setMultiProtocolTagSupport
441   **
442   ** Description:     Update mIsMultiProtocolTag
443   **
444   ** Returns:         None
445   **
446   *******************************************************************************/
447   void setMultiProtocolTagSupport(bool isMultiProtocolSupported);
448 
449   /*******************************************************************************
450   **
451   ** Function:        getMultiProtocolTagSupport
452   **
453   ** Description:     get mIsMultiProtocolTag
454   **
455   ** Returns:         mIsMultiProtocolTag
456   **
457   *******************************************************************************/
458   bool getMultiProtocolTagSupport();
459 
460   /*******************************************************************************
461   **
462   ** Function:        setNumDiscNtf
463   **
464   ** Description:     Update mNumDiscNtf
465   **
466   ** Returns:         None
467   **
468   *******************************************************************************/
469   void setNumDiscNtf(int numDiscNtfValue);
470 
471   /*******************************************************************************
472   **
473   ** Function:        isReselecting
474   **
475   ** Description:     used to check if a reSelect() procedure is ongoing
476   **
477   ** Returns:         value of mIsReselecting variable
478   **
479   *******************************************************************************/
480   bool isReselecting();
481 
482   /*******************************************************************************
483   **
484   ** Function:        setReselect
485   **
486   ** Description:     Called by JNI to indicate status of reSelect() procedure
487   **
488   ** Returns:
489   **
490   *******************************************************************************/
491   void setReselect(bool isReselecting);
492 
493   /*******************************************************************************
494   **
495   ** Function:        getNumDiscNtf
496   **
497   ** Description:     number of discovery notifications received from NFCC after
498   **                  last RF DISCOVERY state
499   **
500   ** Returns:         number of discovery notifications received from NFCC
501   **
502   *******************************************************************************/
503   int getNumDiscNtf();
504 
505  private:
506   std::vector<int> mTechnologyTimeoutsTable;
507   std::vector<int> mTechnologyDefaultTimeoutsTable;
508   nfc_jni_native_data* mNativeData;
509   bool mIsActivated;
510   ActivationState mActivationState;
511   tNFC_PROTOCOL mProtocol;
512   int mtT1tMaxMessageSize;  // T1T max NDEF message size
513   tNFA_STATUS mReadCompletedStatus;
514   int mLastKovioUidLen;         // len of uid of last Kovio tag activated
515   bool mNdefDetectionTimedOut;  // whether NDEF detection algorithm timed out
516   tNFC_RF_TECH_PARAMS
517       mTechParams[MAX_NUM_TECHNOLOGY];  // array of technology parameters
518   SyncEvent mReadCompleteEvent;
519   struct timespec mLastKovioTime;  // time of last Kovio tag activation
520   uint8_t mLastKovioUid[NFC_KOVIO_MAX_LEN];  // uid of last Kovio tag activated
521   bool mIsDynamicTagId;  // whether the tag has dynamic tag ID
522   tNFA_RW_PRES_CHK_OPTION mPresenceCheckAlgorithm;
523   bool mIsFelicaLite;
524   int mTechHandlesDiscData[MAX_NUM_TECHNOLOGY];      // array of tag handles (RF
525                                                      // DISC ID) received from
526                                                      // RF_DISC_NTF
527   int mTechLibNfcTypesDiscData[MAX_NUM_TECHNOLOGY];  // array of detailed tag
528                                                      // types ( RF Protocol)
529                                                      // received from
530                                                      // RF_DISC_NTF
531   int mNumDiscNtf;
532   int mNumDiscTechList;
533   int mTechListTail;  // Index of Last added entry in mTechList
534   bool mIsMultiProtocolTag;
535   int mSelectRetryCount = 0;
536   NfcStatsUtil* mNfcStatsUtil;
537   JNIEnv* mJniEnv = NULL;
538 
539   /*******************************************************************************
540   **
541   ** Function:        IsSameKovio
542   **
543   ** Description:     Checks if tag activate is the same (UID) Kovio tag
544   *previously
545   **                  activated.  This is needed due to a problem with some
546   *Kovio
547   **                  tags re-activating multiple times.
548   **                  activationData: data from activation.
549   **
550   ** Returns:         true if the activation is from the same tag previously
551   **                  activated, false otherwise
552   **
553   *******************************************************************************/
554   bool IsSameKovio(tNFA_ACTIVATED& activationData);
555 
556   /*******************************************************************************
557   **
558   ** Function:        discoverTechnologies
559   **
560   ** Description:     Discover the technologies that NFC service needs by
561   *interpreting
562   **                  the data strucutures from the stack.
563   **                  activationData: data from activation.
564   **
565   ** Returns:         None
566   **
567   *******************************************************************************/
568   void discoverTechnologies(tNFA_ACTIVATED& activationData);
569 
570   /*******************************************************************************
571   **
572   ** Function:        discoverTechnologies
573   **
574   ** Description:     Discover the technologies that NFC service needs by
575   *interpreting
576   **                  the data strucutures from the stack.
577   **                  discoveryData: data from discovery events(s).
578   **
579   ** Returns:         None
580   **
581   *******************************************************************************/
582   void discoverTechnologies(tNFA_DISC_RESULT& discoveryData);
583 
584   /*******************************************************************************
585   **
586   ** Function:        createNativeNfcTag
587   **
588   ** Description:     Create a brand new Java NativeNfcTag object;
589   **                  fill the objects's member variables with data;
590   **                  notify NFC service;
591   **                  activationData: data from activation.
592   **
593   ** Returns:         None
594   **
595   *******************************************************************************/
596   void createNativeNfcTag(tNFA_ACTIVATED& activationData);
597 
598   /*******************************************************************************
599   **
600   ** Function:        fillNativeNfcTagMembers1
601   **
602   ** Description:     Fill NativeNfcTag's members: mProtocols, mTechList,
603   *mTechHandles, mTechLibNfcTypes.
604   **                  e: JVM environment.
605   **                  tag_cls: Java NativeNfcTag class.
606   **                  tag: Java NativeNfcTag object.
607   **
608   ** Returns:         None
609   **
610   *******************************************************************************/
611   void fillNativeNfcTagMembers1(JNIEnv* e, jclass tag_cls, jobject tag);
612 
613   /*******************************************************************************
614   **
615   ** Function:        fillNativeNfcTagMembers2
616   **
617   ** Description:     Fill NativeNfcTag's members: mConnectedTechIndex or
618   *mConnectedTechnology.
619   **                  The original Google's implementation is in
620   *set_target_pollBytes(
621   **                  in com_android_nfc_NativeNfcTag.cpp;
622   **                  e: JVM environment.
623   **                  tag_cls: Java NativeNfcTag class.
624   **                  tag: Java NativeNfcTag object.
625   **                  activationData: data from activation.
626   **
627   ** Returns:         None
628   **
629   *******************************************************************************/
630   void fillNativeNfcTagMembers2(JNIEnv* e, jclass tag_cls, jobject tag,
631                                 tNFA_ACTIVATED& activationData);
632 
633   /*******************************************************************************
634   **
635   ** Function:        fillNativeNfcTagMembers3
636   **
637   ** Description:     Fill NativeNfcTag's members: mTechPollBytes.
638   **                  The original Google's implementation is in
639   *set_target_pollBytes(
640   **                  in com_android_nfc_NativeNfcTag.cpp;
641   **                  e: JVM environment.
642   **                  tag_cls: Java NativeNfcTag class.
643   **                  tag: Java NativeNfcTag object.
644   **                  activationData: data from activation.
645   **
646   ** Returns:         None
647   **
648   *******************************************************************************/
649   void fillNativeNfcTagMembers3(JNIEnv* e, jclass tag_cls, jobject tag,
650                                 tNFA_ACTIVATED& activationData);
651 
652   /*******************************************************************************
653   **
654   ** Function:        fillNativeNfcTagMembers4
655   **
656   ** Description:     Fill NativeNfcTag's members: mTechActBytes.
657   **                  The original Google's implementation is in
658   *set_target_activationBytes()
659   **                  in com_android_nfc_NativeNfcTag.cpp;
660   **                  e: JVM environment.
661   **                  tag_cls: Java NativeNfcTag class.
662   **                  tag: Java NativeNfcTag object.
663   **                  activationData: data from activation.
664   **
665   ** Returns:         None
666   **
667   *******************************************************************************/
668   void fillNativeNfcTagMembers4(JNIEnv* e, jclass tag_cls, jobject tag,
669                                 tNFA_ACTIVATED& activationData);
670 
671   /*******************************************************************************
672   **
673   ** Function:        fillNativeNfcTagMembers5
674   **
675   ** Description:     Fill NativeNfcTag's members: mUid.
676   **                  The original Google's implementation is in
677   *nfc_jni_Discovery_notification_callback()
678   **                  in com_android_nfc_NativeNfcManager.cpp;
679   **                  e: JVM environment.
680   **                  tag_cls: Java NativeNfcTag class.
681   **                  tag: Java NativeNfcTag object.
682   **                  activationData: data from activation.
683   **
684   ** Returns:         None
685   **
686   *******************************************************************************/
687   void fillNativeNfcTagMembers5(JNIEnv* e, jclass tag_cls, jobject tag,
688                                 tNFA_ACTIVATED& activationData);
689 
690   /*******************************************************************************
691   **
692   ** Function:        resetTechnologies
693   **
694   ** Description:     Clear all data related to the technology, protocol of the
695   *tag.
696   **
697   ** Returns:         None
698   **
699   *******************************************************************************/
700   void resetTechnologies();
701 
702   /*******************************************************************************
703   **
704   ** Function:        calculateT1tMaxMessageSize
705   **
706   ** Description:     Calculate type-1 tag's max message size based on header
707   *ROM bytes.
708   **                  activate: reference to activation data.
709   **
710   ** Returns:         None
711   **
712   *******************************************************************************/
713   void calculateT1tMaxMessageSize(tNFA_ACTIVATED& activate);
714 };
715