LCOV - code coverage report
Current view: top level - src/lib - pokitdiscoveryagent.cpp (source / functions) Coverage Total Hit
Project: Dokit Lines: 70.9 % 86 61
Version: Functions: 65.0 % 20 13

            Line data    Source code
       1              : // SPDX-FileCopyrightText: 2022-2024 Paul Colby <git@colby.id.au>
       2              : // SPDX-License-Identifier: LGPL-3.0-or-later
       3              : 
       4              : /*!
       5              :  * \file
       6              :  * Defines the PokitDiscoveryAgent and PokitDiscoveryAgentPrivate classes.
       7              :  */
       8              : 
       9              : #include <qtpokit/pokitdiscoveryagent.h>
      10              : #include <qtpokit/pokitproducts.h>
      11              : #include "pokitdiscoveryagent_p.h"
      12              : 
      13              : #include <qtpokit/statusservice.h>
      14              : 
      15              : #include <QBluetoothUuid>
      16              : 
      17              : /*!
      18              :  * \class PokitDiscoveryAgent
      19              :  *
      20              :  * The PokitDiscoveryAgent class discovers nearby Pokit devices.
      21              :  *
      22              :  * After constructing a PokitDiscoveryAgent object, and subscribing to the relevant signals,
      23              :  * invoke start() to begin discovery.
      24              :  */
      25              : 
      26              : /*!
      27              :  * Constructs a new Pokit device discovery agent with \a parent, using \a deviceAdapter for the
      28              :  * search device.
      29              :  */
      30            0 : PokitDiscoveryAgent::PokitDiscoveryAgent(
      31            0 :     const QBluetoothAddress &deviceAdapter, QObject *parent)
      32            0 :     : QBluetoothDeviceDiscoveryAgent(deviceAdapter, parent),
      33            0 :       d_ptr(new PokitDiscoveryAgentPrivate(this))
      34            0 : {
      35              : 
      36            0 : }
      37              : 
      38              : /*!
      39              :  * Constructs a new Pokit device discovery agent with \a parent.
      40              :  */
      41        18400 : PokitDiscoveryAgent::PokitDiscoveryAgent(QObject * parent)
      42         7745 :     : QBluetoothDeviceDiscoveryAgent(parent),
      43        26145 :       d_ptr(new PokitDiscoveryAgentPrivate(this))
      44        14631 : {
      45              : 
      46        33031 : }
      47              : 
      48              : /*!
      49              :  * \cond internal
      50              :  * Constructs a new Pokit device discovery agent with \a parent, using \a deviceAdapter for the
      51              :  * search device, and private implementation \a d.
      52              :  */
      53            0 : PokitDiscoveryAgent::PokitDiscoveryAgent(
      54              :     PokitDiscoveryAgentPrivate * const d, const QBluetoothAddress &deviceAdapter,
      55            0 :     QObject * const parent)
      56            0 :     : QBluetoothDeviceDiscoveryAgent(deviceAdapter, parent), d_ptr(d)
      57            0 : {
      58              : 
      59            0 : }
      60              : 
      61              : /*!
      62              :  * Constructs a new Pokit device discovery agent with \a parent, and private implementation \a d.
      63              :  */
      64            0 : PokitDiscoveryAgent::PokitDiscoveryAgent(
      65            0 :     PokitDiscoveryAgentPrivate * const d, QObject * const parent)
      66            0 :     : QBluetoothDeviceDiscoveryAgent(parent), d_ptr(d)
      67            0 : {
      68              : 
      69            0 : }
      70              : /// \endcond
      71              : 
      72              : /*!
      73              :  * Destroys this PokitDiscoveryAgent object.
      74              :  */
      75        36228 : PokitDiscoveryAgent::~PokitDiscoveryAgent()
      76        14631 : {
      77        33031 :     delete d_ptr;
      78        50859 : }
      79              : 
      80              : /*!
      81              :  * Starts Pokit device discovery.
      82              :  *
      83              :  * This override simply enforces that \a method must be \c LowEnergyMethod, as all Pokit devices
      84              :  * used Bluetooth Low Energy (BLE).
      85              :  */
      86            0 : void PokitDiscoveryAgent::start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods methods)
      87            0 : {
      88            0 :     Q_D(PokitDiscoveryAgent);
      89            0 :     Q_ASSERT(methods == QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
      90            0 :     qCDebug(d->lc).noquote() << tr("Scanning for Bluetooth Low Energy devices.");
      91            0 :     QBluetoothDeviceDiscoveryAgent::start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
      92            0 : }
      93              : 
      94              : /*!
      95              :  * Starts Pokit device discovery.
      96              :  */
      97          114 : void PokitDiscoveryAgent::start()
      98           48 : {
      99           48 :     Q_D(PokitDiscoveryAgent);
     100          186 :     qCDebug(d->lc).noquote() << tr("Scanning for Bluetooth Low Energy devices.");
     101          162 :     QBluetoothDeviceDiscoveryAgent::start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
     102          162 : }
     103              : 
     104              : /*!
     105              :  * \fn void PokitDiscoveryAgent::pokitDeviceDiscovered(const QBluetoothDeviceInfo &info)
     106              :  *
     107              :  * This signal is emitted when the Pokit device described by \a info is discovered.
     108              :  */
     109              : 
     110              : /*!
     111              :  * \fn void PokitDiscoveryAgent::pokitDeviceUpdated(const QBluetoothDeviceInfo &info, QBluetoothDeviceInfo::Fields updatedFields)
     112              :  *
     113              :  * This signal is emitted when the Pokit device described by \a info is updated. The
     114              :  * \a updatedFields flags tell which information has been updated.
     115              :  */
     116              : 
     117              : /*!
     118              :  * \cond internal
     119              :  * \class PokitDiscoveryAgentPrivate
     120              :  *
     121              :  * The PokitDiscoveryAgentPrivate class provides private implementation for
     122              :  * PokitDiscoveryAgent.
     123              :  */
     124              : 
     125              : /*!
     126              :  * \internal
     127              :  * Constructs a new PokitDiscoveryAgentPrivate object with public implementation \a q.
     128              :  */
     129        18400 : PokitDiscoveryAgentPrivate::PokitDiscoveryAgentPrivate(PokitDiscoveryAgent * const q)
     130        26145 :     : q_ptr(q)
     131        14631 : {
     132        33031 :     connect(q, &QBluetoothDeviceDiscoveryAgent::canceled,
     133        17595 :             this, &PokitDiscoveryAgentPrivate::canceled);
     134              : 
     135        33031 :     connect(q, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered,
     136        17595 :             this, &PokitDiscoveryAgentPrivate::deviceDiscovered);
     137              : 
     138        11964 :     #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) // Signal added in Qt 5.12.
     139        27670 :     connect(q, &QBluetoothDeviceDiscoveryAgent::deviceUpdated,
     140        14928 :             this, &PokitDiscoveryAgentPrivate::deviceUpdated);
     141        11964 :     #endif
     142              : 
     143        33031 :     connect(q,
     144         7137 :         #if (QT_VERSION < QT_VERSION_CHECK(6, 2, 0))
     145         7137 :             QOverload<PokitDiscoveryAgent::Error>::of(&PokitDiscoveryAgent::error),
     146              :         #else
     147         7494 :             &QBluetoothDeviceDiscoveryAgent::errorOccurred,
     148         7494 :         #endif
     149        17595 :         this, &PokitDiscoveryAgentPrivate::error);
     150              : 
     151        33031 :     connect(q, &QBluetoothDeviceDiscoveryAgent::finished,
     152        17595 :             this, &PokitDiscoveryAgentPrivate::finished);
     153        33031 : }
     154              : 
     155              : /*!
     156              :  * Handle scan canceled signals, by simply logging the event for diagnostic purposes.
     157              :  */
     158           38 : void PokitDiscoveryAgentPrivate::canceled() const
     159           44 : {
     160           90 :     qCDebug(lc).noquote() << tr("Pokit device scan cancelled.");
     161           82 : }
     162              : 
     163              : /*!
     164              :  * Handle deviceDiscovered signals.
     165              :  *
     166              :  * Here we simply check if \a info describes a Pokit device, and if so, emit pokitDeviceDiscovered().
     167              :  */
     168          228 : void PokitDiscoveryAgentPrivate::deviceDiscovered(const QBluetoothDeviceInfo &info)
     169          264 : {
     170          264 :     Q_Q(PokitDiscoveryAgent);
     171          492 :     if (!isPokitProduct(info)) return;
     172          360 :     qCDebug(lc).noquote() << tr(R"(Discovered Pokit device "%1" at %2.)")
     173            0 :         .arg(info.name(), info.address().toString());
     174          328 :     Q_EMIT q->pokitDeviceDiscovered(info);
     175          176 : }
     176              : 
     177              : #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) // Required signal, and Fields, added in Qt 5.12.
     178              : /*!
     179              :  * Handle deviceUpdated signals.
     180              :  *
     181              :  * Here we simply check if \a info describes a Pokit device, and if so, emit pokitDeviceUpdated().
     182              :  *
     183              :  * \since Qt 5.12.0
     184              :  */
     185          192 : void PokitDiscoveryAgentPrivate::deviceUpdated(
     186              :     const QBluetoothDeviceInfo &info, QBluetoothDeviceInfo::Fields updatedFields)
     187          210 : {
     188          210 :     Q_Q(PokitDiscoveryAgent);
     189          402 :     if (!isPokitProduct(info)) return;
     190          300 :     qCDebug(lc).noquote() << tr(R"(Pokit device "%1" at %2 updated with RSSI %3.)")
     191            0 :         .arg(info.name(), info.address().toString()).arg(info.rssi());
     192          268 :     Q_EMIT q->pokitDeviceUpdated(info, updatedFields);
     193          140 : }
     194              : #endif
     195              : 
     196              : /*!
     197              :  * Handle scan errors, by simply logging \a error for diagnostic purposes.
     198              :  */
     199          148 : void PokitDiscoveryAgentPrivate::error(const QBluetoothDeviceDiscoveryAgent::Error error) const
     200           92 : {
     201          628 :     qCWarning(lc).noquote() << tr("Pokit device scan error:") << error;
     202          240 : }
     203              : 
     204              : /*!
     205              :  * Handle scan finished signals, by simply logging the event for diagnostic purposes.
     206              :  */
     207           38 : void PokitDiscoveryAgentPrivate::finished() const
     208           44 : {
     209           90 :     qCDebug(lc).noquote() << tr("Pokit device scan finished.");
     210           82 : }
     211              : 
     212              : /// \endcond
        

Generated by: LCOV version 2.0-1