1 /*
2  * Copyright (C) 2023 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 <aidl/Gtest.h>
18 #include <aidl/Vintf.h>
19 #include <aidl/hardware/google/bluetooth/sar/IBluetoothSar.h>
20 #include <android/binder_auto_utils.h>
21 #include <android/binder_manager.h>
22 #include <android/binder_process.h>
23 #include <binder/IServiceManager.h>
24 #include <binder/ProcessState.h>
25 
26 using aidl::hardware::google::bluetooth::sar::IBluetoothSar;
27 using ndk::ScopedAStatus;
28 using ndk::SpAIBinder;
29 
30 class BluetoothSarTest : public ::testing::TestWithParam<std::string> {
31 public:
32   virtual void SetUp() override;
33   virtual void TearDown() override;
34 
35   ndk::ScopedAStatus setAndCheckBluetoothTxPowerCap(int8_t cap);
36   ndk::ScopedAStatus setAndCheckBluetoothTechBasedTxPowerCap(int8_t br_cap, int8_t edr_cap, int8_t ble_cap);
37   ndk::ScopedAStatus setAndCheckBluetoothModeBasedTxPowerCap(const std::array<uint8_t, 3>& chain_0_cap, const std::array<uint8_t, 3>& chain_1_cap, const std::array<uint8_t, 6>& beamforming_cap);
38   ndk::ScopedAStatus setAndCheckBluetoothModeBasedTxPowerCapPlusHR(const std::array<uint8_t, 4>& chain_0_cap, const std::array<uint8_t, 4>& chain_1_cap, const std::array<uint8_t, 8>& beamforming_cap);
39   ndk::ScopedAStatus setAndCheckBluetoothAreaCode(const std::array<uint8_t, 3>& area_code);
40 
41 private:
42   std::shared_ptr<IBluetoothSar> bluetooth_sar_;
43 };
44 
SetUp()45 void BluetoothSarTest::SetUp() {
46   ALOGI("SetUp Bluetooth SAR Test");
47   bluetooth_sar_ = IBluetoothSar::fromBinder(
48       ndk::SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
49   ASSERT_NE(bluetooth_sar_, nullptr);
50 }
51 
TearDown()52 void BluetoothSarTest::TearDown() {
53   ALOGI("TearDown Bluetooth SAR Test");
54   bluetooth_sar_ = nullptr;
55   ASSERT_EQ(bluetooth_sar_, nullptr);
56 }
57 
setAndCheckBluetoothTxPowerCap(int8_t cap)58 ndk::ScopedAStatus BluetoothSarTest::setAndCheckBluetoothTxPowerCap(int8_t cap) {
59   return bluetooth_sar_->setBluetoothTxPowerCap(cap);
60 }
61 
setAndCheckBluetoothTechBasedTxPowerCap(int8_t br_cap,int8_t edr_cap,int8_t ble_cap)62 ndk::ScopedAStatus BluetoothSarTest::setAndCheckBluetoothTechBasedTxPowerCap(
63     int8_t br_cap, int8_t edr_cap, int8_t ble_cap) {
64   return bluetooth_sar_->setBluetoothTechBasedTxPowerCap(br_cap, edr_cap, ble_cap);
65 }
66 
setAndCheckBluetoothModeBasedTxPowerCap(const std::array<uint8_t,3> & chain_0_cap,const std::array<uint8_t,3> & chain_1_cap,const std::array<uint8_t,6> & beamforming_cap)67 ndk::ScopedAStatus BluetoothSarTest::setAndCheckBluetoothModeBasedTxPowerCap(
68     const std::array<uint8_t, 3>& chain_0_cap,
69     const std::array<uint8_t, 3>& chain_1_cap,
70     const std::array<uint8_t, 6>& beamforming_cap) {
71   return bluetooth_sar_->setBluetoothModeBasedTxPowerCap(chain_0_cap, chain_1_cap, beamforming_cap);
72 }
73 
74 
setAndCheckBluetoothModeBasedTxPowerCapPlusHR(const std::array<uint8_t,4> & chain_0_cap,const std::array<uint8_t,4> & chain_1_cap,const std::array<uint8_t,8> & beamforming_cap)75 ndk::ScopedAStatus BluetoothSarTest::setAndCheckBluetoothModeBasedTxPowerCapPlusHR(
76     const std::array<uint8_t, 4>& chain_0_cap,
77     const std::array<uint8_t, 4>& chain_1_cap,
78     const std::array<uint8_t, 8>& beamforming_cap){
79   return bluetooth_sar_->setBluetoothModeBasedTxPowerCapPlusHR(chain_0_cap, chain_1_cap, beamforming_cap);
80 }
81 
TEST_P(BluetoothSarTest,setAndCheckBluetoothTxPowerCap)82 TEST_P(BluetoothSarTest, setAndCheckBluetoothTxPowerCap) {
83   ndk::ScopedAStatus status = setAndCheckBluetoothTxPowerCap(40);
84   ASSERT_TRUE(status.isOk());
85   // check invalid power cap (greater than 80)
86   status = setAndCheckBluetoothTxPowerCap(100);
87   ASSERT_FALSE(status.isOk());
88 }
89 
90 
TEST_P(BluetoothSarTest,setAndCheckBluetoothTechBasedTxPowerCap)91 TEST_P(BluetoothSarTest, setAndCheckBluetoothTechBasedTxPowerCap) {
92   ndk::ScopedAStatus status = setAndCheckBluetoothTechBasedTxPowerCap(10, 20, 30);
93   ASSERT_TRUE(status.isOk());
94   // check invalid power cap (greater than 80)
95   status = setAndCheckBluetoothTechBasedTxPowerCap(10, 120, 30);
96   ASSERT_FALSE(status.isOk());
97 }
98 
TEST_P(BluetoothSarTest,setAndCheckBluetoothModeBasedTxPowerCap)99 TEST_P(BluetoothSarTest, setAndCheckBluetoothModeBasedTxPowerCap) {
100   std::array<uint8_t, 3> chain_0_cap = {10, 20, 30};
101   std::array<uint8_t, 3> chain_1_cap = {15, 25, 35};
102   std::array<uint8_t, 6> beamforming_cap = {10, 20, 30, 40, 50, 60};
103   ndk::ScopedAStatus status =
104       setAndCheckBluetoothModeBasedTxPowerCap(chain_0_cap, chain_1_cap, beamforming_cap);
105   ASSERT_TRUE(status.isOk());
106   // check invalid power cap (greater than 80)
107   std::array<uint8_t, 3> bad_cap = {15, 125, 35};
108   status = setAndCheckBluetoothModeBasedTxPowerCap(chain_0_cap, bad_cap, beamforming_cap);
109   ASSERT_FALSE(status.isOk());
110 }
111 
TEST_P(BluetoothSarTest,setAndCheckBluetoothModeBasedTxPowerCapPlusHR)112 TEST_P(BluetoothSarTest, setAndCheckBluetoothModeBasedTxPowerCapPlusHR) {
113   std::array<uint8_t, 4> chain_0_cap = {10, 20, 30, 40};
114   std::array<uint8_t, 4> chain_1_cap = {15, 25, 35, 45};
115   std::array<uint8_t, 8> beamforming_cap = {10, 20, 30, 40, 50, 60, 70, 80};
116   ndk::ScopedAStatus status =
117       setAndCheckBluetoothModeBasedTxPowerCapPlusHR(chain_0_cap, chain_1_cap, beamforming_cap);
118   ASSERT_TRUE(status.isOk());
119   // check invalid power cap (greater than 80)
120   std::array<uint8_t, 4> bad_cap = {10, 20, 30, 200};
121   status = setAndCheckBluetoothModeBasedTxPowerCapPlusHR(bad_cap, chain_1_cap, beamforming_cap);
122   ASSERT_FALSE(status.isOk());
123 }
124 
125 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothSarTest);
126 INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothSarTest,
127                          testing::ValuesIn(android::getAidlHalInstanceNames(
128                              IBluetoothSar::descriptor)),
129                          android::PrintInstanceNameToString);
130 
main(int argc,char ** argv)131 int main(int argc, char **argv) {
132   ::testing::InitGoogleTest(&argc, argv);
133   ABinderProcess_startThreadPool();
134   int status = RUN_ALL_TESTS();
135   ALOGI("Test result = %d", status);
136   return status;
137 }
138