diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2018-11-23 09:40:48 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2018-11-23 09:40:48 +0100 |
commit | 2034b1bb10720a2f0e6cc97427346f2320c115bc (patch) | |
tree | c382c6e404d48078aac71e2fd3f7f34587b99e7b /include/ble | |
parent | 0e2228fae626ec617a6a1f042ceb6dbdbd774558 (diff) | |
download | ble-toys-2034b1bb10720a2f0e6cc97427346f2320c115bc.tar.gz ble-toys-2034b1bb10720a2f0e6cc97427346f2320c115bc.tar.bz2 ble-toys-2034b1bb10720a2f0e6cc97427346f2320c115bc.tar.xz ble-toys-2034b1bb10720a2f0e6cc97427346f2320c115bc.zip |
o Starting to handle notifications and indications
Cleaning up:
o Using more of the shared_ptr typedefs.
o Adding code styles used by CLion.
Diffstat (limited to 'include/ble')
-rw-r--r-- | include/ble/Bluetooth.h | 38 | ||||
-rw-r--r-- | include/ble/att.h | 21 |
2 files changed, 44 insertions, 15 deletions
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 <map> #include <functional> #include <cstring> +#include <chrono> #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<BluetoothGatt> BluetoothGattPtr; -typedef shared_ptr<BluetoothGattService> BluetoothGattServicePtr; -typedef shared_ptr<BluetoothGattCharacteristic> BluetoothGattCharacteristicPtr; -typedef shared_ptr<BluetoothGattDescriptor> BluetoothGattDescriptorPtr; +typedef std::shared_ptr<BluetoothGatt> BluetoothGattPtr; +typedef std::shared_ptr<BluetoothGattService> BluetoothGattServicePtr; +typedef std::shared_ptr<BluetoothGattCharacteristic> BluetoothGattCharacteristicPtr; +typedef std::shared_ptr<BluetoothGattDescriptor> BluetoothGattDescriptorPtr; +typedef std::shared_ptr<BluetoothDevice> BluetoothDevicePtr; +typedef std::shared_ptr<BluetoothAdapter> 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<BluetoothGattDescriptor> getDescriptor(Uuid uuid) const = 0; + virtual BluetoothGattDescriptorPtr getDescriptor(Uuid uuid) const = 0; - virtual shared_ptr<BluetoothGattDescriptor> 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<shared_ptr<BluetoothGattCharacteristic>> getCharacteristics() const = 0; + virtual vector<BluetoothGattCharacteristicPtr> getCharacteristics() const = 0; virtual o<BluetoothGattCharacteristicPtr> findCharacteristic(Uuid uuid) = 0; @@ -155,7 +163,9 @@ public: virtual void discoverServices() = 0; - virtual vector<shared_ptr<BluetoothGattService>> getServices() const = 0; + virtual void process(std::chrono::system_clock::duration max_duration) = 0; + + virtual vector<BluetoothGattServicePtr> getServices() const = 0; virtual o<BluetoothGattServicePtr> findService(Uuid uuid) = 0; @@ -174,7 +184,7 @@ public: virtual BluetoothAdapter &getAdapter() = 0; - virtual shared_ptr<BluetoothGatt> 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<void(const shared_ptr<BluetoothDevice> &device)>) = 0; + virtual void runScan(std::function<void(const BluetoothDevicePtr &device)>) = 0; - virtual shared_ptr<BluetoothDevice> getDevice(Mac &mac) = 0; + virtual BluetoothDevicePtr getDevice(Mac &mac) = 0; protected: BluetoothAdapter(); @@ -206,10 +216,10 @@ public: ~BluetoothSystem(); - shared_ptr<BluetoothAdapter> getAdapter(string name); + BluetoothAdapterPtr getAdapter(string name); private: - map<string, shared_ptr<BluetoothAdapter>> adapters; + map<string, BluetoothAdapterPtr> 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<AttributeData> 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<std::monostate, ErrorRes, ExchangeMtuReq, ExchangeMtuRes, FindInformationRes, - ReadByGroupTypeRes, ReadByTypeRes>; + ReadByGroupTypeRes, ReadByTypeRes, + HandleValueNotification, HandleValueIndication>; o<AttPduType> 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); |