2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2006 - 2008, 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 * - Set Auto-answer from the dialplan
45 * - transfer CLI command
46 * - boost CLI command and .conf option
47 * - console_video support
51 <depend>portaudio</depend>
56 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
58 #include <sys/signal.h> /* SIGURG */
60 #include <portaudio.h>
62 #include "asterisk/module.h"
63 #include "asterisk/channel.h"
64 #include "asterisk/pbx.h"
65 #include "asterisk/causes.h"
66 #include "asterisk/cli.h"
67 #include "asterisk/musiconhold.h"
68 #include "asterisk/callerid.h"
69 #include "asterisk/astobj2.h"
72 * \brief The sample rate to request from PortAudio
74 * \todo Make this optional. If this is only going to talk to 8 kHz endpoints,
75 * then it makes sense to use 8 kHz natively.
77 #define SAMPLE_RATE 16000
80 * \brief The number of samples to configure the portaudio stream for
82 * 320 samples (20 ms) is the most common frame size in Asterisk. So, the code
83 * in this module reads 320 sample frames from the portaudio stream and queues
84 * them up on the Asterisk channel. Frames of any size can be written to a
85 * portaudio stream, but the portaudio documentation does say that for high
86 * performance applications, the data should be written to Pa_WriteStream in
87 * the same size as what is used to initialize the stream.
89 #define NUM_SAMPLES 320
91 /*! \brief Mono Input */
92 #define INPUT_CHANNELS 1
94 /*! \brief Mono Output */
95 #define OUTPUT_CHANNELS 1
98 * \brief Maximum text message length
99 * \note This should be changed if there is a common definition somewhere
100 * that defines the maximum length of a text message.
102 #define TEXT_SIZE 256
105 #define MIN(a,b) ((a) < (b) ? (a) : (b))
108 #define MAX(a,b) ((a) > (b) ? (a) : (b))
111 /*! \brief Dance, Kirby, Dance! @{ */
112 #define V_BEGIN " --- <(\"<) --- "
113 #define V_END " --- (>\")> ---\n"
116 static const char config_file[] = "console.conf";
119 * \brief Console pvt structure
121 * Currently, this is a singleton object. However, multiple instances will be
122 * needed when this module is updated for multiple device support.
124 static struct console_pvt {
125 AST_DECLARE_STRING_FIELDS(
126 /*! Name of the device */
127 AST_STRING_FIELD(name);
128 AST_STRING_FIELD(input_device);
129 AST_STRING_FIELD(output_device);
130 /*! Default context for outgoing calls */
131 AST_STRING_FIELD(context);
132 /*! Default extension for outgoing calls */
133 AST_STRING_FIELD(exten);
134 /*! Default CallerID number */
135 AST_STRING_FIELD(cid_num);
136 /*! Default CallerID name */
137 AST_STRING_FIELD(cid_name);
138 /*! Default MOH class to listen to, if:
139 * - No MOH class set on the channel
140 * - Peer channel putting this device on hold did not suggest a class */
141 AST_STRING_FIELD(mohinterpret);
142 /*! Default language */
143 AST_STRING_FIELD(language);
145 /*! Current channel for this device */
146 struct ast_channel *owner;
147 /*! Current PortAudio stream for this device */
149 /*! A frame for preparing to queue on to the channel */
151 /*! Running = 1, Not running = 0 */
152 unsigned int streamstate:1;
153 /*! On-hook = 0, Off-hook = 1 */
154 unsigned int hookstate:1;
155 /*! Unmuted = 0, Muted = 1 */
156 unsigned int muted:1;
157 /*! Automatically answer incoming calls */
158 unsigned int autoanswer:1;
159 /*! Ignore context in the console dial CLI command */
160 unsigned int overridecontext:1;
161 /*! Set during a reload so that we know to destroy this if it is no longer
162 * in the configuration file. */
163 unsigned int destroy:1;
164 /*! ID for the stream monitor thread */
168 AST_MUTEX_DEFINE_STATIC(globals_lock);
170 static struct ao2_container *pvts;
171 #define NUM_PVT_BUCKETS 7
173 static struct console_pvt *active_pvt;
174 AST_RWLOCK_DEFINE_STATIC(active_lock);
177 * \brief Global jitterbuffer configuration
179 * \note Disabled by default.
181 static struct ast_jb_conf default_jbconf = {
184 .resync_threshold = -1,
187 static struct ast_jb_conf global_jbconf;
189 /*! Channel Technology Callbacks @{ */
190 static struct ast_channel *console_request(const char *type, int format,
191 void *data, int *cause);
192 static int console_digit_begin(struct ast_channel *c, char digit);
193 static int console_digit_end(struct ast_channel *c, char digit, unsigned int duration);
194 static int console_text(struct ast_channel *c, const char *text);
195 static int console_hangup(struct ast_channel *c);
196 static int console_answer(struct ast_channel *c);
197 static struct ast_frame *console_read(struct ast_channel *chan);
198 static int console_call(struct ast_channel *c, char *dest, int timeout);
199 static int console_write(struct ast_channel *chan, struct ast_frame *f);
200 static int console_indicate(struct ast_channel *chan, int cond,
201 const void *data, size_t datalen);
202 static int console_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
206 * \brief Formats natively supported by this module.
208 #define SUPPORTED_FORMATS ( AST_FORMAT_SLINEAR16 )
210 static const struct ast_channel_tech console_tech = {
212 .description = "Console Channel Driver",
213 .capabilities = SUPPORTED_FORMATS,
214 .requester = console_request,
215 .send_digit_begin = console_digit_begin,
216 .send_digit_end = console_digit_end,
217 .send_text = console_text,
218 .hangup = console_hangup,
219 .answer = console_answer,
220 .read = console_read,
221 .call = console_call,
222 .write = console_write,
223 .indicate = console_indicate,
224 .fixup = console_fixup,
227 /*! \brief lock a console_pvt struct */
228 #define console_pvt_lock(pvt) ao2_lock(pvt)
230 /*! \brief unlock a console_pvt struct */
231 #define console_pvt_unlock(pvt) ao2_unlock(pvt)
233 static inline struct console_pvt *ref_pvt(struct console_pvt *pvt)
240 static inline struct console_pvt *unref_pvt(struct console_pvt *pvt)
246 static struct console_pvt *find_pvt(const char *name)
248 struct console_pvt tmp_pvt = {
252 return ao2_find(pvts, &tmp_pvt, OBJ_POINTER);
256 * \brief Stream monitor thread
258 * \arg data A pointer to the console_pvt structure that contains the portaudio
259 * stream that needs to be monitored.
261 * This function runs in its own thread to monitor data coming in from a
262 * portaudio stream. When enough data is available, it is queued up to
263 * be read from the Asterisk channel.
265 static void *stream_monitor(void *data)
267 struct console_pvt *pvt = data;
268 char buf[NUM_SAMPLES * sizeof(int16_t)];
270 struct ast_frame f = {
271 .frametype = AST_FRAME_VOICE,
272 .subclass = AST_FORMAT_SLINEAR16,
273 .src = "console_stream_monitor",
275 .datalen = sizeof(buf),
276 .samples = sizeof(buf) / sizeof(int16_t),
280 pthread_testcancel();
281 res = Pa_ReadStream(pvt->stream, buf, sizeof(buf) / sizeof(int16_t));
282 pthread_testcancel();
284 if (res == paNoError)
285 ast_queue_frame(pvt->owner, &f);
291 static int open_stream(struct console_pvt *pvt)
293 int res = paInternalError;
295 if (!strcasecmp(pvt->input_device, "default") &&
296 !strcasecmp(pvt->output_device, "default")) {
297 res = Pa_OpenDefaultStream(&pvt->stream, INPUT_CHANNELS, OUTPUT_CHANNELS,
298 paInt16, SAMPLE_RATE, NUM_SAMPLES, NULL, NULL);
300 PaStreamParameters input_params = {
302 .sampleFormat = paInt16,
303 .suggestedLatency = (1.0 / 50.0), /* 20 ms */
304 .device = paNoDevice,
306 PaStreamParameters output_params = {
308 .sampleFormat = paInt16,
309 .suggestedLatency = (1.0 / 50.0), /* 20 ms */
310 .device = paNoDevice,
312 PaDeviceIndex index, num_devices, def_input, def_output;
314 if (!(num_devices = Pa_GetDeviceCount()))
317 def_input = Pa_GetDefaultInputDevice();
318 def_output = Pa_GetDefaultOutputDevice();
321 index < num_devices && (input_params.device == paNoDevice
322 || output_params.device == paNoDevice);
325 const PaDeviceInfo *dev = Pa_GetDeviceInfo(index);
327 if (dev->maxInputChannels) {
328 if ( (index == def_input && !strcasecmp(pvt->input_device, "default")) ||
329 !strcasecmp(pvt->input_device, dev->name) )
330 input_params.device = index;
333 if (dev->maxOutputChannels) {
334 if ( (index == def_output && !strcasecmp(pvt->output_device, "default")) ||
335 !strcasecmp(pvt->output_device, dev->name) )
336 output_params.device = index;
340 if (input_params.device == paNoDevice)
341 ast_log(LOG_ERROR, "No input device found for console device '%s'\n", pvt->name);
342 if (output_params.device == paNoDevice)
343 ast_log(LOG_ERROR, "No output device found for console device '%s'\n", pvt->name);
345 res = Pa_OpenStream(&pvt->stream, &input_params, &output_params,
346 SAMPLE_RATE, NUM_SAMPLES, paNoFlag, NULL, NULL);
352 static int start_stream(struct console_pvt *pvt)
357 console_pvt_lock(pvt);
359 if (pvt->streamstate)
362 pvt->streamstate = 1;
363 ast_debug(1, "Starting stream\n");
365 res = open_stream(pvt);
366 if (res != paNoError) {
367 ast_log(LOG_WARNING, "Failed to open stream - (%d) %s\n",
368 res, Pa_GetErrorText(res));
373 res = Pa_StartStream(pvt->stream);
374 if (res != paNoError) {
375 ast_log(LOG_WARNING, "Failed to start stream - (%d) %s\n",
376 res, Pa_GetErrorText(res));
381 if (ast_pthread_create_background(&pvt->thread, NULL, stream_monitor, pvt)) {
382 ast_log(LOG_ERROR, "Failed to start stream monitor thread\n");
387 console_pvt_unlock(pvt);
392 static int stop_stream(struct console_pvt *pvt)
394 if (!pvt->streamstate)
397 pthread_cancel(pvt->thread);
398 pthread_kill(pvt->thread, SIGURG);
399 pthread_join(pvt->thread, NULL);
401 console_pvt_lock(pvt);
402 Pa_AbortStream(pvt->stream);
403 Pa_CloseStream(pvt->stream);
405 pvt->streamstate = 0;
406 console_pvt_unlock(pvt);
412 * \note Called with the pvt struct locked
414 static struct ast_channel *console_new(struct console_pvt *pvt, const char *ext, const char *ctx, int state)
416 struct ast_channel *chan;
418 if (!(chan = ast_channel_alloc(1, state, pvt->cid_num, pvt->cid_name, NULL,
419 ext, ctx, 0, "Console/%s", pvt->name))) {
423 chan->tech = &console_tech;
424 chan->nativeformats = AST_FORMAT_SLINEAR16;
425 chan->readformat = AST_FORMAT_SLINEAR16;
426 chan->writeformat = AST_FORMAT_SLINEAR16;
427 chan->tech_pvt = ref_pvt(pvt);
431 if (!ast_strlen_zero(pvt->language))
432 ast_string_field_set(chan, language, pvt->language);
434 ast_jb_configure(chan, &global_jbconf);
436 if (state != AST_STATE_DOWN) {
437 if (ast_pbx_start(chan)) {
438 chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
448 static struct ast_channel *console_request(const char *type, int format, void *data, int *cause)
450 int oldformat = format;
451 struct ast_channel *chan = NULL;
452 struct console_pvt *pvt;
454 if (!(pvt = find_pvt(data))) {
455 ast_log(LOG_ERROR, "Console device '%s' not found\n", (char *) data);
459 format &= SUPPORTED_FORMATS;
461 ast_log(LOG_NOTICE, "Channel requested with unsupported format(s): '%d'\n", oldformat);
466 ast_log(LOG_NOTICE, "Console channel already active!\n");
467 *cause = AST_CAUSE_BUSY;
471 console_pvt_lock(pvt);
472 chan = console_new(pvt, NULL, NULL, AST_STATE_DOWN);
473 console_pvt_unlock(pvt);
476 ast_log(LOG_WARNING, "Unable to create new Console channel!\n");
484 static int console_digit_begin(struct ast_channel *c, char digit)
486 ast_verb(1, V_BEGIN "Console Received Beginning of Digit %c" V_END, digit);
488 return -1; /* non-zero to request inband audio */
491 static int console_digit_end(struct ast_channel *c, char digit, unsigned int duration)
493 ast_verb(1, V_BEGIN "Console Received End of Digit %c (duration %u)" V_END,
496 return -1; /* non-zero to request inband audio */
499 static int console_text(struct ast_channel *c, const char *text)
501 ast_verb(1, V_BEGIN "Console Received Text '%s'" V_END, text);
506 static int console_hangup(struct ast_channel *c)
508 struct console_pvt *pvt = c->tech_pvt;
510 ast_verb(1, V_BEGIN "Hangup on Console" V_END);
516 c->tech_pvt = unref_pvt(pvt);
521 static int console_answer(struct ast_channel *c)
523 struct console_pvt *pvt = c->tech_pvt;
525 ast_verb(1, V_BEGIN "Call from Console has been Answered" V_END);
527 ast_setstate(c, AST_STATE_UP);
529 return start_stream(pvt);
533 * \brief Implementation of the ast_channel_tech read() callback
535 * Calling this function is harmless. However, if it does get called, it
536 * is an indication that something weird happened that really shouldn't
537 * have and is worth looking into.
539 * Why should this function not get called? Well, let me explain. There are
540 * a couple of ways to pass on audio that has come from this channel. The way
541 * that this channel driver uses is that once the audio is available, it is
542 * wrapped in an ast_frame and queued onto the channel using ast_queue_frame().
544 * The other method would be signalling to the core that there is audio waiting,
545 * and that it needs to call the channel's read() callback to get it. The way
546 * the channel gets signalled is that one or more file descriptors are placed
547 * in the fds array on the ast_channel which the core will poll() on. When the
548 * fd indicates that input is available, the read() callback is called. This
549 * is especially useful when there is a dedicated file descriptor where the
550 * audio is read from. An example would be the socket for an RTP stream.
552 static struct ast_frame *console_read(struct ast_channel *chan)
554 ast_debug(1, "I should not be called ...\n");
556 return &ast_null_frame;
559 static int console_call(struct ast_channel *c, char *dest, int timeout)
561 struct ast_frame f = { 0, };
562 struct console_pvt *pvt = c->tech_pvt;
564 ast_verb(1, V_BEGIN "Call to device '%s' on console from '%s' <%s>" V_END,
565 dest, c->cid.cid_name, c->cid.cid_num);
567 console_pvt_lock(pvt);
569 if (pvt->autoanswer) {
571 console_pvt_unlock(pvt);
572 ast_verb(1, V_BEGIN "Auto-answered" V_END);
573 f.frametype = AST_FRAME_CONTROL;
574 f.subclass = AST_CONTROL_ANSWER;
576 console_pvt_unlock(pvt);
577 ast_verb(1, V_BEGIN "Type 'console answer' to answer, or use the 'autoanswer' option "
578 "for future calls" V_END);
579 f.frametype = AST_FRAME_CONTROL;
580 f.subclass = AST_CONTROL_RINGING;
581 ast_indicate(c, AST_CONTROL_RINGING);
584 ast_queue_frame(c, &f);
586 return start_stream(pvt);
589 static int console_write(struct ast_channel *chan, struct ast_frame *f)
591 struct console_pvt *pvt = chan->tech_pvt;
593 Pa_WriteStream(pvt->stream, f->data, f->samples);
598 static int console_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
600 struct console_pvt *pvt = chan->tech_pvt;
604 case AST_CONTROL_BUSY:
605 case AST_CONTROL_CONGESTION:
606 case AST_CONTROL_RINGING:
608 res = -1; /* Ask for inband indications */
610 case AST_CONTROL_PROGRESS:
611 case AST_CONTROL_PROCEEDING:
612 case AST_CONTROL_VIDUPDATE:
614 case AST_CONTROL_HOLD:
615 ast_verb(1, V_BEGIN "Console Has Been Placed on Hold" V_END);
616 ast_moh_start(chan, data, pvt->mohinterpret);
618 case AST_CONTROL_UNHOLD:
619 ast_verb(1, V_BEGIN "Console Has Been Retrieved from Hold" V_END);
623 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n",
625 /* The core will play inband indications for us if appropriate */
632 static int console_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
634 struct console_pvt *pvt = newchan->tech_pvt;
636 pvt->owner = newchan;
642 * split a string in extension-context, returns pointers to malloc'ed
644 * If we do not have 'overridecontext' then the last @ is considered as
645 * a context separator, and the context is overridden.
646 * This is usually not very necessary as you can play with the dialplan,
647 * and it is nice not to need it because you have '@' in SIP addresses.
648 * Return value is the buffer address.
650 * \note came from chan_oss
652 static char *ast_ext_ctx(struct console_pvt *pvt, const char *src, char **ext, char **ctx)
654 if (ext == NULL || ctx == NULL)
655 return NULL; /* error */
659 if (src && *src != '\0')
660 *ext = ast_strdup(src);
665 if (!pvt->overridecontext) {
666 /* parse from the right */
667 *ctx = strrchr(*ext, '@');
675 static struct console_pvt *get_active_pvt(void)
677 struct console_pvt *pvt;
679 ast_rwlock_rdlock(&active_lock);
680 pvt = ref_pvt(active_pvt);
681 ast_rwlock_unlock(&active_lock);
686 static char *cli_console_autoanswer(struct ast_cli_entry *e, int cmd,
687 struct ast_cli_args *a)
689 struct console_pvt *pvt = get_active_pvt();
690 char *res = CLI_SUCCESS;
694 e->command = "console set autoanswer [on|off]";
696 "Usage: console set autoanswer [on|off]\n"
697 " Enables or disables autoanswer feature. If used without\n"
698 " argument, displays the current on/off status of autoanswer.\n"
699 " The default value of autoanswer is in 'oss.conf'.\n";
707 ast_cli(a->fd, "No console device is set as active.\n");
711 if (a->argc == e->args - 1) {
712 ast_cli(a->fd, "Auto answer is %s.\n", pvt->autoanswer ? "on" : "off");
717 if (a->argc != e->args) {
719 return CLI_SHOWUSAGE;
722 if (!strcasecmp(a->argv[e->args-1], "on"))
724 else if (!strcasecmp(a->argv[e->args - 1], "off"))
734 static char *cli_console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
736 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH };
737 struct console_pvt *pvt = get_active_pvt();
739 if (cmd == CLI_INIT) {
740 e->command = "console flash";
742 "Usage: console flash\n"
743 " Flashes the call currently placed on the console.\n";
745 } else if (cmd == CLI_GENERATE)
749 ast_cli(a->fd, "No console device is set as active\n");
753 if (a->argc != e->args)
754 return CLI_SHOWUSAGE;
757 ast_cli(a->fd, "No call to flash\n");
764 ast_queue_frame(pvt->owner, &f);
771 static char *cli_console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
774 const char *mye = NULL, *myc = NULL;
775 struct console_pvt *pvt = get_active_pvt();
777 if (cmd == CLI_INIT) {
778 e->command = "console dial";
780 "Usage: console dial [extension[@context]]\n"
781 " Dials a given extension (and context if specified)\n";
783 } else if (cmd == CLI_GENERATE)
787 ast_cli(a->fd, "No console device is currently set as active\n");
791 if (a->argc > e->args + 1)
792 return CLI_SHOWUSAGE;
794 if (pvt->owner) { /* already in a call */
796 struct ast_frame f = { AST_FRAME_DTMF, 0 };
798 if (a->argc == e->args) { /* argument is mandatory here */
799 ast_cli(a->fd, "Already in a call. You can only dial digits until you hangup.\n");
803 s = a->argv[e->args];
804 /* send the string one char at a time */
805 for (i = 0; i < strlen(s); i++) {
807 ast_queue_frame(pvt->owner, &f);
813 /* if we have an argument split it into extension and context */
814 if (a->argc == e->args + 1) {
815 char *ext = NULL, *con = NULL;
816 s = ast_ext_ctx(pvt, a->argv[e->args], &ext, &con);
817 ast_debug(1, "provided '%s', exten '%s' context '%s'\n",
818 a->argv[e->args], mye, myc);
823 /* supply default values if needed */
824 if (ast_strlen_zero(mye))
826 if (ast_strlen_zero(myc))
829 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
830 console_pvt_lock(pvt);
832 console_new(pvt, mye, myc, AST_STATE_RINGING);
833 console_pvt_unlock(pvt);
835 ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
845 static char *cli_console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
847 struct console_pvt *pvt = get_active_pvt();
849 if (cmd == CLI_INIT) {
850 e->command = "console hangup";
852 "Usage: console hangup\n"
853 " Hangs up any call currently placed on the console.\n";
855 } else if (cmd == CLI_GENERATE)
859 ast_cli(a->fd, "No console device is set as active\n");
863 if (a->argc != e->args)
864 return CLI_SHOWUSAGE;
866 if (!pvt->owner && !pvt->hookstate) {
867 ast_cli(a->fd, "No call to hang up\n");
874 ast_queue_hangup(pvt->owner);
881 static char *cli_console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
884 struct console_pvt *pvt = get_active_pvt();
885 char *res = CLI_SUCCESS;
887 if (cmd == CLI_INIT) {
888 e->command = "console {mute|unmute}";
890 "Usage: console {mute|unmute}\n"
891 " Mute/unmute the microphone.\n";
893 } else if (cmd == CLI_GENERATE)
897 ast_cli(a->fd, "No console device is set as active\n");
901 if (a->argc != e->args)
902 return CLI_SHOWUSAGE;
904 s = a->argv[e->args-1];
905 if (!strcasecmp(s, "mute"))
907 else if (!strcasecmp(s, "unmute"))
912 ast_verb(1, V_BEGIN "The Console is now %s" V_END,
913 pvt->muted ? "Muted" : "Unmuted");
920 static char *cli_list_available(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
922 PaDeviceIndex index, num, def_input, def_output;
924 if (cmd == CLI_INIT) {
925 e->command = "console list available";
927 "Usage: console list available\n"
928 " List all available devices.\n";
930 } else if (cmd == CLI_GENERATE)
933 if (a->argc != e->args)
934 return CLI_SHOWUSAGE;
937 "=============================================================\n"
938 "=== Available Devices =======================================\n"
939 "=============================================================\n"
942 num = Pa_GetDeviceCount();
944 ast_cli(a->fd, "(None)\n");
948 def_input = Pa_GetDefaultInputDevice();
949 def_output = Pa_GetDefaultOutputDevice();
950 for (index = 0; index < num; index++) {
951 const PaDeviceInfo *dev = Pa_GetDeviceInfo(index);
954 ast_cli(a->fd, "=== ---------------------------------------------------------\n"
955 "=== Device Name: %s\n", dev->name);
956 if (dev->maxInputChannels)
957 ast_cli(a->fd, "=== ---> %sInput Device\n", (index == def_input) ? "Default " : "");
958 if (dev->maxOutputChannels)
959 ast_cli(a->fd, "=== ---> %sOutput Device\n", (index == def_output) ? "Default " : "");
960 ast_cli(a->fd, "=== ---------------------------------------------------------\n===\n");
963 ast_cli(a->fd, "=============================================================\n\n");
968 static char *cli_list_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
970 struct ao2_iterator i;
971 struct console_pvt *pvt;
973 if (cmd == CLI_INIT) {
974 e->command = "console list devices";
976 "Usage: console list devices\n"
977 " List all configured devices.\n";
979 } else if (cmd == CLI_GENERATE)
982 if (a->argc != e->args)
983 return CLI_SHOWUSAGE;
986 "=============================================================\n"
987 "=== Configured Devices ======================================\n"
988 "=============================================================\n"
991 i = ao2_iterator_init(pvts, 0);
992 while ((pvt = ao2_iterator_next(&i))) {
993 console_pvt_lock(pvt);
995 ast_cli(a->fd, "=== ---------------------------------------------------------\n"
996 "=== Device Name: %s\n"
997 "=== ---> Active: %s\n"
998 "=== ---> Input Device: %s\n"
999 "=== ---> Output Device: %s\n"
1000 "=== ---> Context: %s\n"
1001 "=== ---> Extension: %s\n"
1002 "=== ---> CallerID Num: %s\n"
1003 "=== ---> CallerID Name: %s\n"
1004 "=== ---> MOH Interpret: %s\n"
1005 "=== ---> Language: %s\n"
1006 "=== ---> Muted: %s\n"
1007 "=== ---> Auto-Answer: %s\n"
1008 "=== ---> Override Context: %s\n"
1009 "=== ---------------------------------------------------------\n===\n",
1010 pvt->name, (pvt == active_pvt) ? "Yes" : "No",
1011 pvt->input_device, pvt->output_device, pvt->context,
1012 pvt->exten, pvt->cid_num, pvt->cid_name, pvt->mohinterpret,
1013 pvt->language, pvt->muted ? "Yes" : "No", pvt->autoanswer ? "Yes" : "No",
1014 pvt->overridecontext ? "Yes" : "No");
1016 console_pvt_unlock(pvt);
1020 ast_cli(a->fd, "=============================================================\n\n");
1025 * \brief answer command from the console
1027 static char *cli_console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1029 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
1030 struct console_pvt *pvt = get_active_pvt();
1034 e->command = "console answer";
1036 "Usage: console answer\n"
1037 " Answers an incoming call on the console channel.\n";
1041 return NULL; /* no completion */
1045 ast_cli(a->fd, "No console device is set as active\n");
1049 if (a->argc != e->args) {
1051 return CLI_SHOWUSAGE;
1055 ast_cli(a->fd, "No one is calling us\n");
1062 ast_indicate(pvt->owner, -1);
1064 ast_queue_frame(pvt->owner, &f);
1072 * \brief Console send text CLI command
1074 * \note concatenate all arguments into a single string. argv is NULL-terminated
1075 * so we can use it right away
1077 static char *cli_console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1079 char buf[TEXT_SIZE];
1080 struct console_pvt *pvt = get_active_pvt();
1081 struct ast_frame f = {
1082 .frametype = AST_FRAME_TEXT,
1084 .src = "console_send_text",
1088 if (cmd == CLI_INIT) {
1089 e->command = "console send text";
1091 "Usage: console send text <message>\n"
1092 " Sends a text message for display on the remote terminal.\n";
1094 } else if (cmd == CLI_GENERATE)
1098 ast_cli(a->fd, "No console device is set as active\n");
1102 if (a->argc < e->args + 1) {
1104 return CLI_SHOWUSAGE;
1108 ast_cli(a->fd, "Not in a call\n");
1113 ast_join(buf, sizeof(buf) - 1, a->argv + e->args);
1114 if (ast_strlen_zero(buf)) {
1116 return CLI_SHOWUSAGE;
1121 f.datalen = len + 1;
1123 ast_queue_frame(pvt->owner, &f);
1130 static void set_active(struct console_pvt *pvt, const char *value)
1132 if (pvt == &globals) {
1133 ast_log(LOG_ERROR, "active is only valid as a per-device setting\n");
1137 if (!ast_true(value))
1140 ast_rwlock_wrlock(&active_lock);
1142 unref_pvt(active_pvt);
1143 active_pvt = ref_pvt(pvt);
1144 ast_rwlock_unlock(&active_lock);
1147 static char *cli_console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1149 struct console_pvt *pvt;
1153 e->command = "console active";
1155 "Usage: console active [device]\n"
1156 " If no device is specified. The active console device will be shown.\n"
1157 "Otherwise, the specified device will become the console device active for\n"
1158 "the Asterisk CLI.\n";
1161 if (a->pos == e->args) {
1162 struct ao2_iterator i;
1165 i = ao2_iterator_init(pvts, 0);
1166 while ((pvt = ao2_iterator_next(&i))) {
1167 if (++x > a->n && !strncasecmp(pvt->name, a->word, strlen(a->word)))
1168 res = ast_strdup(pvt->name);
1177 if (a->argc < e->args)
1178 return CLI_SHOWUSAGE;
1180 if (a->argc == e->args) {
1181 pvt = get_active_pvt();
1184 ast_cli(a->fd, "No device is currently set as the active console device.\n");
1186 console_pvt_lock(pvt);
1187 ast_cli(a->fd, "The active console device is '%s'.\n", pvt->name);
1188 console_pvt_unlock(pvt);
1189 pvt = unref_pvt(pvt);
1195 if (!(pvt = find_pvt(a->argv[e->args]))) {
1196 ast_cli(a->fd, "Could not find a device called '%s'.\n", a->argv[e->args]);
1200 set_active(pvt, "yes");
1202 console_pvt_lock(pvt);
1203 ast_cli(a->fd, "The active console device has been set to '%s'\n", pvt->name);
1204 console_pvt_unlock(pvt);
1211 static struct ast_cli_entry cli_console[] = {
1212 AST_CLI_DEFINE(cli_console_dial, "Dial an extension from the console"),
1213 AST_CLI_DEFINE(cli_console_hangup, "Hangup a call on the console"),
1214 AST_CLI_DEFINE(cli_console_mute, "Disable/Enable mic input"),
1215 AST_CLI_DEFINE(cli_console_answer, "Answer an incoming console call"),
1216 AST_CLI_DEFINE(cli_console_sendtext, "Send text to a connected party"),
1217 AST_CLI_DEFINE(cli_console_flash, "Send a flash to the connected party"),
1218 AST_CLI_DEFINE(cli_console_autoanswer, "Turn autoanswer on or off"),
1219 AST_CLI_DEFINE(cli_list_available, "List available devices"),
1220 AST_CLI_DEFINE(cli_list_devices, "List configured devices"),
1221 AST_CLI_DEFINE(cli_console_active, "View or Set the active console device"),
1225 * \brief Set default values for a pvt struct
1227 * \note This function expects the pvt lock to be held.
1229 static void set_pvt_defaults(struct console_pvt *pvt)
1231 if (pvt == &globals) {
1232 ast_string_field_set(pvt, mohinterpret, "default");
1233 ast_string_field_set(pvt, context, "default");
1234 ast_string_field_set(pvt, exten, "s");
1235 ast_string_field_set(pvt, language, "");
1236 ast_string_field_set(pvt, cid_num, "");
1237 ast_string_field_set(pvt, cid_name, "");
1239 pvt->overridecontext = 0;
1240 pvt->autoanswer = 0;
1242 ast_mutex_lock(&globals_lock);
1244 ast_string_field_set(pvt, mohinterpret, globals.mohinterpret);
1245 ast_string_field_set(pvt, context, globals.context);
1246 ast_string_field_set(pvt, exten, globals.exten);
1247 ast_string_field_set(pvt, language, globals.language);
1248 ast_string_field_set(pvt, cid_num, globals.cid_num);
1249 ast_string_field_set(pvt, cid_name, globals.cid_name);
1251 pvt->overridecontext = globals.overridecontext;
1252 pvt->autoanswer = globals.autoanswer;
1254 ast_mutex_unlock(&globals_lock);
1258 static void store_callerid(struct console_pvt *pvt, const char *value)
1263 ast_callerid_split(value, cid_name, sizeof(cid_name),
1264 cid_num, sizeof(cid_num));
1266 ast_string_field_set(pvt, cid_name, cid_name);
1267 ast_string_field_set(pvt, cid_num, cid_num);
1271 * \brief Store a configuration parameter in a pvt struct
1273 * \note This function expects the pvt lock to be held.
1275 static void store_config_core(struct console_pvt *pvt, const char *var, const char *value)
1277 if (pvt == &globals && !ast_jb_read_conf(&global_jbconf, var, value))
1280 CV_START(var, value);
1282 CV_STRFIELD("context", pvt, context);
1283 CV_STRFIELD("extension", pvt, exten);
1284 CV_STRFIELD("mohinterpret", pvt, mohinterpret);
1285 CV_STRFIELD("language", pvt, language);
1286 CV_F("callerid", store_callerid(pvt, value));
1287 CV_BOOL("overridecontext", pvt->overridecontext);
1288 CV_BOOL("autoanswer", pvt->autoanswer);
1290 if (pvt != &globals) {
1291 CV_F("active", set_active(pvt, value))
1292 CV_STRFIELD("input_device", pvt, input_device);
1293 CV_STRFIELD("output_device", pvt, output_device);
1296 ast_log(LOG_WARNING, "Unknown option '%s'\n", var);
1301 static void pvt_destructor(void *obj)
1303 struct console_pvt *pvt = obj;
1305 ast_string_field_free_memory(pvt);
1308 static int init_pvt(struct console_pvt *pvt, const char *name)
1310 pvt->thread = AST_PTHREADT_NULL;
1312 if (ast_string_field_init(pvt, 32))
1315 ast_string_field_set(pvt, name, S_OR(name, ""));
1320 static void build_device(struct ast_config *cfg, const char *name)
1322 struct ast_variable *v;
1323 struct console_pvt *pvt;
1326 if ((pvt = find_pvt(name))) {
1327 console_pvt_lock(pvt);
1328 set_pvt_defaults(pvt);
1331 if (!(pvt = ao2_alloc(sizeof(*pvt), pvt_destructor)))
1333 init_pvt(pvt, name);
1334 set_pvt_defaults(pvt);
1338 for (v = ast_variable_browse(cfg, name); v; v = v->next)
1339 store_config_core(pvt, v->name, v->value);
1342 ao2_link(pvts, pvt);
1344 console_pvt_unlock(pvt);
1349 static int pvt_mark_destroy_cb(void *obj, void *arg, int flags)
1351 struct console_pvt *pvt = obj;
1356 static void destroy_pvts(void)
1358 struct ao2_iterator i;
1359 struct console_pvt *pvt;
1361 i = ao2_iterator_init(pvts, 0);
1362 while ((pvt = ao2_iterator_next(&i))) {
1364 ao2_unlink(pvts, pvt);
1365 ast_rwlock_wrlock(&active_lock);
1366 if (active_pvt == pvt)
1367 active_pvt = unref_pvt(pvt);
1368 ast_rwlock_unlock(&active_lock);
1375 * \brief Load the configuration
1376 * \param reload if this was called due to a reload
1377 * \retval 0 succcess
1378 * \retval -1 failure
1380 static int load_config(int reload)
1382 struct ast_config *cfg;
1383 struct ast_variable *v;
1384 struct ast_flags config_flags = { 0 };
1385 char *context = NULL;
1387 /* default values */
1388 memcpy(&global_jbconf, &default_jbconf, sizeof(global_jbconf));
1389 ast_mutex_lock(&globals_lock);
1390 set_pvt_defaults(&globals);
1391 ast_mutex_unlock(&globals_lock);
1393 if (!(cfg = ast_config_load(config_file, config_flags))) {
1394 ast_log(LOG_NOTICE, "Unable to open configuration file %s!\n", config_file);
1398 ao2_callback(pvts, 0, pvt_mark_destroy_cb, NULL);
1400 ast_mutex_lock(&globals_lock);
1401 for (v = ast_variable_browse(cfg, "general"); v; v = v->next)
1402 store_config_core(&globals, v->name, v->value);
1403 ast_mutex_unlock(&globals_lock);
1405 while ((context = ast_category_browse(cfg, context))) {
1406 if (strcasecmp(context, "general"))
1407 build_device(cfg, context);
1410 ast_config_destroy(cfg);
1417 static int pvt_hash_cb(const void *obj, const int flags)
1419 const struct console_pvt *pvt = obj;
1421 return ast_str_hash(pvt->name);
1424 static int pvt_cmp_cb(void *obj, void *arg, int flags)
1426 struct console_pvt *pvt = obj, *pvt2 = arg;
1428 return !strcasecmp(pvt->name, pvt2->name) ? CMP_MATCH : 0;
1431 static void stop_streams(void)
1433 struct console_pvt *pvt;
1434 struct ao2_iterator i;
1436 i = ao2_iterator_init(pvts, 0);
1437 while ((pvt = ao2_iterator_next(&i))) {
1444 static int unload_module(void)
1446 ast_channel_unregister(&console_tech);
1447 ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console));
1453 /* Will unref all the pvts so they will get destroyed, too */
1456 pvt_destructor(&globals);
1461 static int load_module(void)
1465 init_pvt(&globals, NULL);
1467 if (!(pvts = ao2_container_alloc(NUM_PVT_BUCKETS, pvt_hash_cb, pvt_cmp_cb)))
1473 res = Pa_Initialize();
1474 if (res != paNoError) {
1475 ast_log(LOG_WARNING, "Failed to initialize audio system - (%d) %s\n",
1476 res, Pa_GetErrorText(res));
1477 goto return_error_pa_init;
1480 if (ast_channel_register(&console_tech)) {
1481 ast_log(LOG_ERROR, "Unable to register channel type 'Console'\n");
1482 goto return_error_chan_reg;
1485 if (ast_cli_register_multiple(cli_console, ARRAY_LEN(cli_console)))
1486 goto return_error_cli_reg;
1488 return AST_MODULE_LOAD_SUCCESS;
1490 return_error_cli_reg:
1491 ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console));
1492 return_error_chan_reg:
1493 ast_channel_unregister(&console_tech);
1494 return_error_pa_init:
1499 pvt_destructor(&globals);
1501 return AST_MODULE_LOAD_DECLINE;
1504 static int reload(void)
1506 return load_config(1);
1509 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Console Channel Driver",
1510 .load = load_module,
1511 .unload = unload_module,