• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 <gtest/gtest.h>
18 
19 #include <chrono>
20 #include <queue>
21 #include <thread>
22 
23 #include "com_android_bluetooth_flags.h"
24 #include "hal/hci_backend.h"
25 #include "hal/hci_hal.h"
26 #include "os/thread.h"
27 
28 using ::bluetooth::os::Thread;
29 
30 namespace bluetooth::hal {
31 
32 class TestBackend : public HciBackend {
33 public:
34   static std::chrono::milliseconds initialization_delay;
35 
36   std::shared_ptr<HciBackendCallbacks> callbacks;
37   struct {
38     std::queue<std::vector<uint8_t>> cmd, acl, sco, iso;
39   } queues;
40 
initialize(std::shared_ptr<HciBackendCallbacks> callbacks)41   void initialize(std::shared_ptr<HciBackendCallbacks> callbacks) override {
42     this->callbacks = callbacks;
43     std::thread(
44             [callbacks](std::chrono::milliseconds delay) {
45               std::this_thread::sleep_for(delay);
46               callbacks->initializationComplete();
47             },
48             TestBackend::initialization_delay)
49             .detach();
50   }
51 
sendHciCommand(const std::vector<uint8_t> & command)52   void sendHciCommand(const std::vector<uint8_t>& command) override { queues.cmd.push(command); }
sendAclData(const std::vector<uint8_t> & packet)53   void sendAclData(const std::vector<uint8_t>& packet) override { queues.acl.push(packet); }
sendScoData(const std::vector<uint8_t> & packet)54   void sendScoData(const std::vector<uint8_t>& packet) override { queues.sco.push(packet); }
sendIsoData(const std::vector<uint8_t> & packet)55   void sendIsoData(const std::vector<uint8_t>& packet) override { queues.iso.push(packet); }
56 };
57 
58 std::shared_ptr<TestBackend> backend;
59 std::chrono::milliseconds TestBackend::initialization_delay = std::chrono::milliseconds(0);
60 
CreateAidl()61 std::shared_ptr<HciBackend> HciBackend::CreateAidl() {
62   backend = std::make_shared<TestBackend>();
63   return backend;
64 }
65 
CreateHidl(::bluetooth::os::Handler * handler)66 std::shared_ptr<HciBackend> HciBackend::CreateHidl(
67         [[maybe_unused]] ::bluetooth::os::Handler* handler) {
68   backend = std::make_shared<TestBackend>();
69   return backend;
70 }
71 
72 namespace {
73 
74 class HciHalAndroidTest : public ::testing::Test {
75 protected:
SetUp()76   void SetUp() override {
77     thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
78     handler_ = new os::Handler(thread_);
79     hal = fake_registry_.Start<HciHal>(thread_, handler_);
80   }
81 
TearDown()82   void TearDown() override {
83     handler_->Clear();
84     if (com::android::bluetooth::flags::same_handler_for_all_modules()) {
85       handler_->WaitUntilStopped(bluetooth::kHandlerStopTimeout);
86     }
87     fake_registry_.StopAll();
88     delete handler_;
89     delete thread_;
90   }
91 
92   HciHal* hal;
93 
94 private:
95   ModuleRegistry fake_registry_;
96   Thread* thread_;
97   os::Handler* handler_;
98 };
99 
TEST_F(HciHalAndroidTest,init)100 TEST_F(HciHalAndroidTest, init) {
101   TearDown();
102 
103   TestBackend::initialization_delay = std::chrono::milliseconds(100);
104   const auto t0 = std::chrono::steady_clock::now();
105   SetUp();
106   const auto t1 = std::chrono::steady_clock::now();
107 
108   EXPECT_GE(t1 - t0, TestBackend::initialization_delay);
109   TestBackend::initialization_delay = std::chrono::milliseconds(0);
110 }
111 
112 }  // namespace
113 }  // namespace bluetooth::hal
114