void QBluetoothSocketPrivate::controlReply(ppsResult result) { Q_Q(QBluetoothSocket); if (result.msg == QStringLiteral("connect_service")) { if (!result.errorMsg.isEmpty()) { qWarning() << Q_FUNC_INFO << "Error connecting to service:" << result.errorMsg; errorString = result.errorMsg; socketError = QBluetoothSocket::UnknownSocketError; emit q->error(QBluetoothSocket::UnknownSocketError); q->setSocketState(QBluetoothSocket::UnconnectedState); return; } else { qBBBluetoothDebug() << Q_FUNC_INFO << "Sending request for mount point"; ppsSendControlMessage("get_mount_point_path", 0x1101, m_uuid, m_peerAddress.toString(), this, BT_SPP_CLIENT_SUBTYPE); } } else if (result.msg == QStringLiteral("get_mount_point_path")) { QString path; path = result.dat.first(); qBBBluetoothDebug() << Q_FUNC_INFO << "PATH is" << path; if (!result.errorMsg.isEmpty()) { qWarning() << Q_FUNC_INFO << result.errorMsg; errorString = result.errorMsg; socketError = QBluetoothSocket::UnknownSocketError; emit q->error(QBluetoothSocket::UnknownSocketError); q->setSocketState(QBluetoothSocket::UnconnectedState); return; } else { qBBBluetoothDebug() << "Mount point path is:" << path; socket = ::open(path.toStdString().c_str(), O_RDWR); if (socket == -1) { errorString = QString::fromLocal8Bit(strerror(errno)); qDebug() << Q_FUNC_INFO << socket << " error:" << errno << errorString; //TODO Try if this actually works emit q->error(QBluetoothSocket::UnknownSocketError); q->disconnectFromService(); q->setSocketState(QBluetoothSocket::UnconnectedState); return; } Q_Q(QBluetoothSocket); readNotifier = new QSocketNotifier(socket, QSocketNotifier::Read); QObject::connect(readNotifier, SIGNAL(activated(int)), q, SLOT(_q_readNotify())); connectWriteNotifier = new QSocketNotifier(socket, QSocketNotifier::Write, q); QObject::connect(connectWriteNotifier, SIGNAL(activated(int)), q, SLOT(_q_writeNotify())); connectWriteNotifier->setEnabled(true); readNotifier->setEnabled(true); emit q->connected(); ppsRegisterForEvent(QStringLiteral("service_disconnected"),this); } } }
QVariant ppsReadSetting(const char *property) { int settingsFD; char buf[ppsBufferSize]; if ((settingsFD = qt_safe_open(btSettingsFDPath, O_RDONLY)) == -1) { qWarning() << Q_FUNC_INFO << "failed to open "<< btSettingsFDPath; return QVariant(); } QVariant result; qt_safe_read( settingsFD, &buf, sizeof(buf)); pps_decoder_t decoder; pps_decoder_initialize(&decoder, 0); if (pps_decoder_parse_pps_str(&decoder, buf) == PPS_DECODER_OK) { pps_decoder_push(&decoder, 0); pps_node_type_t nodeType = pps_decoder_type(&decoder, property); if (nodeType == PPS_TYPE_STRING) { const char *dat; if (pps_decoder_get_string(&decoder, property, &dat) == PPS_DECODER_OK) { result = QString::fromUtf8(dat); qBBBluetoothDebug() << "Read setting" << result; } else { qWarning() << Q_FUNC_INFO << "could not read"<< property; return QVariant(); } } else if (nodeType == PPS_TYPE_BOOL) { bool dat; if (pps_decoder_get_bool(&decoder, property, &dat) == PPS_DECODER_OK) { result = dat; qBBBluetoothDebug() << "Read setting" << result; } else { qWarning() << Q_FUNC_INFO << "could not read"<< property; return QVariant(); } } else if (nodeType == PPS_TYPE_NUMBER) { int dat; if (pps_decoder_get_int(&decoder, property, &dat) == PPS_DECODER_OK) { result = dat; qBBBluetoothDebug() << "Read setting" << result; } else { qWarning() << Q_FUNC_INFO << "could not read"<< property; return QVariant(); } } else { qBBBluetoothDebug() << Q_FUNC_INFO << "unrecognized entry for settings"; } } pps_decoder_cleanup(&decoder); qt_safe_close(settingsFD); return result; }
void QBluetoothLocalDevicePrivate::setHostMode(QBluetoothLocalDevice::HostMode mode) { if (!isValid()) return; QBluetoothLocalDevice::HostMode currentHostMode = hostMode(); if (currentHostMode == mode){ return; } //If the device is in PowerOff state and the profile is changed then the power has to be turned on if (currentHostMode == QBluetoothLocalDevice::HostPoweredOff) { qBBBluetoothDebug() << "Powering on"; powerOn(); } if (mode == QBluetoothLocalDevice::HostPoweredOff) { powerOff(); } else if (mode == QBluetoothLocalDevice::HostDiscoverable) { //General discoverable and connectable. setAccess(1); } else if (mode == QBluetoothLocalDevice::HostConnectable) { //Connectable but not discoverable. setAccess(3); } else if (mode == QBluetoothLocalDevice::HostDiscoverableLimitedInquiry) { //Limited discoverable and connectable. setAccess(2); } }
bool ppsReadRemoteDevice(int fd, pps_decoder_t *decoder, QBluetoothAddress *btAddr, QString *deviceName) { char buf[ppsBufferSize * 2]; char addr_buf[18]; addr_buf[17] = '\0'; if (qt_safe_read(fd, &buf, sizeof(buf)) == -1) { qWarning() << Q_FUNC_INFO << "Could not qt_safe_read from pps remote device file"; return false; } qBBBluetoothDebug() << "Remote device" << buf; //the address of the BT device is stored at the beginning of the qt_safe_read if (buf[0] != '-') { memcpy(&addr_buf, &buf[1], 17); } else { //The device was removed memcpy(&addr_buf, &buf[2], 17); return false; } *btAddr = QBluetoothAddress(QString::fromUtf8(addr_buf)); if (pps_decoder_parse_pps_str(decoder, buf) == PPS_DECODER_OK) { const char* name; pps_decoder_push(decoder, 0); if (pps_decoder_get_string(decoder, "name", &name) == PPS_DECODER_OK) (*deviceName) = QString::fromUtf8(name); return true; } return false; }
void QBluetoothLocalDevicePrivate::controlReply(ppsResult result) { qBBBluetoothDebug() << "Ldef reply" << result.msg << result.dat; if (!result.errorMsg.isEmpty()) { qWarning() << Q_FUNC_INFO << result.errorMsg; q_ptr->error(QBluetoothLocalDevice::UnknownError); } }
void QBluetoothLocalDevicePrivate::controlEvent(ppsResult result) { if (result.msg == QString("access_changed")) { if (__newHostMode == -1 && result.dat.size() > 1 && result.dat.first() == "level") { qBBBluetoothDebug() << "New Host mode" << hostMode(); Q_EMIT q_ptr->hostModeStateChanged(hostMode()); } } }
void ppsRegisterControl() { qBBBluetoothDebug() << "Register for Control"; if (count == 0) { ppsCtrlFD = qt_safe_open(btControlFDPath, O_RDWR | O_NONBLOCK); if (ppsCtrlFD == -1) { qWarning() << Q_FUNC_INFO << "ppsCtrlFD - failed to qt_safe_open" << btControlFDPath; } else { ppsCtrlNotifier = new QSocketNotifier(ppsCtrlFD, QSocketNotifier::Read); QObject::connect(ppsCtrlNotifier, SIGNAL(activated(int)), &bbSocketNotifier, SLOT(distribute())); } }
bool endCtrlMessage(pps_encoder_t *encoder) { qBBBluetoothDebug() << "writing" << pps_encoder_buffer(encoder); if (pps_encoder_buffer(encoder) != 0) { int res = qt_safe_write(ppsCtrlFD, pps_encoder_buffer(encoder), pps_encoder_length(encoder)); if (res == -1) { qWarning() << Q_FUNC_INFO << "Error when writing to control FD. Is Bluetooth powerd on?" << errno << ppsCtrlFD << count; return false; } } pps_encoder_cleanup( encoder ); return true; }
void QBluetoothSocketPrivate::connectToService(const QBluetoothAddress &address, QBluetoothUuid uuid, QIODevice::OpenMode openMode) { Q_UNUSED(openMode); qBBBluetoothDebug() << "Connecting socket"; if (isServerSocket) { m_peerAddress = address; m_uuid = uuid; return; } if (state != QBluetoothSocket::UnconnectedState) { qBBBluetoothDebug() << "Socket already connected"; return; } state = QBluetoothSocket::ConnectingState; m_uuid = uuid; m_peerAddress = address; ppsSendControlMessage("connect_service", 0x1101, uuid, address.toString(), this, BT_SPP_CLIENT_SUBTYPE); ppsRegisterForEvent(QStringLiteral("service_connected"),this); ppsRegisterForEvent(QStringLiteral("get_mount_point_path"),this); socketType = QBluetoothSocket::RfcommSocket; }
void ppsRegisterControl() { count++; if (count == 1) { if (ppsCtrlFD != -1) { qBBBluetoothDebug() << "PPS control FD not properly deinitialized"; return; } ppsCtrlFD = qt_safe_open(btControlFDPath, O_RDWR | O_SYNC); if (ppsCtrlFD == -1) { qWarning() << Q_FUNC_INFO << "ppsCtrlFD - failed to qt_safe_open" << btControlFDPath; } else { ppsCtrlNotifier = new QSocketNotifier(ppsCtrlFD, QSocketNotifier::Read); QObject::connect(ppsCtrlNotifier, SIGNAL(activated(int)), &bbSocketNotifier, SLOT(distribute())); } } }
void QBluetoothSocketPrivate::abort() { Q_Q(QBluetoothSocket); qBBBluetoothDebug() << "Disconnecting service"; if (q->state() != QBluetoothSocket::ClosingState) ppsSendControlMessage("disconnect_service", 0x1101, m_uuid, m_peerAddress.toString(), 0, isServerSocket ? BT_SPP_SERVER_SUBTYPE : BT_SPP_CLIENT_SUBTYPE); delete readNotifier; readNotifier = 0; delete connectWriteNotifier; connectWriteNotifier = 0; ::close(socket); q->setSocketState(QBluetoothSocket::UnconnectedState); Q_EMIT q->disconnected(); isServerSocket = false; }
QVariant ppsRemoteDeviceStatus(const QByteArray &address, const char *property) { int rmFD; char buf[ppsBufferSize]; QByteArray filename = btRemoteDevFDPath; filename.append(address); if ((rmFD = qt_safe_open(filename.constData(), O_RDONLY)) < 0) { qWarning() << Q_FUNC_INFO << "failed to open "<< btRemoteDevFDPath << address; return false; } QVariant res; qt_safe_read(rmFD, &buf, sizeof(buf)); pps_decoder_t ppsDecoder; pps_decoder_initialize(&ppsDecoder, 0); if (pps_decoder_parse_pps_str(&ppsDecoder, buf) == PPS_DECODER_OK) { pps_decoder_push(&ppsDecoder, 0); //Find out about the node type pps_node_type_t nodeType = pps_decoder_type(&ppsDecoder, property); if (nodeType == PPS_TYPE_STRING) { const char *dat; pps_decoder_get_string(&ppsDecoder,property,&dat); res = QString::fromUtf8(dat); } else if (nodeType == PPS_TYPE_BOOL) { bool dat; pps_decoder_get_bool(&ppsDecoder,property,&dat); res = QVariant(dat); } else { qBBBluetoothDebug() << "RDStatus: No node type" << property; } } pps_decoder_cleanup(&ppsDecoder); qt_safe_close(rmFD); return res; }
void BBSocketNotifier::distribute() { qBBBluetoothDebug() << "Distributing"; ppsDecodeControlResponse(); }
void ppsDecodeControlResponse() { ppsResult result; ResultType resType = UNKNOWN; if (ppsCtrlFD != -1) { char buf[ppsBufferSize]; qt_safe_read(ppsCtrlFD, &buf, sizeof(buf) ); if (buf[0] != '@') return; qBBBluetoothDebug() << "CTRL Response" << buf; pps_decoder_t ppsDecoder; pps_decoder_initialize(&ppsDecoder, 0); if (pps_decoder_parse_pps_str(&ppsDecoder, buf) == PPS_DECODER_OK) { pps_decoder_push(&ppsDecoder, 0); const char *buf; //The pps response can either be of type 'res', 'msg' or 'evt' if (pps_decoder_get_string(&ppsDecoder, "res", &buf) == PPS_DECODER_OK) { result.msg = QString::fromUtf8(buf); resType = RESPONSE; } else if (pps_decoder_get_string(&ppsDecoder, "msg", &buf) == PPS_DECODER_OK) { result.msg = QString::fromUtf8(buf); resType = MESSAGE; } else if (pps_decoder_get_string(&ppsDecoder, "evt", &buf) == PPS_DECODER_OK) { result.msg = QString::fromUtf8(buf); resType = EVENT; } if (pps_decoder_get_string(&ppsDecoder, "id", &buf) == PPS_DECODER_OK) result.id = QString::fromUtf8(buf).toInt(); //read out the error message if there is one if (pps_decoder_get_string(&ppsDecoder, "errstr", &buf) == PPS_DECODER_OK) result.errorMsg = QString::fromUtf8(buf); int dat; if (pps_decoder_get_int(&ppsDecoder, "err", &dat) == PPS_DECODER_OK) { result.error = dat; } //The dat object can be either a string or a array pps_node_type_t nodeType = pps_decoder_type(&ppsDecoder,"dat"); if (nodeType == PPS_TYPE_STRING) { pps_decoder_get_string(&ppsDecoder,"dat",&buf); result.dat << QString::fromUtf8(buf); } else if (nodeType == PPS_TYPE_OBJECT || nodeType == PPS_TYPE_ARRAY) { pps_decoder_push(&ppsDecoder,"dat"); pps_decoder_goto_index(&ppsDecoder, 0); int len = pps_decoder_length(&ppsDecoder); for (int i = 0; i < len; ++i) { switch ( pps_decoder_type(&ppsDecoder, 0)) { case PPS_TYPE_STRING: result.dat << QString::fromUtf8(pps_decoder_name(&ppsDecoder)); pps_decoder_get_string(&ppsDecoder, 0, &buf); result.dat << QString::fromUtf8(buf); break; case PPS_TYPE_NUMBER: result.dat << QString::fromUtf8(pps_decoder_name(&ppsDecoder)); double dvalue; pps_decoder_get_double(&ppsDecoder, 0, &dvalue); result.dat << QString::number(dvalue); break; default: pps_decoder_next(&ppsDecoder); } } } else { qBBBluetoothDebug() << "Control Response: No node type" << result.msg; } } pps_decoder_cleanup(&ppsDecoder); } if (result.msg == QStringLiteral("radio_init")) { qBBBluetoothDebug() << "Radio initialized"; } else if (result.msg == QStringLiteral("access_changed") && __newHostMode != -1) { qBBBluetoothDebug() << "Access changed after radio init"; ppsSendControlMessage("set_access", QStringLiteral("{\"access\":%1}").arg(__newHostMode), 0); __newHostMode = -1; } if (resType == RESPONSE) { QPair<int, QObject*> wMessage = takeObjectInWList(result.id); if (wMessage.second != 0) wMessage.second->metaObject()->invokeMethod(wMessage.second, "controlReply", Q_ARG(ppsResult, result)); } else if (resType == EVENT) { //qBBBluetoothDebug() << "Distributing event" << result.msg; for (int i=0; i < evtRegistration.size(); i++) { if (result.msg == evtRegistration.at(i).first) evtRegistration.at(i).second->metaObject()->invokeMethod(evtRegistration.at(i).second, "controlEvent", Q_ARG(ppsResult, result)); } } }