OLD | NEW |
| (Empty) |
1 // Copyright 2016 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. | |
4 | |
5 #include "device/bluetooth/bluez/bluetooth_gatt_notify_session_bluez.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "device/bluetooth/bluetooth_adapter.h" | |
10 #include "device/bluetooth/bluetooth_device.h" | |
11 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | |
12 #include "device/bluetooth/bluez/bluetooth_remote_gatt_characteristic_bluez.h" | |
13 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
14 | |
15 namespace bluez { | |
16 | |
17 BluetoothGattNotifySessionBlueZ::BluetoothGattNotifySessionBlueZ( | |
18 scoped_refptr<device::BluetoothAdapter> adapter, | |
19 const std::string& device_address, | |
20 const std::string& service_identifier, | |
21 const std::string& characteristic_identifier, | |
22 const dbus::ObjectPath& characteristic_path) | |
23 : active_(true), | |
24 adapter_(adapter), | |
25 device_address_(device_address), | |
26 service_id_(service_identifier), | |
27 characteristic_id_(characteristic_identifier), | |
28 object_path_(characteristic_path) { | |
29 DCHECK(adapter_.get()); | |
30 DCHECK(!device_address_.empty()); | |
31 DCHECK(!service_id_.empty()); | |
32 DCHECK(!characteristic_id_.empty()); | |
33 DCHECK(object_path_.IsValid()); | |
34 | |
35 bluez::BluezDBusManager::Get() | |
36 ->GetBluetoothGattCharacteristicClient() | |
37 ->AddObserver(this); | |
38 } | |
39 | |
40 BluetoothGattNotifySessionBlueZ::~BluetoothGattNotifySessionBlueZ() { | |
41 bluez::BluezDBusManager::Get() | |
42 ->GetBluetoothGattCharacteristicClient() | |
43 ->RemoveObserver(this); | |
44 Stop(base::Bind(&base::DoNothing)); | |
45 } | |
46 | |
47 std::string BluetoothGattNotifySessionBlueZ::GetCharacteristicIdentifier() | |
48 const { | |
49 return characteristic_id_; | |
50 } | |
51 | |
52 bool BluetoothGattNotifySessionBlueZ::IsActive() { | |
53 // Determine if the session is active. If |active_| is false, then it's | |
54 // been explicitly marked, so return false. | |
55 if (!active_) | |
56 return false; | |
57 | |
58 // The fact that |active_| is true doesn't mean that the session is | |
59 // actually active, since the characteristic might have stopped sending | |
60 // notifications yet this method was called before we processed the | |
61 // observer event (e.g. because somebody else called this method in their | |
62 // bluez::BluetoothGattCharacteristicClient::Observer implementation, which | |
63 // was | |
64 // called before ours). Check the client to see if notifications are still | |
65 // being sent. | |
66 bluez::BluetoothGattCharacteristicClient::Properties* properties = | |
67 bluez::BluezDBusManager::Get() | |
68 ->GetBluetoothGattCharacteristicClient() | |
69 ->GetProperties(object_path_); | |
70 if (!properties || !properties->notifying.value()) | |
71 active_ = false; | |
72 | |
73 return active_; | |
74 } | |
75 | |
76 void BluetoothGattNotifySessionBlueZ::Stop(const base::Closure& callback) { | |
77 if (!active_) { | |
78 VLOG(1) << "Notify session already inactive."; | |
79 callback.Run(); | |
80 return; | |
81 } | |
82 | |
83 // Mark this session as inactive no matter what. | |
84 active_ = false; | |
85 | |
86 device::BluetoothDevice* device = adapter_->GetDevice(device_address_); | |
87 if (!device) | |
88 return; | |
89 | |
90 device::BluetoothRemoteGattService* service = | |
91 device->GetGattService(service_id_); | |
92 if (!service) | |
93 return; | |
94 | |
95 BluetoothRemoteGattCharacteristicBlueZ* chrc = | |
96 static_cast<BluetoothRemoteGattCharacteristicBlueZ*>( | |
97 service->GetCharacteristic(characteristic_id_)); | |
98 if (!chrc) | |
99 return; | |
100 | |
101 chrc->RemoveNotifySession(callback); | |
102 } | |
103 | |
104 void BluetoothGattNotifySessionBlueZ::GattCharacteristicRemoved( | |
105 const dbus::ObjectPath& object_path) { | |
106 if (object_path != object_path_) | |
107 return; | |
108 | |
109 active_ = false; | |
110 } | |
111 | |
112 void BluetoothGattNotifySessionBlueZ::GattCharacteristicPropertyChanged( | |
113 const dbus::ObjectPath& object_path, | |
114 const std::string& property_name) { | |
115 if (object_path != object_path_) | |
116 return; | |
117 | |
118 if (!active_) | |
119 return; | |
120 | |
121 bluez::BluetoothGattCharacteristicClient::Properties* properties = | |
122 bluez::BluezDBusManager::Get() | |
123 ->GetBluetoothGattCharacteristicClient() | |
124 ->GetProperties(object_path_); | |
125 if (!properties) { | |
126 active_ = false; | |
127 return; | |
128 } | |
129 | |
130 if (property_name == properties->notifying.name() && | |
131 !properties->notifying.value()) | |
132 active_ = false; | |
133 } | |
134 | |
135 } // namespace bluez | |
OLD | NEW |