1 /* 2 * Copyright (C) 2019 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 com.android.tradefed.targetprep; 18 19 import com.android.tradefed.build.IBuildInfo; 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 import com.android.tradefed.device.ITestDevice; 22 import com.android.tradefed.log.LogUtil; 23 24 /** 25 * Preparer class for mSIM tests. 26 * 27 * <p>For devices with an mSIM capable modem, this preparer configures the modem to function in mSIM 28 * mode so that all slots can be tested, and then reverts the modem back in teardown. 29 */ 30 public class MultiSimPreparer extends BaseTargetPreparer implements ITargetCleaner { 31 private ITestDevice mDevice; 32 private int mOriginalSims; 33 34 // Wait up to 60 seconds for the device to turn off 35 private static final int SHUTDOWN_TIMEOUT_MS = 60000; 36 37 @Override setUp(ITestDevice device, IBuildInfo buildInfo)38 public void setUp(ITestDevice device, IBuildInfo buildInfo) throws DeviceNotAvailableException { 39 mDevice = device; 40 mOriginalSims = getNumSims(); 41 int maxSims = getMaxPhones(); 42 if (mOriginalSims != maxSims) { 43 if (!setSimCount(getMaxPhones())) { 44 LogUtil.CLog.w("Cannot set sim count before test"); 45 } 46 } 47 } 48 49 @Override tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable throwable)50 public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable throwable) 51 throws DeviceNotAvailableException { 52 if (mOriginalSims != getNumSims()) { 53 if (!setSimCount(mOriginalSims)) { 54 LogUtil.CLog.w("Cannot reset sim count after test"); 55 } 56 } 57 } 58 getMaxPhones()59 private int getMaxPhones() throws DeviceNotAvailableException { 60 try { 61 return Integer.parseInt(executeCmd("telecom get-max-phones")); 62 } catch (NumberFormatException e) { 63 LogUtil.CLog.w("Cannot get max phones. Assuming 1."); 64 return 1; 65 } 66 } 67 getNumSims()68 private int getNumSims() throws DeviceNotAvailableException { 69 String config = executeCmd("telecom get-sim-config"); 70 if ("SSSS".equalsIgnoreCase(config) || config.isEmpty()) { 71 return 1; 72 } else if ("DSDS".equalsIgnoreCase(config)) { 73 return 2; 74 } else if ("TSTS".equalsIgnoreCase(config)) { 75 return 3; 76 } 77 LogUtil.CLog.w("Could not get SIM config, assuming 1 sim"); 78 return 1; 79 } 80 81 // returns true if succeeded, false if failed setSimCount(int sims)82 private boolean setSimCount(int sims) throws DeviceNotAvailableException { 83 if (getNumSims() == sims) { 84 LogUtil.CLog.d("SIM count already correct."); 85 return true; 86 } 87 // reboot and wait for device ready 88 executeCmd("telecom set-sim-count " + sims); 89 executeCmd("reboot"); 90 mDevice.waitForDeviceNotAvailable(SHUTDOWN_TIMEOUT_MS); 91 mDevice.waitForDeviceAvailable(); 92 return getNumSims() == sims; 93 } 94 executeCmd(String cmd)95 private String executeCmd(String cmd) throws DeviceNotAvailableException { 96 return mDevice.executeShellCommand(cmd).trim(); 97 } 98 } 99