Added memory-stored debug output to safe mode.
[cake.git] / workbench / c / WaitForPort.c
blob2fb2505c517f964087037584bb55487f5ffb8dca
1 /*
2 Copyright (C) 2004, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Waits up to 10 seconds for a user specified Port to become available
6 Lang: English
7 */
9 #include <dos/rdargs.h>
10 #include <proto/exec.h>
11 #include <proto/dos.h>
12 #include <devices/timer.h>
13 #include <stdio.h>
14 #include <proto/alib.h>
16 #include <aros/debug.h>
18 const char version[] = "$VER: WaitForPort 0.1 (26.12.2005)";
20 const char WaitForPort_ArgTemplate[] = "P=PORT/A";
21 IPTR WaitForPort_Arguments[2];
23 struct RDArgs *WFP_rda = NULL;
25 struct Device *TimerBase = NULL;
26 static struct MsgPort *timerport = NULL; /* Timer message reply port */
27 static struct timerequest *timerIORequest = NULL; /* template IORequest */
28 ULONG wait_time;
29 ULONG wait_limit;
30 int
31 main(int argc, char *argv[])
33 struct MsgPort *AROSTCP_Port = NULL;
35 wait_time = 0;
37 if ((WFP_rda = ReadArgs(WaitForPort_ArgTemplate, WaitForPort_Arguments, NULL)))
40 if (WaitForPort_Arguments[0])
42 D(bug("[WaitForPort] Waiting for '%s' port\n",WaitForPort_Arguments[0]));
45 timerport = CreateMsgPort();
46 if (timerport != NULL)
48 /* allocate and initialize the template message structure */
49 timerIORequest = (struct timerequest *) CreateIORequest(timerport, sizeof(struct timerequest));
51 if (timerIORequest != NULL)
53 if (!(OpenDevice(TIMERNAME, UNIT_VBLANK,
54 (struct IORequest *)timerIORequest, 0)))
56 /* Make sure that we got at least V36 timer, since we use some
57 * functions defined only in V36 and later. */
59 if ((timerIORequest->tr_node.io_Device)->dd_Library.lib_Version >= 36)
61 /* initialize TimerBase from timerIORequest */
62 TimerBase = timerIORequest->tr_node.io_Device;
64 /* Initialize some fields of the IO request to common values */
65 timerIORequest->tr_node.io_Command = TR_ADDREQUEST;
67 /* NT_UNKNOWN means unused, too (see note on exec/nodes.h) */
68 timerIORequest->tr_node.io_Message.mn_Node.ln_Type = NT_UNKNOWN;
70 timerIORequest->tr_time.tv_micro = 1000000;
71 wait_limit = timerIORequest->tr_time.tv_micro * 10; /* Default to a 10 second wait */
73 BeginIO((struct IORequest *)timerIORequest);
75 /* MAIN LOOP */
76 while(1)
78 D(bug("[WaitForPort] In Wait Loop ..\n"));
79 ULONG mask = SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | (1 << timerport->mp_SigBit);
80 mask = Wait(mask);
81 if (mask & SIGBREAKF_CTRL_C) break;
82 if (mask & SIGBREAKF_CTRL_D) break;
83 if (mask & (1 << timerport->mp_SigBit))
85 D(bug("[WaitForPort] Recieved timer signal? ..\n"));
86 timerIORequest = (struct timerequest *)GetMsg(timerport);
87 if (timerIORequest)
89 AROSTCP_Port = FindPort((char *)WaitForPort_Arguments[0]);
90 wait_time += 1000000;
91 if (!(AROSTCP_Port))
93 if (wait_time > wait_limit)
95 D(bug("[WaitForPort] Timeout Reached\n"));
96 break;
98 D(bug("[WaitForPort] Port not found .. secs=%d\n",wait_time/1000000));
100 else
102 D(bug("[WaitForPort] Port found ... escaping from wait loop\n"));
103 break;
105 timerIORequest->tr_node.io_Command = TR_ADDREQUEST;
106 timerIORequest->tr_time.tv_micro = 1000000;
107 BeginIO((struct IORequest *)timerIORequest);
115 /* CLEANUP */
117 if (timerIORequest)
119 TimerBase = NULL;
121 if (timerIORequest->tr_node.io_Device != NULL) CloseDevice((struct IORequest *)timerIORequest);
123 DeleteIORequest((struct IORequest *)timerIORequest);
124 timerIORequest = NULL;
127 if (timerport)
129 DeleteMsgPort(timerport);
130 timerport = NULL;
134 FreeArgs(WFP_rda);
136 else
138 printf("WaitForPort: Bad Arguments .. Use 'WaitForPort ?' for correct useage\n");
141 if (AROSTCP_Port) return RETURN_OK;
143 return RETURN_WARN;