OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/bluetooth_media_transport_client.h" | 5 #include "device/bluetooth/dbus/bluetooth_media_transport_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
11 #include "dbus/bus.h" | 11 #include "dbus/bus.h" |
12 #include "dbus/message.h" | 12 #include "dbus/message.h" |
13 #include "dbus/object_manager.h" | 13 #include "dbus/object_manager.h" |
14 #include "dbus/object_proxy.h" | 14 #include "dbus/object_proxy.h" |
15 #include "third_party/cros_system_api/dbus/service_constants.h" | 15 #include "third_party/cros_system_api/dbus/service_constants.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // TODO(mcchou): Add these service constants into dbus/service_constants.h | 19 // TODO(mcchou): Add these service constants into dbus/service_constants.h |
20 // later. | 20 // later. |
21 const char kBluetoothMediaTransportInterface[] = "org.bluez.MediaTransport1"; | 21 const char kBluetoothMediaTransportInterface[] = "org.bluez.MediaTransport1"; |
22 | 22 |
23 // Constants used to indicate exceptional error conditions. | 23 // Constants used to indicate exceptional error conditions. |
24 const char kNoResponseError[] = "org.chromium.Error.NoResponse"; | 24 const char kNoResponseError[] = "org.chromium.Error.NoResponse"; |
25 const char kUnexpectedResponse[] = "org.chromium.Error.UnexpectedResponse"; | 25 const char kUnexpectedResponse[] = "org.chromium.Error.UnexpectedResponse"; |
26 | 26 |
27 // Method names of Media Transport interface. | 27 // Method names of Media Transport interface. |
28 const char kAcquire[] = "Acquire"; | 28 const char kAcquire[] = "Acquire"; |
29 const char kTryAcquire[] = "TryAcquire"; | 29 const char kTryAcquire[] = "TryAcquire"; |
30 const char kRelease[] = "Release"; | 30 const char kRelease[] = "Release"; |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 namespace chromeos { | 34 namespace bluez { |
35 | 35 |
36 // static | 36 // static |
37 const char BluetoothMediaTransportClient::kDeviceProperty[] = "Device"; | 37 const char BluetoothMediaTransportClient::kDeviceProperty[] = "Device"; |
38 const char BluetoothMediaTransportClient::kUUIDProperty[] = "UUID"; | 38 const char BluetoothMediaTransportClient::kUUIDProperty[] = "UUID"; |
39 const char BluetoothMediaTransportClient::kCodecProperty[] = "Codec"; | 39 const char BluetoothMediaTransportClient::kCodecProperty[] = "Codec"; |
40 const char BluetoothMediaTransportClient::kConfigurationProperty[] = | 40 const char BluetoothMediaTransportClient::kConfigurationProperty[] = |
41 "Configuration"; | 41 "Configuration"; |
42 const char BluetoothMediaTransportClient::kStateProperty[] = "State"; | 42 const char BluetoothMediaTransportClient::kStateProperty[] = "State"; |
43 const char BluetoothMediaTransportClient::kDelayProperty[] = "Delay"; | 43 const char BluetoothMediaTransportClient::kDelayProperty[] = "Delay"; |
44 const char BluetoothMediaTransportClient::kVolumeProperty[] = "Volume"; | 44 const char BluetoothMediaTransportClient::kVolumeProperty[] = "Volume"; |
(...skipping 10 matching lines...) Expand all Loading... |
55 : dbus::PropertySet(object_proxy, interface_name, callback) { | 55 : dbus::PropertySet(object_proxy, interface_name, callback) { |
56 RegisterProperty(kDeviceProperty, &device); | 56 RegisterProperty(kDeviceProperty, &device); |
57 RegisterProperty(kUUIDProperty, &uuid); | 57 RegisterProperty(kUUIDProperty, &uuid); |
58 RegisterProperty(kCodecProperty, &codec); | 58 RegisterProperty(kCodecProperty, &codec); |
59 RegisterProperty(kConfigurationProperty, &configuration); | 59 RegisterProperty(kConfigurationProperty, &configuration); |
60 RegisterProperty(kStateProperty, &state); | 60 RegisterProperty(kStateProperty, &state); |
61 RegisterProperty(kDelayProperty, &delay); | 61 RegisterProperty(kDelayProperty, &delay); |
62 RegisterProperty(kVolumeProperty, &volume); | 62 RegisterProperty(kVolumeProperty, &volume); |
63 } | 63 } |
64 | 64 |
65 BluetoothMediaTransportClient::Properties::~Properties() { | 65 BluetoothMediaTransportClient::Properties::~Properties() {} |
66 } | |
67 | 66 |
68 class BluetoothMediaTransportClientImpl | 67 class BluetoothMediaTransportClientImpl |
69 : public BluetoothMediaTransportClient, | 68 : public BluetoothMediaTransportClient, |
70 public dbus::ObjectManager::Interface { | 69 public dbus::ObjectManager::Interface { |
71 public: | 70 public: |
72 BluetoothMediaTransportClientImpl() | 71 BluetoothMediaTransportClientImpl() |
73 : object_manager_(nullptr), weak_ptr_factory_(this) {} | 72 : object_manager_(nullptr), weak_ptr_factory_(this) {} |
74 | 73 |
75 ~BluetoothMediaTransportClientImpl() override { | 74 ~BluetoothMediaTransportClientImpl() override { |
76 object_manager_->UnregisterInterface(kBluetoothMediaTransportInterface); | 75 object_manager_->UnregisterInterface(kBluetoothMediaTransportInterface); |
77 } | 76 } |
78 | 77 |
79 // dbus::ObjectManager::Interface overrides. | 78 // dbus::ObjectManager::Interface overrides. |
80 | 79 |
81 dbus::PropertySet* CreateProperties( | 80 dbus::PropertySet* CreateProperties( |
82 dbus::ObjectProxy* object_proxy, | 81 dbus::ObjectProxy* object_proxy, |
83 const dbus::ObjectPath& object_path, | 82 const dbus::ObjectPath& object_path, |
84 const std::string& interface_name) override { | 83 const std::string& interface_name) override { |
85 Properties* properties = new Properties( | 84 Properties* properties = new Properties( |
86 object_proxy, | 85 object_proxy, interface_name, |
87 interface_name, | |
88 base::Bind(&BluetoothMediaTransportClientImpl::OnPropertyChanged, | 86 base::Bind(&BluetoothMediaTransportClientImpl::OnPropertyChanged, |
89 weak_ptr_factory_.GetWeakPtr(), object_path)); | 87 weak_ptr_factory_.GetWeakPtr(), object_path)); |
90 return properties; | 88 return properties; |
91 } | 89 } |
92 | 90 |
93 void ObjectAdded(const dbus::ObjectPath& object_path, | 91 void ObjectAdded(const dbus::ObjectPath& object_path, |
94 const std::string& interface_name) override { | 92 const std::string& interface_name) override { |
95 VLOG(1) << "Remote Media Transport added: " << object_path.value(); | 93 VLOG(1) << "Remote Media Transport added: " << object_path.value(); |
96 FOR_EACH_OBSERVER(BluetoothMediaTransportClient::Observer, | 94 FOR_EACH_OBSERVER(BluetoothMediaTransportClient::Observer, observers_, |
97 observers_, | |
98 MediaTransportAdded(object_path)); | 95 MediaTransportAdded(object_path)); |
99 } | 96 } |
100 | 97 |
101 void ObjectRemoved(const dbus::ObjectPath& object_path, | 98 void ObjectRemoved(const dbus::ObjectPath& object_path, |
102 const std::string& interface_name) override { | 99 const std::string& interface_name) override { |
103 VLOG(1) << "Remote Media Transport removed: " << object_path.value(); | 100 VLOG(1) << "Remote Media Transport removed: " << object_path.value(); |
104 FOR_EACH_OBSERVER(BluetoothMediaTransportClient::Observer, | 101 FOR_EACH_OBSERVER(BluetoothMediaTransportClient::Observer, observers_, |
105 observers_, | |
106 MediaTransportRemoved(object_path)); | 102 MediaTransportRemoved(object_path)); |
107 } | 103 } |
108 | 104 |
109 // BluetoothMediaTransportClient overrides. | 105 // BluetoothMediaTransportClient overrides. |
110 | 106 |
111 void AddObserver(BluetoothMediaTransportClient::Observer* observer) override { | 107 void AddObserver(BluetoothMediaTransportClient::Observer* observer) override { |
112 DCHECK(observer); | 108 DCHECK(observer); |
113 observers_.AddObserver(observer); | 109 observers_.AddObserver(observer); |
114 } | 110 } |
115 | 111 |
116 void RemoveObserver( | 112 void RemoveObserver( |
117 BluetoothMediaTransportClient::Observer* observer) override { | 113 BluetoothMediaTransportClient::Observer* observer) override { |
118 DCHECK(observer); | 114 DCHECK(observer); |
119 observers_.RemoveObserver(observer); | 115 observers_.RemoveObserver(observer); |
120 } | 116 } |
121 | 117 |
122 Properties* GetProperties(const dbus::ObjectPath& object_path) override { | 118 Properties* GetProperties(const dbus::ObjectPath& object_path) override { |
123 DCHECK(object_manager_); | 119 DCHECK(object_manager_); |
124 return static_cast<Properties*>( | 120 return static_cast<Properties*>(object_manager_->GetProperties( |
125 object_manager_->GetProperties(object_path, | 121 object_path, kBluetoothMediaTransportInterface)); |
126 kBluetoothMediaTransportInterface)); | |
127 } | 122 } |
128 | 123 |
129 void Acquire(const dbus::ObjectPath& object_path, | 124 void Acquire(const dbus::ObjectPath& object_path, |
130 const AcquireCallback& callback, | 125 const AcquireCallback& callback, |
131 const ErrorCallback& error_callback) override { | 126 const ErrorCallback& error_callback) override { |
132 VLOG(1) << "Acquire - transport: " << object_path.value(); | 127 VLOG(1) << "Acquire - transport: " << object_path.value(); |
133 | 128 |
134 DCHECK(object_manager_); | 129 DCHECK(object_manager_); |
135 | 130 |
136 dbus::MethodCall method_call(kBluetoothMediaTransportInterface, kAcquire); | 131 dbus::MethodCall method_call(kBluetoothMediaTransportInterface, kAcquire); |
137 | 132 |
138 // Get object proxy. | 133 // Get object proxy. |
139 scoped_refptr<dbus::ObjectProxy> object_proxy( | 134 scoped_refptr<dbus::ObjectProxy> object_proxy( |
140 object_manager_->GetObjectProxy(object_path)); | 135 object_manager_->GetObjectProxy(object_path)); |
141 | 136 |
142 // Call Acquire method of Media Transport interface. | 137 // Call Acquire method of Media Transport interface. |
143 object_proxy->CallMethodWithErrorCallback( | 138 object_proxy->CallMethodWithErrorCallback( |
144 &method_call, | 139 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
145 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
146 base::Bind(&BluetoothMediaTransportClientImpl::OnAcquireSuccess, | 140 base::Bind(&BluetoothMediaTransportClientImpl::OnAcquireSuccess, |
147 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), | 141 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), |
148 base::Bind(&BluetoothMediaTransportClientImpl::OnError, | 142 base::Bind(&BluetoothMediaTransportClientImpl::OnError, |
149 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 143 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
150 } | 144 } |
151 | 145 |
152 void TryAcquire(const dbus::ObjectPath& object_path, | 146 void TryAcquire(const dbus::ObjectPath& object_path, |
153 const AcquireCallback& callback, | 147 const AcquireCallback& callback, |
154 const ErrorCallback& error_callback) override { | 148 const ErrorCallback& error_callback) override { |
155 VLOG(1) << "TryAcquire - transport: " << object_path.value(); | 149 VLOG(1) << "TryAcquire - transport: " << object_path.value(); |
156 | 150 |
157 DCHECK(object_manager_); | 151 DCHECK(object_manager_); |
158 | 152 |
159 dbus::MethodCall method_call(kBluetoothMediaTransportInterface, | 153 dbus::MethodCall method_call(kBluetoothMediaTransportInterface, |
160 kTryAcquire); | 154 kTryAcquire); |
161 | 155 |
162 // Get object proxy. | 156 // Get object proxy. |
163 scoped_refptr<dbus::ObjectProxy> object_proxy( | 157 scoped_refptr<dbus::ObjectProxy> object_proxy( |
164 object_manager_->GetObjectProxy(object_path)); | 158 object_manager_->GetObjectProxy(object_path)); |
165 | 159 |
166 // Call TryAcquire method of Media Transport interface. | 160 // Call TryAcquire method of Media Transport interface. |
167 object_proxy->CallMethodWithErrorCallback( | 161 object_proxy->CallMethodWithErrorCallback( |
168 &method_call, | 162 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
169 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
170 base::Bind(&BluetoothMediaTransportClientImpl::OnAcquireSuccess, | 163 base::Bind(&BluetoothMediaTransportClientImpl::OnAcquireSuccess, |
171 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), | 164 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), |
172 base::Bind(&BluetoothMediaTransportClientImpl::OnError, | 165 base::Bind(&BluetoothMediaTransportClientImpl::OnError, |
173 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 166 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
174 } | 167 } |
175 | 168 |
176 void Release(const dbus::ObjectPath& object_path, | 169 void Release(const dbus::ObjectPath& object_path, |
177 const base::Closure& callback, | 170 const base::Closure& callback, |
178 const ErrorCallback& error_callback) override { | 171 const ErrorCallback& error_callback) override { |
179 VLOG(1) << "Release - transport: " << object_path.value(); | 172 VLOG(1) << "Release - transport: " << object_path.value(); |
180 | 173 |
181 DCHECK(object_manager_); | 174 DCHECK(object_manager_); |
182 | 175 |
183 dbus::MethodCall method_call(kBluetoothMediaTransportInterface, kRelease); | 176 dbus::MethodCall method_call(kBluetoothMediaTransportInterface, kRelease); |
184 | 177 |
185 // Get object proxy. | 178 // Get object proxy. |
186 scoped_refptr<dbus::ObjectProxy> object_proxy( | 179 scoped_refptr<dbus::ObjectProxy> object_proxy( |
187 object_manager_->GetObjectProxy(object_path)); | 180 object_manager_->GetObjectProxy(object_path)); |
188 | 181 |
189 // Call TryAcquire method of Media Transport interface. | 182 // Call TryAcquire method of Media Transport interface. |
190 object_proxy->CallMethodWithErrorCallback( | 183 object_proxy->CallMethodWithErrorCallback( |
191 &method_call, | 184 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
192 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
193 base::Bind(&BluetoothMediaTransportClientImpl::OnSuccess, | 185 base::Bind(&BluetoothMediaTransportClientImpl::OnSuccess, |
194 weak_ptr_factory_.GetWeakPtr(), callback), | 186 weak_ptr_factory_.GetWeakPtr(), callback), |
195 base::Bind(&BluetoothMediaTransportClientImpl::OnError, | 187 base::Bind(&BluetoothMediaTransportClientImpl::OnError, |
196 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 188 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
197 } | 189 } |
198 | 190 |
199 protected: | 191 protected: |
200 void Init(dbus::Bus* bus) override { | 192 void Init(dbus::Bus* bus) override { |
201 DCHECK(bus); | 193 DCHECK(bus); |
202 object_manager_ = bus->GetObjectManager( | 194 object_manager_ = bus->GetObjectManager( |
203 bluetooth_object_manager::kBluetoothObjectManagerServiceName, | 195 bluetooth_object_manager::kBluetoothObjectManagerServiceName, |
204 dbus::ObjectPath( | 196 dbus::ObjectPath( |
205 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); | 197 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); |
206 object_manager_->RegisterInterface(kBluetoothMediaTransportInterface, | 198 object_manager_->RegisterInterface(kBluetoothMediaTransportInterface, this); |
207 this); | |
208 } | 199 } |
209 | 200 |
210 private: | 201 private: |
211 // Called by dbus::PropertySet when a property value is changed. | 202 // Called by dbus::PropertySet when a property value is changed. |
212 void OnPropertyChanged(const dbus::ObjectPath& object_path, | 203 void OnPropertyChanged(const dbus::ObjectPath& object_path, |
213 const std::string& property_name) { | 204 const std::string& property_name) { |
214 VLOG(1) << "Name of the changed property: " << property_name; | 205 VLOG(1) << "Name of the changed property: " << property_name; |
215 | 206 |
216 // Dispatches the change to the corresponding property-changed handler. | 207 // Dispatches the change to the corresponding property-changed handler. |
217 FOR_EACH_OBSERVER( | 208 FOR_EACH_OBSERVER( |
218 BluetoothMediaTransportClient::Observer, observers_, | 209 BluetoothMediaTransportClient::Observer, observers_, |
219 MediaTransportPropertyChanged(object_path, property_name)); | 210 MediaTransportPropertyChanged(object_path, property_name)); |
220 } | 211 } |
221 | 212 |
222 // Called when a response for successful method call is received. | 213 // Called when a response for successful method call is received. |
223 void OnSuccess(const base::Closure& callback, | 214 void OnSuccess(const base::Closure& callback, dbus::Response* response) { |
224 dbus::Response* response) { | |
225 DCHECK(response); | 215 DCHECK(response); |
226 callback.Run(); | 216 callback.Run(); |
227 } | 217 } |
228 | 218 |
229 // Called when a response for |Acquire|/|TryAcquire| method call is received. | 219 // Called when a response for |Acquire|/|TryAcquire| method call is received. |
230 void OnAcquireSuccess(const AcquireCallback& callback, | 220 void OnAcquireSuccess(const AcquireCallback& callback, |
231 const ErrorCallback& error_callback, | 221 const ErrorCallback& error_callback, |
232 dbus::Response* response) { | 222 dbus::Response* response) { |
233 DCHECK(response); | 223 DCHECK(response); |
234 | 224 |
235 dbus::FileDescriptor fd; | 225 dbus::FileDescriptor fd; |
236 uint16_t read_mtu; | 226 uint16_t read_mtu; |
237 uint16_t write_mtu; | 227 uint16_t write_mtu; |
238 | 228 |
239 // Parse the response. | 229 // Parse the response. |
240 dbus::MessageReader reader(response); | 230 dbus::MessageReader reader(response); |
241 if (reader.PopFileDescriptor(&fd) && | 231 if (reader.PopFileDescriptor(&fd) && reader.PopUint16(&read_mtu) && |
242 reader.PopUint16(&read_mtu) && | |
243 reader.PopUint16(&write_mtu)) { | 232 reader.PopUint16(&write_mtu)) { |
244 fd.CheckValidity(); | 233 fd.CheckValidity(); |
245 DCHECK(fd.is_valid()); | 234 DCHECK(fd.is_valid()); |
246 | 235 |
247 VLOG(1) << "OnAcquireSuccess - fd: "<< fd.value() | 236 VLOG(1) << "OnAcquireSuccess - fd: " << fd.value() |
248 <<", read MTU: " << read_mtu | 237 << ", read MTU: " << read_mtu << ", write MTU: " << write_mtu; |
249 <<", write MTU: " << write_mtu; | |
250 | 238 |
251 // The ownership of the file descriptor is transferred to the user | 239 // The ownership of the file descriptor is transferred to the user |
252 // application. | 240 // application. |
253 callback.Run(&fd, read_mtu, write_mtu); | 241 callback.Run(&fd, read_mtu, write_mtu); |
254 return; | 242 return; |
255 } | 243 } |
256 | 244 |
257 error_callback.Run( | 245 error_callback.Run( |
258 kUnexpectedResponse, | 246 kUnexpectedResponse, |
259 "Failed to retrieve file descriptor, read MTU and write MTU."); | 247 "Failed to retrieve file descriptor, read MTU and write MTU."); |
(...skipping 20 matching lines...) Expand all Loading... |
280 dbus::ObjectManager* object_manager_; | 268 dbus::ObjectManager* object_manager_; |
281 | 269 |
282 // List of observers interested in event notifications from us. | 270 // List of observers interested in event notifications from us. |
283 base::ObserverList<BluetoothMediaTransportClient::Observer> observers_; | 271 base::ObserverList<BluetoothMediaTransportClient::Observer> observers_; |
284 | 272 |
285 base::WeakPtrFactory<BluetoothMediaTransportClientImpl> weak_ptr_factory_; | 273 base::WeakPtrFactory<BluetoothMediaTransportClientImpl> weak_ptr_factory_; |
286 | 274 |
287 DISALLOW_COPY_AND_ASSIGN(BluetoothMediaTransportClientImpl); | 275 DISALLOW_COPY_AND_ASSIGN(BluetoothMediaTransportClientImpl); |
288 }; | 276 }; |
289 | 277 |
290 BluetoothMediaTransportClient::BluetoothMediaTransportClient() { | 278 BluetoothMediaTransportClient::BluetoothMediaTransportClient() {} |
291 } | |
292 | 279 |
293 BluetoothMediaTransportClient::~BluetoothMediaTransportClient() { | 280 BluetoothMediaTransportClient::~BluetoothMediaTransportClient() {} |
294 } | |
295 | 281 |
296 BluetoothMediaTransportClient* BluetoothMediaTransportClient::Create() { | 282 BluetoothMediaTransportClient* BluetoothMediaTransportClient::Create() { |
297 return new BluetoothMediaTransportClientImpl(); | 283 return new BluetoothMediaTransportClientImpl(); |
298 } | 284 } |
299 | 285 |
300 } // namespace chromeos | 286 } // namespace bluez |
OLD | NEW |