Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:08 2009

## Asterisk developer's documentation

# app_milliwatt.c File Reference

Digital Milliwatt Test. More...

`#include <stdlib.h>`
`#include <stdio.h>`
`#include <string.h>`
`#include <unistd.h>`
`#include <errno.h>`
`#include "asterisk.h"`
`#include "asterisk/lock.h"`
`#include "asterisk/file.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/pbx.h"`
`#include "asterisk/module.h"`

Include dependency graph for app_milliwatt.c:

Go to the source code of this file.

## Functions

char * | description (void)
| Provides a description of the module.
char * | key ()
| Returns the ASTERISK_GPL_KEY.
int | load_module (void)
| Initialize the module.
static void * | milliwatt_alloc (struct ast_channel *chan, void *params)
static int | milliwatt_exec (struct ast_channel *chan, void *data)
static int | milliwatt_generate (struct ast_channel *chan, void *data, int len, int samples)
static void | milliwatt_release (struct ast_channel *chan, void *data)
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.

## Variables

static char * | app = "Milliwatt"
static char * | descrip
static char | digital_milliwatt [] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e}
| LOCAL_USER_DECL
static struct ast_generator | milliwattgen
| STANDARD_LOCAL_USER
static char * | synopsis = "Generate a Constant 1000Hz tone at 0dbm (mu-law)"
static char * | tdesc = "Digital Milliwatt (mu-law) Test Application"

## Detailed Description

Digital Milliwatt Test.

Definition in file app_milliwatt.c.

## Function Documentation

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 155 of file app_milliwatt.c.

    00156 {
    00157    return tdesc;
    00158 }

char* key | ( | void |  | ) |

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

     char *key(void) {
             return ASTERISK_GPL_KEY;
     }

Returns:
  ASTERISK_GPL_KEY

Definition at line 167 of file app_milliwatt.c.

References ASTERISK_GPL_KEY.

    00168 {
    00169    return ASTERISK_GPL_KEY;
    00170 }

int load_module | ( | void |  | ) |

Initialize the module.

This function is called at module load time. Put all code in here that needs to set up your module's hardware, software, registrations, etc.

Returns:
  This function should return 0 on success and non-zero on failure. If the module is not loaded successfully, Asterisk will call its unload_module() function.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
  int Always 0.

TE STUFF END

Definition at line 150 of file app_milliwatt.c.

References ast_register_application(), and milliwatt_exec().

    00151 {
    00152    return ast_register_application(app, milliwatt_exec, synopsis, descrip);
    00153 }

static void* milliwatt_alloc | ( | struct ast_channel * | chan,
|  | void * | params |
| ) |  |  | `[static]`

Definition at line 58 of file app_milliwatt.c.

References malloc.

    00059 {
    00060 int   *indexp;
    00061    indexp = malloc(sizeof(int));
    00062    if (indexp == NULL) return(NULL);
    00063    *indexp = 0;
    00064    return(indexp);
    00065 }

static int milliwatt_exec | ( | struct ast_channel * | chan,
|  | void * | data |
| ) |  |  | `[static]`

Definition at line 116 of file app_milliwatt.c.

References ast_channel::_state, ast_activate_generator(), ast_answer(), ast_deactivate_generator(), AST_FORMAT_ULAW, ast_log(), ast_safe_sleep(), ast_set_read_format(), ast_set_write_format(), AST_STATE_UP, LOCAL_USER_ADD, LOCAL_USER_REMOVE, LOG_WARNING, and ast_channel::name.

Referenced by load_module().

    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 }

static int milliwatt_generate | ( | struct ast_channel * | chan,
|  | void * | data,
|  | int | len,
|  | int | samples |
| ) |  |  | `[static]`

Definition at line 73 of file app_milliwatt.c.

References AST_FORMAT_ULAW, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), ast_write(), ast_frame::data, ast_frame::datalen, ast_frame::delivery, ast_frame::frametype, LOG_WARNING, ast_frame::mallocd, ast_channel::name, ast_frame::next, ast_frame::offset, ast_frame::prev, ast_frame::samples, ast_frame::src, and ast_frame::subclass.

    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 }

static void milliwatt_release | ( | struct ast_channel * | chan,
|  | void * | data |
| ) |  |  | `[static]`

Definition at line 67 of file app_milliwatt.c.

References free.

    00068 {
    00069    free(data);
    00070    return;
    00071 }

int unload_module | ( | void |  | ) |

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
  Zero on success, or non-zero on error.

Definition at line 139 of file app_milliwatt.c.

References ast_unregister_application(), and STANDARD_HANGUP_LOCALUSERS.

    00140 {
    00141    int res;
    00142
    00143    res = ast_unregister_application(app);
    00144
    00145    STANDARD_HANGUP_LOCALUSERS;
    00146
    00147    return res;
    00148 }

int usecount | ( | void |  | ) |

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
  The module's usecount.

Definition at line 160 of file app_milliwatt.c.

References STANDARD_USECOUNT.

    00161 {
    00162    int res;
    00163    STANDARD_USECOUNT(res);
    00164    return res;
    00165 }

## Variable Documentation

char* app = "Milliwatt"`[static]`

Definition at line 45 of file app_milliwatt.c.

char* descrip`[static]`

Initial value:

    "Milliwatt(): Generate a Constant 1000Hz tone at 0dbm (mu-law)\n"

Definition at line 49 of file app_milliwatt.c.

char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e}`[static]`

Definition at line 56 of file app_milliwatt.c.

LOCAL_USER_DECL

Definition at line 54 of file app_milliwatt.c.

struct ast_generator milliwattgen`[static]`

Definition at line 109 of file app_milliwatt.c.

STANDARD_LOCAL_USER

Definition at line 52 of file app_milliwatt.c.

char* synopsis = "Generate a Constant 1000Hz tone at 0dbm (mu-law)"`[static]`

Definition at line 47 of file app_milliwatt.c.

char* tdesc = "Digital Milliwatt (mu-law) Test Application"`[static]`

Definition at line 43 of file app_milliwatt.c.