Wed Apr 6 11:29:45 2011

Asterisk developer's documentation


func_extstate.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007, Digium, Inc.
00005  *
00006  * Modified from func_devstate.c by Russell Bryant <russell@digium.com> 
00007  * Adam Gundy <adam@starsilk.net>
00008 
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*! \file
00021  *
00022  * \brief Get the state of a hinted extension for dialplan control
00023  *
00024  * \author Adam Gundy <adam@starsilk.net> 
00025  *
00026  * \ingroup functions
00027  */
00028 
00029 #include "asterisk.h"
00030 
00031 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 191140 $")
00032 
00033 #include "asterisk/module.h"
00034 #include "asterisk/channel.h"
00035 #include "asterisk/pbx.h"
00036 #include "asterisk/utils.h"
00037 #include "asterisk/devicestate.h"
00038 
00039 /*** DOCUMENTATION
00040    <function name="EXTENSION_STATE" language="en_US">
00041       <synopsis>
00042          Get an extension's state.
00043       </synopsis> 
00044       <syntax argsep="@">
00045          <parameter name="extension" required="true" />
00046          <parameter name="context">
00047             <para>If it is not specified defaults to <literal>default</literal>.</para>
00048          </parameter>
00049       </syntax>
00050       <description>
00051          <para>The EXTENSION_STATE function can be used to retrieve the state from any
00052          hinted extension. For example:</para>
00053          <para>NoOp(1234@default has state ${EXTENSION_STATE(1234)})</para>
00054          <para>NoOp(4567@home has state ${EXTENSION_STATE(4567@home)})</para>
00055          <para>The possible values returned by this function are:</para>
00056          <para>UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING |
00057          RINGINUSE | HOLDINUSE | ONHOLD</para>
00058       </description>
00059    </function>
00060  ***/
00061 
00062 static const char *ast_extstate_str(int state)
00063 {
00064    const char *res = "UNKNOWN";
00065 
00066    switch (state) {
00067    case AST_EXTENSION_NOT_INUSE:
00068       res = "NOT_INUSE";
00069       break;
00070    case AST_EXTENSION_INUSE:
00071       res = "INUSE";
00072       break;
00073    case AST_EXTENSION_BUSY:
00074       res = "BUSY";
00075       break;
00076    case AST_EXTENSION_UNAVAILABLE:
00077       res = "UNAVAILABLE";
00078       break;
00079    case AST_EXTENSION_RINGING:
00080       res = "RINGING";
00081       break;
00082    case AST_EXTENSION_INUSE | AST_EXTENSION_RINGING:
00083       res = "RINGINUSE";
00084       break;
00085    case AST_EXTENSION_INUSE | AST_EXTENSION_ONHOLD:
00086       res = "HOLDINUSE";
00087       break;
00088    case AST_EXTENSION_ONHOLD:
00089       res = "ONHOLD";
00090       break;
00091    }
00092 
00093    return res;
00094 }
00095 
00096 static int extstate_read(struct ast_channel *chan, const char *cmd, char *data,
00097    char *buf, size_t len)
00098 {
00099    char *exten, *context;
00100 
00101    if (ast_strlen_zero(data)) {
00102       ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
00103       return -1;
00104    }
00105 
00106    context = exten = data;
00107    strsep(&context, "@");
00108    if (ast_strlen_zero(context))
00109       context = "default";
00110 
00111    if (ast_strlen_zero(exten)) {
00112       ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
00113       return -1;
00114    }
00115 
00116    ast_copy_string(buf, 
00117       ast_extstate_str(ast_extension_state(chan, context, exten)), len);
00118 
00119    return 0;
00120 }
00121 
00122 static struct ast_custom_function extstate_function = {
00123    .name = "EXTENSION_STATE",
00124    .read = extstate_read,
00125    .read_max = 12,
00126 };
00127 
00128 static int unload_module(void)
00129 {
00130    int res;
00131 
00132    res = ast_custom_function_unregister(&extstate_function);
00133 
00134    return res;
00135 }
00136 
00137 static int load_module(void)
00138 {
00139    int res;
00140 
00141    res = ast_custom_function_register(&extstate_function);
00142 
00143    return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
00144 }
00145 
00146 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets an extension's state in the dialplan");

Generated on Wed Apr 6 11:29:45 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7