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 67 for (int numSim = 0; numSim < numOfPhone; numSim++) { 68 String targetString = "DataNetworkController-" + Integer.toString(numSim); 69 String tmpString = targetString 70 + result.split(targetString)[1].split("Pending tear down data networks:")[0]; 71 sDataCalls.put(numSim, tmpString); 72 iRadioDataImpls[numSim] 73 .getMockDataServiceInstance().setBridgeTheDataConnection(tmpString); 74 MockCentralizedNetworkAgent.storeDataCall(numSim, tmpString); 75 // Single SIM case 76 if (numOfPhone == 1) { 77 sPreferredDataPhone = numSim; 78 resetCapability(); 79 setInternetToPreferredDataPhone(); 80 } 81 } 82 } 83 84 storeDataCall(int phoneId, String string)85 public static synchronized void storeDataCall(int phoneId, String string) { 86 Log.e(TAG, "storeDataCall(): enter"); 87 String patternDataNetworkController = "DataNetworkController-" + phoneId; 88 String patternAllTtelephonyNetworkRequests = "All telephony network requests:"; 89 String patternCurrentState = "curState=ConnectedState"; 90 try { 91 String[] lines = new String[] {}; 92 String line = 93 string.split(patternDataNetworkController)[1] 94 .split(patternAllTtelephonyNetworkRequests)[0]; 95 if (line.contains(patternCurrentState)) { 96 lines = line.split((patternCurrentState)); 97 } 98 for (String str : lines) { 99 String capabilities = getCapabilities(str); 100 if (capabilities.contains("INTERNET")) { 101 sInternetInfo = 102 patternCurrentState 103 + " " + str 104 + " " + patternAllTtelephonyNetworkRequests; 105 Log.d(TAG, "sInternetInfo: " + sInternetInfo); 106 } else if (capabilities.contains("IMS")) { 107 if (phoneId == 0) { 108 sImsPhone0 = 109 patternDataNetworkController 110 + " " + patternCurrentState 111 + " " + str 112 + " " + patternAllTtelephonyNetworkRequests; 113 } 114 if (phoneId == 1) { 115 sImsPhone1 = 116 patternDataNetworkController 117 + " " + patternCurrentState 118 + " " + str 119 + " " + patternAllTtelephonyNetworkRequests; 120 } 121 } 122 } 123 } catch (Exception e) { 124 Log.e(TAG, "Exception error: [No NetworkAgentInfo]" + e); 125 } 126 } 127 getCapabilities(String string)128 private static String getCapabilities(String string) { 129 Log.e(TAG, "getCapabilities(): enter"); 130 String capabilities = ""; 131 try { 132 capabilities = string.trim().split("Capabilities:")[1].split("LinkUpBandwidth")[0]; 133 Log.d(TAG, "getCapabilities: " + capabilities); 134 } catch (Exception e) { 135 Log.e(TAG, "getCapabilities(): Exception error: " + e); 136 } 137 return capabilities; 138 } 139 resetCapability()140 private static void resetCapability() { 141 Log.e(TAG, "resetCapability(): enter"); 142 for (IRadioDataImpl iRadioData : sIRadioDataImpls) { 143 iRadioData.getMockDataServiceInstance().resetCapability(); 144 } 145 } 146 setInternetToPreferredDataPhone()147 private static void setInternetToPreferredDataPhone() { 148 Log.e(TAG, "setInternetToPreferredDataPhone(): enter"); 149 String DataNetworkControllerPattern = "DataNetworkController-" + sPreferredDataPhone; 150 // ims 151 for (int i = 0; i < sIRadioDataImpls.length; i++) { 152 sIRadioDataImpls[i] 153 .getMockDataServiceInstance() 154 .setBridgeTheDataConnection(i == 0 ? sImsPhone0 : sImsPhone1); 155 } 156 // internet 157 String tmp = DataNetworkControllerPattern + " " + sInternetInfo; 158 sIRadioDataImpls[sPreferredDataPhone] 159 .getMockDataServiceInstance() 160 .setBridgeTheDataConnection(tmp); 161 } 162 } 163