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_endpoint_service_provider.h" | 5 #include "chromeos/dbus/bluetooth_media_endpoint_service_provider.h" |
6 | 6 |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | 7 #include "base/bind.h" |
10 #include "base/logging.h" | 8 #include "base/logging.h" |
11 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
13 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
| 12 #include "chromeos/dbus/bluetooth_media_transport_client.h" |
14 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
15 #include "chromeos/dbus/fake_bluetooth_media_endpoint_service_provider.h" | 14 #include "chromeos/dbus/fake_bluetooth_media_endpoint_service_provider.h" |
16 #include "dbus/bus.h" | 15 #include "dbus/bus.h" |
17 #include "dbus/exported_object.h" | 16 #include "dbus/exported_object.h" |
18 #include "dbus/message.h" | 17 #include "dbus/message.h" |
19 | 18 |
20 namespace { | 19 namespace { |
21 | 20 |
22 // TODO(mcchou): Move these constants to dbus/service_constants.h. | 21 // TODO(mcchou): Move these constants to dbus/service_constants.h. |
23 // Bluetooth Media Endpoint service identifier. | 22 // Bluetooth Media Endpoint service identifier. |
24 const char kBluetoothMediaEndpointInterface[] = "org.bluez.MediaEndpoint1"; | 23 const char kBluetoothMediaEndpointInterface[] = "org.bluez.MediaEndpoint1"; |
25 | 24 |
26 // Bluetooth Media Endpoint methods. | 25 // Method names in Bluetooth Media Endpoint interface. |
27 const char kSetConfiguration[] = "SetConfiguration"; | 26 const char kSetConfiguration[] = "SetConfiguration"; |
28 const char kSelectConfiguration[] = "SelectConfiguration"; | 27 const char kSelectConfiguration[] = "SelectConfiguration"; |
29 const char kClearConfiguration[] = "ClearConfiguration"; | 28 const char kClearConfiguration[] = "ClearConfiguration"; |
30 const char kRelease[] = "Release"; | 29 const char kRelease[] = "Release"; |
31 | 30 |
| 31 const uint8_t kInvalidCodec = 0xff; |
| 32 const char kInvalidState[] = "unknown"; |
| 33 |
32 } // namespace | 34 } // namespace |
33 | 35 |
34 namespace chromeos { | 36 namespace chromeos { |
35 | 37 |
36 // The BluetoothMediaEndopintServiceProvider implementation used in production. | 38 // The BluetoothMediaEndopintServiceProvider implementation used in production. |
37 class CHROMEOS_EXPORT BluetoothMediaEndpointServiceProviderImpl | 39 class CHROMEOS_EXPORT BluetoothMediaEndpointServiceProviderImpl |
38 : public BluetoothMediaEndpointServiceProvider { | 40 : public BluetoothMediaEndpointServiceProvider { |
39 public: | 41 public: |
40 BluetoothMediaEndpointServiceProviderImpl(dbus::Bus* bus, | 42 BluetoothMediaEndpointServiceProviderImpl(dbus::Bus* bus, |
41 const dbus::ObjectPath& object_path, | 43 const dbus::ObjectPath& object_path, |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 } | 113 } |
112 | 114 |
113 // Called by dbus:: when the remote device connects to the Media Endpoint. | 115 // Called by dbus:: when the remote device connects to the Media Endpoint. |
114 void SetConfiguration(dbus::MethodCall* method_call, | 116 void SetConfiguration(dbus::MethodCall* method_call, |
115 dbus::ExportedObject::ResponseSender response_sender) { | 117 dbus::ExportedObject::ResponseSender response_sender) { |
116 DCHECK(OnOriginThread()); | 118 DCHECK(OnOriginThread()); |
117 DCHECK(delegate_); | 119 DCHECK(delegate_); |
118 | 120 |
119 dbus::MessageReader reader(method_call); | 121 dbus::MessageReader reader(method_call); |
120 dbus::ObjectPath transport_path; | 122 dbus::ObjectPath transport_path; |
121 dbus::MessageReader properties(method_call); | 123 dbus::MessageReader property_reader(method_call); |
122 if (!reader.PopObjectPath(&transport_path) || | 124 if (!reader.PopObjectPath(&transport_path) || |
123 !reader.PopArray(&properties)) { | 125 !reader.PopArray(&property_reader)) { |
124 LOG(WARNING) << "SetConfiguration called with incorrect parameters: " | 126 LOG(WARNING) << "SetConfiguration called with incorrect parameters: " |
125 << method_call->ToString(); | 127 << method_call->ToString(); |
126 return; | 128 return; |
127 } | 129 } |
128 | 130 |
129 delegate_->SetConfiguration(transport_path, properties); | 131 // Parses |properties| and passes the property set as a |
| 132 // Delegate::TransportProperties structure to |delegate_|. |
| 133 Delegate::TransportProperties properties; |
| 134 while (property_reader.HasMoreData()) { |
| 135 dbus::MessageReader dict_entry_reader(nullptr); |
| 136 std::string key; |
| 137 if (!property_reader.PopDictEntry(&dict_entry_reader) || |
| 138 !dict_entry_reader.PopString(&key)) { |
| 139 LOG(WARNING) << "SetConfiguration called with incorrect parameters: " |
| 140 << method_call->ToString(); |
| 141 } else if (key == BluetoothMediaTransportClient::kDeviceProperty) { |
| 142 dict_entry_reader.PopVariantOfObjectPath(&properties.device); |
| 143 } else if (key == BluetoothMediaTransportClient::kUUIDProperty) { |
| 144 dict_entry_reader.PopVariantOfString(&properties.uuid); |
| 145 } else if (key == BluetoothMediaTransportClient::kCodecProperty) { |
| 146 dict_entry_reader.PopVariantOfByte(&properties.codec); |
| 147 } else if (key == BluetoothMediaTransportClient::kConfigurationProperty) { |
| 148 dbus::MessageReader variant_reader(nullptr); |
| 149 const uint8_t* bytes = nullptr; |
| 150 size_t length = 0; |
| 151 dict_entry_reader.PopVariant(&variant_reader); |
| 152 variant_reader.PopArrayOfBytes(&bytes, &length); |
| 153 properties.configuration.assign(bytes, bytes + length); |
| 154 } else if (key == BluetoothMediaTransportClient::kStateProperty) { |
| 155 dict_entry_reader.PopVariantOfString(&properties.state); |
| 156 } else if (key == BluetoothMediaTransportClient::kDelayProperty) { |
| 157 properties.delay.reset(new uint16_t()); |
| 158 dict_entry_reader.PopVariantOfUint16(properties.delay.get()); |
| 159 } else if (key == BluetoothMediaTransportClient::kVolumeProperty) { |
| 160 properties.volume.reset(new uint16_t()); |
| 161 dict_entry_reader.PopVariantOfUint16(properties.volume.get()); |
| 162 } |
| 163 } |
| 164 |
| 165 if (properties.codec != kInvalidCodec && |
| 166 properties.state != kInvalidState) { |
| 167 delegate_->SetConfiguration(transport_path, properties); |
| 168 } else { |
| 169 LOG(WARNING) << "SetConfiguration called with incorrect parameters: " |
| 170 << method_call->ToString(); |
| 171 } |
130 | 172 |
131 response_sender.Run(dbus::Response::FromMethodCall(method_call)); | 173 response_sender.Run(dbus::Response::FromMethodCall(method_call)); |
132 } | 174 } |
133 | 175 |
134 // Called by dbus:: when the remote device receives the configuration for | 176 // Called by dbus:: when the remote device receives the configuration for |
135 // media transport. | 177 // media transport. |
136 void SelectConfiguration( | 178 void SelectConfiguration( |
137 dbus::MethodCall* method_call, | 179 dbus::MethodCall* method_call, |
138 dbus::ExportedObject::ResponseSender response_sender) { | 180 dbus::ExportedObject::ResponseSender response_sender) { |
139 DCHECK(OnOriginThread()); | 181 DCHECK(OnOriginThread()); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 response_sender.Run(dbus::Response::FromMethodCall(method_call)); | 223 response_sender.Run(dbus::Response::FromMethodCall(method_call)); |
182 } | 224 } |
183 | 225 |
184 // Called by Bluetooth daemon to do the clean up after unregistering the Media | 226 // Called by Bluetooth daemon to do the clean up after unregistering the Media |
185 // Endpoint. | 227 // Endpoint. |
186 void Release(dbus::MethodCall* method_call, | 228 void Release(dbus::MethodCall* method_call, |
187 dbus::ExportedObject::ResponseSender response_sender) { | 229 dbus::ExportedObject::ResponseSender response_sender) { |
188 DCHECK(OnOriginThread()); | 230 DCHECK(OnOriginThread()); |
189 DCHECK(delegate_); | 231 DCHECK(delegate_); |
190 | 232 |
191 delegate_->Release(); | 233 delegate_->Released(); |
192 | 234 |
193 response_sender.Run(dbus::Response::FromMethodCall(method_call)); | 235 response_sender.Run(dbus::Response::FromMethodCall(method_call)); |
194 } | 236 } |
195 | 237 |
196 // Called by Delegate to response to a method requiring transport | 238 // Called by Delegate to response to a method requiring transport |
197 // configuration. | 239 // configuration. |
198 void OnConfiguration(dbus::MethodCall* method_call, | 240 void OnConfiguration(dbus::MethodCall* method_call, |
199 dbus::ExportedObject::ResponseSender response_sender, | 241 dbus::ExportedObject::ResponseSender response_sender, |
200 const std::vector<uint8_t>& configuration) { | 242 const std::vector<uint8_t>& configuration) { |
201 DCHECK(OnOriginThread()); | 243 DCHECK(OnOriginThread()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 // Weak pointer factory for generating 'this' printers that might live longer | 276 // Weak pointer factory for generating 'this' printers that might live longer |
235 // than we do. | 277 // than we do. |
236 // Note This should remain the last member so it'll be destroyed and | 278 // Note This should remain the last member so it'll be destroyed and |
237 // invalidate it's weak pointers before any other members are destroyed. | 279 // invalidate it's weak pointers before any other members are destroyed. |
238 base::WeakPtrFactory<BluetoothMediaEndpointServiceProviderImpl> | 280 base::WeakPtrFactory<BluetoothMediaEndpointServiceProviderImpl> |
239 weak_ptr_factory_; | 281 weak_ptr_factory_; |
240 | 282 |
241 DISALLOW_COPY_AND_ASSIGN(BluetoothMediaEndpointServiceProviderImpl); | 283 DISALLOW_COPY_AND_ASSIGN(BluetoothMediaEndpointServiceProviderImpl); |
242 }; | 284 }; |
243 | 285 |
| 286 BluetoothMediaEndpointServiceProvider::Delegate::TransportProperties:: |
| 287 TransportProperties() |
| 288 : codec(kInvalidCodec), |
| 289 state(kInvalidState) { |
| 290 } |
| 291 |
| 292 BluetoothMediaEndpointServiceProvider::Delegate::TransportProperties:: |
| 293 ~TransportProperties() { |
| 294 } |
| 295 |
244 BluetoothMediaEndpointServiceProvider::BluetoothMediaEndpointServiceProvider() { | 296 BluetoothMediaEndpointServiceProvider::BluetoothMediaEndpointServiceProvider() { |
245 } | 297 } |
246 | 298 |
247 BluetoothMediaEndpointServiceProvider::~BluetoothMediaEndpointServiceProvider() | 299 BluetoothMediaEndpointServiceProvider:: |
248 {} | 300 ~BluetoothMediaEndpointServiceProvider() { |
| 301 } |
249 | 302 |
250 BluetoothMediaEndpointServiceProvider* | 303 BluetoothMediaEndpointServiceProvider* |
251 BluetoothMediaEndpointServiceProvider::Create( | 304 BluetoothMediaEndpointServiceProvider::Create( |
252 dbus::Bus* bus, | 305 dbus::Bus* bus, |
253 const dbus::ObjectPath& object_path, | 306 const dbus::ObjectPath& object_path, |
254 Delegate* delegate) { | 307 Delegate* delegate) { |
255 // Returns a real implementation. | 308 // Returns a real implementation. |
256 if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) { | 309 if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) { |
257 return new BluetoothMediaEndpointServiceProviderImpl( | 310 return new BluetoothMediaEndpointServiceProviderImpl( |
258 bus, object_path, delegate); | 311 bus, object_path, delegate); |
259 } | 312 } |
260 // Returns a fake implementation. | 313 // Returns a fake implementation. |
261 return new FakeBluetoothMediaEndpointServiceProvider(object_path, delegate); | 314 return new FakeBluetoothMediaEndpointServiceProvider(object_path, delegate); |
262 } | 315 } |
263 | 316 |
264 } // namespace chromeos | 317 } // namespace chromeos |
OLD | NEW |