1 /*
2  * Copyright (C) 2013 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  *  Manage the listen-mode routing table.
19  */
20 #pragma once
21 #include <vector>
22 #include "NfcJniUtil.h"
23 #include "RouteDataSet.h"
24 #include "SyncEvent.h"
25 
26 #include <map>
27 #include "nfa_api.h"
28 #include "nfa_ee_api.h"
29 
30 using namespace std;
31 
32 class RoutingManager {
33  public:
34   static RoutingManager& getInstance();
35   bool initialize(nfc_jni_native_data* native);
36   void deinitialize();
37   void enableRoutingToHost();
38   void disableRoutingToHost();
39   bool addAidRouting(const uint8_t* aid, uint8_t aidLen, int route, int aidInfo,
40                      int power);
41   bool removeAidRouting(const uint8_t* aid, uint8_t aidLen);
42   bool commitRouting();
43   int registerT3tIdentifier(uint8_t* t3tId, uint8_t t3tIdLen);
44   void deregisterT3tIdentifier(int handle);
45   void onNfccShutdown();
46   int registerJniFunctions(JNIEnv* e);
47   bool setNfcSecure(bool enable);
48   void updateRoutingTable();
49   void eeSetPwrAndLinkCtrl(uint8_t config);
50   void updateIsoDepProtocolRoute(int route);
51   tNFA_TECHNOLOGY_MASK updateTechnologyABRoute(int route);
52   void clearRoutingEntry(int clearFlags);
53   void setEeTechRouteUpdateRequired();
54 
55   static const int CLEAR_AID_ENTRIES = 0x01;
56   static const int CLEAR_PROTOCOL_ENTRIES = 0x02;
57   static const int CLEAR_TECHNOLOGY_ENTRIES = 0x04;
58 
59  private:
60   RoutingManager();
61   ~RoutingManager();
62   RoutingManager(const RoutingManager&);
63   RoutingManager& operator=(const RoutingManager&);
64 
65   void handleData(uint8_t technology, const uint8_t* data, uint32_t dataLen,
66                   tNFA_STATUS status);
67   void notifyActivated(uint8_t technology);
68   void notifyDeactivated(uint8_t technology);
69   void notifyEeUpdated();
70   tNFA_TECHNOLOGY_MASK updateEeTechRouteSetting();
71   void updateDefaultProtocolRoute();
72   void updateDefaultRoute();
73   bool isTypeATypeBTechSupportedInEe(tNFA_HANDLE eeHandle);
74 
75   // See AidRoutingManager.java for corresponding
76   // AID_MATCHING_ constants
77 
78   // Every routing table entry is matched exact (BCM20793)
79   static const int AID_MATCHING_EXACT_ONLY = 0x00;
80   // Every routing table entry can be matched either exact or prefix
81   static const int AID_MATCHING_EXACT_OR_PREFIX = 0x01;
82   // Every routing table entry is matched as a prefix
83   static const int AID_MATCHING_PREFIX_ONLY = 0x02;
84 
85   static void nfaEeCallback(tNFA_EE_EVT event, tNFA_EE_CBACK_DATA* eventData);
86   static void stackCallback(uint8_t event, tNFA_CONN_EVT_DATA* eventData);
87   static void nfcFCeCallback(uint8_t event, tNFA_CONN_EVT_DATA* eventData);
88 
89   static int com_android_nfc_cardemulation_doGetDefaultRouteDestination(
90       JNIEnv* e);
91   static int com_android_nfc_cardemulation_doGetDefaultOffHostRouteDestination(
92       JNIEnv* e);
93   static jbyteArray com_android_nfc_cardemulation_doGetOffHostUiccDestination(
94       JNIEnv* e);
95   static jbyteArray com_android_nfc_cardemulation_doGetOffHostEseDestination(
96       JNIEnv* e);
97   static int com_android_nfc_cardemulation_doGetAidMatchingMode(JNIEnv* e);
98   static int com_android_nfc_cardemulation_doGetDefaultIsoDepRouteDestination(
99       JNIEnv* e);
100 
101   std::vector<uint8_t> mRxDataBuffer;
102   map<int, uint16_t> mMapScbrHandle;
103   bool mSecureNfcEnabled;
104 
105   // Fields below are final after initialize()
106   nfc_jni_native_data* mNativeData;
107   int mDefaultOffHostRoute;
108   vector<uint8_t> mOffHostRouteUicc;
109   vector<uint8_t> mOffHostRouteEse;
110   int mDefaultFelicaRoute;
111   int mDefaultEe;
112   int mDefaultIsoDepRoute;
113   int mAidMatchingMode;
114   int mNfcFOnDhHandle;
115   bool mIsScbrSupported;
116   uint16_t mDefaultSysCode;
117   uint16_t mDefaultSysCodeRoute;
118   uint8_t mDefaultSysCodePowerstate;
119   uint8_t mOffHostAidRoutingPowerState;
120   uint8_t mHostListenTechMask;
121   uint8_t mOffHostListenTechMask;
122   bool mDeinitializing;
123   bool mEeInfoChanged;
124   bool mReceivedEeInfo;
125   bool mAidRoutingConfigured;
126   tNFA_EE_CBACK_DATA mCbEventData;
127   tNFA_EE_DISCOVER_REQ mEeInfo;
128   tNFA_TECHNOLOGY_MASK mSeTechMask;
129   static const JNINativeMethod sMethods[];
130   SyncEvent mEeRegisterEvent;
131   SyncEvent mRoutingEvent;
132   SyncEvent mEeUpdateEvent;
133   SyncEvent mEeInfoEvent;
134   SyncEvent mEeSetModeEvent;
135   SyncEvent mEePwrAndLinkCtrlEvent;
136   SyncEvent mAidAddRemoveEvent;
137 };
138