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 
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_2::SetUp() {
44     hidl_string serviceName = GetParam();
45     if (!isServiceValidForDeviceConfiguration(serviceName)) {
46         ALOGI("Skipped the test due to device configuration.");
47         GTEST_SKIP();
48     }
49     radio_v1_2 = ::android::hardware::radio::V1_2::IRadio::getService(serviceName);
50     if (radio_v1_2 == NULL) {
51         sleep(60);
52         radio_v1_2 = ::android::hardware::radio::V1_2::IRadio::getService(serviceName);
53     }
54     ASSERT_NE(nullptr, radio_v1_2.get());
55 
56     radioRsp_v1_2 = new (std::nothrow) RadioResponse_v1_2(*this);
57     ASSERT_NE(nullptr, radioRsp_v1_2.get());
58 
59     count_ = 0;
60     logicalSlotId = -1;
61 
62     radioInd_v1_2 = new (std::nothrow) RadioIndication_v1_2(*this);
63     ASSERT_NE(nullptr, radioInd_v1_2.get());
64 
65     radio_v1_2->setResponseFunctions(radioRsp_v1_2, radioInd_v1_2);
66 
67     updateSimCardStatus();
68     EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_2->rspInfo.type);
69     EXPECT_EQ(serial, radioRsp_v1_2->rspInfo.serial);
70     EXPECT_EQ(RadioError::NONE, radioRsp_v1_2->rspInfo.error);
71 
72     /* Enforce Vts Testing with Sim Status Present only. */
73     EXPECT_EQ(CardState::PRESENT, cardStatus.base.cardState);
74 
75     radioConfig = ::android::hardware::radio::config::V1_1::IRadioConfig::getService();
76 
77     /* Enforce Vts tesing with RadioConfig for network scan excemption. */
78     // Some devices can only perform network scan on logical modem that currently used for packet
79     // data. This exemption is removed in HAL version 1.4. See b/135243177 for additional info.
80     if (radioConfig != NULL) {
81         // RadioConfig 1.1 available, some devices fall in excepmtion category.
82         ASSERT_NE(nullptr, radioConfig.get());
83 
84         radioConfigRsp = new (std::nothrow) RadioConfigResponse(*this);
85         ASSERT_NE(nullptr, radioConfigRsp.get());
86 
87         /* Set radio config response functions */
88         radioConfig->setResponseFunctions(radioConfigRsp, nullptr);
89 
90         /* set preferred data modem */
91         setPreferredDataModem();
92 
93         /* get current logical sim id */
94         getLogicalSimId();
95     }
96 }
97 
getLogicalSimId()98 void RadioHidlTest_v1_2::getLogicalSimId() {
99     serial = GetRandomSerialNumber();
100     radioConfig->getSimSlotsStatus(serial);
101     EXPECT_EQ(std::cv_status::no_timeout, wait());
102     EXPECT_EQ(RadioResponseType::SOLICITED, radioConfigRsp->rspInfo.type);
103     EXPECT_EQ(serial, radioConfigRsp->rspInfo.serial);
104 
105     ASSERT_TRUE(CheckAnyOfErrors(radioConfigRsp->rspInfo.error,
106                                  {RadioError::NONE, RadioError::REQUEST_NOT_SUPPORTED}));
107 
108     if (radioConfigRsp->rspInfo.error != RadioError ::NONE) {
109         ALOGI("Failed to get sim slot status, rspInfo.error = %s\n",
110               toString(radioConfigRsp->rspInfo.error).c_str());
111         return;
112     }
113 
114     if (cardStatus.physicalSlotId < 0 ||
115         cardStatus.physicalSlotId >= radioConfigRsp->simSlotStatus.size()) {
116         ALOGI("Physical slot id: %d is out of range", cardStatus.physicalSlotId);
117         return;
118     }
119 
120     logicalSlotId = radioConfigRsp->simSlotStatus[cardStatus.physicalSlotId].logicalSlotId;
121 }
122 
123 /*
124  * Set preferred data modem
125  */
setPreferredDataModem()126 void RadioHidlTest_v1_2::setPreferredDataModem() {
127     serial = GetRandomSerialNumber();
128     // Even for single sim device, the setPreferredDataModem should still success. Enforce dds on
129     // first logical modem.
130     radioConfig->setPreferredDataModem(serial, DDS_LOGICAL_SLOT_INDEX);
131     EXPECT_EQ(std::cv_status::no_timeout, wait());
132     EXPECT_EQ(RadioResponseType::SOLICITED, radioConfigRsp->rspInfo.type);
133     EXPECT_EQ(serial, radioConfigRsp->rspInfo.serial);
134 
135     ASSERT_TRUE(CheckAnyOfErrors(
136             radioConfigRsp->rspInfo.error,
137             {RadioError::NONE, RadioError::RADIO_NOT_AVAILABLE, RadioError::INTERNAL_ERR}));
138 }
139 
140 /*
141  * Notify that the response message is received.
142  */
notify(int receivedSerial)143 void RadioHidlTest_v1_2::notify(int receivedSerial) {
144     std::unique_lock<std::mutex> lock(mtx_);
145     if (serial == receivedSerial) {
146         count_++;
147         cv_.notify_one();
148     }
149 }
150 
151 /*
152  * Wait till the response message is notified or till TIMEOUT_PERIOD.
153  */
wait()154 std::cv_status RadioHidlTest_v1_2::wait() {
155     std::unique_lock<std::mutex> lock(mtx_);
156 
157     std::cv_status status = std::cv_status::no_timeout;
158     auto now = std::chrono::system_clock::now();
159     while (count_ == 0) {
160         status = cv_.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
161         if (status == std::cv_status::timeout) {
162             return status;
163         }
164     }
165     count_--;
166     return status;
167 }
168 
updateSimCardStatus()169 void RadioHidlTest_v1_2::updateSimCardStatus() {
170     serial = GetRandomSerialNumber();
171     radio_v1_2->getIccCardStatus(serial);
172     EXPECT_EQ(std::cv_status::no_timeout, wait());
173 }
174 
stopNetworkScan()175 void RadioHidlTest_v1_2::stopNetworkScan() {
176     serial = GetRandomSerialNumber();
177     radio_v1_2->stopNetworkScan(serial);
178     EXPECT_EQ(std::cv_status::no_timeout, wait());
179 }
180