summaryrefslogtreecommitdiffstats
path: root/kandy/src/modem.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'kandy/src/modem.cpp')
-rw-r--r--kandy/src/modem.cpp152
1 files changed, 43 insertions, 109 deletions
diff --git a/kandy/src/modem.cpp b/kandy/src/modem.cpp
index 86c36d675..2a275bf4d 100644
--- a/kandy/src/modem.cpp
+++ b/kandy/src/modem.cpp
@@ -30,6 +30,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
+#include <sys/file.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
@@ -75,10 +76,10 @@
Modem::Modem( KandyPrefs *kprefs, TQObject *parent, const char *name ) :
- TQObject(parent, name)
+ TQObject(parent, name), fd(-1)
{
mOpen = false;
-
+
prefs = kprefs;
timer = new TQTimer( this, "modemtimer" );
@@ -185,28 +186,31 @@ bool Modem::open()
{
struct termios tty;
-
close();
- if ( !lockDevice() )
- return false;
+ if (fd == -1)
+ {
+ TQCString dev = TQFile::encodeName( (*prefs).serialDevice() );
+ const char *fdev = dev.data();
+ if ( ( fd = ::open( fdev, O_RDWR | O_NOCTTY | O_NONBLOCK ) ) == -1 ) {
+ emit errorMessage( i18n( "Unable to open device '%1'. "
+ "Please check that you have sufficient permissions." )
+ .arg( fdev ) );
+ return false;
+ }
+ }
- TQCString dev = TQFile::encodeName( (*prefs).serialDevice() );
- const char *fdev = dev.data();
- if ( ( fd = ::open( fdev, O_RDWR | O_NOCTTY | O_NONBLOCK ) ) == -1 ) {
- emit errorMessage( i18n( "Unable to open device '%1'. "
- "Please check that you have sufficient permissions." )
- .arg( fdev ) );
+ if ( !lockDevice() )
return false;
- }
tcflush( fd, TCIOFLUSH );
if ( tcgetattr( fd, &init_tty ) == -1 ) {
int errnumber = errno;
emit errorMessage( i18n( "Communication setup failed (tcgetattr code: %1)" )
.arg(strerror(errnumber)) );
+ unlockDevice();
::close( fd );
- fd = 0;
+ fd = -1;
return false;
}
@@ -221,8 +225,9 @@ bool Modem::open()
if ( tcsetattr( fd, TCSANOW, &tty ) == -1 ) {
emit errorMessage( i18n( "tcsetattr() failed." ) );
+ unlockDevice();
::close( fd );
- fd = 0;
+ fd = -1;
return false;
}
@@ -244,127 +249,56 @@ void Modem::close()
delete sn;
sn = 0;
+ unlockDevice();
+
if ( fd ) {
tcflush( fd, TCIOFLUSH );
tcsetattr( fd, TCSANOW, &init_tty );
::close( fd );
- fd = 0;
+ fd = -1;
}
xreset();
- unlockDevice();
-
mOpen = false;
}
void Modem::flush()
{
- if ( fd ) {
+ if ( fd != -1) {
tcflush( fd, TCIOFLUSH );
bufpos = 0;
}
}
-#ifdef HAVE_LOCKDEV
-#include <lockdev.h>
-#endif
-
bool Modem::lockDevice()
{
- if ( is_locked )
+ if (is_locked)
return true;
-#ifdef HAVE_LOCKDEV
- is_locked = !dev_lock( (*prefs).serialDevice().local8Bit() );
- if (!is_locked)
- 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;
+ if (flock(fd, LOCK_EX))
+ {
+ // Locking failed
+ is_locked = false;
+ emit errorMessage(i18n("Unable to lock device '%1'.").arg((*prefs).serialDevice()));
+ }
+ else
+ {
+ is_locked = true;
}
- 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;
-
- return true;
-#endif
+ return is_locked;
}
void Modem::unlockDevice()
{
-#ifdef HAVE_LOCKDEV
- dev_unlock( (*prefs).serialDevice().local8Bit(), getpid() );
-#else
- if ( is_locked ) {
- TQStringList pathList = TQStringList::split( "/", (*prefs).serialDevice() );
-
- TQFile::remove( (*prefs).lockDirectory() + "/LCK.." + pathList.last() );
- is_locked = false;
- }
-#endif
+ if (fd != -1 && is_locked)
+ {
+ flock(fd, LOCK_UN);
+ is_locked = false;
+ }
}
@@ -373,7 +307,7 @@ bool Modem::dsrOn()
int flags;
- if ( !fd ) {
+ if ( fd == -1 ) {
#ifdef MODEM_DEBUG
fprintf( stderr, "Modem: dsrOn(): File not open.\n" );
#endif
@@ -396,7 +330,7 @@ bool Modem::ctsOn()
int flags;
- if ( !fd ) {
+ if ( fd == -1 ) {
#ifdef MODEM_DEBUG
fprintf( stderr, "Modem: ctsOn(): File not open.\n" );
#endif
@@ -640,7 +574,7 @@ void Modem::init()
{
is_locked = false;
- fd = 0;
+ fd = -1;
sn = 0;
cspeed = B38400;
@@ -687,7 +621,7 @@ ushort Modem::calcCRC()
int i, j;
ushort c = 0;
-
+
for ( i = 0; i < xsize; i++ ) {
c ^= (ushort) buffer[ i ] << 8;
for ( j = 0; j < 8; j++ )