| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_service_provider.
h" | 5 #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_service_provider.
h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 8 #include <iterator> |
| 9 |
| 7 #include "base/callback.h" | 10 #include "base/callback.h" |
| 8 #include "base/logging.h" | 11 #include "base/logging.h" |
| 9 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "device/bluetooth/dbus/bluetooth_gatt_attribute_value_delegate.h" |
| 10 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 14 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| 11 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_service_provi
der.h" | 15 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_service_provi
der.h" |
| 12 #include "device/bluetooth/dbus/fake_bluetooth_gatt_manager_client.h" | 16 #include "device/bluetooth/dbus/fake_bluetooth_gatt_manager_client.h" |
| 17 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 13 | 18 |
| 14 namespace bluez { | 19 namespace bluez { |
| 15 | 20 |
| 21 namespace { |
| 22 |
| 23 bool CanWrite(const std::vector<std::string>& flags) { |
| 24 if (find(flags.begin(), flags.end(), bluetooth_gatt_descriptor::kFlagWrite) != |
| 25 flags.end()) |
| 26 return true; |
| 27 if (find(flags.begin(), flags.end(), |
| 28 bluetooth_gatt_descriptor::kFlagEncryptWrite) != flags.end()) |
| 29 return true; |
| 30 if (find(flags.begin(), flags.end(), |
| 31 bluetooth_gatt_descriptor::kFlagEncryptAuthenticatedWrite) != |
| 32 flags.end()) |
| 33 return true; |
| 34 return false; |
| 35 } |
| 36 |
| 37 bool CanRead(const std::vector<std::string>& flags) { |
| 38 if (find(flags.begin(), flags.end(), bluetooth_gatt_descriptor::kFlagRead) != |
| 39 flags.end()) |
| 40 return true; |
| 41 if (find(flags.begin(), flags.end(), |
| 42 bluetooth_gatt_descriptor::kFlagEncryptRead) != flags.end()) |
| 43 return true; |
| 44 if (find(flags.begin(), flags.end(), |
| 45 bluetooth_gatt_descriptor::kFlagEncryptAuthenticatedRead) != |
| 46 flags.end()) |
| 47 return true; |
| 48 return false; |
| 49 } |
| 50 |
| 51 } // namespace |
| 52 |
| 16 FakeBluetoothGattDescriptorServiceProvider:: | 53 FakeBluetoothGattDescriptorServiceProvider:: |
| 17 FakeBluetoothGattDescriptorServiceProvider( | 54 FakeBluetoothGattDescriptorServiceProvider( |
| 18 const dbus::ObjectPath& object_path, | 55 const dbus::ObjectPath& object_path, |
| 19 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate, | 56 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate, |
| 20 const std::string& uuid, | 57 const std::string& uuid, |
| 21 const std::vector<std::string>& flags, | 58 const std::vector<std::string>& flags, |
| 22 const dbus::ObjectPath& characteristic_path) | 59 const dbus::ObjectPath& characteristic_path) |
| 23 : object_path_(object_path), | 60 : object_path_(object_path), |
| 24 uuid_(uuid), | 61 uuid_(uuid), |
| 62 flags_(flags), |
| 25 characteristic_path_(characteristic_path), | 63 characteristic_path_(characteristic_path), |
| 26 delegate_(std::move(delegate)) { | 64 delegate_(std::move(delegate)) { |
| 27 VLOG(1) << "Creating Bluetooth GATT descriptor: " << object_path_.value(); | 65 VLOG(1) << "Creating Bluetooth GATT descriptor: " << object_path_.value(); |
| 28 | 66 |
| 29 DCHECK(object_path_.IsValid()); | 67 DCHECK(object_path_.IsValid()); |
| 30 DCHECK(characteristic_path_.IsValid()); | 68 DCHECK(characteristic_path_.IsValid()); |
| 31 DCHECK(!uuid.empty()); | 69 DCHECK(!uuid.empty()); |
| 32 DCHECK(delegate_); | 70 DCHECK(delegate_); |
| 33 DCHECK(base::StartsWith(object_path_.value(), | 71 DCHECK(base::StartsWith(object_path_.value(), |
| 34 characteristic_path_.value() + "/", | 72 characteristic_path_.value() + "/", |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 << characteristic_path_.value(); | 113 << characteristic_path_.value(); |
| 76 return; | 114 return; |
| 77 } | 115 } |
| 78 if (!fake_bluetooth_gatt_manager_client->IsServiceRegistered( | 116 if (!fake_bluetooth_gatt_manager_client->IsServiceRegistered( |
| 79 characteristic->service_path())) { | 117 characteristic->service_path())) { |
| 80 VLOG(1) << "GATT descriptor not registered."; | 118 VLOG(1) << "GATT descriptor not registered."; |
| 81 error_callback.Run(); | 119 error_callback.Run(); |
| 82 return; | 120 return; |
| 83 } | 121 } |
| 84 | 122 |
| 123 if (!CanRead(flags_)) { |
| 124 VLOG(1) << "GATT descriptor not readable."; |
| 125 error_callback.Run(); |
| 126 return; |
| 127 } |
| 128 |
| 85 // Pass on to the delegate. | 129 // Pass on to the delegate. |
| 86 DCHECK(delegate_); | 130 DCHECK(delegate_); |
| 87 delegate_->GetValue(callback, error_callback); | 131 delegate_->GetValue(callback, error_callback); |
| 88 } | 132 } |
| 89 | 133 |
| 90 void FakeBluetoothGattDescriptorServiceProvider::SetValue( | 134 void FakeBluetoothGattDescriptorServiceProvider::SetValue( |
| 91 const std::vector<uint8_t>& value, | 135 const std::vector<uint8_t>& value, |
| 92 const base::Closure& callback, | 136 const base::Closure& callback, |
| 93 const device::BluetoothLocalGattService::Delegate::ErrorCallback& | 137 const device::BluetoothLocalGattService::Delegate::ErrorCallback& |
| 94 error_callback) { | 138 error_callback) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 107 << characteristic_path_.value(); | 151 << characteristic_path_.value(); |
| 108 return; | 152 return; |
| 109 } | 153 } |
| 110 if (!fake_bluetooth_gatt_manager_client->IsServiceRegistered( | 154 if (!fake_bluetooth_gatt_manager_client->IsServiceRegistered( |
| 111 characteristic->service_path())) { | 155 characteristic->service_path())) { |
| 112 VLOG(1) << "GATT descriptor not registered."; | 156 VLOG(1) << "GATT descriptor not registered."; |
| 113 error_callback.Run(); | 157 error_callback.Run(); |
| 114 return; | 158 return; |
| 115 } | 159 } |
| 116 | 160 |
| 161 if (!CanWrite(flags_)) { |
| 162 VLOG(1) << "GATT descriptor not writeable."; |
| 163 error_callback.Run(); |
| 164 return; |
| 165 } |
| 166 |
| 117 // Pass on to the delegate. | 167 // Pass on to the delegate. |
| 118 DCHECK(delegate_); | 168 DCHECK(delegate_); |
| 119 delegate_->SetValue(value, callback, error_callback); | 169 delegate_->SetValue(value, callback, error_callback); |
| 120 } | 170 } |
| 121 | 171 |
| 122 const dbus::ObjectPath& | 172 const dbus::ObjectPath& |
| 123 FakeBluetoothGattDescriptorServiceProvider::object_path() const { | 173 FakeBluetoothGattDescriptorServiceProvider::object_path() const { |
| 124 return object_path_; | 174 return object_path_; |
| 125 } | 175 } |
| 126 | 176 |
| 127 } // namespace bluez | 177 } // namespace bluez |
| OLD | NEW |