#ifndef BLUETOOTH_H #define BLUETOOTH_H #include #include #include #include #include #include #include #include #include #include "ble/ByteBuffer.h" #include "ble/att.h" namespace trygvis { namespace bluetooth { using std::string; using std::vector; using std::map; using std::runtime_error; class BluetoothAdapter; class BluetoothDevice; class BluetoothGatt; class BluetoothGattService; class BluetoothGattCharacteristic; class BluetoothGattDescriptor; typedef std::shared_ptr BluetoothGattPtr; typedef std::shared_ptr BluetoothGattServicePtr; typedef std::shared_ptr BluetoothGattCharacteristicPtr; typedef std::shared_ptr BluetoothGattDescriptorPtr; typedef std::shared_ptr BluetoothDevicePtr; typedef std::shared_ptr BluetoothAdapterPtr; class Mac { public: explicit Mac(uint8_t _0, uint8_t _1, uint8_t _2, uint8_t _3, uint8_t _4, uint8_t _5) : bytes() { bytes[0] = _0; bytes[1] = _1; bytes[2] = _2; bytes[3] = _3; bytes[4] = _4; bytes[5] = _5; }; string str() const; // bool operator==(const Mac &other) const; // bool operator!=(const Mac &other) const; // bool operator<(const 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); friend bool operator==(const Mac &a, const Mac &b); private: uint8_t bytes[6]; }; std::ostream& operator<<(std::ostream& os, const Mac& mac); bool operator==(const Mac &a, const Mac &b); bool operator!=(const Mac &a, const Mac &b); bool operator<(const Mac &a, const Mac &b); class BluetoothCallback { virtual ~BluetoothCallback() = default; virtual void onCharacteristicChanged(BluetoothGattPtr& gatt, BluetoothGattCharacteristicPtr& characteristic) = 0; }; class BluetoothGattDescriptor { public: static const uint8_t DISABLE_NOTIFICATION_VALUE[2]; static const uint8_t ENABLE_INDICATION_VALUE[2]; static const uint8_t ENABLE_NOTIFICATION_VALUE[2]; virtual ~BluetoothGattDescriptor() = default; virtual BluetoothGattCharacteristicPtr getCharacteristic() const = 0; virtual uint16_t getHandle() const = 0; virtual const Uuid getUuid() const = 0; template void setValue(const uint8_t (&buffer)[N]) { auto tmp = ByteBuffer::wrap(buffer, N); setValue(&tmp); } virtual void setValue(const ByteBuffer &buffer) = 0; virtual ByteBuffer getValue() const = 0; }; class BluetoothGattCharacteristic { public: virtual ~BluetoothGattCharacteristic() = default; virtual BluetoothGattServicePtr getService() const = 0; virtual uint16_t getHandle() const = 0; virtual const Uuid getUuid() const = 0; virtual CharacteristicProperties getProperties() const = 0; virtual uint16_t getValueHandle() const = 0; virtual BluetoothGattDescriptorPtr getDescriptor(Uuid uuid) const = 0; virtual BluetoothGattDescriptorPtr getDescriptor(ShortUuid uuid) const { return getDescriptor(uuid.toLong()); }; }; class BluetoothGattService { public: virtual ~BluetoothGattService() = default; virtual BluetoothDevice &getDevice() const = 0; virtual Uuid getUuid() const = 0; virtual uint16_t getHandle() const = 0; virtual uint16_t getEndGroupHandle() const = 0; virtual vector getCharacteristics() const = 0; virtual o findCharacteristic(Uuid uuid) = 0; o findCharacteristic(ShortUuid uuid) { return findCharacteristic(uuid.toLong()); }; }; class BluetoothGatt { public: BluetoothGatt(); virtual ~BluetoothGatt(); // No copying BluetoothGatt(const BluetoothGatt &) = delete; BluetoothGatt &operator=(const BluetoothGatt &) = delete; virtual BluetoothDevice &getDevice() const = 0; virtual bool isConnected() const = 0; virtual void writeValue(const BluetoothGattCharacteristicPtr &c, const ByteBuffer &bytes) = 0; virtual void write(const BluetoothGattDescriptorPtr &d) = 0; virtual ByteBuffer readValue(const BluetoothGattCharacteristicPtr &c, ByteBuffer &response) = 0; virtual void discoverServices() = 0; virtual void process(std::chrono::system_clock::duration max_duration) = 0; virtual vector getServices() const = 0; virtual o findService(Uuid uuid) = 0; o findService(ShortUuid uuid) { return findService(uuid.toLong()); } }; class BluetoothDevice { public: BluetoothDevice(); virtual ~BluetoothDevice(); virtual Mac const &getMac() = 0; virtual BluetoothAdapter &getAdapter() = 0; virtual BluetoothGattPtr connectGatt(BluetoothCallback* callback = nullptr) = 0; }; class BluetoothAdapter { public: virtual Mac const &getMac() = 0; virtual void startScan() = 0; virtual void stopScan() = 0; virtual void runScan(std::function) = 0; virtual BluetoothDevicePtr 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(); BluetoothAdapterPtr getAdapter(string name); private: map adapters; }; } // namespace bluetooth } // namespace trygvis #endif