• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #define LOG_TAG "RadioTest"
17 
18 #include "radio_aidl_hal_utils.h"
19 #include <iostream>
20 #include "VtsCoreUtil.h"
21 #include "radio_config_utils.h"
22 #include "radio_sim_utils.h"
23 
24 #define WAIT_TIMEOUT_PERIOD 75
25 
26 sim::CardStatus cardStatus = {};
27 config::SimSlotStatus slotStatus = {};
28 int serial = 0;
29 int count_ = 0;
30 
GetRandomSerialNumber()31 int GetRandomSerialNumber() {
32     return rand();
33 }
34 
CheckAnyOfErrors(RadioError err,std::vector<RadioError> errors,CheckFlag flag)35 ::testing::AssertionResult CheckAnyOfErrors(RadioError err, std::vector<RadioError> errors,
36                                             CheckFlag flag) {
37     const static std::vector<RadioError> generalErrors = {
38             RadioError::RADIO_NOT_AVAILABLE,   RadioError::NO_MEMORY,
39             RadioError::INTERNAL_ERR,          RadioError::SYSTEM_ERR,
40             RadioError::REQUEST_NOT_SUPPORTED, RadioError::CANCELLED};
41     if (flag == CHECK_GENERAL_ERROR || flag == CHECK_OEM_AND_GENERAL_ERROR) {
42         for (size_t i = 0; i < generalErrors.size(); i++) {
43             if (err == generalErrors[i]) {
44                 return testing::AssertionSuccess();
45             }
46         }
47     }
48     if (flag == CHECK_OEM_ERROR || flag == CHECK_OEM_AND_GENERAL_ERROR) {
49         if (err >= RadioError::OEM_ERROR_1 && err <= RadioError::OEM_ERROR_25) {
50             return testing::AssertionSuccess();
51         }
52     }
53     for (size_t i = 0; i < errors.size(); i++) {
54         if (err == errors[i]) {
55             return testing::AssertionSuccess();
56         }
57     }
58     return testing::AssertionFailure() << "RadioError:" + toString(err) + " is returned";
59 }
60 
61 // Runs "pm list features" and attempts to find the specified feature in its output.
deviceSupportsFeature(const char * feature)62 bool deviceSupportsFeature(const char* feature) {
63     bool hasFeature = false;
64     FILE* p = popen("/system/bin/pm list features", "re");
65     if (p) {
66         char* line = NULL;
67         size_t len = 0;
68         while (getline(&line, &len, p) > 0) {
69             if (strstr(line, feature)) {
70                 hasFeature = true;
71                 break;
72             }
73         }
74         pclose(p);
75     } else {
76         __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "popen failed: %d", errno);
77         _exit(EXIT_FAILURE);
78     }
79     __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "Feature %s: %ssupported", feature,
80                         hasFeature ? "" : "not ");
81     return hasFeature;
82 }
83 
isSsSsEnabled()84 bool isSsSsEnabled() {
85     // Do not use checkSubstringInCommandOutput("getprop persist.radio.multisim.config", "")
86     // until b/148904287 is fixed. We need exact matching instead of partial matching. (i.e.
87     // by definition the empty string "" is a substring of any string).
88     return !isDsDsEnabled() && !isTsTsEnabled();
89 }
90 
isDsDsEnabled()91 bool isDsDsEnabled() {
92     return testing::checkSubstringInCommandOutput("getprop persist.radio.multisim.config", "dsds");
93 }
94 
isDsDaEnabled()95 bool isDsDaEnabled() {
96     return testing::checkSubstringInCommandOutput("getprop persist.radio.multisim.config", "dsda");
97 }
98 
isTsTsEnabled()99 bool isTsTsEnabled() {
100     return testing::checkSubstringInCommandOutput("getprop persist.radio.multisim.config", "tsts");
101 }
102 
isVoiceInService(RegState state)103 bool isVoiceInService(RegState state) {
104     return RegState::REG_HOME == state || RegState::REG_ROAMING == state;
105 }
106 
isVoiceEmergencyOnly(RegState state)107 bool isVoiceEmergencyOnly(RegState state) {
108     return RegState::NOT_REG_MT_NOT_SEARCHING_OP_EM == state ||
109            RegState::NOT_REG_MT_SEARCHING_OP_EM == state || RegState::REG_DENIED_EM == state ||
110            RegState::UNKNOWN_EM == state;
111 }
112 
stringEndsWith(std::string const & string,std::string const & end)113 bool stringEndsWith(std::string const& string, std::string const& end) {
114     if (string.size() >= end.size()) {
115         return std::equal(end.rbegin(), end.rend(), string.rbegin());
116     } else {
117         return false;
118     }
119 }
120 
isServiceValidForDeviceConfiguration(std::string & serviceName)121 bool isServiceValidForDeviceConfiguration(std::string& serviceName) {
122     if (isSsSsEnabled()) {
123         // Device is configured as SSSS.
124         if (!stringEndsWith(serviceName, RADIO_SERVICE_SLOT1_NAME)) {
125             ALOGI("%s instance is not valid for SSSS device.", serviceName.c_str());
126             return false;
127         }
128     } else if (isDsDsEnabled()) {
129         // Device is configured as DSDS.
130         if (!stringEndsWith(serviceName, RADIO_SERVICE_SLOT1_NAME) &&
131             !stringEndsWith(serviceName, RADIO_SERVICE_SLOT2_NAME)) {
132             ALOGI("%s instance is not valid for DSDS device.", serviceName.c_str());
133             return false;
134         }
135     } else if (isTsTsEnabled()) {
136         // Device is configured as TSTS.
137         if (!stringEndsWith(serviceName, RADIO_SERVICE_SLOT1_NAME) &&
138             !stringEndsWith(serviceName, RADIO_SERVICE_SLOT2_NAME) &&
139             !stringEndsWith(serviceName, RADIO_SERVICE_SLOT3_NAME)) {
140             ALOGI("%s instance is not valid for TSTS device.", serviceName.c_str());
141             return false;
142         }
143     }
144     return true;
145 }
146 
147 /*
148  * Notify that the response message is received.
149  */
notify(int receivedSerial)150 void RadioServiceTest::notify(int receivedSerial) {
151     std::unique_lock<std::mutex> lock(mtx_);
152     if (serial == receivedSerial) {
153         count_++;
154         cv_.notify_one();
155     }
156 }
157 
158 /*
159  * Wait till the response message is notified or till WAIT_TIMEOUT_PERIOD.
160  */
wait()161 std::cv_status RadioServiceTest::wait() {
162     std::unique_lock<std::mutex> lock(mtx_);
163     std::cv_status status = std::cv_status::no_timeout;
164     auto now = std::chrono::system_clock::now();
165     while (count_ == 0) {
166         status = cv_.wait_until(lock, now + std::chrono::seconds(WAIT_TIMEOUT_PERIOD));
167         if (status == std::cv_status::timeout) {
168             return status;
169         }
170     }
171     count_--;
172     return status;
173 }
174 
175 /**
176  * Specific features on the Radio HAL rely on Radio HAL Capabilities.
177  * The VTS test related to those features must not run if the related capability is disabled.
178  * Typical usage within VTS:
179  * if (getRadioHalCapabilities()) return;
180  */
getRadioHalCapabilities()181 bool RadioServiceTest::getRadioHalCapabilities() {
182     // Get HalDeviceCapabilities from RadioConfig
183     std::shared_ptr<RadioConfigResponse> radioConfigRsp =
184             ndk::SharedRefBase::make<RadioConfigResponse>(*this);
185     std::shared_ptr<RadioConfigIndication> radioConfigInd =
186             ndk::SharedRefBase::make<RadioConfigIndication>(*this);
187     radio_config->setResponseFunctions(radioConfigRsp, radioConfigInd);
188     serial = GetRandomSerialNumber();
189     radio_config->getHalDeviceCapabilities(serial);
190     EXPECT_EQ(std::cv_status::no_timeout, wait());
191     return radioConfigRsp->modemReducedFeatureSet1;
192 }
193 
194 /**
195  * Some VTS tests require the SIM card status to be present before running.
196  * Update the SIM card status, which can be accessed via the extern cardStatus.
197  */
updateSimCardStatus()198 void RadioServiceTest::updateSimCardStatus() {
199     // Update CardStatus from RadioSim
200     std::shared_ptr<RadioSimResponse> radioSimRsp =
201             ndk::SharedRefBase::make<RadioSimResponse>(*this);
202     std::shared_ptr<RadioSimIndication> radioSimInd =
203             ndk::SharedRefBase::make<RadioSimIndication>(*this);
204     radio_sim->setResponseFunctions(radioSimRsp, radioSimInd);
205     serial = GetRandomSerialNumber();
206     radio_sim->getIccCardStatus(serial);
207     EXPECT_EQ(std::cv_status::no_timeout, wait());
208     EXPECT_EQ(RadioResponseType::SOLICITED, radioSimRsp->rspInfo.type);
209     EXPECT_EQ(serial, radioSimRsp->rspInfo.serial);
210     EXPECT_EQ(RadioError::NONE, radioSimRsp->rspInfo.error);
211 }
212 
updateSimSlotStatus(int physicalSlotId)213 void RadioServiceTest::updateSimSlotStatus(int physicalSlotId) {
214     // Update SimSlotStatus from RadioConfig
215     std::shared_ptr<RadioConfigResponse> radioConfigRsp =
216             ndk::SharedRefBase::make<RadioConfigResponse>(*this);
217     std::shared_ptr<RadioConfigIndication> radioConfigInd =
218             ndk::SharedRefBase::make<RadioConfigIndication>(*this);
219     radio_config->setResponseFunctions(radioConfigRsp, radioConfigInd);
220     serial = GetRandomSerialNumber();
221     radio_config->getSimSlotsStatus(serial);
222     EXPECT_EQ(std::cv_status::no_timeout, wait());
223     EXPECT_EQ(RadioResponseType::SOLICITED, radioConfigRsp->rspInfo.type);
224     EXPECT_EQ(serial, radioConfigRsp->rspInfo.serial);
225     EXPECT_EQ(RadioError::NONE, radioConfigRsp->rspInfo.error);
226     if (radioConfigRsp->simSlotStatus.size() > physicalSlotId) {
227         slotStatus = radioConfigRsp->simSlotStatus[physicalSlotId];
228     }
229 }
230