1/*
2 * Copyright (C) 2018 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 <radio_config_hidl_hal_utils.h>
18
19void RadioConfigHidlTest::SetUp() {
20    radioConfig = ::testing::VtsHalHidlTargetTestBase::getService<IRadioConfig>(
21        RadioConfigHidlEnvironment::Instance()->getServiceName<IRadioConfig>(
22            hidl_string(RADIO_SERVICE_NAME)));
23    if (radioConfig == NULL) {
24        sleep(60);
25        radioConfig = ::testing::VtsHalHidlTargetTestBase::getService<IRadioConfig>(
26            RadioConfigHidlEnvironment::Instance()->getServiceName<IRadioConfig>(
27                hidl_string(RADIO_SERVICE_NAME)));
28    }
29    ASSERT_NE(nullptr, radioConfig.get());
30
31    radioConfigRsp = new (std::nothrow) RadioConfigResponse(*this);
32    ASSERT_NE(nullptr, radioConfigRsp.get());
33
34    count_ = 0;
35
36    radioConfigInd = new (std::nothrow) RadioConfigIndication(*this);
37    ASSERT_NE(nullptr, radioConfigInd.get());
38
39    radioConfig->setResponseFunctions(radioConfigRsp, radioConfigInd);
40}
41
42/*
43 * Notify that the response message is received.
44 */
45void RadioConfigHidlTest::notify() {
46    std::unique_lock<std::mutex> lock(mtx_);
47    count_++;
48    cv_.notify_one();
49}
50
51/*
52 * Wait till the response message is notified or till TIMEOUT_PERIOD.
53 */
54std::cv_status RadioConfigHidlTest::wait() {
55    std::unique_lock<std::mutex> lock(mtx_);
56
57    std::cv_status status = std::cv_status::no_timeout;
58    auto now = std::chrono::system_clock::now();
59    while (count_ == 0) {
60        status = cv_.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
61        if (status == std::cv_status::timeout) {
62            return status;
63        }
64    }
65    count_--;
66    return status;
67}
68