aboutsummaryrefslogtreecommitdiffstats
path: root/nci/jni/RoutingManager.h
blob: aef6f8fd663f44304c9a9efbd4b53415d248b924 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/******************************************************************************
 *
 *  The original Work has been changed by NXP Semiconductors.
 *
 *  Copyright (C) 2015 NXP Semiconductors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/
/*
 *  Manage the listen-mode routing table.
 */
#pragma once
#include "SyncEvent.h"
#include "NfcJniUtil.h"
#include "RouteDataSet.h"
#include "SecureElement.h"
#include <vector>
extern "C"
{
    #include "nfa_api.h"
    #include "nfa_ee_api.h"
}
#if(NXP_EXTNS == TRUE)
#define TECHNOLOGY_BASED_ROUTING        0x00
#define PROTOCOL_BASED_ROUTING          0x01
#define AID_BASED_ROUTING               0x02
#define AVAILABLE_PROTO_ENTRIES() ((NFA_GetNCIVersion() == NCI_VERSION_2_0)?0x02:0x03)

/*Size of type and length Fields : No of bytes*/
#define TYPE_LENGTH_SIZE                0x02

#define MAX_GET_ROUTING_BUFFER_SIZE     740
#define EE_HCI_DEFAULT_HANDLE           0x401

typedef struct protoroutInfo {
    uint8_t ee_handle;
    tNFA_PROTOCOL_MASK  protocols_switch_on;
    tNFA_PROTOCOL_MASK  protocols_switch_off;
    tNFA_PROTOCOL_MASK  protocols_battery_off;
    tNFA_PROTOCOL_MASK  protocols_screen_lock;
    tNFA_PROTOCOL_MASK  protocols_screen_off;
    tNFA_PROTOCOL_MASK  protocols_screen_off_lock;
}ProtoRoutInfo_t;

typedef struct routeInfo {
    uint8_t num_entries;
    ProtoRoutInfo_t protoInfo[4];
}RouteInfo_t;
#define POWER_STATE_MASK 0xE7
#define HOST_SCREEN_STATE_MASK 0x09
#endif

//FelicaOnHost
typedef struct{

    uint8_t nfcid2Len;
    uint8_t sysCodeLen;
    uint8_t optParamLen;
    uint16_t nfcid2Handle;
    uint8_t sysCode[2];
    uint8_t nfcid2[8];
    uint8_t* optParam;
    uint8_t InUse;

   // Mutex mMutex; /*add if it is required */
}NfcID2_info_t;

typedef struct
{
    uint8_t protocol;
    uint16_t routeLoc;
    uint8_t power;
    uint8_t enable;
} protoEntry_t;

typedef struct
{
    uint8_t technology;
    uint16_t routeLoc;
    uint8_t power;
    uint8_t enable;
} techEntry_t;

typedef struct
{
    uint16_t nfceeID;//ID for the route location
    tNFA_TECHNOLOGY_MASK    tech_switch_on;     /* default routing - technologies switch_on  */
    tNFA_TECHNOLOGY_MASK    tech_switch_off;    /* default routing - technologies switch_off */
    tNFA_TECHNOLOGY_MASK    tech_battery_off;   /* default routing - technologies battery_off*/
    tNFA_TECHNOLOGY_MASK    tech_screen_lock;    /* default routing - technologies screen_lock*/
    tNFA_TECHNOLOGY_MASK    tech_screen_off;   /* default routing - technologies screen_off*/
    tNFA_TECHNOLOGY_MASK    tech_screen_off_lock;  /* default routing - technologies screen_off_lock*/
    tNFA_PROTOCOL_MASK      proto_switch_on;    /* default routing - protocols switch_on     */
    tNFA_PROTOCOL_MASK      proto_switch_off;   /* default routing - protocols switch_off    */
    tNFA_PROTOCOL_MASK      proto_battery_off;  /* default routing - protocols battery_off   */
    tNFA_PROTOCOL_MASK      proto_screen_lock;   /* default routing - protocols screen_lock    */
    tNFA_PROTOCOL_MASK      proto_screen_off;  /* default routing - protocols screen_off  */
    tNFA_PROTOCOL_MASK      proto_screen_off_lock;  /* default routing - protocols screen_off_lock  */

} LmrtEntry_t;

class RoutingManager
{
public:
#if(NXP_EXTNS == TRUE)
    static const int NFA_SET_AID_ROUTING = 4;
    static const int NFA_SET_TECHNOLOGY_ROUTING = 1;
    static const int NFA_SET_PROTOCOL_ROUTING = 2;
    nfc_jni_native_data* mNativeData;
#endif

    static const int ROUTE_HOST = 0;
    static const int ROUTE_ESE = 1;

    static RoutingManager& getInstance ();
    bool initialize(nfc_jni_native_data* native);
#if(NXP_EXTNS == TRUE)
    void setRouting(bool);
    void getRouting();
    void registerProtoRouteEntry(tNFA_HANDLE ee_handle,
                                 tNFA_PROTOCOL_MASK  protocols_switch_on,
                                 tNFA_PROTOCOL_MASK  protocols_switch_off,
                                 tNFA_PROTOCOL_MASK  protocols_battery_off,
                                 tNFA_PROTOCOL_MASK  protocols_screen_lock,
                                 tNFA_PROTOCOL_MASK  protocols_screen_off,
                                 tNFA_PROTOCOL_MASK  protocols_screen_off_lock
                                 );
    void notifyReRoutingEntry();
    void HandleAddNfcID2_Req();
    void HandleRmvNfcID2_Req();
    void setCeRouteStrictDisable(uint32_t state);
    void setDefaultTechRouting (int seId, int tech_switchon,int tech_switchoff);
    void setDefaultProtoRouting (int seId, int proto_switchon,int proto_switchoff);
    void processGetRoutingRsp(tNFA_DM_CBACK_DATA* eventData, uint8_t* sRoutingBuff);
    void nfaEEConnect();
    void nfaEEDisconnect();
    SyncEvent mEEDisconnectEvt;
    bool setRoutingEntry(int type, int value, int route, int power);
    bool clearRoutingEntry(int type);
    bool setDefaultRoute(const int defaultRoute, const int protoRoute, const int techRoute);
    bool clearAidTable ();
    bool removeNfcid2Routing(uint8_t* nfcID2);
    bool addAidRouting(const uint8_t* aid, uint8_t aidLen, int route, int power, int aidInfo);
    int  addNfcid2Routing(uint8_t* nfcid2, uint8_t aidLen,const uint8_t* syscode,
    int  syscodelen,const uint8_t* optparam, int optparamlen);
    void handleSERemovedNtf();
    bool is_ee_recovery_ongoing();
    void setEtsiReaederState(se_rd_req_state_t newState);
    se_rd_req_state_t getEtsiReaederState();
    Rdr_req_ntf_info_t getSwpRrdReqInfo();
#if(NXP_NFCC_HCE_F == TRUE)
    void notifyT3tConfigure();
#endif
#else
    bool addAidRouting(const uint8_t* aid, uint8_t aidLen, int route);
#endif

    bool addApduRouting(uint8_t route, uint8_t powerState,const uint8_t* apduData,
         uint8_t apduDataLen ,const uint8_t* apduMask, uint8_t apduMaskLen);

    bool removeApduRouting(uint8_t apduDataLen, const uint8_t* apduData);

    void cleanRouting();
    bool removeAidRouting(const uint8_t* aid, uint8_t aidLen);
    bool commitRouting();
    int registerT3tIdentifier(uint8_t* t3tId, uint8_t t3tIdLen);
    void deregisterT3tIdentifier(int handle);
    void onNfccShutdown();
    int registerJniFunctions (JNIEnv* e);
    void ee_removed_disc_ntf_handler(tNFA_HANDLE handle, tNFA_EE_STATUS status);
    SyncEvent mLmrtEvent;
    SyncEvent mEeSetModeEvent;
    SyncEvent mCeRegisterEvent;//FelicaOnHost
    SyncEvent mCeDeRegisterEvent;
    Mutex  mResetHandlerMutex;
    IntervalTimer LmrtRspTimer;
    SyncEvent mEeUpdateEvent;
    IntervalTimer mNfcFRspTimer;
private:
    RoutingManager();
    ~RoutingManager();
    RoutingManager(const RoutingManager&);
    RoutingManager& operator=(const RoutingManager&);

    void handleData (uint8_t technology, const uint8_t* data, uint32_t dataLen, tNFA_STATUS status);
    void notifyActivated (uint8_t technology);
    void notifyDeactivated (uint8_t technology);
    void notifyLmrtFull();
    void printMemberData(void);
    void extractRouteLocationAndPowerStates(const int defaultRoute, const int protoRoute, const int techRoute);
    uint16_t getUiccRouteLocId(const int route);
    void initialiseTableEntries(void);
    void compileProtoEntries(void);
    void compileTechEntries(void);
    void consolidateProtoEntries(void);
    void consolidateTechEntries(void);
    void setProtoRouting(void);
#if(NXP_EXTNS == TRUE)
    void setEmptyAidEntry(void);
#endif
    void setTechRouting(void);
    void processTechEntriesForFwdfunctionality(void);
    void configureOffHostNfceeTechMask(void);
    void checkProtoSeID(void);
    void dumpTables(int);

    static const int DBG               = true;
    //Currently 4 protocols supported namely T3T, ISO-DEP, ISO-7816, NFC-DEP(taken care internally by the libnfc stack)
    static const int MAX_PROTO_ENTRIES = 0x03;
    static const int PROTO_T3T_IDX     = 0x00;
    static const int PROTO_ISODEP_IDX  = 0x01;
    static const int PROTO_ISO7816_IDX = 0x02;
    //Currently 3 Technologies supported namely A,B,F
    static const int MAX_TECH_ENTRIES  = 0x03;
    static const int TECH_A_IDX        = 0x00;
    static const int TECH_B_IDX        = 0x01;
    static const int TECH_F_IDX        = 0x02;
    //Fixed number of Lmrt entries
    static const int MAX_ROUTE_LOC_ENTRIES  = 0x04;
    //Fixed route location Lmrt index
    static const int ROUTE_LOC_HOST_ID_IDX  = 0x00;
    static const int ROUTE_LOC_ESE_ID_IDX   = 0x01;
    static const int ROUTE_LOC_UICC1_ID_IDX = 0x02;
    static const int ROUTE_LOC_UICC2_ID_IDX = 0x03;
    //Fixed route location Lmrt entries
    static const int ROUTE_LOC_HOST_ID      = 0x400;
    static const int ROUTE_LOC_ESE_ID       = SecureElement::EE_HANDLE_0xF3;
    static const int ROUTE_LOC_UICC1_ID     = 0x402;
    static const int ROUTE_LOC_UICC1_ID_NCI2_0 = 0x480;
    static const int ROUTE_LOC_UICC2_ID     = SecureElement::EE_HANDLE_0xF8;
    // Fixed power states masks
    static const int PWR_SWTCH_ON_SCRN_UNLCK_MASK       = 0x01;
    static const int PWR_SWTCH_OFF_MASK                 = 0x02;
    static const int PWR_BATT_OFF_MASK                  = 0x04;
    static const int PWR_SWTCH_ON_SCRN_LOCK_MASK        = 0x08;
    static const int PWR_SWTCH_ON_SCRN_OFF_MASK         = 0x10;
    static const int PWR_SWTCH_ON_SCRN_OFF_LOCK_MASK    = 0x20;
    // See AidRoutingManager.java for corresponding
    // AID_MATCHING_ constants
    // Every routing table entry is matched exact (BCM20793)
    static const int AID_MATCHING_EXACT_ONLY = 0x00;
    // Every routing table entry can be matched either exact or prefix
    static const int AID_MATCHING_EXACT_OR_PREFIX = 0x01;
    // Every routing table entry is matched as a prefix
    static const int AID_MATCHING_PREFIX_ONLY = 0x02;
    // See AidRoutingManager.java for corresponding
    // AID_MATCHING_ platform constants
    //Behavior as per Android-L, supporting prefix match and full
    //match for both OnHost and OffHost apps.
    static const int AID_MATCHING_L = 0x01;
    //Behavior as per Android-KitKat by NXP, supporting prefix match for
    //OffHost and prefix and full both for OnHost apps.
    static const int AID_MATCHING_K = 0x02;
    static void nfcFRspTimerCb(union sigval);
    static void nfaEeCallback (tNFA_EE_EVT event, tNFA_EE_CBACK_DATA* eventData);
    static void stackCallback (uint8_t event, tNFA_CONN_EVT_DATA* eventData);
    static void nfcFCeCallback (uint8_t event, tNFA_CONN_EVT_DATA* eventData);
    static int com_android_nfc_cardemulation_doGetDefaultRouteDestination (JNIEnv* e);
    static int com_android_nfc_cardemulation_doGetDefaultOffHostRouteDestination (JNIEnv* e);
    static int com_android_nfc_cardemulation_doGetAidMatchingMode (JNIEnv* e);
    static int com_android_nfc_cardemulation_doGetAidMatchingPlatform (JNIEnv* e);

    std::vector<uint8_t> mRxDataBuffer;

    // Fields below are final after initialize()
    //int mDefaultEe;
    int mDefaultEeNfcF;
    int mOffHostEe;
    int mActiveSe;
    int mActiveSeNfcF;
    int mAidMatchingMode;
    int mNfcFOnDhHandle;
    int mAidMatchingPlatform;
    tNFA_TECHNOLOGY_MASK mSeTechMask;
    static const JNINativeMethod sMethods [];
    int mDefaultEe; //since this variable is used in both cases moved out of compiler switch
    int mHostListnTechMask;
    int mUiccListnTechMask;
    int mFwdFuntnEnable;
    static int mChipId;
    SyncEvent mEeRegisterEvent;
    SyncEvent mRoutingEvent;
#if(NXP_EXTNS == TRUE)
    bool mIsDirty;
    protoEntry_t mProtoTableEntries[MAX_PROTO_ENTRIES];
    techEntry_t mTechTableEntries[MAX_TECH_ENTRIES];
    LmrtEntry_t mLmrtEntries[MAX_ROUTE_LOC_ENTRIES];
    uint32_t mCeRouteStrictDisable;
    uint32_t mDefaultIso7816SeID;
    uint32_t mDefaultIso7816Powerstate;
    uint32_t mDefaultIsoDepSeID;
    uint32_t mDefaultIsoDepPowerstate;
    uint32_t mDefaultT3TSeID;
    uint32_t mDefaultT3TPowerstate;
    uint32_t mDefaultTechType;
    uint32_t mDefaultTechASeID;
    uint32_t mDefaultTechAPowerstate;
    uint32_t mDefaultTechBSeID;
    uint32_t mDefaultTechBPowerstate;
    uint32_t mDefaultTechFSeID;
    uint32_t mDefaultTechFPowerstate;
    uint32_t mAddAid;
    uint32_t mTechSupportedByEse;
    uint32_t mTechSupportedByUicc1;
    uint32_t mTechSupportedByUicc2;
#endif
    uint32_t mDefaultHCEFRspTimeout;
};