| 1 | // Copyright (C) 2018 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qbluetoothsocket.h" |
| 5 | #include "qbluetoothsocket_bluez_p.h" |
| 6 | #include "qbluetoothdeviceinfo.h" |
| 7 | |
| 8 | #include "bluez/objectmanager_p.h" |
| 9 | #include <QtBluetooth/QBluetoothLocalDevice> |
| 10 | #include "bluez/bluez_data_p.h" |
| 11 | |
| 12 | #include <qplatformdefs.h> |
| 13 | #include <QtCore/private/qcore_unix_p.h> |
| 14 | |
| 15 | #include <QtCore/QLoggingCategory> |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <unistd.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include <QtCore/QSocketNotifier> |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | Q_DECLARE_LOGGING_CATEGORY(QT_BT_BLUEZ) |
| 26 | |
| 27 | using namespace QtBluetoothPrivate; // for D-Bus wrappers |
| 28 | |
| 29 | QBluetoothSocketPrivateBluez::QBluetoothSocketPrivateBluez() |
| 30 | : QBluetoothSocketBasePrivate() |
| 31 | { |
| 32 | secFlags = QBluetooth::Security::Authorization; |
| 33 | } |
| 34 | |
| 35 | QBluetoothSocketPrivateBluez::~QBluetoothSocketPrivateBluez() |
| 36 | { |
| 37 | delete readNotifier; |
| 38 | readNotifier = nullptr; |
| 39 | delete connectWriteNotifier; |
| 40 | connectWriteNotifier = nullptr; |
| 41 | |
| 42 | // If the socket wasn't closed/aborted make sure we free the socket file descriptor |
| 43 | if (socket != -1) |
| 44 | QT_CLOSE(fd: socket); |
| 45 | } |
| 46 | |
| 47 | bool QBluetoothSocketPrivateBluez::ensureNativeSocket(QBluetoothServiceInfo::Protocol type) |
| 48 | { |
| 49 | if (socket != -1) { |
| 50 | if (socketType == type) |
| 51 | return true; |
| 52 | |
| 53 | delete readNotifier; |
| 54 | readNotifier = nullptr; |
| 55 | delete connectWriteNotifier; |
| 56 | connectWriteNotifier = nullptr; |
| 57 | QT_CLOSE(fd: socket); |
| 58 | } |
| 59 | |
| 60 | socketType = type; |
| 61 | |
| 62 | switch (type) { |
| 63 | case QBluetoothServiceInfo::L2capProtocol: |
| 64 | socket = ::socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); |
| 65 | break; |
| 66 | case QBluetoothServiceInfo::RfcommProtocol: |
| 67 | socket = ::socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); |
| 68 | break; |
| 69 | default: |
| 70 | socket = -1; |
| 71 | } |
| 72 | |
| 73 | if (socket == -1) |
| 74 | return false; |
| 75 | |
| 76 | int flags = fcntl(fd: socket, F_GETFL, 0); |
| 77 | fcntl(fd: socket, F_SETFL, flags | O_NONBLOCK); |
| 78 | |
| 79 | Q_Q(QBluetoothSocket); |
| 80 | readNotifier = new QSocketNotifier(socket, QSocketNotifier::Read); |
| 81 | QObject::connect(sender: readNotifier, SIGNAL(activated(QSocketDescriptor)), receiver: this, SLOT(_q_readNotify())); |
| 82 | connectWriteNotifier = new QSocketNotifier(socket, QSocketNotifier::Write, q); |
| 83 | QObject::connect(sender: connectWriteNotifier, SIGNAL(activated(QSocketDescriptor)), receiver: this, SLOT(_q_writeNotify())); |
| 84 | |
| 85 | connectWriteNotifier->setEnabled(false); |
| 86 | readNotifier->setEnabled(false); |
| 87 | |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | void QBluetoothSocketPrivateBluez::connectToServiceHelper(const QBluetoothAddress &address, quint16 port, QIODevice::OpenMode openMode) |
| 93 | { |
| 94 | Q_Q(QBluetoothSocket); |
| 95 | int result = -1; |
| 96 | |
| 97 | if (socket == -1 && !ensureNativeSocket(type: socketType)) { |
| 98 | errorString = QBluetoothSocket::tr(s: "Unknown socket error" ); |
| 99 | q->setSocketError(QBluetoothSocket::SocketError::UnknownSocketError); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | // apply preferred security level |
| 104 | // ignore QBluetooth::Security::Authentication -> not used anymore by kernel |
| 105 | struct bt_security security; |
| 106 | memset(s: &security, c: 0, n: sizeof(security)); |
| 107 | |
| 108 | if (secFlags & QBluetooth::Security::Authorization) |
| 109 | security.level = BT_SECURITY_LOW; |
| 110 | if (secFlags & QBluetooth::Security::Encryption) |
| 111 | security.level = BT_SECURITY_MEDIUM; |
| 112 | if (secFlags & QBluetooth::Security::Secure) |
| 113 | security.level = BT_SECURITY_HIGH; |
| 114 | |
| 115 | if (setsockopt(fd: socket, SOL_BLUETOOTH, BT_SECURITY, |
| 116 | optval: &security, optlen: sizeof(security)) != 0) { |
| 117 | qCWarning(QT_BT_BLUEZ) << "Failed to set socket option, closing socket for safety" << errno; |
| 118 | qCWarning(QT_BT_BLUEZ) << "Error: " << qt_error_string(errno); |
| 119 | errorString = QBluetoothSocket::tr(s: "Cannot set connection security level" ); |
| 120 | q->setSocketError(QBluetoothSocket::SocketError::UnknownSocketError); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | if (socketType == QBluetoothServiceInfo::RfcommProtocol) { |
| 125 | sockaddr_rc addr; |
| 126 | |
| 127 | memset(s: &addr, c: 0, n: sizeof(addr)); |
| 128 | addr.rc_family = AF_BLUETOOTH; |
| 129 | addr.rc_channel = port; |
| 130 | |
| 131 | convertAddress(from: address.toUInt64(), to&: addr.rc_bdaddr.b); |
| 132 | |
| 133 | connectWriteNotifier->setEnabled(true); |
| 134 | readNotifier->setEnabled(true); |
| 135 | |
| 136 | result = ::connect(fd: socket, addr: (sockaddr *)&addr, len: sizeof(addr)); |
| 137 | } else if (socketType == QBluetoothServiceInfo::L2capProtocol) { |
| 138 | sockaddr_l2 addr; |
| 139 | |
| 140 | memset(s: &addr, c: 0, n: sizeof(addr)); |
| 141 | addr.l2_family = AF_BLUETOOTH; |
| 142 | // This is an ugly hack but the socket class does what's needed already. |
| 143 | // For L2CP GATT we need a channel rather than a socket and the LE address type |
| 144 | // We don't want to make this public API offering for now especially since |
| 145 | // only Linux (of all platforms supported by this library) supports this type |
| 146 | // of socket. |
| 147 | |
| 148 | #if QT_CONFIG(bluez) && !defined(QT_BLUEZ_NO_BTLE) |
| 149 | if (lowEnergySocketType) { |
| 150 | addr.l2_cid = htobs(port); |
| 151 | addr.l2_bdaddr_type = lowEnergySocketType; |
| 152 | } else { |
| 153 | addr.l2_psm = htobs(port); |
| 154 | } |
| 155 | #else |
| 156 | addr.l2_psm = htobs(port); |
| 157 | #endif |
| 158 | |
| 159 | convertAddress(from: address.toUInt64(), to&: addr.l2_bdaddr.b); |
| 160 | |
| 161 | connectWriteNotifier->setEnabled(true); |
| 162 | readNotifier->setEnabled(true); |
| 163 | |
| 164 | result = ::connect(fd: socket, addr: (sockaddr *)&addr, len: sizeof(addr)); |
| 165 | } |
| 166 | |
| 167 | if (result >= 0 || (result == -1 && errno == EINPROGRESS)) { |
| 168 | connecting = true; |
| 169 | q->setSocketState(QBluetoothSocket::SocketState::ConnectingState); |
| 170 | q->setOpenMode(openMode); |
| 171 | } else { |
| 172 | errorString = qt_error_string(errno); |
| 173 | q->setSocketError(QBluetoothSocket::SocketError::UnknownSocketError); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void QBluetoothSocketPrivateBluez::connectToService( |
| 178 | const QBluetoothServiceInfo &service, QIODevice::OpenMode openMode) |
| 179 | { |
| 180 | Q_Q(QBluetoothSocket); |
| 181 | |
| 182 | if (q->state() != QBluetoothSocket::SocketState::UnconnectedState |
| 183 | && q->state() != QBluetoothSocket::SocketState::ServiceLookupState) { |
| 184 | qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService called on busy socket" ; |
| 185 | errorString = QBluetoothSocket::tr(s: "Trying to connect while connection is in progress" ); |
| 186 | q->setSocketError(QBluetoothSocket::SocketError::OperationError); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | // we are checking the service protocol and not socketType() |
| 191 | // socketType will change in ensureNativeSocket() |
| 192 | if (service.socketProtocol() == QBluetoothServiceInfo::UnknownProtocol) { |
| 193 | qCWarning(QT_BT_BLUEZ) << "QBluetoothSocket::connectToService cannot " |
| 194 | "connect with 'UnknownProtocol' (type provided by given service)" ; |
| 195 | errorString = QBluetoothSocket::tr(s: "Socket type not supported" ); |
| 196 | q->setSocketError(QBluetoothSocket::SocketError::UnsupportedProtocolError); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | if (service.protocolServiceMultiplexer() > 0) { |
| 201 | Q_ASSERT(service.socketProtocol() == QBluetoothServiceInfo::L2capProtocol); |
| 202 | |
| 203 | if (!ensureNativeSocket(type: QBluetoothServiceInfo::L2capProtocol)) { |
| 204 | errorString = QBluetoothSocket::tr(s: "Unknown socket error" ); |
| 205 | q->setSocketError(QBluetoothSocket::SocketError::UnknownSocketError); |
| 206 | return; |
| 207 | } |
| 208 | connectToServiceHelper(address: service.device().address(), port: service.protocolServiceMultiplexer(), |
| 209 | openMode); |
| 210 | } else if (service.serverChannel() > 0) { |
| 211 | Q_ASSERT(service.socketProtocol() == QBluetoothServiceInfo::RfcommProtocol); |
| 212 | |
| 213 | if (!ensureNativeSocket(type: QBluetoothServiceInfo::RfcommProtocol)) { |
| 214 | errorString = QBluetoothSocket::tr(s: "Unknown socket error" ); |
| 215 | q->setSocketError(QBluetoothSocket::SocketError::UnknownSocketError); |
| 216 | return; |
| 217 | } |
| 218 | connectToServiceHelper(address: service.device().address(), port: service.serverChannel(), openMode); |
| 219 | } else { |
| 220 | // try doing service discovery to see if we can find the socket |
| 221 | if (service.serviceUuid().isNull() |
| 222 | && !service.serviceClassUuids().contains(t: QBluetoothUuid::ServiceClassUuid::SerialPort)) { |
| 223 | qCWarning(QT_BT_BLUEZ) << "No port, no PSM, and no UUID provided. Unable to connect" ; |
| 224 | return; |
| 225 | } |
| 226 | qCDebug(QT_BT_BLUEZ) << "Need a port/psm, doing discovery" ; |
| 227 | q->doDeviceDiscovery(service, openMode); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void QBluetoothSocketPrivateBluez::connectToService( |
| 232 | const QBluetoothAddress &address, const QBluetoothUuid &uuid, |
| 233 | QIODevice::OpenMode openMode) |
| 234 | { |
| 235 | Q_Q(QBluetoothSocket); |
| 236 | |
| 237 | if (q->state() != QBluetoothSocket::SocketState::UnconnectedState) { |
| 238 | qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService called on busy socket" ; |
| 239 | errorString = QBluetoothSocket::tr(s: "Trying to connect while connection is in progress" ); |
| 240 | q->setSocketError(QBluetoothSocket::SocketError::OperationError); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | if (q->socketType() == QBluetoothServiceInfo::UnknownProtocol) { |
| 245 | qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService cannot " |
| 246 | "connect with 'UnknownProtocol' (type provided by given service)" ; |
| 247 | errorString = QBluetoothSocket::tr(s: "Socket type not supported" ); |
| 248 | q->setSocketError(QBluetoothSocket::SocketError::UnsupportedProtocolError); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | QBluetoothServiceInfo service; |
| 253 | QBluetoothDeviceInfo device(address, QString(), QBluetoothDeviceInfo::MiscellaneousDevice); |
| 254 | service.setDevice(device); |
| 255 | service.setServiceUuid(uuid); |
| 256 | q->doDeviceDiscovery(service, openMode); |
| 257 | } |
| 258 | |
| 259 | void QBluetoothSocketPrivateBluez::connectToService( |
| 260 | const QBluetoothAddress &address, quint16 port, QIODevice::OpenMode openMode) |
| 261 | { |
| 262 | Q_Q(QBluetoothSocket); |
| 263 | |
| 264 | if (q->socketType() == QBluetoothServiceInfo::UnknownProtocol) { |
| 265 | qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService cannot " |
| 266 | "connect with 'UnknownProtocol' (type provided by given service)" ; |
| 267 | errorString = QBluetoothSocket::tr(s: "Socket type not supported" ); |
| 268 | q->setSocketError(QBluetoothSocket::SocketError::UnsupportedProtocolError); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | if (q->state() != QBluetoothSocket::SocketState::UnconnectedState) { |
| 273 | qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService called on busy socket" ; |
| 274 | errorString = QBluetoothSocket::tr(s: "Trying to connect while connection is in progress" ); |
| 275 | q->setSocketError(QBluetoothSocket::SocketError::OperationError); |
| 276 | return; |
| 277 | } |
| 278 | connectToServiceHelper(address, port, openMode); |
| 279 | } |
| 280 | |
| 281 | void QBluetoothSocketPrivateBluez::_q_writeNotify() |
| 282 | { |
| 283 | Q_Q(QBluetoothSocket); |
| 284 | if (connecting && state == QBluetoothSocket::SocketState::ConnectingState){ |
| 285 | int errorno, len; |
| 286 | len = sizeof(errorno); |
| 287 | ::getsockopt(fd: socket, SOL_SOCKET, SO_ERROR, optval: &errorno, optlen: (socklen_t*)&len); |
| 288 | if(errorno) { |
| 289 | errorString = qt_error_string(errorCode: errorno); |
| 290 | q->setSocketError(QBluetoothSocket::SocketError::UnknownSocketError); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | q->setSocketState(QBluetoothSocket::SocketState::ConnectedState); |
| 295 | |
| 296 | connectWriteNotifier->setEnabled(false); |
| 297 | connecting = false; |
| 298 | } |
| 299 | else { |
| 300 | if (txBuffer.size() == 0) { |
| 301 | connectWriteNotifier->setEnabled(false); |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | char buf[1024]; |
| 306 | |
| 307 | const auto size = txBuffer.read(target: buf, size: 1024); |
| 308 | const auto writtenBytes = qt_safe_write(fd: socket, data: buf, len: size); |
| 309 | if (writtenBytes < 0) { |
| 310 | switch (errno) { |
| 311 | case EAGAIN: |
| 312 | txBuffer.ungetBlock(block: buf, size); |
| 313 | break; |
| 314 | default: |
| 315 | // every other case returns error |
| 316 | errorString = QBluetoothSocket::tr(s: "Network Error: %1" ).arg(a: qt_error_string(errno)) ; |
| 317 | q->setSocketError(QBluetoothSocket::SocketError::NetworkError); |
| 318 | break; |
| 319 | } |
| 320 | } else { |
| 321 | if (writtenBytes < size) { |
| 322 | // add remainder back to buffer |
| 323 | char* remainder = buf + writtenBytes; |
| 324 | txBuffer.ungetBlock(block: remainder, size: size - writtenBytes); |
| 325 | } |
| 326 | if (writtenBytes > 0) |
| 327 | emit q->bytesWritten(bytes: writtenBytes); |
| 328 | } |
| 329 | |
| 330 | if (txBuffer.size()) { |
| 331 | connectWriteNotifier->setEnabled(true); |
| 332 | } |
| 333 | else if (state == QBluetoothSocket::SocketState::ClosingState) { |
| 334 | connectWriteNotifier->setEnabled(false); |
| 335 | this->close(); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void QBluetoothSocketPrivateBluez::_q_readNotify() |
| 341 | { |
| 342 | Q_Q(QBluetoothSocket); |
| 343 | char *writePointer = rxBuffer.reserve(QPRIVATELINEARBUFFER_BUFFERSIZE); |
| 344 | // qint64 readFromDevice = q->readData(writePointer, QPRIVATELINEARBUFFER_BUFFERSIZE); |
| 345 | const auto readFromDevice = ::read(fd: socket, buf: writePointer, QPRIVATELINEARBUFFER_BUFFERSIZE); |
| 346 | rxBuffer.chop(QPRIVATELINEARBUFFER_BUFFERSIZE - (readFromDevice < 0 ? 0 : readFromDevice)); |
| 347 | if(readFromDevice <= 0){ |
| 348 | int errsv = errno; |
| 349 | readNotifier->setEnabled(false); |
| 350 | connectWriteNotifier->setEnabled(false); |
| 351 | errorString = qt_error_string(errorCode: errsv); |
| 352 | qCWarning(QT_BT_BLUEZ) << Q_FUNC_INFO << socket << "error:" << readFromDevice << errorString; |
| 353 | if (errsv == EHOSTDOWN) |
| 354 | q->setSocketError(QBluetoothSocket::SocketError::HostNotFoundError); |
| 355 | else if (errsv == ECONNRESET) |
| 356 | q->setSocketError(QBluetoothSocket::SocketError::RemoteHostClosedError); |
| 357 | else |
| 358 | q->setSocketError(QBluetoothSocket::SocketError::UnknownSocketError); |
| 359 | |
| 360 | q->disconnectFromService(); |
| 361 | } |
| 362 | else { |
| 363 | emit q->readyRead(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void QBluetoothSocketPrivateBluez::abort() |
| 368 | { |
| 369 | delete readNotifier; |
| 370 | readNotifier = nullptr; |
| 371 | delete connectWriteNotifier; |
| 372 | connectWriteNotifier = nullptr; |
| 373 | |
| 374 | // We don't transition through Closing for abort, so |
| 375 | // we don't call disconnectFromService or |
| 376 | // QBluetoothSocket::close |
| 377 | QT_CLOSE(fd: socket); |
| 378 | socket = -1; |
| 379 | |
| 380 | Q_Q(QBluetoothSocket); |
| 381 | |
| 382 | q->setOpenMode(QIODevice::NotOpen); |
| 383 | q->setSocketState(QBluetoothSocket::SocketState::UnconnectedState); |
| 384 | emit q->readChannelFinished(); |
| 385 | } |
| 386 | |
| 387 | QString QBluetoothSocketPrivateBluez::localName() const |
| 388 | { |
| 389 | const QBluetoothAddress address = localAddress(); |
| 390 | if (address.isNull()) |
| 391 | return QString(); |
| 392 | |
| 393 | QBluetoothLocalDevice device(address); |
| 394 | return device.name(); |
| 395 | } |
| 396 | |
| 397 | QBluetoothAddress QBluetoothSocketPrivateBluez::localAddress() const |
| 398 | { |
| 399 | if (socketType == QBluetoothServiceInfo::RfcommProtocol) { |
| 400 | sockaddr_rc addr; |
| 401 | socklen_t addrLength = sizeof(addr); |
| 402 | |
| 403 | if (::getsockname(fd: socket, addr: reinterpret_cast<sockaddr *>(&addr), len: &addrLength) == 0) |
| 404 | return QBluetoothAddress(convertAddress(from: addr.rc_bdaddr.b)); |
| 405 | } else if (socketType == QBluetoothServiceInfo::L2capProtocol) { |
| 406 | sockaddr_l2 addr; |
| 407 | socklen_t addrLength = sizeof(addr); |
| 408 | |
| 409 | if (::getsockname(fd: socket, addr: reinterpret_cast<sockaddr *>(&addr), len: &addrLength) == 0) |
| 410 | return QBluetoothAddress(convertAddress(from: addr.l2_bdaddr.b)); |
| 411 | } |
| 412 | |
| 413 | return QBluetoothAddress(); |
| 414 | } |
| 415 | |
| 416 | quint16 QBluetoothSocketPrivateBluez::localPort() const |
| 417 | { |
| 418 | if (socketType == QBluetoothServiceInfo::RfcommProtocol) { |
| 419 | sockaddr_rc addr; |
| 420 | socklen_t addrLength = sizeof(addr); |
| 421 | |
| 422 | if (::getsockname(fd: socket, addr: reinterpret_cast<sockaddr *>(&addr), len: &addrLength) == 0) |
| 423 | return addr.rc_channel; |
| 424 | } else if (socketType == QBluetoothServiceInfo::L2capProtocol) { |
| 425 | sockaddr_l2 addr; |
| 426 | socklen_t addrLength = sizeof(addr); |
| 427 | |
| 428 | if (::getsockname(fd: socket, addr: reinterpret_cast<sockaddr *>(&addr), len: &addrLength) == 0) |
| 429 | return addr.l2_psm; |
| 430 | } |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | QString QBluetoothSocketPrivateBluez::peerName() const |
| 436 | { |
| 437 | quint64 bdaddr; |
| 438 | |
| 439 | if (socketType == QBluetoothServiceInfo::RfcommProtocol) { |
| 440 | sockaddr_rc addr; |
| 441 | socklen_t addrLength = sizeof(addr); |
| 442 | |
| 443 | if (::getpeername(fd: socket, addr: reinterpret_cast<sockaddr *>(&addr), len: &addrLength) < 0) |
| 444 | return QString(); |
| 445 | |
| 446 | convertAddress(from: addr.rc_bdaddr.b, to: &bdaddr); |
| 447 | } else if (socketType == QBluetoothServiceInfo::L2capProtocol) { |
| 448 | sockaddr_l2 addr; |
| 449 | socklen_t addrLength = sizeof(addr); |
| 450 | |
| 451 | if (::getpeername(fd: socket, addr: reinterpret_cast<sockaddr *>(&addr), len: &addrLength) < 0) |
| 452 | return QString(); |
| 453 | |
| 454 | convertAddress(from: addr.l2_bdaddr.b, to: &bdaddr); |
| 455 | } else { |
| 456 | qCWarning(QT_BT_BLUEZ) << "peerName() called on socket of unknown type" ; |
| 457 | return QString(); |
| 458 | } |
| 459 | |
| 460 | const QString peerAddress = QBluetoothAddress(bdaddr).toString(); |
| 461 | |
| 462 | initializeBluez5(); |
| 463 | OrgFreedesktopDBusObjectManagerInterface manager( |
| 464 | QStringLiteral("org.bluez" ), QStringLiteral("/" ), QDBusConnection::systemBus()); |
| 465 | QDBusPendingReply<ManagedObjectList> reply = manager.GetManagedObjects(); |
| 466 | reply.waitForFinished(); |
| 467 | if (reply.isError()) |
| 468 | return QString(); |
| 469 | |
| 470 | ManagedObjectList managedObjectList = reply.value(); |
| 471 | for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); |
| 472 | it != managedObjectList.constEnd(); ++it) { |
| 473 | const InterfaceList &ifaceList = it.value(); |
| 474 | |
| 475 | for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); |
| 476 | ++jt) { |
| 477 | const QString &iface = jt.key(); |
| 478 | const QVariantMap &ifaceValues = jt.value(); |
| 479 | |
| 480 | if (iface == QStringLiteral("org.bluez.Device1" )) { |
| 481 | if (ifaceValues.value(QStringLiteral("Address" )).toString() == peerAddress) |
| 482 | return ifaceValues.value(QStringLiteral("Alias" )).toString(); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | return QString(); |
| 487 | } |
| 488 | |
| 489 | QBluetoothAddress QBluetoothSocketPrivateBluez::peerAddress() const |
| 490 | { |
| 491 | if (socketType == QBluetoothServiceInfo::RfcommProtocol) { |
| 492 | sockaddr_rc addr; |
| 493 | socklen_t addrLength = sizeof(addr); |
| 494 | |
| 495 | if (::getpeername(fd: socket, addr: reinterpret_cast<sockaddr *>(&addr), len: &addrLength) == 0) |
| 496 | return QBluetoothAddress(convertAddress(from: addr.rc_bdaddr.b)); |
| 497 | } else if (socketType == QBluetoothServiceInfo::L2capProtocol) { |
| 498 | sockaddr_l2 addr; |
| 499 | socklen_t addrLength = sizeof(addr); |
| 500 | |
| 501 | if (::getpeername(fd: socket, addr: reinterpret_cast<sockaddr *>(&addr), len: &addrLength) == 0) |
| 502 | return QBluetoothAddress(convertAddress(from: addr.l2_bdaddr.b)); |
| 503 | } |
| 504 | |
| 505 | return QBluetoothAddress(); |
| 506 | } |
| 507 | |
| 508 | quint16 QBluetoothSocketPrivateBluez::peerPort() const |
| 509 | { |
| 510 | if (socketType == QBluetoothServiceInfo::RfcommProtocol) { |
| 511 | sockaddr_rc addr; |
| 512 | socklen_t addrLength = sizeof(addr); |
| 513 | |
| 514 | if (::getpeername(fd: socket, addr: reinterpret_cast<sockaddr *>(&addr), len: &addrLength) == 0) |
| 515 | return addr.rc_channel; |
| 516 | } else if (socketType == QBluetoothServiceInfo::L2capProtocol) { |
| 517 | sockaddr_l2 addr; |
| 518 | socklen_t addrLength = sizeof(addr); |
| 519 | |
| 520 | if (::getpeername(fd: socket, addr: reinterpret_cast<sockaddr *>(&addr), len: &addrLength) == 0) |
| 521 | return addr.l2_psm; |
| 522 | } |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | qint64 QBluetoothSocketPrivateBluez::writeData(const char *data, qint64 maxSize) |
| 528 | { |
| 529 | Q_Q(QBluetoothSocket); |
| 530 | |
| 531 | if (state != QBluetoothSocket::SocketState::ConnectedState) { |
| 532 | errorString = QBluetoothSocket::tr(s: "Cannot write while not connected" ); |
| 533 | q->setSocketError(QBluetoothSocket::SocketError::OperationError); |
| 534 | return -1; |
| 535 | } |
| 536 | |
| 537 | if (q->openMode() & QIODevice::Unbuffered) { |
| 538 | auto sz = ::qt_safe_write(fd: socket, data, len: maxSize); |
| 539 | if (sz < 0) { |
| 540 | switch (errno) { |
| 541 | case EAGAIN: |
| 542 | sz = 0; |
| 543 | break; |
| 544 | default: |
| 545 | errorString = QBluetoothSocket::tr(s: "Network Error: %1" ).arg(a: qt_error_string(errno)); |
| 546 | q->setSocketError(QBluetoothSocket::SocketError::NetworkError); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if (sz > 0) |
| 551 | emit q->bytesWritten(bytes: sz); |
| 552 | |
| 553 | return sz; |
| 554 | } |
| 555 | else { |
| 556 | |
| 557 | if(!connectWriteNotifier) |
| 558 | return -1; |
| 559 | |
| 560 | if(txBuffer.size() == 0) { |
| 561 | connectWriteNotifier->setEnabled(true); |
| 562 | QMetaObject::invokeMethod(obj: this, member: "_q_writeNotify" , c: Qt::QueuedConnection); |
| 563 | } |
| 564 | |
| 565 | char *txbuf = txBuffer.reserve(size: maxSize); |
| 566 | memcpy(dest: txbuf, src: data, n: maxSize); |
| 567 | |
| 568 | return maxSize; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | qint64 QBluetoothSocketPrivateBluez::readData(char *data, qint64 maxSize) |
| 573 | { |
| 574 | Q_Q(QBluetoothSocket); |
| 575 | |
| 576 | if (state != QBluetoothSocket::SocketState::ConnectedState) { |
| 577 | errorString = QBluetoothSocket::tr(s: "Cannot read while not connected" ); |
| 578 | q->setSocketError(QBluetoothSocket::SocketError::OperationError); |
| 579 | return -1; |
| 580 | } |
| 581 | |
| 582 | if (!rxBuffer.isEmpty()) |
| 583 | return rxBuffer.read(target: data, size: maxSize); |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | void QBluetoothSocketPrivateBluez::close() |
| 589 | { |
| 590 | // If we have pending data on the write buffer, wait until it has been written, |
| 591 | // after which this close() will be called again |
| 592 | if (txBuffer.size() > 0) |
| 593 | connectWriteNotifier->setEnabled(true); |
| 594 | else |
| 595 | abort(); |
| 596 | } |
| 597 | |
| 598 | bool QBluetoothSocketPrivateBluez::setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType_, |
| 599 | QBluetoothSocket::SocketState socketState, QBluetoothSocket::OpenMode openMode) |
| 600 | { |
| 601 | Q_Q(QBluetoothSocket); |
| 602 | delete readNotifier; |
| 603 | readNotifier = nullptr; |
| 604 | delete connectWriteNotifier; |
| 605 | connectWriteNotifier = nullptr; |
| 606 | |
| 607 | socketType = socketType_; |
| 608 | if (socket != -1) |
| 609 | QT_CLOSE(fd: socket); |
| 610 | |
| 611 | socket = socketDescriptor; |
| 612 | |
| 613 | // ensure that O_NONBLOCK is set on new connections. |
| 614 | int flags = fcntl(fd: socket, F_GETFL, 0); |
| 615 | if (!(flags & O_NONBLOCK)) |
| 616 | fcntl(fd: socket, F_SETFL, flags | O_NONBLOCK); |
| 617 | |
| 618 | readNotifier = new QSocketNotifier(socket, QSocketNotifier::Read); |
| 619 | QObject::connect(sender: readNotifier, SIGNAL(activated(QSocketDescriptor)), receiver: this, SLOT(_q_readNotify())); |
| 620 | connectWriteNotifier = new QSocketNotifier(socket, QSocketNotifier::Write, q); |
| 621 | QObject::connect(sender: connectWriteNotifier, SIGNAL(activated(QSocketDescriptor)), receiver: this, SLOT(_q_writeNotify())); |
| 622 | |
| 623 | q->setOpenMode(openMode); |
| 624 | q->setSocketState(socketState); |
| 625 | |
| 626 | return true; |
| 627 | } |
| 628 | |
| 629 | qint64 QBluetoothSocketPrivateBluez::bytesAvailable() const |
| 630 | { |
| 631 | return rxBuffer.size(); |
| 632 | } |
| 633 | |
| 634 | qint64 QBluetoothSocketPrivateBluez::bytesToWrite() const |
| 635 | { |
| 636 | return txBuffer.size(); |
| 637 | } |
| 638 | |
| 639 | bool QBluetoothSocketPrivateBluez::canReadLine() const |
| 640 | { |
| 641 | return rxBuffer.canReadLine(); |
| 642 | } |
| 643 | |
| 644 | QT_END_NAMESPACE |
| 645 | |
| 646 | #include "moc_qbluetoothsocket_bluez_p.cpp" |
| 647 | |