Fixed out-by-one error in previous commit.
[AROS.git] / workbench / c / WaitForPort.c
blob6c99f4cfceecdf66c0d56e96b8e28a54d2459166
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 <proto/alib.h>
15 #include <aros/debug.h>
17 const char version[] = "$VER: WaitForPort 0.1 (26.12.2005)";
19 const char WaitForPort_ArgTemplate[] = "P=PORT/A";
20 IPTR WaitForPort_Arguments[2];
22 struct RDArgs *WFP_rda = NULL;
24 struct Device *TimerBase = NULL;
25 static struct MsgPort *timerport = NULL; /* Timer message reply port */
26 static struct timerequest *timerIORequest = NULL; /* template IORequest */
27 ULONG wait_time;
28 ULONG wait_limit;
29 int
30 main(int argc, char *argv[])
32 struct MsgPort *AROSTCP_Port = NULL;
34 wait_time = 0;
36 if ((WFP_rda = ReadArgs(WaitForPort_ArgTemplate, WaitForPort_Arguments, NULL)))
39 if (WaitForPort_Arguments[0])
41 D(bug("[WaitForPort] Waiting for '%s' port\n",WaitForPort_Arguments[0]));
44 timerport = CreateMsgPort();
45 if (timerport != NULL)
47 /* allocate and initialize the template message structure */
48 timerIORequest = (struct timerequest *) CreateIORequest(timerport, sizeof(struct timerequest));
50 if (timerIORequest != NULL)
52 if (!(OpenDevice(TIMERNAME, UNIT_VBLANK,
53 (struct IORequest *)timerIORequest, 0)))
55 /* Make sure that we got at least V36 timer, since we use some
56 * functions defined only in V36 and later. */
58 if ((timerIORequest->tr_node.io_Device)->dd_Library.lib_Version >= 36)
60 /* initialize TimerBase from timerIORequest */
61 TimerBase = timerIORequest->tr_node.io_Device;
63 /* Initialize some fields of the IO request to common values */
64 timerIORequest->tr_node.io_Command = TR_ADDREQUEST;
66 /* NT_UNKNOWN means unused, too (see note on exec/nodes.h) */
67 timerIORequest->tr_node.io_Message.mn_Node.ln_Type = NT_UNKNOWN;
69 timerIORequest->tr_time.tv_micro = 1000000;
70 wait_limit = timerIORequest->tr_time.tv_micro * 10; /* Default to a 10 second wait */
72 BeginIO((struct IORequest *)timerIORequest);
74 /* MAIN LOOP */
75 while(1)
77 D(bug("[WaitForPort] In Wait Loop ..\n"));
78 ULONG mask = SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | (1 << timerport->mp_SigBit);
79 mask = Wait(mask);
80 if (mask & SIGBREAKF_CTRL_C) break;
81 if (mask & SIGBREAKF_CTRL_D) break;
82 if (mask & (1 << timerport->mp_SigBit))
84 D(bug("[WaitForPort] Received timer signal?...\n"));
85 timerIORequest = (struct timerequest *)GetMsg(timerport);
86 if (timerIORequest)
88 AROSTCP_Port = FindPort((char *)WaitForPort_Arguments[0]);
89 wait_time += 1000000;
90 if (!(AROSTCP_Port))
92 if (wait_time > wait_limit)
94 D(bug("[WaitForPort] Timeout Reached\n"));
95 break;
97 D(bug("[WaitForPort] Port not found .. secs=%d\n",wait_time/1000000));
99 else
101 D(bug("[WaitForPort] Port found ... escaping from wait loop\n"));
102 break;
104 timerIORequest->tr_node.io_Command = TR_ADDREQUEST;
105 timerIORequest->tr_time.tv_micro = 1000000;
106 BeginIO((struct IORequest *)timerIORequest);
114 /* CLEANUP */
116 if (timerIORequest)
118 TimerBase = NULL;
120 if (timerIORequest->tr_node.io_Device != NULL) CloseDevice((struct IORequest *)timerIORequest);
122 DeleteIORequest((struct IORequest *)timerIORequest);
123 timerIORequest = NULL;
126 if (timerport)
128 DeleteMsgPort(timerport);
129 timerport = NULL;
133 FreeArgs(WFP_rda);
135 else
137 Printf("WaitForPort: Bad Arguments .. Use 'WaitForPort ?' for correct useage\n");
140 if (AROSTCP_Port) return RETURN_OK;
142 return RETURN_WARN;