Make castv2 performance test work.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_remote_gatt_characteristic_chromeos.h
blob077aff12063447ea123ee770938da55aba946af8
1 // Copyright 2014 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 DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_CHROMEOS_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_CHROMEOS_H_
8 #include <map>
9 #include <queue>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 #include "base/memory/weak_ptr.h"
15 #include "chromeos/dbus/bluetooth_gatt_descriptor_client.h"
16 #include "dbus/object_path.h"
17 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
18 #include "device/bluetooth/bluetooth_uuid.h"
20 namespace device {
22 class BluetoothGattDescriptor;
23 class BluetoothGattService;
25 } // namespace device
27 namespace chromeos {
29 class BluetoothRemoteGattDescriptorChromeOS;
30 class BluetoothRemoteGattServiceChromeOS;
32 // The BluetoothRemoteGattCharacteristicChromeOS class implements
33 // BluetoothGattCharacteristic for remote GATT characteristics on the Chrome OS
34 // platform.
35 class BluetoothRemoteGattCharacteristicChromeOS
36 : public device::BluetoothGattCharacteristic,
37 public BluetoothGattDescriptorClient::Observer {
38 public:
39 // device::BluetoothGattCharacteristic overrides.
40 std::string GetIdentifier() const override;
41 device::BluetoothUUID GetUUID() const override;
42 bool IsLocal() const override;
43 const std::vector<uint8>& GetValue() const override;
44 device::BluetoothGattService* GetService() const override;
45 Properties GetProperties() const override;
46 Permissions GetPermissions() const override;
47 bool IsNotifying() const override;
48 std::vector<device::BluetoothGattDescriptor*> GetDescriptors() const override;
49 device::BluetoothGattDescriptor* GetDescriptor(
50 const std::string& identifier) const override;
51 bool AddDescriptor(device::BluetoothGattDescriptor* descriptor) override;
52 bool UpdateValue(const std::vector<uint8>& value) override;
53 void ReadRemoteCharacteristic(const ValueCallback& callback,
54 const ErrorCallback& error_callback) override;
55 void WriteRemoteCharacteristic(const std::vector<uint8>& new_value,
56 const base::Closure& callback,
57 const ErrorCallback& error_callback) override;
58 void StartNotifySession(const NotifySessionCallback& callback,
59 const ErrorCallback& error_callback) override;
61 // Removes one value update session and invokes |callback| on completion. This
62 // decrements the session reference count by 1 and if the number reaches 0,
63 // makes a call to the subsystem to stop notifications from this
64 // characteristic.
65 void RemoveNotifySession(const base::Closure& callback);
67 // Object path of the underlying D-Bus characteristic.
68 const dbus::ObjectPath& object_path() const { return object_path_; }
70 private:
71 friend class BluetoothRemoteGattServiceChromeOS;
73 BluetoothRemoteGattCharacteristicChromeOS(
74 BluetoothRemoteGattServiceChromeOS* service,
75 const dbus::ObjectPath& object_path);
76 ~BluetoothRemoteGattCharacteristicChromeOS() override;
78 // BluetoothGattDescriptorClient::Observer overrides.
79 void GattDescriptorAdded(const dbus::ObjectPath& object_path) override;
80 void GattDescriptorRemoved(const dbus::ObjectPath& object_path) override;
81 void GattDescriptorPropertyChanged(const dbus::ObjectPath& object_path,
82 const std::string& property_name) override;
84 // Called by dbus:: on unsuccessful completion of a request to read or write
85 // the characteristic value.
86 void OnError(const ErrorCallback& error_callback,
87 const std::string& error_name,
88 const std::string& error_message);
90 // Called by dbus:: on successful completion of a request to start
91 // notifications.
92 void OnStartNotifySuccess(const NotifySessionCallback& callback);
94 // Called by dbus:: on unsuccessful completion of a request to start
95 // notifications.
96 void OnStartNotifyError(const ErrorCallback& error_callback,
97 const std::string& error_name,
98 const std::string& error_message);
100 // Called by dbus:: on successful completion of a request to stop
101 // notifications.
102 void OnStopNotifySuccess(const base::Closure& callback);
104 // Called by dbus:: on unsuccessful completion of a request to stop
105 // notifications.
106 void OnStopNotifyError(const base::Closure& callback,
107 const std::string& error_name,
108 const std::string& error_message);
110 // Calls StartNotifySession for each queued request.
111 void ProcessStartNotifyQueue();
113 // Object path of the D-Bus characteristic object.
114 dbus::ObjectPath object_path_;
116 // The GATT service this GATT characteristic belongs to.
117 BluetoothRemoteGattServiceChromeOS* service_;
119 // The total number of currently active value update sessions.
120 size_t num_notify_sessions_;
122 // Calls to StartNotifySession that are pending. This can happen during the
123 // first remote call to start notifications.
124 typedef std::pair<NotifySessionCallback, ErrorCallback>
125 PendingStartNotifyCall;
126 std::queue<PendingStartNotifyCall> pending_start_notify_calls_;
128 // True, if a Start or Stop notify call to bluetoothd is currently pending.
129 bool notify_call_pending_;
131 // Mapping from GATT descriptor object paths to descriptor objects owned by
132 // this characteristic. Since the Chrome OS implementation uses object paths
133 // as unique identifiers, we also use this mapping to return descriptors by
134 // identifier.
135 typedef std::map<dbus::ObjectPath, BluetoothRemoteGattDescriptorChromeOS*>
136 DescriptorMap;
137 DescriptorMap descriptors_;
139 // Note: This should remain the last member so it'll be destroyed and
140 // invalidate its weak pointers before any other members are destroyed.
141 base::WeakPtrFactory<BluetoothRemoteGattCharacteristicChromeOS>
142 weak_ptr_factory_;
144 DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristicChromeOS);
147 } // namespace chromeos
149 #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_CHROMEOS_H_