From bed47c2d1e92c6ae55a1a39d3d905a538ec4d502 Mon Sep 17 00:00:00 2001 From: Emeric Date: Tue, 19 Sep 2023 18:23:09 +0200 Subject: [PATCH] Cleanup the device management code --- src/DeviceFilter.cpp | 15 ++++++++++- src/DeviceFilter.h | 16 ++++++----- src/DeviceManager.cpp | 24 ++++++++++++----- src/DeviceManager.h | 42 ++++++++++++++++++----------- src/device.cpp | 12 ++++----- src/device.h | 6 ++--- src/device_utils.h | 4 ++- src/devices/device_thermobeacon.cpp | 3 +++ 8 files changed, 81 insertions(+), 41 deletions(-) diff --git a/src/DeviceFilter.cpp b/src/DeviceFilter.cpp index 1b54fbc6e..a45a7a5a9 100644 --- a/src/DeviceFilter.cpp +++ b/src/DeviceFilter.cpp @@ -41,6 +41,11 @@ DeviceFilter::~DeviceFilter() // } +void DeviceFilter::invalidatefilter() +{ + invalidateFilter(); +} + /* ************************************************************************** */ bool DeviceFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const @@ -246,7 +251,7 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const return QVariant(); } -void DeviceModel::getDevices(QList &device) +void DeviceModel::getDevices(QList &device) { for (auto d: qAsConst(m_devices)) { @@ -275,6 +280,14 @@ void DeviceModel::removeDevice(Device *d, bool del) } } +void DeviceModel::clearDevices() +{ + for (auto d: qAsConst(m_devices)) + { + removeDevice(d, true); + } +} + void DeviceModel::sanetize() { // diff --git a/src/DeviceFilter.h b/src/DeviceFilter.h index 5b0705b60..55d4e896c 100644 --- a/src/DeviceFilter.h +++ b/src/DeviceFilter.h @@ -26,7 +26,6 @@ #include "device.h" #include -#include #include #include #include @@ -43,6 +42,8 @@ class DeviceFilter : public QSortFilterProxyModel public: DeviceFilter(QObject *parent = nullptr); ~DeviceFilter(); + + void invalidatefilter(); }; /* ************************************************************************** */ @@ -52,21 +53,21 @@ class DeviceModel : public QAbstractListModel Q_OBJECT protected: - QHash roleNames() const; + QHash roleNames() const override; public: DeviceModel(QObject *parent = nullptr); DeviceModel(const DeviceModel &other, QObject *parent = nullptr); ~DeviceModel(); - int rowCount(const QModelIndex &parent = QModelIndex()) const; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool hasDevices() const { return !m_devices.empty(); } - void getDevices(QList &device); + bool hasDevices() const { return !m_devices.isEmpty(); } + void getDevices(QList &device); int getDeviceCount() const { return m_devices.size(); } - QList m_devices; + QList m_devices; enum DeviceRoles { // hw device @@ -89,6 +90,7 @@ class DeviceModel : public QAbstractListModel public slots: void addDevice(Device *d); void removeDevice(Device *d, bool del); + void clearDevices(); void sanetize(); }; diff --git a/src/DeviceManager.cpp b/src/DeviceManager.cpp index 19274644f..7dc38dad9 100644 --- a/src/DeviceManager.cpp +++ b/src/DeviceManager.cpp @@ -111,7 +111,7 @@ DeviceManager::DeviceManager(bool daemon) if (m_dbInternal || m_dbExternal) { - // Load blacklist + // Load device blacklist if (!m_daemonMode) { QSqlQuery queryBlacklist; @@ -213,6 +213,9 @@ DeviceManager::DeviceManager(bool daemon) DeviceManager::~DeviceManager() { + qDeleteAll(m_bluetoothAdapters); + m_bluetoothAdapters.clear(); + delete m_bluetoothAdapter; delete m_discoveryAgent; @@ -415,7 +418,7 @@ bool DeviceManager::checkBluetoothPermissions() if (os_was != m_permOS || gps_was != m_permGPS || loc_was != m_permLocationBLE || loc_bg_was != m_permLocationBKG) { - // this function did changed the Bluetooth permission + // this function did change the Bluetooth permission Q_EMIT permissionsChanged(); } if (btP_was != m_blePermissions) @@ -707,6 +710,8 @@ void DeviceManager::deviceDiscoveryStopped() qDebug() << "DeviceManager::deviceDiscoveryStopped()"; } +/* ************************************************************************** */ + void DeviceManager::setLastRun() { QSqlQuery setLastRun; @@ -1701,6 +1706,16 @@ void DeviceManager::removeDeviceData(const QString &address) /* ************************************************************************** */ /* ************************************************************************** */ +void DeviceManager::invalidate() +{ + m_devices_filter->invalidate(); +} + +void DeviceManager::invalidateFilter() +{ + m_devices_filter->invalidatefilter(); +} + QVariant DeviceManager::getDeviceByProxyIndex(const int index, const DeviceUtils::DeviceType deviceType) const { DeviceFilter *filter = m_devices_filter; @@ -1715,11 +1730,6 @@ QVariant DeviceManager::getDeviceByProxyIndex(const int index, const DeviceUtils return QVariant::fromValue(filter->data(proxyIndex, DeviceModel::PointerRole)); } -void DeviceManager::invalidate() -{ - m_devices_filter->invalidate(); -} - void DeviceManager::orderby(int role, Qt::SortOrder order) { m_devices_filter->setSortRole(role); diff --git a/src/DeviceManager.h b/src/DeviceManager.h index 8b0165a1c..ed8db22db 100644 --- a/src/DeviceManager.h +++ b/src/DeviceManager.h @@ -46,9 +46,8 @@ class DeviceManager: public QObject Q_OBJECT Q_PROPERTY(bool hasDevices READ areDevicesAvailable NOTIFY devicesListUpdated) - - Q_PROPERTY(DeviceFilter *devicesList READ getDevicesFiltered NOTIFY devicesListUpdated) Q_PROPERTY(int deviceCount READ getDeviceCount NOTIFY devicesListUpdated) + Q_PROPERTY(DeviceFilter *devicesList READ getDevicesFiltered NOTIFY devicesListUpdated) Q_PROPERTY(DeviceFilter *devicesPlantList READ getDevicesPlantFiltered NOTIFY devicesListUpdated) Q_PROPERTY(int devicePlantCount READ getDevicePlantCount NOTIFY devicesListUpdated) @@ -61,9 +60,9 @@ class DeviceManager: public QObject //////// - Q_PROPERTY(bool scanning READ isScanning NOTIFY scanningChanged) - Q_PROPERTY(bool listening READ isListening NOTIFY listeningChanged) Q_PROPERTY(bool advertising READ isAdvertising NOTIFY advertisingChanged) + Q_PROPERTY(bool listening READ isListening NOTIFY listeningChanged) + Q_PROPERTY(bool scanning READ isScanning NOTIFY scanningChanged) Q_PROPERTY(bool updating READ isUpdating NOTIFY updatingChanged) Q_PROPERTY(bool syncing READ isSyncing NOTIFY syncingChanged) @@ -102,6 +101,8 @@ class DeviceManager: public QObject QBluetoothDeviceDiscoveryAgent *m_discoveryAgent = nullptr; QBluetoothLocalDevice::HostMode m_ble_hostmode = QBluetoothLocalDevice::HostPoweredOff; + QList m_bluetoothAdapters; + QList m_devices_blacklist; DeviceModel *m_devices_nearby_model = nullptr; @@ -123,14 +124,14 @@ class DeviceManager: public QObject QList m_devices_syncing_queue; QList m_devices_syncing; - bool m_scanning = false; - bool isScanning() const { return m_scanning; } + bool m_advertising = false; + bool isAdvertising() const { return m_advertising; } bool m_listening = false; bool isListening() const { return m_listening; } - bool m_advertising = false; - bool isAdvertising() const { return m_advertising; } + bool m_scanning = false; + bool isScanning() const { return m_scanning; } bool m_updating = false; bool isUpdating() const; @@ -165,13 +166,15 @@ class DeviceManager: public QObject void bluetoothChanged(); void permissionsChanged(); + void adaptersListUpdated(); + void devicesListUpdated(); void devicesNearbyUpdated(); void devicesBlacklistUpdated(); - void scanningChanged(); - void listeningChanged(); void advertisingChanged(); + void listeningChanged(); + void scanningChanged(); void updatingChanged(); void syncingChanged(); void hostModeChanged(); @@ -198,6 +201,11 @@ private slots: bool isDaemon() const { return m_daemonMode; } + // Adapters management + Q_INVOKABLE bool areAdaptersAvailable() const { return m_bluetoothAdapters.size(); } + QVariant getAdapters() const { return QVariant::fromValue(m_bluetoothAdapters); } + int getAdaptersCount() const { return m_bluetoothAdapters.size(); } + // Bluetooth management Q_INVOKABLE bool checkBluetooth(); Q_INVOKABLE bool checkBluetoothPermissions(); @@ -213,8 +221,8 @@ private slots: Q_INVOKABLE void scanDevices_stop(); Q_INVOKABLE void refreshDevices_background(); //!< Refresh devices on the Android background service - Q_INVOKABLE void refreshDevices_listen(); //!< Refresh devices with data >xh old (as they appear nearby) + Q_INVOKABLE void listenDevices_start(); Q_INVOKABLE void listenDevices_stop(); @@ -244,9 +252,9 @@ private slots: Q_INVOKABLE bool areDevicesAvailable() const { return m_devices_model->hasDevices(); } Q_INVOKABLE void disconnectDevices(); - DeviceFilter *getDevicesNearby() const { return m_devices_nearby_filter; } - DeviceFilter *getDevicesFiltered() const { return m_devices_filter; } int getDeviceCount() const { return m_devices_model->getDeviceCount(); } + DeviceFilter *getDevicesFiltered() const { return m_devices_filter; } + DeviceFilter *getDevicesNearby() const { return m_devices_nearby_filter; } DeviceFilter *getDevicesPlantFiltered() const { return m_devicesPlant_filter; } int getDevicePlantCount() const { return m_devicesPlant_model->getDeviceCount(); } @@ -255,9 +263,7 @@ private slots: DeviceFilter *getDevicesEnvFiltered() const { return m_devicesEnv_filter; } int getDeviceEnvCount() const { return m_devicesEnv_model->getDeviceCount(); } - Q_INVOKABLE QVariant getDeviceByProxyIndex(const int index, - const DeviceUtils::DeviceType deviceType = DeviceUtils::DEVICE_UNKNOWN) const; - + // Sorting and filtering Q_INVOKABLE void orderby_manual(); Q_INVOKABLE void orderby_model(); Q_INVOKABLE void orderby_name(); @@ -267,7 +273,11 @@ private slots: Q_INVOKABLE void orderby_insideoutside(); void orderby(int role, Qt::SortOrder order); + Q_INVOKABLE QVariant getDeviceByProxyIndex(const int index, + const DeviceUtils::DeviceType deviceType = DeviceUtils::DEVICE_UNKNOWN) const; + void invalidate(); + void invalidateFilter(); // Device data export Q_INVOKABLE bool exportDataSave(); diff --git a/src/device.cpp b/src/device.cpp index d34252cc7..e8d9aa630 100644 --- a/src/device.cpp +++ b/src/device.cpp @@ -927,10 +927,7 @@ bool Device::hasAddressMAC() const return true; #endif - if (m_deviceAddressMAC.isEmpty()) - return false; - - return true; + return !m_deviceAddressMAC.isEmpty(); } QString Device::getAddressMAC() const @@ -1307,7 +1304,8 @@ void Device::deviceConnected() m_ble_status = DeviceUtils::DEVICE_UPDATING_HISTORY; } else if (m_ble_action == DeviceUtils::ACTION_SCAN || - m_ble_action == DeviceUtils::ACTION_SCAN_WITH_VALUES) + m_ble_action == DeviceUtils::ACTION_SCAN_WITH_VALUES || + m_ble_action == DeviceUtils::ACTION_SCAN_WITHOUT_VALUES) { m_ble_status = DeviceUtils::DEVICE_WORKING; } @@ -1369,7 +1367,9 @@ void Device::deviceErrored(QLowEnergyController::Error error) QLowEnergyController::UnknownError 1 An unknown error has occurred. QLowEnergyController::UnknownRemoteDeviceError 2 The remote Bluetooth Low Energy device with the address passed to the constructor of this class cannot be found. QLowEnergyController::NetworkError 3 The attempt to read from or write to the remote device failed. - QLowEnergyController::InvalidBluetoothAdapterError 4 The local Bluetooth device with the add… QLowEnergyController::AdvertisingError (since Qt 5.7) 6 The attempt to start advertising failed. + QLowEnergyController::InvalidBluetoothAdapterError 4 The local Bluetooth device with the address passed to the constructor of this class cannot be found or there is no local Bluetooth device. + QLowEnergyController::ConnectionError (since Qt 5.5) 5 The attempt to connect to the remote device failed. + QLowEnergyController::AdvertisingError (since Qt 5.7) 6 The attempt to start advertising failed. QLowEnergyController::RemoteHostClosedError (since Qt 5.10) 7 The remote device closed the connection. QLowEnergyController::AuthorizationError (since Qt 5.14) 8 The local Bluetooth device closed the connection due to insufficient authorization. QLowEnergyController::MissingPermissionsError (since Qt 6.4) 9 The operating system requests permissions which were not granted by the user. diff --git a/src/device.h b/src/device.h index 0206ecaac..e662c41d5 100644 --- a/src/device.h +++ b/src/device.h @@ -98,11 +98,11 @@ class Device: public QObject Q_PROPERTY(int serviceClass READ getServiceClass NOTIFY advertisementUpdated) Q_PROPERTY(int bluetoothConfiguration READ getBluetoothConfiguration NOTIFY advertisementUpdated) - Q_PROPERTY(bool enabled READ isEnabled NOTIFY statusUpdated) Q_PROPERTY(int status READ getStatus NOTIFY statusUpdated) Q_PROPERTY(int action READ getAction NOTIFY statusUpdated) - Q_PROPERTY(bool busy READ isBusy NOTIFY statusUpdated) + Q_PROPERTY(bool enabled READ isEnabled NOTIFY statusUpdated) Q_PROPERTY(bool connected READ isConnected NOTIFY statusUpdated) + Q_PROPERTY(bool busy READ isBusy NOTIFY statusUpdated) Q_PROPERTY(bool working READ isWorking NOTIFY statusUpdated) Q_PROPERTY(bool updating READ isUpdating NOTIFY statusUpdated) Q_PROPERTY(bool errored READ isErrored NOTIFY statusUpdated) @@ -245,7 +245,7 @@ class Device: public QObject virtual ~Device(); void setName(const QString &name); - void setDeviceClass(const int major, const int minor, const int service); + virtual void setDeviceClass(const int major, const int minor, const int service); virtual void setCoreConfiguration(const int bleconf); // Device infos diff --git a/src/device_utils.h b/src/device_utils.h index f17b8e33e..9434d50b9 100644 --- a/src/device_utils.h +++ b/src/device_utils.h @@ -73,6 +73,7 @@ class DeviceUtils: public QObject DEVICE_LIGHT = 8, DEVICE_BEACON, DEVICE_REMOTE, + DEVICE_PBP, DEVICE_PGP, }; Q_ENUM(DeviceType) @@ -154,7 +155,8 @@ class DeviceUtils: public QObject ACTION_CALIBRATE, ACTION_SCAN = 64, //!< Scan for services and their characteristics - ACTION_SCAN_WITH_VALUES, //!< Scan for services and their characteristics and associated values + ACTION_SCAN_WITH_VALUES, //!< Scan for services and their characteristics (and associated values) + ACTION_SCAN_WITHOUT_VALUES, //!< Scan for services and their characteristics (without associated values) ACTION_REBOOT = 256, ACTION_SHUTDOWN, diff --git a/src/devices/device_thermobeacon.cpp b/src/devices/device_thermobeacon.cpp index c8ee59546..7f1431f50 100644 --- a/src/devices/device_thermobeacon.cpp +++ b/src/devices/device_thermobeacon.cpp @@ -420,6 +420,9 @@ void DeviceThermoBeacon::parseAdvertisementData(const uint16_t adv_mode, << "[mode: " << adv_mode << " / id: 0x" << QString::number(adv_id, 16) << "]"; qDebug() << "DATA (" << ba.size() << "bytes) > 0x" << ba.toHex(); */ + Q_UNUSED(adv_mode) + Q_UNUSED(adv_id) + // 20 bytes message if (ba.size() == 20) return;