Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:49 2009

## Asterisk developer's documentation

# app_waitforring.c

Go to the documentation of this file.

    00001 /*
    00002  * Asterisk -- An open source telephony toolkit.
    00003  *
    00004  * Copyright (C) 1999 - 2005, Digium, Inc.
    00005  *
    00006  * Mark Spencer <markster@digium.com>
    00007  *
    00008  * See http://www.asterisk.org for more information about
    00009  * the Asterisk project. Please do not directly contact
    00010  * any of the maintainers of this project for assistance;
    00011  * the project provides a web site, mailing lists and IRC
    00012  * channels for your use.
    00013  *
    00014  * This program is free software, distributed under the terms of
    00015  * the GNU General Public License Version 2. See the LICENSE file
    00016  * at the top of the source tree.
    00017  */
    00018
    00019 /*! \file
    00020  *
    00021  * \brief Wait for Ring Application
    00022  *
    00023  * \ingroup applications
    00024  */
    00025
    00026 #include <stdlib.h>
    00027 #include <stdio.h>
    00028 #include <string.h>
    00029 #include <unistd.h>
    00030 #include <sys/types.h>
    00031
    00032 #include "asterisk.h"
    00033
    00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211526 $")
    00035
    00036 #include "asterisk/file.h"
    00037 #include "asterisk/logger.h"
    00038 #include "asterisk/channel.h"
    00039 #include "asterisk/pbx.h"
    00040 #include "asterisk/module.h"
    00041 #include "asterisk/options.h"
    00042 #include "asterisk/lock.h"
    00043
    00044 static char *synopsis = "Wait for Ring Application";
    00045
    00046 static char *tdesc = "Waits until first ring after time";
    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 STANDARD_LOCAL_USER;
    00056
    00057 LOCAL_USER_DECL;
    00058
    00059 static int waitforring_exec(struct ast_channel *chan, void *data)
    00060 {
    00061    struct localuser *u;
    00062    struct ast_frame *f;
    00063    int res = 0;
    00064    int ms;
    00065
    00066    if (!data || (sscanf(data, "%30d", &ms) != 1)) {
    00067                 ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
    00068       return 0;
    00069    }
    00070
    00071    LOCAL_USER_ADD(u);
    00072
    00073    ms *= 1000;
    00074    while(ms > 0) {
    00075       ms = ast_waitfor(chan, ms);
    00076       if (ms < 0) {
    00077          res = ms;
    00078          break;
    00079       }
    00080       if (ms > 0) {
    00081          f = ast_read(chan);
    00082          if (!f) {
    00083             res = -1;
    00084             break;
    00085          }
    00086          if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
    00087             if (option_verbose > 2)
    00088                ast_verbose(VERBOSE_PREFIX_3 "Got a ring but still waiting for timeout\n");
    00089          }
    00090          ast_frfree(f);
    00091       }
    00092    }
    00093    /* Now we're really ready for the ring */
    00094    if (!res) {
    00095       ms = 99999999;
    00096       while(ms > 0) {
    00097          ms = ast_waitfor(chan, ms);
    00098          if (ms < 0) {
    00099             res = ms;
    00100             break;
    00101          }
    00102          if (ms > 0) {
    00103             f = ast_read(chan);
    00104             if (!f) {
    00105                res = -1;
    00106                break;
    00107             }
    00108             if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
    00109                if (option_verbose > 2)
    00110                   ast_verbose(VERBOSE_PREFIX_3 "Got a ring after the timeout\n");
    00111                ast_frfree(f);
    00112                break;
    00113             }
    00114             ast_frfree(f);
    00115          }
    00116       }
    00117    }
    00118    LOCAL_USER_REMOVE(u);
    00119
    00120    return res;
    00121 }
    00122
    00123 int unload_module(void)
    00124 {
    00125    int res;
    00126
    00127    res = ast_unregister_application(app);
    00128
    00129    STANDARD_HANGUP_LOCALUSERS;
    00130
    00131    return res;
    00132 }
    00133
    00134 int load_module(void)
    00135 {
    00136    return ast_register_application(app, waitforring_exec, synopsis, desc);
    00137 }
    00138
    00139 char *description(void)
    00140 {
    00141    return tdesc;
    00142 }
    00143
    00144 int usecount(void)
    00145 {
    00146    int res;
    00147    STANDARD_USECOUNT(res);
    00148    return res;
    00149 }
    00150
    00151 char *key()
    00152 {
    00153    return ASTERISK_GPL_KEY;
    00154 }