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
|
#include "pttyhandler.h"
#include "logcategories.h"
#include <QDebug>
#include <QFile>
#ifndef Q_OS_WIN
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#endif
// Copyright 2017-2024 Elliott H. Liggett & Phil Taylor
pttyHandler::pttyHandler(QString pty, QObject* parent) : QObject(parent)
{
//constructor
this->setObjectName("pttyHandler");
queue = cachingQueue::getInstance();
connect(queue, SIGNAL(rigCapsUpdated(rigCapabilities*)), this, SLOT(receiveRigCaps(rigCapabilities*)));
rigCaps = queue->getRigCaps();
if (pty == "" || pty.toLower() == "none")
{
// Just return if pty is not configured.
return;
}
portName = pty;
#ifdef Q_OS_WIN
// 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 = pty;
#endif
openPort();
}
void pttyHandler::openPort()
{
serialError = false;
bool success=false;
#ifdef Q_OS_WIN
port = new QSerialPort();
port->setPortName(portName);
port->setBaudRate(baudRate);
port->setStopBits(QSerialPort::OneStop);// OneStop is other option
success = port->open(QIODevice::ReadWrite);
if (success) {
connect(port, &QSerialPort::readyRead, this, std::bind(&pttyHandler::receiveDataIn, this, (int)0));
}
#else
// Generic method in Linux/MacOS to find a pty
ptfd = ::posix_openpt(O_RDWR | O_NONBLOCK);
if (ptfd >=0)
{
qInfo(logSerial()) << "Opened pt device: " << ptfd << ", attempting to grant pt status";
if (grantpt(ptfd))
{
qInfo(logSerial()) << "Failed to grantpt";
return;
}
if (unlockpt(ptfd))
{
qInfo(logSerial()) << "Failed to unlock pt";
return;
}
// we're good!
qInfo(logSerial()) << "Opened pseudoterminal, slave name :" << ptsname(ptfd);
// Open the slave device to keep alive.
ptKeepAlive = open(ptsname(ptfd), O_RDONLY);
ptReader = new QSocketNotifier(ptfd, QSocketNotifier::Read, this);
connect(ptReader, &QSocketNotifier::activated,
this, &pttyHandler::receiveDataIn);
success=true;
}
#endif
if (!success)
{
ptfd = 0;
qWarning(logSerial()) << "Could not open pseudo terminal port, please restart.";
isConnected = false;
serialError = true;
emit havePortError(errorType(portName, "Could not open pseudo terminal port. Please restart."));
return;
}
#ifndef Q_OS_WIN
ptDevSlave = QString::fromLocal8Bit(ptsname(ptfd));
if (portName != "" && portName.toLower() != "none")
{
if (!QFile::link(ptDevSlave, portName))
{
qInfo(logSerial()) << "Error creating link to" << ptDevSlave << "from" << portName;
} else {
qInfo(logSerial()) << "Created link to" << ptDevSlave << "from" << portName;
}
}
#endif
isConnected = true;
}
pttyHandler::~pttyHandler()
{
this->closePort();
}
void pttyHandler::receiveDataFromRigToPtty(const QByteArray& data)
{
int fePos=data.lastIndexOf((char)0xfe);
if (fePos > 0 && data.length() > fePos+2)
fePos=fePos-1;
else
{
qDebug(logSerial()) << "Invalid command";
printHex(data,false,true);
}
if (disableTransceive && ((quint8)data[fePos + 2] == 0x00 || (quint8)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 && (quint8)data[fePos + 2] != quint8(0xE1) && (quint8)data[fePos + 3] != quint8(0xE1))
{
// send to the pseudo port as well
// index 2 is dest, 0xE1 is wfview, 0xE0 is assumed to be the other device.
// Changed to "Not 0xE1"
// 0xE1 = wfview
// 0xE0 = pseudo-term host
// 0x00 = broadcast to all
//qInfo(logSerial()) << "Sending data from radio to pseudo-terminal" << data.toHex(' ');
sendDataOut(data);
}
}
void pttyHandler::sendDataOut(const QByteArray& writeData)
{
qint64 bytesWritten = 0;
//qInfo(logSerial()) << "Data to pseudo term:" << writeData.toHex(' ');
//printHex(writeData, false, true);
if (isConnected) {
mutex.lock();
#ifdef Q_OS_WIN
bytesWritten = port->write(writeData);
#else
bytesWritten = ::write(ptfd, writeData.constData(), writeData.size());
#endif
if (bytesWritten != writeData.length()) {
qInfo(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 pttyHandler::receiveDataIn(int fd) {
#ifndef Q_OS_WIN
ssize_t available = 255; // Read up to 'available' bytes
#else
Q_UNUSED(fd);
#endif
// 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
#ifdef Q_OS_WIN
port->startTransaction();
inPortData = port->readAll();
#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)));
return;
}
inPortData.resize(got);
#endif
if (inPortData.startsWith("\xFE\xFE"))
{
if (inPortData.endsWith("\xFD"))
{
// good!
#ifdef Q_OS_WIN
port->commitTransaction();
#endif
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];
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];
qInfo(logSerial()) << "pty remote CI-V changed:" << QString("0x%1").arg((quint8)civId,0,16);
}
// filter C-IV transceive command before forwarding on.
if (rigCaps != Q_NULLPTR && rigCaps->commands.contains(funcCIVTransceive) && inPortData.contains(rigCaps->commands.find(funcCIVTransceive).value().data))
{
//qInfo(logSerial()) << "Filtered transceive command";
//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;
}
}
else if (inPortData.length() > lastFE + 2 && (quint8(inPortData[lastFE + 1]) == civId || quint8(inPortData[lastFE + 2]) == civId))
{
//qInfo(logSerial()) << "Got data from pseudo-terminal to radio" << inPortData.toHex(' ');
emit haveDataFromPort(inPortData);
//qDebug(logSerial()) << "Data from pseudo term:";
//printHex(inPortData, false, true);
}
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);
rolledBack = true;
#ifdef Q_OS_WIN
port->rollbackTransaction();
}
}
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!
}
#else
}
}
#endif
}
void pttyHandler::closePort()
{
#ifdef Q_OS_WIN
if (port != Q_NULLPTR)
{
port->close();
delete port;
}
#else
if (isConnected && portName != "" && portName.toLower() != "none")
{
QFile::remove(portName);
}
if (ptKeepAlive > 0) {
close(ptKeepAlive);
}
#endif
isConnected = false;
}
void pttyHandler::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 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((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++)
{
//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::receiveRigCaps(rigCapabilities* caps)
{
if (caps != Q_NULLPTR) {
qInfo(logSerial()) << "Got rigcaps for:" << caps->modelName;
}
this->rigCaps = caps;
}
|