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. --- LinuxBluetooth.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 10 deletions(-) (limited to 'LinuxBluetooth.cpp') diff --git a/LinuxBluetooth.cpp b/LinuxBluetooth.cpp index ee93dee..14407c5 100644 --- a/LinuxBluetooth.cpp +++ b/LinuxBluetooth.cpp @@ -15,6 +15,8 @@ // Got to love magic constants. Taken from bluez.git/tools/btgatt-client.c #define ATT_CID 4 +#define MAX_MTU 256 + namespace trygvis { class LinuxBluetoothDevice; @@ -48,11 +50,13 @@ namespace trygvis { Mac const &mac() override; + LinuxBluetoothAdapter &adapter() override; + void connect() override; void disconnect() override; - virtual LinuxBluetoothAdapter &adapter(); + void discoverServices() override; private: LinuxBluetoothAdapter &_adapter; @@ -96,8 +100,8 @@ namespace trygvis { D << "connect: mac=" << _mac.str(); l2cap = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); - if(l2cap < 0) { - throw BluetoothException(&_adapter, this, "LinuxBluetoothDevice::connect(): socket(): " + errnoAsString()); + if (l2cap < 0) { + throw BluetoothException(this, "LinuxBluetoothDevice::connect(): socket(): " + errnoAsString()); } memset(&addr, 0, sizeof(addr)); @@ -108,7 +112,7 @@ namespace trygvis { if (bind(l2cap, (struct sockaddr *) &addr, sizeof(addr)) < 0) { close(l2cap); - throw BluetoothException(&_adapter, this, "LinuxBluetoothDevice::connect(): bind(): " + errnoAsString()); + throw BluetoothException(this, "LinuxBluetoothDevice::connect(): bind(): " + errnoAsString()); } struct bt_security btsec; @@ -116,7 +120,7 @@ namespace trygvis { btsec.level = BT_SECURITY_LOW; if (setsockopt(l2cap, SOL_BLUETOOTH, BT_SECURITY, &btsec, sizeof(btsec)) != 0) { close(l2cap); - throw BluetoothException(&_adapter, this, "LinuxBluetoothDevice::connect(): setsockopt(): " + errnoAsString()); + throw BluetoothException(this, "LinuxBluetoothDevice::connect(): setsockopt(): " + errnoAsString()); } memset(&addr, 0, sizeof(addr)); @@ -132,12 +136,44 @@ namespace trygvis { if (::connect(l2cap, (struct sockaddr *) &addr, sizeof(addr)) < 0) { close(l2cap); - throw BluetoothException(&_adapter, this, "LinuxBluetoothDevice::connect(): connect(): " + errnoAsString()); + throw BluetoothException(this, "LinuxBluetoothDevice::connect(): connect(): " + errnoAsString()); } } void LinuxBluetoothDevice::disconnect() { + DF << "mac=" << _mac.str(); + close(l2cap); + } + + void LinuxBluetoothDevice::discoverServices() { + DF; + + uint8_t buffer[MAX_MTU]; + ByteBuffer bytes = ByteBuffer(buffer, MAX_MTU); + + AttPdu pdu = AttPdu(bytes, AttPduType::READ_BY_GROUP_TYPE_REQ). + add16l(0x0001). + add16l(0x1234). + add16l(UUID_PRIMARY_SERVICE); + + D << "pdu.size()=" << pdu.size(); + ssize_t written = write(l2cap, bytes, pdu.size()); + + D << "written=" << written; + uint8_t *buffer = new uint8_t[MAX_MTU]; + + ssize_t r = read(l2cap, buffer, MAX_MTU); + + D << "read: " << r << " bytes"; + + pdu = AttPdu::parse(buffer, r); + + D << "type= " << pdu.getType(); + + if (pdu.getType() != AttPduType::READ_BY_GROUP_TYPE_REQ) { + + } } // ----------------------------------------------------------------------- @@ -146,7 +182,7 @@ namespace trygvis { LinuxBluetoothAdapter::LinuxBluetoothAdapter(int hciDeviceId) { - D << "LinuxBluetoothAdapter(), hciDeviceId " << hciDeviceId; + DF << "hciDeviceId=" << hciDeviceId; this->hciDeviceId = hciDeviceId; hciSocket = ::hci_open_dev(hciDeviceId); @@ -165,7 +201,7 @@ namespace trygvis { } LinuxBluetoothAdapter::~LinuxBluetoothAdapter() { - D << "~LinuxBluetoothAdapter()"; + DF; stopScan(); @@ -173,7 +209,7 @@ namespace trygvis { } void LinuxBluetoothAdapter::startScan() { - D << "startScan()"; + DF; struct hci_dev_info di; @@ -208,7 +244,7 @@ namespace trygvis { } void LinuxBluetoothAdapter::stopScan() { - D << "stopScan()"; + DF; if (!scanning) { return; -- cgit v1.2.3