From 2034b1bb10720a2f0e6cc97427346f2320c115bc Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 23 Nov 2018 09:40:48 +0100 Subject: o Starting to handle notifications and indications Cleaning up: o Using more of the shared_ptr typedefs. o Adding code styles used by CLion. --- include/ble/Bluetooth.h | 38 ++++++++++++++++++++++++-------------- include/ble/att.h | 21 ++++++++++++++++++++- 2 files changed, 44 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/include/ble/Bluetooth.h b/include/ble/Bluetooth.h index 914dc26..5f7cd17 100644 --- a/include/ble/Bluetooth.h +++ b/include/ble/Bluetooth.h @@ -10,6 +10,7 @@ #include #include #include +#include #include "ble/ByteBuffer.h" #include "ble/att.h" @@ -17,7 +18,6 @@ namespace trygvis { namespace bluetooth { -using std::shared_ptr; using std::string; using std::vector; using std::map; @@ -33,10 +33,12 @@ class BluetoothGattService; class BluetoothGattCharacteristic; class BluetoothGattDescriptor; -typedef shared_ptr BluetoothGattPtr; -typedef shared_ptr BluetoothGattServicePtr; -typedef shared_ptr BluetoothGattCharacteristicPtr; -typedef shared_ptr BluetoothGattDescriptorPtr; +typedef std::shared_ptr BluetoothGattPtr; +typedef std::shared_ptr BluetoothGattServicePtr; +typedef std::shared_ptr BluetoothGattCharacteristicPtr; +typedef std::shared_ptr BluetoothGattDescriptorPtr; +typedef std::shared_ptr BluetoothDevicePtr; +typedef std::shared_ptr BluetoothAdapterPtr; class Mac { public: @@ -65,6 +67,12 @@ private: uint8_t bytes[6]; }; +class BluetoothCallback { + virtual ~BluetoothCallback() = default; + + virtual void onCharacteristicChanged(BluetoothGattPtr& gatt, BluetoothGattCharacteristicPtr& characteristic) = 0; +}; + class BluetoothGattDescriptor { public: static const uint8_t DISABLE_NOTIFICATION_VALUE[2]; @@ -104,9 +112,9 @@ public: virtual uint16_t getValueHandle() const = 0; - virtual shared_ptr getDescriptor(Uuid uuid) const = 0; + virtual BluetoothGattDescriptorPtr getDescriptor(Uuid uuid) const = 0; - virtual shared_ptr getDescriptor(ShortUuid uuid) const { + virtual BluetoothGattDescriptorPtr getDescriptor(ShortUuid uuid) const { return getDescriptor(uuid.toLong()); }; }; @@ -123,7 +131,7 @@ public: virtual uint16_t getEndGroupHandle() const = 0; - virtual vector> getCharacteristics() const = 0; + virtual vector getCharacteristics() const = 0; virtual o findCharacteristic(Uuid uuid) = 0; @@ -155,7 +163,9 @@ public: virtual void discoverServices() = 0; - virtual vector> getServices() const = 0; + virtual void process(std::chrono::system_clock::duration max_duration) = 0; + + virtual vector getServices() const = 0; virtual o findService(Uuid uuid) = 0; @@ -174,7 +184,7 @@ public: virtual BluetoothAdapter &getAdapter() = 0; - virtual shared_ptr connectGatt() = 0; + virtual BluetoothGattPtr connectGatt(BluetoothCallback* callback = nullptr) = 0; }; class BluetoothAdapter { @@ -185,9 +195,9 @@ public: virtual void stopScan() = 0; - virtual void runScan(std::function &device)>) = 0; + virtual void runScan(std::function) = 0; - virtual shared_ptr getDevice(Mac &mac) = 0; + virtual BluetoothDevicePtr getDevice(Mac &mac) = 0; protected: BluetoothAdapter(); @@ -206,10 +216,10 @@ public: ~BluetoothSystem(); - shared_ptr getAdapter(string name); + BluetoothAdapterPtr getAdapter(string name); private: - map> adapters; + map adapters; }; } // namespace bluetooth diff --git a/include/ble/att.h b/include/ble/att.h index db21d2e..152a779 100644 --- a/include/ble/att.h +++ b/include/ble/att.h @@ -130,11 +130,26 @@ struct ReadByTypeRes { std::vector attributes; }; +struct HandleValueNotification { + static constexpr auto att_pdu_type = AttPduType::HANDLE_VALUE_NOTIFICATION; + + uint16_t handle; + ByteBuffer data; +}; + +struct HandleValueIndication { + static constexpr auto att_pdu_type = AttPduType::HANDLE_VALUE_INDICATION; + + uint16_t handle; + ByteBuffer data; +}; + using AttVariant = std::variant; + ReadByGroupTypeRes, ReadByTypeRes, + HandleValueNotification, HandleValueIndication>; o attPduType(const AttVariant &v); @@ -154,6 +169,10 @@ public: static FindInformationRes parseFindInformationRes(ByteBuffer &bytes); + static HandleValueNotification parseHandleValueNotification(ByteBuffer &bytes); + + static HandleValueIndication parseHandleValueIndication(ByteBuffer &bytes); + static void parseRead(ByteBuffer &bytes); static void parseWrite(ByteBuffer &bytes); -- cgit v1.2.3