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