16#include <QtBluetooth/qbluetoothdeviceinfo.h>
17#include <QtBluetooth/qbluetoothserviceinfo.h>
19#include <QtCore/qloggingcategory.h>
20#include <QtCore/qrandom.h>
22#include <QtNetwork/qlocalsocket.h>
77 reply.waitForFinished();
82 for (ManagedObjectList::const_iterator
it = objectList.
constBegin();
88 ifaceIter != ifaceList.constEnd(); ++ifaceIter) {
90 if (
path.path().indexOf(adapterPath) != 0)
95 if (
device.adapter().path() != adapterPath)
99 if (btAddress.isNull() || btAddress !=
address)
112 QIODevice::OpenMode openMode)
117 bool success =
false;
120 if (!profileManager) {
128 if (profileContext) {
129 qCDebug(QT_BT_BLUEZ) <<
"Profile context still active. close socket first.";
137 this, &QBluetoothSocketPrivateBluezDBus::remoteConnected);
139 for (
i = 0;
i < 10 && !success;
i++) {
143 profilePath = u
"/qt/btsocket/%1%2/%3"_s.
154 qCWarning(QT_BT_BLUEZ) <<
"Cannot export serial client profile on DBus";
156 delete profileContext;
157 profileContext =
nullptr;
159 errorString = QBluetoothSocket::tr(
"Cannot export profile on DBus");
174 qCDebug(QT_BT_BLUEZ) <<
"Registering client profile on" << profilePath <<
"with options:";
175 qCDebug(QT_BT_BLUEZ) << profileOptions;
180 reply.waitForFinished();
181 if (
reply.isError()) {
182 qCWarning(QT_BT_BLUEZ) <<
"Client profile registration failed:"
186 errorString = QBluetoothSocket::tr(
"Cannot register profile on DBus");
192 if (remoteDevicePath.
isEmpty()) {
194 <<
"Try device discovery first";
197 errorString = QBluetoothSocket::tr(
"Cannot find remote device");
219 if (
reply.isError()) {
220 qCWarning(QT_BT_BLUEZ) <<
"Cannot connect to profile/service.";
224 errorString = QBluetoothSocket::tr(
"Cannot connect to remote profile");
231 if (profileManager) {
232 qCDebug(QT_BT_BLUEZ) <<
"Unregistering client profile on" << profilePath
233 <<
"in connectToServiceReplyHandler() callback.";
236 reply.waitForFinished();
242 delete profileManager;
243 profileManager =
nullptr;
253 targetService = service.serviceUuid();
254 if (targetService.isNull()) {
260 if (targetService.isNull()) {
261 qCWarning(QT_BT_BLUEZ) <<
"Cannot find appropriate serviceUuid"
262 <<
"or SerialPort service class uuid";
263 errorString = QBluetoothSocket::tr(
"Missing serviceUuid or Serial Port service class uuid");
281 qCWarning(QT_BT_BLUEZ) <<
"Invalid address to remote address passed.";
282 errorString = QBluetoothSocket::tr(
"Invalid Bluetooth address passed to connectToService()");
288 qCWarning(QT_BT_BLUEZ) <<
"Cannot find appropriate serviceUuid"
289 <<
"or SerialPort service class uuid";
290 errorString = QBluetoothSocket::tr(
"Missing serviceUuid or Serial Port service class uuid");
296 qCWarning(QT_BT_BLUEZ) <<
"QBluetoothSocketPrivateBluezDBus::connectToService called on busy socket";
297 errorString = QBluetoothSocket::tr(
"Trying to connect while connection is in progress");
303 qCWarning(QT_BT_BLUEZ) <<
"QBluetoothSocketPrivateBluezDBus::connectToService cannot "
304 "connect with 'UnknownProtocol' (type provided by given service)";
305 errorString = QBluetoothSocket::tr(
"Socket type not supported");
311 errorString = QBluetoothSocket::tr(
"Socket type not supported");
327 errorString =
tr(
"Connecting to port is not supported via Bluez DBus");
329 qCWarning(QT_BT_BLUEZ) <<
"Connecting to port is not supported (Uuid required)";
335 localSocket->
close();
343 emit q->readChannelFinished();
377 if (descriptor == -1)
382 socklen_t addrLength =
sizeof(
addr);
384 if (::getsockname(descriptor,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) == 0)
385 return (
addr.rc_channel);
388 socklen_t addrLength =
sizeof(
addr);
390 if (::getsockname(descriptor,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) == 0)
399 if (remoteDevicePath.
isEmpty())
409 if (remoteDevicePath.
isEmpty())
423 if (descriptor == -1)
428 socklen_t addrLength =
sizeof(
addr);
430 if (::getpeername(descriptor,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) == 0)
434 socklen_t addrLength =
sizeof(
addr);
436 if (::getpeername(descriptor,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) == 0)
451 errorString = QBluetoothSocket::tr(
"Cannot write while not connected");
457 return localSocket->
write(
data, maxSize);
470 errorString = QBluetoothSocket::tr(
"Cannot read while not connected");
476 return localSocket->
read(
data, maxSize);
524 int descriptor = ::dup(
fd.fileDescriptor());
528 if (!success || !localSocket->
isValid()) {
531 localSocket =
nullptr;
536 this, &QBluetoothSocketPrivateBluezDBus::socketStateChanged);
557 emit q->readChannelFinished();
566void QBluetoothSocketPrivateBluezDBus::clearSocket()
573 qCDebug(QT_BT_BLUEZ) <<
"Clearing profile called for" << profilePath;
576 localSocket->
close();
578 localSocket =
nullptr;
586 auto reply =
device.DisconnectProfile(profileUuid);
587 reply.waitForFinished();
588 if (
reply.isError()) {
589 qCWarning(QT_BT_BLUEZ) <<
"Disconnect profile failed:"
594 if (profileContext) {
595 delete profileContext;
596 profileContext =
nullptr;
599 if (profileManager) {
600 qCDebug(QT_BT_BLUEZ) <<
"Unregistering client profile on" << profilePath
601 <<
"in clearSocket().";
604 reply.waitForFinished();
610 delete profileManager;
611 profileManager =
nullptr;
614 remoteDevicePath.
clear();
620#include "moc_qbluetoothsocket_bluezdbus_p.cpp"
QString sanitizeNameForDBus(const QString &text)
QString findAdapterForAddress(const QBluetoothAddress &wantedAddress, bool *ok=nullptr)
QMap< QDBusObjectPath, InterfaceList > ManagedObjectList
IOBluetoothDevice * device
void newConnection(const QDBusUnixFileDescriptor &fd)
QDBusPendingReply UnregisterProfile(const QDBusObjectPath &profile)
QDBusPendingReply RegisterProfile(const QDBusObjectPath &profile, const QString &UUID, const QVariantMap &options)
Protocol
This enum describes the socket protocol used by the service.
QBluetoothSocket::OpenMode openMode
QBluetoothServiceInfo::Protocol socketType
QBluetoothSocket::SocketState state
quint16 peerPort() const override
qint64 readData(char *data, qint64 maxSize) override
qint64 bytesAvailable() const override
~QBluetoothSocketPrivateBluezDBus() override
quint16 localPort() const override
bool canReadLine() const override
void connectToService(const QBluetoothServiceInfo &service, QIODevice::OpenMode openMode) override
void connectToServiceHelper(const QBluetoothAddress &address, quint16 port, QIODevice::OpenMode openMode) override
QBluetoothAddress peerAddress() const override
qint64 bytesToWrite() const override
QString peerName() const override
QBluetoothAddress localAddress() const override
qint64 writeData(const char *data, qint64 maxSize) override
bool setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType, QBluetoothSocket::SocketState socketState=QBluetoothSocket::SocketState::ConnectedState, QBluetoothSocket::OpenMode openMode=QBluetoothSocket::ReadWrite) override
void connectToServiceReplyHandler(QDBusPendingCallWatcher *)
bool ensureNativeSocket(QBluetoothServiceInfo::Protocol type) override
QString localName() const override
SocketState
This enum describes the state of the Bluetooth socket.
@ UnsupportedProtocolError
static qint64 applicationPid() Q_DECL_CONST_FUNCTION
QString applicationName
the name of this application
static QDBusConnection systemBus()
Returns a QDBusConnection object opened with the system bus.
void finished(QDBusPendingCallWatcher *self=nullptr)
This signal is emitted when the pending call has finished and its reply is available.
void readyRead()
This signal is emitted once every time new data is available for reading from the device's current re...
void bytesWritten(qint64 bytes)
This signal is emitted every time a payload of data has been written to the device's current write ch...
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read.
The QLocalSocket class provides a local socket.
bool setSocketDescriptor(qintptr socketDescriptor, LocalSocketState socketState=ConnectedState, OpenMode openMode=ReadWrite)
Initializes QLocalSocket with the native socket descriptor socketDescriptor.
virtual qint64 bytesToWrite() const override
\reimp
virtual bool canReadLine() const override
\reimp
LocalSocketState
This enum describes the different states in which a socket can be.
bool isValid() const
Returns true if the socket is valid and ready for use; otherwise returns false.
qintptr socketDescriptor() const
Returns the native socket descriptor of the QLocalSocket object if this is available; otherwise retur...
virtual void close() override
Closes the I/O device for the socket and calls disconnectFromServer() to close the socket's connectio...
virtual qint64 bytesAvailable() const override
\reimp
void stateChanged(QLocalSocket::LocalSocketState socketState)
This signal is emitted whenever QLocalSocket's state changes.
iterator insert(const Key &key, const T &value)
NetworkError error() const
Returns the error that was found during the processing of this request.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
void deleteLater()
\threadsafe
static Q_DECL_CONST_FUNCTION QRandomGenerator * global()
\threadsafe
const_iterator constBegin() const noexcept
const_iterator constEnd() const noexcept
\macro QT_RESTRICTED_CAST_FROM_ASCII
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
void clear()
Clears the contents of the string and makes it null.
QString toString(StringFormat mode=WithBraces) const
bool isNull() const noexcept
Returns true if this is the null UUID {00000000-0000-0000-0000-000000000000}; otherwise returns false...
QSet< QString >::iterator it
void newState(QList< State > &states, const char *token, const char *lexem, bool pre)
Combined button and popup list for selecting options.
static QString findRemoteDevicePath(const QBluetoothAddress &address)
static QT_BEGIN_NAMESPACE const char * socketType(QSocketNotifier::Type type)
#define qCWarning(category,...)
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint GLuint64EXT address
GLdouble GLdouble GLdouble GLdouble q
GLsizei const GLchar *const * path
#define QStringLiteral(str)
QFutureWatcher< int > watcher
QNetworkAccessManager manager