Remove debug output.
[asterisk-bristuff.git] / apps / app_milliwatt.c
blob8b0456014573d7ae517415e837659f9a84c0c002
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 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <errno.h>
38 #include "asterisk/lock.h"
39 #include "asterisk/file.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/module.h"
44 #include "asterisk/utils.h"
46 static char *app = "Milliwatt";
48 static char *synopsis = "Generate a Constant 1000Hz tone at 0dbm (mu-law)";
50 static char *descrip =
51 "Milliwatt(): Generate a Constant 1000Hz tone at 0dbm (mu-law)\n";
54 static char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
56 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
58 return ast_calloc(1, sizeof(int));
61 static void milliwatt_release(struct ast_channel *chan, void *data)
63 free(data);
64 return;
67 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
69 unsigned char buf[AST_FRIENDLY_OFFSET + 640];
70 const int maxsamples = sizeof (buf) / sizeof (buf[0]);
71 int i, *indexp = (int *) data;
72 struct ast_frame wf = {
73 .frametype = AST_FRAME_VOICE,
74 .subclass = AST_FORMAT_ULAW,
75 .offset = AST_FRIENDLY_OFFSET,
76 .data = buf + AST_FRIENDLY_OFFSET,
77 .src = __FUNCTION__,
80 /* Instead of len, use samples, because channel.c generator_force
81 * generate(chan, tmp, 0, 160) ignores len. In any case, len is
82 * a multiple of samples, given by number of samples times bytes per
83 * sample. In the case of ulaw, len = samples. for signed linear
84 * len = 2 * samples */
86 if (samples > maxsamples) {
87 ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", maxsamples, samples);
88 samples = maxsamples;
90 len = samples * sizeof (buf[0]);
91 wf.datalen = len;
92 wf.samples = samples;
93 /* create a buffer containing the digital milliwatt pattern */
94 for(i = 0; i < len; i++)
96 buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++];
97 *indexp &= 7;
99 if (ast_write(chan,&wf) < 0)
101 ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",chan->name,strerror(errno));
102 return -1;
104 return 0;
107 static struct ast_generator milliwattgen =
109 alloc: milliwatt_alloc,
110 release: milliwatt_release,
111 generate: milliwatt_generate,
114 static int milliwatt_exec(struct ast_channel *chan, void *data)
117 struct ast_module_user *u;
118 u = ast_module_user_add(chan);
119 ast_set_write_format(chan, AST_FORMAT_ULAW);
120 ast_set_read_format(chan, AST_FORMAT_ULAW);
121 if (chan->_state != AST_STATE_UP)
123 ast_answer(chan);
125 if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0)
127 ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",chan->name);
128 ast_module_user_remove(u);
129 return -1;
131 while(!ast_safe_sleep(chan, 10000));
132 ast_deactivate_generator(chan);
133 ast_module_user_remove(u);
134 return -1;
137 static int unload_module(void)
139 int res;
141 res = ast_unregister_application(app);
143 ast_module_user_hangup_all();
145 return res;
148 static int load_module(void)
150 return ast_register_application(app, milliwatt_exec, synopsis, descrip);
153 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application");