Add a function, CHANNELS(), which retrieves a list of all active channels.
[asterisk-bristuff.git] / apps / app_milliwatt.c
blob0fcd72151ca0a58f210e083b39b467cb8b58557b
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@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.
19 /*! \file
21 * \brief Digital Milliwatt Test
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 /*** MODULEINFO
29 <depend>res_indications</depend>
30 ***/
32 #include "asterisk.h"
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/module.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
40 static char *app = "Milliwatt";
42 static char *synopsis = "Generate a Constant 1004Hz tone at 0dbm (mu-law)";
44 static char *descrip =
45 " Milliwatt([options]): Generate a Constant 1004Hz tone at 0dbm.\n"
46 "Previous versions of this application generated the tone at 1000Hz. If for\n"
47 "some reason you would prefer that behavior, supply the 'o' option to get the\n"
48 "old behavior.\n"
49 "";
51 static char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
53 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
55 return ast_calloc(1, sizeof(int));
58 static void milliwatt_release(struct ast_channel *chan, void *data)
60 ast_free(data);
61 return;
64 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
66 unsigned char buf[AST_FRIENDLY_OFFSET + 640];
67 const int maxsamples = sizeof (buf) / sizeof (buf[0]);
68 int i, *indexp = (int *) data;
69 struct ast_frame wf = {
70 .frametype = AST_FRAME_VOICE,
71 .subclass = AST_FORMAT_ULAW,
72 .offset = AST_FRIENDLY_OFFSET,
73 .src = __FUNCTION__,
75 wf.data.ptr = buf + AST_FRIENDLY_OFFSET;
77 /* Instead of len, use samples, because channel.c generator_force
78 * generate(chan, tmp, 0, 160) ignores len. In any case, len is
79 * a multiple of samples, given by number of samples times bytes per
80 * sample. In the case of ulaw, len = samples. for signed linear
81 * len = 2 * samples */
82 if (samples > maxsamples) {
83 ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", maxsamples, samples);
84 samples = maxsamples;
87 len = samples * sizeof (buf[0]);
88 wf.datalen = len;
89 wf.samples = samples;
91 /* create a buffer containing the digital milliwatt pattern */
92 for (i = 0; i < len; i++) {
93 buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++];
94 *indexp &= 7;
97 if (ast_write(chan,&wf) < 0) {
98 ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",chan->name,strerror(errno));
99 return -1;
102 return 0;
105 static struct ast_generator milliwattgen = {
106 alloc: milliwatt_alloc,
107 release: milliwatt_release,
108 generate: milliwatt_generate,
111 static int old_milliwatt_exec(struct ast_channel *chan)
113 ast_set_write_format(chan, AST_FORMAT_ULAW);
114 ast_set_read_format(chan, AST_FORMAT_ULAW);
116 if (chan->_state != AST_STATE_UP) {
117 ast_answer(chan);
120 if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) {
121 ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",chan->name);
122 return -1;
125 while (!ast_safe_sleep(chan, 10000))
128 ast_deactivate_generator(chan);
130 return -1;
133 static int milliwatt_exec(struct ast_channel *chan, void *data)
135 const char *options = data;
136 struct ast_app *playtones_app;
137 int res = -1;
139 if (!ast_strlen_zero(options) && strchr(options, 'o')) {
140 return old_milliwatt_exec(chan);
143 if (!(playtones_app = pbx_findapp("Playtones"))) {
144 ast_log(LOG_ERROR, "The Playtones application is required to run Milliwatt()\n");
145 return -1;
148 res = pbx_exec(chan, playtones_app, "1004/1000");
150 while (!res) {
151 res = ast_safe_sleep(chan, 10000);
154 return res;
157 static int unload_module(void)
159 return ast_unregister_application(app);
162 static int load_module(void)
164 return ast_register_application(app, milliwatt_exec, synopsis, descrip);
167 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application");