| 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 #ifndef DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_LOCAL_GATT_SERVICE_BLUEZ_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_LOCAL_GATT_SERVICE_BLUEZ_H_ |
| 6 #define DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_LOCAL_GATT_SERVICE_BLUEZ_H_ | 6 #define DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_LOCAL_GATT_SERVICE_BLUEZ_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 13 #include "device/bluetooth/bluetooth_local_gatt_service.h" | 14 #include "device/bluetooth/bluetooth_local_gatt_service.h" |
| 14 #include "device/bluetooth/bluetooth_uuid.h" | 15 #include "device/bluetooth/bluetooth_uuid.h" |
| 15 #include "device/bluetooth/bluez/bluetooth_gatt_service_bluez.h" | 16 #include "device/bluetooth/bluez/bluetooth_gatt_service_bluez.h" |
| 17 #include "device/bluetooth/bluez/bluetooth_local_gatt_characteristic_bluez.h" |
| 16 | 18 |
| 17 namespace device { | 19 namespace device { |
| 18 | 20 |
| 19 class BluetoothAdapter; | 21 class BluetoothAdapter; |
| 20 class BluetoothDevice; | 22 class BluetoothDevice; |
| 21 | 23 |
| 22 } // namespace device | 24 } // namespace device |
| 23 | 25 |
| 24 namespace bluez { | 26 namespace bluez { |
| 25 | 27 |
| 26 class BluetoothAdapterBlueZ; | 28 class BluetoothAdapterBlueZ; |
| 27 class BluetoothDeviceBlueZ; | 29 class BluetoothLocalGattCharacteristicBlueZ; |
| 28 | 30 |
| 29 // The BluetoothLocalGattServiceBlueZ class implements BluetootGattService | 31 // The BluetoothLocalGattServiceBlueZ class implements BluetootGattService |
| 30 // for local GATT services for platforms that use BlueZ. | 32 // for local GATT services for platforms that use BlueZ. |
| 31 class BluetoothLocalGattServiceBlueZ | 33 class BluetoothLocalGattServiceBlueZ |
| 32 : public BluetoothGattServiceBlueZ, | 34 : public BluetoothGattServiceBlueZ, |
| 33 public device::BluetoothLocalGattService { | 35 public device::BluetoothLocalGattService { |
| 34 public: | 36 public: |
| 35 // device::BluetoothGattService overrides. | 37 // A simpler interface for reacting to GATT attribute value requests by the |
| 36 device::BluetoothUUID GetUUID() const override; | 38 // DBus attribute service providers. |
| 37 bool IsPrimary() const override; | 39 class AttributeValueDelegate { |
| 40 public: |
| 41 virtual ~AttributeValueDelegate() {} |
| 38 | 42 |
| 39 // device::BluetoothLocalGattService overrides. | 43 // This method will be called when a remote device requests to read the |
| 44 // value of the exported GATT attribute. Invoke |callback| with a value |
| 45 // to return that value to the requester. Invoke |error_callback| to report |
| 46 // a failure to read the value. This can happen, for example, if the |
| 47 // attribute has no read permission set. Either callback should be |
| 48 // invoked after a reasonable amount of time, since the request will time |
| 49 // out if left pending for too long causing a disconnection. |
| 50 virtual void GetValue( |
| 51 const device::BluetoothLocalGattService::Delegate::ValueCallback& |
| 52 callback, |
| 53 const device::BluetoothLocalGattService::Delegate::ErrorCallback& |
| 54 error_callback) = 0; |
| 55 |
| 56 // This method will be called, when a remote device requests to write the |
| 57 // value of the exported GATT attribute. Invoke |callback| to report |
| 58 // that the value was successfully written. Invoke |error_callback| to |
| 59 // report a failure to write the value. This can happen, for example, if the |
| 60 // attribute has no write permission set. Either callback should be |
| 61 // invoked after a reasonable amount of time, since the request will time |
| 62 // out if left pending for too long causing a disconnection. |
| 63 virtual void SetValue( |
| 64 const std::vector<uint8_t>& value, |
| 65 const base::Closure& callback, |
| 66 const device::BluetoothLocalGattService::Delegate::ErrorCallback& |
| 67 error_callback) = 0; |
| 68 }; |
| 69 |
| 40 static base::WeakPtr<device::BluetoothLocalGattService> Create( | 70 static base::WeakPtr<device::BluetoothLocalGattService> Create( |
| 41 device::BluetoothAdapter* adapter, | 71 device::BluetoothAdapter* adapter, |
| 42 const device::BluetoothUUID& uuid, | 72 const device::BluetoothUUID& uuid, |
| 43 bool is_primary, | 73 bool is_primary, |
| 44 BluetoothLocalGattService* included_service, | 74 BluetoothLocalGattService* included_service, |
| 45 BluetoothLocalGattService::Delegate* delegate); | 75 BluetoothLocalGattService::Delegate* delegate); |
| 46 void Register(const base::Closure& callback, | |
| 47 const ErrorCallback& error_callback) override; | |
| 48 void Unregister(const base::Closure& callback, | |
| 49 const ErrorCallback& error_callback) override; | |
| 50 | 76 |
| 51 BluetoothLocalGattServiceBlueZ( | 77 BluetoothLocalGattServiceBlueZ( |
| 52 BluetoothAdapterBlueZ* adapter, | 78 BluetoothAdapterBlueZ* adapter, |
| 53 const device::BluetoothUUID& uuid, | 79 const device::BluetoothUUID& uuid, |
| 54 bool is_primary, | 80 bool is_primary, |
| 55 device::BluetoothLocalGattService::Delegate* delegate); | 81 device::BluetoothLocalGattService::Delegate* delegate); |
| 82 |
| 56 ~BluetoothLocalGattServiceBlueZ() override; | 83 ~BluetoothLocalGattServiceBlueZ() override; |
| 57 | 84 |
| 85 // device::BluetoothGattService overrides. |
| 86 device::BluetoothUUID GetUUID() const override; |
| 87 bool IsPrimary() const override; |
| 88 |
| 89 // device::BluetoothLocalGattService overrides. |
| 90 void Register(const base::Closure& callback, |
| 91 const ErrorCallback& error_callback) override; |
| 92 void Unregister(const base::Closure& callback, |
| 93 const ErrorCallback& error_callback) override; |
| 94 |
| 95 const std::vector<std::unique_ptr<BluetoothLocalGattCharacteristicBlueZ>>& |
| 96 GetCharacteristics() const; |
| 97 |
| 98 Delegate* GetDelegate() { return delegate_; } |
| 99 |
| 100 static dbus::ObjectPath AddGuidToObjectPath(const std::string& path); |
| 101 |
| 58 private: | 102 private: |
| 103 friend class BluetoothLocalGattCharacteristicBlueZ; |
| 104 |
| 59 // Called by dbus:: on unsuccessful completion of a request to register a | 105 // Called by dbus:: on unsuccessful completion of a request to register a |
| 60 // local service. | 106 // local service. |
| 61 void OnRegistrationError(const ErrorCallback& error_callback, | 107 void OnRegistrationError(const ErrorCallback& error_callback, |
| 62 const std::string& error_name, | 108 const std::string& error_name, |
| 63 const std::string& error_message); | 109 const std::string& error_message); |
| 64 | 110 |
| 65 BluetoothAdapterBlueZ* adapter_; | 111 void AddCharacteristic( |
| 66 const device::BluetoothUUID& uuid_; | 112 std::unique_ptr<BluetoothLocalGattCharacteristicBlueZ> characteristic); |
| 113 |
| 114 // UUID of this service. |
| 115 device::BluetoothUUID uuid_; |
| 116 |
| 117 // If this service is primary. |
| 67 bool is_primary_; | 118 bool is_primary_; |
| 119 |
| 120 // Delegate to receive read/write requests for attribute values contained |
| 121 // in this service. |
| 68 device::BluetoothLocalGattService::Delegate* delegate_; | 122 device::BluetoothLocalGattService::Delegate* delegate_; |
| 69 | 123 |
| 124 // Characteristics contained by this service. |
| 125 std::vector<std::unique_ptr<BluetoothLocalGattCharacteristicBlueZ>> |
| 126 characteristics_; |
| 127 |
| 70 // Note: This should remain the last member so it'll be destroyed and | 128 // Note: This should remain the last member so it'll be destroyed and |
| 71 // invalidate its weak pointers before any other members are destroyed. | 129 // invalidate its weak pointers before any other members are destroyed. |
| 72 base::WeakPtrFactory<BluetoothLocalGattServiceBlueZ> weak_ptr_factory_; | 130 base::WeakPtrFactory<BluetoothLocalGattServiceBlueZ> weak_ptr_factory_; |
| 73 | 131 |
| 74 DISALLOW_COPY_AND_ASSIGN(BluetoothLocalGattServiceBlueZ); | 132 DISALLOW_COPY_AND_ASSIGN(BluetoothLocalGattServiceBlueZ); |
| 75 }; | 133 }; |
| 76 | 134 |
| 77 } // namespace bluez | 135 } // namespace bluez |
| 78 | 136 |
| 79 #endif // DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_LOCAL_GATT_SERVICE_BLUEZ_H_ | 137 #endif // DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_LOCAL_GATT_SERVICE_BLUEZ_H_ |
| OLD | NEW |