Version 0.2.0 from FTP
[asterisk/asterisk.git] / apps / app_milliwatt.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Digital Milliwatt Test
5  * 
6  * Copyright (C) 2002, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include <pthread.h>
26
27 static char *tdesc = "Digital Milliwatt (mu-law) Test Application";
28
29 static char *app = "Milliwatt";
30
31 static char *synopsis = "Generate a Constant 1000Hz tone at 0dbm (mu-law)";
32
33 static char *descrip = 
34 "Milliwatt(): Generate a Constant 1000Hz tone at 0dbm (mu-law)\n";
35
36 STANDARD_LOCAL_USER;
37
38 LOCAL_USER_DECL;
39
40 static char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
41
42 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
43 {
44 int     *indexp;
45         indexp = malloc(sizeof(int));
46         if (indexp == NULL) return(NULL);
47         *indexp = 0;
48         return(indexp);
49 }
50
51 static void milliwatt_release(struct ast_channel *chan, void *data)
52 {
53         free(data);
54         return;
55 }
56
57 static int milliwatt_generate(struct ast_channel *chan, void *data, int len)
58 {
59         struct ast_frame wf;
60         unsigned char waste[AST_FRIENDLY_OFFSET];
61         unsigned char buf[640];
62         int i,*indexp = (int *) data;
63
64         if (len > sizeof(buf))
65         {
66                 ast_log(LOG_WARNING,"Only doing %d bytes (%d bytes requested)\n",sizeof(buf),len);
67                 len = sizeof(buf);
68         }
69         waste[0] = 0; /* make compiler happy */
70         wf.frametype = AST_FRAME_VOICE;
71         wf.subclass = AST_FORMAT_ULAW;
72         wf.offset = AST_FRIENDLY_OFFSET;
73         wf.mallocd = 0;
74         wf.data = buf;
75         wf.datalen = len;
76         wf.timelen = wf.datalen / 8;
77         wf.src = "app_milliwatt";
78         /* create a buffer containing the digital milliwatt pattern */
79         for(i = 0; i < len; i++)
80         {
81                 buf[i] = digital_milliwatt[(*indexp)++];
82                 *indexp &= 7;
83         }
84         if (ast_write(chan,&wf) < 0)
85         {
86                 ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",chan->name,strerror(errno));
87                 return -1;
88         }
89         return 0;
90 }
91
92 static struct ast_generator milliwattgen = 
93 {
94         alloc: milliwatt_alloc,
95         release: milliwatt_release,
96         generate: milliwatt_generate,
97 } ;
98
99 static int milliwatt_exec(struct ast_channel *chan, void *data)
100 {
101
102         struct localuser *u;
103         LOCAL_USER_ADD(u);
104         ast_set_write_format(chan, AST_FORMAT_ULAW);
105         ast_set_read_format(chan, AST_FORMAT_ULAW);
106         if (chan->_state != AST_STATE_UP)
107         {
108                 ast_answer(chan);
109         }
110         if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0)
111         {
112                 ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",chan->name);
113                 return -1;
114         }
115         while(!ast_safe_sleep(chan, 10000));
116         ast_deactivate_generator(chan);
117         LOCAL_USER_REMOVE(u);
118         return -1;
119 }
120
121 int unload_module(void)
122 {
123         STANDARD_HANGUP_LOCALUSERS;
124         return ast_unregister_application(app);
125 }
126
127 int load_module(void)
128 {
129         return ast_register_application(app, milliwatt_exec, synopsis, descrip);
130 }
131
132 char *description(void)
133 {
134         return tdesc;
135 }
136
137 int usecount(void)
138 {
139         int res;
140         STANDARD_USECOUNT(res);
141         return res;
142 }
143
144 char *key()
145 {
146         return ASTERISK_GPL_KEY;
147 }