Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "base/location.h" | 5 #include "base/location.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/single_thread_task_runner.h" | 7 #include "base/single_thread_task_runner.h" |
| 8 #include "base/threading/thread_task_runner_handle.h" | 8 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "dbus/bus.h" | 9 #include "dbus/bus.h" |
| 10 #include "dbus/message.h" | 10 #include "dbus/message.h" |
| 11 #include "dbus/object_proxy.h" | 11 #include "dbus/object_proxy.h" |
| 12 #include "device/bluetooth/dbus/fake_bluetooth_le_advertisement_service_provider .h" | 12 #include "device/bluetooth/dbus/fake_bluetooth_le_advertisement_service_provider .h" |
| 13 #include "device/bluetooth/dbus/fake_bluetooth_le_advertising_manager_client.h" | 13 #include "device/bluetooth/dbus/fake_bluetooth_le_advertising_manager_client.h" |
| 14 #include "third_party/cros_system_api/dbus/service_constants.h" | 14 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 15 | 15 |
| 16 namespace bluez { | 16 namespace bluez { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 constexpr char kAdvertisingManagerPath[] = "/fake/hci0"; | 20 constexpr char kAdvertisingManagerPath[] = "/fake/hci0"; |
| 21 constexpr uint16_t kMinIntervalMs = 20; | 21 constexpr uint16_t kMinIntervalMs = 20; |
| 22 constexpr uint16_t kMaxIntervalMs = 10240; | 22 constexpr uint16_t kMaxIntervalMs = 10240; |
| 23 | 23 |
| 24 constexpr size_t kMaxBluezAdvertisements = 5; | |
| 25 | |
| 24 } // namespace | 26 } // namespace |
| 25 | 27 |
| 26 FakeBluetoothLEAdvertisingManagerClient:: | 28 FakeBluetoothLEAdvertisingManagerClient:: |
| 27 FakeBluetoothLEAdvertisingManagerClient() {} | 29 FakeBluetoothLEAdvertisingManagerClient() {} |
| 28 | 30 |
| 29 FakeBluetoothLEAdvertisingManagerClient:: | 31 FakeBluetoothLEAdvertisingManagerClient:: |
| 30 ~FakeBluetoothLEAdvertisingManagerClient() {} | 32 ~FakeBluetoothLEAdvertisingManagerClient() {} |
| 31 | 33 |
| 32 void FakeBluetoothLEAdvertisingManagerClient::Init(dbus::Bus* bus) {} | 34 void FakeBluetoothLEAdvertisingManagerClient::Init(dbus::Bus* bus) {} |
| 33 | 35 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 46 if (manager_object_path != dbus::ObjectPath(kAdvertisingManagerPath)) { | 48 if (manager_object_path != dbus::ObjectPath(kAdvertisingManagerPath)) { |
| 47 error_callback.Run(kNoResponseError, "Invalid Advertising Manager path."); | 49 error_callback.Run(kNoResponseError, "Invalid Advertising Manager path."); |
| 48 return; | 50 return; |
| 49 } | 51 } |
| 50 | 52 |
| 51 ServiceProviderMap::iterator iter = | 53 ServiceProviderMap::iterator iter = |
| 52 service_provider_map_.find(advertisement_object_path); | 54 service_provider_map_.find(advertisement_object_path); |
| 53 if (iter == service_provider_map_.end()) { | 55 if (iter == service_provider_map_.end()) { |
| 54 error_callback.Run(bluetooth_advertising_manager::kErrorInvalidArguments, | 56 error_callback.Run(bluetooth_advertising_manager::kErrorInvalidArguments, |
| 55 "Advertisement object not registered"); | 57 "Advertisement object not registered"); |
| 56 } else if (!currently_registered_.value().empty()) { | 58 } else if (currently_registered_.size() > kMaxBluezAdvertisements) { |
|
Rahul Chaturvedi
2016/09/30 19:51:08
Should this be >=?
Eric Caruso
2016/09/30 20:08:29
Yep. Awkward.
| |
| 57 error_callback.Run(bluetooth_advertising_manager::kErrorFailed, | 59 error_callback.Run(bluetooth_advertising_manager::kErrorFailed, |
| 58 "Maximum advertisements reached"); | 60 "Maximum advertisements reached"); |
| 59 } else { | 61 } else { |
| 60 currently_registered_ = advertisement_object_path; | 62 currently_registered_.push_back(advertisement_object_path); |
| 61 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | 63 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 62 } | 64 } |
| 63 } | 65 } |
| 64 | 66 |
| 65 void FakeBluetoothLEAdvertisingManagerClient::UnregisterAdvertisement( | 67 void FakeBluetoothLEAdvertisingManagerClient::UnregisterAdvertisement( |
| 66 const dbus::ObjectPath& manager_object_path, | 68 const dbus::ObjectPath& manager_object_path, |
| 67 const dbus::ObjectPath& advertisement_object_path, | 69 const dbus::ObjectPath& advertisement_object_path, |
| 68 const base::Closure& callback, | 70 const base::Closure& callback, |
| 69 const ErrorCallback& error_callback) { | 71 const ErrorCallback& error_callback) { |
| 70 VLOG(1) << "UnregisterAdvertisment: " << advertisement_object_path.value(); | 72 VLOG(1) << "UnregisterAdvertisment: " << advertisement_object_path.value(); |
| 71 | 73 |
| 72 if (manager_object_path != dbus::ObjectPath(kAdvertisingManagerPath)) { | 74 if (manager_object_path != dbus::ObjectPath(kAdvertisingManagerPath)) { |
| 73 error_callback.Run(kNoResponseError, "Invalid Advertising Manager path."); | 75 error_callback.Run(kNoResponseError, "Invalid Advertising Manager path."); |
| 74 return; | 76 return; |
| 75 } | 77 } |
| 76 | 78 |
| 77 ServiceProviderMap::iterator iter = | 79 auto service_iter = service_provider_map_.find(advertisement_object_path); |
| 78 service_provider_map_.find(advertisement_object_path); | 80 auto reg_iter = |
| 79 if (iter == service_provider_map_.end()) { | 81 std::find(currently_registered_.begin(), currently_registered_.end(), |
| 82 advertisement_object_path); | |
| 83 | |
| 84 if (service_iter == service_provider_map_.end()) { | |
| 80 error_callback.Run(bluetooth_advertising_manager::kErrorDoesNotExist, | 85 error_callback.Run(bluetooth_advertising_manager::kErrorDoesNotExist, |
| 81 "Advertisement not registered"); | 86 "Advertisement not registered"); |
| 82 } else if (advertisement_object_path != currently_registered_) { | 87 } else if (reg_iter == currently_registered_.end()) { |
| 83 error_callback.Run(bluetooth_advertising_manager::kErrorDoesNotExist, | 88 error_callback.Run(bluetooth_advertising_manager::kErrorDoesNotExist, |
| 84 "Does not exist"); | 89 "Does not exist"); |
| 85 } else { | 90 } else { |
| 86 currently_registered_ = dbus::ObjectPath(""); | 91 currently_registered_.erase(reg_iter); |
| 87 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | 92 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 88 } | 93 } |
| 89 } | 94 } |
| 90 | 95 |
| 91 void FakeBluetoothLEAdvertisingManagerClient::SetAdvertisingInterval( | 96 void FakeBluetoothLEAdvertisingManagerClient::SetAdvertisingInterval( |
| 92 const dbus::ObjectPath& object_path, | 97 const dbus::ObjectPath& object_path, |
| 93 uint16_t min_interval_ms, | 98 uint16_t min_interval_ms, |
| 94 uint16_t max_interval_ms, | 99 uint16_t max_interval_ms, |
| 95 const base::Closure& callback, | 100 const base::Closure& callback, |
| 96 const ErrorCallback& error_callback) { | 101 const ErrorCallback& error_callback) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 112 void FakeBluetoothLEAdvertisingManagerClient:: | 117 void FakeBluetoothLEAdvertisingManagerClient:: |
| 113 UnregisterAdvertisementServiceProvider( | 118 UnregisterAdvertisementServiceProvider( |
| 114 FakeBluetoothLEAdvertisementServiceProvider* service_provider) { | 119 FakeBluetoothLEAdvertisementServiceProvider* service_provider) { |
| 115 ServiceProviderMap::iterator iter = | 120 ServiceProviderMap::iterator iter = |
| 116 service_provider_map_.find(service_provider->object_path_); | 121 service_provider_map_.find(service_provider->object_path_); |
| 117 if (iter != service_provider_map_.end() && iter->second == service_provider) | 122 if (iter != service_provider_map_.end() && iter->second == service_provider) |
| 118 service_provider_map_.erase(iter); | 123 service_provider_map_.erase(iter); |
| 119 } | 124 } |
| 120 | 125 |
| 121 } // namespace bluez | 126 } // namespace bluez |
| OLD | NEW |