LCOV - code coverage report
Current view: top level - src/lib - pokitproducts.cpp (source / functions) Coverage Total Hit
Project: Dokit Lines: 100.0 % 134 134
Version: Functions: 100.0 % 16 16

            Line data    Source code
       1              : // SPDX-FileCopyrightText: 2022-2025 Paul Colby <git@colby.id.au>
       2              : // SPDX-License-Identifier: LGPL-3.0-or-later
       3              : 
       4              : /*!
       5              :  * \file
       6              :  * Defines the #PokitProduct helper functions.
       7              :  */
       8              : 
       9              : #include <qtpokit/pokitmeter.h>
      10              : #include <qtpokit/pokitpro.h>
      11              : #include <qtpokit/pokitproducts.h>
      12              : #include <qtpokit/statusservice.h>
      13              : 
      14              : #include "pokitproducts_p.h"
      15              : 
      16              : #include <QCoreApplication>
      17              : #include <QLoggingCategory>
      18              : 
      19         1806 : static Q_LOGGING_CATEGORY(lc, "dokit.pokit.products", QtInfoMsg); ///< Logging category for this file.
      20              : 
      21              : QTPOKIT_BEGIN_NAMESPACE
      22              : 
      23              : namespace {
      24              :     class Private
      25              :     {
      26          945 :         Q_DECLARE_TR_FUNCTIONS(PokitProducts)
      27              :     };
      28              : }
      29              : 
      30              : /*!
      31              :  * Returns \c product as user-friendly string.
      32              :  */
      33          135 : QString toString(const PokitProduct product)
      34          156 : {
      35          291 :     switch (product) {
      36           97 :     case PokitProduct::PokitMeter: return QStringLiteral("Pokit Meter");
      37           97 :     case PokitProduct::PokitPro:   return QStringLiteral("Pokit Pro");
      38          156 :     }
      39          176 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
      40           52 :     return QString();
      41          156 : }
      42              : 
      43              : /*!
      44              :  * Returns \c true if \a info describes a Pokit device.
      45              :  *
      46              :  * Currently, this is based on whether or not \a info's service UUIDs includes a known Pokit
      47              :  * Status service, but this test criteria might be swapped for something else sometime.
      48              :  */
      49         1269 : bool isPokitProduct(const QBluetoothDeviceInfo &info)
      50         1454 : {
      51         3100 :     return isPokitProduct(info.serviceUuids());
      52         1454 : }
      53              : 
      54              : /*!
      55              :  * Returns the #PokitProduct corresponding the Bluetotoh device \a info.
      56              :  *
      57              :  * If \a info is not a Pokit device, then result is undefined.
      58              :  *
      59              :  * \see isPokitProduct
      60              :  */
      61          225 : PokitProduct pokitProduct(const QBluetoothDeviceInfo &info)
      62          260 : {
      63          590 :     return pokitProduct(info.serviceUuids());
      64          260 : }
      65              : 
      66              : /// \cond internal
      67              : 
      68              : /*!
      69              :  * Returns \c true if \a serviceUuids contains a known Pokit Status service UUID.
      70              :  *
      71              :  * Currently, this is the only known way to detect a Pokit device.
      72              :  */
      73         2079 : bool isPokitProduct(const QList<QBluetoothUuid> &serviceUuids)
      74         2210 : {
      75         5201 :     return (serviceUuids.contains(StatusService::ServiceUuids::pokitMeter) ||
      76         4289 :             serviceUuids.contains(StatusService::ServiceUuids::pokitPro));
      77         2210 : }
      78              : 
      79              : 
      80              : /*!
      81              :  * Returns \c true if \a controller describes a Pokit device.
      82              :  *
      83              :  * Currently, this is based on whether or not \a controller's service UUIDs includes a known Pokit
      84              :  * Status service, but this test criteria might be swapped for something else sometime.
      85              :  *
      86              :  * \see isPokitProduct
      87              :  */
      88          270 : bool isPokitProduct(const QLowEnergyController &controller)
      89          132 : {
      90          480 :     return isPokitProduct(controller.services());
      91          132 : }
      92              : 
      93              : /*!
      94              :  * Returns the #PokitProduct corresponding to the Bluetooth \a serviceUuids.
      95              :  *
      96              :  * Currently, this is based on whether or not \a servceUuids includes a known Pokit
      97              :  * Status service, but this test criteria might be swapped for something else sometime.
      98              :  *
      99              :  * \see isPokitProduct
     100              :  */
     101          675 : PokitProduct pokitProduct(const QList<QBluetoothUuid> &serviceUuids)
     102          630 : {
     103         1305 :     if (serviceUuids.contains(StatusService::ServiceUuids::pokitMeter)) {
     104          208 :         return PokitProduct::PokitMeter;
     105          917 :     } else if (serviceUuids.contains(StatusService::ServiceUuids::pokitPro)) {
     106          208 :         return PokitProduct::PokitPro;
     107          274 :     } else {
     108         1082 :         qCWarning(lc).noquote() << Private::tr("Device is not a Pokit product");
     109          620 :         qCDebug(lc).noquote() << "Service UUIDs:" << serviceUuids;
     110          438 :         return PokitProduct::PokitMeter; // Need to fallback to something; Pokit Meter is just the lowest product.
     111          214 :     }
     112          630 : }
     113              : 
     114              : /*!
     115              :  * Returns the #PokitProduct corresponding to the Bluetooth \a controller.
     116              :  *
     117              :  * Currently, this is based on whether or not \a controller's service UUIDs includes a known Pokit
     118              :  * Status service, but this test criteria might be swapped for something else sometime.
     119              :  *
     120              :  * \see isPokitProduct
     121              :  */
     122          225 : PokitProduct pokitProduct(const QLowEnergyController &controller)
     123          110 : {
     124          440 :     return pokitProduct(controller.services());
     125          110 : }
     126              : 
     127              : QTPOKIT_END_NAMESPACE
     128              : 
     129              : /// Encapsulates convenience functions for working with capacitance ranges.
     130              : namespace CapacitanceRange {
     131              : 
     132              : /*!
     133              :  * Returns \a product's capacitance \a range as a human-friendly string.
     134              :  *
     135              :  * \note Since Pokit Meters do not support capacitance measurement, \a product should not be PokitProduct::PokitMeter.
     136              :  *
     137              :  * \see PokitPro::toString(const PokitPro::CapacitanceRange &range)
     138              :  */
     139          495 : QString toString(const PokitProduct product, const quint8 range)
     140          392 : {
     141          887 :     switch (product) {
     142          298 :     case PokitProduct::PokitMeter:
     143          614 :         qCWarning(lc).noquote() << Private::tr("Pokit Meter has no capacitance support");
     144          118 :         return QString();
     145          492 :     case PokitProduct::PokitPro:
     146          492 :         return PokitPro::toString(static_cast<PokitPro::CapacitanceRange>(range));
     147          392 :     }
     148          176 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     149           52 :     return QString();
     150          392 : }
     151              : 
     152              : /*!
     153              :  * Returns the maximum value for \a range in nanofarads, or 0 if \a range is not a known value for \a product.
     154              :  *
     155              :  * \note Since Pokit Meters do not support capacitance measurement, \a product should not be PokitProduct::PokitMeter.
     156              :  *
     157              :  * \see PokitPro::maxValue(const PokitPro::CapacitanceRange &range)
     158              :  */
     159          225 : quint32 maxValue(const PokitProduct product, const quint8 range)
     160          260 : {
     161          485 :     switch (product) {
     162           97 :     case PokitProduct::PokitMeter:
     163          176 :         qCWarning(lc).noquote() << Private::tr("Pokit Meter has no capacitance support");
     164           58 :         return 0;
     165          291 :     case PokitProduct::PokitPro:
     166          291 :         return PokitPro::maxValue(static_cast<PokitPro::CapacitanceRange>(range));
     167          260 :     }
     168          176 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     169           58 :     return 0;
     170          260 : }
     171              : 
     172              : }
     173              : 
     174              : /// Encapsulates convenience functions for working with current ranges.
     175              : namespace CurrentRange {
     176              : 
     177              : /*!
     178              :  * Returns \a product's current \a range as a human-friendly string.
     179              :  *
     180              :  * \see PokitMeter::toString(const PokitMeter::CurrentRange &range)
     181              :  * \see PokitPro::toString(const PokitPro::CurrentRange &range)
     182              :  */
     183         4455 : QString toString(const PokitProduct product, const quint8 range)
     184         2628 : {
     185         7083 :     switch (product) {
     186         6307 :     case PokitProduct::PokitMeter:
     187         6307 :         return PokitMeter::toString(static_cast<PokitMeter::CurrentRange>(range));
     188          679 :     case PokitProduct::PokitPro:
     189          679 :         return PokitPro::toString(static_cast<PokitPro::CurrentRange>(range));
     190         2628 :     }
     191          176 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     192           52 :     return QString();
     193         2628 : }
     194              : 
     195              : /*!
     196              :  * Returns the maximum value for \a range in microamps, or 0 if \a range is not a known value for \a product.
     197              :  *
     198              :  * \see PokitMeter::maxValue(const PokitMeter::CurrentRange &range)
     199              :  * \see PokitPro::maxValue(const PokitPro::CurrentRange &range)
     200              :  */
     201          675 : quint32 maxValue(const PokitProduct product, const quint8 range)
     202          780 : {
     203         1455 :     switch (product) {
     204          679 :     case PokitProduct::PokitMeter:
     205          679 :         return PokitMeter::maxValue(static_cast<PokitMeter::CurrentRange>(range));
     206          679 :     case PokitProduct::PokitPro:
     207          679 :         return PokitPro::maxValue(static_cast<PokitPro::CurrentRange>(range));
     208          780 :     }
     209          176 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     210           58 :     return 0;
     211          780 : }
     212              : 
     213              : }
     214              : 
     215              : /// Encapsulates convenience functions for working with resistance ranges.
     216              : namespace ResistanceRange {
     217              : 
     218              : /*!
     219              :  * Returns \a product's current \a range as a human-friendly string.
     220              :  *
     221              :  * \see PokitMeter::toString(const PokitMeter::ResistanceRange &range)
     222              :  * \see PokitPro::toString(const PokitPro::ResistanceRange &range)
     223              :  */
     224          585 : QString toString(const PokitProduct product, const quint8 range)
     225          496 : {
     226         1081 :     switch (product) {
     227          693 :     case PokitProduct::PokitMeter:
     228          693 :         return PokitMeter::toString(static_cast<PokitMeter::ResistanceRange>(range));
     229          291 :     case PokitProduct::PokitPro:
     230          291 :         return PokitPro::toString(static_cast<PokitPro::ResistanceRange>(range));
     231          496 :     }
     232          176 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     233           52 :     return QString();
     234          496 : }
     235              : 
     236              : /*!
     237              :  * Returns the maximum value for \a range in ohms, or 0 if \a range is not a known value for \a product.
     238              :  *
     239              :  * \see PokitMeter::maxValue(const PokitMeter::ResistanceRange &range)
     240              :  * \see PokitPro::maxValue(const PokitPro::ResistanceRange &range)
     241              :  */
     242          315 : quint32 maxValue(const PokitProduct product, const quint8 range)
     243          364 : {
     244          679 :     switch (product) {
     245          291 :     case PokitProduct::PokitMeter:
     246          291 :         return PokitMeter::maxValue(static_cast<PokitMeter::ResistanceRange>(range));
     247          291 :     case PokitProduct::PokitPro:
     248          291 :         return PokitPro::maxValue(static_cast<PokitPro::ResistanceRange>(range));
     249          364 :     }
     250          176 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     251           58 :     return 0;
     252          364 : }
     253              : 
     254              : }
     255              : 
     256              : /// Encapsulates convenience functions for working with voltage ranges.
     257              : namespace VoltageRange {
     258              : 
     259              : /*!
     260              :  * Returns \a product's current \a range as a human-friendly string.
     261              :  *
     262              :  * \see PokitMeter::toString(const PokitMeter::VoltageRange &range)
     263              :  * \see PokitPro::toString(const PokitPro::VoltageRange &range)
     264              :  */
     265         4455 : QString toString(const PokitProduct product, const quint8 range)
     266         2628 : {
     267         7083 :     switch (product) {
     268         6307 :     case PokitProduct::PokitMeter:
     269         6307 :         return PokitMeter::toString(static_cast<PokitMeter::VoltageRange>(range));
     270          679 :     case PokitProduct::PokitPro:
     271          679 :         return PokitPro::toString(static_cast<PokitPro::VoltageRange>(range));
     272         2628 :     }
     273          176 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     274           52 :     return QString();
     275         2628 : }
     276              : 
     277              : /*!
     278              :  * Returns the maximum value for \a range in millivolts, or 0 if \a range is not a known value for \a product.
     279              :  *
     280              :  * \see PokitMeter::maxValue(const PokitMeter::VoltageRange &range)
     281              :  * \see PokitPro::maxValue(const PokitPro::VoltageRange &range)
     282              :  */
     283          675 : quint32 maxValue(const PokitProduct product, const quint8 range)
     284          780 : {
     285         1455 :     switch (product) {
     286          679 :     case PokitProduct::PokitMeter:
     287          679 :         return PokitMeter::maxValue(static_cast<PokitMeter::VoltageRange>(range));
     288          679 :     case PokitProduct::PokitPro:
     289          679 :         return PokitPro::maxValue(static_cast<PokitPro::VoltageRange>(range));
     290          780 :     }
     291          176 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     292           58 :     return 0;
     293          780 : }
     294              : 
     295              : }
     296              : 
     297              : /// \endcond
        

Generated by: LCOV version 2.3-1