| 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_gatt_service_service_provider.h" | 5 #include "device/bluetooth/dbus/bluetooth_gatt_service_service_provider.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/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 13 #include "chromeos/dbus/fake_bluetooth_gatt_service_service_provider.h" | |
| 14 #include "dbus/exported_object.h" | 12 #include "dbus/exported_object.h" |
| 15 #include "dbus/message.h" | 13 #include "dbus/message.h" |
| 14 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| 15 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_service_provider.h" |
| 16 #include "third_party/cros_system_api/dbus/service_constants.h" | 16 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 17 | 17 |
| 18 namespace chromeos { | 18 namespace bluez { |
| 19 namespace { | 19 namespace { |
| 20 const char kErrorInvalidArgs[] = | 20 const char kErrorInvalidArgs[] = "org.freedesktop.DBus.Error.InvalidArgs"; |
| 21 "org.freedesktop.DBus.Error.InvalidArgs"; | |
| 22 const char kErrorPropertyReadOnly[] = | 21 const char kErrorPropertyReadOnly[] = |
| 23 "org.freedesktop.DBus.Error.PropertyReadOnly"; | 22 "org.freedesktop.DBus.Error.PropertyReadOnly"; |
| 24 } // namespace | 23 } // namespace |
| 25 | 24 |
| 26 // The BluetoothGattServiceServiceProvider implementation used in production. | 25 // The BluetoothGattServiceServiceProvider implementation used in production. |
| 27 class BluetoothGattServiceServiceProviderImpl | 26 class BluetoothGattServiceServiceProviderImpl |
| 28 : public BluetoothGattServiceServiceProvider { | 27 : public BluetoothGattServiceServiceProvider { |
| 29 public: | 28 public: |
| 30 BluetoothGattServiceServiceProviderImpl( | 29 BluetoothGattServiceServiceProviderImpl( |
| 31 dbus::Bus* bus, | 30 dbus::Bus* bus, |
| 32 const dbus::ObjectPath& object_path, | 31 const dbus::ObjectPath& object_path, |
| 33 const std::string& uuid, | 32 const std::string& uuid, |
| 34 const std::vector<dbus::ObjectPath>& includes) | 33 const std::vector<dbus::ObjectPath>& includes) |
| 35 : origin_thread_id_(base::PlatformThread::CurrentId()), | 34 : origin_thread_id_(base::PlatformThread::CurrentId()), |
| 36 uuid_(uuid), | 35 uuid_(uuid), |
| 37 includes_(includes), | 36 includes_(includes), |
| 38 bus_(bus), | 37 bus_(bus), |
| 39 object_path_(object_path), | 38 object_path_(object_path), |
| 40 weak_ptr_factory_(this) { | 39 weak_ptr_factory_(this) { |
| 41 VLOG(1) << "Creating Bluetooth GATT service: " << object_path_.value() | 40 VLOG(1) << "Creating Bluetooth GATT service: " << object_path_.value() |
| 42 << " UUID: " << uuid; | 41 << " UUID: " << uuid; |
| 43 DCHECK(!uuid_.empty()); | 42 DCHECK(!uuid_.empty()); |
| 44 DCHECK(object_path_.IsValid()); | 43 DCHECK(object_path_.IsValid()); |
| 45 DCHECK(bus_); | 44 DCHECK(bus_); |
| 46 | 45 |
| 47 exported_object_ = bus_->GetExportedObject(object_path_); | 46 exported_object_ = bus_->GetExportedObject(object_path_); |
| 48 | 47 |
| 49 exported_object_->ExportMethod( | 48 exported_object_->ExportMethod( |
| 50 dbus::kDBusPropertiesInterface, | 49 dbus::kDBusPropertiesInterface, dbus::kDBusPropertiesGet, |
| 51 dbus::kDBusPropertiesGet, | |
| 52 base::Bind(&BluetoothGattServiceServiceProviderImpl::Get, | 50 base::Bind(&BluetoothGattServiceServiceProviderImpl::Get, |
| 53 weak_ptr_factory_.GetWeakPtr()), | 51 weak_ptr_factory_.GetWeakPtr()), |
| 54 base::Bind(&BluetoothGattServiceServiceProviderImpl::OnExported, | 52 base::Bind(&BluetoothGattServiceServiceProviderImpl::OnExported, |
| 55 weak_ptr_factory_.GetWeakPtr())); | 53 weak_ptr_factory_.GetWeakPtr())); |
| 56 | 54 |
| 57 exported_object_->ExportMethod( | 55 exported_object_->ExportMethod( |
| 58 dbus::kDBusPropertiesInterface, | 56 dbus::kDBusPropertiesInterface, dbus::kDBusPropertiesSet, |
| 59 dbus::kDBusPropertiesSet, | |
| 60 base::Bind(&BluetoothGattServiceServiceProviderImpl::Set, | 57 base::Bind(&BluetoothGattServiceServiceProviderImpl::Set, |
| 61 weak_ptr_factory_.GetWeakPtr()), | 58 weak_ptr_factory_.GetWeakPtr()), |
| 62 base::Bind(&BluetoothGattServiceServiceProviderImpl::OnExported, | 59 base::Bind(&BluetoothGattServiceServiceProviderImpl::OnExported, |
| 63 weak_ptr_factory_.GetWeakPtr())); | 60 weak_ptr_factory_.GetWeakPtr())); |
| 64 | 61 |
| 65 exported_object_->ExportMethod( | 62 exported_object_->ExportMethod( |
| 66 dbus::kDBusPropertiesInterface, | 63 dbus::kDBusPropertiesInterface, dbus::kDBusPropertiesGetAll, |
| 67 dbus::kDBusPropertiesGetAll, | |
| 68 base::Bind(&BluetoothGattServiceServiceProviderImpl::GetAll, | 64 base::Bind(&BluetoothGattServiceServiceProviderImpl::GetAll, |
| 69 weak_ptr_factory_.GetWeakPtr()), | 65 weak_ptr_factory_.GetWeakPtr()), |
| 70 base::Bind(&BluetoothGattServiceServiceProviderImpl::OnExported, | 66 base::Bind(&BluetoothGattServiceServiceProviderImpl::OnExported, |
| 71 weak_ptr_factory_.GetWeakPtr())); | 67 weak_ptr_factory_.GetWeakPtr())); |
| 72 } | 68 } |
| 73 | 69 |
| 74 ~BluetoothGattServiceServiceProviderImpl() override { | 70 ~BluetoothGattServiceServiceProviderImpl() override { |
| 75 VLOG(1) << "Cleaning up Bluetooth GATT service: " << object_path_.value(); | 71 VLOG(1) << "Cleaning up Bluetooth GATT service: " << object_path_.value(); |
| 76 bus_->UnregisterExportedObject(object_path_); | 72 bus_->UnregisterExportedObject(object_path_); |
| 77 } | 73 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 88 dbus::ExportedObject::ResponseSender response_sender) { | 84 dbus::ExportedObject::ResponseSender response_sender) { |
| 89 VLOG(2) << "BluetoothGattServiceServiceProvider::Get: " | 85 VLOG(2) << "BluetoothGattServiceServiceProvider::Get: " |
| 90 << object_path_.value(); | 86 << object_path_.value(); |
| 91 DCHECK(OnOriginThread()); | 87 DCHECK(OnOriginThread()); |
| 92 | 88 |
| 93 dbus::MessageReader reader(method_call); | 89 dbus::MessageReader reader(method_call); |
| 94 | 90 |
| 95 std::string interface_name; | 91 std::string interface_name; |
| 96 std::string property_name; | 92 std::string property_name; |
| 97 if (!reader.PopString(&interface_name) || | 93 if (!reader.PopString(&interface_name) || |
| 98 !reader.PopString(&property_name) || | 94 !reader.PopString(&property_name) || reader.HasMoreData()) { |
| 99 reader.HasMoreData()) { | |
| 100 scoped_ptr<dbus::ErrorResponse> error_response = | 95 scoped_ptr<dbus::ErrorResponse> error_response = |
| 101 dbus::ErrorResponse::FromMethodCall( | 96 dbus::ErrorResponse::FromMethodCall(method_call, kErrorInvalidArgs, |
| 102 method_call, kErrorInvalidArgs, "Expected 'ss'."); | 97 "Expected 'ss'."); |
| 103 response_sender.Run(error_response.Pass()); | 98 response_sender.Run(error_response.Pass()); |
| 104 return; | 99 return; |
| 105 } | 100 } |
| 106 | 101 |
| 107 // Only the GATT service interface is allowed. | 102 // Only the GATT service interface is allowed. |
| 108 if (interface_name != | 103 if (interface_name != |
| 109 bluetooth_gatt_service::kBluetoothGattServiceInterface) { | 104 bluetooth_gatt_service::kBluetoothGattServiceInterface) { |
| 110 scoped_ptr<dbus::ErrorResponse> error_response = | 105 scoped_ptr<dbus::ErrorResponse> error_response = |
| 111 dbus::ErrorResponse::FromMethodCall( | 106 dbus::ErrorResponse::FromMethodCall( |
| 112 method_call, kErrorInvalidArgs, | 107 method_call, kErrorInvalidArgs, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 // service. | 143 // service. |
| 149 void Set(dbus::MethodCall* method_call, | 144 void Set(dbus::MethodCall* method_call, |
| 150 dbus::ExportedObject::ResponseSender response_sender) { | 145 dbus::ExportedObject::ResponseSender response_sender) { |
| 151 VLOG(2) << "BluetoothGattServiceServiceProvider::Set: " | 146 VLOG(2) << "BluetoothGattServiceServiceProvider::Set: " |
| 152 << object_path_.value(); | 147 << object_path_.value(); |
| 153 DCHECK(OnOriginThread()); | 148 DCHECK(OnOriginThread()); |
| 154 | 149 |
| 155 // All of the properties on this interface are read-only, so just return | 150 // All of the properties on this interface are read-only, so just return |
| 156 // error. | 151 // error. |
| 157 scoped_ptr<dbus::ErrorResponse> error_response = | 152 scoped_ptr<dbus::ErrorResponse> error_response = |
| 158 dbus::ErrorResponse::FromMethodCall( | 153 dbus::ErrorResponse::FromMethodCall(method_call, kErrorPropertyReadOnly, |
| 159 method_call, kErrorPropertyReadOnly, | 154 "All properties are read-only."); |
| 160 "All properties are read-only."); | |
| 161 response_sender.Run(error_response.Pass()); | 155 response_sender.Run(error_response.Pass()); |
| 162 } | 156 } |
| 163 | 157 |
| 164 // Called by dbus:: when the Bluetooth daemon fetches all properties of the | 158 // Called by dbus:: when the Bluetooth daemon fetches all properties of the |
| 165 // service. | 159 // service. |
| 166 void GetAll(dbus::MethodCall* method_call, | 160 void GetAll(dbus::MethodCall* method_call, |
| 167 dbus::ExportedObject::ResponseSender response_sender) { | 161 dbus::ExportedObject::ResponseSender response_sender) { |
| 168 VLOG(2) << "BluetoothGattServiceServiceProvider::GetAll: " | 162 VLOG(2) << "BluetoothGattServiceServiceProvider::GetAll: " |
| 169 << object_path_.value(); | 163 << object_path_.value(); |
| 170 DCHECK(OnOriginThread()); | 164 DCHECK(OnOriginThread()); |
| 171 | 165 |
| 172 dbus::MessageReader reader(method_call); | 166 dbus::MessageReader reader(method_call); |
| 173 | 167 |
| 174 std::string interface_name; | 168 std::string interface_name; |
| 175 if (!reader.PopString(&interface_name) || reader.HasMoreData()) { | 169 if (!reader.PopString(&interface_name) || reader.HasMoreData()) { |
| 176 scoped_ptr<dbus::ErrorResponse> error_response = | 170 scoped_ptr<dbus::ErrorResponse> error_response = |
| 177 dbus::ErrorResponse::FromMethodCall( | 171 dbus::ErrorResponse::FromMethodCall(method_call, kErrorInvalidArgs, |
| 178 method_call, kErrorInvalidArgs, "Expected 's'."); | 172 "Expected 's'."); |
| 179 response_sender.Run(error_response.Pass()); | 173 response_sender.Run(error_response.Pass()); |
| 180 return; | 174 return; |
| 181 } | 175 } |
| 182 | 176 |
| 183 // Only the GATT service interface is allowed. | 177 // Only the GATT service interface is allowed. |
| 184 if (interface_name != | 178 if (interface_name != |
| 185 bluetooth_gatt_service::kBluetoothGattServiceInterface) { | 179 bluetooth_gatt_service::kBluetoothGattServiceInterface) { |
| 186 scoped_ptr<dbus::ErrorResponse> error_response = | 180 scoped_ptr<dbus::ErrorResponse> error_response = |
| 187 dbus::ErrorResponse::FromMethodCall( | 181 dbus::ErrorResponse::FromMethodCall( |
| 188 method_call, kErrorInvalidArgs, | 182 method_call, kErrorInvalidArgs, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 214 | 208 |
| 215 writer.CloseContainer(&array_writer); | 209 writer.CloseContainer(&array_writer); |
| 216 | 210 |
| 217 response_sender.Run(response.Pass()); | 211 response_sender.Run(response.Pass()); |
| 218 } | 212 } |
| 219 | 213 |
| 220 // Called by dbus:: when a method is exported. | 214 // Called by dbus:: when a method is exported. |
| 221 void OnExported(const std::string& interface_name, | 215 void OnExported(const std::string& interface_name, |
| 222 const std::string& method_name, | 216 const std::string& method_name, |
| 223 bool success) { | 217 bool success) { |
| 224 LOG_IF(WARNING, !success) << "Failed to export " | 218 LOG_IF(WARNING, !success) << "Failed to export " << interface_name << "." |
| 225 << interface_name << "." << method_name; | 219 << method_name; |
| 226 } | 220 } |
| 227 | 221 |
| 228 // Origin thread (i.e. the UI thread in production). | 222 // Origin thread (i.e. the UI thread in production). |
| 229 base::PlatformThreadId origin_thread_id_; | 223 base::PlatformThreadId origin_thread_id_; |
| 230 | 224 |
| 231 // 128-bit service UUID of this object. | 225 // 128-bit service UUID of this object. |
| 232 std::string uuid_; | 226 std::string uuid_; |
| 233 | 227 |
| 234 // List of object paths that represent other exported GATT services that are | 228 // List of object paths that represent other exported GATT services that are |
| 235 // included from this service. | 229 // included from this service. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 249 // Weak pointer factory for generating 'this' pointers that might live longer | 243 // Weak pointer factory for generating 'this' pointers that might live longer |
| 250 // than we do. | 244 // than we do. |
| 251 // Note: This should remain the last member so it'll be destroyed and | 245 // Note: This should remain the last member so it'll be destroyed and |
| 252 // invalidate its weak pointers before any other members are destroyed. | 246 // invalidate its weak pointers before any other members are destroyed. |
| 253 base::WeakPtrFactory<BluetoothGattServiceServiceProviderImpl> | 247 base::WeakPtrFactory<BluetoothGattServiceServiceProviderImpl> |
| 254 weak_ptr_factory_; | 248 weak_ptr_factory_; |
| 255 | 249 |
| 256 DISALLOW_COPY_AND_ASSIGN(BluetoothGattServiceServiceProviderImpl); | 250 DISALLOW_COPY_AND_ASSIGN(BluetoothGattServiceServiceProviderImpl); |
| 257 }; | 251 }; |
| 258 | 252 |
| 259 BluetoothGattServiceServiceProvider::BluetoothGattServiceServiceProvider() { | 253 BluetoothGattServiceServiceProvider::BluetoothGattServiceServiceProvider() {} |
| 260 } | |
| 261 | 254 |
| 262 BluetoothGattServiceServiceProvider::~BluetoothGattServiceServiceProvider() { | 255 BluetoothGattServiceServiceProvider::~BluetoothGattServiceServiceProvider() {} |
| 263 } | |
| 264 | 256 |
| 265 // static | 257 // static |
| 266 BluetoothGattServiceServiceProvider* | 258 BluetoothGattServiceServiceProvider* |
| 267 BluetoothGattServiceServiceProvider::Create( | 259 BluetoothGattServiceServiceProvider::Create( |
| 268 dbus::Bus* bus, | 260 dbus::Bus* bus, |
| 269 const dbus::ObjectPath& object_path, | 261 const dbus::ObjectPath& object_path, |
| 270 const std::string& uuid, | 262 const std::string& uuid, |
| 271 const std::vector<dbus::ObjectPath>& includes) { | 263 const std::vector<dbus::ObjectPath>& includes) { |
| 272 if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) { | 264 if (!bluez::BluezDBusManager::Get()->IsUsingStub()) { |
| 273 return new BluetoothGattServiceServiceProviderImpl( | 265 return new BluetoothGattServiceServiceProviderImpl(bus, object_path, uuid, |
| 274 bus, object_path, uuid, includes); | 266 includes); |
| 275 } | 267 } |
| 276 return new FakeBluetoothGattServiceServiceProvider( | 268 return new FakeBluetoothGattServiceServiceProvider(object_path, uuid, |
| 277 object_path, uuid, includes); | 269 includes); |
| 278 } | 270 } |
| 279 | 271 |
| 280 } // namespace chromeos | 272 } // namespace bluez |
| OLD | NEW |