11#include "pokitproducts_p.h"
16#include <QLowEnergyController>
31AbstractPokitService::AbstractPokitService(
31AbstractPokitService::AbstractPokitService( {
…}
66 Q_D(
const AbstractPokitService);
67 return d->autoDiscover;
81 Q_D(AbstractPokitService);
82 d->autoDiscover = discover;
92 Q_D(
const AbstractPokitService);
93 return d->pokitProduct;
116 Q_D(AbstractPokitService);
117 d->pokitProduct = product;
125 Q_D(AbstractPokitService);
134 Q_D(
const AbstractPokitService);
204 qCDebug(lc).noquote() <<
tr(
"Already have service object:") <<
service;
209 qCDebug(lc).noquote() <<
tr(
"Service UUID not assigned yet.");
230 qCDebug(lc).noquote() <<
tr(R
"(Descriptor "%1" (%2) read.)")
237 qCDebug(lc).noquote() <<
tr(R
"(Descriptor "%1" (%2) written.)")
243 #if (QT_VERSION < QT_VERSION_CHECK(6, 2, 0))
246 &QLowEnergyService::errorOccurred,
272 qCDebug(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" requested before service assigned.)")
278 return characteristic;
282 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
285 RemoteServiceDiscovered
288 qCWarning(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" requested before service %3 "%4" discovered.)")
291 qCInfo(lc).noquote() << tr("Current service state:") <<
service->state();
295 qCWarning(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" not found in service %3 "%4".)")
316 if (!characteristic.
isValid()) {
319 qCDebug(lc).noquote() <<
tr(R
"(Reading characteristic %1 "%2".)")
321 service->readCharacteristic(characteristic);
335 qCDebug(lc).noquote() <<
tr(R
"(Enabling CCCD for characteristic %1 "%2".)")
338 if (!characteristic.
isValid()) {
343 QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration);
345 qCWarning(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" has no client configuration descriptor.)")
350 service->writeDescriptor(descriptor,
351 #
if (QT_VERSION >= QT_VERSION_CHECK(6, 2, 0))
352 QLowEnergyCharacteristic::CCCDEnableNotification
370 qCDebug(lc).noquote() <<
tr(R
"(Disabling CCCD for characteristic %1 "%2".)")
373 if (!characteristic.
isValid()) {
378 QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration);
380 qCWarning(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" has no client configuration descriptor.)")
385 service->writeDescriptor(descriptor,
386 #
if (QT_VERSION >= QT_VERSION_CHECK(6, 2, 0))
387 QLowEnergyCharacteristic::CCCDDisable
404 const int minSize,
const int maxSize,
405 const bool failOnMax)
407 if (data.
size() < minSize) {
408 qCWarning(lc).noquote() <<
tr(
"%1 requires %n byte/s, but only %2 present: %3",
nullptr, minSize)
412 if ((maxSize >= 0) && (data.
size() > maxSize)) {
413 qCWarning(lc).noquote() <<
tr(
"%1 has %n extraneous byte/s: %2",
nullptr, data.
size()-maxSize)
430 return (data.
size() <= maxSize)
447 qCWarning(lc).noquote() <<
tr(
"Connected with no controller set") <<
sender();
451 qCDebug(lc).noquote() <<
tr(R
"(Connected to "%1" (%2) at %3.)").arg(
468 qCWarning(lc).noquote() <<
tr(
"Discovery finished with no controller set") <<
sender();
472 qCDebug(lc).noquote() <<
tr(R
"(Discovery finished for "%1" (%2) at %3.)").arg(
477 qCWarning(lc).noquote() <<
tr(
"Discovery finished, but service not found.");
479 Q_EMIT q->serviceErrorOccurred(QLowEnergyService::ServiceError::UnknownError);
491 qCDebug(lc).noquote() <<
tr(
"Service error") << newError;
492 Q_EMIT q->serviceErrorOccurred(newError);
505 qCDebug(lc).noquote() <<
tr(
"Service discovered") << newService;
520 qCDebug(lc).noquote() <<
tr(
"State changed to") << newState;
522 if (lc().isDebugEnabled()) {
523 for (
const auto &characteristic:
service->characteristics()) {
526 #define QTPOKIT_INTERNAL_TEST_AND_APPEND(property) \
527 if (characteristic.properties().testFlag(QLowEnergyCharacteristic::property)) { \
528 properties.append(QStringLiteral(#property).toLower());\
531 QTPOKIT_INTERNAL_TEST_AND_APPEND(Broadcasting)
532 QTPOKIT_INTERNAL_TEST_AND_APPEND(Read)
533 QTPOKIT_INTERNAL_TEST_AND_APPEND(WriteNoResponse)
534 QTPOKIT_INTERNAL_TEST_AND_APPEND(Write)
535 QTPOKIT_INTERNAL_TEST_AND_APPEND(Notify)
536 QTPOKIT_INTERNAL_TEST_AND_APPEND(Indicate)
537 QTPOKIT_INTERNAL_TEST_AND_APPEND(WriteSigned)
538 QTPOKIT_INTERNAL_TEST_AND_APPEND(ExtendedProperty)
539 #undef QTPOKIT_INTERNAL_TEST_AND_APPEND
540 qCDebug(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" supports %3.)").arg(characteristic.uuid().toString(),
546 #
if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
549 RemoteServiceDiscovered
553 qCDebug(lc).noquote() <<
tr(
"Service details discovered.");
554 Q_EMIT q->serviceDetailsDiscovered();
568 qCDebug(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" read %n byte/s: %3)", nullptr, value.
size()).
arg(
582 qCDebug(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" written with %Ln byte/s: %3)", nullptr, newValue.
size())
597 qCDebug(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" changed to %Ln byte/s: %3)", nullptr, newValue.
size())
Declares the AbstractPokitService class.
Declares the AbstractPokitServicePrivate class.
The AbstractPokitServicePrivate class provides private implementation for AbstractPokitService.
bool autoDiscover
Whether autodiscovery is enabled or not.
bool disableCharacteristicNotificatons(const QBluetoothUuid &uuid)
Disables client (Pokit device) side notification for characteristic uuid.
bool createServiceObject()
Creates an internal service object from the internal controller.
void discoveryFinished()
Handles QLowEnergyController::discoveryFinished events.
QBluetoothUuid serviceUuid
UUIDs for service.
virtual void characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue)
Handles QLowEnergyService::characteristicChanged events.
virtual void serviceDiscovered(const QBluetoothUuid &newService)
Handles QLowEnergyController::serviceDiscovered events.
AbstractPokitService * q_ptr
Internal q-pointer.
AbstractPokitServicePrivate(const QBluetoothUuid &serviceUuid, QLowEnergyController *controller, AbstractPokitService *const q)
bool enableCharacteristicNotificatons(const QBluetoothUuid &uuid)
Enables client (Pokit device) side notification for characteristic uuid.
virtual void characteristicRead(const QLowEnergyCharacteristic &characteristic, const QByteArray &value)
Handles QLowEnergyService::characteristicRead events.
void connected()
Handles QLowEnergyController::connected events.
QLowEnergyCharacteristic getCharacteristic(const QBluetoothUuid &uuid) const
Get uuid characteristic from the underlying service.
virtual void characteristicWritten(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue)
Handles QLowEnergyService::characteristicWritten events.
void errorOccurred(const QLowEnergyService::ServiceError newError)
Handles QLowEnergyController::errorOccurred events.
void stateChanged(QLowEnergyService::ServiceState newState)
Handles QLowEnergyController::stateChanged events.
bool readCharacteristic(const QBluetoothUuid &uuid)
Read the uuid characteristic.
QLowEnergyService * service
BLE service to read/write characteristics.
QLowEnergyController * controller
BLE controller to fetch the service from.
static QString toHexString(const QByteArray &data, const int maxSize=20)
Returns up to maxSize bytes of data as a human readable hexadecimal string.
static bool checkSize(const QString &label, const QByteArray &data, const int minSize, const int maxSize=-1, const bool failOnMax=false)
Returns false if data is smaller than minSize, otherwise returns failOnMax if data is bigger than max...
std::optional< PokitProduct > pokitProduct
The Pokit product controller is connected to.
The AbstractPokitService class provides a common base for Pokit services classes.
std::optional< PokitProduct > pokitProduct() const
Returns the Pokit product this service is attached to.
bool autoDiscover() const
Returns true if autodiscovery of services and service details is enabled, false otherwise.
QLowEnergyService * service()
Returns a non-const pointer to the internal service object, if any.
void setAutoDiscover(const bool discover=true)
If discover is true, autodiscovery will be attempted.
void setPokitProduct(const PokitProduct product)
Sets the current Pokit product.
AbstractPokitServicePrivate * d_ptr
Internal d-pointer.
virtual ~AbstractPokitService()
Destroys this AbstractPokitService object.
static QString charcteristicToString(const QBluetoothUuid &uuid)
Returns a human-readable name for the uuid characteristic, or a null QString if unknown.
static QString serviceToString(const QBluetoothUuid &uuid)
Returns a human-readable name for the uuid service, or a null QString if unknown.
Declares the PokitDevice class.
PokitProduct
Pokit products known to, and supported by, the QtPokit library.
QTPOKIT_EXPORT QString toString(const PokitProduct product)
Returns product as user-friendly string.
QByteArray fromHex(const QByteArray &hexEncoded)
QByteArray left(int len) const const
QByteArray mid(int pos, int len) const const
QByteArray right(int len) const const
QByteArray toHex() const const
QLowEnergyDescriptor descriptor(const QBluetoothUuid &uuid) const const
bool isValid() const const
QBluetoothUuid uuid() const const
void serviceDiscovered(const QBluetoothUuid &newService)
bool isValid() const const
QString name() const const
QBluetoothUuid uuid() const const
void characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue)
void characteristicRead(const QLowEnergyCharacteristic &characteristic, const QByteArray &value)
void characteristicWritten(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue)
void descriptorRead(const QLowEnergyDescriptor &descriptor, const QByteArray &value)
void descriptorWritten(const QLowEnergyDescriptor &descriptor, const QByteArray &newValue)
QLowEnergyService::ServiceError error() const const
void stateChanged(QLowEnergyService::ServiceState newState)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
QObject * sender() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
QString join(const QString &separator) const const
#define QTPOKIT_BEGIN_NAMESPACE
Macro for starting the QtPokit library's top-most namespace (if one is defined).
#define QTPOKIT_END_NAMESPACE
Macro for ending the QtPokit library's top-most namespace (if one is defined).
QString toString() const const
Declares the DOKIT_USE_STRINGLITERALS macro, and related functions.
#define DOKIT_USE_STRINGLITERALS
Internal macro for using either official Qt string literals (added in Qt 6.4), or our own equivalent ...