Merge "stasic.c: Fix printf format type mismatches with arguments."
[asterisk/asterisk.git] / channels / chan_nbs.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, 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 Network broadcast sound support channel driver
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \ingroup channel_drivers
26  */
27
28 /*** MODULEINFO
29         <depend>nbs</depend>
30         <support_level>deprecated</support_level>
31  ***/
32
33 #include "asterisk.h"
34
35 #include <sys/socket.h>
36 #include <sys/time.h>
37 #include <arpa/inet.h>
38 #include <fcntl.h>
39 #include <sys/ioctl.h>
40 #include <nbs.h>
41
42 #include "asterisk/lock.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/config.h"
45 #include "asterisk/module.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/utils.h"
48 #include "asterisk/format_cache.h"
49
50 static const char tdesc[] = "Network Broadcast Sound Driver";
51
52 static char context[AST_MAX_EXTENSION] = "default";
53 static const char type[] = "NBS";
54
55 /* NBS creates private structures on demand */
56
57 struct nbs_pvt {
58         NBS *nbs;
59         struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
60         char app[16];                                   /* Our app */
61         char stream[80];                                /* Our stream */
62         struct ast_module_user *u;              /*! for holding a reference to this module */
63 };
64
65 static struct ast_channel *nbs_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
66 static int nbs_call(struct ast_channel *ast, const char *dest, int timeout);
67 static int nbs_hangup(struct ast_channel *ast);
68 static struct ast_frame *nbs_xread(struct ast_channel *ast);
69 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame);
70
71 static struct ast_channel_tech nbs_tech = {
72         .type = type,
73         .description = tdesc,
74         .requester = nbs_request,
75         .call = nbs_call,
76         .hangup = nbs_hangup,
77         .read = nbs_xread,
78         .write = nbs_xwrite,
79 };
80
81 static int nbs_call(struct ast_channel *ast, const char *dest, int timeout)
82 {
83         struct nbs_pvt *p;
84
85         p = ast_channel_tech_pvt(ast);
86
87         if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
88                 ast_log(LOG_WARNING, "nbs_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
89                 return -1;
90         }
91         /* When we call, it just works, really, there's no destination...  Just
92            ring the phone and wait for someone to answer */
93         ast_debug(1, "Calling %s on %s\n", dest, ast_channel_name(ast));
94
95         /* If we can't connect, return congestion */
96         if (nbs_connect(p->nbs)) {
97                 ast_log(LOG_WARNING, "NBS Connection failed on %s\n", ast_channel_name(ast));
98                 ast_queue_control(ast, AST_CONTROL_CONGESTION);
99         } else {
100                 ast_setstate(ast, AST_STATE_RINGING);
101                 ast_queue_control(ast, AST_CONTROL_ANSWER);
102         }
103
104         return 0;
105 }
106
107 static void nbs_destroy(struct nbs_pvt *p)
108 {
109         if (p->nbs)
110                 nbs_delstream(p->nbs);
111         ast_module_user_remove(p->u);
112         ast_free(p);
113 }
114
115 static struct nbs_pvt *nbs_alloc(const char *data)
116 {
117         struct nbs_pvt *p;
118         int flags = 0;
119         char stream[256];
120         char *opts;
121
122         ast_copy_string(stream, data, sizeof(stream));
123         if ((opts = strchr(stream, ':'))) {
124                 *opts = '\0';
125                 opts++;
126         } else
127                 opts = "";
128         p = ast_calloc(1, sizeof(*p));
129         if (p) {
130                 if (!ast_strlen_zero(opts)) {
131                         if (strchr(opts, 'm'))
132                                 flags |= NBS_FLAG_MUTE;
133                         if (strchr(opts, 'o'))
134                                 flags |= NBS_FLAG_OVERSPEAK;
135                         if (strchr(opts, 'e'))
136                                 flags |= NBS_FLAG_EMERGENCY;
137                         if (strchr(opts, 'O'))
138                                 flags |= NBS_FLAG_OVERRIDE;
139                 } else
140                         flags = NBS_FLAG_OVERSPEAK;
141
142                 ast_copy_string(p->stream, stream, sizeof(p->stream));
143                 p->nbs = nbs_newstream("asterisk", stream, flags);
144                 if (!p->nbs) {
145                         ast_log(LOG_WARNING, "Unable to allocate new NBS stream '%s' with flags %d\n", stream, flags);
146                         ast_free(p);
147                         p = NULL;
148                 } else {
149                         /* Set for 8000 hz mono, 640 samples */
150                         nbs_setbitrate(p->nbs, 8000);
151                         nbs_setchannels(p->nbs, 1);
152                         nbs_setblocksize(p->nbs, 640);
153                         nbs_setblocking(p->nbs, 0);
154                 }
155         }
156         return p;
157 }
158
159 static int nbs_hangup(struct ast_channel *ast)
160 {
161         struct nbs_pvt *p;
162         p = ast_channel_tech_pvt(ast);
163         ast_debug(1, "nbs_hangup(%s)\n", ast_channel_name(ast));
164         if (!ast_channel_tech_pvt(ast)) {
165                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
166                 return 0;
167         }
168         nbs_destroy(p);
169         ast_channel_tech_pvt_set(ast, NULL);
170         ast_setstate(ast, AST_STATE_DOWN);
171         return 0;
172 }
173
174 static struct ast_frame  *nbs_xread(struct ast_channel *ast)
175 {
176         ast_debug(1, "Returning null frame on %s\n", ast_channel_name(ast));
177
178         return &ast_null_frame;
179 }
180
181 static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame)
182 {
183         struct nbs_pvt *p = ast_channel_tech_pvt(ast);
184         if (ast_channel_state(ast) != AST_STATE_UP) {
185                 /* Don't try tos end audio on-hook */
186                 return 0;
187         }
188         if (nbs_write(p->nbs, frame->data.ptr, frame->datalen / 2) < 0)
189                 return -1;
190         return 0;
191 }
192
193 static struct ast_channel *nbs_new(struct nbs_pvt *i, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
194 {
195         struct ast_channel *tmp;
196         tmp = ast_channel_alloc(1, state, 0, 0, "", "s", context, assignedids, requestor, 0, "NBS/%s", i->stream);
197         if (tmp) {
198                 ast_channel_tech_set(tmp, &nbs_tech);
199                 ast_channel_set_fd(tmp, 0, nbs_fd(i->nbs));
200
201                 ast_channel_nativeformats_set(tmp, nbs_tech.capabilities);
202                 ast_channel_set_rawreadformat(tmp, ast_format_slin);
203                 ast_channel_set_rawwriteformat(tmp, ast_format_slin);
204                 ast_channel_set_writeformat(tmp, ast_format_slin);
205                 ast_channel_set_readformat(tmp, ast_format_slin);
206                 if (state == AST_STATE_RING)
207                         ast_channel_rings_set(tmp, 1);
208                 ast_channel_tech_pvt_set(tmp, i);
209                 ast_channel_context_set(tmp, context);
210                 ast_channel_exten_set(tmp, "s");
211                 ast_channel_language_set(tmp, "");
212                 i->owner = tmp;
213                 i->u = ast_module_user_add(tmp);
214                 ast_channel_unlock(tmp);
215                 if (state != AST_STATE_DOWN) {
216                         if (ast_pbx_start(tmp)) {
217                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
218                                 ast_hangup(tmp);
219                         }
220                 }
221         } else
222                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
223         return tmp;
224 }
225
226
227 static struct ast_channel *nbs_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
228 {
229         struct nbs_pvt *p;
230         struct ast_channel *tmp = NULL;
231
232         if (ast_format_cap_iscompatible_format(cap, ast_format_slin) == AST_FORMAT_CMP_NOT_EQUAL) {
233                 struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
234
235                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n",
236                         ast_format_cap_get_names(cap, &cap_buf));
237                 return NULL;
238         }
239         p = nbs_alloc(data);
240         if (p) {
241                 tmp = nbs_new(p, AST_STATE_DOWN, assignedids, requestor);
242                 if (!tmp)
243                         nbs_destroy(p);
244         }
245         return tmp;
246 }
247
248 static int unload_module(void)
249 {
250         /* First, take us out of the channel loop */
251         ast_channel_unregister(&nbs_tech);
252         ao2_ref(nbs_tech.capabilities, -1);
253         nbs_tech.capabilities = NULL;
254         return 0;
255 }
256
257 static int load_module(void)
258 {
259         if (!(nbs_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
260                 return AST_MODULE_LOAD_DECLINE;
261         }
262         ast_format_cap_append(nbs_tech.capabilities, ast_format_slin, 0);
263         /* Make sure we can register our channel type */
264         if (ast_channel_register(&nbs_tech)) {
265                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
266                 ao2_ref(nbs_tech.capabilities, -1);
267                 nbs_tech.capabilities = NULL;
268                 return AST_MODULE_LOAD_DECLINE;
269         }
270         return AST_MODULE_LOAD_SUCCESS;
271 }
272
273 AST_MODULE_INFO_STANDARD_DEPRECATED(ASTERISK_GPL_KEY, "Network Broadcast Sound Support");