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:        getT1tMaxMessageSize
204   **
205   ** Description:     Get the maximum size (octet) that a T1T can store.
206   **
207   ** Returns:         Maximum size in octets.
208   **
209   *******************************************************************************/
210   int getT1tMaxMessageSize();
211 
212   /*******************************************************************************
213   **
214   ** Function:        isNfcForumT2T
215   **
216   ** Description:     Whether tag is Nfc-Forum based and uses read command for
217   **                  presence check.
218   **
219   ** Returns:         True if tag is isNfcForumT2T.
220   **
221   *******************************************************************************/
222   bool isNfcForumT2T();
223 
224   /*******************************************************************************
225   **
226   ** Function:        isMifareUltralight
227   **
228   ** Description:     Whether the currently activated tag is Mifare Ultralight.
229   **
230   ** Returns:         True if tag is Mifare Ultralight.
231   **
232   *******************************************************************************/
233   bool isMifareUltralight();
234 
235   /*******************************************************************************
236   **
237   ** Function:        isMifareDESFire
238   **
239   ** Description:     Whether the currently activated tag is Mifare DESFire.
240   **
241   ** Returns:         True if tag is Mifare DESFire.
242   **
243   *******************************************************************************/
244   bool isMifareDESFire();
245 
246   /*******************************************************************************
247   **
248   ** Function:        isFelicaLite
249   **
250   ** Description:     Whether the currently activated tag is Felica Lite.
251   **
252   ** Returns:         True if tag is Felica Lite.
253   **
254   *******************************************************************************/
255   bool isFelicaLite();
256 
257   /*******************************************************************************
258   **
259   ** Function:        isT2tNackResponse
260   **
261   ** Description:     Whether the response is a T2T NACK response.
262   **                  See NFC Digital Protocol Technical Specification
263   *(2010-11-17).
264   **                  Chapter 9 (Type 2 Tag Platform), section 9.6 (READ).
265   **                  response: buffer contains T2T response.
266   **                  responseLen: length of the response.
267   **
268   ** Returns:         True if the response is NACK
269   **
270   *******************************************************************************/
271   bool isT2tNackResponse(const uint8_t* response, uint32_t responseLen);
272 
273   /*******************************************************************************
274   **
275   ** Function:        isNdefDetectionTimedOut
276   **
277   ** Description:     Whether NDEF-detection algorithm has timed out.
278   **
279   ** Returns:         True if NDEF-detection algorithm timed out.
280   **
281   *******************************************************************************/
282   bool isNdefDetectionTimedOut();
283 
284   /*******************************************************************************
285   **
286   ** Function         setActive
287   **
288   ** Description      Sets the active state for the object
289   **
290   ** Returns          None.
291   **
292   *******************************************************************************/
293   void setActive(bool active);
294 
295   /*******************************************************************************
296   **
297   ** Function:        isDynamicTagId
298   **
299   ** Description:     Whether a tag has a dynamic tag ID.
300   **
301   ** Returns:         True if ID is dynamic.
302   **
303   *******************************************************************************/
304   bool isDynamicTagId();
305 
306   /*******************************************************************************
307   **
308   ** Function:        resetAllTransceiveTimeouts
309   **
310   ** Description:     Reset all timeouts for all technologies to default values.
311   **
312   ** Returns:         none
313   **
314   *******************************************************************************/
315   void resetAllTransceiveTimeouts();
316 
317   /*******************************************************************************
318   **
319   ** Function:        isDefaultTransceiveTimeout
320   **
321   ** Description:     Is the timeout value for a technology the default value?
322   **                  techId: one of the values in TARGET_TYPE_* defined in
323   *NfcJniUtil.h.
324   **                  timeout: Check this value against the default value.
325   **
326   ** Returns:         True if timeout is equal to the default value.
327   **
328   *******************************************************************************/
329   bool isDefaultTransceiveTimeout(int techId, int timeout);
330 
331   /*******************************************************************************
332   **
333   ** Function:        getTransceiveTimeout
334   **
335   ** Description:     Get the timeout value for one technology.
336   **                  techId: one of the values in TARGET_TYPE_* defined in
337   **                  NfcJniUtil.h
338   **
339   ** Returns:         Timeout value in millisecond.
340   **
341   *******************************************************************************/
342   int getTransceiveTimeout(int techId);
343 
344   /*******************************************************************************
345   **
346   ** Function:        setTransceiveTimeout
347   **
348   ** Description:     Set the timeout value for one technology.
349   **                  techId: one of the values in TARGET_TYPE_* defined in
350   *NfcJniUtil.h
351   **                  timeout: timeout value in millisecond.
352   **
353   ** Returns:         Timeout value.
354   **
355   *******************************************************************************/
356   void setTransceiveTimeout(int techId, int timeout);
357 
358   /*******************************************************************************
359   **
360   ** Function:        getPresenceCheckAlgorithm
361   **
362   ** Description:     Get presence-check algorithm from .conf file.
363   **
364   ** Returns:         Presence-check algorithm.
365   **
366   *******************************************************************************/
367   tNFA_RW_PRES_CHK_OPTION getPresenceCheckAlgorithm();
368 
369   /*******************************************************************************
370   **
371   ** Function:        isInfineonMyDMove
372   **
373   ** Description:     Whether the currently activated tag is Infineon My-D Move.
374   **
375   ** Returns:         True if tag is Infineon My-D Move.
376   **
377   *******************************************************************************/
378   bool isInfineonMyDMove();
379 
380   /*******************************************************************************
381   **
382   ** Function:        isKovioType2Tag
383   **
384   ** Description:     Whether the currently activated tag is Kovio 2Kb RFID tag.
385   **                  It is a NFC Forum type-2 tag.
386   **
387   ** Returns:         True if tag is Kovio 2Kb RFID tag.
388   **
389   *******************************************************************************/
390   bool isKovioType2Tag();
391 
392   /*******************************************************************************
393   **
394   ** Function:        setMultiProtocolTagSupport
395   **
396   ** Description:     Update mIsMultiProtocolTag
397   **
398   ** Returns:         None
399   **
400   *******************************************************************************/
401   void setMultiProtocolTagSupport(bool isMultiProtocolSupported);
402 
403   /*******************************************************************************
404   **
405   ** Function:        setNumDiscNtf
406   **
407   ** Description:     Update mNumDiscNtf
408   **
409   ** Returns:         None
410   **
411   *******************************************************************************/
412   void setNumDiscNtf(int numDiscNtfValue);
413 
414   /*******************************************************************************
415   **
416   ** Function:        isReselecting
417   **
418   ** Description:     used to check if a reSelect() procedure is ongoing
419   **
420   ** Returns:         value of mIsReselecting variable
421   **
422   *******************************************************************************/
423   bool isReselecting();
424 
425   /*******************************************************************************
426   **
427   ** Function:        setReselect
428   **
429   ** Description:     Called by JNI to indicate status of reSelect() procedure
430   **
431   ** Returns:
432   **
433   *******************************************************************************/
434   void setReselect(bool isReselecting);
435 
436   /*******************************************************************************
437   **
438   ** Function:        getNumDiscNtf
439   **
440   ** Description:     number of discovery notifications received from NFCC after
441   **                  last RF DISCOVERY state
442   **
443   ** Returns:         number of discovery notifications received from NFCC
444   **
445   *******************************************************************************/
446   int getNumDiscNtf();
447 
448  private:
449   std::vector<int> mTechnologyTimeoutsTable;
450   std::vector<int> mTechnologyDefaultTimeoutsTable;
451   nfc_jni_native_data* mNativeData;
452   bool mIsActivated;
453   ActivationState mActivationState;
454   tNFC_PROTOCOL mProtocol;
455   int mtT1tMaxMessageSize;  // T1T max NDEF message size
456   tNFA_STATUS mReadCompletedStatus;
457   int mLastKovioUidLen;         // len of uid of last Kovio tag activated
458   bool mNdefDetectionTimedOut;  // whether NDEF detection algorithm timed out
459   tNFC_RF_TECH_PARAMS
460       mTechParams[MAX_NUM_TECHNOLOGY];  // array of technology parameters
461   SyncEvent mReadCompleteEvent;
462   struct timespec mLastKovioTime;  // time of last Kovio tag activation
463   uint8_t mLastKovioUid[NFC_KOVIO_MAX_LEN];  // uid of last Kovio tag activated
464   bool mIsDynamicTagId;  // whether the tag has dynamic tag ID
465   tNFA_RW_PRES_CHK_OPTION mPresenceCheckAlgorithm;
466   bool mIsFelicaLite;
467   int mTechHandlesDiscData[MAX_NUM_TECHNOLOGY];      // array of tag handles (RF
468                                                      // DISC ID) received from
469                                                      // RF_DISC_NTF
470   int mTechLibNfcTypesDiscData[MAX_NUM_TECHNOLOGY];  // array of detailed tag
471                                                      // types ( RF Protocol)
472                                                      // received from
473                                                      // RF_DISC_NTF
474   int mNumDiscNtf;
475   int mNumDiscTechList;
476   int mTechListTail;  // Index of Last added entry in mTechList
477   bool mIsMultiProtocolTag;
478   NfcStatsUtil* mNfcStatsUtil;
479 
480   /*******************************************************************************
481   **
482   ** Function:        IsSameKovio
483   **
484   ** Description:     Checks if tag activate is the same (UID) Kovio tag
485   *previously
486   **                  activated.  This is needed due to a problem with some
487   *Kovio
488   **                  tags re-activating multiple times.
489   **                  activationData: data from activation.
490   **
491   ** Returns:         true if the activation is from the same tag previously
492   **                  activated, false otherwise
493   **
494   *******************************************************************************/
495   bool IsSameKovio(tNFA_ACTIVATED& activationData);
496 
497   /*******************************************************************************
498   **
499   ** Function:        discoverTechnologies
500   **
501   ** Description:     Discover the technologies that NFC service needs by
502   *interpreting
503   **                  the data strucutures from the stack.
504   **                  activationData: data from activation.
505   **
506   ** Returns:         None
507   **
508   *******************************************************************************/
509   void discoverTechnologies(tNFA_ACTIVATED& activationData);
510 
511   /*******************************************************************************
512   **
513   ** Function:        discoverTechnologies
514   **
515   ** Description:     Discover the technologies that NFC service needs by
516   *interpreting
517   **                  the data strucutures from the stack.
518   **                  discoveryData: data from discovery events(s).
519   **
520   ** Returns:         None
521   **
522   *******************************************************************************/
523   void discoverTechnologies(tNFA_DISC_RESULT& discoveryData);
524 
525   /*******************************************************************************
526   **
527   ** Function:        createNativeNfcTag
528   **
529   ** Description:     Create a brand new Java NativeNfcTag object;
530   **                  fill the objects's member variables with data;
531   **                  notify NFC service;
532   **                  activationData: data from activation.
533   **
534   ** Returns:         None
535   **
536   *******************************************************************************/
537   void createNativeNfcTag(tNFA_ACTIVATED& activationData);
538 
539   /*******************************************************************************
540   **
541   ** Function:        fillNativeNfcTagMembers1
542   **
543   ** Description:     Fill NativeNfcTag's members: mProtocols, mTechList,
544   *mTechHandles, mTechLibNfcTypes.
545   **                  e: JVM environment.
546   **                  tag_cls: Java NativeNfcTag class.
547   **                  tag: Java NativeNfcTag object.
548   **
549   ** Returns:         None
550   **
551   *******************************************************************************/
552   void fillNativeNfcTagMembers1(JNIEnv* e, jclass tag_cls, jobject tag);
553 
554   /*******************************************************************************
555   **
556   ** Function:        fillNativeNfcTagMembers2
557   **
558   ** Description:     Fill NativeNfcTag's members: mConnectedTechIndex or
559   *mConnectedTechnology.
560   **                  The original Google's implementation is in
561   *set_target_pollBytes(
562   **                  in com_android_nfc_NativeNfcTag.cpp;
563   **                  e: JVM environment.
564   **                  tag_cls: Java NativeNfcTag class.
565   **                  tag: Java NativeNfcTag object.
566   **                  activationData: data from activation.
567   **
568   ** Returns:         None
569   **
570   *******************************************************************************/
571   void fillNativeNfcTagMembers2(JNIEnv* e, jclass tag_cls, jobject tag,
572                                 tNFA_ACTIVATED& activationData);
573 
574   /*******************************************************************************
575   **
576   ** Function:        fillNativeNfcTagMembers3
577   **
578   ** Description:     Fill NativeNfcTag's members: mTechPollBytes.
579   **                  The original Google's implementation is in
580   *set_target_pollBytes(
581   **                  in com_android_nfc_NativeNfcTag.cpp;
582   **                  e: JVM environment.
583   **                  tag_cls: Java NativeNfcTag class.
584   **                  tag: Java NativeNfcTag object.
585   **                  activationData: data from activation.
586   **
587   ** Returns:         None
588   **
589   *******************************************************************************/
590   void fillNativeNfcTagMembers3(JNIEnv* e, jclass tag_cls, jobject tag,
591                                 tNFA_ACTIVATED& activationData);
592 
593   /*******************************************************************************
594   **
595   ** Function:        fillNativeNfcTagMembers4
596   **
597   ** Description:     Fill NativeNfcTag's members: mTechActBytes.
598   **                  The original Google's implementation is in
599   *set_target_activationBytes()
600   **                  in com_android_nfc_NativeNfcTag.cpp;
601   **                  e: JVM environment.
602   **                  tag_cls: Java NativeNfcTag class.
603   **                  tag: Java NativeNfcTag object.
604   **                  activationData: data from activation.
605   **
606   ** Returns:         None
607   **
608   *******************************************************************************/
609   void fillNativeNfcTagMembers4(JNIEnv* e, jclass tag_cls, jobject tag,
610                                 tNFA_ACTIVATED& activationData);
611 
612   /*******************************************************************************
613   **
614   ** Function:        fillNativeNfcTagMembers5
615   **
616   ** Description:     Fill NativeNfcTag's members: mUid.
617   **                  The original Google's implementation is in
618   *nfc_jni_Discovery_notification_callback()
619   **                  in com_android_nfc_NativeNfcManager.cpp;
620   **                  e: JVM environment.
621   **                  tag_cls: Java NativeNfcTag class.
622   **                  tag: Java NativeNfcTag object.
623   **                  activationData: data from activation.
624   **
625   ** Returns:         None
626   **
627   *******************************************************************************/
628   void fillNativeNfcTagMembers5(JNIEnv* e, jclass tag_cls, jobject tag,
629                                 tNFA_ACTIVATED& activationData);
630 
631   /*******************************************************************************
632   **
633   ** Function:        resetTechnologies
634   **
635   ** Description:     Clear all data related to the technology, protocol of the
636   *tag.
637   **
638   ** Returns:         None
639   **
640   *******************************************************************************/
641   void resetTechnologies();
642 
643   /*******************************************************************************
644   **
645   ** Function:        calculateT1tMaxMessageSize
646   **
647   ** Description:     Calculate type-1 tag's max message size based on header
648   *ROM bytes.
649   **                  activate: reference to activation data.
650   **
651   ** Returns:         None
652   **
653   *******************************************************************************/
654   void calculateT1tMaxMessageSize(tNFA_ACTIVATED& activate);
655 };
656