Home | History | Annotate | Download | only in companion
      1 /*
      2  * Copyright (C) 2014 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 
     18 package com.android.tradefed.targetprep.companion;
     19 
     20 import com.android.tradefed.config.GlobalConfiguration;
     21 import com.android.tradefed.device.DeviceSelectionOptions;
     22 import com.android.tradefed.device.FreeDeviceState;
     23 import com.android.tradefed.device.IDeviceManager;
     24 import com.android.tradefed.device.ITestDevice;
     25 import com.android.tradefed.device.TestDeviceState;
     26 import com.android.tradefed.log.LogUtil.CLog;
     27 
     28 import java.util.HashMap;
     29 import java.util.Map;
     30 
     31 /**
     32  * A class for allocating and freeing companion devices
     33  */
     34 public class CompanionDeviceTracker {
     35 
     36     private static CompanionDeviceTracker sInst = null;
     37 
     38     private Map<ITestDevice, ITestDevice> mDeviceMapping = null;
     39 
     40     private CompanionDeviceTracker() {
     41         mDeviceMapping = new HashMap<ITestDevice, ITestDevice>();
     42     }
     43 
     44     /**
     45      * Retrieves singleton instance of the tracker
     46      */
     47     public static CompanionDeviceTracker getInstance() {
     48         if (sInst == null) {
     49             sInst = new CompanionDeviceTracker();
     50         }
     51         return sInst;
     52     }
     53 
     54     /**
     55      * Allocate a companion device based on selection criteria.
     56      *
     57      * @param device the primary device. used to identify the companion device
     58      * @param opt selection criteria
     59      * @return the device allocated or <code>null</code> if none available
     60      */
     61     public ITestDevice allocateCompanionDevice(ITestDevice device, DeviceSelectionOptions opt) {
     62         ITestDevice companion = getDeviceManager().allocateDevice(opt);
     63         if (companion != null) {
     64             if (mDeviceMapping.containsKey(device)) {
     65                 CLog.w("device %s already has an allocated companion %s",
     66                         device.getSerialNumber(), mDeviceMapping.get(companion).getSerialNumber());
     67             }
     68             CLog.i("allocated companion device %s for primary device %s",
     69                     companion.getSerialNumber(), device.getSerialNumber());
     70             mDeviceMapping.put(device, companion);
     71         }
     72         return companion;
     73     }
     74 
     75     /**
     76      * Free the companion device as identified by the primary device
     77      * @param device the primary device whose corresponding companion device should be freed
     78      * @throws IllegalStateException if no companion devices
     79      */
     80     public void freeCompanionDevice(ITestDevice device) {
     81         if (!mDeviceMapping.containsKey(device)) {
     82             CLog.w("primary device %s has no tracked companion device", device.getSerialNumber());
     83             return;
     84         }
     85         ITestDevice companion = mDeviceMapping.remove(device);
     86         FreeDeviceState deviceState = FreeDeviceState.AVAILABLE;
     87         if (!TestDeviceState.ONLINE.equals(companion.getDeviceState())) {
     88             //If the device is offline at the end of the test
     89             deviceState = FreeDeviceState.UNAVAILABLE;
     90         }
     91         getDeviceManager().freeDevice(companion, deviceState);
     92         CLog.i("freed companion device %s for primary device %s",
     93                 companion.getSerialNumber(), device.getSerialNumber());
     94     }
     95 
     96     /**
     97      * Retrieve the allocated companion device as identified by the primary device
     98      * @param device the primary device that the companion device is allocated with
     99      * @return the companion device or <code>null</code> if not found
    100      */
    101     public ITestDevice getCompanionDevice(ITestDevice device) {
    102         return mDeviceMapping.get(device);
    103     }
    104 
    105     private IDeviceManager getDeviceManager() {
    106         return GlobalConfiguration.getDeviceManagerInstance();
    107     }
    108 
    109 }
    110