#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 namespace std; template using o = std::experimental::optional; class BluetoothAdapter; class BluetoothDevice; class BluetoothGatt; class BluetoothGattService; class BluetoothGattCharacteristic; typedef shared_ptr BluetoothGattPtr; typedef shared_ptr BluetoothGattCharacteristicPtr; typedef shared_ptr BluetoothGattServicePtr; class Mac { public: explicit Mac(uint8_t _5, uint8_t _4, uint8_t _3, uint8_t _2, uint8_t _1, uint8_t _0) : bytes() { bytes[5] = _5; bytes[4] = _4; bytes[3] = _3; bytes[2] = _2; bytes[1] = _1; bytes[0] = _0; }; string str() const; bool operator==(Mac &other) const; bool operator!=(Mac &other) const; void copy(uint8_t &_5, uint8_t &_4, uint8_t &_3, uint8_t &_2, uint8_t &_1, uint8_t &_0) const; static Mac parseMac(string s); friend bool operator<(const Mac &a, const Mac &b); private: uint8_t bytes[6]; }; template class Iterator { public: private: }; template class Collection { public: Iterator begin(); Iterator end(); }; class BluetoothGattCharacteristic { public: virtual ~BluetoothGattCharacteristic() = default; virtual BluetoothGattServicePtr getService() const = 0; virtual uint16_t getHandle() const = 0; virtual const Uuid getUuid() const = 0; virtual uint8_t getProperties() const = 0; virtual uint16_t getValueHandle() const = 0; }; 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 ByteBuffer readValue(const BluetoothGattCharacteristicPtr &c) = 0; virtual void discoverServices() = 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 shared_ptr connectGatt() = 0; }; class BluetoothAdapter { public: virtual Mac const &getMac() = 0; virtual void startScan() = 0; virtual void stopScan() = 0; virtual void runScan(std::function &device)>) = 0; virtual shared_ptr 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(); shared_ptr getAdapter(string name); private: map> adapters; }; } // namespace bluetooth } // namespace trygvis #endif