wfview/pttyhandler.cpp

347 wiersze
10 KiB
C++
Czysty Zwykły widok Historia

2021-03-04 20:19:05 +00:00
#include "pttyhandler.h"
#include "logcategories.h"
#include <QDebug>
2021-05-11 08:38:05 +00:00
#include <QFile>
#ifndef Q_OS_WIN
2021-05-11 08:38:05 +00:00
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
2021-08-25 17:05:52 +00:00
#include <errno.h>
#endif
2021-03-04 20:19:05 +00:00
// Copyright 2017-2021 Elliott H. Liggett & Phil Taylor
pttyHandler::pttyHandler(QString pty, QObject* parent) : QObject(parent)
2021-03-04 20:19:05 +00:00
{
//constructor
2021-05-31 16:55:01 +00:00
if (pty == "" || pty.toLower() == "none")
2021-05-12 08:14:48 +00:00
{
// Just return if pty is not configured.
return;
}
2021-05-11 08:38:05 +00:00
portName = pty;
2021-03-04 20:19:05 +00:00
2021-05-11 08:38:05 +00:00
#ifdef Q_OS_WIN
2021-03-04 20:19:05 +00:00
// 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;
2021-05-05 13:11:00 +00:00
portName = pty;
2021-03-04 20:19:05 +00:00
#endif
openPort();
}
2021-05-11 08:38:05 +00:00
void pttyHandler::openPort()
2021-03-04 20:19:05 +00:00
{
serialError = false;
2021-05-11 08:38:05 +00:00
bool success=false;
2021-03-05 12:53:23 +00:00
#ifdef Q_OS_WIN
port = new QSerialPort();
2021-05-11 08:38:05 +00:00
port->setPortName(portName);
2021-03-05 12:53:23 +00:00
port->setBaudRate(baudRate);
port->setStopBits(QSerialPort::OneStop);// OneStop is other option
2021-03-04 20:19:05 +00:00
success = port->open(QIODevice::ReadWrite);
if (success) {
connect(port, &QSerialPort::readyRead, this, std::bind(&pttyHandler::receiveDataIn, this, (int)0));
}
2021-05-11 08:38:05 +00:00
#else
// Generic method in Linux/MacOS to find a pty
2021-05-31 17:11:24 +00:00
ptfd = ::posix_openpt(O_RDWR | O_NONBLOCK);
2021-05-11 08:38:05 +00:00
if (ptfd >=0)
2021-03-04 20:19:05 +00:00
{
qInfo(logSerial()) << "Opened pt device: " << ptfd << ", attempting to grant pt status";
2021-03-04 20:19:05 +00:00
if (grantpt(ptfd))
{
qInfo(logSerial()) << "Failed to grantpt";
2021-03-04 20:19:05 +00:00
return;
}
if (unlockpt(ptfd))
{
qInfo(logSerial()) << "Failed to unlock pt";
2021-03-04 20:19:05 +00:00
return;
}
// we're good!
qInfo(logSerial()) << "Opened pseudoterminal, slave name :" << ptsname(ptfd);
2021-03-04 20:19:05 +00:00
2021-09-22 09:59:03 +00:00
// Open the slave device to keep alive.
2021-09-22 10:02:09 +00:00
ptKeepAlive = open(ptsname(ptfd), O_RDONLY);
2021-09-22 09:59:03 +00:00
2021-05-11 08:38:05 +00:00
ptReader = new QSocketNotifier(ptfd, QSocketNotifier::Read, this);
connect(ptReader, &QSocketNotifier::activated,
this, &pttyHandler::receiveDataIn);
2021-03-04 20:19:05 +00:00
2021-05-11 08:38:05 +00:00
success=true;
2021-03-04 20:19:05 +00:00
}
2021-05-11 08:38:05 +00:00
#endif
if (!success)
{
2021-03-04 20:19:05 +00:00
ptfd = 0;
qInfo(logSerial()) << "Could not open pseudo terminal port, please restart.";
2021-03-04 20:19:05 +00:00
isConnected = false;
serialError = true;
emit haveSerialPortError(portName, "Could not open pseudo terminal port. Please restart.");
return;
}
2021-05-11 08:38:05 +00:00
#ifndef Q_OS_WIN
ptDevSlave = QString::fromLocal8Bit(ptsname(ptfd));
2021-05-31 16:55:01 +00:00
if (portName != "" && portName.toLower() != "none")
2021-05-11 08:38:05 +00:00
{
if (!QFile::link(ptDevSlave, portName))
{
qInfo(logSerial()) << "Error creating link to" << ptDevSlave << "from" << portName;
2021-05-11 08:38:05 +00:00
} else {
qInfo(logSerial()) << "Created link to" << ptDevSlave << "from" << portName;
2021-05-11 08:38:05 +00:00
}
}
#endif
isConnected = true;
2021-03-04 20:19:05 +00:00
}
pttyHandler::~pttyHandler()
{
this->closePort();
}
void pttyHandler::receiveDataFromRigToPtty(const QByteArray& data)
{
2021-05-31 17:11:24 +00:00
2021-05-31 16:55:01 +00:00
int fePos=data.lastIndexOf((char)0xfe);
if (fePos > 0 && data.length() > fePos+2)
2021-05-31 16:55:01 +00:00
fePos=fePos-1;
else
{
qDebug(logSerial()) << "Invalid command";
printHex(data,false,true);
}
if (disableTransceive && ((unsigned char)data[fePos + 2] == 0x00 || (unsigned char)data[fePos + 3] == 0x00))
{
// Ignore data that is sent to/from transceive address as client has requested transceive disabled.
qDebug(logSerial()) << "Transceive command filtered";
return;
}
if (isConnected && (unsigned char)data[fePos + 2] != 0xE1 && (unsigned char)data[fePos + 3] != 0xE1)
2021-03-04 20:19:05 +00:00
{
// send to the pseudo port as well
// index 2 is dest, 0xE1 is wfview, 0xE0 is assumed to be the other device.
2021-03-05 12:53:23 +00:00
// Changed to "Not 0xE1"
2021-03-04 20:19:05 +00:00
// 0xE1 = wfview
// 0xE0 = pseudo-term host
// 0x00 = broadcast to all
//qInfo(logSerial()) << "Sending data from radio to pseudo-terminal";
2021-05-31 17:11:24 +00:00
sendDataOut(data);
2021-03-04 20:19:05 +00:00
}
}
void pttyHandler::sendDataOut(const QByteArray& writeData)
{
2021-03-04 23:45:22 +00:00
qint64 bytesWritten = 0;
2021-03-04 20:19:05 +00:00
//qInfo(logSerial()) << "Data to pseudo term:";
2021-03-05 12:53:23 +00:00
//printHex(writeData, false, true);
2021-05-12 08:14:48 +00:00
if (isConnected) {
mutex.lock();
2021-05-11 08:38:05 +00:00
#ifdef Q_OS_WIN
2021-05-12 08:14:48 +00:00
bytesWritten = port->write(writeData);
2021-05-11 08:38:05 +00:00
#else
2021-05-12 08:14:48 +00:00
bytesWritten = ::write(ptfd, writeData.constData(), writeData.size());
2021-05-11 08:38:05 +00:00
#endif
2021-05-12 08:14:48 +00:00
if (bytesWritten != writeData.length()) {
qInfo(logSerial()) << "bytesWritten: " << bytesWritten << " length of byte array: " << writeData.length()\
2021-05-12 08:14:48 +00:00
<< " size of byte array: " << writeData.size()\
<< " Wrote all bytes? " << (bool)(bytesWritten == (qint64)writeData.size());
}
mutex.unlock();
2021-03-04 21:06:25 +00:00
}
2021-03-04 20:19:05 +00:00
}
2021-05-11 08:38:05 +00:00
void pttyHandler::receiveDataIn(int fd) {
2021-03-04 20:19:05 +00:00
2021-05-11 08:38:05 +00:00
#ifndef Q_OS_WIN
ssize_t available = 255; // Read up to 'available' bytes
#else
Q_UNUSED(fd);
#endif
2021-03-04 20:19:05 +00:00
2021-05-11 08:38:05 +00:00
// Linux will correctly return the number of available bytes with the FIONREAD ioctl
// Sadly MacOS always returns zero!
#ifdef Q_OS_LINUX
int ret = ::ioctl(fd, FIONREAD, (char *) &available);
if (ret != 0)
return;
#endif
2021-03-04 20:19:05 +00:00
2021-05-11 08:38:05 +00:00
#ifdef Q_OS_WIN
2021-03-04 20:19:05 +00:00
port->startTransaction();
inPortData = port->readAll();
2021-05-11 08:38:05 +00:00
#else
inPortData.resize(available);
ssize_t got = ::read(fd, inPortData.data(), available);
int err = errno;
if (got < 0) {
qInfo(logSerial()) << tr("Read failed: %1").arg(QString::fromLatin1(strerror(err)));
2021-05-11 08:38:05 +00:00
return;
}
inPortData.resize(got);
#endif
2021-03-04 20:19:05 +00:00
if (inPortData.startsWith("\xFE\xFE"))
{
if (inPortData.endsWith("\xFD"))
{
// good!
2021-05-11 08:38:05 +00:00
#ifdef Q_OS_WIN
2021-03-04 20:19:05 +00:00
port->commitTransaction();
2021-05-11 08:38:05 +00:00
#endif
2021-03-04 20:19:05 +00:00
int lastFE = inPortData.lastIndexOf((char)0xfe);
if (civId == 0 && inPortData.length() > lastFE + 2 && (quint8)inPortData[lastFE + 2] > (quint8)0xdf && (quint8)inPortData[lastFE + 2] < (quint8)0xef) {
// This is (should be) the remotes CIV id.
civId = (quint8)inPortData[lastFE + 2];
2022-04-28 09:52:46 +00:00
qInfo(logSerial()) << "pty detected remote CI-V:" << QString("0x%1").arg(civId,0,16);
}
else if (civId != 0 && inPortData.length() > lastFE + 2 && (quint8)inPortData[lastFE + 2] != civId)
{
civId = (quint8)inPortData[lastFE + 2];
2022-04-28 09:52:46 +00:00
qInfo(logSerial()) << "pty remote CI-V changed:" << QString("0x%1").arg((quint8)civId,0,16);
}
// filter C-IV transceive command before forwarding on.
if (inPortData.contains(rigCaps.transceiveCommand))
2021-03-05 12:53:23 +00:00
{
//qInfo(logSerial()) << "Filtered transceive command";
2021-03-05 12:53:23 +00:00
//printHex(inPortData, false, true);
QByteArray reply= QByteArrayLiteral("\xfe\xfe\x00\x00\xfb\xfd");
reply[2] = inPortData[3];
reply[3] = inPortData[2];
sendDataOut(inPortData); // Echo command back
sendDataOut(reply);
if (!disableTransceive) {
qInfo(logSerial()) << "pty requested CI-V Transceive disable";
disableTransceive = true;
}
2021-03-05 12:53:23 +00:00
}
else if (inPortData.length() > lastFE + 2 && ((quint8)inPortData[lastFE + 1] == civId || (quint8)inPortData[lastFE + 2] == civId))
2021-03-04 20:19:05 +00:00
{
emit haveDataFromPort(inPortData);
qDebug(logSerial()) << "Data from pseudo term:";
printHex(inPortData, false, true);
2021-03-04 20:19:05 +00:00
}
if (rolledBack)
{
2022-03-23 15:45:51 +00:00
// qInfo(logSerial()) << "Rolled back and was successful. Length: " << inPortData.length();
2021-03-04 20:19:05 +00:00
//printHex(inPortData, false, true);
rolledBack = false;
}
}
else {
// did not receive the entire thing so roll back:
2022-03-23 15:45:51 +00:00
// qInfo(logSerial()) << "Rolling back transaction. End not detected. Length: " << inPortData.length();
2021-03-04 20:19:05 +00:00
//printHex(inPortData, false, true);
rolledBack = true;
2021-05-11 08:38:05 +00:00
#ifdef Q_OS_WIN
port->rollbackTransaction();
2021-03-04 20:19:05 +00:00
}
}
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!!";
2021-03-04 20:19:05 +00:00
// THIS SHOULD ONLY HAPPEN ONCE!
}
2021-05-11 08:38:05 +00:00
#else
}
}
#endif
2021-03-04 20:19:05 +00:00
}
2021-05-11 08:38:05 +00:00
2021-03-04 20:19:05 +00:00
void pttyHandler::closePort()
{
2021-05-11 08:38:05 +00:00
#ifdef Q_OS_WIN
2021-05-13 22:13:48 +00:00
if (port != Q_NULLPTR)
2021-03-04 20:19:05 +00:00
{
port->close();
delete port;
}
2021-05-11 08:38:05 +00:00
#else
2021-05-31 16:55:01 +00:00
if (isConnected && portName != "" && portName.toLower() != "none")
2021-05-11 08:38:05 +00:00
{
QFile::remove(portName);
}
2021-09-22 09:59:03 +00:00
if (ptKeepAlive > 0) {
close(ptKeepAlive);
}
2021-05-11 08:38:05 +00:00
#endif
2021-03-04 20:19:05 +00:00
isConnected = false;
}
void pttyHandler::debugThis()
{
// Do not use, function is for debug only and subject to change.
qInfo(logSerial()) << "comm debug called.";
2021-03-04 20:19:05 +00:00
inPortData = port->readAll();
emit haveDataFromPort(inPortData);
}
void pttyHandler::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((unsigned char)pdata[i], 2, 16, QChar('0'));
sdata.append(QString("%1 ").arg((unsigned char)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++)
{
//sdata = QString(strings.at(i));
qDebug(logSerial()) << strings.at(i);
}
}
if (printHoriz)
{
qDebug(logSerial()) << index;
qDebug(logSerial()) << sdata;
}
qDebug(logSerial()) << "----- End hex dump -----";
}
void pttyHandler::receiveFoundRigID(rigCapabilities rigCaps) {
this->rigCaps = rigCaps;
qInfo(logSerial) << "Received rigCapabilities for" << rigCaps.modelName;
}
2021-03-04 20:19:05 +00:00