Added locale item (sysmon.cd)
[AROS.git] / workbench / c / WaitForPort.c
blobff50db979c8b7d34860789c03e3f5e7574b40316
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 /******************************************************************************
12 NAME
14 WaitForPort
16 SYNOPSIS
18 P=PORT/A
20 LOCATION
24 FUNCTION
26 Waits up to 10 seconds for a user specified port
27 to become available.
29 INPUTS
31 PORT -- Name of the port to be waited.
33 RESULT
35 NOTES
37 EXAMPLE
39 BUGS
41 SEE ALSO
43 INTERNALS
45 HISTORY
47 ******************************************************************************/
49 #include <dos/rdargs.h>
50 #include <proto/exec.h>
51 #include <proto/dos.h>
52 #include <devices/timer.h>
53 #include <proto/alib.h>
55 #include <aros/debug.h>
57 const char version[] = "$VER: WaitForPort 0.1 (26.12.2005)";
59 const char WaitForPort_ArgTemplate[] = "P=PORT/A";
60 IPTR WaitForPort_Arguments[2];
62 struct RDArgs *WFP_rda = NULL;
64 struct Device *TimerBase = NULL;
65 static struct MsgPort *timerport = NULL; /* Timer message reply port */
66 static struct timerequest *timerIORequest = NULL; /* template IORequest */
67 ULONG wait_time;
68 ULONG wait_limit;
69 int
70 main(int argc, char *argv[])
72 struct MsgPort *AROSTCP_Port = NULL;
74 wait_time = 0;
76 if ((WFP_rda = ReadArgs(WaitForPort_ArgTemplate, WaitForPort_Arguments, NULL)))
79 if (WaitForPort_Arguments[0])
81 D(bug("[WaitForPort] Waiting for '%s' port\n",WaitForPort_Arguments[0]));
84 timerport = CreateMsgPort();
85 if (timerport != NULL)
87 /* allocate and initialize the template message structure */
88 timerIORequest = (struct timerequest *) CreateIORequest(timerport, sizeof(struct timerequest));
90 if (timerIORequest != NULL)
92 if (!(OpenDevice(TIMERNAME, UNIT_VBLANK,
93 (struct IORequest *)timerIORequest, 0)))
95 /* Make sure that we got at least V36 timer, since we use some
96 * functions defined only in V36 and later. */
98 if ((timerIORequest->tr_node.io_Device)->dd_Library.lib_Version >= 36)
100 /* initialize TimerBase from timerIORequest */
101 TimerBase = timerIORequest->tr_node.io_Device;
103 /* Initialize some fields of the IO request to common values */
104 timerIORequest->tr_node.io_Command = TR_ADDREQUEST;
106 /* NT_UNKNOWN means unused, too (see note on exec/nodes.h) */
107 timerIORequest->tr_node.io_Message.mn_Node.ln_Type = NT_UNKNOWN;
109 timerIORequest->tr_time.tv_micro = 1000000;
110 wait_limit = timerIORequest->tr_time.tv_micro * 10; /* Default to a 10 second wait */
112 BeginIO((struct IORequest *)timerIORequest);
114 /* MAIN LOOP */
115 while(1)
117 D(bug("[WaitForPort] In Wait Loop ..\n"));
118 ULONG mask = SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | (1 << timerport->mp_SigBit);
119 mask = Wait(mask);
120 if (mask & SIGBREAKF_CTRL_C) break;
121 if (mask & SIGBREAKF_CTRL_D) break;
122 if (mask & (1 << timerport->mp_SigBit))
124 D(bug("[WaitForPort] Received timer signal?...\n"));
125 timerIORequest = (struct timerequest *)GetMsg(timerport);
126 if (timerIORequest)
128 AROSTCP_Port = FindPort((char *)WaitForPort_Arguments[0]);
129 wait_time += 1000000;
130 if (!(AROSTCP_Port))
132 if (wait_time > wait_limit)
134 D(bug("[WaitForPort] Timeout Reached\n"));
135 break;
137 D(bug("[WaitForPort] Port not found .. secs=%d\n",wait_time/1000000));
139 else
141 D(bug("[WaitForPort] Port found ... escaping from wait loop\n"));
142 break;
144 timerIORequest->tr_node.io_Command = TR_ADDREQUEST;
145 timerIORequest->tr_time.tv_micro = 1000000;
146 BeginIO((struct IORequest *)timerIORequest);
154 /* CLEANUP */
156 if (timerIORequest)
158 TimerBase = NULL;
160 if (timerIORequest->tr_node.io_Device != NULL) CloseDevice((struct IORequest *)timerIORequest);
162 DeleteIORequest((struct IORequest *)timerIORequest);
163 timerIORequest = NULL;
166 if (timerport)
168 DeleteMsgPort(timerport);
169 timerport = NULL;
173 FreeArgs(WFP_rda);
175 else
177 Printf("WaitForPort: Bad Arguments .. Use 'WaitForPort ?' for correct useage\n");
180 if (AROSTCP_Port) return RETURN_OK;
182 return RETURN_WARN;