• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 package android.telephony.mockmodem;
18 
19 import android.telephony.cts.util.TelephonyUtils;
20 import android.util.Log;
21 
22 import androidx.test.platform.app.InstrumentationRegistry;
23 
24 import java.util.HashMap;
25 import java.util.Map;
26 
27 public class MockCentralizedNetworkAgent {
28     private static final String TAG = "MCNA";
29 
30     // The Preferred data phone: slot0 = 0, slot1 = 1.
31     private static int sPreferredDataPhone = -1;
32 
33     // The shell command to get the NetworkAgent information.
34     private static String sQueryTelephonyDebugServiceCommand =
35             "dumpsys activity service com.android.phone.TelephonyDebugService";
36 
37     private static IRadioDataImpl[] sIRadioDataImpls;
38 
39     private static Map<Integer, String> sDataCalls = new HashMap<>();
40 
41     private static String sImsPhone0 = new String();
42     private static String sImsPhone1 = new String();
43     private static String sInternetInfo = new String();
44 
getPreferredDataPhone()45     public static int getPreferredDataPhone() {
46         Log.e(TAG, "getPreferredDataPhone(): enter");
47         return sPreferredDataPhone;
48     }
49 
setPreferredDataPhone(int phoneId)50     public static void setPreferredDataPhone(int phoneId) {
51         Log.e(TAG, "setPreferredDataPhone(): enter");
52         Log.d(TAG, "setPreferredDataPhone: " + phoneId);
53         sPreferredDataPhone =  phoneId;
54         resetCapability();
55         setInternetToPreferredDataPhone();
56     }
57 
setNetworkAgentInfo( IRadioDataImpl[] iRadioDataImpls, int numOfPhone)58     public static void setNetworkAgentInfo(
59             IRadioDataImpl[] iRadioDataImpls, int numOfPhone) throws Exception {
60         Log.e(TAG, "setNetworkAgentInfo(): enter");
61         sIRadioDataImpls = iRadioDataImpls;
62         String result =
63                 TelephonyUtils.executeShellCommand(
64                         InstrumentationRegistry.getInstrumentation(),
65                         sQueryTelephonyDebugServiceCommand);
66         if (result.contains("Use -h for help")) {
67             throw new RuntimeException(result);
68         }
69 
70         for (int numSim = 0; numSim < numOfPhone; numSim++) {
71             String targetString = "DataNetworkController-" + Integer.toString(numSim);
72             if (!result.contains(targetString)) {
73                 Log.e(TAG, "targetString '" + targetString + "' not found in result. Skipping.");
74                 continue;
75             }
76             String[] splitResult = result.split(targetString);
77             if (splitResult.length <= 1) {
78                 Log.e(TAG, "Split result for '" + targetString + "' has length <= 1. Skipping.");
79                 continue;
80             }
81             String tmpString = targetString
82                     + splitResult[1].split("Pending tear down data networks:")[0];
83             sDataCalls.put(numSim, tmpString);
84             iRadioDataImpls[numSim]
85                 .getMockDataServiceInstance().setBridgeTheDataConnection(tmpString);
86             MockCentralizedNetworkAgent.storeDataCall(numSim, tmpString);
87             // Single SIM case
88             if (numOfPhone == 1) {
89                 sPreferredDataPhone = numSim;
90                 resetCapability();
91                 setInternetToPreferredDataPhone();
92             }
93         }
94     }
95 
96 
storeDataCall(int phoneId, String string)97     public static synchronized void storeDataCall(int phoneId, String string) {
98         Log.e(TAG, "storeDataCall(): enter");
99         String patternDataNetworkController = "DataNetworkController-" + phoneId;
100         String patternAllTtelephonyNetworkRequests = "All telephony network requests:";
101         String patternCurrentState = "curState=ConnectedState";
102         try {
103             String[] lines = new String[] {};
104             String line =
105                     string.split(patternDataNetworkController)[1]
106                             .split(patternAllTtelephonyNetworkRequests)[0];
107             if (line.contains(patternCurrentState)) {
108                 lines = line.split((patternCurrentState));
109             }
110             for (String str : lines) {
111                 String capabilities = getCapabilities(str);
112                 if (capabilities.contains("INTERNET")) {
113                     sInternetInfo =
114                         patternCurrentState
115                         + " " + str
116                         + " " + patternAllTtelephonyNetworkRequests;
117                     Log.d(TAG, "sInternetInfo: " + sInternetInfo);
118                 } else if (capabilities.contains("IMS")) {
119                     if (phoneId == 0) {
120                         sImsPhone0 =
121                             patternDataNetworkController
122                             + " " + patternCurrentState
123                             + " " + str
124                             + " " + patternAllTtelephonyNetworkRequests;
125                     }
126                     if (phoneId == 1) {
127                         sImsPhone1 =
128                             patternDataNetworkController
129                             + " " + patternCurrentState
130                             + " " + str
131                             + " " + patternAllTtelephonyNetworkRequests;
132                     }
133                 }
134             }
135         } catch (Exception e) {
136             Log.e(TAG, "Exception error: [No NetworkAgentInfo]" + e);
137         }
138     }
139 
getCapabilities(String string)140     private static String getCapabilities(String string) {
141         Log.e(TAG, "getCapabilities(): enter");
142         String capabilities = "";
143         try {
144             capabilities = string.trim().split("Capabilities:")[1].split("LinkUpBandwidth")[0];
145             Log.d(TAG, "getCapabilities: " + capabilities);
146         } catch (Exception e) {
147             Log.e(TAG, "getCapabilities(): Exception error: " + e);
148         }
149         return capabilities;
150     }
151 
resetCapability()152     private static void resetCapability() {
153         Log.e(TAG, "resetCapability(): enter");
154         for (IRadioDataImpl iRadioData : sIRadioDataImpls) {
155             iRadioData.getMockDataServiceInstance().resetCapability();
156         }
157     }
158 
setInternetToPreferredDataPhone()159     private static void setInternetToPreferredDataPhone() {
160         Log.e(TAG, "setInternetToPreferredDataPhone(): enter");
161         String DataNetworkControllerPattern = "DataNetworkController-" + sPreferredDataPhone;
162         // ims
163         for (int i = 0; i < sIRadioDataImpls.length; i++) {
164             sIRadioDataImpls[i]
165                 .getMockDataServiceInstance()
166                 .setBridgeTheDataConnection(i == 0 ? sImsPhone0 : sImsPhone1);
167         }
168         // internet
169         String tmp = DataNetworkControllerPattern + " " + sInternetInfo;
170         sIRadioDataImpls[sPreferredDataPhone]
171             .getMockDataServiceInstance()
172             .setBridgeTheDataConnection(tmp);
173     }
174 }
175