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