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