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 % 31 31

            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 #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         1589 : 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          798 :         Q_DECLARE_TR_FUNCTIONS(PokitProducts)
      27              :     };
      28              : }
      29              : 
      30              : /*!
      31              :  * Returns \c product as user-friendly string.
      32              :  */
      33          114 : QString toString(const PokitProduct product)
      34          132 : {
      35          246 :     switch (product) {
      36           82 :     case PokitProduct::PokitMeter: return QStringLiteral("Pokit Meter");
      37           82 :     case PokitProduct::PokitPro:   return QStringLiteral("Pokit Pro");
      38          132 :     }
      39          143 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
      40           44 :     return QString();
      41          132 : }
      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         1066 : bool isPokitProduct(const QBluetoothDeviceInfo &info)
      50         1222 : {
      51         2520 :     return isPokitProduct(info.serviceUuids());
      52         1222 : }
      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          190 : PokitProduct pokitProduct(const QBluetoothDeviceInfo &info)
      62          220 : {
      63          485 :     return pokitProduct(info.serviceUuids());
      64          220 : }
      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         1750 : bool isPokitProduct(const QList<QBluetoothUuid> &serviceUuids)
      74         1846 : {
      75         4469 :     return (serviceUuids.contains(StatusService::ServiceUuids::pokitMeter) ||
      76         3596 :             serviceUuids.contains(StatusService::ServiceUuids::pokitPro));
      77         1846 : }
      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          228 : bool isPokitProduct(const QLowEnergyController &controller)
      89           96 : {
      90          372 :     return isPokitProduct(controller.services());
      91           96 : }
      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          570 : PokitProduct pokitProduct(const QList<QBluetoothUuid> &serviceUuids)
     102          520 : {
     103         1090 :     if (serviceUuids.contains(StatusService::ServiceUuids::pokitMeter)) {
     104          176 :         return PokitProduct::PokitMeter;
     105          762 :     } else if (serviceUuids.contains(StatusService::ServiceUuids::pokitPro)) {
     106          176 :         return PokitProduct::PokitPro;
     107          224 :     } else {
     108          861 :         qCWarning(lc).noquote() << Private::tr("Device is not a Pokit product");
     109          490 :         qCDebug(lc).noquote() << "Service UUIDs:" << serviceUuids;
     110          378 :         return PokitProduct::PokitMeter; // Need to fallback to something; Pokit Meter is just the lowest product.
     111          168 :     }
     112          520 : }
     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          190 : PokitProduct pokitProduct(const QLowEnergyController &controller)
     123           80 : {
     124          345 :     return pokitProduct(controller.services());
     125           80 : }
     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          418 : QString toString(const PokitProduct product, const quint8 range)
     140          316 : {
     141          734 :     switch (product) {
     142          244 :     case PokitProduct::PokitMeter:
     143          488 :         qCWarning(lc).noquote() << Private::tr("Pokit Meter has no capacitance support");
     144           92 :         return QString();
     145          408 :     case PokitProduct::PokitPro:
     146          408 :         return PokitPro::toString(static_cast<PokitPro::CapacitanceRange>(range));
     147          316 :     }
     148          143 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     149           44 :     return QString();
     150          316 : }
     151              : 
     152              : /*!
     153              :  *  Returns the maximum value for \a product's \a range in (integer) nanofarads, or the string "Auto".
     154              :  *  If \a range is not a known valid value, then an null QVariant is returned.
     155              :  *
     156              :  *  \note Since Pokit Meters do not support capacitance measurement, \a product should not be PokitProduct::PokitMeter.
     157              :  *
     158              :  *  \see PokitPro::maxValue(const PokitPro::CapacitanceRange &range)
     159              :  */
     160          190 : QVariant maxValue(const PokitProduct product, const quint8 range)
     161          220 : {
     162          410 :     switch (product) {
     163           82 :     case PokitProduct::PokitMeter:
     164          143 :         qCWarning(lc).noquote() << Private::tr("Pokit Meter has no capacitance support");
     165           44 :         return QVariant();
     166          246 :     case PokitProduct::PokitPro:
     167          246 :         return PokitPro::maxValue(static_cast<PokitPro::CapacitanceRange>(range));
     168          220 :     }
     169          143 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     170           44 :     return QVariant();
     171          220 : }
     172              : 
     173              : }
     174              : 
     175              : /// Encapsulates convenience functions for working with current ranges.
     176              : namespace CurrentRange {
     177              : 
     178              : /*!
     179              :  *  Returns \a product's current \a range as a human-friendly string.
     180              :  *
     181              :  *  \see PokitMeter::toString(const PokitMeter::CurrentRange &range)
     182              :  *  \see PokitPro::toString(const PokitPro::CurrentRange &range)
     183              :  */
     184         3762 : QString toString(const PokitProduct product, const quint8 range)
     185         2004 : {
     186         5766 :     switch (product) {
     187         5110 :     case PokitProduct::PokitMeter:
     188         5110 :         return PokitMeter::toString(static_cast<PokitMeter::CurrentRange>(range));
     189          574 :     case PokitProduct::PokitPro:
     190          574 :         return PokitPro::toString(static_cast<PokitPro::CurrentRange>(range));
     191         2004 :     }
     192          143 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     193           44 :     return QString();
     194         2004 : }
     195              : 
     196              : /*!
     197              :  *  Returns the maximum value for \a product's \a range in (integer) microamps, or the string "Auto".
     198              :  *  If \a range is not a known valid value, then an null QVariant is returned.
     199              :  *
     200              :  *  \see PokitMeter::maxValue(const PokitMeter::CurrentRange &range)
     201              :  *  \see PokitPro::maxValue(const PokitPro::CurrentRange &range)
     202              :  */
     203          570 : QVariant maxValue(const PokitProduct product, const quint8 range)
     204          660 : {
     205         1230 :     switch (product) {
     206          574 :     case PokitProduct::PokitMeter:
     207          574 :         return PokitMeter::maxValue(static_cast<PokitMeter::CurrentRange>(range));
     208          574 :     case PokitProduct::PokitPro:
     209          574 :         return PokitPro::maxValue(static_cast<PokitPro::CurrentRange>(range));
     210          660 :     }
     211          143 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     212           44 :     return QVariant();
     213          660 : }
     214              : 
     215              : }
     216              : 
     217              : /// Encapsulates convenience functions for working with resistance ranges.
     218              : namespace ResistanceRange {
     219              : 
     220              : /*!
     221              :  *  Returns \a product's current \a range as a human-friendly string.
     222              :  *
     223              :  *  \see PokitMeter::toString(const PokitMeter::ResistanceRange &range)
     224              :  *  \see PokitPro::toString(const PokitPro::ResistanceRange &range)
     225              :  */
     226          494 : QString toString(const PokitProduct product, const quint8 range)
     227          404 : {
     228          898 :     switch (product) {
     229          570 :     case PokitProduct::PokitMeter:
     230          570 :         return PokitMeter::toString(static_cast<PokitMeter::ResistanceRange>(range));
     231          246 :     case PokitProduct::PokitPro:
     232          246 :         return PokitPro::toString(static_cast<PokitPro::ResistanceRange>(range));
     233          404 :     }
     234          143 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     235           44 :     return QString();
     236          404 : }
     237              : 
     238              : /*!
     239              :  *  Returns the maximum value for \a product's \a range in (integer) ohms, or the string "Auto".
     240              :  *  If \a range is not a known valid value, then an null QVariant is returned.
     241              :  *
     242              :  *  \see PokitMeter::maxValue(const PokitMeter::ResistanceRange &range)
     243              :  *  \see PokitPro::maxValue(const PokitPro::ResistanceRange &range)
     244              :  */
     245          266 : QVariant maxValue(const PokitProduct product, const quint8 range)
     246          308 : {
     247          574 :     switch (product) {
     248          246 :     case PokitProduct::PokitMeter:
     249          246 :         return PokitMeter::maxValue(static_cast<PokitMeter::ResistanceRange>(range));
     250          246 :     case PokitProduct::PokitPro:
     251          246 :         return PokitPro::maxValue(static_cast<PokitPro::ResistanceRange>(range));
     252          308 :     }
     253          143 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     254           44 :     return QVariant();
     255          308 : }
     256              : 
     257              : }
     258              : 
     259              : /// Encapsulates convenience functions for working with voltage ranges.
     260              : namespace VoltageRange {
     261              : 
     262              : /*!
     263              :  *  Returns \a product's current \a range as a human-friendly string.
     264              :  *
     265              :  *  \see PokitMeter::toString(const PokitMeter::VoltageRange &range)
     266              :  *  \see PokitPro::toString(const PokitPro::VoltageRange &range)
     267              :  */
     268         3762 : QString toString(const PokitProduct product, const quint8 range)
     269         2004 : {
     270         5766 :     switch (product) {
     271         5110 :     case PokitProduct::PokitMeter:
     272         5110 :         return PokitMeter::toString(static_cast<PokitMeter::VoltageRange>(range));
     273          574 :     case PokitProduct::PokitPro:
     274          574 :         return PokitPro::toString(static_cast<PokitPro::VoltageRange>(range));
     275         2004 :     }
     276          143 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     277           44 :     return QString();
     278         2004 : }
     279              : 
     280              : /*!
     281              :  *  Returns the maximum value for \a product's \a range in (integer) millivolts, or the string "Auto".
     282              :  *  If \a range is not a known valid value, then an null QVariant is returned.
     283              :  *
     284              :  *  \see PokitMeter::maxValue(const PokitMeter::VoltageRange &range)
     285              :  *  \see PokitPro::maxValue(const PokitPro::VoltageRange &range)
     286              :  */
     287          570 : QVariant maxValue(const PokitProduct product, const quint8 range)
     288          660 : {
     289         1230 :     switch (product) {
     290          574 :     case PokitProduct::PokitMeter:
     291          574 :         return PokitMeter::maxValue(static_cast<PokitMeter::VoltageRange>(range));
     292          574 :     case PokitProduct::PokitPro:
     293          574 :         return PokitPro::maxValue(static_cast<PokitPro::VoltageRange>(range));
     294          660 :     }
     295          143 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     296           44 :     return QVariant();
     297          660 : }
     298              : 
     299              : }
     300              : 
     301              : /// \endcond
        

Generated by: LCOV version 2.2-1