Register nameservers dynamically instead of writing them to disk. It is
[AROS.git] / workbench / devs / networks / ppp / ModemManager / ModemManager.c
blob43c78fd6fabfd822fe2311e6375b2291573865d2
1 /*
2 * $Id$
3 */
5 #define DEBUG 1
6 #include <exec/types.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <math.h>
11 #include <dos/dos.h>
12 #include <intuition/gadgetclass.h>
13 #include <intuition/icclass.h>
14 #include <proto/exec.h>
15 #include <proto/intuition.h>
16 #include <proto/muimaster.h>
17 #include <proto/dos.h>
18 #include <proto/graphics.h>
19 #include <clib/alib_protos.h>
20 #include <proto/muimaster.h>
21 #include <proto/miami.h>
22 #include <utility/hooks.h>
23 #include <libraries/mui.h>
24 #include <aros/debug.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
28 #include "ppp.h"
29 #include "misc.h"
31 #define SimpleText(text) TextObject, MUIA_Text_Contents, (IPTR) text, End
33 #define TIMERVALUE 5
34 #define STRSIZE 100
36 #define STATE_UNPLUGGED 0
37 #define STATE_PLUGGED 1
38 #define STATE_OPENDEV 2
39 #define STATE_NETWORK 3
40 #define STATE_CLOSEDEV 4
42 Object *application=0,*window,*DisConBut,*ConBut;
43 Object *ModemName,*AccessType;
44 Object *IN_Info,*OUT_Info;
46 struct EasyBitmap *SignalBM=0;
48 UBYTE *PortName = "ModemManager";
49 const TEXT version_string[] = "$VER: ModemManager 1.2 (1.3.2012)";
51 ULONG exPhase,exstate;
52 BOOL exSer;
54 struct EasyBitmap{
55 struct BitMap *bm;
56 struct RastPort *rp;
57 Object *MUIbitmap;
60 struct EasyBitmap *MakeBitmap(ULONG x,ULONG y,Object *MUIwindow,Object *MUIbitmap){
61 struct EasyBitmap *ebm=NULL;
62 struct Window *tw=NULL;
64 if( MUIwindow && MUIbitmap ){
65 if( ebm = AllocMem( sizeof(struct EasyBitmap),MEMF_CLEAR|MEMF_PUBLIC)){
67 tw=(struct Window *)XGET( MUIwindow , MUIA_Window_Window );
69 ebm->bm = AllocBitMap(x,y,
70 GetBitMapAttr(tw->RPort->BitMap, BMA_DEPTH),
71 BMF_CLEAR,
72 tw->RPort->BitMap);
73 ebm->rp = CreateRastPort();
74 ebm->rp->BitMap = ebm->bm;
75 ebm->MUIbitmap = MUIbitmap;
76 set( MUIbitmap , MUIA_FixWidth, x );
77 set( MUIbitmap , MUIA_FixHeight, y );
78 set( MUIbitmap , MUIA_Bitmap_Width, x );
79 set( MUIbitmap , MUIA_Bitmap_Height, y );
80 set( MUIbitmap , MUIA_Bitmap_Transparent, 0 );
81 set( MUIbitmap , MUIA_Bitmap_Bitmap, ebm->bm );
82 DoMethod( MUIbitmap , MUIM_Draw);
85 return ebm;
88 void _CloseBitmap(struct EasyBitmap *ebm){
89 if(ebm){
90 FreeRastPort(ebm->rp);
91 FreeBitMap(ebm->bm);
92 FreeMem(ebm,sizeof(struct EasyBitmap));
95 #define CloseBitmap( ebm ) do{_CloseBitmap(ebm);ebm=NULL;}while(0)
98 struct EasyGraph{
99 struct EasyBitmap *ebm;
100 ULONG Xsize,Ysize;
101 FLOAT Max;
102 FLOAT *value;
105 void _CloseGraph(struct EasyGraph *egr){
106 if(egr){
107 CloseBitmap( egr->ebm );
108 if( egr->value ) FreeMem( egr->value , sizeof(FLOAT) * egr->Xsize );
109 FreeMem(egr,sizeof(struct EasyGraph));
112 #define CloseGraph( x ) do{_CloseGraph(x);x=NULL;}while(0)
114 struct EasyGraph *MakeGraph(ULONG x,ULONG y,Object *MUIwindow,Object *MUIbitmap){
115 struct EasyGraph *egr=NULL;
116 if( MUIwindow && MUIbitmap ){
117 if( egr = AllocMem( sizeof(struct EasyGraph),MEMF_CLEAR|MEMF_PUBLIC)){
118 egr->Xsize = x;
119 egr->Ysize = y;
120 egr->Max = 0.0;
121 if( egr->value = AllocMem( sizeof(FLOAT) * x ,MEMF_CLEAR|MEMF_PUBLIC)){
122 if( egr->ebm = MakeBitmap(x,y,MUIwindow,MUIbitmap) ){
123 SetRast(egr->ebm->rp,0);
124 DoMethod( egr->ebm->MUIbitmap , MUIM_Draw );
125 }else CloseGraph(egr);
129 return egr;
132 void UpdateGraph(struct EasyGraph *egr,FLOAT value){
133 LONG i;
134 LONG h;
135 if(egr){
137 for( i= egr->Xsize-1 ; i > 0 ; i-- ){
138 egr->value[i] = egr->value[i-1];
141 egr->value[0] = value;
142 if( value > egr->Max ) egr->Max = value;
144 if( egr->ebm ){
146 SetRast(egr->ebm->rp,0);
147 SetAPen(egr->ebm->rp,1);
148 Move(egr->ebm->rp , 0 , egr->Ysize-1 );
149 Draw(egr->ebm->rp , egr->Xsize-1 , egr->Ysize-1 );
150 SetAPen(egr->ebm->rp,2);
152 if( egr->Max != 0.0 ){
153 for( i=0 ; i < egr->Xsize ; i++ ){
154 h = (LONG)( egr->value[i] / egr->Max * (FLOAT)( egr->Ysize - 2 ) );
155 if(i==0) Move(egr->ebm->rp , i , (egr->Ysize-2) - h );
156 else Draw(egr->ebm->rp , i , (egr->Ysize-2) - h );
158 }else{
159 Move(egr->ebm->rp , 0 , egr->Ysize-2 );
160 Draw(egr->ebm->rp , egr->Xsize-1 , egr->Ysize-2 );
163 DoMethod( egr->ebm->MUIbitmap , MUIM_Draw );
170 BOOL SafePutToPort(struct PPPcontrolMsg *message, STRPTR portname)
172 struct MsgPort *port;
173 Forbid();
174 port = FindPort(portname);
175 if (port){
176 struct Message *M;
177 ForeachNode(&port->mp_MsgList,M){
178 if( (APTR)message == (APTR)M ){
179 // bug("SafePutToPort: message is already here !\n");
180 Permit();
181 return FALSE;
184 PutMsg(port,(struct Message *)message);
186 Permit();
187 return(port ? TRUE : FALSE);
190 BOOL SendCtrlMsg(ULONG command,IPTR Arg,struct Conf *c){
191 struct PPPcontrolMsg *CtrlMsg=0;
192 struct MsgPort *CtrlPort=0;
193 BOOL succes=FALSE;
194 if( CtrlPort = CreatePort(0,0) ){
195 if( CtrlMsg = AllocMem(sizeof(struct PPPcontrolMsg),MEMF_PUBLIC | MEMF_CLEAR)){
197 CtrlMsg->Command = command;
198 CtrlMsg->Arg = Arg;
200 CtrlMsg->DeviceName = c->DeviceName;
201 CtrlMsg->UnitNum = c->SerUnitNum;
202 CtrlMsg->username = c->username;
203 CtrlMsg->password = c->password;
205 CtrlMsg->Msg.mn_Node.ln_Type = NT_MESSAGE;
206 CtrlMsg->Msg.mn_Length = sizeof(struct PPPcontrolMsg);
207 CtrlMsg->Msg.mn_ReplyPort = CtrlPort;
208 if( SafePutToPort(CtrlMsg, "ppp-control") ){
209 WaitPort(CtrlPort);
210 GetMsg(CtrlPort);
211 succes=TRUE;
213 FreeMem(CtrlMsg,sizeof(struct PPPcontrolMsg));
215 DeletePort(CtrlPort);
217 return succes;
220 BOOL SendRequest(void){
221 struct PPPcontrolMsg *CtrlMsg;
222 BOOL result=FALSE;
224 if( CtrlMsg = AllocMem(sizeof(struct PPPcontrolMsg),MEMF_PUBLIC | MEMF_CLEAR)){
225 bug("ModemManager:send info request\n");
226 CtrlMsg->Command = PPP_CTRL_INFO_REQUEST;
227 CtrlMsg->Arg = (IPTR)PortName;
228 CtrlMsg->Msg.mn_Node.ln_Type = NT_MESSAGE;
229 CtrlMsg->Msg.mn_Length = sizeof(struct PPPcontrolMsg);
230 CtrlMsg->Msg.mn_ReplyPort = 0;
231 if( SafePutToPort(CtrlMsg, "ppp-control") ) result=TRUE;
232 FreeMem(CtrlMsg , sizeof(struct PPPcontrolMsg));
234 return result;
238 void speedstr(BYTE *buf,BYTE *label,LONG s){
239 float speed= (float)s;
240 BYTE e = 'b';
241 if( speed > 1000.0 ){
242 e = 'k';
243 speed /= 1000.0;
245 if( speed > 1000.0 ){
246 e = 'M';
247 speed /= 1000.0;
249 snprintf( buf, STRSIZE , speed == (ULONG)speed ? "%s %.0f %c/s" :"%s %.2f %c/s" , label , speed , e );
252 // draw triangular signal meter and show modem name etc..
253 void UpdateModemInfo(struct EasyBitmap *ebm,struct Conf *c)
255 ULONG i;
256 ULONG sig;
257 if( ebm ){
259 SetRast(ebm->rp,0);
260 if( c->signal >= 0 && c->signal != 99 ){
262 sig = c->signal;
263 //sig = (ULONG)( log( (double)c->signal ) / log( 31.0 )*31.0 );
265 SetAPen(ebm->rp,2);
266 for(i=0;i<32;i++){
267 if( sig >= i){
268 Move(ebm->rp, i , 15 );
269 Draw(ebm->rp, i , 15-i/2 );
272 SetAPen(ebm->rp,1);
273 Move(ebm->rp,0,15);
274 Draw(ebm->rp,0,14);
275 Draw(ebm->rp,29,0);
276 Draw(ebm->rp,31,0);
277 Draw(ebm->rp,31,15);
278 Draw(ebm->rp,0,15);
280 DoMethod( ebm->MUIbitmap , MUIM_Draw );
283 <AcT> Network access type
284 0 GSM
285 1 Compact GSM
286 2 UTRAN
287 3 GSM with EGPRS
288 4 UTRAN with HSDPA
289 5 UTRAN with HSUPA
290 6 UTRAN with HSDPA and HSUPA ???
293 set( AccessType , MUIA_Text_Contents,
294 c->AccessType == -1 ? "" :
295 c->AccessType == 0 ? "GSM" :
296 c->AccessType == 1 ? "GPRS" :
297 c->AccessType == 2 ? "3G" :
298 c->AccessType == 3 ? "EDGE" :
299 c->AccessType == 4 ? "3.5G" :
300 c->AccessType == 5 ? "3.75G" :
301 c->AccessType == 6 ? "3.8G" :
302 "?G"
304 set( ModemName , MUIA_Text_Contents, c->modemmodel);
308 static void DisconnectFunc(struct Hook *hook, Object *app, APTR *arg)
311 struct Conf *c = *arg;
312 SendCtrlMsg( PPP_CTRL_SETPHASE , PPP_PHASE_TERMINATE , c );
316 void FindModemUnit(struct Conf *c){
317 struct EasySerial *Ser=0;
318 int result = -1;
319 int i;
321 if( c->SerUnitNum >= 0 ){
322 return;
325 for (i = 0; i < 100; i++)
327 if( Ser = OpenSerial( c->DeviceName ,i ) ){
328 if( TestModem( Ser , c ) ){
329 result = i;
330 DrainSerial( Ser );
331 CloseSerial( Ser );
332 break;
334 DrainSerial( Ser );
335 CloseSerial( Ser );
336 } else break;
338 c->SerUnitNum = result;
342 static void ConnectFunc(struct Hook *hook, Object *app, APTR *arg)
344 struct Conf *c = *arg;
345 struct EasySerial *Ser=0;
347 if( c->state == STATE_PLUGGED ){
349 // is interfacename configured?
350 if( c->InterfaceName[0] == 0 ){
351 set( IN_Info , MUIA_Text_Contents, (IPTR)"ppp Interface not configured!");
352 return;
355 // check if arostcp is running
356 if( FindTask("bsdsocket.library") == NULL ){
357 set( IN_Info , MUIA_Text_Contents, (IPTR)"AROSTCP is not running!");
358 if( StartStack() ){
359 set( OUT_Info , MUIA_Text_Contents, (IPTR)"Starting AROSTCP OK");
360 }else{
361 set( OUT_Info , MUIA_Text_Contents, (IPTR)"Starting AROSTCP FAIL!");
362 return;
366 // check ppp.device
367 if( FindPort("ppp-control") ){
368 // send info request to ppp.device
369 SendRequest();
370 }else{
371 set( OUT_Info, MUIA_Text_Contents, (IPTR)"Can't find ppp.device!");
372 set( IN_Info , MUIA_Text_Contents, (IPTR)"Not configured?");
373 return;
376 if( c->SerUnitNum >=0 ){
377 set( IN_Info , MUIA_Text_Contents, (IPTR)"Open Serial Device...");
378 set( OUT_Info , MUIA_Text_Contents, (IPTR)"");
379 if( Ser = OpenSerial( c->DeviceName ,c->SerUnitNum ) ){
380 set( IN_Info , MUIA_Text_Contents, (IPTR)"Modem Test...");
381 //set( OUT_Info , MUIA_Text_Contents, (IPTR)"");
382 if( TestModem( Ser , c ) ){
383 UpdateModemInfo( SignalBM , c );
384 set( IN_Info , MUIA_Text_Contents, (IPTR)"DialUp...");
385 //set( OUT_Info , MUIA_Text_Contents, (IPTR)"");
386 if( DialUp(Ser,c) ){
387 CloseSerial(Ser);
388 if( SendCtrlMsg( PPP_CTRL_OPEN_SERIAL , 0 , c )){
389 c->state = STATE_OPENDEV;
390 set( OUT_Info , MUIA_Text_Contents, (IPTR)"OK,Starting PPP...");
394 CloseSerial(Ser);
395 }else c->state = STATE_UNPLUGGED;
396 }else c->state = STATE_UNPLUGGED;
398 if( c->state != STATE_OPENDEV ){
399 set( OUT_Info , MUIA_Text_Contents, (IPTR)"ERROR");
405 #define FILEBUFFSIZE 4000
407 void ConfNetWork(struct PPPcontrolMsg *msg,struct Conf *c){
409 struct TagItem tags[] =
411 { SYS_Input, (IPTR)NULL },
412 { SYS_Output, (IPTR)NULL },
413 { SYS_Error, (IPTR)NULL },
414 { SYS_Asynch, (IPTR)FALSE },
415 { TAG_DONE, 0 }
418 struct Library *MiamiBase;
419 TEXT arostcppath[256];
420 UBYTE *buff;
422 arostcppath[0]=0;
423 GetVar( "SYS/Packages/AROSTCP" , arostcppath , 256 , LV_VAR);
425 bug("\n###########################################################\n");
426 bug("PPP is ONLINE !\n");
427 bug("Local IP address %d.%d.%d.%d\n",msg->LocalIP[0],msg->LocalIP[1],msg->LocalIP[2],msg->LocalIP[3]);
428 bug("Remote IP address %d.%d.%d.%d\n",msg->RemoteIP[0],msg->RemoteIP[1],msg->RemoteIP[2],msg->RemoteIP[3]);
430 if( buff = AllocMem( FILEBUFFSIZE , MEMF_CLEAR|MEMF_PUBLIC ) ){
432 bug("Primary DNS address %d.%d.%d.%d\n", msg->PrimaryDNS[0],msg->PrimaryDNS[1],
433 msg->PrimaryDNS[2],msg->PrimaryDNS[3] );
434 bug("Secondary DNS address %d.%d.%d.%d\n", msg->SecondaryDNS[0],msg->SecondaryDNS[1],
435 msg->SecondaryDNS[2],msg->SecondaryDNS[3] );
437 // Register nameservers with TCP/IP stack
438 if( FindTask("bsdsocket.library") != NULL ) {
439 MiamiBase = OpenLibrary("miami.library", 0);
440 if(MiamiBase != NULL) {
441 ClearDynNameServ();
442 struct sockaddr_in ns_addr;
444 ns_addr.sin_len = sizeof(ns_addr);
445 ns_addr.sin_family = AF_INET;
447 ns_addr.sin_addr.s_addr = *(ULONG *)msg->PrimaryDNS;
448 AddDynNameServ((struct sockaddr *)&ns_addr);
450 ns_addr.sin_addr.s_addr = *(ULONG *)msg->SecondaryDNS;
451 AddDynNameServ((struct sockaddr *)&ns_addr);
453 EndDynNameServ();
454 CloseLibrary(MiamiBase);
458 sprintf(buff,"%s/c/ifconfig %s %d.%d.%d.%d %d.%d.%d.%d",
459 arostcppath,
460 c->InterfaceName,
461 msg->LocalIP[0],msg->LocalIP[1],
462 msg->LocalIP[2],msg->LocalIP[3],
463 msg->RemoteIP[0],msg->RemoteIP[1],
464 msg->RemoteIP[2],msg->RemoteIP[3] );
466 bug("Executing command:\"%s\"\n",buff);
467 if( SystemTagList( buff , tags ) != 0 )
468 bug("command FAIL !!!!\n");
470 sprintf(buff,"%s/c/route add default %d.%d.%d.%d",
471 arostcppath,
472 msg->RemoteIP[0],msg->RemoteIP[1],
473 msg->RemoteIP[2],msg->RemoteIP[3] );
475 bug("Executing command:\"%s\"\n",buff);
476 if(SystemTagList( buff , tags ) != 0 )
477 bug("command FAIL !!!!\n");
479 bug("\n############################################################*\n");
481 FreeMem( buff , FILEBUFFSIZE );
485 void HandleMessage(struct PPPcontrolMsg *InfoMsg,struct Conf *c){
486 if( InfoMsg->Msg.mn_Length == sizeof(struct PPPcontrolMsg)
487 && InfoMsg->Command == PPP_CTRL_INFO
489 if( exPhase != InfoMsg->Phase || exSer != InfoMsg->Ser || exstate != c->state ){
490 exPhase = InfoMsg->Phase; exSer = InfoMsg->Ser; exstate = c->state;
491 bug("ModemManager:handlemsg phase=%d,ser=%d,state=%d\n",exPhase,exSer,exstate);
493 // TERMINATE phase in progress ,dont do nothing
494 if( InfoMsg->Phase == PPP_PHASE_TERMINATE ){
495 return;
498 // PPP initializing is ready
499 if( c->state == STATE_OPENDEV && InfoMsg->Phase == PPP_PHASE_NETWORK ){
500 ConfNetWork( InfoMsg ,c );
501 c->state = STATE_NETWORK;
504 // serial connection is lost (device unplugged)
505 if( c->state != STATE_UNPLUGGED && (! InfoMsg->Ser) ){
506 c->state = STATE_PLUGGED;
509 // Connection is ok
510 if( InfoMsg->Phase == PPP_PHASE_NETWORK && InfoMsg->Ser ){
511 c->state = STATE_NETWORK;
514 // net connection is lost
515 if( c->state == STATE_NETWORK && InfoMsg->Phase != PPP_PHASE_NETWORK ){
516 // SendCtrlMsg( PPP_CTRL_CLOSE_SERIAL , 0 , c );
517 c->state = STATE_PLUGGED;
523 int main(void)
525 struct Hook DisconnectHook,ConnectHook;
526 struct EasyTimer *timer=0;
527 BYTE buf[STRSIZE];
528 buf[0]=0;
529 ULONG sigs;
530 struct PPPcontrolMsg *CtrlMsg=0;
531 struct PPPcontrolMsg *InfoMsg=0;
532 struct MsgPort *CtrlPort=0;
533 struct EasySerial *Ser=0;
534 struct Conf *c=0;
536 struct EasyGraph *INegr=0;
537 struct EasyGraph *OUTegr=0;
539 Object *INGraphMUIbm,*OUTGraphMUIbm,*MUISignalBM;
541 ULONG SpeedIn=0,SpeedOUT=0;
543 if( ! FindPort(PortName)){
544 if( timer=OpenTimer() ){
545 if( CtrlPort = CreatePort(PortName,0) ){
546 if( CtrlMsg = AllocMem(sizeof(struct PPPcontrolMsg),MEMF_PUBLIC | MEMF_CLEAR)){
547 if( c = AllocMem(sizeof(struct Conf),MEMF_PUBLIC | MEMF_CLEAR)){
549 NEWLIST(&c->atcl);
551 for(;;)
554 ReadConfig(c);
556 c->state = STATE_UNPLUGGED;
557 c->signal = -1;
558 c->AccessType = -1;
559 SpeedIn =0;
560 SpeedOUT = 0;
562 SetTimer( timer , 0 );
563 application = NULL;
565 // send info request to ppp.device and wait response
566 if( SendRequest() ){
567 bug("ModemManager:wait response\n");
568 sigs = Wait( SIGBREAKF_CTRL_C |
569 (1L<< CtrlPort->mp_SigBit )
572 while( InfoMsg = (struct PPPcontrolMsg*)GetMsg(CtrlPort) ){
573 HandleMessage(InfoMsg,c);
574 ReplyMsg((struct Message *)InfoMsg);
577 if (sigs & SIGBREAKF_CTRL_C) goto shutdown;
580 SetTimer( timer , 1 );
581 bug("ModemManager:wait until %s unit %d open.\n",c->DeviceName ,c->SerUnitNum);
582 while(c->state == STATE_UNPLUGGED)
584 sigs = Wait( SIGBREAKF_CTRL_C |
585 (1L<< CtrlPort->mp_SigBit ) |
586 (1L<< timer->TimeMsg->mp_SigBit )
589 // handle incoming messages
590 while( InfoMsg = (struct PPPcontrolMsg*)GetMsg(CtrlPort) )
591 ReplyMsg((struct Message *)InfoMsg);
593 // Check if modem is plugged in.
594 if(GetMsg(timer->TimeMsg)){
595 FindModemUnit(c);
596 if( c->SerUnitNum >= 0 ){
597 if( Ser = OpenSerial( c->DeviceName ,c->SerUnitNum ) ){
598 if( TestModem( Ser , c ) ){
599 c->state = STATE_PLUGGED;
601 CloseSerial(Ser);
604 SetTimer( timer , 5 );
607 if (sigs & SIGBREAKF_CTRL_C) goto shutdown;
610 bug("ModemManager:Open GUI window\n");
612 DisconnectHook.h_Entry = HookEntry;
613 DisconnectHook.h_SubEntry = (HOOKFUNC) DisconnectFunc;
614 ConnectHook.h_Entry = HookEntry;
615 ConnectHook.h_SubEntry = (HOOKFUNC) ConnectFunc;
617 application = ApplicationObject,
618 SubWindow, window = WindowObject,
619 MUIA_Window_Title, (IPTR) "ModemManager",
620 MUIA_Window_Activate,TRUE,
621 WindowContents, (IPTR) VGroup,
623 Child, (IPTR) VGroup,
624 GroupFrame,
625 Child, (IPTR) HGroup,
626 Child, ModemName = SimpleText("1234567890123"),
627 Child, MUISignalBM = BitmapObject,
628 MUIA_FixWidth, 32,
629 MUIA_FixHeight, 16,
630 End,
631 Child, AccessType = SimpleText("12345"),
632 End,
633 Child, (IPTR) HGroup,
634 Child, IN_Info = SimpleText("12345678901"),
635 Child, INGraphMUIbm = BitmapObject,
636 MUIA_FixWidth, 80,
637 MUIA_FixHeight, 16,
638 End,
639 End,
640 Child, (IPTR) HGroup,
641 Child, OUT_Info = SimpleText("12345678901"),
642 Child, OUTGraphMUIbm = BitmapObject,
643 MUIA_FixWidth, 80,
644 MUIA_FixHeight, 16,
645 End,
646 End,
648 End,
649 Child, (IPTR) HGroup,
650 Child, (IPTR) ( ConBut = SimpleButton(" Connect ")),
651 Child, (IPTR) (DisConBut = SimpleButton(" Disconnect ")),
652 End,
653 End,
654 End,
655 End;
657 if (application)
659 sigs = 0;
661 DoMethod(
662 window, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
663 (IPTR) application, 2, MUIM_Application_ReturnID,
664 MUIV_Application_ReturnID_Quit
667 DoMethod(
668 ConBut,MUIM_Notify,MUIA_Pressed,FALSE,
669 application, (IPTR) 3,
670 MUIM_CallHook, &ConnectHook,c
673 DoMethod(
674 DisConBut,MUIM_Notify,MUIA_Pressed,FALSE,
675 application, (IPTR) 3,
676 MUIM_CallHook, &DisconnectHook,c
680 set(window,MUIA_Window_Open,TRUE);
681 set(DisConBut,MUIA_Disabled,TRUE);
682 set( ConBut,MUIA_Disabled,TRUE);
684 set( IN_Info , MUIA_Text_Contents, (IPTR)"");
685 set( OUT_Info , MUIA_Text_Contents, (IPTR)"");
687 SignalBM = MakeBitmap(32,16,window,MUISignalBM);
688 INegr = MakeGraph(80,16,window,INGraphMUIbm);
689 OUTegr = MakeGraph(80,16,window,OUTGraphMUIbm);
690 set( INGraphMUIbm , MUIA_ShowMe , FALSE );
691 set( OUTGraphMUIbm , MUIA_ShowMe , FALSE );
693 SetTimer( timer , 1 );
694 SendRequest();
696 UpdateModemInfo( SignalBM , c );
698 while(
699 DoMethod(
700 application, MUIM_Application_NewInput, (IPTR) &sigs
701 ) != MUIV_Application_ReturnID_Quit
704 if (sigs){
705 sigs = Wait( sigs |
706 SIGBREAKF_CTRL_C |
707 (1L<< CtrlPort->mp_SigBit ) |
708 (1L<< timer->TimeMsg->mp_SigBit )
711 while( InfoMsg = (struct PPPcontrolMsg*)GetMsg(CtrlPort) ){
712 //bug("ModemManager: received info message num %d\n",InfoMsg->num);
714 HandleMessage(InfoMsg,c);
716 if( c->state == STATE_UNPLUGGED ){
717 set( IN_Info , MUIA_Text_Contents, (IPTR)"Unplugged");
718 set( OUT_Info , MUIA_Text_Contents, (IPTR)"");
719 set(DisConBut, MUIA_Disabled , TRUE );
720 set( ConBut, MUIA_Disabled , TRUE );
722 else if( c->state == STATE_PLUGGED ){
723 set( INGraphMUIbm , MUIA_ShowMe , FALSE );
724 set( OUTGraphMUIbm , MUIA_ShowMe , FALSE );
725 set( IN_Info , MUIA_Text_Contents, (IPTR)"");
726 set( OUT_Info , MUIA_Text_Contents, (IPTR)"");
727 set(DisConBut, MUIA_Disabled , TRUE );
728 set( ConBut, MUIA_Disabled , FALSE );
730 else if( c->state == STATE_OPENDEV && ! InfoMsg->Ser){
731 set( IN_Info , MUIA_Text_Contents, (IPTR)"OpenDevice:");
732 snprintf( buf, STRSIZE , "%s unit %d",c->DeviceName,c->SerUnitNum);
733 set( OUT_Info , MUIA_Text_Contents, (IPTR)buf);
734 set(DisConBut, MUIA_Disabled , FALSE );
735 set( ConBut, MUIA_Disabled , TRUE );
738 else if( c->state == STATE_OPENDEV ){
739 set( IN_Info , MUIA_Text_Contents, (IPTR)"Connection in progress:");
740 UBYTE phase = InfoMsg->Phase;
742 set( OUT_Info , MUIA_Text_Contents,
743 (IPTR)( phase == PPP_PHASE_CONFIGURATION ? "LCP configuration" :
744 phase == PPP_PHASE_AUTHENTICATION ? "Authentication" :
745 phase == PPP_PHASE_PROTOCOL_CONF ? "Protocol configuration" :
746 "Unknow"
749 set(DisConBut, MUIA_Disabled , FALSE );
750 set( ConBut, MUIA_Disabled , TRUE );
751 //set(window,MUIA_Window_Title,(IPTR)c->modemmodel);
753 else if( c->state == STATE_NETWORK ){
754 if( InfoMsg->Phase == PPP_PHASE_TERMINATE ){
755 set(DisConBut, MUIA_Disabled , TRUE );
756 set( ConBut, MUIA_Disabled , TRUE );
757 set( INGraphMUIbm , MUIA_ShowMe , FALSE );
758 set( OUTGraphMUIbm , MUIA_ShowMe , FALSE );
759 set( IN_Info , MUIA_Text_Contents, (IPTR)"Terminate...");
760 set( OUT_Info , MUIA_Text_Contents, (IPTR)"");
761 } else{
762 SpeedIn = InfoMsg->SpeedIn;
763 SpeedOUT = InfoMsg->SpeedOut;
765 speedstr(buf," In",SpeedIn);
766 set( IN_Info , MUIA_Text_Contents, (IPTR)buf);
767 speedstr(buf,"Out",SpeedOUT);
768 set( OUT_Info , MUIA_Text_Contents, (IPTR)buf);
770 set(DisConBut, MUIA_Disabled , FALSE );
771 set( ConBut, MUIA_Disabled , TRUE );
773 set( INGraphMUIbm , MUIA_ShowMe , TRUE );
774 set( OUTGraphMUIbm , MUIA_ShowMe , TRUE );
778 ReplyMsg((struct Message *)InfoMsg);
779 //bug("ModemManager: ReplyMsg OK\n");
783 if(GetMsg(timer->TimeMsg)){
785 if( c->state == STATE_NETWORK ){
786 speedstr(buf," In",SpeedIn);
787 set( IN_Info , MUIA_Text_Contents, (IPTR)buf);
788 speedstr(buf,"Out",SpeedOUT);
789 set( OUT_Info , MUIA_Text_Contents, (IPTR)buf);
790 UpdateGraph(INegr,(FLOAT)SpeedIn);
791 UpdateGraph(OUTegr,(FLOAT)SpeedOUT);
794 // test if modem is unplugged
795 if( c->state == STATE_PLUGGED ){
796 if( Ser = OpenSerial( c->DeviceName ,c->SerUnitNum ) ){
797 CloseSerial(Ser);
798 //UpdateModemInfo( SignalBM , c );
799 } else
800 c->state = STATE_UNPLUGGED;
803 if( c->state == STATE_PLUGGED ){
804 set( OUT_Info , MUIA_Text_Contents, (IPTR)"");
805 set( IN_Info , MUIA_Text_Contents, (IPTR)"");
806 set(DisConBut, MUIA_Disabled , TRUE );
807 set( ConBut, MUIA_Disabled , FALSE );
810 SetTimer( timer , TIMERVALUE );
813 if (sigs & SIGBREAKF_CTRL_C) goto shutdown;
816 if( c->state == STATE_UNPLUGGED ) break;
817 } //GUI loop
819 // MUIV_Application_ReturnID_Quit ?
820 if( c->state != STATE_UNPLUGGED ) break;
822 bug("ModemManager:Device Unplugged -> Close GUI window\n");
823 SetTimer( timer , 0 );
824 MUI_DisposeObject(application);
825 application=NULL;
826 CloseBitmap(SignalBM);
827 CloseGraph(INegr);
828 CloseGraph(OUTegr);
829 } // main loop
831 }}}}}
833 // close all
834 shutdown:
835 bug("ModemManager:ShutDown\n");
836 if(application) MUI_DisposeObject(application);
837 CloseBitmap(SignalBM);
838 CloseGraph(INegr);
839 CloseGraph(OUTegr);
840 CloseTimer(timer);
842 struct at_command *atc;
843 while( atc = (struct at_command *)RemHead( &c->atcl ) ){
844 FreeMem( atc , sizeof(struct at_command) );
847 if(CtrlPort){
848 Forbid();
849 while( GetMsg(CtrlPort) ) ReplyMsg((struct Message *)CtrlMsg);
850 DeletePort(CtrlPort);
851 Permit();
854 if( c ) FreeMem( c , sizeof(struct Conf));
855 if( CtrlMsg ) FreeMem(CtrlMsg , sizeof(struct PPPcontrolMsg));
857 return 0;