app_waitforring.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 239718 $")
00031
00032 #include <stdlib.h>
00033 #include <stdio.h>
00034 #include <string.h>
00035 #include <unistd.h>
00036 #include <sys/types.h>
00037
00038 #include "asterisk/file.h"
00039 #include "asterisk/logger.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/module.h"
00043 #include "asterisk/options.h"
00044 #include "asterisk/lock.h"
00045
00046 static char *synopsis = "Wait for Ring Application";
00047
00048 static char *desc = " WaitForRing(timeout)\n"
00049 "Returns 0 after waiting at least timeout seconds. and\n"
00050 "only after the next ring has completed. Returns 0 on\n"
00051 "success or -1 on hangup\n";
00052
00053 static char *app = "WaitForRing";
00054
00055
00056 static int waitforring_exec(struct ast_channel *chan, void *data)
00057 {
00058 struct ast_module_user *u;
00059 struct ast_frame *f;
00060 struct ast_silence_generator *silgen = NULL;
00061 int res = 0;
00062 int ms;
00063
00064 if (!data || (sscanf(data, "%30d", &ms) != 1)) {
00065 ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
00066 return 0;
00067 }
00068
00069 u = ast_module_user_add(chan);
00070
00071 if (ast_opt_transmit_silence) {
00072 silgen = ast_channel_start_silence_generator(chan);
00073 }
00074
00075 ms *= 1000;
00076 while(ms > 0) {
00077 ms = ast_waitfor(chan, ms);
00078 if (ms < 0) {
00079 res = ms;
00080 break;
00081 }
00082 if (ms > 0) {
00083 f = ast_read(chan);
00084 if (!f) {
00085 res = -1;
00086 break;
00087 }
00088 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
00089 if (option_verbose > 2)
00090 ast_verbose(VERBOSE_PREFIX_3 "Got a ring but still waiting for timeout\n");
00091 }
00092 ast_frfree(f);
00093 }
00094 }
00095
00096 if (!res) {
00097 ms = 99999999;
00098 while(ms > 0) {
00099 ms = ast_waitfor(chan, ms);
00100 if (ms < 0) {
00101 res = ms;
00102 break;
00103 }
00104 if (ms > 0) {
00105 f = ast_read(chan);
00106 if (!f) {
00107 res = -1;
00108 break;
00109 }
00110 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
00111 if (option_verbose > 2)
00112 ast_verbose(VERBOSE_PREFIX_3 "Got a ring after the timeout\n");
00113 ast_frfree(f);
00114 break;
00115 }
00116 ast_frfree(f);
00117 }
00118 }
00119 }
00120 ast_module_user_remove(u);
00121
00122 if (silgen) {
00123 ast_channel_stop_silence_generator(chan, silgen);
00124 }
00125
00126 return res;
00127 }
00128
00129 static int unload_module(void)
00130 {
00131 int res;
00132
00133 res = ast_unregister_application(app);
00134
00135 ast_module_user_hangup_all();
00136
00137 return res;
00138 }
00139
00140 static int load_module(void)
00141 {
00142 return ast_register_application(app, waitforring_exec, synopsis, desc);
00143 }
00144
00145 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Waits until first ring after time");