Sun Feb 23 07:00:00 CET 2003
[asterisk/asterisk.git] / apps / app_waitforring.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Wait for Ring Application
5  * 
6  * Copyright (C) 2003, Digium
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <asterisk/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/pbx.h>
18 #include <asterisk/module.h>
19 #include <asterisk/options.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include <pthread.h>
26
27
28 static char *synopsis = "Wait for Ring Application";
29
30 static char *tdesc = "Waits until first ring after time";
31
32 static char *desc = "  WaitForRing(timeout)\n"
33 "Returns 0 after waiting at least timeout seconds. and\n"
34 "only after the next ring has completed.  Returns 0 on\n"
35 "success or -1 on hangup\n";
36
37 static char *app = "WaitForRing";
38
39 STANDARD_LOCAL_USER;
40
41 LOCAL_USER_DECL;
42
43 static int waitforring_exec(struct ast_channel *chan, void *data)
44 {
45         struct localuser *u;
46         struct ast_frame *f;
47         int res = 0;
48         int ms;
49         if (!data || (sscanf(data, "%d", &ms) != 1)) {
50                 ast_log(LOG_WARNING, "SoftHangup requires an argument (minimum seconds)\n");
51                 return 0;
52         }
53         ms *= 1000;
54         LOCAL_USER_ADD(u);
55         while(ms > 0) {
56                 ms = ast_waitfor(chan, ms);
57                 if (ms < 0) {
58                         res = ms;
59                         break;
60                 }
61                 if (ms > 0) {
62                         f = ast_read(chan);
63                         if (!f) {
64                                 res = -1;
65                                 break;
66                         }
67                         if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
68                                 if (option_verbose > 2)
69                                         ast_verbose(VERBOSE_PREFIX_3 "Got a ring but still waiting for timeout\n");
70                         }
71                         ast_frfree(f);
72                 }
73         }
74         /* Now we're really ready for the ring */
75         if (!res) {
76                 ms = 99999999;
77                 while(ms > 0) {
78                         ms = ast_waitfor(chan, ms);
79                         if (ms < 0) {
80                                 res = ms;
81                                 break;
82                         }
83                         if (ms > 0) {
84                                 f = ast_read(chan);
85                                 if (!f) {
86                                         res = -1;
87                                         break;
88                                 }
89                                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
90                                         if (option_verbose > 2)
91                                                 ast_verbose(VERBOSE_PREFIX_3 "Got a ring after the timeout\n");
92                                         ast_frfree(f);
93                                         break;
94                                 }
95                                 ast_frfree(f);
96                         }
97                 }
98         }
99         LOCAL_USER_REMOVE(u);
100
101         return res;
102 }
103
104 int unload_module(void)
105 {
106         STANDARD_HANGUP_LOCALUSERS;
107         return ast_unregister_application(app);
108 }
109
110 int load_module(void)
111 {
112         return ast_register_application(app, waitforring_exec, synopsis, desc);
113 }
114
115 char *description(void)
116 {
117         return tdesc;
118 }
119
120 int usecount(void)
121 {
122         int res;
123         STANDARD_USECOUNT(res);
124         return res;
125 }
126
127 char *key()
128 {
129         return ASTERISK_GPL_KEY;
130 }