Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:48 2009

## 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  * \ingroup applications
    00024  */
    00025
    00026 #include <stdlib.h>
    00027 #include <stdio.h>
    00028 #include <string.h>
    00029 #include <unistd.h>
    00030 #include <errno.h>
    00031
    00032 #include "asterisk.h"
    00033
    00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 8232 $")
    00035
    00036 #include "asterisk/lock.h"
    00037 #include "asterisk/file.h"
    00038 #include "asterisk/logger.h"
    00039 #include "asterisk/channel.h"
    00040 #include "asterisk/pbx.h"
    00041 #include "asterisk/module.h"
    00042
    00043 static char *tdesc = "Digital Milliwatt (mu-law) Test Application";
    00044
    00045 static char *app = "Milliwatt";
    00046
    00047 static char *synopsis = "Generate a Constant 1000Hz tone at 0dbm (mu-law)";
    00048
    00049 static char *descrip =
    00050 "Milliwatt(): Generate a Constant 1000Hz tone at 0dbm (mu-law)\n";
    00051
    00052 STANDARD_LOCAL_USER;
    00053
    00054 LOCAL_USER_DECL;
    00055
    00056 static char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
    00057
    00058 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
    00059 {
    00060 int   *indexp;
    00061    indexp = malloc(sizeof(int));
    00062    if (indexp == NULL) return(NULL);
    00063    *indexp = 0;
    00064    return(indexp);
    00065 }
    00066
    00067 static void milliwatt_release(struct ast_channel *chan, void *data)
    00068 {
    00069    free(data);
    00070    return;
    00071 }
    00072
    00073 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
    00074 {
    00075    struct ast_frame wf;
    00076    unsigned char buf[AST_FRIENDLY_OFFSET + 640];
    00077    int i,*indexp = (int *) data;
    00078
    00079    if (len + AST_FRIENDLY_OFFSET > sizeof(buf))
    00080    {
    00081       ast_log(LOG_WARNING,"Only doing %d bytes (%d bytes requested)\n",(int)(sizeof(buf) - AST_FRIENDLY_OFFSET),len);
    00082       len = sizeof(buf) - AST_FRIENDLY_OFFSET;
    00083    }
    00084    wf.frametype = AST_FRAME_VOICE;
    00085    wf.subclass = AST_FORMAT_ULAW;
    00086    wf.offset = AST_FRIENDLY_OFFSET;
    00087    wf.mallocd = 0;
    00088    wf.data = buf + AST_FRIENDLY_OFFSET;
    00089    wf.datalen = len;
    00090    wf.samples = wf.datalen;
    00091    wf.src = "app_milliwatt";
    00092    wf.delivery.tv_sec = 0;
    00093    wf.delivery.tv_usec = 0;
    00094    wf.prev = wf.next = NULL;
    00095    /* create a buffer containing the digital milliwatt pattern */
    00096    for(i = 0; i < len; i++)
    00097    {
    00098       buf[AST_FRIENDLY_OFFSET + i] = digital_milliwatt[(*indexp)++];
    00099       *indexp &= 7;
    00100    }
    00101    if (ast_write(chan,&wf) < 0)
    00102    {
    00103       ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",chan->name,strerror(errno));
    00104       return -1;
    00105    }
    00106    return 0;
    00107 }
    00108
    00109 static struct ast_generator milliwattgen =
    00110 {
    00111    alloc: milliwatt_alloc,
    00112    release: milliwatt_release,
    00113    generate: milliwatt_generate,
    00114 } ;
    00115
    00116 static int milliwatt_exec(struct ast_channel *chan, void *data)
    00117 {
    00118
    00119    struct localuser *u;
    00120    LOCAL_USER_ADD(u);
    00121    ast_set_write_format(chan, AST_FORMAT_ULAW);
    00122    ast_set_read_format(chan, AST_FORMAT_ULAW);
    00123    if (chan->_state != AST_STATE_UP)
    00124    {
    00125       ast_answer(chan);
    00126    }
    00127    if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0)
    00128    {
    00129       ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",chan->name);
    00130       LOCAL_USER_REMOVE(u);
    00131       return -1;
    00132    }
    00133    while(!ast_safe_sleep(chan, 10000));
    00134    ast_deactivate_generator(chan);
    00135    LOCAL_USER_REMOVE(u);
    00136    return -1;
    00137 }
    00138
    00139 int unload_module(void)
    00140 {
    00141    int res;
    00142
    00143    res = ast_unregister_application(app);
    00144
    00145    STANDARD_HANGUP_LOCALUSERS;
    00146
    00147    return res;
    00148 }
    00149
    00150 int load_module(void)
    00151 {
    00152    return ast_register_application(app, milliwatt_exec, synopsis, descrip);
    00153 }
    00154
    00155 char *description(void)
    00156 {
    00157    return tdesc;
    00158 }
    00159
    00160 int usecount(void)
    00161 {
    00162    int res;
    00163    STANDARD_USECOUNT(res);
    00164    return res;
    00165 }
    00166
    00167 char *key()
    00168 {
    00169    return ASTERISK_GPL_KEY;
    00170 }