author | zecke <zecke> | 2002-10-11 15:08:46 (UTC) |
---|---|---|
committer | zecke <zecke> | 2002-10-11 15:08:46 (UTC) |
commit | 8c6b8752f69aa527e51f00b3a8526fe03dd9e122 (patch) (side-by-side diff) | |
tree | 074d38fe859dc8482ab0adc53f340669cd501f6e /noncore/apps | |
parent | 4dff3be36a89c2e864278ce85c477f923c4e3407 (diff) | |
download | opie-8c6b8752f69aa527e51f00b3a8526fe03dd9e122.zip opie-8c6b8752f69aa527e51f00b3a8526fe03dd9e122.tar.gz opie-8c6b8752f69aa527e51f00b3a8526fe03dd9e122.tar.bz2 |
Introduce rawIO in the IOLayer
This is needed because ?-modem does not like buffered
IO at all and we at least double buffered it
While sz_transfer is now dead
we've filetransfer which is using DirectIO between IOLayer
and the SZ
IOSerial got adjusted to the rawIO introduction
-rw-r--r-- | noncore/apps/opie-console/filetransfer.cpp | 142 | ||||
-rw-r--r-- | noncore/apps/opie-console/filetransfer.h | 38 | ||||
-rw-r--r-- | noncore/apps/opie-console/io_layer.cpp | 3 | ||||
-rw-r--r-- | noncore/apps/opie-console/io_layer.h | 7 | ||||
-rw-r--r-- | noncore/apps/opie-console/io_serial.cpp | 7 | ||||
-rw-r--r-- | noncore/apps/opie-console/io_serial.h | 1 | ||||
-rw-r--r-- | noncore/apps/opie-console/sz_transfer.cpp | 121 |
7 files changed, 212 insertions, 107 deletions
diff --git a/noncore/apps/opie-console/filetransfer.cpp b/noncore/apps/opie-console/filetransfer.cpp new file mode 100644 index 0000000..78982bd --- a/dev/null +++ b/noncore/apps/opie-console/filetransfer.cpp @@ -0,0 +1,142 @@ +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> + +#include <qcstring.h> +#include <qsocketnotifier.h> + +#include <opie/oprocess.h> + +#include "filetransfer.h" + +bool FileTransfer::terminate = false; +pid_t FileTransfer::m_pid; + +FileTransfer::FileTransfer( Type t, IOLayer* lay ) + : FileTransferLayer( lay ), m_type( t ) { + signal( SIGCHLD, signal_handler ); +} +FileTransfer::~FileTransfer() { +} + +/** + * now we will send the file. + * + * we request an fd. The IOLayer should be closed + * then we will setup a pipe for progress communication + * then we will dup2 the m_fd in the forked process + * to do direct IO from and to the fd + */ +void FileTransfer::sendFile( const QString& file ) { + m_fd = layer()->rawIO(); +// +// m_fd = ::open("/dev/ttyS0", O_RDWR); + + + if ( pipe( m_comm ) < 0 ) + m_comm[0] = m_comm[1] = 0; + if ( pipe( m_info ) < 0 ) + m_info[0] = m_info[1] = 0; + + qWarning("output:"+file ); + m_pid = fork(); + switch( m_pid ) { + case 0:{ + setupChild(); + /* exec */ + char* verbose = "-vv"; + char* binray = "-b"; + + + /* we should never return from here */ + execlp("sz", "sz", verbose, binray, file.latin1(), NULL ); + + /* communication for error!*/ + char resultByte =1; + if (m_info[1] ) + write(m_info[1], &resultByte, 1 ); + _exit( -1 ); + break; + } + default:{ + if ( m_info[1] ) + close( m_info[1] ); + if ( m_info[0] ) for (;;) { + char resultByte; int len; + len = read(m_info[0], &resultByte, 1 ); + /* len == 1 start up failed */ + if ( len == 1 ) + return; + if ( len == -1 ) + if ( (errno == ECHILD ) || (errno == EINTR ) ) + continue; + + // len == 0 or something like this + break; + } + if ( m_info[0] ) + close( m_info[0] ); + + terminate = false; + fd_set fds; + struct timeval timeout; + int sel; + + /* replace by QSocketNotifier!!! */ + while (!terminate) { + FD_ZERO( &fds ); + FD_SET( m_comm[0], &fds ); + sel = select( m_comm[0]+1, &fds, NULL, NULL, &timeout ); + if (sel ) { + if ( FD_ISSET(m_comm[0], &fds ) ) { + printf("out:"); + QByteArray ar(4096); + int len = read(m_comm[0], ar.data(), 4096 ); + for (int i = 0; i < len; i++ ) { + // printf("%c", ar[i] ); + } + printf("\n"); + } + } + } + break; + } + } +} +void FileTransfer::sendFile( const QFile& file ) { + sendFile( file.name() ); +} +void FileTransfer::signal_handler(int ) { + int status; + signal( SIGCHLD, signal_handler ); + waitpid( m_pid, &status, WNOHANG ); + terminate = true; +} +void FileTransfer::setupChild() { + /* + * we do not want to read from our + * information channel + */ + if (m_info[0] ) + close(m_info[0] ); + /* + * FD_CLOEXEC will close the + * fd on successfull exec + */ + if (m_info[1] ) + fcntl(m_info[1], F_SETFD, FD_CLOEXEC ); + + if (m_comm[0] ) + close( m_comm[0] ); + /* + * now set the communication + * m_fd STDIN_FILENO + * STDOUT_FILENO + * STDERR_FILENO + */ + dup2( m_fd, STDIN_FILENO ); + dup2( m_fd, STDOUT_FILENO ); + dup2( m_comm[1], STDERR_FILENO ); +} diff --git a/noncore/apps/opie-console/filetransfer.h b/noncore/apps/opie-console/filetransfer.h new file mode 100644 index 0000000..06c6d12 --- a/dev/null +++ b/noncore/apps/opie-console/filetransfer.h @@ -0,0 +1,38 @@ +#ifndef OPIE_FILE_TRANSFER_H +#define OPIE_FILE_TRANSFER_H + +#include <sys/types.h> + +#include <qfile.h> + +#include "file_layer.h" + +class QSocketNotifier; +class OProcess; +class FileTransfer : public FileTransferLayer{ + Q_OBJECT +public: + enum Type { + SZ = 0, + SX, + SY + }; + FileTransfer( Type t, IOLayer* ); + ~FileTransfer(); + + void sendFile( const QString& file ); + void sendFile( const QFile& ); + +private slots: + void setupChild(); +private: + static pid_t m_pid; + int m_fd; + int m_info[2]; + int m_comm[2]; + Type m_type; + static void signal_handler(int); + static bool terminate; +}; + +#endif diff --git a/noncore/apps/opie-console/io_layer.cpp b/noncore/apps/opie-console/io_layer.cpp index 563a252..79d47f5 100644 --- a/noncore/apps/opie-console/io_layer.cpp +++ b/noncore/apps/opie-console/io_layer.cpp @@ -12,3 +12,6 @@ IOLayer::IOLayer(const Profile &) IOLayer::~IOLayer() { } +int IOLayer::rawIO()const{ + return -1; +} diff --git a/noncore/apps/opie-console/io_layer.h b/noncore/apps/opie-console/io_layer.h index b891b2b..7745021 100644 --- a/noncore/apps/opie-console/io_layer.h +++ b/noncore/apps/opie-console/io_layer.h @@ -49,6 +49,13 @@ public: * a short name */ virtual QString name() const = 0; + + /** + * a file descriptor which opens + * the device for io but does not + * do any ioctling on it... + */ + virtual int rawIO()const; signals: /** * received input as QCString diff --git a/noncore/apps/opie-console/io_serial.cpp b/noncore/apps/opie-console/io_serial.cpp index 929aeff..845f4be 100644 --- a/noncore/apps/opie-console/io_serial.cpp +++ b/noncore/apps/opie-console/io_serial.cpp @@ -157,7 +157,7 @@ void IOSerial::errorOccured() { } void IOSerial::dataArrived() { - QByteArray array(4096); + QByteArray array(4097); int len = read(m_fd, array.data(), 4096); if (len == 0) @@ -175,3 +175,8 @@ QString IOSerial::identifier() const { QString IOSerial::name() const { return "RS232 Serial IO Layer"; } +int IOSerial::rawIO()const { + int fd = ::open(m_device, O_RDWR ); + + return fd; +}; diff --git a/noncore/apps/opie-console/io_serial.h b/noncore/apps/opie-console/io_serial.h index 521dac6..00a6d2c 100644 --- a/noncore/apps/opie-console/io_serial.h +++ b/noncore/apps/opie-console/io_serial.h @@ -35,6 +35,7 @@ public: QString identifier() const; QString name() const; + int rawIO()const; signals: void received(const QByteArray &); void error(int, const QString &); diff --git a/noncore/apps/opie-console/sz_transfer.cpp b/noncore/apps/opie-console/sz_transfer.cpp index 41917c0..0a315cf 100644 --- a/noncore/apps/opie-console/sz_transfer.cpp +++ b/noncore/apps/opie-console/sz_transfer.cpp @@ -5,102 +5,6 @@ #include <stdio.h> #include <sys/termios.h> -/* following function ripped out of minicom's sysdep2.c */ - - -/* - * Set cbreak mode. - * Mode 0 = normal. - * Mode 1 = cbreak, no echo - * Mode 2 = raw, no echo. - * Mode 3 = only return erasechar (for wkeys.c) - * - * Returns: the current erase character. - */ - -static struct termios savetty; -int setcbreak(int mode) -{ -#if 1 - struct termios tty; - static int init = 0; - static int erasechar; - - - if (init == 0) { - tcgetattr(0, &savetty); - erasechar = savetty.c_cc[VERASE]; - init++; - } - - if (mode == 3) return(erasechar); - - - - /* Always return to default settings first */ - tcsetattr(0, TCSADRAIN, &savetty); - - if (mode == 0) { - return(erasechar); - } - - tcgetattr(0, &tty); - if (mode == 1) { - tty.c_oflag &= ~OPOST; - tty.c_lflag &= ~(XCASE|ECHONL|NOFLSH); - tty.c_lflag &= ~(ICANON | ISIG | ECHO); - tty.c_iflag &= ~(ICRNL|INLCR); - tty.c_cflag |= CREAD; - tty.c_cc[VTIME] = 5; - tty.c_cc[VMIN] = 1; - } - if (mode == 2) { /* raw */ - tty.c_iflag &= ~(IGNBRK | IGNCR | INLCR | ICRNL | IUCLC | - IXANY | IXON | IXOFF | INPCK | ISTRIP); - tty.c_iflag |= (BRKINT | IGNPAR); - tty.c_oflag &= ~OPOST; - tty.c_lflag &= ~(XCASE|ECHONL|NOFLSH); - tty.c_lflag &= ~(ICANON | ISIG | ECHO); - tty.c_cflag |= CREAD; - tty.c_cc[VTIME] = 5; - tty.c_cc[VMIN] = 1; - } - tcsetattr(0, TCSADRAIN, &tty); - return(erasechar); -#else - struct sgttyb args; - static int init = 0; - static int erasechar; - - if (init == 0) { - (void) ioctl(0, TIOCGETP, &savetty); - (void) ioctl(0, TIOCGETC, &savetty2); - erasechar = savetty.sg_erase; - init++; - } - - if (mode == 3) return(erasechar); - - if (mode == 0) { - (void) ioctl(0, TIOCSETP, &savetty); - (void) ioctl(0, TIOCSETC, &savetty2); - return(erasechar); - } - - (void) ioctl(0, TIOCGETP, &args); - if (mode == 1) { - args.sg_flags |= CBREAK; - args.sg_flags &= ~(ECHO|RAW); - } - if (mode == 2) { - args.sg_flags |= RAW; - args.sg_flags &= ~(ECHO|CBREAK); - } - (void) ioctl(0, TIOCSETP, &args); - return(erasechar); -#endif - -} SzTransfer::SzTransfer(Type t, IOLayer *layer) : FileTransferLayer(layer), m_t(t) @@ -121,10 +25,10 @@ void SzTransfer::sendFile(const QString& file) { proc = new OProcess; *proc << "sz"; - *proc << "-v" << "-v" << "-v" << "-b" << file; - connect(proc, SIGNAL(processExited(OProcess *)), + *proc << "-v" << "-v" << "-b" << file; + connect(proc, SIGNAL(processExited(OProcess *)), this, SLOT(sent())); - connect(proc, SIGNAL(receivedStdout(OProcess *, char *, int)), + connect(proc, SIGNAL(receivedStdout(OProcess *, char *, int)), this, SLOT(SzReceivedStdout(OProcess *, char *, int))); connect(proc, SIGNAL(receivedStderr(OProcess *, char *, int)), this, SLOT(SzReceivedStderr(OProcess *, char *, int))); @@ -136,27 +40,32 @@ void SzTransfer::sendFile(const QString& file) { void SzTransfer::SzReceivedStdout(OProcess *, char *buffer, int buflen) { - //qWarning("recieved from sz %d bytes", buflen); + qWarning("recieved from sz on stdout %d bytes", buflen); QByteArray data(buflen); data.fill(*buffer, buflen); + for (uint i = 0; i < data.count(); i++ ) { + printf("%c", buffer[i] ); + } + printf("\n"); // send out through the io layer - (layer())->send(data); + layer()->send(data); } void SzTransfer::SzReceivedStderr(OProcess *, char *buffer, int length) { // parse and show data in a progress dialog/widget - printf("\n"); - for (int i = 0; i < length; i++) - printf("%c", buffer[i]); + printf("stderr:\n"); + //for (int i = 0; i < length; i++) + // printf("%c", buffer[i]); + //printf("\n"); } void SzTransfer::receivedStdin(const QByteArray &data) { - //qWarning("recieved from io_serial %d bytes", data.size()); - + qWarning("recieved from io_serial %d bytes", data.size()); + // recieved data from the io layer goes to sz proc->writeStdin(data.data(), data.size()); |