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