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-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         1645 : 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          840 :         Q_DECLARE_TR_FUNCTIONS(PokitProducts)
      27              :     };
      28              : }
      29              : 
      30              : /*!
      31              :  * Returns \c product as user-friendly string.
      32              :  */
      33          120 : QString toString(const PokitProduct product)
      34          141 : {
      35          261 :     switch (product) {
      36           87 :     case PokitProduct::PokitMeter: return QStringLiteral("Pokit Meter");
      37           87 :     case PokitProduct::PokitPro:   return QStringLiteral("Pokit Pro");
      38          141 :     }
      39          154 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
      40           47 :     return QString();
      41          141 : }
      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         1124 : bool isPokitProduct(const QBluetoothDeviceInfo &info)
      50         1309 : {
      51         2694 :     return isPokitProduct(info.serviceUuids());
      52         1309 : }
      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          200 : PokitProduct pokitProduct(const QBluetoothDeviceInfo &info)
      62          235 : {
      63          520 :     return pokitProduct(info.serviceUuids());
      64          235 : }
      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         1844 : bool isPokitProduct(const QList<QBluetoothUuid> &serviceUuids)
      74         1975 : {
      75         4692 :     return (serviceUuids.contains(StatusService::ServiceUuids::pokitMeter) ||
      76         3819 :             serviceUuids.contains(StatusService::ServiceUuids::pokitPro));
      77         1975 : }
      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          240 : bool isPokitProduct(const QLowEnergyController &controller)
      89          102 : {
      90          396 :     return isPokitProduct(controller.services());
      91          102 : }
      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          600 : PokitProduct pokitProduct(const QList<QBluetoothUuid> &serviceUuids)
     102          555 : {
     103         1155 :     if (serviceUuids.contains(StatusService::ServiceUuids::pokitMeter)) {
     104          188 :         return PokitProduct::PokitMeter;
     105          807 :     } else if (serviceUuids.contains(StatusService::ServiceUuids::pokitPro)) {
     106          188 :         return PokitProduct::PokitPro;
     107          239 :     } else {
     108          928 :         qCWarning(lc).noquote() << Private::tr("Device is not a Pokit product");
     109          529 :         qCDebug(lc).noquote() << "Service UUIDs:" << serviceUuids;
     110          389 :         return PokitProduct::PokitMeter; // Need to fallback to something; Pokit Meter is just the lowest product.
     111          179 :     }
     112          555 : }
     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          200 : PokitProduct pokitProduct(const QLowEnergyController &controller)
     123           85 : {
     124          370 :     return pokitProduct(controller.services());
     125           85 : }
     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          440 : QString toString(const PokitProduct product, const quint8 range)
     140          337 : {
     141          777 :     switch (product) {
     142          258 :     case PokitProduct::PokitMeter:
     143          526 :         qCWarning(lc).noquote() << Private::tr("Pokit Meter has no capacitance support");
     144           98 :         return QString();
     145          432 :     case PokitProduct::PokitPro:
     146          432 :         return PokitPro::toString(static_cast<PokitPro::CapacitanceRange>(range));
     147          337 :     }
     148          154 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     149           47 :     return QString();
     150          337 : }
     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          200 : quint32 maxValue(const PokitProduct product, const quint8 range)
     160          235 : {
     161          435 :     switch (product) {
     162           87 :     case PokitProduct::PokitMeter:
     163          154 :         qCWarning(lc).noquote() << Private::tr("Pokit Meter has no capacitance support");
     164           52 :         return 0;
     165          261 :     case PokitProduct::PokitPro:
     166          261 :         return PokitPro::maxValue(static_cast<PokitPro::CapacitanceRange>(range));
     167          235 :     }
     168          154 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     169           52 :     return 0;
     170          235 : }
     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         3960 : QString toString(const PokitProduct product, const quint8 range)
     184         2133 : {
     185         6093 :     switch (product) {
     186         5397 :     case PokitProduct::PokitMeter:
     187         5397 :         return PokitMeter::toString(static_cast<PokitMeter::CurrentRange>(range));
     188          609 :     case PokitProduct::PokitPro:
     189          609 :         return PokitPro::toString(static_cast<PokitPro::CurrentRange>(range));
     190         2133 :     }
     191          154 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     192           47 :     return QString();
     193         2133 : }
     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          600 : quint32 maxValue(const PokitProduct product, const quint8 range)
     202          705 : {
     203         1305 :     switch (product) {
     204          609 :     case PokitProduct::PokitMeter:
     205          609 :         return PokitMeter::maxValue(static_cast<PokitMeter::CurrentRange>(range));
     206          609 :     case PokitProduct::PokitPro:
     207          609 :         return PokitPro::maxValue(static_cast<PokitPro::CurrentRange>(range));
     208          705 :     }
     209          154 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     210           52 :     return 0;
     211          705 : }
     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          520 : QString toString(const PokitProduct product, const quint8 range)
     225          431 : {
     226          951 :     switch (product) {
     227          603 :     case PokitProduct::PokitMeter:
     228          603 :         return PokitMeter::toString(static_cast<PokitMeter::ResistanceRange>(range));
     229          261 :     case PokitProduct::PokitPro:
     230          261 :         return PokitPro::toString(static_cast<PokitPro::ResistanceRange>(range));
     231          431 :     }
     232          154 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     233           47 :     return QString();
     234          431 : }
     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          280 : quint32 maxValue(const PokitProduct product, const quint8 range)
     243          329 : {
     244          609 :     switch (product) {
     245          261 :     case PokitProduct::PokitMeter:
     246          261 :         return PokitMeter::maxValue(static_cast<PokitMeter::ResistanceRange>(range));
     247          261 :     case PokitProduct::PokitPro:
     248          261 :         return PokitPro::maxValue(static_cast<PokitPro::ResistanceRange>(range));
     249          329 :     }
     250          154 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     251           52 :     return 0;
     252          329 : }
     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         3960 : QString toString(const PokitProduct product, const quint8 range)
     266         2133 : {
     267         6093 :     switch (product) {
     268         5397 :     case PokitProduct::PokitMeter:
     269         5397 :         return PokitMeter::toString(static_cast<PokitMeter::VoltageRange>(range));
     270          609 :     case PokitProduct::PokitPro:
     271          609 :         return PokitPro::toString(static_cast<PokitPro::VoltageRange>(range));
     272         2133 :     }
     273          154 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     274           47 :     return QString();
     275         2133 : }
     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          600 : quint32 maxValue(const PokitProduct product, const quint8 range)
     284          705 : {
     285         1305 :     switch (product) {
     286          609 :     case PokitProduct::PokitMeter:
     287          609 :         return PokitMeter::maxValue(static_cast<PokitMeter::VoltageRange>(range));
     288          609 :     case PokitProduct::PokitPro:
     289          609 :         return PokitPro::maxValue(static_cast<PokitPro::VoltageRange>(range));
     290          705 :     }
     291          154 :     qCWarning(lc).noquote() << Private::tr("Unknown PokitProduct value: %1").arg((int)product);
     292           52 :     return 0;
     293          705 : }
     294              : 
     295              : }
     296              : 
     297              : /// \endcond
        

Generated by: LCOV version 2.2-1