From 91e54cf9150b37036447d423857d2bd18e4bf02b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 5 Sep 2018 14:14:42 +0200 Subject: Major overhaul of BLE code: o Starting to remove shared_ptr. The code shouldn't be shared between threads, any thread safety will have to be built on the outside. o Better service discovery, don't fail when there are multiple requests that have to be done. o AttributeData was buggy, now it is just less than ideal. o Much better ByteBuffer. Now it is a simple view + cursor. --- include/ble/misc.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 include/ble/misc.h (limited to 'include/ble/misc.h') diff --git a/include/ble/misc.h b/include/ble/misc.h new file mode 100644 index 0000000..089c3a1 --- /dev/null +++ b/include/ble/misc.h @@ -0,0 +1,84 @@ +#pragma once + +#include +#include +#include + +namespace trygvis { +namespace bluetooth { + +class BluetoothAdapter; +class BluetoothDevice; + +class BluetoothException : public std::runtime_error { +public: + BluetoothException(const BluetoothAdapter *adapter, std::string const &what) : runtime_error(what), adapter(adapter), device(nullptr) {} + + BluetoothException(const BluetoothDevice *device, std::string const &what) : runtime_error(what), adapter(nullptr), device(device) {} + + explicit BluetoothException(std::string const &what) : runtime_error(what), adapter(nullptr), device(nullptr) {} + + const BluetoothAdapter *adapter; + const BluetoothDevice *device; +}; + +struct Uuid { + uint8_t value[16]; + + explicit Uuid(uint8_t value[16]) noexcept : value() { + std::memcpy(this->value, value, 16); + } + + Uuid(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, + uint8_t b8, uint8_t b9, uint8_t b10, uint8_t b11, uint8_t b12, uint8_t b13, uint8_t b14, uint8_t b15) noexcept : value{ + b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15} {} + + bool operator==(const Uuid &other) { + return std::memcmp(value, other.value, 16) == 0; + } + + bool operator==(const Uuid &other) const { + return std::memcmp(value, other.value, 16) == 0; + } + + friend std::ostream &operator<<(std::ostream &s, Uuid const &uuid); + + std::string str() const; + + static Uuid fromShort(uint8_t b2, uint8_t b3) { + return {0x00, 0x00, b2, b3, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; + } +}; + +struct ShortUuid { +private: +public: + explicit ShortUuid(uint16_t value) : value(value) {} + + Uuid toLong() + { + auto b2 = static_cast(value >> 8); + auto b3 = static_cast(value & 0xff); + return Uuid::fromShort(b2, b3); + } + + uint16_t value; +}; + +Uuid makeUuid(const Uuid& base, uint8_t a, uint8_t b); + +namespace uuids { + +const ShortUuid HealthTermometerService{0x1809}; +const ShortUuid DeviceInformationService{0x180a}; +const ShortUuid BatteryService{0x180f}; + +const ShortUuid PRIMARY_SERVICE{0x2800}; +const ShortUuid SECONDARY_SERVICE{0x2801}; +const ShortUuid CHARACTERISTIC{0x2803}; + +const ShortUuid TemperatureMeasurement{0x2A1C}; +} + +} // namespace bluetooth +} // namespace trygvis -- cgit v1.2.3