qserialport_p.h 9.5 KB
Newer Older
dogmaphobic's avatar
dogmaphobic committed
1 2 3 4 5
/****************************************************************************
**
** Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com>
** Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com>
** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
dogmaphobic's avatar
dogmaphobic committed
6
** Contact: http://www.qt.io/licensing/
dogmaphobic's avatar
dogmaphobic committed
7 8 9 10 11 12 13 14
**
** This file is part of the QtSerialPort module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** 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 21 22 23 24 25 26 27
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** 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
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 51 52 53 54 55 56 57 58 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QSERIALPORT_P_H
#define QSERIALPORT_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qserialport.h"

QT_BEGIN_NAMESPACE

class QGCRingBuffer
{
public:
    explicit inline QGCRingBuffer(int growth = 4096) :
        head(0), tail(0), tailBuffer(0), basicBlockSize(growth), bufferSize(0) {
        buffers.append(QByteArray());
    }

    inline int nextDataBlockSize() const {
        return (tailBuffer == 0 ? tail : buffers.first().size()) - head;
    }

    inline const char *readPointer() const {
        return buffers.isEmpty() ? 0 : (buffers.first().constData() + head);
    }

    // access the bytes at a specified position
    // the out-variable length will contain the amount of bytes readable
    // from there, e.g. the amount still the same QByteArray
    inline const char *readPointerAtPosition(qint64 pos, qint64 &length) const {
        if (pos >= 0) {
            pos += head;
            for (int i = 0; i < buffers.size(); ++i) {
                length = (i == tailBuffer ? tail : buffers[i].size());
                if (length > pos) {
                    length -= pos;
                    return buffers[i].constData() + pos;
                }
                pos -= length;
            }
        }

        length = 0;
        return 0;
    }

    inline void free(int bytes) {
        while (bytes > 0) {
            int blockSize = buffers.first().size() - head;

            if (tailBuffer == 0 || blockSize > bytes) {
                bufferSize -= bytes;
                if (bufferSize <= 0)
                    clear(); // try to minify/squeeze us
                else
                    head += bytes;
                return;
            }

            bufferSize -= blockSize;
            bytes -= blockSize;
            buffers.removeFirst();
            --tailBuffer;
            head = 0;
        }
    }

    inline char *reserve(int bytes) {
        if (bytes <= 0)
            return 0;

        // if need buffer reallocation
        if (tail + bytes > buffers.last().size()) {
            if (tail >= basicBlockSize) {
                // shrink this buffer to its current size
                buffers.last().resize(tail);

                // create a new QByteArray
                buffers.append(QByteArray());
                ++tailBuffer;
                tail = 0;
            }
            buffers.last().resize(qMax(basicBlockSize, tail + bytes));
        }

        char *writePtr = buffers.last().data() + tail;
        bufferSize += bytes;
        tail += bytes;
        return writePtr;
    }

    inline void truncate(int pos) {
        if (pos < size())
            chop(size() - pos);
    }

    inline void chop(int bytes) {
        while (bytes > 0) {
            if (tailBuffer == 0 || tail > bytes) {
                bufferSize -= bytes;
                if (bufferSize <= 0)
                    clear(); // try to minify/squeeze us
                else
                    tail -= bytes;
                return;
            }

            bufferSize -= tail;
            bytes -= tail;
            buffers.removeLast();
            --tailBuffer;
            tail = buffers.last().size();
        }
    }

    inline bool isEmpty() const {
        return tailBuffer == 0 && tail == 0;
    }

    inline int getChar() {
        if (isEmpty())
            return -1;
        char c = *readPointer();
        free(1);
        return int(uchar(c));
    }

    inline void putChar(char c) {
        char *ptr = reserve(1);
        *ptr = c;
    }

    inline void ungetChar(char c) {
        --head;
        if (head < 0) {
            buffers.prepend(QByteArray());
            buffers.first().resize(basicBlockSize);
            head = basicBlockSize - 1;
            ++tailBuffer;
        }
        buffers.first()[head] = c;
        ++bufferSize;
    }

    inline int size() const {
        return bufferSize;
    }

    inline void clear() {
        buffers.erase(buffers.begin() + 1, buffers.end());
        buffers.first().clear();

        head = tail = 0;
        tailBuffer = 0;
        bufferSize = 0;
    }

    inline int indexOf(char c) const {
        int index = 0;
        int j = head;
        for (int i = 0; i < buffers.size(); ++i) {
            const char *ptr = buffers[i].constData() + j;
            j = index + (i == tailBuffer ? tail : buffers[i].size()) - j;

            while (index < j) {
                if (*ptr++ == c)
                    return index;
                ++index;
            }
            j = 0;
        }
        return -1;
    }

    inline int indexOf(char c, int maxLength) const {
        int index = 0;
        int j = head;
        for (int i = 0; index < maxLength && i < buffers.size(); ++i) {
            const char *ptr = buffers[i].constData() + j;
            j = qMin(index + (i == tailBuffer ? tail : buffers[i].size()) - j, maxLength);

            while (index < j) {
                if (*ptr++ == c)
                    return index;
                ++index;
            }
            j = 0;
        }
        return -1;
    }

    inline int read(char *data, int maxLength) {
        int bytesToRead = qMin(size(), maxLength);
        int readSoFar = 0;
        while (readSoFar < bytesToRead) {
            int bytesToReadFromThisBlock = qMin(bytesToRead - readSoFar, nextDataBlockSize());
            if (data)
                memcpy(data + readSoFar, readPointer(), bytesToReadFromThisBlock);
            readSoFar += bytesToReadFromThisBlock;
            free(bytesToReadFromThisBlock);
        }
        return readSoFar;
    }

    // read an unspecified amount (will read the first buffer)
    inline QByteArray read() {
        if (bufferSize == 0)
            return QByteArray();

        QByteArray qba(buffers.takeFirst());

        qba.reserve(0); // avoid that resizing needlessly reallocates
        if (tailBuffer == 0) {
            qba.resize(tail);
            tail = 0;
            buffers.append(QByteArray());
        } else {
            --tailBuffer;
        }
        qba.remove(0, head); // does nothing if head is 0
        head = 0;
        bufferSize -= qba.size();
        return qba;
    }

    // append a new buffer to the end
    inline void append(const QByteArray &qba) {
        if (tail == 0) {
            buffers.last() = qba;
        } else {
            buffers.last().resize(tail);
            buffers.append(qba);
            ++tailBuffer;
        }
        tail = qba.size();
        bufferSize += tail;
    }

    inline int skip(int length) {
        return read(0, length);
    }

    inline int readLine(char *data, int maxLength) {
        if (!data || --maxLength <= 0)
            return -1;

        int i = indexOf('\n', maxLength);
        i = read(data, i >= 0 ? (i + 1) : maxLength);

        // Terminate it.
        data[i] = '\0';
        return i;
    }

    inline bool canReadLine() const {
        return indexOf('\n') >= 0;
    }

private:
    QList<QByteArray> buffers;
    int head, tail;
    int tailBuffer; // always buffers.size() - 1
    const int basicBlockSize;
    int bufferSize;
};

class QSerialPortPrivateData
{
public:
    enum IoConstants {
        ReadChunkSize = 512
    };

    QSerialPortPrivateData(QSerialPort *q);
    int timeoutValue(int msecs, int elapsed);

    qint64 readBufferMaxSize;
    QGCRingBuffer readBuffer;
    QGCRingBuffer writeBuffer;
    QSerialPort::SerialPortError error;
    QString systemLocation;
    qint32 inputBaudRate;
    qint32 outputBaudRate;
    QSerialPort::DataBits dataBits;
    QSerialPort::Parity parity;
    QSerialPort::StopBits stopBits;
    QSerialPort::FlowControl flowControl;
    QSerialPort::DataErrorPolicy policy;
    bool settingsRestoredOnClose;
    QSerialPort * const q_ptr;
};

QT_END_NAMESPACE

#endif // QSERIALPORT_P_H