2 // Copyright (C) 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <base/macros.h>
20 #include <hardware/bluetooth.h>
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.
31 // This is declared as an abstract interface so that a fake implementation can
32 // be injected for testing the upper layer.
34 // TODO: (expose callback types directly but via redirection) methods for
35 // initialize, clean up, and set for testing.
36 class BluetoothInterface {
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.
44 // TODO(armansito): We should fix this in the HAL.
47 virtual ~Observer() = default;
49 // All of the events below correspond to callbacks defined in
50 // "bt_callbacks_t" in the HAL API definitions.
52 virtual void AdapterStateChangedCallback(bt_state_t state) = 0;
53 virtual void AdapterPropertiesCallback(bt_status_t status,
55 bt_property_t* properties) = 0;
57 // TODO(armansito): Complete the list of callbacks.
60 // Initialize and clean up the BluetoothInterface singleton. Returns false if
61 // the underlying HAL interface failed to initialize, and true on success.
62 static bool Initialize();
64 // Shuts down and cleans up the interface. CleanUp must be called on the same
65 // thread that called Initialize.
66 static void CleanUp();
68 // Returns true if the interface was initialized and a global singleton has
70 static bool IsInitialized();
72 // Initialize for testing. Use this to inject a test version of
73 // BlueoothInterface. To be used from unit tests only.
74 static void InitializeForTesting(BluetoothInterface* test_instance);
76 // Returns the BluetoothInterface singleton. If the interface has not been
77 // initialized, returns nullptr.
78 static BluetoothInterface* Get();
80 // Add or remove an observer that is interested in notifications from us.
81 virtual void AddObserver(Observer* observer) = 0;
82 virtual void RemoveObserver(Observer* observer) = 0;
84 // The HAL module pointer that represents the standard Bluetooth adapter
85 // management interface. This is implemented in and provided by the shared
86 // Bluetooth library, so this isn't owned by us.
88 // Upper layers can make bt_interface_t API calls through this structure.
89 // However, DO NOT call the "init" function as this is called and managed by
90 // us. The behavior is undefined if "init" is called directly by upper layers.
91 virtual const bt_interface_t* GetHALInterface() const = 0;
93 // The HAL module pointer that represents the underlying Bluetooth adapter.
94 // This is implemented in and provided by the shared Bluetooth library, so
95 // this isn't owned by us.
96 virtual const bluetooth_device_t* GetHALAdapter() const = 0;
99 BluetoothInterface() = default;
100 virtual ~BluetoothInterface() = default;
103 DISALLOW_COPY_AND_ASSIGN(BluetoothInterface);
107 } // namespace bluetooth