2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Manually controlled blinky lights
23 * \author Russell Bryant <russell@digium.com>
27 * \note Props go out to Ahrimanes in \#asterisk for requesting this at 4:30 AM
28 * when I couldn't sleep. :)
33 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/linkedlists.h"
40 #include "asterisk/devicestate.h"
41 #include "asterisk/cli.h"
42 #include "asterisk/astdb.h"
43 #include "asterisk/app.h"
45 static const char astdb_family
[] = "CustomDevstate";
47 static int devstate_read(struct ast_channel
*chan
, const char *cmd
, char *data
, char *buf
, size_t len
)
49 ast_copy_string(buf
, ast_devstate_str(ast_device_state(data
)), len
);
54 static int devstate_write(struct ast_channel
*chan
, const char *cmd
, char *data
, const char *value
)
56 size_t len
= strlen("Custom:");
57 enum ast_device_state state_val
;
59 if (strncasecmp(data
, "Custom:", len
)) {
60 ast_log(LOG_WARNING
, "The DEVICE_STATE function can only be used to set 'Custom:' device state!\n");
64 if (ast_strlen_zero(data
)) {
65 ast_log(LOG_WARNING
, "DEVICE_STATE function called with no custom device name!\n");
69 state_val
= ast_devstate_val(value
);
71 if (state_val
== AST_DEVICE_UNKNOWN
) {
72 ast_log(LOG_ERROR
, "DEVICE_STATE function given invalid state value '%s'\n", value
);
76 ast_db_put(astdb_family
, data
, value
);
78 ast_devstate_changed(state_val
, "Custom:%s", data
);
84 HINT_OPT_NAME
= (1 << 0),
87 AST_APP_OPTIONS(hint_options
, BEGIN_OPTIONS
88 AST_APP_OPTION('n', HINT_OPT_NAME
),
91 static int hint_read(struct ast_channel
*chan
, const char *cmd
, char *data
, char *buf
, size_t len
)
93 char *exten
, *context
;
94 AST_DECLARE_APP_ARGS(args
,
98 struct ast_flags opts
= { 0, };
101 if (ast_strlen_zero(data
)) {
102 ast_log(LOG_WARNING
, "The HINT function requires an extension\n");
106 AST_STANDARD_APP_ARGS(args
, data
);
108 if (ast_strlen_zero(args
.exten
)) {
109 ast_log(LOG_WARNING
, "The HINT function requires an extension\n");
113 context
= exten
= args
.exten
;
114 strsep(&context
, "@");
115 if (ast_strlen_zero(context
))
118 if (!ast_strlen_zero(args
.options
))
119 ast_app_parse_options(hint_options
, &opts
, NULL
, args
.options
);
121 if (ast_test_flag(&opts
, HINT_OPT_NAME
))
122 res
= ast_get_hint(NULL
, 0, buf
, len
, chan
, context
, exten
);
124 res
= ast_get_hint(buf
, len
, NULL
, 0, chan
, context
, exten
);
126 return !res
; /* ast_get_hint returns non-zero on success */
129 static enum ast_device_state
custom_devstate_callback(const char *data
)
133 ast_db_get(astdb_family
, data
, buf
, sizeof(buf
));
135 return ast_devstate_val(buf
);
138 static char *handle_cli_funcdevstate_list(struct ast_cli_entry
*e
, int cmd
, struct ast_cli_args
*a
)
140 struct ast_db_entry
*db_entry
, *db_tree
;
144 e
->command
= "funcdevstate list";
146 "Usage: funcdevstate list\n"
147 " List all custom device states that have been set by using\n"
148 " the DEVICE_STATE dialplan function.\n";
154 if (a
->argc
!= e
->args
)
155 return CLI_SHOWUSAGE
;
158 "---------------------------------------------------------------------\n"
159 "--- Custom Device States --------------------------------------------\n"
160 "---------------------------------------------------------------------\n"
163 db_entry
= db_tree
= ast_db_gettree(astdb_family
, NULL
);
164 for (; db_entry
; db_entry
= db_entry
->next
) {
165 const char *dev_name
= strrchr(db_entry
->key
, '/') + 1;
166 if (dev_name
<= (const char *) 1)
168 ast_cli(a
->fd
, "--- Name: 'Custom:%s' State: '%s'\n"
169 "---\n", dev_name
, db_entry
->data
);
171 ast_db_freetree(db_tree
);
175 "---------------------------------------------------------------------\n"
176 "---------------------------------------------------------------------\n"
182 static char *handle_cli_devstate_list(struct ast_cli_entry
*e
, int cmd
, struct ast_cli_args
*a
)
184 struct ast_db_entry
*db_entry
, *db_tree
;
188 e
->command
= "devstate list";
190 "Usage: devstate list\n"
191 " List all custom device states that have been set by using\n"
192 " the DEVICE_STATE dialplan function.\n";
198 if (a
->argc
!= e
->args
)
199 return CLI_SHOWUSAGE
;
202 "---------------------------------------------------------------------\n"
203 "--- Custom Device States --------------------------------------------\n"
204 "---------------------------------------------------------------------\n"
207 db_entry
= db_tree
= ast_db_gettree(astdb_family
, NULL
);
208 for (; db_entry
; db_entry
= db_entry
->next
) {
209 const char *dev_name
= strrchr(db_entry
->key
, '/') + 1;
210 if (dev_name
<= (const char *) 1)
212 ast_cli(a
->fd
, "--- Name: 'Custom:%s' State: '%s'\n"
213 "---\n", dev_name
, db_entry
->data
);
215 ast_db_freetree(db_tree
);
219 "---------------------------------------------------------------------\n"
220 "---------------------------------------------------------------------\n"
226 static char *handle_cli_devstate_change(struct ast_cli_entry
*e
, int cmd
, struct ast_cli_args
*a
)
229 const char *dev
, *state
;
230 enum ast_device_state state_val
;
234 e
->command
= "devstate change";
236 "Usage: devstate change <device> <state>\n"
237 " Change a custom device to a new state.\n"
238 " The possible values for the state are:\n"
239 "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
240 "RINGINUSE | ONHOLD\n",
243 " devstate change Custom:mystate1 INUSE\n"
244 " devstate change Custom:mystate1 NOT_INUSE\n"
249 static char * const cmds
[] = { "UNKNOWN", "NOT_INUSE", "INUSE", "BUSY",
250 "UNAVAILALBE", "RINGING", "RINGINUSE", "ONHOLD", NULL
};
252 if (a
->pos
== e
->args
+ 1)
253 return ast_cli_complete(a
->word
, cmds
, a
->n
);
259 if (a
->argc
!= e
->args
+ 2)
260 return CLI_SHOWUSAGE
;
262 len
= strlen("Custom:");
263 dev
= a
->argv
[e
->args
];
264 state
= a
->argv
[e
->args
+ 1];
266 if (strncasecmp(dev
, "Custom:", len
)) {
267 ast_cli(a
->fd
, "The devstate command can only be used to set 'Custom:' device state!\n");
272 if (ast_strlen_zero(dev
))
273 return CLI_SHOWUSAGE
;
275 state_val
= ast_devstate_val(state
);
277 if (state_val
== AST_DEVICE_UNKNOWN
)
278 return CLI_SHOWUSAGE
;
280 ast_cli(a
->fd
, "Changing %s to %s\n", dev
, state
);
282 ast_db_put(astdb_family
, dev
, state
);
284 ast_devstate_changed(state_val
, "Custom:%s", dev
);
289 static struct ast_cli_entry cli_funcdevstate_list_deprecated
= AST_CLI_DEFINE(handle_cli_funcdevstate_list
, "List currently known custom device states");
290 static struct ast_cli_entry cli_funcdevstate
[] = {
291 AST_CLI_DEFINE(handle_cli_devstate_list
, "List currently known custom device states", .deprecate_cmd
= &cli_funcdevstate_list_deprecated
),
292 AST_CLI_DEFINE(handle_cli_devstate_change
, "Change a custom device state"),
295 static struct ast_custom_function devstate_function
= {
296 .name
= "DEVICE_STATE",
297 .synopsis
= "Get or Set a device state",
298 .syntax
= "DEVICE_STATE(device)",
300 " The DEVICE_STATE function can be used to retrieve the device state from any\n"
301 "device state provider. For example:\n"
302 " NoOp(SIP/mypeer has state ${DEVICE_STATE(SIP/mypeer)})\n"
303 " NoOp(Conference number 1234 has state ${DEVICE_STATE(MeetMe:1234)})\n"
305 " The DEVICE_STATE function can also be used to set custom device state from\n"
306 "the dialplan. The \"Custom:\" prefix must be used. For example:\n"
307 " Set(DEVICE_STATE(Custom:lamp1)=BUSY)\n"
308 " Set(DEVICE_STATE(Custom:lamp2)=NOT_INUSE)\n"
309 "You can subscribe to the status of a custom device state using a hint in\n"
311 " exten => 1234,hint,Custom:lamp1\n"
313 " The possible values for both uses of this function are:\n"
314 "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
315 "RINGINUSE | ONHOLD\n",
316 .read
= devstate_read
,
317 .write
= devstate_write
,
320 static struct ast_custom_function hint_function
= {
322 .synopsis
= "Get the devices set for a dialplan hint",
323 .syntax
= "HINT(extension[@context][|options])",
325 " The HINT function can be used to retrieve the list of devices that are\n"
326 "mapped to a dialplan hint. For example:\n"
327 " NoOp(Hint for Extension 1234 is ${HINT(1234)})\n"
329 " 'n' - Retrieve name on the hint instead of list of devices\n"
334 static int unload_module(void)
338 res
|= ast_custom_function_unregister(&devstate_function
);
339 res
|= ast_custom_function_unregister(&hint_function
);
340 res
|= ast_devstate_prov_del("Custom");
341 res
|= ast_cli_unregister_multiple(cli_funcdevstate
, ARRAY_LEN(cli_funcdevstate
));
346 static int load_module(void)
349 struct ast_db_entry
*db_entry
, *db_tree
;
351 /* Populate the device state cache on the system with all of the currently
352 * known custom device states. */
353 db_entry
= db_tree
= ast_db_gettree(astdb_family
, NULL
);
354 for (; db_entry
; db_entry
= db_entry
->next
) {
355 const char *dev_name
= strrchr(db_entry
->key
, '/') + 1;
356 if (dev_name
<= (const char *) 1)
358 ast_devstate_changed(ast_devstate_val(db_entry
->data
),
359 "Custom:%s\n", dev_name
);
361 ast_db_freetree(db_tree
);
364 res
|= ast_custom_function_register(&devstate_function
);
365 res
|= ast_custom_function_register(&hint_function
);
366 res
|= ast_devstate_prov_add("Custom", custom_devstate_callback
);
367 res
|= ast_cli_register_multiple(cli_funcdevstate
, ARRAY_LEN(cli_funcdevstate
));
372 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Gets or sets a device state in the dialplan");