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
"(Characterisitc %1 "%2" requested before service assigned.)")
274 if (characteristic.
isValid()) {
275 return characteristic;
279 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
282 RemoteServiceDiscovered
285 qCWarning(lc).noquote() <<
tr(R
"(Characterisitc %1 "%2" requested before service %3 "%4" discovered.)")
288 qCInfo(lc).noquote() << tr("Current service state:") <<
service->
state();
292 qCWarning(lc).noquote() <<
tr(R
"(Characterisitc %1 "%2" not found in service %3 "%4".)")
313 if (!characteristic.
isValid()) {
316 qCDebug(lc).noquote() <<
tr(R
"(Reading characteristic %1 "%2".)")
332 qCDebug(lc).noquote() <<
tr(R
"(Enabling CCCD for characteristic %1 "%2".)")
335 if (!characteristic.
isValid()) {
340 QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration);
342 qCWarning(lc).noquote() <<
tr(R
"(Characterisitc %1 "%2" has no client configuration descriptor.)")
348 #
if (QT_VERSION >= QT_VERSION_CHECK(6, 2, 0))
349 QLowEnergyCharacteristic::CCCDEnableNotification
367 qCDebug(lc).noquote() <<
tr(R
"(Disabling CCCD for characteristic %1 "%2".)")
370 if (!characteristic.
isValid()) {
375 QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration);
377 qCWarning(lc).noquote() <<
tr(R
"(Characterisitc %1 "%2" has no client configuration descriptor.)")
383 #
if (QT_VERSION >= QT_VERSION_CHECK(6, 2, 0))
384 QLowEnergyCharacteristic::CCCDDisable
401 const int minSize,
const int maxSize,
402 const bool failOnMax)
404 if (data.
size() < minSize) {
405 qCWarning(lc).noquote() <<
tr(
"%1 requires %n byte/s, but only %2 present: %3",
nullptr, minSize)
409 if ((maxSize >= 0) && (data.
size() > maxSize)) {
410 qCWarning(lc).noquote() <<
tr(
"%1 has %n extraneous byte/s: %2",
nullptr, data.
size()-maxSize)
427 return (data.
size() <= maxSize)
444 qCWarning(lc).noquote() <<
tr(
"Connected with no controller set") <<
sender();
448 qCDebug(lc).noquote() <<
tr(R
"(Connected to "%1" (%2) at %3.)").arg(
465 qCWarning(lc).noquote() <<
tr(
"Discovery finished with no controller set") <<
sender();
469 qCDebug(lc).noquote() <<
tr(R
"(Discovery finished for "%1" (%2) at %3.)").arg(
474 qCWarning(lc).noquote() <<
tr(
"Discovery finished, but service not found.");
476 Q_EMIT q->serviceErrorOccurred(QLowEnergyService::ServiceError::UnknownError);
488 qCDebug(lc).noquote() <<
tr(
"Service error") << newError;
489 Q_EMIT q->serviceErrorOccurred(newError);
502 qCDebug(lc).noquote() <<
tr(
"Service discovered") << newService;
517 qCDebug(lc).noquote() <<
tr(
"State changed to") << newState;
519 if (lc().isDebugEnabled()) {
523 #define QTPOKIT_INTERNAL_TEST_AND_APPEND(property) \
524 if (characteristic.properties().testFlag(QLowEnergyCharacteristic::property)) { \
525 properties.append(QStringLiteral(#property).toLower());\
528 QTPOKIT_INTERNAL_TEST_AND_APPEND(Broadcasting)
529 QTPOKIT_INTERNAL_TEST_AND_APPEND(Read)
530 QTPOKIT_INTERNAL_TEST_AND_APPEND(WriteNoResponse)
531 QTPOKIT_INTERNAL_TEST_AND_APPEND(Write)
532 QTPOKIT_INTERNAL_TEST_AND_APPEND(Notify)
533 QTPOKIT_INTERNAL_TEST_AND_APPEND(Indicate)
534 QTPOKIT_INTERNAL_TEST_AND_APPEND(WriteSigned)
535 QTPOKIT_INTERNAL_TEST_AND_APPEND(ExtendedProperty)
536 #undef QTPOKIT_INTERNAL_TEST_AND_APPEND
537 qCDebug(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" supports %3.)").arg(characteristic.uuid().toString(),
543 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
546 RemoteServiceDiscovered
550 qCDebug(lc).noquote() <<
tr(
"Service details discovered.");
551 Q_EMIT q->serviceDetailsDiscovered();
565 qCDebug(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" read %n byte/s: %3)", nullptr, value.
size()).
arg(
579 qCDebug(lc).noquote() <<
tr(R
"(Characteristic %1 "%2" written with %Ln byte/s: %3)", nullptr, newValue.
size())
594 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