LCOV - code coverage report
Current view: top level - src/lib - abstractpokitservice.cpp (source / functions) Coverage Total Hit
Project: Dokit Lines: 62.8 % 298 187
Version: Functions: 89.3 % 28 25

            Line data    Source code
       1              : // SPDX-FileCopyrightText: 2022-2026 Paul Colby <git@colby.id.au>
       2              : // SPDX-License-Identifier: LGPL-3.0-or-later
       3              : 
       4              : /*!
       5              :  * \file
       6              :  * Defines the AbstractPokitService and AbstractPokitServicePrivate classes.
       7              :  */
       8              : 
       9              : #include <qtpokit/abstractpokitservice.h>
      10              : #include "abstractpokitservice_p.h"
      11              : #include "pokitproducts_p.h"
      12              : #include "../stringliterals_p.h"
      13              : 
      14              : #include <qtpokit/pokitdevice.h>
      15              : 
      16              : #include <QLowEnergyController>
      17              : 
      18              : QTPOKIT_BEGIN_NAMESPACE
      19              : DOKIT_USE_STRINGLITERALS
      20              : 
      21              : /*!
      22              :  * \class AbstractPokitService
      23              :  *
      24              :  * The AbstractPokitService class provides a common base for Pokit services classes.
      25              :  */
      26              : 
      27              : /*!
      28              :  * \cond internal
      29              :  * Constructs a new Pokit service with \a parent, and private implementation \a d.
      30              :  */
      31        27370 : AbstractPokitService::AbstractPokitService(
      32        27370 :     AbstractPokitServicePrivate * const d, QObject * const parent)
      33        47328 :     : QObject(parent), d_ptr(d)
      34        22406 : {
      35              : 
      36        49776 : }
      37              : /// \endcond
      38              : 
      39              : /*!
      40              :  * Destroys this AbstractPokitService object.
      41              :  */
      42        10220 : AbstractPokitService::~AbstractPokitService()
      43        15526 : {
      44        25746 :     delete d_ptr;
      45        25746 : }
      46              : 
      47              : /*!
      48              :  * \fn virtual bool AbstractPokitService::readCharacteristics() = 0
      49              :  *
      50              :  * Read all characteristics.
      51              :  *
      52              :  * This convenience function will queue refresh requests of all characteristics supported by this
      53              :  * service.
      54              :  *
      55              :  * Relevant `*Service::*Read` signals will be emitted by derived class objects as each
      56              :  * characteristic is successfully read.
      57              :  */
      58              : 
      59              : /*!
      60              :  * Returns `true` if autodiscovery of services and service details is enabled, `false` otherwise.
      61              :  *
      62              :  * \see setAutoDiscover for more information on what autodiscovery provides.
      63              :  */
      64          210 : bool AbstractPokitService::autoDiscover() const
      65          333 : {
      66          333 :     Q_D(const AbstractPokitService);
      67          543 :     return d->autoDiscover;
      68          333 : }
      69              : 
      70              : /*!
      71              :  * If \a discover is \c true, autodiscovery will be attempted.
      72              :  *
      73              :  * Specifically, this may resulting in automatic invocation of:
      74              :  * * QLowEnergyController::discoverServices if/when the internal controller is connected; and
      75              :  * * QLowEnergyService::discoverDetails if/when an internal service object is created.
      76              :  *
      77              :  * \see autoDiscover
      78              :  */
      79          140 : void AbstractPokitService::setAutoDiscover(const bool discover)
      80          222 : {
      81          222 :     Q_D(AbstractPokitService);
      82          362 :     d->autoDiscover = discover;
      83          362 : }
      84              : 
      85              : /*!
      86              :  * Returns the Pokit product this service is attached to.
      87              :  *
      88              :  * \see setPokitProduct
      89              :  */
      90        19950 : std::optional<PokitProduct> AbstractPokitService::pokitProduct() const
      91        11490 : {
      92        11490 :     Q_D(const AbstractPokitService);
      93        31440 :     return d->pokitProduct;
      94        11490 : }
      95              : 
      96              : /*!
      97              :  * Sets the current Pokit \a product.
      98              :  *
      99              :  * This must be called to set the product before this object's BLE controller's services are discovered. If
     100              :  * autoDiscover() is enabled, then this should be done before the controller's \c connectToDevice() is called.
     101              :  *
     102              :  * For example:
     103              :  * ```
     104              :  * Q_ASSERT(isPokitProduct(deviceInfo));
     105              :  * auto controller = QLowEnergyController::createCentral(deviceInfo);
     106              :  * auto service = new DsoService(controller);
     107              :  * service->setPokitProduct(pokitProduct(deviceInfo));
     108              :  * controller->connectToDevice();
     109              :  * ```
     110              :  *
     111              :  * \see autoDiscover
     112              :  * \see pokitProduct
     113              :  */
     114        17430 : void AbstractPokitService::setPokitProduct(const PokitProduct product)
     115        10214 : {
     116        10214 :     Q_D(AbstractPokitService);
     117        27644 :     d->pokitProduct = product;
     118        27644 : }
     119              : 
     120              : /*!
     121              :  * Returns a non-const pointer to the internal service object, if any.
     122              :  */
     123          630 : QLowEnergyService * AbstractPokitService::service()
     124          574 : {
     125          574 :     Q_D(AbstractPokitService);
     126         1204 :     return d->service;
     127          574 : }
     128              : 
     129              : /*!
     130              :  * Returns a const pointer to the internal service object, if any.
     131              :  */
     132           70 : const QLowEnergyService * AbstractPokitService::service() const
     133          111 : {
     134          111 :     Q_D(const AbstractPokitService);
     135          181 :     return d->service;
     136          111 : }
     137              : 
     138              : /*!
     139              :  * \fn void AbstractPokitService::serviceDetailsDiscovered()
     140              :  *
     141              :  * This signal is emitted when the Pokit service details have been discovered.
     142              :  *
     143              :  * Once this signal has been emitted, cached characteristics values should be immediately available
     144              :  * via derived classes' accessor functions, and refreshes can be queued via readCharacteristics()
     145              :  * and any related read functions provided by derived classes.
     146              :  */
     147              : 
     148              : /*!
     149              :  * \fn void AbstractPokitService::serviceErrorOccurred(QLowEnergyService::ServiceError newError)
     150              :  *
     151              :  *  This signal is emitted whenever an error occurs on the underlying QLowEnergyService.
     152              :  */
     153              : 
     154              : /*!
     155              :  * \cond internal
     156              :  * \class AbstractPokitServicePrivate
     157              :  *
     158              :  * The AbstractPokitServicePrivate class provides private implementation for AbstractPokitService.
     159              :  */
     160              : 
     161              : /*!
     162              :  * \internal
     163              :  * Constructs a new AbstractPokitServicePrivate object with public implementation \a q.
     164              :  *
     165              :  * Note, typically the \a serviceUuid should be set validly, however, in the rare case that a
     166              :  * service's UUID can vary (ie the Status Service), \a serviceUuid may be set to a `null`
     167              :  * QBluetoothUuid here, and updated when the correct service UUID is known.
     168              :  *
     169              :  * \see StatusService::ServiceUuids
     170              :  * \see StatusServicePrivate::serviceDiscovered
     171              :  */
     172        27370 : AbstractPokitServicePrivate::AbstractPokitServicePrivate(const QBluetoothUuid &serviceUuid,
     173        27370 :     QLowEnergyController * controller, AbstractPokitService * const q)
     174        47328 :     : controller(controller), serviceUuid(serviceUuid), q_ptr(q)
     175        22406 : {
     176        49776 :     if (controller) {
     177        23328 :         connect(controller, &QLowEnergyController::connected,
     178        13608 :                 this, &AbstractPokitServicePrivate::connected);
     179              : 
     180        23328 :         connect(controller, &QLowEnergyController::discoveryFinished,
     181        13608 :                 this, &AbstractPokitServicePrivate::discoveryFinished);
     182              : 
     183        23328 :         connect(controller, &QLowEnergyController::serviceDiscovered,
     184        13608 :                 this, &AbstractPokitServicePrivate::serviceDiscovered);
     185              : 
     186        23328 :         createServiceObject();
     187         6318 :     }
     188        49776 : }
     189              : 
     190              : /*!
     191              :  * Creates an internal service object from the internal controller.
     192              :  *
     193              :  * Any existing service object will *not* be replaced.
     194              :  *
     195              :  * Returns \c true if a service was created successfully, either now, or sometime previously.
     196              :  */
     197        17570 : bool AbstractPokitServicePrivate::createServiceObject()
     198         6781 : {
     199        24351 :     if (!controller) {
     200          359 :         return false;
     201          359 :     }
     202              : 
     203        23712 :     if (service) {
     204          130 :         qCDebug(lc).noquote() << tr("Already have service object:") << service;
     205           62 :         return true;
     206           26 :     }
     207              : 
     208        19926 :     if (serviceUuid.isNull()) {
     209         1300 :         qCDebug(lc).noquote() << tr("Service UUID not assigned yet.");
     210          620 :         return false;
     211          260 :     }
     212              : 
     213        22656 :     service = controller->createServiceObject(serviceUuid, this);
     214        22656 :     if (!service) {
     215         6136 :         return false;
     216         6136 :     }
     217            0 :     qCDebug(lc).noquote() << tr("Service object created for %1 device:").arg(toString(*this->pokitProduct)) << service;
     218              : 
     219            0 :     connect(service, &QLowEnergyService::stateChanged,
     220            0 :             this, &AbstractPokitServicePrivate::stateChanged);
     221            0 :     connect(service, &QLowEnergyService::characteristicRead,
     222            0 :             this, &AbstractPokitServicePrivate::characteristicRead);
     223            0 :     connect(service, &QLowEnergyService::characteristicWritten,
     224            0 :             this, &AbstractPokitServicePrivate::characteristicWritten);
     225            0 :     connect(service, &QLowEnergyService::characteristicChanged,
     226            0 :             this, &AbstractPokitServicePrivate::characteristicChanged);
     227              : 
     228            0 :     connect(service, &QLowEnergyService::descriptorRead, this,
     229            0 :         [](const QLowEnergyDescriptor &descriptor, const QByteArray &value){
     230            0 :             qCDebug(lc).noquote() << tr(R"(Descriptor "%1" (%2) read.)")
     231            0 :                 .arg(descriptor.name(), descriptor.uuid().toString());
     232            0 :             Q_UNUSED(value)
     233            0 :         });
     234              : 
     235            0 :     connect(service, &QLowEnergyService::descriptorWritten, this,
     236            0 :         [](const QLowEnergyDescriptor &descriptor, const QByteArray &newValue){
     237            0 :             qCDebug(lc).noquote() << tr(R"(Descriptor "%1" (%2) written.)")
     238            0 :                 .arg(descriptor.name(), descriptor.uuid().toString());
     239            0 :             Q_UNUSED(newValue)
     240            0 :         });
     241              : 
     242            0 :     connect(service,
     243            0 :     #if (QT_VERSION < QT_VERSION_CHECK(6, 2, 0))
     244            0 :         QOverload<QLowEnergyService::ServiceError>::of(&QLowEnergyService::error),
     245              :     #else
     246            0 :         &QLowEnergyService::errorOccurred,
     247            0 :     #endif
     248            0 :         this, &AbstractPokitServicePrivate::errorOccurred);
     249              : 
     250            0 :     if (autoDiscover) {
     251            0 :         service->discoverDetails();
     252            0 :     }
     253            0 :     return true;
     254         6136 : }
     255              : 
     256              : /*!
     257              :  * Get \a uuid characteristic from the underlying service. This helper function is equivalent to
     258              :  *
     259              :  * ```
     260              :  * return service->characteristic(uuid);
     261              :  * ```
     262              :  *
     263              :  * except that it performs some sanity checks, such as checking the service object pointer has been
     264              :  * assigned first, and also logs failures in a consistent manner.
     265              :  *
     266              :  * \return
     267              :  */
     268        18130 : QLowEnergyCharacteristic AbstractPokitServicePrivate::getCharacteristic(const QBluetoothUuid &uuid) const
     269        13194 : {
     270        31324 :     if (!service) {
     271        40130 :         qCDebug(lc).noquote() << tr(R"(Characteristic %1 "%2" requested before service assigned.)")
     272            0 :             .arg(uuid.toString(), PokitDevice::charcteristicToString(uuid));
     273        31324 :         return QLowEnergyCharacteristic();
     274        13194 :     }
     275              : 
     276            0 :     if (const QLowEnergyCharacteristic characteristic = service->characteristic(uuid); characteristic.isValid()) {
     277            0 :         return characteristic;
     278            0 :     }
     279              : 
     280            0 :     if (service->state() != QLowEnergyService::
     281            0 :         #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
     282            0 :         ServiceDiscovered
     283              :         #else
     284            0 :         RemoteServiceDiscovered
     285            0 :         #endif
     286            0 :     ) {
     287            0 :         qCWarning(lc).noquote() << tr(R"(Characteristic %1 "%2" requested before service %3 "%4" discovered.)")
     288            0 :             .arg(uuid.toString(), PokitDevice::charcteristicToString(uuid),
     289            0 :             service->serviceUuid().toString(), PokitDevice::serviceToString(service->serviceUuid()));
     290            0 :         qCInfo(lc).noquote() << tr("Current service state:") << service->state();
     291            0 :         return QLowEnergyCharacteristic();
     292            0 :     }
     293              : 
     294            0 :     qCWarning(lc).noquote() << tr(R"(Characteristic %1 "%2" not found in service %3 "%4".)")
     295            0 :         .arg(uuid.toString(), PokitDevice::charcteristicToString(uuid),
     296            0 :         service->serviceUuid().toString(), PokitDevice::serviceToString(service->serviceUuid()));
     297            0 :     return QLowEnergyCharacteristic();
     298            0 : }
     299              : 
     300              : /*!
     301              :  * Read the \a uuid characteristic.
     302              :  *
     303              :  * If successful, the `QLowEnergyService::characteristicRead` signal will be emitted by the internal
     304              :  * service object.  For convenience, derived classes should implement the characteristicRead()
     305              :  * virtual function to handle the read value.
     306              :  *
     307              :  * Returns \c true if the characteristic read request was successfully queued, \c false otherwise.
     308              :  *
     309              :  * \see AbstractPokitService::readCharacteristics()
     310              :  * \see AbstractPokitServicePrivate::characteristicRead()
     311              :  */
     312         1820 : bool AbstractPokitServicePrivate::readCharacteristic(const QBluetoothUuid &uuid)
     313         2886 : {
     314         4706 :     const QLowEnergyCharacteristic characteristic = getCharacteristic(uuid);
     315         4706 :     if (!characteristic.isValid()) {
     316         2886 :         return false;
     317         2886 :     }
     318         1326 :     qCDebug(lc).noquote() << tr(R"(Reading characteristic %1 "%2".)")
     319            0 :         .arg(uuid.toString(), PokitDevice::charcteristicToString(uuid));
     320            0 :     service->readCharacteristic(characteristic);
     321            0 :     return true;
     322         4706 : }
     323              : 
     324              : /*!
     325              :  * Enables client (Pokit device) side notification for characteristic \a uuid.
     326              :  *
     327              :  * Returns \c true if the notification enable request was successfully queued, \c false otherwise.
     328              :  *
     329              :  * \see AbstractPokitServicePrivate::characteristicChanged
     330              :  * \see AbstractPokitServicePrivate::disableCharacteristicNotificatons
     331              :  */
     332          630 : bool AbstractPokitServicePrivate::enableCharacteristicNotificatons(const QBluetoothUuid &uuid)
     333          999 : {
     334         1935 :     qCDebug(lc).noquote() << tr(R"(Enabling CCCD for characteristic %1 "%2".)")
     335            0 :         .arg(uuid.toString(), PokitDevice::charcteristicToString(uuid));
     336         1629 :     QLowEnergyCharacteristic characteristic = getCharacteristic(uuid);
     337         1629 :     if (!characteristic.isValid()) {
     338          999 :         return false;
     339          999 :     }
     340              : 
     341            0 :     QLowEnergyDescriptor descriptor = characteristic.descriptor(
     342            0 :         QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration); // 0x2902
     343            0 :     if (!descriptor.isValid()) {
     344            0 :         qCWarning(lc).noquote() << tr(R"(Characteristic %1 "%2" has no client configuration descriptor.)")
     345            0 :             .arg(uuid.toString(), PokitDevice::charcteristicToString(uuid));
     346            0 :         return false;
     347            0 :     }
     348              : 
     349            0 :     service->writeDescriptor(descriptor,
     350            0 :         #if (QT_VERSION >= QT_VERSION_CHECK(6, 2, 0))
     351            0 :         QLowEnergyCharacteristic::CCCDEnableNotification
     352              :         #else
     353            0 :         QByteArray::fromHex("0100") // See Qt6's QLowEnergyCharacteristic::CCCDEnableNotification.
     354            0 :         #endif
     355            0 :     );
     356            0 :     return true;
     357          630 : }
     358              : 
     359              : /*!
     360              :  * Disables client (Pokit device) side notification for characteristic \a uuid.
     361              :  *
     362              :  * Returns \c true if the notification disable request was successfully queued, \c false otherwise.
     363              :  *
     364              :  * \see AbstractPokitServicePrivate::characteristicChanged
     365              :  * \see AbstractPokitServicePrivate::enableCharacteristicNotificatons
     366              :  */
     367          630 : bool AbstractPokitServicePrivate::disableCharacteristicNotificatons(const QBluetoothUuid &uuid)
     368          999 : {
     369         1935 :     qCDebug(lc).noquote() << tr(R"(Disabling CCCD for characteristic %1 "%2".)")
     370            0 :         .arg(uuid.toString(), PokitDevice::charcteristicToString(uuid));
     371         1629 :     QLowEnergyCharacteristic characteristic = getCharacteristic(uuid);
     372         1629 :     if (!characteristic.isValid()) {
     373          999 :         return false;
     374          999 :     }
     375              : 
     376            0 :     QLowEnergyDescriptor descriptor = characteristic.descriptor(
     377            0 :         QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration); // 0x2902
     378            0 :     if (!descriptor.isValid()) {
     379            0 :         qCWarning(lc).noquote() << tr(R"(Characteristic %1 "%2" has no client configuration descriptor.)")
     380            0 :             .arg(uuid.toString(), PokitDevice::charcteristicToString(uuid));
     381            0 :         return false;
     382            0 :     }
     383              : 
     384            0 :     service->writeDescriptor(descriptor,
     385            0 :         #if (QT_VERSION >= QT_VERSION_CHECK(6, 2, 0))
     386            0 :         QLowEnergyCharacteristic::CCCDDisable
     387              :         #else
     388            0 :         QByteArray::fromHex("0000") // See Qt6's QLowEnergyCharacteristic::CCCDDisable.
     389            0 :         #endif
     390            0 :     );
     391            0 :     return true;
     392          630 : }
     393              : 
     394              : /*!
     395              :  * Returns `false` if \a data is smaller than \a minSize, otherwise returns \a failOnMax if \a data
     396              :  * is bigger than \a maxSize, otherwise returns `true`.
     397              :  *
     398              :  * A warning is logged if either \a minSize or \a maxSize is violated, regardless of the returned
     399              :  * value; ie this funcion can be used to simply warn if \a data is too big, or it can be used to
     400              :  * failed (return `false`) in that case.
     401              :  */
     402         2940 : bool AbstractPokitServicePrivate::checkSize(const QString &label, const QByteArray &data,
     403              :                                             const int minSize, const int maxSize,
     404              :                                             const bool failOnMax)
     405         4662 : {
     406         7602 :     if (data.size() < minSize) {
     407         3705 :         qCWarning(lc).noquote() << tr("%1 requires %n byte/s, but only %2 present: %3", nullptr, minSize)
     408         3718 :             .arg(label).arg(data.size()).arg(toHexString(data));
     409         1911 :         return false;
     410         1443 :     }
     411         5481 :     if ((maxSize >= 0) && (data.size() > maxSize)) {
     412         1585 :         qCWarning(lc).noquote() << tr("%1 has %n extraneous byte/s: %2", nullptr, data.size()-maxSize)
     413         1185 :             .arg(label, toHexString(data.mid(maxSize)));
     414          905 :         return (!failOnMax);
     415          555 :     }
     416         2664 :     return true;
     417         3219 : }
     418              : 
     419              : /*!
     420              :  * Returns up to \a maxSize bytes of \a data as a human readable hexadecimal string. If \a data
     421              :  * exceeds \a maxSize, then \a data is elided in the middle. For example:
     422              :  *
     423              :  * ```
     424              :  * toHex(QBytArray("\x1\x2\x3\x4\x5\x6", 4); // "0x01,02,...,05,06"
     425              :  * ```
     426              :  */
     427         1820 : QString AbstractPokitServicePrivate::toHexString(const QByteArray &data, const int maxSize)
     428         2886 : {
     429         4082 :     return (data.size() <= maxSize)
     430         7328 :         ? QString::fromLatin1("0x%1").arg(QLatin1String(data.toHex(',')))
     431         3486 :         : QString::fromLatin1("0x%1,...,%2").arg(
     432         2501 :             QLatin1String(data.left(maxSize/2-1).toHex(',')),
     433         7871 :             QLatin1String(data.right(maxSize/2-1).toHex(',')));
     434         2886 : }
     435              : 
     436              : /*!
     437              :  * Handles `QLowEnergyController::connected` events.
     438              :  *
     439              :  * If `autoDiscover` is enabled, this will begin service discovery on the newly connected controller.
     440              :  *
     441              :  * \see AbstractPokitService::autoDiscover()
     442              :  */
     443          140 : void AbstractPokitServicePrivate::connected()
     444           52 : {
     445          192 :     if (!controller) {
     446          233 :         qCWarning(lc).noquote() << tr("Connected with no controller set") << sender();
     447           62 :         return;
     448           26 :     }
     449              : 
     450          130 :     qCDebug(lc).noquote() << tr(R"(Connected to "%1" (%2) at %3.)").arg(
     451            0 :         controller->remoteName(), controller->remoteDeviceUuid().toString(),
     452            0 :         controller->remoteAddress().toString());
     453           96 :     if (autoDiscover) {
     454           96 :         controller->discoverServices();
     455           26 :     }
     456           26 : }
     457              : 
     458              : /*!
     459              :  * Handles `QLowEnergyController::discoveryFinished` events.
     460              :  *
     461              :  * As this event indicates that the controller has finished discovering services, this function will
     462              :  * invoke createServiceObject() to create the internal service object (if not already created).
     463              :  */
     464          140 : void AbstractPokitServicePrivate::discoveryFinished()
     465           52 : {
     466          192 :     if (!controller) {
     467          233 :         qCWarning(lc).noquote() << tr("Discovery finished with no controller set") << sender();
     468           62 :         return;
     469           26 :     }
     470              : 
     471          130 :     qCDebug(lc).noquote() << tr(R"(Discovery finished for "%1" (%2) at %3.)").arg(
     472            0 :         controller->remoteName(), controller->remoteDeviceUuid().toString(),
     473            0 :         controller->remoteAddress().toString());
     474              : 
     475           96 :     if (!createServiceObject()) {
     476          246 :         qCWarning(lc).noquote() << tr("Discovery finished, but service not found.");
     477           26 :         Q_Q(AbstractPokitService);
     478           96 :         Q_EMIT q->serviceErrorOccurred(QLowEnergyService::ServiceError::UnknownError);
     479           26 :     }
     480           26 : }
     481              : 
     482              : /*!
     483              :  * Handles `QLowEnergyController::errorOccurred` events.
     484              :  *
     485              :  * This function simply re-emits \a newError as AbstractPokitService::serviceErrorOccurred.
     486              :  */
     487           70 : void AbstractPokitServicePrivate::errorOccurred(const QLowEnergyService::ServiceError newError)
     488          111 : {
     489          111 :     Q_Q(AbstractPokitService);
     490          215 :     qCDebug(lc).noquote() << tr("Service error") << newError;
     491          181 :     Q_EMIT q->serviceErrorOccurred(newError);
     492          181 : }
     493              : 
     494              : /*!
     495              :  * Handles `QLowEnergyController::serviceDiscovered` events.
     496              :  *
     497              :  * If the discovered service is the one this (or rather the derived) class wraps, then
     498              :  * createServiceObject() will be invoked immediately (otherwise it will be invoked after full
     499              :  * service discovery has completed, ie in discoveryFinished()).
     500              :  */
     501          420 : void AbstractPokitServicePrivate::serviceDiscovered(const QBluetoothUuid &newService)
     502          666 : {
     503         1086 :     if ((!service) && (newService == serviceUuid)) {
     504          645 :         qCDebug(lc).noquote() << tr("Service discovered") << newService;
     505          543 :         createServiceObject();
     506          333 :     }
     507         1086 : }
     508              : 
     509              : /*!
     510              :  * Handles `QLowEnergyController::stateChanged` events.
     511              :  *
     512              :  * If \a newState indicates that service details have now been discovered, then
     513              :  * AbstractPokitService::serviceDetailsDiscovered will be emitted.
     514              :  *
     515              :  * \see AbstractPokitService::autoDiscover()
     516              :  */
     517          210 : void AbstractPokitServicePrivate::stateChanged(QLowEnergyService::ServiceState newState)
     518          333 : {
     519          645 :     qCDebug(lc).noquote() << tr("State changed to") << newState;
     520              : 
     521          543 :     if (lc().isDebugEnabled()) {
     522            0 :         for (const auto &characteristic: service->characteristics()) {
     523            0 :             QStringList properties;
     524              :             /// \cond no-doxygen
     525            0 :             #define QTPOKIT_INTERNAL_TEST_AND_APPEND(property) \
     526            0 :             if (characteristic.properties().testFlag(QLowEnergyCharacteristic::property)) { \
     527            0 :                 properties.append(QStringLiteral(#property).toLower());\
     528            0 :             }
     529              :             /// \endcond
     530            0 :             QTPOKIT_INTERNAL_TEST_AND_APPEND(Broadcasting)
     531            0 :             QTPOKIT_INTERNAL_TEST_AND_APPEND(Read)
     532            0 :             QTPOKIT_INTERNAL_TEST_AND_APPEND(WriteNoResponse)
     533            0 :             QTPOKIT_INTERNAL_TEST_AND_APPEND(Write)
     534            0 :             QTPOKIT_INTERNAL_TEST_AND_APPEND(Notify)
     535            0 :             QTPOKIT_INTERNAL_TEST_AND_APPEND(Indicate)
     536            0 :             QTPOKIT_INTERNAL_TEST_AND_APPEND(WriteSigned)
     537            0 :             QTPOKIT_INTERNAL_TEST_AND_APPEND(ExtendedProperty)
     538            0 :             #undef QTPOKIT_INTERNAL_TEST_AND_APPEND
     539            0 :             qCDebug(lc).noquote() << tr(R"(Characteristic %1 "%2" supports %3.)").arg(characteristic.uuid().toString(),
     540            0 :                 PokitDevice::charcteristicToString(characteristic.uuid()), properties.join(u", "_s));
     541            0 :         }
     542            0 :     }
     543              : 
     544          543 :     if (newState == QLowEnergyService::
     545          132 :             #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
     546          132 :             ServiceDiscovered
     547              :             #else
     548          201 :             RemoteServiceDiscovered
     549          201 :             #endif
     550          333 :         ) {
     551          111 :         Q_Q(AbstractPokitService);
     552          215 :         qCDebug(lc).noquote() << tr("Service details discovered.");
     553          181 :         Q_EMIT q->serviceDetailsDiscovered();
     554          111 :     }
     555          543 : }
     556              : 
     557              : /*!
     558              :  * Handles `QLowEnergyService::characteristicRead` events. This base implementation simply debug
     559              :  * logs the event.
     560              :  *
     561              :  * Derived classes should implement this function to handle the successful reads of
     562              :  * \a characteristic, typically by parsing \a value, then emitting a specialised signal.
     563              :  */
     564          420 : void AbstractPokitServicePrivate::characteristicRead(
     565              :     const QLowEnergyCharacteristic &characteristic, const QByteArray &value)
     566          666 : {
     567         1290 :     qCDebug(lc).noquote() << tr(R"(Characteristic %1 "%2" read %n byte/s: %3)", nullptr, value.size()).arg(
     568            0 :         characteristic.uuid().toString(), PokitDevice::charcteristicToString(characteristic.uuid()), toHexString(value));
     569         1086 : }
     570              : 
     571              : /*!
     572              :  * Handles `QLowEnergyService::characteristicWritten` events. This base implementation simply debug
     573              :  * logs the event.
     574              :  *
     575              :  * Derived classes should implement this function to handle the successful writes of
     576              :  * \a characteristic, typically by parsing \a newValue, then emitting a specialised signal.
     577              :  */
     578          420 : void AbstractPokitServicePrivate::characteristicWritten(
     579              :     const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue)
     580          666 : {
     581         1290 :     qCDebug(lc).noquote() << tr(R"(Characteristic %1 "%2" written with %Ln byte/s: %3)", nullptr, newValue.size())
     582            0 :         .arg(characteristic.uuid().toString(), PokitDevice::charcteristicToString(characteristic.uuid()), toHexString(newValue));
     583         1086 : }
     584              : 
     585              : /*!
     586              :  * Handles `QLowEnergyService::characteristicChanged` events. This base implementation simply debug
     587              :  * logs the event.
     588              :  *
     589              :  * If derived classes support characteristics with client-side notification (ie Notify, as opposed
     590              :  * to Read or Write operations), they should implement this function to handle the successful reads of
     591              :  * \a characteristic, typically by parsing \a value, then emitting a specialised signal.
     592              :  */
     593          280 : void AbstractPokitServicePrivate::characteristicChanged(
     594              :     const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue)
     595          444 : {
     596          860 :     qCDebug(lc).noquote() << tr(R"(Characteristic %1 "%2" changed to %Ln byte/s: %3)", nullptr, newValue.size())
     597            0 :         .arg(characteristic.uuid().toString(), PokitDevice::charcteristicToString(characteristic.uuid()), toHexString(newValue));
     598          724 : }
     599              : 
     600              : /// \endcond
     601              : 
     602              : QTPOKIT_END_NAMESPACE
        

Generated by: LCOV version 2.4-0