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

            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              : /*!
      24              :  * Returns \c product as user-friendly string.
      25              :  */
      26          114 : QString toString(const PokitProduct product)
      27          132 : {
      28          246 :     switch (product) {
      29           82 :     case PokitProduct::PokitMeter: return QStringLiteral("Pokit Meter");
      30           82 :     case PokitProduct::PokitPro:   return QStringLiteral("Pokit Pro");
      31          132 :     }
      32          174 :     qCWarning(lc).noquote() << QCoreApplication::translate("PokitProducts",
      33          120 :         "Unknown PokitProduct value: %1", "toString").arg((int)product);
      34           44 :     return QString();
      35          132 : }
      36              : 
      37              : /*!
      38              :  * Returns \c true if \a info describes a Pokit device.
      39              :  *
      40              :  * Currently, this is based on whether or not \a info's service UUIDs includes a known Pokit
      41              :  * Status service, but this test criteria might be swapped for something else sometime.
      42              :  */
      43         1066 : bool isPokitProduct(const QBluetoothDeviceInfo &info)
      44         1222 : {
      45         2520 :     return isPokitProduct(info.serviceUuids());
      46         1222 : }
      47              : 
      48              : /*!
      49              :  * Returns the #PokitProduct corresponding the Bluetotoh device \a info.
      50              :  *
      51              :  * If \a info is not a Pokit device, then result is undefined.
      52              :  *
      53              :  * \see isPokitProduct
      54              :  */
      55          190 : PokitProduct pokitProduct(const QBluetoothDeviceInfo &info)
      56          220 : {
      57          485 :     return pokitProduct(info.serviceUuids());
      58          220 : }
      59              : 
      60              : /// \cond internal
      61              : 
      62              : /*!
      63              :  * Returns \c true if \a serviceUuids contains a known Pokit Status service UUID.
      64              :  *
      65              :  * Currently, this is the only known way to detect a Pokit device.
      66              :  */
      67         1750 : bool isPokitProduct(const QList<QBluetoothUuid> &serviceUuids)
      68         1846 : {
      69         4469 :     return (serviceUuids.contains(StatusService::ServiceUuids::pokitMeter) ||
      70         3596 :             serviceUuids.contains(StatusService::ServiceUuids::pokitPro));
      71         1846 : }
      72              : 
      73              : 
      74              : /*!
      75              :  * Returns \c true if \a controller describes a Pokit device.
      76              :  *
      77              :  * Currently, this is based on whether or not \a controller's service UUIDs includes a known Pokit
      78              :  * Status service, but this test criteria might be swapped for something else sometime.
      79              :  *
      80              :  * \see isPokitProduct
      81              :  */
      82          228 : bool isPokitProduct(const QLowEnergyController &controller)
      83           96 : {
      84          372 :     return isPokitProduct(controller.services());
      85           96 : }
      86              : 
      87              : /*!
      88              :  * Returns the #PokitProduct corresponding to the Bluetooth \a serviceUuids.
      89              :  *
      90              :  * Currently, this is based on whether or not \a servceUuids includes a known Pokit
      91              :  * Status service, but this test criteria might be swapped for something else sometime.
      92              :  *
      93              :  * \see isPokitProduct
      94              :  */
      95          570 : PokitProduct pokitProduct(const QList<QBluetoothUuid> &serviceUuids)
      96          520 : {
      97         1090 :     if (serviceUuids.contains(StatusService::ServiceUuids::pokitMeter)) {
      98          176 :         return PokitProduct::PokitMeter;
      99          762 :     } else if (serviceUuids.contains(StatusService::ServiceUuids::pokitPro)) {
     100          176 :         return PokitProduct::PokitPro;
     101          224 :     } else {
     102          756 :         qCWarning(lc).noquote()
     103          539 :             << QCoreApplication::translate("PokitProducts", "Device is not a Pokit product", "pokitProduct");
     104          490 :         qCDebug(lc).noquote() << "Service UUIDs:" << serviceUuids;
     105          378 :         return PokitProduct::PokitMeter; // Need to fallback to something; Pokit Meter is just the lowest product.
     106          168 :     }
     107          520 : }
     108              : 
     109              : /*!
     110              :  * Returns the #PokitProduct corresponding to the Bluetooth \a controller.
     111              :  *
     112              :  * Currently, this is based on whether or not \a controller's service UUIDs includes a known Pokit
     113              :  * Status service, but this test criteria might be swapped for something else sometime.
     114              :  *
     115              :  * \see isPokitProduct
     116              :  */
     117          190 : PokitProduct pokitProduct(const QLowEnergyController &controller)
     118           80 : {
     119          345 :     return pokitProduct(controller.services());
     120           80 : }
     121              : 
     122              : QTPOKIT_END_NAMESPACE
     123              : 
     124              : /// Encapsulates convenience functions for working with capacitance ranges.
     125              : namespace CapacitanceRange {
     126              : 
     127              : /*!
     128              :  *  Returns \a product's capacitance \a range as a human-friendly string.
     129              :  *
     130              :  *  \note Since Pokit Meters do not support capacitance measurement, \a product should not be PokitProduct::PokitMeter.
     131              :  *
     132              :  *  \see PokitPro::toString(const PokitPro::CapacitanceRange &range)
     133              :  */
     134          418 : QString toString(const PokitProduct product, const quint8 range)
     135          316 : {
     136          734 :     switch (product) {
     137          244 :     case PokitProduct::PokitMeter:
     138          428 :         qCWarning(lc).noquote()
     139          304 :             << QCoreApplication::translate("PokitProducts", "Pokit Meter has no capacitance support", "toString");
     140           92 :         return QString();
     141          408 :     case PokitProduct::PokitPro:
     142          408 :         return PokitPro::toString(static_cast<PokitPro::CapacitanceRange>(range));
     143          316 :     }
     144          174 :     qCWarning(lc).noquote() << QCoreApplication::translate("CapacitanceRange",
     145          120 :         "Unknown PokitProduct value: %1", "toString").arg((int)product);
     146           44 :     return QString();
     147          316 : }
     148              : 
     149              : /*!
     150              :  *  Returns the maximum value for \a product's \a range in (integer) nanofarads, or the string "Auto".
     151              :  *  If \a range is not a known valid value, then an null QVariant is returned.
     152              :  *
     153              :  *  \note Since Pokit Meters do not support capacitance measurement, \a product should not be PokitProduct::PokitMeter.
     154              :  *
     155              :  *  \see PokitPro::maxValue(const PokitPro::CapacitanceRange &range)
     156              :  */
     157          190 : QVariant maxValue(const PokitProduct product, const quint8 range)
     158          220 : {
     159          410 :     switch (product) {
     160           82 :     case PokitProduct::PokitMeter:
     161          128 :         qCWarning(lc).noquote()
     162           97 :             << QCoreApplication::translate("PokitProducts", "Pokit Meter has no capacitance support", "toString");
     163           44 :         return QVariant();
     164          246 :     case PokitProduct::PokitPro:
     165          246 :         return PokitPro::maxValue(static_cast<PokitPro::CapacitanceRange>(range));
     166          220 :     }
     167          174 :     qCWarning(lc).noquote() << QCoreApplication::translate("CapacitanceRange",
     168          120 :         "Unknown PokitProduct value: %1", "maxValue").arg((int)product);
     169           44 :     return QVariant();
     170          220 : }
     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         3762 : QString toString(const PokitProduct product, const quint8 range)
     184         2004 : {
     185         5766 :     switch (product) {
     186         5110 :     case PokitProduct::PokitMeter:
     187         5110 :         return PokitMeter::toString(static_cast<PokitMeter::CurrentRange>(range));
     188          574 :     case PokitProduct::PokitPro:
     189          574 :         return PokitPro::toString(static_cast<PokitPro::CurrentRange>(range));
     190         2004 :     }
     191          174 :     qCWarning(lc).noquote() << QCoreApplication::translate("CurrentRange",
     192          120 :         "Unknown PokitProduct value: %1", "toString").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          174 :     qCWarning(lc).noquote() << QCoreApplication::translate("CurrentRange",
     212          120 :         "Unknown PokitProduct value: %1", "maxValue").arg((int)product);
     213           44 :     return QVariant();
     214          660 : }
     215              : 
     216              : }
     217              : 
     218              : /// Encapsulates convenience functions for working with resistance ranges.
     219              : namespace ResistanceRange {
     220              : 
     221              : /*!
     222              :  *  Returns \a product's current \a range as a human-friendly string.
     223              :  *
     224              :  *  \see PokitMeter::toString(const PokitMeter::ResistanceRange &range)
     225              :  *  \see PokitPro::toString(const PokitPro::ResistanceRange &range)
     226              :  */
     227          494 : QString toString(const PokitProduct product, const quint8 range)
     228          404 : {
     229          898 :     switch (product) {
     230          570 :     case PokitProduct::PokitMeter:
     231          570 :         return PokitMeter::toString(static_cast<PokitMeter::ResistanceRange>(range));
     232          246 :     case PokitProduct::PokitPro:
     233          246 :         return PokitPro::toString(static_cast<PokitPro::ResistanceRange>(range));
     234          404 :     }
     235          174 :     qCWarning(lc).noquote() << QCoreApplication::translate("ResistanceRange",
     236          120 :         "Unknown PokitProduct value: %1", "toString").arg((int)product);
     237           44 :     return QString();
     238          404 : }
     239              : 
     240              : /*!
     241              :  *  Returns the maximum value for \a product's \a range in (integer) ohms, or the string "Auto".
     242              :  *  If \a range is not a known valid value, then an null QVariant is returned.
     243              :  *
     244              :  *  \see PokitMeter::maxValue(const PokitMeter::ResistanceRange &range)
     245              :  *  \see PokitPro::maxValue(const PokitPro::ResistanceRange &range)
     246              :  */
     247          266 : QVariant maxValue(const PokitProduct product, const quint8 range)
     248          308 : {
     249          574 :     switch (product) {
     250          246 :     case PokitProduct::PokitMeter:
     251          246 :         return PokitMeter::maxValue(static_cast<PokitMeter::ResistanceRange>(range));
     252          246 :     case PokitProduct::PokitPro:
     253          246 :         return PokitPro::maxValue(static_cast<PokitPro::ResistanceRange>(range));
     254          308 :     }
     255          174 :     qCWarning(lc).noquote() << QCoreApplication::translate("ResistanceRange",
     256          120 :         "Unknown PokitProduct value: %1", "maxValue").arg((int)product);
     257           44 :     return QVariant();
     258          308 : }
     259              : 
     260              : }
     261              : 
     262              : /// Encapsulates convenience functions for working with voltage ranges.
     263              : namespace VoltageRange {
     264              : 
     265              : /*!
     266              :  *  Returns \a product's current \a range as a human-friendly string.
     267              :  *
     268              :  *  \see PokitMeter::toString(const PokitMeter::VoltageRange &range)
     269              :  *  \see PokitPro::toString(const PokitPro::VoltageRange &range)
     270              :  */
     271         3762 : QString toString(const PokitProduct product, const quint8 range)
     272         2004 : {
     273         5766 :     switch (product) {
     274         5110 :     case PokitProduct::PokitMeter:
     275         5110 :         return PokitMeter::toString(static_cast<PokitMeter::VoltageRange>(range));
     276          574 :     case PokitProduct::PokitPro:
     277          574 :         return PokitPro::toString(static_cast<PokitPro::VoltageRange>(range));
     278         2004 :     }
     279          174 :     qCWarning(lc).noquote() << QCoreApplication::translate("VoltageRange",
     280          120 :         "Unknown PokitProduct value: %1", "toString").arg((int)product);
     281           44 :     return QString();
     282         2004 : }
     283              : 
     284              : /*!
     285              :  *  Returns the maximum value for \a product's \a range in (integer) millivolts, or the string "Auto".
     286              :  *  If \a range is not a known valid value, then an null QVariant is returned.
     287              :  *
     288              :  *  \see PokitMeter::maxValue(const PokitMeter::VoltageRange &range)
     289              :  *  \see PokitPro::maxValue(const PokitPro::VoltageRange &range)
     290              :  */
     291          570 : QVariant maxValue(const PokitProduct product, const quint8 range)
     292          660 : {
     293         1230 :     switch (product) {
     294          574 :     case PokitProduct::PokitMeter:
     295          574 :         return PokitMeter::maxValue(static_cast<PokitMeter::VoltageRange>(range));
     296          574 :     case PokitProduct::PokitPro:
     297          574 :         return PokitPro::maxValue(static_cast<PokitPro::VoltageRange>(range));
     298          660 :     }
     299          174 :     qCWarning(lc).noquote() << QCoreApplication::translate("VoltageRange",
     300          120 :         "Unknown PokitProduct value: %1", "maxValue").arg((int)product);
     301           44 :     return QVariant();
     302          660 : }
     303              : 
     304              : }
     305              : 
     306              : /// \endcond
        

Generated by: LCOV version 2.0-1