qserialport_android.cpp 19.8 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

57
static const char kJniClassName[] {"org/mavlink/qgroundcontrol/QGCActivity"};
dogmaphobic's avatar
dogmaphobic committed
58

59
static void jniDeviceHasDisconnected(JNIEnv *envA, jobject thizA, jlong userDataA)
dogmaphobic's avatar
dogmaphobic committed
60 61 62 63
{
    Q_UNUSED(envA);
    Q_UNUSED(thizA);
    if (userDataA != 0)
64
        (reinterpret_cast<QSerialPortPrivate*>(userDataA))->q_ptr->close();
dogmaphobic's avatar
dogmaphobic committed
65 66
}

67
static void jniDeviceNewData(JNIEnv *envA, jobject thizA, jlong userDataA, jbyteArray dataA)
dogmaphobic's avatar
dogmaphobic committed
68 69 70 71
{
    Q_UNUSED(thizA);
    if (userDataA != 0)
    {
72
        jbyte *bytesL = envA->GetByteArrayElements(dataA, nullptr);
dogmaphobic's avatar
dogmaphobic committed
73
        jsize lenL = envA->GetArrayLength(dataA);
74
        (reinterpret_cast<QSerialPortPrivate*>(userDataA))->newDataArrived(reinterpret_cast<char*>(bytesL), lenL);
dogmaphobic's avatar
dogmaphobic committed
75 76 77 78
        envA->ReleaseByteArrayElements(dataA, bytesL, JNI_ABORT);
    }
}

79
static void jniDeviceException(JNIEnv *envA, jobject thizA, jlong userDataA, jstring messageA)
dogmaphobic's avatar
dogmaphobic committed
80 81 82 83
{
    Q_UNUSED(thizA);
    if(userDataA != 0)
    {
84
        const char *stringL = envA->GetStringUTFChars(messageA, nullptr);
dogmaphobic's avatar
dogmaphobic committed
85 86 87 88
        QString strL = QString::fromUtf8(stringL);
        envA->ReleaseStringUTFChars(messageA, stringL);
        if(envA->ExceptionCheck())
            envA->ExceptionClear();
89
        (reinterpret_cast<QSerialPortPrivate*>(userDataA))->exceptionArrived(strL);
dogmaphobic's avatar
dogmaphobic committed
90 91 92
    }
}

93 94 95 96
static void jniLogDebug(JNIEnv *envA, jobject thizA, jstring messageA)
{
    Q_UNUSED(thizA);

97
    const char *stringL = envA->GetStringUTFChars(messageA, nullptr);
98 99 100 101 102 103 104 105 106 107 108
    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);

109
    const char *stringL = envA->GetStringUTFChars(messageA, nullptr);
110 111 112 113 114 115 116
    QString logMessage = QString::fromUtf8(stringL);
    envA->ReleaseStringUTFChars(messageA, stringL);
    if (envA->ExceptionCheck())
        envA->ExceptionClear();
    qWarning() << logMessage;
}

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

dogmaphobic's avatar
dogmaphobic committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139
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
140 141
void QSerialPortPrivate::setNativeMethods(void)
{
142
    qCDebug(AndroidSerialPortLog) << "Registering Native Functions";
Don Gagne's avatar
Don Gagne committed
143 144 145

    //  REGISTER THE C++ FUNCTION WITH JNI
    JNINativeMethod javaMethods[] {
146 147 148
        {"nativeDeviceHasDisconnected", "(J)V",                     reinterpret_cast<void *>(jniDeviceHasDisconnected)},
        {"nativeDeviceNewData",         "(J[B)V",                   reinterpret_cast<void *>(jniDeviceNewData)},
        {"nativeDeviceException",       "(JLjava/lang/String;)V",   reinterpret_cast<void *>(jniDeviceException)},
149 150
        {"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
151 152 153 154 155 156 157 158 159 160
    };

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

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

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

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

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

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

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

    isReadStopped = false;

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

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

    return true;
}

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

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

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

    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
271 272 273 274 275 276 277
    cleanJavaException();
    QAndroidJniObject::callStaticMethod<void>(
        kJniClassName,
        "stopIoManager",
        "(I)V",
        deviceId);
    cleanJavaException();
dogmaphobic's avatar
dogmaphobic committed
278 279 280 281 282 283 284 285 286
    isReadStopped = true;
}



void QSerialPortPrivate::startReadThread()
{
    if (!isReadStopped)
        return;
dogmaphobic's avatar
dogmaphobic committed
287 288 289 290 291 292 293
    cleanJavaException();
    QAndroidJniObject::callStaticMethod<void>(
        kJniClassName,
        "startIoManager",
        "(I)V",
        deviceId);
    cleanJavaException();
dogmaphobic's avatar
dogmaphobic committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    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
309 310 311 312 313 314 315 316 317
    cleanJavaException();
    bool res = QAndroidJniObject::callStaticMethod<jboolean>(
        kJniClassName,
        "setDataTerminalReady",
        "(IZ)Z",
        deviceId,
        set);
    cleanJavaException();
    return res;
dogmaphobic's avatar
dogmaphobic committed
318 319 320 321 322 323 324 325 326
}

bool QSerialPortPrivate::setRequestToSend(bool set)
{
    if (deviceId == BAD_PORT)
    {
        q_ptr->setError(QSerialPort::NotOpenError);
        return false;
    }
dogmaphobic's avatar
dogmaphobic committed
327 328 329 330 331 332 333 334 335
    cleanJavaException();
    bool res = QAndroidJniObject::callStaticMethod<jboolean>(
        kJniClassName,
        "setRequestToSend",
        "(IZ)Z",
        deviceId,
        set);
    cleanJavaException();
    return res;
dogmaphobic's avatar
dogmaphobic committed
336 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
}

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

dogmaphobic's avatar
dogmaphobic committed
374 375 376
    cleanJavaException();
    return res;
}
dogmaphobic's avatar
dogmaphobic committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

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)
{
397
    int origL = static_cast<int>(readBuffer.size());
dogmaphobic's avatar
dogmaphobic committed
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

    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()))) {
530
        bytesToReadL = static_cast<int>(readBufferMaxSize - readBuffer.size());
dogmaphobic's avatar
dogmaphobic committed
531 532 533 534 535 536 537 538 539
        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);
540
    memcpy(ptr, bytesA, static_cast<size_t>(bytesToReadL));
dogmaphobic's avatar
dogmaphobic committed
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

    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
618
    QAndroidJniEnvironment jniEnv;
619 620
    jbyteArray jarrayL = jniEnv->NewByteArray(static_cast<jsize>(maxSize));
    jniEnv->SetByteArrayRegion(jarrayL, 0, static_cast<jsize>(maxSize), (jbyte*)data);
dogmaphobic's avatar
dogmaphobic committed
621 622 623 624 625 626 627 628 629 630 631
    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
632
    {
dogmaphobic's avatar
dogmaphobic committed
633
        jniEnv->ExceptionClear();
dogmaphobic's avatar
dogmaphobic committed
634
        q_ptr->setErrorString(QStringLiteral("Writing to the device threw an exception"));
dogmaphobic's avatar
dogmaphobic committed
635
        jniEnv->DeleteLocalRef(jarrayL);
dogmaphobic's avatar
dogmaphobic committed
636 637
        return 0;
    }
dogmaphobic's avatar
dogmaphobic committed
638
    jniEnv->DeleteLocalRef(jarrayL);
dogmaphobic's avatar
dogmaphobic committed
639 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
    return resultL;
}

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