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