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