From b55df0b8e46f46fea4d69f98a729cd237d77a6ed Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 10 Feb 2015 21:29:19 +0100 Subject: o wip. --- LinuxBluetooth.cpp | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to 'LinuxBluetooth.cpp') diff --git a/LinuxBluetooth.cpp b/LinuxBluetooth.cpp index 14407c5..d274f84 100644 --- a/LinuxBluetooth.cpp +++ b/LinuxBluetooth.cpp @@ -33,6 +33,8 @@ namespace trygvis { void runScan(void (*callback)(BluetoothDevice &device)); + BluetoothDevice &getDevice(Mac &mac) override; + private: void startScan(); @@ -149,31 +151,32 @@ namespace trygvis { DF; uint8_t buffer[MAX_MTU]; - ByteBuffer bytes = ByteBuffer(buffer, MAX_MTU); + ByteBuffer out = ByteBuffer(buffer, MAX_MTU, 0); - AttPdu pdu = AttPdu(bytes, AttPduType::READ_BY_GROUP_TYPE_REQ). - add16l(0x0001). - add16l(0x1234). - add16l(UUID_PRIMARY_SERVICE); + AttPdu::makeReadByGroupType(out, 0x0001, 0xffff, UUID_PRIMARY_SERVICE); - D << "pdu.size()=" << pdu.size(); - ssize_t written = write(l2cap, bytes, pdu.size()); + D << "pdu.size()=" << out.getSize(); + ssize_t written = write(l2cap, buffer, out.getSize()); D << "written=" << written; - uint8_t *buffer = new uint8_t[MAX_MTU]; - ssize_t r = read(l2cap, buffer, MAX_MTU); + ByteBuffer in = ByteBuffer(buffer, MAX_MTU, r); D << "read: " << r << " bytes"; - pdu = AttPdu::parse(buffer, r); - - D << "type= " << pdu.getType(); + vector* values = AttPdu::parseReadByGroupType(in); - if (pdu.getType() != AttPduType::READ_BY_GROUP_TYPE_REQ) { + D << "READ_BY_GROUP_TYPE response has " + (values->size()) + " values"; + for(auto& data: *values) { +// strstring s; +// s << hex << setfill('0') << setw(2) << ar[i] << " "; +// data.bytes +// D << data. } + + delete values; } // ----------------------------------------------------------------------- @@ -257,6 +260,20 @@ namespace trygvis { } } + BluetoothDevice &LinuxBluetoothAdapter::getDevice(Mac &mac) { + static map devices; + + map::iterator it = devices.find(mac); + + if (it == devices.end()) { + LinuxBluetoothDevice *device = new LinuxBluetoothDevice(*this, mac); + devices[mac] = device; + return *device; + } + + return *it->second; + } + void LinuxBluetoothAdapter::runScan(void (*callback)(BluetoothDevice &device)) { fd_set rfds; FD_ZERO(&rfds); @@ -295,7 +312,7 @@ namespace trygvis { Mac mac = parseMac(advertisingInfo->bdaddr); - LinuxBluetoothDevice device = LinuxBluetoothDevice(*this, mac); + BluetoothDevice &device = getDevice(mac); callback(device); } @@ -309,7 +326,7 @@ namespace trygvis { /* map adapters; - BluetoothAdapter &getDevice(int hciDevice) { + BluetoothAdapter &getAdapter(int hciDevice) { map::iterator it = adapters.find(hciDevice); if (it == adapters.end()) { @@ -322,7 +339,8 @@ namespace trygvis { } */ - BluetoothAdapter *getDevice(int hciDevice) { + BluetoothAdapter *getAdapter(int hciDevice) { return new LinuxBluetoothAdapter(hciDevice); } + }; -- cgit v1.2.3