11#include "pokitproducts_p.h"
15#include <QLowEnergyController>
27AbstractPokitService::AbstractPokitService(
63 return d->autoDiscover;
78 d->autoDiscover = discover;
89 return d->pokitProduct;
113 d->pokitProduct = product;
170 : controller(controller), serviceUuid(serviceUuid), q_ptr(q)
200 qCDebug(lc).noquote() <<
tr(
"Already have service object:") <<
service;
205 qCDebug(lc).noquote() <<
tr(
"Service UUID not assigned yet.");
226 qCDebug(lc).noquote() <<
tr(R
"(Descriptor "%1" (%2) read.)")
233 qCDebug(lc).noquote() <<
tr(R
"(Descriptor "%1" (%2) written.)")
239 #if (QT_VERSION < QT_VERSION_CHECK(6, 2, 0))
242 &QLowEnergyService::errorOccurred,
268 qCDebug(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" requested before service assigned.)")
274 return characteristic;
278 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
281 RemoteServiceDiscovered
284 qCWarning(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" requested before service %3 "%4" discovered.)")
287 qCInfo(lc).noquote() << tr("Current service state:") <<
service->
state();
291 qCWarning(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" not found in service %3 "%4".)")
312 if (!characteristic.
isValid()) {
315 qCDebug(lc).noquote() <<
tr(R
"(Reading characteristic %1 "%2".)")
331 qCDebug(lc).noquote() <<
tr(R
"(Enabling CCCD for characteristic %1 "%2".)")
334 if (!characteristic.
isValid()) {
339 QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration);
341 qCWarning(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" has no client configuration descriptor.)")
347 #
if (QT_VERSION >= QT_VERSION_CHECK(6, 2, 0))
348 QLowEnergyCharacteristic::CCCDEnableNotification
366 qCDebug(lc).noquote() <<
tr(R
"(Disabling CCCD for characteristic %1 "%2".)")
369 if (!characteristic.
isValid()) {
374 QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration);
376 qCWarning(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" has no client configuration descriptor.)")
382 #
if (QT_VERSION >= QT_VERSION_CHECK(6, 2, 0))
383 QLowEnergyCharacteristic::CCCDDisable
400 const int minSize,
const int maxSize,
401 const bool failOnMax)
403 if (data.
size() < minSize) {
404 qCWarning(lc).noquote() <<
tr(
"%1 requires %n byte/s, but only %2 present: %3",
nullptr, minSize)
408 if ((maxSize >= 0) && (data.
size() > maxSize)) {
409 qCWarning(lc).noquote() <<
tr(
"%1 has %n extraneous byte/s: %2",
nullptr, data.
size()-maxSize)
426 return (data.
size() <= maxSize)
443 qCWarning(lc).noquote() <<
tr(
"Connected with no controller set") <<
sender();
447 qCDebug(lc).noquote() <<
tr(R
"(Connected to "%1" (%2) at %3.)").arg(
464 qCWarning(lc).noquote() <<
tr(
"Discovery finished with no controller set") <<
sender();
468 qCDebug(lc).noquote() <<
tr(R
"(Discovery finished for "%1" (%2) at %3.)").arg(
473 qCWarning(lc).noquote() <<
tr(
"Discovery finished, but service not found.");
475 Q_EMIT q->serviceErrorOccurred(QLowEnergyService::ServiceError::UnknownError);
487 qCDebug(lc).noquote() <<
tr(
"Service error") << newError;
488 Q_EMIT q->serviceErrorOccurred(newError);
501 qCDebug(lc).noquote() <<
tr(
"Service discovered") << newService;
516 qCDebug(lc).noquote() <<
tr(
"State changed to") << newState;
518 if (lc().isDebugEnabled()) {
522 #define QTPOKIT_INTERNAL_TEST_AND_APPEND(property) \
523 if (characteristic.properties().testFlag(QLowEnergyCharacteristic::property)) { \
524 properties.append(QStringLiteral(#property).toLower());\
527 QTPOKIT_INTERNAL_TEST_AND_APPEND(Broadcasting)
528 QTPOKIT_INTERNAL_TEST_AND_APPEND(Read)
529 QTPOKIT_INTERNAL_TEST_AND_APPEND(WriteNoResponse)
530 QTPOKIT_INTERNAL_TEST_AND_APPEND(Write)
531 QTPOKIT_INTERNAL_TEST_AND_APPEND(Notify)
532 QTPOKIT_INTERNAL_TEST_AND_APPEND(Indicate)
533 QTPOKIT_INTERNAL_TEST_AND_APPEND(WriteSigned)
534 QTPOKIT_INTERNAL_TEST_AND_APPEND(ExtendedProperty)
535 #undef QTPOKIT_INTERNAL_TEST_AND_APPEND
536 qCDebug(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" supports %3.)").arg(characteristic.uuid().toString(),
542 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
545 RemoteServiceDiscovered
549 qCDebug(lc).noquote() <<
tr(
"Service details discovered.");
550 Q_EMIT q->serviceDetailsDiscovered();
564 qCDebug(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" read %n byte/s: %3)", nullptr, value.
size()).
arg(
578 qCDebug(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" written with %Ln byte/s: %3)", nullptr, newValue.
size())
593 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.
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 characteristc 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 unknonw.
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.
QString toString() const const
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
QLowEnergyService * createServiceObject(const QBluetoothUuid &serviceUuid, QObject *parent)
QBluetoothAddress remoteAddress() const const
QBluetoothUuid remoteDeviceUuid() const const
QString remoteName() const const
void serviceDiscovered(const QBluetoothUuid &newService)
bool isValid() const const
QString name() const const
QBluetoothUuid uuid() const const
QLowEnergyCharacteristic characteristic(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)
QList< QLowEnergyCharacteristic > characteristics() const const
void descriptorRead(const QLowEnergyDescriptor &descriptor, const QByteArray &value)
void descriptorWritten(const QLowEnergyDescriptor &descriptor, const QByteArray &newValue)
QLowEnergyService::ServiceError error() const const
void readCharacteristic(const QLowEnergyCharacteristic &characteristic)
QBluetoothUuid serviceUuid() const const
QLowEnergyService::ServiceState state() const const
void stateChanged(QLowEnergyService::ServiceState newState)
void writeDescriptor(const QLowEnergyDescriptor &descriptor, const QByteArray &newValue)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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
bool isNull() const const
QString toString() const const