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