1 /*
2  * Copyright (C) 2017 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 #include <sap_hidl_hal_utils.h>
18 
SetUp()19 void SapHidlTest::SetUp() {
20     sap = ISap::getService(GetParam());
21     ASSERT_NE(sap, nullptr);
22 
23     sapCb = new SapCallback(*this);
24     ASSERT_NE(sapCb, nullptr);
25 
26     count = 0;
27 
28     sap->setCallback(sapCb);
29 }
30 
TearDown()31 void SapHidlTest::TearDown() {}
32 
notify(int receivedToken)33 void SapHidlTest::notify(int receivedToken) {
34     std::unique_lock<std::mutex> lock(mtx);
35     count++;
36     if (token == receivedToken) {
37         cv.notify_one();
38     }
39 }
40 
wait()41 std::cv_status SapHidlTest::wait() {
42     std::unique_lock<std::mutex> lock(mtx);
43 
44     std::cv_status status = std::cv_status::no_timeout;
45     auto now = std::chrono::system_clock::now();
46     while (count == 0) {
47         status = cv.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
48         if (status == std::cv_status::timeout) {
49             return status;
50         }
51     }
52     count--;
53     return status;
54 }
55