• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 any Context Hub VTS code (i.e. not specific to e.g. HIDL).
19  */
20 
21 #pragma once
22 
23 #include <chrono>
24 #include <future>
25 
26 namespace android {
27 namespace hardware {
28 namespace contexthub {
29 namespace vts_utils {
30 
31 // App ID with vendor "GoogT" (Google Testing), app identifier 0x555555. This
32 // app ID is reserved and must never appear in the list of loaded apps.
33 constexpr uint64_t kNonExistentAppId = 0x476f6f6754555555;
34 
35 // Helper that does explicit conversion of an enum class to its underlying/base
36 // type. Useful for stream output of enum values.
37 template <typename EnumType>
asBaseType(EnumType value)38 inline constexpr typename std::underlying_type<EnumType>::type asBaseType(EnumType value) {
39     return static_cast<typename std::underlying_type<EnumType>::type>(value);
40 }
41 
42 // Wait for a callback to occur (signaled by the given future) up to the
43 // provided timeout. If the future is invalid or the callback does not come
44 // within the given time, returns false.
45 template <class ReturnType>
46 bool waitForCallback(std::future<ReturnType> future, ReturnType* result,
47                      std::chrono::milliseconds timeout = std::chrono::seconds(5)) {
48     auto expiration = std::chrono::system_clock::now() + timeout;
49 
50     EXPECT_NE(result, nullptr);
51     EXPECT_TRUE(future.valid());
52     if (result != nullptr && future.valid()) {
53         std::future_status status = future.wait_until(expiration);
54         EXPECT_NE(status, std::future_status::timeout) << "Timed out waiting for callback";
55 
56         if (status == std::future_status::ready) {
57             *result = future.get();
58             return true;
59         }
60     }
61 
62     return false;
63 }
64 
65 }  // namespace vts_utils
66 }  // namespace contexthub
67 }  // namespace hardware
68 }  // namespace android
69