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_0.h>
18
19void RadioHidlTest::SetUp() {
20    radio = ::testing::VtsHalHidlTargetTestBase::getService<IRadio>(
21        RadioHidlEnvironment::Instance()->getServiceName<IRadio>(hidl_string(RADIO_SERVICE_NAME)));
22    if (radio == NULL) {
23        sleep(60);
24        radio = ::testing::VtsHalHidlTargetTestBase::getService<IRadio>(
25            RadioHidlEnvironment::Instance()->getServiceName<IRadio>(
26                hidl_string(RADIO_SERVICE_NAME)));
27    }
28    ASSERT_NE(nullptr, radio.get());
29
30    radioRsp = new (std::nothrow) RadioResponse(*this);
31    ASSERT_NE(nullptr, radioRsp.get());
32
33    count = 0;
34
35    radioInd = new (std::nothrow) RadioIndication(*this);
36    ASSERT_NE(nullptr, radioInd.get());
37
38    radio->setResponseFunctions(radioRsp, radioInd);
39
40    updateSimCardStatus();
41    EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp->rspInfo.type);
42    EXPECT_EQ(serial, radioRsp->rspInfo.serial);
43    EXPECT_EQ(RadioError::NONE, radioRsp->rspInfo.error);
44
45    /* Enforce Vts Testing with Sim Status Present only. */
46    EXPECT_EQ(CardState::PRESENT, cardStatus.cardState);
47}
48
49void RadioHidlTest::notify(int receivedSerial) {
50    std::unique_lock<std::mutex> lock(mtx);
51    if (serial == receivedSerial) {
52        count++;
53        cv.notify_one();
54    }
55}
56
57std::cv_status RadioHidlTest::wait(int sec) {
58    std::unique_lock<std::mutex> lock(mtx);
59
60    std::cv_status status = std::cv_status::no_timeout;
61    auto now = std::chrono::system_clock::now();
62    while (count == 0) {
63        status = cv.wait_until(lock, now + std::chrono::seconds(sec));
64        if (status == std::cv_status::timeout) {
65            return status;
66        }
67    }
68    count--;
69    return status;
70}
71
72void RadioHidlTest::updateSimCardStatus() {
73    serial = GetRandomSerialNumber();
74    radio->getIccCardStatus(serial);
75    EXPECT_EQ(std::cv_status::no_timeout, wait());
76}