diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-02-10 21:29:19 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-02-10 21:29:19 +0100 |
commit | b55df0b8e46f46fea4d69f98a729cd237d77a6ed (patch) | |
tree | 25e12c22d844bb449dd08d7661d20d42ca8ab0f5 /LinuxBluetooth.cpp | |
parent | 1c97f6df59c825b26ecb18975fd9f62e14fc46ce (diff) | |
download | ble-toys-b55df0b8e46f46fea4d69f98a729cd237d77a6ed.tar.gz ble-toys-b55df0b8e46f46fea4d69f98a729cd237d77a6ed.tar.bz2 ble-toys-b55df0b8e46f46fea4d69f98a729cd237d77a6ed.tar.xz ble-toys-b55df0b8e46f46fea4d69f98a729cd237d77a6ed.zip |
o wip.
Diffstat (limited to 'LinuxBluetooth.cpp')
-rw-r--r-- | LinuxBluetooth.cpp | 50 |
1 files changed, 34 insertions, 16 deletions
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<AttributeData*>* 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<Mac, LinuxBluetoothDevice *> devices; + + map<Mac, LinuxBluetoothDevice *>::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<int, LinuxBluetoothAdapter *> adapters; - BluetoothAdapter &getDevice(int hciDevice) { + BluetoothAdapter &getAdapter(int hciDevice) { map<int, LinuxBluetoothAdapter *>::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); } + }; |