Debian Codegraph @

funcs/func_extstate.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2007, Digium, Inc.
5 *
6 * Modified from func_devstate.c by Russell Bryant <[email protected]>
7 * Adam Gundy <[email protected]>
8
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
14 *
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
18 */
19
20 /*! \file
21 *
22 * \brief Get the state of a hinted extension for dialplan control
23 *
24 * \author Adam Gundy <[email protected]>
25 *
26 * \ingroup functions
27 */
28
29 /*** MODULEINFO
30 <support_level>core</support_level>
31 ***/
32
33 #include "asterisk.h"
34
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/devicestate.h"
40
41 /*** DOCUMENTATION
42 <function name="EXTENSION_STATE" language="en_US">
43 <since>
44 <version>1.6.0</version>
45 </since>
46 <synopsis>
47 Get an extension's state.
48 </synopsis>
49 <syntax argsep="@">
50 <parameter name="extension" required="true" />
51 <parameter name="context">
52 <para>If it is not specified defaults to <literal>default</literal>.</para>
53 </parameter>
54 </syntax>
55 <description>
56 <para>The EXTENSION_STATE function can be used to retrieve the state from any
57 hinted extension. For example:</para>
58 <para>NoOp(1234@default has state ${EXTENSION_STATE(1234)})</para>
59 <para>NoOp(4567@home has state ${EXTENSION_STATE(4567@home)})</para>
60 <para>The possible values returned by this function are:</para>
61 <para>UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING |
62 RINGINUSE | HOLDINUSE | ONHOLD</para>
63 </description>
64 </function>
65 ***/
66
67 static const char *ast_extstate_str(int state)
68 {
69 const char *res = "UNKNOWN";
70
71 switch (state) {
72 case AST_EXTENSION_NOT_INUSE:
73 res = "NOT_INUSE";
74 break;
75 case AST_EXTENSION_INUSE:
76 res = "INUSE";
77 break;
78 case AST_EXTENSION_BUSY:
79 res = "BUSY";
80 break;
81 case AST_EXTENSION_UNAVAILABLE:
82 res = "UNAVAILABLE";
83 break;
84 case AST_EXTENSION_RINGING:
85 res = "RINGING";
86 break;
87 case AST_EXTENSION_INUSE | AST_EXTENSION_RINGING:
88 res = "RINGINUSE";
89 break;
90 case AST_EXTENSION_INUSE | AST_EXTENSION_ONHOLD:
91 res = "HOLDINUSE";
92 break;
93 case AST_EXTENSION_ONHOLD:
94 res = "ONHOLD";
95 break;
96 }
97
98 return res;
99 }
100
101 static int extstate_read(struct ast_channel *chan, const char *cmd, char *data,
102 char *buf, size_t len)
103 {
104 char *exten, *context;
105
106 if (ast_strlen_zero(data)) {
107 ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
108 return -1;
109 }
110
111 context = exten = data;
112 strsep(&context, "@");
113 if (ast_strlen_zero(context))
114 context = "default";
115
116 if (ast_strlen_zero(exten)) {
117 ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
118 return -1;
119 }
120
121 ast_copy_string(buf,
122 ast_extstate_str(ast_extension_state(chan, context, exten)), len);
123
124 return 0;
125 }
126
127 static struct ast_custom_function extstate_function = {
128 .name = "EXTENSION_STATE",
129 .read = extstate_read,
130 .read_max = 12,
131 };
132
133 static int unload_module(void)
134 {
135 int res;
136
137 res = ast_custom_function_unregister(&extstate_function);
138
139 return res;
140 }
141
142 static int load_module(void)
143 {
144 int res;
145
146 res = ast_custom_function_register(&extstate_function);
147
148 return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
149 }
150
151 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets an extension's state in the dialplan");