qserialport_android.cpp 19.9 KB
Newer Older
dogmaphobic's avatar
dogmaphobic committed
1 2 3 4 5
/****************************************************************************
**
** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
** Copyright (C) 2012 Andre Hartmann <aha_1980@gmx.de>
dogmaphobic's avatar
dogmaphobic committed
6
** Contact: http://www.qt.io/licensing/
dogmaphobic's avatar
dogmaphobic committed
7 8 9
**
** This file is part of the QtSerialPort module of the Qt Toolkit.
**
dogmaphobic's avatar
dogmaphobic committed
10
** $QT_BEGIN_LICENSE:LGPL21$
dogmaphobic's avatar
dogmaphobic committed
11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
dogmaphobic's avatar
dogmaphobic committed
15 16 17
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
dogmaphobic's avatar
dogmaphobic committed
18 19 20
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
dogmaphobic's avatar
dogmaphobic committed
21 22 23 24 25 26
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
dogmaphobic's avatar
dogmaphobic committed
27
**
dogmaphobic's avatar
dogmaphobic committed
28 29
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
dogmaphobic's avatar
dogmaphobic committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

//  Written by: S. Michael Goza 2014
//  Adapted for QGC by: Gus Grubba 2015


#include <errno.h>
#include <stdio.h>

#include <QtCore/qelapsedtimer.h>
#include <QtCore/qsocketnotifier.h>
#include <QtCore/qmap.h>
#include <QtAndroidExtras/QtAndroidExtras>
#include <QtAndroidExtras/QAndroidJniObject>

#include "qserialport_android_p.h"

51 52
QGC_LOGGING_CATEGORY(AndroidSerialPortLog, "AndroidSerialPortLog")

dogmaphobic's avatar
dogmaphobic committed
53 54 55 56
QT_BEGIN_NAMESPACE

#define BAD_PORT 0

dogmaphobic's avatar
dogmaphobic committed
57 58
static const char kJniClassName[] {"org/qgroundcontrol/qgchelper/UsbDeviceJNI"};
static const char kJTag[] {"QGC_QSerialPort"};
dogmaphobic's avatar
dogmaphobic committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

static void jniDeviceHasDisconnected(JNIEnv *envA, jobject thizA, jint userDataA)
{
    Q_UNUSED(envA);
    Q_UNUSED(thizA);
    if (userDataA != 0)
        ((QSerialPortPrivate *)userDataA)->q_ptr->close();
}

static void jniDeviceNewData(JNIEnv *envA, jobject thizA, jint userDataA, jbyteArray dataA)
{
    Q_UNUSED(thizA);
    if (userDataA != 0)
    {
        jbyte *bytesL = envA->GetByteArrayElements(dataA, NULL);
        jsize lenL = envA->GetArrayLength(dataA);
        ((QSerialPortPrivate *)userDataA)->newDataArrived((char *)bytesL, lenL);
        envA->ReleaseByteArrayElements(dataA, bytesL, JNI_ABORT);
    }
}

static void jniDeviceException(JNIEnv *envA, jobject thizA, jint userDataA, jstring messageA)
{
    Q_UNUSED(thizA);
    if(userDataA != 0)
    {
        const char *stringL = envA->GetStringUTFChars(messageA, NULL);
        QString strL = QString::fromUtf8(stringL);
        envA->ReleaseStringUTFChars(messageA, stringL);
        if(envA->ExceptionCheck())
            envA->ExceptionClear();
        ((QSerialPortPrivate *)userDataA)->exceptionArrived(strL);
    }
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static void jniLogDebug(JNIEnv *envA, jobject thizA, jstring messageA)
{
    Q_UNUSED(thizA);

    const char *stringL = envA->GetStringUTFChars(messageA, NULL);
    QString logMessage = QString::fromUtf8(stringL);
    envA->ReleaseStringUTFChars(messageA, stringL);
    if (envA->ExceptionCheck())
        envA->ExceptionClear();
    qCDebug(AndroidSerialPortLog) << logMessage;
}

static void jniLogWarning(JNIEnv *envA, jobject thizA, jstring messageA)
{
    Q_UNUSED(thizA);

    const char *stringL = envA->GetStringUTFChars(messageA, NULL);
    QString logMessage = QString::fromUtf8(stringL);
    envA->ReleaseStringUTFChars(messageA, stringL);
    if (envA->ExceptionCheck())
        envA->ExceptionClear();
    qWarning() << logMessage;
}

dogmaphobic's avatar
dogmaphobic committed
118 119 120 121 122 123 124 125 126
void cleanJavaException()
{
    QAndroidJniEnvironment env;
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();
    }
}

dogmaphobic's avatar
dogmaphobic committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140
QSerialPortPrivate::QSerialPortPrivate(QSerialPort *q)
    : QSerialPortPrivateData(q)
    , descriptor(-1)
    , isCustomBaudRateSupported(false)
    , emittedBytesWritten(false)
    , pendingBytesWritten(0)
    , jniDataBits(8)
    , jniStopBits(1)
    , jniParity(0)
    , internalWriteTimeoutMsec(0)
    , isReadStopped(true)
{
}

Don Gagne's avatar
Don Gagne committed
141 142
void QSerialPortPrivate::setNativeMethods(void)
{
143
    qCDebug(AndroidSerialPortLog) << "Registering Native Functions";
Don Gagne's avatar
Don Gagne committed
144 145 146

    //  REGISTER THE C++ FUNCTION WITH JNI
    JNINativeMethod javaMethods[] {
147 148 149 150 151
        {"nativeDeviceHasDisconnected", "(I)V",                     reinterpret_cast<void *>(jniDeviceHasDisconnected)},
        {"nativeDeviceNewData",         "(I[B)V",                   reinterpret_cast<void *>(jniDeviceNewData)},
        {"nativeDeviceException",       "(ILjava/lang/String;)V",   reinterpret_cast<void *>(jniDeviceException)},
        {"qgcLogDebug",                 "(Ljava/lang/String;)V",    reinterpret_cast<void *>(jniLogDebug)},
        {"qgcLogWarning",               "(Ljava/lang/String;)V",    reinterpret_cast<void *>(jniLogWarning)}
Don Gagne's avatar
Don Gagne committed
152 153 154 155 156 157 158 159 160 161
    };

    QAndroidJniEnvironment jniEnv;
    if (jniEnv->ExceptionCheck()) {
        jniEnv->ExceptionDescribe();
        jniEnv->ExceptionClear();
    }

    jclass objectClass = jniEnv->FindClass(kJniClassName);
    if(!objectClass) {
162
        qWarning() << "Couldn't find class:" << kJniClassName;
Don Gagne's avatar
Don Gagne committed
163 164 165 166 167
        return;
    }

    jint val = jniEnv->RegisterNatives(objectClass, javaMethods, sizeof(javaMethods) / sizeof(javaMethods[0]));

168 169 170 171 172
    if (val < 0) {
        qWarning() << "Error registering methods: " << val;
    } else {
        qCDebug(AndroidSerialPortLog) << "Native Functions Registered";
    }
Don Gagne's avatar
Don Gagne committed
173 174 175 176 177 178 179

    if (jniEnv->ExceptionCheck()) {
        jniEnv->ExceptionDescribe();
        jniEnv->ExceptionClear();
    }
}

dogmaphobic's avatar
dogmaphobic committed
180 181 182
bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
{
    rwMode = mode;
183
    qCDebug(AndroidSerialPortLog) << "Opening" << systemLocation.toLatin1().data();
dogmaphobic's avatar
dogmaphobic committed
184 185 186 187 188 189

    QAndroidJniObject jnameL = QAndroidJniObject::fromString(systemLocation);
    cleanJavaException();
    deviceId = QAndroidJniObject::callStaticMethod<jint>(
        kJniClassName,
        "open",
190 191
        "(Landroid/content/Context;Ljava/lang/String;I)I",
        QtAndroid::androidActivity().object(),
dogmaphobic's avatar
dogmaphobic committed
192 193 194 195 196 197 198 199
        jnameL.object<jstring>(),
        (jint)this);
    cleanJavaException();

    isReadStopped = false;

    if (deviceId == BAD_PORT)
    {
200
        qWarning() << "Error opening %s" << systemLocation.toLatin1().data();
dogmaphobic's avatar
dogmaphobic committed
201 202 203
        q_ptr->setError(QSerialPort::DeviceNotFoundError);
        return false;
    }
dogmaphobic's avatar
dogmaphobic committed
204 205 206 207 208 209 210 211 212 213 214 215

    if (rwMode == QIODevice::WriteOnly)
        stopReadThread();

    return true;
}

void QSerialPortPrivate::close()
{
    if (deviceId == BAD_PORT)
        return;

216
    qCDebug(AndroidSerialPortLog) << "Closing" << systemLocation.toLatin1().data();
dogmaphobic's avatar
dogmaphobic committed
217
    cleanJavaException();
dogmaphobic's avatar
dogmaphobic committed
218
    jboolean resultL = QAndroidJniObject::callStaticMethod<jboolean>(
dogmaphobic's avatar
dogmaphobic committed
219
        kJniClassName,
dogmaphobic's avatar
dogmaphobic committed
220 221 222
        "close",
        "(I)Z",
        deviceId);
dogmaphobic's avatar
dogmaphobic committed
223
    cleanJavaException();
dogmaphobic's avatar
dogmaphobic committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

    descriptor = -1;
    isCustomBaudRateSupported = false;
    pendingBytesWritten = 0;
    deviceId = BAD_PORT;

    if (!resultL)
        q_ptr->setErrorString(QStringLiteral("Closing device failed"));
}

bool QSerialPortPrivate::setParameters(int baudRateA, int dataBitsA, int stopBitsA, int parityA)
{
    if (deviceId == BAD_PORT)
    {
        q_ptr->setError(QSerialPort::NotOpenError);
        return false;
    }

dogmaphobic's avatar
dogmaphobic committed
242 243 244 245 246 247 248 249 250 251 252
    cleanJavaException();
    jboolean resultL = QAndroidJniObject::callStaticMethod<jboolean>(
        kJniClassName,
        "setParameters",
        "(IIIII)Z",
        deviceId,
        baudRateA,
        dataBitsA,
        stopBitsA,
        parityA);
    cleanJavaException();
dogmaphobic's avatar
dogmaphobic committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

    if(resultL)
    {
        //  SET THE JNI VALUES TO WHAT WAS SENT
        inputBaudRate = outputBaudRate = baudRateA;
        jniDataBits = dataBitsA;
        jniStopBits = stopBitsA;
        jniParity = parityA;
    }

    return resultL;
}



void QSerialPortPrivate::stopReadThread()
{
    if (isReadStopped)
        return;
dogmaphobic's avatar
dogmaphobic committed
272 273 274 275 276 277 278
    cleanJavaException();
    QAndroidJniObject::callStaticMethod<void>(
        kJniClassName,
        "stopIoManager",
        "(I)V",
        deviceId);
    cleanJavaException();
dogmaphobic's avatar
dogmaphobic committed
279 280 281 282 283 284 285 286 287
    isReadStopped = true;
}



void QSerialPortPrivate::startReadThread()
{
    if (!isReadStopped)
        return;
dogmaphobic's avatar
dogmaphobic committed
288 289 290 291 292 293 294
    cleanJavaException();
    QAndroidJniObject::callStaticMethod<void>(
        kJniClassName,
        "startIoManager",
        "(I)V",
        deviceId);
    cleanJavaException();
dogmaphobic's avatar
dogmaphobic committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    isReadStopped = false;
}

QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals()
{
    return QSerialPort::NoSignal;
}

bool QSerialPortPrivate::setDataTerminalReady(bool set)
{
    if (deviceId == BAD_PORT)
    {
        q_ptr->setError(QSerialPort::NotOpenError);
        return false;
    }
dogmaphobic's avatar
dogmaphobic committed
310 311 312 313 314 315 316 317 318
    cleanJavaException();
    bool res = QAndroidJniObject::callStaticMethod<jboolean>(
        kJniClassName,
        "setDataTerminalReady",
        "(IZ)Z",
        deviceId,
        set);
    cleanJavaException();
    return res;
dogmaphobic's avatar
dogmaphobic committed
319 320 321 322 323 324 325 326 327
}

bool QSerialPortPrivate::setRequestToSend(bool set)
{
    if (deviceId == BAD_PORT)
    {
        q_ptr->setError(QSerialPort::NotOpenError);
        return false;
    }
dogmaphobic's avatar
dogmaphobic committed
328 329 330 331 332 333 334 335 336
    cleanJavaException();
    bool res = QAndroidJniObject::callStaticMethod<jboolean>(
        kJniClassName,
        "setRequestToSend",
        "(IZ)Z",
        deviceId,
        set);
    cleanJavaException();
    return res;
dogmaphobic's avatar
dogmaphobic committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
}

bool QSerialPortPrivate::flush()
{
    return writeDataOneShot();
}

bool QSerialPortPrivate::clear(QSerialPort::Directions directions)
{
    if (deviceId == BAD_PORT)
    {
        q_ptr->setError(QSerialPort::NotOpenError);
        return false;
    }

    bool inputL = false;
    bool outputL = false;

    if (directions == QSerialPort::AllDirections)
        inputL = outputL = true;
    else
    {
        if (directions & QSerialPort::Input)
            inputL = true;

        if (directions & QSerialPort::Output)
            outputL = true;
    }

dogmaphobic's avatar
dogmaphobic committed
366 367 368 369 370 371 372 373
    cleanJavaException();
    bool res = QAndroidJniObject::callStaticMethod<jboolean>(
        kJniClassName,
        "purgeBuffers",
        "(IZZ)Z",
        deviceId,
        inputL,
        outputL);
dogmaphobic's avatar
dogmaphobic committed
374

dogmaphobic's avatar
dogmaphobic committed
375 376 377
    cleanJavaException();
    return res;
}
dogmaphobic's avatar
dogmaphobic committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618

bool QSerialPortPrivate::sendBreak(int duration)
{
    Q_UNUSED(duration);
    return true;
}

bool QSerialPortPrivate::setBreakEnabled(bool set)
{
    Q_UNUSED(set);
    return true;
}

void QSerialPortPrivate::startWriting()
{
    writeDataOneShot();
}

bool QSerialPortPrivate::waitForReadyRead(int msecs)
{
    int origL = readBuffer.size();

    if (origL > 0)
        return true;

    for (int iL=0; iL<msecs; iL++)
    {
        if (origL < readBuffer.size())
            return true;
        else
            QThread::msleep(1);
    }

    return false;
}

bool QSerialPortPrivate::waitForBytesWritten(int msecs)
{
    internalWriteTimeoutMsec = msecs;
    bool retL = writeDataOneShot();
    internalWriteTimeoutMsec = 0;
    return retL;
}

bool QSerialPortPrivate::setBaudRate()
{
    setBaudRate(inputBaudRate, QSerialPort::AllDirections);
    return true;
}

bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
    Q_UNUSED(directions);
    return setParameters(baudRate, jniDataBits, jniStopBits, jniParity);
}

bool QSerialPortPrivate::setDataBits(QSerialPort::DataBits dataBits)
{
    int numBitsL = 8;

    switch (dataBits)
    {
        case QSerialPort::Data5:
            numBitsL = 5;
            break;

        case QSerialPort::Data6:
            numBitsL = 6;
            break;

        case QSerialPort::Data7:
            numBitsL = 7;
            break;

        case QSerialPort::Data8:
        default:
            numBitsL = 8;
            break;
    }
    return setParameters(inputBaudRate, numBitsL, jniStopBits, jniParity);
}

bool QSerialPortPrivate::setParity(QSerialPort::Parity parity)
{
    int parL = 0;
    switch (parity)
    {
        case QSerialPort::SpaceParity:
            parL = 4;
            break;

        case QSerialPort::MarkParity:
            parL = 3;
            break;

        case QSerialPort::EvenParity:
            parL = 2;
            break;

        case QSerialPort::OddParity:
            parL = 1;
            break;

        case QSerialPort::NoParity:
        default:
            parL = 0;
            break;
    }
    return setParameters(inputBaudRate, jniDataBits, jniStopBits, parL);
}

bool QSerialPortPrivate::setStopBits(QSerialPort::StopBits stopBits)
{
    int stopL = 1;
    switch (stopBits)
    {
        case QSerialPort::TwoStop:
            stopL = 2;
            break;

        case QSerialPort::OneAndHalfStop:
            stopL = 3;
            break;

        case QSerialPort::OneStop:
        default:
            stopL = 1;
            break;
    }
    return setParameters(inputBaudRate, jniDataBits, stopL, jniParity);
}

bool QSerialPortPrivate::setFlowControl(QSerialPort::FlowControl flowControl)
{
    Q_UNUSED(flowControl);
    return true;
}

bool QSerialPortPrivate::setDataErrorPolicy(QSerialPort::DataErrorPolicy policy)
{
    this->policy = policy;
    return true;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void QSerialPortPrivate::newDataArrived(char *bytesA, int lengthA)
{
    Q_Q(QSerialPort);

    int bytesToReadL = lengthA;

    // Always buffered, read data from the port into the read buffer
    if (readBufferMaxSize && (bytesToReadL > (readBufferMaxSize - readBuffer.size()))) {
        bytesToReadL = readBufferMaxSize - readBuffer.size();
        if (bytesToReadL <= 0) {
            // Buffer is full. User must read data from the buffer
            // before we can read more from the port.
            stopReadThread();
            return;
        }
    }

    char *ptr = readBuffer.reserve(bytesToReadL);
    memcpy(ptr, bytesA, bytesToReadL);

    emit q->readyRead();
}



void QSerialPortPrivate::exceptionArrived(QString strA)
{
    q_ptr->setErrorString(strA);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool QSerialPortPrivate::writeDataOneShot()
{
    Q_Q(QSerialPort);

    pendingBytesWritten = -1;

    while (!writeBuffer.isEmpty())
    {
        pendingBytesWritten = writeToPort(writeBuffer.readPointer(), writeBuffer.nextDataBlockSize());

        if (pendingBytesWritten <= 0)
        {
            QSerialPort::SerialPortError errorL = decodeSystemError();
            if (errorL != QSerialPort::ResourceError)
                errorL = QSerialPort::WriteError;
            q->setError(errorL);
            return false;
        }

        writeBuffer.free(pendingBytesWritten);

        emit q->bytesWritten(pendingBytesWritten);
    }

    return (pendingBytesWritten < 0)? false: true;
}

QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError() const
{
    QSerialPort::SerialPortError error;
    switch (errno) {
        case ENODEV:
            error = QSerialPort::DeviceNotFoundError;
            break;
        case EACCES:
            error = QSerialPort::PermissionError;
            break;
        case EBUSY:
            error = QSerialPort::PermissionError;
            break;
        case EAGAIN:
            error = QSerialPort::ResourceError;
            break;
        case EIO:
            error = QSerialPort::ResourceError;
            break;
        case EBADF:
            error = QSerialPort::ResourceError;
            break;
        default:
            error = QSerialPort::UnknownError;
            break;
    }
    return error;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
qint64 QSerialPortPrivate::writeToPort(const char *data, qint64 maxSize)
{
    if (deviceId == BAD_PORT)
    {
        q_ptr->setError(QSerialPort::NotOpenError);
        return 0;
    }

dogmaphobic's avatar
dogmaphobic committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632
    QAndroidJniEnvironment jniEnv;
    jbyteArray jarrayL = jniEnv->NewByteArray(maxSize);
    jniEnv->SetByteArrayRegion(jarrayL, 0, maxSize, (jbyte *)data);
    if (jniEnv->ExceptionCheck())
        jniEnv->ExceptionClear();
    int resultL = QAndroidJniObject::callStaticMethod<jint>(
        kJniClassName,
        "write",
        "(I[BI)I",
        deviceId,
        jarrayL,
        internalWriteTimeoutMsec);

    if (jniEnv->ExceptionCheck())
dogmaphobic's avatar
dogmaphobic committed
633
    {
dogmaphobic's avatar
dogmaphobic committed
634
        jniEnv->ExceptionClear();
dogmaphobic's avatar
dogmaphobic committed
635
        q_ptr->setErrorString(QStringLiteral("Writing to the device threw an exception"));
dogmaphobic's avatar
dogmaphobic committed
636
        jniEnv->DeleteLocalRef(jarrayL);
dogmaphobic's avatar
dogmaphobic committed
637 638
        return 0;
    }
dogmaphobic's avatar
dogmaphobic committed
639
    jniEnv->DeleteLocalRef(jarrayL);
dogmaphobic's avatar
dogmaphobic committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
    return resultL;
}

static inline bool evenParity(quint8 c)
{
    c ^= c >> 4;        //(c7 ^ c3)(c6 ^ c2)(c5 ^ c1)(c4 ^ c0)
    c ^= c >> 2;        //[(c7 ^ c3)(c5 ^ c1)][(c6 ^ c2)(c4 ^ c0)]
    c ^= c >> 1;
    return c & 1;       //(c7 ^ c3)(c5 ^ c1)(c6 ^ c2)(c4 ^ c0)
}

typedef QMap<qint32, qint32> BaudRateMap;

// The OS specific defines can be found in termios.h

static const BaudRateMap createStandardBaudRateMap()
{
    BaudRateMap baudRateMap;

#ifdef B50
    baudRateMap.insert(50, B50);
#endif

#ifdef B75
    baudRateMap.insert(75, B75);
#endif

#ifdef B110
    baudRateMap.insert(110, B110);
#endif

#ifdef B134
    baudRateMap.insert(134, B134);
#endif

#ifdef B150
    baudRateMap.insert(150, B150);
#endif

#ifdef B200
    baudRateMap.insert(200, B200);
#endif

#ifdef B300
    baudRateMap.insert(300, B300);
#endif

#ifdef B600
    baudRateMap.insert(600, B600);
#endif

#ifdef B1200
    baudRateMap.insert(1200, B1200);
#endif

#ifdef B1800
    baudRateMap.insert(1800, B1800);
#endif

#ifdef B2400
    baudRateMap.insert(2400, B2400);
#endif

#ifdef B4800
    baudRateMap.insert(4800, B4800);
#endif

#ifdef B7200
    baudRateMap.insert(7200, B7200);
#endif

#ifdef B9600
    baudRateMap.insert(9600, B9600);
#endif

#ifdef B14400
    baudRateMap.insert(14400, B14400);
#endif

#ifdef B19200
    baudRateMap.insert(19200, B19200);
#endif

#ifdef B28800
    baudRateMap.insert(28800, B28800);
#endif

#ifdef B38400
    baudRateMap.insert(38400, B38400);
#endif

#ifdef B57600
    baudRateMap.insert(57600, B57600);
#endif

#ifdef B76800
    baudRateMap.insert(76800, B76800);
#endif

#ifdef B115200
    baudRateMap.insert(115200, B115200);
#endif

#ifdef B230400
    baudRateMap.insert(230400, B230400);
#endif

#ifdef B460800
    baudRateMap.insert(460800, B460800);
#endif

#ifdef B500000
    baudRateMap.insert(500000, B500000);
#endif

#ifdef B576000
    baudRateMap.insert(576000, B576000);
#endif

#ifdef B921600
    baudRateMap.insert(921600, B921600);
#endif

#ifdef B1000000
    baudRateMap.insert(1000000, B1000000);
#endif

#ifdef B1152000
    baudRateMap.insert(1152000, B1152000);
#endif

#ifdef B1500000
    baudRateMap.insert(1500000, B1500000);
#endif

#ifdef B2000000
    baudRateMap.insert(2000000, B2000000);
#endif

#ifdef B2500000
    baudRateMap.insert(2500000, B2500000);
#endif

#ifdef B3000000
    baudRateMap.insert(3000000, B3000000);
#endif

#ifdef B3500000
    baudRateMap.insert(3500000, B3500000);
#endif

#ifdef B4000000
    baudRateMap.insert(4000000, B4000000);
#endif

    return baudRateMap;
}

static const BaudRateMap& standardBaudRateMap()
{
    static const BaudRateMap baudRateMap = createStandardBaudRateMap();
    return baudRateMap;
}

qint32 QSerialPortPrivate::baudRateFromSetting(qint32 setting)
{
    return standardBaudRateMap().key(setting);
}

qint32 QSerialPortPrivate::settingFromBaudRate(qint32 baudRate)
{
    return standardBaudRateMap().value(baudRate);
}

QList<qint32> QSerialPortPrivate::standardBaudRates()
{
    return standardBaudRateMap().keys();
}

QSerialPort::Handle QSerialPort::handle() const
{
    Q_D(const QSerialPort);
    return d->descriptor;
}

qint64 QSerialPortPrivate::bytesToWrite() const
{
    return writeBuffer.size();
}

qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize)
{
    return writeToPort(data, maxSize);
}

QT_END_NAMESPACE