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