summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@theqtcompany.com>2015-12-14 11:42:53 +0100
committerAlex Blasche <alexander.blasche@theqtcompany.com>2015-12-21 12:50:23 +0000
commit60e8e3bfbcd584b49f81ad5b265930cdac0a0628 (patch)
tree53837d1aad8fb680ebd860b5a440774a29126c43
parent3c3df2e6a5d9d7b9421c3421553640502be41e48 (diff)
Make more use of Qt's functions for handling endianness and alignment.
Change-Id: Iea44e99e8c7b38e3bdece4de989770831d40ff83 Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
-rw-r--r--src/bluetooth/bluez/bluez_data_p.h34
-rw-r--r--src/bluetooth/qlowenergycontroller_bluez.cpp36
2 files changed, 23 insertions, 47 deletions
diff --git a/src/bluetooth/bluez/bluez_data_p.h b/src/bluetooth/bluez/bluez_data_p.h
index 456a9374..7db0e50b 100644
--- a/src/bluetooth/bluez/bluez_data_p.h
+++ b/src/bluetooth/bluez/bluez_data_p.h
@@ -134,22 +134,6 @@ struct sockaddr_rc {
// Bt Low Energy related
-#define bt_get_unaligned(ptr) \
-({ \
- struct __attribute__((packed)) { \
- __typeof__(*(ptr)) __v; \
- } *__p = (__typeof__(__p)) (ptr); \
- __p->__v; \
-})
-
-#define bt_put_unaligned(val, ptr) \
-do { \
- struct __attribute__((packed)) { \
- __typeof__(*(ptr)) __v; \
- } *__p = (__typeof__(__p)) (ptr); \
- __p->__v = (val); \
-} while (0)
-
#if __BYTE_ORDER == __LITTLE_ENDIAN
static inline void btoh128(const quint128 *src, quint128 *dst)
@@ -165,15 +149,7 @@ static inline void ntoh128(const quint128 *src, quint128 *dst)
dst->data[15 - i] = src->data[i];
}
-static inline quint16 bt_get_le16(const void *ptr)
-{
- return bt_get_unaligned((const quint16 *) ptr);
-}
#elif __BYTE_ORDER == __BIG_ENDIAN
-static inline quint16 bt_get_le16(const void *ptr)
-{
- return qbswap(bt_get_unaligned((const quint16 *) ptr));
-}
static inline void btoh128(const quint128 *src, quint128 *dst)
{
@@ -191,14 +167,14 @@ static inline void ntoh128(const quint128 *src, quint128 *dst)
#error "Unknown byte order"
#endif
-inline quint8 hostToBt(quint8 val) { return val; }
-inline quint16 hostToBt(quint16 val) { return htobs(val); }
-inline quint32 hostToBt(quint32 val) { return htobl(val); }
-inline quint64 hostToBt(quint64 val) { return htobll(val); }
+static inline quint16 bt_get_le16(const void *ptr)
+{
+ return qFromLittleEndian<quint16>(reinterpret_cast<const uchar *>(ptr));
+}
template<typename T> inline void putBtData(T src, void *dst)
{
- bt_put_unaligned(hostToBt(src), reinterpret_cast<T *>(dst));
+ qToLittleEndian(src, reinterpret_cast<uchar *>(dst));
}
template<> inline void putBtData(quint128 src, void *dst)
{
diff --git a/src/bluetooth/qlowenergycontroller_bluez.cpp b/src/bluetooth/qlowenergycontroller_bluez.cpp
index 2ebff8ab..a69bd2c4 100644
--- a/src/bluetooth/qlowenergycontroller_bluez.cpp
+++ b/src/bluetooth/qlowenergycontroller_bluez.cpp
@@ -1242,9 +1242,9 @@ void QLowEnergyControllerPrivate::sendReadByGroupRequest(
quint8 packet[GRP_TYPE_REQ_HEADER_SIZE];
packet[0] = ATT_OP_READ_BY_GROUP_REQUEST;
- bt_put_unaligned(htobs(start), (quint16 *) &packet[1]);
- bt_put_unaligned(htobs(end), (quint16 *) &packet[3]);
- bt_put_unaligned(htobs(type), (quint16 *) &packet[5]);
+ putBtData(start, &packet[1]);
+ putBtData(end, &packet[3]);
+ putBtData(type, &packet[5]);
QByteArray data(GRP_TYPE_REQ_HEADER_SIZE, Qt::Uninitialized);
memcpy(data.data(), packet, GRP_TYPE_REQ_HEADER_SIZE);
@@ -1280,9 +1280,9 @@ void QLowEnergyControllerPrivate::sendReadByTypeRequest(
quint8 packet[READ_BY_TYPE_REQ_HEADER_SIZE];
packet[0] = ATT_OP_READ_BY_TYPE_REQUEST;
- bt_put_unaligned(htobs(nextHandle), (quint16 *) &packet[1]);
- bt_put_unaligned(htobs(serviceData->endHandle), (quint16 *) &packet[3]);
- bt_put_unaligned(htobs(attributeType), (quint16 *) &packet[5]);
+ putBtData(nextHandle, &packet[1]);
+ putBtData(serviceData->endHandle, &packet[3]);
+ putBtData(attributeType, &packet[5]);
QByteArray data(READ_BY_TYPE_REQ_HEADER_SIZE, Qt::Uninitialized);
memcpy(data.data(), packet, READ_BY_TYPE_REQ_HEADER_SIZE);
@@ -1376,7 +1376,7 @@ void QLowEnergyControllerPrivate::readServiceValues(
for (int i = 0; i < targetHandles.count(); i++) {
pair = targetHandles.at(i);
packet[0] = ATT_OP_READ_REQUEST;
- bt_put_unaligned(htobs(pair.first), (quint16 *) &packet[1]);
+ putBtData(pair.first, &packet[1]);
QByteArray data(READ_REQUEST_HEADER_SIZE, Qt::Uninitialized);
memcpy(data.data(), packet, READ_REQUEST_HEADER_SIZE);
@@ -1431,8 +1431,8 @@ void QLowEnergyControllerPrivate::readServiceValuesByOffset(
}
}
- bt_put_unaligned(htobs(handleToRead), (quint16 *) &packet[1]);
- bt_put_unaligned(htobs(offset), (quint16 *) &packet[3]);
+ putBtData(handleToRead, &packet[1]);
+ putBtData(offset, &packet[3]);
QByteArray data(READ_BLOB_REQUEST_HEADER_SIZE, Qt::Uninitialized);
memcpy(data.data(), packet, READ_BLOB_REQUEST_HEADER_SIZE);
@@ -1495,7 +1495,7 @@ void QLowEnergyControllerPrivate::exchangeMTU()
quint8 packet[MTU_EXCHANGE_HEADER_SIZE];
packet[0] = ATT_OP_EXCHANGE_MTU_REQUEST;
- bt_put_unaligned(htobs(ATT_MAX_LE_MTU), (quint16 *) &packet[1]);
+ putBtData(quint16(ATT_MAX_LE_MTU), &packet[1]);
QByteArray data(MTU_EXCHANGE_HEADER_SIZE, Qt::Uninitialized);
memcpy(data.data(), packet, MTU_EXCHANGE_HEADER_SIZE);
@@ -1613,8 +1613,8 @@ void QLowEnergyControllerPrivate::discoverNextDescriptor(
else
charEndHandle = pendingCharHandles[1] - 1;
- bt_put_unaligned(htobs(charStartHandle), (quint16 *) &packet[1]);
- bt_put_unaligned(htobs(charEndHandle), (quint16 *) &packet[3]);
+ putBtData(charStartHandle, &packet[1]);
+ putBtData(charEndHandle, &packet[3]);
QByteArray data(FIND_INFO_REQUEST_HEADER_SIZE, Qt::Uninitialized);
memcpy(data.data(), packet, FIND_INFO_REQUEST_HEADER_SIZE);
@@ -1649,8 +1649,8 @@ void QLowEnergyControllerPrivate::sendNextPrepareWriteRequest(
quint8 packet[PREPARE_WRITE_HEADER_SIZE];
packet[0] = ATT_OP_PREPARE_WRITE_REQUEST;
- bt_put_unaligned(htobs(targetHandle), (quint16 *) &packet[1]); // attribute handle
- bt_put_unaligned(htobs(offset), (quint16 *) &packet[3]); // offset into newValue
+ putBtData(targetHandle, &packet[1]); // attribute handle
+ putBtData(offset, &packet[3]); // offset into newValue
qCDebug(QT_BT_BLUEZ) << "Writing long characteristic (prepare):"
<< hex << handle;
@@ -1774,7 +1774,7 @@ void QLowEnergyControllerPrivate::readCharacteristic(
quint8 packet[READ_REQUEST_HEADER_SIZE];
packet[0] = ATT_OP_READ_REQUEST;
- bt_put_unaligned(htobs(charDetails.valueHandle), (quint16 *) &packet[1]);
+ putBtData(charDetails.valueHandle, &packet[1]);
QByteArray data(READ_REQUEST_HEADER_SIZE, Qt::Uninitialized);
memcpy(data.data(), packet, READ_REQUEST_HEADER_SIZE);
@@ -1809,7 +1809,7 @@ void QLowEnergyControllerPrivate::readDescriptor(
quint8 packet[READ_REQUEST_HEADER_SIZE];
packet[0] = ATT_OP_READ_REQUEST;
- bt_put_unaligned(htobs(descriptorHandle), (quint16 *) &packet[1]);
+ putBtData(descriptorHandle, &packet[1]);
QByteArray data(READ_REQUEST_HEADER_SIZE, Qt::Uninitialized);
memcpy(data.data(), packet, READ_REQUEST_HEADER_SIZE);
@@ -2320,7 +2320,7 @@ void QLowEnergyControllerPrivate::writeCharacteristicForCentral(
packet[0] = ATT_OP_WRITE_COMMAND;
}
- bt_put_unaligned(htobs(valueHandle), (quint16 *) &packet[1]);
+ putBtData(valueHandle, &packet[1]);
QByteArray data(size, Qt::Uninitialized);
memcpy(data.data(), packet, WRITE_REQUEST_HEADER_SIZE);
@@ -2377,7 +2377,7 @@ void QLowEnergyControllerPrivate::writeDescriptorForCentral(
quint8 packet[WRITE_REQUEST_HEADER_SIZE];
packet[0] = ATT_OP_WRITE_REQUEST;
- bt_put_unaligned(htobs(descriptorHandle), (quint16 *) &packet[1]);
+ putBtData(descriptorHandle, &packet[1]);
const int size = WRITE_REQUEST_HEADER_SIZE + newValue.size();
QByteArray data(size, Qt::Uninitialized);