Stop ignoring .lastclean
[asterisk-bristuff.git] / apps / app_milliwatt.c
blobb63771c380e02fbb214c4d2be905196eff8fbb50
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 <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <errno.h>
42 #include "asterisk/lock.h"
43 #include "asterisk/file.h"
44 #include "asterisk/logger.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/module.h"
48 #include "asterisk/utils.h"
50 static char *app = "Milliwatt";
52 static char *synopsis = "Generate a Constant 1004Hz tone at 0dbm (mu-law)";
54 static char *descrip =
55 " Milliwatt([options]): Generate a Constant 1004Hz tone at 0dbm.\n"
56 "Previous versions of this application generated the tone at 1000Hz. If for\n"
57 "some reason you would prefer that behavior, supply the 'o' option to get the\n"
58 "old behavior.\n"
59 "";
62 static char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
64 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
66 return ast_calloc(1, sizeof(int));
69 static void milliwatt_release(struct ast_channel *chan, void *data)
71 free(data);
72 return;
75 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
77 unsigned char buf[AST_FRIENDLY_OFFSET + 640];
78 const int maxsamples = sizeof (buf) / sizeof (buf[0]);
79 int i, *indexp = (int *) data;
80 struct ast_frame wf = {
81 .frametype = AST_FRAME_VOICE,
82 .subclass = AST_FORMAT_ULAW,
83 .offset = AST_FRIENDLY_OFFSET,
84 .data = buf + AST_FRIENDLY_OFFSET,
85 .src = __FUNCTION__,
88 /* Instead of len, use samples, because channel.c generator_force
89 * generate(chan, tmp, 0, 160) ignores len. In any case, len is
90 * a multiple of samples, given by number of samples times bytes per
91 * sample. In the case of ulaw, len = samples. for signed linear
92 * len = 2 * samples */
94 if (samples > maxsamples) {
95 ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", maxsamples, samples);
96 samples = maxsamples;
99 len = samples * sizeof (buf[0]);
100 wf.datalen = len;
101 wf.samples = samples;
103 /* create a buffer containing the digital milliwatt pattern */
104 for (i = 0; i < len; i++) {
105 buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++];
106 *indexp &= 7;
109 if (ast_write(chan,&wf) < 0) {
110 ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",chan->name,strerror(errno));
111 return -1;
114 return 0;
117 static struct ast_generator milliwattgen = {
118 alloc: milliwatt_alloc,
119 release: milliwatt_release,
120 generate: milliwatt_generate,
123 static int old_milliwatt_exec(struct ast_channel *chan)
125 ast_set_write_format(chan, AST_FORMAT_ULAW);
126 ast_set_read_format(chan, AST_FORMAT_ULAW);
128 if (chan->_state != AST_STATE_UP) {
129 ast_answer(chan);
132 if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) {
133 ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",chan->name);
134 return -1;
137 while (!ast_safe_sleep(chan, 10000))
140 ast_deactivate_generator(chan);
142 return -1;
145 static int milliwatt_exec(struct ast_channel *chan, void *data)
147 const char *options = data;
148 struct ast_app *playtones_app;
149 struct ast_module_user *u;
150 int res = -1;
152 u = ast_module_user_add(chan);
154 if (!ast_strlen_zero(options) && strchr(options, 'o')) {
155 res = old_milliwatt_exec(chan);
156 goto exit_app;
159 if (!(playtones_app = pbx_findapp("Playtones"))) {
160 ast_log(LOG_ERROR, "The Playtones application is required to run Milliwatt()\n");
161 goto exit_app;
164 res = pbx_exec(chan, playtones_app, "1004/1000");
166 while (!res) {
167 res = ast_safe_sleep(chan, 10000);
170 res = 0;
172 exit_app:
173 ast_module_user_remove(u);
175 return res;
178 static int unload_module(void)
180 int res;
182 res = ast_unregister_application(app);
184 ast_module_user_hangup_all();
186 return res;
189 static int load_module(void)
191 return ast_register_application(app, milliwatt_exec, synopsis, descrip);
194 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application");