app_milliwatt.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Digital Milliwatt Test
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  * 
00025  * \ingroup applications
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>core</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419044 $")
00035 
00036 #include "asterisk/module.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/indications.h"
00040 #include "asterisk/format_cache.h"
00041 
00042 /*** DOCUMENTATION
00043    <application name="Milliwatt" language="en_US">
00044       <synopsis>
00045          Generate a Constant 1004Hz tone at 0dbm (mu-law).
00046       </synopsis>
00047       <syntax>
00048          <parameter name="options">
00049             <optionlist>
00050                <option name="o">
00051                   <para>Generate the tone at 1000Hz like previous version.</para>
00052                </option>
00053             </optionlist>
00054          </parameter>
00055       </syntax>
00056       <description>
00057          <para>Previous versions of this application generated the tone at 1000Hz.  If for
00058          some reason you would prefer that behavior, supply the <literal>o</literal> option to get the
00059          old behavior.</para>
00060       </description>
00061    </application>
00062  ***/
00063 
00064 static const char app[] = "Milliwatt";
00065 
00066 static const char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
00067 
00068 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
00069 {
00070    return ast_calloc(1, sizeof(int));
00071 }
00072 
00073 static void milliwatt_release(struct ast_channel *chan, void *data)
00074 {
00075    ast_free(data);
00076    return;
00077 }
00078 
00079 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
00080 {
00081    unsigned char buf[AST_FRIENDLY_OFFSET + 640];
00082    const int maxsamples = ARRAY_LEN(buf) - (AST_FRIENDLY_OFFSET / sizeof(buf[0]));
00083    int i, *indexp = (int *) data, res;
00084    struct ast_frame wf = {
00085       .frametype = AST_FRAME_VOICE,
00086       .offset = AST_FRIENDLY_OFFSET,
00087       .src = __FUNCTION__,
00088    };
00089 
00090    wf.subclass.format = ast_format_ulaw;
00091    wf.data.ptr = buf + AST_FRIENDLY_OFFSET;
00092 
00093    /* Instead of len, use samples, because channel.c generator_force
00094    * generate(chan, tmp, 0, 160) ignores len. In any case, len is
00095    * a multiple of samples, given by number of samples times bytes per
00096    * sample. In the case of ulaw, len = samples. for signed linear
00097    * len = 2 * samples */
00098    if (samples > maxsamples) {
00099       ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", maxsamples, samples);
00100       samples = maxsamples;
00101    }
00102 
00103    len = samples * sizeof (buf[0]);
00104    wf.datalen = len;
00105    wf.samples = samples;
00106 
00107    /* create a buffer containing the digital milliwatt pattern */
00108    for (i = 0; i < len; i++) {
00109       buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++];
00110       *indexp &= 7;
00111    }
00112 
00113    res = ast_write(chan, &wf);
00114    ast_frfree(&wf);
00115 
00116    if (res < 0) {
00117       ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",ast_channel_name(chan),strerror(errno));
00118       return -1;
00119    }
00120 
00121    return 0;
00122 }
00123 
00124 static struct ast_generator milliwattgen = {
00125    .alloc = milliwatt_alloc,
00126    .release = milliwatt_release,
00127    .generate = milliwatt_generate,
00128 };
00129 
00130 static int old_milliwatt_exec(struct ast_channel *chan)
00131 {
00132    ast_set_write_format(chan, ast_format_ulaw);
00133    ast_set_read_format(chan, ast_format_ulaw);
00134 
00135    if (ast_channel_state(chan) != AST_STATE_UP) {
00136       ast_answer(chan);
00137    }
00138 
00139    if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) {
00140       ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",ast_channel_name(chan));
00141       return -1;
00142    }
00143 
00144    while (!ast_safe_sleep(chan, 10000))
00145       ;
00146 
00147    ast_deactivate_generator(chan);
00148 
00149    return -1;
00150 }
00151 
00152 static int milliwatt_exec(struct ast_channel *chan, const char *data)
00153 {
00154    const char *options = data;
00155    int res = -1;
00156 
00157    if (!ast_strlen_zero(options) && strchr(options, 'o')) {
00158       return old_milliwatt_exec(chan);
00159    }
00160 
00161    res = ast_playtones_start(chan, 23255, "1004/1000", 0);
00162 
00163    while (!res) {
00164       res = ast_safe_sleep(chan, 10000);
00165    }
00166 
00167    return res;
00168 }
00169 
00170 static int unload_module(void)
00171 {
00172    return ast_unregister_application(app);
00173 }
00174 
00175 static int load_module(void)
00176 {
00177    return ast_register_application_xml(app, milliwatt_exec);
00178 }
00179 
00180 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application");

Generated on Thu Apr 16 06:27:10 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6