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 
isServiceValidForDeviceConfiguration(hidl_string & serviceName)19 bool isServiceValidForDeviceConfiguration(hidl_string& serviceName) {
20     if (isSsSsEnabled()) {
21         // Device is configured as SSSS.
22         if (serviceName != RADIO_SERVICE_SLOT1_NAME) {
23             ALOGI("%s instance is not valid for SSSS device.", serviceName.c_str());
24             return false;
25         }
26     } else if (isDsDsEnabled()) {
27         // Device is configured as DSDS.
28         if (serviceName != RADIO_SERVICE_SLOT1_NAME && serviceName != RADIO_SERVICE_SLOT2_NAME) {
29             ALOGI("%s instance is not valid for DSDS device.", serviceName.c_str());
30             return false;
31         }
32     } else if (isTsTsEnabled()) {
33         // Device is configured as TSTS.
34         if (serviceName != RADIO_SERVICE_SLOT1_NAME && serviceName != RADIO_SERVICE_SLOT2_NAME &&
35             serviceName != RADIO_SERVICE_SLOT3_NAME) {
36             ALOGI("%s instance is not valid for TSTS device.", serviceName.c_str());
37             return false;
38         }
39     }
40     return true;
41 }
42 
SetUp()43 void RadioHidlTest_v1_5::SetUp() {
44     hidl_string serviceName = GetParam();
45     if (!isServiceValidForDeviceConfiguration(serviceName)) {
46         ALOGI("Skipped the test due to device configuration.");
47         GTEST_SKIP();
48     }
49 
50     radio_v1_5 = android::hardware::radio::V1_5::IRadio::getService(serviceName);
51     ASSERT_NE(nullptr, radio_v1_5.get());
52 
53     radioRsp_v1_5 = new (std::nothrow) RadioResponse_v1_5(*this);
54     ASSERT_NE(nullptr, radioRsp_v1_5.get());
55 
56     count_ = 0;
57 
58     radioInd_v1_5 = new (std::nothrow) RadioIndication_v1_5(*this);
59     ASSERT_NE(nullptr, radioInd_v1_5.get());
60 
61     radio_v1_5->setResponseFunctions(radioRsp_v1_5, radioInd_v1_5);
62 
63     updateSimCardStatus();
64     EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_5->rspInfo.type);
65     EXPECT_EQ(serial, radioRsp_v1_5->rspInfo.serial);
66     EXPECT_EQ(RadioError::NONE, radioRsp_v1_5->rspInfo.error);
67 
68     sp<::android::hardware::radio::config::V1_1::IRadioConfig> radioConfig =
69             ::android::hardware::radio::config::V1_1::IRadioConfig::getService();
70     /* Enforce Vts testing with RadioConfig is existed. */
71     ASSERT_NE(nullptr, radioConfig.get());
72 
73     /* Enforce Vts Testing with Sim Status Present only. */
74     EXPECT_EQ(CardState::PRESENT, cardStatus.base.base.base.cardState);
75 }
76 
77 /*
78  * Notify that the response message is received.
79  */
notify(int receivedSerial)80 void RadioHidlTest_v1_5::notify(int receivedSerial) {
81     std::unique_lock<std::mutex> lock(mtx_);
82     if (serial == receivedSerial) {
83         count_++;
84         cv_.notify_one();
85     }
86 }
87 
88 /*
89  * Wait till the response message is notified or till TIMEOUT_PERIOD.
90  */
wait()91 std::cv_status RadioHidlTest_v1_5::wait() {
92     std::unique_lock<std::mutex> lock(mtx_);
93 
94     std::cv_status status = std::cv_status::no_timeout;
95     auto now = std::chrono::system_clock::now();
96     while (count_ == 0) {
97         status = cv_.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
98         if (status == std::cv_status::timeout) {
99             return status;
100         }
101     }
102     count_--;
103     return status;
104 }
105 
updateSimCardStatus()106 void RadioHidlTest_v1_5::updateSimCardStatus() {
107     serial = GetRandomSerialNumber();
108     radio_v1_5->getIccCardStatus(serial);
109     EXPECT_EQ(std::cv_status::no_timeout, wait());
110 }
111 
stopNetworkScan()112 void RadioHidlTest_v1_5::stopNetworkScan() {
113     serial = GetRandomSerialNumber();
114     radio_v1_5->stopNetworkScan(serial);
115     EXPECT_EQ(std::cv_status::no_timeout, wait());
116 }
117