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