1 /*
2  * Copyright (C) 2018 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_4.h>
18 
SetUp()19 void RadioHidlTest_v1_4::SetUp() {
20     radio_v1_4 = ::android::hardware::radio::V1_4::IRadio::getService(GetParam());
21 
22     if (radio_v1_4 == NULL) {
23         sleep(60);
24         radio_v1_4 = ::android::hardware::radio::V1_4::IRadio::getService(GetParam());
25     }
26     ASSERT_NE(nullptr, radio_v1_4.get());
27 
28     radioRsp_v1_4 = new (std::nothrow) RadioResponse_v1_4(*this);
29     ASSERT_NE(nullptr, radioRsp_v1_4.get());
30 
31     count_ = 0;
32 
33     radioInd_v1_4 = new (std::nothrow) RadioIndication_v1_4(*this);
34     ASSERT_NE(nullptr, radioInd_v1_4.get());
35 
36     radio_v1_4->setResponseFunctions(radioRsp_v1_4, radioInd_v1_4);
37 
38     updateSimCardStatus();
39     EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_4->rspInfo.type);
40     EXPECT_EQ(serial, radioRsp_v1_4->rspInfo.serial);
41     EXPECT_EQ(RadioError::NONE, radioRsp_v1_4->rspInfo.error);
42 
43     sp<::android::hardware::radio::config::V1_1::IRadioConfig> radioConfig =
44             ::android::hardware::radio::config::V1_1::IRadioConfig::getService();
45 
46     /* Enforce Vts tesing with RadioConfig is existed. */
47     ASSERT_NE(nullptr, radioConfig.get());
48 
49     /* Enforce Vts Testing with Sim Status Present only. */
50     EXPECT_EQ(CardState::PRESENT, cardStatus.base.base.cardState);
51 }
52 
53 /*
54  * Notify that the response message is received.
55  */
notify(int receivedSerial)56 void RadioHidlTest_v1_4::notify(int receivedSerial) {
57     std::unique_lock<std::mutex> lock(mtx_);
58     if (serial == receivedSerial) {
59         count_++;
60         cv_.notify_one();
61     }
62 }
63 
64 /*
65  * Wait till the response message is notified or till TIMEOUT_PERIOD.
66  */
wait()67 std::cv_status RadioHidlTest_v1_4::wait() {
68     std::unique_lock<std::mutex> lock(mtx_);
69 
70     std::cv_status status = std::cv_status::no_timeout;
71     auto now = std::chrono::system_clock::now();
72     while (count_ == 0) {
73         status = cv_.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
74         if (status == std::cv_status::timeout) {
75             return status;
76         }
77     }
78     count_--;
79     return status;
80 }
81 
clearPotentialEstablishedCalls()82 void RadioHidlTest_v1_4::clearPotentialEstablishedCalls() {
83     // Get the current call Id to hangup the established emergency call.
84     serial = GetRandomSerialNumber();
85     radio_v1_4->getCurrentCalls(serial);
86     EXPECT_EQ(std::cv_status::no_timeout, wait());
87 
88     // Hang up to disconnect the established call channels.
89     for (const ::android::hardware::radio::V1_2::Call& call : radioRsp_v1_4->currentCalls) {
90         serial = GetRandomSerialNumber();
91         radio_v1_4->hangup(serial, call.base.index);
92         ALOGI("Hang up to disconnect the established call channel: %d", call.base.index);
93         EXPECT_EQ(std::cv_status::no_timeout, wait());
94         // Give some time for modem to disconnect the established call channel.
95         sleep(MODEM_EMERGENCY_CALL_DISCONNECT_TIME);
96     }
97 
98     // Verify there are no more current calls.
99     serial = GetRandomSerialNumber();
100     radio_v1_4->getCurrentCalls(serial);
101     EXPECT_EQ(std::cv_status::no_timeout, wait());
102     EXPECT_EQ(0, radioRsp_v1_4->currentCalls.size());
103 }
104 
updateSimCardStatus()105 void RadioHidlTest_v1_4::updateSimCardStatus() {
106     serial = GetRandomSerialNumber();
107     radio_v1_4->getIccCardStatus(serial);
108     EXPECT_EQ(std::cv_status::no_timeout, wait());
109 }
110 
stopNetworkScan()111 void RadioHidlTest_v1_4::stopNetworkScan() {
112     serial = GetRandomSerialNumber();
113     radio_v1_4->stopNetworkScan(serial);
114     EXPECT_EQ(std::cv_status::no_timeout, wait());
115 }
116