OSDN Git Service

am 34421976: am 93c1853f: am 0f2d4897: service: Add new GattServer abstraction
[android-x86/system-bt.git] / service / adapter.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 <atomic>
20 #include <mutex>
21 #include <string>
22
23 #include <base/macros.h>
24 #include <base/observer_list.h>
25
26 #include "service/adapter_state.h"
27 #include "service/gatt_server.h"
28 #include "service/hal/bluetooth_interface.h"
29 #include "service/low_energy_client.h"
30 #include "service/util/atomic_string.h"
31
32 namespace bluetooth {
33
34 // Represents the local Bluetooth adapter.
35 class Adapter : public hal::BluetoothInterface::Observer {
36  public:
37   // The default values returned before the Adapter is fully initialized and
38   // powered. The complete values for these fields are obtained following a
39   // successful call to "Enable".
40   static const char kDefaultAddress[];
41   static const char kDefaultName[];
42
43   // Observer interface allows other classes to receive notifications from us.
44   // All of the methods in this interface are declared as optional to allow
45   // different layers to process only those events that they are interested in.
46   class Observer {
47    public:
48     virtual ~Observer() = default;
49
50     virtual void OnAdapterStateChanged(Adapter* adapter,
51                                        AdapterState prev_state,
52                                        AdapterState new_state);
53   };
54
55   Adapter();
56   ~Adapter() override;
57
58   // Add or remove an observer.
59   void AddObserver(Observer* observer);
60   void RemoveObserver(Observer* observer);
61
62   // Returns the current Adapter state.
63   AdapterState GetState() const;
64
65   // Returns true, if the adapter radio is current powered.
66   bool IsEnabled() const;
67
68   // Enables Bluetooth. This method will send a request to the Bluetooth adapter
69   // to power up its radio. Returns true, if the request was successfully sent
70   // to the controller, otherwise returns false. A successful call to this
71   // method only means that the enable request has been sent to the Bluetooth
72   // controller and does not imply that the operation itself succeeded.
73   bool Enable();
74
75   // Powers off the Bluetooth radio. Returns true, if the disable request was
76   // successfully sent to the Bluetooth controller.
77   bool Disable();
78
79   // Returns the name currently assigned to the local adapter.
80   std::string GetName() const;
81
82   // Sets the name assigned to the local Bluetooth adapter. This is the name
83   // that the local controller will present to remote devices.
84   bool SetName(const std::string& name);
85
86   // Returns the local adapter addess in string form (XX:XX:XX:XX:XX:XX).
87   std::string GetAddress() const;
88
89   // Returns true if the local adapter supports the Low-Energy
90   // multi-advertisement feature.
91   bool IsMultiAdvertisementSupported() const;
92
93   // Returns a pointer to the LowEnergyClientFactory. This can be used to
94   // register per-application LowEnergyClient instances to perform BLE GAP
95   // operations.
96   LowEnergyClientFactory* GetLowEnergyClientFactory() const;
97
98   // Returns a pointer to the GattServerFactory. This can be used to register
99   // per-application GATT server instances.
100   GattServerFactory* GetGattServerFactory() const;
101
102  private:
103   // hal::BluetoothInterface::Observer overrides.
104   void AdapterStateChangedCallback(bt_state_t state) override;
105   void AdapterPropertiesCallback(bt_status_t status,
106                                  int num_properties,
107                                  bt_property_t* properties) override;
108
109   // Sends a request to set the given HAL adapter property type and value.
110   bool SetAdapterProperty(bt_property_type_t type, void* value, int length);
111
112   // Helper for invoking observer method.
113   void NotifyAdapterStateChanged(AdapterState prev_state,
114                                  AdapterState new_state);
115
116   // The current adapter state.
117   std::atomic<AdapterState> state_;
118
119   // The Bluetooth device address of the local adapter in string from
120   // (i.e.. XX:XX:XX:XX:XX:XX)
121   util::AtomicString address_;
122
123   // The current local adapter name.
124   util::AtomicString name_;
125
126   // The current set of supported LE features as obtained from the stack. The
127   // values here are all initially set to 0 and updated when the corresponding
128   // adapter property has been received from the stack.
129   bt_local_le_features_t local_le_features_;
130
131   // List of observers that are interested in notifications from us.
132   std::mutex observers_lock_;
133   base::ObserverList<Observer> observers_;
134
135   // Factory used to create per-app LowEnergyClient instances.
136   std::unique_ptr<LowEnergyClientFactory> ble_client_factory_;
137
138   // Factory used to create per-app GattServer instances.
139   std::unique_ptr<GattServerFactory> gatt_server_factory_;
140
141   DISALLOW_COPY_AND_ASSIGN(Adapter);
142 };
143
144 }  // namespace bluetooth