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.
5 #include "device/bluetooth/bluetooth_advertisement_chromeos.h"
10 #include "base/bind_helpers.h"
11 #include "base/guid.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string_util.h"
15 #include "chromeos/dbus/bluetooth_le_advertising_manager_client.h"
16 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "dbus/object_path.h"
19 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
20 #include "third_party/cros_system_api/dbus/service_constants.h"
24 void UnregisterFailure(device::BluetoothAdvertisement::ErrorCode error
) {
26 << "BluetoothAdvertisementChromeOS::Unregister failed with error code = "
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
;
45 return device::BluetoothAdvertisement::ErrorCode::
46 INVALID_ADVERTISEMENT_ERROR_CODE
;
49 void RegisterErrorCallbackConnector(
50 const device::BluetoothAdapter::CreateAdvertisementErrorCallback
&
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
));
59 void UnregisterErrorCallbackConnector(
60 const device::BluetoothAdapter::CreateAdvertisementErrorCallback
&
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
));
73 BluetoothAdvertisementChromeOS::BluetoothAdvertisementChromeOS(
74 scoped_ptr
<device::BluetoothAdvertisement::Data
> data
,
75 scoped_refptr
<BluetoothAdapterChromeOS
> 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
);
85 DCHECK(DBusThreadManager::Get());
86 provider_
= BluetoothLEAdvertisementServiceProvider::Create(
87 DBusThreadManager::Get()->GetSystemBus(), advertisement_object_path
, this,
88 static_cast<BluetoothLEAdvertisementServiceProvider::AdvertisementType
>(
90 data
->service_uuids().Pass(), data
->manufacturer_data().Pass(),
91 data
->solicit_uuids().Pass(), data
->service_data().Pass());
94 void BluetoothAdvertisementChromeOS::Register(
95 const base::Closure
& success_callback
,
96 const device::BluetoothAdapter::CreateAdvertisementErrorCallback
&
98 DCHECK(DBusThreadManager::Get());
99 DBusThreadManager::Get()
100 ->GetBluetoothLEAdvertisingManagerClient()
101 ->RegisterAdvertisement(
102 adapter_
->object_path(), provider_
->object_path(), success_callback
,
103 base::Bind(&RegisterErrorCallbackConnector
, error_callback
));
106 BluetoothAdvertisementChromeOS::~BluetoothAdvertisementChromeOS() {
107 Unregister(base::Bind(&base::DoNothing
), base::Bind(&UnregisterFailure
));
110 void BluetoothAdvertisementChromeOS::Unregister(
111 const SuccessCallback
& success_callback
,
112 const ErrorCallback
& error_callback
) {
113 // If we don't have a provider, that means we have already been unregistered,
116 error_callback
.Run(device::BluetoothAdvertisement::ErrorCode::
117 ERROR_ADVERTISEMENT_DOES_NOT_EXIST
);
121 DCHECK(DBusThreadManager::Get());
122 DBusThreadManager::Get()
123 ->GetBluetoothLEAdvertisingManagerClient()
124 ->UnregisterAdvertisement(
125 adapter_
->object_path(), provider_
->object_path(), success_callback
,
126 base::Bind(&UnregisterErrorCallbackConnector
, error_callback
));
130 void BluetoothAdvertisementChromeOS::Released() {
131 LOG(WARNING
) << "Advertisement released.";
133 FOR_EACH_OBSERVER(BluetoothAdvertisement::Observer
, observers_
,
134 AdvertisementReleased(this));
137 } // namespace chromeos