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