Replace direct access to channel name with accessor functions
[asterisk/asterisk.git] / apps / app_milliwatt.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Digital Milliwatt Test
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/module.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/indications.h"
40
41 /*** DOCUMENTATION
42         <application name="Milliwatt" language="en_US">
43                 <synopsis>
44                         Generate a Constant 1004Hz tone at 0dbm (mu-law).
45                 </synopsis>
46                 <syntax>
47                         <parameter name="options">
48                                 <optionlist>
49                                         <option name="o">
50                                                 <para>Generate the tone at 1000Hz like previous version.</para>
51                                         </option>
52                                 </optionlist>
53                         </parameter>
54                 </syntax>
55                 <description>
56                         <para>Previous versions of this application generated the tone at 1000Hz.  If for
57                         some reason you would prefer that behavior, supply the <literal>o</literal> option to get the
58                         old behavior.</para>
59                 </description>
60         </application>
61  ***/
62
63 static const char app[] = "Milliwatt";
64
65 static const char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
66
67 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
68 {
69         return ast_calloc(1, sizeof(int));
70 }
71
72 static void milliwatt_release(struct ast_channel *chan, void *data)
73 {
74         ast_free(data);
75         return;
76 }
77
78 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
79 {
80         unsigned char buf[AST_FRIENDLY_OFFSET + 640];
81         const int maxsamples = ARRAY_LEN(buf);
82         int i, *indexp = (int *) data;
83         struct ast_frame wf = {
84                 .frametype = AST_FRAME_VOICE,
85                 .offset = AST_FRIENDLY_OFFSET,
86                 .src = __FUNCTION__,
87         };
88         ast_format_set(&wf.subclass.format, AST_FORMAT_ULAW, 0);
89         wf.data.ptr = buf + AST_FRIENDLY_OFFSET;
90
91         /* Instead of len, use samples, because channel.c generator_force
92         * generate(chan, tmp, 0, 160) ignores len. In any case, len is
93         * a multiple of samples, given by number of samples times bytes per
94         * sample. In the case of ulaw, len = samples. for signed linear
95         * len = 2 * samples */
96         if (samples > maxsamples) {
97                 ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", maxsamples, samples);
98                 samples = maxsamples;
99         }
100
101         len = samples * sizeof (buf[0]);
102         wf.datalen = len;
103         wf.samples = samples;
104
105         /* create a buffer containing the digital milliwatt pattern */
106         for (i = 0; i < len; i++) {
107                 buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++];
108                 *indexp &= 7;
109         }
110
111         if (ast_write(chan,&wf) < 0) {
112                 ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",ast_channel_name(chan),strerror(errno));
113                 return -1;
114         }
115
116         return 0;
117 }
118
119 static struct ast_generator milliwattgen = {
120         alloc: milliwatt_alloc,
121         release: milliwatt_release,
122         generate: milliwatt_generate,
123 };
124
125 static int old_milliwatt_exec(struct ast_channel *chan)
126 {
127         ast_set_write_format_by_id(chan, AST_FORMAT_ULAW);
128         ast_set_read_format_by_id(chan, AST_FORMAT_ULAW);
129
130         if (chan->_state != AST_STATE_UP) {
131                 ast_answer(chan);
132         }
133
134         if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) {
135                 ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",ast_channel_name(chan));
136                 return -1;
137         }
138
139         while (!ast_safe_sleep(chan, 10000))
140                 ;
141
142         ast_deactivate_generator(chan);
143
144         return -1;
145 }
146
147 static int milliwatt_exec(struct ast_channel *chan, const char *data)
148 {
149         const char *options = data;
150         int res = -1;
151
152         if (!ast_strlen_zero(options) && strchr(options, 'o')) {
153                 return old_milliwatt_exec(chan);
154         }
155
156         res = ast_playtones_start(chan, 23255, "1004/1000", 0);
157
158         while (!res) {
159                 res = ast_safe_sleep(chan, 10000);
160         }
161
162         return res;
163 }
164
165 static int unload_module(void)
166 {
167         return ast_unregister_application(app);
168 }
169
170 static int load_module(void)
171 {
172         return ast_register_application_xml(app, milliwatt_exec);
173 }
174
175 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application");