Wed Jan 8 2020 09:49:43

Asterisk developer's documentation


chan_console.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2006 - 2008, Digium, Inc.
5  *
6  * Russell Bryant <russell@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 /*!
20  * \file
21  * \brief Cross-platform console channel driver
22  *
23  * \author Russell Bryant <russell@digium.com>
24  *
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>
29  *
30  * \ingroup channel_drivers
31  *
32  * \extref Portaudio http://www.portaudio.com/
33  *
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
36  *
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:
43  *
44  * - Set Auto-answer from the dialplan
45  * - transfer CLI command
46  * - boost CLI command and .conf option
47  * - console_video support
48  */
49 
50 /*** MODULEINFO
51  <depend>portaudio</depend>
52  <support_level>extended</support_level>
53  ***/
54 
55 #include "asterisk.h"
56 
57 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 335064 $")
58 
59 #include <sys/signal.h> /* SIGURG */
60 
61 #include <portaudio.h>
62 
63 #include "asterisk/module.h"
64 #include "asterisk/channel.h"
65 #include "asterisk/pbx.h"
66 #include "asterisk/causes.h"
67 #include "asterisk/cli.h"
68 #include "asterisk/musiconhold.h"
69 #include "asterisk/callerid.h"
70 #include "asterisk/astobj2.h"
71 
72 /*!
73  * \brief The sample rate to request from PortAudio
74  *
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.
77  */
78 #define SAMPLE_RATE 16000
79 
80 /*!
81  * \brief The number of samples to configure the portaudio stream for
82  *
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.
89  */
90 #define NUM_SAMPLES 320
91 
92 /*! \brief Mono Input */
93 #define INPUT_CHANNELS 1
94 
95 /*! \brief Mono Output */
96 #define OUTPUT_CHANNELS 1
97 
98 /*!
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.
102  */
103 #define TEXT_SIZE 256
104 
105 /*! \brief Dance, Kirby, Dance! @{ */
106 #define V_BEGIN " --- <(\"<) --- "
107 #define V_END " --- (>\")> ---\n"
108 /*! @} */
109 
110 static const char config_file[] = "console.conf";
111 
112 /*!
113  * \brief Console pvt structure
114  *
115  * Currently, this is a singleton object. However, multiple instances will be
116  * needed when this module is updated for multiple device support.
117  */
118 static struct console_pvt {
120  /*! Name of the device */
124  /*! Default context for outgoing calls */
126  /*! Default extension for outgoing calls */
128  /*! Default CallerID number */
130  /*! Default CallerID name */
132  /*! Default MOH class to listen to, if:
133  * - No MOH class set on the channel
134  * - Peer channel putting this device on hold did not suggest a class */
136  /*! Default language */
138  /*! Default parkinglot */
140  );
141  /*! Current channel for this device */
143  /*! Current PortAudio stream for this device */
144  PaStream *stream;
145  /*! A frame for preparing to queue on to the channel */
146  struct ast_frame fr;
147  /*! Running = 1, Not running = 0 */
148  unsigned int streamstate:1;
149  /*! On-hook = 0, Off-hook = 1 */
150  unsigned int hookstate:1;
151  /*! Unmuted = 0, Muted = 1 */
152  unsigned int muted:1;
153  /*! Automatically answer incoming calls */
154  unsigned int autoanswer:1;
155  /*! Ignore context in the console dial CLI command */
156  unsigned int overridecontext:1;
157  /*! Set during a reload so that we know to destroy this if it is no longer
158  * in the configuration file. */
159  unsigned int destroy:1;
160  /*! ID for the stream monitor thread */
161  pthread_t thread;
162 } globals;
163 
165 
166 static struct ao2_container *pvts;
167 #define NUM_PVT_BUCKETS 7
168 
169 static struct console_pvt *active_pvt;
171 
172 /*!
173  * \brief Global jitterbuffer configuration
174  *
175  * \note Disabled by default.
176  * \note Values shown here match the defaults shown in console.conf.sample
177  */
178 static struct ast_jb_conf default_jbconf = {
179  .flags = 0,
180  .max_size = 200,
181  .resync_threshold = 1000,
182  .impl = "fixed",
183  .target_extra = 40,
184 };
186 
187 /*! Channel Technology Callbacks @{ */
188 static struct ast_channel *console_request(const char *type, format_t format,
189  const struct ast_channel *requestor, void *data, int *cause);
190 static int console_digit_begin(struct ast_channel *c, char digit);
191 static int console_digit_end(struct ast_channel *c, char digit, unsigned int duration);
192 static int console_text(struct ast_channel *c, const char *text);
193 static int console_hangup(struct ast_channel *c);
194 static int console_answer(struct ast_channel *c);
195 static struct ast_frame *console_read(struct ast_channel *chan);
196 static int console_call(struct ast_channel *c, char *dest, int timeout);
197 static int console_write(struct ast_channel *chan, struct ast_frame *f);
198 static int console_indicate(struct ast_channel *chan, int cond,
199  const void *data, size_t datalen);
200 static int console_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
201 /*! @} */
202 
203 /*!
204  * \brief Formats natively supported by this module.
205  */
206 #define SUPPORTED_FORMATS ( AST_FORMAT_SLINEAR16 )
207 
208 static const struct ast_channel_tech console_tech = {
209  .type = "Console",
210  .description = "Console Channel Driver",
211  .capabilities = SUPPORTED_FORMATS,
212  .requester = console_request,
213  .send_digit_begin = console_digit_begin,
214  .send_digit_end = console_digit_end,
215  .send_text = console_text,
216  .hangup = console_hangup,
217  .answer = console_answer,
218  .read = console_read,
219  .call = console_call,
220  .write = console_write,
221  .indicate = console_indicate,
222  .fixup = console_fixup,
223 };
224 
225 /*! \brief lock a console_pvt struct */
226 #define console_pvt_lock(pvt) ao2_lock(pvt)
227 
228 /*! \brief unlock a console_pvt struct */
229 #define console_pvt_unlock(pvt) ao2_unlock(pvt)
230 
231 static inline struct console_pvt *ref_pvt(struct console_pvt *pvt)
232 {
233  if (pvt)
234  ao2_ref(pvt, +1);
235  return pvt;
236 }
237 
238 static inline struct console_pvt *unref_pvt(struct console_pvt *pvt)
239 {
240  ao2_ref(pvt, -1);
241  return NULL;
242 }
243 
244 static struct console_pvt *find_pvt(const char *name)
245 {
246  struct console_pvt tmp_pvt = {
247  .name = name,
248  };
249 
250  return ao2_find(pvts, &tmp_pvt, OBJ_POINTER);
251 }
252 
253 /*!
254  * \brief Stream monitor thread
255  *
256  * \arg data A pointer to the console_pvt structure that contains the portaudio
257  * stream that needs to be monitored.
258  *
259  * This function runs in its own thread to monitor data coming in from a
260  * portaudio stream. When enough data is available, it is queued up to
261  * be read from the Asterisk channel.
262  */
263 static void *stream_monitor(void *data)
264 {
265  struct console_pvt *pvt = data;
266  char buf[NUM_SAMPLES * sizeof(int16_t)];
267  PaError res;
268  struct ast_frame f = {
270  .subclass.codec = AST_FORMAT_SLINEAR16,
271  .src = "console_stream_monitor",
272  .data.ptr = buf,
273  .datalen = sizeof(buf),
274  .samples = sizeof(buf) / sizeof(int16_t),
275  };
276 
277  for (;;) {
278  pthread_testcancel();
279  res = Pa_ReadStream(pvt->stream, buf, sizeof(buf) / sizeof(int16_t));
280  pthread_testcancel();
281 
282  if (!pvt->owner) {
283  return NULL;
284  }
285 
286  if (res == paNoError)
287  ast_queue_frame(pvt->owner, &f);
288  }
289 
290  return NULL;
291 }
292 
293 static int open_stream(struct console_pvt *pvt)
294 {
295  int res = paInternalError;
296 
297  if (!strcasecmp(pvt->input_device, "default") &&
298  !strcasecmp(pvt->output_device, "default")) {
299  res = Pa_OpenDefaultStream(&pvt->stream, INPUT_CHANNELS, OUTPUT_CHANNELS,
300  paInt16, SAMPLE_RATE, NUM_SAMPLES, NULL, NULL);
301  } else {
302  PaStreamParameters input_params = {
303  .channelCount = 1,
304  .sampleFormat = paInt16,
305  .suggestedLatency = (1.0 / 50.0), /* 20 ms */
306  .device = paNoDevice,
307  };
308  PaStreamParameters output_params = {
309  .channelCount = 1,
310  .sampleFormat = paInt16,
311  .suggestedLatency = (1.0 / 50.0), /* 20 ms */
312  .device = paNoDevice,
313  };
314  PaDeviceIndex idx, num_devices, def_input, def_output;
315 
316  if (!(num_devices = Pa_GetDeviceCount()))
317  return res;
318 
319  def_input = Pa_GetDefaultInputDevice();
320  def_output = Pa_GetDefaultOutputDevice();
321 
322  for (idx = 0;
323  idx < num_devices && (input_params.device == paNoDevice
324  || output_params.device == paNoDevice);
325  idx++)
326  {
327  const PaDeviceInfo *dev = Pa_GetDeviceInfo(idx);
328 
329  if (dev->maxInputChannels) {
330  if ( (idx == def_input && !strcasecmp(pvt->input_device, "default")) ||
331  !strcasecmp(pvt->input_device, dev->name) )
332  input_params.device = idx;
333  }
334 
335  if (dev->maxOutputChannels) {
336  if ( (idx == def_output && !strcasecmp(pvt->output_device, "default")) ||
337  !strcasecmp(pvt->output_device, dev->name) )
338  output_params.device = idx;
339  }
340  }
341 
342  if (input_params.device == paNoDevice)
343  ast_log(LOG_ERROR, "No input device found for console device '%s'\n", pvt->name);
344  if (output_params.device == paNoDevice)
345  ast_log(LOG_ERROR, "No output device found for console device '%s'\n", pvt->name);
346 
347  res = Pa_OpenStream(&pvt->stream, &input_params, &output_params,
348  SAMPLE_RATE, NUM_SAMPLES, paNoFlag, NULL, NULL);
349  }
350 
351  return res;
352 }
353 
354 static int start_stream(struct console_pvt *pvt)
355 {
356  PaError res;
357  int ret_val = 0;
358 
359  console_pvt_lock(pvt);
360 
361  /* It is possible for console_hangup to be called before the
362  * stream is started, if this is the case pvt->owner will be NULL
363  * and start_stream should be aborted. */
364  if (pvt->streamstate || !pvt->owner)
365  goto return_unlock;
366 
367  pvt->streamstate = 1;
368  ast_debug(1, "Starting stream\n");
369 
370  res = open_stream(pvt);
371  if (res != paNoError) {
372  ast_log(LOG_WARNING, "Failed to open stream - (%d) %s\n",
373  res, Pa_GetErrorText(res));
374  ret_val = -1;
375  goto return_unlock;
376  }
377 
378  res = Pa_StartStream(pvt->stream);
379  if (res != paNoError) {
380  ast_log(LOG_WARNING, "Failed to start stream - (%d) %s\n",
381  res, Pa_GetErrorText(res));
382  ret_val = -1;
383  goto return_unlock;
384  }
385 
386  if (ast_pthread_create_background(&pvt->thread, NULL, stream_monitor, pvt)) {
387  ast_log(LOG_ERROR, "Failed to start stream monitor thread\n");
388  ret_val = -1;
389  }
390 
391 return_unlock:
392  console_pvt_unlock(pvt);
393 
394  return ret_val;
395 }
396 
397 static int stop_stream(struct console_pvt *pvt)
398 {
399  if (!pvt->streamstate || pvt->thread == AST_PTHREADT_NULL)
400  return 0;
401 
402  pthread_cancel(pvt->thread);
403  pthread_kill(pvt->thread, SIGURG);
404  pthread_join(pvt->thread, NULL);
405 
406  console_pvt_lock(pvt);
407  Pa_AbortStream(pvt->stream);
408  Pa_CloseStream(pvt->stream);
409  pvt->stream = NULL;
410  pvt->streamstate = 0;
411  console_pvt_unlock(pvt);
412 
413  return 0;
414 }
415 
416 /*!
417  * \note Called with the pvt struct locked
418  */
419 static struct ast_channel *console_new(struct console_pvt *pvt, const char *ext, const char *ctx, int state, const char *linkedid)
420 {
421  struct ast_channel *chan;
422 
423  if (!(chan = ast_channel_alloc(1, state, pvt->cid_num, pvt->cid_name, NULL,
424  ext, ctx, linkedid, 0, "Console/%s", pvt->name))) {
425  return NULL;
426  }
427 
428  chan->tech = &console_tech;
432  chan->tech_pvt = ref_pvt(pvt);
433 
434  pvt->owner = chan;
435 
436  if (!ast_strlen_zero(pvt->language))
438 
440 
441  if (state != AST_STATE_DOWN) {
442  if (ast_pbx_start(chan)) {
444  ast_hangup(chan);
445  chan = NULL;
446  } else
447  start_stream(pvt);
448  }
449 
450  return chan;
451 }
452 
453 static struct ast_channel *console_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
454 {
455  format_t oldformat = format;
456  struct ast_channel *chan = NULL;
457  struct console_pvt *pvt;
458  char buf[512];
459 
460  if (!(pvt = find_pvt(data))) {
461  ast_log(LOG_ERROR, "Console device '%s' not found\n", (char *) data);
462  return NULL;
463  }
464 
465  format &= SUPPORTED_FORMATS;
466  if (!format) {
467  ast_log(LOG_NOTICE, "Channel requested with unsupported format(s): '%s'\n", ast_getformatname_multiple(buf, sizeof(buf), oldformat));
468  goto return_unref;
469  }
470 
471  if (pvt->owner) {
472  ast_log(LOG_NOTICE, "Console channel already active!\n");
473  *cause = AST_CAUSE_BUSY;
474  goto return_unref;
475  }
476 
477  console_pvt_lock(pvt);
478  chan = console_new(pvt, NULL, NULL, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
479  console_pvt_unlock(pvt);
480 
481  if (!chan)
482  ast_log(LOG_WARNING, "Unable to create new Console channel!\n");
483 
484 return_unref:
485  unref_pvt(pvt);
486 
487  return chan;
488 }
489 
490 static int console_digit_begin(struct ast_channel *c, char digit)
491 {
492  ast_verb(1, V_BEGIN "Console Received Beginning of Digit %c" V_END, digit);
493 
494  return -1; /* non-zero to request inband audio */
495 }
496 
497 static int console_digit_end(struct ast_channel *c, char digit, unsigned int duration)
498 {
499  ast_verb(1, V_BEGIN "Console Received End of Digit %c (duration %u)" V_END,
500  digit, duration);
501 
502  return -1; /* non-zero to request inband audio */
503 }
504 
505 static int console_text(struct ast_channel *c, const char *text)
506 {
507  ast_verb(1, V_BEGIN "Console Received Text '%s'" V_END, text);
508 
509  return 0;
510 }
511 
512 static int console_hangup(struct ast_channel *c)
513 {
514  struct console_pvt *pvt = c->tech_pvt;
515 
516  ast_verb(1, V_BEGIN "Hangup on Console" V_END);
517 
518  pvt->hookstate = 0;
519  pvt->owner = NULL;
520  stop_stream(pvt);
521 
522  c->tech_pvt = unref_pvt(pvt);
523 
524  return 0;
525 }
526 
527 static int console_answer(struct ast_channel *c)
528 {
529  struct console_pvt *pvt = c->tech_pvt;
530 
531  ast_verb(1, V_BEGIN "Call from Console has been Answered" V_END);
532 
534 
535  return start_stream(pvt);
536 }
537 
538 /*
539  * \brief Implementation of the ast_channel_tech read() callback
540  *
541  * Calling this function is harmless. However, if it does get called, it
542  * is an indication that something weird happened that really shouldn't
543  * have and is worth looking into.
544  *
545  * Why should this function not get called? Well, let me explain. There are
546  * a couple of ways to pass on audio that has come from this channel. The way
547  * that this channel driver uses is that once the audio is available, it is
548  * wrapped in an ast_frame and queued onto the channel using ast_queue_frame().
549  *
550  * The other method would be signalling to the core that there is audio waiting,
551  * and that it needs to call the channel's read() callback to get it. The way
552  * the channel gets signalled is that one or more file descriptors are placed
553  * in the fds array on the ast_channel which the core will poll() on. When the
554  * fd indicates that input is available, the read() callback is called. This
555  * is especially useful when there is a dedicated file descriptor where the
556  * audio is read from. An example would be the socket for an RTP stream.
557  */
558 static struct ast_frame *console_read(struct ast_channel *chan)
559 {
560  ast_debug(1, "I should not be called ...\n");
561 
562  return &ast_null_frame;
563 }
564 
565 static int console_call(struct ast_channel *c, char *dest, int timeout)
566 {
567  struct console_pvt *pvt = c->tech_pvt;
568  enum ast_control_frame_type ctrl;
569 
570  ast_verb(1, V_BEGIN "Call to device '%s' on console from '%s' <%s>" V_END,
571  dest,
572  S_COR(c->caller.id.name.valid, c->caller.id.name.str, ""),
573  S_COR(c->caller.id.number.valid, c->caller.id.number.str, ""));
574 
575  console_pvt_lock(pvt);
576 
577  if (pvt->autoanswer) {
578  pvt->hookstate = 1;
579  console_pvt_unlock(pvt);
580  ast_verb(1, V_BEGIN "Auto-answered" V_END);
581  ctrl = AST_CONTROL_ANSWER;
582  } else {
583  console_pvt_unlock(pvt);
584  ast_verb(1, V_BEGIN "Type 'console answer' to answer, or use the 'autoanswer' option "
585  "for future calls" V_END);
586  ctrl = AST_CONTROL_RINGING;
588  }
589 
590  ast_queue_control(c, ctrl);
591 
592  return start_stream(pvt);
593 }
594 
595 static int console_write(struct ast_channel *chan, struct ast_frame *f)
596 {
597  struct console_pvt *pvt = chan->tech_pvt;
598 
599  Pa_WriteStream(pvt->stream, f->data.ptr, f->samples);
600 
601  return 0;
602 }
603 
604 static int console_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
605 {
606  struct console_pvt *pvt = chan->tech_pvt;
607  int res = 0;
608 
609  switch (cond) {
610  case AST_CONTROL_BUSY:
612  case AST_CONTROL_RINGING:
614  case -1:
615  res = -1; /* Ask for inband indications */
616  break;
621  break;
622  case AST_CONTROL_HOLD:
623  ast_verb(1, V_BEGIN "Console Has Been Placed on Hold" V_END);
624  ast_moh_start(chan, data, pvt->mohinterpret);
625  break;
626  case AST_CONTROL_UNHOLD:
627  ast_verb(1, V_BEGIN "Console Has Been Retrieved from Hold" V_END);
628  ast_moh_stop(chan);
629  break;
630  default:
631  ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n",
632  cond, chan->name);
633  /* The core will play inband indications for us if appropriate */
634  res = -1;
635  }
636 
637  return res;
638 }
639 
640 static int console_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
641 {
642  struct console_pvt *pvt = newchan->tech_pvt;
643 
644  pvt->owner = newchan;
645 
646  return 0;
647 }
648 
649 /*!
650  * split a string in extension-context, returns pointers to malloc'ed
651  * strings.
652  * If we do not have 'overridecontext' then the last @ is considered as
653  * a context separator, and the context is overridden.
654  * This is usually not very necessary as you can play with the dialplan,
655  * and it is nice not to need it because you have '@' in SIP addresses.
656  * Return value is the buffer address.
657  *
658  * \note came from chan_oss
659  */
660 static char *ast_ext_ctx(struct console_pvt *pvt, const char *src, char **ext, char **ctx)
661 {
662  if (ext == NULL || ctx == NULL)
663  return NULL; /* error */
664 
665  *ext = *ctx = NULL;
666 
667  if (src && *src != '\0')
668  *ext = ast_strdup(src);
669 
670  if (*ext == NULL)
671  return NULL;
672 
673  if (!pvt->overridecontext) {
674  /* parse from the right */
675  *ctx = strrchr(*ext, '@');
676  if (*ctx)
677  *(*ctx)++ = '\0';
678  }
679 
680  return *ext;
681 }
682 
683 static struct console_pvt *get_active_pvt(void)
684 {
685  struct console_pvt *pvt;
686 
688  pvt = ref_pvt(active_pvt);
690 
691  return pvt;
692 }
693 
694 static char *cli_console_autoanswer(struct ast_cli_entry *e, int cmd,
695  struct ast_cli_args *a)
696 {
697  struct console_pvt *pvt = get_active_pvt();
698  char *res = CLI_SUCCESS;
699 
700  switch (cmd) {
701  case CLI_INIT:
702  e->command = "console {set|show} autoanswer [on|off]";
703  e->usage =
704  "Usage: console {set|show} autoanswer [on|off]\n"
705  " Enables or disables autoanswer feature. If used without\n"
706  " argument, displays the current on/off status of autoanswer.\n"
707  " The default value of autoanswer is in 'oss.conf'.\n";
708  return NULL;
709 
710  case CLI_GENERATE:
711  return NULL;
712  }
713 
714  if (!pvt) {
715  ast_cli(a->fd, "No console device is set as active.\n");
716  return CLI_FAILURE;
717  }
718 
719  if (a->argc == e->args - 1) {
720  ast_cli(a->fd, "Auto answer is %s.\n", pvt->autoanswer ? "on" : "off");
721  unref_pvt(pvt);
722  return CLI_SUCCESS;
723  }
724 
725  if (a->argc != e->args) {
726  unref_pvt(pvt);
727  return CLI_SHOWUSAGE;
728  }
729 
730  if (!strcasecmp(a->argv[e->args-1], "on"))
731  pvt->autoanswer = 1;
732  else if (!strcasecmp(a->argv[e->args - 1], "off"))
733  pvt->autoanswer = 0;
734  else
735  res = CLI_SHOWUSAGE;
736 
737  unref_pvt(pvt);
738 
739  return res;
740 }
741 
742 static char *cli_console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
743 {
744  struct console_pvt *pvt = get_active_pvt();
745 
746  if (cmd == CLI_INIT) {
747  e->command = "console flash";
748  e->usage =
749  "Usage: console flash\n"
750  " Flashes the call currently placed on the console.\n";
751  return NULL;
752  } else if (cmd == CLI_GENERATE)
753  return NULL;
754 
755  if (!pvt) {
756  ast_cli(a->fd, "No console device is set as active\n");
757  return CLI_FAILURE;
758  }
759 
760  if (a->argc != e->args)
761  return CLI_SHOWUSAGE;
762 
763  if (!pvt->owner) {
764  ast_cli(a->fd, "No call to flash\n");
765  unref_pvt(pvt);
766  return CLI_FAILURE;
767  }
768 
769  pvt->hookstate = 0;
770 
772 
773  unref_pvt(pvt);
774 
775  return CLI_SUCCESS;
776 }
777 
778 static char *cli_console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
779 {
780  char *s = NULL;
781  const char *mye = NULL, *myc = NULL;
782  struct console_pvt *pvt = get_active_pvt();
783 
784  if (cmd == CLI_INIT) {
785  e->command = "console dial";
786  e->usage =
787  "Usage: console dial [extension[@context]]\n"
788  " Dials a given extension (and context if specified)\n";
789  return NULL;
790  } else if (cmd == CLI_GENERATE)
791  return NULL;
792 
793  if (!pvt) {
794  ast_cli(a->fd, "No console device is currently set as active\n");
795  return CLI_FAILURE;
796  }
797 
798  if (a->argc > e->args + 1)
799  return CLI_SHOWUSAGE;
800 
801  if (pvt->owner) { /* already in a call */
802  int i;
803  struct ast_frame f = { AST_FRAME_DTMF };
804  const char *s;
805 
806  if (a->argc == e->args) { /* argument is mandatory here */
807  ast_cli(a->fd, "Already in a call. You can only dial digits until you hangup.\n");
808  unref_pvt(pvt);
809  return CLI_FAILURE;
810  }
811  s = a->argv[e->args];
812  /* send the string one char at a time */
813  for (i = 0; i < strlen(s); i++) {
814  f.subclass.integer = s[i];
815  ast_queue_frame(pvt->owner, &f);
816  }
817  unref_pvt(pvt);
818  return CLI_SUCCESS;
819  }
820 
821  /* if we have an argument split it into extension and context */
822  if (a->argc == e->args + 1) {
823  char *ext = NULL, *con = NULL;
824  s = ast_ext_ctx(pvt, a->argv[e->args], &ext, &con);
825  ast_debug(1, "provided '%s', exten '%s' context '%s'\n",
826  a->argv[e->args], mye, myc);
827  mye = ext;
828  myc = con;
829  }
830 
831  /* supply default values if needed */
832  if (ast_strlen_zero(mye))
833  mye = pvt->exten;
834  if (ast_strlen_zero(myc))
835  myc = pvt->context;
836 
837  if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
838  console_pvt_lock(pvt);
839  pvt->hookstate = 1;
840  console_new(pvt, mye, myc, AST_STATE_RINGING, NULL);
841  console_pvt_unlock(pvt);
842  } else
843  ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
844 
845  free(s);
846 
847  unref_pvt(pvt);
848 
849  return CLI_SUCCESS;
850 }
851 
852 static char *cli_console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
853 {
854  struct console_pvt *pvt = get_active_pvt();
855 
856  if (cmd == CLI_INIT) {
857  e->command = "console hangup";
858  e->usage =
859  "Usage: console hangup\n"
860  " Hangs up any call currently placed on the console.\n";
861  return NULL;
862  } else if (cmd == CLI_GENERATE)
863  return NULL;
864 
865  if (!pvt) {
866  ast_cli(a->fd, "No console device is set as active\n");
867  return CLI_FAILURE;
868  }
869 
870  if (a->argc != e->args)
871  return CLI_SHOWUSAGE;
872 
873  if (!pvt->owner && !pvt->hookstate) {
874  ast_cli(a->fd, "No call to hang up\n");
875  unref_pvt(pvt);
876  return CLI_FAILURE;
877  }
878 
879  pvt->hookstate = 0;
880  if (pvt->owner)
881  ast_queue_hangup(pvt->owner);
882 
883  unref_pvt(pvt);
884 
885  return CLI_SUCCESS;
886 }
887 
888 static char *cli_console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
889 {
890  const char *s;
891  struct console_pvt *pvt = get_active_pvt();
892  char *res = CLI_SUCCESS;
893 
894  if (cmd == CLI_INIT) {
895  e->command = "console {mute|unmute}";
896  e->usage =
897  "Usage: console {mute|unmute}\n"
898  " Mute/unmute the microphone.\n";
899  return NULL;
900  } else if (cmd == CLI_GENERATE)
901  return NULL;
902 
903  if (!pvt) {
904  ast_cli(a->fd, "No console device is set as active\n");
905  return CLI_FAILURE;
906  }
907 
908  if (a->argc != e->args)
909  return CLI_SHOWUSAGE;
910 
911  s = a->argv[e->args-1];
912  if (!strcasecmp(s, "mute"))
913  pvt->muted = 1;
914  else if (!strcasecmp(s, "unmute"))
915  pvt->muted = 0;
916  else
917  res = CLI_SHOWUSAGE;
918 
919  ast_verb(1, V_BEGIN "The Console is now %s" V_END,
920  pvt->muted ? "Muted" : "Unmuted");
921 
922  unref_pvt(pvt);
923 
924  return res;
925 }
926 
927 static char *cli_list_available(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
928 {
929  PaDeviceIndex idx, num, def_input, def_output;
930 
931  if (cmd == CLI_INIT) {
932  e->command = "console list available";
933  e->usage =
934  "Usage: console list available\n"
935  " List all available devices.\n";
936  return NULL;
937  } else if (cmd == CLI_GENERATE)
938  return NULL;
939 
940  if (a->argc != e->args)
941  return CLI_SHOWUSAGE;
942 
943  ast_cli(a->fd, "\n"
944  "=============================================================\n"
945  "=== Available Devices =======================================\n"
946  "=============================================================\n"
947  "===\n");
948 
949  num = Pa_GetDeviceCount();
950  if (!num) {
951  ast_cli(a->fd, "(None)\n");
952  return CLI_SUCCESS;
953  }
954 
955  def_input = Pa_GetDefaultInputDevice();
956  def_output = Pa_GetDefaultOutputDevice();
957  for (idx = 0; idx < num; idx++) {
958  const PaDeviceInfo *dev = Pa_GetDeviceInfo(idx);
959  if (!dev)
960  continue;
961  ast_cli(a->fd, "=== ---------------------------------------------------------\n"
962  "=== Device Name: %s\n", dev->name);
963  if (dev->maxInputChannels)
964  ast_cli(a->fd, "=== ---> %sInput Device\n", (idx == def_input) ? "Default " : "");
965  if (dev->maxOutputChannels)
966  ast_cli(a->fd, "=== ---> %sOutput Device\n", (idx == def_output) ? "Default " : "");
967  ast_cli(a->fd, "=== ---------------------------------------------------------\n===\n");
968  }
969 
970  ast_cli(a->fd, "=============================================================\n\n");
971 
972  return CLI_SUCCESS;
973 }
974 
975 static char *cli_list_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
976 {
977  struct ao2_iterator i;
978  struct console_pvt *pvt;
979 
980  if (cmd == CLI_INIT) {
981  e->command = "console list devices";
982  e->usage =
983  "Usage: console list devices\n"
984  " List all configured devices.\n";
985  return NULL;
986  } else if (cmd == CLI_GENERATE)
987  return NULL;
988 
989  if (a->argc != e->args)
990  return CLI_SHOWUSAGE;
991 
992  ast_cli(a->fd, "\n"
993  "=============================================================\n"
994  "=== Configured Devices ======================================\n"
995  "=============================================================\n"
996  "===\n");
997 
998  i = ao2_iterator_init(pvts, 0);
999  while ((pvt = ao2_iterator_next(&i))) {
1000  console_pvt_lock(pvt);
1001 
1002  ast_cli(a->fd, "=== ---------------------------------------------------------\n"
1003  "=== Device Name: %s\n"
1004  "=== ---> Active: %s\n"
1005  "=== ---> Input Device: %s\n"
1006  "=== ---> Output Device: %s\n"
1007  "=== ---> Context: %s\n"
1008  "=== ---> Extension: %s\n"
1009  "=== ---> CallerID Num: %s\n"
1010  "=== ---> CallerID Name: %s\n"
1011  "=== ---> MOH Interpret: %s\n"
1012  "=== ---> Language: %s\n"
1013  "=== ---> Parkinglot: %s\n"
1014  "=== ---> Muted: %s\n"
1015  "=== ---> Auto-Answer: %s\n"
1016  "=== ---> Override Context: %s\n"
1017  "=== ---------------------------------------------------------\n===\n",
1018  pvt->name, (pvt == active_pvt) ? "Yes" : "No",
1019  pvt->input_device, pvt->output_device, pvt->context,
1020  pvt->exten, pvt->cid_num, pvt->cid_name, pvt->mohinterpret,
1021  pvt->language, pvt->parkinglot, pvt->muted ? "Yes" : "No", pvt->autoanswer ? "Yes" : "No",
1022  pvt->overridecontext ? "Yes" : "No");
1023 
1024  console_pvt_unlock(pvt);
1025  unref_pvt(pvt);
1026  }
1028 
1029  ast_cli(a->fd, "=============================================================\n\n");
1030 
1031  return CLI_SUCCESS;
1032 }
1033 /*!
1034  * \brief answer command from the console
1035  */
1036 static char *cli_console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1037 {
1038  struct console_pvt *pvt = get_active_pvt();
1039 
1040  switch (cmd) {
1041  case CLI_INIT:
1042  e->command = "console answer";
1043  e->usage =
1044  "Usage: console answer\n"
1045  " Answers an incoming call on the console channel.\n";
1046  return NULL;
1047 
1048  case CLI_GENERATE:
1049  return NULL; /* no completion */
1050  }
1051 
1052  if (!pvt) {
1053  ast_cli(a->fd, "No console device is set as active\n");
1054  return CLI_FAILURE;
1055  }
1056 
1057  if (a->argc != e->args) {
1058  unref_pvt(pvt);
1059  return CLI_SHOWUSAGE;
1060  }
1061 
1062  if (!pvt->owner) {
1063  ast_cli(a->fd, "No one is calling us\n");
1064  unref_pvt(pvt);
1065  return CLI_FAILURE;
1066  }
1067 
1068  pvt->hookstate = 1;
1069 
1070  ast_indicate(pvt->owner, -1);
1071 
1073 
1074  unref_pvt(pvt);
1075 
1076  return CLI_SUCCESS;
1077 }
1078 
1079 /*!
1080  * \brief Console send text CLI command
1081  *
1082  * \note concatenate all arguments into a single string. argv is NULL-terminated
1083  * so we can use it right away
1084  */
1085 static char *cli_console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1086 {
1087  char buf[TEXT_SIZE];
1088  struct console_pvt *pvt = get_active_pvt();
1089  struct ast_frame f = {
1091  .data.ptr = buf,
1092  .src = "console_send_text",
1093  };
1094  int len;
1095 
1096  if (cmd == CLI_INIT) {
1097  e->command = "console send text";
1098  e->usage =
1099  "Usage: console send text <message>\n"
1100  " Sends a text message for display on the remote terminal.\n";
1101  return NULL;
1102  } else if (cmd == CLI_GENERATE)
1103  return NULL;
1104 
1105  if (!pvt) {
1106  ast_cli(a->fd, "No console device is set as active\n");
1107  return CLI_FAILURE;
1108  }
1109 
1110  if (a->argc < e->args + 1) {
1111  unref_pvt(pvt);
1112  return CLI_SHOWUSAGE;
1113  }
1114 
1115  if (!pvt->owner) {
1116  ast_cli(a->fd, "Not in a call\n");
1117  unref_pvt(pvt);
1118  return CLI_FAILURE;
1119  }
1120 
1121  ast_join(buf, sizeof(buf) - 1, a->argv + e->args);
1122  if (ast_strlen_zero(buf)) {
1123  unref_pvt(pvt);
1124  return CLI_SHOWUSAGE;
1125  }
1126 
1127  len = strlen(buf);
1128  buf[len] = '\n';
1129  f.datalen = len + 1;
1130 
1131  ast_queue_frame(pvt->owner, &f);
1132 
1133  unref_pvt(pvt);
1134 
1135  return CLI_SUCCESS;
1136 }
1137 
1138 static void set_active(struct console_pvt *pvt, const char *value)
1139 {
1140  if (pvt == &globals) {
1141  ast_log(LOG_ERROR, "active is only valid as a per-device setting\n");
1142  return;
1143  }
1144 
1145  if (!ast_true(value))
1146  return;
1147 
1149  if (active_pvt)
1150  unref_pvt(active_pvt);
1151  active_pvt = ref_pvt(pvt);
1153 }
1154 
1155 static char *cli_console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1156 {
1157  struct console_pvt *pvt;
1158 
1159  switch (cmd) {
1160  case CLI_INIT:
1161  e->command = "console {set|show} active";
1162  e->usage =
1163  "Usage: console {set|show} active [<device>]\n"
1164  " Set or show the active console device for the Asterisk CLI.\n";
1165  return NULL;
1166  case CLI_GENERATE:
1167  if (a->pos == e->args) {
1168  struct ao2_iterator i;
1169  int x = 0;
1170  char *res = NULL;
1171  i = ao2_iterator_init(pvts, 0);
1172  while ((pvt = ao2_iterator_next(&i))) {
1173  if (++x > a->n && !strncasecmp(pvt->name, a->word, strlen(a->word)))
1174  res = ast_strdup(pvt->name);
1175  unref_pvt(pvt);
1176  if (res) {
1178  return res;
1179  }
1180  }
1182  }
1183  return NULL;
1184  }
1185 
1186  if (a->argc < e->args)
1187  return CLI_SHOWUSAGE;
1188 
1189  if (a->argc == 3) {
1190  pvt = get_active_pvt();
1191 
1192  if (!pvt)
1193  ast_cli(a->fd, "No device is currently set as the active console device.\n");
1194  else {
1195  console_pvt_lock(pvt);
1196  ast_cli(a->fd, "The active console device is '%s'.\n", pvt->name);
1197  console_pvt_unlock(pvt);
1198  pvt = unref_pvt(pvt);
1199  }
1200 
1201  return CLI_SUCCESS;
1202  }
1203 
1204  if (!(pvt = find_pvt(a->argv[e->args - 1]))) {
1205  ast_cli(a->fd, "Could not find a device called '%s'.\n", a->argv[e->args]);
1206  return CLI_FAILURE;
1207  }
1208 
1209  set_active(pvt, "yes");
1210 
1211  console_pvt_lock(pvt);
1212  ast_cli(a->fd, "The active console device has been set to '%s'\n", pvt->name);
1213  console_pvt_unlock(pvt);
1214 
1215  unref_pvt(pvt);
1216 
1217  return CLI_SUCCESS;
1218 }
1219 
1220 static struct ast_cli_entry cli_console[] = {
1221  AST_CLI_DEFINE(cli_console_dial, "Dial an extension from the console"),
1222  AST_CLI_DEFINE(cli_console_hangup, "Hangup a call on the console"),
1223  AST_CLI_DEFINE(cli_console_mute, "Disable/Enable mic input"),
1224  AST_CLI_DEFINE(cli_console_answer, "Answer an incoming console call"),
1225  AST_CLI_DEFINE(cli_console_sendtext, "Send text to a connected party"),
1226  AST_CLI_DEFINE(cli_console_flash, "Send a flash to the connected party"),
1227  AST_CLI_DEFINE(cli_console_autoanswer, "Turn autoanswer on or off"),
1228  AST_CLI_DEFINE(cli_list_available, "List available devices"),
1229  AST_CLI_DEFINE(cli_list_devices, "List configured devices"),
1230  AST_CLI_DEFINE(cli_console_active, "View or Set the active console device"),
1231 };
1232 
1233 /*!
1234  * \brief Set default values for a pvt struct
1235  *
1236  * \note This function expects the pvt lock to be held.
1237  */
1238 static void set_pvt_defaults(struct console_pvt *pvt)
1239 {
1240  if (pvt == &globals) {
1241  ast_string_field_set(pvt, mohinterpret, "default");
1242  ast_string_field_set(pvt, context, "default");
1243  ast_string_field_set(pvt, exten, "s");
1244  ast_string_field_set(pvt, language, "");
1245  ast_string_field_set(pvt, cid_num, "");
1246  ast_string_field_set(pvt, cid_name, "");
1247  ast_string_field_set(pvt, parkinglot, "");
1248 
1249  pvt->overridecontext = 0;
1250  pvt->autoanswer = 0;
1251  } else {
1253 
1261 
1263  pvt->autoanswer = globals.autoanswer;
1264 
1266  }
1267 }
1268 
1269 static void store_callerid(struct console_pvt *pvt, const char *value)
1270 {
1271  char cid_name[256];
1272  char cid_num[256];
1273 
1274  ast_callerid_split(value, cid_name, sizeof(cid_name),
1275  cid_num, sizeof(cid_num));
1276 
1277  ast_string_field_set(pvt, cid_name, cid_name);
1278  ast_string_field_set(pvt, cid_num, cid_num);
1279 }
1280 
1281 /*!
1282  * \brief Store a configuration parameter in a pvt struct
1283  *
1284  * \note This function expects the pvt lock to be held.
1285  */
1286 static void store_config_core(struct console_pvt *pvt, const char *var, const char *value)
1287 {
1288  if (pvt == &globals && !ast_jb_read_conf(&global_jbconf, var, value))
1289  return;
1290 
1291  CV_START(var, value);
1292 
1293  CV_STRFIELD("context", pvt, context);
1294  CV_STRFIELD("extension", pvt, exten);
1295  CV_STRFIELD("mohinterpret", pvt, mohinterpret);
1296  CV_STRFIELD("language", pvt, language);
1297  CV_F("callerid", store_callerid(pvt, value));
1298  CV_BOOL("overridecontext", pvt->overridecontext);
1299  CV_BOOL("autoanswer", pvt->autoanswer);
1300  CV_STRFIELD("parkinglot", pvt, parkinglot);
1301 
1302  if (pvt != &globals) {
1303  CV_F("active", set_active(pvt, value))
1304  CV_STRFIELD("input_device", pvt, input_device);
1305  CV_STRFIELD("output_device", pvt, output_device);
1306  }
1307 
1308  ast_log(LOG_WARNING, "Unknown option '%s'\n", var);
1309 
1310  CV_END;
1311 }
1312 
1313 static void pvt_destructor(void *obj)
1314 {
1315  struct console_pvt *pvt = obj;
1316 
1318 }
1319 
1320 static int init_pvt(struct console_pvt *pvt, const char *name)
1321 {
1322  pvt->thread = AST_PTHREADT_NULL;
1323 
1324  if (ast_string_field_init(pvt, 32))
1325  return -1;
1326 
1327  ast_string_field_set(pvt, name, S_OR(name, ""));
1328 
1329  return 0;
1330 }
1331 
1332 static void build_device(struct ast_config *cfg, const char *name)
1333 {
1334  struct ast_variable *v;
1335  struct console_pvt *pvt;
1336  int new = 0;
1337 
1338  if ((pvt = find_pvt(name))) {
1339  console_pvt_lock(pvt);
1340  set_pvt_defaults(pvt);
1341  pvt->destroy = 0;
1342  } else {
1343  if (!(pvt = ao2_alloc(sizeof(*pvt), pvt_destructor)))
1344  return;
1345  init_pvt(pvt, name);
1346  set_pvt_defaults(pvt);
1347  new = 1;
1348  }
1349 
1350  for (v = ast_variable_browse(cfg, name); v; v = v->next)
1351  store_config_core(pvt, v->name, v->value);
1352 
1353  if (new)
1354  ao2_link(pvts, pvt);
1355  else
1356  console_pvt_unlock(pvt);
1357 
1358  unref_pvt(pvt);
1359 }
1360 
1361 static int pvt_mark_destroy_cb(void *obj, void *arg, int flags)
1362 {
1363  struct console_pvt *pvt = obj;
1364  pvt->destroy = 1;
1365  return 0;
1366 }
1367 
1368 static void destroy_pvts(void)
1369 {
1370  struct ao2_iterator i;
1371  struct console_pvt *pvt;
1372 
1373  i = ao2_iterator_init(pvts, 0);
1374  while ((pvt = ao2_iterator_next(&i))) {
1375  if (pvt->destroy) {
1376  ao2_unlink(pvts, pvt);
1378  if (active_pvt == pvt)
1379  active_pvt = unref_pvt(pvt);
1381  }
1382  unref_pvt(pvt);
1383  }
1385 }
1386 
1387 /*!
1388  * \brief Load the configuration
1389  * \param reload if this was called due to a reload
1390  * \retval 0 success
1391  * \retval -1 failure
1392  */
1393 static int load_config(int reload)
1394 {
1395  struct ast_config *cfg;
1396  struct ast_variable *v;
1397  struct ast_flags config_flags = { 0 };
1398  char *context = NULL;
1399 
1400  /* default values */
1401  memcpy(&global_jbconf, &default_jbconf, sizeof(global_jbconf));
1405 
1406  if (!(cfg = ast_config_load(config_file, config_flags))) {
1407  ast_log(LOG_NOTICE, "Unable to open configuration file %s!\n", config_file);
1408  return -1;
1409  } else if (cfg == CONFIG_STATUS_FILEINVALID) {
1410  ast_log(LOG_NOTICE, "Config file %s has an invalid format\n", config_file);
1411  return -1;
1412  }
1413 
1415 
1417  for (v = ast_variable_browse(cfg, "general"); v; v = v->next)
1418  store_config_core(&globals, v->name, v->value);
1420 
1421  while ((context = ast_category_browse(cfg, context))) {
1422  if (strcasecmp(context, "general"))
1423  build_device(cfg, context);
1424  }
1425 
1426  ast_config_destroy(cfg);
1427 
1428  destroy_pvts();
1429 
1430  return 0;
1431 }
1432 
1433 static int pvt_hash_cb(const void *obj, const int flags)
1434 {
1435  const struct console_pvt *pvt = obj;
1436 
1437  return ast_str_case_hash(pvt->name);
1438 }
1439 
1440 static int pvt_cmp_cb(void *obj, void *arg, int flags)
1441 {
1442  struct console_pvt *pvt = obj, *pvt2 = arg;
1443 
1444  return !strcasecmp(pvt->name, pvt2->name) ? CMP_MATCH | CMP_STOP : 0;
1445 }
1446 
1447 static void stop_streams(void)
1448 {
1449  struct console_pvt *pvt;
1450  struct ao2_iterator i;
1451 
1452  i = ao2_iterator_init(pvts, 0);
1453  while ((pvt = ao2_iterator_next(&i))) {
1454  if (pvt->hookstate)
1455  stop_stream(pvt);
1456  unref_pvt(pvt);
1457  }
1459 }
1460 
1461 static int unload_module(void)
1462 {
1463  ast_channel_unregister(&console_tech);
1464  ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console));
1465 
1466  stop_streams();
1467 
1468  Pa_Terminate();
1469 
1470  /* Will unref all the pvts so they will get destroyed, too */
1471  ao2_ref(pvts, -1);
1472 
1474 
1475  return 0;
1476 }
1477 
1478 static int load_module(void)
1479 {
1480  PaError res;
1481 
1482  init_pvt(&globals, NULL);
1483 
1485  goto return_error;
1486 
1487  if (load_config(0))
1488  goto return_error;
1489 
1490  res = Pa_Initialize();
1491  if (res != paNoError) {
1492  ast_log(LOG_WARNING, "Failed to initialize audio system - (%d) %s\n",
1493  res, Pa_GetErrorText(res));
1494  goto return_error_pa_init;
1495  }
1496 
1497  if (ast_channel_register(&console_tech)) {
1498  ast_log(LOG_ERROR, "Unable to register channel type 'Console'\n");
1499  goto return_error_chan_reg;
1500  }
1501 
1502  if (ast_cli_register_multiple(cli_console, ARRAY_LEN(cli_console)))
1503  goto return_error_cli_reg;
1504 
1505  return AST_MODULE_LOAD_SUCCESS;
1506 
1507 return_error_cli_reg:
1508  ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console));
1509 return_error_chan_reg:
1510  ast_channel_unregister(&console_tech);
1511 return_error_pa_init:
1512  Pa_Terminate();
1513 return_error:
1514  if (pvts)
1515  ao2_ref(pvts, -1);
1516  pvts = NULL;
1518 
1519  return AST_MODULE_LOAD_DECLINE;
1520 }
1521 
1522 static int reload(void)
1523 {
1524  return load_config(1);
1525 }
1526 
1527 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Console Channel Driver",
1528  .load = load_module,
1529  .unload = unload_module,
1530  .reload = reload,
1531  .load_pri = AST_MODPRI_CHANNEL_DRIVER,
1532 );
int ast_queue_hangup(struct ast_channel *chan)
Queue a hangup frame.
Definition: channel.c:1569
union ast_frame_subclass subclass
Definition: frame.h:146
int ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2804
static int open_stream(struct console_pvt *pvt)
Definition: chan_console.c:293
struct ast_frame fr
Definition: chan_console.c:146
static char * cli_console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Console send text CLI command.
#define ast_rwlock_rdlock(a)
Definition: lock.h:201
static char mohinterpret[MAX_MUSICCLASS]
Definition: chan_alsa.c:110
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:109
Main Channel structure associated with a channel.
Definition: channel.h:742
static int pvt_hash_cb(const void *obj, const int flags)
Music on hold handling.
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:191
char * str
Subscriber phone number (Malloced)
Definition: channel.h:241
const char *const type
Definition: channel.h:508
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_link(arg1, arg2)
Definition: astobj2.h:785
#define AST_RWLOCK_DEFINE_STATIC(rwlock)
Definition: lock.h:549
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static char parkinglot[AST_MAX_CONTEXT]
Definition: chan_mgcp.c:156
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame with payload.
Definition: channel.c:1601
struct ast_frame ast_null_frame
Definition: frame.c:131
struct ast_party_caller caller
Channel Caller ID information.
Definition: channel.h:804
static void stop_streams(void)
int ast_callerid_split(const char *src, char *name, int namelen, char *num, int numlen)
Definition: callerid.c:1093
static void build_device(struct ast_config *cfg, const char *name)
unsigned int muted
Definition: chan_console.c:152
const ast_string_field cid_num
Definition: chan_console.c:140
static char * cli_console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: chan_console.c:778
static char * cli_console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
#define ast_strdup(a)
Definition: astmm.h:109
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
format_t writeformat
Definition: channel.h:854
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: cli.c:2177
PaStream * stream
Definition: chan_console.c:144
static char * cli_console_autoanswer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: chan_console.c:694
#define AST_CAUSE_SWITCH_CONGESTION
Definition: causes.h:122
int ast_indicate(struct ast_channel *chan, int condition)
Indicates condition of channel.
Definition: channel.c:4393
static int console_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
Definition: chan_console.c:640
const ast_string_field context
Definition: chan_console.c:140
struct ast_party_name name
Subscriber name.
Definition: channel.h:290
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition: channel.c:938
static int reload(void)
void * ptr
Definition: frame.h:160
void * tech_pvt
Definition: channel.h:744
static void set_pvt_defaults(struct console_pvt *pvt)
Set default values for a pvt struct.
unsigned int destroy
Definition: chan_console.c:159
static struct ast_jb_conf global_jbconf
Definition: chan_console.c:185
#define ao2_iterator_next(arg1)
Definition: astobj2.h:1126
static char * cli_console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: chan_console.c:888
descriptor for a cli entry.
Definition: cli.h:165
const int argc
Definition: cli.h:154
#define LOG_WARNING
Definition: logger.h:144
enum ast_pbx_result ast_pbx_start(struct ast_channel *c)
Create a new thread and start the PBX.
Definition: pbx.c:5879
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
Definition: config.c:597
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:910
int ast_jb_read_conf(struct ast_jb_conf *conf, const char *varname, const char *value)
Sets jitterbuffer configuration property.
Definition: abstract_jb.c:577
#define AST_FRAME_DTMF
Definition: frame.h:128
static char * cli_console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: chan_console.c:852
unsigned int flags
Definition: utils.h:201
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
unsigned int overridecontext
Definition: chan_console.c:156
#define var
Definition: ast_expr2f.c:606
Definition: cli.h:146
char * str
Subscriber name (Malloced)
Definition: channel.h:214
static int console_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
Definition: chan_console.c:604
static int load_config(int reload)
Load the configuration.
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
Definition: stringfields.h:235
static int start_stream(struct console_pvt *pvt)
Definition: chan_console.c:354
#define ast_mutex_lock(a)
Definition: lock.h:155
struct ast_channel * owner
Definition: chan_console.c:142
int ast_channel_register(const struct ast_channel_tech *tech)
Register a channel technology (a new channel driver) Called by a channel module to register the kind ...
Definition: channel.c:907
static int console_digit_begin(struct ast_channel *c, char digit)
Definition: chan_console.c:490
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
Create an iterator for a container.
Definition: astobj2.c:818
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
char * text
Definition: app_queue.c:1091
format_t nativeformats
Definition: channel.h:852
const ast_string_field parkinglot
Definition: chan_console.c:140
static int unload_module(void)
static ast_mutex_t globals_lock
Definition: chan_console.c:164
static struct console_pvt * unref_pvt(struct console_pvt *pvt)
Definition: chan_console.c:238
const char * data
Definition: channel.h:755
static char * cli_list_available(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: chan_console.c:927
#define SAMPLE_RATE
The sample rate to request from PortAudio.
Definition: chan_console.c:78
int value
Definition: syslog.c:39
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
static int console_hangup(struct ast_channel *c)
Definition: chan_console.c:512
const ast_string_field linkedid
Definition: channel.h:787
#define ast_rwlock_unlock(a)
Definition: lock.h:200
const char * ext
Definition: http.c:112
void ast_moh_stop(struct ast_channel *chan)
Turn off music on hold on a given channel.
Definition: channel.c:8051
static char cid_num[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:157
#define ast_verb(level,...)
Definition: logger.h:243
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
struct ast_channel * ast_channel_alloc(int needqueue, int state, const char *cid_num, const char *cid_name, const char *acctcode, const char *exten, const char *context, const char *linkedid, const int amaflag, const char *name_fmt,...)
Definition: channel.c:9825
unsigned int autoanswer
Definition: chan_console.c:154
static int console_digit_end(struct ast_channel *c, char digit, unsigned int duration)
Definition: chan_console.c:497
static void store_callerid(struct console_pvt *pvt, const char *value)
int args
This gets set in ast_cli_register()
Definition: cli.h:179
Console pvt structure.
Definition: chan_console.c:118
static struct ast_frame * console_read(struct ast_channel *chan)
Definition: chan_console.c:558
static char * ast_ext_ctx(struct console_pvt *pvt, const char *src, char **ext, char **ctx)
Definition: chan_console.c:660
static struct ast_jb_conf default_jbconf
Global jitterbuffer configuration.
Definition: chan_console.c:178
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:426
static struct ast_channel_tech console_tech
Definition: chan_console.c:208
struct ast_party_id id
Caller party ID.
Definition: channel.h:370
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
const char * value
Definition: config.h:79
static struct ast_cli_entry cli_console[]
#define CV_END
close a variable parsing block
Definition: config.h:733
static int pvt_mark_destroy_cb(void *obj, void *arg, int flags)
General Asterisk PBX channel definitions.
#define CV_START(__in_var, __in_val)
the macro to open a block for variable parsing
Definition: config.h:727
#define TEXT_SIZE
Maximum text message length.
Definition: chan_console.c:103
const int fd
Definition: cli.h:153
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:249
#define ast_config_load(filename, flags)
Load a config file.
Definition: config.h:170
#define AST_PTHREADT_NULL
Definition: lock.h:65
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
const int n
Definition: cli.h:159
ast_cond_t cond
Definition: app_meetme.c:963
int datalen
Definition: frame.h:148
#define AST_STRING_FIELD(name)
Declare a string field.
Definition: stringfields.h:220
char * ast_category_browse(struct ast_config *config, const char *prev)
Goes through categories.
Definition: config.c:810
#define ao2_ref(o, delta)
Definition: astobj2.h:472
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
Definition: strings.h:83
#define NUM_SAMPLES
The number of samples to configure the portaudio stream for.
Definition: chan_console.c:90
#define console_pvt_unlock(pvt)
unlock a console_pvt struct
Definition: chan_console.c:229
static struct ast_channel * console_new(struct console_pvt *pvt, const char *ext, const char *ctx, int state, const char *linkedid)
Definition: chan_console.c:419
static char language[MAX_LANGUAGE]
Definition: chan_alsa.c:108
const char * name
Definition: config.h:77
static struct console_pvt globals
static struct ao2_container * pvts
Definition: chan_console.c:166
unsigned int hookstate
Definition: chan_console.c:150
int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Determine whether an extension exists.
Definition: pbx.c:5400
static void store_config_core(struct console_pvt *pvt, const char *var, const char *value)
Store a configuration parameter in a pvt struct.
#define AST_FORMAT_SLINEAR16
Definition: frame.h:272
Structure to describe a channel &quot;technology&quot;, ie a channel driver See for examples: ...
Definition: channel.h:507
Core PBX routines and definitions.
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *f)
Queue one or more frames to a channel&#39;s frame queue.
Definition: channel.c:1558
#define OUTPUT_CHANNELS
Mono Output.
Definition: chan_console.c:96
const char *const * argv
Definition: cli.h:155
const ast_string_field language
Definition: chan_console.c:140
static void * stream_monitor(void *data)
Stream monitor thread.
Definition: chan_console.c:263
static int init_pvt(struct console_pvt *pvt, const char *name)
#define CV_BOOL(__x, __dst)
helper macros to assign the value to a BOOL, UINT, static string and dynamic string ...
Definition: config.h:742
ast_control_frame_type
Internal control frame subtype field values.
Definition: frame.h:319
#define LOG_ERROR
Definition: logger.h:155
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is &quot;true&quot;. This function checks to see whether a string passed to it is an indication of an &quot;true&quot; value. It checks to see if the string is &quot;yes&quot;, &quot;true&quot;, &quot;y&quot;, &quot;t&quot;, &quot;on&quot; or &quot;1&quot;.
Definition: utils.c:1533
int64_t format_t
Definition: frame_defs.h:32
#define free(a)
Definition: astmm.h:94
static char * cli_console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
answer command from the console
#define CLI_SHOWUSAGE
Definition: cli.h:44
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
const ast_string_field name
Definition: channel.h:787
int ast_moh_start(struct ast_channel *chan, const char *mclass, const char *interpclass)
Turn on music on hold on a given channel.
Definition: channel.c:8040
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static int console_call(struct ast_channel *c, char *dest, int timeout)
Definition: chan_console.c:565
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:430
#define V_END
Definition: chan_console.c:107
#define LOG_NOTICE
Definition: logger.h:133
pthread_t thread
Definition: chan_console.c:161
static void set_active(struct console_pvt *pvt, const char *value)
static int load_module(void)
#define ao2_find(arg1, arg2, arg3)
Definition: astobj2.h:964
static int console_write(struct ast_channel *chan, struct ast_frame *f)
Definition: chan_console.c:595
static ast_rwlock_t active_lock
Definition: chan_console.c:170
#define CLI_FAILURE
Definition: cli.h:45
static const char name[]
char * command
Definition: cli.h:180
static int console_text(struct ast_channel *c, const char *text)
Definition: chan_console.c:505
static struct console_pvt * ref_pvt(struct console_pvt *pvt)
Definition: chan_console.c:231
const ast_string_field input_device
Definition: chan_console.c:140
void ast_join(char *s, size_t len, const char *const w[])
Definition: utils.c:1690
#define INPUT_CHANNELS
Mono Input.
Definition: chan_console.c:93
static struct ast_format f[]
Definition: format_g726.c:181
const char * word
Definition: cli.h:157
#define V_BEGIN
Dance, Kirby, Dance!
Definition: chan_console.c:106
unsigned int flags
Combination of the AST_JB_ENABLED, AST_JB_FORCED and AST_JB_LOG flags.
Definition: abstract_jb.h:58
static const char type[]
Definition: chan_nbs.c:57
void ao2_iterator_destroy(struct ao2_iterator *i)
Destroy a container iterator.
Definition: astobj2.c:833
#define NUM_PVT_BUCKETS
Definition: chan_console.c:167
Structure used to handle boolean flags.
Definition: utils.h:200
static char cid_name[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:158
void ast_jb_configure(struct ast_channel *chan, const struct ast_jb_conf *conf)
Configures a jitterbuffer on a channel.
Definition: abstract_jb.c:616
const char * usage
Definition: cli.h:171
#define ast_rwlock_wrlock(a)
Definition: lock.h:202
static void destroy_pvts(void)
#define CLI_SUCCESS
Definition: cli.h:43
#define CV_F(__pattern, __body)
call a generic function if the name matches.
Definition: config.h:736
static struct console_pvt * active_pvt
Definition: chan_console.c:169
static struct ast_channel * console_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
Definition: chan_console.c:453
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1053
Standard Command Line Interface.
format_t readformat
Definition: channel.h:853
#define ao2_container_alloc(arg1, arg2, arg3)
Definition: astobj2.h:734
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
#define SUPPORTED_FORMATS
Formats natively supported by this module.
Definition: chan_console.c:206
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition: cli.c:2167
const int pos
Definition: cli.h:158
unsigned int streamstate
Definition: chan_console.c:148
int ast_setstate(struct ast_channel *chan, enum ast_channel_state)
Change the state of a channel.
Definition: channel.c:7119
static int pvt_cmp_cb(void *obj, void *arg, int flags)
#define AST_CAUSE_BUSY
Definition: causes.h:148
char * ast_getformatname_multiple(char *buf, size_t size, format_t format)
Get the names of a set of formats.
Definition: frame.c:591
Data structure associated with a single frame of data.
Definition: frame.h:142
int hangupcause
Definition: channel.h:849
Internal Asterisk hangup causes.
const ast_string_field mohinterpret
Definition: chan_console.c:140
static char * cli_list_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: chan_console.c:975
static char * cli_console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: chan_console.c:742
static struct console_pvt * get_active_pvt(void)
Definition: chan_console.c:683
const ast_string_field output_device
Definition: chan_console.c:140
enum ast_frame_type frametype
Definition: frame.h:144
#define console_pvt_lock(pvt)
lock a console_pvt struct
Definition: chan_console.c:226
struct ast_variable * next
Definition: config.h:82
#define CV_STRFIELD(__x, __obj, __field)
Definition: config.h:746
unsigned char valid
TRUE if the name information is valid/present.
Definition: channel.h:229
#define CONFIG_STATUS_FILEINVALID
Definition: config.h:52
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:107
static int console_answer(struct ast_channel *c)
Definition: chan_console.c:527
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
static struct console_pvt * find_pvt(const char *name)
Definition: chan_console.c:244
const ast_string_field name
Definition: chan_console.c:140
static snd_pcm_format_t format
Definition: chan_alsa.c:93
union ast_frame::@172 data
struct ast_channel_tech * tech
Definition: channel.h:743
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:253
unsigned char valid
TRUE if the number information is valid/present.
Definition: channel.h:247
#define ao2_unlink(arg1, arg2)
Definition: astobj2.h:817
General jitterbuffer configuration.
Definition: abstract_jb.h:55
static int stop_stream(struct console_pvt *pvt)
Definition: chan_console.c:397
#define AST_MUTEX_DEFINE_STATIC(mutex)
Definition: lock.h:526
const ast_string_field cid_name
Definition: chan_console.c:140
static force_inline int attribute_pure ast_str_case_hash(const char *str)
Compute a hash value on a case-insensitive string.
Definition: strings.h:989
const ast_string_field exten
Definition: chan_console.c:140
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150
static const char config_file[]
Definition: chan_console.c:110
#define ast_mutex_unlock(a)
Definition: lock.h:156
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:344
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:292
static void pvt_destructor(void *obj)