QByteArray QPpsObject::read(bool *ok) { Q_D(QPpsObject); // reset last error d->error = EOK; // abort if file not open if (!isOpen()) { d->error = EBADF; safeAssign(ok, false); return QByteArray(); } const int maxSize = ppsMaxSize->value; if (maxSize == -1) { qWarning() << "QPpsObject::read: maxSize is equal to -1"; safeAssign(ok, false); return QByteArray(); } QByteArray byteArray; byteArray.resize(maxSize); // resize doesn't initialize the data const int result = qt_safe_read(d->fd, byteArray.data(), byteArray.size()); if (result == -1) { d->error = errno; qWarning() << "QPpsObject::read failed to read pps data, error " << errorString(); safeAssign(ok, false); return QByteArray(); // Specifically return a default-constructed QByteArray. } if (result == 0) { // normalize the behavior of read() when no data is ready so a pps object // put in non-blocking mode via opening w/o wait (read returns 0) looks // the same as a pps object put in non-blocking mode by setting O_NONBLOCK // (read returns EAGAIN) d->error = EAGAIN; safeAssign(ok, false); return QByteArray(); // Specifically return a default-constructed QByteArray. } // resize byte array to amount actually read byteArray.resize(result); safeAssign(ok, true); return byteArray; }
qint64 QNativeSocketEnginePrivate::nativeRead(char *data, qint64 maxSize) { Q_Q(QNativeSocketEngine); if (!q->isValid()) { qWarning("QNativeSocketEngine::nativeRead: Invalid socket"); return -1; } ssize_t r = 0; r = qt_safe_read(socketDescriptor, data, maxSize); if (r < 0) { r = -1; switch (errno) { #if EWOULDBLOCK-0 && EWOULDBLOCK != EAGAIN case EWOULDBLOCK: #endif case EAGAIN: // No data was available for reading r = -2; break; case EBADF: case EINVAL: case EIO: //error string is now set in read(), not here in nativeRead() break; case ECONNRESET: #if defined(Q_OS_VXWORKS) case ESHUTDOWN: #endif r = 0; break; default: break; } } #if defined (QNATIVESOCKETENGINE_DEBUG) qDebug("QNativeSocketEnginePrivate::nativeRead(%p \"%s\", %llu) == %zd", data, qt_prettyDebug(data, qMin(r, ssize_t(16)), r).data(), maxSize, r); #endif return qint64(r); }
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) { qCWarning(QT_BT_QNX) << 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 { qCDebug(QT_BT_QNX) << "RDStatus: No node type" << property; } } pps_decoder_cleanup(&ppsDecoder); qt_safe_close(rmFD); return res; }
static QNetworkConfiguration::BearerType cellularStatus() { QNetworkConfiguration::BearerType ret = QNetworkConfiguration::BearerUnknown; int cellularStatusFD; if ((cellularStatusFD = qt_safe_open(cellularStatusFile, O_RDONLY)) == -1) { qWarning() << "failed to open" << cellularStatusFile; return ret; } char buf[2048]; if (qt_safe_read(cellularStatusFD, &buf, sizeof(buf)) == -1) { qWarning() << "read from PPS file failed:" << strerror(errno); qt_safe_close(cellularStatusFD); return ret; } pps_decoder_t ppsDecoder; if (pps_decoder_initialize(&ppsDecoder, buf) != PPS_DECODER_OK) { qWarning("failed to initialize PPS decoder"); qt_safe_close(cellularStatusFD); return ret; } pps_decoder_error_t err; if ((err = pps_decoder_push(&ppsDecoder, 0)) != PPS_DECODER_OK) { qWarning() << "pps_decoder_push failed" << err; pps_decoder_cleanup(&ppsDecoder); qt_safe_close(cellularStatusFD); return ret; } if (!pps_decoder_is_integer(&ppsDecoder, "network_technology")) { qWarning("field has not the expected data type"); pps_decoder_cleanup(&ppsDecoder); qt_safe_close(cellularStatusFD); return ret; } int type; if (!pps_decoder_get_int(&ppsDecoder, "network_technology", &type) == PPS_DECODER_OK) { qWarning("could not read bearer type from PPS"); pps_decoder_cleanup(&ppsDecoder); qt_safe_close(cellularStatusFD); return ret; } switch (type) { case 0: // 0 == NONE break; // unhandled case 1: // fallthrough, 1 == GSM case 4: // 4 == CDMA_1X ret = QNetworkConfiguration::Bearer2G; break; case 2: // 2 == UMTS ret = QNetworkConfiguration::BearerWCDMA; break; case 8: // 8 == EVDO ret = QNetworkConfiguration::BearerEVDO; break; case 16: // 16 == LTE ret = QNetworkConfiguration::BearerLTE; break; default: qWarning() << "unhandled bearer type" << type; break; } pps_decoder_cleanup(&ppsDecoder); qt_safe_close(cellularStatusFD); return ret; }
void QQnxButtonEventNotifier::updateButtonStates() { // Allocate buffer for pps data char buffer[ppsBufferSize]; // Attempt to read pps data errno = 0; int bytes = qt_safe_read(m_fd, buffer, ppsBufferSize - 1); qButtonDebug() << "Read" << bytes << "bytes of data"; if (bytes == -1) { qWarning("QQNX: failed to read hardware buttons pps object, errno=%d", errno); return; } // We seem to get a spurious read notification after the real one. Ignore it if (bytes == 0) return; // Ensure data is null terminated buffer[bytes] = '\0'; qButtonDebug() << Q_FUNC_INFO << "received PPS message:\n" << buffer; // Process received message QByteArray ppsData = QByteArray::fromRawData(buffer, bytes); QHash<QByteArray, QByteArray> fields; if (!parsePPS(ppsData, &fields)) return; // Update our state and inject key events as needed for (int buttonId = bid_minus; buttonId < ButtonCount; ++buttonId) { // Extract the new button state QByteArray key = m_buttonKeys.at(buttonId); ButtonState newState = (fields.value(key) == QByteArrayLiteral("b_up") ? ButtonUp : ButtonDown); // If state has changed, update our state and inject a keypress event if (m_state[buttonId] != newState) { qButtonDebug() << "Hardware button event: button =" << key << "state =" << fields.value(key); m_state[buttonId] = newState; // Is it a key press or key release event? QEvent::Type type = (newState == ButtonDown) ? QEvent::KeyPress : QEvent::KeyRelease; Qt::Key key; switch (buttonId) { case bid_minus: key = Qt::Key_VolumeDown; break; case bid_playpause: key = Qt::Key_Play; break; case bid_plus: key = Qt::Key_VolumeUp; break; case bid_power: key = Qt::Key_PowerDown; break; default: qButtonDebug() << "Unknown hardware button"; continue; } // No modifiers Qt::KeyboardModifiers modifier = Qt::NoModifier; // Post the event QWindowSystemInterface::handleKeyEvent(QGuiApplication::focusWindow(), type, key, modifier); } } }
void QEvdevKeyboardHandler::readKeycode() { struct ::input_event buffer[32]; int n = 0; forever { int result = qt_safe_read(m_fd, reinterpret_cast<char *>(buffer) + n, sizeof(buffer) - n); if (result == 0) { qWarning("evdevkeyboard: Got EOF from the input device"); return; } else if (result < 0) { if (errno != EINTR && errno != EAGAIN) { qErrnoWarning(errno, "evdevkeyboard: Could not read from input device"); // If the device got disconnected, stop reading, otherwise we get flooded // by the above error over and over again. if (errno == ENODEV) { delete m_notify; m_notify = Q_NULLPTR; qt_safe_close(m_fd); m_fd = -1; } return; } } else { n += result; if (n % sizeof(buffer[0]) == 0) break; } } n /= sizeof(buffer[0]); for (int i = 0; i < n; ++i) { if (buffer[i].type != EV_KEY) continue; quint16 code = buffer[i].code; qint32 value = buffer[i].value; QEvdevKeyboardHandler::KeycodeAction ka; ka = processKeycode(code, value != 0, value == 2); switch (ka) { case QEvdevKeyboardHandler::CapsLockOn: case QEvdevKeyboardHandler::CapsLockOff: switchLed(LED_CAPSL, ka == QEvdevKeyboardHandler::CapsLockOn); break; case QEvdevKeyboardHandler::NumLockOn: case QEvdevKeyboardHandler::NumLockOff: switchLed(LED_NUML, ka == QEvdevKeyboardHandler::NumLockOn); break; case QEvdevKeyboardHandler::ScrollLockOn: case QEvdevKeyboardHandler::ScrollLockOff: switchLed(LED_SCROLLL, ka == QEvdevKeyboardHandler::ScrollLockOn); break; default: // ignore console switching and reboot break; } } }
void ppsDecodeControlResponse() { ppsResult result; ResultType resType = UNKNOWN; if (ppsCtrlFD != -1) { char buf[ppsBufferSize]; qt_safe_read(ppsCtrlFD, &buf, sizeof(buf) ); if (buf[0] != '@') return; qCDebug(QT_BT_QNX) << "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 { qCDebug(QT_BT_QNX) << "Control Response: No node type" << result.msg; } } pps_decoder_cleanup(&ppsDecoder); } if (result.msg == QStringLiteral("radio_init")) { qCDebug(QT_BT_QNX) << "Radio initialized"; } else if (result.msg == QStringLiteral("access_changed") && __newHostMode != -1) { qCDebug(QT_BT_QNX) << "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) { //qCDebug(QT_BT_QNX) << "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)); } } }
//FIXME qint64 NativeSerialEnginePrivate::nativeRead(char *data, qint64 len) { /* WARNING!!! Here, I was not able to implement blocking read-through setting termios (see explanation in prepareTimeouts()). Need to implement a reading with this algorithm: 1. If the timeout interval of the symbol is greater than 0, then waiting to read this character. 2. If the timeout interval of the symbol 0, then return the reading immediately! So I had to use ::select(). Other solution I have found. */ qint64 bytesReaded = 0; QTime readDelay; readDelay.start(); bool sfr = false; bool sfw = false; int msecs = this->charIntervalTimeout / 1000; do { qint64 readFromDevice = qt_safe_read(this->descriptor, (void*)data, len - bytesReaded); if (readFromDevice < 0) { bytesReaded = readFromDevice; break; } bytesReaded += readFromDevice; } while ((msecs > 0) && (this->nativeSelect(msecs, true, false, &sfr, &sfw) > 0) && (bytesReaded < len)); if (bytesReaded < 0) { bytesReaded = -1; switch (errno) { #if EWOULDBLOCK-0 && EWOULDBLOCK != EAGAIN case EWOULDBLOCK: #endif case EAGAIN: // No data was available for reading bytesReaded = -2; break; case EBADF: case EINVAL: case EIO: #if defined (NATIVESERIALENGINE_UNIX_DEBUG) qDebug() << "Linux: NativeSerialEnginePrivate::nativeRead(char *data, qint64 len) \n -> read fail: \n" " - bytes readed, bytesReaded: " << bytesReaded << " bytes \n" " - error code, errno: " << errno << " \n. Error! \n"; #endif break; #ifdef Q_OS_SYMBIAN case EPIPE: #endif case ECONNRESET: #if defined (Q_OS_VXWORKS) case ESHUTDOWN: #endif bytesReaded = 0; break; default:; } } return bytesReaded; }