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_scanning_interface.h"
33 #include "hci/le_security_interface.h"
34 #include "hci/security_interface.h"
35 #include "module.h"
36 #include "os/utils.h"
37 
38 namespace bluetooth {
39 namespace hci {
40 
41 class HciLayer : public Module, public CommandInterface<CommandPacketBuilder> {
42   // LINT.IfChange
43  public:
44   HciLayer();
45   virtual ~HciLayer();
46   DISALLOW_COPY_AND_ASSIGN(HciLayer);
47 
48   void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,
49                       common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override;
50 
51   void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,
52                       common::ContextualOnceCallback<void(CommandStatusView)> on_status) override;
53 
54   virtual common::BidiQueueEnd<AclPacketBuilder, AclPacketView>* GetAclQueueEnd();
55 
56   virtual void RegisterEventHandler(EventCode event_code,
57                                     common::ContextualCallback<void(EventPacketView)> event_handler);
58 
59   virtual void UnregisterEventHandler(EventCode event_code);
60 
61   virtual void RegisterLeEventHandler(SubeventCode subevent_code,
62                                       common::ContextualCallback<void(LeMetaEventView)> event_handler);
63 
64   virtual void UnregisterLeEventHandler(SubeventCode subevent_code);
65 
66   virtual SecurityInterface* GetSecurityInterface(common::ContextualCallback<void(EventPacketView)> event_handler);
67 
68   virtual LeSecurityInterface* GetLeSecurityInterface(common::ContextualCallback<void(LeMetaEventView)> event_handler);
69 
70   virtual AclConnectionInterface* GetAclConnectionInterface(
71       common::ContextualCallback<void(EventPacketView)> event_handler,
72       common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect);
73 
74   virtual LeAclConnectionInterface* GetLeAclConnectionInterface(
75       common::ContextualCallback<void(LeMetaEventView)> event_handler,
76       common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect);
77 
78   virtual LeAdvertisingInterface* GetLeAdvertisingInterface(
79       common::ContextualCallback<void(LeMetaEventView)> event_handler);
80 
81   virtual LeScanningInterface* GetLeScanningInterface(common::ContextualCallback<void(LeMetaEventView)> event_handler);
82 
ToString()83   std::string ToString() const override {
84     return "Hci Layer";
85   }
86 
87   static constexpr std::chrono::milliseconds kHciTimeoutMs = std::chrono::milliseconds(2000);
88 
89   static const ModuleFactory Factory;
90 
91  protected:
92   // Lint.ThenChange(fuzz/fuzz_hci_layer.h)
93   void ListDependencies(ModuleList* list) override;
94 
95   void Start() override;
96 
97   void Stop() override;
98 
99   virtual void Disconnect(uint16_t handle, ErrorCode reason);
100 
101  private:
102   struct impl;
103   struct hal_callbacks;
104   impl* impl_;
105   hal_callbacks* hal_callbacks_;
106 
107   template <typename T>
108   class CommandInterfaceImpl : public CommandInterface<T> {
109    public:
CommandInterfaceImpl(HciLayer & hci)110     explicit CommandInterfaceImpl(HciLayer& hci) : hci_(hci) {}
111     ~CommandInterfaceImpl() override = default;
112 
EnqueueCommand(std::unique_ptr<T> command,common::ContextualOnceCallback<void (CommandCompleteView)> on_complete)113     void EnqueueCommand(std::unique_ptr<T> command,
114                         common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
115       hci_.EnqueueCommand(move(command), std::move(on_complete));
116     }
117 
EnqueueCommand(std::unique_ptr<T> command,common::ContextualOnceCallback<void (CommandStatusView)> on_status)118     void EnqueueCommand(std::unique_ptr<T> command,
119                         common::ContextualOnceCallback<void(CommandStatusView)> on_status) override {
120       hci_.EnqueueCommand(move(command), std::move(on_status));
121     }
122     HciLayer& hci_;
123   };
124 
125   std::list<common::ContextualCallback<void(uint16_t, ErrorCode)>> disconnect_handlers_;
126   void on_disconnection_complete(EventPacketView event_view);
127 
128   // Interfaces
129   CommandInterfaceImpl<ConnectionManagementCommandBuilder> acl_connection_manager_interface_{*this};
130   CommandInterfaceImpl<LeConnectionManagementCommandBuilder> le_acl_connection_manager_interface_{*this};
131   CommandInterfaceImpl<SecurityCommandBuilder> security_interface{*this};
132   CommandInterfaceImpl<LeSecurityCommandBuilder> le_security_interface{*this};
133   CommandInterfaceImpl<LeAdvertisingCommandBuilder> le_advertising_interface{*this};
134   CommandInterfaceImpl<LeScanningCommandBuilder> le_scanning_interface{*this};
135 };
136 }  // namespace hci
137 }  // namespace bluetooth
138