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 #pragma once
18 
19 #include <android-base/logging.h>
20 
21 #include <android/hardware/radio/1.0/types.h>
22 #include <android/log.h>
23 #include <gtest/gtest.h>
24 
25 using ::android::hardware::radio::V1_0::RadioError;
26 using ::android::hardware::radio::V1_0::RegState;
27 using ::android::hardware::radio::V1_0::SapResultCode;
28 using namespace std;
29 
30 /*
31  * MACRO used to skip test case when radio response return error REQUEST_NOT_SUPPORTED
32  * on HAL versions which has deprecated the request interfaces. The MACRO can only be used
33  * AFTER receiving radio response.
34  */
35 #define SKIP_TEST_IF_REQUEST_NOT_SUPPORTED_WITH_HAL(__ver__, __radio__, __radioRsp__)      \
36     do {                                                                                   \
37         sp<::android::hardware::radio::V##__ver__::IRadio> __radio =                       \
38                 ::android::hardware::radio::V##__ver__::IRadio::castFrom(__radio__);       \
39         if (__radio && __radioRsp__->rspInfo.error == RadioError::REQUEST_NOT_SUPPORTED) { \
40             GTEST_SKIP() << "REQUEST_NOT_SUPPORTED";                                       \
41         }                                                                                  \
42     } while (0)
43 
44 enum CheckFlag {
45     CHECK_DEFAULT = 0,
46     CHECK_GENERAL_ERROR = 1,
47     CHECK_OEM_ERROR = 2,
48     CHECK_OEM_AND_GENERAL_ERROR = 3,
49     CHECK_SAP_ERROR = 4,
50 };
51 
52 static constexpr const char* FEATURE_VOICE_CALL = "android.software.connectionservice";
53 
54 static constexpr const char* FEATURE_TELEPHONY = "android.hardware.telephony";
55 
56 static constexpr const char* FEATURE_TELEPHONY_GSM = "android.hardware.telephony.gsm";
57 
58 static constexpr const char* FEATURE_TELEPHONY_CDMA = "android.hardware.telephony.cdma";
59 
60 /*
61  * Generate random serial number for radio test
62  */
63 int GetRandomSerialNumber();
64 
65 /*
66  * Check multiple radio error codes which are possibly returned because of the different
67  * vendor/devices implementations. It allows optional checks for general errors or/and oem errors.
68  */
69 ::testing::AssertionResult CheckAnyOfErrors(RadioError err, std::vector<RadioError> generalError,
70                                             CheckFlag flag = CHECK_DEFAULT);
71 /*
72  * Check multiple sap error codes which are possibly returned because of the different
73  * vendor/devices implementations.
74  */
75 ::testing::AssertionResult CheckAnyOfErrors(SapResultCode err, std::vector<SapResultCode> errors);
76 
77 /*
78  * Check if device supports feature.
79  */
80 bool deviceSupportsFeature(const char* feature);
81 
82 /*
83  * Check if device is in SsSs (Single SIM Single Standby).
84  */
85 bool isSsSsEnabled();
86 
87 /*
88  * Check if device is in DSDS (Dual SIM Dual Standby).
89  */
90 bool isDsDsEnabled();
91 
92 /*
93  * Check if device is in TSTS (Triple SIM Triple Standby).
94  */
95 bool isTsTsEnabled();
96 
97 /*
98  * Check if voice status is in emergency only.
99  */
100 bool isVoiceEmergencyOnly(RegState state);
101 
102 /*
103  * Check if voice status is in service.
104  */
105 bool isVoiceInService(RegState state);
106 
107 /**
108  * Used when waiting for an asynchronous response from the HAL.
109  */
110 class RadioResponseWaiter {
111   protected:
112     std::mutex mtx_;
113     std::condition_variable cv_;
114     int count_;
115 
116   public:
117     /* Serial number for radio request */
118     int serial;
119 
120     /* Used as a mechanism to inform the test about data/event callback */
121     void notify(int receivedSerial);
122 
123     /* Test code calls this function to wait for response */
124     std::cv_status wait();
125 };
126