1 //
2 //  Copyright (C) 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                                                 bt_bdaddr_t* remote_bd_addr,
58                                                 int num_properties,
59                                                 bt_property_t* properties);
60     virtual void DiscoveryStateChangedCallback(bt_discovery_state_t state);
61     virtual void PinRequestCallback(bt_bdaddr_t* remote_bd_addr,
62                                     bt_bdname_t* bd_name, uint32_t cod,
63                                     bool min_16_digit);
64     virtual void SSPRequestCallback(bt_bdaddr_t* remote_bd_addr,
65                                     bt_bdname_t* bd_name, uint32_t cod,
66                                     bt_ssp_variant_t pairing_variant,
67                                     uint32_t pass_key);
68     virtual void BondStateChangedCallback(bt_status_t status,
69                                           bt_bdaddr_t* remote_bd_addr,
70                                           bt_bond_state_t state);
71     virtual void AclStateChangedCallback(bt_status_t status,
72                                          const bt_bdaddr_t& remote_bdaddr,
73                                          bt_acl_state_t state);
74 
75     // TODO(armansito): Complete the list of callbacks.
76   };
77 
78   // Initialize and clean up the BluetoothInterface singleton. Returns false if
79   // the underlying HAL interface failed to initialize, and true on success.
80   static bool Initialize();
81 
82   // Shuts down and cleans up the interface. CleanUp must be called on the same
83   // thread that called Initialize.
84   static void CleanUp();
85 
86   // Returns true if the interface was initialized and a global singleton has
87   // been created.
88   static bool IsInitialized();
89 
90   // Initialize for testing. Use this to inject a test version of
91   // BlueoothInterface. To be used from unit tests only.
92   static void InitializeForTesting(BluetoothInterface* test_instance);
93 
94   // Returns the BluetoothInterface singleton. If the interface has not been
95   // initialized, returns nullptr.
96   static BluetoothInterface* Get();
97 
98   // Add or remove an observer that is interested in notifications from us.
99   virtual void AddObserver(Observer* observer) = 0;
100   virtual void RemoveObserver(Observer* observer) = 0;
101 
102   // The HAL module pointer that represents the standard Bluetooth adapter
103   // management interface. This is implemented in and provided by the shared
104   // Bluetooth library, so this isn't owned by us.
105   //
106   // Upper layers can make bt_interface_t API calls through this structure.
107   // However, DO NOT call the "init" function as this is called and managed by
108   // us. The behavior is undefined if "init" is called directly by upper layers.
109   virtual const bt_interface_t* GetHALInterface() const = 0;
110 
111   // Returns the HAL callbacks that have been initialized previously.
112   virtual bt_callbacks_t* GetHALCallbacks() const = 0;
113 
114   // The HAL module pointer that represents the underlying Bluetooth adapter.
115   // This is implemented in and provided by the shared Bluetooth library, so
116   // this isn't owned by us.
117   virtual const bluetooth_device_t* GetHALAdapter() const = 0;
118 
119  protected:
120   BluetoothInterface() = default;
121   virtual ~BluetoothInterface() = default;
122 
123  private:
124   DISALLOW_COPY_AND_ASSIGN(BluetoothInterface);
125 };
126 
127 }  // namespace hal
128 }  // namespace bluetooth
129