9#include <QtBluetooth/QBluetoothLocalDevice>
12#include <qplatformdefs.h>
13#include <QtCore/private/qcore_unix_p.h>
15#include <QtCore/QLoggingCategory>
21#include <QtCore/QSocketNotifier>
96 errorString = QBluetoothSocket::tr(
"Unknown socket error");
104 memset(&security, 0,
sizeof(security));
114 &security,
sizeof(security)) != 0) {
115 qCWarning(QT_BT_BLUEZ) <<
"Failed to set socket option, closing socket for safety" << errno;
117 errorString = QBluetoothSocket::tr(
"Cannot set connection security level");
126 addr.rc_family = AF_BLUETOOTH;
139 addr.l2_family = AF_BLUETOOTH;
146#if QT_CONFIG(bluez) && !defined(QT_BLUEZ_NO_BTLE)
147 if (lowEnergySocketType) {
149 addr.l2_bdaddr_type = lowEnergySocketType;
165 if (
result >= 0 || (
result == -1 && errno == EINPROGRESS)) {
182 qCWarning(QT_BT_BLUEZ) <<
"QBluetoothSocketPrivateBluez::connectToService called on busy socket";
183 errorString = QBluetoothSocket::tr(
"Trying to connect while connection is in progress");
191 qCWarning(QT_BT_BLUEZ) <<
"QBluetoothSocket::connectToService cannot "
192 "connect with 'UnknownProtocol' (type provided by given service)";
193 errorString = QBluetoothSocket::tr(
"Socket type not supported");
198 if (service.protocolServiceMultiplexer() > 0) {
202 errorString = QBluetoothSocket::tr(
"Unknown socket error");
208 }
else if (service.serverChannel() > 0) {
212 errorString = QBluetoothSocket::tr(
"Unknown socket error");
219 if (service.serviceUuid().isNull()
221 qCWarning(QT_BT_BLUEZ) <<
"No port, no PSM, and no UUID provided. Unable to connect";
224 qCDebug(QT_BT_BLUEZ) <<
"Need a port/psm, doing discovery";
231 QIODevice::OpenMode openMode)
236 qCWarning(QT_BT_BLUEZ) <<
"QBluetoothSocketPrivateBluez::connectToService called on busy socket";
237 errorString = QBluetoothSocket::tr(
"Trying to connect while connection is in progress");
243 qCWarning(QT_BT_BLUEZ) <<
"QBluetoothSocketPrivateBluez::connectToService cannot "
244 "connect with 'UnknownProtocol' (type provided by given service)";
245 errorString = QBluetoothSocket::tr(
"Socket type not supported");
252 service.setDevice(
device);
253 service.setServiceUuid(uuid);
263 qCWarning(QT_BT_BLUEZ) <<
"QBluetoothSocketPrivateBluez::connectToService cannot "
264 "connect with 'UnknownProtocol' (type provided by given service)";
265 errorString = QBluetoothSocket::tr(
"Socket type not supported");
271 qCWarning(QT_BT_BLUEZ) <<
"QBluetoothSocketPrivateBluez::connectToService called on busy socket";
272 errorString = QBluetoothSocket::tr(
"Trying to connect while connection is in progress");
279void QBluetoothSocketPrivateBluez::_q_writeNotify()
284 len =
sizeof(errorno);
285 ::getsockopt(
socket, SOL_SOCKET, SO_ERROR, &errorno, (socklen_t*)&
len);
307 if (writtenBytes < 0) {
319 if (writtenBytes <
size) {
321 char* remainder =
buf + writtenBytes;
324 if (writtenBytes > 0)
325 emit q->bytesWritten(writtenBytes);
338void QBluetoothSocketPrivateBluez::_q_readNotify()
351 if (errsv == EHOSTDOWN)
353 else if (errsv == ECONNRESET)
358 q->disconnectFromService();
382 emit q->readChannelFinished();
399 socklen_t addrLength =
sizeof(
addr);
401 if (::getsockname(
socket,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) == 0)
405 socklen_t addrLength =
sizeof(
addr);
407 if (::getsockname(
socket,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) == 0)
418 socklen_t addrLength =
sizeof(
addr);
420 if (::getsockname(
socket,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) == 0)
421 return addr.rc_channel;
424 socklen_t addrLength =
sizeof(
addr);
426 if (::getsockname(
socket,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) == 0)
439 socklen_t addrLength =
sizeof(
addr);
441 if (::getpeername(
socket,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) < 0)
447 socklen_t addrLength =
sizeof(
addr);
449 if (::getpeername(
socket,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) < 0)
454 qCWarning(QT_BT_BLUEZ) <<
"peerName() called on socket of unknown type";
464 reply.waitForFinished();
475 const QString &iface = jt.key();
491 socklen_t addrLength =
sizeof(
addr);
493 if (::getpeername(
socket,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) == 0)
497 socklen_t addrLength =
sizeof(
addr);
499 if (::getpeername(
socket,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) == 0)
510 socklen_t addrLength =
sizeof(
addr);
512 if (::getpeername(
socket,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) == 0)
513 return addr.rc_channel;
516 socklen_t addrLength =
sizeof(
addr);
518 if (::getpeername(
socket,
reinterpret_cast<sockaddr *
>(&
addr), &addrLength) == 0)
530 errorString = QBluetoothSocket::tr(
"Cannot write while not connected");
549 emit q->bytesWritten(sz);
564 memcpy(txbuf,
data, maxSize);
575 errorString = QBluetoothSocket::tr(
"Cannot read while not connected");
609 socket = socketDescriptor;
613 if (!(
flags & O_NONBLOCK))
622 q->setSocketState(socketState);
644#include "moc_qbluetoothsocket_bluez_p.cpp"
#define QPRIVATELINEARBUFFER_BUFFERSIZE
QT_BEGIN_NAMESPACE void initializeBluez5()
#define BT_SECURITY_MEDIUM
IOBluetoothDevice * device
Protocol
This enum describes the socket protocol used by the service.
QBluetoothSocket::OpenMode openMode
QBluetooth::SecurityFlags secFlags
QPrivateLinearBuffer rxBuffer
QSocketNotifier * readNotifier
QBluetoothServiceInfo::Protocol socketType
QSocketNotifier * connectWriteNotifier
QBluetoothSocket::SocketState state
QPrivateLinearBuffer txBuffer
QString localName() const override
qint64 writeData(const char *data, qint64 maxSize) override
bool ensureNativeSocket(QBluetoothServiceInfo::Protocol type) override
quint16 localPort() const override
qint64 bytesAvailable() const override
QBluetoothAddress peerAddress() const override
qint64 readData(char *data, qint64 maxSize) override
QString peerName() const override
void connectToService(const QBluetoothServiceInfo &service, QIODevice::OpenMode openMode) override
bool canReadLine() const override
~QBluetoothSocketPrivateBluez() override
QBluetoothAddress localAddress() const override
qint64 bytesToWrite() const override
bool setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType, QBluetoothSocket::SocketState socketState=QBluetoothSocket::SocketState::ConnectedState, QBluetoothSocket::OpenMode openMode=QBluetoothSocket::ReadWrite) override
void connectToServiceHelper(const QBluetoothAddress &address, quint16 port, QIODevice::OpenMode openMode) override
quint16 peerPort() const override
SocketState
This enum describes the state of the Bluetooth socket.
@ UnsupportedProtocolError
static QDBusConnection systemBus()
Returns a QDBusConnection object opened with the system bus.
T value(const Key &key, const T &defaultValue=T()) const
const_iterator constBegin() const
const_iterator constEnd() const
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
qsizetype read(char *target, qsizetype size)
void ungetBlock(const char *block, qsizetype size)
void chop(qsizetype size)
char * reserve(qsizetype size)
void setEnabled(bool)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.
\macro QT_RESTRICTED_CAST_FROM_ASCII
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
static bool readFromDevice(QIODevice *device, QJsonArray *allMetaObjects)
QSet< QString >::iterator it
Combined button and popup list for selecting options.
static void convertAddress(const quint64 from, quint8(&to)[6])
static qint64 qt_safe_write(int fd, const void *data, qint64 len)
Q_DECL_COLD_FUNCTION Q_CORE_EXPORT QString qt_error_string(int errorCode=-1)
#define qCWarning(category,...)
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLuint GLenum GLsizei const GLchar * buf
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint GLuint64EXT address
GLdouble GLdouble GLdouble GLdouble q
#define QStringLiteral(str)
unsigned long long quint64
ReturnedValue read(const char *data)
QNetworkAccessManager manager
char * toString(const MyType &t)
[31]