Wed May 16 06:33:20 2012

Asterisk developer's documentation


app_page.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (c) 2004 - 2006 Digium, Inc.  All rights reserved.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * This code is released under the GNU General Public License
00009  * version 2.0.  See LICENSE for more information.
00010  *
00011  * See http://www.asterisk.org for more information about
00012  * the Asterisk project. Please do not directly contact
00013  * any of the maintainers of this project for assistance;
00014  * the project provides a web site, mailing lists and IRC
00015  * channels for your use.
00016  *
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief page() - Paging application
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  *
00025  * \ingroup applications
00026  */
00027 
00028 /*** MODULEINFO
00029    <depend>app_confbridge</depend>
00030    <support_level>core</support_level>
00031  ***/
00032 
00033 #include "asterisk.h"
00034 
00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 360365 $")
00036 
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/module.h"
00040 #include "asterisk/file.h"
00041 #include "asterisk/app.h"
00042 #include "asterisk/chanvars.h"
00043 #include "asterisk/utils.h"
00044 #include "asterisk/devicestate.h"
00045 #include "asterisk/dial.h"
00046 
00047 /*** DOCUMENTATION
00048    <application name="Page" language="en_US">
00049       <synopsis>
00050          Page series of phones
00051       </synopsis>
00052       <syntax>
00053          <parameter name="Technology/Resource" required="true" argsep="&amp;">
00054             <argument name="Technology/Resource" required="true">
00055                <para>Specification of the device(s) to dial. These must be in the format of
00056                <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
00057                represents a particular channel driver, and <replaceable>Resource</replaceable> represents a resource
00058                available to that particular channel driver.</para>
00059             </argument>
00060             <argument name="Technology2/Resource2" multiple="true">
00061                <para>Optional extra devices to dial inparallel</para>
00062                <para>If you need more then one enter them as Technology2/Resource2&amp;
00063                Technology3/Resourse3&amp;.....</para>
00064             </argument>
00065          </parameter>
00066          <parameter name="options">
00067             <optionlist>
00068                <option name="d">
00069                   <para>Full duplex audio</para>
00070                </option>
00071                <option name="i">
00072                   <para>Ignore attempts to forward the call</para>
00073                </option>
00074                <option name="q">
00075                   <para>Quiet, do not play beep to caller</para>
00076                </option>
00077                <option name="r">
00078                   <para>Record the page into a file (ConfBridge option <literal>r</literal>)</para>
00079                </option>
00080                <option name="s">
00081                   <para>Only dial a channel if its device state says that it is <literal>NOT_INUSE</literal></para>
00082                </option>
00083                <option name="A">
00084                   <argument name="x" required="true">
00085                      <para>The announcement to playback in all devices</para>
00086                   </argument>
00087                   <para>Play an announcement simultaneously to all paged participants</para>
00088                </option>
00089                <option name="n">
00090                   <para>Do not play simultaneous announcement to caller (implies <literal>A(x)</literal>)</para>
00091                </option>
00092             </optionlist>
00093          </parameter>
00094          <parameter name="timeout">
00095             <para>Specify the length of time that the system will attempt to connect a call.
00096             After this duration, any intercom calls that have not been answered will be hung up by the
00097             system.</para>
00098          </parameter>
00099       </syntax>
00100       <description>
00101          <para>Places outbound calls to the given <replaceable>technology</replaceable>/<replaceable>resource</replaceable>
00102          and dumps them into a conference bridge as muted participants. The original
00103          caller is dumped into the conference as a speaker and the room is
00104          destroyed when the original callers leaves.</para>
00105       </description>
00106       <see-also>
00107          <ref type="application">ConfBridge</ref>
00108       </see-also>
00109    </application>
00110  ***/
00111 static const char * const app_page= "Page";
00112 
00113 enum page_opt_flags {
00114    PAGE_DUPLEX = (1 << 0),
00115    PAGE_QUIET = (1 << 1),
00116    PAGE_RECORD = (1 << 2),
00117    PAGE_SKIP = (1 << 3),
00118    PAGE_IGNORE_FORWARDS = (1 << 4),
00119    PAGE_ANNOUNCE = (1 << 5),
00120    PAGE_NOCALLERANNOUNCE = (1 << 6),
00121 };
00122 
00123 enum {
00124    OPT_ARG_ANNOUNCE = 0,
00125    OPT_ARG_ARRAY_SIZE = 1,
00126 };
00127 
00128 AST_APP_OPTIONS(page_opts, {
00129    AST_APP_OPTION('d', PAGE_DUPLEX),
00130    AST_APP_OPTION('q', PAGE_QUIET),
00131    AST_APP_OPTION('r', PAGE_RECORD),
00132    AST_APP_OPTION('s', PAGE_SKIP),
00133    AST_APP_OPTION('i', PAGE_IGNORE_FORWARDS),
00134    AST_APP_OPTION_ARG('A', PAGE_ANNOUNCE, OPT_ARG_ANNOUNCE),
00135    AST_APP_OPTION('n', PAGE_NOCALLERANNOUNCE),
00136 });
00137 
00138 /* We use this structure as a way to pass this to all dialed channels */
00139 struct page_options {
00140    char *opts[OPT_ARG_ARRAY_SIZE];
00141    struct ast_flags flags;
00142 };
00143 
00144 static void page_state_callback(struct ast_dial *dial)
00145 {
00146    struct ast_channel *chan;
00147    struct page_options *options;
00148 
00149    if (ast_dial_state(dial) != AST_DIAL_RESULT_ANSWERED ||
00150        !(chan = ast_dial_answered(dial)) ||
00151        !(options = ast_dial_get_user_data(dial))) {
00152       return;
00153    }
00154 
00155    ast_func_write(chan, "CONFBRIDGE(bridge,template)", "default_bridge");
00156 
00157    if (ast_test_flag(&options->flags, PAGE_RECORD)) {
00158       ast_func_write(chan, "CONFBRIDGE(bridge,record_conference)", "yes");
00159    }
00160 
00161    ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
00162    ast_func_write(chan, "CONFBRIDGE(user,end_marked)", "yes");
00163 
00164    if (!ast_test_flag(&options->flags, PAGE_DUPLEX)) {
00165       ast_func_write(chan, "CONFBRIDGE(user,startmuted)", "yes");
00166    }
00167 
00168    if (ast_test_flag(&options->flags, PAGE_ANNOUNCE) && !ast_strlen_zero(options->opts[OPT_ARG_ANNOUNCE])) {
00169       ast_func_write(chan, "CONFBRIDGE(user,announcement)", options->opts[OPT_ARG_ANNOUNCE]);
00170    }
00171 }
00172 
00173 static int page_exec(struct ast_channel *chan, const char *data)
00174 {
00175    char *tech, *resource, *tmp;
00176    char confbridgeopts[128], originator[AST_CHANNEL_NAME];
00177    struct page_options options = { { 0, }, { 0, } };
00178    unsigned int confid = ast_random();
00179    struct ast_app *app;
00180    int res = 0, pos = 0, i = 0;
00181    struct ast_dial **dial_list;
00182    unsigned int num_dials;
00183    int timeout = 0;
00184    char *parse;
00185 
00186    AST_DECLARE_APP_ARGS(args,
00187       AST_APP_ARG(devices);
00188       AST_APP_ARG(options);
00189       AST_APP_ARG(timeout);
00190    );
00191 
00192    if (ast_strlen_zero(data)) {
00193       ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
00194       return -1;
00195    }
00196 
00197    if (!(app = pbx_findapp("ConfBridge"))) {
00198       ast_log(LOG_WARNING, "There is no ConfBridge application available!\n");
00199       return -1;
00200    };
00201 
00202    parse = ast_strdupa(data);
00203 
00204    AST_STANDARD_APP_ARGS(args, parse);
00205 
00206    ast_copy_string(originator, ast_channel_name(chan), sizeof(originator));
00207    if ((tmp = strchr(originator, '-'))) {
00208       *tmp = '\0';
00209    }
00210 
00211    if (!ast_strlen_zero(args.options)) {
00212       ast_app_parse_options(page_opts, &options.flags, options.opts, args.options);
00213    }
00214 
00215    if (!ast_strlen_zero(args.timeout)) {
00216       timeout = atoi(args.timeout);
00217    }
00218 
00219    snprintf(confbridgeopts, sizeof(confbridgeopts), "ConfBridge,%u", confid);
00220 
00221    /* Count number of extensions in list by number of ampersands + 1 */
00222    num_dials = 1;
00223    tmp = args.devices;
00224    while (*tmp) {
00225       if (*tmp == '&') {
00226          num_dials++;
00227       }
00228       tmp++;
00229    }
00230 
00231    if (!(dial_list = ast_calloc(num_dials, sizeof(struct ast_dial *)))) {
00232       ast_log(LOG_ERROR, "Can't allocate %ld bytes for dial list\n", (long)(sizeof(struct ast_dial *) * num_dials));
00233       return -1;
00234    }
00235 
00236    /* Go through parsing/calling each device */
00237    while ((tech = strsep(&args.devices, "&"))) {
00238       int state = 0;
00239       struct ast_dial *dial = NULL;
00240 
00241       /* don't call the originating device */
00242       if (!strcasecmp(tech, originator))
00243          continue;
00244 
00245       /* If no resource is available, continue on */
00246       if (!(resource = strchr(tech, '/'))) {
00247          ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
00248          continue;
00249       }
00250 
00251       /* Ensure device is not in use if skip option is enabled */
00252       if (ast_test_flag(&options.flags, PAGE_SKIP)) {
00253          state = ast_device_state(tech);
00254          if (state == AST_DEVICE_UNKNOWN) {
00255             ast_log(LOG_WARNING, "Destination '%s' has device state '%s'. Paging anyway.\n", tech, ast_devstate2str(state));
00256          } else if (state != AST_DEVICE_NOT_INUSE) {
00257             ast_log(LOG_WARNING, "Destination '%s' has device state '%s'.\n", tech, ast_devstate2str(state));
00258             continue;
00259          }
00260       }
00261 
00262       *resource++ = '\0';
00263 
00264       /* Create a dialing structure */
00265       if (!(dial = ast_dial_create())) {
00266          ast_log(LOG_WARNING, "Failed to create dialing structure.\n");
00267          continue;
00268       }
00269 
00270       /* Append technology and resource */
00271       if (ast_dial_append(dial, tech, resource) == -1) {
00272          ast_log(LOG_ERROR, "Failed to add %s to outbound dial\n", tech);
00273          continue;
00274       }
00275 
00276       /* Set ANSWER_EXEC as global option */
00277       ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, confbridgeopts);
00278 
00279       if (timeout) {
00280          ast_dial_set_global_timeout(dial, timeout * 1000);
00281       }
00282 
00283       if (ast_test_flag(&options.flags, PAGE_IGNORE_FORWARDS)) {
00284          ast_dial_option_global_enable(dial, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL);
00285       }
00286 
00287       ast_dial_set_state_callback(dial, &page_state_callback);
00288       ast_dial_set_user_data(dial, &options);
00289 
00290       /* Run this dial in async mode */
00291       ast_dial_run(dial, chan, 1);
00292 
00293       /* Put in our dialing array */
00294       dial_list[pos++] = dial;
00295    }
00296 
00297    if (!ast_test_flag(&options.flags, PAGE_QUIET)) {
00298       res = ast_streamfile(chan, "beep", ast_channel_language(chan));
00299       if (!res)
00300          res = ast_waitstream(chan, "");
00301    }
00302 
00303    if (!res) {
00304       ast_func_write(chan, "CONFBRIDGE(bridge,template)", "default_bridge");
00305 
00306       if (ast_test_flag(&options.flags, PAGE_RECORD)) {
00307          ast_func_write(chan, "CONFBRIDGE(bridge,record_conference)", "yes");
00308       }
00309 
00310       ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
00311       ast_func_write(chan, "CONFBRIDGE(user,marked)", "yes");
00312 
00313       snprintf(confbridgeopts, sizeof(confbridgeopts), "%u", confid);
00314 
00315       pbx_exec(chan, app, confbridgeopts);
00316    }
00317 
00318    /* Go through each dial attempt cancelling, joining, and destroying */
00319    for (i = 0; i < pos; i++) {
00320       struct ast_dial *dial = dial_list[i];
00321 
00322       /* We have to wait for the async thread to exit as it's possible ConfBridge won't throw them out immediately */
00323       ast_dial_join(dial);
00324 
00325       /* Hangup all channels */
00326       ast_dial_hangup(dial);
00327 
00328       /* Destroy dialing structure */
00329       ast_dial_destroy(dial);
00330    }
00331 
00332    ast_free(dial_list);
00333 
00334    return -1;
00335 }
00336 
00337 static int unload_module(void)
00338 {
00339    return ast_unregister_application(app_page);
00340 }
00341 
00342 static int load_module(void)
00343 {
00344    return ast_register_application_xml(app_page, page_exec);
00345 }
00346 
00347 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Page Multiple Phones");
00348 

Generated on Wed May 16 06:33:20 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6