Named ACLs: Introduces a system for creating and sharing ACLs
[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 (p->calls->start_cid_detect) {
165                 return p->calls->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 (p->calls->stop_cid_detect) {
173                 return p->calls->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 (p->calls->get_callerid) {
181                 return p->calls->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 (p->calls->get_orig_dialstring) {
189                 return p->calls->get_orig_dialstring(p->chan_pvt);
190         }
191         return "";
192 }
193
194 static int analog_get_event(struct analog_pvt *p)
195 {
196         if (p->calls->get_event) {
197                 return p->calls->get_event(p->chan_pvt);
198         }
199         return -1;
200 }
201
202 static int analog_wait_event(struct analog_pvt *p)
203 {
204         if (p->calls->wait_event) {
205                 return p->calls->wait_event(p->chan_pvt);
206         }
207         return -1;
208 }
209
210 static int analog_have_progressdetect(struct analog_pvt *p)
211 {
212         if (p->calls->have_progressdetect) {
213                 return p->calls->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 (p->calls->swap_subs) {
349                 p->calls->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 (p->calls->allocate_sub) {
356                 int res;
357                 res = p->calls->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 (p->calls->unallocate_sub) {
371                 return p->calls->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 (p->calls->send_callerid) {
387                 return p->calls->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 (p->calls->dsp_reset_and_flush_digits) {
416                 return p->calls->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 (p->calls->play_tone) {
426                 return p->calls->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 (p->calls->set_new_owner) {
435                 p->calls->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 (!p->calls->new_ast_channel) {
444                 return NULL;
445         }
446
447         c = p->calls->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 (p->calls->set_echocanceller) {
461                 return p->calls->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 (p->calls->train_echocanceller) {
469                 return p->calls->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 (p->calls->is_off_hook) {
477                 return p->calls->is_off_hook(p->chan_pvt);
478         }
479         return -1;
480 }
481
482 static int analog_ring(struct analog_pvt *p)
483 {
484         if (p->calls->ring) {
485                 return p->calls->ring(p->chan_pvt);
486         }
487         return -1;
488 }
489
490 static int analog_flash(struct analog_pvt *p)
491 {
492         if (p->calls->flash) {
493                 return p->calls->flash(p->chan_pvt);
494         }
495         return -1;
496 }
497
498 static int analog_start(struct analog_pvt *p)
499 {
500         if (p->calls->start) {
501                 return p->calls->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 (p->calls->dial_digits) {
509                 return p->calls->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 (p->calls->on_hook) {
517                 return p->calls->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 (p->calls->set_outgoing) {
526                 p->calls->set_outgoing(p->chan_pvt, is_outgoing);
527         }
528 }
529
530 static int analog_check_for_conference(struct analog_pvt *p)
531 {
532         if (p->calls->check_for_conference) {
533                 return p->calls->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 (p->calls->all_subchannels_hungup) {
541                 p->calls->all_subchannels_hungup(p->chan_pvt);
542         }
543 }
544
545 static void analog_unlock_private(struct analog_pvt *p)
546 {
547         if (p->calls->unlock_private) {
548                 p->calls->unlock_private(p->chan_pvt);
549         }
550 }
551
552 static void analog_lock_private(struct analog_pvt *p)
553 {
554         if (p->calls->lock_private) {
555                 p->calls->lock_private(p->chan_pvt);
556         }
557 }
558
559 static void analog_deadlock_avoidance_private(struct analog_pvt *p)
560 {
561         if (p->calls->deadlock_avoidance_private) {
562                 p->calls->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 (p->calls->off_hook) {
605                 return p->calls->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 (p->calls->set_needringing) {
613                 return p->calls->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 (p->calls->set_polarity) {
621                 return p->calls->set_polarity(p->chan_pvt, value);
622         }
623 }
624 #endif
625
626 static void analog_start_polarityswitch(struct analog_pvt *p)
627 {
628         if (p->calls->start_polarityswitch) {
629                 return p->calls->start_polarityswitch(p->chan_pvt);
630         }
631 }
632 static void analog_answer_polarityswitch(struct analog_pvt *p)
633 {
634         if (p->calls->answer_polarityswitch) {
635                 return p->calls->answer_polarityswitch(p->chan_pvt);
636         }
637 }
638
639 static void analog_hangup_polarityswitch(struct analog_pvt *p)
640 {
641         if (p->calls->hangup_polarityswitch) {
642                 return p->calls->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 (p->calls->dsp_set_digitmode) {
649                 return p->calls->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 (p->calls->handle_dtmf) {
657                 p->calls->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 (p->calls->wink) {
664                 return p->calls->wink(p->chan_pvt, index);
665         }
666         return -1;
667 }
668
669 static int analog_has_voicemail(struct analog_pvt *p)
670 {
671         if (p->calls->has_voicemail) {
672                 return p->calls->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 (p->calls->is_dialing) {
680                 return p->calls->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 (p->calls->conf_add) {
783                                 p->calls->conf_add(p->chan_pvt, x);
784                         }
785                         needconf++;
786                 } else {
787                         if (p->calls->conf_del) {
788                                 p->calls->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 (p->calls->complete_conference_update) {
795                 p->calls->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 (p->calls->stop_callwait) {
891                 return p->calls->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 (p->calls->callwait) {
900                 return p->calls->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 (p->calls->set_callwaiting) {
909                 p->calls->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 (p->calls->set_cadence) {
916                 return p->calls->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 (p->calls->set_dialing) {
924                 return p->calls->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 (p->calls->set_alarm) {
932                 return p->calls->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 (!p->calls->set_ringtimeout) {
940                 return;
941         }
942         p->calls->set_ringtimeout(p->chan_pvt, ringt);
943 }
944
945 static void analog_set_waitingfordt(struct analog_pvt *p, struct ast_channel *ast)
946 {
947         if (p->calls->set_waitingfordt) {
948                 return p->calls->set_waitingfordt(p->chan_pvt, ast);
949         }
950 }
951
952 static int analog_check_waitingfordt(struct analog_pvt *p)
953 {
954         if (p->calls->check_waitingfordt) {
955                 return p->calls->check_waitingfordt(p->chan_pvt);
956         }
957
958         return 0;
959 }
960
961 static void analog_set_confirmanswer(struct analog_pvt *p, int flag)
962 {
963         if (!p->calls->set_confirmanswer) {
964                 return;
965         }
966         p->calls->set_confirmanswer(p->chan_pvt, flag);
967 }
968
969 static int analog_check_confirmanswer(struct analog_pvt *p)
970 {
971         if (p->calls->check_confirmanswer) {
972                 return p->calls->check_confirmanswer(p->chan_pvt);
973         }
974
975         return 0;
976 }
977
978 static void analog_cancel_cidspill(struct analog_pvt *p)
979 {
980         if (!p->calls->cancel_cidspill) {
981                 return;
982         }
983
984         p->calls->cancel_cidspill(p->chan_pvt);
985 }
986
987 static int analog_confmute(struct analog_pvt *p, int mute)
988 {
989         if (p->calls->confmute) {
990                 return p->calls->confmute(p->chan_pvt, mute);
991         }
992         return 0;
993 }
994
995 static void analog_set_pulsedial(struct analog_pvt *p, int flag)
996 {
997         if (!p->calls->set_pulsedial) {
998                 return;
999         }
1000         p->calls->set_pulsedial(p->chan_pvt, flag);
1001 }
1002
1003 static int analog_set_linear_mode(struct analog_pvt *p, enum analog_sub sub, int linear_mode)
1004 {
1005         if (p->calls->set_linear_mode) {
1006                 /* Return provides old linear_mode setting or error indication */
1007                 return p->calls->set_linear_mode(p->chan_pvt, sub, linear_mode);
1008         }
1009         return -1;
1010 }
1011
1012 static void analog_set_inthreeway(struct analog_pvt *p, enum analog_sub sub, int inthreeway)
1013 {
1014         p->subs[sub].inthreeway = inthreeway;
1015         if (p->calls->set_inthreeway) {
1016                 p->calls->set_inthreeway(p->chan_pvt, sub, inthreeway);
1017         }
1018 }
1019
1020 int analog_call(struct analog_pvt *p, struct ast_channel *ast, const char *rdest, int timeout)
1021 {
1022         int res, idx, mysig;
1023         char *c, *n, *l;
1024         char dest[256]; /* must be same length as p->dialdest */
1025
1026         ast_debug(1, "CALLING CID_NAME: %s CID_NUM:: %s\n",
1027                 S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, ""),
1028                 S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, ""));
1029
1030         ast_copy_string(dest, rdest, sizeof(dest));
1031         ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1032
1033         if ((ast_channel_state(ast) == AST_STATE_BUSY)) {
1034                 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_BUSY);
1035                 return 0;
1036         }
1037
1038         if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
1039                 ast_log(LOG_WARNING, "analog_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
1040                 return -1;
1041         }
1042
1043         p->dialednone = 0;
1044         analog_set_outgoing(p, 1);
1045
1046         mysig = p->sig;
1047         if (p->outsigmod > -1) {
1048                 mysig = p->outsigmod;
1049         }
1050
1051         switch (mysig) {
1052         case ANALOG_SIG_FXOLS:
1053         case ANALOG_SIG_FXOGS:
1054         case ANALOG_SIG_FXOKS:
1055                 if (p->owner == ast) {
1056                         /* Normal ring, on hook */
1057
1058                         /* Don't send audio while on hook, until the call is answered */
1059                         analog_set_dialing(p, 1);
1060                         analog_set_cadence(p, ast); /* and set p->cidrings */
1061
1062                         /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1063                         c = strchr(dest, '/');
1064                         if (c) {
1065                                 c++;
1066                         }
1067                         if (c && (strlen(c) < p->stripmsd)) {
1068                                 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1069                                 c = NULL;
1070                         }
1071                         if (c) {
1072                                 p->dop.op = ANALOG_DIAL_OP_REPLACE;
1073                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1074                                 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
1075                         } else {
1076                                 p->dop.dialstr[0] = '\0';
1077                         }
1078
1079                         if (analog_ring(p)) {
1080                                 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1081                                 return -1;
1082                         }
1083                         analog_set_dialing(p, 1);
1084                 } else {
1085                         /* Call waiting call */
1086                         if (ast_channel_connected(ast)->id.number.valid && ast_channel_connected(ast)->id.number.str) {
1087                                 ast_copy_string(p->callwait_num, ast_channel_connected(ast)->id.number.str, sizeof(p->callwait_num));
1088                         } else {
1089                                 p->callwait_num[0] = '\0';
1090                         }
1091                         if (ast_channel_connected(ast)->id.name.valid && ast_channel_connected(ast)->id.name.str) {
1092                                 ast_copy_string(p->callwait_name, ast_channel_connected(ast)->id.name.str, sizeof(p->callwait_name));
1093                         } else {
1094                                 p->callwait_name[0] = '\0';
1095                         }
1096
1097                         /* Call waiting tone instead */
1098                         if (analog_callwait(p)) {
1099                                 return -1;
1100                         }
1101                         /* Make ring-back */
1102                         if (analog_play_tone(p, ANALOG_SUB_CALLWAIT, ANALOG_TONE_RINGTONE)) {
1103                                 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast_channel_name(ast));
1104                         }
1105
1106                 }
1107                 n = ast_channel_connected(ast)->id.name.valid ? ast_channel_connected(ast)->id.name.str : NULL;
1108                 l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
1109                 if (l) {
1110                         ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
1111                 } else {
1112                         p->lastcid_num[0] = '\0';
1113                 }
1114                 if (n) {
1115                         ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
1116                 } else {
1117                         p->lastcid_name[0] = '\0';
1118                 }
1119
1120                 if (p->use_callerid) {
1121                         p->caller.id.name.str = p->lastcid_name;
1122                         p->caller.id.number.str = p->lastcid_num;
1123                 }
1124
1125                 ast_setstate(ast, AST_STATE_RINGING);
1126                 idx = analog_get_index(ast, p, 0);
1127                 if (idx > -1) {
1128                         struct ast_cc_config_params *cc_params;
1129
1130                         /* This is where the initial ringing frame is queued for an analog call.
1131                          * As such, this is a great time to offer CCNR to the caller if it's available.
1132                          */
1133                         cc_params = ast_channel_get_cc_config_params(p->subs[idx].owner);
1134                         if (cc_params) {
1135                                 switch (ast_get_cc_monitor_policy(cc_params)) {
1136                                 case AST_CC_MONITOR_NEVER:
1137                                         break;
1138                                 case AST_CC_MONITOR_NATIVE:
1139                                 case AST_CC_MONITOR_ALWAYS:
1140                                 case AST_CC_MONITOR_GENERIC:
1141                                         ast_queue_cc_frame(p->subs[idx].owner, AST_CC_GENERIC_MONITOR_TYPE,
1142                                                 analog_get_orig_dialstring(p), AST_CC_CCNR, NULL);
1143                                         break;
1144                                 }
1145                         }
1146                         ast_queue_control(p->subs[idx].owner, AST_CONTROL_RINGING);
1147                 }
1148                 break;
1149         case ANALOG_SIG_FXSLS:
1150         case ANALOG_SIG_FXSGS:
1151         case ANALOG_SIG_FXSKS:
1152                 if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
1153                         ast_debug(1, "Ignore possible polarity reversal on line seizure\n");
1154                         p->polaritydelaytv = ast_tvnow();
1155                 }
1156                 /* fall through */
1157         case ANALOG_SIG_EMWINK:
1158         case ANALOG_SIG_EM:
1159         case ANALOG_SIG_EM_E1:
1160         case ANALOG_SIG_FEATD:
1161         case ANALOG_SIG_FEATDMF:
1162         case ANALOG_SIG_E911:
1163         case ANALOG_SIG_FGC_CAMA:
1164         case ANALOG_SIG_FGC_CAMAMF:
1165         case ANALOG_SIG_FEATB:
1166         case ANALOG_SIG_SFWINK:
1167         case ANALOG_SIG_SF:
1168         case ANALOG_SIG_SF_FEATD:
1169         case ANALOG_SIG_SF_FEATDMF:
1170         case ANALOG_SIG_FEATDMF_TA:
1171         case ANALOG_SIG_SF_FEATB:
1172                 c = strchr(dest, '/');
1173                 if (c) {
1174                         c++;
1175                 } else {
1176                         c = "";
1177                 }
1178                 if (strlen(c) < p->stripmsd) {
1179                         ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1180                         return -1;
1181                 }
1182                 res = analog_start(p);
1183                 if (res < 0) {
1184                         if (errno != EINPROGRESS) {
1185                                 return -1;
1186                         }
1187                 }
1188                 ast_debug(1, "Dialing '%s'\n", c);
1189                 p->dop.op = ANALOG_DIAL_OP_REPLACE;
1190
1191                 c += p->stripmsd;
1192
1193                 switch (mysig) {
1194                 case ANALOG_SIG_FEATD:
1195                         l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
1196                         if (l) {
1197                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
1198                         } else {
1199                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
1200                         }
1201                         break;
1202                 case ANALOG_SIG_FEATDMF:
1203                         l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
1204                         if (l) {
1205                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
1206                         } else {
1207                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
1208                         }
1209                         break;
1210                 case ANALOG_SIG_FEATDMF_TA:
1211                 {
1212                         const char *cic = "", *ozz = "";
1213
1214                         /* If you have to go through a Tandem Access point you need to use this */
1215 #ifndef STANDALONE
1216                         ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
1217                         if (!ozz) {
1218                                 ozz = analog_defaultozz;
1219                         }
1220                         cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
1221                         if (!cic) {
1222                                 cic = analog_defaultcic;
1223                         }
1224 #endif
1225                         if (!ozz || !cic) {
1226                                 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
1227                                 return -1;
1228                         }
1229                         snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
1230                         snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
1231                         p->whichwink = 0;
1232                 }
1233                         break;
1234                 case ANALOG_SIG_E911:
1235                         ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
1236                         break;
1237                 case ANALOG_SIG_FGC_CAMA:
1238                         snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
1239                         break;
1240                 case ANALOG_SIG_FGC_CAMAMF:
1241                 case ANALOG_SIG_FEATB:
1242                         snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
1243                         break;
1244                 default:
1245                         if (p->pulse) {
1246                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
1247                         } else {
1248                                 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
1249                         }
1250                         break;
1251                 }
1252
1253                 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1254                         memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1255                         strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1256                         p->echorest[sizeof(p->echorest) - 1] = '\0';
1257                         p->echobreak = 1;
1258                         p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1259                 } else {
1260                         p->echobreak = 0;
1261                 }
1262                 analog_set_waitingfordt(p, ast);
1263                 if (!res) {
1264                         if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
1265                                 int saveerr = errno;
1266
1267                                 analog_on_hook(p);
1268                                 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
1269                                 return -1;
1270                         }
1271                 } else {
1272                         ast_debug(1, "Deferring dialing...\n");
1273                 }
1274                 analog_set_dialing(p, 1);
1275                 if (ast_strlen_zero(c)) {
1276                         p->dialednone = 1;
1277                 }
1278                 ast_setstate(ast, AST_STATE_DIALING);
1279                 break;
1280         default:
1281                 ast_debug(1, "not yet implemented\n");
1282                 return -1;
1283         }
1284         return 0;
1285 }
1286
1287 int analog_hangup(struct analog_pvt *p, struct ast_channel *ast)
1288 {
1289         int res;
1290         int idx, x;
1291
1292         ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1293         if (!ast_channel_tech_pvt(ast)) {
1294                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1295                 return 0;
1296         }
1297
1298         idx = analog_get_index(ast, p, 1);
1299
1300         x = 0;
1301         if (p->origcid_num) {
1302                 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
1303                 ast_free(p->origcid_num);
1304                 p->origcid_num = NULL;
1305         }
1306         if (p->origcid_name) {
1307                 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
1308                 ast_free(p->origcid_name);
1309                 p->origcid_name = NULL;
1310         }
1311
1312         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1313
1314         ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1315                 p->channel, idx, p->subs[ANALOG_SUB_REAL].allocd, p->subs[ANALOG_SUB_CALLWAIT].allocd, p->subs[ANALOG_SUB_THREEWAY].allocd);
1316         if (idx > -1) {
1317                 /* Real channel, do some fixup */
1318                 p->subs[idx].owner = NULL;
1319                 p->polarity = POLARITY_IDLE;
1320                 analog_set_linear_mode(p, idx, 0);
1321                 switch (idx) {
1322                 case ANALOG_SUB_REAL:
1323                         if (p->subs[ANALOG_SUB_CALLWAIT].allocd && p->subs[ANALOG_SUB_THREEWAY].allocd) {
1324                                 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
1325                                 if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
1326                                         /* We had flipped over to answer a callwait and now it's gone */
1327                                         ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
1328                                         /* Move to the call-wait, but un-own us until they flip back. */
1329                                         analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
1330                                         analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
1331                                         analog_set_new_owner(p, NULL);
1332                                 } else {
1333                                         /* The three way hung up, but we still have a call wait */
1334                                         ast_debug(1, "We were in the threeway and have a callwait still.  Ditching the threeway.\n");
1335                                         analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
1336                                         analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1337                                         if (p->subs[ANALOG_SUB_REAL].inthreeway) {
1338                                                 /* This was part of a three way call.  Immediately make way for
1339                                                    another call */
1340                                                 ast_debug(1, "Call was complete, setting owner to former third call\n");
1341                                                 analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
1342                                                 analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
1343                                         } else {
1344                                                 /* This call hasn't been completed yet...  Set owner to NULL */
1345                                                 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
1346                                                 analog_set_new_owner(p, NULL);
1347                                         }
1348                                 }
1349                         } else if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
1350                                 /* Need to hold the lock for real-call, private, and call-waiting call */
1351                                 analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
1352                                 if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
1353                                         /* The call waiting call dissappeared. */
1354                                         analog_set_new_owner(p, NULL);
1355                                         break;
1356                                 }
1357
1358                                 /* Move to the call-wait and switch back to them. */
1359                                 analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
1360                                 analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
1361                                 analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
1362                                 if (ast_channel_state(p->owner) != AST_STATE_UP) {
1363                                         ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_ANSWER);
1364                                 }
1365                                 if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner)) {
1366                                         ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
1367                                 }
1368                                 /* Unlock the call-waiting call that we swapped to real-call. */
1369                                 ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
1370                         } else if (p->subs[ANALOG_SUB_THREEWAY].allocd) {
1371                                 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
1372                                 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1373                                 if (p->subs[ANALOG_SUB_REAL].inthreeway) {
1374                                         /* This was part of a three way call.  Immediately make way for
1375                                            another call */
1376                                         ast_debug(1, "Call was complete, setting owner to former third call\n");
1377                                         analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
1378                                         analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
1379                                 } else {
1380                                         /* This call hasn't been completed yet...  Set owner to NULL */
1381                                         ast_debug(1, "Call was incomplete, setting owner to NULL\n");
1382                                         analog_set_new_owner(p, NULL);
1383                                 }
1384                         }
1385                         break;
1386                 case ANALOG_SUB_CALLWAIT:
1387                         /* Ditch the holding callwait call, and immediately make it available */
1388                         if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
1389                                 /* Need to hold the lock for call-waiting call, private, and 3-way call */
1390                                 analog_lock_sub_owner(p, ANALOG_SUB_THREEWAY);
1391
1392                                 /* This is actually part of a three way, placed on hold.  Place the third part
1393                                    on music on hold now */
1394                                 if (p->subs[ANALOG_SUB_THREEWAY].owner && ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
1395                                         ast_queue_control_data(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CONTROL_HOLD,
1396                                                 S_OR(p->mohsuggest, NULL),
1397                                                 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
1398                                 }
1399                                 analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 0);
1400                                 /* Make it the call wait now */
1401                                 analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
1402                                 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1403                                 if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
1404                                         /* Unlock the 3-way call that we swapped to call-waiting call. */
1405                                         ast_channel_unlock(p->subs[ANALOG_SUB_CALLWAIT].owner);
1406                                 }
1407                         } else {
1408                                 analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
1409                         }
1410                         break;
1411                 case ANALOG_SUB_THREEWAY:
1412                         /* Need to hold the lock for 3-way call, private, and call-waiting call */
1413                         analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
1414                         if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
1415                                 /* The other party of the three way call is currently in a call-wait state.
1416                                    Start music on hold for them, and take the main guy out of the third call */
1417                                 analog_set_inthreeway(p, ANALOG_SUB_CALLWAIT, 0);
1418                                 if (p->subs[ANALOG_SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[ANALOG_SUB_CALLWAIT].owner)) {
1419                                         ast_queue_control_data(p->subs[ANALOG_SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
1420                                                 S_OR(p->mohsuggest, NULL),
1421                                                 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
1422                                 }
1423                         }
1424                         if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
1425                                 ast_channel_unlock(p->subs[ANALOG_SUB_CALLWAIT].owner);
1426                         }
1427                         analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
1428                         /* If this was part of a three way call index, let us make
1429                            another three way call */
1430                         analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1431                         break;
1432                 default:
1433                         /*
1434                          * Should never happen.
1435                          * This wasn't any sort of call, so how are we an index?
1436                          */
1437                         ast_log(LOG_ERROR, "Index found but not any type of call?\n");
1438                         break;
1439                 }
1440         }
1441
1442         if (!p->subs[ANALOG_SUB_REAL].owner && !p->subs[ANALOG_SUB_CALLWAIT].owner && !p->subs[ANALOG_SUB_THREEWAY].owner) {
1443                 analog_set_new_owner(p, NULL);
1444                 analog_set_ringtimeout(p, 0);
1445                 analog_set_confirmanswer(p, 0);
1446                 analog_set_pulsedial(p, 0);
1447                 analog_set_outgoing(p, 0);
1448                 p->onhooktime = time(NULL);
1449                 p->cidrings = 1;
1450
1451                 /* Perform low level hangup if no owner left */
1452                 res = analog_on_hook(p);
1453                 if (res < 0) {
1454                         ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast_channel_name(ast));
1455                 }
1456                 switch (p->sig) {
1457                 case ANALOG_SIG_FXOGS:
1458                 case ANALOG_SIG_FXOLS:
1459                 case ANALOG_SIG_FXOKS:
1460                         /* If they're off hook, try playing congestion */
1461                         if (analog_is_off_hook(p)) {
1462                                 analog_hangup_polarityswitch(p);
1463                                 analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
1464                         } else {
1465                                 analog_play_tone(p, ANALOG_SUB_REAL, -1);
1466                         }
1467                         break;
1468                 case ANALOG_SIG_FXSGS:
1469                 case ANALOG_SIG_FXSLS:
1470                 case ANALOG_SIG_FXSKS:
1471                         /* Make sure we're not made available for at least two seconds assuming
1472                            we were actually used for an inbound or outbound call. */
1473                         if (ast_channel_state(ast) != AST_STATE_RESERVED) {
1474                                 time(&p->guardtime);
1475                                 p->guardtime += 2;
1476                         }
1477                         break;
1478                 default:
1479                         analog_play_tone(p, ANALOG_SUB_REAL, -1);
1480                         break;
1481                 }
1482
1483                 analog_set_echocanceller(p, 0);
1484
1485                 x = 0;
1486                 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
1487                 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
1488                 p->callwaitcas = 0;
1489                 analog_set_callwaiting(p, p->permcallwaiting);
1490                 p->hidecallerid = p->permhidecallerid;
1491                 analog_set_dialing(p, 0);
1492                 analog_update_conf(p);
1493                 analog_all_subchannels_hungup(p);
1494         }
1495
1496         analog_stop_callwait(p);
1497
1498         ast_verb(3, "Hanging up on '%s'\n", ast_channel_name(ast));
1499
1500         return 0;
1501 }
1502
1503 int analog_answer(struct analog_pvt *p, struct ast_channel *ast)
1504 {
1505         int res = 0;
1506         int idx;
1507         int oldstate = ast_channel_state(ast);
1508
1509         ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1510         ast_setstate(ast, AST_STATE_UP);
1511         idx = analog_get_index(ast, p, 1);
1512         if (idx < 0) {
1513                 idx = ANALOG_SUB_REAL;
1514         }
1515         switch (p->sig) {
1516         case ANALOG_SIG_FXSLS:
1517         case ANALOG_SIG_FXSGS:
1518         case ANALOG_SIG_FXSKS:
1519                 analog_set_ringtimeout(p, 0);
1520                 /* Fall through */
1521         case ANALOG_SIG_EM:
1522         case ANALOG_SIG_EM_E1:
1523         case ANALOG_SIG_EMWINK:
1524         case ANALOG_SIG_FEATD:
1525         case ANALOG_SIG_FEATDMF:
1526         case ANALOG_SIG_FEATDMF_TA:
1527         case ANALOG_SIG_E911:
1528         case ANALOG_SIG_FGC_CAMA:
1529         case ANALOG_SIG_FGC_CAMAMF:
1530         case ANALOG_SIG_FEATB:
1531         case ANALOG_SIG_SF:
1532         case ANALOG_SIG_SFWINK:
1533         case ANALOG_SIG_SF_FEATD:
1534         case ANALOG_SIG_SF_FEATDMF:
1535         case ANALOG_SIG_SF_FEATB:
1536         case ANALOG_SIG_FXOLS:
1537         case ANALOG_SIG_FXOGS:
1538         case ANALOG_SIG_FXOKS:
1539                 /* Pick up the line */
1540                 ast_debug(1, "Took %s off hook\n", ast_channel_name(ast));
1541                 if (p->hanguponpolarityswitch) {
1542                         gettimeofday(&p->polaritydelaytv, NULL);
1543                 }
1544                 res = analog_off_hook(p);
1545                 analog_play_tone(p, idx, -1);
1546                 analog_set_dialing(p, 0);
1547                 if ((idx == ANALOG_SUB_REAL) && p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
1548                         if (oldstate == AST_STATE_RINGING) {
1549                                 ast_debug(1, "Finally swapping real and threeway\n");
1550                                 analog_play_tone(p, ANALOG_SUB_THREEWAY, -1);
1551                                 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
1552                                 analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
1553                         }
1554                 }
1555
1556                 switch (p->sig) {
1557                 case ANALOG_SIG_FXSLS:
1558                 case ANALOG_SIG_FXSKS:
1559                 case ANALOG_SIG_FXSGS:
1560                         analog_set_echocanceller(p, 1);
1561                         analog_train_echocanceller(p);
1562                         break;
1563                 case ANALOG_SIG_FXOLS:
1564                 case ANALOG_SIG_FXOKS:
1565                 case ANALOG_SIG_FXOGS:
1566                         analog_answer_polarityswitch(p);
1567                         break;
1568                 default:
1569                         break;
1570                 }
1571                 break;
1572         default:
1573                 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
1574                 res = -1;
1575                 break;
1576         }
1577         ast_setstate(ast, AST_STATE_UP);
1578         return res;
1579 }
1580
1581 static int analog_handles_digit(struct ast_frame *f)
1582 {
1583         char subclass = toupper(f->subclass.integer);
1584
1585         switch (subclass) {
1586         case '1':
1587         case '2':
1588         case '3':
1589         case '4':
1590         case '5':
1591         case '6':
1592         case '7':
1593         case '9':
1594         case 'A':
1595         case 'B':
1596         case 'C':
1597         case 'D':
1598         case 'E':
1599         case 'F':
1600                 return 1;
1601         default:
1602                 return 0;
1603         }
1604 }
1605
1606 void analog_handle_dtmf(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub idx, struct ast_frame **dest)
1607 {
1608         struct ast_frame *f = *dest;
1609
1610         ast_debug(1, "%s DTMF digit: 0x%02X '%c' on %s\n",
1611                 f->frametype == AST_FRAME_DTMF_BEGIN ? "Begin" : "End",
1612                 f->subclass.integer, f->subclass.integer, ast_channel_name(ast));
1613
1614         if (analog_check_confirmanswer(p)) {
1615                 if (f->frametype == AST_FRAME_DTMF_END) {
1616                         ast_debug(1, "Confirm answer on %s!\n", ast_channel_name(ast));
1617                         /* Upon receiving a DTMF digit, consider this an answer confirmation instead
1618                         of a DTMF digit */
1619                         p->subs[idx].f.frametype = AST_FRAME_CONTROL;
1620                         p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
1621                         /* Reset confirmanswer so DTMF's will behave properly for the duration of the call */
1622                         analog_set_confirmanswer(p, 0);
1623                 } else {
1624                         p->subs[idx].f.frametype = AST_FRAME_NULL;
1625                         p->subs[idx].f.subclass.integer = 0;
1626                 }
1627                 *dest = &p->subs[idx].f;
1628         } else if (p->callwaitcas) {
1629                 if (f->frametype == AST_FRAME_DTMF_END) {
1630                         if ((f->subclass.integer == 'A') || (f->subclass.integer == 'D')) {
1631                                 ast_debug(1, "Got some DTMF, but it's for the CAS\n");
1632                                 p->caller.id.name.str = p->callwait_name;
1633                                 p->caller.id.number.str = p->callwait_num;
1634                                 analog_send_callerid(p, 1, &p->caller);
1635                         }
1636                         if (analog_handles_digit(f)) {
1637                                 p->callwaitcas = 0;
1638                         }
1639                 }
1640                 p->subs[idx].f.frametype = AST_FRAME_NULL;
1641                 p->subs[idx].f.subclass.integer = 0;
1642                 *dest = &p->subs[idx].f;
1643         } else {
1644                 analog_cb_handle_dtmf(p, ast, idx, dest);
1645         }
1646 }
1647
1648 static int analog_my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
1649 {
1650         char c;
1651
1652         *str = 0; /* start with empty output buffer */
1653         for (;;) {
1654                 /* Wait for the first digit (up to specified ms). */
1655                 c = ast_waitfordigit(chan, ms);
1656                 /* if timeout, hangup or error, return as such */
1657                 if (c < 1) {
1658                         return c;
1659                 }
1660                 *str++ = c;
1661                 *str = 0;
1662                 if (strchr(term, c)) {
1663                         return 1;
1664                 }
1665         }
1666 }
1667
1668 static int analog_handle_notify_message(struct ast_channel *chan, struct analog_pvt *p, int cid_flags, int neon_mwievent)
1669 {
1670         if (p->calls->handle_notify_message) {
1671                 p->calls->handle_notify_message(chan, p->chan_pvt, cid_flags, neon_mwievent);
1672                 return 0;
1673         }
1674         return -1;
1675 }
1676
1677 static int analog_increase_ss_count(struct analog_pvt *p)
1678 {
1679         if (p->calls->increase_ss_count) {
1680                 p->calls->increase_ss_count();
1681                 return 0;
1682         }
1683         return -1;
1684 }
1685
1686 static int analog_decrease_ss_count(struct analog_pvt *p)
1687 {
1688         if (p->calls->decrease_ss_count) {
1689                 p->calls->decrease_ss_count();
1690                 return 0;
1691         }
1692         return -1;
1693 }
1694
1695 static int analog_distinctive_ring(struct ast_channel *chan, struct analog_pvt *p, int idx, int *ringdata)
1696 {
1697         if (p->calls->distinctive_ring) {
1698                 return p->calls->distinctive_ring(chan, p->chan_pvt, idx, ringdata);
1699         }
1700         return -1;
1701
1702 }
1703
1704 static void analog_get_and_handle_alarms(struct analog_pvt *p)
1705 {
1706         if (p->calls->get_and_handle_alarms) {
1707                 return p->calls->get_and_handle_alarms(p->chan_pvt);
1708         }
1709 }
1710
1711 static void *analog_get_bridged_channel(struct analog_pvt *p, struct ast_channel *chan)
1712 {
1713         if (p->calls->get_sigpvt_bridged_channel) {
1714                 return p->calls->get_sigpvt_bridged_channel;
1715         }
1716         return NULL;
1717 }
1718
1719 static int analog_get_sub_fd(struct analog_pvt *p, enum analog_sub sub)
1720 {
1721         if (p->calls->get_sub_fd) {
1722                 return p->calls->get_sub_fd(p->chan_pvt, sub);
1723         }
1724         return -1;
1725 }
1726
1727 #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))
1728
1729 static int analog_canmatch_featurecode(const char *exten)
1730 {
1731         int extlen = strlen(exten);
1732         const char *pickup_ext;
1733         if (!extlen) {
1734                 return 1;
1735         }
1736         pickup_ext = ast_pickup_ext();
1737         if (extlen < strlen(pickup_ext) && !strncmp(pickup_ext, exten, extlen)) {
1738                 return 1;
1739         }
1740         /* hardcoded features are *60, *67, *69, *70, *72, *73, *78, *79, *82, *0 */
1741         if (exten[0] == '*' && extlen < 3) {
1742                 if (extlen == 1) {
1743                         return 1;
1744                 }
1745                 /* "*0" should be processed before it gets here */
1746                 switch (exten[1]) {
1747                 case '6':
1748                 case '7':
1749                 case '8':
1750                         return 1;
1751                 }
1752         }
1753         return 0;
1754 }
1755
1756 static void *__analog_ss_thread(void *data)
1757 {
1758         struct analog_pvt *p = data;
1759         struct ast_channel *chan = p->ss_astchan;
1760         char exten[AST_MAX_EXTENSION] = "";
1761         char exten2[AST_MAX_EXTENSION] = "";
1762         char dtmfcid[300];
1763         char dtmfbuf[300];
1764         char namebuf[ANALOG_MAX_CID];
1765         char numbuf[ANALOG_MAX_CID];
1766         struct callerid_state *cs = NULL;
1767         char *name = NULL, *number = NULL;
1768         int flags = 0;
1769         struct ast_smdi_md_message *smdi_msg = NULL;
1770         int timeout;
1771         int getforward = 0;
1772         char *s1, *s2;
1773         int len = 0;
1774         int res;
1775         int idx;
1776         struct ast_callid *callid;
1777
1778         analog_increase_ss_count(p);
1779
1780         ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1781
1782         if (!chan) {
1783                 /* What happened to the channel? */
1784                 goto quit;
1785         }
1786
1787         if ((callid = ast_channel_callid(chan))) {
1788                 ast_callid_threadassoc_add(callid);
1789                 ast_callid_unref(callid);
1790         }
1791
1792         /* in the bizarre case where the channel has become a zombie before we
1793            even get started here, abort safely
1794         */
1795         if (!ast_channel_tech_pvt(chan)) {
1796                 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", ast_channel_name(chan));
1797                 ast_hangup(chan);
1798                 goto quit;
1799         }
1800
1801         ast_verb(3, "Starting simple switch on '%s'\n", ast_channel_name(chan));
1802         idx = analog_get_index(chan, p, 0);
1803         if (idx < 0) {
1804                 ast_hangup(chan);
1805                 goto quit;
1806         }
1807         analog_dsp_reset_and_flush_digits(p);
1808         switch (p->sig) {
1809         case ANALOG_SIG_FEATD:
1810         case ANALOG_SIG_FEATDMF:
1811         case ANALOG_SIG_FEATDMF_TA:
1812         case ANALOG_SIG_E911:
1813         case ANALOG_SIG_FGC_CAMAMF:
1814         case ANALOG_SIG_FEATB:
1815         case ANALOG_SIG_EMWINK:
1816         case ANALOG_SIG_SF_FEATD:
1817         case ANALOG_SIG_SF_FEATDMF:
1818         case ANALOG_SIG_SF_FEATB:
1819         case ANALOG_SIG_SFWINK:
1820                 if (analog_wink(p, idx))
1821                         goto quit;
1822                 /* Fall through */
1823         case ANALOG_SIG_EM:
1824         case ANALOG_SIG_EM_E1:
1825         case ANALOG_SIG_SF:
1826         case ANALOG_SIG_FGC_CAMA:
1827                 res = analog_play_tone(p, idx, -1);
1828
1829                 analog_dsp_reset_and_flush_digits(p);
1830
1831                 /* set digit mode appropriately */
1832                 if (ANALOG_NEED_MFDETECT(p)) {
1833                         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
1834                 } else {
1835                         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1836                 }
1837
1838                 memset(dtmfbuf, 0, sizeof(dtmfbuf));
1839                 /* Wait for the first digit only if immediate=no */
1840                 if (!p->immediate) {
1841                         /* Wait for the first digit (up to 5 seconds). */
1842                         res = ast_waitfordigit(chan, 5000);
1843                 } else {
1844                         res = 0;
1845                 }
1846                 if (res > 0) {
1847                         /* save first char */
1848                         dtmfbuf[0] = res;
1849                         switch (p->sig) {
1850                         case ANALOG_SIG_FEATD:
1851                         case ANALOG_SIG_SF_FEATD:
1852                                 res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
1853                                 if (res > 0) {
1854                                         res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
1855                                 }
1856                                 if (res < 1) {
1857                                         analog_dsp_reset_and_flush_digits(p);
1858                                 }
1859                                 break;
1860                         case ANALOG_SIG_FEATDMF_TA:
1861                                 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1862                                 if (res < 1) {
1863                                         analog_dsp_reset_and_flush_digits(p);
1864                                 }
1865                                 if (analog_wink(p, idx)) {
1866                                         goto quit;
1867                                 }
1868                                 dtmfbuf[0] = 0;
1869                                 /* Wait for the first digit (up to 5 seconds). */
1870                                 res = ast_waitfordigit(chan, 5000);
1871                                 if (res <= 0) {
1872                                         break;
1873                                 }
1874                                 dtmfbuf[0] = res;
1875                                 /* fall through intentionally */
1876                         case ANALOG_SIG_FEATDMF:
1877                         case ANALOG_SIG_E911:
1878                         case ANALOG_SIG_FGC_CAMAMF:
1879                         case ANALOG_SIG_SF_FEATDMF:
1880                                 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1881                                 /* if international caca, do it again to get real ANO */
1882                                 if ((p->sig == ANALOG_SIG_FEATDMF) && (dtmfbuf[1] != '0') 
1883                                         && (strlen(dtmfbuf) != 14)) {
1884                                         if (analog_wink(p, idx)) {
1885                                                 goto quit;
1886                                         }
1887                                         dtmfbuf[0] = 0;
1888                                         /* Wait for the first digit (up to 5 seconds). */
1889                                         res = ast_waitfordigit(chan, 5000);
1890                                         if (res <= 0) {
1891                                                 break;
1892                                         }
1893                                         dtmfbuf[0] = res;
1894                                         res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1895                                 }
1896                                 if (res > 0) {
1897                                         /* if E911, take off hook */
1898                                         if (p->sig == ANALOG_SIG_E911) {
1899                                                 analog_off_hook(p);
1900                                         }
1901                                         res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
1902                                 }
1903                                 if (res < 1) {
1904                                         analog_dsp_reset_and_flush_digits(p);
1905                                 }
1906                                 break;
1907                         case ANALOG_SIG_FEATB:
1908                         case ANALOG_SIG_SF_FEATB:
1909                                 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1910                                 if (res < 1) {
1911                                         analog_dsp_reset_and_flush_digits(p);
1912                                 }
1913                                 break;
1914                         case ANALOG_SIG_EMWINK:
1915                                 /* if we received a '*', we are actually receiving Feature Group D
1916                                    dial syntax, so use that mode; otherwise, fall through to normal
1917                                    mode
1918                                 */
1919                                 if (res == '*') {
1920                                         res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
1921                                         if (res > 0) {
1922                                                 res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
1923                                         }
1924                                         if (res < 1) {
1925                                                 analog_dsp_reset_and_flush_digits(p);
1926                                         }
1927                                         break;
1928                                 }
1929                         default:
1930                                 /* If we got the first digit, get the rest */
1931                                 len = 1;
1932                                 dtmfbuf[len] = '\0';
1933                                 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, ast_channel_context(chan), dtmfbuf, 1, p->cid_num)) {
1934                                         if (ast_exists_extension(chan, ast_channel_context(chan), dtmfbuf, 1, p->cid_num)) {
1935                                                 timeout = analog_matchdigittimeout;
1936                                         } else {
1937                                                 timeout = analog_gendigittimeout;
1938                                         }
1939                                         res = ast_waitfordigit(chan, timeout);
1940                                         if (res < 0) {
1941                                                 ast_debug(1, "waitfordigit returned < 0...\n");
1942                                                 ast_hangup(chan);
1943                                                 goto quit;
1944                                         } else if (res) {
1945                                                 dtmfbuf[len++] = res;
1946                                                 dtmfbuf[len] = '\0';
1947                                         } else {
1948                                                 break;
1949                                         }
1950                                 }
1951                                 break;
1952                         }
1953                 }
1954                 if (res == -1) {
1955                         ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
1956                         ast_hangup(chan);
1957                         goto quit;
1958                 } else if (res < 0) {
1959                         ast_debug(1, "Got hung up before digits finished\n");
1960                         ast_hangup(chan);
1961                         goto quit;
1962                 }
1963
1964                 if (p->sig == ANALOG_SIG_FGC_CAMA) {
1965                         char anibuf[100];
1966
1967                         if (ast_safe_sleep(chan,1000) == -1) {
1968                                 ast_hangup(chan);
1969                                 goto quit;
1970                         }
1971                         analog_off_hook(p);
1972                         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
1973                         res = analog_my_getsigstr(chan, anibuf, "#", 10000);
1974                         if ((res > 0) && (strlen(anibuf) > 2)) {
1975                                 if (anibuf[strlen(anibuf) - 1] == '#') {
1976                                         anibuf[strlen(anibuf) - 1] = 0;
1977                                 }
1978                                 ast_set_callerid(chan, anibuf + 2, NULL, anibuf + 2);
1979                         }
1980                         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1981                 }
1982
1983                 ast_copy_string(exten, dtmfbuf, sizeof(exten));
1984                 if (ast_strlen_zero(exten)) {
1985                         ast_copy_string(exten, "s", sizeof(exten));
1986                 }
1987                 if (p->sig == ANALOG_SIG_FEATD || p->sig == ANALOG_SIG_EMWINK) {
1988                         /* Look for Feature Group D on all E&M Wink and Feature Group D trunks */
1989                         if (exten[0] == '*') {
1990                                 char *stringp=NULL;
1991                                 ast_copy_string(exten2, exten, sizeof(exten2));
1992                                 /* Parse out extension and callerid */
1993                                 stringp=exten2 +1;
1994                                 s1 = strsep(&stringp, "*");
1995                                 s2 = strsep(&stringp, "*");
1996                                 if (s2) {
1997                                         if (!ast_strlen_zero(p->cid_num)) {
1998                                                 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
1999                                         } else {
2000                                                 ast_set_callerid(chan, s1, NULL, s1);
2001                                         }
2002                                         ast_copy_string(exten, s2, sizeof(exten));
2003                                 } else {
2004                                         ast_copy_string(exten, s1, sizeof(exten));
2005                                 }
2006                         } else if (p->sig == ANALOG_SIG_FEATD) {
2007                                 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d.  Assuming E&M Wink instead\n", p->channel);
2008                         }
2009                 }
2010                 if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
2011                         if (exten[0] == '*') {
2012                                 char *stringp=NULL;
2013                                 ast_copy_string(exten2, exten, sizeof(exten2));
2014                                 /* Parse out extension and callerid */
2015                                 stringp=exten2 +1;
2016                                 s1 = strsep(&stringp, "#");
2017                                 s2 = strsep(&stringp, "#");
2018                                 if (s2) {
2019                                         if (!ast_strlen_zero(p->cid_num)) {
2020                                                 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
2021                                         } else {
2022                                                 if (*(s1 + 2)) {
2023                                                         ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
2024                                                 }
2025                                         }
2026                                         ast_copy_string(exten, s2 + 1, sizeof(exten));
2027                                 } else {
2028                                         ast_copy_string(exten, s1 + 2, sizeof(exten));
2029                                 }
2030                         } else {
2031                                 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d.  Assuming E&M Wink instead\n", p->channel);
2032                         }
2033                 }
2034                 if ((p->sig == ANALOG_SIG_E911) || (p->sig == ANALOG_SIG_FGC_CAMAMF)) {
2035                         if (exten[0] == '*') {
2036                                 char *stringp=NULL;
2037                                 ast_copy_string(exten2, exten, sizeof(exten2));
2038                                 /* Parse out extension and callerid */
2039                                 stringp=exten2 +1;
2040                                 s1 = strsep(&stringp, "#");
2041                                 s2 = strsep(&stringp, "#");
2042                                 if (s2 && (*(s2 + 1) == '0')) {
2043                                         if (*(s2 + 2)) {
2044                                                 ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
2045                                         }
2046                                 }
2047                                 if (s1) {
2048                                         ast_copy_string(exten, s1, sizeof(exten));
2049                                 } else {
2050                                         ast_copy_string(exten, "911", sizeof(exten));
2051                                 }
2052                         } else {
2053                                 ast_log(LOG_WARNING, "Got a non-E911/FGC CAMA input on channel %d.  Assuming E&M Wink instead\n", p->channel);
2054                         }
2055                 }
2056                 if (p->sig == ANALOG_SIG_FEATB) {
2057                         if (exten[0] == '*') {
2058                                 char *stringp=NULL;
2059                                 ast_copy_string(exten2, exten, sizeof(exten2));
2060                                 /* Parse out extension and callerid */
2061                                 stringp=exten2 +1;
2062                                 s1 = strsep(&stringp, "#");
2063                                 ast_copy_string(exten, exten2 + 1, sizeof(exten));
2064                         } else {
2065                                 ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d.  Assuming E&M Wink instead\n", p->channel);
2066                         }
2067                 }
2068                 if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
2069                         analog_wink(p, idx);
2070                         /*
2071                          * Some switches require a minimum guard time between the last
2072                          * FGD wink and something that answers immediately.  This
2073                          * ensures it.
2074                          */
2075                         if (ast_safe_sleep(chan, 100)) {
2076                                 ast_hangup(chan);
2077                                 goto quit;
2078                         }
2079                 }
2080                 analog_set_echocanceller(p, 1);
2081
2082                 analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
2083
2084                 if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1,
2085                         ast_channel_caller(chan)->id.number.valid ? ast_channel_caller(chan)->id.number.str : NULL)) {
2086                         ast_channel_exten_set(chan, exten);
2087                         analog_dsp_reset_and_flush_digits(p);
2088                         res = ast_pbx_run(chan);
2089                         if (res) {
2090                                 ast_log(LOG_WARNING, "PBX exited non-zero\n");
2091                                 res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2092                         }
2093                         goto quit;
2094                 } else {
2095                         ast_verb(3, "Unknown extension '%s' in context '%s' requested\n", exten, ast_channel_context(chan));
2096                         sleep(2);
2097                         res = analog_play_tone(p, idx, ANALOG_TONE_INFO);
2098                         if (res < 0) {
2099                                 ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
2100                         } else {
2101                                 sleep(1);
2102                         }
2103                         res = ast_streamfile(chan, "ss-noservice", ast_channel_language(chan));
2104                         if (res >= 0) {
2105                                 ast_waitstream(chan, "");
2106                         }
2107                         res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2108                         ast_hangup(chan);
2109                         goto quit;
2110                 }
2111                 break;
2112         case ANALOG_SIG_FXOLS:
2113         case ANALOG_SIG_FXOGS:
2114         case ANALOG_SIG_FXOKS:
2115                 /* Read the first digit */
2116                 timeout = analog_firstdigittimeout;
2117                 /* If starting a threeway call, never timeout on the first digit so someone
2118                    can use flash-hook as a "hold" feature */
2119                 if (p->subs[ANALOG_SUB_THREEWAY].owner) {
2120                         timeout = 999999;
2121                 }
2122                 while (len < AST_MAX_EXTENSION-1) {
2123                         /* Read digit unless it's supposed to be immediate, in which case the
2124                            only answer is 's' */
2125                         if (p->immediate) {
2126                                 res = 's';
2127                         } else {
2128                                 res = ast_waitfordigit(chan, timeout);
2129                         }
2130                         timeout = 0;
2131                         if (res < 0) {
2132                                 ast_debug(1, "waitfordigit returned < 0...\n");
2133                                 res = analog_play_tone(p, idx, -1);
2134                                 ast_hangup(chan);
2135                                 goto quit;
2136                         } else if (res) {
2137                                 ast_debug(1,"waitfordigit returned '%c' (%d), timeout = %d\n", res, res, timeout);
2138                                 exten[len++]=res;
2139                                 exten[len] = '\0';
2140                         }
2141                         if (!ast_ignore_pattern(ast_channel_context(chan), exten)) {
2142                                 analog_play_tone(p, idx, -1);
2143                         } else {
2144                                 analog_play_tone(p, idx, ANALOG_TONE_DIALTONE);
2145                         }
2146                         if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num) && !ast_parking_ext_valid(exten, chan, ast_channel_context(chan))) {
2147                                 if (!res || !ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2148                                         if (getforward) {
2149                                                 /* Record this as the forwarding extension */
2150                                                 ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
2151                                                 ast_verb(3, "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
2152                                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2153                                                 if (res) {
2154                                                         break;
2155                                                 }
2156                                                 usleep(500000);
2157                                                 res = analog_play_tone(p, idx, -1);
2158                                                 sleep(1);
2159                                                 memset(exten, 0, sizeof(exten));
2160                                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALTONE);
2161                                                 len = 0;
2162                                                 getforward = 0;
2163                                         } else {
2164                                                 res = analog_play_tone(p, idx, -1);
2165                                                 ast_channel_exten_set(chan, exten);
2166                                                 if (!ast_strlen_zero(p->cid_num)) {
2167                                                         if (!p->hidecallerid) {
2168                                                                 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
2169                                                         } else {
2170                                                                 ast_set_callerid(chan, NULL, NULL, p->cid_num);
2171                                                         }
2172                                                 }
2173                                                 if (!ast_strlen_zero(p->cid_name)) {
2174                                                         if (!p->hidecallerid) {
2175                                                                 ast_set_callerid(chan, NULL, p->cid_name, NULL);
2176                                                         }
2177                                                 }
2178                                                 ast_setstate(chan, AST_STATE_RING);
2179                                                 analog_set_echocanceller(p, 1);
2180                                                 res = ast_pbx_run(chan);
2181                                                 if (res) {
2182                                                         ast_log(LOG_WARNING, "PBX exited non-zero\n");
2183                                                         res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2184                                                 }
2185                                                 goto quit;
2186                                         }
2187                                 } else {
2188                                         /* It's a match, but they just typed a digit, and there is an ambiguous match,
2189                                            so just set the timeout to analog_matchdigittimeout and wait some more */
2190                                         timeout = analog_matchdigittimeout;
2191                                 }
2192                         } else if (res == 0) {
2193                                 ast_debug(1, "not enough digits (and no ambiguous match)...\n");
2194                                 res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2195                                 analog_wait_event(p);
2196                                 ast_hangup(chan);
2197                                 goto quit;
2198                         } else if (p->callwaiting && !strcmp(exten, "*70")) {
2199                                 ast_verb(3, "Disabling call waiting on %s\n", ast_channel_name(chan));
2200                                 /* Disable call waiting if enabled */
2201                                 analog_set_callwaiting(p, 0);
2202                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2203                                 if (res) {
2204                                         ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
2205                                                 ast_channel_name(chan), strerror(errno));
2206                                 }
2207                                 len = 0;
2208                                 memset(exten, 0, sizeof(exten));
2209                                 timeout = analog_firstdigittimeout;
2210
2211                         } else if (!strcmp(exten,ast_pickup_ext())) {
2212                                 /* Scan all channels and see if there are any
2213                                  * ringing channels that have call groups
2214                                  * that equal this channels pickup group
2215                                  */
2216                                 if (idx == ANALOG_SUB_REAL) {
2217                                         /* Switch us from Third call to Call Wait */
2218                                         if (p->subs[ANALOG_SUB_THREEWAY].owner) {
2219                                                 /* If you make a threeway call and the *8# a call, it should actually
2220                                                    look like a callwait */
2221                                                 analog_alloc_sub(p, ANALOG_SUB_CALLWAIT);
2222                                                 analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
2223                                                 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
2224                                         }
2225                                         analog_set_echocanceller(p, 1);
2226                                         if (ast_pickup_call(chan)) {
2227                                                 ast_debug(1, "No call pickup possible...\n");
2228                                                 res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2229                                                 analog_wait_event(p);
2230                                         }
2231                                         ast_hangup(chan);
2232                                         goto quit;
2233                                 } else {
2234                                         ast_log(LOG_WARNING, "Huh?  Got *8# on call not on real\n");
2235                                         ast_hangup(chan);
2236                                         goto quit;
2237                                 }
2238
2239                         } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
2240                                 ast_verb(3, "Disabling Caller*ID on %s\n", ast_channel_name(chan));
2241                                 /* Disable Caller*ID if enabled */
2242                                 p->hidecallerid = 1;
2243                                 ast_party_number_free(&ast_channel_caller(chan)->id.number);
2244                                 ast_party_number_init(&ast_channel_caller(chan)->id.number);
2245                                 ast_party_name_free(&ast_channel_caller(chan)->id.name);
2246                                 ast_party_name_init(&ast_channel_caller(chan)->id.name);
2247                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2248                                 if (res) {
2249                                         ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
2250                                                 ast_channel_name(chan), strerror(errno));
2251                                 }
2252                                 len = 0;
2253                                 memset(exten, 0, sizeof(exten));
2254                                 timeout = analog_firstdigittimeout;
2255                         } else if (p->callreturn && !strcmp(exten, "*69")) {
2256                                 res = 0;
2257                                 if (!ast_strlen_zero(p->lastcid_num)) {
2258                                         res = ast_say_digit_str(chan, p->lastcid_num, "", ast_channel_language(chan));
2259                                 }
2260                                 if (!res) {
2261                                         res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2262                                 }
2263                                 break;
2264                         } else if (!strcmp(exten, "*78")) {
2265                                 /* Do not disturb enabled */
2266                                 analog_dnd(p, 1);
2267                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2268                                 getforward = 0;
2269                                 memset(exten, 0, sizeof(exten));
2270                                 len = 0;
2271                         } else if (!strcmp(exten, "*79")) {
2272                                 /* Do not disturb disabled */
2273                                 analog_dnd(p, 0);
2274                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2275                                 getforward = 0;
2276                                 memset(exten, 0, sizeof(exten));
2277                                 len = 0;
2278                         } else if (p->cancallforward && !strcmp(exten, "*72")) {
2279                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2280                                 getforward = 1;
2281                                 memset(exten, 0, sizeof(exten));
2282                                 len = 0;
2283                         } else if (p->cancallforward && !strcmp(exten, "*73")) {
2284                                 ast_verb(3, "Cancelling call forwarding on channel %d\n", p->channel);
2285                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2286                                 memset(p->call_forward, 0, sizeof(p->call_forward));
2287                                 getforward = 0;
2288                                 memset(exten, 0, sizeof(exten));
2289                                 len = 0;
2290                         } else if ((p->transfer || p->canpark) && ast_parking_ext_valid(exten, chan, ast_channel_context(chan)) &&
2291                                                 p->subs[ANALOG_SUB_THREEWAY].owner &&
2292                                                 ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
2293                                 /* This is a three way call, the main call being a real channel,
2294                                         and we're parking the first call. */
2295                                 ast_masq_park_call_exten(
2296                                         ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner), chan, exten,
2297                                         ast_channel_context(chan), 0, NULL);
2298                                 ast_verb(3, "Parking call to '%s'\n", ast_channel_name(chan));
2299                                 break;
2300                         } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
2301                                 ast_verb(3, "Blacklisting number %s\n", p->lastcid_num);
2302                                 res = ast_db_put("blacklist", p->lastcid_num, "1");
2303                                 if (!res) {
2304                                         res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2305                                         memset(exten, 0, sizeof(exten));
2306                                         len = 0;
2307                                 }
2308                         } else if (p->hidecallerid && !strcmp(exten, "*82")) {
2309                                 ast_verb(3, "Enabling Caller*ID on %s\n", ast_channel_name(chan));
2310                                 /* Enable Caller*ID if enabled */
2311                                 p->hidecallerid = 0;
2312                                 ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
2313                                 res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
2314                                 if (res) {
2315                                         ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
2316                                                 ast_channel_name(chan), strerror(errno));
2317                                 }
2318                                 len = 0;
2319                                 memset(exten, 0, sizeof(exten));
2320                                 timeout = analog_firstdigittimeout;
2321                         } else if (!strcmp(exten, "*0")) {
2322                                 struct ast_channel *nbridge = p->subs[ANALOG_SUB_THREEWAY].owner;
2323                                 struct analog_pvt *pbridge = NULL;
2324                                 /* set up the private struct of the bridged one, if any */
2325                                 if (nbridge && ast_bridged_channel(nbridge)) {
2326                                         pbridge = analog_get_bridged_channel(p, nbridge);
2327                                 }
2328                                 if (nbridge && pbridge &&
2329                                     (ast_channel_tech(nbridge) == p->chan_tech) &&
2330                                     (ast_channel_tech(ast_bridged_channel(nbridge)) == p->chan_tech) &&
2331                                     ISTRUNK(pbridge)) {
2332                                         /* Clear out the dial buffer */
2333                                         p->dop.dialstr[0] = '\0';
2334                                         /* flash hookswitch */
2335                                         if ((analog_flash(p) == -1) && (errno != EINPROGRESS)) {
2336                                                 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
2337                                                         ast_channel_name(nbridge), strerror(errno));
2338                                         }
2339                                         analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
2340                                         analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
2341                                         analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
2342                                         if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner)) {
2343                                                 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
2344                                         }
2345                                         ast_hangup(chan);
2346                                         goto quit;
2347                                 } else {
2348                                         analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2349                                         analog_wait_event(p);
2350                                         analog_play_tone(p, idx, -1);
2351                                         analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
2352                                         analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
2353                                         analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
2354                                         ast_hangup(chan);
2355                                         goto quit;
2356                                 }
2357                         } else if (!ast_canmatch_extension(chan, ast_channel_context(chan), exten, 1,
2358                                 ast_channel_caller(chan)->id.number.valid ? ast_channel_caller(chan)->id.number.str : NULL)
2359                                 && !analog_canmatch_featurecode(exten)) {
2360                                 ast_debug(1, "Can't match %s from '%s' in context %s\n", exten,
2361                                         ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str
2362                                                 ? ast_channel_caller(chan)->id.number.str : "<Unknown Caller>",
2363                                         ast_channel_context(chan));
2364                                 break;
2365                         }
2366                         if (!timeout) {
2367                                 timeout = analog_gendigittimeout;
2368                         }
2369                         if (len && !ast_ignore_pattern(ast_channel_context(chan), exten)) {
2370                                 analog_play_tone(p, idx, -1);
2371                         }
2372                 }
2373                 break;
2374         case ANALOG_SIG_FXSLS:
2375         case ANALOG_SIG_FXSGS:
2376         case ANALOG_SIG_FXSKS:
2377                 /* check for SMDI messages */
2378                 if (p->use_smdi && p->smdi_iface) {
2379                         smdi_msg = ast_smdi_md_message_wait(p->smdi_iface, ANALOG_SMDI_MD_WAIT_TIMEOUT);
2380                         if (smdi_msg != NULL) {
2381                                 ast_channel_exten_set(chan, smdi_msg->fwd_st);
2382
2383                                 if (smdi_msg->type == 'B')
2384                                         pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "b");
2385                                 else if (smdi_msg->type == 'N')
2386                                         pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "u");
2387
2388                                 ast_debug(1, "Received SMDI message on %s\n", ast_channel_name(chan));
2389                         } else {
2390                                 ast_log(LOG_WARNING, "SMDI enabled but no SMDI message present\n");
2391                         }
2392                 }
2393
2394                 if (p->use_callerid && (p->cid_signalling == CID_SIG_SMDI && smdi_msg)) {
2395                         number = smdi_msg->calling_st;
2396
2397                 /* If we want caller id, we're in a prering state due to a polarity reversal
2398                  * and we're set to use a polarity reversal to trigger the start of caller id,
2399                  * grab the caller id and wait for ringing to start... */
2400                 } else if (p->use_callerid && (ast_channel_state(chan) == AST_STATE_PRERING
2401                         && (p->cid_start == ANALOG_CID_START_POLARITY
2402                                 || p->cid_start == ANALOG_CID_START_POLARITY_IN
2403                                 || p->cid_start == ANALOG_CID_START_DTMF_NOALERT))) {
2404                         /* If set to use DTMF CID signalling, listen for DTMF */
2405                         if (p->cid_signalling == CID_SIG_DTMF) {
2406                                 int k = 0;
2407                                 int oldlinearity; 
2408                                 cs = NULL;
2409                                 ast_debug(1, "Receiving DTMF cid on channel %s\n", ast_channel_name(chan));
2410
2411                                 oldlinearity = analog_set_linear_mode(p, idx, 0);
2412
2413                                 /*
2414                                  * We are the only party interested in the Rx stream since
2415                                  * we have not answered yet.  We don't need or even want DTMF
2416                                  * emulation.  The DTMF digits can come so fast that emulation
2417                                  * can drop some of them.
2418                                  */
2419                                 ast_set_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
2420                                 res = 4000;/* This is a typical OFF time between rings. */
2421                                 for (;;) {
2422                                         struct ast_frame *f;
2423                                         res = ast_waitfor(chan, res);
2424                                         if (res <= 0) {
2425                                                 /*
2426                                                  * We do not need to restore the analog_set_linear_mode()
2427                                                  * or AST_FLAG_END_DTMF_ONLY flag settings since we
2428                                                  * are hanging up the channel.
2429                                                  */
2430                                                 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
2431                                                         "Exiting simple switch\n");
2432                                                 ast_hangup(chan);
2433                                                 goto quit;
2434                                         }
2435                                         f = ast_read(chan);
2436                                         if (!f) {
2437                                                 break;
2438                                         }
2439                                         if (f->frametype == AST_FRAME_DTMF) {
2440                                                 if (k < ARRAY_LEN(dtmfbuf) - 1) {
2441                                                         dtmfbuf[k++] = f->subclass.integer;
2442                                                 }
2443                                                 ast_debug(1, "CID got digit '%c'\n", f->subclass.integer);
2444                                                 res = 4000;/* This is a typical OFF time between rings. */
2445                                         }
2446                                         ast_frfree(f);
2447                                         if (ast_channel_state(chan) == AST_STATE_RING ||
2448                                                 ast_channel_state(chan) == AST_STATE_RINGING) {
2449                                                 break; /* Got ring */
2450                                         }
2451                                 }
2452                                 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
2453                                 dtmfbuf[k] = '\0';
2454
2455                                 analog_set_linear_mode(p, idx, oldlinearity);
2456
2457                                 /* Got cid and ring. */
2458                                 ast_debug(1, "CID got string '%s'\n", dtmfbuf);
2459                                 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
2460                                 ast_debug(1, "CID is '%s', flags %d\n", dtmfcid, flags);
2461                                 /* If first byte is NULL, we have no cid */
2462                                 if (!ast_strlen_zero(dtmfcid)) {
2463                                         number = dtmfcid;
2464                                 } else {
2465                                         number = NULL;
2466                                 }
2467
2468                         /* If set to use V23 Signalling, launch our FSK gubbins and listen for it */
2469                         } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) {
2470                                 int timeout = 10000;  /* Ten seconds */
2471                                 struct timeval start = ast_tvnow();
2472                                 enum analog_event ev;
2473
2474                                 namebuf[0] = 0;
2475                                 numbuf[0] = 0;
2476
2477                                 if (!analog_start_cid_detect(p, p->cid_signalling)) {
2478                                         while (1) {
2479                                                 res = analog_get_callerid(p, namebuf, numbuf, &ev, timeout - ast_tvdiff_ms(ast_tvnow(), start));
2480
2481                                                 if (res == 0) {
2482                                                         break;
2483                                                 }
2484
2485                                                 if (res == 1) {
2486                                                         if (ev == ANALOG_EVENT_NOALARM) {
2487                                                                 analog_set_alarm(p, 0);
2488                                                         }
2489                                                         if (p->cid_signalling == CID_SIG_V23_JP) {
2490                                                                 if (ev == ANALOG_EVENT_RINGBEGIN) {
2491                                                                         analog_off_hook(p);
2492                                                                         usleep(1);
2493                                                                 }
2494                                                         } else {
2495                                                                 ev = ANALOG_EVENT_NONE;
2496                                                                 break;
2497                                                         }
2498                                                 }
2499
2500                                                 if (ast_tvdiff_ms(ast_tvnow(), start) > timeout)
2501                                                         break;
2502
2503                                         }
2504                                         name = namebuf;
2505                                         number = numbuf;
2506
2507                                         analog_stop_cid_detect(p);
2508
2509                                         if (p->cid_signalling == CID_SIG_V23_JP) {
2510                                                 res = analog_on_hook(p);
2511                                                 usleep(1);
2512                                         }
2513
2514                                         /* Finished with Caller*ID, now wait for a ring to make sure there really is a call coming */
2515                                         res = 4000;/* This is a typical OFF time between rings. */
2516                                         for (;;) {
2517                                                 struct ast_frame *f;
2518                                                 res = ast_waitfor(chan, res);
2519                                                 if (res <= 0) {
2520                                                         ast_log(LOG_WARNING, "CID timed out waiting for ring. "
2521                                                                 "Exiting simple switch\n");
2522                                                         ast_hangup(chan);
2523                                                         goto quit;
2524                                                 }
2525                                                 if (!(f = ast_read(chan))) {
2526                                                         ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n");
2527                                                         ast_hangup(chan);
2528                                                         goto quit;
2529                                                 }
2530                                                 ast_frfree(f);
2531                                                 if (ast_channel_state(chan) == AST_STATE_RING ||
2532                                                         ast_channel_state(chan) == AST_STATE_RINGING)
2533                                                         break; /* Got ring */
2534                                         }
2535
2536                                         if (analog_distinctive_ring(chan, p, idx, NULL)) {
2537                                                 goto quit;
2538                                         }
2539
2540                                         if (res < 0) {
2541                                                 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", ast_channel_name(chan));
2542                                         }
2543                                 } else {
2544                                         ast_log(LOG_WARNING, "Unable to get caller ID space\n");
2545                                 }
2546                         } else {
2547                                 ast_log(LOG_WARNING, "Channel %s in prering "
2548                                         "state, but I have nothing to do. "
2549                                         "Terminating simple switch, should be "
2550                                         "restarted by the actual ring.\n",
2551                                         ast_channel_name(chan));
2552                                 ast_hangup(chan);
2553                                 goto quit;
2554                         }
2555                 } else if (p->use_callerid && p->cid_start == ANALOG_CID_START_RING) {
2556                         int timeout = 10000;  /* Ten seconds */
2557                         struct timeval start = ast_tvnow();
2558                         enum analog_event ev;
2559                         int curRingData[RING_PATTERNS] = { 0 };
2560                         int receivedRingT = 0;
2561
2562                         namebuf[0] = 0;
2563                         numbuf[0] = 0;
2564
2565                         if (!analog_start_cid_detect(p, p->cid_signalling)) {
2566                                 while (1) {
2567                                         res = analog_get_callerid(p, namebuf, numbuf, &ev, timeout - ast_tvdiff_ms(ast_tvnow(), start));
2568
2569                                         if (res == 0) {
2570                                                 break;
2571                                         }
2572
2573                                         if (res == 1 || res == 2) {
2574                                                 if (ev == ANALOG_EVENT_NOALARM) {
2575                                                         analog_set_alarm(p, 0);
2576                                                 } else if (ev == ANALOG_EVENT_POLARITY && p->hanguponpolarityswitch && p->polarity == POLARITY_REV) {
2577                                                         ast_debug(1, "Hanging up due to polarity reversal on channel %d while detecting callerid\n", p->channel);
2578                                                         p->polarity = POLARITY_IDLE;
2579                                                         ast_hangup(chan);
2580                                                         goto quit;
2581                                                 } else if (ev != ANALOG_EVENT_NONE && ev != ANALOG_EVENT_RINGBEGIN && ev != ANALOG_EVENT_RINGOFFHOOK) {
2582                                                         break;
2583                                                 }
2584                                                 if (res != 2) {
2585                                                         /* Let us detect callerid when the telco uses distinctive ring */
2586                                                         curRingData[receivedRingT] = p->ringt;
2587
2588                                                         if (p->ringt < p->ringt_base/2) {
2589                                                                 break;
2590                                                         }
2591                                                         /* Increment the ringT counter so we can match it against
2592                                                            values in chan_dahdi.conf for distinctive ring */
2593                                                         if (++receivedRingT == RING_PATTERNS) {
2594                                                                 break;
2595                                                         }
2596                                                 }
2597                                         }
2598
2599                                         if (ast_tvdiff_ms(ast_tvnow(), start) > timeout) {
2600                                                 break;
2601                                         }
2602
2603                                 }
2604                                 name = namebuf;
2605                                 number = numbuf;
2606
2607                                 analog_stop_cid_detect(p);
2608
2609                                 if (analog_distinctive_ring(chan, p, idx, curRingData)) {
2610                                         goto quit;
2611                                 }
2612
2613                                 if (res < 0) {
2614                                         ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", ast_channel_name(chan));
2615                                 }
2616                         } else {
2617                                 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
2618                         }
2619                 } else {
2620                         cs = NULL;
2621                 }
2622
2623                 if (number) {
2624                         ast_shrink_phone_number(number);
2625                 }
2626                 ast_set_callerid(chan, number, name, number);
2627
2628                 if (cs) {
2629                         callerid_free(cs);
2630                 }
2631
2632                 analog_handle_notify_message(chan, p, flags, -1);
2633
2634                 ast_setstate(chan, AST_STATE_RING);
2635                 ast_channel_rings_set(chan, 1);
2636                 analog_set_ringtimeout(p, p->ringt_base);
2637                 res = ast_pbx_run(chan);
2638                 if (res) {
2639                         ast_hangup(chan);
2640                         ast_log(LOG_WARNING, "PBX exited non-zero\n");
2641                 }
2642                 goto quit;
2643         default:
2644                 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);
2645                 break;
2646         }
2647         res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
2648         if (res < 0) {
2649                 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
2650         }
2651         ast_hangup(chan);
2652 quit:
2653         if (smdi_msg) {
2654                 ASTOBJ_UNREF(smdi_msg, ast_smdi_md_message_destroy);
2655         }
2656         analog_decrease_ss_count(p);
2657         return NULL;
2658 }
2659
2660 int analog_ss_thread_start(struct analog_pvt *p, struct ast_channel *chan)
2661 {
2662         pthread_t threadid;
2663
2664         return ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, p);
2665 }
2666
2667 static struct ast_frame *__analog_handle_event(struct analog_pvt *p, struct ast_channel *ast)
2668 {
2669         int res, x;
2670         int mysig;
2671         enum analog_sub idx;
2672         char *c;
2673         pthread_t threadid;
2674         struct ast_channel *chan;
2675         struct ast_frame *f;
2676         struct ast_control_pvt_cause_code *cause_code = NULL;
2677         int data_size = sizeof(*cause_code);
2678         char *subclass = NULL;
2679
2680         ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
2681
2682         idx = analog_get_index(ast, p, 0);
2683         if (idx < 0) {
2684                 return &ast_null_frame;
2685         }
2686         if (idx != ANALOG_SUB_REAL) {
2687                 ast_log(LOG_ERROR, "We got an event on a non real sub.  Fix it!\n");
2688         }
2689
2690         mysig = p->sig;
2691         if (p->outsigmod > -1) {
2692                 mysig = p->outsigmod;
2693         }
2694
2695         p->subs[idx].f.frametype = AST_FRAME_NULL;
2696         p->subs[idx].f.subclass.integer = 0;
2697         p->subs[idx].f.datalen = 0;
2698         p->subs[idx].f.samples = 0;
2699         p->subs[idx].f.mallocd = 0;
2700         p->subs[idx].f.offset = 0;
2701         p->subs[idx].f.src = "dahdi_handle_event";
2702         p->subs[idx].f.data.ptr = NULL;
2703         f = &p->subs[idx].f;
2704
2705         res = analog_get_event(p);
2706
2707         ast_debug(1, "Got event %s(%d) on channel %d (index %d)\n", analog_event2str(res), res, p->channel, idx);
2708
2709         if (res & (ANALOG_EVENT_PULSEDIGIT | ANALOG_EVENT_DTMFUP)) {
2710                 analog_set_pulsedial(p, (res & ANALOG_EVENT_PULSEDIGIT) ? 1 : 0);
2711                 ast_debug(1, "Detected %sdigit '%c'\n", (res & ANALOG_EVENT_PULSEDIGIT) ? "pulse ": "", res & 0xff);
2712                 analog_confmute(p, 0);
2713                 p->subs[idx].f.frametype = AST_FRAME_DTMF_END;
2714                 p->subs[idx].f.subclass.integer = res & 0xff;
2715                 analog_handle_dtmf(p, ast, idx, &f);
2716                 return f;
2717         }
2718
2719         if (res & ANALOG_EVENT_DTMFDOWN) {
2720                 ast_debug(1, "DTMF Down '%c'\n", res & 0xff);
2721                 /* Mute conference */
2722                 analog_confmute(p, 1);
2723                 p->subs[idx].f.frametype = AST_FRAME_DTMF_BEGIN;
2724                 p->subs[idx].f.subclass.integer = res & 0xff;
2725                 analog_handle_dtmf(p, ast, idx, &f);
2726                 return f;
2727         }
2728
2729         switch (res) {
2730         case ANALOG_EVENT_ALARM:
2731         case ANALOG_EVENT_POLARITY:
2732         case ANALOG_EVENT_ONHOOK:
2733                 /* add length of "ANALOG " */
2734                 data_size += 7;
2735                 subclass = analog_event2str(res);
2736                 data_size += strlen(subclass);
2737                 cause_code = alloca(data_size);
2738                 ast_copy_string(cause_code->chan_name, ast_channel_name(ast), AST_CHANNEL_NAME);
2739                 snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "ANALOG %s", subclass);
2740                 break;
2741         default:
2742                 break;
2743         }
2744
2745         switch (res) {
2746         case ANALOG_EVENT_EC_DISABLED:
2747                 ast_verb(3, "Channel %d echo canceler disabled due to CED detection\n", p->channel);
2748                 analog_set_echocanceller(p, 0);
2749                 break;
2750 #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
2751         case ANALOG_EVENT_TX_CED_DETECTED:
2752                 ast_verb(3, "Channel %d detected a CED tone towards the network.\n", p->channel);
2753                 break;
2754         case ANALOG_EVENT_RX_CED_DETECTED:
2755                 ast_verb(3, "Channel %d detected a CED tone from the network.\n", p->channel);
2756                 break;
2757         case ANALOG_EVENT_EC_NLP_DISABLED:
2758                 ast_verb(3, "Channel %d echo canceler disabled its NLP.\n", p->channel);
2759                 break;
2760         case ANALOG_EVENT_EC_NLP_ENABLED:
2761                 ast_verb(3, "Channel %d echo canceler enabled its NLP.\n", p->channel);
2762                 break;
2763 #endif
2764         case ANALOG_EVENT_PULSE_START:
2765                 /* Stop tone if there's a pulse start and the PBX isn't started */
2766                 if (!ast_channel_pbx(ast))
2767                         analog_play_tone(p, ANALOG_SUB_REAL, -1);
2768                 break;
2769         case ANALOG_EVENT_DIALCOMPLETE:
2770                 if (p->inalarm) {
2771                         break;
2772                 }
2773                 x = analog_is_dialing(p, idx);
2774                 if (!x) { /* if not still dialing in driver */
2775                         analog_set_echocanceller(p, 1);
2776                         if (p->echobreak) {
2777                                 analog_train_echocanceller(p);
2778                                 ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
2779                                 p->dop.op = ANALOG_DIAL_OP_REPLACE;
2780                                 if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
2781                                         int dial_err = errno;
2782                                         ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(dial_err));
2783                                 }
2784                                 p->echobreak = 0;
2785                         } else {
2786                                 analog_set_dialing(p, 0);
2787                                 if ((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) {
2788                                         /* if thru with dialing after offhook */
2789                                         if (ast_channel_state(ast) == AST_STATE_DIALING_OFFHOOK) {
2790                                                 ast_setstate(ast, AST_STATE_UP);
2791                                                 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
2792                                                 p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
2793                                                 break;
2794                                         } else { /* if to state wait for offhook to dial rest */
2795                                                 /* we now wait for off hook */
2796                                                 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
2797                                         }
2798                                 }
2799                                 if (ast_channel_state(ast) == AST_STATE_DIALING) {
2800                                         if (analog_have_progressdetect(p)) {
2801                                                 ast_debug(1, "Done dialing, but waiting for progress detection before doing more...\n");
2802                                         } else if (analog_check_confirmanswer(p) || (!p->dialednone
2803                                                 && ((mysig == ANALOG_SIG_EM) || (mysig == ANALOG_SIG_EM_E1)
2804                                                         || (mysig == ANALOG_SIG_EMWINK) || (mysig == ANALOG_SIG_FEATD)
2805                                                         || (mysig == ANALOG_SIG_FEATDMF_TA) || (mysig == ANALOG_SIG_FEATDMF)
2806                                                         || (mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA)
2807                                               &