Asterisk - The Open Source Telephony Project  21.4.1
sig_analog.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2009, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \brief Analog signaling module
22  *
23  * \author Matthew Fredrickson <creslin@digium.com>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 #include <errno.h>
33 #include <ctype.h>
34 
35 #include "asterisk/utils.h"
36 #include "asterisk/options.h"
37 #include "asterisk/pickup.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/file.h"
40 #include "asterisk/callerid.h"
41 #include "asterisk/say.h"
42 #include "asterisk/manager.h"
43 #include "asterisk/astdb.h"
44 #include "asterisk/features.h"
45 #include "asterisk/causes.h"
46 #include "asterisk/features_config.h"
47 #include "asterisk/bridge.h"
48 #include "asterisk/parking.h"
49 
50 #include "sig_analog.h"
51 
52 /*** DOCUMENTATION
53  ***/
54 
55 /*! \note
56  * Define if you want to check the hook state for an FXO (FXS signalled) interface
57  * before dialing on it. Certain FXO interfaces always think they're out of
58  * service with this method however.
59  */
60 /* #define DAHDI_CHECK_HOOKSTATE */
61 
62 #define POLARITY_IDLE 0
63 #define POLARITY_REV 1
64 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
65 static char analog_defaultcic[64] = "";
66 static char analog_defaultozz[64] = "";
67 
68 static const struct {
69  enum analog_sigtype sigtype;
70  const char *name;
71 } sigtypes[] = {
72  { ANALOG_SIG_FXOLS, "fxo_ls" },
73  { ANALOG_SIG_FXOKS, "fxo_ks" },
74  { ANALOG_SIG_FXOGS, "fxo_gs" },
75  { ANALOG_SIG_FXSLS, "fxs_ls" },
76  { ANALOG_SIG_FXSKS, "fxs_ks" },
77  { ANALOG_SIG_FXSGS, "fxs_gs" },
78  { ANALOG_SIG_EMWINK, "em_w" },
79  { ANALOG_SIG_EM, "em" },
80  { ANALOG_SIG_EM_E1, "em_e1" },
81  { ANALOG_SIG_FEATD, "featd" },
82  { ANALOG_SIG_FEATDMF, "featdmf" },
83  { ANALOG_SIG_FEATDMF_TA, "featdmf_ta" },
84  { ANALOG_SIG_FEATB, "featb" },
85  { ANALOG_SIG_FGC_CAMA, "fgccama" },
86  { ANALOG_SIG_FGC_CAMAMF, "fgccamamf" },
87  { ANALOG_SIG_SF, "sf" },
88  { ANALOG_SIG_SFWINK, "sf_w" },
89  { ANALOG_SIG_SF_FEATD, "sf_featd" },
90  { ANALOG_SIG_SF_FEATDMF, "sf_featdmf" },
91  { ANALOG_SIG_SF_FEATB, "sf_featb" },
92  { ANALOG_SIG_E911, "e911" },
93 };
94 
95 static const struct {
96  unsigned int cid_type;
97  const char *name;
98 } cidtypes[] = {
99  { CID_SIG_BELL, "bell" },
100  { CID_SIG_V23, "v23" },
101  { CID_SIG_V23_JP, "v23_jp" },
102  { CID_SIG_DTMF, "dtmf" },
103  /* "smdi" is intentionally not supported here, as there is a much better
104  * way to do this in the dialplan now. */
105 };
106 
107 #define ISTRUNK(p) ((p->sig == ANALOG_SIG_FXSLS) || (p->sig == ANALOG_SIG_FXSKS) || \
108  (p->sig == ANALOG_SIG_FXSGS))
109 
110 enum analog_sigtype analog_str_to_sigtype(const char *name)
111 {
112  int i;
113 
114  for (i = 0; i < ARRAY_LEN(sigtypes); i++) {
115  if (!strcasecmp(sigtypes[i].name, name)) {
116  return sigtypes[i].sigtype;
117  }
118  }
119 
120  return 0;
121 }
122 
123 const char *analog_sigtype_to_str(enum analog_sigtype sigtype)
124 {
125  int i;
126 
127  for (i = 0; i < ARRAY_LEN(sigtypes); i++) {
128  if (sigtype == sigtypes[i].sigtype) {
129  return sigtypes[i].name;
130  }
131  }
132 
133  return "Unknown";
134 }
135 
136 unsigned int analog_str_to_cidtype(const char *name)
137 {
138  int i;
139 
140  for (i = 0; i < ARRAY_LEN(cidtypes); i++) {
141  if (!strcasecmp(cidtypes[i].name, name)) {
142  return cidtypes[i].cid_type;
143  }
144  }
145 
146  return 0;
147 }
148 
149 const char *analog_cidtype_to_str(unsigned int cid_type)
150 {
151  int i;
152 
153  for (i = 0; i < ARRAY_LEN(cidtypes); i++) {
154  if (cid_type == cidtypes[i].cid_type) {
155  return cidtypes[i].name;
156  }
157  }
158 
159  return "Unknown";
160 }
161 
162 static int analog_start_cid_detect(struct analog_pvt *p, int cid_signalling)
163 {
164  if (analog_callbacks.start_cid_detect) {
165  return analog_callbacks.start_cid_detect(p->chan_pvt, cid_signalling);
166  }
167  return -1;
168 }
169 
170 static int analog_stop_cid_detect(struct analog_pvt *p)
171 {
172  if (analog_callbacks.stop_cid_detect) {
173  return analog_callbacks.stop_cid_detect(p->chan_pvt);
174  }
175  return -1;
176 }
177 
178 static int analog_get_callerid(struct analog_pvt *p, char *name, char *number, enum analog_event *ev, size_t timeout)
179 {
180  if (analog_callbacks.get_callerid) {
181  return analog_callbacks.get_callerid(p->chan_pvt, name, number, ev, timeout);
182  }
183  return -1;
184 }
185 
186 static const char *analog_get_orig_dialstring(struct analog_pvt *p)
187 {
188  if (analog_callbacks.get_orig_dialstring) {
189  return analog_callbacks.get_orig_dialstring(p->chan_pvt);
190  }
191  return "";
192 }
193 
194 static int analog_get_event(struct analog_pvt *p)
195 {
196  if (analog_callbacks.get_event) {
197  return analog_callbacks.get_event(p->chan_pvt);
198  }
199  return -1;
200 }
201 
202 static int analog_wait_event(struct analog_pvt *p)
203 {
204  if (analog_callbacks.wait_event) {
205  return analog_callbacks.wait_event(p->chan_pvt);
206  }
207  return -1;
208 }
209 
210 static int analog_have_progressdetect(struct analog_pvt *p)
211 {
212  if (analog_callbacks.have_progressdetect) {
213  return analog_callbacks.have_progressdetect(p->chan_pvt);
214  }
215  /* Don't have progress detection. */
216  return 0;
217 }
218 
219 #define gen_analog_field_callback(type, callback_name, def_value) \
220  static type analog_get_##callback_name(struct analog_pvt *p) \
221  { \
222  if (!analog_callbacks.get_##callback_name) { \
223  return def_value; \
224  } \
225  return analog_callbacks.get_##callback_name(p->chan_pvt); \
226  }
227 
228 gen_analog_field_callback(int, firstdigit_timeout, ANALOG_FIRST_DIGIT_TIMEOUT);
229 gen_analog_field_callback(int, interdigit_timeout, ANALOG_INTER_DIGIT_TIMEOUT);
230 gen_analog_field_callback(int, matchdigit_timeout, ANALOG_MATCH_DIGIT_TIMEOUT);
231 
232 #undef gen_analog_field_callback
233 
234 enum analog_cid_start analog_str_to_cidstart(const char *value)
235 {
236  if (!strcasecmp(value, "ring")) {
237  return ANALOG_CID_START_RING;
238  } else if (!strcasecmp(value, "polarity")) {
239  return ANALOG_CID_START_POLARITY;
240  } else if (!strcasecmp(value, "polarity_in")) {
241  return ANALOG_CID_START_POLARITY_IN;
242  } else if (!strcasecmp(value, "dtmf")) {
243  return ANALOG_CID_START_DTMF_NOALERT;
244  }
245 
246  return 0;
247 }
248 
249 const char *analog_cidstart_to_str(enum analog_cid_start cid_start)
250 {
251  switch (cid_start) {
252  case ANALOG_CID_START_RING:
253  return "Ring";
254  case ANALOG_CID_START_POLARITY:
255  return "Polarity";
256  case ANALOG_CID_START_POLARITY_IN:
257  return "Polarity_In";
258  case ANALOG_CID_START_DTMF_NOALERT:
259  return "DTMF";
260  }
261 
262  return "Unknown";
263 }
264 
265 static char *analog_event2str(enum analog_event event)
266 {
267  char *res;
268  switch (event) {
269  case ANALOG_EVENT_ONHOOK:
270  res = "ANALOG_EVENT_ONHOOK";
271  break;
272  case ANALOG_EVENT_RINGOFFHOOK:
273  res = "ANALOG_EVENT_RINGOFFHOOK";
274  break;
275  case ANALOG_EVENT_WINKFLASH:
276  res = "ANALOG_EVENT_WINKFLASH";
277  break;
278  case ANALOG_EVENT_ALARM:
279  res = "ANALOG_EVENT_ALARM";
280  break;
281  case ANALOG_EVENT_NOALARM:
282  res = "ANALOG_EVENT_NOALARM";
283  break;
284  case ANALOG_EVENT_DIALCOMPLETE:
285  res = "ANALOG_EVENT_DIALCOMPLETE";
286  break;
287  case ANALOG_EVENT_HOOKCOMPLETE:
288  res = "ANALOG_EVENT_HOOKCOMPLETE";
289  break;
290  case ANALOG_EVENT_PULSE_START:
291  res = "ANALOG_EVENT_PULSE_START";
292  break;
293  case ANALOG_EVENT_POLARITY:
294  res = "ANALOG_EVENT_POLARITY";
295  break;
296  case ANALOG_EVENT_RINGBEGIN:
297  res = "ANALOG_EVENT_RINGBEGIN";
298  break;
299  case ANALOG_EVENT_EC_DISABLED:
300  res = "ANALOG_EVENT_EC_DISABLED";
301  break;
302  case ANALOG_EVENT_RINGERON:
303  res = "ANALOG_EVENT_RINGERON";
304  break;
305  case ANALOG_EVENT_RINGEROFF:
306  res = "ANALOG_EVENT_RINGEROFF";
307  break;
308  case ANALOG_EVENT_REMOVED:
309  res = "ANALOG_EVENT_REMOVED";
310  break;
311  case ANALOG_EVENT_NEONMWI_ACTIVE:
312  res = "ANALOG_EVENT_NEONMWI_ACTIVE";
313  break;
314  case ANALOG_EVENT_NEONMWI_INACTIVE:
315  res = "ANALOG_EVENT_NEONMWI_INACTIVE";
316  break;
317 #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
318  case ANALOG_EVENT_TX_CED_DETECTED:
319  res = "ANALOG_EVENT_TX_CED_DETECTED";
320  break;
321  case ANALOG_EVENT_RX_CED_DETECTED:
322  res = "ANALOG_EVENT_RX_CED_DETECTED";
323  break;
324  case ANALOG_EVENT_EC_NLP_DISABLED:
325  res = "ANALOG_EVENT_EC_NLP_DISABLED";
326  break;
327  case ANALOG_EVENT_EC_NLP_ENABLED:
328  res = "ANALOG_EVENT_EC_NLP_ENABLED";
329  break;
330 #endif
331  case ANALOG_EVENT_PULSEDIGIT:
332  res = "ANALOG_EVENT_PULSEDIGIT";
333  break;
334  case ANALOG_EVENT_DTMFDOWN:
335  res = "ANALOG_EVENT_DTMFDOWN";
336  break;
337  case ANALOG_EVENT_DTMFUP:
338  res = "ANALOG_EVENT_DTMFUP";
339  break;
340  default:
341  res = "UNKNOWN/OTHER";
342  break;
343  }
344 
345  return res;
346 }
347 
348 static void analog_swap_subs(struct analog_pvt *p, enum analog_sub a, enum analog_sub b)
349 {
350  int tinthreeway;
351  struct ast_channel *towner;
352 
353  ast_debug(1, "Swapping %u and %u\n", a, b);
354 
355  towner = p->subs[a].owner;
356  p->subs[a].owner = p->subs[b].owner;
357  p->subs[b].owner = towner;
358 
359  tinthreeway = p->subs[a].inthreeway;
360  p->subs[a].inthreeway = p->subs[b].inthreeway;
361  p->subs[b].inthreeway = tinthreeway;
362 
364  analog_callbacks.swap_subs(p->chan_pvt, a, p->subs[a].owner, b, p->subs[b].owner);
365  }
366 }
367 
368 static int analog_alloc_sub(struct analog_pvt *p, enum analog_sub x)
369 {
370  if (analog_callbacks.allocate_sub) {
371  int res;
372  res = analog_callbacks.allocate_sub(p->chan_pvt, x);
373  if (!res) {
374  p->subs[x].allocd = 1;
375  }
376  return res;
377  }
378  return 0;
379 }
380 
381 static int analog_unalloc_sub(struct analog_pvt *p, enum analog_sub x)
382 {
383  p->subs[x].allocd = 0;
384  p->subs[x].owner = NULL;
385  if (analog_callbacks.unallocate_sub) {
386  return analog_callbacks.unallocate_sub(p->chan_pvt, x);
387  }
388  return 0;
389 }
390 
391 static int analog_send_callerid(struct analog_pvt *p, int cwcid, struct ast_party_caller *caller)
392 {
393  ast_debug(1, "Sending callerid. CID_NAME: '%s' CID_NUM: '%s'\n",
394  caller->id.name.str,
395  caller->id.number.str);
396 
397  if (cwcid) {
398  p->callwaitcas = 0;
399  }
400 
401  if (analog_callbacks.send_callerid) {
402  return analog_callbacks.send_callerid(p->chan_pvt, cwcid, caller);
403  }
404  return 0;
405 }
406 
407 #define analog_get_index(ast, p, nullok) _analog_get_index(ast, p, nullok, __PRETTY_FUNCTION__, __LINE__)
408 static int _analog_get_index(struct ast_channel *ast, struct analog_pvt *p, int nullok, const char *fname, unsigned long line)
409 {
410  int res;
411  if (p->subs[ANALOG_SUB_REAL].owner == ast) {
412  res = ANALOG_SUB_REAL;
413  } else if (p->subs[ANALOG_SUB_CALLWAIT].owner == ast) {
414  res = ANALOG_SUB_CALLWAIT;
415  } else if (p->subs[ANALOG_SUB_THREEWAY].owner == ast) {
416  res = ANALOG_SUB_THREEWAY;
417  } else {
418  res = -1;
419  if (!nullok) {
420  ast_log(LOG_WARNING,
421  "Unable to get index for '%s' on channel %d (%s(), line %lu)\n",
422  ast ? ast_channel_name(ast) : "", p->channel, fname, line);
423  }
424  }
425  return res;
426 }
427 
428 static int analog_dsp_reset_and_flush_digits(struct analog_pvt *p)
429 {
430  if (analog_callbacks.dsp_reset_and_flush_digits) {
431  return analog_callbacks.dsp_reset_and_flush_digits(p->chan_pvt);
432  }
433 
434  /* Return 0 since I think this is unnecessary to do in most cases it is used. Mostly only for ast_dsp */
435  return 0;
436 }
437 
438 static int analog_play_tone(struct analog_pvt *p, enum analog_sub sub, enum analog_tone tone)
439 {
440  if (analog_callbacks.play_tone) {
441  return analog_callbacks.play_tone(p->chan_pvt, sub, tone);
442  }
443  return -1;
444 }
445 
446 static void analog_set_new_owner(struct analog_pvt *p, struct ast_channel *new_owner)
447 {
448  p->owner = new_owner;
449  if (analog_callbacks.set_new_owner) {
450  analog_callbacks.set_new_owner(p->chan_pvt, new_owner);
451  }
452 }
453 
454 static struct ast_channel * analog_new_ast_channel(struct analog_pvt *p, int state, int startpbx, enum analog_sub sub, const struct ast_channel *requestor)
455 {
456  struct ast_channel *c;
457 
458  if (!analog_callbacks.new_ast_channel) {
459  return NULL;
460  }
461 
462  c = analog_callbacks.new_ast_channel(p->chan_pvt, state, startpbx, sub, requestor);
463  if (c) {
464  ast_channel_call_forward_set(c, p->call_forward);
465  }
466  p->subs[sub].owner = c;
467  if (!p->owner) {
468  analog_set_new_owner(p, c);
469  }
470  return c;
471 }
472 
473 static int analog_set_echocanceller(struct analog_pvt *p, int enable)
474 {
475  if (analog_callbacks.set_echocanceller) {
476  return analog_callbacks.set_echocanceller(p->chan_pvt, enable);
477  }
478  return -1;
479 }
480 
481 static int analog_train_echocanceller(struct analog_pvt *p)
482 {
483  if (analog_callbacks.train_echocanceller) {
484  return analog_callbacks.train_echocanceller(p->chan_pvt);
485  }
486  return -1;
487 }
488 
489 static int analog_is_off_hook(struct analog_pvt *p)
490 {
491  if (analog_callbacks.is_off_hook) {
492  return analog_callbacks.is_off_hook(p->chan_pvt);
493  }
494  return -1;
495 }
496 
497 static int analog_ring(struct analog_pvt *p)
498 {
499  if (analog_callbacks.ring) {
500  return analog_callbacks.ring(p->chan_pvt);
501  }
502  return -1;
503 }
504 
505 static int analog_flash(struct analog_pvt *p)
506 {
507  if (analog_callbacks.flash) {
508  return analog_callbacks.flash(p->chan_pvt);
509  }
510  return -1;
511 }
512 
513 static int analog_start(struct analog_pvt *p)
514 {
515  if (analog_callbacks.start) {
516  return analog_callbacks.start(p->chan_pvt);
517  }
518  return -1;
519 }
520 
521 static int analog_dial_digits(struct analog_pvt *p, enum analog_sub sub, struct analog_dialoperation *dop)
522 {
523  if (analog_callbacks.dial_digits) {
524  return analog_callbacks.dial_digits(p->chan_pvt, sub, dop);
525  }
526  return -1;
527 }
528 
529 static int analog_on_hook(struct analog_pvt *p)
530 {
532  return analog_callbacks.on_hook(p->chan_pvt);
533  }
534  return -1;
535 }
536 
537 static void analog_set_outgoing(struct analog_pvt *p, int is_outgoing)
538 {
539  p->outgoing = is_outgoing;
540  if (analog_callbacks.set_outgoing) {
541  analog_callbacks.set_outgoing(p->chan_pvt, is_outgoing);
542  }
543 }
544 
545 static int analog_check_for_conference(struct analog_pvt *p)
546 {
547  if (analog_callbacks.check_for_conference) {
548  return analog_callbacks.check_for_conference(p->chan_pvt);
549  }
550  return -1;
551 }
552 
553 static void analog_all_subchannels_hungup(struct analog_pvt *p)
554 {
555  if (analog_callbacks.all_subchannels_hungup) {
556  analog_callbacks.all_subchannels_hungup(p->chan_pvt);
557  }
558 }
559 
560 static void analog_unlock_private(struct analog_pvt *p)
561 {
562  if (analog_callbacks.unlock_private) {
563  analog_callbacks.unlock_private(p->chan_pvt);
564  }
565 }
566 
567 static void analog_lock_private(struct analog_pvt *p)
568 {
569  if (analog_callbacks.lock_private) {
570  analog_callbacks.lock_private(p->chan_pvt);
571  }
572 }
573 
574 static void analog_deadlock_avoidance_private(struct analog_pvt *p)
575 {
576  if (analog_callbacks.deadlock_avoidance_private) {
577  analog_callbacks.deadlock_avoidance_private(p->chan_pvt);
578  } else {
579  /* Fallback to manual avoidance if callback not present. */
580  analog_unlock_private(p);
581  usleep(1);
582  analog_lock_private(p);
583  }
584 }
585 
586 /*!
587  * \internal
588  * \brief Obtain the specified subchannel owner lock if the owner exists.
589  *
590  * \param pvt Analog private struct.
591  * \param sub_idx Subchannel owner to lock.
592  *
593  * \note Assumes the analog_lock_private(pvt->chan_pvt) is already obtained.
594  *
595  * \note
596  * Because deadlock avoidance may have been necessary, you need to confirm
597  * the state of things before continuing.
598  */
599 static void analog_lock_sub_owner(struct analog_pvt *pvt, enum analog_sub sub_idx)
600 {
601  for (;;) {
602  if (!pvt->subs[sub_idx].owner) {
603  /* No subchannel owner pointer */
604  break;
605  }
606  if (!ast_channel_trylock(pvt->subs[sub_idx].owner)) {
607  /* Got subchannel owner lock */
608  break;
609  }
610  /* We must unlock the private to avoid the possibility of a deadlock */
611  analog_deadlock_avoidance_private(pvt);
612  }
613 }
614 
615 static int analog_off_hook(struct analog_pvt *p)
616 {
618  return analog_callbacks.off_hook(p->chan_pvt);
619  }
620  return -1;
621 }
622 
623 static void analog_set_needringing(struct analog_pvt *p, int value)
624 {
625  if (analog_callbacks.set_needringing) {
626  analog_callbacks.set_needringing(p->chan_pvt, value);
627  }
628 }
629 
630 #if 0
631 static void analog_set_polarity(struct analog_pvt *p, int value)
632 {
634  analog_callbacks.set_polarity(p->chan_pvt, value);
635  }
636 }
637 #endif
638 
639 static void analog_start_polarityswitch(struct analog_pvt *p)
640 {
643  }
644 }
645 static void analog_answer_polarityswitch(struct analog_pvt *p)
646 {
649  }
650 }
651 
652 static void analog_hangup_polarityswitch(struct analog_pvt *p)
653 {
656  }
657 }
658 
659 static int analog_dsp_set_digitmode(struct analog_pvt *p, enum analog_dsp_digitmode mode)
660 {
661  if (analog_callbacks.dsp_set_digitmode) {
662  return analog_callbacks.dsp_set_digitmode(p->chan_pvt, mode);
663  }
664  return -1;
665 }
666 
667 static void analog_cb_handle_dtmf(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub analog_index, struct ast_frame **dest)
668 {
669  if (analog_callbacks.handle_dtmf) {
670  analog_callbacks.handle_dtmf(p->chan_pvt, ast, analog_index, dest);
671  }
672 }
673 
674 static int analog_wink(struct analog_pvt *p, enum analog_sub index)
675 {
676  if (analog_callbacks.wink) {
677  return analog_callbacks.wink(p->chan_pvt, index);
678  }
679  return -1;
680 }
681 
682 static int analog_has_voicemail(struct analog_pvt *p)
683 {
684  if (analog_callbacks.has_voicemail) {
685  return analog_callbacks.has_voicemail(p->chan_pvt);
686  }
687  return -1;
688 }
689 
690 static int analog_is_dialing(struct analog_pvt *p, enum analog_sub index)
691 {
692  if (analog_callbacks.is_dialing) {
693  return analog_callbacks.is_dialing(p->chan_pvt, index);
694  }
695  return -1;
696 }
697 
698 /*!
699  * \internal
700  * \brief Attempt to transfer 3-way call.
701  *
702  * \param p Analog private structure.
703  *
704  * \note On entry these locks are held: real-call, private, 3-way call.
705  * \note On exit these locks are held: real-call, private.
706  *
707  * \retval 0 on success.
708  * \retval -1 on error.
709  */
710 static int analog_attempt_transfer(struct analog_pvt *p)
711 {
712  struct ast_channel *owner_real;
713  struct ast_channel *owner_3way;
714  enum ast_transfer_result xfer_res;
715  int res = 0;
716 
717  owner_real = ast_channel_ref(p->subs[ANALOG_SUB_REAL].owner);
718  owner_3way = ast_channel_ref(p->subs[ANALOG_SUB_THREEWAY].owner);
719 
720  ast_verb(3, "TRANSFERRING %s to %s\n",
721  ast_channel_name(owner_3way), ast_channel_name(owner_real));
722 
723  ast_channel_unlock(owner_real);
724  ast_channel_unlock(owner_3way);
725  analog_unlock_private(p);
726 
727  xfer_res = ast_bridge_transfer_attended(owner_3way, owner_real);
728  if (xfer_res != AST_BRIDGE_TRANSFER_SUCCESS) {
729  ast_softhangup(owner_3way, AST_SOFTHANGUP_DEV);
730  res = -1;
731  }
732 
733  /* Must leave with these locked. */
734  ast_channel_lock(owner_real);
735  analog_lock_private(p);
736 
737  ast_channel_unref(owner_real);
738  ast_channel_unref(owner_3way);
739 
740  return res;
741 }
742 
743 static int analog_update_conf(struct analog_pvt *p)
744 {
745  int x;
746  int needconf = 0;
747 
748  /* Start with the obvious, general stuff */
749  for (x = 0; x < 3; x++) {
750  /* Look for three way calls */
751  if ((p->subs[x].allocd) && p->subs[x].inthreeway) {
752  if (analog_callbacks.conf_add) {
753  analog_callbacks.conf_add(p->chan_pvt, x);
754  }
755  needconf++;
756  } else {
757  if (analog_callbacks.conf_del) {
758  analog_callbacks.conf_del(p->chan_pvt, x);
759  }
760  }
761  }
762  ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
763 
764  if (analog_callbacks.complete_conference_update) {
765  analog_callbacks.complete_conference_update(p->chan_pvt, needconf);
766  }
767  return 0;
768 }
769 
770 struct ast_channel * analog_request(struct analog_pvt *p, int *callwait, const struct ast_channel *requestor)
771 {
772  struct ast_channel *ast;
773 
774  ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
775  *callwait = (p->owner != NULL);
776 
777  if (p->owner) {
778  if (analog_alloc_sub(p, ANALOG_SUB_CALLWAIT)) {
779  ast_log(LOG_ERROR, "Unable to alloc subchannel\n");
780  return NULL;
781  }
782  }
783 
784  analog_set_outgoing(p, 1);
785  ast = analog_new_ast_channel(p, AST_STATE_RESERVED, 0,
786  p->owner ? ANALOG_SUB_CALLWAIT : ANALOG_SUB_REAL, requestor);
787  if (!ast) {
788  analog_set_outgoing(p, 0);
789  }
790  return ast;
791 }
792 
793 int analog_available(struct analog_pvt *p)
794 {
795  int offhook;
796 
797  ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
798 
799  /* If do not disturb, definitely not */
800  if (p->dnd) {
801  return 0;
802  }
803  /* If guard time, definitely not */
804  if (p->guardtime && (time(NULL) < p->guardtime)) {
805  return 0;
806  }
807 
808  /* If line is being held, definitely not (don't allow call waitings to an on-hook phone) */
809  if (p->cshactive) {
810  return 0;
811  }
812 
813  /* If no owner definitely available */
814  if (!p->owner) {
815  offhook = analog_is_off_hook(p);
816 
817  /* TDM FXO card, "onhook" means out of service (no battery on the line) */
818  if ((p->sig == ANALOG_SIG_FXSLS) || (p->sig == ANALOG_SIG_FXSKS) || (p->sig == ANALOG_SIG_FXSGS)) {
819 #ifdef DAHDI_CHECK_HOOKSTATE
820  if (offhook) {
821  return 1;
822  }
823  return 0;
824 #endif
825  /* TDM FXS card, "offhook" means someone took the hook off so it's unavailable! */
826  } else if (offhook) {
827  ast_debug(1, "Channel %d off hook, can't use\n", p->channel);
828  /* Not available when the other end is off hook */
829  return 0;
830  }
831  return 1;
832  }
833 
834  /* If it's not an FXO, forget about call wait */
835  if ((p->sig != ANALOG_SIG_FXOKS) && (p->sig != ANALOG_SIG_FXOLS) && (p->sig != ANALOG_SIG_FXOGS)) {
836  return 0;
837  }
838 
839  if (!p->callwaiting) {
840  /* If they don't have call waiting enabled, then for sure they're unavailable at this point */
841  return 0;
842  }
843 
844  if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
845  /* If there is already a call waiting call, then we can't take a second one */
846  return 0;
847  }
848 
849  if ((ast_channel_state(p->owner) != AST_STATE_UP) &&
850  ((ast_channel_state(p->owner) != AST_STATE_RINGING) || p->outgoing)) {
851  /* If the current call is not up, then don't allow the call */
852  return 0;
853  }
854  if ((p->subs[ANALOG_SUB_THREEWAY].owner) && (!p->subs[ANALOG_SUB_THREEWAY].inthreeway)) {
855  /* Can't take a call wait when the three way calling hasn't been merged yet. */
856  return 0;
857  }
858  /* We're cool */
859  return 1;
860 }
861 
862 static int analog_stop_callwait(struct analog_pvt *p)
863 {
864  p->callwaitcas = 0;
865  if (analog_callbacks.stop_callwait) {
866  return analog_callbacks.stop_callwait(p->chan_pvt);
867  }
868  return 0;
869 }
870 
871 static int analog_callwait(struct analog_pvt *p)
872 {
874  if (analog_callbacks.callwait) {
875  return analog_callbacks.callwait(p->chan_pvt);
876  }
877  return 0;
878 }
879 
880 static void analog_set_callwaiting(struct analog_pvt *p, int callwaiting_enable)
881 {
882  p->callwaiting = callwaiting_enable;
883  if (analog_callbacks.set_callwaiting) {
884  analog_callbacks.set_callwaiting(p->chan_pvt, callwaiting_enable);
885  }
886 }
887 
888 static void analog_set_cadence(struct analog_pvt *p, struct ast_channel *chan)
889 {
890  if (analog_callbacks.set_cadence) {
891  analog_callbacks.set_cadence(p->chan_pvt, &p->cidrings, chan);
892  }
893 }
894 
895 static void analog_set_dialing(struct analog_pvt *p, int is_dialing)
896 {
897  p->dialing = is_dialing;
898  if (analog_callbacks.set_dialing) {
899  analog_callbacks.set_dialing(p->chan_pvt, is_dialing);
900  }
901 }
902 
903 static void analog_set_alarm(struct analog_pvt *p, int in_alarm)
904 {
905  p->inalarm = in_alarm;
906  if (analog_callbacks.set_alarm) {
907  analog_callbacks.set_alarm(p->chan_pvt, in_alarm);
908  }
909 }
910 
911 static void analog_set_ringtimeout(struct analog_pvt *p, int ringt)
912 {
913  p->ringt = ringt;
914  if (analog_callbacks.set_ringtimeout) {
915  analog_callbacks.set_ringtimeout(p->chan_pvt, ringt);
916  }
917 }
918 
919 static void analog_set_waitingfordt(struct analog_pvt *p, struct ast_channel *ast)
920 {
921  if (analog_callbacks.set_waitingfordt) {
922  analog_callbacks.set_waitingfordt(p->chan_pvt, ast);
923  }
924 }
925 
926 static int analog_check_waitingfordt(struct analog_pvt *p)
927 {
928  if (analog_callbacks.check_waitingfordt) {
929  return analog_callbacks.check_waitingfordt(p->chan_pvt);
930  }
931 
932  return 0;
933 }
934 
935 static void analog_set_confirmanswer(struct analog_pvt *p, int flag)
936 {
937  if (analog_callbacks.set_confirmanswer) {
938  analog_callbacks.set_confirmanswer(p->chan_pvt, flag);
939  }
940 }
941 
942 static int analog_check_confirmanswer(struct analog_pvt *p)
943 {
944  if (analog_callbacks.check_confirmanswer) {
945  return analog_callbacks.check_confirmanswer(p->chan_pvt);
946  }
947 
948  return 0;
949 }
950 
951 static void analog_cancel_cidspill(struct analog_pvt *p)
952 {
953  if (analog_callbacks.cancel_cidspill) {
954  analog_callbacks.cancel_cidspill(p->chan_pvt);
955  }
956 }
957 
958 static int analog_confmute(struct analog_pvt *p, int mute)
959 {
960  if (analog_callbacks.confmute) {
961  return analog_callbacks.confmute(p->chan_pvt, mute);
962  }
963  return 0;
964 }
965 
966 static void analog_set_pulsedial(struct analog_pvt *p, int flag)
967 {
968  if (analog_callbacks.set_pulsedial) {
969  analog_callbacks.set_pulsedial(p->chan_pvt, flag);
970  }
971 }
972 
973 static int analog_set_linear_mode(struct analog_pvt *p, enum analog_sub sub, int linear_mode)
974 {
975  if (analog_callbacks.set_linear_mode) {
976  /* Return provides old linear_mode setting or error indication */
977  return analog_callbacks.set_linear_mode(p->chan_pvt, sub, linear_mode);
978  }
979  return -1;
980 }
981 
982 static void analog_set_inthreeway(struct analog_pvt *p, enum analog_sub sub, int inthreeway)
983 {
984  p->subs[sub].inthreeway = inthreeway;
985  if (analog_callbacks.set_inthreeway) {
986  analog_callbacks.set_inthreeway(p->chan_pvt, sub, inthreeway);
987  }
988 }
989 
990 int analog_call(struct analog_pvt *p, struct ast_channel *ast, const char *rdest, int timeout)
991 {
992  int res, idx, mysig;
993  char *c, *n, *l;
994  char dest[256]; /* must be same length as p->dialdest */
995 
996  ast_debug(1, "CALLING CID_NAME: %s CID_NUM:: %s\n",
997  S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, ""),
998  S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, ""));
999 
1000  ast_copy_string(dest, rdest, sizeof(dest));
1001  ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1002 
1003  if ((ast_channel_state(ast) == AST_STATE_BUSY)) {
1005  return 0;
1006  }
1007 
1009  ast_log(LOG_WARNING, "analog_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
1010  return -1;
1011  }
1012 
1013  p->dialednone = 0;
1014  analog_set_outgoing(p, 1);
1015 
1016  mysig = p->sig;
1017  if (p->outsigmod > -1) {
1018  mysig = p->outsigmod;
1019  }
1020 
1021  switch (mysig) {
1022  case ANALOG_SIG_FXOLS:
1023  case ANALOG_SIG_FXOGS:
1024  case ANALOG_SIG_FXOKS:
1025  if (p->owner == ast) {
1026  /* Normal ring, on hook */
1027 
1028  /* Don't send audio while on hook, until the call is answered */
1029  analog_set_dialing(p, 1);
1030  analog_set_cadence(p, ast); /* and set p->cidrings */
1031 
1032  /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1033  c = strchr(dest, '/');
1034  if (c) {
1035  c++;
1036  }
1037  if (c && (strlen(c) < p->stripmsd)) {
1038  ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1039  c = NULL;
1040  }
1041  if (c && (strlen(c) > sizeof(p->dop.dialstr) - 3 /* "Tw\0" */)) {
1042  ast_log(LOG_WARNING, "Number '%s' is longer than %d bytes\n", c, (int)sizeof(p->dop.dialstr) - 2);
1043  c = NULL;
1044  }
1045  if (c) {
1046  p->dop.op = ANALOG_DIAL_OP_REPLACE;
1047  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1048  ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
1049  } else {
1050  p->dop.dialstr[0] = '\0';
1051  }
1052 
1053  if (analog_ring(p)) {
1054  ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1055  return -1;
1056  }
1057  analog_set_dialing(p, 1);
1058  } else {
1059  /* Call waiting call */
1060  if (ast_channel_connected(ast)->id.number.valid && ast_channel_connected(ast)->id.number.str) {
1061  ast_copy_string(p->callwait_num, ast_channel_connected(ast)->id.number.str, sizeof(p->callwait_num));
1062  } else {
1063  p->callwait_num[0] = '\0';
1064  }
1065  if (ast_channel_connected(ast)->id.name.valid && ast_channel_connected(ast)->id.name.str) {
1066  ast_copy_string(p->callwait_name, ast_channel_connected(ast)->id.name.str, sizeof(p->callwait_name));
1067  } else {
1068  p->callwait_name[0] = '\0';
1069  }
1070 
1071  /* Call waiting tone instead */
1072  if (analog_callwait(p)) {
1073  return -1;
1074  }
1075  /* Make ring-back */
1076  if (analog_play_tone(p, ANALOG_SUB_CALLWAIT, ANALOG_TONE_RINGTONE)) {
1077  ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast_channel_name(ast));
1078  }
1079 
1080  }
1081 
1082  /* Name and Number */
1083  n = ast_channel_connected(ast)->id.name.valid ? ast_channel_connected(ast)->id.name.str : NULL;
1084  l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
1085  if (l) {
1086  ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
1087  } else {
1088  p->lastcid_num[0] = '\0';
1089  }
1090  if (n) {
1091  ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
1092  } else {
1093  p->lastcid_name[0] = '\0';
1094  }
1095 
1096  if (p->use_callerid) {
1097  const char *qual_var;
1098 
1099  /* Caller ID Name and Number */
1100  p->caller.id.name.str = p->lastcid_name;
1101  p->caller.id.number.str = p->lastcid_num;
1102  p->caller.id.name.valid = ast_channel_connected(ast)->id.name.valid;
1103  p->caller.id.number.valid = ast_channel_connected(ast)->id.number.valid;
1104  p->caller.id.name.presentation = ast_channel_connected(ast)->id.name.presentation;
1105  p->caller.id.number.presentation = ast_channel_connected(ast)->id.number.presentation;
1106 
1107  /* Redirecting Reason */
1108  p->redirecting_reason = ast_channel_redirecting(ast)->from.number.valid ? ast_channel_redirecting(ast)->reason.code : -1;
1109 
1110  /* Call Qualifier */
1111  ast_channel_lock(ast);
1112  /* XXX In the future, we may want to make this a CALLERID or CHANNEL property and fetch it from there. */
1113  qual_var = pbx_builtin_getvar_helper(ast, "CALL_QUALIFIER");
1114  p->call_qualifier = ast_true(qual_var) ? 1 : 0;
1115  ast_channel_unlock(ast);
1116  }
1117 
1119  idx = analog_get_index(ast, p, 0);
1120  if (idx > -1) {
1121  struct ast_cc_config_params *cc_params;
1122 
1123  /* This is where the initial ringing frame is queued for an analog call.
1124  * As such, this is a great time to offer CCNR to the caller if it's available.
1125  */
1126  cc_params = ast_channel_get_cc_config_params(p->subs[idx].owner);
1127  if (cc_params) {
1128  switch (ast_get_cc_monitor_policy(cc_params)) {
1129  case AST_CC_MONITOR_NEVER:
1130  break;
1131  case AST_CC_MONITOR_NATIVE:
1132  case AST_CC_MONITOR_ALWAYS:
1135  analog_get_orig_dialstring(p), AST_CC_CCNR, NULL);
1136  break;
1137  }
1138  }
1139  ast_queue_control(p->subs[idx].owner, AST_CONTROL_RINGING);
1140  }
1141  break;
1142  case ANALOG_SIG_FXSLS:
1143  case ANALOG_SIG_FXSGS:
1144  case ANALOG_SIG_FXSKS:
1145  if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
1146  ast_debug(1, "Ignore possible polarity reversal on line seizure\n");
1147  p->polaritydelaytv = ast_tvnow();
1148  }
1149  /* fall through */
1150  case ANALOG_SIG_EMWINK:
1151  case ANALOG_SIG_EM:
1152  case ANALOG_SIG_EM_E1:
1153  case ANALOG_SIG_FEATD:
1154  case ANALOG_SIG_FEATDMF:
1155  case ANALOG_SIG_E911:
1156  case ANALOG_SIG_FGC_CAMA:
1157  case ANALOG_SIG_FGC_CAMAMF:
1158  case ANALOG_SIG_FEATB:
1159  case ANALOG_SIG_SFWINK:
1160  case ANALOG_SIG_SF:
1161  case ANALOG_SIG_SF_FEATD:
1162  case ANALOG_SIG_SF_FEATDMF:
1163  case ANALOG_SIG_FEATDMF_TA:
1164  case ANALOG_SIG_SF_FEATB:
1165  c = strchr(dest, '/');
1166  if (c) {
1167  c++;
1168  } else {
1169  c = "";
1170  }
1171  if (strlen(c) < p->stripmsd) {
1172  ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1173  return -1;
1174  }
1175  res = analog_start(p);
1176  if (res < 0) {
1177  if (errno != EINPROGRESS) {
1178  return -1;
1179  }
1180  }
1181  ast_debug(1, "Dialing '%s'\n", c);
1182  p->dop.op = ANALOG_DIAL_OP_REPLACE;
1183 
1184  c += p->stripmsd;
1185 
1186  switch (mysig) {
1187  case ANALOG_SIG_FEATD:
1188  l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
1189  if (l) {
1190  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
1191  } else {
1192  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
1193  }
1194  break;
1195  case ANALOG_SIG_FEATDMF:
1196  l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
1197  if (l) {
1198  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
1199  } else {
1200  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
1201  }
1202  break;
1203  case ANALOG_SIG_FEATDMF_TA:
1204  {
1205  const char *cic = "", *ozz = "";
1206 
1207  /* If you have to go through a Tandem Access point you need to use this */
1208 #ifndef STANDALONE
1209  ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
1210  if (!ozz) {
1211  ozz = analog_defaultozz;
1212  }
1213  cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
1214  if (!cic) {
1215  cic = analog_defaultcic;
1216  }
1217 #endif
1218  if (!ozz || !cic) {
1219  ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
1220  return -1;
1221  }
1222  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
1223  snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
1224  p->whichwink = 0;
1225  }
1226  break;
1227  case ANALOG_SIG_E911:
1228  ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
1229  break;
1230  case ANALOG_SIG_FGC_CAMA:
1231  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
1232  break;
1233  case ANALOG_SIG_FGC_CAMAMF:
1234  case ANALOG_SIG_FEATB:
1235  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
1236  break;
1237  default:
1238  if (p->pulse) {
1239  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
1240  } else {
1241  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
1242  }
1243  break;
1244  }
1245 
1246  if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1247  memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1248  strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1249  p->echorest[sizeof(p->echorest) - 1] = '\0';
1250  p->echobreak = 1;
1251  p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1252  } else {
1253  p->echobreak = 0;
1254  }
1255  analog_set_waitingfordt(p, ast);
1256  if (!res) {
1257  if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
1258  analog_on_hook(p);
1259  return -1;
1260  }
1261  } else {
1262  ast_debug(1, "Deferring dialing...\n");
1263  }
1264  analog_set_dialing(p, 1);
1265  if (ast_strlen_zero(c)) {
1266  p->dialednone = 1;
1267  }
1269  break;
1270  default:
1271  ast_debug(1, "not yet implemented\n");
1272  return -1;
1273  }
1274  return 0;
1275 }
1276 
1277 int analog_hangup(struct analog_pvt *p, struct ast_channel *ast)
1278 {
1279  int res;
1280  int idx, x;
1281 
1282  ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1283  if (!ast_channel_tech_pvt(ast)) {
1284  ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1285  return 0;
1286  }
1287 
1288  idx = analog_get_index(ast, p, 1);
1289 
1290  x = 0;
1291  if (p->origcid_num) {
1292  ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
1293  ast_free(p->origcid_num);
1294  p->origcid_num = NULL;
1295  }
1296  if (p->origcid_name) {
1297  ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
1298  ast_free(p->origcid_name);
1299  p->origcid_name = NULL;
1300  }
1301 
1302  analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1303 
1304  ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1305  p->channel, idx, p->subs[ANALOG_SUB_REAL].allocd, p->subs[ANALOG_SUB_CALLWAIT].allocd, p->subs[ANALOG_SUB_THREEWAY].allocd);
1306  if (idx > -1) {
1307  /* Real channel, do some fixup */
1308  p->cshactive = 0;
1309  p->subs[idx].owner = NULL;
1310  p->polarity = POLARITY_IDLE;
1311  analog_set_linear_mode(p, idx, 0);
1312  switch (idx) {
1313  case ANALOG_SUB_REAL:
1314  if (p->subs[ANALOG_SUB_CALLWAIT].allocd && p->subs[ANALOG_SUB_THREEWAY].allocd) {
1315  ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
1316  if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
1317  /* We had flipped over to answer a callwait and now it's gone */
1318  ast_debug(1, "We were flipped over to the callwait, moving back and not owning.\n");
1319  /* Move to the call-wait, but un-own us until they flip back. */
1320  analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
1321  analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
1322  analog_set_new_owner(p, NULL);
1323  } else {
1324  /* The three way hung up, but we still have a call wait */
1325  ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
1326  analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
1327  analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1328  if (p->subs[ANALOG_SUB_REAL].inthreeway) {
1329  /* This was part of a three way call. Immediately make way for
1330  another call */
1331  ast_debug(1, "Call was complete, setting owner to former third call\n");
1332  analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
1333  analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
1334  } else {
1335  /* This call hasn't been completed yet... Set owner to NULL */
1336  ast_debug(1, "Call was incomplete, setting owner to NULL\n");
1337  analog_set_new_owner(p, NULL);
1338  }
1339  }
1340  } else if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
1341  /* Need to hold the lock for real-call, private, and call-waiting call */
1342  analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
1343  if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
1344  /* The call waiting call dissappeared. */
1345  analog_set_new_owner(p, NULL);
1346  break;
1347  }
1348 
1349  /* Move to the call-wait and switch back to them. */
1350  analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
1351  analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
1352  analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
1353  if (ast_channel_state(p->owner) != AST_STATE_UP) {
1355  }
1357  /* Unlock the call-waiting call that we swapped to real-call. */
1358  ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
1359  } else if (p->subs[ANALOG_SUB_THREEWAY].allocd) {
1360  analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
1361  analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1362  if (p->subs[ANALOG_SUB_REAL].inthreeway) {
1363  /* This was part of a three way call. Immediately make way for
1364  another call */
1365  ast_debug(1, "Call was complete, setting owner to former third call\n");
1366  analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
1367  analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
1368  } else {
1369  /* This call hasn't been completed yet... Set owner to NULL */
1370  ast_debug(1, "Call was incomplete, setting owner to NULL\n");
1371  analog_set_new_owner(p, NULL);
1372  }
1373  }
1374  break;
1375  case ANALOG_SUB_CALLWAIT:
1376  /* Ditch the holding callwait call, and immediately make it available */
1377  if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
1378  /* Need to hold the lock for call-waiting call, private, and 3-way call */
1379  analog_lock_sub_owner(p, ANALOG_SUB_THREEWAY);
1380 
1381  /* This is actually part of a three way, placed on hold. Place the third part
1382  on music on hold now */
1383  if (p->subs[ANALOG_SUB_THREEWAY].owner) {
1384  ast_queue_hold(p->subs[ANALOG_SUB_THREEWAY].owner, p->mohsuggest);
1385  }
1386  analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 0);
1387  /* Make it the call wait now */
1388  analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
1389  analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1390  if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
1391  /* Unlock the 3-way call that we swapped to call-waiting call. */
1392  ast_channel_unlock(p->subs[ANALOG_SUB_CALLWAIT].owner);
1393  }
1394  } else {
1395  analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
1396  }
1397  break;
1398  case ANALOG_SUB_THREEWAY:
1399  /* Need to hold the lock for 3-way call, private, and call-waiting call */
1400  analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
1401  if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
1402  /* The other party of the three way call is currently in a call-wait state.
1403  Start music on hold for them, and take the main guy out of the third call */
1404  analog_set_inthreeway(p, ANALOG_SUB_CALLWAIT, 0);
1405  if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
1406  ast_queue_hold(p->subs[ANALOG_SUB_CALLWAIT].owner, p->mohsuggest);
1407  }
1408  }
1409  if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
1410  ast_channel_unlock(p->subs[ANALOG_SUB_CALLWAIT].owner);
1411  }
1412  analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
1413  /* If this was part of a three way call index, let us make
1414  another three way call */
1415  analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1416  break;
1417  default:
1418  /*
1419  * Should never happen.
1420  * This wasn't any sort of call, so how are we an index?
1421  */
1422  ast_log(LOG_ERROR, "Index found but not any type of call?\n");
1423  break;
1424  }
1425  }
1426 
1427  if (!p->subs[ANALOG_SUB_REAL].owner && !p->subs[ANALOG_SUB_CALLWAIT].owner && !p->subs[ANALOG_SUB_THREEWAY].owner) {
1428  analog_set_new_owner(p, NULL);
1429  analog_set_ringtimeout(p, 0);
1430  analog_set_confirmanswer(p, 0);
1431  analog_set_pulsedial(p, 0);
1432  analog_set_outgoing(p, 0);
1433  p->onhooktime = time(NULL);
1434  p->cidrings = 1;
1435 
1436  /* Perform low level hangup if no owner left */
1437  res = analog_on_hook(p);
1438  if (res < 0) {
1439  ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast_channel_name(ast));
1440  }
1441  switch (p->sig) {
1442  case ANALOG_SIG_FXOGS:
1443  case ANALOG_SIG_FXOLS:
1444  case ANALOG_SIG_FXOKS:
1445  /* If they're off hook, try playing congestion */
1446  if (analog_is_off_hook(p)) {
1447  analog_hangup_polarityswitch(p);
1448  analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
1449  } else {
1450  analog_play_tone(p, ANALOG_SUB_REAL, -1);
1451  }
1452  break;
1453  case ANALOG_SIG_FXSGS:
1454  case ANALOG_SIG_FXSLS:
1455  case ANALOG_SIG_FXSKS:
1456  /* Make sure we're not made available for at least two seconds assuming
1457  we were actually used for an inbound or outbound call. */
1458  if (ast_channel_state(ast) != AST_STATE_RESERVED) {
1459  time(&p->guardtime);
1460  p->guardtime += 2;
1461  }
1462  break;
1463  default:
1464  analog_play_tone(p, ANALOG_SUB_REAL, -1);
1465  break;
1466  }
1467 
1468  analog_set_echocanceller(p, 0);
1469 
1470  x = 0;
1471  ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
1472  ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
1473  p->callwaitcas = 0;
1474  analog_set_callwaiting(p, p->permcallwaiting);
1475  /* In theory, the below is not necessary since we set hidecallerid = permhidecaller when calls start,
1476  * but this ensures the setting is defaulted properly when channels are idle, too. */
1477  p->hidecallerid = p->permhidecallerid;
1478  analog_set_dialing(p, 0);
1479  analog_update_conf(p);
1480  analog_all_subchannels_hungup(p);
1481  }
1482 
1483  analog_stop_callwait(p);
1484 
1485  ast_verb(3, "Hanging up on '%s'\n", ast_channel_name(ast));
1486 
1487  return 0;
1488 }
1489 
1490 int analog_answer(struct analog_pvt *p, struct ast_channel *ast)
1491 {
1492  int res = 0;
1493  int idx;
1494  int oldstate = ast_channel_state(ast);
1495 
1496  ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1497  ast_setstate(ast, AST_STATE_UP);
1498  idx = analog_get_index(ast, p, 1);
1499  if (idx < 0) {
1500  idx = ANALOG_SUB_REAL;
1501  }
1502  switch (p->sig) {
1503  case ANALOG_SIG_FXSLS:
1504  case ANALOG_SIG_FXSGS:
1505  case ANALOG_SIG_FXSKS:
1506  analog_set_ringtimeout(p, 0);
1507  /* Fall through */
1508  case ANALOG_SIG_EM:
1509  case ANALOG_SIG_EM_E1:
1510  case ANALOG_SIG_EMWINK:
1511  case ANALOG_SIG_FEATD:
1512  case ANALOG_SIG_FEATDMF:
1513  case ANALOG_SIG_FEATDMF_TA:
1514  case ANALOG_SIG_E911:
1515  case ANALOG_SIG_FGC_CAMA:
1516  case ANALOG_SIG_FGC_CAMAMF:
1517  case ANALOG_SIG_FEATB:
1518  case ANALOG_SIG_SF:
1519  case ANALOG_SIG_SFWINK:
1520  case ANALOG_SIG_SF_FEATD:
1521  case ANALOG_SIG_SF_FEATDMF:
1522  case ANALOG_SIG_SF_FEATB:
1523  case ANALOG_SIG_FXOLS:
1524  case ANALOG_SIG_FXOGS:
1525  case ANALOG_SIG_FXOKS:
1526  /* Pick up the line */
1527  ast_debug(1, "Took %s off hook\n", ast_channel_name(ast));
1528  if (p->hanguponpolarityswitch) {
1529  gettimeofday(&p->polaritydelaytv, NULL);
1530  }
1531  res = analog_off_hook(p);
1532  analog_play_tone(p, idx, -1);
1533  analog_set_dialing(p, 0);
1534  if ((idx == ANALOG_SUB_REAL) && p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
1535  if (oldstate == AST_STATE_RINGING) {
1536  ast_debug(1, "Finally swapping real and threeway\n");
1537  analog_play_tone(p, ANALOG_SUB_THREEWAY, -1);
1538  analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
1539  analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
1540  }
1541  }
1542 
1543  switch (p->sig) {
1544  case ANALOG_SIG_FXSLS:
1545  case ANALOG_SIG_FXSKS:
1546  case ANALOG_SIG_FXSGS:
1547  analog_set_echocanceller(p, 1);
1548  analog_train_echocanceller(p);
1549  break;
1550  case ANALOG_SIG_FXOLS:
1551  case ANALOG_SIG_FXOKS:
1552  case ANALOG_SIG_FXOGS:
1553  analog_answer_polarityswitch(p);
1554  break;
1555  default:
1556  break;
1557  }
1558  break;
1559  default:
1560  ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
1561  res = -1;
1562  break;
1563  }
1564  ast_setstate(ast, AST_STATE_UP);
1565  return res;
1566 }
1567 
1568 static int analog_handles_digit(struct ast_frame *f)
1569 {
1570  char subclass = toupper(f->subclass.integer);
1571 
1572  switch (subclass) {
1573  case '1':
1574  case '2':
1575  case '3':
1576  case '4':
1577  case '5':
1578  case '6':
1579  case '7':
1580  case '9':
1581  case 'A':
1582  case 'B':
1583  case 'C':
1584  case 'D':
1585  case 'E':
1586  case 'F':
1587  return 1;
1588  default:
1589  return 0;
1590  }
1591 }
1592 
1593 void analog_handle_dtmf(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub idx, struct ast_frame **dest)
1594 {
1595  struct ast_frame *f = *dest;
1596 
1597  ast_debug(1, "%s DTMF digit: 0x%02X '%c' on %s\n",
1598  f->frametype == AST_FRAME_DTMF_BEGIN ? "Begin" : "End",
1599  (unsigned)f->subclass.integer, f->subclass.integer, ast_channel_name(ast));
1600 
1601  if (analog_check_confirmanswer(p)) {
1602  if (f->frametype == AST_FRAME_DTMF_END) {
1603  ast_debug(1, "Confirm answer on %s!\n", ast_channel_name(ast));
1604  /* Upon receiving a DTMF digit, consider this an answer confirmation instead
1605  of a DTMF digit */
1606  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
1608  /* Reset confirmanswer so DTMF's will behave properly for the duration of the call */
1609  analog_set_confirmanswer(p, 0);
1610  } else {
1611  p->subs[idx].f.frametype = AST_FRAME_NULL;
1612  p->subs[idx].f.subclass.integer = 0;
1613  }
1614  *dest = &p->subs[idx].f;
1615  } else if (p->callwaitcas) {
1616  if (f->frametype == AST_FRAME_DTMF_END) {
1617  if ((f->subclass.integer == 'A') || (f->subclass.integer == 'D')) {
1618  ast_debug(1, "Got some DTMF, but it's for the CAS\n");
1619  p->caller.id.name.str = p->callwait_name;
1620  p->caller.id.number.str = p->callwait_num;
1621  analog_send_callerid(p, 1, &p->caller);
1622  }
1623  if (analog_handles_digit(f)) {
1624  p->callwaitcas = 0;
1625  }
1626  }
1627  p->subs[idx].f.frametype = AST_FRAME_NULL;
1628  p->subs[idx].f.subclass.integer = 0;
1629  *dest = &p->subs[idx].f;
1630  } else {
1631  analog_cb_handle_dtmf(p, ast, idx, dest);
1632  }
1633 }
1634 
1635 static int analog_my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
1636 {
1637  char c;
1638 
1639  *str = 0; /* start with empty output buffer */
1640  for (;;) {
1641  /* Wait for the first digit (up to specified ms). */
1642  c = ast_waitfordigit(chan, ms);
1643  /* if timeout, hangup or error, return as such */
1644  if (c < 1) {
1645  return c;
1646  }
1647  *str++ = c;
1648  *str = 0;
1649  if (strchr(term, c)) {
1650  return 1;
1651  }
1652  }
1653 }
1654 
1655 static int analog_handle_notify_message(struct ast_channel *chan, struct analog_pvt *p, int cid_flags, int neon_mwievent)
1656 {
1657  if (analog_callbacks.handle_notify_message) {
1658  analog_callbacks.handle_notify_message(chan, p->chan_pvt, cid_flags, neon_mwievent);
1659  return 0;
1660  }
1661  return -1;
1662 }
1663 
1664 static void analog_increase_ss_count(void)
1665 {
1666  if (analog_callbacks.increase_ss_count) {
1667  analog_callbacks.increase_ss_count();
1668  }
1669 }
1670 
1671 static void analog_decrease_ss_count(void)
1672 {
1673  if (analog_callbacks.decrease_ss_count) {
1674  analog_callbacks.decrease_ss_count();
1675  }
1676 }
1677 
1678 static int analog_distinctive_ring(struct ast_channel *chan, struct analog_pvt *p, int idx, int *ringdata)
1679 {
1680  if (!p->usedistinctiveringdetection) {
1681  return 0;
1682  }
1683  if (analog_callbacks.distinctive_ring) {
1684  return analog_callbacks.distinctive_ring(chan, p->chan_pvt, idx, ringdata);
1685  }
1686  return -1;
1687 
1688 }
1689 
1690 static void analog_get_and_handle_alarms(struct analog_pvt *p)
1691 {
1692  if (analog_callbacks.get_and_handle_alarms) {
1693  analog_callbacks.get_and_handle_alarms(p->chan_pvt);
1694  }
1695 }
1696 
1697 static void *analog_get_bridged_channel(struct ast_channel *chan)
1698 {
1699  if (analog_callbacks.get_sigpvt_bridged_channel) {
1700  return analog_callbacks.get_sigpvt_bridged_channel(chan);
1701  }
1702  return NULL;
1703 }
1704 
1705 static int analog_get_sub_fd(struct analog_pvt *p, enum analog_sub sub)
1706 {
1707  if (analog_callbacks.get_sub_fd) {
1708  return analog_callbacks.get_sub_fd(p->chan_pvt, sub);
1709  }
1710  return -1;
1711 }
1712 
1713 #define ANALOG_NEED_MFDETECT(p) (((p)->sig == ANALOG_SIG_FEATDMF) || ((p)->sig == ANALOG_SIG_FEATDMF_TA) || ((p)->sig == ANALOG_SIG_E911) || ((p)->sig == ANALOG_SIG_FGC_CAMA) || ((p)->sig == ANALOG_SIG_FGC_CAMAMF) || ((p)->sig == ANALOG_SIG_FEATB))
1714 
1715 static int analog_canmatch_featurecode(const char *pickupexten, const char *exten)
1716 {
1717  int extlen = strlen(exten);
1718  if (!extlen) {
1719  return 1;
1720  }
1721  if (extlen < strlen(pickupexten) && !strncmp(pickupexten, exten, extlen)) {
1722  return 1;
1723  }
1724  /* hardcoded features are *60, *67, *69, *70, *72, *73, *78, *79, *82, *0 */
1725  if (exten[0] == '*' && extlen < 3) {
1726  if (extlen == 1) {
1727  return 1;
1728  }
1729  /* "*0" should be processed before it gets here */
1730  switch (exten[1]) {
1731  case '6':
1732  case '7':
1733  case '8':
1734  return 1;
1735  }
1736  }
1737  return 0;
1738 }
1739 
1740 static void *__analog_ss_thread(void *data)
1741 {
1742  struct analog_pvt *p = data;
1743  struct ast_channel *chan = p->ss_astchan;
1744  char exten[AST_MAX_EXTENSION] = "";
1745  char exten2[AST_MAX_EXTENSION] = "";
1746  char dtmfcid[300];
1747  char dtmfbuf[300];
1748  char namebuf[ANALOG_MAX_CID];
1749  char numbuf[ANALOG_MAX_CID];
1750  char *name = NULL, *number = NULL;
1751  int flags = 0;
1752  struct ast_smdi_md_message *smdi_msg = NULL;
1753  int timeout;
1754  int getforward = 0;
1755  char *s1, *s2;
1756  int len = 0;
1757  int res;
1758  int idx;
1759  ast_callid callid;
1760  RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
1761  const char *pickupexten;
1762 
1763  analog_increase_ss_count();
1764 
1765  ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1766 
1767  ast_assert(chan != NULL);
1768 
1769  if ((callid = ast_channel_callid(chan))) {
1771  }
1772 
1773  /* in the bizarre case where the channel has become a zombie before we
1774  even get started here, abort safely
1775  */
1776  if (!ast_channel_tech_pvt(chan)) {
1777  ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", ast_channel_name(chan));
1778  ast_hangup(chan);
1779  goto quit;
1780  }
1781 
1782  ast_verb(3, "Starting simple switch on '%s'\n", ast_channel_name(chan));
1783  idx = analog_get_index(chan, p, 0);
1784  if (idx < 0) {
1785  ast_hangup(chan);
1786  goto quit;
1787  }
1788 
1789  ast_channel_lock(chan);
1790  pickup_cfg = ast_get_chan_features_pickup_config(chan);
1791  if (!pickup_cfg) {
1792  ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
1793  pickupexten = "";
1794  } else {
1795  pickupexten = ast_strdupa(pickup_cfg->pickupexten);
1796  }
1797  ast_channel_unlock(chan);
1798 
1799  analog_dsp_reset_and_flush_digits(p);
1800  switch (p->sig) {
1801  case ANALOG_SIG_FEATD:
1802  case ANALOG_SIG_FEATDMF:
1803  case ANALOG_SIG_FEATDMF_TA:
1804  case ANALOG_SIG_E911:
1805  case ANALOG_SIG_FGC_CAMAMF:
1806  case ANALOG_SIG_FEATB:
1807  case ANALOG_SIG_EMWINK:
1808  case ANALOG_SIG_SF_FEATD:
1809  case ANALOG_SIG_SF_FEATDMF:
1810  case ANALOG_SIG_SF_FEATB:
1811  case ANALOG_SIG_SFWINK:
1812  if (analog_wink(p, idx))
1813  goto quit;
1814  /* Fall through */
1815  case ANALOG_SIG_EM:
1816  case ANALOG_SIG_EM_E1:
1817  case ANALOG_SIG_SF:
1818  case ANALOG_SIG_FGC_CAMA:
1819  res = analog_play_tone(p, idx, -1);
1820 
1821  analog_dsp_reset_and_flush_digits(p);
1822 
1823  /* set digit mode appropriately */
1824  if (ANALOG_NEED_MFDETECT(p)) {
1825  analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
1826  } else {
1827  analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1828  }
1829 
1830  memset(dtmfbuf, 0, sizeof(dtmfbuf));
1831  /* Wait for the first digit only if immediate=no */
1832  if (!p->immediate) {
1833  /* Wait for the first digit (up to 5 seconds). */
1834  res = ast_waitfordigit(chan, 5000);
1835  } else {
1836  res = 0;
1837  }
1838  if (res > 0) {
1839  /* save first char */
1840  dtmfbuf[0] = res;
1841  switch (p->sig) {
1842  case ANALOG_SIG_FEATD:
1843  case ANALOG_SIG_SF_FEATD:
1844  res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
1845  if (res > 0) {
1846  res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
1847  }
1848  if (res < 1) {
1849  analog_dsp_reset_and_flush_digits(p);
1850  }
1851  break;
1852  case ANALOG_SIG_FEATDMF_TA:
1853  res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1854  if (res < 1) {
1855  analog_dsp_reset_and_flush_digits(p);
1856  }
1857  if (analog_wink(p, idx)) {
1858  goto quit;
1859  }
1860  dtmfbuf[0] = 0;
1861  /* Wait for the first digit (up to 5 seconds). */
1862  res = ast_waitfordigit(chan, 5000);
1863  if (res <= 0) {
1864  break;
1865  }
1866  dtmfbuf[0] = res;
1867  /* fall through intentionally */
1868  case ANALOG_SIG_FEATDMF:
1869  case ANALOG_SIG_E911:
1870  case ANALOG_SIG_FGC_CAMAMF:
1871  case ANALOG_SIG_SF_FEATDMF:
1872  res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1873  /* if international caca, do it again to get real ANO */
1874  if ((p->sig == ANALOG_SIG_FEATDMF) && (dtmfbuf[1] != '0')
1875  && (strlen(dtmfbuf) != 14)) {
1876  if (analog_wink(p, idx)) {
1877  goto quit;
1878  }
1879  dtmfbuf[0] = 0;
1880  /* Wait for the first digit (up to 5 seconds). */
1881  res = ast_waitfordigit(chan, 5000);
1882  if (res <= 0) {
1883  break;
1884  }
1885  dtmfbuf[0] = res;
1886  res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1887  }
1888  if (res > 0) {
1889  /* if E911, take off hook */
1890  if (p->sig == ANALOG_SIG_E911) {
1891  analog_off_hook(p);
1892  }
1893  }
1894  if (res < 1) {
1895  analog_dsp_reset_and_flush_digits(p);
1896  }
1897  break;
1898  case ANALOG_SIG_FEATB:
1899  case ANALOG_SIG_SF_FEATB:
1900  res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1901  if (res < 1) {
1902  analog_dsp_reset_and_flush_digits(p);
1903  }
1904  break;
1905  case ANALOG_SIG_EMWINK:
1906  /* if we received a '*', we are actually receiving Feature Group D
1907  dial syntax, so use that mode; otherwise, fall through to normal
1908  mode
1909  */
1910  if (res == '*') {
1911  res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
1912  if (res > 0) {
1913  res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
1914  }
1915  if (res < 1) {
1916  analog_dsp_reset_and_flush_digits(p);
1917  }
1918  break;
1919  }
1920  default:
1921  /* If we got the first digit, get the rest */
1922  len = 1;
1923  dtmfbuf[len] = '\0';
1924  while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, ast_channel_context(chan), dtmfbuf, 1, p->cid_num)) {
1925  if (ast_exists_extension(chan, ast_channel_context(chan), dtmfbuf, 1, p->cid_num)) {
1926  timeout = analog_get_matchdigit_timeout(p);
1927  } else {
1928  timeout = analog_get_interdigit_timeout(p);
1929  }
1930  res = ast_waitfordigit(chan, timeout);
1931  if (res < 0) {
1932  ast_debug(1, "waitfordigit returned < 0...\n");
1933  ast_hangup(chan);
1934  goto quit;
1935  } else if (res) {
1936  dtmfbuf[len++] = res;
1937  dtmfbuf[len] = '\0';
1938  } else {
1939  break;
1940  }
1941  }
1942  break;
1943  }
1944  }
1945  if (res == -1) {
1946  ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
1947  ast_hangup(chan);
1948  goto quit;
1949  } else if (res < 0) {
1950  ast_debug(1, "Got hung up before digits finished\n");
1951  ast_hangup(chan);
1952  goto quit;
1953  }
1954 
1955  if (p->sig == ANALOG_SIG_FGC_CAMA || p->sig == ANALOG_SIG_FGC_CAMAMF) {
1956  /* This if block is where we process ANI for CAMA */
1957 
1958  char anibuf[100];
1959  struct ast_party_caller *caller;
1960 
1961  /* cnoffset is the point at which we pull the calling number out
1962  * of anibuf. Must be the number of ani_info_digits + 1 to account
1963  * for the KP, which is considered a digit. */
1964 
1965  /* The 1XB with ANI-B will send a full 10 digits
1966  * or 2 digits in case of ANI failure.
1967  * (CD-95811-01 Section II, page 10)
1968  * 10 digit string example: *08320123#
1969  * 2 digit string example: *2
1970  * KP (*) and ST (#) are considered to be digits */
1971 
1972  int cnoffset = p->ani_info_digits + 1;
1973  ast_debug(1, "cnoffset: %d\n", cnoffset);
1974 
1975  /* This is how long to wait before the wink to start ANI spill
1976  * Pulled from chan_dahdi.conf, default is 1000ms */
1977  if (ast_safe_sleep(chan,p->ani_wink_time) == -1) {
1978  ast_hangup(chan);
1979  goto quit;
1980  }
1981  analog_off_hook(p);
1982  ast_debug(1, "Sent wink to signal ANI start\n");
1983  analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
1984 
1985  /* ani_timeout is configured in chan_dahdi.conf. default is 10000ms.
1986  * ST, STP, ST2P, ST3P can all signal transmission complete, regardless of timeout */
1987  res = analog_my_getsigstr(chan, anibuf, "#ABC", p->ani_timeout);
1988 
1989  /* so we can work with the ani buffer */
1990  pbx_builtin_setvar_helper(chan, "ANIBUF", anibuf);
1991 
1992  /* as long as we get a terminating pulse OR the length of ANI buffer is at least >=2, we can treat
1993  * this as a complete spill for the purposes of setting anistart */
1994  if ((res > 0) || (strlen(anibuf) >= 2)) {
1995  char anistart[2] = "X";
1996  char f[101] = {0};
1997  if (strchr("#ABC", anibuf[strlen(anibuf) - 1])) {
1998  anistart[0] = anibuf[strlen(anibuf) - 1];
1999  anibuf[strlen(anibuf) - 1] = 0;
2000  }
2001  ast_set_callerid(chan, anibuf + cnoffset, NULL, anibuf + cnoffset);
2002 
2003  caller = ast_channel_caller(chan);
2004  strncpy(f, &(anibuf[1]), MIN((int)(p->ani_info_digits), sizeof(f)-1));
2005  caller->ani2 = atoi(f);
2006 
2007  anibuf[cnoffset] = 0;
2008 
2009  /* so we can work with the different start pulses as used in ANI-D */
2010  pbx_builtin_setvar_helper(chan, "ANISTART", anistart);
2011  /* so we can use our ANI INFO digits in our dialplan */
2012  pbx_builtin_setvar_helper(chan, "ANI2", anibuf + 1);
2013  }
2014  analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
2015  }
2016 
2017  ast_copy_string(exten, dtmfbuf, sizeof(exten));
2018  if (ast_strlen_zero(exten)) {
2019  ast_copy_string(exten, "s", sizeof(exten));
2020  }
2021  if (p->sig == ANALOG_SIG_FEATD || p->sig == ANALOG_SIG_EMWINK) {
2022  /* Look for Feature Group D on all E&M Wink and Feature Group D trunks */
2023  if (exten[0] == '*') {
2024  char *stringp=NULL;
2025  ast_copy_string(exten2, exten, sizeof(exten2));
2026  /* Parse out extension and callerid */
2027  stringp=exten2 +1;
2028  s1 = strsep(&stringp, "*");
2029  s2 = strsep(&stringp, "*");
2030  if (s2) {
2031  if (!ast_strlen_zero(p->cid_num)) {
2032  ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
2033  } else {
2034  ast_set_callerid(chan, s1, NULL, s1);
2035  }
2036  ast_copy_string(exten, s2, sizeof(exten));
2037  } else {
2038  ast_copy_string(exten, s1, sizeof(exten));
2039  }
2040  } else if (p->sig == ANALOG_SIG_FEATD) {
2041  ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
2042  }
2043  }
2044  if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
2045  if (exten[0] == '*') {
2046  char *stringp=NULL;
2047  struct ast_party_caller *caller;
2048 
2049  ast_copy_string(exten2, exten, sizeof(exten2));
2050  /* Parse out extension and callerid */
2051  stringp=exten2 +1;
2052  s1 = strsep(&stringp, "#");
2053  s2 = strsep(&stringp, "#");
2054  if (s2) {
2055  if (!ast_strlen_zero(p->cid_num)) {
2056  ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
2057  } else {
2058  if (*(s1 + 2)) {
2059  ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
2060  }
2061  }
2062  ast_copy_string(exten, s2 + 1, sizeof(exten));
2063  } else {
2064  ast_copy_string(exten, s1 + 2, sizeof(exten));
2065  }
2066 
2067  /* The first two digits are ani2 information. */
2068  caller = ast_channel_caller(chan);
2069  s1[2] = '\0';
2070  caller->ani2 = atoi(s1);
2071  } else {
2072  ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
2073  }
2074  }
2075  if ((p->sig == ANALOG_SIG_E911) || (p->sig == ANALOG_SIG_FGC_CAMAMF)) {
2076  if (exten[0] == '*') {
2077  char *stringp=NULL;
2078  ast_copy_string(exten2, exten, sizeof(exten2));
2079  /* Parse out extension and callerid */
2080  stringp=exten2 +1;
2081  s1 = strsep(&stringp, "#");
2082  s2 = strsep(&stringp, "#");
2083  if (s2 && (*(s2 + 1) == '0')) {
2084  if (*(s2 + 2)) {
2085  ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
2086  }
2087  }
2088  if (s1) {
2089  ast_copy_string(exten, s1, sizeof(exten));
2090  } else {
2091  ast_copy_string(exten, "911", sizeof(exten));
2092  }
2093  } else {
2094  ast_log(LOG_WARNING, "A KP was expected to start signaling for Feature Group C CAMA-MF, but we got something else. Received: %s on channel %d\n", exten, p->channel);
2095  }
2096  }
2097  if (p->sig == ANALOG_SIG_FEATB) {
2098  if (exten[0] == '*') {
2099  char *stringp=NULL;
2100  ast_copy_string(exten2, exten, sizeof(exten2));
2101  /* Parse out extension and callerid */
2102  stringp=exten2 +1;
2103  s1 = strsep(&stringp, "#");
2104  ast_copy_string(exten, exten2 + 1, sizeof(exten));
2105  } else {
2106  ast_log(LOG_WARNING, "A KP was expected to start signaling for Feature Group B, but we got something else. Received: %s on channel %d\n", exten, p->channel);
2107  }
2108  }
2109  if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
2110  analog_wink(p, idx);
2111  /*
2112  * Some switches require a minimum guard time between the last
2113  * FGD wink and something that answers immediately. This
2114  * ensures it.
2115  */
2116  if (ast_safe_sleep(chan, 100)) {
2117  ast_hangup(chan);
2118  goto quit;
2119  }
2120  }
2121  analog_set_echocanceller(p, 1);
2122 
2123  analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
2124 
2125  if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1,
2126  ast_channel_caller(chan)->id.number.valid ? ast_channel_caller(chan)->id.number.str : NULL)) {
2127  ast_channel_exten_set(chan, exten);
2128  analog_dsp_reset_and_flush_digits(p);
2129  res = ast_pbx_run(chan);
2130  if (res) {
2131  ast_log(LOG_WARNING, "PBX exited non-zero\n");
2132  res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2133  }
2134  goto quit;
2135  } else {
2136  ast_verb(3, "Unknown extension '%s' in context '%s' requested\n", exten, ast_channel_context(chan));
2137  sleep(2);
2138  res = analog_play_tone(p, idx, ANALOG_TONE_INFO);
2139  if (res < 0) {
2140  ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
2141  } else {
2142  sleep(1);
2143  }
2144  res = ast_streamfile(chan, "ss-noservice", ast_channel_language(chan));
2145  if (res >= 0) {
2146  ast_waitstream(chan, "");
2147  }
2148  res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2149  ast_hangup(chan);
2150  goto quit;
2151  }
2152  break;
2153  case ANALOG_SIG_FXOLS:
2154  case ANALOG_SIG_FXOGS:
2155  case ANALOG_SIG_FXOKS:
2156  /* Set our default presentation.
2157  * This is necessary because the presentation for each call is independent
2158  * (though the default may be the same).
2159  * For example, if hidecallerid=yes and somebody makes a call with *82,
2160  * then makes a 3-way call, the presentation for the 2nd call should still
2161  * be blocked, unless that also had a *82.
2162  * For this reason, setting hidecallerid = permhidecallerid on hangup
2163  * is NOT sufficient, as the *82 from the first call could "leak" into
2164  * subsequent ones made before a hangup, improperly leaking a number
2165  * that should have been hidden.
2166  */
2167  p->hidecallerid = p->permhidecallerid;
2168 
2169  /* Read the first digit */
2170  timeout = analog_get_firstdigit_timeout(p);
2171  /* If starting a threeway call, never timeout on the first digit so someone
2172  * can use flash-hook as a "hold" feature...
2173  * ...Unless three-way dial tone should time out to silence, in which case the default suffices. */
2174  if (!p->threewaysilenthold && p->subs[ANALOG_SUB_THREEWAY].owner) {
2175  timeout = INT_MAX;
2176  }
2177  while (len < AST_MAX_EXTENSION-1) {
2178  int is_exten_parking = 0;
2179 
2180  /* Read digit unless it's supposed to be immediate, in which case the
2181  only answer is 's' */
2182  if (p->immediate) {
2183  res = 's';
2184  } else {
2185  res = ast_waitfordigit(chan, timeout);
2186  }
2187  timeout = 0;
2188  if (res < 0) {
2189  ast_debug(1, "waitfordigit returned < 0...\n");
2190  res = analog_play_tone(p, idx, -1);
2191  ast_hangup(chan);
2192  goto quit;
2193  } else if (res) {
2194  ast_debug(1,"waitfordigit returned '%c' (%d), timeout = %d\n", res, res, timeout);
2195  exten[len++]=res;
2196  exten[len] = '\0';
2197  }
2198  if (!ast_ignore_pattern(ast_channel_context(chan), exten)) {
2199  analog_play_tone(p, idx, -1);
2200  } else {
2201  analog_play_tone(p, idx, ANALOG_TONE_DIALTONE);
2202  }
2204  is_exten_parking = ast_parking_is_exten_park(ast_channel_context(chan), exten);
2205  }
2206  if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num) && !is_exten_parking) {
2207  if (!res || !ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2208  if (getforward) {
2209  /* Record this as the forwarding extension */
2210  ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
2211  ast_verb(3, "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
2212  res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2213  if (res) {
2214  break;
2215  }
2216  usleep(500000);
2217  res = analog_play_tone(p, idx, -1);
2218  sleep(1);
2219  memset(exten, 0, sizeof(exten));
2220  res = analog_play_tone(p, idx, ANALOG_TONE_DIALTONE);
2221  len = 0;
2222  getforward = 0;
2223  } else {
2224  res = analog_play_tone(p, idx, -1);
2225  ast_channel_lock(chan);
2226  ast_channel_exten_set(chan, exten);
2227 
2228  /* Properly set the presentation.
2229  * We need to do this here as well, because p->hidecallerid might be set
2230  * due to permanent blocking, not star-67/star-82 usage. */
2231  if (p->hidecallerid) {
2232  ast_channel_caller(chan)->id.number.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
2233  ast_channel_caller(chan)->id.name.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
2234  } else {
2235  ast_channel_caller(chan)->id.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
2236  ast_channel_caller(chan)->id.name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
2237  }
2238 
2240  ast_channel_unlock(chan);
2241  analog_set_echocanceller(p, 1);
2242  res = ast_pbx_run(chan);
2243  if (res) {
2244  ast_log(LOG_WARNING, "PBX exited non-zero\n");
2245  res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2246  }
2247  goto quit;
2248  }
2249  } else {
2250  /* It's a match, but they just typed a digit, and there is an ambiguous match,
2251  so just set the timeout to analog_matchdigittimeout and wait some more */
2252  timeout = analog_get_matchdigit_timeout(p);
2253  }
2254  } else if (res == 0) {
2255  ast_debug(1, "not enough digits (and no ambiguous match)...\n");
2256  if (p->threewaysilenthold) {
2257  ast_debug(1, "Nothing dialed at three-way dial tone, timed out to silent hold\n");
2258  } else {
2259  res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2260  }
2261  analog_wait_event(p);
2262  ast_hangup(chan);
2263  goto quit;
2264  } else if (p->callwaiting && !strcmp(exten, "*70")) {
2265  ast_verb(3, "Disabling call waiting on %s\n", ast_channel_name(chan));
2266  /* Disable call waiting if enabled */
2267  analog_set_callwaiting(p, 0);
2268  res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2269  if (res) {
2270  ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
2271  ast_channel_name(chan), strerror(errno));
2272  }
2273  len = 0;
2274  memset(exten, 0, sizeof(exten));
2275  timeout = analog_get_firstdigit_timeout(p);
2276 
2277  } else if (!strcmp(exten, pickupexten)) {
2278  /* Scan all channels and see if there are any
2279  * ringing channels that have call groups
2280  * that equal this channel's pickup group
2281  */
2282  if (idx == ANALOG_SUB_REAL) {
2283  /* Switch us from Third call to Call Wait */
2284  if (p->subs[ANALOG_SUB_THREEWAY].owner) {
2285  /* If you make a threeway call and then *8# a call, it should actually
2286  look like a callwait */
2287  analog_alloc_sub(p, ANALOG_SUB_CALLWAIT);
2288  analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
2289  analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
2290  }
2291  analog_set_echocanceller(p, 1);
2292  if (ast_pickup_call(chan)) {
2293  ast_debug(1, "No call pickup possible...\n");
2294  res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2295  analog_wait_event(p);
2296  }
2297  ast_hangup(chan);
2298  goto quit;
2299  } else {
2300  ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n");
2301  ast_hangup(chan);
2302  goto quit;
2303  }
2304  /* While the DMS-100 allows dialing as many *67s and *82s in succession as one's heart may desire,
2305  * the 5ESS does not, it only allows pure toggling (and only once!). So, it's not incorrect
2306  * to prevent people from dialing *67 if that won't actually do anything. */
2307  } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
2308  ast_verb(3, "Blocking Caller*ID on %s\n", ast_channel_name(chan));
2309  /* Disable Caller*ID if enabled */
2310  p->hidecallerid = 1;
2311  ast_channel_caller(chan)->id.number.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
2312  ast_channel_caller(chan)->id.name.presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
2313  res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2314  if (res) {
2315  ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
2316  ast_channel_name(chan), strerror(errno));
2317  }
2318  len = 0;
2319  memset(exten, 0, sizeof(exten));
2320  timeout = analog_get_firstdigit_timeout(p);
2321  } else if (p->callreturn && !strcmp(exten, "*69")) {
2322  res = 0;
2323  if (!ast_strlen_zero(p->lastcid_num)) {
2324  res = ast_say_digit_str(chan, p->lastcid_num, "", ast_channel_language(chan));
2325  }
2326  if (!res) {
2327  res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2328  }
2329  break;
2330  } else if (!strcmp(exten, "*78")) {
2331  /* Do not disturb enabled */
2332  analog_dnd(p, 1);
2333  res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2334  getforward = 0;
2335  memset(exten, 0, sizeof(exten));
2336  len = 0;
2337  } else if (!strcmp(exten, "*79")) {
2338  /* Do not disturb disabled */
2339  analog_dnd(p, 0);
2340  res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2341  getforward = 0;
2342  memset(exten, 0, sizeof(exten));
2343  len = 0;
2344  } else if (p->cancallforward && !strcmp(exten, "*72")) {
2345  res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2346  getforward = 1;
2347  memset(exten, 0, sizeof(exten));
2348  len = 0;
2349  } else if (p->cancallforward && !strcmp(exten, "*73")) {
2350  ast_verb(3, "Cancelling call forwarding on channel %d\n", p->channel);
2351  res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2352  memset(p->call_forward, 0, sizeof(p->call_forward));
2353  getforward = 0;
2354  memset(exten, 0, sizeof(exten));
2355  len = 0;
2356  } else if ((p->transfer || p->canpark) && is_exten_parking
2357  && p->subs[ANALOG_SUB_THREEWAY].owner) {
2358  struct ast_bridge_channel *bridge_channel;
2359 
2360  /*
2361  * This is a three way call, the main call being a real channel,
2362  * and we're parking the first call.
2363  */
2364  ast_channel_lock(p->subs[ANALOG_SUB_THREEWAY].owner);
2365  bridge_channel = ast_channel_get_bridge_channel(p->subs[ANALOG_SUB_THREEWAY].owner);
2366  ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
2367  if (bridge_channel) {
2368  if (!ast_parking_blind_transfer_park(bridge_channel, ast_channel_context(chan), exten, NULL, NULL)) {
2369  /*
2370  * Swap things around between the three-way and real call so we
2371  * can hear where the channel got parked.
2372  */
2373  analog_lock_private(p);
2374  analog_set_new_owner(p, p->subs[ANALOG_SUB_THREEWAY].owner);
2375  analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
2376  analog_unlock_private(p);
2377 
2378  ast_verb(3, "%s: Parked call\n", ast_channel_name(chan));
2379  ast_hangup(chan);
2380  ao2_ref(bridge_channel, -1);
2381  goto quit;
2382  }
2383  ao2_ref(bridge_channel, -1);
2384  }
2385  break;
2386  } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
2387  ast_verb(3, "Blacklisting number %s\n", p->lastcid_num);
2388  res = ast_db_put("blacklist", p->lastcid_num, "1");
2389  if (!res) {
2390  res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2391  memset(exten, 0, sizeof(exten));
2392  len = 0;
2393  }
2394  } else if (p->hidecallerid && !strcmp(exten, "*82")) {
2395  ast_verb(3, "Allowing Caller*ID on %s\n", ast_channel_name(chan));
2396  /* Enable Caller*ID if enabled */
2397  p->hidecallerid = 0;
2398  ast_channel_caller(chan)->id.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
2399  ast_channel_caller(chan)->id.name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
2400  res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2401  if (res) {
2402  ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
2403  ast_channel_name(chan), strerror(errno));
2404  }
2405  len = 0;
2406  memset(exten, 0, sizeof(exten));
2407  timeout = analog_get_firstdigit_timeout(p);
2408  } else if (!strcmp(exten, "*0")) {
2409  struct ast_channel *nbridge = p->subs[ANALOG_SUB_THREEWAY].owner;
2410  struct analog_pvt *pbridge = NULL;
2411  /* set up the private struct of the bridged one, if any */
2412  if (nbridge) {
2413  pbridge = analog_get_bridged_channel(nbridge);
2414  }
2415  if (pbridge && ISTRUNK(pbridge)) {
2416  /* Clear out the dial buffer */
2417  p->dop.dialstr[0] = '\0';
2418  /* flash hookswitch */
2419  if ((analog_flash(pbridge) == -1) && (errno != EINPROGRESS)) {
2420  ast_log(LOG_WARNING,
2421  "Unable to flash-hook bridged trunk from channel %s: %s\n",
2422  ast_channel_name(nbridge), strerror(errno));
2423  }
2424  analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
2425  analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
2426  analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
2428  ast_hangup(chan);
2429  goto quit;
2430  } else {
2431  analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2432  analog_wait_event(p);
2433  analog_play_tone(p, idx, -1);
2434  analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
2435  analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
2436  analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
2437  ast_hangup(chan);
2438  goto quit;
2439  }
2440  } else if (!ast_canmatch_extension(chan, ast_channel_context(chan), exten, 1,
2441  ast_channel_caller(chan)->id.number.valid ? ast_channel_caller(chan)->id.number.str : NULL)
2442  && !analog_canmatch_featurecode(pickupexten, exten)) {
2443  ast_debug(1, "Can't match %s from '%s' in context %s\n", exten,
2444  ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str
2445  ? ast_channel_caller(chan)->id.number.str : "<Unknown Caller>",
2446  ast_channel_context(chan));
2447  break;
2448  }
2449  if (!timeout) {
2450  timeout = analog_get_interdigit_timeout(p);
2451  }
2452  if (len && !ast_ignore_pattern(ast_channel_context(chan), exten)) {
2453  analog_play_tone(p, idx, -1);
2454  }
2455  }
2456  break;
2457  case ANALOG_SIG_FXSLS:
2458  case ANALOG_SIG_FXSGS:
2459  case ANALOG_SIG_FXSKS:
2460  /* check for SMDI messages */
2461  if (p->use_smdi && p->smdi_iface) {
2462  smdi_msg = ast_smdi_md_message_wait(p->smdi_iface, ANALOG_SMDI_MD_WAIT_TIMEOUT);
2463  if (smdi_msg != NULL) {
2464  ast_channel_exten_set(chan, smdi_msg->fwd_st);
2465 
2466  if (smdi_msg->type == 'B')
2467  pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "b");
2468  else if (smdi_msg->type == 'N')
2469  pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "u");
2470 
2471  ast_debug(1, "Received SMDI message on %s\n", ast_channel_name(chan));
2472  } else {
2473  ast_log(LOG_WARNING, "SMDI enabled but no SMDI message present\n");
2474  }
2475  }
2476 
2477  if (p->use_callerid && (p->cid_signalling == CID_SIG_SMDI && smdi_msg)) {
2478  number = smdi_msg->calling_st;
2479 
2480  /* If we want caller id, we're in a prering state due to a polarity reversal
2481  * and we're set to use a polarity reversal to trigger the start of caller id,
2482  * grab the caller id and wait for ringing to start... */
2483  } else if (p->use_callerid && (ast_channel_state(chan) == AST_STATE_PRERING
2484  && (p->cid_start == ANALOG_CID_START_POLARITY
2485  || p->cid_start == ANALOG_CID_START_POLARITY_IN
2486  || p->cid_start == ANALOG_CID_START_DTMF_NOALERT))) {
2487  /* If set to use DTMF CID signalling, listen for DTMF */
2488  if (p->cid_signalling == CID_SIG_DTMF) {
2489  int k = 0;
2490  int oldlinearity;
2491  int timeout_ms;
2492  int ms;
2493  struct timeval start = ast_tvnow();
2494  ast_debug(1, "Receiving DTMF cid on channel %s\n", ast_channel_name(chan));
2495 
2496  oldlinearity = analog_set_linear_mode(p, idx, 0);
2497 
2498  /*
2499  * We are the only party interested in the Rx stream since
2500  * we have not answered yet. We don't need or even want DTMF
2501  * emulation. The DTMF digits can come so fast that emulation
2502  * can drop some of them.
2503  */
2504  ast_channel_lock(chan);
2505  ast_set_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
2506  ast_channel_unlock(chan);
2507  timeout_ms = 4000;/* This is a typical OFF time between rings. */
2508  for (;;) {
2509  struct ast_frame *f;
2510 
2511  ms = ast_remaining_ms(start, timeout_ms);
2512  res = ast_waitfor(chan, ms);
2513  if (res <= 0) {
2514  /*
2515  * We do not need to restore the analog_set_linear_mode()
2516  * or AST_FLAG_END_DTMF_ONLY flag settings since we
2517  * are hanging up the channel.
2518  */
2519  ast_log(LOG_WARNING,
2520  "DTMFCID timed out waiting for ring. Exiting simple switch\n");
2521  ast_hangup(chan);
2522  goto quit;
2523  }
2524  f = ast_read(chan);
2525  if (!f) {
2526  break;
2527  }
2528  if (f->frametype == AST_FRAME_DTMF) {
2529  if (k < ARRAY_LEN(dtmfbuf) - 1) {
2530  dtmfbuf[k++] = f->subclass.integer;
2531  }
2532  ast_debug(1, "CID got digit '%c'\n", f->subclass.integer);
2533  start = ast_tvnow();
2534  }
2535  ast_frfree(f);
2536  if (ast_channel_state(chan) == AST_STATE_RING ||
2538  break; /* Got ring */
2539  }
2540  }
2541  ast_channel_lock(chan);
2542  ast_clear_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
2543  ast_channel_unlock(chan);
2544  dtmfbuf[k] = '\0';
2545 
2546  analog_set_linear_mode(p, idx, oldlinearity);
2547 
2548  /* Got cid and ring. */
2549  ast_debug(1, "CID got string '%s'\n", dtmfbuf);
2550  callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
2551  ast_debug(1, "CID is '%s', flags %d\n", dtmfcid, flags);
2552  /* If first byte is NULL, we have no cid */
2553  if (!ast_strlen_zero(dtmfcid)) {
2554  number = dtmfcid;
2555  } else {
2556  number = NULL;
2557  }
2558 
2559  /* If set to use V23 Signalling, launch our FSK gubbins and listen for it */
2560  } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) {
2561  namebuf[0] = 0;
2562  numbuf[0] = 0;
2563 
2564  if (!analog_start_cid_detect(p, p->cid_signalling)) {
2565  int timeout = 10000; /* Ten seconds */
2566  struct timeval start = ast_tvnow();
2567  enum analog_event ev;
2568  int off_ms;
2569  int ms;
2570  struct timeval off_start;
2571 
2572  if (!p->usedistinctiveringdetection) {
2573  /* Disable distinctive ring timeout count */
2574  analog_set_ringtimeout(p, 0);
2575  }
2576  while ((ms = ast_remaining_ms(start, timeout))) {
2577  res = analog_get_callerid(p, namebuf, numbuf, &ev, ms);
2578  if (res < 0) {
2579  ast_log(LOG_WARNING,
2580  "CallerID returned with error on channel '%s'\n",
2581  ast_channel_name(chan));
2582  break;
2583  }
2584  if (res == 0) {
2585  break;
2586  }
2587  if (res != 1) {
2588  continue;
2589  }
2590  if (ev == ANALOG_EVENT_NOALARM) {
2591  analog_set_alarm(p, 0);
2592  }
2593  if (p->cid_signalling == CID_SIG_V23_JP) {
2594  if (ev == ANALOG_EVENT_RINGBEGIN) {
2595  analog_off_hook(p);
2596  usleep(1);
2597  }
2598  } else {
2599  break;
2600  }
2601  }
2602 
2603  name = namebuf;
2604  number = numbuf;
2605 
2606  if (p->cid_signalling == CID_SIG_V23_JP) {
2607  analog_on_hook(p);
2608  usleep(1);
2609  }
2610 
2611  /* Finished with Caller*ID, now wait for a ring to make sure there really is a call coming */
2612  off_start = ast_tvnow();
2613  off_ms = 4000;/* This is a typical OFF time between rings. */
2614  while ((ms = ast_remaining_ms(off_start, off_ms))) {
2615  struct ast_frame *f;
2616 
2617  res = ast_waitfor(chan, ms);
2618  if (res <= 0) {
2619  ast_log(LOG_WARNING,
2620  "CID timed out waiting for ring. Exiting simple switch\n");
2621  analog_stop_cid_detect(p);
2622  ast_hangup(chan);
2623  goto quit;
2624  }
2625  if (!(f = ast_read(chan))) {
2626  ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n");
2627  analog_stop_cid_detect(p);
2628  ast_hangup(chan);
2629  goto quit;
2630  }
2631  ast_frfree(f);
2632  if (ast_channel_state(chan) == AST_STATE_RING ||
2634  break; /* Got ring */
2635  }
2636 
2637  res = analog_distinctive_ring(chan, p, idx, NULL);
2638  analog_stop_cid_detect(p);
2639  if (res) {
2640  goto quit;
2641  }
2642  } else {
2643  ast_log(LOG_WARNING, "Unable to get caller ID space\n");
2644  }
2645  } else {
2646  ast_log(LOG_WARNING,
2647  "Channel %s in prering state, but I have nothing to do. Terminating simple switch, should be restarted by the actual ring.\n",
2648  ast_channel_name(chan));
2649  ast_hangup(chan);
2650  goto quit;
2651  }
2652  } else if (p->use_callerid && p->cid_start == ANALOG_CID_START_RING) {
2653  namebuf[0] = 0;
2654  numbuf[0] = 0;
2655 
2656  if (!analog_start_cid_detect(p, p->cid_signalling)) {
2657  int timeout = 10000; /* Ten seconds */
2658  struct timeval start = ast_tvnow();
2659  enum analog_event ev;
2660  int ring_data[RING_PATTERNS] = { 0 };
2661  int ring_data_idx = 0;
2662  int ms;
2663 
2664  if (!p->usedistinctiveringdetection) {
2665  /* Disable distinctive ring timeout count */
2666  analog_set_ringtimeout(p, 0);
2667  }
2668  while ((ms = ast_remaining_ms(start, timeout))) {
2669  res = analog_get_callerid(p, namebuf, numbuf, &ev, ms);
2670  if (res < 0) {
2671  ast_log(LOG_WARNING,
2672  "CallerID returned with error on channel '%s'\n",
2673  ast_channel_name(chan));
2674  break;
2675  }
2676  if (res == 0) {
2677  break;
2678  }
2679  if (res != 1) {
2680  continue;
2681  }
2682  if (ev == ANALOG_EVENT_NOALARM) {
2683  analog_set_alarm(p, 0);
2684  } else if (ev == ANALOG_EVENT_POLARITY
2685  && p->hanguponpolarityswitch
2686  && p->polarity == POLARITY_REV) {
2687  ast_debug(1,
2688  "Hanging up due to polarity reversal on channel %d while detecting callerid\n",
2689  p->channel);
2690  p->polarity = POLARITY_IDLE;
2691  analog_stop_cid_detect(p);
2692  ast_hangup(chan);
2693  goto quit;
2694  } else if (ev == ANALOG_EVENT_RINGOFFHOOK
2695  && p->usedistinctiveringdetection
2696  && ring_data_idx < RING_PATTERNS) {
2697  /*
2698  * Detect callerid while collecting possible
2699  * distinctive ring pattern.
2700  */
2701  ring_data[ring_data_idx] = p->ringt;
2702  ++ring_data_idx;
2703  }
2704  }
2705 
2706  name = namebuf;
2707  number = numbuf;
2708 
2709  res = analog_distinctive_ring(chan, p, idx, ring_data);
2710  analog_stop_cid_detect(p);
2711  if (res) {
2712  goto quit;
2713  }
2714  } else {
2715  ast_log(LOG_WARNING, "Unable to get caller ID space\n");
2716  }
2717  }
2718 
2719  if (number) {
2720  ast_shrink_phone_number(number);
2721  }
2722  ast_set_callerid(chan, number, name, number);
2723 
2724  analog_handle_notify_message(chan, p, flags, -1);
2725 
2726  ast_channel_lock(chan);
2728  ast_channel_rings_set(chan, 1);
2729  ast_channel_unlock(chan);
2730  analog_set_ringtimeout(p, p->ringt_base);
2731  res = ast_pbx_run(chan);
2732  if (res) {
2733  ast_hangup(chan);
2734  ast_log(LOG_WARNING, "PBX exited non-zero\n");
2735  }
2736  goto quit;
2737  default:
2738  ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", analog_sigtype_to_str(p->sig), p->channel);
2739  break;
2740  }
2741  res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2742  if (res < 0) {
2743  ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
2744  }
2745  ast_hangup(chan);
2746 quit:
2747  ao2_cleanup(smdi_msg);
2748  analog_decrease_ss_count();
2749  return NULL;
2750 }
2751 
2752 int analog_ss_thread_start(struct analog_pvt *p, struct ast_channel *chan)
2753 {
2754  pthread_t threadid;
2755 
2756  p->ss_astchan = chan;
2757  return ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, p);
2758 }
2759 
2760 static void analog_publish_channel_alarm_clear(int channel)
2761 {
2762  RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
2763 
2764  ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", channel);
2765  body = ast_json_pack("{s: i}", "Channel", channel);
2766  if (!body) {
2767  return;
2768  }
2769 
2770  ast_manager_publish_event("AlarmClear", EVENT_FLAG_SYSTEM, body);
2771 }
2772 
2773 static struct ast_frame *__analog_handle_event(struct analog_pvt *p, struct ast_channel *ast)
2774 {
2775  int res, x;
2776  int mysig;
2777  int idx;
2778  char *c;
2779  pthread_t threadid;
2780  struct ast_channel *chan;
2781  struct ast_frame *f;
2782  struct ast_control_pvt_cause_code *cause_code = NULL;
2783  int data_size = sizeof(*cause_code);
2784  char *subclass = NULL;
2785 
2786  ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
2787 
2788  idx = analog_get_index(ast, p, 0);
2789  if (idx < 0) {
2790  return &ast_null_frame;
2791  }
2792  if (idx != ANALOG_SUB_REAL) {
2793  ast_log(LOG_ERROR, "We got an event on a non real sub. Fix it!\n");
2794  }
2795 
2796  mysig = p->sig;
2797  if (p->outsigmod > -1) {
2798  mysig = p->outsigmod;
2799  }
2800 
2801  p->subs[idx].f.frametype = AST_FRAME_NULL;
2802  p->subs[idx].f.subclass.integer = 0;
2803  p->subs[idx].f.datalen = 0;
2804  p->subs[idx].f.samples = 0;
2805  p->subs[idx].f.mallocd = 0;
2806  p->subs[idx].f.offset = 0;
2807  p->subs[idx].f.src = "dahdi_handle_event";
2808  p->subs[idx].f.data.ptr = NULL;
2809  f = &p->subs[idx].f;
2810 
2811  res = analog_get_event(p);
2812 
2813  ast_debug(1, "Got event %s(%d) on channel %d (index %u)\n", analog_event2str(res), res, p->channel, idx);
2814 
2815  if (res & (ANALOG_EVENT_PULSEDIGIT | ANALOG_EVENT_DTMFUP)) {
2816  analog_set_pulsedial(p, (res & ANALOG_EVENT_PULSEDIGIT) ? 1 : 0);
2817  ast_debug(1, "Detected %sdigit '%c'\n", (res & ANALOG_EVENT_PULSEDIGIT) ? "pulse ": "", res & 0xff);
2818  analog_confmute(p, 0);
2819  if (p->dialmode == ANALOG_DIALMODE_BOTH || p->dialmode == ANALOG_DIALMODE_PULSE) {
2820  p->subs[idx].f.frametype = AST_FRAME_DTMF_END;
2821  p->subs[idx].f.subclass.integer = res & 0xff;
2822  analog_handle_dtmf(p, ast, idx, &f);
2823  } else {
2824  ast_debug(1, "Dropping pulse digit '%c' because pulse dialing disabled on channel %d\n", res & 0xff, p->channel);
2825  }
2826  return f;
2827  }
2828 
2829  if (res & ANALOG_EVENT_DTMFDOWN) {
2830  ast_debug(1, "DTMF Down '%c'\n", res & 0xff);
2831  /* Mute conference */
2832  analog_confmute(p, 1);
2834  p->subs[idx].f.subclass.integer = res & 0xff;
2835  analog_handle_dtmf(p, ast, idx, &f);
2836  return f;
2837  }
2838 
2839  switch (res) {
2840  case ANALOG_EVENT_ALARM:
2841  case ANALOG_EVENT_POLARITY:
2842  case ANALOG_EVENT_ONHOOK:
2843  /* add length of "ANALOG " */
2844  data_size += 7;
2845  subclass = analog_event2str(res);
2846  data_size += strlen(subclass);
2847  cause_code = ast_alloca(data_size);
2848  memset(cause_code, 0, data_size);
2849  cause_code->ast_cause = AST_CAUSE_NORMAL_CLEARING;
2850  ast_copy_string(cause_code->chan_name, ast_channel_name(ast), AST_CHANNEL_NAME);
2851  snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "ANALOG %s", subclass);
2852  break;
2853  default:
2854  break;
2855  }
2856 
2857  switch (res) {
2858  case ANALOG_EVENT_EC_DISABLED:
2859  ast_verb(3, "Channel %d echo canceller disabled due to CED detection\n", p->channel);
2860  analog_set_echocanceller(p, 0);
2861  break;
2862 #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
2863  case ANALOG_EVENT_TX_CED_DETECTED:
2864  ast_verb(3, "Channel %d detected a CED tone towards the network.\n", p->channel);
2865  break;
2866  case ANALOG_EVENT_RX_CED_DETECTED:
2867  ast_verb(3, "Channel %d detected a CED tone from the network.\n", p->channel);
2868  break;
2869  case ANALOG_EVENT_EC_NLP_DISABLED:
2870  ast_verb(3, "Channel %d echo canceller disabled its NLP.\n", p->channel);
2871  break;
2872  case ANALOG_EVENT_EC_NLP_ENABLED:
2873  ast_verb(3, "Channel %d echo canceller enabled its NLP.\n", p->channel);
2874  break;
2875 #endif
2876  case ANALOG_EVENT_PULSE_START:
2877  /* Stop tone if there's a pulse start and the PBX isn't started */
2878  if (!ast_channel_pbx(ast))
2879  analog_play_tone(p, ANALOG_SUB_REAL, -1);
2880  break;
2881  case ANALOG_EVENT_DIALCOMPLETE:
2882  if (p->inalarm) {
2883  break;
2884  }
2885  x = analog_is_dialing(p, idx);
2886  if (!x) { /* if not still dialing in driver */
2887  analog_set_echocanceller(p, 1);
2888  if (p->echobreak) {
2889  analog_train_echocanceller(p);
2890  ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
2891  p->dop.op = ANALOG_DIAL_OP_REPLACE;
2892  analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
2893  p->echobreak = 0;
2894  } else {
2895  analog_set_dialing(p, 0);
2896  if ((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) {
2897  /* if thru with dialing after offhook */
2899  ast_setstate(ast, AST_STATE_UP);
2900  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
2902  break;
2903  } else { /* if to state wait for offhook to dial rest */
2904  /* we now wait for off hook */
2906  }
2907  }
2908  if (ast_channel_state(ast) == AST_STATE_DIALING) {
2909  if (analog_have_progressdetect(p)) {
2910  ast_debug(1, "Done dialing, but waiting for progress detection before doing more...\n");
2911  } else if (analog_check_confirmanswer(p) || (!p->dialednone
2912  && ((mysig == ANALOG_SIG_EM) || (mysig == ANALOG_SIG_EM_E1)
2913  || (mysig == ANALOG_SIG_EMWINK) || (mysig == ANALOG_SIG_FEATD)
2914  || (mysig == ANALOG_SIG_FEATDMF_TA) || (mysig == ANALOG_SIG_FEATDMF)
2915  || (mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA)
2916  || (mysig == ANALOG_SIG_FGC_CAMAMF) || (mysig == ANALOG_SIG_FEATB)
2917  || (mysig == ANALOG_SIG_SF) || (mysig == ANALOG_SIG_SFWINK)
2918  || (mysig == ANALOG_SIG_SF_FEATD) || (mysig == ANALOG_SIG_SF_FEATDMF)
2919  || (mysig == ANALOG_SIG_SF_FEATB)))) {
2921  } else if (!p->answeronpolarityswitch) {
2922  ast_setstate(ast, AST_STATE_UP);
2923  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
2925  /* If aops=0 and hops=1, this is necessary */
2926  p->polarity = POLARITY_REV;
2927  } else {
2928  /* Start clean, so we can catch the change to REV polarity when party answers */
2929  p->polarity = POLARITY_IDLE;
2930  }
2931  }
2932  }
2933  }
2934  break;
2935  case ANALOG_EVENT_ALARM:
2936  analog_set_alarm(p, 1);
2937  analog_get_and_handle_alarms(p);
2938  cause_code->ast_cause = AST_CAUSE_NETWORK_OUT_OF_ORDER;
2939  case ANALOG_EVENT_ONHOOK:
2940  if (p->calledsubscriberheld && (p->sig == ANALOG_SIG_FXOLS || p->sig == ANALOG_SIG_FXOGS || p->sig == ANALOG_SIG_FXOKS) && idx == ANALOG_SUB_REAL) {
2941  ast_debug(4, "Channel state on %s is %d\n", ast_channel_name(ast), ast_channel_state(ast));
2942  /* Called Subscriber Held: don't let the called party hang up on an incoming call immediately (if it's the only call). */
2943  if (p->subs[ANALOG_SUB_CALLWAIT].owner || p->subs[ANALOG_SUB_THREEWAY].owner) {
2944  ast_debug(2, "Letting this call hang up normally, since it's not the only call\n");
2945  } else if (!p->owner || !p->subs[ANALOG_SUB_REAL].owner || ast_channel_state(ast) != AST_STATE_UP) {
2946  ast_debug(2, "Called Subscriber Held does not apply: channel state is %d\n", ast_channel_state(ast));
2947  } else if (!p->owner || !p->subs[ANALOG_SUB_REAL].owner || strcmp(ast_channel_appl(p->subs[ANALOG_SUB_REAL].owner), "AppDial")) {
2948  /* Called Subscriber held only applies to incoming calls, not outgoing calls.
2949  * We can't use p->outgoing because that is always true, for both incoming and outgoing calls, so it's not accurate.
2950  * We can check the channel application/data instead.
2951  * For incoming calls to the channel, it will look like: AppDial / (Outgoing Line)
2952  * We only want this behavior for regular calls anyways (and not, say, Queue),
2953  * so this would actually work great. But accessing ast_channel_appl can cause a crash if there are no calls left,
2954  * so this check must occur AFTER we confirm the channel state *is* still UP.
2955  */
2956  ast_debug(2, "Called Subscriber Held does not apply: not an incoming call\n");
2957  } else if (analog_is_off_hook(p)) {
2958  ast_log(LOG_WARNING, "Got ONHOOK but channel %d is off hook?\n", p->channel); /* Shouldn't happen */
2959  } else {
2960  ast_verb(3, "Holding incoming call %s for channel %d\n", ast_channel_name(ast), p->channel);
2961  /* Inhibit dahdi_hangup from getting called, and do nothing else now.
2962  * When the DAHDI channel goes off hook again, it'll just get reconnected with the incoming call,
2963  * to which, as far as its concerned, nothing has happened. */
2964  p->cshactive = 1; /* Keep track that this DAHDI channel is currently being held by an incoming call. */
2965  break;
2966  }
2967  }
2968  ast_queue_control_data(ast, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
2969  ast_channel_hangupcause_hash_set(ast, cause_code, data_size);
2970  switch (p->sig) {
2971  case ANALOG_SIG_FXOLS:
2972  case ANALOG_SIG_FXOGS:
2973  case ANALOG_SIG_FXOKS:
2974  analog_start_polarityswitch(p);
2975  p->fxsoffhookstate = 0;
2976  p->onhooktime = time(NULL);
2977  p->msgstate = -1;
2978  /* Check for some special conditions regarding call waiting */
2979  if (idx == ANALOG_SUB_REAL) {
2980  /* The normal line was hung up */
2981  if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
2982  /* Need to hold the lock for real-call, private, and call-waiting call */
2983  analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
2984  if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
2985  /*
2986  * The call waiting call disappeared.
2987  * This is now a normal hangup.
2988  */
2989  analog_set_echocanceller(p, 0);
2990  return NULL;
2991  }
2992 
2993  /* There's a call waiting call, so ring the phone, but make it unowned in the meantime */
2994  analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
2995  ast_verb(3, "Channel %d still has (callwait) call, ringing phone\n", p->channel);
2996  analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
2997  analog_stop_callwait(p);
2998  analog_set_new_owner(p, NULL);
2999  /* Don't start streaming audio yet if the incoming call isn't up yet */
3000  if (ast_channel_state(p->subs[ANALOG_SUB_REAL].owner) != AST_STATE_UP) {
3001  analog_set_dialing(p, 1);
3002  }
3003  /* Unlock the call-waiting call that we swapped to real-call. */
3004  ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
3005  analog_ring(p);
3006  } else if (p->subs[ANALOG_SUB_THREEWAY].owner) {
3007  unsigned int mssinceflash;
3008 
3009  /* Need to hold the lock for real-call, private, and 3-way call */
3010  analog_lock_sub_owner(p, ANALOG_SUB_THREEWAY);
3011  if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
3012  ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
3013  /* Just hangup */
3014  return NULL;
3015  }
3016  if (p->owner != ast) {
3017  ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
3018  ast_log(LOG_WARNING, "This isn't good...\n");
3019  /* Just hangup */
3020  return NULL;
3021  }
3022 
3023  mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
3024  ast_debug(1, "Last flash was %u ms ago\n", mssinceflash);
3025  if (mssinceflash < MIN_MS_SINCE_FLASH) {
3026  /* It hasn't been long enough since the last flashook. This is probably a bounce on
3027  hanging up. Hangup both channels now */
3028  ast_debug(1, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
3029  ast_queue_hangup_with_cause(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CAUSE_NO_ANSWER);
3031  ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
3032  } else if ((ast_channel_pbx(ast)) || (ast_channel_state(ast) == AST_STATE_UP)) {
3033  if (p->transfer) {
3034  /* In any case this isn't a threeway call anymore */
3035  analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
3036  analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 0);
3037 
3038  /* Only attempt transfer if the phone is ringing; why transfer to busy tone eh? */
3039  if (!p->transfertobusy && ast_channel_state(ast) == AST_STATE_BUSY) {
3040  /* Swap subs and dis-own channel */
3041  analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
3042  /* Unlock the 3-way call that we swapped to real-call. */
3043  ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
3044  analog_set_new_owner(p, NULL);
3045  /* Ring the phone */
3046  analog_ring(p);
3047  } else if (!analog_attempt_transfer(p)) {
3048  /*
3049  * Transfer successful. Don't actually hang up at this point.
3050  * Let our channel legs of the calls die off as the transfer
3051  * percolates through the core.
3052  */
3053  break;
3054  }
3055  } else {
3057  ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
3058  }
3059  } else {
3060  /* Swap subs and dis-own channel */
3061  analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
3062  /* Unlock the 3-way call that we swapped to real-call. */
3063  ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
3064  analog_set_new_owner(p, NULL);
3065  /* Ring the phone */
3066  analog_ring(p);
3067  }
3068  }
3069  } else {
3070  ast_log(LOG_WARNING, "Got a hangup and my index is %u?\n", idx);
3071  }
3072  /* Fall through */
3073  default:
3074  analog_set_echocanceller(p, 0);
3075  return NULL;
3076  }
3077  break;
3078  case ANALOG_EVENT_RINGOFFHOOK:
3079  if (p->inalarm) {
3080  break;
3081  }
3082  /* for E911, its supposed to wait for offhook then dial
3083  the second half of the dial string */
3084  if (((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) && (ast_channel_state(ast) == AST_STATE_DIALING_OFFHOOK)) {
3085  c = strchr(p->dialdest, '/');
3086  if (c) {
3087  c++;
3088  } else {
3089  c = p->dialdest;
3090  }
3091 
3092  if (*c) {
3093  int numchars = snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
3094  if (numchars >= sizeof(p->dop.dialstr)) {
3095  ast_log(LOG_WARNING, "Dial string '%s' truncated\n", c);
3096  }
3097  } else {
3098  ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
3099  }
3100 
3101  if (strlen(p->dop.dialstr) > 4) {
3102  memset(p->echorest, 'w', sizeof(p->echorest) - 1);
3103  strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
3104  p->echorest[sizeof(p->echorest) - 1] = '\0';
3105  p->echobreak = 1;
3106  p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
3107  } else {
3108  p->echobreak = 0;
3109  }
3110  if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
3111  analog_on_hook(p);
3112  return NULL;
3113  }
3114  analog_set_dialing(p, 1);
3115  return &p->subs[idx].f;
3116  }
3117  switch (p->sig) {
3118  case ANALOG_SIG_FXOLS:
3119  case ANALOG_SIG_FXOGS:
3120  case ANALOG_SIG_FXOKS:
3121  p->fxsoffhookstate = 1;
3122  switch (ast_channel_state(ast)) {
3123  case AST_STATE_RINGING:
3124  analog_set_echocanceller(p, 1);
3125  analog_train_echocanceller(p);
3126  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
3128  /* Make sure it stops ringing */
3129  analog_set_needringing(p, 0);
3130  analog_off_hook(p);
3131  ast_debug(1, "channel %d answered\n", p->channel);
3132 
3133  /* Cancel any running CallerID spill */
3134  analog_cancel_cidspill(p);
3135 
3136  analog_set_dialing(p, 0);
3137  p->callwaitcas = 0;
3138  if (analog_check_confirmanswer(p)) {
3139  /* Ignore answer if "confirm answer" is enabled */
3140  p->subs[idx].f.frametype = AST_FRAME_NULL;
3141  p->subs[idx].f.subclass.integer = 0;
3142  } else if (!ast_strlen_zero(p->dop.dialstr)) {
3143  /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */
3144  res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
3145  if (res) {
3146  p->dop.dialstr[0] = '\0';
3147  return NULL;
3148  } else {
3149  ast_debug(1, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
3150  p->subs[idx].f.frametype = AST_FRAME_NULL;
3151  p->subs[idx].f.subclass.integer = 0;
3152  analog_set_dialing(p, 1);
3153  }
3154  p->dop.dialstr[0] = '\0';
3156  } else {
3157  ast_setstate(ast, AST_STATE_UP);
3158  analog_answer_polarityswitch(p);
3159  }
3160  return &p->subs[idx].f;
3161  case AST_STATE_DOWN:
3163  ast_channel_rings_set(ast, 1);
3164  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
3166  ast_debug(1, "channel %d picked up\n", p->channel);
3167  return &p->subs[idx].f;
3168  case AST_STATE_UP:
3169  /* Make sure it stops ringing */
3170  analog_off_hook(p);
3171  /* Okay -- probably call waiting */
3172  ast_queue_unhold(p->owner);
3173  break;
3174  case AST_STATE_RESERVED:
3175  /* Start up dialtone */
3176  if (analog_has_voicemail(p)) {
3177  res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_STUTTER);
3178  } else {
3179  res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_DIALTONE);
3180  }
3181  break;
3182  default:
3183  ast_log(LOG_WARNING, "FXO phone off hook in weird state %u??\n", ast_channel_state(ast));
3184  }
3185  break;
3186  case ANALOG_SIG_FXSLS:
3187  case ANALOG_SIG_FXSGS:
3188  case ANALOG_SIG_FXSKS:
3189  if (ast_channel_state(ast) == AST_STATE_RING) {
3190  analog_set_ringtimeout(p, p->ringt_base);
3191  }
3192 
3193  /* Fall through */
3194  case ANALOG_SIG_EM:
3195  case ANALOG_SIG_EM_E1:
3196  case ANALOG_SIG_EMWINK:
3197  case ANALOG_SIG_FEATD:
3198  case ANALOG_SIG_FEATDMF:
3199  case ANALOG_SIG_FEATDMF_TA:
3200  case ANALOG_SIG_E911:
3201  case ANALOG_SIG_FGC_CAMA:
3202  case ANALOG_SIG_FGC_CAMAMF:
3203  case ANALOG_SIG_FEATB:
3204  case ANALOG_SIG_SF:
3205  case ANALOG_SIG_SFWINK:
3206  case ANALOG_SIG_SF_FEATD:
3207  case ANALOG_SIG_SF_FEATDMF:
3208  case ANALOG_SIG_SF_FEATB:
3209  switch (ast_channel_state(ast)) {
3210  case AST_STATE_PRERING:
3212  /* Fall through */
3213  case AST_STATE_DOWN:
3214  case AST_STATE_RING:
3215  ast_debug(1, "Ring detected\n");
3216  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
3218  break;
3219  case AST_STATE_RINGING:
3220  case AST_STATE_DIALING:
3221  if (p->outgoing) {
3222  ast_debug(1, "Line answered\n");
3223  if (analog_check_confirmanswer(p)) {
3224  p->subs[idx].f.frametype = AST_FRAME_NULL;
3225  p->subs[idx].f.subclass.integer = 0;
3226  } else {
3227  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
3229  ast_setstate(ast, AST_STATE_UP);
3230  }
3231  break;
3232  }
3233  /* Fall through */
3234  default:
3235  ast_log(LOG_WARNING, "Ring/Off-hook in strange state %u on channel %d\n", ast_channel_state(ast), p->channel);
3236  break;
3237  }
3238  break;
3239  default:
3240  ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
3241  break;
3242  }
3243  break;
3244  case ANALOG_EVENT_RINGBEGIN:
3245  switch (p->sig) {
3246  case ANALOG_SIG_FXSLS:
3247  case ANALOG_SIG_FXSGS:
3248  case ANALOG_SIG_FXSKS:
3249  if (ast_channel_state(ast) == AST_STATE_RING) {
3250  analog_set_ringtimeout(p, p->ringt_base);
3251  }
3252  break;
3253  default:
3254  break;
3255  }
3256  break;
3257  case ANALOG_EVENT_RINGEROFF:
3258  if (p->inalarm) break;
3259  ast_channel_rings_set(ast, ast_channel_rings(ast) + 1);
3260  if (ast_channel_rings(ast) == p->cidrings) {
3261  analog_send_callerid(p, 0, &p->caller);
3262  }
3263 
3264  if (ast_channel_rings(ast) > p->cidrings) {
3265  analog_cancel_cidspill(p);
3266  p->callwaitcas = 0;
3267  }
3268  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
3270  break;
3271  case ANALOG_EVENT_RINGERON:
3272  break;
3273  case ANALOG_EVENT_NOALARM:
3274  analog_set_alarm(p, 0);
3275  analog_publish_channel_alarm_clear(p->channel);
3276  break;
3277  case ANALOG_EVENT_WINKFLASH:
3278  if (p->inalarm) {
3279  break;
3280  }
3281  /* Remember last time we got a flash-hook */
3282  gettimeofday(&p->flashtime, NULL);
3283  switch (mysig) {
3284  case ANALOG_SIG_FXOLS:
3285  case ANALOG_SIG_FXOGS:
3286  case ANALOG_SIG_FXOKS:
3287  ast_debug(1, "Winkflash, index: %u, normal: %d, callwait: %d, thirdcall: %d\n",
3288  idx, analog_get_sub_fd(p, ANALOG_SUB_REAL), analog_get_sub_fd(p, ANALOG_SUB_CALLWAIT), analog_get_sub_fd(p, ANALOG_SUB_THREEWAY));
3289 
3290  /* Cancel any running CallerID spill */
3291  analog_cancel_cidspill(p);
3292  p->callwaitcas = 0;
3293 
3294  if (idx != ANALOG_SUB_REAL) {
3295  ast_log(LOG_WARNING, "Got flash hook with index %u on channel %d?!?\n", idx, p->channel);
3296  goto winkflashdone;
3297  }
3298 
3299  if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
3300  /* Need to hold the lock for real-call, private, and call-waiting call */
3301  analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
3302  if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
3303  /*
3304  * The call waiting call dissappeared.
3305  * Let's just ignore this flash-hook.
3306  */
3307  ast_log(LOG_NOTICE, "Whoa, the call-waiting call disappeared.\n");
3308  goto winkflashdone;
3309  }
3310 
3311  /* Swap to call-wait */
3312  analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_CALLWAIT);
3313  analog_play_tone(p, ANALOG_SUB_REAL, -1);
3314  analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
3315  ast_debug(1, "Making %s the new owner\n", ast_channel_name(p->owner));
3319  }
3320  analog_stop_callwait(p);
3321 
3322  /* Start music on hold if appropriate */
3323  if (!p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
3324  ast_queue_hold(p->subs[ANALOG_SUB_CALLWAIT].owner, p->mohsuggest);
3325  }
3326  ast_queue_hold(p->subs[ANALOG_SUB_REAL].owner, p->mohsuggest);
3328 
3329  /* Unlock the call-waiting call that we swapped to real-call. */
3330  ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
3331  } else if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
3332  if (!p->threewaycalling) {
3333  /* Just send a flash if no 3-way calling */
3335  goto winkflashdone;
3336  } else if (!analog_check_for_conference(p)) {
3337  ast_callid callid = 0;
3338  int callid_created;
3339  char cid_num[256];
3340  char cid_name[256];
3341 
3342  cid_num[0] = '\0';
3343  cid_name[0] = '\0';
3344  if (p->dahditrcallerid && p->owner) {
3345  if (ast_channel_caller(p->owner)->id.number.valid
3346  && ast_channel_caller(p->owner)->id.number.str) {
3347  ast_copy_string(cid_num, ast_channel_caller(p->owner)->id.number.str,
3348  sizeof(cid_num));
3349  }
3350  if (ast_channel_caller(p->owner)->id.name.valid
3351  && ast_channel_caller(p->owner)->id.name.str) {
3352  ast_copy_string(cid_name, ast_channel_caller(p->owner)->id.name.str,
3353  sizeof(cid_name));
3354  }
3355  }
3356  /* XXX This section needs much more error checking!!! XXX */
3357  /* Start a 3-way call if feasible */
3358  if (!((ast_channel_pbx(ast)) ||
3359  (ast_channel_state(ast) == AST_STATE_UP) ||
3360  (ast_channel_state(ast) == AST_STATE_RING))) {
3361  ast_debug(1, "Flash when call not up or ringing\n");
3362  goto winkflashdone;
3363  }
3364  if (analog_alloc_sub(p, ANALOG_SUB_THREEWAY)) {
3365  ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
3366  goto winkflashdone;
3367  }
3368 
3369  callid_created = ast_callid_threadstorage_auto(&callid);
3370 
3371  /*
3372  * Make new channel
3373  *
3374  * We cannot hold the p or ast locks while creating a new
3375  * channel.
3376  */
3377  analog_unlock_private(p);
3378  ast_channel_unlock(ast);
3379  chan = analog_new_ast_channel(p, AST_STATE_RESERVED, 0, ANALOG_SUB_THREEWAY, NULL);
3380  ast_channel_lock(ast);
3381  analog_lock_private(p);
3382  if (!chan) {
3383  ast_log(LOG_WARNING,
3384  "Cannot allocate new call structure on channel %d\n",
3385  p->channel);
3386  analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
3387  ast_callid_threadstorage_auto_clean(callid, callid_created);
3388  goto winkflashdone;
3389  }
3390  if (p->dahditrcallerid) {
3391  if (!p->origcid_num) {
3392  p->origcid_num = ast_strdup(p->cid_num);
3393  }
3394  if (!p->origcid_name) {
3395  p->origcid_name = ast_strdup(p->cid_name);
3396  }
3397  ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
3398  ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
3399  }
3400  /* Swap things around between the three-way and real call */
3401  analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
3402  /* Disable echo canceller for better dialing */
3403  analog_set_echocanceller(p, 0);
3404  res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_DIALRECALL);
3405  if (res) {
3406  ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
3407  }
3408  analog_set_new_owner(p, chan);
3409  p->ss_astchan = chan;
3410  if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, p)) {
3411  ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
3412  res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
3413  analog_set_echocanceller(p, 1);
3414  ast_hangup(chan);
3415  } else {
3416  ast_verb(3, "Started three way call on channel %d\n", p->channel);
3417 
3418  /* Start music on hold */
3419  ast_queue_hold(p->subs[ANALOG_SUB_THREEWAY].owner, p->mohsuggest);
3420  }
3421  ast_callid_threadstorage_auto_clean(callid, callid_created);
3422  }
3423  } else {
3424  /* Already have a 3 way call */
3425  enum analog_sub orig_3way_sub;
3426 
3427  /* Need to hold the lock for real-call, private, and 3-way call */
3428  analog_lock_sub_owner(p, ANALOG_SUB_THREEWAY);
3429  if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
3430  /*
3431  * The 3-way call dissappeared.
3432  * Let's just ignore this flash-hook.
3433  */
3434  ast_log(LOG_NOTICE, "Whoa, the 3-way call disappeared.\n");
3435  goto winkflashdone;
3436  }
3437  orig_3way_sub = ANALOG_SUB_THREEWAY;
3438 
3439  if (p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
3440  /* Call is already up, drop the last person */
3441  ast_debug(1, "Got flash with three way call up, dropping last call on %d\n", p->channel);
3442  /* If the primary call isn't answered yet, use it */
3443  if ((ast_channel_state(p->subs[ANALOG_SUB_REAL].owner) != AST_STATE_UP) &&
3445  /* Swap back -- we're dropping the real 3-way that isn't finished yet*/
3446  analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
3447  orig_3way_sub = ANALOG_SUB_REAL;
3448  analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
3449  }
3450  /* Drop the last call and stop the conference */
3451  ast_verb(3, "Dropping three-way call on %s\n", ast_channel_name(p->subs[ANALOG_SUB_THREEWAY].owner));
3453  analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
3454  analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 0);
3455  } else {
3456  /* Lets see what we're up to */
3457  if (((ast_channel_pbx(ast)) || (ast_channel_state(ast) == AST_STATE_UP)) &&
3458  (p->transfertobusy || (ast_channel_state(ast) != AST_STATE_BUSY))) {
3459  ast_verb(3, "Building conference call with %s and %s\n",
3460  ast_channel_name(p->subs[ANALOG_SUB_THREEWAY].owner),
3461  ast_channel_name(p->subs[ANALOG_SUB_REAL].owner));
3462  /* Put them in the threeway, and flip */
3463  analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 1);
3464  analog_set_inthreeway(p, ANALOG_SUB_REAL, 1);
3465  analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
3466  orig_3way_sub = ANALOG_SUB_REAL;
3467  ast_queue_unhold(p->subs[orig_3way_sub].owner);
3468  analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
3469  } else {
3470  ast_verb(3, "Dumping incomplete call on %s\n", ast_channel_name(p->subs[ANALOG_SUB_THREEWAY].owner));
3471  analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
3472  orig_3way_sub = ANALOG_SUB_REAL;
3474  analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
3476  analog_set_echocanceller(p, 1);
3477  }
3478  }
3479  ast_channel_unlock(p->subs[orig_3way_sub].owner);
3480  }
3481 winkflashdone:
3482  analog_update_conf(p);
3483  break;
3484  case ANALOG_SIG_EM:
3485  case ANALOG_SIG_EM_E1:
3486  case ANALOG_SIG_FEATD:
3487  case ANALOG_SIG_SF:
3488  case ANALOG_SIG_SFWINK:
3489  case ANALOG_SIG_SF_FEATD:
3490  case ANALOG_SIG_FXSLS:
3491  case ANALOG_SIG_FXSGS:
3492  if (p->dialing) {
3493  ast_debug(1, "Ignoring wink on channel %d\n", p->channel);
3494  } else {
3495  ast_debug(1, "Got wink in weird state %u on channel %d\n", ast_channel_state(ast), p->channel);
3496  }
3497  break;
3498  case ANALOG_SIG_FEATDMF_TA:
3499  switch (p->whichwink) {
3500  case 0:
3501  ast_debug(1, "ANI2 set to '%d' and ANI is '%s'\n", ast_channel_caller(p->owner)->ani2,
3502  S_COR(ast_channel_caller(p->owner)->ani.number.valid,
3503  ast_channel_caller(p->owner)->ani.number.str, ""));
3504  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#",
3505  ast_channel_caller(p->owner)->ani2,
3506  S_COR(ast_channel_caller(p->owner)->ani.number.valid,
3507  ast_channel_caller(p->owner)->ani.number.str, ""));
3508  break;
3509  case 1:
3510  ast_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr));
3511  break;
3512  case 2:
3513  ast_log(LOG_WARNING, "Received unexpected wink on channel of type ANALOG_SIG_FEATDMF_TA\n");
3514  return NULL;
3515  }
3516  p->whichwink++;
3517  /* Fall through */
3518  case ANALOG_SIG_FEATDMF:
3519  case ANALOG_SIG_E911:
3520  case ANALOG_SIG_FGC_CAMAMF:
3521  case ANALOG_SIG_FGC_CAMA:
3522  case ANALOG_SIG_FEATB:
3523  case ANALOG_SIG_SF_FEATDMF:
3524  case ANALOG_SIG_SF_FEATB:
3525  case ANALOG_SIG_EMWINK:
3526  /* FGD MF and EMWINK *Must* wait for wink */
3527  if (!ast_strlen_zero(p->dop.dialstr)) {
3528  res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
3529  if (res) {
3530  p->dop.dialstr[0] = '\0';
3531  return NULL;
3532  } else {
3533  ast_debug(1, "Sent deferred digit string on channel %d: %s\n", p->channel, p->dop.dialstr);
3534  }
3535  }
3536  p->dop.dialstr[0] = '\0';
3537  break;
3538  default:
3539  ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
3540  }
3541  break;
3542  case ANALOG_EVENT_HOOKCOMPLETE:
3543  if (p->inalarm) break;
3544  if (analog_check_waitingfordt(p)) {
3545  break;
3546  }
3547  switch (mysig) {
3548  case ANALOG_SIG_FXSLS: /* only interesting for FXS */
3549  case ANALOG_SIG_FXSGS:
3550  case ANALOG_SIG_FXSKS:
3551  case ANALOG_SIG_EM:
3552  case ANALOG_SIG_EM_E1:
3553  case ANALOG_SIG_EMWINK:
3554  case ANALOG_SIG_FEATD:
3555  case ANALOG_SIG_SF:
3556  case ANALOG_SIG_SFWINK:
3557  case ANALOG_SIG_SF_FEATD:
3558  if (!ast_strlen_zero(p->dop.dialstr)) {
3559  res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
3560  if (res) {
3561  p->dop.dialstr[0] = '\0';
3562  return NULL;
3563  } else {
3564  ast_debug(1, "Sent deferred digit string on channel %d: %s\n", p->channel, p->dop.dialstr);
3565  }
3566  }
3567  p->dop.dialstr[0] = '\0';
3568  p->dop.op = ANALOG_DIAL_OP_REPLACE;
3569  break;
3570  case ANALOG_SIG_FEATDMF:
3571  case ANALOG_SIG_FEATDMF_TA:
3572  case ANALOG_SIG_E911:
3573  case ANALOG_SIG_FGC_CAMA:
3574  case ANALOG_SIG_FGC_CAMAMF:
3575  case ANALOG_SIG_FEATB:
3576  case ANALOG_SIG_SF_FEATDMF:
3577  case ANALOG_SIG_SF_FEATB:
3578  ast_debug(1, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
3579  break;
3580  default:
3581  break;
3582  }
3583  break;
3584  case ANALOG_EVENT_POLARITY:
3585  /*
3586  * If we get a Polarity Switch event, this could be
3587  * due to line seizure, remote end connect or remote end disconnect.
3588  *
3589  * Check to see if we should change the polarity state and
3590  * mark the channel as UP or if this is an indication
3591  * of remote end disconnect.
3592  */
3593 
3594  if (p->polarityonanswerdelay > 0) {
3595  /* check if event is not too soon after OffHook or Answer */
3596  if (ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
3597  switch (ast_channel_state(ast)) {
3598  case AST_STATE_DIALING: /*!< Digits (or equivalent) have been dialed */
3599  case AST_STATE_RINGING: /*!< Remote end is ringing */
3600  if (p->answeronpolarityswitch) {
3601  ast_debug(1, "Answering on polarity switch! channel %d\n", p->channel);
3603  p->polarity = POLARITY_REV;
3604  if (p->hanguponpolarityswitch) {
3605  p->polaritydelaytv = ast_tvnow();
3606  }
3607  } else {
3608  ast_debug(1, "Ignore Answer on polarity switch, channel %d\n", p->channel);
3609  }
3610  break;
3611 
3612  case AST_STATE_UP: /*!< Line is up */
3613  case AST_STATE_RING: /*!< Line is ringing */
3614  if (p->hanguponpolarityswitch) {
3615  ast_debug(1, "HangingUp on polarity switch! channel %d\n", p->channel);
3616  ast_queue_control_data(ast, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
3617  ast_channel_hangupcause_hash_set(ast, cause_code, data_size);
3619  p->polarity = POLARITY_IDLE;
3620  } else {
3621  ast_debug(1, "Ignore Hangup on polarity switch, channel %d\n", p->channel);
3622  }
3623  break;
3624 
3625  case AST_STATE_DOWN: /*!< Channel is down and available */
3626  case AST_STATE_RESERVED: /*!< Channel is down, but reserved */
3627  case AST_STATE_OFFHOOK: /*!< Channel is off hook */
3628  case AST_STATE_BUSY: /*!< Line is busy */
3629  case AST_STATE_DIALING_OFFHOOK: /*!< Digits (or equivalent) have been dialed while offhook */
3630  case AST_STATE_PRERING: /*!< Channel has detected an incoming call and is waiting for ring */
3631  default:
3632  if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
3633  ast_debug(1, "Ignoring Polarity switch on channel %d, state %u\n", p->channel, ast_channel_state(ast));
3634  }
3635  break;
3636  }
3637 
3638  } else {
3639  /* event is too soon after OffHook or Answer */
3640  switch (ast_channel_state(ast)) {
3641  case AST_STATE_DIALING: /*!< Digits (or equivalent) have been dialed */
3642  case AST_STATE_RINGING: /*!< Remote end is ringing */
3643  if (p->answeronpolarityswitch) {
3644  ast_debug(1, "Polarity switch detected but NOT answering (too close to OffHook event) on channel %d, state %u\n", p->channel, ast_channel_state(ast));
3645  }
3646  break;
3647 
3648  case AST_STATE_UP: /*!< Line is up */
3649  case AST_STATE_RING: /*!< Line is ringing */
3650  if (p->hanguponpolarityswitch) {
3651  ast_debug(1, "Polarity switch detected but NOT hanging up (too close to Answer event) on channel %d, state %u\n", p->channel, ast_channel_state(ast));
3652  }
3653  break;
3654 
3655  default:
3656  if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
3657  ast_debug(1, "Polarity switch detected (too close to previous event) on channel %d, state %u\n", p->channel, ast_channel_state(ast));
3658  }
3659  break;
3660  }
3661  }
3662  }
3663 
3664  /* Added more log_debug information below to provide a better indication of what is going on */
3665  ast_debug(1, "Polarity Reversal event occured - DEBUG 2: channel %d, state %u, pol= %d, aonp= %d, honp= %d, pdelay= %d, tv= %" PRIi64 "\n", p->channel, ast_channel_state(ast), p->polarity, p->answeronpolarityswitch, p->hanguponpolarityswitch, p->polarityonanswerdelay, ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) );
3666  break;
3667  default:
3668  ast_debug(1, "Dunno what to do with event %d on channel %d\n", res, p->channel);
3669  }
3670  return &p->subs[idx].f;
3671 }
3672 
3673 struct ast_frame *analog_exception(struct analog_pvt *p, struct ast_channel *ast)
3674 {
3675  int res;
3676  int idx;
3677  struct ast_frame *f;
3678 
3679  ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
3680 
3681  idx = analog_get_index(ast, p, 1);
3682  if (idx < 0) {
3683  idx = ANALOG_SUB_REAL;
3684  }
3685 
3686  p->subs[idx].f.frametype = AST_FRAME_NULL;
3687  p->subs[idx].f.datalen = 0;
3688  p->subs[idx].f.samples = 0;
3689  p->subs[idx].f.mallocd = 0;
3690  p->subs[idx].f.offset = 0;
3691  p->subs[idx].f.subclass.integer = 0;
3692  p->subs[idx].f.delivery = ast_tv(0,0);
3693  p->subs[idx].f.src = "dahdi_exception";
3694  p->subs[idx].f.data.ptr = NULL;
3695 
3696  if (!p->owner) {
3697  /* If nobody owns us, absorb the event appropriately, otherwise
3698  we loop indefinitely. This occurs when, during call waiting, the
3699  other end hangs up our channel so that it no longer exists, but we
3700  have neither FLASH'd nor ONHOOK'd to signify our desire to
3701  change to the other channel. */
3702  res = analog_get_event(p);
3703 
3704  /* Switch to real if there is one and this isn't something really silly... */
3705  if ((res != ANALOG_EVENT_RINGEROFF) && (res != ANALOG_EVENT_RINGERON) &&
3706  (res != ANALOG_EVENT_HOOKCOMPLETE)) {
3707  ast_debug(1, "Restoring owner of channel %d on event %d\n", p->channel, res);
3708  analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
3709  if (p->owner && ast != p->owner) {
3710  /*
3711  * Could this even happen?
3712  * Possible deadlock because we do not have the real-call lock.
3713  */
3714  ast_log(LOG_WARNING, "Event %s on %s is not restored owner %s\n",
3715  analog_event2str(res), ast_channel_name(ast), ast_channel_name(p->owner));
3716  }
3717  if (p->owner) {
3718  ast_queue_unhold(p->owner);
3719  }
3720  }
3721  switch (res) {
3722  case ANALOG_EVENT_ONHOOK:
3723  analog_set_echocanceller(p, 0);
3724  if (p->owner) {
3725  ast_verb(3, "Channel %s still has call, ringing phone\n", ast_channel_name(p->owner));
3726  analog_ring(p);
3727  analog_stop_callwait(p);
3728  } else {
3729  ast_log(LOG_WARNING, "Absorbed %s, but nobody is left!?!?\n",
3730  analog_event2str(res));
3731  }
3732  analog_update_conf(p);
3733  break;
3734  case ANALOG_EVENT_RINGOFFHOOK:
3735  analog_set_echocanceller(p, 1);
3736  analog_off_hook(p);
3737  if (p->owner && (ast_channel_state(p->owner) == AST_STATE_RINGING)) {
3739  analog_set_dialing(p, 0);
3740  }
3741  break;
3742  case ANALOG_EVENT_HOOKCOMPLETE:
3743  case ANALOG_EVENT_RINGERON:
3744  case ANALOG_EVENT_RINGEROFF:
3745  /* Do nothing */
3746  break;
3747  case ANALOG_EVENT_WINKFLASH:
3748  gettimeofday(&p->flashtime, NULL);
3749  if (p->owner) {
3750  ast_verb(3, "Channel %d flashed to other channel %s\n", p->channel, ast_channel_name(p->owner));
3751  if (ast_channel_state(p->owner) != AST_STATE_UP) {
3752  /* Answer if necessary */
3755  }
3756  analog_stop_callwait(p);
3757  ast_queue_unhold(p->owner);
3758  } else {
3759  ast_log(LOG_WARNING, "Absorbed %s, but nobody is left!?!?\n",
3760  analog_event2str(res));
3761  }
3762  analog_update_conf(p);
3763  break;
3764  default:
3765  ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", analog_event2str(res));
3766  break;
3767  }
3768  f = &p->subs[idx].f;
3769  return f;
3770  }
3771  ast_debug(1, "Exception on %d, channel %d\n", ast_channel_fd(ast, 0), p->channel);
3772  /* If it's not us, return NULL immediately */
3773  if (ast != p->owner) {
3774  ast_log(LOG_WARNING, "We're %s, not %s\n", ast_channel_name(ast), ast_channel_name(p->owner));
3775  f = &p->subs[idx].f;
3776  return f;
3777  }
3778 
3779  f = __analog_handle_event(p, ast);
3780  if (!f) {
3781  const char *name = ast_strdupa(ast_channel_name(ast));
3782 
3783  /* Tell the CDR this DAHDI device hung up */
3784  analog_unlock_private(p);
3785  ast_channel_unlock(ast);
3786  ast_set_hangupsource(ast, name, 0);
3787  ast_channel_lock(ast);
3788  analog_lock_private(p);
3789  }
3790  return f;
3791 }
3792 
3793 void *analog_handle_init_event(struct analog_pvt *i, int event)
3794 {
3795  int res;
3796  pthread_t threadid;
3797  struct ast_channel *chan;
3798  ast_callid callid = 0;
3799  int callid_created;
3800 
3801  ast_debug(1, "channel (%d) - signaling (%d) - event (%s)\n",
3802  i->channel, i->sig, analog_event2str(event));
3803 
3804  /* Handle an event on a given channel for the monitor thread. */
3805  switch (event) {
3806  case ANALOG_EVENT_WINKFLASH:
3807  case ANALOG_EVENT_RINGBEGIN:
3808  switch (i->sig) {
3809  case ANALOG_SIG_FXSLS:
3810  case ANALOG_SIG_FXSGS:
3811  case ANALOG_SIG_FXSKS:
3812  if (i->immediate) {
3813  if (i->use_callerid || i->usedistinctiveringdetection) {
3814  ast_log(LOG_WARNING, "Can't start PBX immediately, must wait for Caller ID / distinctive ring\n");
3815  } else {
3816  /* If we don't care about Caller ID or Distinctive Ring, then there's
3817  * no need to wait for anything before accepting the call, as
3818  * waiting will buy us nothing.
3819  * So if the channel is configured for immediate, actually start immediately
3820  * and get the show on the road as soon as possible. */
3821  ast_debug(1, "Disabling ring timeout (previously %d) to begin handling immediately\n", i->ringt_base);
3822  analog_set_ringtimeout(i, 0);
3823  }
3824  }
3825  break;
3826  default:
3827  break;
3828  }
3829  /* Fall through */
3830  if (!(ISTRUNK(i) && i->immediate && !i->use_callerid && !i->usedistinctiveringdetection)) {
3831  break;
3832  }
3833  case ANALOG_EVENT_RINGOFFHOOK:
3834  if (i->inalarm) {
3835  break;
3836  }
3837  /* Got a ring/answer. What kind of channel are we? */
3838  switch (i->sig) {
3839  case ANALOG_SIG_FXOLS:
3840  case ANALOG_SIG_FXOGS:
3841  case ANALOG_SIG_FXOKS:
3842  res = analog_off_hook(i);
3843  i->fxsoffhookstate = 1;
3844  i->cshactive = 0;
3845  if (res && (errno == EBUSY)) {
3846  break;
3847  }
3848  callid_created = ast_callid_threadstorage_auto(&callid);
3849 
3850  /* Cancel VMWI spill */
3851  analog_cancel_cidspill(i);
3852 
3853  if (i->immediate) {
3854  analog_set_echocanceller(i, 1);
3855  /* The channel is immediately up. Start right away */
3856  if (i->immediatering) {
3857  /* Play fake ringing, if we've been told to... */
3858  res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_RINGTONE);
3859  }
3860  chan = analog_new_ast_channel(i, AST_STATE_RING, 1, ANALOG_SUB_REAL, NULL);
3861  if (!chan) {
3862  ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
3863  res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
3864  if (res < 0) {
3865  ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
3866  }
3867  }
3868  } else {
3869  /* Check for callerid, digits, etc */
3870  chan = analog_new_ast_channel(i, AST_STATE_RESERVED, 0, ANALOG_SUB_REAL, NULL);
3871  i->ss_astchan = chan;
3872  if (chan) {
3873  if (analog_has_voicemail(i)) {
3874  res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_STUTTER);
3875  } else {
3876  res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_DIALTONE);
3877  }
3878  if (res < 0)
3879  ast_log(LOG_WARNING, "Unable to play dialtone on channel %d, do you have defaultzone and loadzone defined?\n", i->channel);
3880 
3881  if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
3882  ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
3883  res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
3884  if (res < 0) {
3885  ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
3886  }
3887  ast_hangup(chan);
3888  }
3889  } else
3890  ast_log(LOG_WARNING, "Unable to create channel\n");
3891  }
3892  ast_callid_threadstorage_auto_clean(callid, callid_created);
3893  break;
3894  case ANALOG_SIG_FXSLS:
3895  case ANALOG_SIG_FXSGS:
3896  case ANALOG_SIG_FXSKS:
3897  analog_set_ringtimeout(i, i->ringt_base);
3898  /* Fall through */
3899  case ANALOG_SIG_EMWINK:
3900  case ANALOG_SIG_FEATD:
3901  case ANALOG_SIG_FEATDMF:
3902  case ANALOG_SIG_FEATDMF_TA:
3903  case ANALOG_SIG_E911:
3904  case ANALOG_SIG_FGC_CAMA:
3905  case ANALOG_SIG_FGC_CAMAMF:
3906  case ANALOG_SIG_FEATB:
3907  case ANALOG_SIG_EM:
3908  case ANALOG_SIG_EM_E1:
3909  case ANALOG_SIG_SFWINK:
3910  case ANALOG_SIG_SF_FEATD:
3911  case ANALOG_SIG_SF_FEATDMF:
3912  case ANALOG_SIG_SF_FEATB:
3913  case ANALOG_SIG_SF:
3914  callid_created = ast_callid_threadstorage_auto(&callid);
3915  /* Check for callerid, digits, etc */
3916  if (i->cid_start == ANALOG_CID_START_POLARITY_IN || i->cid_start == ANALOG_CID_START_DTMF_NOALERT) {
3917  chan = analog_new_ast_channel(i, AST_STATE_PRERING, 0, ANALOG_SUB_REAL, NULL);
3918  } else {
3919  chan = analog_new_ast_channel(i, AST_STATE_RING, 0, ANALOG_SUB_REAL, NULL);
3920  }
3921  i->ss_astchan = chan;
3922  if (!chan) {
3923  ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
3924  } else if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
3925  ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
3926  res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
3927  if (res < 0) {
3928  ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
3929  }
3930  ast_hangup(chan);
3931  }
3932  ast_callid_threadstorage_auto_clean(callid, callid_created);
3933  break;
3934  default:
3935  ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", analog_sigtype_to_str(i->sig), i->channel);
3936  res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
3937  if (res < 0) {
3938  ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
3939  }
3940  return NULL;
3941  }
3942  break;
3943  case ANALOG_EVENT_NOALARM:
3944  analog_set_alarm(i, 0);
3945  analog_publish_channel_alarm_clear(i->channel);
3946  break;
3947  case ANALOG_EVENT_ALARM:
3948  analog_set_alarm(i, 1);
3949  analog_get_and_handle_alarms(i);
3950  /* fall thru intentionally */
3951  case ANALOG_EVENT_ONHOOK:
3952  /* Back on hook. Hang up. */
3953  switch (i->sig) {
3954  case ANALOG_SIG_FXOLS:
3955  case ANALOG_SIG_FXOGS:
3956  i->fxsoffhookstate = 0;
3957  analog_start_polarityswitch(i);
3958  /* Fall through */
3959  case ANALOG_SIG_FEATD:
3960  case ANALOG_SIG_FEATDMF:
3961  case ANALOG_SIG_FEATDMF_TA:
3962  case ANALOG_SIG_E911:
3963  case ANALOG_SIG_FGC_CAMA:
3964  case ANALOG_SIG_FGC_CAMAMF:
3965  case ANALOG_SIG_FEATB:
3966  case ANALOG_SIG_EM:
3967  case ANALOG_SIG_EM_E1:
3968  case ANALOG_SIG_EMWINK:
3969  case ANALOG_SIG_SF_FEATD:
3970  case ANALOG_SIG_SF_FEATDMF:
3971  case ANALOG_SIG_SF_FEATB:
3972  case ANALOG_SIG_SF:
3973  case ANALOG_SIG_SFWINK:
3974  case ANALOG_SIG_FXSLS:
3975  case ANALOG_SIG_FXSGS:
3976  case ANALOG_SIG_FXSKS:
3977  analog_set_echocanceller(i, 0);
3978  res = analog_play_tone(i, ANALOG_SUB_REAL, -1);
3979  analog_on_hook(i);
3980  break;
3981  case ANALOG_SIG_FXOKS:
3982  i->fxsoffhookstate = 0;
3983  analog_start_polarityswitch(i);
3984  analog_set_echocanceller(i, 0);
3985  /* Diddle the battery for the zhone */
3986 #ifdef ZHONE_HACK
3987  analog_off_hook(i);
3988  usleep(1);
3989 #endif
3990  res = analog_play_tone(i, ANALOG_SUB_REAL, -1);
3991  analog_on_hook(i);
3992  break;
3993  default:
3994  ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", analog_sigtype_to_str(i->sig), i->channel);
3995  res = analog_play_tone(i, ANALOG_SUB_REAL, -1);
3996  return NULL;
3997  }
3998  break;
3999  case ANALOG_EVENT_POLARITY:
4000  switch (i->sig) {
4001  case ANALOG_SIG_FXSLS:
4002  case ANALOG_SIG_FXSKS:
4003  case ANALOG_SIG_FXSGS:
4004  callid_created = ast_callid_threadstorage_auto(&callid);
4005  /* We have already got a PR before the channel was
4006  created, but it wasn't handled. We need polarity
4007  to be REV for remote hangup detection to work.
4008  At least in Spain */
4009  if (i->hanguponpolarityswitch) {
4010  i->polarity = POLARITY_REV;
4011  }
4012  if (i->cid_start == ANALOG_CID_START_POLARITY || i->cid_start == ANALOG_CID_START_POLARITY_IN) {
4013  i->polarity = POLARITY_REV;
4014  ast_verb(2, "Starting post polarity CID detection on channel %d\n",
4015  i->channel);
4016  chan = analog_new_ast_channel(i, AST_STATE_PRERING, 0, ANALOG_SUB_REAL, NULL);
4017  i->ss_astchan = chan;
4018  if (!chan) {
4019  ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
4020  } else if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
4021  ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
4022  ast_hangup(chan);
4023  }
4024  }
4025  ast_callid_threadstorage_auto_clean(callid, callid_created);
4026  break;
4027  default:
4028  ast_log(LOG_WARNING,
4029  "handle_init_event detected polarity reversal on non-FXO (ANALOG_SIG_FXS) interface %d\n",
4030  i->channel);
4031  break;
4032  }
4033  break;
4034  case ANALOG_EVENT_DTMFCID:
4035  switch (i->sig) {
4036  case ANALOG_SIG_FXSLS:
4037  case ANALOG_SIG_FXSKS:
4038  case ANALOG_SIG_FXSGS:
4039  callid_created = ast_callid_threadstorage_auto(&callid);
4040  if (i->cid_start == ANALOG_CID_START_DTMF_NOALERT) {
4041  ast_verb(2, "Starting DTMF CID detection on channel %d\n",
4042  i->channel);
4043  chan = analog_new_ast_channel(i, AST_STATE_PRERING, 0, ANALOG_SUB_REAL, NULL);
4044  i->ss_astchan = chan;
4045  if (!chan) {
4046  ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
4047  } else if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
4048  ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
4049  ast_hangup(chan);
4050  }
4051  }
4052  ast_callid_threadstorage_auto_clean(callid, callid_created);
4053  break;
4054  default:
4055  ast_log(LOG_WARNING,
4056  "handle_init_event detected dtmfcid generation event on non-FXO (ANALOG_SIG_FXS) interface %d\n",
4057  i->channel);
4058  break;
4059  }
4060  break;
4061  case ANALOG_EVENT_REMOVED: /* destroy channel, will actually do so in do_monitor */
4062  ast_log(LOG_NOTICE, "Got ANALOG_EVENT_REMOVED. Destroying channel %d\n",
4063  i->channel);
4064  return i->chan_pvt;
4065  case ANALOG_EVENT_NEONMWI_ACTIVE:
4066  analog_handle_notify_message(NULL, i, -1, ANALOG_EVENT_NEONMWI_ACTIVE);
4067  break;
4068  case ANALOG_EVENT_NEONMWI_INACTIVE:
4069  analog_handle_notify_message(NULL, i, -1, ANALOG_EVENT_NEONMWI_INACTIVE);
4070  break;
4071  }
4072  return NULL;
4073 }
4074 
4075 
4076 struct analog_pvt *analog_new(enum analog_sigtype signallingtype, void *private_data)
4077 {
4078  struct analog_pvt *p;
4079 
4080  p = ast_calloc(1, sizeof(*p));
4081  if (!p) {
4082  return p;
4083  }
4084 
4085  p->outsigmod = ANALOG_SIG_NONE;
4086  p->sig = signallingtype;
4087  p->chan_pvt = private_data;
4088 
4089  /* Some defaults for values */
4090  p->cid_start = ANALOG_CID_START_RING;
4091  p->cid_signalling = CID_SIG_BELL;
4092  /* Sub real is assumed to always be alloc'd */
4093  p->subs[ANALOG_SUB_REAL].allocd = 1;
4094 
4095  return p;
4096 }
4097 
4098 /*!
4099  * \brief Delete the analog private structure.
4100  * \since 1.8
4101  *
4102  * \param doomed Analog private structure to delete.
4103  */
4104 void analog_delete(struct analog_pvt *doomed)
4105 {
4106  ast_free(doomed);
4107 }
4108 
4109 int analog_config_complete(struct analog_pvt *p)
4110 {
4111  /* No call waiting on non FXS channels */
4112  if ((p->sig != ANALOG_SIG_FXOKS) && (p->sig != ANALOG_SIG_FXOLS) && (p->sig != ANALOG_SIG_FXOGS)) {
4113  p->permcallwaiting = 0;
4114  }
4115 
4116  analog_set_callwaiting(p, p->permcallwaiting);
4117 
4118  return 0;
4119 }
4120 
4121 void analog_free(struct analog_pvt *p)
4122 {
4123  ast_free(p);
4124 }
4125 
4126 /* called while dahdi_pvt is locked in dahdi_fixup */
4127 int analog_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, void *newp)
4128 {
4129  struct analog_pvt *new_pvt = newp;
4130  int x;
4131  ast_debug(1, "New owner for channel %d is %s\n", new_pvt->channel, ast_channel_name(newchan));
4132  if (new_pvt->owner == oldchan) {
4133  analog_set_new_owner(new_pvt, newchan);
4134  }
4135  for (x = 0; x < 3; x++) {
4136  if (new_pvt->subs[x].owner == oldchan) {
4137  new_pvt->subs[x].owner = newchan;
4138  }
4139  }
4140 
4141  analog_update_conf(new_pvt);
4142  return 0;
4143 }
4144 
4145 static void analog_publish_dnd_state(int channel, const char *status)
4146 {
4147  RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
4148  RAII_VAR(struct ast_str *, dahdichan, ast_str_create(32), ast_free);
4149  if (!dahdichan) {
4150  return;
4151  }
4152 
4153  ast_str_set(&dahdichan, 0, "DAHDI/%d", channel);
4154 
4155  body = ast_json_pack("{s: s, s: s}",
4156  "Channel", ast_str_buffer(dahdichan),
4157  "Status", status);
4158  if (!body) {
4159  return;
4160  }
4161 
4162  ast_manager_publish_event("DNDState", EVENT_FLAG_SYSTEM, body);
4163 }
4164 
4165 int analog_dnd(struct analog_pvt *p, int flag)
4166 {
4167  if (flag == -1) {
4168  return p->dnd;
4169  }
4170 
4171  p->dnd = flag;
4172 
4173  ast_verb(3, "%s DND on channel %d\n",
4174  flag ? "Enabled" : "Disabled",
4175  p->channel);
4176  analog_publish_dnd_state(p->channel, flag ? "enabled" : "disabled");
4177 
4178  return 0;
4179 }
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1574
enum analog_dialmode dialmode
Definition: sig_analog.h:321
int presentation
Q.931 encoded presentation-indicator encoded field.
Definition: channel.h:277
int(*const on_hook)(void *pvt)
Set channel on hook.
Definition: sig_analog.h:165
#define POLARITY_IDLE
Definition: sig_analog.c:62
void ast_set_callerid(struct ast_channel *chan, const char *cid_num, const char *cid_name, const char *cid_ani)
Set caller ID number, name and ANI and generate AMI event.
Definition: channel.c:7334
int ast_matchmore_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Looks to see if adding anything to this extension might match something. (exists ^ canmatch) ...
Definition: pbx.c:4195
unsigned int use_callerid
Definition: sig_analog.h:307
Main Channel structure associated with a channel.
struct ast_smdi_interface * smdi_iface
The SMDI interface to get SMDI messages from.
Definition: sig_analog.h:315
char * str
Subscriber phone number (Malloced)
Definition: channel.h:291
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1293
void(*const answer_polarityswitch)(void *pvt)
Switch FXS line polarity, based on answeronpolarityswitch=yes.
Definition: sig_analog.h:174
void(*const start_polarityswitch)(void *pvt)
Reset FXS line polarity to IDLE, based on answeronpolarityswitch and hanguponpolarityswitch.
Definition: sig_analog.h:172
Asterisk main include file. File version handling, generic pbx functions.
enum ast_transfer_result ast_bridge_transfer_attended(struct ast_channel *to_transferee, struct ast_channel *to_transfer_target)
Attended transfer.
Definition: bridge.c:4677
char chan_name[AST_CHANNEL_NAME]
struct ast_flags flags
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame without payload.
Definition: channel.c:1231
struct ast_party_caller caller
Channel Caller ID information.
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
int presentation
Q.931 presentation-indicator and screening-indicator encoded fields.
Definition: channel.h:295
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
Call Parking API.
struct ast_party_id id
Connected party ID.
Definition: channel.h:458
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2958
static struct ast_frame * __analog_handle_event(struct analog_pvt *p, struct ast_channel *ast)
Definition: sig_analog.c:2773
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_party_name name
Subscriber name.
Definition: channel.h:340
struct ast_party_id from
Who is redirecting the call (Sent to the party the call is redirected toward)
Definition: channel.h:527
int ast_queue_unhold(struct ast_channel *chan)
Queue an unhold frame.
Definition: channel.c:1216
int ast_softhangup(struct ast_channel *chan, int cause)
Softly hangup up a channel.
Definition: channel.c:2471
Call Pickup API.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
void(*const hangup_polarityswitch)(void *pvt)
Switch FXS line polarity, based on answeronpolarityswitch and hanguponpolarityswitch.
Definition: sig_analog.h:176
unsigned int cshactive
Definition: sig_analog.h:334
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4257
int ast_say_digit_str(struct ast_channel *chan, const char *num, const char *ints, const char *lang)
says digits of a string
Definition: channel.c:8259
struct analog_callback analog_callbacks
Definition: chan_dahdi.c:3616
int ast_ignore_pattern(const char *context, const char *pattern)
Checks to see if a number should be ignored.
Definition: pbx.c:6879
int cid_signalling
Definition: sig_analog.h:323
time_t guardtime
Definition: sig_analog.h:363
ast_channel_state
ast_channel states
Definition: channelstate.h:35
char * str
Subscriber name (Malloced)
Definition: channel.h:264
Definition: astman.c:222
#define ANALOG_MATCH_DIGIT_TIMEOUT
Default time (ms) to wait, in case of ambiguous match.
Definition: sig_analog.h:42
int ast_callid_threadassoc_add(ast_callid callid)
Adds a known callid to thread storage of the calling thread.
Definition: logger.c:2320
unsigned int dialing
Definition: sig_analog.h:336
int ast_parking_blind_transfer_park(struct ast_bridge_channel *parker, const char *context, const char *exten, transfer_channel_cb parked_channel_cb, struct transfer_channel_data *parked_channel_data)
Perform a blind transfer to a parking extension.
Definition: parking.c:143
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
unsigned int transfertobusy
Definition: sig_analog.h:306
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define AST_OPTION_TDD
struct timeval flashtime
Definition: sig_analog.h:364
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
int ast_queue_cc_frame(struct ast_channel *chan, const char *const monitor_type, const char *const dialstring, enum ast_cc_service_type service, void *private_data)
Queue an AST_CONTROL_CC frame.
Definition: ccss.c:4114
ast_transfer_result
Definition: bridge.h:1098
int ast_channel_setoption(struct ast_channel *channel, int option, void *data, int datalen, int block)
Sets an option on a channel.
Definition: channel.c:7422
int code
enum AST_REDIRECTING_REASON value for redirection
Definition: channel.h:510
struct ast_smdi_md_message * ast_smdi_md_message_wait(struct ast_smdi_interface *iface, int timeout)
Get the next SMDI message from the queue.
Definition: res_smdi.c:539
int ast_canmatch_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Looks for a valid matching extension.
Definition: pbx.c:4190
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
struct ast_frame_subclass subclass
struct ast_cc_config_params * ast_channel_get_cc_config_params(struct ast_channel *chan)
Get the CCSS parameters from a channel.
Definition: channel.c:10474
unsigned int use_smdi
TRUE if SMDI (Simplified Message Desk Interface) is enabled.
Definition: sig_analog.h:313
Utility functions.
int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
Queue a hangup frame with hangupcause set.
Definition: channel.c:1166
void(*const set_polarity)(void *pvt, int value)
Set FXS line polarity to 0=IDLE NZ=REVERSED.
Definition: sig_analog.h:170
enum ast_cc_monitor_policies ast_get_cc_monitor_policy(struct ast_cc_config_params *config)
Get the cc_monitor_policy.
Definition: ccss.c:876
struct ast_channel * owner
Definition: sig_analog.h:277
Number structure.
Definition: app_followme.c:154
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
struct ast_party_id id
Caller party ID.
Definition: channel.h:420
struct ast_party_id ani
Automatic Number Identification (ANI)
Definition: channel.h:427
void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
Set the source of the hangup in this channel and it's bridge.
Definition: channel.c:2499
const char * src
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
void ast_callid_threadstorage_auto_clean(ast_callid callid, int callid_created)
Use in conjunction with ast_callid_threadstorage_auto. Cleans up the references and if the callid was...
Definition: logger.c:2378
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define AST_MAX_EXTENSION
Definition: channel.h:134
Caller Party information.
Definition: channel.h:418
#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:87
unsigned int immediatering
Definition: sig_analog.h:299
void(*const swap_subs)(void *pvt, enum analog_sub a, struct ast_channel *new_a_owner, enum analog_sub b, struct ast_channel *new_b_owner)
Definition: sig_analog.h:206
#define ast_debug(level,...)
Log a DEBUG message.
analog_sub
Definition: sig_analog.h:108
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:4175
An SMDI message desk message.
Definition: smdi.h:65
int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
Queue a hold frame.
Definition: channel.c:1191
Core PBX routines and definitions.
#define AST_CC_GENERIC_MONITOR_TYPE
Definition: ccss.h:455
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
int ast_pickup_call(struct ast_channel *chan)
Pickup a call.
Definition: pickup.c:199
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: utils.c:2199
Support for dynamic strings.
Definition: strings.h:623
int ast_parking_is_exten_park(const char *context, const char *exten)
Determine if the context/exten is a "parking" extension.
Definition: parking.c:179
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
Definition: utils.c:2281
int ani2
Automatic Number Identification 2 (Info Digits)
Definition: channel.h:433
unsigned int callwaiting
Definition: sig_analog.h:333
void analog_delete(struct analog_pvt *doomed)
Delete the analog private structure.
Definition: sig_analog.c:4104
int(*const off_hook)(void *pvt)
Set channel off hook.
Definition: sig_analog.h:167
char * origcid_num
Definition: sig_analog.h:367
int ast_parking_provider_registered(void)
Check whether a parking provider is registered.
Definition: parking.c:241
union ast_frame::@224 data
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
int whichwink
Definition: sig_analog.h:365
#define AST_CHANNEL_NAME
Definition: channel.h:171
unsigned int dahditrcallerid
Definition: sig_analog.h:296
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2541
int redirecting_reason
Definition: sig_analog.h:356
int msgstate
-1 = unknown, 0 = no messages, 1 = new messages available
Definition: sig_analog.h:284
char * origcid_name
Definition: sig_analog.h:368
unsigned int permhidecallerid
Definition: sig_analog.h:301
struct ast_party_redirecting_reason reason
Reason for the redirection.
Definition: channel.h:542
unsigned int dnd
Definition: sig_analog.h:337
struct ast_bridge_channel * ast_channel_get_bridge_channel(struct ast_channel *chan)
Get a reference to the channel's bridge pointer.
Definition: channel.c:10582
struct timeval delivery
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
struct ast_frame ast_null_frame
Definition: main/frame.c:79
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3175
int ast_callid_threadstorage_auto(ast_callid *callid)
Checks thread storage for a callid and stores a reference if it exists. If not, then a new one will b...
Definition: logger.c:2356
#define ANALOG_FIRST_DIGIT_TIMEOUT
Default time (ms) to detect first digit.
Definition: sig_analog.h:38
Structure that contains information regarding a channel in a bridge.
int cidrings
Definition: sig_analog.h:358
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2947
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3162
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
unsigned int callwaitingcallerid
Definition: sig_analog.h:309
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:235
unsigned int call_qualifier
Definition: sig_analog.h:349
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1840
int ast_setstate(struct ast_channel *chan, enum ast_channel_state)
Change the state of a channel.
Definition: channel.c:7386
int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control, const void *data, size_t datalen)
Queue a control frame with payload.
Definition: channel.c:1238
Interface header for analog signaling module.
enum ast_pbx_result ast_pbx_run(struct ast_channel *c)
Execute the PBX in the current thread.
Definition: pbx.c:4755
#define MIN_MS_SINCE_FLASH
Definition: sig_analog.c:64
Data structure associated with a single frame of data.
Internal Asterisk hangup causes.
#define ANALOG_INTER_DIGIT_TIMEOUT
Default time (ms) to detect following digits.
Definition: sig_analog.h:40
Abstract JSON element (object, array, string, int, ...).
Options provided by main asterisk program.
unsigned int threewaysilenthold
Definition: sig_analog.h:304
#define AST_OPTION_TONE_VERIFY
unsigned int callwaitcas
TRUE if Call Waiting (CW) CPE Alert Signal (CAS) is being sent.
Definition: sig_analog.h:348
struct ast_frame f
Definition: sig_analog.h:265
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:342
enum ast_frame_type frametype
unsigned char valid
TRUE if the name information is valid/present.
Definition: channel.h:279
Call Parking and Pickup API Includes code and algorithms from the Zapata library. ...
struct analog_subchannel subs[3]
Definition: sig_analog.h:279
void callerid_get_dtmf(char *cidstring, char *number, int *flags)
Get and parse DTMF-based callerid.
Definition: callerid.c:211
int ast_softhangup_nolock(struct ast_channel *chan, int cause)
Softly hangup up a channel (no channel lock)
Definition: channel.c:2458
void ast_shrink_phone_number(char *n)
Shrink a phone number in place to just digits (more accurately it just removes ()'s, .'s, and -'s...
Definition: callerid.c:1101
Say numbers and dates (maybe words one day too)
Bridging API.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
Persistent data storage (akin to *doze registry)
unsigned char valid
TRUE if the number information is valid/present.
Definition: channel.h:297
void ast_channel_hangupcause_hash_set(struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
Sets the HANGUPCAUSE hash and optionally the SIP_CAUSE hash on the given channel. ...
Definition: channel.c:4346
unsigned int calledsubscriberheld
Definition: sig_analog.h:292
unsigned int permcallwaiting
Definition: sig_analog.h:300
void ast_manager_publish_event(const char *type, int class_type, struct ast_json *obj)
Publish an event to AMI.
Definition: manager.c:2063
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
Configuration relating to call pickup.
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:342