File: commhandler.cpp

package info (click to toggle)
wfview 2.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,256 kB
  • sloc: cpp: 43,386; ansic: 3,196; sh: 32; xml: 29; makefile: 11
file content (418 lines) | stat: -rw-r--r-- 13,979 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
366
367
368
369
370
371
372
373
374
375
376
377
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
#include "commhandler.h"
#include "logcategories.h"

#include <QDebug>

// Copyright 2017-2020 Elliott H. Liggett

commHandler::commHandler(QObject* parent) : QObject(parent)
{
    //constructor
    // TODO: The following should become arguments and/or functions
    // Add signal/slot everywhere for comm port setup.
    // Consider how to "re-setup" and how to save the state for next time.
    baudrate = 115200;
    stopbits = 1;
    portName = "/dev/ttyUSB0";
    this->pttType = pttCIV;
    this->halfDuplex=false;
    init();
}

commHandler::commHandler(QString portName, quint32 baudRate, quint8 wfFormat, QObject* parent) : QObject(parent)
{
    //constructor
    // grab baud rate and other comm port details
    // if they need to be changed later, please
    // destroy this and create a new one.

    if (wfFormat == 1) { // Single waterfall packet
        combineWf = true;
        qDebug(logSerial()) << "Combine Waterfall Mode Enabled!";
    }

    // TODO: The following should become arguments and/or functions
    // Add signal/slot everywhere for comm port setup.
    // Consider how to "re-setup" and how to save the state for next time.
    baudrate = baudRate;
    stopbits = 1;
    this->portName = portName;
    this->pttType = pttCIV;
    init();

}

void commHandler::init()
{
    if (port != Q_NULLPTR) {
        delete port;
        port = Q_NULLPTR;
        isConnected = false;
    }

    port = new QSerialPort();
    setupComm(); // basic parameters
    openPort();
    // qInfo(logSerial()) << "Serial buffer size: " << port->readBufferSize();
    //port->setReadBufferSize(1024); // manually. 256 never saw any return from the radio. why...
    //qInfo(logSerial()) << "Serial buffer size: " << port->readBufferSize();

    connect(port, SIGNAL(readyRead()), this, SLOT(receiveDataIn()));
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
    connect(port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError)));
#else
    connect(port, SIGNAL(errorOccurred(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError)));
#endif

    connect(this, SIGNAL(sendDataOutToPort(const QByteArray&)),this,SLOT(sendDataOut(const QByteArray &)));
    lastDataReceived = QTime::currentTime();
}

commHandler::~commHandler()
{
    qInfo(logSerial()) << "Closing serial port: " << port->portName();
    if (isConnected) {
        this->closePort();
    }
    delete port;
}

void commHandler::setHalfDuplex(bool en)
{
    halfDuplex=en;
    qInfo(logSerial()) << "Setting half duplex comms: " << ((en) ? "Enabled":"Disabled");
}

void commHandler::setupComm()
{
    serialError = false;
    port->setPortName(portName);
    port->setBaudRate(baudrate);
    port->setStopBits(QSerialPort::OneStop);// OneStop is other option
}

void commHandler::receiveDataFromUserToRig(const QByteArray &data)
{
    sendDataOut(data);
}

void commHandler::sendDataOut(const QByteArray &writeData)
{
    if (halfDuplex)
    {
        QMutexLocker locker(&duplexMutex);
    }

    // Recycle port to attempt reconnection.
    if (lastDataReceived.msecsTo(QTime::currentTime()) > 10000) {
        qDebug(logSerial()) << "Serial port error? Attempting reconnect...";
        lastDataReceived = QTime::currentTime();
        QTimer::singleShot(100, this, SLOT(init()));
        QTimer::singleShot(500, this, [=]() { sendDataOut(writeData); });
        return;
    }

    mutex.lock();

    previousSent = writeData;

    qint64 bytesWritten;

    if(pttType != pttCIV)
    {
        // Size:    1  2  3    4    5    6    7    8
        //index:    0  1  2    3    4    5    6    7
        //Query:   FE FE TO FROM 0x1C 0x00 0xFD
        //PTT On:  FE FE TO FROM 0x1C 0x00 0x01 0xFD
        //PTT Off: FE FE TO FROM 0x1C 0x00 0x00 0xFD

        if(writeData.endsWith(QByteArrayLiteral("\x1C\x00\xFD")))
        {
            // Query
            bool ptt = false;

            if (pttType == pttRTS)
                ptt = port->isRequestToSend();
            else if (pttType == pttDTR)
                ptt = port->isDataTerminalReady();

            //qDebug(logSerial()) << "Looks like PTT Query, ptt is:"<<ptt;

            QByteArray pttreturncmd = QByteArray("\xFE\xFE");
            pttreturncmd.append(writeData.at(3));
            pttreturncmd.append(writeData.at(2));
            pttreturncmd.append(QByteArray("\x1C\x00", 2));
            pttreturncmd.append((char)ptt);
            pttreturncmd.append("\xFD");

            emit haveDataFromPort(pttreturncmd);
            
            mutex.unlock();
            return;
        } else if(writeData.endsWith(QByteArrayLiteral("\x1C\x00\x01\xFD")))
        {
            // PTT ON
            if (pttType == pttRTS) {
                port->setRequestToSend(true);
            }
            else if (pttType == pttDTR) {
                port->setDataTerminalReady(true);
            }
            mutex.unlock();
            return;
        } else if(writeData.endsWith(QByteArrayLiteral("\x1C\x00\x00\xFD")))
        {
            // PTT OFF
            if (pttType == pttRTS) {
                port->setRequestToSend(false);
            }
            else if (pttType == pttDTR) {
                port->setDataTerminalReady(false);
            }

            mutex.unlock();
            return;
        }
    }

    bytesWritten = port->write(writeData);
    
    if(bytesWritten != (qint64)writeData.size())
    {
    qDebug(logSerial()) << "bytesWritten: " << bytesWritten << " length of byte array: " << writeData.length()\
             << " size of byte array: " << writeData.size()\
             << " Wrote all bytes? " << (bool) (bytesWritten == (qint64)writeData.size());
    }

    mutex.unlock();
}

void commHandler::receiveDataIn()
{
    if (halfDuplex)
    {
        QMutexLocker locker(&duplexMutex);
    }
    // connected to comm port data signal

    // Here we get a little specific to CIV radios
    // because we know what constitutes a valid "frame" of data.

    // new code:
    lastDataReceived = QTime::currentTime();
    port->startTransaction();
    inPortData = port->readAll();

    if (inPortData.startsWith("\xFC\xFC\xFC\xFC\xFC"))
    {
        // Collision detected by remote end, re-send previous command.
        qInfo(logSerial()) << "COLLISION, resending:"<<previousSent.toHex(' ');
        port->commitTransaction();
        emit sendDataOutToPort(previousSent);
        return;
    }

    if(inPortData.size() == 1)
    {
        // Generally for baud <= 9600
        if (inPortData == "\xFE")
        {
            // This will get hit twice.
            // After the FE FE, we transition into
            // the normal .startsWith FE FE block
            // where the normal rollback code can handle things.
            port->rollbackTransaction();
            rolledBack = true;
            return;
        }
    }

    if (inPortData.startsWith("\xFE\xFE"))
    {
        if(inPortData.contains("\xFC"))
        {
            qInfo(logSerial()) << "Transaction contains collision data. Dumping.";
            //printHex(inPortData, false, true);
            port->commitTransaction();
            return;
        }
        if(inPortData.endsWith("\xFD"))
        {
            // good!
            port->commitTransaction();

            //payloadIn = data.right(data.length() - 4);

            // Do we need to combine waterfall into single packet?
            //int combined = 0;
            if (combineWf) {
                int pos = inPortData.indexOf(QByteArrayLiteral("\x27\x00\x00"));
                int fdPos = inPortData.mid(pos).indexOf(QByteArrayLiteral("\xfd"));
                //printHex(inPortData, false, true);
                while (pos > -1 && fdPos > -1) {

                    spectrumDivisionNumber = inPortData[pos + 3] & 0x0f;
                    spectrumDivisionNumber += ((inPortData[pos + 3] & 0xf0) >> 4) * 10;

                    if (spectrumDivisionNumber == 1) {
                        // This is the first waveform data.
                        spectrumDivisionMax = 0;
                        spectrumDivisionMax = inPortData[pos + 4] & 0x0f;
                        spectrumDivisionMax += ((inPortData[pos + 4] & 0xf0) >> 4) * 10;
                        spectrumData.clear();
                        spectrumData = inPortData.mid(pos - 4, fdPos+4); // Don't include terminating FD
                        spectrumData[8] = spectrumData[7]; // Make max = current;
                        //qDebug() << "New Spectrum seq:" << spectrumDivisionNumber << "pos = " << pos << "len" << fdPos;

                    }
                    else  if (spectrumDivisionNumber > lastSpectrum && spectrumDivisionNumber <= spectrumDivisionMax) {
                        spectrumData.insert(spectrumData.length(), inPortData.mid(pos + 5, fdPos-5));
                        //qInfo() << "Added spectrum seq:" << spectrumDivisionNumber << "len" << fdPos-5<< "Spec" << spectrumData.length();
                        //printHex(inPortData.mid((pos+4),fdPos - (pos+5)), false, true);
                    }
                    else {
                        qDebug() << "Invalid Spectrum Division received" << spectrumDivisionNumber << "last Spectrum" << lastSpectrum;
                    }

                    lastSpectrum = spectrumDivisionNumber;
                        
                    if (spectrumDivisionNumber == spectrumDivisionMax) {
                        //qDebug() << "Got Spectrum! length=" << spectrumData.length();
                        spectrumData.append("\xfd"); // Need to add FD on the end.
                        //printHex(spectrumData, false, true);
                        emit haveDataFromPort(spectrumData);
                        lastSpectrum = 0;
                    }
                    inPortData = inPortData.remove(pos-4, fdPos+5);
                    pos = inPortData.indexOf(QByteArrayLiteral("\x27\x00\x00"));
                    fdPos = inPortData.mid(pos).indexOf(QByteArrayLiteral("\xfd"));
                }
                // If we still have data left, let the main function deal with it, any spectrum data has been removed
                if (inPortData.length() == 0)
                {
                    return;
                }
               // qDebug() << "Got extra data!";
                //printHex(inPortData, false, true);
            }
            // 
            emit haveDataFromPort(inPortData);

            if(rolledBack)
            {
                // qInfo(logSerial()) << "Rolled back and was successful. Length: " << inPortData.length();
                //printHex(inPortData, false, true);
                rolledBack = false;
            }
        } else {
            // did not receive the entire thing so roll back:
            // qInfo(logSerial()) << "Rolling back transaction. End not detected. Length: " << inPortData.length();
            //printHex(inPortData, false, true);
            port->rollbackTransaction();
            rolledBack = true;
        }
    } else {
        port->commitTransaction(); // do not emit data, do not keep data.
        //qInfo(logSerial()) << "Warning: received data with invalid start. Dropping data.";
        //qInfo(logSerial()) << "THIS SHOULD ONLY HAPPEN ONCE!!";
        // THIS SHOULD ONLY HAPPEN ONCE!

        // unrecoverable. We did not receive the start and must
        // have missed it earlier because we did not roll back to
        // preserve the beginning.

        //printHex(inPortData, false, true);

    }
}

void commHandler::setPTTType(pttType_t ptt)
{
    this->pttType = ptt;
}

void commHandler::openPort()
{
    bool success;
    success = port->open(QIODevice::ReadWrite);
    if(success)
    {
        port->setDataTerminalReady(false);
        port->setRequestToSend(false);
        isConnected = true;
        qInfo(logSerial()) << "Opened port: " << portName;
        return;
    } else {
        qInfo(logSerial()) << "Could not open serial port " << portName << " , please check Radio Access under Settings.";
        isConnected = false;
        serialError = true;
        emit havePortError(errorType(true, portName, "Could not open Serial or USB port.\nPlease check Radio Access under Settings."));
        return;
    }
}

void commHandler::closePort()
{
    if(port)
    {
        port->close();
        //delete port;
    }
    isConnected = false;
}

void commHandler::debugThis()
{
    // Do not use, function is for debug only and subject to change.
    qInfo(logSerial()) << "comm debug called.";

    inPortData = port->readAll();
    emit haveDataFromPort(inPortData);
}


void commHandler::printHex(const QByteArray &pdata, bool printVert, bool printHoriz)
{
    qDebug(logSerial()) << "---- Begin hex dump -----:";
    QString sdata("DATA:  ");
    QString index("INDEX: ");
    QStringList strings;

    for(int i=0; i < pdata.length(); i++)
    {
        strings << QString("[%1]: %2").arg(i,8,10,QChar('0')).arg((quint8)pdata[i], 2, 16, QChar('0'));
        sdata.append(QString("%1 ").arg((quint8)pdata[i], 2, 16, QChar('0')) );
        index.append(QString("%1 ").arg(i, 2, 10, QChar('0')));
    }

    if(printVert)
    {
        for(int i=0; i < strings.length(); i++)
        {
            qDebug(logSerial()) << strings.at(i);
        }
    }

    if(printHoriz)
    {
        qDebug(logSerial()) << index;
        qDebug(logSerial()) << sdata;
    }
    qDebug(logSerial()) << "----- End hex dump -----";
}


void commHandler::handleError(QSerialPort::SerialPortError err)
{
    switch (err) {
        case QSerialPort::NoError:
            break;
        default:
            if (lastDataReceived.msecsTo(QTime::currentTime()) > 2000) {
                qDebug(logSerial()) << "Serial port" << port->portName() << "Error, attempting disconnect/reconnect";
                lastDataReceived = QTime::currentTime();
                QTimer::singleShot(500, this, SLOT(init()));
            }
            break;
    }
}