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