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: 211569 $")
00031
00032 #include "asterisk/file.h"
00033 #include "asterisk/channel.h"
00034 #include "asterisk/pbx.h"
00035 #include "asterisk/module.h"
00036 #include "asterisk/lock.h"
00037
00038 static char *synopsis = "Wait for Ring Application";
00039
00040 static char *desc = " WaitForRing(timeout):\n"
00041 "Returns 0 after waiting at least timeout seconds. and\n"
00042 "only after the next ring has completed. Returns 0 on\n"
00043 "success or -1 on hangup\n";
00044
00045 static char *app = "WaitForRing";
00046
00047
00048 static int waitforring_exec(struct ast_channel *chan, void *data)
00049 {
00050 struct ast_frame *f;
00051 int res = 0;
00052 double s;
00053 int ms;
00054
00055 if (!data || (sscanf(data, "%30lg", &s) != 1)) {
00056 ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
00057 return 0;
00058 }
00059
00060 ms = s * 1000.0;
00061 while (ms > 0) {
00062 ms = ast_waitfor(chan, ms);
00063 if (ms < 0) {
00064 res = ms;
00065 break;
00066 }
00067 if (ms > 0) {
00068 f = ast_read(chan);
00069 if (!f) {
00070 res = -1;
00071 break;
00072 }
00073 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
00074 ast_verb(3, "Got a ring but still waiting for timeout\n");
00075 }
00076 ast_frfree(f);
00077 }
00078 }
00079
00080 if (!res) {
00081 ms = 99999999;
00082 while(ms > 0) {
00083 ms = ast_waitfor(chan, ms);
00084 if (ms < 0) {
00085 res = ms;
00086 break;
00087 }
00088 if (ms > 0) {
00089 f = ast_read(chan);
00090 if (!f) {
00091 res = -1;
00092 break;
00093 }
00094 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
00095 ast_verb(3, "Got a ring after the timeout\n");
00096 ast_frfree(f);
00097 break;
00098 }
00099 ast_frfree(f);
00100 }
00101 }
00102 }
00103
00104 return res;
00105 }
00106
00107 static int unload_module(void)
00108 {
00109 return ast_unregister_application(app);
00110 }
00111
00112 static int load_module(void)
00113 {
00114 return ast_register_application(app, waitforring_exec, synopsis, desc);
00115 }
00116
00117 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Waits until first ring after time");