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 <android-base/logging.h>
18 #include <radio_hidl_hal_utils_v1_0.h>
19 
isServiceValidForDeviceConfiguration(hidl_string & serviceName)20 bool isServiceValidForDeviceConfiguration(hidl_string& serviceName) {
21     if (isSsSsEnabled()) {
22         // Device is configured as SSSS.
23         if (serviceName != RADIO_SERVICE_SLOT1_NAME) {
24             ALOGI("%s instance is not valid for SSSS device.", serviceName.c_str());
25             return false;
26         }
27     } else if (isDsDsEnabled()) {
28         // Device is configured as DSDS.
29         if (serviceName != RADIO_SERVICE_SLOT1_NAME && serviceName != RADIO_SERVICE_SLOT2_NAME) {
30             ALOGI("%s instance is not valid for DSDS device.", serviceName.c_str());
31             return false;
32         }
33     } else if (isTsTsEnabled()) {
34         // Device is configured as TSTS.
35         if (serviceName != RADIO_SERVICE_SLOT1_NAME && serviceName != RADIO_SERVICE_SLOT2_NAME &&
36             serviceName != RADIO_SERVICE_SLOT3_NAME) {
37             ALOGI("%s instance is not valid for TSTS device.", serviceName.c_str());
38             return false;
39         }
40     }
41     return true;
42 }
43 
SetUp()44 void RadioHidlTest::SetUp() {
45     hidl_string serviceName = GetParam();
46     if (!isServiceValidForDeviceConfiguration(serviceName)) {
47         ALOGI("Skipped the test due to device configuration.");
48         GTEST_SKIP();
49     }
50 
51     radio = IRadio::getService(serviceName);
52     if (radio == NULL) {
53         LOG(DEBUG) << "Radio is NULL, waiting 1 minute to retry";
54         sleep(60);
55         radio = IRadio::getService(serviceName);
56     }
57     ASSERT_NE(nullptr, radio.get());
58 
59     radioRsp = new (std::nothrow) RadioResponse(*this);
60     ASSERT_NE(nullptr, radioRsp.get());
61 
62     count = 0;
63 
64     radioInd = new (std::nothrow) RadioIndication(*this);
65     ASSERT_NE(nullptr, radioInd.get());
66 
67     radio->setResponseFunctions(radioRsp, radioInd);
68 
69     updateSimCardStatus();
70     EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp->rspInfo.type);
71     EXPECT_EQ(serial, radioRsp->rspInfo.serial);
72     EXPECT_EQ(RadioError::NONE, radioRsp->rspInfo.error);
73 
74     /* Enforce Vts Testing with Sim Status Present only. */
75     EXPECT_EQ(CardState::PRESENT, cardStatus.cardState);
76 }
77 
notify(int receivedSerial)78 void RadioHidlTest::notify(int receivedSerial) {
79     std::unique_lock<std::mutex> lock(mtx);
80     if (serial == receivedSerial) {
81         count++;
82         cv.notify_one();
83     }
84 }
85 
wait(int sec)86 std::cv_status RadioHidlTest::wait(int sec) {
87     std::unique_lock<std::mutex> lock(mtx);
88 
89     std::cv_status status = std::cv_status::no_timeout;
90     auto now = std::chrono::system_clock::now();
91     while (count == 0) {
92         status = cv.wait_until(lock, now + std::chrono::seconds(sec));
93         if (status == std::cv_status::timeout) {
94             return status;
95         }
96     }
97     count--;
98     return status;
99 }
100 
updateSimCardStatus()101 void RadioHidlTest::updateSimCardStatus() {
102     serial = GetRandomSerialNumber();
103     radio->getIccCardStatus(serial);
104     EXPECT_EQ(std::cv_status::no_timeout, wait());
105 }
106