1 /*
2  * Copyright (C) 2020 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  * Utils file for HIDL related VTS code.
19  */
20 #pragma once
21 
22 #include <android/hardware/contexthub/1.0/IContexthub.h>
23 #include <gtest/gtest.h>
24 #include <hidl/HidlSupport.h>
25 #include <hidl/ServiceManagement.h>
26 #include <utils/StrongPointer.h>
27 
28 #include <vector>
29 
30 namespace android {
31 namespace hardware {
32 namespace contexthub {
33 namespace vts_utils {
34 
35 #define ASSERT_OK(result) ASSERT_EQ(result, ::android::hardware::contexthub::V1_0::Result::OK)
36 #define EXPECT_OK(result) EXPECT_EQ(result, ::android::hardware::contexthub::V1_0::Result::OK)
37 
38 // Synchronously queries IContexthub::getHubs() and returns the result
39 hidl_vec<V1_0::ContextHub> getHubsSync(V1_0::IContexthub* hubApi);
40 
41 // Create a vector of tuples that include each IContexthub service paired with each hub ID it
42 // exposes via getHubs(). Each tuple represents a test target that we should run the VTS suite
43 // against.
44 template <class IContexthubVersion>
getHalAndHubIdList()45 static std::vector<std::tuple<std::string, std::string>> getHalAndHubIdList() {
46     std::vector<std::tuple<std::string, std::string>> parameters;
47     std::vector<std::string> serviceNames =
48             ::android::hardware::getAllHalInstanceNames(IContexthubVersion::descriptor);
49     for (const std::string& serviceName : serviceNames) {
50         sp<IContexthubVersion> hubApi = IContexthubVersion::getService(serviceName);
51         if (hubApi != nullptr) {
52             hidl_vec<V1_0::ContextHub> hubs = getHubsSync(hubApi.get());
53             for (const V1_0::ContextHub& hub : hubs) {
54                 parameters.push_back(std::make_tuple(serviceName, std::to_string(hub.hubId)));
55             }
56         }
57     }
58 
59     return parameters;
60 }
61 
62 }  // namespace vts_utils
63 }  // namespace contexthub
64 }  // namespace hardware
65 }  // namespace android
66