Remove unnecessary checks before calls to ast_strlen_zero. Also, change
[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 #include "asterisk/utils.h"
49
50 static const char desc[] = "Network Broadcast Sound Support";
51 static const char type[] = "NBS";
52 static const char tdesc[] = "Network Broadcast Sound Driver";
53
54 static int usecnt =0;
55
56 /* Only linear is allowed */
57 static int prefformat = AST_FORMAT_SLINEAR;
58
59 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
60
61 static char context[AST_MAX_EXTENSION] = "default";
62
63 /* NBS creates private structures on demand */
64    
65 struct nbs_pvt {
66         NBS *nbs;
67         struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
68         char app[16];                                   /* Our app */
69         char stream[80];                                /* Our stream */
70         struct ast_frame fr;                    /* "null" frame */
71 };
72
73 static struct ast_channel *nbs_request(const char *type, int format, void *data, int *cause);
74 static int nbs_call(struct ast_channel *ast, char *dest, int timeout);
75 static int nbs_hangup(struct ast_channel *ast);
76 static struct ast_frame *nbs_xread(struct ast_channel *ast);
77 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame);
78
79 static const struct ast_channel_tech nbs_tech = {
80         .type = type,
81         .description = tdesc,
82         .capabilities = AST_FORMAT_SLINEAR,
83         .requester = nbs_request,
84         .call = nbs_call,
85         .hangup = nbs_hangup,
86         .read = nbs_xread,
87         .write = nbs_xwrite,
88 };
89
90 static int nbs_call(struct ast_channel *ast, char *dest, int timeout)
91 {
92         struct nbs_pvt *p;
93
94         p = ast->tech_pvt;
95
96         if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
97                 ast_log(LOG_WARNING, "nbs_call called on %s, neither down nor reserved\n", ast->name);
98                 return -1;
99         }
100         /* When we call, it just works, really, there's no destination...  Just
101            ring the phone and wait for someone to answer */
102         if (option_debug)
103                 ast_log(LOG_DEBUG, "Calling %s on %s\n", dest, ast->name);
104
105         /* If we can't connect, return congestion */
106         if (nbs_connect(p->nbs)) {
107                 ast_log(LOG_WARNING, "NBS Connection failed on %s\n", ast->name);
108                 ast_queue_control(ast, AST_CONTROL_CONGESTION);
109         } else {
110                 ast_setstate(ast, AST_STATE_RINGING);
111                 ast_queue_control(ast, AST_CONTROL_ANSWER);
112         }
113
114         return 0;
115 }
116
117 static void nbs_destroy(struct nbs_pvt *p)
118 {
119         if (p->nbs)
120                 nbs_delstream(p->nbs);
121         free(p);
122 }
123
124 static struct nbs_pvt *nbs_alloc(void *data)
125 {
126         struct nbs_pvt *p;
127         int flags = 0;
128         char stream[256] = "";
129         char *opts;
130         strncpy(stream, data, sizeof(stream) - 1);
131         if ((opts = strchr(stream, ':'))) {
132                 *opts = '\0';
133                 opts++;
134         } else
135                 opts = "";
136         p = malloc(sizeof(struct nbs_pvt));
137         if (p) {
138                 memset(p, 0, sizeof(struct nbs_pvt));
139                 if (!ast_strlen_zero(opts)) {
140                         if (strchr(opts, 'm'))
141                                 flags |= NBS_FLAG_MUTE;
142                         if (strchr(opts, 'o'))
143                                 flags |= NBS_FLAG_OVERSPEAK;
144                         if (strchr(opts, 'e'))
145                                 flags |= NBS_FLAG_EMERGENCY;
146                         if (strchr(opts, 'O'))
147                                 flags |= NBS_FLAG_OVERRIDE;
148                 } else
149                         flags = NBS_FLAG_OVERSPEAK;
150                 
151                 strncpy(p->stream, stream, sizeof(p->stream) - 1);
152                 p->nbs = nbs_newstream("asterisk", stream, flags);
153                 if (!p->nbs) {
154                         ast_log(LOG_WARNING, "Unable to allocate new NBS stream '%s' with flags %d\n", stream, flags);
155                         free(p);
156                         p = NULL;
157                 } else {
158                         /* Set for 8000 hz mono, 640 samples */
159                         nbs_setbitrate(p->nbs, 8000);
160                         nbs_setchannels(p->nbs, 1);
161                         nbs_setblocksize(p->nbs, 640);
162                         nbs_setblocking(p->nbs, 0);
163                 }
164         }
165         return p;
166 }
167
168 static int nbs_hangup(struct ast_channel *ast)
169 {
170         struct nbs_pvt *p;
171         p = ast->tech_pvt;
172         if (option_debug)
173                 ast_log(LOG_DEBUG, "nbs_hangup(%s)\n", ast->name);
174         if (!ast->tech_pvt) {
175                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
176                 return 0;
177         }
178         nbs_destroy(p);
179         ast->tech_pvt = NULL;
180         ast_setstate(ast, AST_STATE_DOWN);
181         return 0;
182 }
183
184 static struct ast_frame  *nbs_xread(struct ast_channel *ast)
185 {
186         struct nbs_pvt *p = ast->tech_pvt;
187         
188
189         /* Some nice norms */
190         p->fr.datalen = 0;
191         p->fr.samples = 0;
192         p->fr.data =  NULL;
193         p->fr.src = type;
194         p->fr.offset = 0;
195         p->fr.mallocd=0;
196         p->fr.delivery.tv_sec = 0;
197         p->fr.delivery.tv_usec = 0;
198
199         ast_log(LOG_DEBUG, "Returning null frame on %s\n", ast->name);
200
201         return &p->fr;
202 }
203
204 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame)
205 {
206         struct nbs_pvt *p = ast->tech_pvt;
207         /* Write a frame of (presumably voice) data */
208         if (frame->frametype != AST_FRAME_VOICE) {
209                 if (frame->frametype != AST_FRAME_IMAGE)
210                         ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
211                 return 0;
212         }
213         if (!(frame->subclass &
214                 (AST_FORMAT_SLINEAR))) {
215                 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
216                 return 0;
217         }
218         if (ast->_state != AST_STATE_UP) {
219                 /* Don't try tos end audio on-hook */
220                 return 0;
221         }
222         if (nbs_write(p->nbs, frame->data, frame->datalen / 2) < 0) 
223                 return -1;
224         return 0;
225 }
226
227 static struct ast_channel *nbs_new(struct nbs_pvt *i, int state)
228 {
229         struct ast_channel *tmp;
230         tmp = ast_channel_alloc(1);
231         if (tmp) {
232                 tmp->tech = &nbs_tech;
233                 snprintf(tmp->name, sizeof(tmp->name), "NBS/%s", i->stream);
234                 tmp->type = type;
235                 tmp->fds[0] = nbs_fd(i->nbs);
236                 tmp->nativeformats = prefformat;
237                 tmp->rawreadformat = prefformat;
238                 tmp->rawwriteformat = prefformat;
239                 tmp->writeformat = prefformat;
240                 tmp->readformat = prefformat;
241                 ast_setstate(tmp, state);
242                 if (state == AST_STATE_RING)
243                         tmp->rings = 1;
244                 tmp->tech_pvt = i;
245                 strncpy(tmp->context, context, sizeof(tmp->context)-1);
246                 strncpy(tmp->exten, "s",  sizeof(tmp->exten) - 1);
247                 tmp->language[0] = '\0';
248                 i->owner = tmp;
249                 ast_mutex_lock(&usecnt_lock);
250                 usecnt++;
251                 ast_mutex_unlock(&usecnt_lock);
252                 ast_update_use_count();
253                 if (state != AST_STATE_DOWN) {
254                         if (ast_pbx_start(tmp)) {
255                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
256                                 ast_hangup(tmp);
257                         }
258                 }
259         } else
260                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
261         return tmp;
262 }
263
264
265 static struct ast_channel *nbs_request(const char *type, int format, void *data, int *cause)
266 {
267         int oldformat;
268         struct nbs_pvt *p;
269         struct ast_channel *tmp = NULL;
270         
271         oldformat = format;
272         format &= (AST_FORMAT_SLINEAR);
273         if (!format) {
274                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
275                 return NULL;
276         }
277         p = nbs_alloc(data);
278         if (p) {
279                 tmp = nbs_new(p, AST_STATE_DOWN);
280                 if (!tmp)
281                         nbs_destroy(p);
282         }
283         return tmp;
284 }
285
286 static int __unload_module(void)
287 {
288         /* First, take us out of the channel loop */
289         ast_channel_unregister(&nbs_tech);
290         return 0;
291 }
292
293 int unload_module(void)
294 {
295         return __unload_module();
296 }
297
298 int load_module()
299 {
300         /* Make sure we can register our channel type */
301         if (ast_channel_register(&nbs_tech)) {
302                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
303                 __unload_module();
304                 return -1;
305         }
306         return 0;
307 }
308
309 int usecount()
310 {
311         return usecnt;
312 }
313
314 char *description()
315 {
316         return (char *) desc;
317 }
318
319 char *key()
320 {
321         return ASTERISK_GPL_KEY;
322 }