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_DEVICE_CLIENT_H_
6 #define CHROMEOS_DBUS_BLUETOOTH_DEVICE_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 // BluetoothDeviceClient is used to communicate with objects representing
22 // remote Bluetooth Devices.
23 class CHROMEOS_EXPORT BluetoothDeviceClient
: public DBusClient
{
25 // Structure of properties associated with bluetooth devices.
26 struct Properties
: public dbus::PropertySet
{
27 // The Bluetooth device address of the device. Read-only.
28 dbus::Property
<std::string
> address
;
30 // The Bluetooth friendly name of the device. Read-only, to give a
31 // different local name, use the |alias| property.
32 dbus::Property
<std::string
> name
;
34 // Proposed icon name for the device according to the freedesktop.org
35 // icon naming specification. Read-only.
36 dbus::Property
<std::string
> icon
;
38 // The Bluetooth class of the device. Read-only.
39 dbus::Property
<uint32
> bluetooth_class
;
41 // The GAP external appearance of the device. Read-only.
42 dbus::Property
<uint16
> appearance
;
44 // Unique numeric identifier for the vendor of the device. Read-only.
45 dbus::Property
<uint16
> vendor
;
47 // List of 128-bit UUIDs that represent the available remote services.
49 dbus::Property
<std::vector
<std::string
> > uuids
;
51 // Transmitted power level. This field is avaliable only for LE devices
52 // that include this field in AD. Read-only.
53 dbus::Property
<int16
> tx_power
;
55 // Indicates that the device is currently paired. Read-only.
56 dbus::Property
<bool> paired
;
58 // Indicates that the device is currently connected. Read-only.
59 dbus::Property
<bool> connected
;
61 // Whether the device is trusted, and connections should be always
62 // accepted and attempted when the device is visible.
63 dbus::Property
<bool> trusted
;
65 // Whether the device is blocked, connections will be always rejected
66 // and the device will not be visible.
67 dbus::Property
<bool> blocked
;
69 // Local alias for the device, if not set, is equal to |name|.
70 dbus::Property
<std::string
> alias
;
72 // Object path of the adapter the device belongs to. Read-only.
73 dbus::Property
<dbus::ObjectPath
> adapter
;
75 // Indicates whether the device is likely to only support pre-2.1
76 // PIN Code pairing rather than 2.1 Secure Simple Pairing, this can
77 // give false positives. Read-only.
78 dbus::Property
<bool> legacy_pairing
;
80 // Remote Device ID information in Linux kernel modalias format. Read-only.
81 dbus::Property
<std::string
> modalias
;
83 // Received signal strength indicator that is set when the device is
84 // discovered during inquiry. Read-only.
85 dbus::Property
<int16
> rssi
;
87 Properties(dbus::ObjectProxy
* object_proxy
,
88 const std::string
& interface_name
,
89 const PropertyChangedCallback
& callback
);
90 ~Properties() override
;
93 // Interface for observing changes from a remote bluetooth device.
96 virtual ~Observer() {}
98 // Called when the remote device with object path |object_path| is added
99 // to the set of known devices.
100 virtual void DeviceAdded(const dbus::ObjectPath
& object_path
) {}
102 // Called when the remote device with object path |object_path| is removed
103 // from the set of known devices.
104 virtual void DeviceRemoved(const dbus::ObjectPath
& object_path
) {}
106 // Called when the device with object path |object_path| has a
107 // change in value of the property named |property_name|.
108 virtual void DevicePropertyChanged(const dbus::ObjectPath
& object_path
,
109 const std::string
& property_name
) {}
112 ~BluetoothDeviceClient() override
;
114 // Adds and removes observers for events on all remote bluetooth
115 // devices. Check the |object_path| parameter of observer methods to
116 // determine which device is issuing the event.
117 virtual void AddObserver(Observer
* observer
) = 0;
118 virtual void RemoveObserver(Observer
* observer
) = 0;
120 // Returns the list of device object paths associated with the given adapter
121 // identified by the D-Bus object path |adapter_path|.
122 virtual std::vector
<dbus::ObjectPath
> GetDevicesForAdapter(
123 const dbus::ObjectPath
& adapter_path
) = 0;
125 // Obtain the properties for the device with object path |object_path|,
126 // any values should be copied if needed.
127 virtual Properties
* GetProperties(const dbus::ObjectPath
& object_path
) = 0;
129 // The ErrorCallback is used by device methods to indicate failure.
130 // It receives two arguments: the name of the error in |error_name| and
131 // an optional message in |error_message|.
132 typedef base::Callback
<void(const std::string
& error_name
,
133 const std::string
& error_message
)> ErrorCallback
;
135 // Connects to the device with object path |object_path|, connecting any
136 // profiles that can be connected to and have been flagged as auto-connected;
137 // may be used to connect additional profiles for an already connected device,
138 // and succeeds if at least one profile is connected.
139 virtual void Connect(const dbus::ObjectPath
& object_path
,
140 const base::Closure
& callback
,
141 const ErrorCallback
& error_callback
) = 0;
143 // Disconnects the device with object path |object_path|, terminating
144 // the low-level ACL connection and any profiles using it.
145 virtual void Disconnect(const dbus::ObjectPath
& object_path
,
146 const base::Closure
& callback
,
147 const ErrorCallback
& error_callback
) = 0;
149 // Connects to the profile |uuid| on the device with object path
150 // |object_path|, provided that the profile has been registered with a
151 // handler on the local device.
152 virtual void ConnectProfile(const dbus::ObjectPath
& object_path
,
153 const std::string
& uuid
,
154 const base::Closure
& callback
,
155 const ErrorCallback
& error_callback
) = 0;
157 // Disconnects from the profile |uuid| on the device with object path
159 virtual void DisconnectProfile(const dbus::ObjectPath
& object_path
,
160 const std::string
& uuid
,
161 const base::Closure
& callback
,
162 const ErrorCallback
& error_callback
) = 0;
164 // Initiates pairing with the device with object path |object_path| and
165 // retrieves all SDP records or GATT primary services. An agent must be
166 // registered to handle the pairing request.
167 virtual void Pair(const dbus::ObjectPath
& object_path
,
168 const base::Closure
& callback
,
169 const ErrorCallback
& error_callback
) = 0;
171 // Cancels an in-progress pairing with the device with object path
172 // |object_path| initiated by Pair().
173 virtual void CancelPairing(const dbus::ObjectPath
& object_path
,
174 const base::Closure
& callback
,
175 const ErrorCallback
& error_callback
) = 0;
177 // The callback invoked for a successful GetConnInfo API call with the
178 // RSSI, TX power, and maximum TX power of the current connection.
179 typedef base::Callback
<void(int16 rssi
,
180 int16 transmit_power
,
181 int16 max_transmit_power
)> ConnInfoCallback
;
183 // Returns the RSSI, TX power, and maximum TX power of a connection to the
184 // device with object path |object_path|. If the device is not connected, then
185 // an error will be returned.
186 virtual void GetConnInfo(const dbus::ObjectPath
& object_path
,
187 const ConnInfoCallback
& callback
,
188 const ErrorCallback
& error_callback
) = 0;
190 // Creates the instance.
191 static BluetoothDeviceClient
* Create();
193 // Constants used to indicate exceptional error conditions.
194 static const char kNoResponseError
[];
195 static const char kUnknownDeviceError
[];
198 BluetoothDeviceClient();
201 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceClient
);
204 } // namespace chromeos
206 #endif // CHROMEOS_DBUS_BLUETOOTH_DEVICE_CLIENT_H_