From f317a1e978a34555a5ab5b1dc4b99b783dfd0c58 Mon Sep 17 00:00:00 2001 From: Fabian Bumberger Date: Mon, 19 Nov 2012 19:24:07 +0100 Subject: QNX adding bluetooth service discovery agent Change-Id: I56ed0b2473318046fd34f156d49aac33ffcf5738 Reviewed-by: Alex --- src/bluetooth/bluetooth.pro | 2 +- src/bluetooth/qbluetoothservicediscoveryagent_p.h | 23 +++ .../qbluetoothservicediscoveryagent_qnx.cpp | 202 +++++++++++++++++++++ 3 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 src/bluetooth/qbluetoothservicediscoveryagent_qnx.cpp (limited to 'src/bluetooth') diff --git a/src/bluetooth/bluetooth.pro b/src/bluetooth/bluetooth.pro index 6e52a032..0ff968ac 100644 --- a/src/bluetooth/bluetooth.pro +++ b/src/bluetooth/bluetooth.pro @@ -88,7 +88,7 @@ config_bluez:contains(QT_CONFIG, dbus) { qbluetoothdevicediscoveryagent_qnx.cpp \ qbluetoothlocaldevice_qnx.cpp \ qbluetoothserviceinfo_p.cpp \ - qbluetoothservicediscoveryagent_p.cpp \ + qbluetoothservicediscoveryagent_qnx.cpp \ qbluetoothsocket_p.cpp \ ql2capserver_p.cpp \ qrfcommserver_p.cpp \ diff --git a/src/bluetooth/qbluetoothservicediscoveryagent_p.h b/src/bluetooth/qbluetoothservicediscoveryagent_p.h index 591b2a5f..e97e801d 100644 --- a/src/bluetooth/qbluetoothservicediscoveryagent_p.h +++ b/src/bluetooth/qbluetoothservicediscoveryagent_p.h @@ -59,6 +59,12 @@ class QXmlStreamReader; QT_END_NAMESPACE #endif +#ifdef QTM_QNX_BLUETOOTH +#include "qnx/ppshelpers_p.h" +#include +#include +#endif + QT_BEGIN_HEADER QTBLUETOOTH_BEGIN_NAMESPACE @@ -66,7 +72,13 @@ QTBLUETOOTH_BEGIN_NAMESPACE class QBluetoothDeviceDiscoveryAgent; class QBluetoothServiceDiscoveryAgentPrivate +#ifdef QTM_QNX_BLUETOOTH +: public QObject { + Q_OBJECT +#else +{ +#endif Q_DECLARE_PUBLIC(QBluetoothServiceDiscoveryAgent) public: @@ -107,6 +119,17 @@ private: QVariant readAttributeValue(QXmlStreamReader &xml); #endif +#ifdef QTM_QNX_BLUETOOTH +private Q_SLOTS: + void remoteDevicesChanged(int fd); + void controlReply(ppsResult result); + void controlEvent(ppsResult result); + +private: + int m_rdfd; + QSocketNotifier *rdNotifier; +#endif + public: QBluetoothServiceDiscoveryAgent::Error error; QString errorString; diff --git a/src/bluetooth/qbluetoothservicediscoveryagent_qnx.cpp b/src/bluetooth/qbluetoothservicediscoveryagent_qnx.cpp new file mode 100644 index 00000000..4eb5da05 --- /dev/null +++ b/src/bluetooth/qbluetoothservicediscoveryagent_qnx.cpp @@ -0,0 +1,202 @@ +/*************************************************************************** +** +** Copyright (C) 2012 Research In Motion +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qbluetoothservicediscoveryagent.h" +#include "qbluetoothservicediscoveryagent_p.h" + +#include "qbluetoothdeviceinfo.h" +#include "qbluetoothdevicediscoveryagent.h" + +#include +#include "qbluetoothuuid.h" + +#include + +#include + +QTBLUETOOTH_BEGIN_NAMESPACE + +QBluetoothServiceDiscoveryAgentPrivate::QBluetoothServiceDiscoveryAgentPrivate(const QBluetoothAddress &address) + : rdNotifier(0), error(QBluetoothServiceDiscoveryAgent::NoError), state(Inactive), deviceAddress(address), + deviceDiscoveryAgent(0), mode(QBluetoothServiceDiscoveryAgent::MinimalDiscovery) +{ +} + +QBluetoothServiceDiscoveryAgentPrivate::~QBluetoothServiceDiscoveryAgentPrivate() +{ +} + +void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &address) +{ + Q_Q(QBluetoothServiceDiscoveryAgent); + qBBBluetoothDebug() << "Starting Service discovery for" << address.toString(); + if ((m_rdfd = qt_safe_open((QByteArray("/pps/services/bluetooth/remote_devices/%1").append(address.toString().toUtf8().constData())).constData(), O_RDONLY)) == -1) { + qWarning() << "Failed to open " << "/pps/services/bluetooth/remote_devices/" << address.toString(); + } else { + if (rdNotifier) + delete rdNotifier; + rdNotifier = new QSocketNotifier(m_rdfd, QSocketNotifier::Read, this); + if (rdNotifier) { + connect(rdNotifier, SIGNAL(activated(int)), this, SLOT(remoteDevicesChanged(int))); + } else { + qWarning() << "Service Discovery: Failed to connect to rdNotifier"; + error = QBluetoothServiceDiscoveryAgent::DeviceDiscoveryError; + errorString = QStringLiteral("Failed to connect to rdNotifier"); + q->error(error); + return; + } + } + + ppsRegisterControl(); + ppsSendControlMessage("service_query", QStringLiteral("{\"addr\":\"%1\"}").arg(address.toString())); + ppsRegisterForEvent(QStringLiteral("service_query"), this); +} + +void QBluetoothServiceDiscoveryAgentPrivate::stop() +{ + if (rdNotifier) + delete rdNotifier; + rdNotifier = 0; + ppsUnregisterControl(); +} + +void QBluetoothServiceDiscoveryAgentPrivate::remoteDevicesChanged(int fd) +{ + pps_decoder_t ppsDecoder; + pps_decoder_initialize(&ppsDecoder, 0); + + QBluetoothAddress deviceAddr; + QString deviceName; + + if (!ppsReadRemoteDevice(fd, &ppsDecoder, &deviceAddr, &deviceName)) { + pps_decoder_cleanup(&ppsDecoder); + return; + } + + pps_decoder_push(&ppsDecoder, "available_services"); + + const char *next_service = 0; + for (int service_count=0; pps_decoder_get_string(&ppsDecoder, 0, &next_service ) == PPS_DECODER_OK; service_count++) { + if (next_service == 0) + break; + + qBBBluetoothDebug() << Q_FUNC_INFO << "Service" << next_service; + + QBluetoothServiceInfo serviceInfo; + serviceInfo.setDevice(discoveredDevices.at(0)); + + QBluetoothServiceInfo::Sequence protocolDescriptorList; + protocolDescriptorList << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)); + + bool ok; + QBluetoothUuid suuid(QByteArray(next_service).toUInt(&ok,16)); + if (!ok) { + QList serviceName = QByteArray(next_service).split(':'); + if (serviceName.size() == 2) { + serviceInfo.setServiceUuid(QBluetoothUuid(serviceName.last().toUInt())); + suuid = QBluetoothUuid((quint16)(serviceName.first().toUInt(&ok,16))); + if (suuid == QBluetoothUuid::SerialPort) + protocolDescriptorList << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)); + } + } else { + //We do not have anything better, so we set the service class UUID as service UUID + serviceInfo.setServiceUuid(suuid); + } + + serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); + + QList serviceClassId; + serviceClassId << suuid; + serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, QVariant::fromValue(serviceClassId)); + + serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, + QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); + serviceInfo.setDevice(discoveredDevices.at(0)); + + bool entryExists = false; + //Did we already discover this service? + foreach (QBluetoothServiceInfo sInfo, q_ptr->discoveredServices()) { + if (sInfo.device() == serviceInfo.device() + && sInfo.serviceUuid() == serviceInfo.serviceUuid() + && sInfo.serviceClassUuids() == serviceInfo.serviceClassUuids()) { + entryExists = true; + //qBBBluetoothDebug() << "Entry exists" << serviceInfo.serviceClassUuids().first() << sInfo.serviceClassUuids().first(); + break; + } + } + + if (!entryExists) { + qBBBluetoothDebug() << "Adding service" << next_service << " " << serviceInfo.socketProtocol(); + discoveredServices << serviceInfo; + q_ptr->serviceDiscovered(serviceInfo); + } + } + + pps_decoder_cleanup(&ppsDecoder); +} + +void QBluetoothServiceDiscoveryAgentPrivate::controlReply(ppsResult result) +{ + Q_Q(QBluetoothServiceDiscoveryAgent); + if (!result.errorMsg.isEmpty()) { + qWarning() << Q_FUNC_INFO << result.errorMsg; + errorString = result.errorMsg; + error = QBluetoothServiceDiscoveryAgent::DeviceDiscoveryError; + q->error(error); + } else { + _q_serviceDiscoveryFinished(); + } +} + +void QBluetoothServiceDiscoveryAgentPrivate::controlEvent(ppsResult result) +{ + Q_Q(QBluetoothServiceDiscoveryAgent); + if (!result.errorMsg.isEmpty()) { + qWarning() << Q_FUNC_INFO << result.errorMsg; + errorString = result.errorMsg; + error = QBluetoothServiceDiscoveryAgent::DeviceDiscoveryError; + q->error(error); + } else { + _q_serviceDiscoveryFinished(); + } +} + +QTBLUETOOTH_END_NAMESPACE -- cgit v1.2.3