2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2006 - 2007, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
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.
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.
21 * \brief Cross-platform console channel driver
23 * \author Russell Bryant <russell@digium.com>
25 * \note Some of the code in this file came from chan_oss and chan_alsa.
26 * chan_oss, Mark Spencer <markster@digium.com>
27 * chan_oss, Luigi Rizzo
28 * chan_alsa, Matthew Fredrickson <creslin@digium.com>
30 * \ingroup channel_drivers
32 * \extref Portaudio http://www.portaudio.com/
34 * To install portaudio v19 from svn, check it out using the following command:
35 * - svn co https://www.portaudio.com/repos/portaudio/branches/v19-devel
37 * \note Since this works with any audio system that libportaudio supports,
38 * including ALSA and OSS, this may someday deprecate chan_alsa and chan_oss.
39 * However, before that can be done, it needs to *at least* have all of the
40 * features that these other channel drivers have. The features implemented
41 * in at least one of the other console channel drivers that are not yet
42 * implemented here are:
44 * - Multiple device support
45 * - with "active" CLI command
46 * - Set Auto-answer from the dialplan
47 * - transfer CLI command
48 * - boost CLI command and .conf option
49 * - console_video support
53 <depend>portaudio</depend>
58 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
60 #include <sys/signal.h> /* SIGURG */
62 #include <portaudio.h>
64 #include "asterisk/module.h"
65 #include "asterisk/channel.h"
66 #include "asterisk/pbx.h"
67 #include "asterisk/causes.h"
68 #include "asterisk/cli.h"
69 #include "asterisk/musiconhold.h"
70 #include "asterisk/callerid.h"
73 * \brief The sample rate to request from PortAudio
75 * \todo Make this optional. If this is only going to talk to 8 kHz endpoints,
76 * then it makes sense to use 8 kHz natively.
78 #define SAMPLE_RATE 16000
81 * \brief The number of samples to configure the portaudio stream for
83 * 320 samples (20 ms) is the most common frame size in Asterisk. So, the code
84 * in this module reads 320 sample frames from the portaudio stream and queues
85 * them up on the Asterisk channel. Frames of any size can be written to a
86 * portaudio stream, but the portaudio documentation does say that for high
87 * performance applications, the data should be written to Pa_WriteStream in
88 * the same size as what is used to initialize the stream.
90 #define NUM_SAMPLES 320
92 /*! \brief Mono Input */
93 #define INPUT_CHANNELS 1
95 /*! \brief Mono Output */
96 #define OUTPUT_CHANNELS 1
99 * \brief Maximum text message length
100 * \note This should be changed if there is a common definition somewhere
101 * that defines the maximum length of a text message.
103 #define TEXT_SIZE 256
106 #define MIN(a,b) ((a) < (b) ? (a) : (b))
109 #define MAX(a,b) ((a) > (b) ? (a) : (b))
112 /*! \brief Dance, Kirby, Dance! @{ */
113 #define V_BEGIN " --- <(\"<) --- "
114 #define V_END " --- (>\")> ---\n"
117 static const char config_file[] = "console.conf";
120 * \brief Console pvt structure
122 * Currently, this is a singleton object. However, multiple instances will be
123 * needed when this module is updated for multiple device support.
125 static struct console_pvt {
126 AST_DECLARE_STRING_FIELDS(
127 /*! Name of the device */
128 AST_STRING_FIELD(name);
129 /*! Default context for outgoing calls */
130 AST_STRING_FIELD(context);
131 /*! Default extension for outgoing calls */
132 AST_STRING_FIELD(exten);
133 /*! Default CallerID number */
134 AST_STRING_FIELD(cid_num);
135 /*! Default CallerID name */
136 AST_STRING_FIELD(cid_name);
137 /*! Default MOH class to listen to, if:
138 * - No MOH class set on the channel
139 * - Peer channel putting this device on hold did not suggest a class */
140 AST_STRING_FIELD(mohinterpret);
141 /*! Default language */
142 AST_STRING_FIELD(language);
144 /*! Current channel for this device */
145 struct ast_channel *owner;
146 /*! Current PortAudio stream for this device */
148 /*! A frame for preparing to queue on to the channel */
150 /*! Running = 1, Not running = 0 */
151 unsigned int streamstate:1;
152 /*! On-hook = 0, Off-hook = 1 */
153 unsigned int hookstate:1;
154 /*! Unmuted = 0, Muted = 1 */
155 unsigned int muted:1;
156 /*! Automatically answer incoming calls */
157 unsigned int autoanswer:1;
158 /*! Ignore context in the console dial CLI command */
159 unsigned int overridecontext:1;
160 /*! Lock to protect data in this struct */
162 /*! ID for the stream monitor thread */
165 .__lock = AST_MUTEX_INIT_VALUE,
166 .thread = AST_PTHREADT_NULL,
170 * \brief Global jitterbuffer configuration
172 * \note Disabled by default.
174 static struct ast_jb_conf default_jbconf = {
177 .resync_threshold = -1,
180 static struct ast_jb_conf global_jbconf;
182 /*! Channel Technology Callbacks @{ */
183 static struct ast_channel *console_request(const char *type, int format,
184 void *data, int *cause);
185 static int console_digit_begin(struct ast_channel *c, char digit);
186 static int console_digit_end(struct ast_channel *c, char digit, unsigned int duration);
187 static int console_text(struct ast_channel *c, const char *text);
188 static int console_hangup(struct ast_channel *c);
189 static int console_answer(struct ast_channel *c);
190 static struct ast_frame *console_read(struct ast_channel *chan);
191 static int console_call(struct ast_channel *c, char *dest, int timeout);
192 static int console_write(struct ast_channel *chan, struct ast_frame *f);
193 static int console_indicate(struct ast_channel *chan, int cond,
194 const void *data, size_t datalen);
195 static int console_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
199 * \brief Formats natively supported by this module.
201 #define SUPPORTED_FORMATS ( AST_FORMAT_SLINEAR16 )
203 static const struct ast_channel_tech console_tech = {
205 .description = "Console Channel Driver",
206 .capabilities = SUPPORTED_FORMATS,
207 .requester = console_request,
208 .send_digit_begin = console_digit_begin,
209 .send_digit_end = console_digit_end,
210 .send_text = console_text,
211 .hangup = console_hangup,
212 .answer = console_answer,
213 .read = console_read,
214 .call = console_call,
215 .write = console_write,
216 .indicate = console_indicate,
217 .fixup = console_fixup,
220 /*! \brief lock a console_pvt struct */
221 #define console_pvt_lock(pvt) ast_mutex_lock(&(pvt)->__lock)
223 /*! \brief unlock a console_pvt struct */
224 #define console_pvt_unlock(pvt) ast_mutex_unlock(&(pvt)->__lock)
227 * \brief Stream monitor thread
229 * \arg data A pointer to the console_pvt structure that contains the portaudio
230 * stream that needs to be monitored.
232 * This function runs in its own thread to monitor data coming in from a
233 * portaudio stream. When enough data is available, it is queued up to
234 * be read from the Asterisk channel.
236 static void *stream_monitor(void *data)
238 struct console_pvt *pvt = data;
239 char buf[NUM_SAMPLES * sizeof(int16_t)];
241 struct ast_frame f = {
242 .frametype = AST_FRAME_VOICE,
243 .subclass = AST_FORMAT_SLINEAR16,
244 .src = "console_stream_monitor",
246 .datalen = sizeof(buf),
247 .samples = sizeof(buf) / sizeof(int16_t),
251 pthread_testcancel();
252 res = Pa_ReadStream(pvt->stream, buf, sizeof(buf) / sizeof(int16_t));
253 pthread_testcancel();
255 if (res == paNoError)
256 ast_queue_frame(pvt->owner, &f);
262 static int start_stream(struct console_pvt *pvt)
267 console_pvt_lock(pvt);
269 if (pvt->streamstate)
272 pvt->streamstate = 1;
273 ast_debug(1, "Starting stream\n");
275 res = Pa_OpenDefaultStream(&pvt->stream, INPUT_CHANNELS, OUTPUT_CHANNELS,
276 paInt16, SAMPLE_RATE, NUM_SAMPLES, NULL, NULL);
277 if (res != paNoError) {
278 ast_log(LOG_WARNING, "Failed to open default audio device - (%d) %s\n",
279 res, Pa_GetErrorText(res));
284 res = Pa_StartStream(pvt->stream);
285 if (res != paNoError) {
286 ast_log(LOG_WARNING, "Failed to start stream - (%d) %s\n",
287 res, Pa_GetErrorText(res));
292 if (ast_pthread_create_background(&pvt->thread, NULL, stream_monitor, pvt)) {
293 ast_log(LOG_ERROR, "Failed to start stream monitor thread\n");
298 console_pvt_unlock(pvt);
303 static int stop_stream(struct console_pvt *pvt)
305 if (!pvt->streamstate)
308 pthread_cancel(pvt->thread);
309 pthread_kill(pvt->thread, SIGURG);
310 pthread_join(pvt->thread, NULL);
312 console_pvt_lock(pvt);
313 Pa_AbortStream(pvt->stream);
314 Pa_CloseStream(pvt->stream);
316 pvt->streamstate = 0;
317 console_pvt_unlock(pvt);
323 * \note Called with the pvt struct locked
325 static struct ast_channel *console_new(struct console_pvt *pvt, const char *ext, const char *ctx, int state)
327 struct ast_channel *chan;
329 if (!(chan = ast_channel_alloc(1, state, pvt->cid_num, pvt->cid_name, NULL,
330 ext, ctx, 0, "Console/%s", pvt->name))) {
334 chan->tech = &console_tech;
335 chan->nativeformats = AST_FORMAT_SLINEAR16;
336 chan->readformat = AST_FORMAT_SLINEAR16;
337 chan->writeformat = AST_FORMAT_SLINEAR16;
338 chan->tech_pvt = pvt;
342 if (!ast_strlen_zero(pvt->language))
343 ast_string_field_set(chan, language, pvt->language);
345 ast_jb_configure(chan, &global_jbconf);
347 if (state != AST_STATE_DOWN) {
348 if (ast_pbx_start(chan)) {
349 chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
359 static struct ast_channel *console_request(const char *type, int format, void *data, int *cause)
361 int oldformat = format;
362 struct ast_channel *chan;
363 struct console_pvt *pvt = &console_pvt;
365 format &= SUPPORTED_FORMATS;
367 ast_log(LOG_NOTICE, "Channel requested with unsupported format(s): '%d'\n", oldformat);
372 ast_log(LOG_NOTICE, "Console channel already active!\n");
373 *cause = AST_CAUSE_BUSY;
377 console_pvt_lock(pvt);
378 chan = console_new(pvt, NULL, NULL, AST_STATE_DOWN);
379 console_pvt_unlock(pvt);
382 ast_log(LOG_WARNING, "Unable to create new Console channel!\n");
387 static int console_digit_begin(struct ast_channel *c, char digit)
389 ast_verb(1, V_BEGIN "Console Received Beginning of Digit %c" V_END, digit);
391 return -1; /* non-zero to request inband audio */
394 static int console_digit_end(struct ast_channel *c, char digit, unsigned int duration)
396 ast_verb(1, V_BEGIN "Console Received End of Digit %c (duration %u)" V_END,
399 return -1; /* non-zero to request inband audio */
402 static int console_text(struct ast_channel *c, const char *text)
404 ast_verb(1, V_BEGIN "Console Received Text '%s'" V_END, text);
409 static int console_hangup(struct ast_channel *c)
411 struct console_pvt *pvt = &console_pvt;
413 ast_verb(1, V_BEGIN "Hangup on Console" V_END);
424 static int console_answer(struct ast_channel *c)
426 struct console_pvt *pvt = &console_pvt;
428 ast_verb(1, V_BEGIN "Call from Console has been Answered" V_END);
430 ast_setstate(c, AST_STATE_UP);
432 return start_stream(pvt);
436 * \brief Implementation of the ast_channel_tech read() callback
438 * Calling this function is harmless. However, if it does get called, it
439 * is an indication that something weird happened that really shouldn't
440 * have and is worth looking into.
442 * Why should this function not get called? Well, let me explain. There are
443 * a couple of ways to pass on audio that has come from this channel. The way
444 * that this channel driver uses is that once the audio is available, it is
445 * wrapped in an ast_frame and queued onto the channel using ast_queue_frame().
447 * The other method would be signalling to the core that there is audio waiting,
448 * and that it needs to call the channel's read() callback to get it. The way
449 * the channel gets signalled is that one or more file descriptors are placed
450 * in the fds array on the ast_channel which the core will poll() on. When the
451 * fd indicates that input is available, the read() callback is called. This
452 * is especially useful when there is a dedicated file descriptor where the
453 * audio is read from. An example would be the socket for an RTP stream.
455 static struct ast_frame *console_read(struct ast_channel *chan)
457 ast_debug(1, "I should not be called ...\n");
459 return &ast_null_frame;
462 static int console_call(struct ast_channel *c, char *dest, int timeout)
464 struct ast_frame f = { 0, };
465 struct console_pvt *pvt = &console_pvt;
467 ast_verb(1, V_BEGIN "Call to device '%s' on console from '%s' <%s>" V_END,
468 dest, c->cid.cid_name, c->cid.cid_num);
470 console_pvt_lock(pvt);
472 if (pvt->autoanswer) {
474 console_pvt_unlock(pvt);
475 ast_verb(1, V_BEGIN "Auto-answered" V_END);
476 f.frametype = AST_FRAME_CONTROL;
477 f.subclass = AST_CONTROL_ANSWER;
479 console_pvt_unlock(pvt);
480 ast_verb(1, V_BEGIN "Type 'console answer' to answer, or use the 'autoanswer' option "
481 "for future calls" V_END);
482 f.frametype = AST_FRAME_CONTROL;
483 f.subclass = AST_CONTROL_RINGING;
484 ast_indicate(c, AST_CONTROL_RINGING);
487 ast_queue_frame(c, &f);
489 return start_stream(pvt);
492 static int console_write(struct ast_channel *chan, struct ast_frame *f)
494 struct console_pvt *pvt = &console_pvt;
496 Pa_WriteStream(pvt->stream, f->data, f->samples);
501 static int console_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
503 struct console_pvt *pvt = chan->tech_pvt;
507 case AST_CONTROL_BUSY:
508 case AST_CONTROL_CONGESTION:
509 case AST_CONTROL_RINGING:
511 res = -1; /* Ask for inband indications */
513 case AST_CONTROL_PROGRESS:
514 case AST_CONTROL_PROCEEDING:
515 case AST_CONTROL_VIDUPDATE:
517 case AST_CONTROL_HOLD:
518 ast_verb(1, V_BEGIN "Console Has Been Placed on Hold" V_END);
519 ast_moh_start(chan, data, pvt->mohinterpret);
521 case AST_CONTROL_UNHOLD:
522 ast_verb(1, V_BEGIN "Console Has Been Retrieved from Hold" V_END);
526 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n",
528 /* The core will play inband indications for us if appropriate */
535 static int console_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
537 struct console_pvt *pvt = &console_pvt;
539 pvt->owner = newchan;
545 * split a string in extension-context, returns pointers to malloc'ed
547 * If we do not have 'overridecontext' then the last @ is considered as
548 * a context separator, and the context is overridden.
549 * This is usually not very necessary as you can play with the dialplan,
550 * and it is nice not to need it because you have '@' in SIP addresses.
551 * Return value is the buffer address.
553 * \note came from chan_oss
555 static char *ast_ext_ctx(struct console_pvt *pvt, const char *src, char **ext, char **ctx)
557 if (ext == NULL || ctx == NULL)
558 return NULL; /* error */
562 if (src && *src != '\0')
563 *ext = ast_strdup(src);
568 if (!pvt->overridecontext) {
569 /* parse from the right */
570 *ctx = strrchr(*ext, '@');
578 static char *cli_console_autoanswer(struct ast_cli_entry *e, int cmd,
579 struct ast_cli_args *a)
581 struct console_pvt *pvt = &console_pvt;
585 e->command = "console set autoanswer [on|off]";
587 "Usage: console set autoanswer [on|off]\n"
588 " Enables or disables autoanswer feature. If used without\n"
589 " argument, displays the current on/off status of autoanswer.\n"
590 " The default value of autoanswer is in 'oss.conf'.\n";
597 if (a->argc == e->args - 1) {
598 ast_cli(a->fd, "Auto answer is %s.\n", pvt->autoanswer ? "on" : "off");
602 if (a->argc != e->args)
603 return CLI_SHOWUSAGE;
606 ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n",
611 if (!strcasecmp(a->argv[e->args-1], "on"))
613 else if (!strcasecmp(a->argv[e->args - 1], "off"))
616 return CLI_SHOWUSAGE;
621 static char *cli_console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
623 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH };
624 struct console_pvt *pvt = &console_pvt;
626 if (cmd == CLI_INIT) {
627 e->command = "console flash";
629 "Usage: console flash\n"
630 " Flashes the call currently placed on the console.\n";
632 } else if (cmd == CLI_GENERATE)
635 if (a->argc != e->args)
636 return CLI_SHOWUSAGE;
639 ast_cli(a->fd, "No call to flash\n");
645 ast_queue_frame(pvt->owner, &f);
650 static char *cli_console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
653 const char *mye = NULL, *myc = NULL;
654 struct console_pvt *pvt = &console_pvt;
656 if (cmd == CLI_INIT) {
657 e->command = "console dial";
659 "Usage: console dial [extension[@context]]\n"
660 " Dials a given extension (and context if specified)\n";
662 } else if (cmd == CLI_GENERATE)
665 if (a->argc > e->args + 1)
666 return CLI_SHOWUSAGE;
668 if (pvt->owner) { /* already in a call */
670 struct ast_frame f = { AST_FRAME_DTMF, 0 };
672 if (a->argc == e->args) { /* argument is mandatory here */
673 ast_cli(a->fd, "Already in a call. You can only dial digits until you hangup.\n");
676 s = a->argv[e->args];
677 /* send the string one char at a time */
678 for (i = 0; i < strlen(s); i++) {
680 ast_queue_frame(pvt->owner, &f);
685 /* if we have an argument split it into extension and context */
686 if (a->argc == e->args + 1) {
687 char *ext = NULL, *con = NULL;
688 s = ast_ext_ctx(pvt, a->argv[e->args], &ext, &con);
689 ast_debug(1, "provided '%s', exten '%s' context '%s'\n",
690 a->argv[e->args], mye, myc);
695 /* supply default values if needed */
696 if (ast_strlen_zero(mye))
698 if (ast_strlen_zero(myc))
701 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
702 console_pvt_lock(pvt);
704 console_new(pvt, mye, myc, AST_STATE_RINGING);
705 console_pvt_unlock(pvt);
707 ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
715 static char *cli_console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
717 struct console_pvt *pvt = &console_pvt;
719 if (cmd == CLI_INIT) {
720 e->command = "console hangup";
722 "Usage: console hangup\n"
723 " Hangs up any call currently placed on the console.\n";
725 } else if (cmd == CLI_GENERATE)
728 if (a->argc != e->args)
729 return CLI_SHOWUSAGE;
731 if (!pvt->owner && !pvt->hookstate) {
732 ast_cli(a->fd, "No call to hang up\n");
738 ast_queue_hangup(pvt->owner);
743 static char *cli_console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
746 struct console_pvt *pvt = &console_pvt;
748 if (cmd == CLI_INIT) {
749 e->command = "console {mute|unmute}";
751 "Usage: console {mute|unmute}\n"
752 " Mute/unmute the microphone.\n";
754 } else if (cmd == CLI_GENERATE)
757 if (a->argc != e->args)
758 return CLI_SHOWUSAGE;
760 s = a->argv[e->args-1];
761 if (!strcasecmp(s, "mute"))
763 else if (!strcasecmp(s, "unmute"))
766 return CLI_SHOWUSAGE;
768 ast_verb(1, V_BEGIN "The Console is now %s" V_END,
769 pvt->muted ? "Muted" : "Unmuted");
774 static char *cli_list_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
776 PaDeviceIndex index, num, def_input, def_output;
778 if (cmd == CLI_INIT) {
779 e->command = "console list devices";
781 "Usage: console list devices\n"
782 " List all available devices.\n";
784 } else if (cmd == CLI_GENERATE)
787 if (a->argc != e->args)
788 return CLI_SHOWUSAGE;
790 ast_cli(a->fd, "Available Devices:\n---------------------------------\n");
792 num = Pa_GetDeviceCount();
794 ast_cli(a->fd, "(None)\n");
798 def_input = Pa_GetDefaultInputDevice();
799 def_output = Pa_GetDefaultOutputDevice();
800 for (index = 0; index < num; index++) {
801 const PaDeviceInfo *dev = Pa_GetDeviceInfo(index);
804 ast_cli(a->fd, "Device Name: %s\n", dev->name);
805 if (index == def_input)
806 ast_cli(a->fd, " ---> Default Input Device\n");
807 if (index == def_output)
808 ast_cli(a->fd, " ---> Default Output Device\n");
815 * \brief answer command from the console
817 static char *cli_console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
819 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
820 struct console_pvt *pvt = &console_pvt;
824 e->command = "console answer";
826 "Usage: console answer\n"
827 " Answers an incoming call on the console channel.\n";
831 return NULL; /* no completion */
834 if (a->argc != e->args)
835 return CLI_SHOWUSAGE;
838 ast_cli(a->fd, "No one is calling us\n");
844 ast_indicate(pvt->owner, -1);
846 ast_queue_frame(pvt->owner, &f);
852 * \brief Console send text CLI command
854 * \note concatenate all arguments into a single string. argv is NULL-terminated
855 * so we can use it right away
857 static char *cli_console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
860 struct console_pvt *pvt = &console_pvt;
861 struct ast_frame f = {
862 .frametype = AST_FRAME_TEXT,
864 .src = "console_send_text",
868 if (cmd == CLI_INIT) {
869 e->command = "console send text";
871 "Usage: console send text <message>\n"
872 " Sends a text message for display on the remote terminal.\n";
874 } else if (cmd == CLI_GENERATE)
877 if (a->argc < e->args + 1)
878 return CLI_SHOWUSAGE;
881 ast_cli(a->fd, "Not in a call\n");
885 ast_join(buf, sizeof(buf) - 1, a->argv + e->args);
886 if (ast_strlen_zero(buf))
887 return CLI_SHOWUSAGE;
893 ast_queue_frame(pvt->owner, &f);
898 static struct ast_cli_entry cli_console[] = {
899 AST_CLI_DEFINE(cli_console_dial, "Dial an extension from the console"),
900 AST_CLI_DEFINE(cli_console_hangup, "Hangup a call on the console"),
901 AST_CLI_DEFINE(cli_console_mute, "Disable/Enable mic input"),
902 AST_CLI_DEFINE(cli_console_answer, "Answer an incoming console call"),
903 AST_CLI_DEFINE(cli_console_sendtext, "Send text to a connected party"),
904 AST_CLI_DEFINE(cli_console_flash, "Send a flash to the connected party"),
905 AST_CLI_DEFINE(cli_console_autoanswer, "Turn autoanswer on or off"),
906 AST_CLI_DEFINE(cli_list_devices, "List available devices"),
910 * \brief Set default values for a pvt struct
912 * \note This function expects the pvt lock to be held.
914 static void set_pvt_defaults(struct console_pvt *pvt, int reload)
917 /* This should be changed for multiple device support. Right now,
918 * there is no way to change the name of a device. The default
919 * input and output sound devices are the only ones supported. */
920 ast_string_field_set(pvt, name, "default");
923 ast_string_field_set(pvt, mohinterpret, "default");
924 ast_string_field_set(pvt, context, "default");
925 ast_string_field_set(pvt, exten, "s");
926 ast_string_field_set(pvt, language, "");
927 ast_string_field_set(pvt, cid_num, "");
928 ast_string_field_set(pvt, cid_name, "");
930 pvt->overridecontext = 0;
934 static void store_callerid(struct console_pvt *pvt, const char *value)
939 ast_callerid_split(value, cid_name, sizeof(cid_name),
940 cid_num, sizeof(cid_num));
942 ast_string_field_set(pvt, cid_name, cid_name);
943 ast_string_field_set(pvt, cid_num, cid_num);
947 * \brief Store a configuration parameter in a pvt struct
949 * \note This function expects the pvt lock to be held.
951 static void store_config_core(struct console_pvt *pvt, const char *var, const char *value)
953 if (!ast_jb_read_conf(&global_jbconf, var, value))
956 CV_START(var, value);
958 CV_STRFIELD("context", pvt, context);
959 CV_STRFIELD("extension", pvt, exten);
960 CV_STRFIELD("mohinterpret", pvt, mohinterpret);
961 CV_STRFIELD("language", pvt, language);
962 CV_F("callerid", store_callerid(pvt, value));
963 CV_BOOL("overridecontext", pvt->overridecontext);
964 CV_BOOL("autoanswer", pvt->autoanswer);
966 ast_log(LOG_WARNING, "Unknown option '%s'\n", var);
972 * \brief Load the configuration
973 * \param reload if this was called due to a reload
977 static int load_config(int reload)
979 struct ast_config *cfg;
980 struct ast_variable *v;
981 struct console_pvt *pvt = &console_pvt;
982 struct ast_flags config_flags = { 0 };
986 memcpy(&global_jbconf, &default_jbconf, sizeof(global_jbconf));
988 console_pvt_lock(pvt);
990 set_pvt_defaults(pvt, reload);
992 if (!(cfg = ast_config_load(config_file, config_flags))) {
993 ast_log(LOG_NOTICE, "Unable to open configuration file %s!\n", config_file);
997 for (v = ast_variable_browse(cfg, "general"); v; v = v->next)
998 store_config_core(pvt, v->name, v->value);
1000 ast_config_destroy(cfg);
1005 console_pvt_unlock(pvt);
1009 static int init_pvt(struct console_pvt *pvt)
1011 if (ast_string_field_init(pvt, 32))
1014 if (ast_mutex_init(&pvt->__lock)) {
1015 ast_log(LOG_ERROR, "Failed to initialize mutex\n");
1022 static void destroy_pvt(struct console_pvt *pvt)
1024 ast_string_field_free_memory(pvt);
1026 ast_mutex_destroy(&pvt->__lock);
1029 static int unload_module(void)
1031 struct console_pvt *pvt = &console_pvt;
1038 ast_channel_unregister(&console_tech);
1039 ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console));
1046 static int load_module(void)
1049 struct console_pvt *pvt = &console_pvt;
1057 res = Pa_Initialize();
1058 if (res != paNoError) {
1059 ast_log(LOG_WARNING, "Failed to initialize audio system - (%d) %s\n",
1060 res, Pa_GetErrorText(res));
1061 goto return_error_pa_init;
1064 if (ast_channel_register(&console_tech)) {
1065 ast_log(LOG_ERROR, "Unable to register channel type 'Console'\n");
1066 goto return_error_chan_reg;
1069 if (ast_cli_register_multiple(cli_console, ARRAY_LEN(cli_console)))
1070 goto return_error_cli_reg;
1072 return AST_MODULE_LOAD_SUCCESS;
1074 return_error_cli_reg:
1075 ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console));
1076 return_error_chan_reg:
1077 ast_channel_unregister(&console_tech);
1078 return_error_pa_init:
1083 return AST_MODULE_LOAD_DECLINE;
1086 static int reload(void)
1088 return load_config(1);
1091 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Console Channel Driver",
1092 .load = load_module,
1093 .unload = unload_module,