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 #include <aidl/Gtest.h>
17 #include <aidl/Vintf.h>
18 #include <aidl/hardware/google/bluetooth/ext/IBluetoothExt.h>
19 #include <android/binder_auto_utils.h>
20 #include <android/binder_manager.h>
21 #include <android/binder_process.h>
22 #include <binder/IServiceManager.h>
23 #include <binder/ProcessState.h>
24 
25 using aidl::hardware::google::bluetooth::ext::IBluetoothExt;
26 using ndk::ScopedAStatus;
27 using ndk::SpAIBinder;
28 
29 class BluetoothExtTest : public ::testing::TestWithParam<std::string> {
30 public:
31   virtual void SetUp() override;
32   virtual void TearDown() override;
33 
34   ndk::ScopedAStatus setBluetoothCmdPacket(uint16_t opcode, const std::vector<uint8_t>& params, bool* ret_ptr);
35 
36 private:
37   std::shared_ptr<IBluetoothExt> bluetooth_ext_;
38 };
39 
SetUp()40 void BluetoothExtTest::SetUp() {
41   ALOGI("SetUp Bluetooth Ext Test");
42   bluetooth_ext_ = IBluetoothExt::fromBinder(
43       ndk::SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
44   ASSERT_NE(bluetooth_ext_, nullptr);
45 }
46 
TearDown()47 void BluetoothExtTest::TearDown() {
48   ALOGI("Tear Down Bluetooth Ext Test");
49   bluetooth_ext_ = nullptr;
50   ASSERT_EQ(bluetooth_ext_, nullptr);
51 }
52 
setBluetoothCmdPacket(uint16_t opcode,const std::vector<uint8_t> & params,bool * ret_ptr)53 ndk::ScopedAStatus BluetoothExtTest::setBluetoothCmdPacket(uint16_t opcode, const std::vector<uint8_t>& params, bool* ret_ptr) {
54   ndk::ScopedAStatus status = bluetooth_ext_->setBluetoothCmdPacket(opcode, params, ret_ptr);
55   if (!(*ret_ptr)) {
56     return ndk::ScopedAStatus::fromServiceSpecificError(STATUS_BAD_VALUE);
57   }
58   return status;
59 }
60 
TEST_P(BluetoothExtTest,setBluetoothCmdPacket)61 TEST_P(BluetoothExtTest, setBluetoothCmdPacket) {
62   uint16_t opcode = 0xfe20;
63   std::vector<uint8_t> params = {0x20, 0xfe, 0x08, 0x14, 0x01, 0xff, 0x00, 0x10, 0x00, 0x01, 0x00};
64   bool is_valid_packet = false;
65   ndk::ScopedAStatus status = setBluetoothCmdPacket(opcode, params, &is_valid_packet);
66   ASSERT_TRUE(is_valid_packet);
67   ASSERT_TRUE(status.isOk());
68 }
69 
70 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothExtTest);
71 INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothExtTest,
72                          testing::ValuesIn(android::getAidlHalInstanceNames(
73                              IBluetoothExt::descriptor)),
74                          android::PrintInstanceNameToString);
75 
main(int argc,char ** argv)76 int main(int argc, char **argv) {
77   ::testing::InitGoogleTest(&argc, argv);
78   ABinderProcess_startThreadPool();
79   int status = RUN_ALL_TESTS();
80   ALOGI("Test result = %d", status);
81   return status;
82 }
83 
84