Sat Apr 12 07:12:16 2008

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

Generated on Sat Apr 12 07:12:16 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.5