File indexing completed on 2024-04-28 03:50:13

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2010 Wes Hardaker <hardaker@users.sourceforge.net>
0004 //
0005 
0006 #include "AprsTTY.h"
0007 
0008 #include <QSerialPort>
0009 
0010 #include "MarbleDebug.h"
0011 
0012 #include "AprsGatherer.h"
0013 
0014 using namespace Marble;
0015 
0016 AprsTTY::AprsTTY( const QString &ttyName )
0017     : m_ttyName( ttyName ),
0018       m_numErrors( 0 )
0019 {
0020 }
0021 
0022 AprsTTY::~AprsTTY()
0023 {
0024 }
0025 
0026 QString
0027 AprsTTY::sourceName() const
0028 {
0029     return QString( "TTY" );
0030 }
0031 
0032 bool
0033 AprsTTY::canDoDirect() const
0034 {
0035     return true;
0036 }
0037 
0038 QIODevice *
0039 AprsTTY::openSocket() 
0040 {
0041     QSerialPort *m_port = new QSerialPort( m_ttyName );
0042     m_port->setBaudRate( QSerialPort::Baud9600, QSerialPort::Input );
0043     m_port->setParity( QSerialPort::NoParity );
0044     m_port->setDataBits( QSerialPort::Data8 );
0045     m_port->setStopBits( QSerialPort::OneStop );
0046 //    m_port->setTimeout( 60000 ); // ms
0047     m_port->open( QIODevice::ReadOnly );
0048     mDebug() << "opened TTY socket";
0049     if ( m_port->isOpen() ) {
0050         mDebug() << "connected to " << m_ttyName.toLocal8Bit().data();
0051     } else {
0052         delete m_port;
0053         m_port = nullptr;
0054         mDebug() << "**** failed to open terminal " << m_ttyName.toLocal8Bit().data() << " ****";
0055     }
0056     return m_port;
0057 }
0058 
0059 void
0060 AprsTTY::checkReadReturn( int length, QIODevice **socket,
0061                           AprsGatherer *gatherer ) 
0062 {
0063     if ( length < 0 || ( length == 0 && m_numErrors > 5 ) ) {
0064         // hard error.  try reopening
0065         mDebug() << "**** restarting TTY socket";
0066         delete *socket;
0067         gatherer->sleepFor( 1 );
0068         *socket = openSocket();
0069         return;
0070     }
0071     if ( length == 0 ) {
0072         ++m_numErrors;
0073         mDebug() << "**** Odd: read zero bytes from TTY socket";
0074         return;
0075     }
0076     return;
0077 }
0078