forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth_remote_gatt_service_bluez.cc
257 lines (218 loc) · 9.2 KB
/
bluetooth_remote_gatt_service_bluez.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <iterator>
#include "base/logging.h"
#include "dbus/property.h"
#include "device/bluetooth/bluez/bluetooth_adapter_bluez.h"
#include "device/bluetooth/bluez/bluetooth_device_bluez.h"
#include "device/bluetooth/bluez/bluetooth_gatt_characteristic_bluez.h"
#include "device/bluetooth/bluez/bluetooth_remote_gatt_characteristic_bluez.h"
#include "device/bluetooth/bluez/bluetooth_remote_gatt_descriptor_bluez.h"
#include "device/bluetooth/bluez/bluetooth_remote_gatt_service_bluez.h"
#include "device/bluetooth/dbus/bluez_dbus_manager.h"
namespace bluez {
BluetoothRemoteGattServiceBlueZ::BluetoothRemoteGattServiceBlueZ(
BluetoothAdapterBlueZ* adapter,
BluetoothDeviceBlueZ* device,
const dbus::ObjectPath& object_path)
: BluetoothGattServiceBlueZ(adapter, object_path),
device_(device),
weak_ptr_factory_(this) {
VLOG(1) << "Creating remote GATT service with identifier: "
<< object_path.value();
DCHECK(GetAdapter());
bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()->AddObserver(
this);
bluez::BluezDBusManager::Get()
->GetBluetoothGattCharacteristicClient()
->AddObserver(this);
// Add all known GATT characteristics.
const std::vector<dbus::ObjectPath>& gatt_chars =
bluez::BluezDBusManager::Get()
->GetBluetoothGattCharacteristicClient()
->GetCharacteristics();
for (std::vector<dbus::ObjectPath>::const_iterator iter = gatt_chars.begin();
iter != gatt_chars.end(); ++iter)
GattCharacteristicAdded(*iter);
}
BluetoothRemoteGattServiceBlueZ::~BluetoothRemoteGattServiceBlueZ() {
bluez::BluezDBusManager::Get()
->GetBluetoothGattServiceClient()
->RemoveObserver(this);
bluez::BluezDBusManager::Get()
->GetBluetoothGattCharacteristicClient()
->RemoveObserver(this);
// Clean up all the characteristics. Copy the characteristics list here and
// clear the original so that when we send GattCharacteristicRemoved(),
// GetCharacteristics() returns no characteristics.
CharacteristicMap characteristics = characteristics_;
characteristics_.clear();
for (CharacteristicMap::iterator iter = characteristics.begin();
iter != characteristics.end(); ++iter) {
DCHECK(GetAdapter());
GetAdapter()->NotifyGattCharacteristicRemoved(iter->second);
delete iter->second;
}
}
device::BluetoothUUID BluetoothRemoteGattServiceBlueZ::GetUUID() const {
bluez::BluetoothGattServiceClient::Properties* properties =
bluez::BluezDBusManager::Get()
->GetBluetoothGattServiceClient()
->GetProperties(object_path());
DCHECK(properties);
return device::BluetoothUUID(properties->uuid.value());
}
bool BluetoothRemoteGattServiceBlueZ::IsPrimary() const {
bluez::BluetoothGattServiceClient::Properties* properties =
bluez::BluezDBusManager::Get()
->GetBluetoothGattServiceClient()
->GetProperties(object_path());
DCHECK(properties);
return properties->primary.value();
}
device::BluetoothDevice* BluetoothRemoteGattServiceBlueZ::GetDevice() const {
return device_;
}
std::vector<device::BluetoothRemoteGattCharacteristic*>
BluetoothRemoteGattServiceBlueZ::GetCharacteristics() const {
std::vector<device::BluetoothRemoteGattCharacteristic*> characteristics;
for (CharacteristicMap::const_iterator iter = characteristics_.begin();
iter != characteristics_.end(); ++iter) {
characteristics.push_back(iter->second);
}
return characteristics;
}
std::vector<device::BluetoothRemoteGattService*>
BluetoothRemoteGattServiceBlueZ::GetIncludedServices() const {
// TODO(armansito): Return the actual included services here.
return std::vector<device::BluetoothRemoteGattService*>();
}
device::BluetoothRemoteGattCharacteristic*
BluetoothRemoteGattServiceBlueZ::GetCharacteristic(
const std::string& identifier) const {
CharacteristicMap::const_iterator iter =
characteristics_.find(dbus::ObjectPath(identifier));
if (iter == characteristics_.end())
return nullptr;
return iter->second;
}
void BluetoothRemoteGattServiceBlueZ::NotifyServiceChanged() {
// Don't send service changed unless we know that all characteristics have
// already been discovered. This is to prevent spammy events before sending
// out the first Gatt
if (!device_->IsGattServicesDiscoveryComplete())
return;
DCHECK(GetAdapter());
GetAdapter()->NotifyGattServiceChanged(this);
}
void BluetoothRemoteGattServiceBlueZ::NotifyDescriptorAddedOrRemoved(
BluetoothRemoteGattCharacteristicBlueZ* characteristic,
BluetoothRemoteGattDescriptorBlueZ* descriptor,
bool added) {
DCHECK(characteristic->GetService() == this);
DCHECK(descriptor->GetCharacteristic() == characteristic);
DCHECK(GetAdapter());
if (added) {
GetAdapter()->NotifyGattDescriptorAdded(descriptor);
return;
}
GetAdapter()->NotifyGattDescriptorRemoved(descriptor);
}
void BluetoothRemoteGattServiceBlueZ::NotifyDescriptorValueChanged(
BluetoothRemoteGattCharacteristicBlueZ* characteristic,
BluetoothRemoteGattDescriptorBlueZ* descriptor,
const std::vector<uint8_t>& value) {
DCHECK(characteristic->GetService() == this);
DCHECK(descriptor->GetCharacteristic() == characteristic);
DCHECK(GetAdapter());
GetAdapter()->NotifyGattDescriptorValueChanged(descriptor, value);
}
void BluetoothRemoteGattServiceBlueZ::GattServicePropertyChanged(
const dbus::ObjectPath& object_path,
const std::string& property_name) {
if (object_path != this->object_path())
return;
VLOG(1) << "Service property changed: \"" << property_name << "\", "
<< object_path.value();
bluez::BluetoothGattServiceClient::Properties* properties =
bluez::BluezDBusManager::Get()
->GetBluetoothGattServiceClient()
->GetProperties(object_path);
DCHECK(properties);
NotifyServiceChanged();
}
void BluetoothRemoteGattServiceBlueZ::GattCharacteristicAdded(
const dbus::ObjectPath& object_path) {
if (characteristics_.find(object_path) != characteristics_.end()) {
VLOG(1) << "Remote GATT characteristic already exists: "
<< object_path.value();
return;
}
bluez::BluetoothGattCharacteristicClient::Properties* properties =
bluez::BluezDBusManager::Get()
->GetBluetoothGattCharacteristicClient()
->GetProperties(object_path);
DCHECK(properties);
if (properties->service.value() != this->object_path()) {
VLOG(2) << "Remote GATT characteristic does not belong to this service.";
return;
}
VLOG(1) << "Adding new remote GATT characteristic for GATT service: "
<< GetIdentifier() << ", UUID: " << GetUUID().canonical_value();
BluetoothRemoteGattCharacteristicBlueZ* characteristic =
new BluetoothRemoteGattCharacteristicBlueZ(this, object_path);
characteristics_[object_path] = characteristic;
DCHECK(characteristic->GetIdentifier() == object_path.value());
DCHECK(characteristic->GetUUID().IsValid());
DCHECK(GetAdapter());
GetAdapter()->NotifyGattCharacteristicAdded(characteristic);
}
void BluetoothRemoteGattServiceBlueZ::GattCharacteristicRemoved(
const dbus::ObjectPath& object_path) {
CharacteristicMap::iterator iter = characteristics_.find(object_path);
if (iter == characteristics_.end()) {
VLOG(2) << "Unknown GATT characteristic removed: " << object_path.value();
return;
}
VLOG(1) << "Removing remote GATT characteristic from service: "
<< GetIdentifier() << ", UUID: " << GetUUID().canonical_value();
BluetoothRemoteGattCharacteristicBlueZ* characteristic = iter->second;
DCHECK(characteristic->object_path() == object_path);
characteristics_.erase(iter);
DCHECK(GetAdapter());
GetAdapter()->NotifyGattCharacteristicRemoved(characteristic);
delete characteristic;
}
void BluetoothRemoteGattServiceBlueZ::GattCharacteristicPropertyChanged(
const dbus::ObjectPath& object_path,
const std::string& property_name) {
CharacteristicMap::iterator iter = characteristics_.find(object_path);
if (iter == characteristics_.end()) {
VLOG(3) << "Properties of unknown characteristic changed";
return;
}
// We may receive a property changed event in certain cases, e.g. when the
// characteristic "Flags" property has been updated with values from the
// "Characteristic Extended Properties" descriptor. In this case, kick off
// a service changed observer event to let observers refresh the
// characteristics.
bluez::BluetoothGattCharacteristicClient::Properties* properties =
bluez::BluezDBusManager::Get()
->GetBluetoothGattCharacteristicClient()
->GetProperties(object_path);
DCHECK(properties);
DCHECK(GetAdapter());
if (property_name == properties->flags.name())
NotifyServiceChanged();
else if (property_name == properties->value.name()) {
DCHECK_GE(iter->second->num_of_characteristic_value_read_in_progress_, 0);
if (iter->second->num_of_characteristic_value_read_in_progress_ > 0) {
--iter->second->num_of_characteristic_value_read_in_progress_;
} else {
GetAdapter()->NotifyGattCharacteristicValueChanged(
iter->second, properties->value.value());
}
}
}
} // namespace bluez