7 #include <exec/types.h>
8 #include <exec/resident.h>
10 #include <exec/ports.h>
11 #include <exec/errors.h>
12 #include <exec/lists.h>
16 #include <aros/libcall.h>
17 #include <aros/symbolsets.h>
20 #include <devices/sana2.h>
21 #include <devices/sana2specialstats.h>
22 #include <devices/newstyle.h>
23 #include <devices/timer.h>
24 #include <devices/serial.h>
25 #include <devices/timer.h>
27 #include <utility/utility.h>
28 #include <utility/tagitem.h>
29 #include <utility/hooks.h>
31 #include <proto/exec.h>
32 #include <proto/dos.h>
34 #include <proto/oop.h>
36 #include <dos/dostags.h>
37 #define SYS_Error SYS_Output
39 #include <proto/timer.h>
40 #include <proto/utility.h>
44 #include <aros/debug.h>
53 #include <devices/serial.h>
54 #include <devices/timer.h>
61 #define PREFSFILE "ENV:MobileBroadband.prefs"
63 #define INTERFACEFILE "ENV:sys/net/interfaces"
65 #define INTERFACEFILE "ENV:AROSTCP/db/interfaces"
71 TEXT arostcppath
[256];
73 struct TagItem tags
[] =
75 { SYS_Input
, (IPTR
)NULL
},
76 { SYS_Output
, (IPTR
)NULL
},
77 { SYS_Error
, (IPTR
)NULL
},
78 { SYS_Asynch
, (IPTR
)TRUE
},
83 GetVar( "SYS/Packages/AROSTCP" , arostcppath
, 256 , LV_VAR
);
84 AddPart(arostcppath
, "C", 256);
85 AddPart(arostcppath
, "AROSTCP", 256);
87 bug("Start AROSTCP: %s\n",arostcppath
);
88 SystemTagList(arostcppath
, tags
);
90 /* Check if startup successful */
92 while (!FindTask("bsdsocket.library"))
94 if (trycount
> 9) return FALSE
;
102 BOOL
ReadConfig(struct Conf
*c
){
103 TEXT linebuff
[1024],*tok
;
105 struct at_command
*atc
;
108 strcpy( c
->modemmodel
, "Unknow");
109 strcpy( c
->username
, "DummyName");
110 strcpy( c
->password
, "DummyName");
111 strcpy( c
->DeviceName
, "usbmodem.device");
112 c
->InterfaceName
[0] = 0;
114 c
->SerUnitNum
= -1; // default: test all units
115 c
->CommandTimeOut
= 10; // default timeout 10 sec.
117 while( atc
= (struct at_command
*)RemHead( &c
->atcl
) ){
118 FreeMem( atc
, sizeof(struct at_command
) );
121 bug("Read config file:%s\n",PREFSFILE
);
122 if(ConfigFile
= Open( PREFSFILE
,MODE_OLDFILE
)){
123 while(FGets(ConfigFile
, linebuff
, STRSIZE
)){
124 if( ( linebuff
[0] == '#' ) | ( linebuff
[0] == ';' ) ) /* Skip comment lines */
127 // bug("line:%s:\n",linebuff);
129 if( tok
= strtok( &linebuff
[0]," \t\n") ){
131 if( strcasecmp("DEVICE",tok
) == 0 ){
132 if( tok
= strtok( NULL
, " \n" ) ) strcpy( c
->DeviceName
, tok
);
133 }else if( strcasecmp("UNIT",tok
) == 0 ){
134 if( tok
= strtok( NULL
, " \n" ) ) c
->SerUnitNum
= atoi( tok
);
135 }else if( strcasecmp("TIMEOUT",tok
) == 0 ){
136 if( tok
= strtok( NULL
, " \n" ) ) c
->CommandTimeOut
= atoi( tok
);
137 }else if( strcasecmp("USERNAME",tok
) == 0 ){
138 if( tok
= strtok( NULL
, " \n" ) ) strcpy( c
->username
, tok
);
139 }else if( strcasecmp("PASSWORD",tok
) == 0 ){
140 if( tok
= strtok( NULL
, " \n" ) ) strcpy( c
->password
, tok
);
143 else if( strcasecmp("SEND",tok
) == 0 ){
144 if( tok
= strtok( NULL
, "\n" ) ){
145 if(atc
= AllocMem(sizeof(struct at_command
), MEMF_CLEAR
| MEMF_PUBLIC
)){
146 strcpy( atc
->str
, tok
);
147 AddTail( &c
->atcl
, (struct Node
*)atc
);
155 bug("ModemManager:Config file missing !!!!\n");
159 bug("Read config file:%s\n",INTERFACEFILE
);
160 if(ConfigFile
= Open( INTERFACEFILE
,MODE_OLDFILE
)){
161 while(FGets(ConfigFile
, linebuff
, STRSIZE
)){
162 if( ( linebuff
[0] == '#' ) | ( linebuff
[0] == ';' ) ) /* Skip comment lines */
164 if( strcasestr( linebuff
, "ppp.device" ) != NULL
){
165 if( tok
= strtok( linebuff
, " " ) ){ strcpy( c
->InterfaceName
, tok
);
172 bug("ModemManager:Config file missing !!!!\n");
176 if( c
->InterfaceName
[0] == 0 ){
177 bug("ModemManager:No PPP interface defined !!!!\n");
182 bug(" Interface %s\n",c
->InterfaceName
);
183 bug(" Device %s\n",c
->DeviceName
);
184 bug(" Unit %d\n",c
->SerUnitNum
);
185 bug(" username %s\n",c
->username
);
186 bug(" password %s\n",c
->password
);
187 bug(" TimeOut %d\n",c
->CommandTimeOut
);
189 ForeachNode(&c
->atcl
,atc
){
190 bug(" Init%d \"%s\"\n",++i
,atc
->str
);
197 BOOL
TestModem(struct EasySerial
*s
,struct Conf
*c
){
202 UBYTE delim
[] = ": ,\n\r";
204 if( buf
= AllocMem(STRSIZE
,MEMF_CLEAR
|MEMF_PUBLIC
)){
209 strcpy( c
->modemmodel
, "Unknow" );
213 DoStr( s
, "\r\r\r" );
218 if( ! GetResponse(s
,buf
,STRSIZE
,5)){
220 result
= FALSE
; break;
225 DoStr( s
, "ATE 0\r" );
226 if( ! GetResponse(s
,buf
,STRSIZE
,5)){
228 result
= FALSE
; break;
233 DoStr( s
, "AT+GMM\r" );
234 if( ! GetResponse(s
,buf
,STRSIZE
,5)){
235 bug("AT+GMM FAIL\n");
237 else if( tok
= strtok( buf
, "\n\r" ) ){
238 strcpy( c
->modemmodel
, tok
);
241 // ask signal strength
243 DoStr( s
, "AT+CSQ\r" );
244 if( ! GetResponse(s
,buf
,STRSIZE
,5)){
245 bug("AT+CSQ FAIL\n");
247 else if( tok
= strtok( buf
, delim
) ){
248 if( tok
= strtok( NULL
, delim
) ){
249 if( isdigit( tok
[0] ) ) c
->signal
= atoi( tok
);
253 // Network Registration Status
255 DoStr( s
, "AT+COPS?\r" );
256 if( ! GetResponse(s
,buf
,STRSIZE
,5)){
257 bug("AT+COPS? FAIL\n");
259 if( tok
= strtok( buf
, delim
) ){
260 if( tok
= strtok( NULL
, delim
) ){
261 if( tok
= strtok( NULL
, delim
) ){
262 if( tok
= strtok( NULL
, delim
) ){
263 if( tok
= strtok( NULL
, delim
) ){
264 if( isdigit( tok
[0] ) ) c
->AccessType
= atoi( tok
);
268 bug("model=%s\n",c
->modemmodel
);
269 bug("AccessType=%d\n",c
->AccessType
);
270 bug("signal=%d\n",c
->signal
);
274 FreeMem( buf
, STRSIZE
);
280 BOOL
DialUp(struct EasySerial
*s
,struct Conf
*c
){
282 struct at_command
*atc
=NULL
;
283 UBYTE buf
[PPP_MAXARGLEN
];
286 ForeachNode(&c
->atcl
,atc
){
288 bug("SEND \"%s\"\n",atc
->str
);
291 if( GetResponse( s
, buf
, PPP_MAXARGLEN
, c
->CommandTimeOut
)){
292 if( strcasestr(buf
,"CONNECT") ) return TRUE
;
305 void SetTimer(struct EasyTimer
* t
,const ULONG s
){
309 AbortIO((struct IORequest
*)t
->TimeReq
);
310 WaitIO((struct IORequest
*)t
->TimeReq
);
311 while(GetMsg(t
->TimeMsg
));
315 t
->TimeReq
->tr_time
.tv_secs
= s
;
316 t
->TimeReq
->tr_time
.tv_micro
= 0;
317 ((struct IORequest
*)t
->TimeReq
)->io_Command
= TR_ADDREQUEST
;
318 SendIO( (struct IORequest
*)t
->TimeReq
);
319 //bug("setTimer ok\n");
325 void CloseTimer(struct EasyTimer
* t
){
327 //bug("closeTimer\n");
329 AbortIO((struct IORequest
*)t
->TimeReq
);
330 WaitIO((struct IORequest
*)t
->TimeReq
);
331 while(GetMsg(t
->TimeMsg
));
332 CloseDevice((struct IORequest
*)t
->TimeReq
);
333 DeleteIORequest(t
->TimeReq
);
335 if(t
->TimeMsg
) DeleteMsgPort(t
->TimeMsg
);
336 FreeMem( t
, sizeof(struct EasyTimer
) );
337 //bug("closeTimer ok\n");
341 struct EasyTimer
* OpenTimer(){
343 //bug("OpenTimer\n");
344 if( t
= AllocMem( sizeof(struct EasyTimer
),MEMF_CLEAR
|MEMF_PUBLIC
) ){
345 if( t
->TimeMsg
= CreateMsgPort() ){
346 if( t
->TimeReq
= CreateIORequest((struct MsgPort
*)t
->TimeMsg
, sizeof(struct timerequest
))){
347 if(!OpenDevice("timer.device", UNIT_VBLANK
,(struct IORequest
*)t
->TimeReq
, 0)){
348 t
->TimeReq
->tr_time
.tv_secs
= 0;
349 t
->TimeReq
->tr_time
.tv_micro
= 1;
350 ((struct IORequest
*)t
->TimeReq
)->io_Command
= TR_ADDREQUEST
;
351 DoIO( (struct IORequest
*)t
->TimeReq
);
353 //bug("OpenTimer ok!\n");
359 //bug("OpenTimer FAIL!\n");
364 VOID
DoStr(struct EasySerial
*s
,const STRPTR str
){
365 DoBYTES(s
,str
,strlen((char*) str
));
368 void DoBYTES(struct EasySerial
*s
, BYTE
*p
,ULONG len
){
371 if( ! s
->Ok
) return;
373 WaitIO((struct IORequest
*)s
->SerTx
);
375 s
->SerTx
->IOSer
.io_Length
= len
;
376 s
->SerTx
->IOSer
.io_Data
= p
;
377 s
->SerTx
->IOSer
.io_Command
= CMD_WRITE
;
378 DoIO((struct IORequest
*)s
->SerTx
);
381 void SendBYTES(struct EasySerial
*s
, BYTE
*p
, ULONG len
){
384 if( ! s
->Ok
) return;
386 WaitIO((struct IORequest
*)s
->SerTx
);
388 s
->SerTx
->IOSer
.io_Length
= len
;
389 s
->SerTx
->IOSer
.io_Data
= p
;
390 s
->SerTx
->IOSer
.io_Command
= CMD_WRITE
;
391 SendIO((struct IORequest
*)s
->SerTx
);
394 void DrainSerial(struct EasySerial
*s
){
397 if( ! s
->Ok
) return;
402 AbortIO((struct IORequest
*)s
->SerRx
);
403 WaitIO((struct IORequest
*)s
->SerRx
);
404 while(GetMsg(s
->RxPort
));
408 for(;;){ // Read crap out of serial device.
410 s
->SerRx
->IOSer
.io_Command
= SDCMD_QUERY
;
411 DoIO((struct IORequest
*)s
->SerRx
);
413 if( s
->SerRx
->IOSer
.io_Error
){
414 bug("DrainSerial(): OOOOPS lost device!\n");
419 if( s
->SerRx
->IOSer
.io_Actual
== 0 ) break;
421 s
->SerRx
->IOSer
.io_Command
= CMD_READ
;
422 s
->SerRx
->IOSer
.io_Data
= s
->RxBuff
;
423 s
->SerRx
->IOSer
.io_Length
= 1;
424 DoIO((struct IORequest
*)s
->SerRx
);
425 if( s
->SerRx
->IOSer
.io_Error
){
426 bug("DrainSerial(): CMD_READ error!");
428 // bug("%s",s->RxBuff);
431 //bug("Drain end\n");
436 AbortIO((struct IORequest
*)s
->SerTx
);
437 WaitIO((struct IORequest
*)s
->SerTx
);
438 while(GetMsg(s
->TxPort
));
444 BOOL
GetResponse(struct EasySerial
*s
,UBYTE
*Buffer
,ULONG maxbuffer
,LONG timeout
){
454 if( ! s
) return FALSE
;
455 if( ! s
->Ok
) return FALSE
;
457 bug("GetResponse:\n");
459 if( t
= OpenTimer() ){
463 QueueSerRequest( s
, 1 );
471 sigset
= (1L<< s
->RxPort
->mp_SigBit
) |
472 (1L<< t
->TimeMsg
->mp_SigBit
);
476 if( GetMsg( s
->RxPort
) ){
478 if( len
<= ( maxbuffer
-1 ) ){
479 Buffer
[len
] = s
->RxBuff
[0];
481 bug( "%s" , c
[0] == '\r' ? "<\\r>" :
482 c
[0] == '\n' ? "<\\n>" :
485 if( Buffer
[len
] == '\r' ) Buffer
[len
] = '\n';
487 if( strcasestr(Buffer
,"OK\n") != NULL
|
488 strcasestr(Buffer
,"CONNECT") != NULL
493 else if( strcasestr(Buffer
,"NO CARRIER\n") != NULL
|
494 strcasestr(Buffer
,"ERROR\n") != NULL
|
495 strcasestr(Buffer
,"BUSY\n") != NULL
503 QueueSerRequest( s
, 1 );
510 if( GetMsg( t
->TimeMsg
) ){
511 bug( "GetResponse TimeOut ERROR\n");
521 AbortIO((struct IORequest
*)s
->SerRx
);
522 WaitIO((struct IORequest
*)s
->SerRx
);
523 while(GetMsg(s
->RxPort
));
525 bug("GetResponse end\n");
531 struct EasySerial
*OpenSerial(BYTE
*name
,ULONG unit
){
532 struct EasySerial
*s
= NULL
;
536 if( ! (s
= AllocMem( sizeof(struct EasySerial
),MEMF_CLEAR
|MEMF_PUBLIC
))) break;
537 if( ! (s
->TxBuff
= AllocMem( SERIAL_BUFSIZE
,MEMF_CLEAR
|MEMF_PUBLIC
))) break;
538 if( ! (s
->RxBuff
= AllocMem( SERIAL_BUFSIZE
,MEMF_CLEAR
|MEMF_PUBLIC
))) break;
539 if( ! (s
->TxPort
= CreateMsgPort())) break;
540 if( ! (s
->SerTx
= CreateIORequest(s
->TxPort
,sizeof(struct IOExtSer
)))) break;
542 if( OpenDevice( name
, unit
, (struct IORequest
*)s
->SerTx
,0)){
543 DeleteIORequest(s
->SerTx
);
548 bug("OpenSerial:OpenDevice: \"%s\" unit %d\n",name
,unit
);
550 bug("Test CMD_WRITE\n");
551 s
->SerTx
->IOSer
.io_Length
= 0;
552 s
->SerTx
->IOSer
.io_Data
= " ";
553 s
->SerTx
->IOSer
.io_Command
= CMD_WRITE
;
554 if( DoIO((struct IORequest
*)s
->SerTx
)){
558 if( ! (s
->RxPort
= CreateMsgPort()) ) break;
559 if( ! (s
->SerRx
= CreateIORequest(s
->RxPort
,sizeof(struct IOExtSer
)))) break;
561 bug("Test SDCMD_QUERY\n");
562 s
->SerRx
->IOSer
.io_Device
= s
->SerTx
->IOSer
.io_Device
;
563 s
->SerRx
->IOSer
.io_Unit
= s
->SerTx
->IOSer
.io_Unit
;
564 s
->SerRx
->IOSer
.io_Command
= SDCMD_QUERY
;
565 if( DoIO((struct IORequest
*)s
->SerRx
)){
569 D(bug("OpenSerial OK\n"));
581 VOID
_CloseSerial(struct EasySerial
*s
){
587 AbortIO((struct IORequest
*)s
->SerRx
);
588 WaitIO((struct IORequest
*)s
->SerRx
);
589 while(GetMsg(s
->RxPort
));
593 AbortIO((struct IORequest
*)s
->SerTx
);
594 WaitIO((struct IORequest
*)s
->SerTx
);
595 while(GetMsg(s
->TxPort
));
598 if(s
->SerTx
) CloseDevice((struct IORequest
*)s
->SerTx
);
600 if(s
->SerTx
) DeleteIORequest(s
->SerTx
);
601 if(s
->TxPort
) DeleteMsgPort(s
->TxPort
);
603 if(s
->SerRx
) DeleteIORequest(s
->SerRx
);
604 if(s
->RxPort
) DeleteMsgPort(s
->RxPort
);
606 if( s
->TxBuff
) FreeMem( s
->TxBuff
, SERIAL_BUFSIZE
);
607 if( s
->RxBuff
) FreeMem( s
->RxBuff
, SERIAL_BUFSIZE
);
608 FreeMem( s
, sizeof(struct EasySerial
) );
613 VOID
QueueSerRequest(struct EasySerial
*s
, LONG maxlength
){
616 if( ! s
->Ok
) return;
617 if( maxlength
< 1 ) return;
619 if( maxlength
> SERIAL_BUFSIZE
) maxlength
= SERIAL_BUFSIZE
;
620 s
->SerRx
->IOSer
.io_Command
= SDCMD_QUERY
;
621 DoIO((struct IORequest
*)s
->SerRx
);
623 if( s
->SerRx
->IOSer
.io_Error
){
624 bug("QueueSerRequest() OOOOPS lost device!\n");
629 s
->SerRx
->IOSer
.io_Command
= CMD_READ
;
630 s
->SerRx
->IOSer
.io_Data
= s
->RxBuff
;
631 s
->SerRx
->IOSer
.io_Length
= s
->SerRx
->IOSer
.io_Actual
;
633 if( s
->SerRx
->IOSer
.io_Length
> maxlength
)
634 s
->SerRx
->IOSer
.io_Length
= maxlength
;
635 /* If the number of bytes available is zero, queue a request
637 if(!s
->SerRx
->IOSer
.io_Length
)
638 s
->SerRx
->IOSer
.io_Length
= 1;
640 SendIO((struct IORequest
*)s
->SerRx
);
641 if( s
->SerRx
->IOSer
.io_Error
){
642 bug("QueueSerRequest() SendIO -> CMD_READ error!");