aboutsummaryrefslogtreecommitdiff
path: root/include/ble/misc.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/ble/misc.h')
-rw-r--r--include/ble/misc.h84
1 files changed, 84 insertions, 0 deletions
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 <cstring>
+#include <cctype>
+#include <stdexcept>
+
+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<uint8_t>(value >> 8);
+ auto b3 = static_cast<uint8_t>(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