Sat Aug 6 00:39:20 2011

Asterisk developer's documentation


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    <depend>res_indications</depend>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 209838 $")
00035 
00036 #include <stdlib.h>
00037 #include <stdio.h>
00038 #include <string.h>
00039 #include <unistd.h>
00040 #include <errno.h>
00041 #include <limits.h>
00042 
00043 #include "asterisk/lock.h"
00044 #include "asterisk/file.h"
00045 #include "asterisk/logger.h"
00046 #include "asterisk/channel.h"
00047 #include "asterisk/pbx.h"
00048 #include "asterisk/module.h"
00049 #include "asterisk/utils.h"
00050 #include "asterisk/indications.h"
00051 
00052 static char *app = "Milliwatt";
00053 
00054 static char *synopsis = "Generate a Constant 1004Hz tone at 0dbm (mu-law)";
00055 
00056 static char *descrip = 
00057 "   Milliwatt([options]): Generate a Constant 1004Hz tone at 0dbm.\n"
00058 "Previous versions of this application generated the tone at 1000Hz.  If for\n"
00059 "some reason you would prefer that behavior, supply the 'o' option to get the\n"
00060 "old behavior.\n"
00061 "";
00062 
00063 
00064 static char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
00065 
00066 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
00067 {
00068    return ast_calloc(1, sizeof(int));
00069 }
00070 
00071 static void milliwatt_release(struct ast_channel *chan, void *data)
00072 {
00073    free(data);
00074    return;
00075 }
00076 
00077 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
00078 {
00079    unsigned char buf[AST_FRIENDLY_OFFSET + 640];
00080    const int maxsamples = sizeof (buf) / sizeof (buf[0]);
00081    int i, *indexp = (int *) data;
00082    struct ast_frame wf = {
00083       .frametype = AST_FRAME_VOICE,
00084       .subclass = AST_FORMAT_ULAW,
00085       .offset = AST_FRIENDLY_OFFSET,
00086       .data = buf + AST_FRIENDLY_OFFSET,
00087       .src = __FUNCTION__,
00088    };
00089 
00090    /* Instead of len, use samples, because channel.c generator_force
00091    * generate(chan, tmp, 0, 160) ignores len. In any case, len is
00092    * a multiple of samples, given by number of samples times bytes per
00093    * sample. In the case of ulaw, len = samples. for signed linear
00094    * len = 2 * samples */
00095 
00096    if (samples > maxsamples) {
00097       ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", maxsamples, samples);
00098       samples = maxsamples;
00099    }
00100 
00101    len = samples * sizeof (buf[0]);
00102    wf.datalen = len;
00103    wf.samples = samples;
00104 
00105    /* create a buffer containing the digital milliwatt pattern */
00106    for (i = 0; i < len; i++) {
00107       buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++];
00108       *indexp &= 7;
00109    }
00110 
00111    if (ast_write(chan,&wf) < 0) {
00112       ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",chan->name,strerror(errno));
00113       return -1;
00114    }
00115 
00116    return 0;
00117 }
00118 
00119 static struct ast_generator milliwattgen = {
00120    alloc: milliwatt_alloc,
00121    release: milliwatt_release,
00122    generate: milliwatt_generate,
00123 };
00124 
00125 static int old_milliwatt_exec(struct ast_channel *chan)
00126 {
00127    ast_set_write_format(chan, AST_FORMAT_ULAW);
00128    ast_set_read_format(chan, AST_FORMAT_ULAW);
00129 
00130    if (chan->_state != AST_STATE_UP) {
00131       ast_answer(chan);
00132    }
00133 
00134    if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0) {
00135       ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",chan->name);
00136       return -1;
00137    }
00138 
00139    while (!ast_safe_sleep(chan, 10000))
00140       ;
00141 
00142    ast_deactivate_generator(chan);
00143 
00144    return -1;
00145 }
00146 
00147 static int milliwatt_exec(struct ast_channel *chan, void *data)
00148 {
00149    const char *options = data;
00150    struct ast_module_user *u;
00151    int res = -1;
00152 
00153    u = ast_module_user_add(chan);
00154 
00155    if (!ast_strlen_zero(options) && strchr(options, 'o')) {
00156       res = old_milliwatt_exec(chan);
00157       goto exit_app;
00158    }
00159 
00160    res = ast_playtones_start(chan, 23255, "1004/1000", 0);
00161 
00162    while (!res) {
00163       res = ast_safe_sleep(chan, 10000);
00164    }
00165 
00166    res = 0;
00167 
00168 exit_app:
00169    ast_module_user_remove(u);
00170 
00171    return res;
00172 }
00173 
00174 static int unload_module(void)
00175 {
00176    int res;
00177 
00178    res = ast_unregister_application(app);
00179 
00180    ast_module_user_hangup_all();
00181 
00182    return res;
00183 }
00184 
00185 static int load_module(void)
00186 {
00187    return ast_register_application(app, milliwatt_exec, synopsis, descrip);
00188 }
00189 
00190 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Digital Milliwatt (mu-law) Test Application");

Generated on Sat Aug 6 00:39:20 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7