diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2016-04-12 20:06:47 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2016-04-12 20:06:47 +0200 |
commit | 2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d (patch) | |
tree | 19f9ce796886e216a608fa5e938bd2bd4f0d0b55 /ble/LinuxBluetooth.cpp | |
parent | ce07550c57172443c10a66957b50085e273d20b3 (diff) | |
download | ble-toys-2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d.tar.gz ble-toys-2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d.tar.bz2 ble-toys-2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d.tar.xz ble-toys-2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d.zip |
Soil Moisture: Adding support for controlling lights.
Bluetooth: refectorying, trying to be more c++ idiomatic and modern.
SM/Diller: adding bluetooth to Diller bridge.
Diffstat (limited to 'ble/LinuxBluetooth.cpp')
-rw-r--r-- | ble/LinuxBluetooth.cpp | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/ble/LinuxBluetooth.cpp b/ble/LinuxBluetooth.cpp index 587016c..d4073d1 100644 --- a/ble/LinuxBluetooth.cpp +++ b/ble/LinuxBluetooth.cpp @@ -28,6 +28,15 @@ class LinuxBluetoothAdapter; class LinuxBluetoothManager; +class LinuxBluetoothGattService : public DefaultBluetoothGattService<LinuxBluetoothDevice, DefaultBluetoothGattCharacteristic> { +public: + LinuxBluetoothGattService(LinuxBluetoothDevice &device, const uuid_t uuid, const uint16_t handle, + const uint16_t endGroupHandle) : DefaultBluetoothGattService(device, uuid, handle, endGroupHandle) { + }; + + LinuxBluetoothGattService(const LinuxBluetoothGattService &o) = delete; +}; + class LinuxBluetoothAdapter final : public DefaultBluetoothAdapter { public: LinuxBluetoothAdapter(int hciDeviceId, Mac &mac); @@ -72,7 +81,7 @@ private: LinuxBluetoothGatt *gatt; }; -class LinuxBluetoothGatt final : public DefaultBluetoothGatt<LinuxBluetoothDevice> { +class LinuxBluetoothGatt final : public DefaultBluetoothGatt<LinuxBluetoothDevice, LinuxBluetoothGattService> { public: LinuxBluetoothGatt(LinuxBluetoothDevice &device); @@ -86,9 +95,9 @@ public: void discoverServices() override; - void writeValue(const BluetoothGattCharacteristic &c, const ByteBuffer &bytes) override; + void writeValue(const BluetoothGattCharacteristicPtr &c, const ByteBuffer &bytes) override; - ByteBuffer readValue(const BluetoothGattCharacteristic &c) override; + ByteBuffer readValue(const BluetoothGattCharacteristicPtr &c) override; private: void connect(); @@ -252,24 +261,24 @@ uuid_t readUuid(BluetoothDevice *device, const ByteBuffer &bytes) { return u; } -void LinuxBluetoothGatt::writeValue(const BluetoothGattCharacteristic &c, const ByteBuffer &bytes) { - LOG_DEBUG("Writing to characteristic " << c.getUuid() << ": " << bytes.toString()); +void LinuxBluetoothGatt::writeValue(const BluetoothGattCharacteristicPtr &c, const ByteBuffer &bytes) { + LOG_DEBUG("Writing to characteristic " << c->getUuid() << ": " << bytes.toString()); shared_ptr<uint8_t> buffer(new uint8_t[MAX_MTU]); ByteBuffer out = ByteBuffer(buffer, MAX_MTU); - AttPdu::makeWrite(out, c.getValueHandle(), bytes); + AttPdu::makeWrite(out, c->getValueHandle(), bytes); ByteBuffer in = writeAndRead(out, buffer, MAX_MTU); AttPdu::parseWrite(in); } -ByteBuffer LinuxBluetoothGatt::readValue(const BluetoothGattCharacteristic &c) { +ByteBuffer LinuxBluetoothGatt::readValue(const BluetoothGattCharacteristicPtr &c) { shared_ptr<uint8_t> buffer(new uint8_t[MAX_MTU]); ByteBuffer out = ByteBuffer(buffer, MAX_MTU); - AttPdu::makeRead(out, c.getValueHandle()); + AttPdu::makeRead(out, c->getValueHandle()); ByteBuffer in = writeAndRead(out, buffer, MAX_MTU); @@ -279,7 +288,7 @@ ByteBuffer LinuxBluetoothGatt::readValue(const BluetoothGattCharacteristic &c) { auto response = in.view(); - LOG_DEBUG("Value of characteristic " << c.getUuid() << "=" << response.toString()); + LOG_DEBUG("Value of characteristic " << c->getUuid() << "=" << response.toString()); return response; } @@ -308,7 +317,7 @@ void LinuxBluetoothGatt::discoverServices() { uuid_t u = readUuid(&device, data.value); - addService(new DefaultBluetoothGattService(device, u, data.handle, endGroupHandle)); + addService(make_shared<LinuxBluetoothGattService>(device, u, data.handle, endGroupHandle)); } // auto last = values.back(); @@ -358,7 +367,7 @@ void LinuxBluetoothGatt::discoverServices() { // ", valueHandle: 0x" << setw(4) << setfill('0') << hex << (int) valueHandle << // ", uuid: " << uuid; - s->addCharacteristic(new DefaultBluetoothGattCharacteristic(*s, c.handle, uuid, properties, valueHandle)); + s->addCharacteristic(std::move(make_shared<DefaultBluetoothGattCharacteristic>(s, c.handle, uuid, properties, valueHandle))); } startHandle = lastHandle + (uint8_t) 2; |