Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


func_extstate.c
Go to the documentation of this file.
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 <russell@digium.com>
7  * Adam Gundy <adam@starsilk.net>
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 <adam@starsilk.net>
25  *
26  * \ingroup functions
27  */
28 
29 /*** MODULEINFO
30  <support_level>core</support_level>
31  ***/
32 
33 #include "asterisk.h"
34 
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
36 
37 #include "asterisk/module.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/devicestate.h"
42 
43 /*** DOCUMENTATION
44  <function name="EXTENSION_STATE" language="en_US">
45  <synopsis>
46  Get an extension's state.
47  </synopsis>
48  <syntax argsep="@">
49  <parameter name="extension" required="true" />
50  <parameter name="context">
51  <para>If it is not specified defaults to <literal>default</literal>.</para>
52  </parameter>
53  </syntax>
54  <description>
55  <para>The EXTENSION_STATE function can be used to retrieve the state from any
56  hinted extension. For example:</para>
57  <para>NoOp(1234@default has state ${EXTENSION_STATE(1234)})</para>
58  <para>NoOp(4567@home has state ${EXTENSION_STATE(4567@home)})</para>
59  <para>The possible values returned by this function are:</para>
60  <para>UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING |
61  RINGINUSE | HOLDINUSE | ONHOLD</para>
62  </description>
63  </function>
64  ***/
65 
66 static const char *ast_extstate_str(int state)
67 {
68  const char *res = "UNKNOWN";
69 
70  switch (state) {
72  res = "NOT_INUSE";
73  break;
75  res = "INUSE";
76  break;
77  case AST_EXTENSION_BUSY:
78  res = "BUSY";
79  break;
81  res = "UNAVAILABLE";
82  break;
84  res = "RINGING";
85  break;
87  res = "RINGINUSE";
88  break;
90  res = "HOLDINUSE";
91  break;
93  res = "ONHOLD";
94  break;
95  }
96 
97  return res;
98 }
99 
100 static int extstate_read(struct ast_channel *chan, const char *cmd, char *data,
101  char *buf, size_t len)
102 {
103  char *exten, *context;
104 
105  if (ast_strlen_zero(data)) {
106  ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
107  return -1;
108  }
109 
110  context = exten = data;
111  strsep(&context, "@");
112  if (ast_strlen_zero(context))
113  context = "default";
114 
115  if (ast_strlen_zero(exten)) {
116  ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
117  return -1;
118  }
119 
120  ast_copy_string(buf,
121  ast_extstate_str(ast_extension_state(chan, context, exten)), len);
122 
123  return 0;
124 }
125 
127  .name = "EXTENSION_STATE",
128  .read = extstate_read,
129  .read_max = 12,
130 };
131 
132 static int unload_module(void)
133 {
134  int res;
135 
136  res = ast_custom_function_unregister(&extstate_function);
137 
138  return res;
139 }
140 
141 static int load_module(void)
142 {
143  int res;
144 
145  res = ast_custom_function_register(&extstate_function);
146 
148 }
149 
150 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets an extension's state in the dialplan");
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:109
Main Channel structure associated with a channel.
Definition: channel.h:742
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Asterisk main include file. File version handling, generic pbx functions.
char * strsep(char **str, const char *delims)
Device state management.
#define LOG_WARNING
Definition: logger.h:144
static int load_module(void)
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
Utility functions.
static const char * ast_extstate_str(int state)
Definition: func_extstate.c:66
General Asterisk PBX channel definitions.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
Data structure associated with a custom dialplan function.
Definition: pbx.h:95
static int extstate_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Core PBX routines and definitions.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
int ast_extension_state(struct ast_channel *c, const char *context, const char *exten)
Uses hint and devicestate callback to get the state of an extension.
Definition: pbx.c:4914
static int unload_module(void)
const char * name
Definition: pbx.h:96
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:107
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
static struct ast_custom_function extstate_function
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180