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 package com.android.nfc.cardemulation;
17 
18 import android.os.SystemProperties;
19 import android.util.Log;
20 
21 import androidx.annotation.VisibleForTesting;
22 
23 import com.android.nfc.NfcService;
24 
25 import java.util.Arrays;
26 
27 public class RoutingOptionManager {
28     public final String TAG = "RoutingOptionManager";
29 
30     static final boolean DBG = SystemProperties.getBoolean("persist.nfc.debug_enabled", false);
31 
32     static final int ROUTE_UNKNOWN = -1;
33 
34     int mDefaultRoute;
35     int mDefaultIsoDepRoute;
36     int mDefaultOffHostRoute;
37     final byte[] mOffHostRouteUicc;
38     final byte[] mOffHostRouteEse;
39     final int mAidMatchingSupport;
40 
41     int mOverrideDefaultRoute = ROUTE_UNKNOWN;
42     int mOverrideDefaultIsoDepRoute = ROUTE_UNKNOWN;
43     int mOverrideDefaultOffHostRoute = ROUTE_UNKNOWN;
44 
45     boolean mIsRoutingTableOverrided = false;
46 
47     @VisibleForTesting
doGetDefaultRouteDestination()48     native int doGetDefaultRouteDestination();
49     @VisibleForTesting
doGetDefaultIsoDepRouteDestination()50     native int doGetDefaultIsoDepRouteDestination();
51     @VisibleForTesting
doGetDefaultOffHostRouteDestination()52     native int doGetDefaultOffHostRouteDestination();
53     @VisibleForTesting
doGetOffHostUiccDestination()54     native byte[] doGetOffHostUiccDestination();
55     @VisibleForTesting
doGetOffHostEseDestination()56     native byte[] doGetOffHostEseDestination();
57     @VisibleForTesting
doGetAidMatchingMode()58     native int doGetAidMatchingMode();
59 
getInstance()60     public static RoutingOptionManager getInstance() {
61         return RoutingOptionManager.Singleton.INSTANCE;
62     }
63 
64     private static class Singleton {
65         private static final RoutingOptionManager INSTANCE = new RoutingOptionManager();
66     }
67 
68     @VisibleForTesting
RoutingOptionManager()69     RoutingOptionManager() {
70         mDefaultRoute = doGetDefaultRouteDestination();
71         if (DBG)
72             Log.d(TAG, "mDefaultRoute=0x" + Integer.toHexString(mDefaultRoute));
73         mDefaultIsoDepRoute = doGetDefaultIsoDepRouteDestination();
74         if (DBG)
75             Log.d(TAG, "mDefaultIsoDepRoute=0x" + Integer.toHexString(mDefaultIsoDepRoute));
76         mDefaultOffHostRoute = doGetDefaultOffHostRouteDestination();
77         if (DBG)
78             Log.d(TAG, "mDefaultOffHostRoute=0x" + Integer.toHexString(mDefaultOffHostRoute));
79         mOffHostRouteUicc = doGetOffHostUiccDestination();
80         if (DBG)
81             Log.d(TAG, "mOffHostRouteUicc=" + Arrays.toString(mOffHostRouteUicc));
82         mOffHostRouteEse = doGetOffHostEseDestination();
83         if (DBG)
84             Log.d(TAG, "mOffHostRouteEse=" + Arrays.toString(mOffHostRouteEse));
85         mAidMatchingSupport = doGetAidMatchingMode();
86         if (DBG) Log.d(TAG, "mAidMatchingSupport=0x" + Integer.toHexString(mAidMatchingSupport));
87     }
88 
89 //    public void overrideDefaultRoute(int defaultRoute) {
90 //        mOverrideDefaultRoute = defaultRoute;
91 //        NfcService.getInstance().setIsoDepProtocolRoute(defaultRoute);
92 //    }
93 
overrideDefaultIsoDepRoute(int isoDepRoute)94     public void overrideDefaultIsoDepRoute(int isoDepRoute) {
95         mOverrideDefaultRoute = isoDepRoute;
96         mOverrideDefaultIsoDepRoute = isoDepRoute;
97         NfcService.getInstance().setIsoDepProtocolRoute(isoDepRoute);
98     }
99 
overrideDefaultOffHostRoute(int offHostRoute)100     public void overrideDefaultOffHostRoute(int offHostRoute) {
101         mOverrideDefaultOffHostRoute = offHostRoute;
102         NfcService.getInstance().setTechnologyABRoute(offHostRoute);
103     }
104 
recoverOverridedRoutingTable()105     public void recoverOverridedRoutingTable() {
106         NfcService.getInstance().setIsoDepProtocolRoute(mDefaultIsoDepRoute);
107         NfcService.getInstance().setTechnologyABRoute(mDefaultOffHostRoute);
108         mOverrideDefaultRoute = mOverrideDefaultIsoDepRoute = mOverrideDefaultOffHostRoute
109             = ROUTE_UNKNOWN;
110     }
111 
getOverrideDefaultRoute()112     public int getOverrideDefaultRoute() {
113         return mOverrideDefaultRoute;
114     }
115 
getDefaultRoute()116     public int getDefaultRoute() {
117         return mDefaultRoute;
118     }
119 
getDefaultIsoDepRoute()120     public int getDefaultIsoDepRoute() {
121         return mDefaultIsoDepRoute;
122     }
123 
getDefaultOffHostRoute()124     public int getDefaultOffHostRoute() {
125         return mDefaultOffHostRoute;
126     }
127 
getOffHostRouteUicc()128     public byte[] getOffHostRouteUicc() {
129         return mOffHostRouteUicc;
130     }
131 
getOffHostRouteEse()132     public byte[] getOffHostRouteEse() {
133         return mOffHostRouteEse;
134     }
135 
getAidMatchingSupport()136     public int getAidMatchingSupport() {
137         return mAidMatchingSupport;
138     }
139 
isRoutingTableOverrided()140     public boolean isRoutingTableOverrided() {
141         return mOverrideDefaultIsoDepRoute != ROUTE_UNKNOWN
142             || mOverrideDefaultOffHostRoute != ROUTE_UNKNOWN;
143     }
144 }
145