From 1c97f6df59c825b26ecb18975fd9f62e14fc46ce Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 10 Feb 2015 19:02:57 +0100 Subject: o wip. --- Bluetooth.cpp | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) (limited to 'Bluetooth.cpp') diff --git a/Bluetooth.cpp b/Bluetooth.cpp index 22f67ec..43faf0a 100644 --- a/Bluetooth.cpp +++ b/Bluetooth.cpp @@ -14,12 +14,12 @@ namespace trygvis { std::ostringstream buf; buf - << setw(2) << hex << setfill('0') << (int)bytes[5] << ":" - << setw(2) << hex << setfill('0') << (int)bytes[4] << ":" - << setw(2) << hex << setfill('0') << (int)bytes[3] << ":" - << setw(2) << hex << setfill('0') << (int)bytes[2] << ":" - << setw(2) << hex << setfill('0') << (int)bytes[1] << ":" - << setw(2) << hex << setfill('0') << (int)bytes[0]; + << setw(2) << hex << setfill('0') << (int) bytes[5] << ":" + << setw(2) << hex << setfill('0') << (int) bytes[4] << ":" + << setw(2) << hex << setfill('0') << (int) bytes[3] << ":" + << setw(2) << hex << setfill('0') << (int) bytes[2] << ":" + << setw(2) << hex << setfill('0') << (int) bytes[1] << ":" + << setw(2) << hex << setfill('0') << (int) bytes[0]; return buf.str(); } @@ -46,6 +46,39 @@ namespace trygvis { return new Mac(bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5]); } + AttPdu::AttPdu(ByteBuffer &bytes) : bytes(bytes) { + } + + AttPdu::AttPdu(ByteBuffer &bytes, AttPduType type) : bytes(bytes) { + bytes.add8(type); + } + + AttPduType AttPdu::getType() { + return (AttPduType) bytes.get8(0); + } + + AttPdu AttPdu::parse(ByteBuffer & bytes) { + if(size == 0) { + throw BluetoothException("PDU is too small"); + } + + AttPdu pdu = AttPdu(bytes, size); + + AttPduType type = pdu.getType(); + + switch (type) { + case READ_BY_GROUP_TYPE_RES: + if (size < 4) { + throw BluetoothException("Bad READ_BY_GROUP_TYPE_RES packet, expected at least 4 octets, got " + size); + } + return pdu; + default: + throw BluetoothException("Uknown PDU type: " + type); + } + + return pdu; + } + // ----------------------------------------------------------------------- // Adapter // ----------------------------------------------------------------------- -- cgit v1.2.3