diff options
Diffstat (limited to 'ble/LinuxBluetooth.cpp')
-rw-r--r-- | ble/LinuxBluetooth.cpp | 83 |
1 files changed, 31 insertions, 52 deletions
diff --git a/ble/LinuxBluetooth.cpp b/ble/LinuxBluetooth.cpp index d4073d1..7e7c9dc 100644 --- a/ble/LinuxBluetooth.cpp +++ b/ble/LinuxBluetooth.cpp @@ -28,9 +28,9 @@ class LinuxBluetoothAdapter; class LinuxBluetoothManager; -class LinuxBluetoothGattService : public DefaultBluetoothGattService<LinuxBluetoothDevice, DefaultBluetoothGattCharacteristic> { +class LinuxBluetoothGattService : public DefaultBluetoothGattService<LinuxBluetoothDevice> { public: - LinuxBluetoothGattService(LinuxBluetoothDevice &device, const uuid_t uuid, const uint16_t handle, + LinuxBluetoothGattService(LinuxBluetoothDevice &device, const Uuid uuid, const uint16_t handle, const uint16_t endGroupHandle) : DefaultBluetoothGattService(device, uuid, handle, endGroupHandle) { }; @@ -41,7 +41,7 @@ class LinuxBluetoothAdapter final : public DefaultBluetoothAdapter { public: LinuxBluetoothAdapter(int hciDeviceId, Mac &mac); - ~LinuxBluetoothAdapter(); + ~LinuxBluetoothAdapter() override; LinuxBluetoothAdapter(const LinuxBluetoothAdapter &) = delete; @@ -67,9 +67,9 @@ private: class LinuxBluetoothDevice final : public DefaultBluetoothDevice<LinuxBluetoothAdapter>, public std::enable_shared_from_this<LinuxBluetoothDevice> { public: - LinuxBluetoothDevice(LinuxBluetoothAdapter &adapter, Mac &mac); + explicit LinuxBluetoothDevice(LinuxBluetoothAdapter &adapter, Mac &mac); - virtual ~LinuxBluetoothDevice(); + ~LinuxBluetoothDevice() override; LinuxBluetoothDevice(const LinuxBluetoothDevice &) = delete; @@ -83,9 +83,9 @@ private: class LinuxBluetoothGatt final : public DefaultBluetoothGatt<LinuxBluetoothDevice, LinuxBluetoothGattService> { public: - LinuxBluetoothGatt(LinuxBluetoothDevice &device); + explicit LinuxBluetoothGatt(LinuxBluetoothDevice &device); - ~LinuxBluetoothGatt(); + ~LinuxBluetoothGatt() override; LinuxBluetoothGatt(const LinuxBluetoothGatt&) = delete; @@ -110,6 +110,7 @@ private: ByteBuffer writeAndRead(ByteBuffer &out, shared_ptr<uint8_t> buffer, size_t size); + int l2cap; }; @@ -124,7 +125,7 @@ string errnoAsString() { // ----------------------------------------------------------------------- Mac parseMac(bdaddr_t &a) { - return Mac(a.b[5], a.b[4], a.b[3], a.b[2], a.b[1], a.b[0]); + return Mac{a.b[5], a.b[4], a.b[3], a.b[2], a.b[1], a.b[0]}; } // ----------------------------------------------------------------------- @@ -137,16 +138,13 @@ LinuxBluetoothDevice::LinuxBluetoothDevice(LinuxBluetoothAdapter &adapter, Mac & LinuxBluetoothDevice::~LinuxBluetoothDevice() { LOG_DEBUG("Closing device " << mac.str()); - if (gatt) { - delete gatt; - } + + delete gatt; }; shared_ptr<BluetoothGatt> LinuxBluetoothDevice::connectGatt() { // Make sure that we close the old connection and create a new one when the user asks for it. - if (gatt) { - delete gatt; - } + delete gatt; gatt = new LinuxBluetoothGatt(*this); @@ -158,7 +156,7 @@ shared_ptr<BluetoothGatt> LinuxBluetoothDevice::connectGatt() { // ----------------------------------------------------------------------- LinuxBluetoothGatt::LinuxBluetoothGatt(LinuxBluetoothDevice &device) : - DefaultBluetoothGatt(device) { + DefaultBluetoothGatt(device), l2cap() { connect(); } @@ -171,7 +169,7 @@ bool LinuxBluetoothGatt::isConnected() const { } void LinuxBluetoothGatt::connect() { - struct sockaddr_l2 addr; + struct sockaddr_l2 addr{}; LOG_DEBUG("connect: mac=" << device.getMac().str()); @@ -191,7 +189,7 @@ void LinuxBluetoothGatt::connect() { throw BluetoothException(&device, "bind(): " + errnoAsString()); } - struct bt_security btsec; + struct bt_security btsec{}; memset(&btsec, 0, sizeof(btsec)); btsec.level = BT_SECURITY_LOW; if (setsockopt(l2cap, SOL_BLUETOOTH, BT_SECURITY, &btsec, sizeof(btsec)) != 0) { @@ -225,40 +223,21 @@ void LinuxBluetoothGatt::disconnect() { close(l2cap); } -uuid_t readUuid(BluetoothDevice *device, const ByteBuffer &bytes) { +Uuid readUuid(BluetoothDevice *device, const ByteBuffer &bytes) { size_t bytesLeft = bytes.getBytesLeft(); - uuid_t u; - if (bytesLeft == 2) { uint8_t bs[16] = BLUETOOTH_UUID_INITIALIZER; bs[2] = bytes.get8(1); bs[3] = bytes.get8(0); - memcpy(&u, bs, 16); + + return Uuid(bs); } else if (bytesLeft == 16) { - uint8_t bs[16]; - bs[15] = bytes.get8(0); - bs[14] = bytes.get8(1); - bs[13] = bytes.get8(2); - bs[12] = bytes.get8(3); - bs[11] = bytes.get8(4); - bs[10] = bytes.get8(5); - bs[9] = bytes.get8(6); - bs[8] = bytes.get8(7); - bs[7] = bytes.get8(8); - bs[6] = bytes.get8(9); - bs[5] = bytes.get8(10); - bs[4] = bytes.get8(11); - bs[3] = bytes.get8(12); - bs[2] = bytes.get8(13); - bs[1] = bytes.get8(14); - bs[0] = bytes.get8(15); - memcpy(&u, bs, 16); + return {bytes.get8(0), bytes.get8(1), bytes.get8(2), bytes.get8(3), bytes.get8(4), bytes.get8(5), bytes.get8(6), bytes.get8(7), + bytes.get8(8), bytes.get8(9), bytes.get8(10), bytes.get8(11), bytes.get8(12), bytes.get8(13), bytes.get8(14), bytes.get8(15)}; } else { throw BluetoothException(device, "Unexpected bytes left: " + to_string(bytesLeft)); } - - return u; } void LinuxBluetoothGatt::writeValue(const BluetoothGattCharacteristicPtr &c, const ByteBuffer &bytes) { @@ -302,7 +281,7 @@ void LinuxBluetoothGatt::discoverServices() { vector<AttributeData> values = discoverServices(startHandle); // Shouldn't happen, but you never know. - if (values.size() == 0) { + if (values.empty()) { break; } @@ -315,7 +294,7 @@ void LinuxBluetoothGatt::discoverServices() { // ", endGroupHandle: 0x" << hex << setw(4) << setfill('0') << endGroupHandle << // ", value: " << data.value.toString(); - uuid_t u = readUuid(&device, data.value); + auto u = readUuid(&device, data.value); addService(make_shared<LinuxBluetoothGattService>(device, u, data.handle, endGroupHandle)); } @@ -344,7 +323,7 @@ void LinuxBluetoothGatt::discoverServices() { do { vector<AttributeData> values = discoverCharacteristics(startHandle, 0xffff); - if (values.size() == 0) { + if (values.empty()) { break; } @@ -360,7 +339,7 @@ void LinuxBluetoothGatt::discoverServices() { uint8_t properties = c.value.read8(); uint16_t valueHandle = c.value.read16le(); - uuid_t uuid = readUuid(&device, c.value); + auto uuid = readUuid(&device, c.value); // D << "characteristic: handle: " << setw(2) << setfill('0') << hex << (int) c.handle << // ", properties: " << setw(2) << setfill('0') << hex << (int) properties << @@ -435,7 +414,7 @@ vector<AttributeData> LinuxBluetoothGatt::discoverCharacteristics(uint16_t start // ----------------------------------------------------------------------- LinuxBluetoothAdapter::LinuxBluetoothAdapter(int hciDeviceId, Mac &mac) : DefaultBluetoothAdapter(mac), - scanning(false) { + scanning(false), hciFilter() { LOG_DEBUG("hciDeviceId=" << hciDeviceId); this->hciDeviceId = hciDeviceId; @@ -466,7 +445,7 @@ LinuxBluetoothAdapter::~LinuxBluetoothAdapter() { } void LinuxBluetoothAdapter::startScan() { - struct hci_dev_info di; + struct hci_dev_info di{}; if (hci_devinfo(hciDeviceId, &di) < 0) { throw BluetoothException(this, "Could not query device info: " + errnoAsString()); @@ -541,11 +520,11 @@ void LinuxBluetoothAdapter::runScan(std::function<void(const shared_ptr<Bluetoot FD_SET(hciSocket, &rfds); // Linux can change tv, so it has to be reinitialized - struct timeval tv; + struct timeval tv{}; tv.tv_sec = 1; tv.tv_usec = 0; - int selected = select(hciSocket + 1, &rfds, NULL, NULL, &tv); + int selected = select(hciSocket + 1, &rfds, nullptr, nullptr, &tv); if (selected == -1) { // Someone stopped the scan, so this is no problem @@ -564,13 +543,13 @@ void LinuxBluetoothAdapter::runScan(std::function<void(const shared_ptr<Bluetoot unsigned char hciEventBuf[HCI_MAX_EVENT_SIZE]; ssize_t len = read(hciSocket, hciEventBuf, sizeof(hciEventBuf)); - evt_le_meta_event *metaEvent = (evt_le_meta_event *) (hciEventBuf + (1 + HCI_EVENT_HDR_SIZE)); + auto *metaEvent = (evt_le_meta_event *) (hciEventBuf + (1 + HCI_EVENT_HDR_SIZE)); len -= (1 + HCI_EVENT_HDR_SIZE); LOG_DEBUG("metaEvent->subevent = " << std::hex << (int) metaEvent->subevent); if (metaEvent->subevent == EVT_LE_ADVERTISING_REPORT) { - le_advertising_info *advertisingInfo = (le_advertising_info *) (metaEvent->data + 1); + auto *advertisingInfo = (le_advertising_info *) (metaEvent->data + 1); Mac mac = parseMac(advertisingInfo->bdaddr); @@ -605,7 +584,7 @@ shared_ptr<BluetoothAdapter> getAdapterImpl(string name) { throw BluetoothException("Bad device name: " + name); } - struct hci_dev_info di; + struct hci_dev_info di{}; if (hci_devinfo(hciDevice, &di) < 0) { throw BluetoothException("Could not query device info: " + errnoAsString()); } |