Chromium Code Reviews| 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 bus_->UnregisterExportedObject(object_path_); | |
| 47 } | |
| 48 | |
| 49 bool BluetoothGattApplicationServiceProviderImpl::OnOriginThread() { | |
| 50 return base::PlatformThread::CurrentId() == origin_thread_id_; | |
| 51 } | |
| 52 | |
| 53 template <typename AttributeProvider> | |
| 54 void BluetoothGattApplicationServiceProviderImpl::WriteObjectStruct( | |
|
scheib
2016/04/28 04:54:28
Add unit tests code coverage for the Write* method
rkc
2016/04/28 18:13:23
There is no simple way to test real DBus code. If
scheib
2016/04/28 20:46:29
I'm suggesting that just the serialization code in
rkc
2016/04/29 03:53:12
1700 LoC more, but we have a unit test now. :)
| |
| 55 dbus::MessageWriter* writer, | |
| 56 const std::string& attribute_interface, | |
| 57 AttributeProvider* attribute_provider) { | |
| 58 // Open a struct entry for { object_path : interface_list }. | |
| 59 dbus::MessageWriter object_struct_writer(NULL); | |
| 60 // [ (oa(sa{sv}) ] | |
| 61 writer->OpenStruct(&object_struct_writer); | |
| 62 | |
| 63 // Key: Object path. [ (o ] | |
| 64 object_struct_writer.AppendVariantOfObjectPath( | |
| 65 attribute_provider->object_path()); | |
| 66 | |
| 67 // Value: Open array for single entry interface_list. [ a(sa{sv}) ] | |
| 68 dbus::MessageWriter interface_array_writer(NULL); | |
| 69 object_struct_writer.OpenArray("(sa{sv})", &interface_array_writer); | |
| 70 WriteInterfaceStruct(&interface_array_writer, attribute_interface, | |
| 71 attribute_provider); | |
| 72 object_struct_writer.CloseContainer(&interface_array_writer); | |
| 73 | |
| 74 writer->CloseContainer(&object_struct_writer); | |
| 75 } | |
| 76 | |
| 77 template <typename AttributeProvider> | |
| 78 void BluetoothGattApplicationServiceProviderImpl::WriteInterfaceStruct( | |
| 79 dbus::MessageWriter* writer, | |
| 80 const std::string& attribute_interface, | |
| 81 AttributeProvider* attribute_provider) { | |
| 82 // Open a struct entry for { interface_name : properties_list }. | |
| 83 dbus::MessageWriter interface_struct_writer(NULL); | |
| 84 // [ (sa{sv}) ] | |
| 85 writer->OpenStruct(&interface_struct_writer); | |
| 86 | |
| 87 // Key: Interface name. [ (s ] | |
| 88 interface_struct_writer.AppendVariantOfString(attribute_interface); | |
| 89 // Value: Open a array for properties_list. [ a{sv}) ] | |
| 90 WriteAttributeProperties(&interface_struct_writer, attribute_provider); | |
| 91 writer->CloseContainer(&interface_struct_writer); | |
| 92 } | |
| 93 | |
| 94 void BluetoothGattApplicationServiceProviderImpl::WriteAttributeProperties( | |
| 95 dbus::MessageWriter* writer, | |
| 96 BluetoothGattServiceServiceProvider* service_provider) { | |
| 97 service_provider->WriteProperties(writer); | |
| 98 } | |
| 99 | |
| 100 void BluetoothGattApplicationServiceProviderImpl::WriteAttributeProperties( | |
| 101 dbus::MessageWriter* writer, | |
| 102 BluetoothGattCharacteristicServiceProvider* characteristic_provider) { | |
| 103 characteristic_provider->WriteProperties(writer, nullptr); | |
| 104 } | |
| 105 | |
| 106 void BluetoothGattApplicationServiceProviderImpl::WriteAttributeProperties( | |
| 107 dbus::MessageWriter* writer, | |
| 108 BluetoothGattDescriptorServiceProvider* descriptor_provider) { | |
| 109 descriptor_provider->WriteProperties(writer, nullptr); | |
| 110 } | |
| 111 | |
| 112 void BluetoothGattApplicationServiceProviderImpl::GetManagedObjects( | |
| 113 dbus::MethodCall* method_call, | |
| 114 dbus::ExportedObject::ResponseSender response_sender) { | |
| 115 VLOG(2) << "BluetoothGattApplicationServiceProvider::GetManagedObjects: " | |
| 116 << object_path_.value(); | |
| 117 DCHECK(OnOriginThread()); | |
| 118 | |
| 119 dbus::MessageReader reader(method_call); | |
| 120 | |
| 121 std::unique_ptr<dbus::Response> response = | |
| 122 dbus::Response::FromMethodCall(method_call); | |
| 123 | |
| 124 // The expected format by GetAll is [ a(oa(sa{sv})) ] | |
| 125 dbus::MessageWriter writer(response.get()); | |
| 126 dbus::MessageWriter array_writer(nullptr); | |
| 127 | |
| 128 writer.OpenArray("(oa(sa{sv}))", &array_writer); | |
| 129 | |
| 130 for (const auto& service_provider : service_providers_) { | |
| 131 WriteObjectStruct(&array_writer, | |
| 132 bluetooth_gatt_service::kBluetoothGattServiceInterface, | |
| 133 service_provider.get()); | |
| 134 } | |
| 135 for (const auto& characteristic_provider : characteristic_providers_) { | |
| 136 WriteObjectStruct( | |
| 137 &array_writer, | |
| 138 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface, | |
| 139 characteristic_provider.get()); | |
| 140 } | |
| 141 for (const auto& descriptor_provider : descriptor_providers_) { | |
| 142 WriteObjectStruct( | |
| 143 &array_writer, | |
| 144 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, | |
| 145 descriptor_provider.get()); | |
| 146 } | |
| 147 | |
| 148 writer.CloseContainer(&array_writer); | |
| 149 | |
| 150 response_sender.Run(std::move(response)); | |
| 151 } | |
| 152 | |
| 153 // Called by dbus:: when a method is exported. | |
| 154 void BluetoothGattApplicationServiceProviderImpl::OnExported( | |
| 155 const std::string& interface_name, | |
| 156 const std::string& method_name, | |
| 157 bool success) { | |
| 158 LOG_IF(WARNING, !success) << "Failed to export " << interface_name << "." | |
| 159 << method_name; | |
| 160 } | |
| 161 | |
| 162 } // namespace bluez | |
| OLD | NEW |