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 /*** MODULEINFO
00030    <support_level>core</support_level>
00031  ***/
00032 
00033 #include "asterisk.h"
00034 
00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328259 $")
00036 
00037 #include "asterisk/module.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/utils.h"
00041 #include "asterisk/devicestate.h"
00042 
00043 /*** DOCUMENTATION
00044    <function name="EXTENSION_STATE" language="en_US">
00045       <synopsis>
00046          Get an extension's state.
00047       </synopsis> 
00048       <syntax argsep="@">
00049          <parameter name="extension" required="true" />
00050          <parameter name="context">
00051             <para>If it is not specified defaults to <literal>default</literal>.</para>
00052          </parameter>
00053       </syntax>
00054       <description>
00055          <para>The EXTENSION_STATE function can be used to retrieve the state from any
00056          hinted extension. For example:</para>
00057          <para>NoOp(1234@default has state ${EXTENSION_STATE(1234)})</para>
00058          <para>NoOp(4567@home has state ${EXTENSION_STATE(4567@home)})</para>
00059          <para>The possible values returned by this function are:</para>
00060          <para>UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING |
00061          RINGINUSE | HOLDINUSE | ONHOLD</para>
00062       </description>
00063    </function>
00064  ***/
00065 
00066 static const char *ast_extstate_str(int state)
00067 {
00068    const char *res = "UNKNOWN";
00069 
00070    switch (state) {
00071    case AST_EXTENSION_NOT_INUSE:
00072       res = "NOT_INUSE";
00073       break;
00074    case AST_EXTENSION_INUSE:
00075       res = "INUSE";
00076       break;
00077    case AST_EXTENSION_BUSY:
00078       res = "BUSY";
00079       break;
00080    case AST_EXTENSION_UNAVAILABLE:
00081       res = "UNAVAILABLE";
00082       break;
00083    case AST_EXTENSION_RINGING:
00084       res = "RINGING";
00085       break;
00086    case AST_EXTENSION_INUSE | AST_EXTENSION_RINGING:
00087       res = "RINGINUSE";
00088       break;
00089    case AST_EXTENSION_INUSE | AST_EXTENSION_ONHOLD:
00090       res = "HOLDINUSE";
00091       break;
00092    case AST_EXTENSION_ONHOLD:
00093       res = "ONHOLD";
00094       break;
00095    }
00096 
00097    return res;
00098 }
00099 
00100 static int extstate_read(struct ast_channel *chan, const char *cmd, char *data,
00101    char *buf, size_t len)
00102 {
00103    char *exten, *context;
00104 
00105    if (ast_strlen_zero(data)) {
00106       ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
00107       return -1;
00108    }
00109 
00110    context = exten = data;
00111    strsep(&context, "@");
00112    if (ast_strlen_zero(context))
00113       context = "default";
00114 
00115    if (ast_strlen_zero(exten)) {
00116       ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
00117       return -1;
00118    }
00119 
00120    ast_copy_string(buf, 
00121       ast_extstate_str(ast_extension_state(chan, context, exten)), len);
00122 
00123    return 0;
00124 }
00125 
00126 static struct ast_custom_function extstate_function = {
00127    .name = "EXTENSION_STATE",
00128    .read = extstate_read,
00129    .read_max = 12,
00130 };
00131 
00132 static int unload_module(void)
00133 {
00134    int res;
00135 
00136    res = ast_custom_function_unregister(&extstate_function);
00137 
00138    return res;
00139 }
00140 
00141 static int load_module(void)
00142 {
00143    int res;
00144 
00145    res = ast_custom_function_register(&extstate_function);
00146 
00147    return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
00148 }
00149 
00150 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets an extension's state in the dialplan");

Generated on Thu Apr 16 06:27:35 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6