Skip to content

Commit

Permalink
Cleanup the device management code
Browse files Browse the repository at this point in the history
  • Loading branch information
emericg committed Sep 19, 2023
1 parent ef7743b commit bed47c2
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 41 deletions.
15 changes: 14 additions & 1 deletion src/DeviceFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ DeviceFilter::~DeviceFilter()
//
}

void DeviceFilter::invalidatefilter()
{
invalidateFilter();
}

/* ************************************************************************** */

bool DeviceFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
Expand Down Expand Up @@ -246,7 +251,7 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const
return QVariant();
}

void DeviceModel::getDevices(QList<Device *> &device)
void DeviceModel::getDevices(QList <Device *> &device)
{
for (auto d: qAsConst(m_devices))
{
Expand Down Expand Up @@ -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()
{
//
Expand Down
16 changes: 9 additions & 7 deletions src/DeviceFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "device.h"

#include <QObject>
#include <QByteArray>
#include <QMetaType>
#include <QAbstractListModel>
#include <QSortFilterProxyModel>
Expand All @@ -43,6 +42,8 @@ class DeviceFilter : public QSortFilterProxyModel
public:
DeviceFilter(QObject *parent = nullptr);
~DeviceFilter();

void invalidatefilter();
};

/* ************************************************************************** */
Expand All @@ -52,21 +53,21 @@ class DeviceModel : public QAbstractListModel
Q_OBJECT

protected:
QHash<int, QByteArray> roleNames() const;
QHash <int, QByteArray> 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 *> &device);
bool hasDevices() const { return !m_devices.isEmpty(); }
void getDevices(QList <Device *> &device);
int getDeviceCount() const { return m_devices.size(); }

QList<Device *> m_devices;
QList <Device *> m_devices;

enum DeviceRoles {
// hw device
Expand All @@ -89,6 +90,7 @@ class DeviceModel : public QAbstractListModel
public slots:
void addDevice(Device *d);
void removeDevice(Device *d, bool del);
void clearDevices();
void sanetize();
};

Expand Down
24 changes: 17 additions & 7 deletions src/DeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ DeviceManager::DeviceManager(bool daemon)

if (m_dbInternal || m_dbExternal)
{
// Load blacklist
// Load device blacklist
if (!m_daemonMode)
{
QSqlQuery queryBlacklist;
Expand Down Expand Up @@ -213,6 +213,9 @@ DeviceManager::DeviceManager(bool daemon)

DeviceManager::~DeviceManager()
{
qDeleteAll(m_bluetoothAdapters);
m_bluetoothAdapters.clear();

delete m_bluetoothAdapter;
delete m_discoveryAgent;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -707,6 +710,8 @@ void DeviceManager::deviceDiscoveryStopped()
qDebug() << "DeviceManager::deviceDiscoveryStopped()";
}

/* ************************************************************************** */

void DeviceManager::setLastRun()
{
QSqlQuery setLastRun;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
42 changes: 26 additions & 16 deletions src/DeviceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -102,6 +101,8 @@ class DeviceManager: public QObject
QBluetoothDeviceDiscoveryAgent *m_discoveryAgent = nullptr;
QBluetoothLocalDevice::HostMode m_ble_hostmode = QBluetoothLocalDevice::HostPoweredOff;

QList <QObject *> m_bluetoothAdapters;

QList <QString> m_devices_blacklist;

DeviceModel *m_devices_nearby_model = nullptr;
Expand All @@ -123,14 +124,14 @@ class DeviceManager: public QObject
QList <QObject *> m_devices_syncing_queue;
QList <QObject *> 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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();

Expand Down Expand Up @@ -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(); }
Expand All @@ -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();
Expand All @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/device_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class DeviceUtils: public QObject
DEVICE_LIGHT = 8,
DEVICE_BEACON,
DEVICE_REMOTE,
DEVICE_PBP,
DEVICE_PGP,
};
Q_ENUM(DeviceType)
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/devices/device_thermobeacon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit bed47c2

Please sign in to comment.