OSDN Git Service

resolved conflicts for b8cc54d1 to mnc-dr-dev-plus-aosp
[android-x86/system-bt.git] / service / hal / bluetooth_interface.h
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) = 0;
53     virtual void AdapterPropertiesCallback(bt_status_t status,
54                                            int num_properties,
55                                            bt_property_t* properties) = 0;
56
57     // TODO(armansito): Complete the list of callbacks.
58   };
59
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();
63
64   // Shuts down and cleans up the interface. CleanUp must be called on the same
65   // thread that called Initialize.
66   static void CleanUp();
67
68   // Returns true if the interface was initialized and a global singleton has
69   // been created.
70   static bool IsInitialized();
71
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);
75
76   // Returns the BluetoothInterface singleton. If the interface has not been
77   // initialized, returns nullptr.
78   static BluetoothInterface* Get();
79
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;
83
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.
87   //
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;
92
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;
97
98  protected:
99   BluetoothInterface() = default;
100   virtual ~BluetoothInterface() = default;
101
102  private:
103   DISALLOW_COPY_AND_ASSIGN(BluetoothInterface);
104 };
105
106 }  // namespace hal
107 }  // namespace bluetooth