• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 #pragma once
18 
19 #include <chrono>
20 #include <list>
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 
26 #include "address.h"
27 #include "class_of_device.h"
28 #include "common/bidi_queue.h"
29 #include "common/callback.h"
30 #include "common/contextual_callback.h"
31 #include "hal/hci_hal.h"
32 #include "hci/acl_connection_interface.h"
33 #include "hci/hci_packets.h"
34 #include "hci/le_acl_connection_interface.h"
35 #include "hci/le_advertising_interface.h"
36 #include "hci/le_iso_interface.h"
37 #include "hci/le_scanning_interface.h"
38 #include "hci/le_security_interface.h"
39 #include "hci/security_interface.h"
40 #include "module.h"
41 #include "os/utils.h"
42 
43 namespace bluetooth {
44 namespace hci {
45 
46 class HciLayer : public Module, public CommandInterface<CommandBuilder> {
47   // LINT.IfChange
48  public:
49   HciLayer();
50   HciLayer(const HciLayer&) = delete;
51   HciLayer& operator=(const HciLayer&) = delete;
52 
53   virtual ~HciLayer();
54 
55   void EnqueueCommand(
56       std::unique_ptr<CommandBuilder> command,
57       common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override;
58 
59   void EnqueueCommand(
60       std::unique_ptr<CommandBuilder> command,
61       common::ContextualOnceCallback<void(CommandStatusView)> on_status) override;
62 
63   virtual common::BidiQueueEnd<AclBuilder, AclView>* GetAclQueueEnd();
64 
65   virtual common::BidiQueueEnd<ScoBuilder, ScoView>* GetScoQueueEnd();
66 
67   virtual common::BidiQueueEnd<IsoBuilder, IsoView>* GetIsoQueueEnd();
68 
69   virtual void RegisterEventHandler(EventCode event_code, common::ContextualCallback<void(EventView)> event_handler);
70 
71   virtual void UnregisterEventHandler(EventCode event_code);
72 
73   virtual void RegisterLeEventHandler(SubeventCode subevent_code,
74                                       common::ContextualCallback<void(LeMetaEventView)> event_handler);
75 
76   virtual void UnregisterLeEventHandler(SubeventCode subevent_code);
77 
78   virtual SecurityInterface* GetSecurityInterface(common::ContextualCallback<void(EventView)> event_handler);
79 
80   virtual LeSecurityInterface* GetLeSecurityInterface(common::ContextualCallback<void(LeMetaEventView)> event_handler);
81 
82   virtual AclConnectionInterface* GetAclConnectionInterface(
83       common::ContextualCallback<void(EventView)> event_handler,
84       common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect,
85       common::ContextualCallback<void(hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t)>
86           on_read_remote_version_complete);
87   virtual void PutAclConnectionInterface();
88 
89   virtual LeAclConnectionInterface* GetLeAclConnectionInterface(
90       common::ContextualCallback<void(LeMetaEventView)> event_handler,
91       common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect,
92       common::ContextualCallback<void(hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t)>
93           on_read_remote_version_complete);
94   virtual void PutLeAclConnectionInterface();
95 
96   virtual LeAdvertisingInterface* GetLeAdvertisingInterface(
97       common::ContextualCallback<void(LeMetaEventView)> event_handler);
98 
99   virtual LeScanningInterface* GetLeScanningInterface(common::ContextualCallback<void(LeMetaEventView)> event_handler);
100 
101   virtual LeIsoInterface* GetLeIsoInterface(common::ContextualCallback<void(LeMetaEventView)> event_handler);
102 
ToString()103   std::string ToString() const override {
104     return "Hci Layer";
105   }
106 
107   static constexpr std::chrono::milliseconds kHciTimeoutMs = std::chrono::milliseconds(2000);
108   static constexpr std::chrono::milliseconds kHciTimeoutRestartMs = std::chrono::milliseconds(5000);
109 
110   static const ModuleFactory Factory;
111 
112  protected:
113   // LINT.ThenChange(fuzz/fuzz_hci_layer.h)
114   void ListDependencies(ModuleList* list) const override;
115 
116   void Start() override;
117 
118   void Stop() override;
119 
120   virtual void Disconnect(uint16_t handle, ErrorCode reason);
121   virtual void ReadRemoteVersion(
122       hci::ErrorCode hci_status,
123       uint16_t handle,
124       uint8_t version,
125       uint16_t manufacturer_name,
126       uint16_t sub_version);
127 
128   std::list<common::ContextualCallback<void(uint16_t, ErrorCode)>> disconnect_handlers_;
129   std::list<common::ContextualCallback<void(hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t)>>
130       read_remote_version_handlers_;
131 
132  private:
133   struct impl;
134   struct hal_callbacks;
135   impl* impl_;
136   hal_callbacks* hal_callbacks_;
137 
138   template <typename T>
139   class CommandInterfaceImpl : public CommandInterface<T> {
140    public:
CommandInterfaceImpl(HciLayer & hci)141     explicit CommandInterfaceImpl(HciLayer& hci) : hci_(hci) {}
142     ~CommandInterfaceImpl() = default;
143 
EnqueueCommand(std::unique_ptr<T> command,common::ContextualOnceCallback<void (CommandCompleteView)> on_complete)144     void EnqueueCommand(std::unique_ptr<T> command,
145                         common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
146       hci_.EnqueueCommand(std::move(command), std::move(on_complete));
147     }
148 
EnqueueCommand(std::unique_ptr<T> command,common::ContextualOnceCallback<void (CommandStatusView)> on_status)149     void EnqueueCommand(std::unique_ptr<T> command,
150                         common::ContextualOnceCallback<void(CommandStatusView)> on_status) override {
151       hci_.EnqueueCommand(std::move(command), std::move(on_status));
152     }
153     HciLayer& hci_;
154   };
155 
156   std::mutex callback_handlers_guard_;
157   void on_disconnection_complete(EventView event_view);
158   void on_read_remote_version_complete(EventView event_view);
159 
160   // Interfaces
161   CommandInterfaceImpl<AclCommandBuilder> acl_connection_manager_interface_{*this};
162   CommandInterfaceImpl<AclCommandBuilder> le_acl_connection_manager_interface_{*this};
163   CommandInterfaceImpl<SecurityCommandBuilder> security_interface{*this};
164   CommandInterfaceImpl<LeSecurityCommandBuilder> le_security_interface{*this};
165   CommandInterfaceImpl<LeAdvertisingCommandBuilder> le_advertising_interface{*this};
166   CommandInterfaceImpl<LeScanningCommandBuilder> le_scanning_interface{*this};
167   CommandInterfaceImpl<LeIsoCommandBuilder> le_iso_interface{*this};
168 };
169 }  // namespace hci
170 }  // namespace bluetooth
171