From ba6324930118a62e05d3869f99543c4a7507f105 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 22 Feb 2015 15:36:06 +0100 Subject: o Splitting out public header files into it's own directory. --- apps/CMakeLists.txt | 2 +- apps/ble-inspect-device.cpp | 7 +- apps/sm-get-value.cpp | 7 +- ble/Bluetooth.cpp | 2 +- ble/Bluetooth.h | 252 -------------------------------------------- ble/BluetoothImpl.h | 2 +- ble/ByteBuffer.cpp | 2 +- ble/ByteBuffer.h | 97 ----------------- ble/CMakeLists.txt | 1 + include/ble/Bluetooth.h | 252 ++++++++++++++++++++++++++++++++++++++++++++ include/ble/ByteBuffer.h | 97 +++++++++++++++++ test/ByteBufferTest.cpp | 9 +- test/CMakeLists.txt | 1 + 13 files changed, 362 insertions(+), 369 deletions(-) delete mode 100644 ble/Bluetooth.h delete mode 100644 ble/ByteBuffer.h create mode 100644 include/ble/Bluetooth.h create mode 100644 include/ble/ByteBuffer.h diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index a138336..aefb0fb 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -10,7 +10,7 @@ pkg_check_modules(BLUEZ bluez REQUIRED) find_package(Threads REQUIRED) foreach(app ${APPS}) - include_directories("${PROJECT_SOURCE_DIR}/ble") + include_directories("${PROJECT_SOURCE_DIR}/include") add_executable(${app} ${app}.cpp) add_dependencies(${app} ble) diff --git a/apps/ble-inspect-device.cpp b/apps/ble-inspect-device.cpp index b9abe11..cdc8375 100644 --- a/apps/ble-inspect-device.cpp +++ b/apps/ble-inspect-device.cpp @@ -2,8 +2,7 @@ #include #include #include -#include "Bluetooth.h" -#include "log.h" +#include "ble/Bluetooth.h" using namespace std; using namespace trygvis::bluetooth; @@ -50,10 +49,10 @@ int main(int argc, char *argv[]) { return EXIT_SUCCESS; } catch (std::runtime_error ex) { - W << "std::runtime_error: " << ex.what(); + cout << "std::runtime_error: " << ex.what() << endl; return EXIT_FAILURE; } catch (std::exception ex) { - W << "std::exception: " << ex.what(); + cout << "std::exception: " << ex.what() << endl; return EXIT_FAILURE; } } diff --git a/apps/sm-get-value.cpp b/apps/sm-get-value.cpp index 66d35f1..e4ddc16 100644 --- a/apps/sm-get-value.cpp +++ b/apps/sm-get-value.cpp @@ -1,9 +1,8 @@ #include #include #include -#include "Bluetooth.h" +#include "ble/Bluetooth.h" #include "soil-moisture.h" -#include "log.h" using namespace std; using namespace trygvis::bluetooth; @@ -135,10 +134,10 @@ int main(int argc, char *argv[]) { return EXIT_SUCCESS; } catch (std::runtime_error ex) { - W << "std::runtime_error: " << ex.what(); + cout << "std::runtime_error: " << ex.what() << endl; return EXIT_FAILURE; } catch (std::exception ex) { - W << "std::exception: " << ex.what(); + cout << "std::exception: " << ex.what() << endl; return EXIT_FAILURE; } } diff --git a/ble/Bluetooth.cpp b/ble/Bluetooth.cpp index fe63b4f..66f97b7 100644 --- a/ble/Bluetooth.cpp +++ b/ble/Bluetooth.cpp @@ -1,4 +1,4 @@ -#include "Bluetooth.h" +#include "ble/Bluetooth.h" #include "BluetoothImpl.h" #include diff --git a/ble/Bluetooth.h b/ble/Bluetooth.h deleted file mode 100644 index 1a1394e..0000000 --- a/ble/Bluetooth.h +++ /dev/null @@ -1,252 +0,0 @@ -#ifndef BLUETOOTH_H -#define BLUETOOTH_H - -#include -#include -#include -#include - -#include "ByteBuffer.h" - -namespace trygvis { -namespace bluetooth { - -using namespace std; - -struct SpecUuid { -public: - SpecUuid(uint16_t value) : value(value) { - } - - uint16_t value; -}; - -namespace uuids { -const SpecUuid PRIMARY_SERVICE = SpecUuid(0x2800); -const SpecUuid SECONDARY_SERVICE = SpecUuid(0x2801); -const SpecUuid CHARACTERISTIC = SpecUuid(0x2803); -} - -class BluetoothAdapter; - -class BluetoothDevice; - -class BluetoothGattService; - -class BluetoothGattCharacteristic; - -class BluetoothException : public runtime_error { -public: - BluetoothException(const BluetoothAdapter *adapter, string const &what) : - runtime_error(what), adapter(adapter), device(nullptr) { - } - - BluetoothException(const BluetoothDevice *device, string const &what) : - runtime_error(what), adapter(nullptr), device(device) { - } - - BluetoothException(string const &what) : - runtime_error(what), adapter(nullptr), device(nullptr) { - } - - const BluetoothAdapter *adapter; - const BluetoothDevice *device; -}; - -class Mac { -public: - Mac(uint8_t _0, uint8_t _1, uint8_t _2, uint8_t _3, uint8_t _4, uint8_t _5) { - bytes[0] = _0; - bytes[1] = _1; - bytes[2] = _2; - bytes[3] = _3; - bytes[4] = _4; - bytes[5] = _5; - }; - - string str() const; - - bool operator==(Mac &other) const; - - bool operator!=(Mac &other) const; - - void copy(uint8_t &_0, uint8_t &_1, uint8_t &_2, uint8_t &_3, uint8_t &_4, uint8_t &_5) const; - - static Mac parseMac(string s); - - friend bool operator<(const Mac &a, const Mac &b); - -private: - uint8_t bytes[6]; -}; - -class BluetoothGattCharacteristic { -public: - virtual ~BluetoothGattCharacteristic() { - }; - - virtual BluetoothGattService &getService() const = 0; - - virtual uint16_t getHandle() const = 0; - - virtual const boost::uuids::uuid getUuid() const = 0; - - virtual uint8_t getProperties() const = 0; - - virtual uint16_t getValueHandle() const = 0; -}; - -class BluetoothGattService { -public: - virtual ~BluetoothGattService() { - }; - - virtual BluetoothDevice &getDevice() const = 0; - - virtual boost::uuids::uuid getUuid() const = 0; - - virtual uint16_t getHandle() const = 0; - - virtual uint16_t getEndGroupHandle() const = 0; - - virtual const vector getCharacteristics() const = 0; - - virtual void addCharacteristic(BluetoothGattCharacteristic *characteristic) = 0; - - virtual const boost::optional findCharacteristic(boost::uuids::uuid uuid) const = 0; -}; - -class BluetoothGatt { -public: - BluetoothGatt(); - - virtual ~BluetoothGatt(); - - virtual BluetoothDevice &getDevice() const = 0; - - virtual void connect() = 0; - - virtual void disconnect() = 0; - - virtual void writeValue(const BluetoothGattCharacteristic &c, const ByteBuffer &bytes) = 0; - - virtual ByteBuffer readValue(const BluetoothGattCharacteristic &c) = 0; - - virtual void discoverServices() = 0; - - virtual const vector getServices() const = 0; - - virtual const boost::optional findService(boost::uuids::uuid uuid) const = 0; -}; - -class BluetoothDevice { -public: - BluetoothDevice(); - - virtual ~BluetoothDevice(); - - virtual Mac const &getMac() = 0; - - virtual BluetoothAdapter &getAdapter() = 0; - - virtual BluetoothGatt &connectGatt() = 0; -}; - -class BluetoothAdapter { -public: - virtual void startScan() = 0; - - virtual void stopScan() = 0; - - virtual void runScan(void (callback)(BluetoothDevice &device)) = 0; - - virtual BluetoothDevice &getDevice(Mac &mac) = 0; - -protected: - BluetoothAdapter(); - - virtual ~BluetoothAdapter(); -}; - -/** -* Right this is only RAII support to properly call shutdown(). -* -* TODO: move getAdapter() here. Make this control all shutdowns. -*/ -class BluetoothSystem { -public: - BluetoothSystem(); - - ~BluetoothSystem(); -}; - -enum AttPduType { - ERROR = 0x00, - INVALID_HANDLE = 0x01, - READ_BY_TYPE_REQ = 0x08, - READ_BY_TYPE_RES = 0x09, - READ_REQ = 0x0a, - READ_RES = 0x0b, - READ_BY_GROUP_TYPE_REQ = 0x10, - READ_BY_GROUP_TYPE_RES = 0x11, - WRITE_REQ = 0x12, - WRITE_RES = 0x13, -}; - -class AttributeData; - -class AttPdu { -public: - AttPdu(ByteBuffer &bytes); - - AttPdu(ByteBuffer &bytes, AttPduType type); - - AttPduType getType(); - - static vector parseReadByGroupType(ByteBuffer &bytes); - - static vector parseReadByType(ByteBuffer &bytes); - - static void parseRead(ByteBuffer &bytes); - - static void parseWrite(ByteBuffer &bytes); - - static void makeReadByGroupType(ByteBuffer &bytes, uint16_t startHandle, uint16_t endHandle, SpecUuid uuid); - - static void makeReadByType(ByteBuffer &bytes, uint16_t startHandle, uint16_t endHandle, SpecUuid uuid); - - static void makeRead(ByteBuffer &bytes, uint16_t handle); - - static void makeWrite(ByteBuffer &req, uint16_t handle, const ByteBuffer &bytes); - -private: - static void checkType(ByteBuffer &bytes, AttPduType type); - - static vector parse(ByteBuffer &bytes, AttPduType type); - - ByteBuffer &bytes; -}; - -class AttributeData { -public: - ~AttributeData(); - - static AttributeData fromByteBuffer(ByteBuffer &value); - - const uint16_t handle; - ByteBuffer value; - -private: - AttributeData(uint16_t handle, ByteBuffer value); -}; - -BluetoothAdapter &getAdapter(int hciDevice); - -void shutdown(); - -boost::uuids::uuid makeUuid(const boost::uuids::uuid base, uint8_t a, uint8_t b); - -} -} - -#endif diff --git a/ble/BluetoothImpl.h b/ble/BluetoothImpl.h index c9ba80c..967c2e7 100644 --- a/ble/BluetoothImpl.h +++ b/ble/BluetoothImpl.h @@ -1,7 +1,7 @@ #ifndef BLUETOOTH_IMPL_H #define BLUETOOTH_IMPL_H -#include "Bluetooth.h" +#include "ble/Bluetooth.h" #include "log.h" #include #include diff --git a/ble/ByteBuffer.cpp b/ble/ByteBuffer.cpp index 377334b..d1c923e 100644 --- a/ble/ByteBuffer.cpp +++ b/ble/ByteBuffer.cpp @@ -1,4 +1,4 @@ -#include "ByteBuffer.h" +#include "ble/ByteBuffer.h" #include #include #include diff --git a/ble/ByteBuffer.h b/ble/ByteBuffer.h deleted file mode 100644 index f884d6e..0000000 --- a/ble/ByteBuffer.h +++ /dev/null @@ -1,97 +0,0 @@ -#ifndef BYTE_STREAM_WRAPPER_H -#define BYTE_STREAM_WRAPPER_H - -#include -#include -#include -#include -#include - -class ByteBufferException : public std::runtime_error { -public: - ByteBufferException(std::string const &what) : std::runtime_error(what) { - } -}; - -class ByteBuffer { -public: - static ByteBuffer alloc(std::size_t capacity); - - ByteBuffer(const std::shared_ptr bytes, size_t capacity); - - ByteBuffer(const std::shared_ptr bytes, size_t capacity, size_t size); - - ByteBuffer(const std::shared_ptr bytes, size_t capacity, size_t size, size_t zero); - - inline size_t getSize() const { - return end - zero; - } - - inline size_t getCapacity() const { - return capacity; - } - - inline size_t getCursor() const { - return ptr - zero; - } - - inline size_t getBytesLeft() const { - return end - ptr; - } - - inline void setCursor(size_t newCursor) { - ptr = (uint8_t *) &zero[newCursor]; - } - - inline void skip(size_t length) { - ptr += length; - } - - ByteBuffer &write8(uint8_t value); - - ByteBuffer &write16le(uint16_t value); - - /** - * Appends the entire buffer. Make a view if you want to write a part of it. - */ - ByteBuffer &write(const ByteBuffer &value); - - ByteBuffer &write(const uint8_t *bytes, size_t len); - - uint8_t get8(size_t index) const; - - uint8_t read8(); - - uint16_t read16le(); - - void copy(uint8_t *bytes, size_t length) const; - - /** - * Creates a view from cursor to size. - */ - ByteBuffer view() const; - - // TODO: should return const - ByteBuffer view(size_t length) const; - - std::string toString() const; - -private: - ByteBuffer(const std::shared_ptr bytes, size_t capacity, const uint8_t *zero, const uint8_t *end); - - ByteBuffer view(uint8_t *ptr, const uint8_t *end) const; - - void checkAndUpdateEnd(size_t count); - - void assertCanAccessRelative(size_t diff) const; - - void assertCanAccessIndex(uint8_t *p) const; - - const std::shared_ptr bytes; - const size_t capacity; - const uint8_t *zero; - const uint8_t *end; - uint8_t *ptr; -}; - -#endif diff --git a/ble/CMakeLists.txt b/ble/CMakeLists.txt index 1ecfb6c..405f125 100644 --- a/ble/CMakeLists.txt +++ b/ble/CMakeLists.txt @@ -1,3 +1,4 @@ file(GLOB SOURCE_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) add_library(ble ${SOURCE_FILES}) +include_directories("${PROJECT_SOURCE_DIR}/include") diff --git a/include/ble/Bluetooth.h b/include/ble/Bluetooth.h new file mode 100644 index 0000000..1a1394e --- /dev/null +++ b/include/ble/Bluetooth.h @@ -0,0 +1,252 @@ +#ifndef BLUETOOTH_H +#define BLUETOOTH_H + +#include +#include +#include +#include + +#include "ByteBuffer.h" + +namespace trygvis { +namespace bluetooth { + +using namespace std; + +struct SpecUuid { +public: + SpecUuid(uint16_t value) : value(value) { + } + + uint16_t value; +}; + +namespace uuids { +const SpecUuid PRIMARY_SERVICE = SpecUuid(0x2800); +const SpecUuid SECONDARY_SERVICE = SpecUuid(0x2801); +const SpecUuid CHARACTERISTIC = SpecUuid(0x2803); +} + +class BluetoothAdapter; + +class BluetoothDevice; + +class BluetoothGattService; + +class BluetoothGattCharacteristic; + +class BluetoothException : public runtime_error { +public: + BluetoothException(const BluetoothAdapter *adapter, string const &what) : + runtime_error(what), adapter(adapter), device(nullptr) { + } + + BluetoothException(const BluetoothDevice *device, string const &what) : + runtime_error(what), adapter(nullptr), device(device) { + } + + BluetoothException(string const &what) : + runtime_error(what), adapter(nullptr), device(nullptr) { + } + + const BluetoothAdapter *adapter; + const BluetoothDevice *device; +}; + +class Mac { +public: + Mac(uint8_t _0, uint8_t _1, uint8_t _2, uint8_t _3, uint8_t _4, uint8_t _5) { + bytes[0] = _0; + bytes[1] = _1; + bytes[2] = _2; + bytes[3] = _3; + bytes[4] = _4; + bytes[5] = _5; + }; + + string str() const; + + bool operator==(Mac &other) const; + + bool operator!=(Mac &other) const; + + void copy(uint8_t &_0, uint8_t &_1, uint8_t &_2, uint8_t &_3, uint8_t &_4, uint8_t &_5) const; + + static Mac parseMac(string s); + + friend bool operator<(const Mac &a, const Mac &b); + +private: + uint8_t bytes[6]; +}; + +class BluetoothGattCharacteristic { +public: + virtual ~BluetoothGattCharacteristic() { + }; + + virtual BluetoothGattService &getService() const = 0; + + virtual uint16_t getHandle() const = 0; + + virtual const boost::uuids::uuid getUuid() const = 0; + + virtual uint8_t getProperties() const = 0; + + virtual uint16_t getValueHandle() const = 0; +}; + +class BluetoothGattService { +public: + virtual ~BluetoothGattService() { + }; + + virtual BluetoothDevice &getDevice() const = 0; + + virtual boost::uuids::uuid getUuid() const = 0; + + virtual uint16_t getHandle() const = 0; + + virtual uint16_t getEndGroupHandle() const = 0; + + virtual const vector getCharacteristics() const = 0; + + virtual void addCharacteristic(BluetoothGattCharacteristic *characteristic) = 0; + + virtual const boost::optional findCharacteristic(boost::uuids::uuid uuid) const = 0; +}; + +class BluetoothGatt { +public: + BluetoothGatt(); + + virtual ~BluetoothGatt(); + + virtual BluetoothDevice &getDevice() const = 0; + + virtual void connect() = 0; + + virtual void disconnect() = 0; + + virtual void writeValue(const BluetoothGattCharacteristic &c, const ByteBuffer &bytes) = 0; + + virtual ByteBuffer readValue(const BluetoothGattCharacteristic &c) = 0; + + virtual void discoverServices() = 0; + + virtual const vector getServices() const = 0; + + virtual const boost::optional findService(boost::uuids::uuid uuid) const = 0; +}; + +class BluetoothDevice { +public: + BluetoothDevice(); + + virtual ~BluetoothDevice(); + + virtual Mac const &getMac() = 0; + + virtual BluetoothAdapter &getAdapter() = 0; + + virtual BluetoothGatt &connectGatt() = 0; +}; + +class BluetoothAdapter { +public: + virtual void startScan() = 0; + + virtual void stopScan() = 0; + + virtual void runScan(void (callback)(BluetoothDevice &device)) = 0; + + virtual BluetoothDevice &getDevice(Mac &mac) = 0; + +protected: + BluetoothAdapter(); + + virtual ~BluetoothAdapter(); +}; + +/** +* Right this is only RAII support to properly call shutdown(). +* +* TODO: move getAdapter() here. Make this control all shutdowns. +*/ +class BluetoothSystem { +public: + BluetoothSystem(); + + ~BluetoothSystem(); +}; + +enum AttPduType { + ERROR = 0x00, + INVALID_HANDLE = 0x01, + READ_BY_TYPE_REQ = 0x08, + READ_BY_TYPE_RES = 0x09, + READ_REQ = 0x0a, + READ_RES = 0x0b, + READ_BY_GROUP_TYPE_REQ = 0x10, + READ_BY_GROUP_TYPE_RES = 0x11, + WRITE_REQ = 0x12, + WRITE_RES = 0x13, +}; + +class AttributeData; + +class AttPdu { +public: + AttPdu(ByteBuffer &bytes); + + AttPdu(ByteBuffer &bytes, AttPduType type); + + AttPduType getType(); + + static vector parseReadByGroupType(ByteBuffer &bytes); + + static vector parseReadByType(ByteBuffer &bytes); + + static void parseRead(ByteBuffer &bytes); + + static void parseWrite(ByteBuffer &bytes); + + static void makeReadByGroupType(ByteBuffer &bytes, uint16_t startHandle, uint16_t endHandle, SpecUuid uuid); + + static void makeReadByType(ByteBuffer &bytes, uint16_t startHandle, uint16_t endHandle, SpecUuid uuid); + + static void makeRead(ByteBuffer &bytes, uint16_t handle); + + static void makeWrite(ByteBuffer &req, uint16_t handle, const ByteBuffer &bytes); + +private: + static void checkType(ByteBuffer &bytes, AttPduType type); + + static vector parse(ByteBuffer &bytes, AttPduType type); + + ByteBuffer &bytes; +}; + +class AttributeData { +public: + ~AttributeData(); + + static AttributeData fromByteBuffer(ByteBuffer &value); + + const uint16_t handle; + ByteBuffer value; + +private: + AttributeData(uint16_t handle, ByteBuffer value); +}; + +BluetoothAdapter &getAdapter(int hciDevice); + +void shutdown(); + +boost::uuids::uuid makeUuid(const boost::uuids::uuid base, uint8_t a, uint8_t b); + +} +} + +#endif diff --git a/include/ble/ByteBuffer.h b/include/ble/ByteBuffer.h new file mode 100644 index 0000000..f884d6e --- /dev/null +++ b/include/ble/ByteBuffer.h @@ -0,0 +1,97 @@ +#ifndef BYTE_STREAM_WRAPPER_H +#define BYTE_STREAM_WRAPPER_H + +#include +#include +#include +#include +#include + +class ByteBufferException : public std::runtime_error { +public: + ByteBufferException(std::string const &what) : std::runtime_error(what) { + } +}; + +class ByteBuffer { +public: + static ByteBuffer alloc(std::size_t capacity); + + ByteBuffer(const std::shared_ptr bytes, size_t capacity); + + ByteBuffer(const std::shared_ptr bytes, size_t capacity, size_t size); + + ByteBuffer(const std::shared_ptr bytes, size_t capacity, size_t size, size_t zero); + + inline size_t getSize() const { + return end - zero; + } + + inline size_t getCapacity() const { + return capacity; + } + + inline size_t getCursor() const { + return ptr - zero; + } + + inline size_t getBytesLeft() const { + return end - ptr; + } + + inline void setCursor(size_t newCursor) { + ptr = (uint8_t *) &zero[newCursor]; + } + + inline void skip(size_t length) { + ptr += length; + } + + ByteBuffer &write8(uint8_t value); + + ByteBuffer &write16le(uint16_t value); + + /** + * Appends the entire buffer. Make a view if you want to write a part of it. + */ + ByteBuffer &write(const ByteBuffer &value); + + ByteBuffer &write(const uint8_t *bytes, size_t len); + + uint8_t get8(size_t index) const; + + uint8_t read8(); + + uint16_t read16le(); + + void copy(uint8_t *bytes, size_t length) const; + + /** + * Creates a view from cursor to size. + */ + ByteBuffer view() const; + + // TODO: should return const + ByteBuffer view(size_t length) const; + + std::string toString() const; + +private: + ByteBuffer(const std::shared_ptr bytes, size_t capacity, const uint8_t *zero, const uint8_t *end); + + ByteBuffer view(uint8_t *ptr, const uint8_t *end) const; + + void checkAndUpdateEnd(size_t count); + + void assertCanAccessRelative(size_t diff) const; + + void assertCanAccessIndex(uint8_t *p) const; + + const std::shared_ptr bytes; + const size_t capacity; + const uint8_t *zero; + const uint8_t *end; + uint8_t *ptr; +}; + +#endif diff --git a/test/ByteBufferTest.cpp b/test/ByteBufferTest.cpp index bf18387..fb31da3 100644 --- a/test/ByteBufferTest.cpp +++ b/test/ByteBufferTest.cpp @@ -1,10 +1,9 @@ -#include "ByteBuffer.h" +#include "ble/ByteBuffer.h" #include "log.h" #define BOOST_TEST_MODULE "ByteBuffer" #include -#include "Bluetooth.h" #define checkBuffer(buffer, capacity, size, cursor) \ if(false) {D << "capacity=" << buffer.getCapacity() << ", size=" << buffer.getSize() << ", cursor=" << buffer.getCursor();} \ @@ -14,12 +13,6 @@ using namespace std; -template -struct do_nothing { - void operator()(T *) { - } -}; - class Bytes { public: Bytes(size_t size) : capacity(size) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a417d2b..e9f217f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,6 +12,7 @@ foreach(testSrc ${TEST_SRCS}) add_executable(${testName} ${testSrc}) include_directories("${PROJECT_SOURCE_DIR}/ble") + include_directories("${PROJECT_SOURCE_DIR}/include") add_dependencies(${testName} ble) target_link_libraries(${testName} ble) target_link_libraries(${testName} pthread) -- cgit v1.2.3