|
|
@ -30,6 +30,7 @@
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <unistd.h>
|
|
|
@ -75,7 +76,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Modem::Modem( KandyPrefs *kprefs, TQObject *parent, const char *name ) :
|
|
|
|
Modem::Modem( KandyPrefs *kprefs, TQObject *parent, const char *name ) :
|
|
|
|
TQObject(parent, name)
|
|
|
|
TQObject(parent, name), fd(-1)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mOpen = false;
|
|
|
|
mOpen = false;
|
|
|
|
|
|
|
|
|
|
|
@ -185,12 +186,10 @@ bool Modem::open()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
struct termios tty;
|
|
|
|
struct termios tty;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
close();
|
|
|
|
close();
|
|
|
|
|
|
|
|
|
|
|
|
if ( !lockDevice() )
|
|
|
|
if (fd == -1)
|
|
|
|
return false;
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
TQCString dev = TQFile::encodeName( (*prefs).serialDevice() );
|
|
|
|
TQCString dev = TQFile::encodeName( (*prefs).serialDevice() );
|
|
|
|
const char *fdev = dev.data();
|
|
|
|
const char *fdev = dev.data();
|
|
|
|
if ( ( fd = ::open( fdev, O_RDWR | O_NOCTTY | O_NONBLOCK ) ) == -1 ) {
|
|
|
|
if ( ( fd = ::open( fdev, O_RDWR | O_NOCTTY | O_NONBLOCK ) ) == -1 ) {
|
|
|
@ -199,14 +198,19 @@ bool Modem::open()
|
|
|
|
.arg( fdev ) );
|
|
|
|
.arg( fdev ) );
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( !lockDevice() )
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
tcflush( fd, TCIOFLUSH );
|
|
|
|
tcflush( fd, TCIOFLUSH );
|
|
|
|
if ( tcgetattr( fd, &init_tty ) == -1 ) {
|
|
|
|
if ( tcgetattr( fd, &init_tty ) == -1 ) {
|
|
|
|
int errnumber = errno;
|
|
|
|
int errnumber = errno;
|
|
|
|
emit errorMessage( i18n( "Communication setup failed (tcgetattr code: %1)" )
|
|
|
|
emit errorMessage( i18n( "Communication setup failed (tcgetattr code: %1)" )
|
|
|
|
.arg(strerror(errnumber)) );
|
|
|
|
.arg(strerror(errnumber)) );
|
|
|
|
|
|
|
|
unlockDevice();
|
|
|
|
::close( fd );
|
|
|
|
::close( fd );
|
|
|
|
fd = 0;
|
|
|
|
fd = -1;
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -221,8 +225,9 @@ bool Modem::open()
|
|
|
|
|
|
|
|
|
|
|
|
if ( tcsetattr( fd, TCSANOW, &tty ) == -1 ) {
|
|
|
|
if ( tcsetattr( fd, TCSANOW, &tty ) == -1 ) {
|
|
|
|
emit errorMessage( i18n( "tcsetattr() failed." ) );
|
|
|
|
emit errorMessage( i18n( "tcsetattr() failed." ) );
|
|
|
|
|
|
|
|
unlockDevice();
|
|
|
|
::close( fd );
|
|
|
|
::close( fd );
|
|
|
|
fd = 0;
|
|
|
|
fd = -1;
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -244,127 +249,56 @@ void Modem::close()
|
|
|
|
delete sn;
|
|
|
|
delete sn;
|
|
|
|
sn = 0;
|
|
|
|
sn = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unlockDevice();
|
|
|
|
|
|
|
|
|
|
|
|
if ( fd ) {
|
|
|
|
if ( fd ) {
|
|
|
|
tcflush( fd, TCIOFLUSH );
|
|
|
|
tcflush( fd, TCIOFLUSH );
|
|
|
|
tcsetattr( fd, TCSANOW, &init_tty );
|
|
|
|
tcsetattr( fd, TCSANOW, &init_tty );
|
|
|
|
::close( fd );
|
|
|
|
::close( fd );
|
|
|
|
fd = 0;
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xreset();
|
|
|
|
xreset();
|
|
|
|
|
|
|
|
|
|
|
|
unlockDevice();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mOpen = false;
|
|
|
|
mOpen = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Modem::flush()
|
|
|
|
void Modem::flush()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if ( fd ) {
|
|
|
|
if ( fd != -1) {
|
|
|
|
tcflush( fd, TCIOFLUSH );
|
|
|
|
tcflush( fd, TCIOFLUSH );
|
|
|
|
bufpos = 0;
|
|
|
|
bufpos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LOCKDEV
|
|
|
|
|
|
|
|
#include <lockdev.h>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Modem::lockDevice()
|
|
|
|
bool Modem::lockDevice()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (is_locked)
|
|
|
|
if (is_locked)
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LOCKDEV
|
|
|
|
if (flock(fd, LOCK_EX))
|
|
|
|
is_locked = !dev_lock( (*prefs).serialDevice().local8Bit() );
|
|
|
|
{
|
|
|
|
if (!is_locked)
|
|
|
|
// Locking failed
|
|
|
|
emit errorMessage( i18n( "Unable to lock device '%1'." ).arg(
|
|
|
|
is_locked = false;
|
|
|
|
(*prefs).serialDevice() ));
|
|
|
|
emit errorMessage(i18n("Unable to lock device '%1'.").arg((*prefs).serialDevice()));
|
|
|
|
return is_locked;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
ssize_t count;
|
|
|
|
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
int lfd;
|
|
|
|
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
TQStringList pathList;
|
|
|
|
|
|
|
|
TQString fileName, content;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pathList = TQStringList::split( "/", (*prefs).serialDevice() );
|
|
|
|
|
|
|
|
fileName = (*prefs).lockDirectory() + "/LCK.." + pathList.last();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( !access( TQFile::encodeName( fileName ).data(), F_OK ) ) {
|
|
|
|
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( ( lfd = ::open( TQFile::encodeName( fileName ), O_RDONLY ) ) < 0 ) {
|
|
|
|
|
|
|
|
emit errorMessage( i18n( "Unable to open lock file '%1'.")
|
|
|
|
|
|
|
|
.arg( fileName ) );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
count = read( lfd, buf, 79 );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( count < 0 ) {
|
|
|
|
|
|
|
|
emit errorMessage( i18n( "Unable to read lock file '%1'.")
|
|
|
|
|
|
|
|
.arg( fileName ) );
|
|
|
|
|
|
|
|
::close( lfd );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[ count ] = 0;
|
|
|
|
|
|
|
|
::close( lfd );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
count = sscanf( buf, "%d", &pid );
|
|
|
|
|
|
|
|
if ( ( count != 1 ) || ( pid <= 0 ) ) {
|
|
|
|
|
|
|
|
emit errorMessage( i18n( "Unable to get PID from file '%1'.")
|
|
|
|
|
|
|
|
.arg( fileName ) );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( !kill( (pid_t) pid, 0 ) ) {
|
|
|
|
|
|
|
|
emit errorMessage( i18n( "Process with PID %1, which is locking the device, is still running.")
|
|
|
|
|
|
|
|
.arg( pid ) );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( errno != ESRCH ) {
|
|
|
|
|
|
|
|
emit errorMessage( i18n( "Unable to emit signal to PID of existing lock file.") );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( ( lfd = creat( TQFile::encodeName( fileName ).data(), 0644 ) ) == -1 ) {
|
|
|
|
|
|
|
|
emit errorMessage( i18n( "Unable to create lock file '%1'. "
|
|
|
|
|
|
|
|
"Please check that you have sufficient permissions.")
|
|
|
|
|
|
|
|
.arg( fileName ) );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
pid = (int) getpid();
|
|
|
|
{
|
|
|
|
pw = getpwuid( getuid() );
|
|
|
|
|
|
|
|
content.sprintf( "%08d %s %s", pid, "kandy", pw->pw_name );
|
|
|
|
|
|
|
|
write( lfd, TQFile::encodeName( content ).data(), content.length() );
|
|
|
|
|
|
|
|
::close( lfd );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
is_locked = true;
|
|
|
|
is_locked = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return is_locked;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Modem::unlockDevice()
|
|
|
|
void Modem::unlockDevice()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LOCKDEV
|
|
|
|
if (fd != -1 && is_locked)
|
|
|
|
dev_unlock( (*prefs).serialDevice().local8Bit(), getpid() );
|
|
|
|
{
|
|
|
|
#else
|
|
|
|
flock(fd, LOCK_UN);
|
|
|
|
if ( is_locked ) {
|
|
|
|
|
|
|
|
TQStringList pathList = TQStringList::split( "/", (*prefs).serialDevice() );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TQFile::remove( (*prefs).lockDirectory() + "/LCK.." + pathList.last() );
|
|
|
|
|
|
|
|
is_locked = false;
|
|
|
|
is_locked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -373,7 +307,7 @@ bool Modem::dsrOn()
|
|
|
|
int flags;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( !fd ) {
|
|
|
|
if ( fd == -1 ) {
|
|
|
|
#ifdef MODEM_DEBUG
|
|
|
|
#ifdef MODEM_DEBUG
|
|
|
|
fprintf( stderr, "Modem: dsrOn(): File not open.\n" );
|
|
|
|
fprintf( stderr, "Modem: dsrOn(): File not open.\n" );
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
@ -396,7 +330,7 @@ bool Modem::ctsOn()
|
|
|
|
int flags;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( !fd ) {
|
|
|
|
if ( fd == -1 ) {
|
|
|
|
#ifdef MODEM_DEBUG
|
|
|
|
#ifdef MODEM_DEBUG
|
|
|
|
fprintf( stderr, "Modem: ctsOn(): File not open.\n" );
|
|
|
|
fprintf( stderr, "Modem: ctsOn(): File not open.\n" );
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
@ -640,7 +574,7 @@ void Modem::init()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
is_locked = false;
|
|
|
|
is_locked = false;
|
|
|
|
|
|
|
|
|
|
|
|
fd = 0;
|
|
|
|
fd = -1;
|
|
|
|
sn = 0;
|
|
|
|
sn = 0;
|
|
|
|
|
|
|
|
|
|
|
|
cspeed = B38400;
|
|
|
|
cspeed = B38400;
|
|
|
|