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