summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings/ppp/modem.cpp
Unidiff
Diffstat (limited to 'noncore/settings/networksettings/ppp/modem.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/networksettings/ppp/modem.cpp45
1 files changed, 22 insertions, 23 deletions
diff --git a/noncore/settings/networksettings/ppp/modem.cpp b/noncore/settings/networksettings/ppp/modem.cpp
index 5139482..002c8e7 100644
--- a/noncore/settings/networksettings/ppp/modem.cpp
+++ b/noncore/settings/networksettings/ppp/modem.cpp
@@ -17,259 +17,258 @@
17 * This program is distributed in the hope that it will be useful, 17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details. 20 * Library General Public License for more details.
21 * 21 *
22 * You should have received a copy of the GNU Library General Public 22 * You should have received a copy of the GNU Library General Public
23 * License along with this program; if not, write to the Free 23 * License along with this program; if not, write to the Free
24 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */ 25 */
26 26
27#include <errno.h> 27#include <errno.h>
28#include <stdlib.h> 28#include <stdlib.h>
29#include <unistd.h> 29#include <unistd.h>
30#include <fcntl.h> 30#include <fcntl.h>
31#include <signal.h> 31#include <signal.h>
32#include <sys/ioctl.h> 32#include <sys/ioctl.h>
33#include <setjmp.h> 33#include <setjmp.h>
34#include <regex.h> 34#include <regex.h>
35#include <qregexp.h> 35#include <qregexp.h>
36#include <assert.h> 36#include <assert.h>
37#include <string.h> 37#include <string.h>
38 38
39#ifdef HAVE_RESOLV_H 39#ifdef HAVE_RESOLV_H
40# include <arpa/nameser.h> 40# include <arpa/nameser.h>
41# include <resolv.h> 41# include <resolv.h>
42#endif 42#endif
43 43
44#ifndef _PATH_RESCONF 44#ifndef _PATH_RESCONF
45#define _PATH_RESCONF "/etc/resolv.conf" 45#define _PATH_RESCONF "/etc/resolv.conf"
46#endif 46#endif
47 47
48#define strlcpy strcpy 48#define strlcpy strcpy
49#include "auth.h" 49#include "auth.h"
50#include "modem.h" 50#include "modem.h"
51#include "pppdata.h" 51#include "pppdata.h"
52//#include <klocale.h> 52//#include <klocale.h>
53#define i18n QObject::tr 53#define i18n QObject::tr
54#define qError qDebug 54#define qError qDebug
55//#include <kdebug.h> 55//#include <kdebug.h>
56//#include <config.h> 56//#include <config.h>
57 57
58#define MY_ASSERT(x) if (!(x)) { \ 58#define MY_ASSERT(x) if (!(x)) { \
59 qFatal( "ASSERT: \"%s\" in %s (%d)\n",#x,__FILE__,__LINE__); \ 59 qFatal( "ASSERT: \"%s\" in %s (%d)\n",#x,__FILE__,__LINE__); \
60 exit(1); } 60 exit(1); }
61 61
62 62
63static sigjmp_buf jmp_buffer; 63static sigjmp_buf jmp_buffer;
64 64
65Modem *Modem::modem = 0; 65//Modem *Modem::modem = 0;
66 66
67 67
68const char* pppdPath() { 68const char* pppdPath() {
69 // wasting a few bytes 69 // wasting a few bytes
70 static char buffer[sizeof(PPPDSEARCHPATH)+sizeof(PPPDNAME)]; 70 static char buffer[sizeof(PPPDSEARCHPATH)+sizeof(PPPDNAME)];
71 static char *pppdPath = 0L; 71 static char *pppdPath = 0L;
72 char *p; 72 char *p;
73 73
74 if(pppdPath == 0L) { 74 if(pppdPath == 0L) {
75 const char *c = PPPDSEARCHPATH; 75 const char *c = PPPDSEARCHPATH;
76 while(*c != '\0') { 76 while(*c != '\0') {
77 while(*c == ':') 77 while(*c == ':')
78 c++; 78 c++;
79 p = buffer; 79 p = buffer;
80 while(*c != '\0' && *c != ':') 80 while(*c != '\0' && *c != ':')
81 *p++ = *c++; 81 *p++ = *c++;
82 *p = '\0'; 82 *p = '\0';
83 strcat(p, "/"); 83 strcat(p, "/");
84 strcat(p, PPPDNAME); 84 strcat(p, PPPDNAME);
85 if(access(buffer, F_OK) == 0) 85 if(access(buffer, F_OK) == 0)
86 return (pppdPath = buffer); 86 return (pppdPath = buffer);
87 } 87 }
88 } 88 }
89 89
90 return pppdPath; 90 return pppdPath;
91} 91}
92 92
93 93
94Modem::Modem() 94Modem::Modem( PPPData* pd )
95{ 95{
96 if (Modem::modem != 0) return; //CORRECT? 96 _pppdata = pd;
97 modemfd = -1; 97 modemfd = -1;
98 _pppdExitStatus = -1; 98 _pppdExitStatus = -1;
99 pppdPid = -1; 99 pppdPid = -1;
100 sn = 0L; 100 sn = 0L;
101 data_mode = false; 101 data_mode = false;
102 modem_is_locked = false; 102 modem_is_locked = false;
103 lockfile[0] = '\0'; 103 lockfile[0] = '\0';
104 device = "/dev/modem"; 104 device = "/dev/modem";
105 modem = this;
106} 105}
107 106
108 107
109Modem::~Modem() { 108Modem::~Modem()
110 modem = 0; 109{
111} 110}
112 111
113 112
114speed_t Modem::modemspeed() { 113speed_t Modem::modemspeed() {
115 // convert the string modem speed int the gpppdata object to a t_speed type 114 // convert the string modem speed int the gpppdata object to a t_speed type
116 // to set the modem. The constants here should all be ifdef'd because 115 // to set the modem. The constants here should all be ifdef'd because
117 // other systems may not have them 116 // other systems may not have them
118 int i = PPPData::data()->speed().toInt()/100; 117 int i = _pppdata->speed().toInt()/100;
119 118
120 switch(i) { 119 switch(i) {
121 case 24: 120 case 24:
122 return B2400; 121 return B2400;
123 break; 122 break;
124 case 96: 123 case 96:
125 return B9600; 124 return B9600;
126 break; 125 break;
127 case 192: 126 case 192:
128 return B19200; 127 return B19200;
129 break; 128 break;
130 case 384: 129 case 384:
131 return B38400; 130 return B38400;
132 break; 131 break;
133#ifdef B57600 132#ifdef B57600
134 case 576: 133 case 576:
135 return B57600; 134 return B57600;
136 break; 135 break;
137#endif 136#endif
138 137
139#ifdef B115200 138#ifdef B115200
140 case 1152: 139 case 1152:
141 return B115200; 140 return B115200;
142 break; 141 break;
143#endif 142#endif
144 143
145#ifdef B230400 144#ifdef B230400
146 case 2304: 145 case 2304:
147 return B230400; 146 return B230400;
148 break; 147 break;
149#endif 148#endif
150 149
151#ifdef B460800 150#ifdef B460800
152 case 4608: 151 case 4608:
153 return B460800; 152 return B460800;
154 break; 153 break;
155#endif 154#endif
156 155
157 default: 156 default:
158 return B38400; 157 return B38400;
159 break; 158 break;
160 } 159 }
161} 160}
162 161
163bool Modem::opentty() { 162bool Modem::opentty() {
164 // int flags; 163 // int flags;
165 164
166//begin if((modemfd = Requester::rq->openModem(gpppdata.modemDevice()))<0) { 165//begin if((modemfd = Requester::rq->openModem(gpppdata.modemDevice()))<0) {
167 close(modemfd); 166 close(modemfd);
168 device = PPPData::data()->modemDevice(); 167 device = _pppdata->modemDevice();
169 if ((modemfd = open(device, O_RDWR|O_NDELAY|O_NOCTTY)) == -1) { 168 if ((modemfd = open(device, O_RDWR|O_NDELAY|O_NOCTTY)) == -1) {
170 qDebug("error opening modem device !"); 169 qDebug("error opening modem device !");
171 errmsg = i18n("Unable to open modem."); 170 errmsg = i18n("Unable to open modem.");
172 return false; 171 return false;
173 } 172 }
174//bend if((modemfd = Requester::rq->openModem(gpppdata.modemDevice()))<0) { 173//bend if((modemfd = Requester::rq->openModem(gpppdata.modemDevice()))<0) {
175//} 174//}
176 175
177#if 0 176#if 0
178 if(PPPData::data()->UseCDLine()) { 177 if(_pppdata->UseCDLine()) {
179 if(ioctl(modemfd, TIOCMGET, &flags) == -1) { 178 if(ioctl(modemfd, TIOCMGET, &flags) == -1) {
180 errmsg = i18n("Unable to detect state of CD line."); 179 errmsg = i18n("Unable to detect state of CD line.");
181 ::close(modemfd); 180 ::close(modemfd);
182 modemfd = -1; 181 modemfd = -1;
183 return false; 182 return false;
184 } 183 }
185 if ((flags&TIOCM_CD) == 0) { 184 if ((flags&TIOCM_CD) == 0) {
186 errmsg = i18n("The modem is not ready."); 185 errmsg = i18n("The modem is not ready.");
187 ::close(modemfd); 186 ::close(modemfd);
188 modemfd = -1; 187 modemfd = -1;
189 return false; 188 return false;
190 } 189 }
191 } 190 }
192#endif 191#endif
193 192
194 tcdrain (modemfd); 193 tcdrain (modemfd);
195 tcflush (modemfd, TCIOFLUSH); 194 tcflush (modemfd, TCIOFLUSH);
196 195
197 if(tcgetattr(modemfd, &tty) < 0){ 196 if(tcgetattr(modemfd, &tty) < 0){
198 // this helps in some cases 197 // this helps in some cases
199 tcsendbreak(modemfd, 0); 198 tcsendbreak(modemfd, 0);
200 sleep(1); 199 sleep(1);
201 if(tcgetattr(modemfd, &tty) < 0){ 200 if(tcgetattr(modemfd, &tty) < 0){
202 errmsg = i18n("The modem is busy."); 201 errmsg = i18n("The modem is busy.");
203 ::close(modemfd); 202 ::close(modemfd);
204 modemfd = -1; 203 modemfd = -1;
205 return false; 204 return false;
206 } 205 }
207 } 206 }
208 207
209 memset(&initial_tty,'\0',sizeof(initial_tty)); 208 memset(&initial_tty,'\0',sizeof(initial_tty));
210 209
211 initial_tty = tty; 210 initial_tty = tty;
212 211
213 tty.c_cc[VMIN] = 0; // nonblocking 212 tty.c_cc[VMIN] = 0; // nonblocking
214 tty.c_cc[VTIME] = 0; 213 tty.c_cc[VTIME] = 0;
215 tty.c_oflag = 0; 214 tty.c_oflag = 0;
216 tty.c_lflag = 0; 215 tty.c_lflag = 0;
217 216
218 tty.c_cflag &= ~(CSIZE | CSTOPB | PARENB); 217 tty.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
219 tty.c_cflag |= CS8 | CREAD; 218 tty.c_cflag |= CS8 | CREAD;
220 tty.c_cflag |= CLOCAL; // ignore modem status lines 219 tty.c_cflag |= CLOCAL; // ignore modem status lines
221 tty.c_iflag = IGNBRK | IGNPAR /* | ISTRIP */ ; 220 tty.c_iflag = IGNBRK | IGNPAR /* | ISTRIP */ ;
222 tty.c_lflag &= ~ICANON; // non-canonical mode 221 tty.c_lflag &= ~ICANON; // non-canonical mode
223 tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHOKE); 222 tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHOKE);
224 223
225 224
226 if(PPPData::data()->flowcontrol() != "None") { 225 if(_pppdata->flowcontrol() != "None") {
227 if(PPPData::data()->flowcontrol() == "CRTSCTS") { 226 if(_pppdata->flowcontrol() == "CRTSCTS") {
228 tty.c_cflag |= CRTSCTS; 227 tty.c_cflag |= CRTSCTS;
229 } 228 }
230 else { 229 else {
231 tty.c_iflag |= IXON | IXOFF; 230 tty.c_iflag |= IXON | IXOFF;
232 tty.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */ 231 tty.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
233 tty.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */ 232 tty.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
234 } 233 }
235 } 234 }
236 else { 235 else {
237 tty.c_cflag &= ~CRTSCTS; 236 tty.c_cflag &= ~CRTSCTS;
238 tty.c_iflag &= ~(IXON | IXOFF); 237 tty.c_iflag &= ~(IXON | IXOFF);
239 } 238 }
240 239
241 cfsetospeed(&tty, modemspeed()); 240 cfsetospeed(&tty, modemspeed());
242 cfsetispeed(&tty, modemspeed()); 241 cfsetispeed(&tty, modemspeed());
243 242
244 tcdrain(modemfd); 243 tcdrain(modemfd);
245 244
246 if(tcsetattr(modemfd, TCSANOW, &tty) < 0){ 245 if(tcsetattr(modemfd, TCSANOW, &tty) < 0){
247 errmsg = i18n("The modem is busy."); 246 errmsg = i18n("The modem is busy.");
248 ::close(modemfd); 247 ::close(modemfd);
249 modemfd=-1; 248 modemfd=-1;
250 return false; 249 return false;
251 } 250 }
252 251
253 errmsg = i18n("Modem Ready."); 252 errmsg = i18n("Modem Ready.");
254 return true; 253 return true;
255} 254}
256 255
257 256
258bool Modem::closetty() { 257bool Modem::closetty() {
259 if(modemfd >=0 ) { 258 if(modemfd >=0 ) {
260 stop(); 259 stop();
261 /* discard data not read or transmitted */ 260 /* discard data not read or transmitted */
262 tcflush(modemfd, TCIOFLUSH); 261 tcflush(modemfd, TCIOFLUSH);
263 262
264 if(tcsetattr(modemfd, TCSANOW, &initial_tty) < 0){ 263 if(tcsetattr(modemfd, TCSANOW, &initial_tty) < 0){
265 errmsg = i18n("Can't restore tty settings: tcsetattr()\n"); 264 errmsg = i18n("Can't restore tty settings: tcsetattr()\n");
266 ::close(modemfd); 265 ::close(modemfd);
267 modemfd = -1; 266 modemfd = -1;
268 return false; 267 return false;
269 } 268 }
270 ::close(modemfd); 269 ::close(modemfd);
271 modemfd = -1; 270 modemfd = -1;
272 } 271 }
273 272
274 return true; 273 return true;
275} 274}
@@ -308,202 +307,202 @@ void Modem::startNotifier() {
308 if(sn == 0) { 307 if(sn == 0) {
309 sn = new QSocketNotifier(modemfd, QSocketNotifier::Read, this); 308 sn = new QSocketNotifier(modemfd, QSocketNotifier::Read, this);
310 connect(sn, SIGNAL(activated(int)), SLOT(readtty(int))); 309 connect(sn, SIGNAL(activated(int)), SLOT(readtty(int)));
311 qDebug("QSocketNotifier started!"); 310 qDebug("QSocketNotifier started!");
312 } else { 311 } else {
313 qDebug("QSocketNotifier re-enabled!"); 312 qDebug("QSocketNotifier re-enabled!");
314 sn->setEnabled(true); 313 sn->setEnabled(true);
315 } 314 }
316 } 315 }
317} 316}
318 317
319 318
320void Modem::stopNotifier() { 319void Modem::stopNotifier() {
321 if(sn != 0) { 320 if(sn != 0) {
322 sn->setEnabled(false); 321 sn->setEnabled(false);
323 disconnect(sn); 322 disconnect(sn);
324 delete sn; 323 delete sn;
325 sn = 0; 324 sn = 0;
326 qDebug( "QSocketNotifier stopped!" ); 325 qDebug( "QSocketNotifier stopped!" );
327 } 326 }
328} 327}
329 328
330 329
331void Modem::flush() { 330void Modem::flush() {
332 char c; 331 char c;
333 while(read(modemfd, &c, 1) == 1); 332 while(read(modemfd, &c, 1) == 1);
334} 333}
335 334
336 335
337bool Modem::writeChar(unsigned char c) { 336bool Modem::writeChar(unsigned char c) {
338 int s; 337 int s;
339 do { 338 do {
340 s = write(modemfd, &c, 1); 339 s = write(modemfd, &c, 1);
341 if (s < 0) { 340 if (s < 0) {
342 qError( "write() in Modem::writeChar failed" ); 341 qError( "write() in Modem::writeChar failed" );
343 return false; 342 return false;
344 } 343 }
345 } while(s == 0); 344 } while(s == 0);
346 345
347 return true; 346 return true;
348} 347}
349 348
350 349
351bool Modem::writeLine(const char *buf) { 350bool Modem::writeLine(const char *buf) {
352 int len = strlen(buf); 351 int len = strlen(buf);
353 char *b = new char[len+2]; 352 char *b = new char[len+2];
354 memcpy(b, buf, len); 353 memcpy(b, buf, len);
355 // different modems seem to need different line terminations 354 // different modems seem to need different line terminations
356 QString term = PPPData::data()->enter(); 355 QString term = _pppdata->enter();
357 if(term == "LF") 356 if(term == "LF")
358 b[len++]='\n'; 357 b[len++]='\n';
359 else if(term == "CR") 358 else if(term == "CR")
360 b[len++]='\r'; 359 b[len++]='\r';
361 else if(term == "CR/LF") { 360 else if(term == "CR/LF") {
362 b[len++]='\r'; 361 b[len++]='\r';
363 b[len++]='\n'; 362 b[len++]='\n';
364 } 363 }
365 int l = len; 364 int l = len;
366 while(l) { 365 while(l) {
367 int wr = write(modemfd, &b[len-l], l); 366 int wr = write(modemfd, &b[len-l], l);
368 if(wr < 0) { 367 if(wr < 0) {
369 // TODO do something meaningful with the error code (or ignore it 368 // TODO do something meaningful with the error code (or ignore it
370 qError( "write() in Modem::writeLine failed" ); 369 qError( "write() in Modem::writeLine failed" );
371 delete[] b; 370 delete[] b;
372 return false; 371 return false;
373 } 372 }
374 l -= wr; 373 l -= wr;
375 } 374 }
376 delete[] b; 375 delete[] b;
377 return true; 376 return true;
378} 377}
379 378
380 379
381bool Modem::hangup() { 380bool Modem::hangup() {
382 // this should really get the modem to hang up and go into command mode 381 // this should really get the modem to hang up and go into command mode
383 // If anyone sees a fault in the following please let me know, since 382 // If anyone sees a fault in the following please let me know, since
384 // this is probably the most imporant snippet of code in the whole of 383 // this is probably the most imporant snippet of code in the whole of
385 // kppp. If people complain about kppp being stuck, this piece of code 384 // kppp. If people complain about kppp being stuck, this piece of code
386 // is most likely the reason. 385 // is most likely the reason.
387 struct termios temptty; 386 struct termios temptty;
388 387
389 if(modemfd >= 0) { 388 if(modemfd >= 0) {
390 389
391 // is this Escape & HangupStr stuff really necessary ? (Harri) 390 // is this Escape & HangupStr stuff really necessary ? (Harri)
392 391
393 if (data_mode) escape_to_command_mode(); 392 if (data_mode) escape_to_command_mode();
394 393
395 // Then hangup command 394 // Then hangup command
396 writeLine(PPPData::data()->modemHangupStr().local8Bit()); 395 writeLine(_pppdata->modemHangupStr().local8Bit());
397 396
398 usleep(PPPData::data()->modemInitDelay() * 10000); // 0.01 - 3.0 sec 397 usleep(_pppdata->modemInitDelay() * 10000); // 0.01 - 3.0 sec
399 398
400#ifndef DEBUG_WO_DIALING 399#ifndef DEBUG_WO_DIALING
401 if (sigsetjmp(jmp_buffer, 1) == 0) { 400 if (sigsetjmp(jmp_buffer, 1) == 0) {
402 // set alarm in case tcsendbreak() hangs 401 // set alarm in case tcsendbreak() hangs
403 signal(SIGALRM, alarm_handler); 402 signal(SIGALRM, alarm_handler);
404 alarm(2); 403 alarm(2);
405 404
406 tcsendbreak(modemfd, 0); 405 tcsendbreak(modemfd, 0);
407 406
408 alarm(0); 407 alarm(0);
409 signal(SIGALRM, SIG_IGN); 408 signal(SIGALRM, SIG_IGN);
410 } else { 409 } else {
411 // we reach this point if the alarm handler got called 410 // we reach this point if the alarm handler got called
412 closetty(); 411 closetty();
413 close(modemfd); 412 close(modemfd);
414 modemfd = -1; 413 modemfd = -1;
415 errmsg = i18n("The modem does not respond."); 414 errmsg = i18n("The modem does not respond.");
416 return false; 415 return false;
417 } 416 }
418 417
419#ifndef __svr4__ // drops DTR but doesn't set it afterwards again. not good for init. 418#ifndef __svr4__ // drops DTR but doesn't set it afterwards again. not good for init.
420 tcgetattr(modemfd, &temptty); 419 tcgetattr(modemfd, &temptty);
421 cfsetospeed(&temptty, B0); 420 cfsetospeed(&temptty, B0);
422 cfsetispeed(&temptty, B0); 421 cfsetispeed(&temptty, B0);
423 tcsetattr(modemfd, TCSAFLUSH, &temptty); 422 tcsetattr(modemfd, TCSAFLUSH, &temptty);
424#else 423#else
425 int modemstat; 424 int modemstat;
426 ioctl(modemfd, TIOCMGET, &modemstat); 425 ioctl(modemfd, TIOCMGET, &modemstat);
427 modemstat &= ~TIOCM_DTR; 426 modemstat &= ~TIOCM_DTR;
428 ioctl(modemfd, TIOCMSET, &modemstat); 427 ioctl(modemfd, TIOCMSET, &modemstat);
429 ioctl(modemfd, TIOCMGET, &modemstat); 428 ioctl(modemfd, TIOCMGET, &modemstat);
430 modemstat |= TIOCM_DTR; 429 modemstat |= TIOCM_DTR;
431 ioctl(modemfd, TIOCMSET, &modemstat); 430 ioctl(modemfd, TIOCMSET, &modemstat);
432#endif 431#endif
433 432
434 usleep(PPPData::data()->modemInitDelay() * 10000); // 0.01 - 3.0 secs 433 usleep(_pppdata->modemInitDelay() * 10000); // 0.01 - 3.0 secs
435 434
436 cfsetospeed(&temptty, modemspeed()); 435 cfsetospeed(&temptty, modemspeed());
437 cfsetispeed(&temptty, modemspeed()); 436 cfsetispeed(&temptty, modemspeed());
438 tcsetattr(modemfd, TCSAFLUSH, &temptty); 437 tcsetattr(modemfd, TCSAFLUSH, &temptty);
439#endif 438#endif
440 return true; 439 return true;
441 } else 440 } else
442 return false; 441 return false;
443} 442}
444 443
445 444
446void Modem::escape_to_command_mode() { 445void Modem::escape_to_command_mode() {
447 // Send Properly bracketed escape code to put the modem back into command state. 446 // Send Properly bracketed escape code to put the modem back into command state.
448 // A modem will accept AT commands only when it is in command state. 447 // A modem will accept AT commands only when it is in command state.
449 // When a modem sends the host the CONNECT string, that signals 448 // When a modem sends the host the CONNECT string, that signals
450 // that the modem is now in the connect state (no long accepts AT commands.) 449 // that the modem is now in the connect state (no long accepts AT commands.)
451 // Need to send properly timed escape sequence to put modem in command state. 450 // Need to send properly timed escape sequence to put modem in command state.
452 // Escape codes and guard times are controlled by S2 and S12 values. 451 // Escape codes and guard times are controlled by S2 and S12 values.
453 // 452 //
454 tcflush(modemfd, TCIOFLUSH); 453 tcflush(modemfd, TCIOFLUSH);
455 454
456 // +3 because quiet time must be greater than guard time. 455 // +3 because quiet time must be greater than guard time.
457 usleep((PPPData::data()->modemEscapeGuardTime()+3)*20000); 456 usleep((_pppdata->modemEscapeGuardTime()+3)*20000);
458 QCString tmp = PPPData::data()->modemEscapeStr().local8Bit(); 457 QCString tmp = _pppdata->modemEscapeStr().local8Bit();
459 write(modemfd, tmp.data(), tmp.length()); 458 write(modemfd, tmp.data(), tmp.length());
460 tcflush(modemfd, TCIOFLUSH); 459 tcflush(modemfd, TCIOFLUSH);
461 usleep((PPPData::data()->modemEscapeGuardTime()+3)*20000); 460 usleep((_pppdata->modemEscapeGuardTime()+3)*20000);
462 461
463 data_mode = false; 462 data_mode = false;
464} 463}
465 464
466 465
467const QString Modem::modemMessage() { 466const QString Modem::modemMessage() {
468 return errmsg; 467 return errmsg;
469} 468}
470 469
471 470
472QString Modem::parseModemSpeed(const QString &s) { 471QString Modem::parseModemSpeed(const QString &s) {
473 // this is a small (and bad) parser for modem speeds 472 // this is a small (and bad) parser for modem speeds
474 int rx = -1; 473 int rx = -1;
475 int tx = -1; 474 int tx = -1;
476 int i; 475 int i;
477 QString result; 476 QString result;
478 477
479 qDebug( "Modem reported result string: %s", s.latin1()); 478 qDebug( "Modem reported result string: %s", s.latin1());
480 479
481 const int RXMAX = 7; 480 const int RXMAX = 7;
482 const int TXMAX = 2; 481 const int TXMAX = 2;
483 QRegExp rrx[RXMAX] = { 482 QRegExp rrx[RXMAX] = {
484 QRegExp("[0-9]+[:/ ]RX", false), 483 QRegExp("[0-9]+[:/ ]RX", false),
485 QRegExp("[0-9]+RX", false), 484 QRegExp("[0-9]+RX", false),
486 QRegExp("[/: -][0-9]+[/: ]", false), 485 QRegExp("[/: -][0-9]+[/: ]", false),
487 QRegExp("[/: -][0-9]+$", false), 486 QRegExp("[/: -][0-9]+$", false),
488 QRegExp("CARRIER [^0-9]*[0-9]+", false), 487 QRegExp("CARRIER [^0-9]*[0-9]+", false),
489 QRegExp("CONNECT [^0-9]*[0-9]+", false), 488 QRegExp("CONNECT [^0-9]*[0-9]+", false),
490 QRegExp("[0-9]+") // panic mode 489 QRegExp("[0-9]+") // panic mode
491 }; 490 };
492 491
493 QRegExp trx[TXMAX] = { 492 QRegExp trx[TXMAX] = {
494 QRegExp("[0-9]+[:/ ]TX", false), 493 QRegExp("[0-9]+[:/ ]TX", false),
495 QRegExp("[0-9]+TX", false) 494 QRegExp("[0-9]+TX", false)
496 }; 495 };
497 496
498 for(i = 0; i < RXMAX; i++) { 497 for(i = 0; i < RXMAX; i++) {
499 int len, idx, result; 498 int len, idx, result;
500 if((idx = rrx[i].match(s,0,&len)) > -1) { 499 if((idx = rrx[i].match(s,0,&len)) > -1) {
501// if((idx = rrx[i].search(s)) > -1) { 500// if((idx = rrx[i].search(s)) > -1) {
502 // len = rrx[i].matchedLength(); 501 // len = rrx[i].matchedLength();
503 502
504 // 503 //
505 // rrx[i] has been matched, idx contains the start of the match 504 // rrx[i] has been matched, idx contains the start of the match
506 // and len contains how long the match is. Extract the match. 505 // and len contains how long the match is. Extract the match.
507 // 506 //
508 QString sub = s.mid(idx, len); 507 QString sub = s.mid(idx, len);
509 508
@@ -529,137 +528,137 @@ QString Modem::parseModemSpeed(const QString &s) {
529 if((idx = trx[i].match(s,0,&len)) > -1) { 528 if((idx = trx[i].match(s,0,&len)) > -1) {
530// if((idx = trx[i].search(s)) > -1) { 529// if((idx = trx[i].search(s)) > -1) {
531// len = trx[i].matchedLength(); 530// len = trx[i].matchedLength();
532 531
533 // 532 //
534 // trx[i] has been matched, idx contains the start of the match 533 // trx[i] has been matched, idx contains the start of the match
535 // and len contains how long the match is. Extract the match. 534 // and len contains how long the match is. Extract the match.
536 // 535 //
537 QString sub = s.mid(idx, len); 536 QString sub = s.mid(idx, len);
538 537
539 // 538 //
540 // Now extract the digits only from the match, which will then 539 // Now extract the digits only from the match, which will then
541 // be converted to an int. 540 // be converted to an int.
542 // 541 //
543 if((idx = rrx[RXMAX-1].match(sub,0,&len)) > -1) { 542 if((idx = rrx[RXMAX-1].match(sub,0,&len)) > -1) {
544// if((idx = rrx[RXMAX-1].search(sub)) > -1) { 543// if((idx = rrx[RXMAX-1].search(sub)) > -1) {
545// len = rrx[RXMAX-1].matchedLength(); 544// len = rrx[RXMAX-1].matchedLength();
546 sub = sub.mid(idx, len); 545 sub = sub.mid(idx, len);
547 result = sub.toInt(); 546 result = sub.toInt();
548 if(result > 0) { 547 if(result > 0) {
549 tx = result; 548 tx = result;
550 break; 549 break;
551 } 550 }
552 } 551 }
553 } 552 }
554 } 553 }
555 554
556 if(rx == -1 && tx == -1) 555 if(rx == -1 && tx == -1)
557 result = i18n("Unknown speed"); 556 result = i18n("Unknown speed");
558 else if(tx == -1) 557 else if(tx == -1)
559 result.setNum(rx); 558 result.setNum(rx);
560 else if(rx == -1) // should not happen 559 else if(rx == -1) // should not happen
561 result.setNum(tx); 560 result.setNum(tx);
562 else 561 else
563 result.sprintf("%d/%d", rx, tx); 562 result.sprintf("%d/%d", rx, tx);
564 563
565 qDebug( "The parsed result is: %s", result.latin1()); 564 qDebug( "The parsed result is: %s", result.latin1());
566 565
567 return result; 566 return result;
568} 567}
569 568
570 569
571// Lock modem device. Returns 0 on success 1 if the modem is locked and -1 if 570// Lock modem device. Returns 0 on success 1 if the modem is locked and -1 if
572// a lock file can't be created ( permission problem ) 571// a lock file can't be created ( permission problem )
573int Modem::lockdevice() { 572int Modem::lockdevice() {
574 int fd; 573 int fd;
575 char newlock[80]=""; // safe 574 char newlock[80]=""; // safe
576 575
577 if(!PPPData::data()->modemLockFile()) { 576 if(!_pppdata->modemLockFile()) {
578 qDebug("The user doesn't want a lockfile."); 577 qDebug("The user doesn't want a lockfile.");
579 return 0; 578 return 0;
580 } 579 }
581 580
582 if (modem_is_locked) 581 if (modem_is_locked)
583 return 1; 582 return 1;
584 583
585 QString lockfile = LOCK_DIR"/LCK.."; 584 QString lockfile = LOCK_DIR"/LCK..";
586 lockfile += PPPData::data()->modemDevice().mid(5); // append everything after /dev/ 585 lockfile += _pppdata->modemDevice().mid(5); // append everything after /dev/
587 586
588 if(access(QFile::encodeName(lockfile), F_OK) == 0) { 587 if(access(QFile::encodeName(lockfile), F_OK) == 0) {
589// if ((fd = Requester::rq-> 588// if ((fd = Requester::rq->
590if ((fd = openLockfile(QFile::encodeName(lockfile), O_RDONLY)) >= 0) { 589if ((fd = openLockfile(QFile::encodeName(lockfile), O_RDONLY)) >= 0) {
591 // Mario: it's not necessary to read more than lets say 32 bytes. If 590 // Mario: it's not necessary to read more than lets say 32 bytes. If
592 // file has more than 32 bytes, skip the rest 591 // file has more than 32 bytes, skip the rest
593 char oldlock[33]; // safe 592 char oldlock[33]; // safe
594 int sz = read(fd, &oldlock, 32); 593 int sz = read(fd, &oldlock, 32);
595 close (fd); 594 close (fd);
596 if (sz <= 0) 595 if (sz <= 0)
597 return 1; 596 return 1;
598 oldlock[sz] = '\0'; 597 oldlock[sz] = '\0';
599 598
600 qDebug( "Device is locked by: %s", oldlock); 599 qDebug( "Device is locked by: %s", oldlock);
601 600
602 int oldpid; 601 int oldpid;
603 int match = sscanf(oldlock, "%d", &oldpid); 602 int match = sscanf(oldlock, "%d", &oldpid);
604 603
605 // found a pid in lockfile ? 604 // found a pid in lockfile ?
606 if (match < 1 || oldpid <= 0) 605 if (match < 1 || oldpid <= 0)
607 return 1; 606 return 1;
608 607
609 // check if process exists 608 // check if process exists
610 if (kill((pid_t)oldpid, 0) == 0 || errno != ESRCH) 609 if (kill((pid_t)oldpid, 0) == 0 || errno != ESRCH)
611 return 1; 610 return 1;
612 611
613 qDebug( "lockfile is stale" ); 612 qDebug( "lockfile is stale" );
614 } 613 }
615 } 614 }
616 615
617 fd = openLockfile(PPPData::data()->modemDevice(),O_WRONLY|O_TRUNC|O_CREAT); 616 fd = openLockfile(_pppdata->modemDevice(),O_WRONLY|O_TRUNC|O_CREAT);
618 if(fd >= 0) { 617 if(fd >= 0) {
619 sprintf(newlock,"%010d\n", getpid()); 618 sprintf(newlock,"%010d\n", getpid());
620 qDebug("Locking Device: %s", newlock); 619 qDebug("Locking Device: %s", newlock);
621 620
622 write(fd, newlock, strlen(newlock)); 621 write(fd, newlock, strlen(newlock));
623 close(fd); 622 close(fd);
624 modem_is_locked=true; 623 modem_is_locked=true;
625 624
626 return 0; 625 return 0;
627 } 626 }
628 627
629 return -1; 628 return -1;
630 629
631} 630}
632 631
633 632
634// UnLock modem device 633// UnLock modem device
635void Modem::unlockdevice() { 634void Modem::unlockdevice() {
636 if (modem_is_locked) { 635 if (modem_is_locked) {
637 qDebug( "UnLocking Modem Device" ); 636 qDebug( "UnLocking Modem Device" );
638 close(modemfd); 637 close(modemfd);
639 modemfd = -1; 638 modemfd = -1;
640 unlink(lockfile); 639 unlink(lockfile);
641 lockfile[0] = '\0'; 640 lockfile[0] = '\0';
642 modem_is_locked=false; 641 modem_is_locked=false;
643 } 642 }
644} 643}
645 644
646int Modem::openLockfile( QString lockfile, int flags) 645int Modem::openLockfile( QString lockfile, int flags)
647{ 646{
648 int fd; 647 int fd;
649 int mode; 648 int mode;
650 flags = O_RDONLY; 649 flags = O_RDONLY;
651 if(flags == O_WRONLY|O_TRUNC|O_CREAT) 650 if(flags == O_WRONLY|O_TRUNC|O_CREAT)
652 mode = 0644; 651 mode = 0644;
653 else 652 else
654 mode = 0; 653 mode = 0;
655 654
656 lockfile = LOCK_DIR; 655 lockfile = LOCK_DIR;
657 lockfile += "/LCK.."; 656 lockfile += "/LCK..";
658 lockfile += device.right( device.length() - device.findRev("/") -1 ); 657 lockfile += device.right( device.length() - device.findRev("/") -1 );
659 qDebug("lockfile >%s<",lockfile.latin1()); 658 qDebug("lockfile >%s<",lockfile.latin1());
660 // TODO: 659 // TODO:
661 // struct stat st; 660 // struct stat st;
662 // if(stat(lockfile.data(), &st) == -1) { 661 // if(stat(lockfile.data(), &st) == -1) {
663 // if(errno == EBADF) 662 // if(errno == EBADF)
664 // return -1; 663 // return -1;
665 // } else { 664 // } else {
@@ -943,80 +942,80 @@ bool Modem::killpppd() {
943 } 942 }
944 return true; 943 return true;
945} 944}
946 945
947 946
948void Modem::parseargs(char* buf, char** args) { 947void Modem::parseargs(char* buf, char** args) {
949 int nargs = 0; 948 int nargs = 0;
950 int quotes; 949 int quotes;
951 950
952 while(nargs < MaxArgs-1 && *buf != '\0') { 951 while(nargs < MaxArgs-1 && *buf != '\0') {
953 952
954 quotes = 0; 953 quotes = 0;
955 954
956 // Strip whitespace. Use nulls, so that the previous argument is 955 // Strip whitespace. Use nulls, so that the previous argument is
957 // terminated automatically. 956 // terminated automatically.
958 957
959 while ((*buf == ' ' ) || (*buf == '\t' ) || (*buf == '\n' ) ) 958 while ((*buf == ' ' ) || (*buf == '\t' ) || (*buf == '\n' ) )
960 *buf++ = '\0'; 959 *buf++ = '\0';
961 960
962 // detect begin of quoted argument 961 // detect begin of quoted argument
963 if (*buf == '"' || *buf == '\'') { 962 if (*buf == '"' || *buf == '\'') {
964 quotes = *buf; 963 quotes = *buf;
965 *buf++ = '\0'; 964 *buf++ = '\0';
966 } 965 }
967 966
968 // save the argument 967 // save the argument
969 if(*buf != '\0') { 968 if(*buf != '\0') {
970 *args++ = buf; 969 *args++ = buf;
971 nargs++; 970 nargs++;
972 } 971 }
973 972
974 if (!quotes) 973 if (!quotes)
975 while ((*buf != '\0') && (*buf != '\n') && 974 while ((*buf != '\0') && (*buf != '\n') &&
976 (*buf != '\t') && (*buf != ' ')) 975 (*buf != '\t') && (*buf != ' '))
977 buf++; 976 buf++;
978 else { 977 else {
979 while ((*buf != '\0') && (*buf != quotes)) 978 while ((*buf != '\0') && (*buf != quotes))
980 buf++; 979 buf++;
981 *buf++ = '\0'; 980 *buf++ = '\0';
982 } 981 }
983 } 982 }
984 983
985 *args = 0L; 984 *args = 0L;
986} 985}
987 986
988bool Modem::execPPPDaemon(const QString & arguments) 987bool Modem::execPPPDaemon(const QString & arguments)
989{ 988{
990 if(execpppd(arguments)==0) { 989 if(execpppd(arguments)==0) {
991 PPPData::data()->setpppdRunning(true); 990 _pppdata->setpppdRunning(true);
992 return true; 991 return true;
993 } else 992 } else
994 return false; 993 return false;
995} 994}
996 995
997void Modem::killPPPDaemon() 996void Modem::killPPPDaemon()
998{ 997{
999 PPPData::data()->setpppdRunning(false); 998 _pppdata->setpppdRunning(false);
1000 killpppd(); 999 killpppd();
1001} 1000}
1002 1001
1003int Modem::pppdExitStatus() 1002int Modem::pppdExitStatus()
1004{ 1003{
1005 return _pppdExitStatus; 1004 return _pppdExitStatus;
1006} 1005}
1007 1006
1008int Modem::openResolv(int flags) 1007int Modem::openResolv(int flags)
1009{ 1008{
1010 int fd; 1009 int fd;
1011 if ((fd = open(_PATH_RESCONF, flags)) == -1) { 1010 if ((fd = open(_PATH_RESCONF, flags)) == -1) {
1012 qDebug("error opening resolv.conf!"); 1011 qDebug("error opening resolv.conf!");
1013 fd = open(DEVNULL, O_RDONLY); 1012 fd = open(DEVNULL, O_RDONLY);
1014 } 1013 }
1015 return fd; 1014 return fd;
1016} 1015}
1017 1016
1018bool Modem::setHostname(const QString & name) 1017bool Modem::setHostname(const QString & name)
1019{ 1018{
1020 return sethostname(name, name.length()) == 0; 1019 return sethostname(name, name.length()) == 0;
1021} 1020}
1022 1021