OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_SERVICE_PROVIDER_IMP
L_H_ |
| 6 #define DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_SERVICE_PROVIDER_IMP
L_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/threading/platform_thread.h" |
| 16 #include "dbus/bus.h" |
| 17 #include "dbus/exported_object.h" |
| 18 #include "dbus/message.h" |
| 19 #include "dbus/object_path.h" |
| 20 #include "device/bluetooth/dbus/bluetooth_gatt_attribute_value_delegate.h" |
| 21 #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider.h
" |
| 22 |
| 23 namespace bluez { |
| 24 |
| 25 // The BluetoothGattCharacteristicServiceProvider implementation used in |
| 26 // production. |
| 27 class DEVICE_BLUETOOTH_EXPORT BluetoothGattCharacteristicServiceProviderImpl |
| 28 : public BluetoothGattCharacteristicServiceProvider { |
| 29 public: |
| 30 BluetoothGattCharacteristicServiceProviderImpl( |
| 31 dbus::Bus* bus, |
| 32 const dbus::ObjectPath& object_path, |
| 33 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate, |
| 34 const std::string& uuid, |
| 35 const std::vector<std::string>& flags, |
| 36 const std::vector<std::string>& permissions, |
| 37 const dbus::ObjectPath& service_path); |
| 38 |
| 39 ~BluetoothGattCharacteristicServiceProviderImpl() override; |
| 40 |
| 41 // For testing. |
| 42 BluetoothGattCharacteristicServiceProviderImpl( |
| 43 const dbus::ObjectPath& object_path, |
| 44 const std::string& uuid, |
| 45 const dbus::ObjectPath& service_path); |
| 46 |
| 47 // BluetoothGattCharacteristicServiceProvider override. |
| 48 void SendValueChanged(const std::vector<uint8_t>& value) override; |
| 49 |
| 50 private: |
| 51 // Returns true if the current thread is on the origin thread. |
| 52 bool OnOriginThread(); |
| 53 |
| 54 // Called by dbus:: when the Bluetooth daemon fetches a single property of |
| 55 // the characteristic. |
| 56 void Get(dbus::MethodCall* method_call, |
| 57 dbus::ExportedObject::ResponseSender response_sender); |
| 58 |
| 59 // Called by dbus:: when the Bluetooth daemon sets a single property of the |
| 60 // characteristic. |
| 61 void Set(dbus::MethodCall* method_call, |
| 62 dbus::ExportedObject::ResponseSender response_sender); |
| 63 |
| 64 // Called by dbus:: when the Bluetooth daemon fetches all properties of the |
| 65 // characteristic. |
| 66 void GetAll(dbus::MethodCall* method_call, |
| 67 dbus::ExportedObject::ResponseSender response_sender); |
| 68 |
| 69 // Called by dbus:: when a method is exported. |
| 70 void OnExported(const std::string& interface_name, |
| 71 const std::string& method_name, |
| 72 bool success); |
| 73 |
| 74 // Called by the Delegate in response to a method to call to get all |
| 75 // properties, in which the delegate has successfully returned the |
| 76 // characteristic value. |
| 77 void OnGetAll(dbus::MethodCall* method_call, |
| 78 dbus::ExportedObject::ResponseSender response_sender, |
| 79 const std::vector<uint8_t>& value); |
| 80 |
| 81 // Writes an array of the service's properties into the provided writer. |
| 82 void WriteProperties(dbus::MessageWriter* writer, |
| 83 const std::vector<uint8_t>* value) override; |
| 84 |
| 85 // Called by the Delegate in response to a successful method call to get the |
| 86 // characteristic value. |
| 87 void OnGet(dbus::MethodCall* method_call, |
| 88 dbus::ExportedObject::ResponseSender response_sender, |
| 89 const std::vector<uint8_t>& value); |
| 90 |
| 91 // Called by the Delegate in response to a successful method call to set the |
| 92 // characteristic value. |
| 93 void OnSet(dbus::MethodCall* method_call, |
| 94 dbus::ExportedObject::ResponseSender response_sender); |
| 95 |
| 96 // Called by the Delegate in response to a failed method call to get or set |
| 97 // the characteristic value. |
| 98 void OnFailure(dbus::MethodCall* method_call, |
| 99 dbus::ExportedObject::ResponseSender response_sender); |
| 100 |
| 101 const dbus::ObjectPath& object_path() const override; |
| 102 |
| 103 // Origin thread (i.e. the UI thread in production). |
| 104 base::PlatformThreadId origin_thread_id_; |
| 105 |
| 106 // 128-bit characteristic UUID of this object. |
| 107 std::string uuid_; |
| 108 |
| 109 // D-Bus bus object is exported on, not owned by this object and must |
| 110 // outlive it. |
| 111 dbus::Bus* bus_; |
| 112 |
| 113 // Incoming methods to get and set the "Value" property are passed on to the |
| 114 // delegate and callbacks passed to generate a reply. |delegate_| is generally |
| 115 // the object that owns this one and must outlive it. |
| 116 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate_; |
| 117 |
| 118 // D-Bus object path of object we are exporting, kept so we can unregister |
| 119 // again in our destructor. |
| 120 dbus::ObjectPath object_path_; |
| 121 |
| 122 // Object path of the GATT service that the exported characteristic belongs |
| 123 // to. |
| 124 dbus::ObjectPath service_path_; |
| 125 |
| 126 // D-Bus object we are exporting, owned by this object. |
| 127 scoped_refptr<dbus::ExportedObject> exported_object_; |
| 128 |
| 129 // Weak pointer factory for generating 'this' pointers that might live longer |
| 130 // than we do. |
| 131 // Note: This should remain the last member so it'll be destroyed and |
| 132 // invalidate its weak pointers before any other members are destroyed. |
| 133 base::WeakPtrFactory<BluetoothGattCharacteristicServiceProviderImpl> |
| 134 weak_ptr_factory_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristicServiceProviderImpl); |
| 137 }; |
| 138 |
| 139 } // namespace bluez |
| 140 |
| 141 #endif // DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_SERVICE_PROVIDER_
IMPL_H_ |
OLD | NEW |