1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROMEOS_DBUS_BLUETOOTH_ADAPTER_CLIENT_H_
6 #define CHROMEOS_DBUS_BLUETOOTH_ADAPTER_CLIENT_H_
11 #include "base/callback.h"
12 #include "base/observer_list.h"
13 #include "base/values.h"
14 #include "chromeos/chromeos_export.h"
15 #include "chromeos/dbus/dbus_client.h"
16 #include "dbus/object_path.h"
17 #include "dbus/property.h"
21 // BluetoothAdapterClient is used to communicate with objects representing
22 // local Bluetooth Adapters.
23 class CHROMEOS_EXPORT BluetoothAdapterClient
: public DBusClient
{
25 // A DiscoveryFilter represents a filter passed to the SetDiscoveryFilter
27 struct DiscoveryFilter
{
31 // Copy content of |filter| into this filter
32 void CopyFrom(const DiscoveryFilter
& filter
);
34 scoped_ptr
<std::vector
<std::string
>> uuids
;
35 scoped_ptr
<int16_t> rssi
;
36 scoped_ptr
<uint16_t> pathloss
;
37 scoped_ptr
<std::string
> transport
;
39 DISALLOW_COPY_AND_ASSIGN(DiscoveryFilter
);
42 // Structure of properties associated with bluetooth adapters.
43 struct Properties
: public dbus::PropertySet
{
44 // The Bluetooth device address of the adapter. Read-only.
45 dbus::Property
<std::string
> address
;
47 // The Bluetooth system name, generally derived from the hostname.
48 dbus::Property
<std::string
> name
;
50 // The Bluetooth friendly name of the adapter, unlike remote devices,
51 // this property can be changed to change the presentation for when
52 // the adapter is discoverable.
53 dbus::Property
<std::string
> alias
;
55 // The Bluetooth class of the adapter device. Read-only.
56 dbus::Property
<uint32
> bluetooth_class
;
58 // Whether the adapter radio is powered.
59 dbus::Property
<bool> powered
;
61 // Whether the adapter is discoverable by other Bluetooth devices.
62 // |discovering_timeout| is used to automatically disable after a time
64 dbus::Property
<bool> discoverable
;
66 // Whether the adapter accepts incoming pairing requests from other
67 // Bluetooth devices. |pairable_timeout| is used to automatically disable
68 // after a time period.
69 dbus::Property
<bool> pairable
;
71 // The timeout in seconds to cease accepting incoming pairing requests
72 // after |pairable| is set to true. Zero means adapter remains pairable
74 dbus::Property
<uint32
> pairable_timeout
;
76 // The timeout in seconds to cease the adapter being discoverable by
77 // other Bluetooth devices after |discoverable| is set to true. Zero
78 // means adapter remains discoverable forever.
79 dbus::Property
<uint32
> discoverable_timeout
;
81 // Indicates that the adapter is discovering other Bluetooth Devices.
82 // Read-only. Use StartDiscovery() to begin discovery.
83 dbus::Property
<bool> discovering
;
85 // List of 128-bit UUIDs that represent the available local services.
87 dbus::Property
<std::vector
<std::string
> > uuids
;
89 // Local Device ID information in Linux kernel modalias format. Read-only.
90 dbus::Property
<std::string
> modalias
;
92 Properties(dbus::ObjectProxy
* object_proxy
,
93 const std::string
& interface_name
,
94 const PropertyChangedCallback
& callback
);
95 ~Properties() override
;
98 // Interface for observing changes from a local bluetooth adapter.
101 virtual ~Observer() {}
103 // Called when the adapter with object path |object_path| is added to the
105 virtual void AdapterAdded(const dbus::ObjectPath
& object_path
) {}
107 // Called when the adapter with object path |object_path| is removed from
109 virtual void AdapterRemoved(const dbus::ObjectPath
& object_path
) {}
111 // Called when the adapter with object path |object_path| has a
112 // change in value of the property named |property_name|.
113 virtual void AdapterPropertyChanged(const dbus::ObjectPath
& object_path
,
114 const std::string
& property_name
) {}
117 ~BluetoothAdapterClient() override
;
119 // Adds and removes observers for events on all local bluetooth
120 // adapters. Check the |object_path| parameter of observer methods to
121 // determine which adapter is issuing the event.
122 virtual void AddObserver(Observer
* observer
) = 0;
123 virtual void RemoveObserver(Observer
* observer
) = 0;
125 // Returns the list of adapter object paths known to the system.
126 virtual std::vector
<dbus::ObjectPath
> GetAdapters() = 0;
128 // Obtain the properties for the adapter with object path |object_path|,
129 // any values should be copied if needed.
130 virtual Properties
* GetProperties(const dbus::ObjectPath
& object_path
) = 0;
132 // The ErrorCallback is used by adapter methods to indicate failure.
133 // It receives two arguments: the name of the error in |error_name| and
134 // an optional message in |error_message|.
135 typedef base::Callback
<void(const std::string
& error_name
,
136 const std::string
& error_message
)> ErrorCallback
;
138 // Starts a device discovery on the adapter with object path |object_path|.
139 virtual void StartDiscovery(const dbus::ObjectPath
& object_path
,
140 const base::Closure
& callback
,
141 const ErrorCallback
& error_callback
) = 0;
143 // Cancels any previous device discovery on the adapter with object path
145 virtual void StopDiscovery(const dbus::ObjectPath
& object_path
,
146 const base::Closure
& callback
,
147 const ErrorCallback
& error_callback
) = 0;
149 // Removes from the adapter with object path |object_path| the remote
150 // device with object path |object_path| from the list of known devices
151 // and discards any pairing information.
152 virtual void RemoveDevice(const dbus::ObjectPath
& object_path
,
153 const dbus::ObjectPath
& device_path
,
154 const base::Closure
& callback
,
155 const ErrorCallback
& error_callback
) = 0;
157 // Sets the device discovery filter on the adapter with object path
158 // |object_path|. When this method is called with no filter parameter, filter
160 // SetDiscoveryFilter can be called before StartDiscovery. It is useful when
161 // client will create first discovery session, to ensure that proper scan
162 // will be started right after call to StartDiscovery.
163 virtual void SetDiscoveryFilter(const dbus::ObjectPath
& object_path
,
164 const DiscoveryFilter
& discovery_filter
,
165 const base::Closure
& callback
,
166 const ErrorCallback
& error_callback
) = 0;
168 // Creates the instance.
169 static BluetoothAdapterClient
* Create();
171 // Constants used to indicate exceptional error conditions.
172 static const char kNoResponseError
[];
173 static const char kUnknownAdapterError
[];
176 BluetoothAdapterClient();
179 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClient
);
182 } // namespace chromeos
184 #endif // CHROMEOS_DBUS_BLUETOOTH_ADAPTER_CLIENT_H_