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