1 /*
2  * Copyright (C) 2010 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.device;
18 
19 import com.android.ddmlib.AndroidDebugBridge;
20 import com.android.tradefed.command.remote.DeviceDescriptor;
21 import com.android.tradefed.util.CommandResult;
22 import com.android.tradefed.util.IRunUtil;
23 
24 import java.io.PrintWriter;
25 import java.util.List;
26 import java.util.concurrent.TimeUnit;
27 
28 /**
29  * Interface for managing the set of available devices for testing.
30  */
31 public interface IDeviceManager {
32     /**
33      * A listener for fastboot state changes.
34      */
35     public static interface IFastbootListener {
36         /**
37          * Callback when fastboot state has been updated for all devices.
38          */
stateUpdated()39         public void stateUpdated();
40     }
41 
42     /**
43      * Initialize the device manager. This must be called once and only once before any other
44      * methods are called.
45      */
init()46     public void init();
47 
48     /**
49      * Initialize the device manager with a device filter. This filter can be used to instruct
50      * the DeviceManager to ignore certain connected devices.
51      *
52      * @param globalDeviceFilter the device filter
53      */
init(IDeviceSelection globalDeviceFilter, List<IDeviceMonitor> deviceMonitors)54     public void init(IDeviceSelection globalDeviceFilter, List<IDeviceMonitor> deviceMonitors);
55 
56     /**
57      * Request a physical device for testing
58      *
59      * @return a {@link ITestDevice} for testing, or <code>null</code> if one is not available
60      */
allocateDevice()61     public ITestDevice allocateDevice();
62 
63     /**
64      * Request a device for testing that meets certain criteria.
65      *
66      * @param options the {@link IDeviceSelection} the device should meet.
67      * @return a {@link ITestDevice} for testing, or <code>null</code> if one
68      *         is not available
69      */
allocateDevice(IDeviceSelection options)70     public ITestDevice allocateDevice(IDeviceSelection options);
71 
72     /**
73      * Request a device for testing that meets certain criteria.
74      *
75      * @param options the {@link IDeviceSelection} the device should meet.
76      * @param isTemporary whether or not a temporary NullDevice should be created.
77      * @return a {@link ITestDevice} for testing, or <code>null</code> if one is not available
78      */
allocateDevice(IDeviceSelection options, boolean isTemporary)79     public ITestDevice allocateDevice(IDeviceSelection options, boolean isTemporary);
80 
81     /**
82      * Rudely allocate a device, even if its not currently available.
83      * <p/>
84      * Will have no effect if device is already allocated.
85      *
86      * @param serial the device serial to allocate
87      * @return the {@link ITestDevice}, or <code>null</code> if it could not be allocated
88      */
forceAllocateDevice(String serial)89     public ITestDevice forceAllocateDevice(String serial);
90 
91     /**
92      * Return a device to the pool
93      * <p/>
94      * Attempts to return a device that hasn't been previously allocated will be ignored.
95      *
96      * @param device the {@link ITestDevice} to free
97      * @param state the {@link com.android.tradefed.device.FreeDeviceState}. Used to control if
98      *        device is returned to available device pool.
99      */
freeDevice(ITestDevice device, FreeDeviceState state)100     public void freeDevice(ITestDevice device, FreeDeviceState state);
101 
102     /**
103      * A helper method to execute shell command on available device.
104      *
105      * @param serial The device serial.
106      * @param command The shell command.
107      * @param timeout The amount of time for the command to complete.
108      * @param timeUnit The unit for the timeout.
109      * @return A {@link CommandResult}.
110      */
executeCmdOnAvailableDevice( String serial, String command, long timeout, TimeUnit timeUnit)111     public CommandResult executeCmdOnAvailableDevice(
112             String serial, String command, long timeout, TimeUnit timeUnit);
113 
114     /**
115      * Helper method to launch emulator.
116      * <p/>
117      * Will launch the emulator as specified by the caller
118      *
119      * @param device the placeholder {@link ITestDevice} representing allocated emulator device
120      * @param bootTimeout the time in ms to wait for the emulator to boot
121      * @param runUtil
122      * @param emulatorArgs command line arguments to launch the emulator
123      * @throws DeviceNotAvailableException if emulator fails to boot or come online
124      */
launchEmulator(ITestDevice device, long bootTimeout, IRunUtil runUtil, List<String> emulatorArgs)125     void launchEmulator(ITestDevice device, long bootTimeout, IRunUtil runUtil,
126             List<String> emulatorArgs) throws DeviceNotAvailableException;
127 
128     /**
129      * Shut down the given emulator.
130      * <p/>
131      * Blocks until emulator disappears from adb. Will have no effect if emulator is already not
132      * available.
133      *
134      * @param device the {@link ITestDevice} representing emulator to shut down
135      * @throws DeviceNotAvailableException if emulator fails to shut down
136      */
killEmulator(ITestDevice device)137     public void killEmulator(ITestDevice device) throws DeviceNotAvailableException;
138 
139     /**
140      * Connect to a device with adb-over-tcp
141      * <p/>
142      * This method allocates a new device, which should eventually be freed via
143      * {@link #disconnectFromTcpDevice(ITestDevice)}
144      * <p/>
145      * The returned {@link ITestDevice} will be online, but may not be responsive.
146      * <p/>
147      * Note that performing action such as a reboot on a tcp connected device, will sever the
148      * tcp connection to the device, and result in a {@link DeviceNotAvailableException}
149      *
150      * @param ipAndPort the original ip address and port of the device to connect to
151      * @return the {@link ITestDevice} or <code>null</code> if a tcp connection could not be formed
152      */
connectToTcpDevice(String ipAndPort)153     public ITestDevice connectToTcpDevice(String ipAndPort);
154 
155     /**
156      * Disconnect from an adb-over-tcp connected device.
157      * <p/>
158      * Switches the device back to usb mode, and frees it.
159      *
160      * @param tcpDevice the device currently in tcp mode, previously allocated via
161      *            {@link #connectToTcpDevice(String)}
162      * @return <code>true</code> if switch to usb mode was successful
163      */
disconnectFromTcpDevice(ITestDevice tcpDevice)164     public boolean disconnectFromTcpDevice(ITestDevice tcpDevice);
165 
166     /**
167      * A helper method that switches the given usb device to adb-over-tcp mode, and then connects to
168      * it via {@link #connectToTcpDevice(String)}.
169      *
170      * @param usbDevice the device currently in usb mode
171      * @return the newly allocated {@link ITestDevice} in tcp mode or <code>null</code> if a tcp
172      *         connection could not be formed
173      * @throws DeviceNotAvailableException if the connection with <var>usbDevice</var> was lost and
174      *         could not be recovered
175      */
reconnectDeviceToTcp(ITestDevice usbDevice)176     public ITestDevice reconnectDeviceToTcp(ITestDevice usbDevice)
177             throws DeviceNotAvailableException;
178 
179     /**
180      * Stops device monitoring services, and terminates the ddm library.
181      * <p/>
182      * This must be called upon application termination.
183      *
184      * @see AndroidDebugBridge#terminate()
185      */
terminate()186     public void terminate();
187 
188     /** Stops the device recovery thread. */
terminateDeviceRecovery()189     public void terminateDeviceRecovery();
190 
191     /** Stop the Device Monitors. */
terminateDeviceMonitor()192     public void terminateDeviceMonitor();
193 
194     /** Like {@link #terminate()}, but attempts to forcefully shut down adb as well. */
terminateHard()195     public void terminateHard();
196 
197     /**
198      * Like {@link #terminateHard()}.
199      *
200      * @param reason optional reason given for the termination.
201      */
terminateHard(String reason)202     public default void terminateHard(String reason) {
203         terminateHard();
204     }
205 
206     /** Stop adb bridge and services depend on adb connections. */
stopAdbBridge()207     public void stopAdbBridge();
208 
209     /**
210      * Restart (if {@link #stopAdbBridge()} was called) adb bridge and services depend on adb
211      * connections.
212      */
restartAdbBridge()213     public void restartAdbBridge();
214 
215     /**
216      * Returns a list of DeviceDescriptors for all known devices
217      *
218      * @return a list of {@link DeviceDescriptor} for all known devices
219      */
listAllDevices()220     public List<DeviceDescriptor> listAllDevices();
221 
222     /**
223      * Returns a list of DeviceDescriptors for all known devices
224      *
225      * @param shortDescriptor whether to limit descriptors to minimum info
226      * @return a list of {@link DeviceDescriptor} for all known devices
227      */
listAllDevices(boolean shortDescriptor)228     public List<DeviceDescriptor> listAllDevices(boolean shortDescriptor);
229 
230     /**
231      * Returns the DeviceDescriptor with the given serial.
232      *
233      * @param serial serial number for the device to get
234      * @return the {@link DeviceDescriptor} for the selected device, or null if the serial does not
235      *     match a known device.
236      */
getDeviceDescriptor(String serial)237     public DeviceDescriptor getDeviceDescriptor(String serial);
238 
239     /**
240      * Output a user-friendly description containing list of known devices, their state, and values
241      * for commonly used {@link IDeviceSelection} options.
242      *
243      * @param printWriter the {@link PrintWriter} to output the description to
244      * @param includeStub Whether or not to display stub devices too.
245      */
displayDevicesInfo(PrintWriter printWriter, boolean includeStub)246     public void displayDevicesInfo(PrintWriter printWriter, boolean includeStub);
247 
248     /**
249      * Informs the manager that a listener is interested in fastboot state changes.
250      * <p/>
251      * Currently a {@link IDeviceManager} will only monitor devices in fastboot if there are one or
252      * more active listeners.
253      * <p/>
254      * TODO: this is a bit of a hack - find a better solution
255      *
256      * @param listener
257      */
addFastbootListener(IFastbootListener listener)258     public void addFastbootListener(IFastbootListener listener);
259 
260     /**
261      * Informs the manager that a listener is no longer interested in fastboot state changes.
262      * @param listener
263      */
removeFastbootListener(IFastbootListener listener)264     public void removeFastbootListener(IFastbootListener listener);
265 
266     /**
267      * Determine if given serial represents a null device
268      */
isNullDevice(String serial)269     public boolean isNullDevice(String serial);
270 
271     /**
272      * Determine if given serial represents a emulator
273      */
isEmulator(String serial)274     public boolean isEmulator(String serial);
275 
276     /**
277      * Adds a {@link IDeviceMonitor}
278      */
addDeviceMonitor(IDeviceMonitor mon)279     public void addDeviceMonitor(IDeviceMonitor mon);
280 
281     /**
282      * Removes a previously added {@link IDeviceMonitor}. Has no effect if mon has not been added.
283      */
removeDeviceMonitor(IDeviceMonitor mon)284     public void removeDeviceMonitor(IDeviceMonitor mon);
285 
286     /** Returns the path to the adb binary to use. */
getAdbPath()287     public String getAdbPath();
288 
289     /** Returns the path to the fastboot binary to use. */
getFastbootPath()290     public String getFastbootPath();
291 
292     /**
293      * Wait until a first physical device is connected. If a device was connected before, it
294      * returns directly True. If no device was added, it returns false after timeout.
295      *
296      * @param timeout time to wait in millisecond before returning false.
297      */
waitForFirstDeviceAdded(long timeout)298     public boolean waitForFirstDeviceAdded(long timeout);
299 
300     /** Get the adb version currently in use by the device manager. */
getAdbVersion()301     public String getAdbVersion();
302 
303     /**
304      * Add a device to fastboot monitor. The fastboot monitor will use 'fastboot_serial' to
305      * communicate with the device.
306      *
307      * @param serial the device's serial number.
308      * @param fastboot_serial the device's fastboot mode serial number.
309      */
addMonitoringTcpFastbootDevice(String serial, String fastboot_serial)310     public void addMonitoringTcpFastbootDevice(String serial, String fastboot_serial);
311 
312     /**
313      * Returns whether or not we should check in {@link NativeDeviceStateMonitor} the file system is
314      * mounted properly.
315      */
isFileSystemMountCheckEnabled()316     public default boolean isFileSystemMountCheckEnabled() {
317         return false;
318     }
319 }
320