File: func_devstate.c

package info (click to toggle)
asterisk 1:1.6.2.9-2+squeeze12
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 67,296 kB
  • ctags: 65,026
  • sloc: ansic: 327,660; sh: 11,153; cpp: 5,940; perl: 3,078; makefile: 2,594; yacc: 2,140; asm: 642; xml: 309; sql: 290; tcl: 113; php: 62
file content (352 lines) | stat: -rw-r--r-- 10,106 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2007, Digium, Inc.
 *
 * Russell Bryant <russell@digium.com> 
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Manually controlled blinky lights
 *
 * \author Russell Bryant <russell@digium.com> 
 *
 * \ingroup functions
 *
 * \todo Delete the entry from AstDB when set to nothing like Set(DEVICE_STATE(Custom:lamp1)=)
 *
 * \note Props go out to Ahrimanes in \#asterisk for requesting this at 4:30 AM
 *       when I couldn't sleep.  :)
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision: 193336 $")

#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/linkedlists.h"
#include "asterisk/devicestate.h"
#include "asterisk/cli.h"
#include "asterisk/astdb.h"
#include "asterisk/app.h"

/*** DOCUMENTATION
	<function name="DEVICE_STATE" language="en_US">
		<synopsis>
			Get or Set a device state.
		</synopsis>
		<syntax>
			<parameter name="device" required="true" />
		</syntax>
		<description>
			<para>The DEVICE_STATE function can be used to retrieve the device state from any
			device state provider. For example:</para>
			<para>NoOp(SIP/mypeer has state ${DEVICE_STATE(SIP/mypeer)})</para>
			<para>NoOp(Conference number 1234 has state ${DEVICE_STATE(MeetMe:1234)})</para>
			<para>The DEVICE_STATE function can also be used to set custom device state from
			the dialplan.  The <literal>Custom:</literal> prefix must be used. For example:</para>
			<para>Set(DEVICE_STATE(Custom:lamp1)=BUSY)</para>
			<para>Set(DEVICE_STATE(Custom:lamp2)=NOT_INUSE)</para>
			<para>You can subscribe to the status of a custom device state using a hint in
			the dialplan:</para>
			<para>exten => 1234,hint,Custom:lamp1</para>
			<para>The possible values for both uses of this function are:</para>
			<para>UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING |
			RINGINUSE | ONHOLD</para>
		</description>
	</function>
	<function name="HINT" language="en_US">
		<synopsis>
			Get the devices set for a dialplan hint.
		</synopsis>
		<syntax>
			<parameter name="extension" required="true" argsep="@">
				<argument name="extension" required="true" />
				<argument name="context" />
			</parameter>
			<parameter name="options">
				<optionlist>
					<option name="n">
						<para>Retrieve name on the hint instead of list of devices.</para>
					</option>
				</optionlist>
			</parameter>
		</syntax>
		<description>
			<para>The HINT function can be used to retrieve the list of devices that are
			mapped to a dialplan hint. For example:</para>
			<para>NoOp(Hint for Extension 1234 is ${HINT(1234)})</para>
		</description>
	</function>
 ***/


static const char astdb_family[] = "CustomDevstate";

static int devstate_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
	ast_copy_string(buf, ast_devstate_str(ast_device_state(data)), len);

	return 0;
}

static int devstate_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
{
	size_t len = strlen("Custom:");
	enum ast_device_state state_val;

	if (strncasecmp(data, "Custom:", len)) {
		ast_log(LOG_WARNING, "The DEVICE_STATE function can only be used to set 'Custom:' device state!\n");
		return -1;
	}
	data += len;
	if (ast_strlen_zero(data)) {
		ast_log(LOG_WARNING, "DEVICE_STATE function called with no custom device name!\n");
		return -1;
	}

	state_val = ast_devstate_val(value);

	if (state_val == AST_DEVICE_UNKNOWN) {
		ast_log(LOG_ERROR, "DEVICE_STATE function given invalid state value '%s'\n", value);
		return -1;
	}

	ast_db_put(astdb_family, data, value);

	ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", data);

	return 0;
}

enum {
	HINT_OPT_NAME = (1 << 0),
};

AST_APP_OPTIONS(hint_options, BEGIN_OPTIONS
	AST_APP_OPTION('n', HINT_OPT_NAME),
END_OPTIONS );

static int hint_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
	char *exten, *context;
	AST_DECLARE_APP_ARGS(args,
		AST_APP_ARG(exten);
		AST_APP_ARG(options);
	);
	struct ast_flags opts = { 0, };
	int res;

	if (ast_strlen_zero(data)) {
		ast_log(LOG_WARNING, "The HINT function requires an extension\n");
		return -1;
	}

	AST_STANDARD_APP_ARGS(args, data);

	if (ast_strlen_zero(args.exten)) {
		ast_log(LOG_WARNING, "The HINT function requires an extension\n");
		return -1;
	}

	context = exten = args.exten;
	strsep(&context, "@");
	if (ast_strlen_zero(context))
		context = "default";

	if (!ast_strlen_zero(args.options))
		ast_app_parse_options(hint_options, &opts, NULL, args.options);

	if (ast_test_flag(&opts, HINT_OPT_NAME))
		res = ast_get_hint(NULL, 0, buf, len, chan, context, exten);
	else
		res = ast_get_hint(buf, len, NULL, 0, chan, context, exten);

	return !res; /* ast_get_hint returns non-zero on success */
}

static enum ast_device_state custom_devstate_callback(const char *data)
{
	char buf[256] = "";

	ast_db_get(astdb_family, data, buf, sizeof(buf));

	return ast_devstate_val(buf);
}

static char *handle_cli_devstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	struct ast_db_entry *db_entry, *db_tree;

	switch (cmd) {
	case CLI_INIT:
		e->command = "devstate list";
		e->usage =
			"Usage: devstate list\n"
			"       List all custom device states that have been set by using\n"
			"       the DEVICE_STATE dialplan function.\n";
		return NULL;
	case CLI_GENERATE:
		return NULL;
	}

	if (a->argc != e->args)
		return CLI_SHOWUSAGE;

	ast_cli(a->fd, "\n"
	        "---------------------------------------------------------------------\n"
	        "--- Custom Device States --------------------------------------------\n"
	        "---------------------------------------------------------------------\n"
	        "---\n");

	db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
	for (; db_entry; db_entry = db_entry->next) {
		const char *dev_name = strrchr(db_entry->key, '/') + 1;
		if (dev_name <= (const char *) 1)
			continue;
		ast_cli(a->fd, "--- Name: 'Custom:%s'  State: '%s'\n"
		               "---\n", dev_name, db_entry->data);
	}
	ast_db_freetree(db_tree);
	db_tree = NULL;

	ast_cli(a->fd,
	        "---------------------------------------------------------------------\n"
	        "---------------------------------------------------------------------\n"
	        "\n");

	return CLI_SUCCESS;
}

static char *handle_cli_devstate_change(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
    size_t len;
	const char *dev, *state;
	enum ast_device_state state_val;

	switch (cmd) {
	case CLI_INIT:
		e->command = "devstate change";
		e->usage =
			"Usage: devstate change <device> <state>\n"
			"       Change a custom device to a new state.\n"
			"       The possible values for the state are:\n"
			"UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
			"RINGINUSE | ONHOLD\n",
			"\n"
			"Examples:\n"
			"       devstate change Custom:mystate1 INUSE\n"
			"       devstate change Custom:mystate1 NOT_INUSE\n"
			"       \n";
		return NULL;
	case CLI_GENERATE:
	{
		static char * const cmds[] = { "UNKNOWN", "NOT_INUSE", "INUSE", "BUSY",
			"UNAVAILABLE", "RINGING", "RINGINUSE", "ONHOLD", NULL };

		if (a->pos == e->args + 1)
			return ast_cli_complete(a->word, cmds, a->n);

		return NULL;
	}
	}

	if (a->argc != e->args + 2)
		return CLI_SHOWUSAGE;

	len = strlen("Custom:");
	dev = a->argv[e->args];
	state = a->argv[e->args + 1];

	if (strncasecmp(dev, "Custom:", len)) {
		ast_cli(a->fd, "The devstate command can only be used to set 'Custom:' device state!\n");
		return CLI_FAILURE;
	}

	dev += len;
	if (ast_strlen_zero(dev))
		return CLI_SHOWUSAGE;

	state_val = ast_devstate_val(state);

	if (state_val == AST_DEVICE_UNKNOWN)
		return CLI_SHOWUSAGE;

	ast_cli(a->fd, "Changing %s to %s\n", dev, state);

	ast_db_put(astdb_family, dev, state);

	ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", dev);

	return CLI_SUCCESS;
}

static struct ast_cli_entry cli_funcdevstate[] = {
	AST_CLI_DEFINE(handle_cli_devstate_list, "List currently known custom device states"),
	AST_CLI_DEFINE(handle_cli_devstate_change, "Change a custom device state"),
};

static struct ast_custom_function devstate_function = {
	.name = "DEVICE_STATE",
	.read = devstate_read,
	.write = devstate_write,
};

static struct ast_custom_function hint_function = {
	.name = "HINT",
	.read = hint_read,
};

static int unload_module(void)
{
	int res = 0;

	res |= ast_custom_function_unregister(&devstate_function);
	res |= ast_custom_function_unregister(&hint_function);
	res |= ast_devstate_prov_del("Custom");
	res |= ast_cli_unregister_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));

	return res;
}

static int load_module(void)
{
	int res = 0;
	struct ast_db_entry *db_entry, *db_tree;

	/* Populate the device state cache on the system with all of the currently
	 * known custom device states. */
	db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
	for (; db_entry; db_entry = db_entry->next) {
		const char *dev_name = strrchr(db_entry->key, '/') + 1;
		if (dev_name <= (const char *) 1)
			continue;
		ast_devstate_changed(ast_devstate_val(db_entry->data),
			AST_DEVSTATE_CACHABLE, "Custom:%s\n", dev_name);
	}
	ast_db_freetree(db_tree);
	db_tree = NULL;

	res |= ast_custom_function_register(&devstate_function);
	res |= ast_custom_function_register(&hint_function);
	res |= ast_devstate_prov_add("Custom", custom_devstate_callback);
	res |= ast_cli_register_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));

	return res;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets or sets a device state in the dialplan");