Use find_user for existsmailbox
[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 #include <errno.h>
25
26 #include <pthread.h>
27
28 static char *tdesc = "Digital Milliwatt (mu-law) Test Application";
29
30 static char *app = "Milliwatt";
31
32 static char *synopsis = "Generate a Constant 1000Hz tone at 0dbm (mu-law)";
33
34 static char *descrip = 
35 "Milliwatt(): Generate a Constant 1000Hz tone at 0dbm (mu-law)\n";
36
37 STANDARD_LOCAL_USER;
38
39 LOCAL_USER_DECL;
40
41 static char digital_milliwatt[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
42
43 static void *milliwatt_alloc(struct ast_channel *chan, void *params)
44 {
45 int     *indexp;
46         indexp = malloc(sizeof(int));
47         if (indexp == NULL) return(NULL);
48         *indexp = 0;
49         return(indexp);
50 }
51
52 static void milliwatt_release(struct ast_channel *chan, void *data)
53 {
54         free(data);
55         return;
56 }
57
58 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
59 {
60         struct ast_frame wf;
61         unsigned char waste[AST_FRIENDLY_OFFSET];
62         unsigned char buf[640];
63         int i,*indexp = (int *) data;
64
65         if (len > sizeof(buf))
66         {
67                 ast_log(LOG_WARNING,"Only doing %d bytes (%d bytes requested)\n",sizeof(buf),len);
68                 len = sizeof(buf);
69         }
70         waste[0] = 0; /* make compiler happy */
71         wf.frametype = AST_FRAME_VOICE;
72         wf.subclass = AST_FORMAT_ULAW;
73         wf.offset = AST_FRIENDLY_OFFSET;
74         wf.mallocd = 0;
75         wf.data = buf;
76         wf.datalen = len;
77         wf.samples = wf.datalen;
78         wf.src = "app_milliwatt";
79         wf.delivery.tv_sec = 0;
80         wf.delivery.tv_usec = 0;
81         /* create a buffer containing the digital milliwatt pattern */
82         for(i = 0; i < len; i++)
83         {
84                 buf[i] = digital_milliwatt[(*indexp)++];
85                 *indexp &= 7;
86         }
87         if (ast_write(chan,&wf) < 0)
88         {
89                 ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",chan->name,strerror(errno));
90                 return -1;
91         }
92         return 0;
93 }
94
95 static struct ast_generator milliwattgen = 
96 {
97         alloc: milliwatt_alloc,
98         release: milliwatt_release,
99         generate: milliwatt_generate,
100 } ;
101
102 static int milliwatt_exec(struct ast_channel *chan, void *data)
103 {
104
105         struct localuser *u;
106         LOCAL_USER_ADD(u);
107         ast_set_write_format(chan, AST_FORMAT_ULAW);
108         ast_set_read_format(chan, AST_FORMAT_ULAW);
109         if (chan->_state != AST_STATE_UP)
110         {
111                 ast_answer(chan);
112         }
113         if (ast_activate_generator(chan,&milliwattgen,"milliwatt") < 0)
114         {
115                 ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n",chan->name);
116                 return -1;
117         }
118         while(!ast_safe_sleep(chan, 10000));
119         ast_deactivate_generator(chan);
120         LOCAL_USER_REMOVE(u);
121         return -1;
122 }
123
124 int unload_module(void)
125 {
126         STANDARD_HANGUP_LOCALUSERS;
127         return ast_unregister_application(app);
128 }
129
130 int load_module(void)
131 {
132         return ast_register_application(app, milliwatt_exec, synopsis, descrip);
133 }
134
135 char *description(void)
136 {
137         return tdesc;
138 }
139
140 int usecount(void)
141 {
142         int res;
143         STANDARD_USECOUNT(res);
144         return res;
145 }
146
147 char *key()
148 {
149         return ASTERISK_GPL_KEY;
150 }