Add HANGUPCAUSE information to callee channels
[asterisk/asterisk.git] / channels / sig_analog.c
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/pbx.h"
38 #include "asterisk/file.h"
39 #include "asterisk/callerid.h"
40 #include "asterisk/say.h"
41 #include "asterisk/manager.h"
42 #include "asterisk/astdb.h"
43 #include "asterisk/features.h"
44 #include "asterisk/cel.h"
45 #include "asterisk/causes.h"
46
47 #include "sig_analog.h"
48
49 /*** DOCUMENTATION
50  ***/
51
52 /*! \note
53  * Define if you want to check the hook state for an FXO (FXS signalled) interface
54  * before dialing on it.  Certain FXO interfaces always think they're out of
55  * service with this method however.
56  */
57 /* #define DAHDI_CHECK_HOOKSTATE */
58
59 #define POLARITY_IDLE 0
60 #define POLARITY_REV    1
61 #define MIN_MS_SINCE_FLASH                      ( (2000) )      /*!< 2000 ms */
62 static int analog_matchdigittimeout = 3000;
63 static int analog_gendigittimeout = 8000;
64 static int analog_firstdigittimeout = 16000;
65 static char analog_defaultcic[64] = "";
66 static char analog_defaultozz[64] = "";
67
68 static const struct {
69         enum analog_sigtype sigtype;
70         const char const *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 const *name;
98 } cidtypes[] = {
99         { CID_SIG_BELL,   "bell" },
100         { CID_SIG_V23,    "v23" },
101         { CID_SIG_V23_JP, "v23_jp" },
102         { CID_SIG_DTMF,   "dtmf" },
103         /* "smdi" is intentionally not supported here, as there is a much better
104          * way to do this in the dialplan now. */
105 };
106
107 #define ISTRUNK(p) ((p->sig == ANALOG_SIG_FXSLS) || (p->sig == ANALOG_SIG_FXSKS) || \
108                                         (p->sig == ANALOG_SIG_FXSGS))
109
110 enum analog_sigtype analog_str_to_sigtype(const char *name)
111 {
112         int i;
113
114         for (i = 0; i < ARRAY_LEN(sigtypes); i++) {
115                 if (!strcasecmp(sigtypes[i].name, name)) {
116                         return sigtypes[i].sigtype;
117                 }
118         }
119
120         return 0;
121 }
122
123 const char *analog_sigtype_to_str(enum analog_sigtype sigtype)
124 {
125         int i;
126
127         for (i = 0; i < ARRAY_LEN(sigtypes); i++) {
128                 if (sigtype == sigtypes[i].sigtype) {
129                         return sigtypes[i].name;
130                 }
131         }
132
133         return "Unknown";
134 }
135
136 unsigned int analog_str_to_cidtype(const char *name)
137 {
138         int i;
139
140         for (i = 0; i < ARRAY_LEN(cidtypes); i++) {
141                 if (!strcasecmp(cidtypes[i].name, name)) {
142                         return cidtypes[i].cid_type;
143                 }
144         }
145
146         return 0;
147 }
148
149 const char *analog_cidtype_to_str(unsigned int cid_type)
150 {
151         int i;
152
153         for (i = 0; i < ARRAY_LEN(cidtypes); i++) {
154                 if (cid_type == cidtypes[i].cid_type) {
155                         return cidtypes[i].name;
156                 }
157         }
158
159         return "Unknown";
160 }
161
162 static int analog_start_cid_detect(struct analog_pvt *p, int cid_signalling)
163 {
164         if (analog_callbacks.start_cid_detect) {
165                 return analog_callbacks.start_cid_detect(p->chan_pvt, cid_signalling);
166         }
167         return -1;
168 }
169
170 static int analog_stop_cid_detect(struct analog_pvt *p)
171 {
172         if (analog_callbacks.stop_cid_detect) {
173                 return analog_callbacks.stop_cid_detect(p->chan_pvt);
174         }
175         return -1;
176 }
177
178 static int analog_get_callerid(struct analog_pvt *p, char *name, char *number, enum analog_event *ev, size_t timeout)
179 {
180         if (analog_callbacks.get_callerid) {
181                 return analog_callbacks.get_callerid(p->chan_pvt, name, number, ev, timeout);
182         }
183         return -1;
184 }
185
186 static const char *analog_get_orig_dialstring(struct analog_pvt *p)
187 {
188         if (analog_callbacks.get_orig_dialstring) {
189                 return analog_callbacks.get_orig_dialstring(p->chan_pvt);
190         }
191         return "";
192 }
193
194 static int analog_get_event(struct analog_pvt *p)
195 {
196         if (analog_callbacks.get_event) {
197                 return analog_callbacks.get_event(p->chan_pvt);
198         }
199         return -1;
200 }
201
202 static int analog_wait_event(struct analog_pvt *p)
203 {
204         if (analog_callbacks.wait_event) {
205                 return analog_callbacks.wait_event(p->chan_pvt);
206         }
207         return -1;
208 }
209
210 static int analog_have_progressdetect(struct analog_pvt *p)
211 {
212         if (analog_callbacks.have_progressdetect) {
213                 return analog_callbacks.have_progressdetect(p->chan_pvt);
214         }
215         /* Don't have progress detection. */
216         return 0;
217 }
218
219 enum analog_cid_start analog_str_to_cidstart(const char *value)
220 {
221         if (!strcasecmp(value, "ring")) {
222                 return ANALOG_CID_START_RING;
223         } else if (!strcasecmp(value, "polarity")) {
224                 return ANALOG_CID_START_POLARITY;
225         } else if (!strcasecmp(value, "polarity_in")) {
226                 return ANALOG_CID_START_POLARITY_IN;
227         } else if (!strcasecmp(value, "dtmf")) {
228                 return ANALOG_CID_START_DTMF_NOALERT;
229         }
230
231         return 0;
232 }
233
234 const char *analog_cidstart_to_str(enum analog_cid_start cid_start)
235 {
236         switch (cid_start) {
237         case ANALOG_CID_START_RING:
238                 return "Ring";
239         case ANALOG_CID_START_POLARITY:
240                 return "Polarity";
241         case ANALOG_CID_START_POLARITY_IN:
242                 return "Polarity_In";
243         case ANALOG_CID_START_DTMF_NOALERT:
244                 return "DTMF";
245         }
246
247         return "Unknown";
248 }
249
250 static char *analog_event2str(enum analog_event event)
251 {
252         char *res;
253         switch (event) {
254         case ANALOG_EVENT_ONHOOK:
255                 res = "ANALOG_EVENT_ONHOOK";
256                 break;
257         case ANALOG_EVENT_RINGOFFHOOK:
258                 res = "ANALOG_EVENT_RINGOFFHOOK";
259                 break;
260         case ANALOG_EVENT_WINKFLASH:
261                 res = "ANALOG_EVENT_WINKFLASH";
262                 break;
263         case ANALOG_EVENT_ALARM:
264                 res = "ANALOG_EVENT_ALARM";
265                 break;
266         case ANALOG_EVENT_NOALARM:
267                 res = "ANALOG_EVENT_NOALARM";
268                 break;
269         case ANALOG_EVENT_DIALCOMPLETE:
270                 res = "ANALOG_EVENT_DIALCOMPLETE";
271                 break;
272         case ANALOG_EVENT_HOOKCOMPLETE:
273                 res = "ANALOG_EVENT_HOOKCOMPLETE";
274                 break;
275         case ANALOG_EVENT_PULSE_START:
276                 res = "ANALOG_EVENT_PULSE_START";
277                 break;
278         case ANALOG_EVENT_POLARITY:
279                 res = "ANALOG_EVENT_POLARITY";
280                 break;
281         case ANALOG_EVENT_RINGBEGIN:
282                 res = "ANALOG_EVENT_RINGBEGIN";
283                 break;
284         case ANALOG_EVENT_EC_DISABLED:
285                 res = "ANALOG_EVENT_EC_DISABLED";
286                 break;
287         case ANALOG_EVENT_RINGERON:
288                 res = "ANALOG_EVENT_RINGERON";
289                 break;
290         case ANALOG_EVENT_RINGEROFF:
291                 res = "ANALOG_EVENT_RINGEROFF";
292                 break;
293         case ANALOG_EVENT_REMOVED:
294                 res = "ANALOG_EVENT_REMOVED";
295                 break;
296         case ANALOG_EVENT_NEONMWI_ACTIVE:
297                 res = "ANALOG_EVENT_NEONMWI_ACTIVE";
298                 break;
299         case ANALOG_EVENT_NEONMWI_INACTIVE:
300                 res = "ANALOG_EVENT_NEONMWI_INACTIVE";
301                 break;
302 #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
303         case ANALOG_EVENT_TX_CED_DETECTED:
304                 res = "ANALOG_EVENT_TX_CED_DETECTED";
305                 break;
306         case ANALOG_EVENT_RX_CED_DETECTED:
307                 res = "ANALOG_EVENT_RX_CED_DETECTED";
308                 break;
309         case ANALOG_EVENT_EC_NLP_DISABLED:
310                 res = "ANALOG_EVENT_EC_NLP_DISABLED";
311                 break;
312         case ANALOG_EVENT_EC_NLP_ENABLED:
313                 res = "ANALOG_EVENT_EC_NLP_ENABLED";
314                 break;
315 #endif
316         case ANALOG_EVENT_PULSEDIGIT:
317                 res = "ANALOG_EVENT_PULSEDIGIT";
318                 break;
319         case ANALOG_EVENT_DTMFDOWN:
320                 res = "ANALOG_EVENT_DTMFDOWN";
321                 break;
322         case ANALOG_EVENT_DTMFUP:
323                 res = "ANALOG_EVENT_DTMFUP";
324                 break;
325         default:
326                 res = "UNKNOWN/OTHER";
327                 break;
328         }
329
330         return res;
331 }
332
333 static void analog_swap_subs(struct analog_pvt *p, enum analog_sub a, enum analog_sub b)
334 {
335         int tinthreeway;
336         struct ast_channel *towner;
337
338         ast_debug(1, "Swapping %d and %d\n", a, b);
339
340         towner = p->subs[a].owner;
341         p->subs[a].owner = p->subs[b].owner;
342         p->subs[b].owner = towner;
343
344         tinthreeway = p->subs[a].inthreeway;
345         p->subs[a].inthreeway = p->subs[b].inthreeway;
346         p->subs[b].inthreeway = tinthreeway;
347
348         if (analog_callbacks.swap_subs) {
349                 analog_callbacks.swap_subs(p->chan_pvt, a, p->subs[a].owner, b, p->subs[b].owner);
350         }
351 }
352
353 static int analog_alloc_sub(struct analog_pvt *p, enum analog_sub x)
354 {
355         if (analog_callbacks.allocate_sub) {
356                 int res;
357                 res = analog_callbacks.allocate_sub(p->chan_pvt, x);
358                 if (!res) {
359                         p->subs[x].allocd = 1;
360                 }
361                 return res;
362         }
363         return 0;
364 }
365
366 static int analog_unalloc_sub(struct analog_pvt *p, enum analog_sub x)
367 {
368         p->subs[x].allocd = 0;
369         p->subs[x].owner = NULL;
370         if (analog_callbacks.unallocate_sub) {
371                 return analog_callbacks.unallocate_sub(p->chan_pvt, x);
372         }
373         return 0;
374 }
375
376 static int analog_send_callerid(struct analog_pvt *p, int cwcid, struct ast_party_caller *caller)
377 {
378         ast_debug(1, "Sending callerid.  CID_NAME: '%s' CID_NUM: '%s'\n",
379                 caller->id.name.str,
380                 caller->id.number.str);
381
382         if (cwcid) {
383                 p->callwaitcas = 0;
384         }
385
386         if (analog_callbacks.send_callerid) {
387                 return analog_callbacks.send_callerid(p->chan_pvt, cwcid, caller);
388         }
389         return 0;
390 }
391
392 #define analog_get_index(ast, p, nullok)        _analog_get_index(ast, p, nullok, __PRETTY_FUNCTION__, __LINE__)
393 static int _analog_get_index(struct ast_channel *ast, struct analog_pvt *p, int nullok, const char *fname, unsigned long line)
394 {
395         int res;
396         if (p->subs[ANALOG_SUB_REAL].owner == ast) {
397                 res = ANALOG_SUB_REAL;
398         } else if (p->subs[ANALOG_SUB_CALLWAIT].owner == ast) {
399                 res = ANALOG_SUB_CALLWAIT;
400         } else if (p->subs[ANALOG_SUB_THREEWAY].owner == ast) {
401                 res = ANALOG_SUB_THREEWAY;
402         } else {
403                 res = -1;
404                 if (!nullok) {
405                         ast_log(LOG_WARNING,
406                                 "Unable to get index for '%s' on channel %d (%s(), line %lu)\n",
407                                 ast ? ast_channel_name(ast) : "", p->channel, fname, line);
408                 }
409         }
410         return res;
411 }
412
413 static int analog_dsp_reset_and_flush_digits(struct analog_pvt *p)
414 {
415         if (analog_callbacks.dsp_reset_and_flush_digits) {
416                 return analog_callbacks.dsp_reset_and_flush_digits(p->chan_pvt);
417         }
418
419         /* Return 0 since I think this is unnecessary to do in most cases it is used.  Mostly only for ast_dsp */
420         return 0;
421 }
422
423 static int analog_play_tone(struct analog_pvt *p, enum analog_sub sub, enum analog_tone tone)
424 {
425         if (analog_callbacks.play_tone) {
426                 return analog_callbacks.play_tone(p->chan_pvt, sub, tone);
427         }
428         return -1;
429 }
430
431 static void analog_set_new_owner(struct analog_pvt *p, struct ast_channel *new_owner)
432 {
433         p->owner = new_owner;
434         if (analog_callbacks.set_new_owner) {
435                 analog_callbacks.set_new_owner(p->chan_pvt, new_owner);
436         }
437 }
438
439 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)
440 {
441         struct ast_channel *c;
442
443         if (!analog_callbacks.new_ast_channel) {
444                 return NULL;
445         }
446
447         c = analog_callbacks.new_ast_channel(p->chan_pvt, state, startpbx, sub, requestor);
448         if (c) {
449                 ast_channel_call_forward_set(c, p->call_forward);
450         }
451         p->subs[sub].owner = c;
452         if (!p->owner) {
453                 analog_set_new_owner(p, c);
454         }
455         return c;
456 }
457
458 static int analog_set_echocanceller(struct analog_pvt *p, int enable)
459 {
460         if (analog_callbacks.set_echocanceller) {
461                 return analog_callbacks.set_echocanceller(p->chan_pvt, enable);
462         }
463         return -1;
464 }
465
466 static int analog_train_echocanceller(struct analog_pvt *p)
467 {
468         if (analog_callbacks.train_echocanceller) {
469                 return analog_callbacks.train_echocanceller(p->chan_pvt);
470         }
471         return -1;
472 }
473
474 static int analog_is_off_hook(struct analog_pvt *p)
475 {
476         if (analog_callbacks.is_off_hook) {
477                 return analog_callbacks.is_off_hook(p->chan_pvt);
478         }
479         return -1;
480 }
481
482 static int analog_ring(struct analog_pvt *p)
483 {
484         if (analog_callbacks.ring) {
485                 return analog_callbacks.ring(p->chan_pvt);
486         }
487         return -1;
488 }
489
490 static int analog_flash(struct analog_pvt *p)
491 {
492         if (analog_callbacks.flash) {
493                 return analog_callbacks.flash(p->chan_pvt);
494         }
495         return -1;
496 }
497
498 static int analog_start(struct analog_pvt *p)
499 {
500         if (analog_callbacks.start) {
501                 return analog_callbacks.start(p->chan_pvt);
502         }
503         return -1;
504 }
505
506 static int analog_dial_digits(struct analog_pvt *p, enum analog_sub sub, struct analog_dialoperation *dop)
507 {
508         if (analog_callbacks.dial_digits) {
509                 return analog_callbacks.dial_digits(p->chan_pvt, sub, dop);
510         }
511         return -1;
512 }
513
514 static int analog_on_hook(struct analog_pvt *p)
515 {
516         if (analog_callbacks.on_hook) {
517                 return analog_callbacks.on_hook(p->chan_pvt);
518         }
519         return -1;
520 }
521
522 static void analog_set_outgoing(struct analog_pvt *p, int is_outgoing)
523 {
524         p->outgoing = is_outgoing;
525         if (analog_callbacks.set_outgoing) {
526                 analog_callbacks.set_outgoing(p->chan_pvt, is_outgoing);
527         }
528 }
529
530 static int analog_check_for_conference(struct analog_pvt *p)
531 {
532         if (analog_callbacks.check_for_conference) {
533                 return analog_callbacks.check_for_conference(p->chan_pvt);
534         }
535         return -1;
536 }
537
538 static void analog_all_subchannels_hungup(struct analog_pvt *p)
539 {
540         if (analog_callbacks.all_subchannels_hungup) {
541                 analog_callbacks.all_subchannels_hungup(p->chan_pvt);
542         }
543 }
544
545 static void analog_unlock_private(struct analog_pvt *p)
546 {
547         if (analog_callbacks.unlock_private) {
548                 analog_callbacks.unlock_private(p->chan_pvt);
549         }
550 }
551
552 static void analog_lock_private(struct analog_pvt *p)
553 {
554         if (analog_callbacks.lock_private) {
555                 analog_callbacks.lock_private(p->chan_pvt);
556         }
557 }
558
559 static void analog_deadlock_avoidance_private(struct analog_pvt *p)
560 {
561         if (analog_callbacks.deadlock_avoidance_private) {
562                 analog_callbacks.deadlock_avoidance_private(p->chan_pvt);
563         } else {
564                 /* Fallback to manual avoidance if callback not present. */
565                 analog_unlock_private(p);
566                 usleep(1);
567                 analog_lock_private(p);
568         }
569 }
570
571 /*!
572  * \internal
573  * \brief Obtain the specified subchannel owner lock if the owner exists.
574  *
575  * \param pvt Analog private struct.
576  * \param sub_idx Subchannel owner to lock.
577  *
578  * \note Assumes the analog_lock_private(pvt->chan_pvt) is already obtained.
579  *
580  * \note
581  * Because deadlock avoidance may have been necessary, you need to confirm
582  * the state of things before continuing.
583  *
584  * \return Nothing
585  */
586 static void analog_lock_sub_owner(struct analog_pvt *pvt, enum analog_sub sub_idx)
587 {
588         for (;;) {
589                 if (!pvt->subs[sub_idx].owner) {
590                         /* No subchannel owner pointer */
591                         break;
592                 }
593                 if (!ast_channel_trylock(pvt->subs[sub_idx].owner)) {
594                         /* Got subchannel owner lock */
595                         break;
596                 }
597                 /* We must unlock the private to avoid the possibility of a deadlock */
598                 analog_deadlock_avoidance_private(pvt);
599         }
600 }
601
602 static int analog_off_hook(struct analog_pvt *p)
603 {
604         if (analog_callbacks.off_hook) {
605                 return analog_callbacks.off_hook(p->chan_pvt);
606         }
607         return -1;
608 }
609
610 static void analog_set_needringing(struct analog_pvt *p, int value)
611 {
612         if (analog_callbacks.set_needringing) {
613                 analog_callbacks.set_needringing(p->chan_pvt, value);
614         }
615 }
616
617 #if 0
618 static void analog_set_polarity(struct analog_pvt *p, int value)
619 {
620         if (analog_callbacks.set_polarity) {
621                 analog_callbacks.set_polarity(p->chan_pvt, value);
622         }
623 }
624 #endif
625
626 static void analog_start_polarityswitch(struct analog_pvt *p)
627 {
628         if (analog_callbacks.start_polarityswitch) {
629                 analog_callbacks.start_polarityswitch(p->chan_pvt);
630         }
631 }
632 static void analog_answer_polarityswitch(struct analog_pvt *p)
633 {
634         if (analog_callbacks.answer_polarityswitch) {
635                 analog_callbacks.answer_polarityswitch(p->chan_pvt);
636         }
637 }
638
639 static void analog_hangup_polarityswitch(struct analog_pvt *p)
640 {
641         if (analog_callbacks.hangup_polarityswitch) {
642                 analog_callbacks.hangup_polarityswitch(p->chan_pvt);
643         }
644 }
645
646 static int analog_dsp_set_digitmode(struct analog_pvt *p, enum analog_dsp_digitmode mode)
647 {
648         if (analog_callbacks.dsp_set_digitmode) {
649                 return analog_callbacks.dsp_set_digitmode(p->chan_pvt, mode);
650         }
651         return -1;
652 }
653
654 static void analog_cb_handle_dtmf(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub analog_index, struct ast_frame **dest)
655 {
656         if (analog_callbacks.handle_dtmf) {
657                 analog_callbacks.handle_dtmf(p->chan_pvt, ast, analog_index, dest);
658         }
659 }
660
661 static int analog_wink(struct analog_pvt *p, enum analog_sub index)
662 {
663         if (analog_callbacks.wink) {
664                 return analog_callbacks.wink(p->chan_pvt, index);
665         }
666         return -1;
667 }
668
669 static int analog_has_voicemail(struct analog_pvt *p)
670 {
671         if (analog_callbacks.has_voicemail) {
672                 return analog_callbacks.has_voicemail(p->chan_pvt);
673         }
674         return -1;
675 }
676
677 static int analog_is_dialing(struct analog_pvt *p, enum analog_sub index)
678 {
679         if (analog_callbacks.is_dialing) {
680                 return analog_callbacks.is_dialing(p->chan_pvt, index);
681         }
682         return -1;
683 }
684
685 /*!
686  * \internal
687  * \brief Attempt to transfer 3-way call.
688  *
689  * \param p Analog private structure.
690  * \param inthreeway TRUE if the 3-way call is conferenced.
691  *
692  * \note
693  * On entry these locks are held: real-call, private, 3-way call.
694  *
695  * \retval 1 Transfer successful.  3-way call is unlocked and subchannel is unalloced.
696  *         Swapped real and 3-way subchannel.
697  * \retval 0 Transfer successful.  3-way call is unlocked and subchannel is unalloced.
698  * \retval -1 on error.  Caller must unlock 3-way call.
699  */
700 static int analog_attempt_transfer(struct analog_pvt *p, int inthreeway)
701 {
702         struct ast_channel *owner_real;
703         struct ast_channel *owner_3way;
704         struct ast_channel *bridge_real;
705         struct ast_channel *bridge_3way;
706
707         owner_real = p->subs[ANALOG_SUB_REAL].owner;
708         owner_3way = p->subs[ANALOG_SUB_THREEWAY].owner;
709         bridge_real = ast_bridged_channel(owner_real);
710         bridge_3way = ast_bridged_channel(owner_3way);
711
712         /*
713          * In order to transfer, we need at least one of the channels to
714          * actually be in a call bridge.  We can't conference two
715          * applications together.  Why would we want to?
716          */
717         if (bridge_3way) {
718                 ast_verb(3, "TRANSFERRING %s to %s\n", ast_channel_name(owner_3way), ast_channel_name(owner_real));
719                 ast_cel_report_event(owner_3way,
720                         (ast_channel_state(owner_real) == AST_STATE_RINGING
721                                 || ast_channel_state(owner_3way) == AST_STATE_RINGING)
722                                 ? AST_CEL_BLINDTRANSFER : AST_CEL_ATTENDEDTRANSFER,
723                         NULL, ast_channel_linkedid(owner_3way), NULL);
724
725                 /*
726                  * The three-way party we're about to transfer is on hold if he
727                  * is not in a three way conference.
728                  */
729                 if (ast_channel_transfer_masquerade(owner_real, ast_channel_connected(owner_real), 0,
730                         bridge_3way, ast_channel_connected(owner_3way), !inthreeway)) {
731                         ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
732                                 ast_channel_name(bridge_3way), ast_channel_name(owner_real));
733                         return -1;
734                 }
735
736                 /* Three-way is now the REAL */
737                 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
738                 ast_channel_unlock(owner_3way);
739                 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
740                 /* Tell the caller not to hangup */
741                 return 1;
742         } else if (bridge_real) {
743                 /* Try transferring the other way. */
744                 ast_verb(3, "TRANSFERRING %s to %s\n", ast_channel_name(owner_real), ast_channel_name(owner_3way));
745                 ast_cel_report_event(owner_3way,
746                         (ast_channel_state(owner_real) == AST_STATE_RINGING
747                                 || ast_channel_state(owner_3way) == AST_STATE_RINGING)
748                                 ? AST_CEL_BLINDTRANSFER : AST_CEL_ATTENDEDTRANSFER,
749                         NULL, ast_channel_linkedid(owner_3way), NULL);
750
751                 /*
752                  * The three-way party we're about to transfer is on hold if he
753                  * is not in a three way conference.
754                  */
755                 if (ast_channel_transfer_masquerade(owner_3way, ast_channel_connected(owner_3way),
756                         !inthreeway, bridge_real, ast_channel_connected(owner_real), 0)) {
757                         ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
758                                 ast_channel_name(bridge_real), ast_channel_name(owner_3way));
759                         return -1;
760                 }
761
762                 /* Orphan the channel after releasing the lock */
763                 ast_channel_unlock(owner_3way);
764                 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
765                 return 0;
766         } else {
767                 ast_debug(1, "Neither %s nor %s are in a bridge, nothing to transfer\n",
768                         ast_channel_name(owner_real), ast_channel_name(owner_3way));
769                 return -1;
770         }
771 }
772
773 static int analog_update_conf(struct analog_pvt *p)
774 {
775         int x;
776         int needconf = 0;
777
778         /* Start with the obvious, general stuff */
779         for (x = 0; x < 3; x++) {
780                 /* Look for three way calls */
781                 if ((p->subs[x].allocd) && p->subs[x].inthreeway) {
782                         if (analog_callbacks.conf_add) {
783                                 analog_callbacks.conf_add(p->chan_pvt, x);
784                         }
785                         needconf++;
786                 } else {
787                         if (analog_callbacks.conf_del) {
788                                 analog_callbacks.conf_del(p->chan_pvt, x);
789                         }
790                 }
791         }
792         ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
793
794         if (analog_callbacks.complete_conference_update) {
795                 analog_callbacks.complete_conference_update(p->chan_pvt, needconf);
796         }
797         return 0;
798 }
799
800 struct ast_channel * analog_request(struct analog_pvt *p, int *callwait, const struct ast_channel *requestor)
801 {
802         struct ast_channel *ast;
803
804         ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
805         *callwait = (p->owner != NULL);
806
807         if (p->owner) {
808                 if (analog_alloc_sub(p, ANALOG_SUB_CALLWAIT)) {
809                         ast_log(LOG_ERROR, "Unable to alloc subchannel\n");
810                         return NULL;
811                 }
812         }
813
814         analog_set_outgoing(p, 1);
815         ast = analog_new_ast_channel(p, AST_STATE_RESERVED, 0,
816                 p->owner ? ANALOG_SUB_CALLWAIT : ANALOG_SUB_REAL, requestor);
817         if (!ast) {
818                 analog_set_outgoing(p, 0);
819         }
820         return ast;
821 }
822
823 int analog_available(struct analog_pvt *p)
824 {
825         int offhook;
826
827         ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
828
829         /* If do not disturb, definitely not */
830         if (p->dnd) {
831                 return 0;
832         }
833         /* If guard time, definitely not */
834         if (p->guardtime && (time(NULL) < p->guardtime)) {
835                 return 0;
836         }
837
838         /* If no owner definitely available */
839         if (!p->owner) {
840                 offhook = analog_is_off_hook(p);
841
842                 /* TDM FXO card, "onhook" means out of service (no battery on the line) */
843                 if ((p->sig == ANALOG_SIG_FXSLS) || (p->sig == ANALOG_SIG_FXSKS) || (p->sig == ANALOG_SIG_FXSGS)) {
844 #ifdef DAHDI_CHECK_HOOKSTATE
845                         if (offhook) {
846                                 return 1;
847                         }
848                         return 0;
849 #endif
850                 /* TDM FXS card, "offhook" means someone took the hook off so it's unavailable! */
851                 } else if (offhook) {
852                         ast_debug(1, "Channel %d off hook, can't use\n", p->channel);
853                         /* Not available when the other end is off hook */
854                         return 0;
855                 }
856                 return 1;
857         }
858
859         /* If it's not an FXO, forget about call wait */
860         if ((p->sig != ANALOG_SIG_FXOKS) && (p->sig != ANALOG_SIG_FXOLS) && (p->sig != ANALOG_SIG_FXOGS)) {
861                 return 0;
862         }
863
864         if (!p->callwaiting) {
865                 /* If they don't have call waiting enabled, then for sure they're unavailable at this point */
866                 return 0;
867         }
868
869         if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
870                 /* If there is already a call waiting call, then we can't take a second one */
871                 return 0;
872         }
873
874         if ((ast_channel_state(p->owner) != AST_STATE_UP) &&
875             ((ast_channel_state(p->owner) != AST_STATE_RINGING) || p->outgoing)) {
876                 /* If the current call is not up, then don't allow the call */
877                 return 0;
878         }
879         if ((p->subs[ANALOG_SUB_THREEWAY].owner) && (!p->subs[ANALOG_SUB_THREEWAY].inthreeway)) {
880                 /* Can't take a call wait when the three way calling hasn't been merged yet. */
881                 return 0;
882         }
883         /* We're cool */
884         return 1;
885 }
886
887 static int analog_stop_callwait(struct analog_pvt *p)
888 {
889         p->callwaitcas = 0;
890         if (analog_callbacks.stop_callwait) {
891                 return analog_callbacks.stop_callwait(p->chan_pvt);
892         }
893         return 0;
894 }
895
896 static int analog_callwait(struct analog_pvt *p)
897 {
898         p->callwaitcas = p->callwaitingcallerid;
899         if (analog_callbacks.callwait) {
900                 return analog_callbacks.callwait(p->chan_pvt);
901         }
902         return 0;
903 }
904
905 static void analog_set_callwaiting(struct analog_pvt *p, int callwaiting_enable)
906 {
907         p->callwaiting = callwaiting_enable;
908         if (analog_callbacks.set_callwaiting) {
909                 analog_callbacks.set_callwaiting(p->chan_pvt, callwaiting_enable);
910         }
911 }
912
913 static void analog_set_cadence(struct analog_pvt *p, struct ast_channel *chan)
914 {
915         if (analog_callbacks.set_cadence) {
916                 analog_callbacks.set_cadence(p->chan_pvt, &p->cidrings, chan);
917         }
918 }
919
920 static void analog_set_dialing(struct analog_pvt *p, int is_dialing)
921 {
922         p->dialing = is_dialing;
923         if (analog_callbacks.set_dialing) {
924                 analog_callbacks.set_dialing(p->chan_pvt, is_dialing);
925         }
926 }
927
928 static void analog_set_alarm(struct analog_pvt *p, int in_alarm)
929 {
930         p->inalarm = in_alarm;
931         if (analog_callbacks.set_alarm) {
932                 analog_callbacks.set_alarm(p->chan_pvt, in_alarm);
933         }
934 }
935
936 static void analog_set_ringtimeout(struct analog_pvt *p, int ringt)
937 {
938         p->ringt = ringt;
939         if (analog_callbacks.set_ringtimeout) {
940                 analog_callbacks.set_ringtimeout(p->chan_pvt, ringt);
941         }
942 }
943
944 static void analog_set_waitingfordt(struct analog_pvt *p, struct ast_channel *ast)
945 {
946         if (analog_callbacks.set_waitingfordt) {
947                 analog_callbacks.set_waitingfordt(p->chan_pvt, ast);
948         }
949 }
950
951 static int analog_check_waitingfordt(struct analog_pvt *p)
952 {
953         if (analog_callbacks.check_waitingfordt) {
954                 return analog_callbacks.check_waitingfordt(p->chan_pvt);
955         }
956
957         return 0;
958 }
959
960 static void analog_set_confirmanswer(struct analog_pvt *p, int flag)
961 {
962         if (analog_callbacks.set_confirmanswer) {
963                 analog_callbacks.set_confirmanswer(p->chan_pvt, flag);
964         }
965 }
966
967 static int analog_check_confirmanswer(struct analog_pvt *p)
968 {
969         if (analog_callbacks.check_confirmanswer) {
970                 return analog_callbacks.check_confirmanswer(p->chan_pvt);
971         }
972
973         return 0;
974 }
975
976 static void analog_cancel_cidspill(struct analog_pvt *p)
977 {
978         if (analog_callbacks.cancel_cidspill) {
979                 analog_callbacks.cancel_cidspill(p->chan_pvt);
980         }
981 }
982
983 static int analog_confmute(struct analog_pvt *p, int mute)
984 {
985         if (analog_callbacks.confmute) {
986                 return analog_callbacks.confmute(p->chan_pvt, mute);
987         }
988         return 0;
989 }
990
991 static void analog_set_pulsedial(struct analog_pvt *p, int flag)
992 {
993         if (analog_callbacks.set_pulsedial) {
994                 analog_callbacks.set_pulsedial(p->chan_pvt, flag);
995         }
996 }
997
998 static int analog_set_linear_mode(struct analog_pvt *p, enum analog_sub sub, int linear_mode)
999 {
1000         if (analog_callbacks.set_linear_mode) {
1001                 /* Return provides old linear_mode setting or error indication */
1002                 return analog_callbacks.set_linear_mode(p->chan_pvt, sub, linear_mode);
1003         }
1004         return -1;
1005 }
1006
1007 static void analog_set_inthreeway(struct analog_pvt *p, enum analog_sub sub, int inthreeway)
1008 {
1009         p->subs[sub].inthreeway = inthreeway;
1010         if (analog_callbacks.set_inthreeway) {
1011                 analog_callbacks.set_inthreeway(p->chan_pvt, sub, inthreeway);
1012         }
1013 }
1014
1015 int analog_call(struct analog_pvt *p, struct ast_channel *ast, const char *rdest, int timeout)
1016 {
1017         int res, idx, mysig;
1018         char *c, *n, *l;
1019         char dest[256]; /* must be same length as p->dialdest */
1020
1021         ast_debug(1, "CALLING CID_NAME: %s CID_NUM:: %s\n",
1022                 S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, ""),
1023                 S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, ""));
1024
1025         ast_copy_string(dest, rdest, sizeof(dest));
1026         ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1027
1028         if ((ast_channel_state(ast) == AST_STATE_BUSY)) {
1029                 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_BUSY);
1030                 return 0;
1031         }
1032
1033         if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
1034                 ast_log(LOG_WARNING, "analog_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
1035                 return -1;
1036         }
1037
1038         p->dialednone = 0;
1039         analog_set_outgoing(p, 1);
1040
1041         mysig = p->sig;
1042         if (p->outsigmod > -1) {
1043                 mysig = p->outsigmod;
1044         }
1045
1046         switch (mysig) {
1047         case ANALOG_SIG_FXOLS:
1048         case ANALOG_SIG_FXOGS:
1049         case ANALOG_SIG_FXOKS:
1050                 if (p->owner == ast) {
1051                         /* Normal ring, on hook */
1052
1053                         /* Don't send audio while on hook, until the call is answered */
1054                         analog_set_dialing(p, 1);
1055                         analog_set_cadence(p, ast); /* and set p->cidrings */
1056
1057                         /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1058                         c = strchr(dest, '/');
1059                         if (c) {
1060                                 c++;
1061                         }
1062                         if (c && (strlen(c) < p->stripmsd)) {
1063                                 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1064                                 c = NULL;
1065                         }
1066                         if (c) {
1067                                 p->dop.op = ANALOG_DIAL_OP_REPLACE;
1068                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1069                                 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
1070                         } else {
1071                                 p->dop.dialstr[0] = '\0';
1072                         }
1073
1074                         if (analog_ring(p)) {
1075                                 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1076                                 return -1;
1077                         }
1078                         analog_set_dialing(p, 1);
1079                 } else {
1080                         /* Call waiting call */
1081                         if (ast_channel_connected(ast)->id.number.valid && ast_channel_connected(ast)->id.number.str) {
1082                                 ast_copy_string(p->callwait_num, ast_channel_connected(ast)->id.number.str, sizeof(p->callwait_num));
1083                         } else {
1084                                 p->callwait_num[0] = '\0';
1085                         }
1086                         if (ast_channel_connected(ast)->id.name.valid && ast_channel_connected(ast)->id.name.str) {
1087                                 ast_copy_string(p->callwait_name, ast_channel_connected(ast)->id.name.str, sizeof(p->callwait_name));
1088                         } else {
1089                                 p->callwait_name[0] = '\0';
1090                         }
1091
1092                         /* Call waiting tone instead */
1093                         if (analog_callwait(p)) {
1094                                 return -1;
1095                         }
1096                         /* Make ring-back */
1097                         if (analog_play_tone(p, ANALOG_SUB_CALLWAIT, ANALOG_TONE_RINGTONE)) {
1098                                 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast_channel_name(ast));
1099                         }
1100
1101                 }
1102                 n = ast_channel_connected(ast)->id.name.valid ? ast_channel_connected(ast)->id.name.str : NULL;
1103                 l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
1104                 if (l) {
1105                         ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
1106                 } else {
1107                         p->lastcid_num[0] = '\0';
1108                 }
1109                 if (n) {
1110                         ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
1111                 } else {
1112                         p->lastcid_name[0] = '\0';
1113                 }
1114
1115                 if (p->use_callerid) {
1116                         p->caller.id.name.str = p->lastcid_name;
1117                         p->caller.id.number.str = p->lastcid_num;
1118                 }
1119
1120                 ast_setstate(ast, AST_STATE_RINGING);
1121                 idx = analog_get_index(ast, p, 0);
1122                 if (idx > -1) {
1123                         struct ast_cc_config_params *cc_params;
1124
1125                         /* This is where the initial ringing frame is queued for an analog call.
1126                          * As such, this is a great time to offer CCNR to the caller if it's available.
1127                          */
1128                         cc_params = ast_channel_get_cc_config_params(p->subs[idx].owner);
1129                         if (cc_params) {
1130                                 switch (ast_get_cc_monitor_policy(cc_params)) {
1131                                 case AST_CC_MONITOR_NEVER:
1132                                         break;
1133                                 case AST_CC_MONITOR_NATIVE:
1134                                 case AST_CC_MONITOR_ALWAYS:
1135                                 case AST_CC_MONITOR_GENERIC:
1136                                         ast_queue_cc_frame(p->subs[idx].owner, AST_CC_GENERIC_MONITOR_TYPE,
1137                                                 analog_get_orig_dialstring(p), AST_CC_CCNR, NULL);
1138                                         break;
1139                                 }
1140                         }
1141                         ast_queue_control(p->subs[idx].owner, AST_CONTROL_RINGING);
1142                 }
1143                 break;
1144         case ANALOG_SIG_FXSLS:
1145         case ANALOG_SIG_FXSGS:
1146         case ANALOG_SIG_FXSKS:
1147                 if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
1148                         ast_debug(1, "Ignore possible polarity reversal on line seizure\n");
1149                         p->polaritydelaytv = ast_tvnow();
1150                 }
1151                 /* fall through */
1152         case ANALOG_SIG_EMWINK:
1153         case ANALOG_SIG_EM:
1154         case ANALOG_SIG_EM_E1:
1155         case ANALOG_SIG_FEATD:
1156         case ANALOG_SIG_FEATDMF:
1157         case ANALOG_SIG_E911:
1158         case ANALOG_SIG_FGC_CAMA:
1159         case ANALOG_SIG_FGC_CAMAMF:
1160         case ANALOG_SIG_FEATB:
1161         case ANALOG_SIG_SFWINK:
1162         case ANALOG_SIG_SF:
1163         case ANALOG_SIG_SF_FEATD:
1164         case ANALOG_SIG_SF_FEATDMF:
1165         case ANALOG_SIG_FEATDMF_TA:
1166         case ANALOG_SIG_SF_FEATB:
1167                 c = strchr(dest, '/');
1168                 if (c) {
1169                         c++;
1170                 } else {
1171                         c = "";
1172                 }
1173                 if (strlen(c) < p->stripmsd) {
1174                         ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1175                         return -1;
1176                 }
1177                 res = analog_start(p);
1178                 if (res < 0) {
1179                         if (errno != EINPROGRESS) {
1180                                 return -1;
1181                         }
1182                 }
1183                 ast_debug(1, "Dialing '%s'\n", c);
1184                 p->dop.op = ANALOG_DIAL_OP_REPLACE;
1185
1186                 c += p->stripmsd;
1187
1188                 switch (mysig) {
1189                 case ANALOG_SIG_FEATD:
1190                         l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
1191                         if (l) {
1192                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
1193                         } else {
1194                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
1195                         }
1196                         break;
1197                 case ANALOG_SIG_FEATDMF:
1198                         l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
1199                         if (l) {
1200                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
1201                         } else {
1202                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
1203                         }
1204                         break;
1205                 case ANALOG_SIG_FEATDMF_TA:
1206                 {
1207                         const char *cic = "", *ozz = "";
1208
1209                         /* If you have to go through a Tandem Access point you need to use this */
1210 #ifndef STANDALONE
1211                         ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
1212                         if (!ozz) {
1213                                 ozz = analog_defaultozz;
1214                         }
1215                         cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
1216                         if (!cic) {
1217                                 cic = analog_defaultcic;
1218                         }
1219 #endif
1220                         if (!ozz || !cic) {
1221                                 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
1222                                 return -1;
1223                         }
1224                         snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
1225                         snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
1226                         p->whichwink = 0;
1227                 }
1228                         break;
1229                 case ANALOG_SIG_E911:
1230                         ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
1231                         break;
1232                 case ANALOG_SIG_FGC_CAMA:
1233                         snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
1234                         break;
1235                 case ANALOG_SIG_FGC_CAMAMF:
1236                 case ANALOG_SIG_FEATB:
1237                         snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
1238                         break;
1239                 default:
1240                         if (p->pulse) {
1241                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
1242                         } else {
1243                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
1244                         }
1245                         break;
1246                 }
1247
1248                 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1249                         memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1250                         strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1251                         p->echorest[sizeof(p->echorest) - 1] = '\0';
1252                         p->echobreak = 1;
1253                         p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1254                 } else {
1255                         p->echobreak = 0;
1256                 }
1257                 analog_set_waitingfordt(p, ast);
1258                 if (!res) {
1259                         if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
1260                                 int saveerr = errno;
1261
1262                                 analog_on_hook(p);
1263                                 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
1264                                 return -1;
1265                         }
1266                 } else {
1267                         ast_debug(1, "Deferring dialing...\n");
1268                 }
1269                 analog_set_dialing(p, 1);
1270                 if (ast_strlen_zero(c)) {
1271                         p->dialednone = 1;
1272                 }
1273                 ast_setstate(ast, AST_STATE_DIALING);
1274                 break;
1275         default:
1276                 ast_debug(1, "not yet implemented\n");
1277                 return -1;
1278         }
1279         return 0;
1280 }
1281
1282 int analog_hangup(struct analog_pvt *p, struct ast_channel *ast)
1283 {
1284         int res;
1285         int idx, x;
1286
1287         ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1288         if (!ast_channel_tech_pvt(ast)) {
1289                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1290                 return 0;
1291         }
1292
1293         idx = analog_get_index(ast, p, 1);
1294
1295         x = 0;
1296         if (p->origcid_num) {
1297                 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
1298                 ast_free(p->origcid_num);
1299                 p->origcid_num = NULL;
1300         }
1301         if (p->origcid_name) {
1302                 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
1303                 ast_free(p->origcid_name);
1304                 p->origcid_name = NULL;
1305         }
1306
1307         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1308
1309         ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1310                 p->channel, idx, p->subs[ANALOG_SUB_REAL].allocd, p->subs[ANALOG_SUB_CALLWAIT].allocd, p->subs[ANALOG_SUB_THREEWAY].allocd);
1311         if (idx > -1) {
1312                 /* Real channel, do some fixup */
1313                 p->subs[idx].owner = NULL;
1314                 p->polarity = POLARITY_IDLE;
1315                 analog_set_linear_mode(p, idx, 0);
1316                 switch (idx) {
1317                 case ANALOG_SUB_REAL:
1318                         if (p->subs[ANALOG_SUB_CALLWAIT].allocd && p->subs[ANALOG_SUB_THREEWAY].allocd) {
1319                                 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
1320                                 if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
1321                                         /* We had flipped over to answer a callwait and now it's gone */
1322                                         ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
1323                                         /* Move to the call-wait, but un-own us until they flip back. */
1324                                         analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
1325                                         analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
1326                                         analog_set_new_owner(p, NULL);
1327                                 } else {
1328                                         /* The three way hung up, but we still have a call wait */
1329                                         ast_debug(1, "We were in the threeway and have a callwait still.  Ditching the threeway.\n");
1330                                         analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
1331                                         analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1332                                         if (p->subs[ANALOG_SUB_REAL].inthreeway) {
1333                                                 /* This was part of a three way call.  Immediately make way for
1334                                                    another call */
1335                                                 ast_debug(1, "Call was complete, setting owner to former third call\n");
1336                                                 analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
1337                                                 analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
1338                                         } else {
1339                                                 /* This call hasn't been completed yet...  Set owner to NULL */
1340                                                 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
1341                                                 analog_set_new_owner(p, NULL);
1342                                         }
1343                                 }
1344                         } else if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
1345                                 /* Need to hold the lock for real-call, private, and call-waiting call */
1346                                 analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
1347                                 if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
1348                                         /* The call waiting call dissappeared. */
1349                                         analog_set_new_owner(p, NULL);
1350                                         break;
1351                                 }
1352
1353                                 /* Move to the call-wait and switch back to them. */
1354                                 analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
1355                                 analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
1356                                 analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
1357                                 if (ast_channel_state(p->owner) != AST_STATE_UP) {
1358                                         ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_ANSWER);
1359                                 }
1360                                 if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner)) {
1361                                         ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
1362                                 }
1363                                 /* Unlock the call-waiting call that we swapped to real-call. */
1364                                 ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
1365                         } else if (p->subs[ANALOG_SUB_THREEWAY].allocd) {
1366                                 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
1367                                 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1368                                 if (p->subs[ANALOG_SUB_REAL].inthreeway) {
1369                                         /* This was part of a three way call.  Immediately make way for
1370                                            another call */
1371                                         ast_debug(1, "Call was complete, setting owner to former third call\n");
1372                                         analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
1373                                         analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
1374                                 } else {
1375                                         /* This call hasn't been completed yet...  Set owner to NULL */
1376                                         ast_debug(1, "Call was incomplete, setting owner to NULL\n");
1377                                         analog_set_new_owner(p, NULL);
1378                                 }
1379                         }
1380                         break;
1381                 case ANALOG_SUB_CALLWAIT:
1382                         /* Ditch the holding callwait call, and immediately make it available */
1383                         if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
1384                                 /* Need to hold the lock for call-waiting call, private, and 3-way call */
1385                                 analog_lock_sub_owner(p, ANALOG_SUB_THREEWAY);
1386
1387                                 /* This is actually part of a three way, placed on hold.  Place the third part
1388                                    on music on hold now */
1389                                 if (p->subs[ANALOG_SUB_THREEWAY].owner && ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
1390                                         ast_queue_control_data(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CONTROL_HOLD,
1391                                                 S_OR(p->mohsuggest, NULL),
1392                                                 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
1393                                 }
1394                                 analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 0);
1395                                 /* Make it the call wait now */
1396                                 analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
1397                                 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1398                                 if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
1399                                         /* Unlock the 3-way call that we swapped to call-waiting call. */
1400                                         ast_channel_unlock(p->subs[ANALOG_SUB_CALLWAIT].owner);
1401                                 }
1402                         } else {
1403                                 analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
1404                         }
1405                         break;
1406                 case ANALOG_SUB_THREEWAY:
1407                         /* Need to hold the lock for 3-way call, private, and call-waiting call */
1408                         analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
1409                         if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
1410                                 /* The other party of the three way call is currently in a call-wait state.
1411                                    Start music on hold for them, and take the main guy out of the third call */
1412                                 analog_set_inthreeway(p, ANALOG_SUB_CALLWAIT, 0);
1413                                 if (p->subs[ANALOG_SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[ANALOG_SUB_CALLWAIT].owner)) {
1414                                         ast_queue_control_data(p->subs[ANALOG_SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
1415                                                 S_OR(p->mohsuggest, NULL),
1416                                                 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
1417                                 }
1418                         }
1419                         if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
1420                                 ast_channel_unlock(p->subs[ANALOG_SUB_CALLWAIT].owner);
1421                         }
1422                         analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
1423                         /* If this was part of a three way call index, let us make
1424                            another three way call */
1425                         analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1426                         break;
1427                 default:
1428                         /*
1429                          * Should never happen.
1430                          * This wasn't any sort of call, so how are we an index?
1431                          */
1432                         ast_log(LOG_ERROR, "Index found but not any type of call?\n");
1433                         break;
1434                 }
1435         }
1436
1437         if (!p->subs[ANALOG_SUB_REAL].owner && !p->subs[ANALOG_SUB_CALLWAIT].owner && !p->subs[ANALOG_SUB_THREEWAY].owner) {
1438                 analog_set_new_owner(p, NULL);
1439                 analog_set_ringtimeout(p, 0);
1440                 analog_set_confirmanswer(p, 0);
1441                 analog_set_pulsedial(p, 0);
1442                 analog_set_outgoing(p, 0);
1443                 p->onhooktime = time(NULL);
1444                 p->cidrings = 1;
1445
1446                 /* Perform low level hangup if no owner left */
1447                 res = analog_on_hook(p);
1448                 if (res < 0) {
1449                         ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast_channel_name(ast));
1450                 }
1451                 switch (p->sig) {
1452                 case ANALOG_SIG_FXOGS:
1453                 case ANALOG_SIG_FXOLS:
1454                 case ANALOG_SIG_FXOKS:
1455                         /* If they're off hook, try playing congestion */
1456                         if (analog_is_off_hook(p)) {
1457                                 analog_hangup_polarityswitch(p);
1458                                 analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
1459                         } else {
1460                                 analog_play_tone(p, ANALOG_SUB_REAL, -1);
1461                         }
1462                         break;
1463                 case ANALOG_SIG_FXSGS:
1464                 case ANALOG_SIG_FXSLS:
1465                 case ANALOG_SIG_FXSKS:
1466                         /* Make sure we're not made available for at least two seconds assuming
1467                            we were actually used for an inbound or outbound call. */
1468                         if (ast_channel_state(ast) != AST_STATE_RESERVED) {
1469                                 time(&p->guardtime);
1470                                 p->guardtime += 2;
1471                         }
1472                         break;
1473                 default:
1474                         analog_play_tone(p, ANALOG_SUB_REAL, -1);
1475                         break;
1476                 }
1477
1478                 analog_set_echocanceller(p, 0);
1479
1480                 x = 0;
1481                 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
1482                 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
1483                 p->callwaitcas = 0;
1484                 analog_set_callwaiting(p, p->permcallwaiting);
1485                 p->hidecallerid = p->permhidecallerid;
1486                 analog_set_dialing(p, 0);
1487                 analog_update_conf(p);
1488                 analog_all_subchannels_hungup(p);
1489         }
1490
1491         analog_stop_callwait(p);
1492
1493         ast_verb(3, "Hanging up on '%s'\n", ast_channel_name(ast));
1494
1495         return 0;
1496 }
1497
1498 int analog_answer(struct analog_pvt *p, struct ast_channel *ast)
1499 {
1500         int res = 0;
1501         int idx;
1502         int oldstate = ast_channel_state(ast);
1503
1504         ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1505         ast_setstate(ast, AST_STATE_UP);
1506         idx = analog_get_index(ast, p, 1);
1507         if (idx < 0) {
1508                 idx = ANALOG_SUB_REAL;
1509         }
1510         switch (p->sig) {
1511         case ANALOG_SIG_FXSLS:
1512         case ANALOG_SIG_FXSGS:
1513         case ANALOG_SIG_FXSKS:
1514                 analog_set_ringtimeout(p, 0);
1515                 /* Fall through */
1516         case ANALOG_SIG_EM:
1517         case ANALOG_SIG_EM_E1:
1518         case ANALOG_SIG_EMWINK:
1519         case ANALOG_SIG_FEATD:
1520         case ANALOG_SIG_FEATDMF:
1521         case ANALOG_SIG_FEATDMF_TA:
1522         case ANALOG_SIG_E911:
1523         case ANALOG_SIG_FGC_CAMA:
1524         case ANALOG_SIG_FGC_CAMAMF:
1525         case ANALOG_SIG_FEATB:
1526         case ANALOG_SIG_SF:
1527         case ANALOG_SIG_SFWINK:
1528         case ANALOG_SIG_SF_FEATD:
1529         case ANALOG_SIG_SF_FEATDMF:
1530         case ANALOG_SIG_SF_FEATB:
1531         case ANALOG_SIG_FXOLS:
1532         case ANALOG_SIG_FXOGS:
1533         case ANALOG_SIG_FXOKS:
1534                 /* Pick up the line */
1535                 ast_debug(1, "Took %s off hook\n", ast_channel_name(ast));
1536                 if (p->hanguponpolarityswitch) {
1537                         gettimeofday(&p->polaritydelaytv, NULL);
1538                 }
1539                 res = analog_off_hook(p);
1540                 analog_play_tone(p, idx, -1);
1541                 analog_set_dialing(p, 0);
1542                 if ((idx == ANALOG_SUB_REAL) && p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
1543                         if (oldstate == AST_STATE_RINGING) {
1544                                 ast_debug(1, "Finally swapping real and threeway\n");
1545                                 analog_play_tone(p, ANALOG_SUB_THREEWAY, -1);
1546                                 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
1547                                 analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
1548                         }
1549                 }
1550
1551                 switch (p->sig) {
1552                 case ANALOG_SIG_FXSLS:
1553                 case ANALOG_SIG_FXSKS:
1554                 case ANALOG_SIG_FXSGS:
1555                         analog_set_echocanceller(p, 1);
1556                         analog_train_echocanceller(p);
1557                         break;
1558                 case ANALOG_SIG_FXOLS:
1559                 case ANALOG_SIG_FXOKS:
1560                 case ANALOG_SIG_FXOGS:
1561                         analog_answer_polarityswitch(p);
1562                         break;
1563                 default:
1564                         break;
1565                 }
1566                 break;
1567         default:
1568                 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
1569                 res = -1;
1570                 break;
1571         }
1572         ast_setstate(ast, AST_STATE_UP);
1573         return res;
1574 }
1575
1576 static int analog_handles_digit(struct ast_frame *f)
1577 {
1578         char subclass = toupper(f->subclass.integer);
1579
1580         switch (subclass) {
1581         case '1':
1582         case '2':
1583         case '3':
1584         case '4':
1585         case '5':
1586         case '6':
1587         case '7':
1588         case '9':
1589         case 'A':
1590         case 'B':
1591         case 'C':
1592         case 'D':
1593         case 'E':
1594         case 'F':
1595                 return 1;
1596         default:
1597                 return 0;
1598         }
1599 }
1600
1601 void analog_handle_dtmf(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub idx, struct ast_frame **dest)
1602 {
1603         struct ast_frame *f = *dest;
1604
1605         ast_debug(1, "%s DTMF digit: 0x%02X '%c' on %s\n",
1606                 f->frametype == AST_FRAME_DTMF_BEGIN ? "Begin" : "End",
1607                 f->subclass.integer, f->subclass.integer, ast_channel_name(ast));
1608
1609         if (analog_check_confirmanswer(p)) {
1610                 if (f->frametype == AST_FRAME_DTMF_END) {
1611                         ast_debug(1, "Confirm answer on %s!\n", ast_channel_name(ast));
1612                         /* Upon receiving a DTMF digit, consider this an answer confirmation instead
1613                         of a DTMF digit */
1614                         p->subs[idx].f.frametype = AST_FRAME_CONTROL;
1615                         p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
1616                         /* Reset confirmanswer so DTMF's will behave properly for the duration of the call */
1617                         analog_set_confirmanswer(p, 0);
1618                 } else {
1619                         p->subs[idx].f.frametype = AST_FRAME_NULL;
1620                         p->subs[idx].f.subclass.integer = 0;
1621                 }
1622                 *dest = &p->subs[idx].f;
1623         } else if (p->callwaitcas) {
1624                 if (f->frametype == AST_FRAME_DTMF_END) {
1625                         if ((f->subclass.integer == 'A') || (f->subclass.integer == 'D')) {
1626                                 ast_debug(1, "Got some DTMF, but it's for the CAS\n");
1627                                 p->caller.id.name.str = p->callwait_name;
1628                                 p->caller.id.number.str = p->callwait_num;
1629                                 analog_send_callerid(p, 1, &p->caller);
1630                         }
1631                         if (analog_handles_digit(f)) {
1632                                 p->callwaitcas = 0;
1633                         }
1634                 }
1635                 p->subs[idx].f.frametype = AST_FRAME_NULL;
1636                 p->subs[idx].f.subclass.integer = 0;
1637                 *dest = &p->subs[idx].f;
1638         } else {
1639                 analog_cb_handle_dtmf(p, ast, idx, dest);
1640         }
1641 }
1642
1643 static int analog_my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
1644 {
1645         char c;
1646
1647         *str = 0; /* start with empty output buffer */
1648         for (;;) {
1649                 /* Wait for the first digit (up to specified ms). */
1650                 c = ast_waitfordigit(chan, ms);
1651                 /* if timeout, hangup or error, return as such */
1652                 if (c < 1) {
1653                         return c;
1654                 }
1655                 *str++ = c;
1656                 *str = 0;
1657                 if (strchr(term, c)) {
1658                         return 1;
1659                 }
1660         }
1661 }
1662
1663 static int analog_handle_notify_message(struct ast_channel *chan, struct analog_pvt *p, int cid_flags, int neon_mwievent)
1664 {
1665         if (analog_callbacks.handle_notify_message) {
1666                 analog_callbacks.handle_notify_message(chan, p->chan_pvt, cid_flags, neon_mwievent);
1667                 return 0;
1668         }
1669         return -1;
1670 }
1671
1672 static void analog_increase_ss_count(void)
1673 {
1674         if (analog_callbacks.increase_ss_count) {
1675                 analog_callbacks.increase_ss_count();
1676         }
1677 }
1678
1679 static void analog_decrease_ss_count(void)
1680 {
1681         if (analog_callbacks.decrease_ss_count) {
1682                 analog_callbacks.decrease_ss_count();
1683         }
1684 }
1685
1686 static int analog_distinctive_ring(struct ast_channel *chan, struct analog_pvt *p, int idx, int *ringdata)
1687 {
1688         if (analog_callbacks.distinctive_ring) {
1689                 return analog_callbacks.distinctive_ring(chan, p->chan_pvt, idx, ringdata);
1690         }
1691         return -1;
1692
1693 }
1694
1695 static void analog_get_and_handle_alarms(struct analog_pvt *p)
1696 {
1697         if (analog_callbacks.get_and_handle_alarms) {
1698                 analog_callbacks.get_and_handle_alarms(p->chan_pvt);
1699         }
1700 }
1701
1702 static void *analog_get_bridged_channel(struct ast_channel *chan)
1703 {
1704         if (analog_callbacks.get_sigpvt_bridged_channel) {
1705                 return analog_callbacks.get_sigpvt_bridged_channel(chan);
1706         }
1707         return NULL;
1708 }
1709
1710 static int analog_get_sub_fd(struct analog_pvt *p, enum analog_sub sub)
1711 {
1712         if (analog_callbacks.get_sub_fd) {
1713                 return analog_callbacks.get_sub_fd(p->chan_pvt, sub);
1714         }
1715         return -1;
1716 }
1717
1718 #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))
1719
1720 static int analog_canmatch_featurecode(const char *exten)
1721 {
1722         int extlen = strlen(exten);
1723         const char *pickup_ext;
1724         if (!extlen) {
1725                 return 1;
1726         }
1727         pickup_ext = ast_pickup_ext();
1728         if (extlen < strlen(pickup_ext) && !strncmp(pickup_ext, exten, extlen)) {
1729                 return 1;
1730         }
1731         /* hardcoded features are *60, *67, *69, *70, *72, *73, *78, *79, *82, *0 */
1732         if (exten[0] == '*' && extlen < 3) {
1733                 if (extlen == 1) {
1734                         return 1;
1735                 }
1736                 /* "*0" should be processed before it gets here */
1737                 switch (exten[1]) {
1738                 case '6':
1739                 case '7':
1740                 case '8':
1741                         return 1;
1742                 }
1743         }
1744         return 0;
1745 }
1746
1747 static void *__analog_ss_thread(void *data)
1748 {
1749         struct analog_pvt *p = data;
1750         struct ast_channel *chan = p->ss_astchan;
1751         char exten[AST_MAX_EXTENSION] = "";
1752         char exten2[AST_MAX_EXTENSION] = "";
1753         char dtmfcid[300];
1754         char dtmfbuf[300];
1755         char namebuf[ANALOG_MAX_CID];
1756         char numbuf[ANALOG_MAX_CID];
1757         struct callerid_state *cs = NULL;
1758         char *name = NULL, *number = NULL;
1759         int flags = 0;
1760         struct ast_smdi_md_message *smdi_msg = NULL;
1761         int timeout;
1762         int getforward = 0;
1763         char *s1, *s2;
1764         int len = 0;
1765         int res;
1766         int idx;
1767         struct ast_callid *callid;
1768
1769         analog_increase_ss_count();
1770
1771         ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1772
1773         if (!chan) {
1774                 /* What happened to the channel? */
1775                 goto quit;
1776         }
1777
1778         if ((callid = ast_channel_callid(chan))) {
1779                 ast_callid_threadassoc_add(callid);
1780                 ast_callid_unref(callid);
1781         }
1782
1783         /* in the bizarre case where the channel has become a zombie before we
1784            even get started here, abort safely
1785         */
1786         if (!ast_channel_tech_pvt(chan)) {
1787                 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", ast_channel_name(chan));
1788                 ast_hangup(chan);
1789                 goto quit;
1790         }
1791
1792         ast_verb(3, "Starting simple switch on '%s'\n", ast_channel_name(chan));
1793         idx = analog_get_index(chan, p, 0);
1794         if (idx < 0) {
1795                 ast_hangup(chan);
1796                 goto quit;
1797         }
1798         analog_dsp_reset_and_flush_digits(p);
1799         switch (p->sig) {
1800         case ANALOG_SIG_FEATD:
1801         case ANALOG_SIG_FEATDMF:
1802         case ANALOG_SIG_FEATDMF_TA:
1803         case ANALOG_SIG_E911:
1804         case ANALOG_SIG_FGC_CAMAMF:
1805         case ANALOG_SIG_FEATB:
1806         case ANALOG_SIG_EMWINK:
1807         case ANALOG_SIG_SF_FEATD:
1808         case ANALOG_SIG_SF_FEATDMF:
1809         case ANALOG_SIG_SF_FEATB:
1810         case ANALOG_SIG_SFWINK:
1811                 if (analog_wink(p, idx))
1812                         goto quit;
1813                 /* Fall through */
1814         case ANALOG_SIG_EM:
1815         case ANALOG_SIG_EM_E1:
1816         case ANALOG_SIG_SF:
1817         case ANALOG_SIG_FGC_CAMA:
1818                 res = analog_play_tone(p, idx, -1);
1819
1820                 analog_dsp_reset_and_flush_digits(p);
1821
1822                 /* set digit mode appropriately */
1823                 if (ANALOG_NEED_MFDETECT(p)) {
1824                         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
1825                 } else {
1826                         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1827                 }
1828
1829                 memset(dtmfbuf, 0, sizeof(dtmfbuf));
1830                 /* Wait for the first digit only if immediate=no */
1831                 if (!p->immediate) {
1832                         /* Wait for the first digit (up to 5 seconds). */
1833                         res = ast_waitfordigit(chan, 5000);
1834                 } else {
1835                         res = 0;
1836                 }
1837                 if (res > 0) {
1838                         /* save first char */
1839                         dtmfbuf[0] = res;
1840                         switch (p->sig) {
1841                         case ANALOG_SIG_FEATD:
1842                         case ANALOG_SIG_SF_FEATD:
1843                                 res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
1844                                 if (res > 0) {
1845                                         res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
1846                                 }
1847                                 if (res < 1) {
1848                                         analog_dsp_reset_and_flush_digits(p);
1849                                 }
1850                                 break;
1851                         case ANALOG_SIG_FEATDMF_TA:
1852                                 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1853                                 if (res < 1) {
1854                                         analog_dsp_reset_and_flush_digits(p);
1855                                 }
1856                                 if (analog_wink(p, idx)) {
1857                                         goto quit;
1858                                 }
1859                                 dtmfbuf[0] = 0;
1860                                 /* Wait for the first digit (up to 5 seconds). */
1861                                 res = ast_waitfordigit(chan, 5000);
1862                                 if (res <= 0) {
1863                                         break;
1864                                 }
1865                                 dtmfbuf[0] = res;
1866                                 /* fall through intentionally */
1867                         case ANALOG_SIG_FEATDMF:
1868                         case ANALOG_SIG_E911:
1869                         case ANALOG_SIG_FGC_CAMAMF:
1870                         case ANALOG_SIG_SF_FEATDMF:
1871                                 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1872                                 /* if international caca, do it again to get real ANO */
1873                                 if ((p->sig == ANALOG_SIG_FEATDMF) && (dtmfbuf[1] != '0') 
1874                                         && (strlen(dtmfbuf) != 14)) {
1875                                         if (analog_wink(p, idx)) {
1876                                                 goto quit;
1877                                         }
1878                                         dtmfbuf[0] = 0;
1879                                         /* Wait for the first digit (up to 5 seconds). */
1880                                         res = ast_waitfordigit(chan, 5000);
1881                                         if (res <= 0) {
1882                                                 break;
1883                                         }
1884                                         dtmfbuf[0] = res;
1885                                         res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1886                                 }
1887                                 if (res > 0) {
1888                                         /* if E911, take off hook */
1889                                         if (p->sig == ANALOG_SIG_E911) {
1890                                                 analog_off_hook(p);
1891                                         }
1892                                         res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
1893                                 }
1894                                 if (res < 1) {
1895                                         analog_dsp_reset_and_flush_digits(p);
1896                                 }
1897                                 break;
1898                         case ANALOG_SIG_FEATB:
1899                         case ANALOG_SIG_SF_FEATB:
1900                                 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1901                                 if (res < 1) {
1902                                         analog_dsp_reset_and_flush_digits(p);
1903                                 }
1904                                 break;
1905                         case ANALOG_SIG_EMWINK:
1906                                 /* if we received a '*', we are actually receiving Feature Group D
1907                                    dial syntax, so use that mode; otherwise, fall through to normal
1908                                    mode
1909                                 */
1910                                 if (res == '*') {
1911                                         res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
1912                                         if (res > 0) {
1913                                                 res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
1914                                         }
1915                                         if (res < 1) {
1916                                                 analog_dsp_reset_and_flush_digits(p);
1917                                         }
1918                                         break;
1919                                 }
1920                         default:
1921                                 /* If we got the first digit, get the rest */
1922                                 len = 1;
1923                                 dtmfbuf[len] = '\0';
1924                                 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, ast_channel_context(chan), dtmfbuf, 1, p->cid_num)) {
1925                                         if (ast_exists_extension(chan, ast_channel_context(chan), dtmfbuf, 1, p->cid_num)) {
1926                                                 timeout = analog_matchdigittimeout;
1927                                         } else {
1928                                                 timeout = analog_gendigittimeout;
1929                                         }
1930                                         res = ast_waitfordigit(chan, timeout);
1931                                         if (res < 0) {
1932                                                 ast_debug(1, "waitfordigit returned < 0...\n");
1933                                                 ast_hangup(chan);
1934                                                 goto quit;
1935                                         } else if (res) {
1936                                                 dtmfbuf[len++] = res;
1937                                                 dtmfbuf[len] = '\0';
1938                                         } else {
1939                                                 break;
1940                                         }
1941                                 }
1942                                 break;
1943                         }
1944                 }
1945                 if (res == -1) {
1946                         ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
1947                         ast_hangup(chan);
1948                         goto quit;
1949                 } else if (res < 0) {
1950                         ast_debug(1, "Got hung up before digits finished\n");
1951                         ast_hangup(chan);
1952                         goto quit;
1953                 }
1954
1955                 if (p->sig == ANALOG_SIG_FGC_CAMA) {
1956                         char anibuf[100];
1957
1958                         if (ast_safe_sleep(chan,1000) == -1) {
1959                                 ast_hangup(chan);
1960                                 goto quit;
1961                         }
1962                         analog_off_hook(p);
1963                         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
1964                         res = analog_my_getsigstr(chan, anibuf, "#", 10000);
1965                         if ((res > 0) && (strlen(anibuf) > 2)) {
1966                                 if (anibuf[strlen(anibuf) - 1] == '#') {
1967                                         anibuf[strlen(anibuf) - 1] = 0;
1968                                 }
1969                                 ast_set_callerid(chan, anibuf + 2, NULL, anibuf + 2);
1970                         }
1971                         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1972                 }
1973
1974                 ast_copy_string(exten, dtmfbuf, sizeof(exten));
1975                 if (ast_strlen_zero(exten)) {
1976                         ast_copy_string(exten, "s", sizeof(exten));
1977                 }
1978                 if (p->sig == ANALOG_SIG_FEATD || p->sig == ANALOG_SIG_EMWINK) {
1979                         /* Look for Feature Group D on all E&M Wink and Feature Group D trunks */
1980                         if (exten[0] == '*') {
1981                                 char *stringp=NULL;
1982                                 ast_copy_string(exten2, exten, sizeof(exten2));
1983                                 /* Parse out extension and callerid */
1984                                 stringp=exten2 +1;
1985                                 s1 = strsep(&stringp, "*");
1986                                 s2 = strsep(&stringp, "*");
1987                                 if (s2) {
1988                                         if (!ast_strlen_zero(p->cid_num)) {
1989                                                 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
1990                                         } else {
1991                                                 ast_set_callerid(chan, s1, NULL, s1);
1992                                         }
1993                                         ast_copy_string(exten, s2, sizeof(exten));
1994                                 } else {
1995                                         ast_copy_string(exten, s1, sizeof(exten));
1996                                 }
1997                         } else if (p->sig == ANALOG_SIG_FEATD) {
1998                                 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d.  Assuming E&M Wink instead\n", p->channel);
1999                         }
2000                 }
2001                 if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
2002                         if (exten[0] == '*') {
2003                                 char *stringp=NULL;
2004                                 ast_copy_string(exten2, exten, sizeof(exten2));
2005                                 /* Parse out extension and callerid */
2006                                 stringp=exten2 +1;
2007                                 s1 = strsep(&stringp, "#");
2008                                 s2 = strsep(&stringp, "#");
2009                                 if (s2) {
2010                                         if (!ast_strlen_zero(p->cid_num)) {
2011                                                 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
2012                                         } else {
2013                                                 if (*(s1 + 2)) {
2014                                                         ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
2015                                                 }
2016                                         }
2017                                         ast_copy_string(exten, s2 + 1, sizeof(exten));
2018                                 } else {
2019                                         ast_copy_string(exten, s1 + 2, sizeof(exten));
2020                                 }
2021                         } else {
2022                                 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d.  Assuming E&M Wink instead\n", p->channel);
2023                         }
2024                 }
2025                 if ((p->sig == ANALOG_SIG_E911) || (p->sig == ANALOG_SIG_FGC_CAMAMF)) {
2026                         if (exten[0] == '*') {
2027                                 char *stringp=NULL;
2028                                 ast_copy_string(exten2, exten, sizeof(exten2));
2029                                 /* Parse out extension and callerid */
2030                                 stringp=exten2 +1;
2031                                 s1 = strsep(&stringp, "#");
2032                                 s2 = strsep(&stringp, "#");
2033                                 if (s2 && (*(s2 + 1) == '0')) {
2034                                         if (*(s2 + 2)) {
2035                                                 ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
2036                                         }
2037                                 }
2038                                 if (s1) {
2039                                         ast_copy_string(exten, s1, sizeof(exten));
2040                                 } else {
2041                                         ast_copy_string(exten, "911", sizeof(exten));
2042                                 }
2043                         } else {
2044                                 ast_log(LOG_WARNING, "Got a non-E911/FGC CAMA input on channel %d.  Assuming E&M Wink instead\n", p->channel);
2045                         }
2046                 }
2047                 if (p->sig == ANALOG_SIG_FEATB) {
2048                         if (exten[0] == '*') {
2049                                 char *stringp=NULL;
2050                                 ast_copy_string(exten2, exten, sizeof(exten2));
2051                                 /* Parse out extension and callerid */
2052                                 stringp=exten2 +1;
2053                                 s1 = strsep(&stringp, "#");
2054                                 ast_copy_string(exten, exten2 + 1, sizeof(exten));
2055                         } else {
2056                                 ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d.  Assuming E&M Wink instead\n", p->channel);
2057                         }
2058                 }
2059                 if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
2060                         analog_wink(p, idx);
2061                         /*
2062                          * Some switches require a minimum guard time between the last
2063                          * FGD wink and something that answers immediately.  This
2064                          * ensures it.
2065                          */
2066                         if (ast_safe_sleep(chan, 100)) {
2067                                 ast_hangup(chan);
2068                                 goto quit;
2069                         }
2070                 }
2071                 analog_set_echocanceller(p, 1);
2072
2073                 analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
2074
2075                 if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1,
2076                         ast_channel_caller(chan)->id.number.valid ? ast_channel_caller(chan)->id.number.str : NULL)) {
2077                         ast_channel_exten_set(chan, exten);
2078                         analog_dsp_reset_and_flush_digits(p);
2079                         res = ast_pbx_run(chan);
2080                         if (res) {
2081                                 ast_log(LOG_WARNING, "PBX exited non-zero\n");
2082                                 res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2083                         }
2084                         goto quit;
2085                 } else {
2086                         ast_verb(3, "Unknown extension '%s' in context '%s' requested\n", exten, ast_channel_context(chan));
2087                         sleep(2);
2088                         res = analog_play_tone(p, idx, ANALOG_TONE_INFO);
2089                         if (res < 0) {
2090                                 ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
2091                         } else {
2092                                 sleep(1);
2093                         }
2094                         res = ast_streamfile(chan, "ss-noservice", ast_channel_language(chan));
2095                         if (res >= 0) {
2096                                 ast_waitstream(chan, "");
2097                         }
2098                         res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2099                         ast_hangup(chan);
2100                         goto quit;
2101                 }
2102                 break;
2103         case ANALOG_SIG_FXOLS:
2104         case ANALOG_SIG_FXOGS:
2105         case ANALOG_SIG_FXOKS:
2106                 /* Read the first digit */
2107                 timeout = analog_firstdigittimeout;
2108                 /* If starting a threeway call, never timeout on the first digit so someone
2109                    can use flash-hook as a "hold" feature */
2110                 if (p->subs[ANALOG_SUB_THREEWAY].owner) {
2111                         timeout = 999999;
2112                 }
2113                 while (len < AST_MAX_EXTENSION-1) {
2114                         /* Read digit unless it's supposed to be immediate, in which case the
2115                            only answer is 's' */
2116                         if (p->immediate) {
2117                                 res = 's';
2118                         } else {
2119                                 res = ast_waitfordigit(chan, timeout);
2120                         }
2121                         timeout = 0;
2122                         if (res < 0) {
2123                                 ast_debug(1, "waitfordigit returned < 0...\n");
2124                                 res = analog_play_tone(p, idx, -1);
2125                                 ast_hangup(chan);
2126                                 goto quit;
2127                         } else if (res) {
2128                                 ast_debug(1,"waitfordigit returned '%c' (%d), timeout = %d\n", res, res, timeout);
2129                                 exten[len++]=res;
2130                                 exten[len] = '\0';
2131                         }
2132                         if (!ast_ignore_pattern(ast_channel_context(chan), exten)) {
2133                                 analog_play_tone(p, idx, -1);
2134                         } else {
2135                                 analog_play_tone(p, idx, ANALOG_TONE_DIALTONE);
2136                         }
2137                         if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num) && !ast_parking_ext_valid(exten, chan, ast_channel_context(chan))) {
2138                                 if (!res || !ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2139                                         if (getforward) {
2140                                                 /* Record this as the forwarding extension */
2141                                                 ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
2142                                                 ast_verb(3, "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
2143                                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2144                                                 if (res) {
2145                                                         break;
2146                                                 }
2147                                                 usleep(500000);
2148                                                 res = analog_play_tone(p, idx, -1);
2149                                                 sleep(1);
2150                                                 memset(exten, 0, sizeof(exten));
2151                                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALTONE);
2152                                                 len = 0;
2153                                                 getforward = 0;
2154                                         } else {
2155                                                 res = analog_play_tone(p, idx, -1);
2156                                                 ast_channel_exten_set(chan, exten);
2157                                                 if (!ast_strlen_zero(p->cid_num)) {
2158                                                         if (!p->hidecallerid) {
2159                                                                 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
2160                                                         } else {
2161                                                                 ast_set_callerid(chan, NULL, NULL, p->cid_num);
2162                                                         }
2163                                                 }
2164                                                 if (!ast_strlen_zero(p->cid_name)) {
2165                                                         if (!p->hidecallerid) {
2166                                                                 ast_set_callerid(chan, NULL, p->cid_name, NULL);
2167                                                         }
2168                                                 }
2169                                                 ast_setstate(chan, AST_STATE_RING);
2170                                                 analog_set_echocanceller(p, 1);
2171                                                 res = ast_pbx_run(chan);
2172                                                 if (res) {
2173                                                         ast_log(LOG_WARNING, "PBX exited non-zero\n");
2174                                                         res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2175                                                 }
2176                                                 goto quit;
2177                                         }
2178                                 } else {
2179                                         /* It's a match, but they just typed a digit, and there is an ambiguous match,
2180                                            so just set the timeout to analog_matchdigittimeout and wait some more */
2181                                         timeout = analog_matchdigittimeout;
2182                                 }
2183                         } else if (res == 0) {
2184                                 ast_debug(1, "not enough digits (and no ambiguous match)...\n");
2185                                 res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2186                                 analog_wait_event(p);
2187                                 ast_hangup(chan);
2188                                 goto quit;
2189                         } else if (p->callwaiting && !strcmp(exten, "*70")) {
2190                                 ast_verb(3, "Disabling call waiting on %s\n", ast_channel_name(chan));
2191                                 /* Disable call waiting if enabled */
2192                                 analog_set_callwaiting(p, 0);
2193                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2194                                 if (res) {
2195                                         ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
2196                                                 ast_channel_name(chan), strerror(errno));
2197                                 }
2198                                 len = 0;
2199                                 memset(exten, 0, sizeof(exten));
2200                                 timeout = analog_firstdigittimeout;
2201
2202                         } else if (!strcmp(exten,ast_pickup_ext())) {
2203                                 /* Scan all channels and see if there are any
2204                                  * ringing channels that have call groups
2205                                  * that equal this channels pickup group
2206                                  */
2207                                 if (idx == ANALOG_SUB_REAL) {
2208                                         /* Switch us from Third call to Call Wait */
2209                                         if (p->subs[ANALOG_SUB_THREEWAY].owner) {
2210                                                 /* If you make a threeway call and the *8# a call, it should actually
2211                                                    look like a callwait */
2212                                                 analog_alloc_sub(p, ANALOG_SUB_CALLWAIT);
2213                                                 analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
2214                                                 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
2215                                         }
2216                                         analog_set_echocanceller(p, 1);
2217                                         if (ast_pickup_call(chan)) {
2218                                                 ast_debug(1, "No call pickup possible...\n");
2219                                                 res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2220                                                 analog_wait_event(p);
2221                                         }
2222                                         ast_hangup(chan);
2223                                         goto quit;
2224                                 } else {
2225                                         ast_log(LOG_WARNING, "Huh?  Got *8# on call not on real\n");
2226                                         ast_hangup(chan);
2227                                         goto quit;
2228                                 }
2229
2230                         } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
2231                                 ast_verb(3, "Disabling Caller*ID on %s\n", ast_channel_name(chan));
2232                                 /* Disable Caller*ID if enabled */
2233                                 p->hidecallerid = 1;
2234                                 ast_party_number_free(&ast_channel_caller(chan)->id.number);
2235                                 ast_party_number_init(&ast_channel_caller(chan)->id.number);
2236                                 ast_party_name_free(&ast_channel_caller(chan)->id.name);
2237                                 ast_party_name_init(&ast_channel_caller(chan)->id.name);
2238                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2239                                 if (res) {
2240                                         ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
2241                                                 ast_channel_name(chan), strerror(errno));
2242                                 }
2243                                 len = 0;
2244                                 memset(exten, 0, sizeof(exten));
2245                                 timeout = analog_firstdigittimeout;
2246                         } else if (p->callreturn && !strcmp(exten, "*69")) {
2247                                 res = 0;
2248                                 if (!ast_strlen_zero(p->lastcid_num)) {
2249                                         res = ast_say_digit_str(chan, p->lastcid_num, "", ast_channel_language(chan));
2250                                 }
2251                                 if (!res) {
2252                                         res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2253                                 }
2254                                 break;
2255                         } else if (!strcmp(exten, "*78")) {
2256                                 /* Do not disturb enabled */
2257                                 analog_dnd(p, 1);
2258                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2259                                 getforward = 0;
2260                                 memset(exten, 0, sizeof(exten));
2261                                 len = 0;
2262                         } else if (!strcmp(exten, "*79")) {
2263                                 /* Do not disturb disabled */
2264                                 analog_dnd(p, 0);
2265                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2266                                 getforward = 0;
2267                                 memset(exten, 0, sizeof(exten));
2268                                 len = 0;
2269                         } else if (p->cancallforward && !strcmp(exten, "*72")) {
2270                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2271                                 getforward = 1;
2272                                 memset(exten, 0, sizeof(exten));
2273                                 len = 0;
2274                         } else if (p->cancallforward && !strcmp(exten, "*73")) {
2275                                 ast_verb(3, "Cancelling call forwarding on channel %d\n", p->channel);
2276                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2277                                 memset(p->call_forward, 0, sizeof(p->call_forward));
2278                                 getforward = 0;
2279                                 memset(exten, 0, sizeof(exten));
2280                                 len = 0;
2281                         } else if ((p->transfer || p->canpark) && ast_parking_ext_valid(exten, chan, ast_channel_context(chan)) &&
2282                                                 p->subs[ANALOG_SUB_THREEWAY].owner &&
2283                                                 ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
2284                                 /* This is a three way call, the main call being a real channel,
2285                                         and we're parking the first call. */
2286                                 ast_masq_park_call_exten(
2287                                         ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner), chan, exten,
2288                                         ast_channel_context(chan), 0, NULL);
2289                                 ast_verb(3, "Parking call to '%s'\n", ast_channel_name(chan));
2290                                 break;
2291                         } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
2292                                 ast_verb(3, "Blacklisting number %s\n", p->lastcid_num);
2293                                 res = ast_db_put("blacklist", p->lastcid_num, "1");
2294                                 if (!res) {
2295                                         res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2296                                         memset(exten, 0, sizeof(exten));
2297                                         len = 0;
2298                                 }
2299                         } else if (p->hidecallerid && !strcmp(exten, "*82")) {
2300                                 ast_verb(3, "Enabling Caller*ID on %s\n", ast_channel_name(chan));
2301                                 /* Enable Caller*ID if enabled */
2302                                 p->hidecallerid = 0;
2303                                 ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
2304                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2305                                 if (res) {
2306                                         ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
2307                                                 ast_channel_name(chan), strerror(errno));
2308                                 }
2309                                 len = 0;
2310                                 memset(exten, 0, sizeof(exten));
2311                                 timeout = analog_firstdigittimeout;
2312                         } else if (!strcmp(exten, "*0")) {
2313                                 struct ast_channel *nbridge = p->subs[ANALOG_SUB_THREEWAY].owner;
2314                                 struct analog_pvt *pbridge = NULL;
2315                                 /* set up the private struct of the bridged one, if any */
2316                                 if (nbridge) {
2317                                         pbridge = analog_get_bridged_channel(nbridge);
2318                                 }
2319                                 if (pbridge && ISTRUNK(pbridge)) {
2320                                         /* Clear out the dial buffer */
2321                                         p->dop.dialstr[0] = '\0';
2322                                         /* flash hookswitch */
2323                                         if ((analog_flash(pbridge) == -1) && (errno != EINPROGRESS)) {
2324                                                 ast_log(LOG_WARNING,
2325                                                         "Unable to flash-hook bridged trunk from channel %s: %s\n",
2326                                                         ast_channel_name(nbridge), strerror(errno));
2327                                         }
2328                                         analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
2329                                         analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
2330                                         analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
2331                                         if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner)) {
2332                                                 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
2333                                         }
2334                                         ast_hangup(chan);
2335                                         goto quit;
2336                                 } else {
2337                                         analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2338                                         analog_wait_event(p);
2339                                         analog_play_tone(p, idx, -1);
2340                                         analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
2341                                         analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
2342                                         analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
2343                                         ast_hangup(chan);
2344                                         goto quit;
2345                                 }
2346                         } else if (!ast_canmatch_extension(chan, ast_channel_context(chan), exten, 1,
2347                                 ast_channel_caller(chan)->id.number.valid ? ast_channel_caller(chan)->id.number.str : NULL)
2348                                 && !analog_canmatch_featurecode(exten)) {
2349                                 ast_debug(1, "Can't match %s from '%s' in context %s\n", exten,
2350                                         ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str
2351                                                 ? ast_channel_caller(chan)->id.number.str : "<Unknown Caller>",
2352                                         ast_channel_context(chan));
2353                                 break;
2354                         }
2355                         if (!timeout) {
2356                                 timeout = analog_gendigittimeout;
2357                         }
2358                         if (len && !ast_ignore_pattern(ast_channel_context(chan), exten)) {
2359                                 analog_play_tone(p, idx, -1);
2360                         }
2361                 }
2362                 break;
2363         case ANALOG_SIG_FXSLS:
2364         case ANALOG_SIG_FXSGS:
2365         case ANALOG_SIG_FXSKS:
2366                 /* check for SMDI messages */
2367                 if (p->use_smdi && p->smdi_iface) {
2368                         smdi_msg = ast_smdi_md_message_wait(p->smdi_iface, ANALOG_SMDI_MD_WAIT_TIMEOUT);
2369                         if (smdi_msg != NULL) {
2370                                 ast_channel_exten_set(chan, smdi_msg->fwd_st);
2371
2372                                 if (smdi_msg->type == 'B')
2373                                         pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "b");
2374                                 else if (smdi_msg->type == 'N')
2375                                         pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "u");
2376
2377                                 ast_debug(1, "Received SMDI message on %s\n", ast_channel_name(chan));
2378                         } else {
2379                                 ast_log(LOG_WARNING, "SMDI enabled but no SMDI message present\n");
2380                         }
2381                 }
2382
2383                 if (p->use_callerid && (p->cid_signalling == CID_SIG_SMDI && smdi_msg)) {
2384                         number = smdi_msg->calling_st;
2385
2386                 /* If we want caller id, we're in a prering state due to a polarity reversal
2387                  * and we're set to use a polarity reversal to trigger the start of caller id,
2388                  * grab the caller id and wait for ringing to start... */
2389                 } else if (p->use_callerid && (ast_channel_state(chan) == AST_STATE_PRERING
2390                         && (p->cid_start == ANALOG_CID_START_POLARITY
2391                                 || p->cid_start == ANALOG_CID_START_POLARITY_IN
2392                                 || p->cid_start == ANALOG_CID_START_DTMF_NOALERT))) {
2393                         /* If set to use DTMF CID signalling, listen for DTMF */
2394                         if (p->cid_signalling == CID_SIG_DTMF) {
2395                                 int k = 0;
2396                                 int oldlinearity; 
2397                                 cs = NULL;
2398                                 ast_debug(1, "Receiving DTMF cid on channel %s\n", ast_channel_name(chan));
2399
2400                                 oldlinearity = analog_set_linear_mode(p, idx, 0);
2401
2402                                 /*
2403                                  * We are the only party interested in the Rx stream since
2404                                  * we have not answered yet.  We don't need or even want DTMF
2405                                  * emulation.  The DTMF digits can come so fast that emulation
2406                                  * can drop some of them.
2407                                  */
2408                                 ast_set_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
2409                                 res = 4000;/* This is a typical OFF time between rings. */
2410                                 for (;;) {
2411                                         struct ast_frame *f;
2412                                         res = ast_waitfor(chan, res);
2413                                         if (res <= 0) {
2414                                                 /*
2415                                                  * We do not need to restore the analog_set_linear_mode()
2416                                                  * or AST_FLAG_END_DTMF_ONLY flag settings since we
2417                                                  * are hanging up the channel.
2418                                                  */
2419                                                 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
2420                                                         "Exiting simple switch\n");
2421                                                 ast_hangup(chan);
2422                                                 goto quit;
2423                                         }
2424                                         f = ast_read(chan);
2425                                         if (!f) {
2426                                                 break;
2427                                         }
2428                                         if (f->frametype == AST_FRAME_DTMF) {
2429                                                 if (k < ARRAY_LEN(dtmfbuf) - 1) {
2430                                                         dtmfbuf[k++] = f->subclass.integer;
2431                                                 }
2432                                                 ast_debug(1, "CID got digit '%c'\n", f->subclass.integer);
2433                                                 res = 4000;/* This is a typical OFF time between rings. */
2434                                         }
2435                                         ast_frfree(f);
2436                                         if (ast_channel_state(chan) == AST_STATE_RING ||
2437                                                 ast_channel_state(chan) == AST_STATE_RINGING) {
2438                                                 break; /* Got ring */
2439                                         }
2440                                 }
2441                                 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
2442                                 dtmfbuf[k] = '\0';
2443
2444                                 analog_set_linear_mode(p, idx, oldlinearity);
2445
2446                                 /* Got cid and ring. */
2447                                 ast_debug(1, "CID got string '%s'\n", dtmfbuf);
2448                                 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
2449                                 ast_debug(1, "CID is '%s', flags %d\n", dtmfcid, flags);
2450                                 /* If first byte is NULL, we have no cid */
2451                                 if (!ast_strlen_zero(dtmfcid)) {
2452                                         number = dtmfcid;
2453                                 } else {
2454                                         number = NULL;
2455                                 }
2456
2457                         /* If set to use V23 Signalling, launch our FSK gubbins and listen for it */
2458                         } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) {
2459                                 int timeout = 10000;  /* Ten seconds */
2460                                 struct timeval start = ast_tvnow();
2461                                 enum analog_event ev;
2462
2463                                 namebuf[0] = 0;
2464                                 numbuf[0] = 0;
2465
2466                                 if (!analog_start_cid_detect(p, p->cid_signalling)) {
2467                                         while (1) {
2468                                                 res = analog_get_callerid(p, namebuf, numbuf, &ev, timeout - ast_tvdiff_ms(ast_tvnow(), start));
2469
2470                                                 if (res == 0) {
2471                                                         break;
2472                                                 }
2473
2474                                                 if (res == 1) {
2475                                                         if (ev == ANALOG_EVENT_NOALARM) {
2476                                                                 analog_set_alarm(p, 0);
2477                                                         }
2478                                                         if (p->cid_signalling == CID_SIG_V23_JP) {
2479                                                                 if (ev == ANALOG_EVENT_RINGBEGIN) {
2480                                                                         analog_off_hook(p);
2481                                                                         usleep(1);
2482                                                                 }
2483                                                         } else {
2484                                                                 ev = ANALOG_EVENT_NONE;
2485                                                                 break;
2486                                                         }
2487                                                 }
2488
2489                                                 if (ast_tvdiff_ms(ast_tvnow(), start) > timeout)
2490                                                         break;
2491
2492                                         }
2493                                         name = namebuf;
2494                                         number = numbuf;
2495
2496                                         analog_stop_cid_detect(p);
2497
2498                                         if (p->cid_signalling == CID_SIG_V23_JP) {
2499                                                 res = analog_on_hook(p);
2500                                                 usleep(1);
2501                                         }
2502
2503                                         /* Finished with Caller*ID, now wait for a ring to make sure there really is a call coming */
2504                                         res = 4000;/* This is a typical OFF time between rings. */
2505                                         for (;;) {
2506                                                 struct ast_frame *f;
2507                                                 res = ast_waitfor(chan, res);
2508                                                 if (res <= 0) {
2509                                                         ast_log(LOG_WARNING, "CID timed out waiting for ring. "
2510                                                                 "Exiting simple switch\n");
2511                                                         ast_hangup(chan);
2512                                                         goto quit;
2513                                                 }
2514                                                 if (!(f = ast_read(chan))) {
2515                                                         ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n");
2516                                                         ast_hangup(chan);
2517                                                         goto quit;
2518                                                 }
2519                                                 ast_frfree(f);
2520                                                 if (ast_channel_state(chan) == AST_STATE_RING ||
2521                                                         ast_channel_state(chan) == AST_STATE_RINGING)
2522                                                         break; /* Got ring */
2523                                         }
2524
2525                                         if (analog_distinctive_ring(chan, p, idx, NULL)) {
2526                                                 goto quit;
2527                                         }
2528
2529                                         if (res < 0) {
2530                                                 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", ast_channel_name(chan));
2531                                         }
2532                                 } else {
2533                                         ast_log(LOG_WARNING, "Unable to get caller ID space\n");
2534                                 }
2535                         } else {
2536                                 ast_log(LOG_WARNING, "Channel %s in prering "
2537                                         "state, but I have nothing to do. "
2538                                         "Terminating simple switch, should be "
2539                                         "restarted by the actual ring.\n",
2540                                         ast_channel_name(chan));
2541                                 ast_hangup(chan);
2542                                 goto quit;
2543                         }
2544                 } else if (p->use_callerid && p->cid_start == ANALOG_CID_START_RING) {
2545                         int timeout = 10000;  /* Ten seconds */
2546                         struct timeval start = ast_tvnow();
2547                         enum analog_event ev;
2548                         int curRingData[RING_PATTERNS] = { 0 };
2549                         int receivedRingT = 0;
2550
2551                         namebuf[0] = 0;
2552                         numbuf[0] = 0;
2553
2554                         if (!analog_start_cid_detect(p, p->cid_signalling)) {
2555                                 while (1) {
2556                                         res = analog_get_callerid(p, namebuf, numbuf, &ev, timeout - ast_tvdiff_ms(ast_tvnow(), start));
2557
2558                                         if (res == 0) {
2559                                                 break;
2560                                         }
2561
2562                                         if (res == 1 || res == 2) {
2563                                                 if (ev == ANALOG_EVENT_NOALARM) {
2564                                                         analog_set_alarm(p, 0);
2565                                                 } else if (ev == ANALOG_EVENT_POLARITY && p->hanguponpolarityswitch && p->polarity == POLARITY_REV) {
2566                                                         ast_debug(1, "Hanging up due to polarity reversal on channel %d while detecting callerid\n", p->channel);
2567                                                         p->polarity = POLARITY_IDLE;
2568                                                         ast_hangup(chan);
2569                                                         goto quit;
2570                                                 } else if (ev != ANALOG_EVENT_NONE && ev != ANALOG_EVENT_RINGBEGIN && ev != ANALOG_EVENT_RINGOFFHOOK) {
2571                                                         break;
2572                                                 }
2573                                                 if (res != 2) {
2574                                                         /* Let us detect callerid when the telco uses distinctive ring */
2575                                                         curRingData[receivedRingT] = p->ringt;
2576
2577                                                         if (p->ringt < p->ringt_base/2) {
2578                                                                 break;
2579                                                         }
2580                                                         /* Increment the ringT counter so we can match it against
2581                                                            values in chan_dahdi.conf for distinctive ring */
2582                                                         if (++receivedRingT == RING_PATTERNS) {
2583                                                                 break;
2584                                                         }
2585                                                 }
2586                                         }
2587
2588                                         if (ast_tvdiff_ms(ast_tvnow(), start) > timeout) {
2589                                                 break;
2590                                         }
2591
2592                                 }
2593                                 name = namebuf;
2594                                 number = numbuf;
2595
2596                                 analog_stop_cid_detect(p);
2597
2598                                 if (analog_distinctive_ring(chan, p, idx, curRingData)) {
2599                                         goto quit;
2600                                 }
2601
2602                                 if (res < 0) {
2603                                         ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", ast_channel_name(chan));
2604                                 }
2605                         } else {
2606                                 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
2607                         }
2608                 } else {
2609                         cs = NULL;
2610                 }
2611
2612                 if (number) {
2613                         ast_shrink_phone_number(number);
2614                 }
2615                 ast_set_callerid(chan, number, name, number);
2616
2617                 if (cs) {
2618                         callerid_free(cs);
2619                 }
2620
2621                 analog_handle_notify_message(chan, p, flags, -1);
2622
2623                 ast_setstate(chan, AST_STATE_RING);
2624                 ast_channel_rings_set(chan, 1);
2625                 analog_set_ringtimeout(p, p->ringt_base);
2626                 res = ast_pbx_run(chan);
2627                 if (res) {
2628                         ast_hangup(chan);
2629                         ast_log(LOG_WARNING, "PBX exited non-zero\n");
2630                 }
2631                 goto quit;
2632         default:
2633                 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);
2634                 break;
2635         }
2636         res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2637         if (res < 0) {
2638                 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
2639         }
2640         ast_hangup(chan);
2641 quit:
2642         if (smdi_msg) {
2643                 ASTOBJ_UNREF(smdi_msg, ast_smdi_md_message_destroy);
2644         }
2645         analog_decrease_ss_count();
2646         return NULL;
2647 }
2648
2649 int analog_ss_thread_start(struct analog_pvt *p, struct ast_channel *chan)
2650 {
2651         pthread_t threadid;
2652
2653         return ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, p);
2654 }
2655
2656 static struct ast_frame *__analog_handle_event(struct analog_pvt *p, struct ast_channel *ast)
2657 {
2658         int res, x;
2659         int mysig;
2660         enum analog_sub idx;
2661         char *c;
2662         pthread_t threadid;
2663         struct ast_channel *chan;
2664         struct ast_frame *f;
2665         struct ast_control_pvt_cause_code *cause_code = NULL;
2666         int data_size = sizeof(*cause_code);
2667         char *subclass = NULL;
2668
2669         ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
2670
2671         idx = analog_get_index(ast, p, 0);
2672         if (idx < 0) {
2673                 return &ast_null_frame;
2674         }
2675         if (idx != ANALOG_SUB_REAL) {
2676                 ast_log(LOG_ERROR, "We got an event on a non real sub.  Fix it!\n");
2677         }
2678
2679         mysig = p->sig;
2680         if (p->outsigmod > -1) {
2681                 mysig = p->outsigmod;
2682         }
2683
2684         p->subs[idx].f.frametype = AST_FRAME_NULL;
2685         p->subs[idx].f.subclass.integer = 0;
2686         p->subs[idx].f.datalen = 0;
2687         p->subs[idx].f.samples = 0;
2688         p->subs[idx].f.mallocd = 0;
2689         p->subs[idx].f.offset = 0;
2690         p->subs[idx].f.src = "dahdi_handle_event";
2691         p->subs[idx].f.data.ptr = NULL;
2692         f = &p->subs[idx].f;
2693
2694         res = analog_get_event(p);
2695
2696         ast_debug(1, "Got event %s(%d) on channel %d (index %d)\n", analog_event2str(res), res, p->channel, idx);
2697
2698         if (res & (ANALOG_EVENT_PULSEDIGIT | ANALOG_EVENT_DTMFUP)) {
2699                 analog_set_pulsedial(p, (res & ANALOG_EVENT_PULSEDIGIT) ? 1 : 0);
2700                 ast_debug(1, "Detected %sdigit '%c'\n", (res & ANALOG_EVENT_PULSEDIGIT) ? "pulse ": "", res & 0xff);
2701                 analog_confmute(p, 0);
2702                 p->subs[idx].f.frametype = AST_FRAME_DTMF_END;
2703                 p->subs[idx].f.subclass.integer = res & 0xff;
2704                 analog_handle_dtmf(p, ast, idx, &f);
2705                 return f;
2706         }
2707
2708         if (res & ANALOG_EVENT_DTMFDOWN) {
2709                 ast_debug(1, "DTMF Down '%c'\n", res & 0xff);
2710                 /* Mute conference */
2711                 analog_confmute(p, 1);
2712                 p->subs[idx].f.frametype = AST_FRAME_DTMF_BEGIN;
2713                 p->subs[idx].f.subclass.integer = res & 0xff;
2714                 analog_handle_dtmf(p, ast, idx, &f);
2715                 return f;
2716         }
2717
2718         switch (res) {
2719         case ANALOG_EVENT_ALARM:
2720         case ANALOG_EVENT_POLARITY:
2721         case ANALOG_EVENT_ONHOOK:
2722                 /* add length of "ANALOG " */
2723                 data_size += 7;
2724                 subclass = analog_event2str(res);
2725                 data_size += strlen(subclass);
2726                 cause_code = ast_alloca(data_size);
2727                 cause_code->ast_cause = AST_CAUSE_NORMAL_CLEARING;
2728                 ast_copy_string(cause_code->chan_name, ast_channel_name(ast), AST_CHANNEL_NAME);
2729                 snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "ANALOG %s", subclass);
2730                 break;
2731         default:
2732                 break;
2733         }
2734
2735         switch (res) {
2736         case ANALOG_EVENT_EC_DISABLED:
2737                 ast_verb(3, "Channel %d echo canceler disabled due to CED detection\n", p->channel);
2738                 analog_set_echocanceller(p, 0);
2739                 break;
2740 #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
2741         case ANALOG_EVENT_TX_CED_DETECTED:
2742                 ast_verb(3, "Channel %d detected a CED tone towards the network.\n", p->channel);
2743                 break;
2744         case ANALOG_EVENT_RX_CED_DETECTED:
2745                 ast_verb(3, "Channel %d detected a CED tone from the network.\n", p->channel);
2746                 break;
2747         case ANALOG_EVENT_EC_NLP_DISABLED:
2748                 ast_verb(3, "Channel %d echo canceler disabled its NLP.\n", p->channel);
2749                 break;
2750         case ANALOG_EVENT_EC_NLP_ENABLED:
2751                 ast_verb(3, "Channel %d echo canceler enabled its NLP.\n", p->channel);
2752                 break;
2753 #endif
2754         case ANALOG_EVENT_PULSE_START:
2755                 /* Stop tone if there's a pulse start and the PBX isn't started */
2756                 if (!ast_channel_pbx(ast))
2757                         analog_play_tone(p, ANALOG_SUB_REAL, -1);
2758                 break;
2759         case ANALOG_EVENT_DIALCOMPLETE:
2760                 if (p->inalarm) {
2761                         break;
2762                 }
2763                 x = analog_is_dialing(p, idx);
2764                 if (!x) { /* if not still dialing in driver */
2765                         analog_set_echocanceller(p, 1);
2766                         if (p->echobreak) {
2767                                 analog_train_echocanceller(p);
2768                                 ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
2769                                 p->dop.op = ANALOG_DIAL_OP_REPLACE;
2770                                 if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
2771                                         int dial_err = errno;
2772                                         ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(dial_err));
2773                                 }
2774                                 p->echobreak = 0;
2775                         } else {
2776                                 analog_set_dialing(p, 0);
2777                                 if ((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) {
2778                                         /* if thru with dialing after offhook */
2779                                         if (ast_channel_state(ast) == AST_STATE_DIALING_OFFHOOK) {
2780                                                 ast_setstate(ast, AST_STATE_UP);
2781                                                 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
2782                                                 p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
2783                                                 break;
2784                                         } else { /* if to state wait for offhook to dial rest */
2785                                                 /* we now wait for off hook */
2786                                                 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
2787                                         }
2788                                 }
2789                                 if (ast_channel_state(ast) == AST_STATE_DIALING) {
2790                                         if (analog_have_progressdetect(p)) {
2791                                                 ast_debug(1, "Done dialing, but waiting for progress detection before doing more...\n");
2792                                         } else if (analog_check_confirmanswer(p) || (!p->dialednone
2793                                                 && ((mysig == ANALOG_SIG_EM) || (mysig == ANALOG_SIG_EM_E1)
2794                                                         || (mysig == ANALOG_SIG_EMWINK) || (mysig == ANALOG_SIG_FEATD)
2795                                                         || (mysig == ANALOG_SIG_FEATDMF_TA) || (mysig == ANALOG_SIG_FEATDMF)
2796                                                         || (mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA)
2797                                                         || (mysig == ANALOG_SIG_FGC_CAMAMF) || (mysig == ANALOG_SIG_FEATB)
2798                                                         || (mysig == ANALOG_SIG_SF) || (mysig == ANALOG_SIG_SFWINK)
2799                                                         || (mysig == ANALOG_SIG_SF_FEATD) || (mysig == ANALOG_SIG_SF_FEATDMF)
2800                                                         || (mysig == ANALOG_SIG_SF_FEATB)))) {
2801                                                 ast_setstate(ast, AST_STATE_RINGING);
2802                                         } else if (!p->answeronpolarityswitch) {