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