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

          Line data    Source code
       1             : // SPDX-FileCopyrightText: 2022-2023 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         819 : 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          54 : QString toString(const PokitProduct product)
      27             : {
      28          54 :     switch (product) {
      29          18 :     case PokitProduct::PokitMeter: return QStringLiteral("Pokit Meter");
      30          18 :     case PokitProduct::PokitPro:   return QStringLiteral("Pokit Pro");
      31             :     }
      32          56 :     qCWarning(lc).noquote() << QCoreApplication::translate("PokitProducts",
      33          28 :         "Unknown PokitProduct value: %1", "toString").arg((int)product);
      34             :     return QString();
      35             : }
      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         492 : bool isPokitProduct(const QBluetoothDeviceInfo &info)
      44             : {
      45         608 :     return isPokitProduct(info.serviceUuids());
      46             : }
      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          90 : PokitProduct pokitProduct(const QBluetoothDeviceInfo &info)
      56             : {
      57         110 :     return pokitProduct(info.serviceUuids());
      58             : }
      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         816 : bool isPokitProduct(const QList<QBluetoothUuid> &serviceUuids)
      68             : {
      69        1342 :     return (serviceUuids.contains(StatusService::ServiceUuids::pokitMeter) ||
      70         816 :             serviceUuids.contains(StatusService::ServiceUuids::pokitPro));
      71             : }
      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         108 : bool isPokitProduct(const QLowEnergyController &controller)
      83             : {
      84         132 :     return isPokitProduct(controller.services());
      85             : }
      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         270 : PokitProduct pokitProduct(const QList<QBluetoothUuid> &serviceUuids)
      96             : {
      97         270 :     if (serviceUuids.contains(StatusService::ServiceUuids::pokitMeter)) {
      98             :         return PokitProduct::PokitMeter;
      99         198 :     } else if (serviceUuids.contains(StatusService::ServiceUuids::pokitPro)) {
     100             :         return PokitProduct::PokitPro;
     101             :     } else {
     102         252 :         qCWarning(lc).noquote()
     103         154 :             << QCoreApplication::translate("PokitProducts", "Device is not a Pokit product", "pokitProduct");
     104         126 :         qCDebug(lc).noquote() << "Service UUIDs:" << serviceUuids;
     105         126 :         return PokitProduct::PokitMeter; // Need to fallback to something; Pokit Meter is just the lowest product.
     106             :     }
     107             : }
     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          90 : PokitProduct pokitProduct(const QLowEnergyController &controller)
     118             : {
     119         110 :     return pokitProduct(controller.services());
     120             : }
     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         198 : QString toString(const PokitProduct product, const quint8 range)
     135             : {
     136         198 :     switch (product) {
     137          72 :     case PokitProduct::PokitMeter:
     138         144 :         qCWarning(lc).noquote()
     139          88 :             << QCoreApplication::translate("PokitProducts", "Pokit Meter has no capacitance support", "toString");
     140             :         return QString();
     141         108 :     case PokitProduct::PokitPro:
     142         108 :         return PokitPro::toString(static_cast<PokitPro::CapacitanceRange>(range));
     143             :     }
     144          56 :     qCWarning(lc).noquote() << QCoreApplication::translate("CapacitanceRange",
     145          28 :         "Unknown PokitProduct value: %1", "toString").arg((int)product);
     146             :     return QString();
     147             : }
     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          90 : QVariant maxValue(const PokitProduct product, const quint8 range)
     158             : {
     159          90 :     switch (product) {
     160          18 :     case PokitProduct::PokitMeter:
     161          36 :         qCWarning(lc).noquote()
     162          22 :             << QCoreApplication::translate("PokitProducts", "Pokit Meter has no capacitance support", "toString");
     163             :         return QVariant();
     164          54 :     case PokitProduct::PokitPro:
     165          54 :         return PokitPro::maxValue(static_cast<PokitPro::CapacitanceRange>(range));
     166             :     }
     167          56 :     qCWarning(lc).noquote() << QCoreApplication::translate("CapacitanceRange",
     168          28 :         "Unknown PokitProduct value: %1", "maxValue").arg((int)product);
     169             :     return QVariant();
     170             : }
     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        1782 : QString toString(const PokitProduct product, const quint8 range) {
     184        1782 :     switch (product) {
     185        1638 :     case PokitProduct::PokitMeter:
     186        1638 :         return PokitMeter::toString(static_cast<PokitMeter::CurrentRange>(range));
     187         126 :     case PokitProduct::PokitPro:
     188         126 :         return PokitPro::toString(static_cast<PokitPro::CurrentRange>(range));
     189             :     }
     190          56 :     qCWarning(lc).noquote() << QCoreApplication::translate("CurrentRange",
     191          28 :         "Unknown PokitProduct value: %1", "toString").arg((int)product);
     192             :     return QString();
     193             : }
     194             : 
     195             : /*!
     196             :  *  Returns the maximum value for \a product's \a range in (integer) microamps, or the string "Auto".
     197             :  *  If \a range is not a known valid value, then an null QVariant is returned.
     198             :  *
     199             :  *  \see PokitMeter::maxValue(const PokitMeter::CurrentRange &range)
     200             :  *  \see PokitPro::maxValue(const PokitPro::CurrentRange &range)
     201             :  */
     202         270 : QVariant maxValue(const PokitProduct product, const quint8 range)
     203             : {
     204         270 :     switch (product) {
     205         126 :     case PokitProduct::PokitMeter:
     206         126 :         return PokitMeter::maxValue(static_cast<PokitMeter::CurrentRange>(range));
     207         126 :     case PokitProduct::PokitPro:
     208         126 :         return PokitPro::maxValue(static_cast<PokitPro::CurrentRange>(range));
     209             :     }
     210          56 :     qCWarning(lc).noquote() << QCoreApplication::translate("CurrentRange",
     211          28 :         "Unknown PokitProduct value: %1", "maxValue").arg((int)product);
     212             :     return QVariant();
     213             : }
     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         234 : QString toString(const PokitProduct product, const quint8 range)
     227             : {
     228         234 :     switch (product) {
     229         162 :     case PokitProduct::PokitMeter:
     230         162 :         return PokitMeter::toString(static_cast<PokitMeter::ResistanceRange>(range));
     231          54 :     case PokitProduct::PokitPro:
     232          54 :         return PokitPro::toString(static_cast<PokitPro::ResistanceRange>(range));
     233             :     }
     234          56 :     qCWarning(lc).noquote() << QCoreApplication::translate("ResistanceRange",
     235          28 :         "Unknown PokitProduct value: %1", "toString").arg((int)product);
     236             :     return QString();
     237             : }
     238             : 
     239             : /*!
     240             :  *  Returns the maximum value for \a product's \a range in (integer) ohms, or the string "Auto".
     241             :  *  If \a range is not a known valid value, then an null QVariant is returned.
     242             :  *
     243             :  *  \see PokitMeter::maxValue(const PokitMeter::ResistanceRange &range)
     244             :  *  \see PokitPro::maxValue(const PokitPro::ResistanceRange &range)
     245             :  */
     246         126 : QVariant maxValue(const PokitProduct product, const quint8 range)
     247             : {
     248         126 :     switch (product) {
     249          54 :     case PokitProduct::PokitMeter:
     250          54 :         return PokitMeter::maxValue(static_cast<PokitMeter::ResistanceRange>(range));
     251          54 :     case PokitProduct::PokitPro:
     252          54 :         return PokitPro::maxValue(static_cast<PokitPro::ResistanceRange>(range));
     253             :     }
     254          56 :     qCWarning(lc).noquote() << QCoreApplication::translate("ResistanceRange",
     255          28 :         "Unknown PokitProduct value: %1", "maxValue").arg((int)product);
     256             :     return QVariant();
     257             : }
     258             : 
     259             : }
     260             : 
     261             : /// Encapsulates convenience functions for working with voltage ranges.
     262             : namespace VoltageRange {
     263             : 
     264             : /*!
     265             :  *  Returns \a product's current \a range as a human-friendly string.
     266             :  *
     267             :  *  \see PokitMeter::toString(const PokitMeter::VoltageRange &range)
     268             :  *  \see PokitPro::toString(const PokitPro::VoltageRange &range)
     269             :  */
     270        1782 : QString toString(const PokitProduct product, const quint8 range)
     271             : {
     272        1782 :     switch (product) {
     273        1638 :     case PokitProduct::PokitMeter:
     274        1638 :         return PokitMeter::toString(static_cast<PokitMeter::VoltageRange>(range));
     275         126 :     case PokitProduct::PokitPro:
     276         126 :         return PokitPro::toString(static_cast<PokitPro::VoltageRange>(range));
     277             :     }
     278          56 :     qCWarning(lc).noquote() << QCoreApplication::translate("VoltageRange",
     279          28 :         "Unknown PokitProduct value: %1", "toString").arg((int)product);
     280             :     return QString();
     281             : }
     282             : 
     283             : /*!
     284             :  *  Returns the maximum value for \a product's \a range in (integer) millivolts, or the string "Auto".
     285             :  *  If \a range is not a known valid value, then an null QVariant is returned.
     286             :  *
     287             :  *  \see PokitMeter::maxValue(const PokitMeter::VoltageRange &range)
     288             :  *  \see PokitPro::maxValue(const PokitPro::VoltageRange &range)
     289             :  */
     290         270 : QVariant maxValue(const PokitProduct product, const quint8 range)
     291             : {
     292         270 :     switch (product) {
     293         126 :     case PokitProduct::PokitMeter:
     294         126 :         return PokitMeter::maxValue(static_cast<PokitMeter::VoltageRange>(range));
     295         126 :     case PokitProduct::PokitPro:
     296         126 :         return PokitPro::maxValue(static_cast<PokitPro::VoltageRange>(range));
     297             :     }
     298          56 :     qCWarning(lc).noquote() << QCoreApplication::translate("VoltageRange",
     299          28 :         "Unknown PokitProduct value: %1", "maxValue").arg((int)product);
     300             :     return QVariant();
     301             : }
     302             : 
     303             : }
     304             : 
     305             : /// \endcond

Generated by: LCOV version 1.14