From 5926b05afa21eaac36c185e7fc458710efa30b02 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 21 Feb 2015 23:58:39 +0100 Subject: o Support for reading and writing characteristics. --- ble/Bluetooth.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'ble/Bluetooth.cpp') diff --git a/ble/Bluetooth.cpp b/ble/Bluetooth.cpp index 31f82e3..fe63b4f 100644 --- a/ble/Bluetooth.cpp +++ b/ble/Bluetooth.cpp @@ -3,7 +3,6 @@ #include #include -#include namespace trygvis { namespace bluetooth { @@ -49,7 +48,7 @@ void Mac::copy(uint8_t &_0, uint8_t &_1, uint8_t &_2, uint8_t &_3, uint8_t &_4, _5 = bytes[5]; } -Mac Mac::parseMac(string s) throw(BluetoothException) { +Mac Mac::parseMac(string s) { unsigned int bytes[6]; int count = sscanf(s.c_str(), "%02x:%02x:%02x:%02x:%02x:%02x", &bytes[0], &bytes[1], &bytes[2], &bytes[3], &bytes[4], &bytes[5]); @@ -86,6 +85,17 @@ void AttPdu::makeReadByType(ByteBuffer &bytes, uint16_t startHandle, uint16_t en bytes.write16le(uuid.value); } +void AttPdu::makeRead(ByteBuffer &bytes, uint16_t handle) { + bytes.write8(AttPduType::READ_REQ); + bytes.write16le(handle); +} + +void AttPdu::makeWrite(ByteBuffer &req, uint16_t handle, const ByteBuffer &bytes) { + req.write8(AttPduType::WRITE_REQ); + req.write16le(handle); + req.write(bytes); +} + vector AttPdu::parse(ByteBuffer &bytes, AttPduType type) { DF << "bytes: " << bytes.toString(); @@ -128,6 +138,22 @@ vector AttPdu::parseReadByType(ByteBuffer &bytes) { return parse(bytes, READ_BY_TYPE_RES); } +void AttPdu::parseRead(ByteBuffer &bytes) { + AttPduType t = (AttPduType) bytes.read8(); + + if (t != READ_RES) { + throw BluetoothException("Unexpected type: " + to_string(t)); + } +} + +void AttPdu::parseWrite(ByteBuffer &bytes) { + AttPduType t = (AttPduType) bytes.read8(); + + if (t != WRITE_RES) { + throw BluetoothException("Unexpected type: " + to_string(t)); + } +} + // ----------------------------------------------------------------------- // AttributeData // ----------------------------------------------------------------------- -- cgit v1.2.3