Merge "astobj2: Create function to copy weak proxied objects from container."
[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 #include "asterisk/module.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/indications.h"
38 #include "asterisk/format_cache.h"
39
40 /*** DOCUMENTATION
41         <application name="Milliwatt" language="en_US">
42                 <synopsis>
43                         Generate a Constant 1004Hz tone at 0dbm (mu-law).
44                 </synopsis>
45                 <syntax>
46                         <parameter name="options">
47                                 <optionlist>
48                                         <option name="o">
49                                                 <para>Generate the tone at 1000Hz like previous version.</para>
50                                         </option>
51                                 </optionlist>
52                         </parameter>
53                 </syntax>
54                 <description>
55                         <para>Previous versions of this application generated the tone at 1000Hz.  If for
56                         some reason you would prefer that behavior, supply the <literal>o</literal> option to get the
57                         old behavior.</para>
58                 </description>
59         </application>
60  ***/
61
62 static const char app[] = "Milliwatt";
63
64 static const char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
65
66 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
67 {
68         return ast_calloc(1, sizeof(int));
69 }
70
71 static void milliwatt_release(struct ast_channel *chan, void *data)
72 {
73         ast_free(data);
74         return;
75 }
76
77 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
78 {
79         unsigned char buf[AST_FRIENDLY_OFFSET + 640];
80         const int maxsamples = ARRAY_LEN(buf) - (AST_FRIENDLY_OFFSET / sizeof(buf[0]));
81         int i, *indexp = (int *) data, res;
82         struct ast_frame wf = {
83                 .frametype = AST_FRAME_VOICE,
84                 .offset = AST_FRIENDLY_OFFSET,
85                 .src = __FUNCTION__,
86         };
87
88         wf.subclass.format = ast_format_ulaw;
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         res = ast_write(chan, &wf);
112         ast_frfree(&wf);
113
114         if (res < 0) {
115                 ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",ast_channel_name(chan),strerror(errno));
116                 return -1;
117         }
118
119         return 0;
120 }
121
122 static struct ast_generator milliwattgen = {
123         .alloc = milliwatt_alloc,
124         .release = milliwatt_release,
125         .generate = milliwatt_generate,
126 };
127
128 static int old_milliwatt_exec(struct ast_channel *chan)
129 {
130         ast_set_write_format(chan, ast_format_ulaw);
131         ast_set_read_format(chan, ast_format_ulaw);
132
133         if (ast_channel_state(chan) != AST_STATE_UP) {
134                 ast_answer(chan);
135         }
136
137         if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) {
138                 ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",ast_channel_name(chan));
139                 return -1;
140         }
141
142         while (!ast_safe_sleep(chan, 10000))
143                 ;
144
145         ast_deactivate_generator(chan);
146
147         return -1;
148 }
149
150 static int milliwatt_exec(struct ast_channel *chan, const char *data)
151 {
152         const char *options = data;
153         int res = -1;
154
155         if (!ast_strlen_zero(options) && strchr(options, 'o')) {
156                 return old_milliwatt_exec(chan);
157         }
158
159         res = ast_playtones_start(chan, 23255, "1004/1000", 0);
160
161         while (!res) {
162                 res = ast_safe_sleep(chan, 10000);
163         }
164
165         return res;
166 }
167
168 static int unload_module(void)
169 {
170         return ast_unregister_application(app);
171 }
172
173 static int load_module(void)
174 {
175         return ast_register_application_xml(app, milliwatt_exec);
176 }
177
178 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application");