blob: 8b04e3f04fa614667142c33e854a88167e6dee69 [file] [log] [blame]
Brian Duddie01eb01a2020-02-14 15:35:57 -08001/*
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 */
Brian Duddie01eb01a2020-02-14 15:35:57 -080016
Arthur Ishigurofe0f98e2021-08-12 14:43:39 -070017/**
18 * Utils file for any Context Hub VTS code (i.e. not specific to e.g. HIDL).
19 */
20
21#pragma once
Brian Duddie01eb01a2020-02-14 15:35:57 -080022
23#include <chrono>
24#include <future>
Brian Duddie01eb01a2020-02-14 15:35:57 -080025
26namespace android {
27namespace hardware {
28namespace contexthub {
29namespace vts_utils {
30
Anthony Stangea2c3db12020-12-16 15:04:02 -050031// 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.
33constexpr uint64_t kNonExistentAppId = 0x476f6f6754555555;
34
Brian Duddie01eb01a2020-02-14 15:35:57 -080035// Helper that does explicit conversion of an enum class to its underlying/base
36// type. Useful for stream output of enum values.
37template <typename EnumType>
38inline constexpr typename std::underlying_type<EnumType>::type asBaseType(EnumType value) {
39 return static_cast<typename std::underlying_type<EnumType>::type>(value);
40}
41
Anthony Stangea2c3db12020-12-16 15:04:02 -050042// 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.
45template <class ReturnType>
46bool 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
Brian Duddie01eb01a2020-02-14 15:35:57 -080065} // namespace vts_utils
66} // namespace contexthub
67} // namespace hardware
68} // namespace android