summaryrefslogtreecommitdiff
authorzecke <zecke>2002-10-12 01:21:45 (UTC)
committer zecke <zecke>2002-10-12 01:21:45 (UTC)
commitb256987cda5449e3abfd3bd4c2d0650530b5a562 (patch) (unidiff)
treeb739480699193ae0a2bd7a5ea9bfb3ce00923d8b
parenta92703ad1ed767713de21c2389b06434dc223eba (diff)
downloadopie-b256987cda5449e3abfd3bd4c2d0650530b5a562.zip
opie-b256987cda5449e3abfd3bd4c2d0650530b5a562.tar.gz
opie-b256987cda5449e3abfd3bd4c2d0650530b5a562.tar.bz2
Implemented Y-Moden and X-Modem
The parsing needs to ne adjusted for these gusys though..
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-console/filetransfer.cpp15
1 files changed, 14 insertions, 1 deletions
diff --git a/noncore/apps/opie-console/filetransfer.cpp b/noncore/apps/opie-console/filetransfer.cpp
index 97552fe..b39dc95 100644
--- a/noncore/apps/opie-console/filetransfer.cpp
+++ b/noncore/apps/opie-console/filetransfer.cpp
@@ -1,237 +1,250 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <errno.h> 3#include <errno.h>
4#include <fcntl.h> 4#include <fcntl.h>
5#include <unistd.h> 5#include <unistd.h>
6 6
7#include <qcstring.h> 7#include <qcstring.h>
8#include <qsocketnotifier.h> 8#include <qsocketnotifier.h>
9 9
10#include <opie/oprocess.h> 10#include <opie/oprocess.h>
11 11
12#include "procctl.h" 12#include "procctl.h"
13#include "filetransfer.h" 13#include "filetransfer.h"
14 14
15 15
16FileTransfer::FileTransfer( Type t, IOLayer* lay ) 16FileTransfer::FileTransfer( Type t, IOLayer* lay )
17 : FileTransferLayer( lay ), m_type( t ) { 17 : FileTransferLayer( lay ), m_type( t ) {
18 signal(SIGPIPE, SIG_IGN ); 18 signal(SIGPIPE, SIG_IGN );
19 19
20 m_not = 0l; 20 m_not = 0l;
21 m_proc = 0l; 21 m_proc = 0l;
22} 22}
23FileTransfer::~FileTransfer() { 23FileTransfer::~FileTransfer() {
24} 24}
25 25
26/** 26/**
27 * now we will send the file. 27 * now we will send the file.
28 * 28 *
29 * we request an fd. The IOLayer should be closed 29 * we request an fd. The IOLayer should be closed
30 * then we will setup a pipe for progress communication 30 * then we will setup a pipe for progress communication
31 * then we will dup2 the m_fd in the forked process 31 * then we will dup2 the m_fd in the forked process
32 * to do direct IO from and to the fd 32 * to do direct IO from and to the fd
33 */ 33 */
34void FileTransfer::sendFile( const QString& file ) { 34void FileTransfer::sendFile( const QString& file ) {
35 m_prog =-1; 35 m_prog =-1;
36 m_fd = layer()->rawIO(); 36 m_fd = layer()->rawIO();
37// 37//
38// m_fd = ::open("/dev/ttyS0", O_RDWR); 38// m_fd = ::open("/dev/ttyS0", O_RDWR);
39 39
40 m_file = file; 40 m_file = file;
41 if ( pipe( m_comm ) < 0 ) 41 if ( pipe( m_comm ) < 0 )
42 m_comm[0] = m_comm[1] = 0; 42 m_comm[0] = m_comm[1] = 0;
43 if ( pipe( m_info ) < 0 ) 43 if ( pipe( m_info ) < 0 )
44 m_info[0] = m_info[1] = 0; 44 m_info[0] = m_info[1] = 0;
45 45
46 46
47 m_pid = fork(); 47 m_pid = fork();
48 switch( m_pid ) { 48 switch( m_pid ) {
49 case -1: 49 case -1:
50 emit error( StartError, tr("Was not able to fork") ); 50 emit error( StartError, tr("Was not able to fork") );
51 break; 51 break;
52 case 0:{ 52 case 0:{
53 setupChild(); 53 setupChild();
54 qWarning("output:"+file ); 54 qWarning("output:"+file );
55 /* exec */ 55 /* exec */
56 char* verbose = "-vv"; 56 char* verbose = "-vv";
57 char* binray = "-b"; 57 char* binray = "-b";
58 58
59 59
60 char* typus;
61 switch(m_type ) {
62 case SZ:
63 typus = "";
64 break;
65 case SX:
66 typus = "-X";
67 break;
68 case SY:
69 typus = "--ymodem";
70 break;
71 }
72
60 /* we should never return from here */ 73 /* we should never return from here */
61 execlp("sz", "sz", verbose, binray, file.latin1(), NULL ); 74 execlp("sz", "sz", verbose, binray, file.latin1(), typus, NULL );
62 75
63 /* communication for error!*/ 76 /* communication for error!*/
64 char resultByte =1; 77 char resultByte =1;
65 if (m_info[1] ) 78 if (m_info[1] )
66 write(m_info[1], &resultByte, 1 ); 79 write(m_info[1], &resultByte, 1 );
67 _exit( -1 ); 80 _exit( -1 );
68 break; 81 break;
69 } 82 }
70 default:{ 83 default:{
71 if ( m_info[1] ) 84 if ( m_info[1] )
72 close( m_info[1] ); 85 close( m_info[1] );
73 if ( m_info[0] ) for (;;) { 86 if ( m_info[0] ) for (;;) {
74 char resultByte; int len; 87 char resultByte; int len;
75 len = read(m_info[0], &resultByte, 1 ); 88 len = read(m_info[0], &resultByte, 1 );
76 /* len == 1 start up failed */ 89 /* len == 1 start up failed */
77 if ( len == 1 ) { 90 if ( len == 1 ) {
78 emit error( StartError, tr("Could not start") ); 91 emit error( StartError, tr("Could not start") );
79 return; 92 return;
80 } 93 }
81 if ( len == -1 ) 94 if ( len == -1 )
82 if ( (errno == ECHILD ) || (errno == EINTR ) ) 95 if ( (errno == ECHILD ) || (errno == EINTR ) )
83 continue; 96 continue;
84 97
85 // len == 0 or something like this 98 // len == 0 or something like this
86 break; 99 break;
87 } 100 }
88 if ( m_info[0] ) 101 if ( m_info[0] )
89 close( m_info[0] ); 102 close( m_info[0] );
90 103
91 104
92 105
93 /* replace by QSocketNotifier!!! */ 106 /* replace by QSocketNotifier!!! */
94 m_not = new QSocketNotifier(m_comm[0], QSocketNotifier::Read ); 107 m_not = new QSocketNotifier(m_comm[0], QSocketNotifier::Read );
95 connect(m_not, SIGNAL(activated(int) ), 108 connect(m_not, SIGNAL(activated(int) ),
96 this, SLOT(slotRead() ) ); 109 this, SLOT(slotRead() ) );
97 if ( pipe(m_term) < 0 ) 110 if ( pipe(m_term) < 0 )
98 m_term[0] = m_term[1] = 0; 111 m_term[0] = m_term[1] = 0;
99 112
100 ProcCtl::self()->add(m_pid, m_term[1] ); 113 ProcCtl::self()->add(m_pid, m_term[1] );
101 m_proc = new QSocketNotifier(m_term[0], QSocketNotifier::Read ); 114 m_proc = new QSocketNotifier(m_term[0], QSocketNotifier::Read );
102 connect(m_proc, SIGNAL(activated(int) ), 115 connect(m_proc, SIGNAL(activated(int) ),
103 this, SLOT(slotExec() ) ); 116 this, SLOT(slotExec() ) );
104 117
105 } 118 }
106 break; 119 break;
107 } 120 }
108} 121}
109/* 122/*
110 * let's call the one with the filename 123 * let's call the one with the filename
111 */ 124 */
112void FileTransfer::sendFile( const QFile& file ) { 125void FileTransfer::sendFile( const QFile& file ) {
113 sendFile( file.name() ); 126 sendFile( file.name() );
114} 127}
115 128
116/* 129/*
117 * setting up communication 130 * setting up communication
118 * between parent child and ioLayer 131 * between parent child and ioLayer
119 */ 132 */
120void FileTransfer::setupChild() { 133void FileTransfer::setupChild() {
121 /* 134 /*
122 * we do not want to read from our 135 * we do not want to read from our
123 * information channel 136 * information channel
124 */ 137 */
125 if (m_info[0] ) 138 if (m_info[0] )
126 close(m_info[0] ); 139 close(m_info[0] );
127 /* 140 /*
128 * FD_CLOEXEC will close the 141 * FD_CLOEXEC will close the
129 * fd on successfull exec 142 * fd on successfull exec
130 */ 143 */
131 if (m_info[1] ) 144 if (m_info[1] )
132 fcntl(m_info[1], F_SETFD, FD_CLOEXEC ); 145 fcntl(m_info[1], F_SETFD, FD_CLOEXEC );
133 146
134 if (m_comm[0] ) 147 if (m_comm[0] )
135 close( m_comm[0] ); 148 close( m_comm[0] );
136 /* 149 /*
137 * now set the communication 150 * now set the communication
138 * m_fd STDIN_FILENO 151 * m_fd STDIN_FILENO
139 * STDOUT_FILENO 152 * STDOUT_FILENO
140 * STDERR_FILENO 153 * STDERR_FILENO
141 */ 154 */
142 dup2( m_fd, STDIN_FILENO ); 155 dup2( m_fd, STDIN_FILENO );
143 dup2( m_fd, STDOUT_FILENO ); 156 dup2( m_fd, STDOUT_FILENO );
144 dup2( m_comm[1], STDERR_FILENO ); 157 dup2( m_comm[1], STDERR_FILENO );
145} 158}
146 159
147/* 160/*
148 * read from the stderr of the child 161 * read from the stderr of the child
149 * process 162 * process
150 */ 163 */
151void FileTransfer::slotRead() { 164void FileTransfer::slotRead() {
152 QByteArray ar(4096); 165 QByteArray ar(4096);
153 int len = read(m_comm[0], ar.data(), 4096 ); 166 int len = read(m_comm[0], ar.data(), 4096 );
154 qWarning("slot read %d", len); 167 qWarning("slot read %d", len);
155 for (int i = 0; i < len; i++ ) { 168 for (int i = 0; i < len; i++ ) {
156 // printf("%c", ar[i] ); 169 // printf("%c", ar[i] );
157 } 170 }
158 ar.resize( len ); 171 ar.resize( len );
159 QString str( ar ); 172 QString str( ar );
160 qWarning(str.simplifyWhiteSpace() ); 173 qWarning(str.simplifyWhiteSpace() );
161 QStringList lis = QStringList::split(' ', str ); 174 QStringList lis = QStringList::split(' ', str );
162 /* 175 /*
163 * Transfer finished.. either complete or incomplete 176 * Transfer finished.. either complete or incomplete
164 */ 177 */
165 if ( lis[0].simplifyWhiteSpace() == "Transfer" ) { 178 if ( lis[0].simplifyWhiteSpace() == "Transfer" ) {
166 qWarning("sent!!!!"); 179 qWarning("sent!!!!");
167 emit sent(); 180 emit sent();
168 return; 181 return;
169 } 182 }
170 /* 183 /*
171 * do progress reading 184 * do progress reading
172 */ 185 */
173 slotProgress( lis ); 186 slotProgress( lis );
174 187
175 188
176} 189}
177/* 190/*
178 * find the progress 191 * find the progress
179 */ 192 */
180void FileTransfer::slotProgress( const QStringList& list ) { 193void FileTransfer::slotProgress( const QStringList& list ) {
181 bool complete = true; 194 bool complete = true;
182 int min, sec; 195 int min, sec;
183 int bps; 196 int bps;
184 unsigned long sent, total; 197 unsigned long sent, total;
185 198
186 min = sec = bps = -1; 199 min = sec = bps = -1;
187 sent = total = 0; 200 sent = total = 0;
188 201
189 // Data looks like this 202 // Data looks like this
190 // 0 1 2 3 4 5 203 // 0 1 2 3 4 5
191 // Bytes Sent 65536/11534336 BPS:7784 ETA 24:33 204 // Bytes Sent 65536/11534336 BPS:7784 ETA 24:33
192 QStringList progi = QStringList::split('/', list[2].simplifyWhiteSpace() ); 205 QStringList progi = QStringList::split('/', list[2].simplifyWhiteSpace() );
193 sent = progi[0].toULong(&complete ); 206 sent = progi[0].toULong(&complete );
194 if (!complete ) return; 207 if (!complete ) return;
195 208
196 total = progi[1].toULong(&complete ); 209 total = progi[1].toULong(&complete );
197 if (!complete || total == 0) { 210 if (!complete || total == 0) {
198 return; 211 return;
199 } 212 }
200 213
201 qWarning("%s, %d, %d", progi.join("/").latin1(), sent, total ); 214 qWarning("%s, %d, %d", progi.join("/").latin1(), sent, total );
202 215
203 double pro = (double)sent/total; 216 double pro = (double)sent/total;
204 int prog = pro * 100; 217 int prog = pro * 100;
205 218
206 // speed 219 // speed
207 progi = QStringList::split(':', list[3].simplifyWhiteSpace() ); 220 progi = QStringList::split(':', list[3].simplifyWhiteSpace() );
208 bps = progi[1].toInt(); 221 bps = progi[1].toInt();
209 222
210 // time 223 // time
211 progi = QStringList::split(':', list[5].simplifyWhiteSpace() ); 224 progi = QStringList::split(':', list[5].simplifyWhiteSpace() );
212 min = progi[0].toInt(); 225 min = progi[0].toInt();
213 sec = progi[1].toInt(); 226 sec = progi[1].toInt();
214 227
215 228
216 if ( prog > m_prog ) { 229 if ( prog > m_prog ) {
217 m_prog = prog; 230 m_prog = prog;
218 emit progress(m_file, m_prog, bps, -1, min , sec ); 231 emit progress(m_file, m_prog, bps, -1, min , sec );
219 } 232 }
220 233
221} 234}
222void FileTransfer::cancel() { 235void FileTransfer::cancel() {
223 ::kill(m_pid,9 ); 236 ::kill(m_pid,9 );
224 delete m_not; 237 delete m_not;
225} 238}
226void FileTransfer::slotExec() { 239void FileTransfer::slotExec() {
227 qWarning("exited!"); 240 qWarning("exited!");
228 char buf[2]; 241 char buf[2];
229 ::read(m_term[0], buf, 1 ); 242 ::read(m_term[0], buf, 1 );
230 delete m_proc; 243 delete m_proc;
231 delete m_not; 244 delete m_not;
232 close( m_term[0] ); 245 close( m_term[0] );
233 close( m_term[1] ); 246 close( m_term[1] );
234 close( m_comm[0] ); 247 close( m_comm[0] );
235 close( m_comm[1] ); 248 close( m_comm[1] );
236 emit sent(); 249 emit sent();
237} 250}