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 <radio_hidl_hal_utils_v1_2.h>
18
19void RadioHidlTest_v1_2::SetUp() {
20    radio_v1_2 =
21        ::testing::VtsHalHidlTargetTestBase::getService<::android::hardware::radio::V1_2::IRadio>(
22            RadioHidlEnvironment::Instance()
23                ->getServiceName<::android::hardware::radio::V1_2::IRadio>(
24                    hidl_string(RADIO_SERVICE_NAME)));
25    if (radio_v1_2 == NULL) {
26        sleep(60);
27        radio_v1_2 = ::testing::VtsHalHidlTargetTestBase::getService<
28            ::android::hardware::radio::V1_2::IRadio>(
29            RadioHidlEnvironment::Instance()
30                ->getServiceName<::android::hardware::radio::V1_2::IRadio>(
31                    hidl_string(RADIO_SERVICE_NAME)));
32    }
33    ASSERT_NE(nullptr, radio_v1_2.get());
34
35    radioRsp_v1_2 = new (std::nothrow) RadioResponse_v1_2(*this);
36    ASSERT_NE(nullptr, radioRsp_v1_2.get());
37
38    count_ = 0;
39
40    radioInd_v1_2 = new (std::nothrow) RadioIndication_v1_2(*this);
41    ASSERT_NE(nullptr, radioInd_v1_2.get());
42
43    radio_v1_2->setResponseFunctions(radioRsp_v1_2, radioInd_v1_2);
44
45    updateSimCardStatus();
46    EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_2->rspInfo.type);
47    EXPECT_EQ(serial, radioRsp_v1_2->rspInfo.serial);
48    EXPECT_EQ(RadioError::NONE, radioRsp_v1_2->rspInfo.error);
49
50    /* Enforce Vts Testing with Sim Status Present only. */
51    EXPECT_EQ(CardState::PRESENT, cardStatus.base.cardState);
52}
53
54/*
55 * Notify that the response message is received.
56 */
57void RadioHidlTest_v1_2::notify(int receivedSerial) {
58    std::unique_lock<std::mutex> lock(mtx_);
59    if (serial == receivedSerial) {
60        count_++;
61        cv_.notify_one();
62    }
63}
64
65/*
66 * Wait till the response message is notified or till TIMEOUT_PERIOD.
67 */
68std::cv_status RadioHidlTest_v1_2::wait() {
69    std::unique_lock<std::mutex> lock(mtx_);
70
71    std::cv_status status = std::cv_status::no_timeout;
72    auto now = std::chrono::system_clock::now();
73    while (count_ == 0) {
74        status = cv_.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
75        if (status == std::cv_status::timeout) {
76            return status;
77        }
78    }
79    count_--;
80    return status;
81}
82
83void RadioHidlTest_v1_2::updateSimCardStatus() {
84    serial = GetRandomSerialNumber();
85    radio_v1_2->getIccCardStatus(serial);
86    EXPECT_EQ(std::cv_status::no_timeout, wait());
87}