| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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/bluetooth_advertisement_bluez.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/bind_helpers.h" | |
| 12 #include "base/guid.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/strings/string_util.h" | |
| 15 #include "dbus/bus.h" | |
| 16 #include "dbus/object_path.h" | |
| 17 #include "device/bluetooth/bluetooth_adapter_bluez.h" | |
| 18 #include "device/bluetooth/dbus/bluetooth_le_advertising_manager_client.h" | |
| 19 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
| 20 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 void UnregisterFailure(device::BluetoothAdvertisement::ErrorCode error) { | |
| 25 LOG(ERROR) | |
| 26 << "BluetoothAdvertisementBlueZ::Unregister failed with error code = " | |
| 27 << error; | |
| 28 } | |
| 29 | |
| 30 device::BluetoothAdvertisement::ErrorCode GetErrorCodeFromErrorStrings( | |
| 31 const std::string& error_name, | |
| 32 const std::string& error_message) { | |
| 33 if (error_name == bluetooth_advertising_manager::kErrorFailed || | |
| 34 error_name == bluetooth_advertising_manager::kErrorAlreadyExists) { | |
| 35 return device::BluetoothAdvertisement::ErrorCode:: | |
| 36 ERROR_ADVERTISEMENT_ALREADY_EXISTS; | |
| 37 } else if (error_name == | |
| 38 bluetooth_advertising_manager::kErrorInvalidArguments) { | |
| 39 return device::BluetoothAdvertisement::ErrorCode:: | |
| 40 ERROR_ADVERTISEMENT_INVALID_LENGTH; | |
| 41 } else if (error_name == bluetooth_advertising_manager::kErrorDoesNotExist) { | |
| 42 return device::BluetoothAdvertisement::ErrorCode:: | |
| 43 ERROR_ADVERTISEMENT_DOES_NOT_EXIST; | |
| 44 } | |
| 45 return device::BluetoothAdvertisement::ErrorCode:: | |
| 46 INVALID_ADVERTISEMENT_ERROR_CODE; | |
| 47 } | |
| 48 | |
| 49 void RegisterErrorCallbackConnector( | |
| 50 const device::BluetoothAdapter::CreateAdvertisementErrorCallback& | |
| 51 error_callback, | |
| 52 const std::string& error_name, | |
| 53 const std::string& error_message) { | |
| 54 LOG(ERROR) << "Error while registering advertisement. error_name = " | |
| 55 << error_name << ", error_message = " << error_message; | |
| 56 error_callback.Run(GetErrorCodeFromErrorStrings(error_name, error_message)); | |
| 57 } | |
| 58 | |
| 59 void UnregisterErrorCallbackConnector( | |
| 60 const device::BluetoothAdapter::CreateAdvertisementErrorCallback& | |
| 61 error_callback, | |
| 62 const std::string& error_name, | |
| 63 const std::string& error_message) { | |
| 64 LOG(WARNING) << "Error while unregistering advertisement. error_name = " | |
| 65 << error_name << ", error_message = " << error_message; | |
| 66 error_callback.Run(GetErrorCodeFromErrorStrings(error_name, error_message)); | |
| 67 } | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 namespace bluez { | |
| 72 | |
| 73 BluetoothAdvertisementBlueZ::BluetoothAdvertisementBlueZ( | |
| 74 std::unique_ptr<device::BluetoothAdvertisement::Data> data, | |
| 75 scoped_refptr<BluetoothAdapterBlueZ> adapter) | |
| 76 : adapter_(adapter) { | |
| 77 // Generate a new object path - make sure that we strip any -'s from the | |
| 78 // generated GUID string since object paths can only contain alphanumeric | |
| 79 // characters and _ characters. | |
| 80 std::string GuidString = base::GenerateGUID(); | |
| 81 base::RemoveChars(GuidString, "-", &GuidString); | |
| 82 dbus::ObjectPath advertisement_object_path = | |
| 83 dbus::ObjectPath("/org/chromium/bluetooth_advertisement/" + GuidString); | |
| 84 | |
| 85 DCHECK(bluez::BluezDBusManager::Get()); | |
| 86 provider_ = bluez::BluetoothLEAdvertisementServiceProvider::Create( | |
| 87 bluez::BluezDBusManager::Get()->GetSystemBus(), advertisement_object_path, | |
| 88 this, | |
| 89 static_cast< | |
| 90 bluez::BluetoothLEAdvertisementServiceProvider::AdvertisementType>( | |
| 91 data->type()), | |
| 92 data->service_uuids(), data->manufacturer_data(), data->solicit_uuids(), | |
| 93 data->service_data()); | |
| 94 } | |
| 95 | |
| 96 void BluetoothAdvertisementBlueZ::Register( | |
| 97 const base::Closure& success_callback, | |
| 98 const device::BluetoothAdapter::CreateAdvertisementErrorCallback& | |
| 99 error_callback) { | |
| 100 DCHECK(bluez::BluezDBusManager::Get()); | |
| 101 bluez::BluezDBusManager::Get() | |
| 102 ->GetBluetoothLEAdvertisingManagerClient() | |
| 103 ->RegisterAdvertisement( | |
| 104 adapter_->object_path(), provider_->object_path(), success_callback, | |
| 105 base::Bind(&RegisterErrorCallbackConnector, error_callback)); | |
| 106 } | |
| 107 | |
| 108 BluetoothAdvertisementBlueZ::~BluetoothAdvertisementBlueZ() { | |
| 109 Unregister(base::Bind(&base::DoNothing), base::Bind(&UnregisterFailure)); | |
| 110 } | |
| 111 | |
| 112 void BluetoothAdvertisementBlueZ::Unregister( | |
| 113 const SuccessCallback& success_callback, | |
| 114 const ErrorCallback& error_callback) { | |
| 115 // If we don't have a provider, that means we have already been unregistered, | |
| 116 // return an error. | |
| 117 if (!provider_) { | |
| 118 error_callback.Run(device::BluetoothAdvertisement::ErrorCode:: | |
| 119 ERROR_ADVERTISEMENT_DOES_NOT_EXIST); | |
| 120 return; | |
| 121 } | |
| 122 | |
| 123 DCHECK(bluez::BluezDBusManager::Get()); | |
| 124 bluez::BluezDBusManager::Get() | |
| 125 ->GetBluetoothLEAdvertisingManagerClient() | |
| 126 ->UnregisterAdvertisement( | |
| 127 adapter_->object_path(), provider_->object_path(), success_callback, | |
| 128 base::Bind(&UnregisterErrorCallbackConnector, error_callback)); | |
| 129 provider_.reset(); | |
| 130 } | |
| 131 | |
| 132 void BluetoothAdvertisementBlueZ::Released() { | |
| 133 LOG(WARNING) << "Advertisement released."; | |
| 134 provider_.reset(); | |
| 135 FOR_EACH_OBSERVER(BluetoothAdvertisement::Observer, observers_, | |
| 136 AdvertisementReleased(this)); | |
| 137 } | |
| 138 | |
| 139 } // namespace bluez | |
| OLD | NEW |