1 /*
2  * Copyright (C) 2018 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 package com.android.tradefed.module;
17 
18 import com.android.tradefed.invoker.IInvocationContext;
19 import com.android.tradefed.config.Option;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.device.ITestDevice;
22 import com.android.tradefed.log.LogUtil.CLog;
23 import com.android.tradefed.testtype.suite.module.IModuleController;
24 import com.android.tradefed.testtype.suite.module.BaseModuleController;
25 
26 /**
27  * Controller to skip the HAL adapter test if the targeting HAL is not available.
28  * Only used for single-device testing or the primary device in multi-device
29  * testing.
30  */
31 public class VtsHalAdapterModuleController extends BaseModuleController {
32     // Command to list the registered instance for the given hal@version.
33     static final String LIST_HAL_CMD =
34             "lshal -ti --neat 2>/dev/null | grep -e '^hwbinder' | awk '{print $2}' | grep %s";
35 
36     @Option(name = "hal-package-name",
37             description =
38                     "The package name of a target HAL required, e.g., android.hardware.foo@1.0")
39     private String mPackageName = null;
40 
41     /**
42      * {@inheritDoc}
43      */
44     @Override
shouldRun(IInvocationContext context)45     public RunStrategy shouldRun(IInvocationContext context) {
46         ITestDevice device = context.getDevices().get(0);
47         try {
48             String out = device.executeShellCommand(String.format(LIST_HAL_CMD, mPackageName));
49             if (out.isEmpty()) {
50                 CLog.w("The specific HAL service is not running. Skip test module.");
51                 return RunStrategy.SKIP_MODULE_TESTCASES;
52             }
53             return RunStrategy.RUN;
54         } catch (DeviceNotAvailableException e) {
55             CLog.e("Failed to list HAL service.");
56             return RunStrategy.RUN;
57         }
58     }
59 }
60