diff options
Diffstat (limited to 'Bluetooth.h')
-rw-r--r-- | Bluetooth.h | 179 |
1 files changed, 91 insertions, 88 deletions
diff --git a/Bluetooth.h b/Bluetooth.h index 7bc5bc3..1b5d5f0 100644 --- a/Bluetooth.h +++ b/Bluetooth.h @@ -10,127 +10,130 @@ #include "ByteBuffer.h" namespace trygvis { - using namespace std; - - class BluetoothAdapter; - - class BluetoothDevice; - - 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; +namespace bluetooth { +using namespace std; + +class BluetoothAdapter; + +class BluetoothDevice; + +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; }; - 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; - string str() const; + bool operator==(Mac &other) const; - bool operator==(Mac &other) 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; - 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) throw(BluetoothException); - static Mac *parseMac(string s) throw(BluetoothException); + friend bool operator<(const Mac &a, const Mac &b); - friend bool operator<(const Mac &a, const Mac &b); - private: - uint8_t bytes[6]; - }; +private: + uint8_t bytes[6]; +}; - class BluetoothDevice { - public: - virtual Mac const &mac() = 0; +class BluetoothDevice { +public: + virtual Mac const &mac() = 0; - virtual BluetoothAdapter &adapter() = 0; + virtual BluetoothAdapter &adapter() = 0; - virtual void connect() = 0; + virtual void connect() = 0; - virtual void disconnect() = 0; + virtual void disconnect() = 0; - virtual void discoverServices() = 0; - }; + virtual void discoverServices() = 0; +}; - class BluetoothAdapter { - public: - BluetoothAdapter() { - }; +class BluetoothAdapter { +public: + virtual void stopScan() = 0; - virtual ~BluetoothAdapter(); + virtual void runScan(void (callback)(BluetoothDevice &device)) = 0; - virtual void runScan(void (callback)(BluetoothDevice &device)) = 0; + virtual BluetoothDevice &getDevice(Mac &mac) = 0; - virtual void stopScan() = 0; +protected: + BluetoothAdapter(); - virtual void startScan() = 0; + virtual ~BluetoothAdapter(); +}; - virtual BluetoothDevice &getDevice(Mac& mac) = 0; - }; +enum AttPduType { + ERROR = 0x00, + READ_BY_GROUP_TYPE_REQ = 0x10, + READ_BY_GROUP_TYPE_RES = 0x11 +}; - enum AttPduType { - ERROR = 0x00, - READ_BY_GROUP_TYPE_REQ = 0x10, - READ_BY_GROUP_TYPE_RES = 0x11 - }; +class AttributeData; - class AttributeData; +class AttPdu { +public: + AttPdu(ByteBuffer &bytes); - class AttPdu { - public: - AttPdu(ByteBuffer &bytes); + AttPdu(ByteBuffer &bytes, AttPduType type); - AttPdu(ByteBuffer &bytes, AttPduType type); + AttPduType getType(); - AttPduType getType(); + static vector<AttributeData> parseReadByGroupType(ByteBuffer &bytes); - static vector <AttributeData> parseReadByGroupType(ByteBuffer &bytes); + static void makeReadByGroupType(ByteBuffer &bytes, uint16_t startHandle, uint16_t endHandle, uint16_t uuid); - static void makeReadByGroupType(ByteBuffer &bytes, uint16_t startHandle, uint16_t endHandle, uint16_t uuid); +private: + static void checkType(ByteBuffer &bytes, AttPduType type); - private: - static void checkType(ByteBuffer &bytes, AttPduType type); + ByteBuffer &bytes; +}; - ByteBuffer &bytes; - }; +class AttributeData { +public: + ~AttributeData(); - class AttributeData { - public: - ~AttributeData(); + static AttributeData fromByteBuffer(ByteBuffer &value); - static AttributeData fromByteBuffer(ByteBuffer &value); + const uint16_t handle; + const uint16_t groupEndHandle; + const ByteBuffer value; - const uint16_t handle; - const uint16_t groupEndHandle; - const ByteBuffer value; +private: + AttributeData(uint16_t handle, uint16_t groupEndHandle, ByteBuffer value); +}; - private: - AttributeData(uint16_t handle, uint16_t groupEndHandle, ByteBuffer value); - }; +BluetoothAdapter &getAdapter(int hciDevice); + +void shutdown(); -// BluetoothAdapter &getAdapter(int hciDevice); - BluetoothAdapter *getAdapter(int hciDevice); +} } #endif |