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