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