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