| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/bluez/bluetooth_local_gatt_service_bluez.h" | 5 #include <device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.h> |
| 6 | 6 |
| 7 #include "base/callback.h" | |
| 8 #include "base/guid.h" | 7 #include "base/guid.h" |
| 9 #include "base/logging.h" | 8 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 12 #include "dbus/object_path.h" | 11 #include "dbus/object_path.h" |
| 13 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" | 12 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" |
| 14 | 13 |
| 14 namespace device { |
| 15 |
| 16 // static |
| 17 base::WeakPtr<BluetoothLocalGattService> BluetoothLocalGattService::Create( |
| 18 BluetoothAdapter* adapter, |
| 19 const BluetoothUUID& uuid, |
| 20 bool is_primary, |
| 21 BluetoothLocalGattService* included_service, |
| 22 BluetoothLocalGattService::Delegate* delegate) { |
| 23 return bluez::BluetoothLocalGattServiceBlueZ::Create( |
| 24 adapter, uuid, is_primary, included_service, delegate); |
| 25 } |
| 26 |
| 27 } // device |
| 28 |
| 15 namespace bluez { | 29 namespace bluez { |
| 16 | 30 |
| 17 BluetoothLocalGattServiceBlueZ::BluetoothLocalGattServiceBlueZ( | 31 BluetoothLocalGattServiceBlueZ::BluetoothLocalGattServiceBlueZ( |
| 18 BluetoothAdapterBlueZ* adapter, | 32 BluetoothAdapterBlueZ* adapter, |
| 19 const device::BluetoothUUID& uuid, | 33 const device::BluetoothUUID& uuid, |
| 20 bool is_primary, | 34 bool is_primary, |
| 21 device::BluetoothLocalGattService::Delegate* delegate) | 35 device::BluetoothLocalGattService::Delegate* delegate) |
| 22 : BluetoothGattServiceBlueZ(adapter), | 36 : BluetoothGattServiceBlueZ(adapter), |
| 23 uuid_(uuid), | 37 uuid_(uuid), |
| 24 is_primary_(is_primary), | 38 is_primary_(is_primary), |
| 25 delegate_(delegate), | 39 delegate_(delegate), |
| 26 weak_ptr_factory_(this) { | 40 weak_ptr_factory_(this) { |
| 27 // TODO(rkc): Move this code in a common location. It is used by | 41 // TODO(rkc): Get base application path from adapter and prefix it here. |
| 28 // BluetoothAdvertisementBlueZ() also. | 42 object_path_ = AddGuidToObjectPath("/service"); |
| 29 std::string GuidString = base::GenerateGUID(); | |
| 30 base::RemoveChars(GuidString, "-", &GuidString); | |
| 31 object_path_ = dbus::ObjectPath(adapter_->object_path().value() + | |
| 32 "/service/" + GuidString); | |
| 33 VLOG(1) << "Creating local GATT service with identifier: " | 43 VLOG(1) << "Creating local GATT service with identifier: " |
| 34 << object_path_.value(); | 44 << object_path_.value(); |
| 35 } | 45 } |
| 36 | 46 |
| 37 BluetoothLocalGattServiceBlueZ::~BluetoothLocalGattServiceBlueZ() {} | 47 BluetoothLocalGattServiceBlueZ::~BluetoothLocalGattServiceBlueZ() {} |
| 38 | 48 |
| 39 device::BluetoothUUID BluetoothLocalGattServiceBlueZ::GetUUID() const { | 49 device::BluetoothUUID BluetoothLocalGattServiceBlueZ::GetUUID() const { |
| 40 return uuid_; | 50 return uuid_; |
| 41 } | 51 } |
| 42 | 52 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 56 static_cast<BluetoothAdapterBlueZ*>(adapter); | 66 static_cast<BluetoothAdapterBlueZ*>(adapter); |
| 57 BluetoothLocalGattServiceBlueZ* service = new BluetoothLocalGattServiceBlueZ( | 67 BluetoothLocalGattServiceBlueZ* service = new BluetoothLocalGattServiceBlueZ( |
| 58 adapter_bluez, uuid, is_primary, delegate); | 68 adapter_bluez, uuid, is_primary, delegate); |
| 59 adapter_bluez->AddLocalGattService(base::WrapUnique(service)); | 69 adapter_bluez->AddLocalGattService(base::WrapUnique(service)); |
| 60 return service->weak_ptr_factory_.GetWeakPtr(); | 70 return service->weak_ptr_factory_.GetWeakPtr(); |
| 61 } | 71 } |
| 62 | 72 |
| 63 void BluetoothLocalGattServiceBlueZ::Register( | 73 void BluetoothLocalGattServiceBlueZ::Register( |
| 64 const base::Closure& callback, | 74 const base::Closure& callback, |
| 65 const ErrorCallback& error_callback) { | 75 const ErrorCallback& error_callback) { |
| 66 // TODO(rkc): Call adapter_->RegisterGattService. | 76 // GetAdapter()->RegisterGattService(this, callback, error_callback); |
| 67 } | 77 } |
| 68 | 78 |
| 69 void BluetoothLocalGattServiceBlueZ::Unregister( | 79 void BluetoothLocalGattServiceBlueZ::Unregister( |
| 70 const base::Closure& callback, | 80 const base::Closure& callback, |
| 71 const ErrorCallback& error_callback) { | 81 const ErrorCallback& error_callback) { |
| 72 // TODO(rkc): Call adapter_->UnregisterGattService. | 82 DCHECK(GetAdapter()); |
| 83 // GetAdapter()->UnregisterGattService(this, callback, error_callback); |
| 73 } | 84 } |
| 74 | 85 |
| 75 void BluetoothLocalGattServiceBlueZ::OnRegistrationError( | 86 const std::vector<std::unique_ptr<BluetoothLocalGattCharacteristicBlueZ>>& |
| 76 const ErrorCallback& error_callback, | 87 BluetoothLocalGattServiceBlueZ::GetCharacteristics() const { |
| 77 const std::string& error_name, | 88 return characteristics_; |
| 78 const std::string& error_message) { | 89 } |
| 79 VLOG(1) << "[Un]Register Service failed: " << error_name | 90 |
| 80 << ", message: " << error_message; | 91 void BluetoothLocalGattServiceBlueZ::AddCharacteristic( |
| 81 error_callback.Run( | 92 std::unique_ptr<BluetoothLocalGattCharacteristicBlueZ> characteristic) { |
| 82 BluetoothGattServiceBlueZ::DBusErrorToServiceError(error_name)); | 93 characteristics_.push_back(std::move(characteristic)); |
| 94 } |
| 95 |
| 96 // static |
| 97 dbus::ObjectPath BluetoothLocalGattServiceBlueZ::AddGuidToObjectPath( |
| 98 const std::string& path) { |
| 99 std::string GuidString = base::GenerateGUID(); |
| 100 base::RemoveChars(GuidString, "-", &GuidString); |
| 101 |
| 102 return dbus::ObjectPath(path + GuidString); |
| 83 } | 103 } |
| 84 | 104 |
| 85 } // namespace bluez | 105 } // namespace bluez |
| OLD | NEW |