diff options
author | Fabian Bumberger <fbumberger@rim.com> | 2013-08-02 18:09:20 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-08-29 14:06:54 +0200 |
commit | d8d903826daa8db49e437b76fb80693ab8feb01e (patch) | |
tree | 8e794636168a3955a5f33c8dbb5a51b11a285b2c /src/bluetooth | |
parent | bfc49d07b0638927aca3cf63238a80f98ee516a7 (diff) |
Merge QRfcommServer and QL2capServer to QBluetoothServer
Task-number: QTBUG-32722
Change-Id: Ia75ad5af2f81b737a8cca09dba919a7dc0fa0f8a
Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'src/bluetooth')
17 files changed, 448 insertions, 1103 deletions
diff --git a/src/bluetooth/bluetooth.pro b/src/bluetooth/bluetooth.pro index ebf9bcec..1415b851 100644 --- a/src/bluetooth/bluetooth.pro +++ b/src/bluetooth/bluetooth.pro @@ -17,8 +17,7 @@ PUBLIC_HEADERS += \ qbluetoothdevicediscoveryagent.h\ qbluetoothservicediscoveryagent.h\ qbluetoothsocket.h\ - qrfcommserver.h \ - ql2capserver.h \ + qbluetoothserver.h \ qbluetooth.h \ qbluetoothlocaldevice.h \ qbluetoothtransfermanager.h \ @@ -33,8 +32,7 @@ PRIVATE_HEADERS += \ qbluetoothdevicediscoveryagent_p.h\ qbluetoothservicediscoveryagent_p.h\ qbluetoothsocket_p.h\ - qrfcommserver_p.h \ - ql2capserver_p.h \ + qbluetoothserver_p.h\ qbluetoothtransferreply_p.h \ qbluetoothtransferrequest_p.h \ qprivatelinearbuffer_p.h \ @@ -49,8 +47,7 @@ SOURCES += \ qbluetoothdevicediscoveryagent.cpp\ qbluetoothservicediscoveryagent.cpp\ qbluetoothsocket.cpp\ - qrfcommserver.cpp \ - ql2capserver.cpp \ + qbluetoothserver.cpp \ qbluetoothlocaldevice.cpp \ qbluetooth.cpp \ qbluetoothtransfermanager.cpp \ @@ -71,11 +68,10 @@ config_bluez:qtHaveModule(dbus) { qbluetoothdevicediscoveryagent_bluez.cpp\ qbluetoothservicediscoveryagent_bluez.cpp \ qbluetoothsocket_bluez.cpp \ - qrfcommserver_bluez.cpp \ + qbluetoothserver_bluez.cpp \ qbluetoothlocaldevice_bluez.cpp \ qbluetoothtransferreply_bluez.cpp \ - qbluetoothtransfermanager_bluez.cpp \ - ql2capserver_bluez.cpp + qbluetoothtransfermanager_bluez.cpp } else:qnx{ DEFINES += QT_QNX_BLUETOOTH #BT_BBPPSDEBUG @@ -91,8 +87,7 @@ config_bluez:qtHaveModule(dbus) { qbluetoothserviceinfo_qnx.cpp \ qbluetoothservicediscoveryagent_qnx.cpp \ qbluetoothsocket_qnx.cpp \ - ql2capserver_p.cpp \ - qrfcommserver_qnx.cpp \ + qbluetoothserver_qnx.cpp \ qbluetoothtransfermanager_qnx.cpp \ qbluetoothtransferreply_qnx.cpp @@ -105,8 +100,7 @@ config_bluez:qtHaveModule(dbus) { qbluetoothserviceinfo_p.cpp \ qbluetoothservicediscoveryagent_p.cpp \ qbluetoothsocket_p.cpp \ - ql2capserver_p.cpp \ - qrfcommserver_p.cpp \ + qbluetoothserver_p.cpp \ qbluetoothtransfermanager_p.cpp } diff --git a/src/bluetooth/qrfcommserver.cpp b/src/bluetooth/qbluetoothserver.cpp index 53ab2791..88da60a7 100644 --- a/src/bluetooth/qrfcommserver.cpp +++ b/src/bluetooth/qbluetoothserver.cpp @@ -39,20 +39,20 @@ ** ****************************************************************************/ -#include "qrfcommserver.h" -#include "qrfcommserver_p.h" +#include "qbluetoothserver.h" +#include "qbluetoothserver_p.h" #include "qbluetoothsocket.h" #include "qbluetoothserviceinfo.h" QT_BEGIN_NAMESPACE_BLUETOOTH /*! - \class QRfcommServer + \class QBluetoothServer \inmodule QtBluetooth - \brief The QRfcommServer class uses the RFCOMM protocol to communicate with + \brief The QBluetoothServer class uses the RFCOMM or L2cap protocol to communicate with a Bluetooth device. - QRfcommServer is used to implement Bluetooth services over RFCOMM. + QBluetoothServer is used to implement Bluetooth services over RFCOMM or L2cap. Start listening for incoming connections with listen(). Wait till the newConnection() signal is emitted when a new connection is established, and call nextPendingConnection() to get a QBluetoothSocket @@ -60,13 +60,22 @@ QT_BEGIN_NAMESPACE_BLUETOOTH To enable other devices to find your service, create a QBluetoothServiceInfo with the applicable attributes for your service and register it using QBluetoothServiceInfo::registerService(). - Call serverPort() to get the RFCOMM channel number that is being used. + Call serverPort() to get the channel number that is being used. \sa QBluetoothServiceInfo, QBluetoothSocket */ /*! - \fn void QRfcommServer::newConnection() + \enum QBluetoothServer::ServerType + + This enum describes the Bluetooth server type. + + \value L2capServer L2CAP server. + \value RfcommServer RFCOMM server. +*/ + +/*! + \fn void QBluetoothServer::newConnection() This signal is emitted when a new connection is available. @@ -77,24 +86,24 @@ QT_BEGIN_NAMESPACE_BLUETOOTH */ /*! - \fn void QRfcommServer::close() + \fn void QBluetoothServer::close() Closes and resets the listening socket. */ /*! - \fn bool QRfcommServer::listen(const QBluetoothAddress &address, quint16 port) + \fn bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port) Start listening for incoming connections to \a address on \a port. - Returns true if the operation succeeded and the RFCOMM server is listening for + Returns true if the operation succeeded and the server is listening for incoming connections, otherwise returns false. \sa isListening(), newConnection() */ /*! - \fn void QRfcommServer::setMaxPendingConnections(int numConnections) + \fn void QBluetoothServer::setMaxPendingConnections(int numConnections) Sets the maximum number of pending connections to \a numConnections. @@ -102,48 +111,48 @@ QT_BEGIN_NAMESPACE_BLUETOOTH */ /*! - \fn bool QRfcommServer::hasPendingConnections() const + \fn bool QBluetoothServer::hasPendingConnections() const Returns true if a connection is pending, otherwise false. */ /*! - \fn QBluetoothSocket *QRfcommServer::nextPendingConnection() + \fn QBluetoothSocket *QBluetoothServer::nextPendingConnection() - Returns a pointer QBluetoothSocket for the next pending connection. It is the callers - responsibility to delete pointer. + Returns a pointer to aQBluetoothSocket for the next pending connection. It is the callers + responsibility to delete the pointer. */ /*! - \fn QBluetoothAddress QRfcommServer::serverAddress() const + \fn QBluetoothAddress QBluetoothServer::serverAddress() const Returns the server address. */ /*! - \fn quint16 QRfcommServer::serverPort() const + \fn quint16 QBluetoothServer::serverPort() const Returns the server port number. */ /*! - Constructs an RFCOMM server with \a parent. + Constructs a bluetooth server with \a parent and a ServerType. */ -QRfcommServer::QRfcommServer(QObject *parent) - : QObject(parent), d_ptr(new QRfcommServerPrivate) +QBluetoothServer::QBluetoothServer(ServerType serverType, QObject *parent) + : QObject(parent), d_ptr(new QBluetoothServerPrivate(serverType)) { d_ptr->q_ptr = this; } /*! - Destroys the RFCOMM server. + Destroys the bluetooth server. */ -QRfcommServer::~QRfcommServer() +QBluetoothServer::~QBluetoothServer() { delete d_ptr; } /*! - \fn QBluetoothServiceInfo QRfcommServer::listen(const QBluetoothUuid &uuid, const QString &serviceName) + \fn QBluetoothServiceInfo QBluetoothServer::listen(const QBluetoothUuid &uuid, const QString &serviceName) Convenience function for registering an SPP service with \a uuid and \a serviceName. Because this function already registers the service, the QBluetoothServiceInfo object @@ -152,14 +161,17 @@ QRfcommServer::~QRfcommServer() Returns a registered QBluetoothServiceInfo instance if sucessful otherwise an invalid QBluetoothServiceInfo. - This function is equivalent to following code snippet. + For an RFCOMM server this function is equivalent to following code snippet. - \snippet qrfcommserver.cpp listen + \snippet qbluetoothserver.cpp listen + \snippet qbluetoothserver.cpp listen2 + \snippet qbluetoothserver.cpp listen3 \sa isListening(), newConnection(), listen() */ -QBluetoothServiceInfo QRfcommServer::listen(const QBluetoothUuid &uuid, const QString &serviceName) +QBluetoothServiceInfo QBluetoothServer::listen(const QBluetoothUuid &uuid, const QString &serviceName) { + Q_D(const QBluetoothServer); if (!listen()) return QBluetoothServiceInfo(); //! [listen] @@ -181,24 +193,30 @@ QBluetoothServiceInfo QRfcommServer::listen(const QBluetoothUuid &uuid, const QS protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)); protocolDescriptorList.append(QVariant::fromValue(protocol)); protocol.clear(); +//! [listen] + if (d->serverType == RfcommServer) { +//! [listen2] protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) << QVariant::fromValue(quint8(serverPort())); +//! [listen2] + } +//! [listen3] protocolDescriptorList.append(QVariant::fromValue(protocol)); serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); bool result = serviceInfo.registerService(); -//! [listen] +//! [listen3] if (!result) return QBluetoothServiceInfo(); return serviceInfo; } /*! - Returns true if the RFCOMM server is listening for incoming connections, otherwise false. + Returns true if the server is listening for incoming connections, otherwise false. */ -bool QRfcommServer::isListening() const +bool QBluetoothServer::isListening() const { - Q_D(const QRfcommServer); + Q_D(const QBluetoothServer); #ifdef QT_QNX_BLUETOOTH if (!d->socket) @@ -213,23 +231,23 @@ bool QRfcommServer::isListening() const \sa setMaxPendingConnections() */ -int QRfcommServer::maxPendingConnections() const +int QBluetoothServer::maxPendingConnections() const { - Q_D(const QRfcommServer); + Q_D(const QBluetoothServer); return d->maxPendingConnections; } /*! - \fn QRfcommServer::setSecurityFlags(QBluetooth::SecurityFlags security) + \fn QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security) Sets the Bluetooth security flags to \a security. This function must be called before calling listen(). */ /*! - \fn QBluetooth::SecurityFlags QRfcommServer::securityFlags() const + \fn QBluetooth::SecurityFlags QBluetoothServer::securityFlags() const Returns the Bluetooth security flags. */ -#include "moc_qrfcommserver.cpp" +#include "moc_qbluetoothserver.cpp" QT_END_NAMESPACE_BLUETOOTH diff --git a/src/bluetooth/qrfcommserver.h b/src/bluetooth/qbluetoothserver.h index a4625208..a464fb85 100644 --- a/src/bluetooth/qrfcommserver.h +++ b/src/bluetooth/qbluetoothserver.h @@ -39,8 +39,8 @@ ** ****************************************************************************/ -#ifndef QRFCOMMSERVER_H -#define QRFCOMMSERVER_H +#ifndef QBLUETOOTHSERVER_H +#define QBLUETOOTHSERVER_H #include <QtBluetooth/qbluetoothglobal.h> @@ -53,16 +53,20 @@ QT_BEGIN_NAMESPACE_BLUETOOTH -class QRfcommServerPrivate; +class QBluetoothServerPrivate; class QBluetoothSocket; -class Q_BLUETOOTH_EXPORT QRfcommServer : public QObject +class Q_BLUETOOTH_EXPORT QBluetoothServer : public QObject { Q_OBJECT public: - QRfcommServer(QObject *parent = 0); - ~QRfcommServer(); + enum ServerType { + L2capServer = 0, + RfcommServer + }; + QBluetoothServer(ServerType serverType, QObject *parent = 0); + ~QBluetoothServer(); void close(); @@ -86,10 +90,10 @@ Q_SIGNALS: void newConnection(); protected: - QRfcommServerPrivate *d_ptr; + QBluetoothServerPrivate *d_ptr; private: - Q_DECLARE_PRIVATE(QRfcommServer) + Q_DECLARE_PRIVATE(QBluetoothServer) #ifdef QT_BLUEZ_BLUETOOTH Q_PRIVATE_SLOT(d_func(), void _q_newConnection()) diff --git a/src/bluetooth/qbluetoothserver_bluez.cpp b/src/bluetooth/qbluetoothserver_bluez.cpp new file mode 100644 index 00000000..82f9bc53 --- /dev/null +++ b/src/bluetooth/qbluetoothserver_bluez.cpp @@ -0,0 +1,315 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** 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 "qbluetoothserver.h" +#include "qbluetoothserver_p.h" +#include "qbluetoothsocket.h" + +#include <QtCore/QSocketNotifier> + +#include <QtCore/QDebug> + +#include <bluetooth/bluetooth.h> +#include <bluetooth/rfcomm.h> +#include <bluetooth/l2cap.h> + +#include <errno.h> + +QT_BEGIN_NAMESPACE_BLUETOOTH + +static inline void convertAddress(quint64 from, quint8 (&to)[6]) +{ + to[0] = (from >> 0) & 0xff; + to[1] = (from >> 8) & 0xff; + to[2] = (from >> 16) & 0xff; + to[3] = (from >> 24) & 0xff; + to[4] = (from >> 32) & 0xff; + to[5] = (from >> 40) & 0xff; +} + +QBluetoothServerPrivate::QBluetoothServerPrivate(QBluetoothServer::ServerType sType) + : maxPendingConnections(1), socketNotifier(0), serverType(sType) +{ + if (sType == QBluetoothServer::RfcommServer) + socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket); + else + socket = new QBluetoothSocket(QBluetoothSocket::L2capSocket); +} + +QBluetoothServerPrivate::~QBluetoothServerPrivate() +{ + delete socketNotifier; + + delete socket; +} + +void QBluetoothServerPrivate::_q_newConnection() +{ + // disable socket notifier until application calls nextPendingConnection(). + socketNotifier->setEnabled(false); + + emit q_ptr->newConnection(); +} + +void QBluetoothServer::close() +{ + Q_D(QBluetoothServer); + + delete d->socketNotifier; + d->socketNotifier = 0; + + d->socket->close(); +} + +bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port) +{ + Q_D(QBluetoothServer); + + int sock = d->socket->socketDescriptor(); + if (sock < 0) + return false; + + if (d->serverType == RfcommServer) { + sockaddr_rc addr; + + addr.rc_family = AF_BLUETOOTH; + addr.rc_channel = port; + + if (!address.isNull()) + convertAddress(address.toUInt64(), addr.rc_bdaddr.b); + else + convertAddress(Q_UINT64_C(0), addr.rc_bdaddr.b); + + + if (::bind(sock, reinterpret_cast<sockaddr *>(&addr), sizeof(sockaddr_rc)) < 0) + return false; + } else { + sockaddr_l2 addr; + + memset(&addr, 0, sizeof(sockaddr_l2)); + addr.l2_family = AF_BLUETOOTH; + addr.l2_psm = port; + + if (!address.isNull()) + convertAddress(address.toUInt64(), addr.l2_bdaddr.b); + else + convertAddress(Q_UINT64_C(0), addr.l2_bdaddr.b); + + if (::bind(sock, reinterpret_cast<sockaddr *>(&addr), sizeof(sockaddr_l2)) < 0) + return false; + } + + if (::listen(sock, d->maxPendingConnections) < 0) + return false; + + d->socket->setSocketState(QBluetoothSocket::ListeningState); + + if (!d->socketNotifier) { + d->socketNotifier = new QSocketNotifier(d->socket->socketDescriptor(), + QSocketNotifier::Read); + connect(d->socketNotifier, SIGNAL(activated(int)), this, SLOT(_q_newConnection())); + } + + return true; +} + +void QBluetoothServer::setMaxPendingConnections(int numConnections) +{ + Q_D(QBluetoothServer); + + if (d->socket->state() == QBluetoothSocket::UnconnectedState) + d->maxPendingConnections = numConnections; +} + +bool QBluetoothServer::hasPendingConnections() const +{ + Q_D(const QBluetoothServer); + + if (!d || !d->socketNotifier) + return false; + + // if the socket notifier is disable there is a pending connection waiting for us to accept. + return !d->socketNotifier->isEnabled(); +} + +QBluetoothSocket *QBluetoothServer::nextPendingConnection() +{ + Q_D(QBluetoothServer); + + if (!hasPendingConnections()) + return 0; + + int pending; + if (d->serverType == RfcommServer) { + sockaddr_rc addr; + socklen_t length = sizeof(sockaddr_rc); + pending = ::accept(d->socket->socketDescriptor(), + reinterpret_cast<sockaddr *>(&addr), &length); + } else { + sockaddr_l2 addr; + socklen_t length = sizeof(sockaddr_l2); + pending = ::accept(d->socket->socketDescriptor(), + reinterpret_cast<sockaddr *>(&addr), &length); + } + + if (pending >= 0) { + QBluetoothSocket *newSocket = new QBluetoothSocket; + if (d->serverType == RfcommServer) + newSocket->setSocketDescriptor(pending, QBluetoothSocket::RfcommSocket); + else + newSocket->setSocketDescriptor(pending, QBluetoothSocket::L2capSocket); + + d->socketNotifier->setEnabled(true); + + return newSocket; + } else { + d->socketNotifier->setEnabled(true); + } + + return 0; +} + +QBluetoothAddress QBluetoothServer::serverAddress() const +{ + Q_D(const QBluetoothServer); + + return d->socket->localAddress(); +} + +quint16 QBluetoothServer::serverPort() const +{ + Q_D(const QBluetoothServer); + + return d->socket->localPort(); +} + +void QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security) +{ + Q_D(QBluetoothServer); + + int lm = 0; + if (security == QBluetooth::NoSecurity) + lm = 0; + + if (d->serverType == RfcommServer) { + if (security.testFlag(QBluetooth::Authorization)) + lm |= RFCOMM_LM_AUTH; + if (security.testFlag(QBluetooth::Authentication)) + lm |= RFCOMM_LM_TRUSTED; + if (security.testFlag(QBluetooth::Encryption)) + lm |= RFCOMM_LM_ENCRYPT; + if (security.testFlag(QBluetooth::Secure)) + lm |= RFCOMM_LM_SECURE; + + qDebug() << hex << "Setting lm to" << lm << security; + + if (setsockopt(d->socket->socketDescriptor(), SOL_RFCOMM, RFCOMM_LM, &lm, sizeof(lm)) < 0){ + qWarning() << "Failed to set socket option, closing socket for safety" << errno; + qWarning() << "Error: " << strerror(errno); + d->socket->close(); + } + } else { + if (security.testFlag(QBluetooth::Authorization)) + lm |= L2CAP_LM_AUTH; + if (security.testFlag(QBluetooth::Authentication)) + lm |= L2CAP_LM_TRUSTED; + if (security.testFlag(QBluetooth::Encryption)) + lm |= L2CAP_LM_ENCRYPT; + if (security.testFlag(QBluetooth::Secure)) + lm |= L2CAP_LM_SECURE; + + if (setsockopt(d->socket->socketDescriptor(), SOL_L2CAP, L2CAP_LM, &lm, sizeof(lm)) < 0){ + qWarning() << "Failed to set socket option, closing socket for safety" << errno; + qWarning() << "Error: " << strerror(errno); + d->socket->close(); + } + } + +} + +QBluetooth::SecurityFlags QBluetoothServer::securityFlags() const +{ + Q_D(const QBluetoothServer); + + int lm = 0; + int len = sizeof(lm); + int security = QBluetooth::NoSecurity; + + if (d->serverType == RfcommServer) { + if (getsockopt(d->socket->socketDescriptor(), SOL_RFCOMM, RFCOMM_LM, &lm, (socklen_t *)&len) < 0) { + qWarning() << "Failed to get security flags" << strerror(errno); + return QBluetooth::NoSecurity; + } + + if (lm & RFCOMM_LM_SECURE) + security |= QBluetooth::Secure; + + if (lm & RFCOMM_LM_ENCRYPT) + security |= QBluetooth::Encryption; + + if (lm & RFCOMM_LM_TRUSTED) + security |= QBluetooth::Authentication; + + if (lm & RFCOMM_LM_AUTH) + security |= QBluetooth::Authorization; + } else { + if (getsockopt(d->socket->socketDescriptor(), SOL_L2CAP, L2CAP_LM, &lm, (socklen_t *)&len) < 0) { + qWarning() << "Failed to get security flags" << strerror(errno); + return QBluetooth::NoSecurity; + } + + if (lm & L2CAP_LM_SECURE) + security |= QBluetooth::Secure; + + if (lm & L2CAP_LM_ENCRYPT) + security |= QBluetooth::Encryption; + + if (lm & L2CAP_LM_TRUSTED) + security |= QBluetooth::Authentication; + + if (lm & L2CAP_LM_AUTH) + security |= QBluetooth::Authorization; + } + + return static_cast<QBluetooth::Security>(security); +} + +QT_END_NAMESPACE_BLUETOOTH diff --git a/src/bluetooth/qrfcommserver_p.cpp b/src/bluetooth/qbluetoothserver_p.cpp index 9e71e86e..b502889f 100644 --- a/src/bluetooth/qrfcommserver_p.cpp +++ b/src/bluetooth/qbluetoothserver_p.cpp @@ -39,69 +39,63 @@ ** ****************************************************************************/ -#include "qrfcommserver.h" -#include "qrfcommserver_p.h" +#include "qbluetoothserver.h" +#include "qbluetoothserver_p.h" #include "qbluetoothsocket.h" QT_BEGIN_NAMESPACE_BLUETOOTH -QRfcommServerPrivate::QRfcommServerPrivate() +QBluetoothServerPrivate::QBluetoothServerPrivate(QBluetoothServer::ServerType sType) + : serverType(sType) { } -QRfcommServerPrivate::~QRfcommServerPrivate() +QBluetoothServerPrivate::~QBluetoothServerPrivate() { } -void QRfcommServer::close() +void QBluetoothServer::close() { } -bool QRfcommServer::listen(const QBluetoothAddress &address, quint16 port) +bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port) { Q_UNUSED(address); Q_UNUSED(port); return false; } -void QRfcommServer::setMaxPendingConnections(int numConnections) +void QBluetoothServer::setMaxPendingConnections(int numConnections) { Q_UNUSED(numConnections); } -bool QRfcommServer::hasPendingConnections() const +bool QBluetoothServer::hasPendingConnections() const { return false; } -QBluetoothSocket *QRfcommServer::nextPendingConnection() +QBluetoothSocket *QBluetoothServer::nextPendingConnection() { return 0; } -QBluetoothAddress QRfcommServer::serverAddress() const +QBluetoothAddress QBluetoothServer::serverAddress() const { return QBluetoothAddress(); } -quint16 QRfcommServer::serverPort() const +quint16 QBluetoothServer::serverPort() const { return 0; } - -#ifdef QT_BLUEZ_BLUETOOTH -void QRfcommServerPrivate::_q_newConnection() -{ -} -#endif - -void QRfcommServer::setSecurityFlags(QBluetooth::SecurityFlags security) +void QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security) { Q_UNUSED(security); } -QBluetooth::SecurityFlags QRfcommServer::securityFlags() const +QBluetooth::SecurityFlags QBluetoothServer::securityFlags() const { return QBluetooth::NoSecurity; } diff --git a/src/bluetooth/qrfcommserver_p.h b/src/bluetooth/qbluetoothserver_p.h index 44c7b22a..a834e9ba 100644 --- a/src/bluetooth/qrfcommserver_p.h +++ b/src/bluetooth/qbluetoothserver_p.h @@ -39,12 +39,13 @@ ** ****************************************************************************/ -#ifndef QRFCOMMSERVER_P_H -#define QRFCOMMSERVER_P_H +#ifndef QBLUETOOTHSERVER_P_H +#define QBLUETOOTHSERVER_P_H #include <QtGlobal> #include <QList> #include <QtBluetooth/QBluetoothSocket> +#include "qbluetoothserver.h" #include "qbluetooth.h" #ifdef QT_QNX_BLUETOOTH @@ -60,9 +61,9 @@ QT_BEGIN_NAMESPACE_BLUETOOTH class QBluetoothAddress; class QBluetoothSocket; -class QRfcommServer; +class QBluetoothServer; -class QRfcommServerPrivate +class QBluetoothServerPrivate #ifdef QT_QNX_BLUETOOTH : public QObject { @@ -70,11 +71,11 @@ class QRfcommServerPrivate #else { #endif - Q_DECLARE_PUBLIC(QRfcommServer) + Q_DECLARE_PUBLIC(QBluetoothServer) public: - QRfcommServerPrivate(); - ~QRfcommServerPrivate(); + QBluetoothServerPrivate(QBluetoothServer::ServerType serverType); + ~QBluetoothServerPrivate(); #ifdef QT_BLUEZ_BLUETOOTH void _q_newConnection(); @@ -85,6 +86,7 @@ public: int maxPendingConnections; QBluetooth::SecurityFlags securityFlags; + QBluetoothServer::ServerType serverType; #ifdef QT_QNX_BLUETOOTH QList<QBluetoothSocket *> activeSockets; @@ -92,7 +94,7 @@ public: #endif protected: - QRfcommServer *q_ptr; + QBluetoothServer *q_ptr; private: #ifdef QT_QNX_BLUETOOTH diff --git a/src/bluetooth/qrfcommserver_qnx.cpp b/src/bluetooth/qbluetoothserver_qnx.cpp index 3196341d..a7c7f349 100644 --- a/src/bluetooth/qrfcommserver_qnx.cpp +++ b/src/bluetooth/qbluetoothserver_qnx.cpp @@ -39,8 +39,8 @@ ** ****************************************************************************/ -#include "qrfcommserver.h" -#include "qrfcommserver_p.h" +#include "qbluetoothserver.h" +#include "qbluetoothserver_p.h" #include "qbluetoothsocket.h" #include "qbluetoothsocket_p.h" #include "qbluetoothlocaldevice.h" @@ -51,25 +51,25 @@ QT_BEGIN_NAMESPACE_BLUETOOTH -extern QHash<QRfcommServerPrivate*, int> __fakeServerPorts; +extern QHash<QBluetoothServerPrivate*, int> __fakeServerPorts; -QRfcommServerPrivate::QRfcommServerPrivate() - : socket(0),maxPendingConnections(1),securityFlags(QBluetooth::NoSecurity) +QBluetoothServerPrivate::QBluetoothServerPrivate(QBluetoothServer::ServerType sType) + : socket(0),maxPendingConnections(1),securityFlags(QBluetooth::NoSecurity), serverType(sType) { ppsRegisterControl(); } -QRfcommServerPrivate::~QRfcommServerPrivate() +QBluetoothServerPrivate::~QBluetoothServerPrivate() { - Q_Q(QRfcommServer); + Q_Q(QBluetoothServer); q->close(); __fakeServerPorts.remove(this); ppsUnregisterControl(this); } -void QRfcommServerPrivate::controlReply(ppsResult result) +void QBluetoothServerPrivate::controlReply(ppsResult result) { - Q_Q(QRfcommServer); + Q_Q(QBluetoothServer); if (result.msg == QStringLiteral("register_server")) { qBBBluetoothDebug() << "SPP: Server registration succesfull"; @@ -95,7 +95,7 @@ void QRfcommServerPrivate::controlReply(ppsResult result) } } -void QRfcommServerPrivate::controlEvent(ppsResult result) +void QBluetoothServerPrivate::controlEvent(ppsResult result) { if (result.msg == QStringLiteral("service_connected")) { qBBBluetoothDebug() << "SPP: Server: Sending request for mount point path"; @@ -118,9 +118,9 @@ void QRfcommServerPrivate::controlEvent(ppsResult result) } } -void QRfcommServer::close() +void QBluetoothServer::close() { - Q_D(QRfcommServer); + Q_D(QBluetoothServer); if (!d->socket) { // there is no way to propagate the error to user // so just ignore the problem ;) @@ -134,10 +134,10 @@ void QRfcommServer::close() qApp->processEvents(QEventLoop::ExcludeUserInputEvents); } -bool QRfcommServer::listen(const QBluetoothAddress &address, quint16 port) +bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port) { Q_UNUSED(address) - Q_D(QRfcommServer); + Q_D(QBluetoothServer); // listen has already been called before if (d->socket && d->socket->state() == QBluetoothSocket::ListeningState) return true; @@ -170,52 +170,52 @@ bool QRfcommServer::listen(const QBluetoothAddress &address, quint16 port) return true; } -void QRfcommServer::setMaxPendingConnections(int numConnections) +void QBluetoothServer::setMaxPendingConnections(int numConnections) { - Q_D(QRfcommServer); + Q_D(QBluetoothServer); d->maxPendingConnections = numConnections; //Currently not used } -QBluetoothAddress QRfcommServer::serverAddress() const +QBluetoothAddress QBluetoothServer::serverAddress() const { - Q_D(const QRfcommServer); + Q_D(const QBluetoothServer); if (d->socket) return d->socket->localAddress(); else return QBluetoothAddress(); } -quint16 QRfcommServer::serverPort() const +quint16 QBluetoothServer::serverPort() const { //Currently we do not have access to the port - Q_D(const QRfcommServer); - return __fakeServerPorts.value((QRfcommServerPrivate*)d); + Q_D(const QBluetoothServer); + return __fakeServerPorts.value((QBluetoothServerPrivate*)d); } -bool QRfcommServer::hasPendingConnections() const +bool QBluetoothServer::hasPendingConnections() const { - Q_D(const QRfcommServer); + Q_D(const QBluetoothServer); return !d->activeSockets.isEmpty(); } -QBluetoothSocket *QRfcommServer::nextPendingConnection() +QBluetoothSocket *QBluetoothServer::nextPendingConnection() { - Q_D(QRfcommServer); + Q_D(QBluetoothServer); if (d->activeSockets.isEmpty()) return 0; return d->activeSockets.takeFirst(); } -void QRfcommServer::setSecurityFlags(QBluetooth::SecurityFlags security) +void QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security) { - Q_D(QRfcommServer); + Q_D(QBluetoothServer); d->securityFlags = security; //not used } -QBluetooth::SecurityFlags QRfcommServer::securityFlags() const +QBluetooth::SecurityFlags QBluetoothServer::securityFlags() const { - Q_D(const QRfcommServer); + Q_D(const QBluetoothServer); return d->securityFlags; //not used } diff --git a/src/bluetooth/qbluetoothserviceinfo_qnx.cpp b/src/bluetooth/qbluetoothserviceinfo_qnx.cpp index b90a570d..72c4e499 100644 --- a/src/bluetooth/qbluetoothserviceinfo_qnx.cpp +++ b/src/bluetooth/qbluetoothserviceinfo_qnx.cpp @@ -42,8 +42,8 @@ #include "qbluetoothserviceinfo.h" #include "qbluetoothserviceinfo_p.h" -#include "qrfcommserver_p.h" -#include "qrfcommserver.h" +#include "qbluetoothserver_p.h" +#include "qbluetoothserver.h" QT_BEGIN_NAMESPACE_BLUETOOTH @@ -84,7 +84,7 @@ void QBluetoothServiceInfoPrivate::removeRegisteredAttribute(quint16 attributeId registered = false; } -extern QHash<QRfcommServerPrivate*, int> __fakeServerPorts; +extern QHash<QBluetoothServerPrivate*, int> __fakeServerPorts; bool QBluetoothServiceInfoPrivate::registerService() const { diff --git a/src/bluetooth/qbluetoothsocket.cpp b/src/bluetooth/qbluetoothsocket.cpp index 3fa96851..380cb7b9 100644 --- a/src/bluetooth/qbluetoothsocket.cpp +++ b/src/bluetooth/qbluetoothsocket.cpp @@ -56,7 +56,7 @@ QT_BEGIN_NAMESPACE_BLUETOOTH \class QBluetoothSocket \inmodule QtBluetooth \brief The QBluetoothSocket class enables connection to a Bluetooth device - running an RFCOMM server. + running a bluetooth server. QBluetoothSocket supports two socket types, \l {QBluetoothSocket::L2capSocket}{L2CAP} and \l {QBluetoothSocket::RfcommSocket}{RFCOMM}. diff --git a/src/bluetooth/qbluetoothsocket.h b/src/bluetooth/qbluetoothsocket.h index 500ea2ad..adf8a22a 100644 --- a/src/bluetooth/qbluetoothsocket.h +++ b/src/bluetooth/qbluetoothsocket.h @@ -60,10 +60,8 @@ class Q_BLUETOOTH_EXPORT QBluetoothSocket : public QIODevice Q_OBJECT Q_DECLARE_PRIVATE(QBluetoothSocket) - friend class QRfcommServer; - friend class QRfcommServerPrivate; - friend class QL2capServer; - friend class QL2capServerPrivate; + friend class QBluetoothServer; + friend class QBluetoothServerPrivate; public: enum SocketType { diff --git a/src/bluetooth/ql2capserver.cpp b/src/bluetooth/ql2capserver.cpp deleted file mode 100644 index 4557f07b..00000000 --- a/src/bluetooth/ql2capserver.cpp +++ /dev/null @@ -1,180 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** 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 "ql2capserver.h" -#include "ql2capserver_p.h" -#include "qbluetoothsocket.h" - -QT_BEGIN_NAMESPACE_BLUETOOTH - -/*! - \class QL2capServer - \inmodule QtBluetooth - \brief The QL2capServer class allows you to connect to a service using the - L2CAP protocol. - - QL2capServer is used to implement Bluetooth services over L2CAP. - - Start listening for incoming connections with listen(). Wait till the newConnection() signal - is emitted when the connection is established, and call nextPendingConnection() to get a QBluetoothSocket - for the connection. - - To enable other devices to find your service, create a QBluetoothServiceInfo with the applicable - attributes for your service and register it using QBluetoothServiceInfo::registerService(). Call - serverPort() to get the L2CAP port number that is being used. - - \sa QBluetoothServiceInfo, QBluetoothSocket -*/ - -/*! - \fn void QL2capServer::newConnection() - - This signal is emitted when a new connection is available. - - The connected slot should call nextPendingConnection() to get a QBluetoothSocket object to send - and receive data over the connection. - - \sa nextPendingConnection(), hasPendingConnections() -*/ - -/*! - \fn void QL2capServer::close() - - Closes and resets the listening socket. -*/ - -/*! - \fn bool QL2capServer::listen(const QBluetoothAddress &address, quint16 port) - - Start listening for incoming connections to \a address on \a port. - - Returns true if the operation succeeded and the L2CAP server is listening for incoming - connections, otherwise returns false. - - \sa isListening(), newConnection() -*/ - -/*! - \fn void QL2capServer::setMaxPendingConnections(int numConnections) - - Sets the maximum number of pending connections to \a numConnections. - - \sa maxPendingConnections() -*/ - -/*! - \fn bool QL2capServer::hasPendingConnections() const - - Returns true if a connection is pending, otherwise false. -*/ - -/*! - \fn QBluetoothSocket *QL2capServer::nextPendingConnection() - - Returns a pointer to a QBluetoothSocket for the next pending connection. It is the callers - responsibility to delete the pointer. -*/ - -/*! - \fn QBluetoothAddress QL2capServer::serverAddress() const - - Returns the server address. -*/ - -/*! - \fn quint16 QL2capServer::serverPort() const - - Returns the server port number. -*/ - -/*! - Constructs an L2CAP server with \a parent. -*/ -QL2capServer::QL2capServer(QObject *parent) -: QObject(parent), d_ptr(new QL2capServerPrivate) -{ - d_ptr->q_ptr = this; -} - -/*! - Destroys the L2CAP server. -*/ -QL2capServer::~QL2capServer() -{ - delete d_ptr; -} - -/*! - Returns true if the L2CAP server is listening for incoming connections, otherwise false. -*/ -bool QL2capServer::isListening() const -{ - Q_D(const QL2capServer); - - return d->socket->state() == QBluetoothSocket::ListeningState; -} - -/*! - Returns the maximum number of pending connections. - - \sa setMaxPendingConnections() -*/ -int QL2capServer::maxPendingConnections() const -{ - Q_D(const QL2capServer); - - return d->maxPendingConnections; -} - -/*! - \fn void QL2capServer::setSecurityFlags(QBluetooth::SecurityFlags security) - Sets the Bluetooth security flags to \a security. This function must be called before calling listen(). -*/ - -/*! - \fn QBluetooth::SecurityFlags QL2capServer::securityFlags() const - - Returns the Bluetooth security flags. -*/ - -#include "moc_ql2capserver.cpp" - -QT_END_NAMESPACE_BLUETOOTH diff --git a/src/bluetooth/ql2capserver.h b/src/bluetooth/ql2capserver.h deleted file mode 100644 index 7f9a8150..00000000 --- a/src/bluetooth/ql2capserver.h +++ /dev/null @@ -1,100 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** 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$ -** -****************************************************************************/ - -#ifndef QL2CAPSERVER_H -#define QL2CAPSERVER_H - -#include <QtBluetooth/qbluetoothglobal.h> - -#include <QObject> - -#include <QtBluetooth/QBluetoothAddress> -#include <QtBluetooth/qbluetooth.h> -#include <QtBluetooth/QBluetoothSocket> - -QT_BEGIN_NAMESPACE_BLUETOOTH - -class QL2capServerPrivate; -class QBluetoothSocket; - -class Q_BLUETOOTH_EXPORT QL2capServer : public QObject -{ - Q_OBJECT - -public: - QL2capServer(QObject *parent = 0); - ~QL2capServer(); - - void close(); - - bool listen(const QBluetoothAddress &address = QBluetoothAddress(), quint16 port = 0); - bool isListening() const; - - void setMaxPendingConnections(int numConnections); - int maxPendingConnections() const; - - bool hasPendingConnections() const; - QBluetoothSocket *nextPendingConnection(); - - QBluetoothAddress serverAddress() const; - quint16 serverPort() const; - - void setSecurityFlags(QBluetooth::SecurityFlags security); - QBluetooth::SecurityFlags securityFlags() const; - -Q_SIGNALS: - void newConnection(); - -protected: - QL2capServerPrivate *d_ptr; - -private: - Q_DECLARE_PRIVATE(QL2capServer) - -#ifdef QT_BLUEZ_BLUETOOTH - Q_PRIVATE_SLOT(d_func(), void _q_newConnection()) -#endif - -}; - -QT_END_NAMESPACE_BLUETOOTH - -#endif diff --git a/src/bluetooth/ql2capserver_bluez.cpp b/src/bluetooth/ql2capserver_bluez.cpp deleted file mode 100644 index 70dd2222..00000000 --- a/src/bluetooth/ql2capserver_bluez.cpp +++ /dev/null @@ -1,248 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** 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 "ql2capserver.h" -#include "ql2capserver_p.h" -#include "qbluetoothsocket.h" - -#include <QtCore/QSocketNotifier> - -#include <QtCore/QDebug> - -#include <bluetooth/bluetooth.h> -#include <bluetooth/l2cap.h> - -#include <errno.h> - -QT_BEGIN_NAMESPACE_BLUETOOTH - -static inline void convertAddress(quint64 from, quint8 (&to)[6]) -{ - to[0] = (from >> 0) & 0xff; - to[1] = (from >> 8) & 0xff; - to[2] = (from >> 16) & 0xff; - to[3] = (from >> 24) & 0xff; - to[4] = (from >> 32) & 0xff; - to[5] = (from >> 40) & 0xff; -} - -QL2capServerPrivate::QL2capServerPrivate() -: pending(false), maxPendingConnections(1), socketNotifier(0) -{ - socket = new QBluetoothSocket(QBluetoothSocket::L2capSocket); -} - -QL2capServerPrivate::~QL2capServerPrivate() -{ - qDebug() << "Deleted"; - delete socketNotifier; - - delete socket; -} - -void QL2capServerPrivate::_q_newConnection() -{ - // disable socket notifier until application calls nextPendingConnection(). - socketNotifier->setEnabled(false); - - emit q_ptr->newConnection(); -} - -void QL2capServer::close() -{ - Q_D(QL2capServer); - - d->socket->close(); -} - -bool QL2capServer::listen(const QBluetoothAddress &address, quint16 port) -{ - Q_D(QL2capServer); - - int sock = d->socket->socketDescriptor(); - if (sock < 0) - return false; - - sockaddr_l2 addr; - - memset(&addr, 0, sizeof(sockaddr_l2)); - addr.l2_family = AF_BLUETOOTH; - addr.l2_psm = port; - - if (!address.isNull()) - convertAddress(address.toUInt64(), addr.l2_bdaddr.b); - else - convertAddress(Q_UINT64_C(0), addr.l2_bdaddr.b); - - if (::bind(sock, reinterpret_cast<sockaddr *>(&addr), sizeof(sockaddr_l2)) < 0) - return false; - - if (::listen(sock, d->maxPendingConnections) < 0) - return false; - - d->socket->setSocketState(QBluetoothSocket::ListeningState); - - delete d->socketNotifier; - - d->socketNotifier = new QSocketNotifier(d->socket->socketDescriptor(), - QSocketNotifier::Read); - connect(d->socketNotifier, SIGNAL(activated(int)), this, SLOT(_q_newConnection())); - - return true; -} - -void QL2capServer::setMaxPendingConnections(int numConnections) -{ - Q_D(QL2capServer); - - if (d->socket->state() == QBluetoothSocket::UnconnectedState) - d->maxPendingConnections = numConnections; -} - -bool QL2capServer::hasPendingConnections() const -{ - Q_D(const QL2capServer); - - if (!d || !d->socketNotifier) - return false; - - // if the socket notifier is disable there is a pending connection waiting for us to accept. - return !d->socketNotifier->isEnabled(); -} - -QBluetoothSocket *QL2capServer::nextPendingConnection() -{ - Q_D(QL2capServer); - - if (!hasPendingConnections()) - return 0; - - sockaddr_l2 addr; - socklen_t length = sizeof(sockaddr_l2); - - int pending = ::accept(d->socket->socketDescriptor(), - reinterpret_cast<sockaddr *>(&addr), &length); - if (pending >= 0) { - QBluetoothSocket *newSocket = new QBluetoothSocket; - newSocket->setSocketDescriptor(pending, QBluetoothSocket::L2capSocket); - - d->socketNotifier->setEnabled(true); - - return newSocket; - } else { -// qDebug() << "Hmm, could have sworn there was a connection waiting to be accepted!" << errno; - d->socketNotifier->setEnabled(true); - } - - return 0; -} - -QBluetoothAddress QL2capServer::serverAddress() const -{ - Q_D(const QL2capServer); - - return d->socket->localAddress(); -} - -quint16 QL2capServer::serverPort() const -{ - Q_D(const QL2capServer); - - return d->socket->localPort(); -} - -void QL2capServer::setSecurityFlags(QBluetooth::SecurityFlags security) -{ - Q_D(QL2capServer); - - int lm = 0; - if(security == QBluetooth::NoSecurity){ - lm = 0; - } - if(security.testFlag(QBluetooth::Authorization)){ - lm |= L2CAP_LM_AUTH; - } - if(security.testFlag(QBluetooth::Authentication)){ - lm |= L2CAP_LM_TRUSTED; - } - if(security.testFlag(QBluetooth::Encryption)){ - lm |= L2CAP_LM_ENCRYPT; - } - if(security.testFlag(QBluetooth::Secure)){ - lm |= L2CAP_LM_SECURE; - } - - if(setsockopt(d->socket->socketDescriptor(), SOL_L2CAP, L2CAP_LM, &lm, sizeof(lm)) < 0){ - qWarning() << "Failed to set socket option, closing socket for safety" << errno; - qWarning() << "Error: " << strerror(errno); - d->socket->close(); - } -} - -QBluetooth::SecurityFlags QL2capServer::securityFlags() const -{ - Q_D(const QL2capServer); - - int lm = 0; - int len = sizeof(lm); - int security = QBluetooth::NoSecurity; - - if(getsockopt(d->socket->socketDescriptor(), SOL_L2CAP, L2CAP_LM, &lm, (socklen_t *)&len) < 0) { - qWarning() << "Failed to get security flags" << strerror(errno); - return QBluetooth::NoSecurity; - } - - if(lm & L2CAP_LM_SECURE) - security |= QBluetooth::Secure; - - if(lm & L2CAP_LM_ENCRYPT) - security |= QBluetooth::Encryption; - - if(lm & L2CAP_LM_TRUSTED) - security |= QBluetooth::Authentication; - - if(lm & L2CAP_LM_AUTH) - security |= QBluetooth::Authorization; - - return static_cast<QBluetooth::SecurityFlags>(security); -} - -QT_END_NAMESPACE_BLUETOOTH diff --git a/src/bluetooth/ql2capserver_p.cpp b/src/bluetooth/ql2capserver_p.cpp deleted file mode 100644 index 47487a28..00000000 --- a/src/bluetooth/ql2capserver_p.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** 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 "ql2capserver.h" -#include "ql2capserver_p.h" -#include "qbluetoothsocket.h" - -QT_BEGIN_NAMESPACE_BLUETOOTH - -QL2capServerPrivate::QL2capServerPrivate() -{ -} - -QL2capServerPrivate::~QL2capServerPrivate() -{ -} - -void QL2capServer::close() -{ -} - -bool QL2capServer::listen(const QBluetoothAddress &address, quint16 port) -{ - Q_UNUSED(address); - Q_UNUSED(port); - - return false; -} - -void QL2capServer::setMaxPendingConnections(int numConnections) -{ - Q_UNUSED(numConnections); -} - -bool QL2capServer::hasPendingConnections() const -{ - return false; -} - -QBluetoothSocket *QL2capServer::nextPendingConnection() -{ - return 0; -} - -QBluetoothAddress QL2capServer::serverAddress() const -{ - return QBluetoothAddress(); -} - -quint16 QL2capServer::serverPort() const -{ - return 0; -} - -void QL2capServer::setSecurityFlags(QBluetooth::SecurityFlags security) -{ - Q_UNUSED(security); -} - -QBluetooth::SecurityFlags QL2capServer::securityFlags() const -{ - return QBluetooth::NoSecurity; -} - - - -#ifdef QT_BLUEZ_BLUETOOTH -void QL2capServerPrivate::_q_newConnection() -{ -} -#endif - -QT_END_NAMESPACE_BLUETOOTH diff --git a/src/bluetooth/ql2capserver_p.h b/src/bluetooth/ql2capserver_p.h deleted file mode 100644 index e2dcf279..00000000 --- a/src/bluetooth/ql2capserver_p.h +++ /dev/null @@ -1,90 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** 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$ -** -****************************************************************************/ - -#ifndef QL2CAPSERVER_P_H -#define QL2CAPSERVER_P_H - -#include <QtBluetooth/qbluetoothglobal.h> -#include <QtBluetooth/QBluetoothSocket> - -#ifdef QT_BLUEZ_BLUETOOTH -QT_FORWARD_DECLARE_CLASS(QSocketNotifier) -#endif - -QT_BEGIN_NAMESPACE_BLUETOOTH - -class QBluetoothAddress; -class QBluetoothSocket; - -class QL2capServer; - -class QL2capServerPrivate -{ - Q_DECLARE_PUBLIC(QL2capServer) - -public: - QL2capServerPrivate(); - ~QL2capServerPrivate(); - -#ifdef QT_BLUEZ_BLUETOOTH - void _q_newConnection(); -#endif - - -public: - QBluetoothSocket *socket; - bool pending; - - int maxPendingConnections; - QBluetooth::SecurityFlags securityFlags; - -protected: - QL2capServer *q_ptr; - -private: -#ifdef QT_BLUEZ_BLUETOOTH - QSocketNotifier *socketNotifier; -#endif -}; - -QT_END_NAMESPACE_BLUETOOTH - -#endif diff --git a/src/bluetooth/qnx/ppshelpers.cpp b/src/bluetooth/qnx/ppshelpers.cpp index 0028ba95..6f6ba344 100644 --- a/src/bluetooth/qnx/ppshelpers.cpp +++ b/src/bluetooth/qnx/ppshelpers.cpp @@ -42,7 +42,7 @@ #include "ppshelpers_p.h" #include <QtCore/private/qcore_unix_p.h> #include <QDebug> -#include "../qrfcommserver_p.h" +#include "../qbluetoothserver_p.h" #include <QTimer> QT_BEGIN_NAMESPACE_BLUETOOTH @@ -66,7 +66,7 @@ static QList<QPair<int, QObject*> > waitingCtrlMsgs; static BBSocketNotifier bbSocketNotifier; -QHash<QRfcommServerPrivate*, int> __fakeServerPorts; +QHash<QBluetoothServerPrivate*, int> __fakeServerPorts; QList<QPair<QString, QObject*> > evtRegistration; diff --git a/src/bluetooth/qrfcommserver_bluez.cpp b/src/bluetooth/qrfcommserver_bluez.cpp deleted file mode 100644 index 3701f348..00000000 --- a/src/bluetooth/qrfcommserver_bluez.cpp +++ /dev/null @@ -1,251 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** 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 "qrfcommserver.h" -#include "qrfcommserver_p.h" -#include "qbluetoothsocket.h" - -#include <QtCore/QSocketNotifier> - -#include <QtCore/QDebug> - -#include <bluetooth/bluetooth.h> -#include <bluetooth/rfcomm.h> - -#include <errno.h> - -QT_BEGIN_NAMESPACE_BLUETOOTH - -static inline void convertAddress(quint64 from, quint8 (&to)[6]) -{ - to[0] = (from >> 0) & 0xff; - to[1] = (from >> 8) & 0xff; - to[2] = (from >> 16) & 0xff; - to[3] = (from >> 24) & 0xff; - to[4] = (from >> 32) & 0xff; - to[5] = (from >> 40) & 0xff; -} - -QRfcommServerPrivate::QRfcommServerPrivate() -: maxPendingConnections(1), socketNotifier(0) -{ - socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket); -} - -QRfcommServerPrivate::~QRfcommServerPrivate() -{ - delete socketNotifier; - - delete socket; -} - -void QRfcommServerPrivate::_q_newConnection() -{ - // disable socket notifier until application calls nextPendingConnection(). - socketNotifier->setEnabled(false); - - emit q_ptr->newConnection(); -} - -void QRfcommServer::close() -{ - Q_D(QRfcommServer); - - delete d->socketNotifier; - d->socketNotifier = 0; - - d->socket->close(); -} - -bool QRfcommServer::listen(const QBluetoothAddress &address, quint16 port) -{ - Q_D(QRfcommServer); - - int sock = d->socket->socketDescriptor(); - if (sock < 0) - return false; - - sockaddr_rc addr; - - addr.rc_family = AF_BLUETOOTH; - addr.rc_channel = port; - - if (!address.isNull()) - convertAddress(address.toUInt64(), addr.rc_bdaddr.b); - else - convertAddress(Q_UINT64_C(0), addr.rc_bdaddr.b); - - if (::bind(sock, reinterpret_cast<sockaddr *>(&addr), sizeof(sockaddr_rc)) < 0) - return false; - - if (::listen(sock, d->maxPendingConnections) < 0) - return false; - - d->socket->setSocketState(QBluetoothSocket::ListeningState); - - if (!d->socketNotifier) { - d->socketNotifier = new QSocketNotifier(d->socket->socketDescriptor(), - QSocketNotifier::Read); - connect(d->socketNotifier, SIGNAL(activated(int)), this, SLOT(_q_newConnection())); - } - - return true; -} - -void QRfcommServer::setMaxPendingConnections(int numConnections) -{ - Q_D(QRfcommServer); - - if (d->socket->state() == QBluetoothSocket::UnconnectedState) - d->maxPendingConnections = numConnections; -} - -bool QRfcommServer::hasPendingConnections() const -{ - Q_D(const QRfcommServer); - - if (!d || !d->socketNotifier) - return false; - - // if the socket notifier is disable there is a pending connection waiting for us to accept. - return !d->socketNotifier->isEnabled(); -} - -QBluetoothSocket *QRfcommServer::nextPendingConnection() -{ - Q_D(QRfcommServer); - - if (!hasPendingConnections()) - return 0; - - sockaddr_rc addr; - socklen_t length = sizeof(sockaddr_rc); - - int pending = ::accept(d->socket->socketDescriptor(), - reinterpret_cast<sockaddr *>(&addr), &length); - if (pending >= 0) { - QBluetoothSocket *newSocket = new QBluetoothSocket; - newSocket->setSocketDescriptor(pending, QBluetoothSocket::RfcommSocket); - - d->socketNotifier->setEnabled(true); - - return newSocket; - } else { - qDebug() << "Hmm, could have sworn there was a connection waiting to be accepted!"; - d->socketNotifier->setEnabled(true); - } - - return 0; -} - -QBluetoothAddress QRfcommServer::serverAddress() const -{ - Q_D(const QRfcommServer); - - return d->socket->localAddress(); -} - -quint16 QRfcommServer::serverPort() const -{ - Q_D(const QRfcommServer); - - return d->socket->localPort(); -} - -void QRfcommServer::setSecurityFlags(QBluetooth::SecurityFlags security) -{ - Q_D(QRfcommServer); - - int lm = 0; - if(security == QBluetooth::NoSecurity){ - lm = 0; - } - if(security.testFlag(QBluetooth::Authorization)){ - lm |= RFCOMM_LM_AUTH; - } - if(security.testFlag(QBluetooth::Authentication)) { - lm |= RFCOMM_LM_TRUSTED; - } - if(security.testFlag(QBluetooth::Encryption)){ - lm |= RFCOMM_LM_ENCRYPT; - } - if(security.testFlag(QBluetooth::Secure)){ - lm |= RFCOMM_LM_SECURE; - } - - qDebug() << hex << "Setting lm to" << lm << security; - - if (setsockopt(d->socket->socketDescriptor(), SOL_RFCOMM, RFCOMM_LM, &lm, sizeof(lm)) < 0){ - qWarning() << "Failed to set socket option, closing socket for safety" << errno; - qWarning() << "Error: " << strerror(errno); - d->socket->close(); - } -} - -QBluetooth::SecurityFlags QRfcommServer::securityFlags() const -{ - Q_D(const QRfcommServer); - - int lm = 0; - int len = sizeof(lm); - int security = QBluetooth::NoSecurity; - - if (getsockopt(d->socket->socketDescriptor(), SOL_RFCOMM, RFCOMM_LM, &lm, (socklen_t *)&len) < 0) { - qWarning() << "Failed to get security flags" << strerror(errno); - return QBluetooth::NoSecurity; - } - - if(lm & RFCOMM_LM_SECURE) - security |= QBluetooth::Secure; - - if(lm & RFCOMM_LM_ENCRYPT) - security |= QBluetooth::Encryption; - - if(lm & RFCOMM_LM_TRUSTED) - security |= QBluetooth::Authentication; - - if(lm & RFCOMM_LM_AUTH) - security |= QBluetooth::Authorization; - - return static_cast<QBluetooth::Security>(security); -} - -QT_END_NAMESPACE_BLUETOOTH |