• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include <android-base/stringprintf.h>
22 #include <base/logging.h>
23 #include <nativehelper/JNIHelp.h>
24 #include <nativehelper/ScopedLocalRef.h>
25 
26 #include "JavaClassConstants.h"
27 #include "RoutingManager.h"
28 #include "nfa_ce_api.h"
29 #include "nfa_ee_api.h"
30 #include "nfc_config.h"
31 
32 using android::base::StringPrintf;
33 
34 extern bool gActivated;
35 extern SyncEvent gDeactivatedEvent;
36 extern bool nfc_debug_enabled;
37 
38 const JNINativeMethod RoutingManager::sMethods[] = {
39     {"doGetDefaultRouteDestination", "()I",
40      (void*)RoutingManager::
41          com_android_nfc_cardemulation_doGetDefaultRouteDestination},
42     {"doGetDefaultOffHostRouteDestination", "()I",
43      (void*)RoutingManager::
44          com_android_nfc_cardemulation_doGetDefaultOffHostRouteDestination},
45     {"doGetOffHostUiccDestination", "()[B",
46      (void*)RoutingManager::
47          com_android_nfc_cardemulation_doGetOffHostUiccDestination},
48     {"doGetOffHostEseDestination", "()[B",
49      (void*)RoutingManager::
50          com_android_nfc_cardemulation_doGetOffHostEseDestination},
51     {"doGetAidMatchingMode", "()I",
52      (void*)RoutingManager::com_android_nfc_cardemulation_doGetAidMatchingMode},
53     {"doGetDefaultIsoDepRouteDestination", "()I",
54      (void*)RoutingManager::
55          com_android_nfc_cardemulation_doGetDefaultIsoDepRouteDestination}};
56 
57 static const int MAX_NUM_EE = 5;
58 // SCBR from host works only when App is in foreground
59 static const uint8_t SYS_CODE_PWR_STATE_HOST = 0x01;
60 static const uint16_t DEFAULT_SYS_CODE = 0xFEFE;
61 
62 static const uint8_t AID_ROUTE_QUAL_PREFIX = 0x10;
63 
RoutingManager()64 RoutingManager::RoutingManager() : mAidRoutingConfigured(false) {
65   static const char fn[] = "RoutingManager::RoutingManager()";
66 
67   mDefaultOffHostRoute =
68       NfcConfig::getUnsigned(NAME_DEFAULT_OFFHOST_ROUTE, 0x00);
69 
70   if (NfcConfig::hasKey(NAME_OFFHOST_ROUTE_UICC)) {
71     mOffHostRouteUicc = NfcConfig::getBytes(NAME_OFFHOST_ROUTE_UICC);
72   }
73 
74   if (NfcConfig::hasKey(NAME_OFFHOST_ROUTE_ESE)) {
75     mOffHostRouteEse = NfcConfig::getBytes(NAME_OFFHOST_ROUTE_ESE);
76   }
77 
78   mDefaultFelicaRoute = NfcConfig::getUnsigned(NAME_DEFAULT_NFCF_ROUTE, 0x00);
79   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
80       "%s: Active SE for Nfc-F is 0x%02X", fn, mDefaultFelicaRoute);
81 
82   mDefaultEe = NfcConfig::getUnsigned(NAME_DEFAULT_ROUTE, 0x00);
83   DLOG_IF(INFO, nfc_debug_enabled)
84       << StringPrintf("%s: default route is 0x%02X", fn, mDefaultEe);
85 
86   mAidMatchingMode =
87       NfcConfig::getUnsigned(NAME_AID_MATCHING_MODE, AID_MATCHING_EXACT_ONLY);
88 
89   mDefaultSysCodeRoute =
90       NfcConfig::getUnsigned(NAME_DEFAULT_SYS_CODE_ROUTE, 0xC0);
91 
92   mDefaultSysCodePowerstate =
93       NfcConfig::getUnsigned(NAME_DEFAULT_SYS_CODE_PWR_STATE, 0x19);
94 
95   mDefaultSysCode = DEFAULT_SYS_CODE;
96   if (NfcConfig::hasKey(NAME_DEFAULT_SYS_CODE)) {
97     std::vector<uint8_t> pSysCode = NfcConfig::getBytes(NAME_DEFAULT_SYS_CODE);
98     if (pSysCode.size() == 0x02) {
99       mDefaultSysCode = ((pSysCode[0] << 8) | ((int)pSysCode[1] << 0));
100       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
101           "%s: DEFAULT_SYS_CODE: 0x%02X", __func__, mDefaultSysCode);
102     }
103   }
104 
105   mOffHostAidRoutingPowerState =
106       NfcConfig::getUnsigned(NAME_OFFHOST_AID_ROUTE_PWR_STATE, 0x01);
107 
108   mDefaultIsoDepRoute = NfcConfig::getUnsigned(NAME_DEFAULT_ISODEP_ROUTE, 0x0);
109 
110   memset(&mEeInfo, 0, sizeof(mEeInfo));
111   mReceivedEeInfo = false;
112   mSeTechMask = 0x00;
113   mIsScbrSupported = false;
114 
115   mNfcFOnDhHandle = NFA_HANDLE_INVALID;
116 
117   mDeinitializing = false;
118   mEeInfoChanged = false;
119 }
120 
~RoutingManager()121 RoutingManager::~RoutingManager() {}
122 
initialize(nfc_jni_native_data * native)123 bool RoutingManager::initialize(nfc_jni_native_data* native) {
124   static const char fn[] = "RoutingManager::initialize()";
125   mNativeData = native;
126   mRxDataBuffer.clear();
127 
128   {
129     SyncEventGuard guard(mEeRegisterEvent);
130     DLOG_IF(INFO, nfc_debug_enabled) << fn << ": try ee register";
131     tNFA_STATUS nfaStat = NFA_EeRegister(nfaEeCallback);
132     if (nfaStat != NFA_STATUS_OK) {
133       LOG(ERROR) << StringPrintf("%s: fail ee register; error=0x%X", fn,
134                                  nfaStat);
135       return false;
136     }
137     mEeRegisterEvent.wait();
138   }
139 
140   if ((mDefaultOffHostRoute != 0) || (mDefaultFelicaRoute != 0)) {
141     // Wait for EE info if needed
142     SyncEventGuard guard(mEeInfoEvent);
143     if (!mReceivedEeInfo) {
144       LOG(INFO) << fn << "Waiting for EE info";
145       mEeInfoEvent.wait();
146     }
147   }
148   mSeTechMask = updateEeTechRouteSetting();
149 
150   // Tell the host-routing to only listen on Nfc-A
151   tNFA_STATUS nfaStat = NFA_CeSetIsoDepListenTech(NFA_TECHNOLOGY_MASK_A);
152   if (nfaStat != NFA_STATUS_OK)
153     LOG(ERROR) << StringPrintf("Failed to configure CE IsoDep technologies");
154 
155   // Register a wild-card for AIDs routed to the host
156   nfaStat = NFA_CeRegisterAidOnDH(NULL, 0, stackCallback);
157   if (nfaStat != NFA_STATUS_OK)
158     LOG(ERROR) << fn << "Failed to register wildcard AID for DH";
159 
160   updateDefaultRoute();
161   updateDefaultProtocolRoute();
162 
163   return true;
164 }
165 
getInstance()166 RoutingManager& RoutingManager::getInstance() {
167   static RoutingManager manager;
168   return manager;
169 }
170 
enableRoutingToHost()171 void RoutingManager::enableRoutingToHost() {
172   static const char fn[] = "RoutingManager::enableRoutingToHost()";
173   tNFA_STATUS nfaStat;
174   SyncEventGuard guard(mRoutingEvent);
175 
176   // Default routing for T3T protocol
177   if (!mIsScbrSupported && mDefaultEe == NFC_DH_ID) {
178     nfaStat = NFA_EeSetDefaultProtoRouting(NFC_DH_ID, NFA_PROTOCOL_MASK_T3T, 0,
179                                            0, 0, 0, 0);
180     if (nfaStat == NFA_STATUS_OK)
181       mRoutingEvent.wait();
182     else
183       LOG(ERROR) << fn << "Fail to set default proto routing for T3T";
184   }
185 
186   // Default routing for IsoDep protocol
187   tNFA_PROTOCOL_MASK protoMask = NFA_PROTOCOL_MASK_ISO_DEP;
188   if (mDefaultIsoDepRoute == NFC_DH_ID) {
189     nfaStat = NFA_EeSetDefaultProtoRouting(
190         NFC_DH_ID, protoMask, 0, 0, mSecureNfcEnabled ? 0 : protoMask, 0, 0);
191     if (nfaStat == NFA_STATUS_OK)
192       mRoutingEvent.wait();
193     else
194       LOG(ERROR) << fn << "Fail to set default proto routing for IsoDep";
195   }
196 
197   // Route Nfc-A to host if we don't have a SE
198   tNFA_TECHNOLOGY_MASK techMask = NFA_TECHNOLOGY_MASK_A;
199   if ((mSeTechMask & NFA_TECHNOLOGY_MASK_A) == 0) {
200     nfaStat = NFA_EeSetDefaultTechRouting(
201         NFC_DH_ID, techMask, 0, 0, mSecureNfcEnabled ? 0 : techMask,
202         mSecureNfcEnabled ? 0 : techMask, mSecureNfcEnabled ? 0 : techMask);
203     if (nfaStat == NFA_STATUS_OK)
204       mRoutingEvent.wait();
205     else
206       LOG(ERROR) << fn << "Fail to set default tech routing for Nfc-A";
207   }
208 
209   // Route Nfc-F to host if we don't have a SE
210   techMask = NFA_TECHNOLOGY_MASK_F;
211   if ((mSeTechMask & NFA_TECHNOLOGY_MASK_F) == 0) {
212     nfaStat = NFA_EeSetDefaultTechRouting(
213         NFC_DH_ID, techMask, 0, 0, mSecureNfcEnabled ? 0 : techMask,
214         mSecureNfcEnabled ? 0 : techMask, mSecureNfcEnabled ? 0 : techMask);
215     if (nfaStat == NFA_STATUS_OK)
216       mRoutingEvent.wait();
217     else
218       LOG(ERROR) << fn << "Fail to set default tech routing for Nfc-F";
219   }
220 }
221 
disableRoutingToHost()222 void RoutingManager::disableRoutingToHost() {
223   static const char fn[] = "RoutingManager::disableRoutingToHost()";
224   tNFA_STATUS nfaStat;
225   SyncEventGuard guard(mRoutingEvent);
226 
227   // Default routing for IsoDep protocol
228   if (mDefaultIsoDepRoute == NFC_DH_ID) {
229     nfaStat =
230         NFA_EeClearDefaultProtoRouting(NFC_DH_ID, NFA_PROTOCOL_MASK_ISO_DEP);
231     if (nfaStat == NFA_STATUS_OK)
232       mRoutingEvent.wait();
233     else
234       LOG(ERROR) << fn << "Fail to set default proto routing for IsoDep";
235   }
236 
237   // Default routing for Nfc-A technology if we don't have a SE
238   if ((mSeTechMask & NFA_TECHNOLOGY_MASK_A) == 0) {
239     nfaStat = NFA_EeClearDefaultTechRouting(NFC_DH_ID, NFA_TECHNOLOGY_MASK_A);
240     if (nfaStat == NFA_STATUS_OK)
241       mRoutingEvent.wait();
242     else
243       LOG(ERROR) << fn << "Fail to set default tech routing for Nfc-A";
244   }
245 
246   // Default routing for Nfc-F technology if we don't have a SE
247   if ((mSeTechMask & NFA_TECHNOLOGY_MASK_F) == 0) {
248     nfaStat = NFA_EeClearDefaultTechRouting(NFC_DH_ID, NFA_TECHNOLOGY_MASK_F);
249     if (nfaStat == NFA_STATUS_OK)
250       mRoutingEvent.wait();
251     else
252       LOG(ERROR) << fn << "Fail to set default tech routing for Nfc-F";
253   }
254 
255   // Default routing for T3T protocol
256   if (!mIsScbrSupported && mDefaultEe == NFC_DH_ID) {
257     nfaStat = NFA_EeClearDefaultProtoRouting(NFC_DH_ID, NFA_PROTOCOL_MASK_T3T);
258     if (nfaStat == NFA_STATUS_OK)
259       mRoutingEvent.wait();
260     else
261       LOG(ERROR) << fn << "Fail to set default proto routing for T3T";
262   }
263 }
264 
addAidRouting(const uint8_t * aid,uint8_t aidLen,int route,int aidInfo)265 bool RoutingManager::addAidRouting(const uint8_t* aid, uint8_t aidLen,
266                                    int route, int aidInfo) {
267   static const char fn[] = "RoutingManager::addAidRouting";
268   DLOG_IF(INFO, nfc_debug_enabled) << fn << ": enter";
269   uint8_t powerState = 0x01;
270   if (!mSecureNfcEnabled) {
271     powerState = (route != 0x00) ? mOffHostAidRoutingPowerState : 0x11;
272   }
273   SyncEventGuard guard(mRoutingEvent);
274   mAidRoutingConfigured = false;
275   tNFA_STATUS nfaStat =
276       NFA_EeAddAidRouting(route, aidLen, (uint8_t*)aid, powerState, aidInfo);
277   if (nfaStat == NFA_STATUS_OK) {
278     mRoutingEvent.wait();
279   }
280   if (mAidRoutingConfigured) {
281     DLOG_IF(INFO, nfc_debug_enabled) << fn << ": routed AID";
282     return true;
283   } else {
284     LOG(ERROR) << fn << ": failed to route AID";
285     return false;
286   }
287 }
288 
removeAidRouting(const uint8_t * aid,uint8_t aidLen)289 bool RoutingManager::removeAidRouting(const uint8_t* aid, uint8_t aidLen) {
290   static const char fn[] = "RoutingManager::removeAidRouting";
291   DLOG_IF(INFO, nfc_debug_enabled) << fn << ": enter";
292   SyncEventGuard guard(mRoutingEvent);
293   mAidRoutingConfigured = false;
294   tNFA_STATUS nfaStat = NFA_EeRemoveAidRouting(aidLen, (uint8_t*)aid);
295   if (nfaStat == NFA_STATUS_OK) {
296     mRoutingEvent.wait();
297   }
298   if (mAidRoutingConfigured) {
299     DLOG_IF(INFO, nfc_debug_enabled) << fn << ": removed AID";
300     return true;
301   } else {
302     LOG(ERROR) << fn << ": failed to remove AID";
303     return false;
304   }
305 }
306 
commitRouting()307 bool RoutingManager::commitRouting() {
308   static const char fn[] = "RoutingManager::commitRouting";
309   tNFA_STATUS nfaStat = 0;
310   DLOG_IF(INFO, nfc_debug_enabled) << fn;
311   if(mEeInfoChanged) {
312     mSeTechMask = updateEeTechRouteSetting();
313     mEeInfoChanged = false;
314   }
315   {
316     SyncEventGuard guard(mEeUpdateEvent);
317     nfaStat = NFA_EeUpdateNow();
318     if (nfaStat == NFA_STATUS_OK) {
319       mEeUpdateEvent.wait();  // wait for NFA_EE_UPDATED_EVT
320     }
321   }
322   return (nfaStat == NFA_STATUS_OK);
323 }
324 
onNfccShutdown()325 void RoutingManager::onNfccShutdown() {
326   static const char fn[] = "RoutingManager:onNfccShutdown";
327   if (mDefaultOffHostRoute == 0x00 && mDefaultFelicaRoute == 0x00) return;
328 
329   tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
330   uint8_t actualNumEe = MAX_NUM_EE;
331   tNFA_EE_INFO eeInfo[MAX_NUM_EE];
332   mDeinitializing = true;
333 
334   memset(&eeInfo, 0, sizeof(eeInfo));
335   if ((nfaStat = NFA_EeGetInfo(&actualNumEe, eeInfo)) != NFA_STATUS_OK) {
336     LOG(ERROR) << StringPrintf("%s: fail get info; error=0x%X", fn, nfaStat);
337     return;
338   }
339   if (actualNumEe != 0) {
340     for (uint8_t xx = 0; xx < actualNumEe; xx++) {
341       bool bIsOffHostEEPresent =
342           (NFC_GetNCIVersion() < NCI_VERSION_2_0)
343               ? (eeInfo[xx].num_interface != 0)
344               : (eeInfo[xx].ee_interface[0] !=
345                  NCI_NFCEE_INTERFACE_HCI_ACCESS) &&
346                     (eeInfo[xx].ee_status == NFA_EE_STATUS_ACTIVE);
347       if (bIsOffHostEEPresent) {
348         DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
349             "%s: Handle: 0x%04x Change Status Active to Inactive", fn,
350             eeInfo[xx].ee_handle);
351         SyncEventGuard guard(mEeSetModeEvent);
352         if ((nfaStat = NFA_EeModeSet(eeInfo[xx].ee_handle,
353                                      NFA_EE_MD_DEACTIVATE)) == NFA_STATUS_OK) {
354           mEeSetModeEvent.wait();  // wait for NFA_EE_MODE_SET_EVT
355         } else {
356           LOG(ERROR) << fn << "Failed to set EE inactive";
357         }
358       }
359     }
360   } else {
361     DLOG_IF(INFO, nfc_debug_enabled) << fn << ": No active EEs found";
362   }
363 }
364 
notifyActivated(uint8_t technology)365 void RoutingManager::notifyActivated(uint8_t technology) {
366   JNIEnv* e = NULL;
367   ScopedAttach attach(mNativeData->vm, &e);
368   if (e == NULL) {
369     LOG(ERROR) << "jni env is null";
370     return;
371   }
372 
373   e->CallVoidMethod(mNativeData->manager,
374                     android::gCachedNfcManagerNotifyHostEmuActivated,
375                     (int)technology);
376   if (e->ExceptionCheck()) {
377     e->ExceptionClear();
378     LOG(ERROR) << "fail notify";
379   }
380 }
381 
notifyDeactivated(uint8_t technology)382 void RoutingManager::notifyDeactivated(uint8_t technology) {
383   mRxDataBuffer.clear();
384   JNIEnv* e = NULL;
385   ScopedAttach attach(mNativeData->vm, &e);
386   if (e == NULL) {
387     LOG(ERROR) << "jni env is null";
388     return;
389   }
390 
391   e->CallVoidMethod(mNativeData->manager,
392                     android::gCachedNfcManagerNotifyHostEmuDeactivated,
393                     (int)technology);
394   if (e->ExceptionCheck()) {
395     e->ExceptionClear();
396     LOG(ERROR) << StringPrintf("fail notify");
397   }
398 }
399 
handleData(uint8_t technology,const uint8_t * data,uint32_t dataLen,tNFA_STATUS status)400 void RoutingManager::handleData(uint8_t technology, const uint8_t* data,
401                                 uint32_t dataLen, tNFA_STATUS status) {
402   if (status == NFC_STATUS_CONTINUE) {
403     if (dataLen > 0) {
404       mRxDataBuffer.insert(mRxDataBuffer.end(), &data[0],
405                            &data[dataLen]);  // append data; more to come
406     }
407     return;  // expect another NFA_CE_DATA_EVT to come
408   } else if (status == NFA_STATUS_OK) {
409     if (dataLen > 0) {
410       mRxDataBuffer.insert(mRxDataBuffer.end(), &data[0],
411                            &data[dataLen]);  // append data
412     }
413     // entire data packet has been received; no more NFA_CE_DATA_EVT
414   } else if (status == NFA_STATUS_FAILED) {
415     LOG(ERROR) << "RoutingManager::handleData: read data fail";
416     goto TheEnd;
417   }
418 
419   {
420     JNIEnv* e = NULL;
421     ScopedAttach attach(mNativeData->vm, &e);
422     if (e == NULL) {
423       LOG(ERROR) << "jni env is null";
424       goto TheEnd;
425     }
426 
427     ScopedLocalRef<jobject> dataJavaArray(
428         e, e->NewByteArray(mRxDataBuffer.size()));
429     if (dataJavaArray.get() == NULL) {
430       LOG(ERROR) << "fail allocate array";
431       goto TheEnd;
432     }
433 
434     e->SetByteArrayRegion((jbyteArray)dataJavaArray.get(), 0,
435                           mRxDataBuffer.size(), (jbyte*)(&mRxDataBuffer[0]));
436     if (e->ExceptionCheck()) {
437       e->ExceptionClear();
438       LOG(ERROR) << "fail fill array";
439       goto TheEnd;
440     }
441 
442     e->CallVoidMethod(mNativeData->manager,
443                       android::gCachedNfcManagerNotifyHostEmuData,
444                       (int)technology, dataJavaArray.get());
445     if (e->ExceptionCheck()) {
446       e->ExceptionClear();
447       LOG(ERROR) << "fail notify";
448     }
449   }
450 TheEnd:
451   mRxDataBuffer.clear();
452 }
453 
notifyEeUpdated()454 void RoutingManager::notifyEeUpdated() {
455   JNIEnv* e = NULL;
456   ScopedAttach attach(mNativeData->vm, &e);
457   if (e == NULL) {
458     LOG(ERROR) << "jni env is null";
459     return;
460   }
461 
462   e->CallVoidMethod(mNativeData->manager,
463                     android::gCachedNfcManagerNotifyEeUpdated);
464   if (e->ExceptionCheck()) {
465     e->ExceptionClear();
466     LOG(ERROR) << "fail notify";
467   }
468 }
469 
stackCallback(uint8_t event,tNFA_CONN_EVT_DATA * eventData)470 void RoutingManager::stackCallback(uint8_t event,
471                                    tNFA_CONN_EVT_DATA* eventData) {
472   static const char fn[] = "RoutingManager::stackCallback";
473   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: event=0x%X", fn, event);
474   RoutingManager& routingManager = RoutingManager::getInstance();
475 
476   switch (event) {
477     case NFA_CE_REGISTERED_EVT: {
478       tNFA_CE_REGISTERED& ce_registered = eventData->ce_registered;
479       DLOG_IF(INFO, nfc_debug_enabled)
480           << StringPrintf("%s: NFA_CE_REGISTERED_EVT; status=0x%X; h=0x%X", fn,
481                           ce_registered.status, ce_registered.handle);
482     } break;
483 
484     case NFA_CE_DEREGISTERED_EVT: {
485       tNFA_CE_DEREGISTERED& ce_deregistered = eventData->ce_deregistered;
486       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
487           "%s: NFA_CE_DEREGISTERED_EVT; h=0x%X", fn, ce_deregistered.handle);
488     } break;
489 
490     case NFA_CE_ACTIVATED_EVT: {
491       routingManager.notifyActivated(NFA_TECHNOLOGY_MASK_A);
492     } break;
493 
494     case NFA_DEACTIVATED_EVT:
495     case NFA_CE_DEACTIVATED_EVT: {
496       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
497           "%s: NFA_DEACTIVATED_EVT, NFA_CE_DEACTIVATED_EVT", fn);
498       routingManager.notifyDeactivated(NFA_TECHNOLOGY_MASK_A);
499       SyncEventGuard g(gDeactivatedEvent);
500       gActivated = false;  // guard this variable from multi-threaded access
501       gDeactivatedEvent.notifyOne();
502     } break;
503 
504     case NFA_CE_DATA_EVT: {
505       tNFA_CE_DATA& ce_data = eventData->ce_data;
506       DLOG_IF(INFO, nfc_debug_enabled)
507           << StringPrintf("%s: NFA_CE_DATA_EVT; stat=0x%X; h=0x%X; data len=%u",
508                           fn, ce_data.status, ce_data.handle, ce_data.len);
509       getInstance().handleData(NFA_TECHNOLOGY_MASK_A, ce_data.p_data,
510                                ce_data.len, ce_data.status);
511     } break;
512   }
513 }
514 
updateRoutingTable()515 void RoutingManager::updateRoutingTable() {
516   updateEeTechRouteSetting();
517   updateDefaultProtocolRoute();
518   updateDefaultRoute();
519 }
520 
updateDefaultProtocolRoute()521 void RoutingManager::updateDefaultProtocolRoute() {
522   static const char fn[] = "RoutingManager::updateDefaultProtocolRoute";
523 
524   // Default Routing for ISO-DEP
525   tNFA_PROTOCOL_MASK protoMask = NFA_PROTOCOL_MASK_ISO_DEP;
526   tNFA_STATUS nfaStat;
527   if (mDefaultIsoDepRoute != NFC_DH_ID) {
528     nfaStat = NFA_EeClearDefaultProtoRouting(mDefaultIsoDepRoute, protoMask);
529     nfaStat = NFA_EeSetDefaultProtoRouting(
530         mDefaultIsoDepRoute, protoMask, mSecureNfcEnabled ? 0 : protoMask, 0,
531         mSecureNfcEnabled ? 0 : protoMask, mSecureNfcEnabled ? 0 : protoMask,
532         mSecureNfcEnabled ? 0 : protoMask);
533   } else {
534     nfaStat = NFA_EeClearDefaultProtoRouting(NFC_DH_ID, protoMask);
535     nfaStat = NFA_EeSetDefaultProtoRouting(
536         NFC_DH_ID, protoMask, 0, 0, mSecureNfcEnabled ? 0 : protoMask, 0, 0);
537   }
538   if (nfaStat == NFA_STATUS_OK)
539     DLOG_IF(INFO, nfc_debug_enabled)
540         << fn << ": Succeed to register default ISO-DEP route";
541   else
542     LOG(ERROR) << fn << ": failed to register default ISO-DEP route";
543 
544   // Default routing for T3T protocol
545   if (!mIsScbrSupported) {
546     SyncEventGuard guard(mRoutingEvent);
547     tNFA_PROTOCOL_MASK protoMask = NFA_PROTOCOL_MASK_T3T;
548     if (mDefaultEe == NFC_DH_ID) {
549       nfaStat =
550           NFA_EeSetDefaultProtoRouting(NFC_DH_ID, protoMask, 0, 0, 0, 0, 0);
551     } else {
552       nfaStat = NFA_EeClearDefaultProtoRouting(mDefaultEe, protoMask);
553       nfaStat = NFA_EeSetDefaultProtoRouting(
554           mDefaultEe, protoMask, 0, 0, mSecureNfcEnabled ? 0 : protoMask,
555           mSecureNfcEnabled ? 0 : protoMask, mSecureNfcEnabled ? 0 : protoMask);
556     }
557     if (nfaStat == NFA_STATUS_OK)
558       mRoutingEvent.wait();
559     else
560       LOG(ERROR) << fn << "Fail to set default proto routing for T3T";
561   }
562 }
563 
updateDefaultRoute()564 void RoutingManager::updateDefaultRoute() {
565   static const char fn[] = "RoutingManager::updateDefaultRoute";
566   if (NFC_GetNCIVersion() != NCI_VERSION_2_0) return;
567 
568   // Register System Code for routing
569   SyncEventGuard guard(mRoutingEvent);
570   tNFA_STATUS nfaStat = NFA_EeAddSystemCodeRouting(
571       mDefaultSysCode, mDefaultSysCodeRoute,
572       mSecureNfcEnabled ? 0x01 : mDefaultSysCodePowerstate);
573   if (nfaStat == NFA_STATUS_NOT_SUPPORTED) {
574     mIsScbrSupported = false;
575     LOG(ERROR) << fn << ": SCBR not supported";
576   } else if (nfaStat == NFA_STATUS_OK) {
577     mIsScbrSupported = true;
578     mRoutingEvent.wait();
579     DLOG_IF(INFO, nfc_debug_enabled)
580         << fn << ": Succeed to register system code";
581   } else {
582     LOG(ERROR) << fn << ": Fail to register system code";
583   }
584 
585   // Register zero lengthy Aid for default Aid Routing
586   if (mDefaultEe != mDefaultIsoDepRoute) {
587     uint8_t powerState = 0x01;
588     if (!mSecureNfcEnabled)
589       powerState = (mDefaultEe != 0x00) ? mOffHostAidRoutingPowerState : 0x11;
590     nfaStat = NFA_EeAddAidRouting(mDefaultEe, 0, NULL, powerState,
591                                   AID_ROUTE_QUAL_PREFIX);
592     if (nfaStat == NFA_STATUS_OK)
593       DLOG_IF(INFO, nfc_debug_enabled)
594           << fn << ": Succeed to register zero length AID";
595     else
596       LOG(ERROR) << fn << ": failed to register zero length AID";
597   }
598 }
599 
updateEeTechRouteSetting()600 tNFA_TECHNOLOGY_MASK RoutingManager::updateEeTechRouteSetting() {
601   static const char fn[] = "RoutingManager::updateEeTechRouteSetting";
602   tNFA_TECHNOLOGY_MASK allSeTechMask = 0x00;
603 
604   if (mDefaultOffHostRoute == 0 && mDefaultFelicaRoute == 0)
605     return allSeTechMask;
606 
607   DLOG_IF(INFO, nfc_debug_enabled)
608       << fn << ": Number of EE is " << (int)mEeInfo.num_ee;
609 
610   tNFA_STATUS nfaStat;
611   for (uint8_t i = 0; i < mEeInfo.num_ee; i++) {
612     tNFA_HANDLE eeHandle = mEeInfo.ee_disc_info[i].ee_handle;
613     tNFA_TECHNOLOGY_MASK seTechMask = 0;
614 
615     DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
616         "%s   EE[%u] Handle: 0x%04x  techA: 0x%02x  techB: "
617         "0x%02x  techF: 0x%02x  techBprime: 0x%02x",
618         fn, i, eeHandle, mEeInfo.ee_disc_info[i].la_protocol,
619         mEeInfo.ee_disc_info[i].lb_protocol,
620         mEeInfo.ee_disc_info[i].lf_protocol,
621         mEeInfo.ee_disc_info[i].lbp_protocol);
622 
623     if ((mDefaultOffHostRoute != 0) &&
624         (eeHandle == (mDefaultOffHostRoute | NFA_HANDLE_GROUP_EE))) {
625       if (mEeInfo.ee_disc_info[i].la_protocol != 0)
626         seTechMask |= NFA_TECHNOLOGY_MASK_A;
627       if (mEeInfo.ee_disc_info[i].lb_protocol != 0)
628         seTechMask |= NFA_TECHNOLOGY_MASK_B;
629     }
630     if ((mDefaultFelicaRoute != 0) &&
631         (eeHandle == (mDefaultFelicaRoute | NFA_HANDLE_GROUP_EE))) {
632       if (mEeInfo.ee_disc_info[i].lf_protocol != 0)
633         seTechMask |= NFA_TECHNOLOGY_MASK_F;
634     }
635 
636     DLOG_IF(INFO, nfc_debug_enabled)
637         << StringPrintf("%s: seTechMask[%u]=0x%02x", fn, i, seTechMask);
638     if (seTechMask != 0x00) {
639       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
640           "Configuring tech mask 0x%02x on EE 0x%04x", seTechMask, eeHandle);
641 
642       nfaStat = NFA_CeConfigureUiccListenTech(eeHandle, seTechMask);
643       if (nfaStat != NFA_STATUS_OK)
644         LOG(ERROR) << fn << "Failed to configure UICC listen technologies.";
645 
646       // clear previous before setting new power state
647       nfaStat = NFA_EeClearDefaultTechRouting(eeHandle, seTechMask);
648       if (nfaStat != NFA_STATUS_OK)
649         LOG(ERROR) << fn << "Failed to clear EE technology routing.";
650 
651       nfaStat = NFA_EeSetDefaultTechRouting(
652           eeHandle, seTechMask, mSecureNfcEnabled ? 0 : seTechMask, 0,
653           mSecureNfcEnabled ? 0 : seTechMask,
654           mSecureNfcEnabled ? 0 : seTechMask,
655           mSecureNfcEnabled ? 0 : seTechMask);
656       if (nfaStat != NFA_STATUS_OK)
657         LOG(ERROR) << fn << "Failed to configure UICC technology routing.";
658 
659       allSeTechMask |= seTechMask;
660     }
661   }
662 
663   // Clear DH technology route on NFC-A
664   if ((allSeTechMask & NFA_TECHNOLOGY_MASK_A) != 0) {
665     nfaStat = NFA_EeClearDefaultTechRouting(NFC_DH_ID, NFA_TECHNOLOGY_MASK_A);
666     if (nfaStat != NFA_STATUS_OK)
667       LOG(ERROR) << "Failed to clear DH technology routing on NFC-A.";
668   }
669 
670   // Clear DH technology route on NFC-F
671   if ((allSeTechMask & NFA_TECHNOLOGY_MASK_F) != 0) {
672     nfaStat = NFA_EeClearDefaultTechRouting(NFC_DH_ID, NFA_TECHNOLOGY_MASK_F);
673     if (nfaStat != NFA_STATUS_OK)
674       LOG(ERROR) << "Failed to clear DH technology routing on NFC-F.";
675   }
676   return allSeTechMask;
677 }
678 
679 /*******************************************************************************
680 **
681 ** Function:        nfaEeCallback
682 **
683 ** Description:     Receive execution environment-related events from stack.
684 **                  event: Event code.
685 **                  eventData: Event data.
686 **
687 ** Returns:         None
688 **
689 *******************************************************************************/
nfaEeCallback(tNFA_EE_EVT event,tNFA_EE_CBACK_DATA * eventData)690 void RoutingManager::nfaEeCallback(tNFA_EE_EVT event,
691                                    tNFA_EE_CBACK_DATA* eventData) {
692   static const char fn[] = "RoutingManager::nfaEeCallback";
693 
694   RoutingManager& routingManager = RoutingManager::getInstance();
695   if (eventData) routingManager.mCbEventData = *eventData;
696 
697   switch (event) {
698     case NFA_EE_REGISTER_EVT: {
699       SyncEventGuard guard(routingManager.mEeRegisterEvent);
700       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
701           "%s: NFA_EE_REGISTER_EVT; status=%u", fn, eventData->ee_register);
702       routingManager.mEeRegisterEvent.notifyOne();
703     } break;
704 
705     case NFA_EE_DEREGISTER_EVT: {
706       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
707           "%s: NFA_EE_DEREGISTER_EVT; status=0x%X", fn, eventData->status);
708       routingManager.mReceivedEeInfo = false;
709       routingManager.mDeinitializing = false;
710     } break;
711 
712     case NFA_EE_MODE_SET_EVT: {
713       SyncEventGuard guard(routingManager.mEeSetModeEvent);
714       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
715           "%s: NFA_EE_MODE_SET_EVT; status: 0x%04X  handle: 0x%04X  ", fn,
716           eventData->mode_set.status, eventData->mode_set.ee_handle);
717       routingManager.mEeSetModeEvent.notifyOne();
718     } break;
719 
720     case NFA_EE_SET_TECH_CFG_EVT: {
721       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
722           "%s: NFA_EE_SET_TECH_CFG_EVT; status=0x%X", fn, eventData->status);
723       SyncEventGuard guard(routingManager.mRoutingEvent);
724       routingManager.mRoutingEvent.notifyOne();
725     } break;
726 
727     case NFA_EE_CLEAR_TECH_CFG_EVT: {
728       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
729           "%s: NFA_EE_CLEAR_TECH_CFG_EVT; status=0x%X", fn, eventData->status);
730       SyncEventGuard guard(routingManager.mRoutingEvent);
731       routingManager.mRoutingEvent.notifyOne();
732     } break;
733 
734     case NFA_EE_SET_PROTO_CFG_EVT: {
735       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
736           "%s: NFA_EE_SET_PROTO_CFG_EVT; status=0x%X", fn, eventData->status);
737       SyncEventGuard guard(routingManager.mRoutingEvent);
738       routingManager.mRoutingEvent.notifyOne();
739     } break;
740 
741     case NFA_EE_CLEAR_PROTO_CFG_EVT: {
742       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
743           "%s: NFA_EE_CLEAR_PROTO_CFG_EVT; status=0x%X", fn, eventData->status);
744       SyncEventGuard guard(routingManager.mRoutingEvent);
745       routingManager.mRoutingEvent.notifyOne();
746     } break;
747 
748     case NFA_EE_ACTION_EVT: {
749       tNFA_EE_ACTION& action = eventData->action;
750       if (action.trigger == NFC_EE_TRIG_SELECT)
751         DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
752             "%s: NFA_EE_ACTION_EVT; h=0x%X; trigger=select (0x%X)", fn,
753             action.ee_handle, action.trigger);
754       else if (action.trigger == NFC_EE_TRIG_APP_INIT) {
755         tNFC_APP_INIT& app_init = action.param.app_init;
756         DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
757             "%s: NFA_EE_ACTION_EVT; h=0x%X; trigger=app-init "
758             "(0x%X); aid len=%u; data len=%u",
759             fn, action.ee_handle, action.trigger, app_init.len_aid,
760             app_init.len_data);
761       } else if (action.trigger == NFC_EE_TRIG_RF_PROTOCOL)
762         DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
763             "%s: NFA_EE_ACTION_EVT; h=0x%X; trigger=rf protocol (0x%X)", fn,
764             action.ee_handle, action.trigger);
765       else if (action.trigger == NFC_EE_TRIG_RF_TECHNOLOGY)
766         DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
767             "%s: NFA_EE_ACTION_EVT; h=0x%X; trigger=rf tech (0x%X)", fn,
768             action.ee_handle, action.trigger);
769       else
770         DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
771             "%s: NFA_EE_ACTION_EVT; h=0x%X; unknown trigger (0x%X)", fn,
772             action.ee_handle, action.trigger);
773     } break;
774 
775     case NFA_EE_DISCOVER_REQ_EVT: {
776       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
777           "%s: NFA_EE_DISCOVER_REQ_EVT; status=0x%X; num ee=%u", __func__,
778           eventData->discover_req.status, eventData->discover_req.num_ee);
779       SyncEventGuard guard(routingManager.mEeInfoEvent);
780       memcpy(&routingManager.mEeInfo, &eventData->discover_req,
781              sizeof(routingManager.mEeInfo));
782       if (routingManager.mReceivedEeInfo && !routingManager.mDeinitializing) {
783         routingManager.mEeInfoChanged = true;
784         routingManager.notifyEeUpdated();
785       }
786       routingManager.mReceivedEeInfo = true;
787       routingManager.mEeInfoEvent.notifyOne();
788     } break;
789 
790     case NFA_EE_NO_CB_ERR_EVT:
791       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
792           "%s: NFA_EE_NO_CB_ERR_EVT  status=%u", fn, eventData->status);
793       break;
794 
795     case NFA_EE_ADD_AID_EVT: {
796       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
797           "%s: NFA_EE_ADD_AID_EVT  status=%u", fn, eventData->status);
798       SyncEventGuard guard(routingManager.mRoutingEvent);
799       routingManager.mAidRoutingConfigured =
800           (eventData->status == NFA_STATUS_OK);
801       routingManager.mRoutingEvent.notifyOne();
802     } break;
803 
804     case NFA_EE_ADD_SYSCODE_EVT: {
805       SyncEventGuard guard(routingManager.mRoutingEvent);
806       routingManager.mRoutingEvent.notifyOne();
807       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
808           "%s: NFA_EE_ADD_SYSCODE_EVT  status=%u", fn, eventData->status);
809     } break;
810 
811     case NFA_EE_REMOVE_SYSCODE_EVT: {
812       SyncEventGuard guard(routingManager.mRoutingEvent);
813       routingManager.mRoutingEvent.notifyOne();
814       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
815           "%s: NFA_EE_REMOVE_SYSCODE_EVT  status=%u", fn, eventData->status);
816     } break;
817 
818     case NFA_EE_REMOVE_AID_EVT: {
819       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
820           "%s: NFA_EE_REMOVE_AID_EVT  status=%u", fn, eventData->status);
821       SyncEventGuard guard(routingManager.mRoutingEvent);
822       routingManager.mAidRoutingConfigured =
823           (eventData->status == NFA_STATUS_OK);
824       routingManager.mRoutingEvent.notifyOne();
825     } break;
826 
827     case NFA_EE_NEW_EE_EVT: {
828       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
829           "%s: NFA_EE_NEW_EE_EVT  h=0x%X; status=%u", fn,
830           eventData->new_ee.ee_handle, eventData->new_ee.ee_status);
831     } break;
832 
833     case NFA_EE_UPDATED_EVT: {
834       DLOG_IF(INFO, nfc_debug_enabled)
835           << StringPrintf("%s: NFA_EE_UPDATED_EVT", fn);
836       SyncEventGuard guard(routingManager.mEeUpdateEvent);
837       routingManager.mEeUpdateEvent.notifyOne();
838     } break;
839 
840     default:
841       DLOG_IF(INFO, nfc_debug_enabled)
842           << StringPrintf("%s: unknown event=%u ????", fn, event);
843       break;
844   }
845 }
846 
registerT3tIdentifier(uint8_t * t3tId,uint8_t t3tIdLen)847 int RoutingManager::registerT3tIdentifier(uint8_t* t3tId, uint8_t t3tIdLen) {
848   static const char fn[] = "RoutingManager::registerT3tIdentifier";
849 
850   DLOG_IF(INFO, nfc_debug_enabled)
851       << fn << ": Start to register NFC-F system on DH";
852 
853   if (t3tIdLen != (2 + NCI_RF_F_UID_LEN + NCI_T3T_PMM_LEN)) {
854     LOG(ERROR) << fn << ": Invalid length of T3T Identifier";
855     return NFA_HANDLE_INVALID;
856   }
857 
858   mNfcFOnDhHandle = NFA_HANDLE_INVALID;
859 
860   uint16_t systemCode;
861   uint8_t nfcid2[NCI_RF_F_UID_LEN];
862   uint8_t t3tPmm[NCI_T3T_PMM_LEN];
863 
864   systemCode = (((int)t3tId[0] << 8) | ((int)t3tId[1] << 0));
865   memcpy(nfcid2, t3tId + 2, NCI_RF_F_UID_LEN);
866   memcpy(t3tPmm, t3tId + 10, NCI_T3T_PMM_LEN);
867   {
868     SyncEventGuard guard(mRoutingEvent);
869     tNFA_STATUS nfaStat = NFA_CeRegisterFelicaSystemCodeOnDH(
870         systemCode, nfcid2, t3tPmm, nfcFCeCallback);
871     if (nfaStat == NFA_STATUS_OK) {
872       mRoutingEvent.wait();
873     } else {
874       LOG(ERROR) << fn << ": Fail to register NFC-F system on DH";
875       return NFA_HANDLE_INVALID;
876     }
877   }
878   DLOG_IF(INFO, nfc_debug_enabled)
879       << fn << ": Succeed to register NFC-F system on DH";
880 
881   // Register System Code for routing
882   if (mIsScbrSupported) {
883     SyncEventGuard guard(mRoutingEvent);
884     tNFA_STATUS nfaStat = NFA_EeAddSystemCodeRouting(systemCode, NCI_DH_ID,
885                                                      SYS_CODE_PWR_STATE_HOST);
886     if (nfaStat == NFA_STATUS_OK) {
887       mRoutingEvent.wait();
888     }
889     if ((nfaStat != NFA_STATUS_OK) || (mCbEventData.status != NFA_STATUS_OK)) {
890       LOG(ERROR) << StringPrintf("%s: Fail to register system code on DH", fn);
891       return NFA_HANDLE_INVALID;
892     }
893     DLOG_IF(INFO, nfc_debug_enabled)
894         << StringPrintf("%s: Succeed to register system code on DH", fn);
895     // add handle and system code pair to the map
896     mMapScbrHandle.emplace(mNfcFOnDhHandle, systemCode);
897   } else {
898     LOG(ERROR) << StringPrintf("%s: SCBR Not supported", fn);
899   }
900 
901   return mNfcFOnDhHandle;
902 }
903 
deregisterT3tIdentifier(int handle)904 void RoutingManager::deregisterT3tIdentifier(int handle) {
905   static const char fn[] = "RoutingManager::deregisterT3tIdentifier";
906 
907   DLOG_IF(INFO, nfc_debug_enabled)
908       << StringPrintf("%s: Start to deregister NFC-F system on DH", fn);
909   {
910     SyncEventGuard guard(mRoutingEvent);
911     tNFA_STATUS nfaStat = NFA_CeDeregisterFelicaSystemCodeOnDH(handle);
912     if (nfaStat == NFA_STATUS_OK) {
913       mRoutingEvent.wait();
914       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
915           "%s: Succeeded in deregistering NFC-F system on DH", fn);
916     } else {
917       LOG(ERROR) << StringPrintf("%s: Fail to deregister NFC-F system on DH",
918                                  fn);
919     }
920   }
921   if (mIsScbrSupported) {
922     map<int, uint16_t>::iterator it = mMapScbrHandle.find(handle);
923     // find system code for given handle
924     if (it != mMapScbrHandle.end()) {
925       uint16_t systemCode = it->second;
926       mMapScbrHandle.erase(handle);
927       if (systemCode != 0) {
928         SyncEventGuard guard(mRoutingEvent);
929         tNFA_STATUS nfaStat = NFA_EeRemoveSystemCodeRouting(systemCode);
930         if (nfaStat == NFA_STATUS_OK) {
931           mRoutingEvent.wait();
932           DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
933               "%s: Succeeded in deregistering system Code on DH", fn);
934         } else {
935           LOG(ERROR) << StringPrintf("%s: Fail to deregister system Code on DH",
936                                      fn);
937         }
938       }
939     }
940   }
941 }
942 
nfcFCeCallback(uint8_t event,tNFA_CONN_EVT_DATA * eventData)943 void RoutingManager::nfcFCeCallback(uint8_t event,
944                                     tNFA_CONN_EVT_DATA* eventData) {
945   static const char fn[] = "RoutingManager::nfcFCeCallback";
946   RoutingManager& routingManager = RoutingManager::getInstance();
947 
948   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: 0x%x", __func__, event);
949 
950   switch (event) {
951     case NFA_CE_REGISTERED_EVT: {
952       DLOG_IF(INFO, nfc_debug_enabled)
953           << StringPrintf("%s: registerd event notified", fn);
954       routingManager.mNfcFOnDhHandle = eventData->ce_registered.handle;
955       SyncEventGuard guard(routingManager.mRoutingEvent);
956       routingManager.mRoutingEvent.notifyOne();
957     } break;
958     case NFA_CE_DEREGISTERED_EVT: {
959       DLOG_IF(INFO, nfc_debug_enabled)
960           << StringPrintf("%s: deregisterd event notified", fn);
961       SyncEventGuard guard(routingManager.mRoutingEvent);
962       routingManager.mRoutingEvent.notifyOne();
963     } break;
964     case NFA_CE_ACTIVATED_EVT: {
965       DLOG_IF(INFO, nfc_debug_enabled)
966           << StringPrintf("%s: activated event notified", fn);
967       routingManager.notifyActivated(NFA_TECHNOLOGY_MASK_F);
968     } break;
969     case NFA_CE_DEACTIVATED_EVT: {
970       DLOG_IF(INFO, nfc_debug_enabled)
971           << StringPrintf("%s: deactivated event notified", fn);
972       routingManager.notifyDeactivated(NFA_TECHNOLOGY_MASK_F);
973     } break;
974     case NFA_CE_DATA_EVT: {
975       DLOG_IF(INFO, nfc_debug_enabled)
976           << StringPrintf("%s: data event notified", fn);
977       tNFA_CE_DATA& ce_data = eventData->ce_data;
978       routingManager.handleData(NFA_TECHNOLOGY_MASK_F, ce_data.p_data,
979                                 ce_data.len, ce_data.status);
980     } break;
981     default: {
982       DLOG_IF(INFO, nfc_debug_enabled)
983           << StringPrintf("%s: unknown event=%u ????", fn, event);
984     } break;
985   }
986 }
987 
setNfcSecure(bool enable)988 bool RoutingManager::setNfcSecure(bool enable) {
989   mSecureNfcEnabled = enable;
990   DLOG_IF(INFO, true) << "setNfcSecure NfcService " << enable;
991   return true;
992 }
993 
deinitialize()994 void RoutingManager::deinitialize() {
995   onNfccShutdown();
996   NFA_EeDeregister(nfaEeCallback);
997 }
998 
registerJniFunctions(JNIEnv * e)999 int RoutingManager::registerJniFunctions(JNIEnv* e) {
1000   static const char fn[] = "RoutingManager::registerJniFunctions";
1001   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", fn);
1002   return jniRegisterNativeMethods(
1003       e, "com/android/nfc/cardemulation/AidRoutingManager", sMethods,
1004       NELEM(sMethods));
1005 }
1006 
com_android_nfc_cardemulation_doGetDefaultRouteDestination(JNIEnv *)1007 int RoutingManager::com_android_nfc_cardemulation_doGetDefaultRouteDestination(
1008     JNIEnv*) {
1009   return getInstance().mDefaultEe;
1010 }
1011 
1012 int RoutingManager::
com_android_nfc_cardemulation_doGetDefaultOffHostRouteDestination(JNIEnv *)1013     com_android_nfc_cardemulation_doGetDefaultOffHostRouteDestination(JNIEnv*) {
1014   return getInstance().mDefaultOffHostRoute;
1015 }
1016 
1017 jbyteArray
com_android_nfc_cardemulation_doGetOffHostUiccDestination(JNIEnv * e)1018 RoutingManager::com_android_nfc_cardemulation_doGetOffHostUiccDestination(
1019     JNIEnv* e) {
1020   std::vector<uint8_t> uicc = getInstance().mOffHostRouteUicc;
1021   if (uicc.size() == 0) {
1022     return NULL;
1023   }
1024   CHECK(e);
1025   jbyteArray uiccJavaArray = e->NewByteArray(uicc.size());
1026   CHECK(uiccJavaArray);
1027   e->SetByteArrayRegion(uiccJavaArray, 0, uicc.size(), (jbyte*)&uicc[0]);
1028   return uiccJavaArray;
1029 }
1030 
1031 jbyteArray
com_android_nfc_cardemulation_doGetOffHostEseDestination(JNIEnv * e)1032 RoutingManager::com_android_nfc_cardemulation_doGetOffHostEseDestination(
1033     JNIEnv* e) {
1034   std::vector<uint8_t> ese = getInstance().mOffHostRouteEse;
1035   if (ese.size() == 0) {
1036     return NULL;
1037   }
1038   CHECK(e);
1039   jbyteArray eseJavaArray = e->NewByteArray(ese.size());
1040   CHECK(eseJavaArray);
1041   e->SetByteArrayRegion(eseJavaArray, 0, ese.size(), (jbyte*)&ese[0]);
1042   return eseJavaArray;
1043 }
1044 
com_android_nfc_cardemulation_doGetAidMatchingMode(JNIEnv *)1045 int RoutingManager::com_android_nfc_cardemulation_doGetAidMatchingMode(
1046     JNIEnv*) {
1047   return getInstance().mAidMatchingMode;
1048 }
1049 
1050 int RoutingManager::
com_android_nfc_cardemulation_doGetDefaultIsoDepRouteDestination(JNIEnv *)1051     com_android_nfc_cardemulation_doGetDefaultIsoDepRouteDestination(JNIEnv*) {
1052   return getInstance().mDefaultIsoDepRoute;
1053 }
1054