3c6583f8f677d50781bf0cdc51bc43edca74f129
[asterisk/asterisk.git] / channels / chan_nbs.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Generic Linux Telephony Interface driver
22  * 
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/socket.h>
28 #include <sys/time.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <arpa/inet.h>
33 #include <fcntl.h>
34 #include <sys/ioctl.h>
35 #include <nbs.h>
36
37 #include "asterisk.h"
38
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40
41 #include "asterisk/lock.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/config.h"
44 #include "asterisk/logger.h"
45 #include "asterisk/module.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/options.h"
48
49 static const char desc[] = "Network Broadcast Sound Support";
50 static const char type[] = "NBS";
51 static const char tdesc[] = "Network Broadcast Sound Driver";
52
53 static int usecnt =0;
54
55 /* Only linear is allowed */
56 static int prefformat = AST_FORMAT_SLINEAR;
57
58 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
59
60 static char context[AST_MAX_EXTENSION] = "default";
61
62 /* NBS creates private structures on demand */
63    
64 struct nbs_pvt {
65         NBS *nbs;
66         struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
67         char app[16];                                   /* Our app */
68         char stream[80];                                /* Our stream */
69         struct ast_frame fr;                    /* "null" frame */
70 };
71
72 static struct ast_channel *nbs_request(const char *type, int format, void *data, int *cause);
73 static int nbs_call(struct ast_channel *ast, char *dest, int timeout);
74 static int nbs_hangup(struct ast_channel *ast);
75 static struct ast_frame *nbs_xread(struct ast_channel *ast);
76 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame);
77
78 static const struct ast_channel_tech nbs_tech = {
79         .type = type,
80         .description = tdesc,
81         .capabilities = AST_FORMAT_SLINEAR,
82         .requester = nbs_request,
83         .call = nbs_call,
84         .hangup = nbs_hangup,
85         .read = nbs_xread,
86         .write = nbs_xwrite,
87 };
88
89 static int nbs_call(struct ast_channel *ast, char *dest, int timeout)
90 {
91         struct nbs_pvt *p;
92
93         p = ast->tech_pvt;
94
95         if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
96                 ast_log(LOG_WARNING, "nbs_call called on %s, neither down nor reserved\n", ast->name);
97                 return -1;
98         }
99         /* When we call, it just works, really, there's no destination...  Just
100            ring the phone and wait for someone to answer */
101         if (option_debug)
102                 ast_log(LOG_DEBUG, "Calling %s on %s\n", dest, ast->name);
103
104         /* If we can't connect, return congestion */
105         if (nbs_connect(p->nbs)) {
106                 ast_log(LOG_WARNING, "NBS Connection failed on %s\n", ast->name);
107                 ast_queue_control(ast, AST_CONTROL_CONGESTION);
108         } else {
109                 ast_setstate(ast, AST_STATE_RINGING);
110                 ast_queue_control(ast, AST_CONTROL_ANSWER);
111         }
112
113         return 0;
114 }
115
116 static void nbs_destroy(struct nbs_pvt *p)
117 {
118         if (p->nbs)
119                 nbs_delstream(p->nbs);
120         free(p);
121 }
122
123 static struct nbs_pvt *nbs_alloc(void *data)
124 {
125         struct nbs_pvt *p;
126         int flags = 0;
127         char stream[256] = "";
128         char *opts;
129         strncpy(stream, data, sizeof(stream) - 1);
130         if ((opts = strchr(stream, ':'))) {
131                 *opts = '\0';
132                 opts++;
133         } else
134                 opts = "";
135         p = malloc(sizeof(struct nbs_pvt));
136         if (p) {
137                 memset(p, 0, sizeof(struct nbs_pvt));
138                 if (strlen(opts)) {
139                         if (strchr(opts, 'm'))
140                                 flags |= NBS_FLAG_MUTE;
141                         if (strchr(opts, 'o'))
142                                 flags |= NBS_FLAG_OVERSPEAK;
143                         if (strchr(opts, 'e'))
144                                 flags |= NBS_FLAG_EMERGENCY;
145                         if (strchr(opts, 'O'))
146                                 flags |= NBS_FLAG_OVERRIDE;
147                 } else
148                         flags = NBS_FLAG_OVERSPEAK;
149                 
150                 strncpy(p->stream, stream, sizeof(p->stream) - 1);
151                 p->nbs = nbs_newstream("asterisk", stream, flags);
152                 if (!p->nbs) {
153                         ast_log(LOG_WARNING, "Unable to allocate new NBS stream '%s' with flags %d\n", stream, flags);
154                         free(p);
155                         p = NULL;
156                 } else {
157                         /* Set for 8000 hz mono, 640 samples */
158                         nbs_setbitrate(p->nbs, 8000);
159                         nbs_setchannels(p->nbs, 1);
160                         nbs_setblocksize(p->nbs, 640);
161                         nbs_setblocking(p->nbs, 0);
162                 }
163         }
164         return p;
165 }
166
167 static int nbs_hangup(struct ast_channel *ast)
168 {
169         struct nbs_pvt *p;
170         p = ast->tech_pvt;
171         if (option_debug)
172                 ast_log(LOG_DEBUG, "nbs_hangup(%s)\n", ast->name);
173         if (!ast->tech_pvt) {
174                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
175                 return 0;
176         }
177         nbs_destroy(p);
178         ast->tech_pvt = NULL;
179         ast_setstate(ast, AST_STATE_DOWN);
180         return 0;
181 }
182
183 static struct ast_frame  *nbs_xread(struct ast_channel *ast)
184 {
185         struct nbs_pvt *p = ast->tech_pvt;
186         
187
188         /* Some nice norms */
189         p->fr.datalen = 0;
190         p->fr.samples = 0;
191         p->fr.data =  NULL;
192         p->fr.src = type;
193         p->fr.offset = 0;
194         p->fr.mallocd=0;
195         p->fr.delivery.tv_sec = 0;
196         p->fr.delivery.tv_usec = 0;
197
198         ast_log(LOG_DEBUG, "Returning null frame on %s\n", ast->name);
199
200         return &p->fr;
201 }
202
203 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame)
204 {
205         struct nbs_pvt *p = ast->tech_pvt;
206         /* Write a frame of (presumably voice) data */
207         if (frame->frametype != AST_FRAME_VOICE) {
208                 if (frame->frametype != AST_FRAME_IMAGE)
209                         ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
210                 return 0;
211         }
212         if (!(frame->subclass &
213                 (AST_FORMAT_SLINEAR))) {
214                 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
215                 return 0;
216         }
217         if (ast->_state != AST_STATE_UP) {
218                 /* Don't try tos end audio on-hook */
219                 return 0;
220         }
221         if (nbs_write(p->nbs, frame->data, frame->datalen / 2) < 0) 
222                 return -1;
223         return 0;
224 }
225
226 static struct ast_channel *nbs_new(struct nbs_pvt *i, int state)
227 {
228         struct ast_channel *tmp;
229         tmp = ast_channel_alloc(1);
230         if (tmp) {
231                 tmp->tech = &nbs_tech;
232                 snprintf(tmp->name, sizeof(tmp->name), "NBS/%s", i->stream);
233                 tmp->type = type;
234                 tmp->fds[0] = nbs_fd(i->nbs);
235                 tmp->nativeformats = prefformat;
236                 tmp->rawreadformat = prefformat;
237                 tmp->rawwriteformat = prefformat;
238                 tmp->writeformat = prefformat;
239                 tmp->readformat = prefformat;
240                 ast_setstate(tmp, state);
241                 if (state == AST_STATE_RING)
242                         tmp->rings = 1;
243                 tmp->tech_pvt = i;
244                 strncpy(tmp->context, context, sizeof(tmp->context)-1);
245                 strncpy(tmp->exten, "s",  sizeof(tmp->exten) - 1);
246                 tmp->language[0] = '\0';
247                 i->owner = tmp;
248                 ast_mutex_lock(&usecnt_lock);
249                 usecnt++;
250                 ast_mutex_unlock(&usecnt_lock);
251                 ast_update_use_count();
252                 if (state != AST_STATE_DOWN) {
253                         if (ast_pbx_start(tmp)) {
254                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
255                                 ast_hangup(tmp);
256                         }
257                 }
258         } else
259                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
260         return tmp;
261 }
262
263
264 static struct ast_channel *nbs_request(const char *type, int format, void *data, int *cause)
265 {
266         int oldformat;
267         struct nbs_pvt *p;
268         struct ast_channel *tmp = NULL;
269         
270         oldformat = format;
271         format &= (AST_FORMAT_SLINEAR);
272         if (!format) {
273                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
274                 return NULL;
275         }
276         p = nbs_alloc(data);
277         if (p) {
278                 tmp = nbs_new(p, AST_STATE_DOWN);
279                 if (!tmp)
280                         nbs_destroy(p);
281         }
282         return tmp;
283 }
284
285 static int __unload_module(void)
286 {
287         /* First, take us out of the channel loop */
288         ast_channel_unregister(&nbs_tech);
289         return 0;
290 }
291
292 int unload_module(void)
293 {
294         return __unload_module();
295 }
296
297 int load_module()
298 {
299         /* Make sure we can register our channel type */
300         if (ast_channel_register(&nbs_tech)) {
301                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
302                 __unload_module();
303                 return -1;
304         }
305         return 0;
306 }
307
308 int usecount()
309 {
310         return usecnt;
311 }
312
313 char *description()
314 {
315         return (char *) desc;
316 }
317
318 char *key()
319 {
320         return ASTERISK_GPL_KEY;
321 }