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