| 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 "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_api.h" | 5 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_api.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <iterator> | 9 #include <iterator> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 std::unique_ptr<device::BluetoothAdvertisement::ServiceData> created_data( | 164 std::unique_ptr<device::BluetoothAdvertisement::ServiceData> created_data( |
| 165 new device::BluetoothAdvertisement::ServiceData()); | 165 new device::BluetoothAdvertisement::ServiceData()); |
| 166 for (const auto& it : *service_data) { | 166 for (const auto& it : *service_data) { |
| 167 std::vector<uint8_t> data(it.data.size()); | 167 std::vector<uint8_t> data(it.data.size()); |
| 168 std::copy(it.data.begin(), it.data.end(), data.begin()); | 168 std::copy(it.data.begin(), it.data.end(), data.begin()); |
| 169 (*created_data)[it.uuid] = data; | 169 (*created_data)[it.uuid] = data; |
| 170 } | 170 } |
| 171 return created_data; | 171 return created_data; |
| 172 } | 172 } |
| 173 | 173 |
| 174 bool HasProperty( |
| 175 const std::vector<apibtle::CharacteristicProperty>& api_properties, |
| 176 apibtle::CharacteristicProperty property) { |
| 177 if (find(api_properties.begin(), api_properties.end(), property) != |
| 178 api_properties.end()) |
| 179 return true; |
| 180 return false; |
| 181 } |
| 182 |
| 183 device::BluetoothGattCharacteristic::Properties GetBluetoothProperties( |
| 184 const std::vector<apibtle::CharacteristicProperty>& api_properties) { |
| 185 device::BluetoothGattCharacteristic::Properties properties = |
| 186 device::BluetoothGattCharacteristic::PROPERTY_NONE; |
| 187 |
| 188 static_assert( |
| 189 apibtle::CHARACTERISTIC_PROPERTY_LAST == 10, |
| 190 "Update required if the number of characteristic properties changes."); |
| 191 |
| 192 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_BROADCAST)) |
| 193 properties |= device::BluetoothGattCharacteristic::PROPERTY_BROADCAST; |
| 194 |
| 195 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_READ)) |
| 196 properties |= device::BluetoothGattCharacteristic::PROPERTY_READ; |
| 197 if (HasProperty(api_properties, |
| 198 apibtle::CHARACTERISTIC_PROPERTY_WRITEWITHOUTRESPONSE)) |
| 199 properties |= |
| 200 device::BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE; |
| 201 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_WRITE)) |
| 202 properties |= device::BluetoothGattCharacteristic::PROPERTY_WRITE; |
| 203 |
| 204 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_NOTIFY)) |
| 205 properties |= device::BluetoothGattCharacteristic::PROPERTY_NOTIFY; |
| 206 |
| 207 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_INDICATE)) |
| 208 properties |= device::BluetoothGattCharacteristic::PROPERTY_INDICATE; |
| 209 |
| 210 if (HasProperty(api_properties, |
| 211 apibtle::CHARACTERISTIC_PROPERTY_AUTHENTICATEDSIGNEDWRITES)) |
| 212 properties |= device::BluetoothGattCharacteristic:: |
| 213 PROPERTY_AUTHENTICATED_SIGNED_WRITES; |
| 214 if (HasProperty(api_properties, |
| 215 apibtle::CHARACTERISTIC_PROPERTY_EXTENDEDPROPERTIES)) |
| 216 properties |= |
| 217 device::BluetoothGattCharacteristic::PROPERTY_EXTENDED_PROPERTIES; |
| 218 if (HasProperty(api_properties, |
| 219 apibtle::CHARACTERISTIC_PROPERTY_RELIABLEWRITE)) |
| 220 properties |= device::BluetoothGattCharacteristic::PROPERTY_RELIABLE_WRITE; |
| 221 if (HasProperty(api_properties, |
| 222 apibtle::CHARACTERISTIC_PROPERTY_WRITABLEAUXILIARIES)) |
| 223 properties |= |
| 224 device::BluetoothGattCharacteristic::PROPERTY_WRITABLE_AUXILIARIES; |
| 225 |
| 226 return properties; |
| 227 } |
| 228 |
| 174 } // namespace | 229 } // namespace |
| 175 | 230 |
| 176 | 231 |
| 177 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > | 232 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > |
| 178 g_factory = LAZY_INSTANCE_INITIALIZER; | 233 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 179 | 234 |
| 180 // static | 235 // static |
| 181 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* | 236 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* |
| 182 BluetoothLowEnergyAPI::GetFactoryInstance() { | 237 BluetoothLowEnergyAPI::GetFactoryInstance() { |
| 183 return g_factory.Pointer(); | 238 return g_factory.Pointer(); |
| (...skipping 989 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1173 device::BluetoothLocalGattService* service = | 1228 device::BluetoothLocalGattService* service = |
| 1174 event_router_->adapter()->GetGattService(params_->service_id); | 1229 event_router_->adapter()->GetGattService(params_->service_id); |
| 1175 if (!service) { | 1230 if (!service) { |
| 1176 Respond(Error(kErrorInvalidServiceId)); | 1231 Respond(Error(kErrorInvalidServiceId)); |
| 1177 return; | 1232 return; |
| 1178 } | 1233 } |
| 1179 | 1234 |
| 1180 base::WeakPtr<device::BluetoothLocalGattCharacteristic> characteristic = | 1235 base::WeakPtr<device::BluetoothLocalGattCharacteristic> characteristic = |
| 1181 device::BluetoothLocalGattCharacteristic::Create( | 1236 device::BluetoothLocalGattCharacteristic::Create( |
| 1182 device::BluetoothUUID(params_->characteristic.uuid), | 1237 device::BluetoothUUID(params_->characteristic.uuid), |
| 1183 device::BluetoothGattCharacteristic::Properties(), | 1238 GetBluetoothProperties(params_->characteristic.properties), |
| 1184 device::BluetoothGattCharacteristic::Permissions(), service); | 1239 device::BluetoothGattCharacteristic::Permissions(), service); |
| 1185 | 1240 |
| 1186 // Keep a track of this characteristic so we can look it up later if a | 1241 // Keep a track of this characteristic so we can look it up later if a |
| 1187 // descriptor lists it as its parent. | 1242 // descriptor lists it as its parent. |
| 1188 event_router_->AddLocalCharacteristic(characteristic->GetIdentifier(), | 1243 event_router_->AddLocalCharacteristic(characteristic->GetIdentifier(), |
| 1189 service->GetIdentifier()); | 1244 service->GetIdentifier()); |
| 1190 | 1245 |
| 1191 Respond(ArgumentList(apibtle::CreateCharacteristic::Results::Create( | 1246 Respond(ArgumentList(apibtle::CreateCharacteristic::Results::Create( |
| 1192 characteristic->GetIdentifier()))); | 1247 characteristic->GetIdentifier()))); |
| 1193 } | 1248 } |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1270 std::vector<uint8_t> uint8_vector(params_->response.value->begin(), | 1325 std::vector<uint8_t> uint8_vector(params_->response.value->begin(), |
| 1271 params_->response.value->end()); | 1326 params_->response.value->end()); |
| 1272 event_router_->HandleRequestResponse( | 1327 event_router_->HandleRequestResponse( |
| 1273 extension(), params_->response.request_id, params_->response.is_error, | 1328 extension(), params_->response.request_id, params_->response.is_error, |
| 1274 uint8_vector); | 1329 uint8_vector); |
| 1275 Respond(NoArguments()); | 1330 Respond(NoArguments()); |
| 1276 } | 1331 } |
| 1277 | 1332 |
| 1278 } // namespace api | 1333 } // namespace api |
| 1279 } // namespace extensions | 1334 } // namespace extensions |
| OLD | NEW |