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