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