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/dbus/bluetooth_gatt_application_service_provider_impl
.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 10 |
| 11 namespace bluez { |
| 12 |
| 13 BluetoothGattApplicationServiceProviderImpl:: |
| 14 BluetoothGattApplicationServiceProviderImpl( |
| 15 dbus::Bus* bus, |
| 16 const dbus::ObjectPath& object_path, |
| 17 const std::map<dbus::ObjectPath, BluetoothLocalGattServiceBlueZ*>& |
| 18 services) |
| 19 : origin_thread_id_(base::PlatformThread::CurrentId()), |
| 20 bus_(bus), |
| 21 object_path_(object_path), |
| 22 weak_ptr_factory_(this) { |
| 23 VLOG(1) << "Creating Bluetooth GATT application: " << object_path_.value(); |
| 24 DCHECK(object_path_.IsValid()); |
| 25 DCHECK(bus_); |
| 26 |
| 27 exported_object_ = bus_->GetExportedObject(object_path_); |
| 28 |
| 29 exported_object_->ExportMethod( |
| 30 dbus::kDBusObjectManagerInterface, |
| 31 dbus::kDBusObjectManagerGetManagedObjects, |
| 32 base::Bind( |
| 33 &BluetoothGattApplicationServiceProviderImpl::GetManagedObjects, |
| 34 weak_ptr_factory_.GetWeakPtr()), |
| 35 base::Bind(&BluetoothGattApplicationServiceProviderImpl::OnExported, |
| 36 weak_ptr_factory_.GetWeakPtr())); |
| 37 |
| 38 BluetoothGattApplicationServiceProvider::CreateAttributeServiceProviders( |
| 39 bus, services, &service_providers_, &characteristic_providers_, |
| 40 &descriptor_providers_); |
| 41 } |
| 42 |
| 43 BluetoothGattApplicationServiceProviderImpl:: |
| 44 ~BluetoothGattApplicationServiceProviderImpl() { |
| 45 VLOG(1) << "Cleaning up Bluetooth GATT service: " << object_path_.value(); |
| 46 if (bus_) |
| 47 bus_->UnregisterExportedObject(object_path_); |
| 48 } |
| 49 |
| 50 BluetoothGattApplicationServiceProviderImpl:: |
| 51 BluetoothGattApplicationServiceProviderImpl( |
| 52 const dbus::ObjectPath& object_path) |
| 53 : origin_thread_id_(base::PlatformThread::CurrentId()), |
| 54 bus_(nullptr), |
| 55 object_path_(object_path), |
| 56 weak_ptr_factory_(this) { |
| 57 VLOG(1) << "Creating Fake Bluetooth GATT application service provider."; |
| 58 } |
| 59 |
| 60 bool BluetoothGattApplicationServiceProviderImpl::OnOriginThread() { |
| 61 return base::PlatformThread::CurrentId() == origin_thread_id_; |
| 62 } |
| 63 |
| 64 template <typename AttributeProvider> |
| 65 void BluetoothGattApplicationServiceProviderImpl::WriteObjectStruct( |
| 66 dbus::MessageWriter* writer, |
| 67 const std::string& attribute_interface, |
| 68 AttributeProvider* attribute_provider) { |
| 69 // Open a struct entry for { object_path : interface_list }. |
| 70 dbus::MessageWriter object_struct_writer(NULL); |
| 71 // [ (oa(sa{sv}) ] |
| 72 writer->OpenStruct(&object_struct_writer); |
| 73 |
| 74 // Key: Object path. [ (o ] |
| 75 object_struct_writer.AppendObjectPath(attribute_provider->object_path()); |
| 76 |
| 77 // Value: Open array for single entry interface_list. [ a(sa{sv}) ] |
| 78 dbus::MessageWriter interface_array_writer(NULL); |
| 79 object_struct_writer.OpenArray("(sa{sv})", &interface_array_writer); |
| 80 WriteInterfaceStruct(&interface_array_writer, attribute_interface, |
| 81 attribute_provider); |
| 82 object_struct_writer.CloseContainer(&interface_array_writer); |
| 83 |
| 84 writer->CloseContainer(&object_struct_writer); |
| 85 } |
| 86 |
| 87 template <typename AttributeProvider> |
| 88 void BluetoothGattApplicationServiceProviderImpl::WriteInterfaceStruct( |
| 89 dbus::MessageWriter* writer, |
| 90 const std::string& attribute_interface, |
| 91 AttributeProvider* attribute_provider) { |
| 92 // Open a struct entry for { interface_name : properties_list }. |
| 93 dbus::MessageWriter interface_struct_writer(NULL); |
| 94 // [ (sa{sv}) ] |
| 95 writer->OpenStruct(&interface_struct_writer); |
| 96 |
| 97 // Key: Interface name. [ (s ] |
| 98 interface_struct_writer.AppendString(attribute_interface); |
| 99 // Value: Open a array for properties_list. [ a{sv}) ] |
| 100 WriteAttributeProperties(&interface_struct_writer, attribute_provider); |
| 101 writer->CloseContainer(&interface_struct_writer); |
| 102 } |
| 103 |
| 104 void BluetoothGattApplicationServiceProviderImpl::WriteAttributeProperties( |
| 105 dbus::MessageWriter* writer, |
| 106 BluetoothGattServiceServiceProvider* service_provider) { |
| 107 service_provider->WriteProperties(writer); |
| 108 } |
| 109 |
| 110 void BluetoothGattApplicationServiceProviderImpl::WriteAttributeProperties( |
| 111 dbus::MessageWriter* writer, |
| 112 BluetoothGattCharacteristicServiceProvider* characteristic_provider) { |
| 113 characteristic_provider->WriteProperties(writer, nullptr); |
| 114 } |
| 115 |
| 116 void BluetoothGattApplicationServiceProviderImpl::WriteAttributeProperties( |
| 117 dbus::MessageWriter* writer, |
| 118 BluetoothGattDescriptorServiceProvider* descriptor_provider) { |
| 119 descriptor_provider->WriteProperties(writer, nullptr); |
| 120 } |
| 121 |
| 122 void BluetoothGattApplicationServiceProviderImpl::GetManagedObjects( |
| 123 dbus::MethodCall* method_call, |
| 124 dbus::ExportedObject::ResponseSender response_sender) { |
| 125 VLOG(2) << "BluetoothGattApplicationServiceProvider::GetManagedObjects: " |
| 126 << object_path_.value(); |
| 127 DCHECK(OnOriginThread()); |
| 128 |
| 129 dbus::MessageReader reader(method_call); |
| 130 |
| 131 std::unique_ptr<dbus::Response> response = |
| 132 dbus::Response::FromMethodCall(method_call); |
| 133 |
| 134 // The expected format by GetAll is [ a(oa(sa{sv})) ] |
| 135 dbus::MessageWriter writer(response.get()); |
| 136 dbus::MessageWriter array_writer(nullptr); |
| 137 |
| 138 writer.OpenArray("(oa(sa{sv}))", &array_writer); |
| 139 |
| 140 for (const auto& service_provider : service_providers_) { |
| 141 WriteObjectStruct(&array_writer, |
| 142 bluetooth_gatt_service::kBluetoothGattServiceInterface, |
| 143 service_provider.get()); |
| 144 } |
| 145 for (const auto& characteristic_provider : characteristic_providers_) { |
| 146 WriteObjectStruct( |
| 147 &array_writer, |
| 148 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, |
| 149 characteristic_provider.get()); |
| 150 } |
| 151 for (const auto& descriptor_provider : descriptor_providers_) { |
| 152 WriteObjectStruct( |
| 153 &array_writer, |
| 154 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, |
| 155 descriptor_provider.get()); |
| 156 } |
| 157 |
| 158 writer.CloseContainer(&array_writer); |
| 159 |
| 160 response_sender.Run(std::move(response)); |
| 161 } |
| 162 |
| 163 // Called by dbus:: when a method is exported. |
| 164 void BluetoothGattApplicationServiceProviderImpl::OnExported( |
| 165 const std::string& interface_name, |
| 166 const std::string& method_name, |
| 167 bool success) { |
| 168 LOG_IF(WARNING, !success) << "Failed to export " << interface_name << "." |
| 169 << method_name; |
| 170 } |
| 171 |
| 172 } // namespace bluez |
OLD | NEW |