Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame] | 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 | */ |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame] | 16 | |
Arthur Ishiguro | fe0f98e | 2021-08-12 14:43:39 -0700 | [diff] [blame] | 17 | /** |
| 18 | * Utils file for any Context Hub VTS code (i.e. not specific to e.g. HIDL). |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame] | 22 | |
| 23 | #include <chrono> |
| 24 | #include <future> |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace contexthub { |
| 29 | namespace vts_utils { |
| 30 | |
Anthony Stange | a2c3db1 | 2020-12-16 15:04:02 -0500 | [diff] [blame] | 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 | |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame] | 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> |
| 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 | |
Anthony Stange | a2c3db1 | 2020-12-16 15:04:02 -0500 | [diff] [blame] | 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 | |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame] | 65 | } // namespace vts_utils |
| 66 | } // namespace contexthub |
| 67 | } // namespace hardware |
| 68 | } // namespace android |