Asterisk - The Open Source Telephony Project  GIT-master-a24979a
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"
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 {
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 
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 {
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 {
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 {
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 {
190  }
191  return "";
192 }
193 
194 static int analog_get_event(struct analog_pvt *p)
195 {
198  }
199  return -1;
200 }
201 
202 static int analog_wait_event(struct analog_pvt *p)
203 {
206  }
207  return -1;
208 }
209 
211 {
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 
231 
232 #undef gen_analog_field_callback
233 
235 {
236  if (!strcasecmp(value, "ring")) {
237  return ANALOG_CID_START_RING;
238  } else if (!strcasecmp(value, "polarity")) {
240  } else if (!strcasecmp(value, "polarity_in")) {
242  } else if (!strcasecmp(value, "dtmf")) {
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) {
253  return "Ring";
255  return "Polarity";
257  return "Polarity_In";
259  return "DTMF";
260  }
261 
262  return "Unknown";
263 }
264 
266 {
267  char *res;
268  switch (event) {
269  case ANALOG_EVENT_ONHOOK:
270  res = "ANALOG_EVENT_ONHOOK";
271  break;
273  res = "ANALOG_EVENT_RINGOFFHOOK";
274  break;
276  res = "ANALOG_EVENT_WINKFLASH";
277  break;
278  case ANALOG_EVENT_ALARM:
279  res = "ANALOG_EVENT_ALARM";
280  break;
282  res = "ANALOG_EVENT_NOALARM";
283  break;
285  res = "ANALOG_EVENT_DIALCOMPLETE";
286  break;
288  res = "ANALOG_EVENT_HOOKCOMPLETE";
289  break;
291  res = "ANALOG_EVENT_PULSE_START";
292  break;
294  res = "ANALOG_EVENT_POLARITY";
295  break;
297  res = "ANALOG_EVENT_RINGBEGIN";
298  break;
300  res = "ANALOG_EVENT_EC_DISABLED";
301  break;
303  res = "ANALOG_EVENT_RINGERON";
304  break;
306  res = "ANALOG_EVENT_RINGEROFF";
307  break;
309  res = "ANALOG_EVENT_REMOVED";
310  break;
312  res = "ANALOG_EVENT_NEONMWI_ACTIVE";
313  break;
315  res = "ANALOG_EVENT_NEONMWI_INACTIVE";
316  break;
317 #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
319  res = "ANALOG_EVENT_TX_CED_DETECTED";
320  break;
322  res = "ANALOG_EVENT_RX_CED_DETECTED";
323  break;
325  res = "ANALOG_EVENT_EC_NLP_DISABLED";
326  break;
328  res = "ANALOG_EVENT_EC_NLP_ENABLED";
329  break;
330 #endif
332  res = "ANALOG_EVENT_PULSEDIGIT";
333  break;
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 
365  }
366 }
367 
368 static int analog_alloc_sub(struct analog_pvt *p, enum analog_sub x)
369 {
371  int res;
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;
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 
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) {
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 
429 {
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 {
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;
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 
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) {
469  }
470  return c;
471 }
472 
473 static int analog_set_echocanceller(struct analog_pvt *p, int enable)
474 {
476  return analog_callbacks.set_echocanceller(p->chan_pvt, enable);
477  }
478  return -1;
479 }
480 
482 {
485  }
486  return -1;
487 }
488 
489 static int analog_is_off_hook(struct analog_pvt *p)
490 {
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 {
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;
541  analog_callbacks.set_outgoing(p->chan_pvt, is_outgoing);
542  }
543 }
544 
546 {
549  }
550  return -1;
551 }
552 
554 {
557  }
558 }
559 
560 static void analog_unlock_private(struct analog_pvt *p)
561 {
564  }
565 }
566 
567 static void analog_lock_private(struct analog_pvt *p)
568 {
571  }
572 }
573 
575 {
578  } else {
579  /* Fallback to manual avoidance if callback not present. */
581  usleep(1);
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 */
612  }
613 }
614 
615 static int analog_off_hook(struct analog_pvt *p)
616 {
619  }
620  return -1;
621 }
622 
623 static void analog_set_needringing(struct analog_pvt *p, int value)
624 {
627  }
628 }
629 
630 #if 0
631 static void analog_set_polarity(struct analog_pvt *p, int value)
632 {
635  }
636 }
637 #endif
638 
640 {
643  }
644 }
646 {
649  }
650 }
651 
653 {
656  }
657 }
658 
660 {
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 {
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 {
686  }
687  return -1;
688 }
689 
690 static int analog_is_dialing(struct analog_pvt *p, enum analog_sub index)
691 {
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);
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);
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) {
754  }
755  needconf++;
756  } else {
759  }
760  }
761  }
762  ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
763 
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) {
779  ast_log(LOG_ERROR, "Unable to alloc subchannel\n");
780  return NULL;
781  }
782  }
783 
784  analog_set_outgoing(p, 1);
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 
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 no owner definitely available */
809  if (!p->owner) {
810  offhook = analog_is_off_hook(p);
811 
812  /* TDM FXO card, "onhook" means out of service (no battery on the line) */
813  if ((p->sig == ANALOG_SIG_FXSLS) || (p->sig == ANALOG_SIG_FXSKS) || (p->sig == ANALOG_SIG_FXSGS)) {
814 #ifdef DAHDI_CHECK_HOOKSTATE
815  if (offhook) {
816  return 1;
817  }
818  return 0;
819 #endif
820  /* TDM FXS card, "offhook" means someone took the hook off so it's unavailable! */
821  } else if (offhook) {
822  ast_debug(1, "Channel %d off hook, can't use\n", p->channel);
823  /* Not available when the other end is off hook */
824  return 0;
825  }
826  return 1;
827  }
828 
829  /* If it's not an FXO, forget about call wait */
830  if ((p->sig != ANALOG_SIG_FXOKS) && (p->sig != ANALOG_SIG_FXOLS) && (p->sig != ANALOG_SIG_FXOGS)) {
831  return 0;
832  }
833 
834  if (!p->callwaiting) {
835  /* If they don't have call waiting enabled, then for sure they're unavailable at this point */
836  return 0;
837  }
838 
839  if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
840  /* If there is already a call waiting call, then we can't take a second one */
841  return 0;
842  }
843 
844  if ((ast_channel_state(p->owner) != AST_STATE_UP) &&
846  /* If the current call is not up, then don't allow the call */
847  return 0;
848  }
850  /* Can't take a call wait when the three way calling hasn't been merged yet. */
851  return 0;
852  }
853  /* We're cool */
854  return 1;
855 }
856 
857 static int analog_stop_callwait(struct analog_pvt *p)
858 {
859  p->callwaitcas = 0;
862  }
863  return 0;
864 }
865 
866 static int analog_callwait(struct analog_pvt *p)
867 {
871  }
872  return 0;
873 }
874 
875 static void analog_set_callwaiting(struct analog_pvt *p, int callwaiting_enable)
876 {
877  p->callwaiting = callwaiting_enable;
879  analog_callbacks.set_callwaiting(p->chan_pvt, callwaiting_enable);
880  }
881 }
882 
883 static void analog_set_cadence(struct analog_pvt *p, struct ast_channel *chan)
884 {
887  }
888 }
889 
890 static void analog_set_dialing(struct analog_pvt *p, int is_dialing)
891 {
892  p->dialing = is_dialing;
894  analog_callbacks.set_dialing(p->chan_pvt, is_dialing);
895  }
896 }
897 
898 static void analog_set_alarm(struct analog_pvt *p, int in_alarm)
899 {
900  p->inalarm = in_alarm;
902  analog_callbacks.set_alarm(p->chan_pvt, in_alarm);
903  }
904 }
905 
906 static void analog_set_ringtimeout(struct analog_pvt *p, int ringt)
907 {
908  p->ringt = ringt;
911  }
912 }
913 
914 static void analog_set_waitingfordt(struct analog_pvt *p, struct ast_channel *ast)
915 {
918  }
919 }
920 
922 {
925  }
926 
927  return 0;
928 }
929 
930 static void analog_set_confirmanswer(struct analog_pvt *p, int flag)
931 {
934  }
935 }
936 
938 {
941  }
942 
943  return 0;
944 }
945 
946 static void analog_cancel_cidspill(struct analog_pvt *p)
947 {
950  }
951 }
952 
953 static int analog_confmute(struct analog_pvt *p, int mute)
954 {
957  }
958  return 0;
959 }
960 
961 static void analog_set_pulsedial(struct analog_pvt *p, int flag)
962 {
965  }
966 }
967 
968 static int analog_set_linear_mode(struct analog_pvt *p, enum analog_sub sub, int linear_mode)
969 {
971  /* Return provides old linear_mode setting or error indication */
972  return analog_callbacks.set_linear_mode(p->chan_pvt, sub, linear_mode);
973  }
974  return -1;
975 }
976 
977 static void analog_set_inthreeway(struct analog_pvt *p, enum analog_sub sub, int inthreeway)
978 {
979  p->subs[sub].inthreeway = inthreeway;
981  analog_callbacks.set_inthreeway(p->chan_pvt, sub, inthreeway);
982  }
983 }
984 
985 int analog_call(struct analog_pvt *p, struct ast_channel *ast, const char *rdest, int timeout)
986 {
987  int res, idx, mysig;
988  char *c, *n, *l;
989  char dest[256]; /* must be same length as p->dialdest */
990 
991  ast_debug(1, "CALLING CID_NAME: %s CID_NUM:: %s\n",
992  S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, ""),
993  S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, ""));
994 
995  ast_copy_string(dest, rdest, sizeof(dest));
996  ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
997 
998  if ((ast_channel_state(ast) == AST_STATE_BUSY)) {
1000  return 0;
1001  }
1002 
1004  ast_log(LOG_WARNING, "analog_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
1005  return -1;
1006  }
1007 
1008  p->dialednone = 0;
1009  analog_set_outgoing(p, 1);
1010 
1011  mysig = p->sig;
1012  if (p->outsigmod > -1) {
1013  mysig = p->outsigmod;
1014  }
1015 
1016  switch (mysig) {
1017  case ANALOG_SIG_FXOLS:
1018  case ANALOG_SIG_FXOGS:
1019  case ANALOG_SIG_FXOKS:
1020  if (p->owner == ast) {
1021  /* Normal ring, on hook */
1022 
1023  /* Don't send audio while on hook, until the call is answered */
1024  analog_set_dialing(p, 1);
1025  analog_set_cadence(p, ast); /* and set p->cidrings */
1026 
1027  /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1028  c = strchr(dest, '/');
1029  if (c) {
1030  c++;
1031  }
1032  if (c && (strlen(c) < p->stripmsd)) {
1033  ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1034  c = NULL;
1035  }
1036  if (c && (strlen(c) > sizeof(p->dop.dialstr) - 3 /* "Tw\0" */)) {
1037  ast_log(LOG_WARNING, "Number '%s' is longer than %d bytes\n", c, (int)sizeof(p->dop.dialstr) - 2);
1038  c = NULL;
1039  }
1040  if (c) {
1042  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1043  ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
1044  } else {
1045  p->dop.dialstr[0] = '\0';
1046  }
1047 
1048  if (analog_ring(p)) {
1049  ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1050  return -1;
1051  }
1052  analog_set_dialing(p, 1);
1053  } else {
1054  /* Call waiting call */
1055  if (ast_channel_connected(ast)->id.number.valid && ast_channel_connected(ast)->id.number.str) {
1057  } else {
1058  p->callwait_num[0] = '\0';
1059  }
1060  if (ast_channel_connected(ast)->id.name.valid && ast_channel_connected(ast)->id.name.str) {
1062  } else {
1063  p->callwait_name[0] = '\0';
1064  }
1065 
1066  /* Call waiting tone instead */
1067  if (analog_callwait(p)) {
1068  return -1;
1069  }
1070  /* Make ring-back */
1072  ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast_channel_name(ast));
1073  }
1074 
1075  }
1078  if (l) {
1079  ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
1080  } else {
1081  p->lastcid_num[0] = '\0';
1082  }
1083  if (n) {
1084  ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
1085  } else {
1086  p->lastcid_name[0] = '\0';
1087  }
1088 
1089  if (p->use_callerid) {
1090  p->caller.id.name.str = p->lastcid_name;
1091  p->caller.id.number.str = p->lastcid_num;
1092  }
1093 
1095  idx = analog_get_index(ast, p, 0);
1096  if (idx > -1) {
1097  struct ast_cc_config_params *cc_params;
1098 
1099  /* This is where the initial ringing frame is queued for an analog call.
1100  * As such, this is a great time to offer CCNR to the caller if it's available.
1101  */
1102  cc_params = ast_channel_get_cc_config_params(p->subs[idx].owner);
1103  if (cc_params) {
1104  switch (ast_get_cc_monitor_policy(cc_params)) {
1105  case AST_CC_MONITOR_NEVER:
1106  break;
1107  case AST_CC_MONITOR_NATIVE:
1108  case AST_CC_MONITOR_ALWAYS:
1112  break;
1113  }
1114  }
1116  }
1117  break;
1118  case ANALOG_SIG_FXSLS:
1119  case ANALOG_SIG_FXSGS:
1120  case ANALOG_SIG_FXSKS:
1122  ast_debug(1, "Ignore possible polarity reversal on line seizure\n");
1123  p->polaritydelaytv = ast_tvnow();
1124  }
1125  /* fall through */
1126  case ANALOG_SIG_EMWINK:
1127  case ANALOG_SIG_EM:
1128  case ANALOG_SIG_EM_E1:
1129  case ANALOG_SIG_FEATD:
1130  case ANALOG_SIG_FEATDMF:
1131  case ANALOG_SIG_E911:
1132  case ANALOG_SIG_FGC_CAMA:
1133  case ANALOG_SIG_FGC_CAMAMF:
1134  case ANALOG_SIG_FEATB:
1135  case ANALOG_SIG_SFWINK:
1136  case ANALOG_SIG_SF:
1137  case ANALOG_SIG_SF_FEATD:
1138  case ANALOG_SIG_SF_FEATDMF:
1139  case ANALOG_SIG_FEATDMF_TA:
1140  case ANALOG_SIG_SF_FEATB:
1141  c = strchr(dest, '/');
1142  if (c) {
1143  c++;
1144  } else {
1145  c = "";
1146  }
1147  if (strlen(c) < p->stripmsd) {
1148  ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1149  return -1;
1150  }
1151  res = analog_start(p);
1152  if (res < 0) {
1153  if (errno != EINPROGRESS) {
1154  return -1;
1155  }
1156  }
1157  ast_debug(1, "Dialing '%s'\n", c);
1159 
1160  c += p->stripmsd;
1161 
1162  switch (mysig) {
1163  case ANALOG_SIG_FEATD:
1165  if (l) {
1166  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
1167  } else {
1168  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
1169  }
1170  break;
1171  case ANALOG_SIG_FEATDMF:
1173  if (l) {
1174  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
1175  } else {
1176  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
1177  }
1178  break;
1179  case ANALOG_SIG_FEATDMF_TA:
1180  {
1181  const char *cic = "", *ozz = "";
1182 
1183  /* If you have to go through a Tandem Access point you need to use this */
1184 #ifndef STANDALONE
1185  ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
1186  if (!ozz) {
1187  ozz = analog_defaultozz;
1188  }
1189  cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
1190  if (!cic) {
1191  cic = analog_defaultcic;
1192  }
1193 #endif
1194  if (!ozz || !cic) {
1195  ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
1196  return -1;
1197  }
1198  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
1199  snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
1200  p->whichwink = 0;
1201  }
1202  break;
1203  case ANALOG_SIG_E911:
1204  ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
1205  break;
1206  case ANALOG_SIG_FGC_CAMA:
1207  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
1208  break;
1209  case ANALOG_SIG_FGC_CAMAMF:
1210  case ANALOG_SIG_FEATB:
1211  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
1212  break;
1213  default:
1214  if (p->pulse) {
1215  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
1216  } else {
1217  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
1218  }
1219  break;
1220  }
1221 
1222  if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1223  memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1224  strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1225  p->echorest[sizeof(p->echorest) - 1] = '\0';
1226  p->echobreak = 1;
1227  p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1228  } else {
1229  p->echobreak = 0;
1230  }
1231  analog_set_waitingfordt(p, ast);
1232  if (!res) {
1233  if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
1234  analog_on_hook(p);
1235  return -1;
1236  }
1237  } else {
1238  ast_debug(1, "Deferring dialing...\n");
1239  }
1240  analog_set_dialing(p, 1);
1241  if (ast_strlen_zero(c)) {
1242  p->dialednone = 1;
1243  }
1245  break;
1246  default:
1247  ast_debug(1, "not yet implemented\n");
1248  return -1;
1249  }
1250  return 0;
1251 }
1252 
1253 int analog_hangup(struct analog_pvt *p, struct ast_channel *ast)
1254 {
1255  int res;
1256  int idx, x;
1257 
1258  ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1259  if (!ast_channel_tech_pvt(ast)) {
1260  ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1261  return 0;
1262  }
1263 
1264  idx = analog_get_index(ast, p, 1);
1265 
1266  x = 0;
1267  if (p->origcid_num) {
1268  ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
1269  ast_free(p->origcid_num);
1270  p->origcid_num = NULL;
1271  }
1272  if (p->origcid_name) {
1273  ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
1274  ast_free(p->origcid_name);
1275  p->origcid_name = NULL;
1276  }
1277 
1279 
1280  ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1282  if (idx > -1) {
1283  /* Real channel, do some fixup */
1284  p->subs[idx].owner = NULL;
1285  p->polarity = POLARITY_IDLE;
1286  analog_set_linear_mode(p, idx, 0);
1287  switch (idx) {
1288  case ANALOG_SUB_REAL:
1290  ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
1292  /* We had flipped over to answer a callwait and now it's gone */
1293  ast_debug(1, "We were flipped over to the callwait, moving back and not owning.\n");
1294  /* Move to the call-wait, but un-own us until they flip back. */
1298  } else {
1299  /* The three way hung up, but we still have a call wait */
1300  ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
1303  if (p->subs[ANALOG_SUB_REAL].inthreeway) {
1304  /* This was part of a three way call. Immediately make way for
1305  another call */
1306  ast_debug(1, "Call was complete, setting owner to former third call\n");
1309  } else {
1310  /* This call hasn't been completed yet... Set owner to NULL */
1311  ast_debug(1, "Call was incomplete, setting owner to NULL\n");
1313  }
1314  }
1315  } else if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
1316  /* Need to hold the lock for real-call, private, and call-waiting call */
1318  if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
1319  /* The call waiting call dissappeared. */
1321  break;
1322  }
1323 
1324  /* Move to the call-wait and switch back to them. */
1328  if (ast_channel_state(p->owner) != AST_STATE_UP) {
1330  }
1332  /* Unlock the call-waiting call that we swapped to real-call. */
1334  } else if (p->subs[ANALOG_SUB_THREEWAY].allocd) {
1337  if (p->subs[ANALOG_SUB_REAL].inthreeway) {
1338  /* This was part of a three way call. Immediately make way for
1339  another call */
1340  ast_debug(1, "Call was complete, setting owner to former third call\n");
1343  } else {
1344  /* This call hasn't been completed yet... Set owner to NULL */
1345  ast_debug(1, "Call was incomplete, setting owner to NULL\n");
1347  }
1348  }
1349  break;
1350  case ANALOG_SUB_CALLWAIT:
1351  /* Ditch the holding callwait call, and immediately make it available */
1353  /* Need to hold the lock for call-waiting call, private, and 3-way call */
1355 
1356  /* This is actually part of a three way, placed on hold. Place the third part
1357  on music on hold now */
1358  if (p->subs[ANALOG_SUB_THREEWAY].owner) {
1360  }
1362  /* Make it the call wait now */
1365  if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
1366  /* Unlock the 3-way call that we swapped to call-waiting call. */
1368  }
1369  } else {
1371  }
1372  break;
1373  case ANALOG_SUB_THREEWAY:
1374  /* Need to hold the lock for 3-way call, private, and call-waiting call */
1377  /* The other party of the three way call is currently in a call-wait state.
1378  Start music on hold for them, and take the main guy out of the third call */
1380  if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
1382  }
1383  }
1384  if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
1386  }
1388  /* If this was part of a three way call index, let us make
1389  another three way call */
1391  break;
1392  default:
1393  /*
1394  * Should never happen.
1395  * This wasn't any sort of call, so how are we an index?
1396  */
1397  ast_log(LOG_ERROR, "Index found but not any type of call?\n");
1398  break;
1399  }
1400  }
1401 
1404  analog_set_ringtimeout(p, 0);
1406  analog_set_pulsedial(p, 0);
1407  analog_set_outgoing(p, 0);
1408  p->onhooktime = time(NULL);
1409  p->cidrings = 1;
1410 
1411  /* Perform low level hangup if no owner left */
1412  res = analog_on_hook(p);
1413  if (res < 0) {
1414  ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast_channel_name(ast));
1415  }
1416  switch (p->sig) {
1417  case ANALOG_SIG_FXOGS:
1418  case ANALOG_SIG_FXOLS:
1419  case ANALOG_SIG_FXOKS:
1420  /* If they're off hook, try playing congestion */
1421  if (analog_is_off_hook(p)) {
1424  } else {
1426  }
1427  break;
1428  case ANALOG_SIG_FXSGS:
1429  case ANALOG_SIG_FXSLS:
1430  case ANALOG_SIG_FXSKS:
1431  /* Make sure we're not made available for at least two seconds assuming
1432  we were actually used for an inbound or outbound call. */
1433  if (ast_channel_state(ast) != AST_STATE_RESERVED) {
1434  time(&p->guardtime);
1435  p->guardtime += 2;
1436  }
1437  break;
1438  default:
1440  break;
1441  }
1442 
1444 
1445  x = 0;
1446  ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
1447  ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
1448  p->callwaitcas = 0;
1451  analog_set_dialing(p, 0);
1452  analog_update_conf(p);
1454  }
1455 
1457 
1458  ast_verb(3, "Hanging up on '%s'\n", ast_channel_name(ast));
1459 
1460  return 0;
1461 }
1462 
1463 int analog_answer(struct analog_pvt *p, struct ast_channel *ast)
1464 {
1465  int res = 0;
1466  int idx;
1467  int oldstate = ast_channel_state(ast);
1468 
1469  ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1470  ast_setstate(ast, AST_STATE_UP);
1471  idx = analog_get_index(ast, p, 1);
1472  if (idx < 0) {
1473  idx = ANALOG_SUB_REAL;
1474  }
1475  switch (p->sig) {
1476  case ANALOG_SIG_FXSLS:
1477  case ANALOG_SIG_FXSGS:
1478  case ANALOG_SIG_FXSKS:
1479  analog_set_ringtimeout(p, 0);
1480  /* Fall through */
1481  case ANALOG_SIG_EM:
1482  case ANALOG_SIG_EM_E1:
1483  case ANALOG_SIG_EMWINK:
1484  case ANALOG_SIG_FEATD:
1485  case ANALOG_SIG_FEATDMF:
1486  case ANALOG_SIG_FEATDMF_TA:
1487  case ANALOG_SIG_E911:
1488  case ANALOG_SIG_FGC_CAMA:
1489  case ANALOG_SIG_FGC_CAMAMF:
1490  case ANALOG_SIG_FEATB:
1491  case ANALOG_SIG_SF:
1492  case ANALOG_SIG_SFWINK:
1493  case ANALOG_SIG_SF_FEATD:
1494  case ANALOG_SIG_SF_FEATDMF:
1495  case ANALOG_SIG_SF_FEATB:
1496  case ANALOG_SIG_FXOLS:
1497  case ANALOG_SIG_FXOGS:
1498  case ANALOG_SIG_FXOKS:
1499  /* Pick up the line */
1500  ast_debug(1, "Took %s off hook\n", ast_channel_name(ast));
1501  if (p->hanguponpolarityswitch) {
1502  gettimeofday(&p->polaritydelaytv, NULL);
1503  }
1504  res = analog_off_hook(p);
1505  analog_play_tone(p, idx, -1);
1506  analog_set_dialing(p, 0);
1507  if ((idx == ANALOG_SUB_REAL) && p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
1508  if (oldstate == AST_STATE_RINGING) {
1509  ast_debug(1, "Finally swapping real and threeway\n");
1513  }
1514  }
1515 
1516  switch (p->sig) {
1517  case ANALOG_SIG_FXSLS:
1518  case ANALOG_SIG_FXSKS:
1519  case ANALOG_SIG_FXSGS:
1522  break;
1523  case ANALOG_SIG_FXOLS:
1524  case ANALOG_SIG_FXOKS:
1525  case ANALOG_SIG_FXOGS:
1527  break;
1528  default:
1529  break;
1530  }
1531  break;
1532  default:
1533  ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
1534  res = -1;
1535  break;
1536  }
1537  ast_setstate(ast, AST_STATE_UP);
1538  return res;
1539 }
1540 
1541 static int analog_handles_digit(struct ast_frame *f)
1542 {
1543  char subclass = toupper(f->subclass.integer);
1544 
1545  switch (subclass) {
1546  case '1':
1547  case '2':
1548  case '3':
1549  case '4':
1550  case '5':
1551  case '6':
1552  case '7':
1553  case '9':
1554  case 'A':
1555  case 'B':
1556  case 'C':
1557  case 'D':
1558  case 'E':
1559  case 'F':
1560  return 1;
1561  default:
1562  return 0;
1563  }
1564 }
1565 
1566 void analog_handle_dtmf(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub idx, struct ast_frame **dest)
1567 {
1568  struct ast_frame *f = *dest;
1569 
1570  ast_debug(1, "%s DTMF digit: 0x%02X '%c' on %s\n",
1571  f->frametype == AST_FRAME_DTMF_BEGIN ? "Begin" : "End",
1572  (unsigned)f->subclass.integer, f->subclass.integer, ast_channel_name(ast));
1573 
1574  if (analog_check_confirmanswer(p)) {
1575  if (f->frametype == AST_FRAME_DTMF_END) {
1576  ast_debug(1, "Confirm answer on %s!\n", ast_channel_name(ast));
1577  /* Upon receiving a DTMF digit, consider this an answer confirmation instead
1578  of a DTMF digit */
1579  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
1581  /* Reset confirmanswer so DTMF's will behave properly for the duration of the call */
1583  } else {
1584  p->subs[idx].f.frametype = AST_FRAME_NULL;
1585  p->subs[idx].f.subclass.integer = 0;
1586  }
1587  *dest = &p->subs[idx].f;
1588  } else if (p->callwaitcas) {
1589  if (f->frametype == AST_FRAME_DTMF_END) {
1590  if ((f->subclass.integer == 'A') || (f->subclass.integer == 'D')) {
1591  ast_debug(1, "Got some DTMF, but it's for the CAS\n");
1592  p->caller.id.name.str = p->callwait_name;
1593  p->caller.id.number.str = p->callwait_num;
1594  analog_send_callerid(p, 1, &p->caller);
1595  }
1596  if (analog_handles_digit(f)) {
1597  p->callwaitcas = 0;
1598  }
1599  }
1600  p->subs[idx].f.frametype = AST_FRAME_NULL;
1601  p->subs[idx].f.subclass.integer = 0;
1602  *dest = &p->subs[idx].f;
1603  } else {
1604  analog_cb_handle_dtmf(p, ast, idx, dest);
1605  }
1606 }
1607 
1608 static int analog_my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
1609 {
1610  char c;
1611 
1612  *str = 0; /* start with empty output buffer */
1613  for (;;) {
1614  /* Wait for the first digit (up to specified ms). */
1615  c = ast_waitfordigit(chan, ms);
1616  /* if timeout, hangup or error, return as such */
1617  if (c < 1) {
1618  return c;
1619  }
1620  *str++ = c;
1621  *str = 0;
1622  if (strchr(term, c)) {
1623  return 1;
1624  }
1625  }
1626 }
1627 
1628 static int analog_handle_notify_message(struct ast_channel *chan, struct analog_pvt *p, int cid_flags, int neon_mwievent)
1629 {
1631  analog_callbacks.handle_notify_message(chan, p->chan_pvt, cid_flags, neon_mwievent);
1632  return 0;
1633  }
1634  return -1;
1635 }
1636 
1637 static void analog_increase_ss_count(void)
1638 {
1641  }
1642 }
1643 
1644 static void analog_decrease_ss_count(void)
1645 {
1648  }
1649 }
1650 
1651 static int analog_distinctive_ring(struct ast_channel *chan, struct analog_pvt *p, int idx, int *ringdata)
1652 {
1653  if (!p->usedistinctiveringdetection) {
1654  return 0;
1655  }
1657  return analog_callbacks.distinctive_ring(chan, p->chan_pvt, idx, ringdata);
1658  }
1659  return -1;
1660 
1661 }
1662 
1664 {
1667  }
1668 }
1669 
1670 static void *analog_get_bridged_channel(struct ast_channel *chan)
1671 {
1674  }
1675  return NULL;
1676 }
1677 
1678 static int analog_get_sub_fd(struct analog_pvt *p, enum analog_sub sub)
1679 {
1682  }
1683  return -1;
1684 }
1685 
1686 #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))
1687 
1688 static int analog_canmatch_featurecode(const char *pickupexten, const char *exten)
1689 {
1690  int extlen = strlen(exten);
1691  if (!extlen) {
1692  return 1;
1693  }
1694  if (extlen < strlen(pickupexten) && !strncmp(pickupexten, exten, extlen)) {
1695  return 1;
1696  }
1697  /* hardcoded features are *60, *67, *69, *70, *72, *73, *78, *79, *82, *0 */
1698  if (exten[0] == '*' && extlen < 3) {
1699  if (extlen == 1) {
1700  return 1;
1701  }
1702  /* "*0" should be processed before it gets here */
1703  switch (exten[1]) {
1704  case '6':
1705  case '7':
1706  case '8':
1707  return 1;
1708  }
1709  }
1710  return 0;
1711 }
1712 
1713 static void *__analog_ss_thread(void *data)
1714 {
1715  struct analog_pvt *p = data;
1716  struct ast_channel *chan = p->ss_astchan;
1717  char exten[AST_MAX_EXTENSION] = "";
1718  char exten2[AST_MAX_EXTENSION] = "";
1719  char dtmfcid[300];
1720  char dtmfbuf[300];
1721  char namebuf[ANALOG_MAX_CID];
1722  char numbuf[ANALOG_MAX_CID];
1723  char *name = NULL, *number = NULL;
1724  int flags = 0;
1725  struct ast_smdi_md_message *smdi_msg = NULL;
1726  int timeout;
1727  int getforward = 0;
1728  char *s1, *s2;
1729  int len = 0;
1730  int res;
1731  int idx;
1732  ast_callid callid;
1733  RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
1734  const char *pickupexten;
1735 
1737 
1738  ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1739 
1740  if (!chan) {
1741  /* What happened to the channel? */
1742  goto quit;
1743  }
1744 
1745  if ((callid = ast_channel_callid(chan))) {
1747  }
1748 
1749  /* in the bizarre case where the channel has become a zombie before we
1750  even get started here, abort safely
1751  */
1752  if (!ast_channel_tech_pvt(chan)) {
1753  ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", ast_channel_name(chan));
1754  ast_hangup(chan);
1755  goto quit;
1756  }
1757 
1758  ast_verb(3, "Starting simple switch on '%s'\n", ast_channel_name(chan));
1759  idx = analog_get_index(chan, p, 0);
1760  if (idx < 0) {
1761  ast_hangup(chan);
1762  goto quit;
1763  }
1764 
1765  ast_channel_lock(chan);
1766  pickup_cfg = ast_get_chan_features_pickup_config(chan);
1767  if (!pickup_cfg) {
1768  ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
1769  pickupexten = "";
1770  } else {
1771  pickupexten = ast_strdupa(pickup_cfg->pickupexten);
1772  }
1773  ast_channel_unlock(chan);
1774 
1776  switch (p->sig) {
1777  case ANALOG_SIG_FEATD:
1778  case ANALOG_SIG_FEATDMF:
1779  case ANALOG_SIG_FEATDMF_TA:
1780  case ANALOG_SIG_E911:
1781  case ANALOG_SIG_FGC_CAMAMF:
1782  case ANALOG_SIG_FEATB:
1783  case ANALOG_SIG_EMWINK:
1784  case ANALOG_SIG_SF_FEATD:
1785  case ANALOG_SIG_SF_FEATDMF:
1786  case ANALOG_SIG_SF_FEATB:
1787  case ANALOG_SIG_SFWINK:
1788  if (analog_wink(p, idx))
1789  goto quit;
1790  /* Fall through */
1791  case ANALOG_SIG_EM:
1792  case ANALOG_SIG_EM_E1:
1793  case ANALOG_SIG_SF:
1794  case ANALOG_SIG_FGC_CAMA:
1795  res = analog_play_tone(p, idx, -1);
1796 
1798 
1799  /* set digit mode appropriately */
1800  if (ANALOG_NEED_MFDETECT(p)) {
1802  } else {
1804  }
1805 
1806  memset(dtmfbuf, 0, sizeof(dtmfbuf));
1807  /* Wait for the first digit only if immediate=no */
1808  if (!p->immediate) {
1809  /* Wait for the first digit (up to 5 seconds). */
1810  res = ast_waitfordigit(chan, 5000);
1811  } else {
1812  res = 0;
1813  }
1814  if (res > 0) {
1815  /* save first char */
1816  dtmfbuf[0] = res;
1817  switch (p->sig) {
1818  case ANALOG_SIG_FEATD:
1819  case ANALOG_SIG_SF_FEATD:
1820  res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
1821  if (res > 0) {
1822  res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
1823  }
1824  if (res < 1) {
1826  }
1827  break;
1828  case ANALOG_SIG_FEATDMF_TA:
1829  res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1830  if (res < 1) {
1832  }
1833  if (analog_wink(p, idx)) {
1834  goto quit;
1835  }
1836  dtmfbuf[0] = 0;
1837  /* Wait for the first digit (up to 5 seconds). */
1838  res = ast_waitfordigit(chan, 5000);
1839  if (res <= 0) {
1840  break;
1841  }
1842  dtmfbuf[0] = res;
1843  /* fall through intentionally */
1844  case ANALOG_SIG_FEATDMF:
1845  case ANALOG_SIG_E911:
1846  case ANALOG_SIG_FGC_CAMAMF:
1847  case ANALOG_SIG_SF_FEATDMF:
1848  res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1849  /* if international caca, do it again to get real ANO */
1850  if ((p->sig == ANALOG_SIG_FEATDMF) && (dtmfbuf[1] != '0')
1851  && (strlen(dtmfbuf) != 14)) {
1852  if (analog_wink(p, idx)) {
1853  goto quit;
1854  }
1855  dtmfbuf[0] = 0;
1856  /* Wait for the first digit (up to 5 seconds). */
1857  res = ast_waitfordigit(chan, 5000);
1858  if (res <= 0) {
1859  break;
1860  }
1861  dtmfbuf[0] = res;
1862  res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1863  }
1864  if (res > 0) {
1865  /* if E911, take off hook */
1866  if (p->sig == ANALOG_SIG_E911) {
1867  analog_off_hook(p);
1868  }
1869  }
1870  if (res < 1) {
1872  }
1873  break;
1874  case ANALOG_SIG_FEATB:
1875  case ANALOG_SIG_SF_FEATB:
1876  res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1877  if (res < 1) {
1879  }
1880  break;
1881  case ANALOG_SIG_EMWINK:
1882  /* if we received a '*', we are actually receiving Feature Group D
1883  dial syntax, so use that mode; otherwise, fall through to normal
1884  mode
1885  */
1886  if (res == '*') {
1887  res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
1888  if (res > 0) {
1889  res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
1890  }
1891  if (res < 1) {
1893  }
1894  break;
1895  }
1896  default:
1897  /* If we got the first digit, get the rest */
1898  len = 1;
1899  dtmfbuf[len] = '\0';
1900  while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, ast_channel_context(chan), dtmfbuf, 1, p->cid_num)) {
1901  if (ast_exists_extension(chan, ast_channel_context(chan), dtmfbuf, 1, p->cid_num)) {
1902  timeout = analog_get_matchdigit_timeout(p);
1903  } else {
1904  timeout = analog_get_interdigit_timeout(p);
1905  }
1906  res = ast_waitfordigit(chan, timeout);
1907  if (res < 0) {
1908  ast_debug(1, "waitfordigit returned < 0...\n");
1909  ast_hangup(chan);
1910  goto quit;
1911  } else if (res) {
1912  dtmfbuf[len++] = res;
1913  dtmfbuf[len] = '\0';
1914  } else {
1915  break;
1916  }
1917  }
1918  break;
1919  }
1920  }
1921  if (res == -1) {
1922  ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
1923  ast_hangup(chan);
1924  goto quit;
1925  } else if (res < 0) {
1926  ast_debug(1, "Got hung up before digits finished\n");
1927  ast_hangup(chan);
1928  goto quit;
1929  }
1930 
1931  if (p->sig == ANALOG_SIG_FGC_CAMA || p->sig == ANALOG_SIG_FGC_CAMAMF) {
1932  /* This if block is where we process ANI for CAMA */
1933 
1934  char anibuf[100];
1935  struct ast_party_caller *caller;
1936 
1937  /* cnoffset is the point at which we pull the calling number out
1938  * of anibuf. Must be the number of ani_info_digits + 1 to account
1939  * for the KP, which is considered a digit. */
1940 
1941  /* The 1XB with ANI-B will send a full 10 digits
1942  * or 2 digits in case of ANI failure.
1943  * (CD-95811-01 Section II, page 10)
1944  * 10 digit string example: *08320123#
1945  * 2 digit string example: *2
1946  * KP (*) and ST (#) are considered to be digits */
1947 
1948  int cnoffset = p->ani_info_digits + 1;
1949  ast_debug(1, "cnoffset: %d\n", cnoffset);
1950 
1951  /* This is how long to wait before the wink to start ANI spill
1952  * Pulled from chan_dahdi.conf, default is 1000ms */
1953  if (ast_safe_sleep(chan,p->ani_wink_time) == -1) {
1954  ast_hangup(chan);
1955  goto quit;
1956  }
1957  analog_off_hook(p);
1958  ast_debug(1, "Sent wink to signal ANI start\n");
1960 
1961  /* ani_timeout is configured in chan_dahdi.conf. default is 10000ms.
1962  * ST, STP, ST2P, ST3P can all signal transmission complete, regardless of timeout */
1963  res = analog_my_getsigstr(chan, anibuf, "#ABC", p->ani_timeout);
1964 
1965  /* so we can work with the ani buffer */
1966  pbx_builtin_setvar_helper(chan, "ANIBUF", anibuf);
1967 
1968  /* as long as we get a terminating pulse OR the length of ANI buffer is at least >=2, we can treat
1969  * this as a complete spill for the purposes of setting anistart */
1970  if ((res > 0) || (strlen(anibuf) >= 2)) {
1971  char anistart[2] = "X";
1972  char f[101] = {0};
1973  if (strchr("#ABC", anibuf[strlen(anibuf) - 1])) {
1974  anistart[0] = anibuf[strlen(anibuf) - 1];
1975  anibuf[strlen(anibuf) - 1] = 0;
1976  }
1977  ast_set_callerid(chan, anibuf + cnoffset, NULL, anibuf + cnoffset);
1978 
1979  caller = ast_channel_caller(chan);
1980  strncpy(f, &(anibuf[1]), MIN((int)(p->ani_info_digits), sizeof(f)-1));
1981  caller->ani2 = atoi(f);
1982 
1983  anibuf[cnoffset] = 0;
1984 
1985  /* so we can work with the different start pulses as used in ANI-D */
1986  pbx_builtin_setvar_helper(chan, "ANISTART", anistart);
1987  /* so we can use our ANI INFO digits in our dialplan */
1988  pbx_builtin_setvar_helper(chan, "ANI2", anibuf + 1);
1989  }
1991  }
1992 
1993  ast_copy_string(exten, dtmfbuf, sizeof(exten));
1994  if (ast_strlen_zero(exten)) {
1995  ast_copy_string(exten, "s", sizeof(exten));
1996  }
1997  if (p->sig == ANALOG_SIG_FEATD || p->sig == ANALOG_SIG_EMWINK) {
1998  /* Look for Feature Group D on all E&M Wink and Feature Group D trunks */
1999  if (exten[0] == '*') {
2000  char *stringp=NULL;
2001  ast_copy_string(exten2, exten, sizeof(exten2));
2002  /* Parse out extension and callerid */
2003  stringp=exten2 +1;
2004  s1 = strsep(&stringp, "*");
2005  s2 = strsep(&stringp, "*");
2006  if (s2) {
2007  if (!ast_strlen_zero(p->cid_num)) {
2008  ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
2009  } else {
2010  ast_set_callerid(chan, s1, NULL, s1);
2011  }
2012  ast_copy_string(exten, s2, sizeof(exten));
2013  } else {
2014  ast_copy_string(exten, s1, sizeof(exten));
2015  }
2016  } else if (p->sig == ANALOG_SIG_FEATD) {
2017  ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
2018  }
2019  }
2020  if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
2021  if (exten[0] == '*') {
2022  char *stringp=NULL;
2023  struct ast_party_caller *caller;
2024 
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  if (*(s1 + 2)) {
2035  ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
2036  }
2037  }
2038  ast_copy_string(exten, s2 + 1, sizeof(exten));
2039  } else {
2040  ast_copy_string(exten, s1 + 2, sizeof(exten));
2041  }
2042 
2043  /* The first two digits are ani2 information. */
2044  caller = ast_channel_caller(chan);
2045  s1[2] = '\0';
2046  caller->ani2 = atoi(s1);
2047  } else {
2048  ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
2049  }
2050  }
2051  if ((p->sig == ANALOG_SIG_E911) || (p->sig == ANALOG_SIG_FGC_CAMAMF)) {
2052  if (exten[0] == '*') {
2053  char *stringp=NULL;
2054  ast_copy_string(exten2, exten, sizeof(exten2));
2055  /* Parse out extension and callerid */
2056  stringp=exten2 +1;
2057  s1 = strsep(&stringp, "#");
2058  s2 = strsep(&stringp, "#");
2059  if (s2 && (*(s2 + 1) == '0')) {
2060  if (*(s2 + 2)) {
2061  ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
2062  }
2063  }
2064  if (s1) {
2065  ast_copy_string(exten, s1, sizeof(exten));
2066  } else {
2067  ast_copy_string(exten, "911", sizeof(exten));
2068  }
2069  } else {
2070  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);
2071  }
2072  }
2073  if (p->sig == ANALOG_SIG_FEATB) {
2074  if (exten[0] == '*') {
2075  char *stringp=NULL;
2076  ast_copy_string(exten2, exten, sizeof(exten2));
2077  /* Parse out extension and callerid */
2078  stringp=exten2 +1;
2079  s1 = strsep(&stringp, "#");
2080  ast_copy_string(exten, exten2 + 1, sizeof(exten));
2081  } else {
2082  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);
2083  }
2084  }
2085  if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
2086  analog_wink(p, idx);
2087  /*
2088  * Some switches require a minimum guard time between the last
2089  * FGD wink and something that answers immediately. This
2090  * ensures it.
2091  */
2092  if (ast_safe_sleep(chan, 100)) {
2093  ast_hangup(chan);
2094  goto quit;
2095  }
2096  }
2098 
2100 
2101  if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1,
2102  ast_channel_caller(chan)->id.number.valid ? ast_channel_caller(chan)->id.number.str : NULL)) {
2105  res = ast_pbx_run(chan);
2106  if (res) {
2107  ast_log(LOG_WARNING, "PBX exited non-zero\n");
2109  }
2110  goto quit;
2111  } else {
2112  ast_verb(3, "Unknown extension '%s' in context '%s' requested\n", exten, ast_channel_context(chan));
2113  sleep(2);
2114  res = analog_play_tone(p, idx, ANALOG_TONE_INFO);
2115  if (res < 0) {
2116  ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
2117  } else {
2118  sleep(1);
2119  }
2120  res = ast_streamfile(chan, "ss-noservice", ast_channel_language(chan));
2121  if (res >= 0) {
2122  ast_waitstream(chan, "");
2123  }
2125  ast_hangup(chan);
2126  goto quit;
2127  }
2128  break;
2129  case ANALOG_SIG_FXOLS:
2130  case ANALOG_SIG_FXOGS:
2131  case ANALOG_SIG_FXOKS:
2132  /* Read the first digit */
2133  timeout = analog_get_firstdigit_timeout(p);
2134  /* If starting a threeway call, never timeout on the first digit so someone
2135  can use flash-hook as a "hold" feature */
2136  if (p->subs[ANALOG_SUB_THREEWAY].owner) {
2137  timeout = 999999;
2138  }
2139  while (len < AST_MAX_EXTENSION-1) {
2140  int is_exten_parking = 0;
2141 
2142  /* Read digit unless it's supposed to be immediate, in which case the
2143  only answer is 's' */
2144  if (p->immediate) {
2145  res = 's';
2146  } else {
2147  res = ast_waitfordigit(chan, timeout);
2148  }
2149  timeout = 0;
2150  if (res < 0) {
2151  ast_debug(1, "waitfordigit returned < 0...\n");
2152  res = analog_play_tone(p, idx, -1);
2153  ast_hangup(chan);
2154  goto quit;
2155  } else if (res) {
2156  ast_debug(1,"waitfordigit returned '%c' (%d), timeout = %d\n", res, res, timeout);
2157  exten[len++]=res;
2158  exten[len] = '\0';
2159  }
2161  analog_play_tone(p, idx, -1);
2162  } else {
2164  }
2166  is_exten_parking = ast_parking_is_exten_park(ast_channel_context(chan), exten);
2167  }
2168  if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num) && !is_exten_parking) {
2169  if (!res || !ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2170  if (getforward) {
2171  /* Record this as the forwarding extension */
2173  ast_verb(3, "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
2175  if (res) {
2176  break;
2177  }
2178  usleep(500000);
2179  res = analog_play_tone(p, idx, -1);
2180  sleep(1);
2181  memset(exten, 0, sizeof(exten));
2182  res = analog_play_tone(p, idx, ANALOG_TONE_DIALTONE);
2183  len = 0;
2184  getforward = 0;
2185  } else {
2186  res = analog_play_tone(p, idx, -1);
2187  ast_channel_lock(chan);
2189  if (!ast_strlen_zero(p->cid_num)) {
2190  if (!p->hidecallerid) {
2191  ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
2192  } else {
2193  ast_set_callerid(chan, NULL, NULL, p->cid_num);
2194  }
2195  }
2196  if (!ast_strlen_zero(p->cid_name)) {
2197  if (!p->hidecallerid) {
2198  ast_set_callerid(chan, NULL, p->cid_name, NULL);
2199  }
2200  }
2202  ast_channel_unlock(chan);
2204  res = ast_pbx_run(chan);
2205  if (res) {
2206  ast_log(LOG_WARNING, "PBX exited non-zero\n");
2208  }
2209  goto quit;
2210  }
2211  } else {
2212  /* It's a match, but they just typed a digit, and there is an ambiguous match,
2213  so just set the timeout to analog_matchdigittimeout and wait some more */
2214  timeout = analog_get_matchdigit_timeout(p);
2215  }
2216  } else if (res == 0) {
2217  ast_debug(1, "not enough digits (and no ambiguous match)...\n");
2219  analog_wait_event(p);
2220  ast_hangup(chan);
2221  goto quit;
2222  } else if (p->callwaiting && !strcmp(exten, "*70")) {
2223  ast_verb(3, "Disabling call waiting on %s\n", ast_channel_name(chan));
2224  /* Disable call waiting if enabled */
2225  analog_set_callwaiting(p, 0);
2227  if (res) {
2228  ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
2229  ast_channel_name(chan), strerror(errno));
2230  }
2231  len = 0;
2232  memset(exten, 0, sizeof(exten));
2233  timeout = analog_get_firstdigit_timeout(p);
2234 
2235  } else if (!strcmp(exten, pickupexten)) {
2236  /* Scan all channels and see if there are any
2237  * ringing channels that have call groups
2238  * that equal this channels pickup group
2239  */
2240  if (idx == ANALOG_SUB_REAL) {
2241  /* Switch us from Third call to Call Wait */
2242  if (p->subs[ANALOG_SUB_THREEWAY].owner) {
2243  /* If you make a threeway call and the *8# a call, it should actually
2244  look like a callwait */
2248  }
2250  if (ast_pickup_call(chan)) {
2251  ast_debug(1, "No call pickup possible...\n");
2253  analog_wait_event(p);
2254  }
2255  ast_hangup(chan);
2256  goto quit;
2257  } else {
2258  ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n");
2259  ast_hangup(chan);
2260  goto quit;
2261  }
2262 
2263  } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
2264  ast_verb(3, "Disabling Caller*ID on %s\n", ast_channel_name(chan));
2265  /* Disable Caller*ID if enabled */
2266  p->hidecallerid = 1;
2272  if (res) {
2273  ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
2274  ast_channel_name(chan), strerror(errno));
2275  }
2276  len = 0;
2277  memset(exten, 0, sizeof(exten));
2278  timeout = analog_get_firstdigit_timeout(p);
2279  } else if (p->callreturn && !strcmp(exten, "*69")) {
2280  res = 0;
2281  if (!ast_strlen_zero(p->lastcid_num)) {
2282  res = ast_say_digit_str(chan, p->lastcid_num, "", ast_channel_language(chan));
2283  }
2284  if (!res) {
2286  }
2287  break;
2288  } else if (!strcmp(exten, "*78")) {
2289  /* Do not disturb enabled */
2290  analog_dnd(p, 1);
2292  getforward = 0;
2293  memset(exten, 0, sizeof(exten));
2294  len = 0;
2295  } else if (!strcmp(exten, "*79")) {
2296  /* Do not disturb disabled */
2297  analog_dnd(p, 0);
2299  getforward = 0;
2300  memset(exten, 0, sizeof(exten));
2301  len = 0;
2302  } else if (p->cancallforward && !strcmp(exten, "*72")) {
2304  getforward = 1;
2305  memset(exten, 0, sizeof(exten));
2306  len = 0;
2307  } else if (p->cancallforward && !strcmp(exten, "*73")) {
2308  ast_verb(3, "Cancelling call forwarding on channel %d\n", p->channel);
2310  memset(p->call_forward, 0, sizeof(p->call_forward));
2311  getforward = 0;
2312  memset(exten, 0, sizeof(exten));
2313  len = 0;
2314  } else if ((p->transfer || p->canpark) && is_exten_parking
2315  && p->subs[ANALOG_SUB_THREEWAY].owner) {
2316  struct ast_bridge_channel *bridge_channel;
2317 
2318  /*
2319  * This is a three way call, the main call being a real channel,
2320  * and we're parking the first call.
2321  */
2325  if (bridge_channel) {
2327  /*
2328  * Swap things around between the three-way and real call so we
2329  * can hear where the channel got parked.
2330  */
2335 
2336  ast_verb(3, "%s: Parked call\n", ast_channel_name(chan));
2337  ast_hangup(chan);
2338  ao2_ref(bridge_channel, -1);
2339  goto quit;
2340  }
2341  ao2_ref(bridge_channel, -1);
2342  }
2343  break;
2344  } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
2345  ast_verb(3, "Blacklisting number %s\n", p->lastcid_num);
2346  res = ast_db_put("blacklist", p->lastcid_num, "1");
2347  if (!res) {
2349  memset(exten, 0, sizeof(exten));
2350  len = 0;
2351  }
2352  } else if (p->hidecallerid && !strcmp(exten, "*82")) {
2353  ast_verb(3, "Enabling Caller*ID on %s\n", ast_channel_name(chan));
2354  /* Enable Caller*ID if enabled */
2355  p->hidecallerid = 0;
2358  if (res) {
2359  ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
2360  ast_channel_name(chan), strerror(errno));
2361  }
2362  len = 0;
2363  memset(exten, 0, sizeof(exten));
2364  timeout = analog_get_firstdigit_timeout(p);
2365  } else if (!strcmp(exten, "*0")) {
2366  struct ast_channel *nbridge = p->subs[ANALOG_SUB_THREEWAY].owner;
2367  struct analog_pvt *pbridge = NULL;
2368  /* set up the private struct of the bridged one, if any */
2369  if (nbridge) {
2370  pbridge = analog_get_bridged_channel(nbridge);
2371  }
2372  if (pbridge && ISTRUNK(pbridge)) {
2373  /* Clear out the dial buffer */
2374  p->dop.dialstr[0] = '\0';
2375  /* flash hookswitch */
2376  if ((analog_flash(pbridge) == -1) && (errno != EINPROGRESS)) {
2378  "Unable to flash-hook bridged trunk from channel %s: %s\n",
2379  ast_channel_name(nbridge), strerror(errno));
2380  }
2385  ast_hangup(chan);
2386  goto quit;
2387  } else {
2389  analog_wait_event(p);
2390  analog_play_tone(p, idx, -1);
2394  ast_hangup(chan);
2395  goto quit;
2396  }
2397  } else if (!ast_canmatch_extension(chan, ast_channel_context(chan), exten, 1,
2398  ast_channel_caller(chan)->id.number.valid ? ast_channel_caller(chan)->id.number.str : NULL)
2399  && !analog_canmatch_featurecode(pickupexten, exten)) {
2400  ast_debug(1, "Can't match %s from '%s' in context %s\n", exten,
2401  ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str
2402  ? ast_channel_caller(chan)->id.number.str : "<Unknown Caller>",
2403  ast_channel_context(chan));
2404  break;
2405  }
2406  if (!timeout) {
2407  timeout = analog_get_interdigit_timeout(p);
2408  }
2409  if (len && !ast_ignore_pattern(ast_channel_context(chan), exten)) {
2410  analog_play_tone(p, idx, -1);
2411  }
2412  }
2413  break;
2414  case ANALOG_SIG_FXSLS:
2415  case ANALOG_SIG_FXSGS:
2416  case ANALOG_SIG_FXSKS:
2417  /* check for SMDI messages */
2418  if (p->use_smdi && p->smdi_iface) {
2420  if (smdi_msg != NULL) {
2421  ast_channel_exten_set(chan, smdi_msg->fwd_st);
2422 
2423  if (smdi_msg->type == 'B')
2424  pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "b");
2425  else if (smdi_msg->type == 'N')
2426  pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "u");
2427 
2428  ast_debug(1, "Received SMDI message on %s\n", ast_channel_name(chan));
2429  } else {
2430  ast_log(LOG_WARNING, "SMDI enabled but no SMDI message present\n");
2431  }
2432  }
2433 
2434  if (p->use_callerid && (p->cid_signalling == CID_SIG_SMDI && smdi_msg)) {
2435  number = smdi_msg->calling_st;
2436 
2437  /* If we want caller id, we're in a prering state due to a polarity reversal
2438  * and we're set to use a polarity reversal to trigger the start of caller id,
2439  * grab the caller id and wait for ringing to start... */
2440  } else if (p->use_callerid && (ast_channel_state(chan) == AST_STATE_PRERING
2444  /* If set to use DTMF CID signalling, listen for DTMF */
2445  if (p->cid_signalling == CID_SIG_DTMF) {
2446  int k = 0;
2447  int oldlinearity;
2448  int timeout_ms;
2449  int ms;
2450  struct timeval start = ast_tvnow();
2451  ast_debug(1, "Receiving DTMF cid on channel %s\n", ast_channel_name(chan));
2452 
2453  oldlinearity = analog_set_linear_mode(p, idx, 0);
2454 
2455  /*
2456  * We are the only party interested in the Rx stream since
2457  * we have not answered yet. We don't need or even want DTMF
2458  * emulation. The DTMF digits can come so fast that emulation
2459  * can drop some of them.
2460  */
2461  ast_channel_lock(chan);
2463  ast_channel_unlock(chan);
2464  timeout_ms = 4000;/* This is a typical OFF time between rings. */
2465  for (;;) {
2466  struct ast_frame *f;
2467 
2468  ms = ast_remaining_ms(start, timeout_ms);
2469  res = ast_waitfor(chan, ms);
2470  if (res <= 0) {
2471  /*
2472  * We do not need to restore the analog_set_linear_mode()
2473  * or AST_FLAG_END_DTMF_ONLY flag settings since we
2474  * are hanging up the channel.
2475  */
2477  "DTMFCID timed out waiting for ring. Exiting simple switch\n");
2478  ast_hangup(chan);
2479  goto quit;
2480  }
2481  f = ast_read(chan);
2482  if (!f) {
2483  break;
2484  }
2485  if (f->frametype == AST_FRAME_DTMF) {
2486  if (k < ARRAY_LEN(dtmfbuf) - 1) {
2487  dtmfbuf[k++] = f->subclass.integer;
2488  }
2489  ast_debug(1, "CID got digit '%c'\n", f->subclass.integer);
2490  start = ast_tvnow();
2491  }
2492  ast_frfree(f);
2493  if (ast_channel_state(chan) == AST_STATE_RING ||
2495  break; /* Got ring */
2496  }
2497  }
2498  ast_channel_lock(chan);
2500  ast_channel_unlock(chan);
2501  dtmfbuf[k] = '\0';
2502 
2503  analog_set_linear_mode(p, idx, oldlinearity);
2504 
2505  /* Got cid and ring. */
2506  ast_debug(1, "CID got string '%s'\n", dtmfbuf);
2507  callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
2508  ast_debug(1, "CID is '%s', flags %d\n", dtmfcid, flags);
2509  /* If first byte is NULL, we have no cid */
2510  if (!ast_strlen_zero(dtmfcid)) {
2511  number = dtmfcid;
2512  } else {
2513  number = NULL;
2514  }
2515 
2516  /* If set to use V23 Signalling, launch our FSK gubbins and listen for it */
2517  } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) {
2518  namebuf[0] = 0;
2519  numbuf[0] = 0;
2520 
2522  int timeout = 10000; /* Ten seconds */
2523  struct timeval start = ast_tvnow();
2524  enum analog_event ev;
2525  int off_ms;
2526  int ms;
2527  struct timeval off_start;
2528 
2529  if (!p->usedistinctiveringdetection) {
2530  /* Disable distinctive ring timeout count */
2531  analog_set_ringtimeout(p, 0);
2532  }
2533  while ((ms = ast_remaining_ms(start, timeout))) {
2534  res = analog_get_callerid(p, namebuf, numbuf, &ev, ms);
2535  if (res < 0) {
2537  "CallerID returned with error on channel '%s'\n",
2538  ast_channel_name(chan));
2539  break;
2540  }
2541  if (res == 0) {
2542  break;
2543  }
2544  if (res != 1) {
2545  continue;
2546  }
2547  if (ev == ANALOG_EVENT_NOALARM) {
2548  analog_set_alarm(p, 0);
2549  }
2550  if (p->cid_signalling == CID_SIG_V23_JP) {
2551  if (ev == ANALOG_EVENT_RINGBEGIN) {
2552  analog_off_hook(p);
2553  usleep(1);
2554  }
2555  } else {
2556  break;
2557  }
2558  }
2559 
2560  name = namebuf;
2561  number = numbuf;
2562 
2563  if (p->cid_signalling == CID_SIG_V23_JP) {
2564  analog_on_hook(p);
2565  usleep(1);
2566  }
2567 
2568  /* Finished with Caller*ID, now wait for a ring to make sure there really is a call coming */
2569  off_start = ast_tvnow();
2570  off_ms = 4000;/* This is a typical OFF time between rings. */
2571  while ((ms = ast_remaining_ms(off_start, off_ms))) {
2572  struct ast_frame *f;
2573 
2574  res = ast_waitfor(chan, ms);
2575  if (res <= 0) {
2577  "CID timed out waiting for ring. Exiting simple switch\n");
2579  ast_hangup(chan);
2580  goto quit;
2581  }
2582  if (!(f = ast_read(chan))) {
2583  ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n");
2585  ast_hangup(chan);
2586  goto quit;
2587  }
2588  ast_frfree(f);
2589  if (ast_channel_state(chan) == AST_STATE_RING ||
2591  break; /* Got ring */
2592  }
2593 
2594  res = analog_distinctive_ring(chan, p, idx, NULL);
2596  if (res) {
2597  goto quit;
2598  }
2599  } else {
2600  ast_log(LOG_WARNING, "Unable to get caller ID space\n");
2601  }
2602  } else {
2604  "Channel %s in prering state, but I have nothing to do. Terminating simple switch, should be restarted by the actual ring.\n",
2605  ast_channel_name(chan));
2606  ast_hangup(chan);
2607  goto quit;
2608  }
2609  } else if (p->use_callerid && p->cid_start == ANALOG_CID_START_RING) {
2610  namebuf[0] = 0;
2611  numbuf[0] = 0;
2612 
2614  int timeout = 10000; /* Ten seconds */
2615  struct timeval start = ast_tvnow();
2616  enum analog_event ev;
2617  int ring_data[RING_PATTERNS] = { 0 };
2618  int ring_data_idx = 0;
2619  int ms;
2620 
2621  if (!p->usedistinctiveringdetection) {
2622  /* Disable distinctive ring timeout count */
2623  analog_set_ringtimeout(p, 0);
2624  }
2625  while ((ms = ast_remaining_ms(start, timeout))) {
2626  res = analog_get_callerid(p, namebuf, numbuf, &ev, ms);
2627  if (res < 0) {
2629  "CallerID returned with error on channel '%s'\n",
2630  ast_channel_name(chan));
2631  break;
2632  }
2633  if (res == 0) {
2634  break;
2635  }
2636  if (res != 1) {
2637  continue;
2638  }
2639  if (ev == ANALOG_EVENT_NOALARM) {
2640  analog_set_alarm(p, 0);
2641  } else if (ev == ANALOG_EVENT_POLARITY
2643  && p->polarity == POLARITY_REV) {
2644  ast_debug(1,
2645  "Hanging up due to polarity reversal on channel %d while detecting callerid\n",
2646  p->channel);
2647  p->polarity = POLARITY_IDLE;
2649  ast_hangup(chan);
2650  goto quit;
2651  } else if (ev == ANALOG_EVENT_RINGOFFHOOK
2653  && ring_data_idx < RING_PATTERNS) {
2654  /*
2655  * Detect callerid while collecting possible
2656  * distinctive ring pattern.
2657  */
2658  ring_data[ring_data_idx] = p->ringt;
2659  ++ring_data_idx;
2660  }
2661  }
2662 
2663  name = namebuf;
2664  number = numbuf;
2665 
2666  res = analog_distinctive_ring(chan, p, idx, ring_data);
2668  if (res) {
2669  goto quit;
2670  }
2671  } else {
2672  ast_log(LOG_WARNING, "Unable to get caller ID space\n");
2673  }
2674  }
2675 
2676  if (number) {
2678  }
2680 
2681  analog_handle_notify_message(chan, p, flags, -1);
2682 
2683  ast_channel_lock(chan);
2685  ast_channel_rings_set(chan, 1);
2686  ast_channel_unlock(chan);
2688  res = ast_pbx_run(chan);
2689  if (res) {
2690  ast_hangup(chan);
2691  ast_log(LOG_WARNING, "PBX exited non-zero\n");
2692  }
2693  goto quit;
2694  default:
2695  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);
2696  break;
2697  }
2699  if (res < 0) {
2700  ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
2701  }
2702  ast_hangup(chan);
2703 quit:
2704  ao2_cleanup(smdi_msg);
2706  return NULL;
2707 }
2708 
2709 int analog_ss_thread_start(struct analog_pvt *p, struct ast_channel *chan)
2710 {
2711  pthread_t threadid;
2712 
2713  return ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, p);
2714 }
2715 
2716 static void analog_publish_channel_alarm_clear(int channel)
2717 {
2718  RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
2719 
2720  ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", channel);
2721  body = ast_json_pack("{s: i}", "Channel", channel);
2722  if (!body) {
2723  return;
2724  }
2725 
2726  ast_manager_publish_event("AlarmClear", EVENT_FLAG_SYSTEM, body);
2727 }
2728 
2729 static struct ast_frame *__analog_handle_event(struct analog_pvt *p, struct ast_channel *ast)
2730 {
2731  int res, x;
2732  int mysig;
2733  int idx;
2734  char *c;
2735  pthread_t threadid;
2736  struct ast_channel *chan;
2737  struct ast_frame *f;
2738  struct ast_control_pvt_cause_code *cause_code = NULL;
2739  int data_size = sizeof(*cause_code);
2740  char *subclass = NULL;
2741 
2742  ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
2743 
2744  idx = analog_get_index(ast, p, 0);
2745  if (idx < 0) {
2746  return &ast_null_frame;
2747  }
2748  if (idx != ANALOG_SUB_REAL) {
2749  ast_log(LOG_ERROR, "We got an event on a non real sub. Fix it!\n");
2750  }
2751 
2752  mysig = p->sig;
2753  if (p->outsigmod > -1) {
2754  mysig = p->outsigmod;
2755  }
2756 
2757  p->subs[idx].f.frametype = AST_FRAME_NULL;
2758  p->subs[idx].f.subclass.integer = 0;
2759  p->subs[idx].f.datalen = 0;
2760  p->subs[idx].f.samples = 0;
2761  p->subs[idx].f.mallocd = 0;
2762  p->subs[idx].f.offset = 0;
2763  p->subs[idx].f.src = "dahdi_handle_event";
2764  p->subs[idx].f.data.ptr = NULL;
2765  f = &p->subs[idx].f;
2766 
2767  res = analog_get_event(p);
2768 
2769  ast_debug(1, "Got event %s(%d) on channel %d (index %u)\n", analog_event2str(res), res, p->channel, idx);
2770 
2772  analog_set_pulsedial(p, (res & ANALOG_EVENT_PULSEDIGIT) ? 1 : 0);
2773  ast_debug(1, "Detected %sdigit '%c'\n", (res & ANALOG_EVENT_PULSEDIGIT) ? "pulse ": "", res & 0xff);
2774  analog_confmute(p, 0);
2775  p->subs[idx].f.frametype = AST_FRAME_DTMF_END;
2776  p->subs[idx].f.subclass.integer = res & 0xff;
2777  analog_handle_dtmf(p, ast, idx, &f);
2778  return f;
2779  }
2780 
2781  if (res & ANALOG_EVENT_DTMFDOWN) {
2782  ast_debug(1, "DTMF Down '%c'\n", res & 0xff);
2783  /* Mute conference */
2784  analog_confmute(p, 1);
2786  p->subs[idx].f.subclass.integer = res & 0xff;
2787  analog_handle_dtmf(p, ast, idx, &f);
2788  return f;
2789  }
2790 
2791  switch (res) {
2792  case ANALOG_EVENT_ALARM:
2793  case ANALOG_EVENT_POLARITY:
2794  case ANALOG_EVENT_ONHOOK:
2795  /* add length of "ANALOG " */
2796  data_size += 7;
2797  subclass = analog_event2str(res);
2798  data_size += strlen(subclass);
2799  cause_code = ast_alloca(data_size);
2800  memset(cause_code, 0, data_size);
2801  cause_code->ast_cause = AST_CAUSE_NORMAL_CLEARING;
2803  snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "ANALOG %s", subclass);
2804  break;
2805  default:
2806  break;
2807  }
2808 
2809  switch (res) {
2811  ast_verb(3, "Channel %d echo canceler disabled due to CED detection\n", p->channel);
2813  break;
2814 #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
2816  ast_verb(3, "Channel %d detected a CED tone towards the network.\n", p->channel);
2817  break;
2819  ast_verb(3, "Channel %d detected a CED tone from the network.\n", p->channel);
2820  break;
2822  ast_verb(3, "Channel %d echo canceler disabled its NLP.\n", p->channel);
2823  break;
2825  ast_verb(3, "Channel %d echo canceler enabled its NLP.\n", p->channel);
2826  break;
2827 #endif
2829  /* Stop tone if there's a pulse start and the PBX isn't started */
2830  if (!ast_channel_pbx(ast))
2832  break;
2834  if (p->inalarm) {
2835  break;
2836  }
2837  x = analog_is_dialing(p, idx);
2838  if (!x) { /* if not still dialing in driver */
2840  if (p->echobreak) {
2842  ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
2845  p->echobreak = 0;
2846  } else {
2847  analog_set_dialing(p, 0);
2848  if ((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) {
2849  /* if thru with dialing after offhook */
2851  ast_setstate(ast, AST_STATE_UP);
2852  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
2854  break;
2855  } else { /* if to state wait for offhook to dial rest */
2856  /* we now wait for off hook */
2858  }
2859  }
2860  if (ast_channel_state(ast) == AST_STATE_DIALING) {
2861  if (analog_have_progressdetect(p)) {
2862  ast_debug(1, "Done dialing, but waiting for progress detection before doing more...\n");
2863  } else if (analog_check_confirmanswer(p) || (!p->dialednone
2864  && ((mysig == ANALOG_SIG_EM) || (mysig == ANALOG_SIG_EM_E1)
2865  || (mysig == ANALOG_SIG_EMWINK) || (mysig == ANALOG_SIG_FEATD)
2866  || (mysig == ANALOG_SIG_FEATDMF_TA) || (mysig == ANALOG_SIG_FEATDMF)
2867  || (mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA)
2868  || (mysig == ANALOG_SIG_FGC_CAMAMF) || (mysig == ANALOG_SIG_FEATB)
2869  || (mysig == ANALOG_SIG_SF) || (mysig == ANALOG_SIG_SFWINK)
2870  || (mysig == ANALOG_SIG_SF_FEATD) || (mysig == ANALOG_SIG_SF_FEATDMF)
2871  || (mysig == ANALOG_SIG_SF_FEATB)))) {
2873  } else if (!p->answeronpolarityswitch) {
2874  ast_setstate(ast, AST_STATE_UP);
2875  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
2877  /* If aops=0 and hops=1, this is necessary */
2878  p->polarity = POLARITY_REV;
2879  } else {
2880  /* Start clean, so we can catch the change to REV polarity when party answers */
2881  p->polarity = POLARITY_IDLE;
2882  }
2883  }
2884  }
2885  }
2886  break;
2887  case ANALOG_EVENT_ALARM:
2888  analog_set_alarm(p, 1);
2891  case ANALOG_EVENT_ONHOOK:
2892  ast_queue_control_data(ast, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
2893  ast_channel_hangupcause_hash_set(ast, cause_code, data_size);
2894  switch (p->sig) {
2895  case ANALOG_SIG_FXOLS:
2896  case ANALOG_SIG_FXOGS:
2897  case ANALOG_SIG_FXOKS:
2899  p->fxsoffhookstate = 0;
2900  p->onhooktime = time(NULL);
2901  p->msgstate = -1;
2902  /* Check for some special conditions regarding call waiting */
2903  if (idx == ANALOG_SUB_REAL) {
2904  /* The normal line was hung up */
2905  if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
2906  /* Need to hold the lock for real-call, private, and call-waiting call */
2908  if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
2909  /*
2910  * The call waiting call dissappeared.
2911  * This is now a normal hangup.
2912  */
2914  return NULL;
2915  }
2916 
2917  /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
2919  ast_verb(3, "Channel %d still has (callwait) call, ringing phone\n", p->channel);
2923  /* Don't start streaming audio yet if the incoming call isn't up yet */
2925  analog_set_dialing(p, 1);
2926  }
2927  /* Unlock the call-waiting call that we swapped to real-call. */
2929  analog_ring(p);
2930  } else if (p->subs[ANALOG_SUB_THREEWAY].owner) {
2931  unsigned int mssinceflash;
2932 
2933  /* Need to hold the lock for real-call, private, and 3-way call */
2935  if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
2936  ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
2937  /* Just hangup */
2938  return NULL;
2939  }
2940  if (p->owner != ast) {
2942  ast_log(LOG_WARNING, "This isn't good...\n");
2943  /* Just hangup */
2944  return NULL;
2945  }
2946 
2947  mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
2948  ast_debug(1, "Last flash was %u ms ago\n", mssinceflash);
2949  if (mssinceflash < MIN_MS_SINCE_FLASH) {
2950  /* It hasn't been long enough since the last flashook. This is probably a bounce on
2951  hanging up. Hangup both channels now */
2952  ast_debug(1, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
2956  } else if ((ast_channel_pbx(ast)) || (ast_channel_state(ast) == AST_STATE_UP)) {
2957  if (p->transfer) {
2958  /* In any case this isn't a threeway call anymore */
2961 
2962  /* Only attempt transfer if the phone is ringing; why transfer to busy tone eh? */
2963  if (!p->transfertobusy && ast_channel_state(ast) == AST_STATE_BUSY) {
2964  /* Swap subs and dis-own channel */
2966  /* Unlock the 3-way call that we swapped to real-call. */
2969  /* Ring the phone */
2970  analog_ring(p);
2971  } else if (!analog_attempt_transfer(p)) {
2972  /*
2973  * Transfer successful. Don't actually hang up at this point.
2974  * Let our channel legs of the calls die off as the transfer
2975  * percolates through the core.
2976  */
2977  break;
2978  }
2979  } else {
2982  }
2983  } else {
2984  /* Swap subs and dis-own channel */
2986  /* Unlock the 3-way call that we swapped to real-call. */
2989  /* Ring the phone */
2990  analog_ring(p);
2991  }
2992  }
2993  } else {
2994  ast_log(LOG_WARNING, "Got a hangup and my index is %u?\n", idx);
2995  }
2996  /* Fall through */
2997  default:
2999  return NULL;
3000  }
3001  break;
3003  if (p->inalarm) {
3004  break;
3005  }
3006  /* for E911, its supposed to wait for offhook then dial
3007  the second half of the dial string */
3008  if (((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) && (ast_channel_state(ast) == AST_STATE_DIALING_OFFHOOK)) {
3009  c = strchr(p->dialdest, '/');
3010  if (c) {
3011  c++;
3012  } else {
3013  c = p->dialdest;
3014  }
3015 
3016  if (*c) {
3017  int numchars = snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
3018  if (numchars >= sizeof(p->dop.dialstr)) {
3019  ast_log(LOG_WARNING, "Dial string '%s' truncated\n", c);
3020  }
3021  } else {
3022  ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
3023  }
3024 
3025  if (strlen(p->dop.dialstr) > 4) {
3026  memset(p->echorest, 'w', sizeof(p->echorest) - 1);
3027  strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
3028  p->echorest[sizeof(p->echorest) - 1] = '\0';
3029  p->echobreak = 1;
3030  p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
3031  } else {
3032  p->echobreak = 0;
3033  }
3034  if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
3035  analog_on_hook(p);
3036  return NULL;
3037  }
3038  analog_set_dialing(p, 1);
3039  return &p->subs[idx].f;
3040  }
3041  switch (p->sig) {
3042  case ANALOG_SIG_FXOLS:
3043  case ANALOG_SIG_FXOGS:
3044  case ANALOG_SIG_FXOKS:
3045  p->fxsoffhookstate = 1;
3046  switch (ast_channel_state(ast)) {
3047  case AST_STATE_RINGING:
3050  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
3052  /* Make sure it stops ringing */
3053  analog_set_needringing(p, 0);
3054  analog_off_hook(p);
3055  ast_debug(1, "channel %d answered\n", p->channel);
3056 
3057  /* Cancel any running CallerID spill */
3059 
3060  analog_set_dialing(p, 0);
3061  p->callwaitcas = 0;
3062  if (analog_check_confirmanswer(p)) {
3063  /* Ignore answer if "confirm answer" is enabled */
3064  p->subs[idx].f.frametype = AST_FRAME_NULL;
3065  p->subs[idx].f.subclass.integer = 0;
3066  } else if (!ast_strlen_zero(p->dop.dialstr)) {
3067  /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */
3068  res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
3069  if (res) {
3070  p->dop.dialstr[0] = '\0';
3071  return NULL;
3072  } else {
3073  ast_debug(1, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
3074  p->subs[idx].f.frametype = AST_FRAME_NULL;
3075  p->subs[idx].f.subclass.integer = 0;
3076  analog_set_dialing(p, 1);
3077  }
3078  p->dop.dialstr[0] = '\0';
3080  } else {
3081  ast_setstate(ast, AST_STATE_UP);
3083  }
3084  return &p->subs[idx].f;
3085  case AST_STATE_DOWN:
3087  ast_channel_rings_set(ast, 1);
3088  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
3090  ast_debug(1, "channel %d picked up\n", p->channel);
3091  return &p->subs[idx].f;
3092  case AST_STATE_UP:
3093  /* Make sure it stops ringing */
3094  analog_off_hook(p);
3095  /* Okay -- probably call waiting */
3096  ast_queue_unhold(p->owner);
3097  break;
3098  case AST_STATE_RESERVED:
3099  /* Start up dialtone */
3100  if (analog_has_voicemail(p)) {
3102  } else {
3104  }
3105  break;
3106  default:
3107  ast_log(LOG_WARNING, "FXO phone off hook in weird state %u??\n", ast_channel_state(ast));
3108  }
3109  break;
3110  case ANALOG_SIG_FXSLS:
3111  case ANALOG_SIG_FXSGS:
3112  case ANALOG_SIG_FXSKS:
3113  if (ast_channel_state(ast) == AST_STATE_RING) {
3115  }
3116 
3117  /* Fall through */
3118  case ANALOG_SIG_EM:
3119  case ANALOG_SIG_EM_E1:
3120  case ANALOG_SIG_EMWINK:
3121  case ANALOG_SIG_FEATD:
3122  case ANALOG_SIG_FEATDMF:
3123  case ANALOG_SIG_FEATDMF_TA:
3124  case ANALOG_SIG_E911:
3125  case ANALOG_SIG_FGC_CAMA:
3126  case ANALOG_SIG_FGC_CAMAMF:
3127  case ANALOG_SIG_FEATB:
3128  case ANALOG_SIG_SF:
3129  case ANALOG_SIG_SFWINK:
3130  case ANALOG_SIG_SF_FEATD:
3131  case ANALOG_SIG_SF_FEATDMF:
3132  case ANALOG_SIG_SF_FEATB:
3133  switch (ast_channel_state(ast)) {
3134  case AST_STATE_PRERING:
3136  /* Fall through */
3137  case AST_STATE_DOWN:
3138  case AST_STATE_RING:
3139  ast_debug(1, "Ring detected\n");
3140  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
3142  break;
3143  case AST_STATE_RINGING:
3144  case AST_STATE_DIALING:
3145  if (p->outgoing) {
3146  ast_debug(1, "Line answered\n");
3147  if (analog_check_confirmanswer(p)) {
3148  p->subs[idx].f.frametype = AST_FRAME_NULL;
3149  p->subs[idx].f.subclass.integer = 0;
3150  } else {
3151  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
3153  ast_setstate(ast, AST_STATE_UP);
3154  }
3155  break;
3156  }
3157  /* Fall through */
3158  default:
3159  ast_log(LOG_WARNING, "Ring/Off-hook in strange state %u on channel %d\n", ast_channel_state(ast), p->channel);
3160  break;
3161  }
3162  break;
3163  default:
3164  ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
3165  break;
3166  }
3167  break;
3169  switch (p->sig) {
3170  case ANALOG_SIG_FXSLS:
3171  case ANALOG_SIG_FXSGS:
3172  case ANALOG_SIG_FXSKS:
3173  if (ast_channel_state(ast) == AST_STATE_RING) {
3175  }
3176  break;
3177  default:
3178  break;
3179  }
3180  break;
3182  if (p->inalarm) break;
3184  if (ast_channel_rings(ast) == p->cidrings) {
3185  analog_send_callerid(p, 0, &p->caller);
3186  }
3187 
3188  if (ast_channel_rings(ast) > p->cidrings) {
3190  p->callwaitcas = 0;
3191  }
3192  p->subs[idx].f.frametype = AST_FRAME_CONTROL;
3194  break;
3195  case ANALOG_EVENT_RINGERON:
3196  break;
3197  case ANALOG_EVENT_NOALARM:
3198  analog_set_alarm(p, 0);
3200  break;
3202  if (p->inalarm) {
3203  break;
3204  }
3205  /* Remember last time we got a flash-hook */
3206  gettimeofday(&p->flashtime, NULL);
3207  switch (mysig) {
3208  case ANALOG_SIG_FXOLS:
3209  case ANALOG_SIG_FXOGS:
3210  case ANALOG_SIG_FXOKS:
3211  ast_debug(1, "Winkflash, index: %u, normal: %d, callwait: %d, thirdcall: %d\n",
3213 
3214  /* Cancel any running CallerID spill */
3216  p->callwaitcas = 0;
3217 
3218  if (idx != ANALOG_SUB_REAL) {
3219  ast_log(LOG_WARNING, "Got flash hook with index %u on channel %d?!?\n", idx, p->channel);
3220  goto winkflashdone;
3221  }
3222 
3223  if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
3224  /* Need to hold the lock for real-call, private, and call-waiting call */
3226  if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
3227  /*
3228  * The call waiting call dissappeared.
3229  * Let's just ignore this flash-hook.
3230  */
3231  ast_log(LOG_NOTICE, "Whoa, the call-waiting call disappeared.\n");
3232  goto winkflashdone;
3233  }
3234 
3235  /* Swap to call-wait */
3239  ast_debug(1, "Making %s the new owner\n", ast_channel_name(p->owner));
3243  }
3245 
3246  /* Start music on hold if appropriate */
3247  if (!p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
3249  }
3252 
3253  /* Unlock the call-waiting call that we swapped to real-call. */
3255  } else if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
3256  if (!p->threewaycalling) {
3257  /* Just send a flash if no 3-way calling */
3259  goto winkflashdone;
3260  } else if (!analog_check_for_conference(p)) {
3261  ast_callid callid = 0;
3262  int callid_created;
3263  char cid_num[256];
3264  char cid_name[256];
3265 
3266  cid_num[0] = '\0';
3267  cid_name[0] = '\0';
3268  if (p->dahditrcallerid && p->owner) {
3270  && ast_channel_caller(p->owner)->id.number.str) {
3272  sizeof(cid_num));
3273  }
3275  && ast_channel_caller(p->owner)->id.name.str) {
3277  sizeof(cid_name));
3278  }
3279  }
3280  /* XXX This section needs much more error checking!!! XXX */
3281  /* Start a 3-way call if feasible */
3282  if (!((ast_channel_pbx(ast)) ||
3283  (ast_channel_state(ast) == AST_STATE_UP) ||
3284  (ast_channel_state(ast) == AST_STATE_RING))) {
3285  ast_debug(1, "Flash when call not up or ringing\n");
3286  goto winkflashdone;
3287  }
3289  ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
3290  goto winkflashdone;
3291  }
3292 
3293  callid_created = ast_callid_threadstorage_auto(&callid);
3294 
3295  /*
3296  * Make new channel
3297  *
3298  * We cannot hold the p or ast locks while creating a new
3299  * channel.
3300  */
3302  ast_channel_unlock(ast);
3304  ast_channel_lock(ast);
3306  if (!chan) {
3308  "Cannot allocate new call structure on channel %d\n",
3309  p->channel);
3311  ast_callid_threadstorage_auto_clean(callid, callid_created);
3312  goto winkflashdone;
3313  }
3314  if (p->dahditrcallerid) {
3315  if (!p->origcid_num) {
3316  p->origcid_num = ast_strdup(p->cid_num);
3317  }
3318  if (!p->origcid_name) {
3319  p->origcid_name = ast_strdup(p->cid_name);
3320  }
3321  ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
3322  ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
3323  }
3324  /* Swap things around between the three-way and real call */
3326  /* Disable echo canceller for better dialing */
3329  if (res) {
3330  ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
3331  }
3332  analog_set_new_owner(p, chan);
3333  p->ss_astchan = chan;
3334  if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, p)) {
3335  ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
3338  ast_hangup(chan);
3339  } else {
3340  ast_verb(3, "Started three way call on channel %d\n", p->channel);
3341 
3342  /* Start music on hold */
3344  }
3345  ast_callid_threadstorage_auto_clean(callid, callid_created);
3346  }
3347  } else {
3348  /* Already have a 3 way call */
3349  enum analog_sub orig_3way_sub;
3350 
3351  /* Need to hold the lock for real-call, private, and 3-way call */
3353  if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
3354  /*
3355  * The 3-way call dissappeared.
3356  * Let's just ignore this flash-hook.
3357  */
3358  ast_log(LOG_NOTICE, "Whoa, the 3-way call disappeared.\n");
3359  goto winkflashdone;
3360  }
3361  orig_3way_sub = ANALOG_SUB_THREEWAY;
3362 
3364  /* Call is already up, drop the last person */
3365  ast_debug(1, "Got flash with three way call up, dropping last call on %d\n", p->channel);
3366  /* If the primary call isn't answered yet, use it */
3369  /* Swap back -- we're dropping the real 3-way that isn't finished yet*/
3371  orig_3way_sub = ANALOG_SUB_REAL;
3373  }
3374  /* Drop the last call and stop the conference */
3375  ast_verb(3, "Dropping three-way call on %s\n", ast_channel_name(p->subs[ANALOG_SUB_THREEWAY].owner));
3379  } else {
3380  /* Lets see what we're up to */
3381  if (((ast_channel_pbx(ast)) || (ast_channel_state(ast) == AST_STATE_UP)) &&
3382  (p->transfertobusy || (ast_channel_state(ast) != AST_STATE_BUSY))) {
3383  ast_verb(3, "Building conference call with %s and %s\n",
3386  /* Put them in the threeway, and flip */
3389  if (ast_channel_state(ast) == AST_STATE_UP) {
3391  orig_3way_sub = ANALOG_SUB_REAL;
3392  }
3393  ast_queue_unhold(p->subs[orig_3way_sub].owner);
3395  } else {
3396  ast_verb(3, "Dumping incomplete call on %s\n", ast_channel_name(p->subs[ANALOG_SUB_THREEWAY].owner));
3398  orig_3way_sub = ANALOG_SUB_REAL;
3403  }
3404  }
3405  ast_channel_unlock(p->subs[orig_3way_sub].owner);
3406  }
3407 winkflashdone:
3408  analog_update_conf(p);
3409  break;
3410  case ANALOG_SIG_EM:
3411  case ANALOG_SIG_EM_E1:
3412  case ANALOG_SIG_FEATD:
3413  case ANALOG_SIG_SF:
3414  case ANALOG_SIG_SFWINK:
3415  case ANALOG_SIG_SF_FEATD:
3416  case ANALOG_SIG_FXSLS:
3417  case ANALOG_SIG_FXSGS:
3418  if (p->dialing) {
3419  ast_debug(1, "Ignoring wink on channel %d\n", p->channel);
3420  } else {
3421  ast_debug(1, "Got wink in weird state %u on channel %d\n", ast_channel_state(ast), p->channel);
3422  }
3423  break;
3424  case ANALOG_SIG_FEATDMF_TA:
3425  switch (p->whichwink) {
3426  case 0:
3427  ast_debug(1, "ANI2 set to '%d' and ANI is '%s'\n", ast_channel_caller(p->owner)->ani2,
3429  ast_channel_caller(p->owner)->ani.number.str, ""));
3430  snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#",
3433  ast_channel_caller(p->owner)->ani.number.str, ""));
3434  break;
3435  case 1:
3436  ast_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr));
3437  break;
3438  case 2:
3439  ast_log(LOG_WARNING, "Received unexpected wink on channel of type ANALOG_SIG_FEATDMF_TA\n");
3440  return NULL;
3441  }
3442  p->whichwink++;
3443  /* Fall through */
3444  case ANALOG_SIG_FEATDMF:
3445  case ANALOG_SIG_E911:
3446  case ANALOG_SIG_FGC_CAMAMF:
3447  case ANALOG_SIG_FGC_CAMA:
3448  case ANALOG_SIG_FEATB:
3449  case ANALOG_SIG_SF_FEATDMF:
3450  case ANALOG_SIG_SF_FEATB:
3451  case ANALOG_SIG_EMWINK:
3452  /* FGD MF and EMWINK *Must* wait for wink */
3453  if (!ast_strlen_zero(p->dop.dialstr)) {
3454  res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
3455  if (res) {
3456  p->dop.dialstr[0] = '\0';
3457  return NULL;
3458  } else {
3459  ast_debug(1, "Sent deferred digit string on channel %d: %s\n", p->channel, p->dop.dialstr);
3460  }
3461  }
3462  p->dop.dialstr[0] = '\0';
3463  break;
3464  default:
3465  ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
3466  }
3467  break;
3469  if (p->inalarm) break;
3470  if (analog_check_waitingfordt(p)) {
3471  break;
3472  }
3473  switch (mysig) {
3474  case ANALOG_SIG_FXSLS: /* only interesting for FXS */
3475  case ANALOG_SIG_FXSGS:
3476  case ANALOG_SIG_FXSKS:
3477  case ANALOG_SIG_EM:
3478  case ANALOG_SIG_EM_E1:
3479  case ANALOG_SIG_EMWINK:
3480  case ANALOG_SIG_FEATD:
3481  case ANALOG_SIG_SF:
3482  case ANALOG_SIG_SFWINK:
3483  case ANALOG_SIG_SF_FEATD:
3484  if (!ast_strlen_zero(p->dop.dialstr)) {
3485  res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
3486  if (res) {
3487  p->dop.dialstr[0] = '\0';
3488  return NULL;
3489  } else {
3490  ast_debug(1, "Sent deferred digit string on channel %d: %s\n", p->channel, p->dop.dialstr);
3491  }
3492  }
3493  p->dop.dialstr[0] = '\0';
3495  break;
3496  case ANALOG_SIG_FEATDMF:
3497  case ANALOG_SIG_FEATDMF_TA:
3498  case ANALOG_SIG_E911:
3499  case ANALOG_SIG_FGC_CAMA:
3500  case ANALOG_SIG_FGC_CAMAMF:
3501  case ANALOG_SIG_FEATB:
3502  case ANALOG_SIG_SF_FEATDMF:
3503  case ANALOG_SIG_SF_FEATB:
3504  ast_debug(1, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
3505  break;
3506  default:
3507  break;
3508  }
3509  break;
3510  case ANALOG_EVENT_POLARITY:
3511  /*
3512  * If we get a Polarity Switch event, this could be
3513  * due to line seizure, remote end connect or remote end disconnect.
3514  *
3515  * Check to see if we should change the polarity state and
3516  * mark the channel as UP or if this is an indication
3517  * of remote end disconnect.
3518  */
3519 
3520  if (p->polarityonanswerdelay > 0) {
3521  /* check if event is not too soon after OffHook or Answer */
3523  switch (ast_channel_state(ast)) {
3524  case AST_STATE_DIALING: /*!< Digits (or equivalent) have been dialed */
3525  case AST_STATE_RINGING: /*!< Remote end is ringing */
3526  if (p->answeronpolarityswitch) {
3527  ast_debug(1, "Answering on polarity switch! channel %d\n", p->channel);
3529  p->polarity = POLARITY_REV;
3530  if (p->hanguponpolarityswitch) {
3531  p->polaritydelaytv = ast_tvnow();
3532  }
3533  } else {
3534  ast_debug(1, "Ignore Answer on polarity switch, channel %d\n", p->channel);
3535  }
3536  break;
3537 
3538  case AST_STATE_UP: /*!< Line is up */
3539  case AST_STATE_RING: /*!< Line is ringing */
3540  if (p->hanguponpolarityswitch) {
3541  ast_debug(1, "HangingUp on polarity switch! channel %d\n", p->channel);
3542  ast_queue_control_data(ast, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
3543  ast_channel_hangupcause_hash_set(ast, cause_code, data_size);
3545  p->polarity = POLARITY_IDLE;
3546  } else {
3547  ast_debug(1, "Ignore Hangup on polarity switch, channel %d\n", p->channel);
3548  }
3549  break;
3550 
3551  case AST_STATE_DOWN: /*!< Channel is down and available */
3552  case AST_STATE_RESERVED: /*!< Channel is down, but reserved */
3553  case AST_STATE_OFFHOOK: /*!< Channel is off hook */
3554  case AST_STATE_BUSY: /*!< Line is busy */
3555  case AST_STATE_DIALING_OFFHOOK: /*!< Digits (or equivalent) have been dialed while offhook */
3556  case AST_STATE_PRERING: /*!< Channel has detected an incoming call and is waiting for ring */
3557  default:
3559  ast_debug(1, "Ignoring Polarity switch on channel %d, state %u\n", p->channel, ast_channel_state(ast));
3560  }
3561  break;
3562  }
3563 
3564  } else {
3565  /* event is too soon after OffHook or Answer */
3566  switch (ast_channel_state(ast)) {
3567  case AST_STATE_DIALING: /*!< Digits (or equivalent) have been dialed */
3568  case AST_STATE_RINGING: /*!< Remote end is ringing */
3569  if (p->answeronpolarityswitch) {
3570  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));
3571  }
3572  break;
3573 
3574  case AST_STATE_UP: /*!< Line is up */
3575  case AST_STATE_RING: /*!< Line is ringing */
3576  if (p->hanguponpolarityswitch) {
3577  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));
3578  }
3579  break;
3580 
3581  default:
3583  ast_debug(1, "Polarity switch detected (too close to previous event) on channel %d, state %u\n", p->channel, ast_channel_state(ast));
3584  }
3585  break;
3586  }
3587  }
3588  }
3589 
3590  /* Added more log_debug information below to provide a better indication of what is going on */
3591  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) );
3592  break;
3593  default:
3594  ast_debug(1, "Dunno what to do with event %d on channel %d\n", res, p->channel);
3595  }
3596  return &p->subs[idx].f;
3597 }
3598 
3599 struct ast_frame *analog_exception(struct analog_pvt *p, struct ast_channel *ast)
3600 {
3601  int res;
3602  int idx;
3603  struct ast_frame *f;
3604 
3605  ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
3606 
3607  idx = analog_get_index(ast, p, 1);
3608  if (idx < 0) {
3609  idx = ANALOG_SUB_REAL;
3610  }
3611 
3612  p->subs[idx].f.frametype = AST_FRAME_NULL;
3613  p->subs[idx].f.datalen = 0;
3614  p->subs[idx].f.samples = 0;
3615  p->subs[idx].f.mallocd = 0;
3616  p->subs[idx].f.offset = 0;
3617  p->subs[idx].f.subclass.integer = 0;
3618  p->subs[idx].f.delivery = ast_tv(0,0);
3619  p->subs[idx].f.src = "dahdi_exception";
3620  p->subs[idx].f.data.ptr = NULL;
3621 
3622  if (!p->owner) {
3623  /* If nobody owns us, absorb the event appropriately, otherwise
3624  we loop indefinitely. This occurs when, during call waiting, the
3625  other end hangs up our channel so that it no longer exists, but we
3626  have neither FLASH'd nor ONHOOK'd to signify our desire to
3627  change to the other channel. */
3628  res = analog_get_event(p);
3629 
3630  /* Switch to real if there is one and this isn't something really silly... */
3631  if ((res != ANALOG_EVENT_RINGEROFF) && (res != ANALOG_EVENT_RINGERON) &&
3632  (res != ANALOG_EVENT_HOOKCOMPLETE)) {
3633  ast_debug(1, "Restoring owner of channel %d on event %d\n", p->channel, res);
3635  if (p->owner && ast != p->owner) {
3636  /*
3637  * Could this even happen?
3638  * Possible deadlock because we do not have the real-call lock.
3639  */
3640  ast_log(LOG_WARNING, "Event %s on %s is not restored owner %s\n",
3642  }
3643  if (p->owner) {
3644  ast_queue_unhold(p->owner);
3645  }
3646  }
3647  switch (res) {
3648  case ANALOG_EVENT_ONHOOK:
3650  if (p->owner) {
3651  ast_verb(3, "Channel %s still has call, ringing phone\n", ast_channel_name(p->owner));
3652  analog_ring(p);
3654  } else {
3655  ast_log(LOG_WARNING, "Absorbed %s, but nobody is left!?!?\n",
3656  analog_event2str(res));
3657  }
3658  analog_update_conf(p);
3659  break;
3662  analog_off_hook(p);
3663  if (p->owner && (ast_channel_state(p->owner) == AST_STATE_RINGING)) {
3665  analog_set_dialing(p, 0);
3666  }
3667  break;
3669  case ANALOG_EVENT_RINGERON:
3671  /* Do nothing */
3672  break;
3674  gettimeofday(&p->flashtime, NULL);
3675  if (p->owner) {
3676  ast_verb(3, "Channel %d flashed to other channel %s\n", p->channel, ast_channel_name(p->owner));
3677  if (ast_channel_state(p->owner) != AST_STATE_UP) {
3678  /* Answer if necessary */
3681  }
3683  ast_queue_unhold(p->owner);
3684  } else {
3685  ast_log(LOG_WARNING, "Absorbed %s, but nobody is left!?!?\n",
3686  analog_event2str(res));
3687  }
3688  analog_update_conf(p);
3689  break;
3690  default:
3691  ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", analog_event2str(res));
3692  break;
3693  }
3694  f = &p->subs[idx].f;
3695  return f;
3696  }
3697  ast_debug(1, "Exception on %d, channel %d\n", ast_channel_fd(ast, 0), p->channel);
3698  /* If it's not us, return NULL immediately */
3699  if (ast != p->owner) {
3700  ast_log(LOG_WARNING, "We're %s, not %s\n", ast_channel_name(ast), ast_channel_name(p->owner));
3701  f = &p->subs[idx].f;
3702  return f;
3703  }
3704 
3705  f = __analog_handle_event(p, ast);
3706  if (!f) {
3707  const char *name = ast_strdupa(ast_channel_name(ast));
3708 
3709  /* Tell the CDR this DAHDI device hung up */
3711  ast_channel_unlock(ast);
3712  ast_set_hangupsource(ast, name, 0);
3713  ast_channel_lock(ast);
3715  }
3716  return f;
3717 }
3718 
3720 {
3721  int res;
3722  pthread_t threadid;
3723  struct ast_channel *chan;
3724  ast_callid callid = 0;
3725  int callid_created;
3726 
3727  ast_debug(1, "channel (%d) - signaling (%d) - event (%s)\n",
3728  i->channel, i->sig, analog_event2str(event));
3729 
3730  /* Handle an event on a given channel for the monitor thread. */
3731  switch (event) {
3734  if (i->inalarm) {
3735  break;
3736  }
3737  /* Got a ring/answer. What kind of channel are we? */
3738  switch (i->sig) {
3739  case ANALOG_SIG_FXOLS:
3740  case ANALOG_SIG_FXOGS:
3741  case ANALOG_SIG_FXOKS:
3742  res = analog_off_hook(i);
3743  i->fxsoffhookstate = 1;
3744  if (res && (errno == EBUSY)) {
3745  break;
3746  }
3747  callid_created = ast_callid_threadstorage_auto(&callid);
3748 
3749  /* Cancel VMWI spill */
3751 
3752  if (i->immediate) {
3754  /* The channel is immediately up. Start right away */
3757  if (!chan) {
3758  ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
3760  if (res < 0) {
3761  ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
3762  }
3763  }
3764  } else {
3765  /* Check for callerid, digits, etc */
3767  i->ss_astchan = chan;
3768  if (chan) {
3769  if (analog_has_voicemail(i)) {
3771  } else {
3773  }
3774  if (res < 0)
3775  ast_log(LOG_WARNING, "Unable to play dialtone on channel %d, do you have defaultzone and loadzone defined?\n", i->channel);
3776 
3777  if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
3778  ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
3780  if (res < 0) {
3781  ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
3782  }
3783  ast_hangup(chan);
3784  }
3785  } else
3786  ast_log(LOG_WARNING, "Unable to create channel\n");
3787  }
3789  break;
3790  case ANALOG_SIG_FXSLS:
3791  case ANALOG_SIG_FXSGS:
3792  case ANALOG_SIG_FXSKS:
3794  /* Fall through */
3795  case ANALOG_SIG_EMWINK:
3796  case ANALOG_SIG_FEATD:
3797  case ANALOG_SIG_FEATDMF:
3798  case ANALOG_SIG_FEATDMF_TA:
3799  case ANALOG_SIG_E911:
3800  case ANALOG_SIG_FGC_CAMA:
3801  case ANALOG_SIG_FGC_CAMAMF:
3802  case ANALOG_SIG_FEATB:
3803  case ANALOG_SIG_EM:
3804  case ANALOG_SIG_EM_E1:
3805  case ANALOG_SIG_SFWINK:
3806  case ANALOG_SIG_SF_FEATD:
3807  case ANALOG_SIG_SF_FEATDMF:
3808  case ANALOG_SIG_SF_FEATB:
3809  case ANALOG_SIG_SF:
3810  callid_created = ast_callid_threadstorage_auto(&callid);
3811  /* Check for callerid, digits, etc */
3814  } else {
3816  }
3817  i->ss_astchan = chan;
3818  if (!chan) {
3819  ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
3820  } else if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
3821  ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
3823  if (res < 0) {
3824  ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
3825  }
3826  ast_hangup(chan);
3827  }
3829  break;
3830  default:
3831  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);
3833  if (res < 0) {
3834  ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
3835  }
3836  return NULL;
3837  }
3838  break;
3839  case ANALOG_EVENT_NOALARM:
3840  analog_set_alarm(i, 0);
3842  break;
3843  case ANALOG_EVENT_ALARM:
3844  analog_set_alarm(i, 1);
3846  /* fall thru intentionally */
3847  case ANALOG_EVENT_ONHOOK:
3848  /* Back on hook. Hang up. */
3849  switch (i->sig) {
3850  case ANALOG_SIG_FXOLS:
3851  case ANALOG_SIG_FXOGS:
3852  i->fxsoffhookstate = 0;
3854  /* Fall through */
3855  case ANALOG_SIG_FEATD:
3856  case ANALOG_SIG_FEATDMF:
3857  case ANALOG_SIG_FEATDMF_TA:
3858  case ANALOG_SIG_E911:
3859  case ANALOG_SIG_FGC_CAMA:
3860  case ANALOG_SIG_FGC_CAMAMF:
3861  case ANALOG_SIG_FEATB:
3862  case ANALOG_SIG_EM:
3863  case ANALOG_SIG_EM_E1:
3864  case ANALOG_SIG_EMWINK:
3865  case ANALOG_SIG_SF_FEATD:
3866  case ANALOG_SIG_SF_FEATDMF:
3867  case ANALOG_SIG_SF_FEATB:
3868  case ANALOG_SIG_SF:
3869  case ANALOG_SIG_SFWINK:
3870  case ANALOG_SIG_FXSLS:
3871  case ANALOG_SIG_FXSGS:
3872  case ANALOG_SIG_FXSKS:
3874  res = analog_play_tone(i, ANALOG_SUB_REAL, -1);
3875  analog_on_hook(i);
3876  break;
3877  case ANALOG_SIG_FXOKS:
3878  i->fxsoffhookstate = 0;
3881  /* Diddle the battery for the zhone */
3882 #ifdef ZHONE_HACK
3883  analog_off_hook(i);
3884  usleep(1);
3885 #endif
3886  res = analog_play_tone(i, ANALOG_SUB_REAL, -1);
3887  analog_on_hook(i);
3888  break;
3889  default:
3890  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);
3891  res = analog_play_tone(i, ANALOG_SUB_REAL, -1);
3892  return NULL;
3893  }
3894  break;
3895  case ANALOG_EVENT_POLARITY:
3896  switch (i->sig) {
3897  case ANALOG_SIG_FXSLS:
3898  case ANALOG_SIG_FXSKS:
3899  case ANALOG_SIG_FXSGS:
3900  callid_created = ast_callid_threadstorage_auto(&callid);
3901  /* We have already got a PR before the channel was
3902  created, but it wasn't handled. We need polarity
3903  to be REV for remote hangup detection to work.
3904  At least in Spain */
3905  if (i->hanguponpolarityswitch) {
3906  i->polarity = POLARITY_REV;
3907  }
3909  i->polarity = POLARITY_REV;
3910  ast_verb(2, "Starting post polarity CID detection on channel %d\n",
3911  i->channel);
3913  i->ss_astchan = chan;
3914  if (!chan) {
3915  ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
3916  } else if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
3917  ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
3918  ast_hangup(chan);
3919  }
3920  }
3922  break;
3923  default:
3925  "handle_init_event detected polarity reversal on non-FXO (ANALOG_SIG_FXS) interface %d\n",
3926  i->channel);
3927  break;
3928  }
3929  break;
3930  case ANALOG_EVENT_DTMFCID:
3931  switch (i->sig) {
3932  case ANALOG_SIG_FXSLS:
3933  case ANALOG_SIG_FXSKS:
3934  case ANALOG_SIG_FXSGS:
3935  callid_created = ast_callid_threadstorage_auto(&callid);
3937  ast_verb(2, "Starting DTMF CID detection on channel %d\n",
3938  i->channel);
3940  i->ss_astchan = chan;
3941  if (!chan) {
3942  ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
3943  } else if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
3944  ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
3945  ast_hangup(chan);
3946  }
3947  }
3949  break;
3950  default:
3952  "handle_init_event detected dtmfcid generation event on non-FXO (ANALOG_SIG_FXS) interface %d\n",
3953  i->channel);
3954  break;
3955  }
3956  break;
3957  case ANALOG_EVENT_REMOVED: /* destroy channel, will actually do so in do_monitor */
3958  ast_log(LOG_NOTICE, "Got ANALOG_EVENT_REMOVED. Destroying channel %d\n",
3959  i->channel);
3960  return i->chan_pvt;
3963  break;
3966  break;
3967  }
3968  return NULL;
3969 }
3970 
3971 
3972 struct analog_pvt *analog_new(enum analog_sigtype signallingtype, void *private_data)
3973 {
3974  struct analog_pvt *p;
3975 
3976  p = ast_calloc(1, sizeof(*p));
3977  if (!p) {
3978  return p;
3979  }
3980 
3982  p->sig = signallingtype;
3983  p->chan_pvt = private_data;
3984 
3985  /* Some defaults for values */
3988  /* Sub real is assumed to always be alloc'd */
3989  p->subs[ANALOG_SUB_REAL].allocd = 1;
3990 
3991  return p;
3992 }
3993 
3994 /*!
3995  * \brief Delete the analog private structure.
3996  * \since 1.8
3997  *
3998  * \param doomed Analog private structure to delete.
3999  */
4000 void analog_delete(struct analog_pvt *doomed)
4001 {
4002  ast_free(doomed);
4003 }
4004 
4006 {
4007  /* No call waiting on non FXS channels */
4008  if ((p->sig != ANALOG_SIG_FXOKS) && (p->sig != ANALOG_SIG_FXOLS) && (p->sig != ANALOG_SIG_FXOGS)) {
4009  p->permcallwaiting = 0;
4010  }
4011 
4013 
4014  return 0;
4015 }
4016 
4017 void analog_free(struct analog_pvt *p)
4018 {
4019  ast_free(p);
4020 }
4021 
4022 /* called while dahdi_pvt is locked in dahdi_fixup */
4023 int analog_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, void *newp)
4024 {
4025  struct analog_pvt *new_pvt = newp;
4026  int x;
4027  ast_debug(1, "New owner for channel %d is %s\n", new_pvt->channel, ast_channel_name(newchan));
4028  if (new_pvt->owner == oldchan) {
4029  analog_set_new_owner(new_pvt, newchan);
4030  }
4031  for (x = 0; x < 3; x++) {
4032  if (new_pvt->subs[x].owner == oldchan) {
4033  new_pvt->subs[x].owner = newchan;
4034  }
4035  }
4036 
4037  analog_update_conf(new_pvt);
4038  return 0;
4039 }
4040 
4041 static void analog_publish_dnd_state(int channel, const char *status)
4042 {
4043  RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
4044  RAII_VAR(struct ast_str *, dahdichan, ast_str_create(32), ast_free);
4045  if (!dahdichan) {
4046  return;
4047  }
4048 
4049  ast_str_set(&dahdichan, 0, "DAHDI/%d", channel);
4050 
4051  body = ast_json_pack("{s: s, s: s}",
4052  "Channel", ast_str_buffer(dahdichan),
4053  "Status", status);
4054  if (!body) {
4055  return;
4056  }
4057 
4058  ast_manager_publish_event("DNDState", EVENT_FLAG_SYSTEM, body);
4059 }
4060 
4061 int analog_dnd(struct analog_pvt *p, int flag)
4062 {
4063  if (flag == -1) {
4064  return p->dnd;
4065  }
4066 
4067  p->dnd = flag;
4068 
4069  ast_verb(3, "%s DND on channel %d\n",
4070  flag ? "Enabled" : "Disabled",
4071  p->channel);
4072  analog_publish_dnd_state(p->channel, flag ? "enabled" : "disabled");
4073 
4074  return 0;
4075 }
jack_status_t status
Definition: app_jack.c:146
const char * str
Definition: app_jack.c:147
Persistent data storage (akin to *doze registry)
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:327
Asterisk main include file. File version handling, generic pbx functions.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
Bridging API.
ast_transfer_result
Definition: bridge.h:1098
@ AST_BRIDGE_TRANSFER_SUCCESS
Definition: bridge.h:1100
enum ast_transfer_result ast_bridge_transfer_attended(struct ast_channel *to_transferee, struct ast_channel *to_transfer_target)
Attended transfer.
Definition: bridge.c:4662
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
#define CID_SIG_V23_JP
Definition: callerid.h:62
#define CID_SIG_SMDI
Definition: callerid.h:63
void ast_shrink_phone_number(char *n)
Shrink a phone number in place to just digits (more accurately it just removes ()'s,...
Definition: callerid.c:947
#define CID_SIG_DTMF
Definition: callerid.h:61
#define CID_SIG_V23
Definition: callerid.h:60
void callerid_get_dtmf(char *cidstring, char *number, int *flags)
Get and parse DTMF-based callerid.
Definition: callerid.c:201
#define CID_SIG_BELL
Definition: callerid.h:59
Internal Asterisk hangup causes.
#define AST_CAUSE_NETWORK_OUT_OF_ORDER
Definition: causes.h:121
#define AST_CAUSE_NO_ANSWER
Definition: causes.h:109
#define AST_CAUSE_NORMAL_CLEARING
Definition: causes.h:106
#define AST_CC_GENERIC_MONITOR_TYPE
Definition: ccss.h:471
@ AST_CC_CCNR
Definition: ccss.h:38
@ AST_CC_MONITOR_NEVER
Definition: ccss.h:76
@ AST_CC_MONITOR_ALWAYS
Definition: ccss.h:87
@ AST_CC_MONITOR_NATIVE
Definition: ccss.h:78
@ AST_CC_MONITOR_GENERIC
Definition: ccss.h:83
enum ast_cc_monitor_policies ast_get_cc_monitor_policy(struct ast_cc_config_params *config)
Get the cc_monitor_policy.
Definition: ccss.c:884
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:4147
static int mute
Definition: chan_alsa.c:148
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:122
struct analog_callback analog_callbacks
Definition: chan_dahdi.c:3392
static char cid_num[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:167
static char cid_name[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:168
void ast_channel_exten_set(struct ast_channel *chan, const char *value)
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3176
void ast_channel_rings_set(struct ast_channel *chan, int value)
void ast_party_name_init(struct ast_party_name *init)
Initialize the given name structure.
Definition: channel.c:1585
struct ast_party_connected_line * ast_channel_connected(struct ast_channel *chan)
void ast_party_number_init(struct ast_party_number *init)
Initialize the given number structure.
Definition: channel.c:1638
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2542
int ast_channel_rings(const struct ast_channel *chan)
void * ast_channel_tech_pvt(const struct ast_channel *chan)
@ AST_SOFTHANGUP_EXPLICIT
Definition: channel.h:1148
@ AST_SOFTHANGUP_DEV
Definition: channel.h:1121
struct ast_pbx * ast_channel_pbx(const struct ast_channel *chan)
#define ast_channel_lock(chan)
Definition: channel.h:2922
const char * ast_channel_context(const struct ast_channel *chan)
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3163
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame without payload.
Definition: channel.c:1225
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2947
ast_callid ast_channel_callid(const struct ast_channel *chan)
const char * ast_channel_name(const struct ast_channel *chan)
#define ast_channel_trylock(chan)
Definition: channel.h:2924
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:10660
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:1232
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4292
void ast_party_name_free(struct ast_party_name *doomed)
Destroy the party name contents.
Definition: channel.c:1632
@ AST_FLAG_END_DTMF_ONLY
Definition: channel.h:1007
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:7417
int ast_channel_fd(const struct ast_channel *chan, int which)
struct ast_flags * ast_channel_flags(struct ast_channel *chan)