aboutsummaryrefslogtreecommitdiff
path: root/Bluetooth.cpp
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-02-15 23:29:17 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-02-15 23:35:36 +0100
commit0d0543e4daeb1c6b01d4799736026b6f3aef9779 (patch)
tree0f14cac6264710700c70f18c79e41a8aaa6a8cfd /Bluetooth.cpp
parent60d5440dd3514e71b87948ff5ed30ee38445b8a5 (diff)
downloadble-toys-0d0543e4daeb1c6b01d4799736026b6f3aef9779.tar.gz
ble-toys-0d0543e4daeb1c6b01d4799736026b6f3aef9779.tar.bz2
ble-toys-0d0543e4daeb1c6b01d4799736026b6f3aef9779.tar.xz
ble-toys-0d0543e4daeb1c6b01d4799736026b6f3aef9779.zip
valgrind tests
Diffstat (limited to 'Bluetooth.cpp')
-rw-r--r--Bluetooth.cpp221
1 files changed, 127 insertions, 94 deletions
diff --git a/Bluetooth.cpp b/Bluetooth.cpp
index dd9e48e..e6d6d28 100644
--- a/Bluetooth.cpp
+++ b/Bluetooth.cpp
@@ -2,137 +2,170 @@
#include <iomanip>
#include <string.h>
#include "Bluetooth.h"
+#include "BluetoothImpl.h"
namespace trygvis {
- using namespace std;
+namespace bluetooth {
+using namespace std;
- // -----------------------------------------------------------------------
- // Mac
- // -----------------------------------------------------------------------
+// -----------------------------------------------------------------------
+// Mac
+// -----------------------------------------------------------------------
- string Mac::str() const {
- std::ostringstream buf;
+string Mac::str() const {
+ std::ostringstream buf;
- buf
- << setw(2) << hex << setfill('0') << (int) bytes[0] << ":"
- << setw(2) << hex << setfill('0') << (int) bytes[1] << ":"
- << setw(2) << hex << setfill('0') << (int) bytes[2] << ":"
- << setw(2) << hex << setfill('0') << (int) bytes[3] << ":"
- << setw(2) << hex << setfill('0') << (int) bytes[4] << ":"
- << setw(2) << hex << setfill('0') << (int) bytes[5];
+ buf
+ << setw(2) << hex << setfill('0') << (int) bytes[0] << ":"
+ << setw(2) << hex << setfill('0') << (int) bytes[1] << ":"
+ << setw(2) << hex << setfill('0') << (int) bytes[2] << ":"
+ << setw(2) << hex << setfill('0') << (int) bytes[3] << ":"
+ << setw(2) << hex << setfill('0') << (int) bytes[4] << ":"
+ << setw(2) << hex << setfill('0') << (int) bytes[5];
- return buf.str();
- }
+ return buf.str();
+}
- bool Mac::operator==(Mac &other) const {
- const uint8_t* b = bytes;
- return memcmp(b, other.bytes, sizeof(bytes)) == 0;
- }
+bool Mac::operator==(Mac &other) const {
+ const uint8_t *b = bytes;
+ return memcmp(b, other.bytes, sizeof(bytes)) == 0;
+}
- bool Mac::operator!=(Mac &other) const {
- return !operator==(other);
- }
+bool Mac::operator!=(Mac &other) const {
+ return !operator==(other);
+}
- bool operator<(const Mac &a, const Mac &b) {
- return memcmp(a.bytes, b.bytes, sizeof(a.bytes)) < 0;
- }
+bool operator<(const Mac &a, const Mac &b) {
+ return memcmp(a.bytes, b.bytes, sizeof(a.bytes)) < 0;
+}
+
+void Mac::copy(uint8_t &_0, uint8_t &_1, uint8_t &_2, uint8_t &_3, uint8_t &_4, uint8_t &_5) const {
+ _0 = bytes[0];
+ _1 = bytes[1];
+ _2 = bytes[2];
+ _3 = bytes[3];
+ _4 = bytes[4];
+ _5 = bytes[5];
+}
- void Mac::copy(uint8_t &_0, uint8_t &_1, uint8_t &_2, uint8_t &_3, uint8_t &_4, uint8_t &_5) const {
- _0 = bytes[0];
- _1 = bytes[1];
- _2 = bytes[2];
- _3 = bytes[3];
- _4 = bytes[4];
- _5 = bytes[5];
+Mac Mac::parseMac(string s) throw(BluetoothException) {
+ 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]);
+
+ if (count != 6) {
+ throw BluetoothException("Unable to parse mac: " + s);
}
- Mac *Mac::parseMac(string s) throw(BluetoothException) {
- 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]);
+ return Mac((uint8_t) bytes[0], (uint8_t) bytes[1], (uint8_t) bytes[2], (uint8_t) bytes[3], (uint8_t) bytes[4], (uint8_t) bytes[5]);
+}
- if (count != 6) {
- throw BluetoothException("Unable to parse mac: " + s);
- }
+AttPdu::AttPdu(ByteBuffer &bytes) : bytes(bytes) {
+}
- return new Mac((uint8_t) bytes[0], (uint8_t) bytes[1], (uint8_t) bytes[2], (uint8_t) bytes[3], (uint8_t) bytes[4], (uint8_t) bytes[5]);
- }
+AttPdu::AttPdu(ByteBuffer &bytes, AttPduType type) : bytes(bytes) {
+ bytes.add8(type);
+}
- AttPdu::AttPdu(ByteBuffer &bytes) : bytes(bytes) {
- }
+AttPduType AttPdu::getType() {
+ return (AttPduType) bytes.get8(0);
+}
- AttPdu::AttPdu(ByteBuffer &bytes, AttPduType type) : bytes(bytes) {
- bytes.add8(type);
+void AttPdu::makeReadByGroupType(ByteBuffer &bytes, uint16_t startHandle, uint16_t endHandle, uint16_t uuid) {
+ bytes.setCursor(0);
+ bytes.add8(AttPduType::READ_BY_GROUP_TYPE_REQ);
+ bytes.add16le(startHandle);
+ bytes.add16le(endHandle);
+ bytes.add16le(uuid);
+}
+
+void AttPdu::checkType(ByteBuffer &bytes, AttPduType type) {
+ if (bytes.getSize() == 0) {
+ throw BluetoothException("PDU is too small");
}
- AttPduType AttPdu::getType() {
- return (AttPduType) bytes.get8(0);
+ bytes.setCursor(0);
+ AttPduType t = (AttPduType) bytes.get8();
+
+ if (t != type) {
+ throw BluetoothException("Unexpected type: " + t);
}
+}
- void AttPdu::makeReadByGroupType(ByteBuffer &bytes, uint16_t startHandle, uint16_t endHandle, uint16_t uuid) {
- bytes.setCursor(0);
- bytes.add8(AttPduType::READ_BY_GROUP_TYPE_REQ);
- bytes.add16le(startHandle);
- bytes.add16le(endHandle);
- bytes.add16le(uuid);
+vector<AttributeData> AttPdu::parseReadByGroupType(ByteBuffer &bytes) {
+ DF << "bytes: " << bytes.toString();
+
+ checkType(bytes, READ_BY_GROUP_TYPE_RES);
+
+ if (bytes.getSize() < 4) {
+ throw BluetoothException("Bad READ_BY_GROUP_TYPE_RES packet, expected at least 4 octets, got " + bytes.getSize());
}
- void AttPdu::checkType(ByteBuffer &bytes, AttPduType type) {
- if (bytes.getSize() == 0) {
- throw BluetoothException("PDU is too small");
- }
+ uint8_t length = bytes.get8();
+ DF << "length=" << (int) length;
- bytes.setCursor(0);
- AttPduType t = (AttPduType) bytes.get8();
+ size_t count = (bytes.getSize() - 2) / length;
+ DF << "count=" << count;
- if (t != type) {
- throw BluetoothException("Unexpected type: " + t);
- }
+ vector<AttributeData> values;
+ for (int i = 0; i < count; i++) {
+ values.push_back(AttributeData::fromByteBuffer(bytes, length));
}
- vector<AttributeData> AttPdu::parseReadByGroupType(ByteBuffer &bytes) {
- checkType(bytes, READ_BY_GROUP_TYPE_RES);
+ return values;
+}
- if (bytes.getSize() < 4) {
- throw BluetoothException("Bad READ_BY_GROUP_TYPE_RES packet, expected at least 4 octets, got " + bytes.getSize());
- }
+// -----------------------------------------------------------------------
+// AttributeData
+// -----------------------------------------------------------------------
- uint8_t length = bytes.get8();
- DF << "length=" << (int) length;
+AttributeData AttributeData::fromByteBuffer(ByteBuffer &bytes, uint8_t length) {
+ uint16_t handle = bytes.get16le();
+ uint16_t groupEndHandle = bytes.get16le();
- size_t count = (bytes.getSize() - 2) / length;
- DF << "count=" << count;
+ return AttributeData(handle, groupEndHandle, bytes.view(length - 4));
+}
- vector<AttributeData> values;
- for (int i = 0; i < count; i++) {
- values.push_back(AttributeData::fromByteBuffer(bytes, length));
- }
+AttributeData::AttributeData(uint16_t handle, uint16_t groupEndHandle, ByteBuffer value) :
+ handle(handle), groupEndHandle(groupEndHandle), value(value) {
+}
- return values;
- }
+AttributeData::~AttributeData() {
+}
- // -----------------------------------------------------------------------
- // AttributeData
- // -----------------------------------------------------------------------
+// -----------------------------------------------------------------------
+// Adapter
+// -----------------------------------------------------------------------
- AttributeData AttributeData::fromByteBuffer(ByteBuffer &bytes, uint8_t length) {
- uint16_t handle = bytes.get16le();
- uint16_t groupEndHandle = bytes.get16le();
+BluetoothAdapter::BluetoothAdapter() {
+}
- return AttributeData(handle, groupEndHandle, bytes.view(length));
- }
+BluetoothAdapter::~BluetoothAdapter() {
+}
- AttributeData::AttributeData(uint16_t handle, uint16_t groupEndHandle, ByteBuffer value) :
- handle(handle), groupEndHandle(groupEndHandle), value(value) {
- }
+/*
+map<int, LinuxBluetoothAdapter *> adapters;
- AttributeData::~AttributeData() {
+BluetoothAdapter &getAdapter(int hciDevice) {
+ map<int, LinuxBluetoothAdapter *>::iterator it = adapters.find(hciDevice);
+
+ if (it == adapters.end()) {
+ LinuxBluetoothAdapter *adapter = new LinuxBluetoothAdapter(hciDevice);
+ adapters[hciDevice] = adapter;
+ return *adapter;
}
- // -----------------------------------------------------------------------
- // Adapter
- // -----------------------------------------------------------------------
+ return *it->second;
+}
+*/
- BluetoothAdapter::~BluetoothAdapter() {
- }
+BluetoothAdapter &getAdapter(int hciDevice) {
+ return getAdapterImpl(hciDevice);
+}
+
+void shutdown() {
+ shutdownImpl();
+}
+
+}
};