1 /*
2  * Copyright 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/ccc/BnBluetoothCccCallback.h>
20 #include <aidl/hardware/google/bluetooth/ccc/IBluetoothCcc.h>
21 #include <aidl/hardware/google/bluetooth/ccc/IBluetoothCccCallback.h>
22 #include <android-base/logging.h>
23 #include <android/binder_manager.h>
24 #include <android/binder_process.h>
25 #include <binder/IServiceManager.h>
26 #include <utils/Log.h>
27 
28 #include <vector>
29 #include <array>
30 
31 using ::aidl::hardware::google::bluetooth::ccc::IBluetoothCcc;
32 using ::aidl::hardware::google::bluetooth::ccc::IBluetoothCccCallback;
33 using ::aidl::hardware::google::bluetooth::ccc::Direction;
34 using ::aidl::hardware::google::bluetooth::ccc::LmpEventId;
35 using ::aidl::hardware::google::bluetooth::ccc::Timestamp;
36 using ::ndk::ScopedAStatus;
37 
38 class BluetoothCccTest : public ::testing::TestWithParam<std::string> {
39  public:
40   class BluetoothCccCallback
41       : public ::aidl::hardware::google::bluetooth::ccc::BnBluetoothCccCallback {
42    public:
43     BluetoothCccCallback() = default;
44 
onEventGenerated(const Timestamp &,const std::array<uint8_t,6> &,Direction,LmpEventId,char16_t)45     ::ndk::ScopedAStatus onEventGenerated(const Timestamp& /* timestamp */,
46                                           const std::array<uint8_t, 6>& /* address */,
47                                           Direction /* direction */,
48                                           LmpEventId /* lmpEventId */,
49                                           char16_t /* eventCounter */) override {
50         return ::ndk::ScopedAStatus::ok();
51     }
52 
onRegistered(bool)53     ::ndk::ScopedAStatus onRegistered(bool) override {
54         return ::ndk::ScopedAStatus::ok();
55     }
56   };
57 
SetUp()58   virtual void SetUp() override {
59     ALOGI("SetUp Ccc Test");
60     bluetooth_ccc = IBluetoothCcc::fromBinder(
61         ndk::SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
62     ASSERT_NE(bluetooth_ccc, nullptr);
63 
64     ccc_callback = ndk::SharedRefBase::make<BluetoothCccCallback>();
65     ASSERT_NE(ccc_callback, nullptr);
66   }
67 
TearDown()68   virtual void TearDown() override {
69     ALOGI("TearDown Ccc Test");
70     bluetooth_ccc = nullptr;
71     ASSERT_EQ(bluetooth_ccc, nullptr);
72   }
73 
74   // test functions to call
75   ScopedAStatus registerForLmpEvents(std::array<uint8_t, 6>);
76   ScopedAStatus unregisterLmpEvents(std::array<uint8_t, 6>);
77 
78 private:
79   std::shared_ptr<IBluetoothCcc> bluetooth_ccc;
80   std::shared_ptr<BluetoothCccCallback> ccc_callback;
81 };
82 
83 
registerForLmpEvents(std::array<uint8_t,6> addr)84 ScopedAStatus BluetoothCccTest::registerForLmpEvents(std::array<uint8_t, 6> addr) {
85   std::vector<LmpEventId> events = {LmpEventId::CONNECT_IND,
86                                     LmpEventId::LL_PHY_UPDATE_IND};
87   return bluetooth_ccc->registerForLmpEvents(ccc_callback, std::array<uint8_t, 6>(addr), events);
88 }
89 
unregisterLmpEvents(std::array<uint8_t,6> addr)90 ScopedAStatus BluetoothCccTest::unregisterLmpEvents(std::array<uint8_t, 6> addr) {
91   return bluetooth_ccc->unregisterLmpEvents(std::array<uint8_t, 6>(addr));
92 }
93 
TEST_P(BluetoothCccTest,RegisterForLmpEvents)94 TEST_P(BluetoothCccTest, RegisterForLmpEvents) {
95   ALOGI("Run test RegisterForLmpEvents");
96   ScopedAStatus status = registerForLmpEvents(
97           std::array<uint8_t, 6>{0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
98   ASSERT_TRUE(status.isOk());
99 }
100 
TEST_P(BluetoothCccTest,UnregisterLmpEvents)101 TEST_P(BluetoothCccTest, UnregisterLmpEvents) {
102   std::array<uint8_t, 6> addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
103   ScopedAStatus status = registerForLmpEvents(addr);
104   ASSERT_TRUE(status.isOk());
105   status = unregisterLmpEvents(addr);
106   ASSERT_TRUE(status.isOk());
107 }
108 
109 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothCccTest);
110 INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothCccTest,
111                          testing::ValuesIn(android::getAidlHalInstanceNames(
112                              IBluetoothCcc::descriptor)),
113                          android::PrintInstanceNameToString);
114 
main(int argc,char ** argv)115 int main(int argc, char **argv) {
116   ::testing::InitGoogleTest(&argc, argv);
117   ABinderProcess_startThreadPool();
118   int status = RUN_ALL_TESTS();
119   ALOGI("Test result = %d", status);
120   return status;
121 }
122