From d720fa36ad4768ed1b948a92ba5287c30093fbec Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 26 Jul 2015 18:33:15 +0200 Subject: o Overhaul of the bluetooth code. - Adding support for reading FLOAT (specified in IEEE 11073-20601) values from a bluetooth device. - More shared pointers to help keep track of the object's lifecycle. Makes sure that the connections are released back to Linux, Linux is way to sensitive with crashing applications. o Adding support for reading the temperature sensors from the SoilMoisture device. --- include/ble/Bluetooth.h | 18 ++++++++++-------- include/ble/ByteBuffer.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/ble/Bluetooth.h b/include/ble/Bluetooth.h index d238377..8da602f 100644 --- a/include/ble/Bluetooth.h +++ b/include/ble/Bluetooth.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "ByteBuffer.h" @@ -115,7 +116,7 @@ public: virtual void addCharacteristic(BluetoothGattCharacteristic *characteristic) = 0; - virtual const boost::optional findCharacteristic(boost::uuids::uuid uuid) const = 0; + virtual const boost::optional findCharacteristic(boost::uuids::uuid uuid) const = 0; }; class BluetoothGatt { @@ -165,9 +166,9 @@ public: virtual void stopScan() = 0; - virtual void runScan(std::function) = 0; + virtual void runScan(std::function &device)>) = 0; - virtual BluetoothDevice &getDevice(Mac &mac) = 0; + virtual shared_ptr getDevice(Mac &mac) = 0; protected: BluetoothAdapter(); @@ -185,6 +186,11 @@ public: BluetoothSystem(); ~BluetoothSystem(); + + shared_ptr getAdapter(string name); + +private: + map> adapters; }; enum AttPduType { @@ -227,7 +233,7 @@ public: static void makeWrite(ByteBuffer &req, uint16_t handle, const ByteBuffer &bytes); private: - static void checkType(ByteBuffer &bytes, AttPduType type); +// static void checkType(ByteBuffer &bytes, AttPduType type); static vector parse(ByteBuffer &bytes, AttPduType type); @@ -247,10 +253,6 @@ private: AttributeData(uint16_t handle, ByteBuffer value); }; -shared_ptr getAdapter(int hciDevice); - -void shutdown(); - boost::uuids::uuid makeUuid(const boost::uuids::uuid base, uint8_t a, uint8_t b); } diff --git a/include/ble/ByteBuffer.h b/include/ble/ByteBuffer.h index d977c67..1032187 100644 --- a/include/ble/ByteBuffer.h +++ b/include/ble/ByteBuffer.h @@ -7,6 +7,29 @@ #include #include #include +#include + +namespace FLOAT { +static const uint32_t positive_infinity = 0x007FFFFE; +static const uint32_t negative_infinity = 0x00800002; +static const uint32_t NaN = 0x007FFFFF; +static const uint32_t NRes = 0x00800000; +static const uint32_t FLAOT_reserved_value = 0x00800001; + +// (2^23 - 3) * 10^127 +static const double max = 8.388604999999999e+133; +static const double min = -max; + +static const double mantissa_max = 0x007FFFFD; +static const double exponent_max = 127; +static const double exponent_min = -128; + +// 10^-128 +static const double epsilon = 1e-128; + +// 10^upper(23 * log(2) / log(10)) +static const double precision = 10000000; +} class ByteBufferException : public std::runtime_error { public: @@ -53,6 +76,8 @@ public: ByteBuffer &write16le(uint16_t value); + ByteBuffer &write32le(uint32_t value); + /** * Appends the entire buffer. Make a view if you want to write a part of it. */ @@ -60,12 +85,21 @@ public: ByteBuffer &write(const uint8_t *bytes, size_t len); + ByteBuffer &writeFLOAT(double d); + uint8_t get8(size_t index) const; uint8_t read8(); uint16_t read16le(); + uint32_t read32le(); + + /** + * IEEE-11073 32-bit FLOAT + */ + double readFLOAT(); + void copy(uint8_t *bytes, size_t length) const; /** -- cgit v1.2.3