summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNedim Hadzic <nhadzic@blackberry.com>2013-11-27 17:13:59 +0100
committerNedim Hadzic <nhadzic@blackberry.com>2013-11-28 16:32:07 +0100
commit12ac88fa8d87d8fd86aa86bc009c7a78503648ee (patch)
tree5708457bcefa1c019913443023a97b4a1bc6a84f /src
parentbeb84e5fcdc79b3e9a613d9d5c36ac27952a5546 (diff)
Implemented the characteristic error handling.
In case of enabling notifications and writing values to the LE device, some errors can occur and which are reported now. Change-Id: I02c6deab925d06916c5b56705c9423386400f713 Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'src')
-rw-r--r--src/bluetooth/qlowenergycharacteristicinfo.cpp16
-rw-r--r--src/bluetooth/qlowenergycharacteristicinfo.h2
-rw-r--r--src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp45
-rw-r--r--src/bluetooth/qlowenergycharacteristicinfo_p.h2
-rw-r--r--src/bluetooth/qlowenergycharacteristicinfo_qnx.cpp18
-rw-r--r--src/bluetooth/qlowenergycontroller.cpp21
-rw-r--r--src/bluetooth/qlowenergycontroller.h2
-rw-r--r--src/bluetooth/qlowenergycontroller_p.h1
8 files changed, 82 insertions, 25 deletions
diff --git a/src/bluetooth/qlowenergycharacteristicinfo.cpp b/src/bluetooth/qlowenergycharacteristicinfo.cpp
index 232d03ca..44a7b58d 100644
--- a/src/bluetooth/qlowenergycharacteristicinfo.cpp
+++ b/src/bluetooth/qlowenergycharacteristicinfo.cpp
@@ -249,9 +249,10 @@ bool QLowEnergyCharacteristicInfo::isNotificationCharacteristic() const
}
/*!
- Writes the value \a value directly to LE device.
+ Writes the value \a value directly to LE device. If the value was not written successfully
+ an error will be emitted with an error string.
- \sa setValue()
+ \sa errorString()
*/
void QLowEnergyCharacteristicInfo::writeValue(const QByteArray &value)
{
@@ -291,4 +292,15 @@ QList<QLowEnergyDescriptorInfo> QLowEnergyCharacteristicInfo::descriptors() cons
return d_ptr->descriptorsList;
}
+/*!
+ Returns an error string if error occurred. An error is emitted in the
+ QLowEnergyController class.
+
+ \sa QLowEnergyController::error(const QLowEnergyCharacteristicInfo &)
+*/
+QString QLowEnergyCharacteristicInfo::errorString() const
+{
+ return d_ptr->errorString;
+}
+
QT_END_NAMESPACE
diff --git a/src/bluetooth/qlowenergycharacteristicinfo.h b/src/bluetooth/qlowenergycharacteristicinfo.h
index 0622b5d9..2e28e337 100644
--- a/src/bluetooth/qlowenergycharacteristicinfo.h
+++ b/src/bluetooth/qlowenergycharacteristicinfo.h
@@ -101,6 +101,8 @@ public:
bool isValid() const;
+ QString errorString() const;
+
protected:
QSharedPointer<QLowEnergyCharacteristicInfoPrivate> d_ptr;
diff --git a/src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp b/src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp
index fa57ffc4..f66100ed 100644
--- a/src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp
+++ b/src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp
@@ -55,7 +55,7 @@
QT_BEGIN_NAMESPACE
QLowEnergyCharacteristicInfoPrivate::QLowEnergyCharacteristicInfoPrivate():
- value (QByteArray()), permission(0), notification (false), handle(QStringLiteral("0x0000")), properties(QVariantMap()), characteristic(0), m_signalConnected(false)
+ value (QByteArray()), permission(0), notification (false), handle(QStringLiteral("0x0000")), properties(QVariantMap()), errorString(""), characteristic(0), m_signalConnected(false)
{
process = process->instance();
t=0;
@@ -129,26 +129,31 @@ void QLowEnergyCharacteristicInfoPrivate::replyReceived(const QString &reply)
void QLowEnergyCharacteristicInfoPrivate::setValue(const QByteArray &wantedValue)
{
- process = process->instance();
- if (!m_signalConnected) {
- connect(process, SIGNAL(replySend(const QString &)), this, SLOT(replyReceived(const QString &)));
- m_signalConnected = true;
- }
- value = wantedValue;
- QString command;
- if (notification == true)
- command = QStringLiteral("char-write-req ") + notificationHandle + QStringLiteral(" ") + QString::fromLocal8Bit(value.constData());
- else
- command = QStringLiteral("char-write-req ") + handle + QStringLiteral(" ") + QString::fromLocal8Bit(value.constData());
+ if (permission & QLowEnergyCharacteristicInfo::Write) {
+ process = process->instance();
+ if (!m_signalConnected) {
+ connect(process, SIGNAL(replySend(const QString &)), this, SLOT(replyReceived(const QString &)));
+ m_signalConnected = true;
+ }
+ value = wantedValue;
+ QString command;
+ if (notification == true)
+ command = QStringLiteral("char-write-req ") + notificationHandle + QStringLiteral(" ") + QString::fromLocal8Bit(value.constData());
+ else
+ command = QStringLiteral("char-write-req ") + handle + QStringLiteral(" ") + QString::fromLocal8Bit(value.constData());
-#ifdef QT_LOWENERGYCHARACTERISTIC_DEBUG
- qDebug() << command << t << process;
-#endif
- process->executeCommand(command);
- process->executeCommand(QStringLiteral("\n"));
- t++;
-
+ #ifdef QT_LOWENERGYCHARACTERISTIC_DEBUG
+ qDebug() << command << t << process;
+ #endif
+ process->executeCommand(command);
+ process->executeCommand(QStringLiteral("\n"));
+ t++;
+ }
+ else {
+ errorString = QStringLiteral("This characteristic does not support write operations.");
+ emit error(uuid);
+ }
}
bool QLowEnergyCharacteristicInfoPrivate::enableNotification()
@@ -162,6 +167,8 @@ bool QLowEnergyCharacteristicInfoPrivate::enableNotification()
#ifdef QT_LOWENERGYCHARACTERISTIC_DEBUG
qDebug() << "Notification changes not allowed";
#endif
+ errorString = QStringLiteral("This characteristic does not support notifications.");
+ emit error(uuid);
return false;
}
QByteArray val;
diff --git a/src/bluetooth/qlowenergycharacteristicinfo_p.h b/src/bluetooth/qlowenergycharacteristicinfo_p.h
index 379ca5d4..7a42f4a3 100644
--- a/src/bluetooth/qlowenergycharacteristicinfo_p.h
+++ b/src/bluetooth/qlowenergycharacteristicinfo_p.h
@@ -86,6 +86,7 @@ public:
QString notificationHandle;
int instance;
QVariantMap properties;
+ QString errorString;
QList<QLowEnergyDescriptorInfo> descriptorsList;
#ifdef QT_QNX_BLUETOOTH
bt_gatt_characteristic_t characteristic;
@@ -104,6 +105,7 @@ public Q_SLOTS:
Q_SIGNALS:
void notifyValue(const QBluetoothUuid &);
+ void error(const QBluetoothUuid &);
private:
#ifdef QT_BLUEZ_BLUETOOTH
diff --git a/src/bluetooth/qlowenergycharacteristicinfo_qnx.cpp b/src/bluetooth/qlowenergycharacteristicinfo_qnx.cpp
index b5d4bf8c..f5de6b5d 100644
--- a/src/bluetooth/qlowenergycharacteristicinfo_qnx.cpp
+++ b/src/bluetooth/qlowenergycharacteristicinfo_qnx.cpp
@@ -89,7 +89,7 @@ int stringToBuffer(const QString &stringData, uint8_t *buffer, int bufferLength)
}
QLowEnergyCharacteristicInfoPrivate::QLowEnergyCharacteristicInfoPrivate():
- name(QString()), value(QByteArray()), permission(0), instance(-1), properties(QVariantMap()), handle(QStringLiteral("0x0000"))
+ name(QString()), value(QByteArray()), permission(0), instance(-1), handle(QStringLiteral("0x0000")), properties(QVariantMap()), errorString("")
{
}
@@ -138,18 +138,22 @@ bool QLowEnergyCharacteristicInfoPrivate::enableNotification()
{
if (instance == -1) {
qBBBluetoothDebug() << " GATT service not connected ";
- //q_ptr->error(QLowEnergyCharacteristicInfo::NotConnected);
+ errorString = QStringLiteral("Service is not connected");
+ emit error(uuid);
return false;
}
if ( (permission & QLowEnergyCharacteristicInfo::Notify) == 0) {
qBBBluetoothDebug() << "Notification changes not allowed";
+ errorString = QStringLiteral("This characteristic does not support notifications.");
+ emit error(uuid);
return false;
}
int rc = bt_gatt_enable_notify(instance, &characteristic, 1);
if (rc != 0) {
qBBBluetoothDebug() << "bt_gatt_enable_notify errno=" << errno << strerror(errno);
- //emit q_ptr->error(QLowEnergyCharacteristicInfo::NotificationFail);
+ errorString = QString::fromLatin1(strerror(errno));
+ emit error(uuid);
return false;
} else {
qBBBluetoothDebug() << "bt_gatt_enable_notify was presumably OK";
@@ -164,6 +168,8 @@ void QLowEnergyCharacteristicInfoPrivate::setValue(const QByteArray &wantedValue
uint8_t *characteristicBuffer = (uint8_t *)alloca(characteristicLen / 2 + 1);
if (!characteristicBuffer) {
qBBBluetoothDebug() << "GATT characteristic: Not enough memory";
+ errorString = QStringLiteral("Not enough memory.");
+ emit error(uuid);
bt_gatt_disconnect_instance(instance);
return;
}
@@ -176,9 +182,15 @@ void QLowEnergyCharacteristicInfoPrivate::setValue(const QByteArray &wantedValue
if (byteCount < 0) {
qBBBluetoothDebug() << "Unable to write characteristic value: " << errno << strerror(errno);
+ errorString = QStringLiteral("Unable to write characteristic value: ") + QString::fromLatin1(strerror(errno));
+ emit error(uuid);
}
}
}
+ else {
+ errorString = QStringLiteral("Characteristic does not allow write operations. The wanted value was not written to the device.");
+ emit error(uuid);
+ }
value = wantedValue;
properties[QStringLiteral("value")] = value;
emit notifyValue(uuid);
diff --git a/src/bluetooth/qlowenergycontroller.cpp b/src/bluetooth/qlowenergycontroller.cpp
index c74a67b2..eef5c81a 100644
--- a/src/bluetooth/qlowenergycontroller.cpp
+++ b/src/bluetooth/qlowenergycontroller.cpp
@@ -81,12 +81,20 @@ QT_BEGIN_NAMESPACE
/*!
\fn void QLowEnergyController::error(const QLowEnergyServiceInfo &)
- This signal is emitted when an error occurs.
+ This signal is emitted when the service error occurs.
\sa QLowEnergyServiceInfo::errorString()
*/
/*!
+ \fn void QLowEnergyController::error(const QLowEnergyCharacteristicInfo &)
+
+ This signal is emitted when the characteristic error occurs.
+
+ \sa QLowEnergyCharacteristicInfo::errorString()
+*/
+
+/*!
\fn void QLowEnergyController::disconnected(const QLowEnergyServiceInfo &)
Emits disconnected signal with disconnected LE service.
@@ -129,6 +137,17 @@ void QLowEnergyControllerPrivate::_q_serviceError(const QBluetoothUuid &uuid)
}
}
+void QLowEnergyControllerPrivate::_q_characteristicError(const QBluetoothUuid &uuid)
+{
+ for (int i = 0; i < m_leServices.size(); i++) {
+ QList<QLowEnergyCharacteristicInfo> characteristics = m_leServices.at(i).characteristics();
+ for (int j = 0; j < characteristics.size(); j++) {
+ if (characteristics.at(j).uuid() == uuid)
+ emit q_ptr->error(characteristics.at(j));
+ }
+ }
+}
+
void QLowEnergyControllerPrivate::_q_valueReceived(const QBluetoothUuid &uuid)
{
diff --git a/src/bluetooth/qlowenergycontroller.h b/src/bluetooth/qlowenergycontroller.h
index 4f49acb5..432e6669 100644
--- a/src/bluetooth/qlowenergycontroller.h
+++ b/src/bluetooth/qlowenergycontroller.h
@@ -69,6 +69,7 @@ public:
Q_SIGNALS:
void connected(const QLowEnergyServiceInfo &);
void error(const QLowEnergyServiceInfo &);
+ void error(const QLowEnergyCharacteristicInfo &);
void disconnected(const QLowEnergyServiceInfo &);
void valueChanged(const QLowEnergyCharacteristicInfo &);
@@ -79,6 +80,7 @@ private:
Q_PRIVATE_SLOT(d_func(), void _q_serviceConnected(const QBluetoothUuid &uuid))
Q_PRIVATE_SLOT(d_func(), void _q_serviceError(const QBluetoothUuid &uuid))
+ Q_PRIVATE_SLOT(d_func(), void _q_characteristicError(const QBluetoothUuid &uuid))
Q_PRIVATE_SLOT(d_func(), void _q_valueReceived(const QBluetoothUuid &uuid))
Q_PRIVATE_SLOT(d_func(), void _q_serviceDisconnected(const QBluetoothUuid &uuid))
};
diff --git a/src/bluetooth/qlowenergycontroller_p.h b/src/bluetooth/qlowenergycontroller_p.h
index 96ec3730..b19f7024 100644
--- a/src/bluetooth/qlowenergycontroller_p.h
+++ b/src/bluetooth/qlowenergycontroller_p.h
@@ -56,6 +56,7 @@ public:
void _q_serviceConnected(const QBluetoothUuid &uuid);
void _q_serviceError(const QBluetoothUuid &uuid);
+ void _q_characteristicError(const QBluetoothUuid &uuid);
void _q_valueReceived(const QBluetoothUuid &uuid);
void _q_serviceDisconnected(const QBluetoothUuid &uuid);