Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
app_milliwatt.c
Go to the documentation of this file.
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"
39
40/*** DOCUMENTATION
41 <application name="Milliwatt" language="en_US">
42 <since>
43 <version>0.1.12</version>
44 </since>
45 <synopsis>
46 Generates a 1004 Hz test tone at 0dbm (mu-law).
47 </synopsis>
48 <syntax>
49 <parameter name="options">
50 <optionlist>
51 <option name="m">
52 <para>Generate a 1004 Hz Milliwatt test tone at 0dbm, with a
53 1 second silent interval. This option must be specified
54 if you are using this for a milliwatt test line.</para>
55 </option>
56 <option name="o">
57 <para>Generate a constant tone at 1000 Hz like previous version.</para>
58 </option>
59 </optionlist>
60 </parameter>
61 </syntax>
62 <description>
63 <para>Generates a 1004 Hz test tone.</para>
64 <para>By default, this application does not provide a Milliwatt test tone. It simply
65 plays a 1004 Hz tone, which is not suitable for performing a milliwatt test.
66 The <literal>m</literal> option should be used so that a real Milliwatt test tone
67 is provided. This will include a 1 second silent interval every 10 seconds.</para>
68 <para>Previous versions of this application generated a constant tone at 1000 Hz. If for
69 some reason you would prefer that behavior, supply the <literal>o</literal> option to get the
70 old behavior.</para>
71 </description>
72 </application>
73 ***/
74
75static const char app[] = "Milliwatt";
76
77static const char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
78
79static void *milliwatt_alloc(struct ast_channel *chan, void *params)
80{
81 return ast_calloc(1, sizeof(int));
82}
83
84static void milliwatt_release(struct ast_channel *chan, void *data)
85{
86 ast_free(data);
87 return;
88}
89
90static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
91{
92 unsigned char buf[AST_FRIENDLY_OFFSET + 640];
93 const int maxsamples = ARRAY_LEN(buf) - (AST_FRIENDLY_OFFSET / sizeof(buf[0]));
94 int i, *indexp = (int *) data, res;
95 struct ast_frame wf = {
97 .offset = AST_FRIENDLY_OFFSET,
98 .src = __FUNCTION__,
99 };
100
103
104 /* Instead of len, use samples, because channel.c generator_force
105 * generate(chan, tmp, 0, 160) ignores len. In any case, len is
106 * a multiple of samples, given by number of samples times bytes per
107 * sample. In the case of ulaw, len = samples. for signed linear
108 * len = 2 * samples */
109 if (samples > maxsamples) {
110 ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", maxsamples, samples);
111 samples = maxsamples;
112 }
113
114 len = samples * sizeof (buf[0]);
115 wf.datalen = len;
116 wf.samples = samples;
117
118 /* create a buffer containing the digital milliwatt pattern */
119 for (i = 0; i < len; i++) {
120 buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++];
121 *indexp &= 7;
122 }
123
124 res = ast_write(chan, &wf);
125 ast_frfree(&wf);
126
127 if (res < 0) {
128 ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",ast_channel_name(chan),strerror(errno));
129 return -1;
130 }
131
132 return 0;
133}
134
137 .release = milliwatt_release,
138 .generate = milliwatt_generate,
139};
140
141static int old_milliwatt_exec(struct ast_channel *chan)
142{
145
146 if (ast_channel_state(chan) != AST_STATE_UP) {
147 ast_answer(chan);
148 }
149
150 if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) {
151 ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",ast_channel_name(chan));
152 return -1;
153 }
154
155 while (!ast_safe_sleep(chan, 10000))
156 ;
157
159
160 return -1;
161}
162
163static int milliwatt_exec(struct ast_channel *chan, const char *data)
164{
165 const char *options = data;
166 int res = -1;
167
168 if (!ast_strlen_zero(options) && strchr(options, 'o')) {
169 return old_milliwatt_exec(chan);
170 }
171 if (!ast_strlen_zero(options) && strchr(options, 'm')) {
172 res = ast_playtones_start(chan, 23255, "1004/9000,0/1000", 0);
173 } else {
174 res = ast_playtones_start(chan, 23255, "1004/1000", 0);
175 }
176
177 while (!res) {
178 res = ast_safe_sleep(chan, 10000);
179 }
180
181 return res;
182}
183
184static int unload_module(void)
185{
187}
188
189static int load_module(void)
190{
192}
193
194AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application");
static const char app[]
Definition: app_milliwatt.c:75
static void * milliwatt_alloc(struct ast_channel *chan, void *params)
Definition: app_milliwatt.c:79
static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
Definition: app_milliwatt.c:90
static int milliwatt_exec(struct ast_channel *chan, const char *data)
static void milliwatt_release(struct ast_channel *chan, void *data)
Definition: app_milliwatt.c:84
static struct ast_generator milliwattgen
static const char digital_milliwatt[]
Definition: app_milliwatt.c:77
static int load_module(void)
static int unload_module(void)
static int old_milliwatt_exec(struct ast_channel *chan)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
Definition: channel.c:2979
void ast_deactivate_generator(struct ast_channel *chan)
Definition: channel.c:2921
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:5161
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5779
int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
Sets write format on channel chan.
Definition: channel.c:5820
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2834
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1601
ast_channel_state
ast_channel states
Definition: channelstate.h:35
@ AST_STATE_UP
Definition: channelstate.h:42
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
Media Format Cache API.
struct ast_format * ast_format_ulaw
Built-in cached ulaw format.
Definition: format_cache.c:86
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_frfree(fr)
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
@ AST_FRAME_VOICE
#define LOG_WARNING
Tone Indication Support.
int ast_playtones_start(struct ast_channel *chan, int vol, const char *tonelist, int interruptible)
Start playing a list of tones on a channel.
Definition: indications.c:302
int errno
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
Core PBX routines and definitions.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Main Channel structure associated with a channel.
struct ast_format * format
Data structure associated with a single frame of data.
union ast_frame::@228 data
struct ast_frame_subclass subclass
enum ast_frame_type frametype
void *(* alloc)(struct ast_channel *chan, void *params)
Definition: channel.h:228
static struct test_options options
#define ARRAY_LEN(a)
Definition: utils.h:666