• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright 2015 Google, Inc.
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 <hardware/bluetooth.h>
20 
21 #include "types/raw_address.h"
22 
23 namespace bluetooth {
24 namespace hal {
25 
26 // This class represents the HAL Bluetooth adapter interface, wrapping around
27 // the underlying bt_interface_t structure, its methods, and callbacks. A single
28 // instance of this class exists per application and it allows multiple classes
29 // to interface with the global HAL interface by multiplexing callbacks among
30 // registered clients.
31 //
32 // This is declared as an abstract interface so that a fake implementation can
33 // be injected for testing the upper layer.
34 //
35 // TODO: (expose callback types directly but via redirection) methods for
36 // initialize, clean up, and set for testing.
37 class BluetoothInterface {
38  public:
39   // The standard Bluetooth adapter management callback interface. The HAL
40   // interface doesn't allow registering "user data" that carries context beyond
41   // the callback parameters, forcing implementations to deal with global
42   // variables. The Observer interface is to redirect these events to interested
43   // parties in an object-oriented manner.
44   //
45   // TODO(armansito): We should fix this in the HAL.
46   class Observer {
47    public:
48     virtual ~Observer() = default;
49 
50     // All of the events below correspond to callbacks defined in
51     // "bt_callbacks_t" in the HAL API definitions.
52 
53     virtual void AdapterStateChangedCallback(bt_state_t state);
54     virtual void AdapterPropertiesCallback(bt_status_t status,
55                                            int num_properties,
56                                            bt_property_t* properties);
57     virtual void RemoteDevicePropertiesCallback(bt_status_t status,
58                                                 RawAddress* remote_bd_addr,
59                                                 int num_properties,
60                                                 bt_property_t* properties);
61     virtual void DeviceFoundCallback(int num_properties,
62                                      bt_property_t* properties);
63     virtual void DiscoveryStateChangedCallback(bt_discovery_state_t state);
64     virtual void PinRequestCallback(RawAddress* remote_bd_addr,
65                                     bt_bdname_t* bd_name, uint32_t cod,
66                                     bool min_16_digit);
67     virtual void SSPRequestCallback(RawAddress* remote_bd_addr,
68                                     bt_bdname_t* bd_name, uint32_t cod,
69                                     bt_ssp_variant_t pairing_variant,
70                                     uint32_t pass_key);
71     virtual void BondStateChangedCallback(bt_status_t status,
72                                           RawAddress* remote_bd_addr,
73                                           bt_bond_state_t state,
74                                           int fail_reason);
75     virtual void AclStateChangedCallback(bt_status_t status,
76                                          const RawAddress& remote_bdaddr,
77                                          bt_acl_state_t state,
78                                          int transport_link_type,
79                                          bt_hci_error_code_t hci_reason);
80     virtual void LinkQualityReportCallback(
81         uint64_t timestamp, int report_id, int rssi, int snr,
82         int retransmission_count, int packets_not_receive_count,
83         int negative_acknowledgement_count);
84 
85     virtual void SwitchBufferSizeCallback(bool is_low_latency_buffer_size);
86     virtual void SwitchCodecCallback(bool is_low_latency_buffer_size);
87 
88     // TODO(armansito): Complete the list of callbacks.
89   };
90 
91   // Initialize and clean up the BluetoothInterface singleton. Returns false if
92   // the underlying HAL interface failed to initialize, and true on success.
93   static bool Initialize();
94 
95   // Shuts down and cleans up the interface. CleanUp must be called on the same
96   // thread that called Initialize.
97   static void CleanUp();
98 
99   // Returns true if the interface was initialized and a global singleton has
100   // been created.
101   static bool IsInitialized();
102 
103   // Initialize for testing. Use this to inject a test version of
104   // BlueoothInterface. To be used from unit tests only.
105   static void InitializeForTesting(BluetoothInterface* test_instance);
106 
107   // Returns the BluetoothInterface singleton. If the interface has not been
108   // initialized, returns nullptr.
109   static BluetoothInterface* Get();
110 
111   // Add or remove an observer that is interested in notifications from us.
112   virtual void AddObserver(Observer* observer) = 0;
113   virtual void RemoveObserver(Observer* observer) = 0;
114 
115   // The HAL module pointer that represents the standard Bluetooth adapter
116   // management interface. This is implemented in and provided by the shared
117   // Bluetooth library, so this isn't owned by us.
118   //
119   // Upper layers can make bt_interface_t API calls through this structure.
120   // However, DO NOT call the "init" function as this is called and managed by
121   // us. The behavior is undefined if "init" is called directly by upper layers.
122   virtual const bt_interface_t* GetHALInterface() const = 0;
123 
124   // Returns the HAL callbacks that have been initialized previously.
125   virtual bt_callbacks_t* GetHALCallbacks() const = 0;
126 
127  protected:
128   BluetoothInterface() = default;
129   BluetoothInterface(const BluetoothInterface&) = delete;
130   BluetoothInterface& operator=(const BluetoothInterface&) = delete;
131 
132   virtual ~BluetoothInterface() = default;
133 };
134 
135 }  // namespace hal
136 }  // namespace bluetooth
137