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