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