Miscellaneous minor fixes to sig_analog.
[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         if (!chan) {
1341                 /* What happened to the channel? */
1342                 goto quit;
1343         }
1344         /* in the bizarre case where the channel has become a zombie before we
1345            even get started here, abort safely
1346         */
1347         if (!chan->tech_pvt) {
1348                 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
1349                 ast_hangup(chan);
1350                 goto quit;
1351         }
1352
1353         ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
1354         index = analog_get_index(chan, p, 1);
1355         if (index < 0) {
1356                 ast_log(LOG_WARNING, "Huh?\n");
1357                 ast_hangup(chan);
1358                 goto quit;
1359         }
1360         analog_dsp_reset_and_flush_digits(p);
1361         switch (p->sig) {
1362         case ANALOG_SIG_FEATD:
1363         case ANALOG_SIG_FEATDMF:
1364         case ANALOG_SIG_FEATDMF_TA:
1365         case ANALOG_SIG_E911:
1366         case ANALOG_SIG_FGC_CAMAMF:
1367         case ANALOG_SIG_FEATB:
1368         case ANALOG_SIG_EMWINK:
1369         case ANALOG_SIG_SF_FEATD:
1370         case ANALOG_SIG_SF_FEATDMF:
1371         case ANALOG_SIG_SF_FEATB:
1372         case ANALOG_SIG_SFWINK:
1373                 if (analog_wink(p, index))
1374                         goto quit;
1375                 /* Fall through */
1376         case ANALOG_SIG_EM:
1377         case ANALOG_SIG_EM_E1:
1378         case ANALOG_SIG_SF:
1379         case ANALOG_SIG_FGC_CAMA:
1380                 res = analog_play_tone(p, index, -1);
1381
1382                 analog_dsp_reset_and_flush_digits(p);
1383
1384                 if (ANALOG_NEED_MFDETECT(p)) {
1385                         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
1386                 } else
1387                         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1388
1389                 memset(dtmfbuf, 0, sizeof(dtmfbuf));
1390                 /* Wait for the first digit only if immediate=no */
1391                 if (!p->immediate)
1392                         /* Wait for the first digit (up to 5 seconds). */
1393                         res = ast_waitfordigit(chan, 5000);
1394                 else
1395                         res = 0;
1396                 if (res > 0) {
1397                         /* save first char */
1398                         dtmfbuf[0] = res;
1399                         switch (p->sig) {
1400                         case ANALOG_SIG_FEATD:
1401                         case ANALOG_SIG_SF_FEATD:
1402                                 res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
1403                                 if (res > 0)
1404                                         res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
1405                                 if (res < 1)
1406                                         analog_dsp_reset_and_flush_digits(p);
1407                                 break;
1408                         case ANALOG_SIG_FEATDMF_TA:
1409                                 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1410                                 if (res < 1)
1411                                         analog_dsp_reset_and_flush_digits(p);
1412                                 if (analog_wink(p, index)) goto quit;
1413                                 dtmfbuf[0] = 0;
1414                                 /* Wait for the first digit (up to 5 seconds). */
1415                                 res = ast_waitfordigit(chan, 5000);
1416                                 if (res <= 0) break;
1417                                 dtmfbuf[0] = res;
1418                                 /* fall through intentionally */
1419                         case ANALOG_SIG_FEATDMF:
1420                         case ANALOG_SIG_E911:
1421                         case ANALOG_SIG_FGC_CAMAMF:
1422                         case ANALOG_SIG_SF_FEATDMF:
1423                                 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1424                                 /* if international caca, do it again to get real ANO */
1425                                 if ((p->sig == ANALOG_SIG_FEATDMF) && (dtmfbuf[1] != '0') && (strlen(dtmfbuf) != 14))
1426                                 {
1427                                         if (analog_wink(p, index)) goto quit;
1428                                         dtmfbuf[0] = 0;
1429                                         /* Wait for the first digit (up to 5 seconds). */
1430                                         res = ast_waitfordigit(chan, 5000);
1431                                         if (res <= 0) break;
1432                                         dtmfbuf[0] = res;
1433                                         res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1434                                 }
1435                                 if (res > 0) {
1436                                         /* if E911, take off hook */
1437                                         if (p->sig == ANALOG_SIG_E911)
1438                                                 analog_off_hook(p);
1439                                         res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
1440                                 }
1441                                 if (res < 1)
1442                                         analog_dsp_reset_and_flush_digits(p);
1443                                 break;
1444                         case ANALOG_SIG_FEATB:
1445                         case ANALOG_SIG_SF_FEATB:
1446                                 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1447                                 if (res < 1)
1448                                         analog_dsp_reset_and_flush_digits(p);
1449                                 break;
1450                         case ANALOG_SIG_EMWINK:
1451                                 /* if we received a '*', we are actually receiving Feature Group D
1452                                    dial syntax, so use that mode; otherwise, fall through to normal
1453                                    mode
1454                                 */
1455                                 if (res == '*') {
1456                                         res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
1457                                         if (res > 0)
1458                                                 res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
1459                                         if (res < 1)
1460                                                 analog_dsp_reset_and_flush_digits(p);
1461                                         break;
1462                                 }
1463                         default:
1464                                 /* If we got the first digit, get the rest */
1465                                 len = 1;
1466                                 dtmfbuf[len] = '\0';
1467                                 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
1468                                         if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
1469                                                 timeout = analog_matchdigittimeout;
1470                                         } else {
1471                                                 timeout = analog_gendigittimeout;
1472                                         }
1473                                         res = ast_waitfordigit(chan, timeout);
1474                                         if (res < 0) {
1475                                                 ast_debug(1, "waitfordigit returned < 0...\n");
1476                                                 ast_hangup(chan);
1477                                                 goto quit;
1478                                         } else if (res) {
1479                                                 dtmfbuf[len++] = res;
1480                                                 dtmfbuf[len] = '\0';
1481                                         } else {
1482                                                 break;
1483                                         }
1484                                 }
1485                                 break;
1486                         }
1487                 }
1488                 if (res == -1) {
1489                         ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
1490                         ast_hangup(chan);
1491                         goto quit;
1492                 } else if (res < 0) {
1493                         ast_debug(1, "Got hung up before digits finished\n");
1494                         ast_hangup(chan);
1495                         goto quit;
1496                 }
1497
1498                 if (p->sig == ANALOG_SIG_FGC_CAMA) {
1499                         char anibuf[100];
1500
1501                         if (ast_safe_sleep(chan,1000) == -1) {
1502                                 ast_hangup(chan);
1503                                 goto quit;
1504                         }
1505                         analog_off_hook(p);
1506                         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
1507                         res = analog_my_getsigstr(chan, anibuf, "#", 10000);
1508                         if ((res > 0) && (strlen(anibuf) > 2)) {
1509                                 if (anibuf[strlen(anibuf) - 1] == '#')
1510                                         anibuf[strlen(anibuf) - 1] = 0;
1511                                 ast_set_callerid(chan, anibuf + 2, NULL, anibuf + 2);
1512                         }
1513                         analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1514                 }
1515
1516                 ast_copy_string(exten, dtmfbuf, sizeof(exten));
1517                 if (ast_strlen_zero(exten))
1518                         ast_copy_string(exten, "s", sizeof(exten));
1519                 if (p->sig == ANALOG_SIG_FEATD || p->sig == ANALOG_SIG_EMWINK) {
1520                         /* Look for Feature Group D on all E&M Wink and Feature Group D trunks */
1521                         if (exten[0] == '*') {
1522                                 char *stringp=NULL;
1523                                 ast_copy_string(exten2, exten, sizeof(exten2));
1524                                 /* Parse out extension and callerid */
1525                                 stringp=exten2 +1;
1526                                 s1 = strsep(&stringp, "*");
1527                                 s2 = strsep(&stringp, "*");
1528                                 if (s2) {
1529                                         if (!ast_strlen_zero(p->cid_num))
1530                                                 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
1531                                         else
1532                                                 ast_set_callerid(chan, s1, NULL, s1);
1533                                         ast_copy_string(exten, s2, sizeof(exten));
1534                                 } else
1535                                         ast_copy_string(exten, s1, sizeof(exten));
1536                         } else if (p->sig == ANALOG_SIG_FEATD)
1537                                 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d.  Assuming E&M Wink instead\n", p->channel);
1538                 }
1539                 if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
1540                         if (exten[0] == '*') {
1541                                 char *stringp=NULL;
1542                                 ast_copy_string(exten2, exten, sizeof(exten2));
1543                                 /* Parse out extension and callerid */
1544                                 stringp=exten2 +1;
1545                                 s1 = strsep(&stringp, "#");
1546                                 s2 = strsep(&stringp, "#");
1547                                 if (s2) {
1548                                         if (!ast_strlen_zero(p->cid_num))
1549                                                 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
1550                                         else
1551                                                 if (*(s1 + 2))
1552                                                         ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
1553                                         ast_copy_string(exten, s2 + 1, sizeof(exten));
1554                                 } else
1555                                         ast_copy_string(exten, s1 + 2, sizeof(exten));
1556                         } else
1557                                 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d.  Assuming E&M Wink instead\n", p->channel);
1558                 }
1559                 if ((p->sig == ANALOG_SIG_E911) || (p->sig == ANALOG_SIG_FGC_CAMAMF)) {
1560                         if (exten[0] == '*') {
1561                                 char *stringp=NULL;
1562                                 ast_copy_string(exten2, exten, sizeof(exten2));
1563                                 /* Parse out extension and callerid */
1564                                 stringp=exten2 +1;
1565                                 s1 = strsep(&stringp, "#");
1566                                 s2 = strsep(&stringp, "#");
1567                                 if (s2 && (*(s2 + 1) == '0')) {
1568                                         if (*(s2 + 2))
1569                                                 ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
1570                                 }
1571                                 if (s1) ast_copy_string(exten, s1, sizeof(exten));
1572                                 else ast_copy_string(exten, "911", sizeof(exten));
1573                         } else
1574                                 ast_log(LOG_WARNING, "Got a non-E911/FGC CAMA input on channel %d.  Assuming E&M Wink instead\n", p->channel);
1575                 }
1576                 if (p->sig == ANALOG_SIG_FEATB) {
1577                         if (exten[0] == '*') {
1578                                 char *stringp=NULL;
1579                                 ast_copy_string(exten2, exten, sizeof(exten2));
1580                                 /* Parse out extension and callerid */
1581                                 stringp=exten2 +1;
1582                                 s1 = strsep(&stringp, "#");
1583                                 ast_copy_string(exten, exten2 + 1, sizeof(exten));
1584                         } else
1585                                 ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d.  Assuming E&M Wink instead\n", p->channel);
1586                 }
1587                 if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
1588                         analog_wink(p, index);
1589                         /* some switches require a minimum guard time between
1590                         the last FGD wink and something that answers
1591                         immediately. This ensures it */
1592                         if (ast_safe_sleep(chan,100)) goto quit;
1593                 }
1594                 analog_set_echocanceller(p, 1);
1595
1596                 analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1597
1598                 if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) {
1599                         ast_copy_string(chan->exten, exten, sizeof(chan->exten));
1600                         analog_dsp_reset_and_flush_digits(p);
1601                         res = ast_pbx_run(chan);
1602                         if (res) {
1603                                 ast_log(LOG_WARNING, "PBX exited non-zero\n");
1604                                 res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
1605                         }
1606                         goto quit;
1607                 } else {
1608                         ast_verb(3, "Unknown extension '%s' in context '%s' requested\n", exten, chan->context);
1609                         sleep(2);
1610                         res = analog_play_tone(p, index, ANALOG_TONE_INFO);
1611                         if (res < 0)
1612                                 ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
1613                         else
1614                                 sleep(1);
1615                         res = ast_streamfile(chan, "ss-noservice", chan->language);
1616                         if (res >= 0)
1617                                 ast_waitstream(chan, "");
1618                         res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
1619                         ast_hangup(chan);
1620                         goto quit;
1621                 }
1622                 break;
1623         case ANALOG_SIG_FXOLS:
1624         case ANALOG_SIG_FXOGS:
1625         case ANALOG_SIG_FXOKS:
1626                 /* Read the first digit */
1627                 timeout = analog_firstdigittimeout;
1628                 /* If starting a threeway call, never timeout on the first digit so someone
1629                    can use flash-hook as a "hold" feature */
1630                 if (p->subs[ANALOG_SUB_THREEWAY].owner)
1631                         timeout = 999999;
1632                 while (len < AST_MAX_EXTENSION-1) {
1633                         /* Read digit unless it's supposed to be immediate, in which case the
1634                            only answer is 's' */
1635                         if (p->immediate)
1636                                 res = 's';
1637                         else
1638                                 res = ast_waitfordigit(chan, timeout);
1639                         timeout = 0;
1640                         if (res < 0) {
1641                                 ast_debug(1, "waitfordigit returned < 0...\n");
1642                                 res = analog_play_tone(p, index, -1);
1643                                 ast_hangup(chan);
1644                                 goto quit;
1645                         } else if (res)  {
1646                                 exten[len++]=res;
1647                                 exten[len] = '\0';
1648                         }
1649                         if (!ast_ignore_pattern(chan->context, exten))
1650                                 analog_play_tone(p, index, -1);
1651                         else
1652                                 analog_play_tone(p, index, ANALOG_TONE_DIALTONE);
1653                         if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) {
1654                                 if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
1655                                         if (getforward) {
1656                                                 /* Record this as the forwarding extension */
1657                                                 ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
1658                                                 ast_verb(3, "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
1659                                                 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1660                                                 if (res)
1661                                                         break;
1662                                                 usleep(500000);
1663                                                 res = analog_play_tone(p, index, -1);
1664                                                 sleep(1);
1665                                                 memset(exten, 0, sizeof(exten));
1666                                                 res = analog_play_tone(p, index, ANALOG_TONE_DIALTONE);
1667                                                 len = 0;
1668                                                 getforward = 0;
1669                                         } else  {
1670                                                 res = analog_play_tone(p, index, -1);
1671                                                 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
1672                                                 if (!ast_strlen_zero(p->cid_num)) {
1673                                                         if (!p->hidecallerid)
1674                                                                 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
1675                                                         else
1676                                                                 ast_set_callerid(chan, NULL, NULL, p->cid_num);
1677                                                 }
1678                                                 if (!ast_strlen_zero(p->cid_name)) {
1679                                                         if (!p->hidecallerid)
1680                                                                 ast_set_callerid(chan, NULL, p->cid_name, NULL);
1681                                                 }
1682                                                 ast_setstate(chan, AST_STATE_RING);
1683                                                 analog_set_echocanceller(p, 1);
1684                                                 res = ast_pbx_run(chan);
1685                                                 if (res) {
1686                                                         ast_log(LOG_WARNING, "PBX exited non-zero\n");
1687                                                         res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
1688                                                 }
1689                                                 goto quit;
1690                                         }
1691                                 } else {
1692                                         /* It's a match, but they just typed a digit, and there is an ambiguous match,
1693                                            so just set the timeout to analog_matchdigittimeout and wait some more */
1694                                         timeout = analog_matchdigittimeout;
1695                                 }
1696                         } else if (res == 0) {
1697                                 ast_debug(1, "not enough digits (and no ambiguous match)...\n");
1698                                 res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
1699                                 analog_wait_event(p);
1700                                 ast_hangup(chan);
1701                                 goto quit;
1702                         } else if (p->callwaiting && !strcmp(exten, "*70")) {
1703                                 ast_verb(3, "Disabling call waiting on %s\n", chan->name);
1704                                 /* Disable call waiting if enabled */
1705                                 p->callwaiting = 0;
1706                                 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1707                                 if (res) {
1708                                         ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
1709                                                 chan->name, strerror(errno));
1710                                 }
1711                                 len = 0;
1712                                 memset(exten, 0, sizeof(exten));
1713                                 timeout = analog_firstdigittimeout;
1714
1715                         } else if (!strcmp(exten,ast_pickup_ext())) {
1716                                 /* Scan all channels and see if there are any
1717                                  * ringing channels that have call groups
1718                                  * that equal this channels pickup group
1719                                  */
1720                                 if (index == ANALOG_SUB_REAL) {
1721                                         /* Switch us from Third call to Call Wait */
1722                                         if (p->subs[ANALOG_SUB_THREEWAY].owner) {
1723                                                 /* If you make a threeway call and the *8# a call, it should actually
1724                                                    look like a callwait */
1725                                                 analog_alloc_sub(p, ANALOG_SUB_CALLWAIT);
1726                                                 analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
1727                                                 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1728                                         }
1729                                         analog_set_echocanceller(p, 1);
1730                                         if (ast_pickup_call(chan)) {
1731                                                 ast_debug(1, "No call pickup possible...\n");
1732                                                 res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
1733                                                 analog_wait_event(p);
1734                                         }
1735                                         ast_hangup(chan);
1736                                         goto quit;
1737                                 } else {
1738                                         ast_log(LOG_WARNING, "Huh?  Got *8# on call not on real\n");
1739                                         ast_hangup(chan);
1740                                         goto quit;
1741                                 }
1742                         } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
1743                                 ast_verb(3, "Disabling Caller*ID on %s\n", chan->name);
1744                                 /* Disable Caller*ID if enabled */
1745                                 p->hidecallerid = 1;
1746                                 if (chan->cid.cid_num)
1747                                         free(chan->cid.cid_num);
1748                                 chan->cid.cid_num = NULL;
1749                                 if (chan->cid.cid_name)
1750                                         free(chan->cid.cid_name);
1751                                 chan->cid.cid_name = NULL;
1752                                 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1753                                 if (res) {
1754                                         ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
1755                                                 chan->name, strerror(errno));
1756                                 }
1757                                 len = 0;
1758                                 memset(exten, 0, sizeof(exten));
1759                                 timeout = analog_firstdigittimeout;
1760                         } else if (p->callreturn && !strcmp(exten, "*69")) {
1761                                 res = 0;
1762                                 if (!ast_strlen_zero(p->lastcid_num)) {
1763                                         res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language);
1764                                 }
1765                                 if (!res)
1766                                         res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1767                                 break;
1768                         } else if (!strcmp(exten, "*78")) {
1769                                 /* Do not disturb */
1770                                 ast_verb(3, "Enabled DND on channel %d\n", p->channel);
1771                                 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
1772                                               "Channel: DAHDI/%d\r\n"
1773                                               "Status: enabled\r\n", p->channel);
1774                                 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1775                                 p->dnd = 1;
1776                                 getforward = 0;
1777                                 memset(exten, 0, sizeof(exten));
1778                                 len = 0;
1779                         } else if (!strcmp(exten, "*79")) {
1780                                 /* Do not disturb */
1781                                 ast_verb(3, "Disabled DND on channel %d\n", p->channel);
1782                                 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
1783                                               "Channel: DAHDI/%d\r\n"
1784                                               "Status: disabled\r\n", p->channel);
1785                                 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1786                                 p->dnd = 0;
1787                                 getforward = 0;
1788                                 memset(exten, 0, sizeof(exten));
1789                                 len = 0;
1790                         } else if (p->cancallforward && !strcmp(exten, "*72")) {
1791                                 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1792                                 getforward = 1;
1793                                 memset(exten, 0, sizeof(exten));
1794                                 len = 0;
1795                         } else if (p->cancallforward && !strcmp(exten, "*73")) {
1796                                 ast_verb(3, "Cancelling call forwarding on channel %d\n", p->channel);
1797                                 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1798                                 memset(p->call_forward, 0, sizeof(p->call_forward));
1799                                 getforward = 0;
1800                                 memset(exten, 0, sizeof(exten));
1801                                 len = 0;
1802                         } else if ((p->transfer || p->canpark) && !strcmp(exten, ast_parking_ext()) &&
1803                                                 p->subs[ANALOG_SUB_THREEWAY].owner &&
1804                                                 ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
1805                                 /* This is a three way call, the main call being a real channel,
1806                                         and we're parking the first call. */
1807                                 ast_masq_park_call(ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner), chan, 0, NULL);
1808                                 ast_verb(3, "Parking call to '%s'\n", chan->name);
1809                                 break;
1810                         } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
1811                                 ast_verb(3, "Blacklisting number %s\n", p->lastcid_num);
1812                                 res = ast_db_put("blacklist", p->lastcid_num, "1");
1813                                 if (!res) {
1814                                         res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1815                                         memset(exten, 0, sizeof(exten));
1816                                         len = 0;
1817                                 }
1818                         } else if (p->hidecallerid && !strcmp(exten, "*82")) {
1819                                 ast_verb(3, "Enabling Caller*ID on %s\n", chan->name);
1820                                 /* Enable Caller*ID if enabled */
1821                                 p->hidecallerid = 0;
1822                                 if (chan->cid.cid_num)
1823                                         free(chan->cid.cid_num);
1824                                 chan->cid.cid_num = NULL;
1825                                 if (chan->cid.cid_name)
1826                                         free(chan->cid.cid_name);
1827                                 chan->cid.cid_name = NULL;
1828                                 ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
1829                                 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1830                                 if (res) {
1831                                         ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
1832                                                 chan->name, strerror(errno));
1833                                 }
1834                                 len = 0;
1835                                 memset(exten, 0, sizeof(exten));
1836                                 timeout = analog_firstdigittimeout;
1837                         } else if (!strcmp(exten, "*0")) {
1838                                 struct ast_channel *nbridge = p->subs[ANALOG_SUB_THREEWAY].owner;
1839                                 struct analog_pvt *pbridge = NULL;
1840                                   /* set up the private struct of the bridged one, if any */
1841                                 if (nbridge && ast_bridged_channel(nbridge))
1842                                         pbridge = analog_get_bridged_channel(p, nbridge);
1843                                 if (nbridge && pbridge &&
1844                                     (nbridge->tech == p->chan_tech) &&
1845                                     (ast_bridged_channel(nbridge)->tech == p->chan_tech) &&
1846                                     ISTRUNK(pbridge)) {
1847                                         /* Clear out the dial buffer */
1848                                         p->dop.dialstr[0] = '\0';
1849                                         /* flash hookswitch */
1850                                         if ((analog_flash(p) == -1) && (errno != EINPROGRESS)) {
1851                                                 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
1852                                                         nbridge->name, strerror(errno));
1853                                         }
1854                                         analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
1855                                         analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1856                                         p->owner = p->subs[ANALOG_SUB_REAL].owner;
1857                                         if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner))
1858                                                 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
1859                                         ast_hangup(chan);
1860                                         goto quit;
1861                                 } else {
1862                                         analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
1863                                         analog_wait_event(p);
1864                                         analog_play_tone(p, index, -1);
1865                                         analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
1866                                         analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1867                                         p->owner = p->subs[ANALOG_SUB_REAL].owner;
1868                                         ast_hangup(chan);
1869                                         goto quit;
1870                                 }
1871                         } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
1872                                                         ((exten[0] != '*') || (strlen(exten) > 2))) {
1873                                 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);
1874                                 break;
1875                         }
1876                         if (!timeout)
1877                                 timeout = analog_gendigittimeout;
1878                         if (len && !ast_ignore_pattern(chan->context, exten))
1879                                 analog_play_tone(p, index, -1);
1880                 }
1881                 break;
1882         case ANALOG_SIG_FXSLS:
1883         case ANALOG_SIG_FXSGS:
1884         case ANALOG_SIG_FXSKS:
1885
1886                 /* If we want caller id, we're in a prering state due to a polarity reversal
1887                  * and we're set to use a polarity reversal to trigger the start of caller id,
1888                  * grab the caller id and wait for ringing to start... */
1889                 if (p->use_callerid && (chan->_state == AST_STATE_PRERING && (p->cid_start == ANALOG_CID_START_POLARITY || p->cid_start == ANALOG_CID_START_POLARITY_IN))) {
1890                         /* If set to use DTMF CID signalling, listen for DTMF */
1891                         if (p->cid_signalling == CID_SIG_DTMF) {
1892                                 int i = 0;
1893                                 cs = NULL;
1894                                 ast_debug(1, "Receiving DTMF cid on "
1895                                         "channel %s\n", chan->name);
1896
1897                                 analog_set_linear_mode(p, index, 0);
1898
1899                                 res = 2000;
1900                                 for (;;) {
1901                                         struct ast_frame *f;
1902                                         res = ast_waitfor(chan, res);
1903                                         if (res <= 0) {
1904                                                 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
1905                                                         "Exiting simple switch\n");
1906                                                 ast_hangup(chan);
1907                                                 goto quit;
1908                                         }
1909                                         if (!(f = ast_read(chan)))
1910                                                 break;
1911                                         if (f->frametype == AST_FRAME_DTMF) {
1912                                                 dtmfbuf[i++] = f->subclass;
1913                                                 ast_debug(1, "CID got digit '%c'\n", f->subclass);
1914                                                 res = 2000;
1915                                         }
1916                                         ast_frfree(f);
1917                                         if (chan->_state == AST_STATE_RING ||
1918                                             chan->_state == AST_STATE_RINGING)
1919                                                 break; /* Got ring */
1920                                 }
1921                                 dtmfbuf[i] = '\0';
1922
1923                                 analog_set_linear_mode(p, index, 1);
1924
1925                                 /* Got cid and ring. */
1926                                 ast_debug(1, "CID got string '%s'\n", dtmfbuf);
1927                                 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
1928                                 ast_debug(1, "CID is '%s', flags %d\n",
1929                                         dtmfcid, flags);
1930                                 /* If first byte is NULL, we have no cid */
1931                                 if (!ast_strlen_zero(dtmfcid))
1932                                         number = dtmfcid;
1933                                 else
1934                                         number = NULL;
1935
1936                         /* If set to use V23 Signalling, launch our FSK gubbins and listen for it */
1937                         } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) {
1938                                 int timeout = 10000;  /* Ten seconds */
1939                                 struct timeval start = ast_tvnow();
1940                                 enum analog_event ev;
1941
1942                                 namebuf[0] = 0;
1943                                 numbuf[0] = 0;
1944
1945                                 if (!analog_start_cid_detect(p, p->cid_signalling)) {
1946                                         while (1) {
1947                                                 res = analog_get_callerid(p, namebuf, numbuf, &ev, timeout - ast_tvdiff_ms(ast_tvnow(), start));
1948
1949                                                 if (res == 0) {
1950                                                         break;
1951                                                 }
1952
1953                                                 if (res == 1) {
1954                                                         if (p->cid_signalling == CID_SIG_V23_JP) {
1955                                                                 if (ev == ANALOG_EVENT_RINGBEGIN) {
1956                                                                         analog_off_hook(p);
1957                                                                         usleep(1);
1958                                                                 }
1959                                                         } else {
1960                                                                 ev = ANALOG_EVENT_NONE;
1961                                                                 break;
1962                                                         }
1963                                                 }
1964
1965                                                 if (ast_tvdiff_ms(ast_tvnow(), start) > timeout)
1966                                                         break;
1967
1968                                         }
1969                                         name = namebuf;
1970                                         number = numbuf;
1971
1972                                         analog_stop_cid_detect(p);
1973
1974                                         if (p->cid_signalling == CID_SIG_V23_JP) {
1975                                                 res = analog_on_hook(p);
1976                                                 usleep(1);
1977                                                 res = 4000;
1978                                         } else {
1979                                                 /* Finished with Caller*ID, now wait for a ring to make sure there really is a call coming */
1980                                                 res = 2000;
1981                                         }
1982
1983                                         for (;;) {
1984                                                 struct ast_frame *f;
1985                                                 res = ast_waitfor(chan, res);
1986                                                 if (res <= 0) {
1987                                                         ast_log(LOG_WARNING, "CID timed out waiting for ring. "
1988                                                                 "Exiting simple switch\n");
1989                                                         ast_hangup(chan);
1990                                                         goto quit;
1991                                                 }
1992                                                 if (!(f = ast_read(chan))) {
1993                                                         ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n");
1994                                                         ast_hangup(chan);
1995                                                         goto quit;
1996                                                 }
1997                                                 ast_frfree(f);
1998                                                 if (chan->_state == AST_STATE_RING ||
1999                                                         chan->_state == AST_STATE_RINGING)
2000                                                         break; /* Got ring */
2001                                         }
2002
2003                                         if (analog_distinctive_ring(chan, p, index, NULL))
2004                                                 goto quit;
2005
2006                                         if (res < 0) {
2007                                                 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
2008                                         }
2009                                 } else
2010                                         ast_log(LOG_WARNING, "Unable to get caller ID space\n");
2011
2012                         } else {
2013                                 ast_log(LOG_WARNING, "Channel %s in prering "
2014                                         "state, but I have nothing to do. "
2015                                         "Terminating simple switch, should be "
2016                                         "restarted by the actual ring.\n",
2017                                         chan->name);
2018                                 ast_hangup(chan);
2019                                 goto quit;
2020                         }
2021                 } else if (p->use_callerid && p->cid_start == ANALOG_CID_START_RING) {
2022                         int timeout = 10000;  /* Ten seconds */
2023                         struct timeval start = ast_tvnow();
2024                         enum analog_event ev;
2025                         int curRingData[3] = { 0 };
2026                         int receivedRingT = 0;
2027
2028                         namebuf[0] = 0;
2029                         numbuf[0] = 0;
2030
2031                         if (!analog_start_cid_detect(p, p->cid_signalling)) {
2032                                 while (1) {
2033                                         res = analog_get_callerid(p, namebuf, numbuf, &ev, timeout - ast_tvdiff_ms(ast_tvnow(), start));
2034
2035                                         if (res == 0) {
2036                                                 break;
2037                                         }
2038
2039                                         if (res == 1 || res == 2) {
2040                                                 if (ev == ANALOG_EVENT_POLARITY && p->hanguponpolarityswitch && p->polarity == POLARITY_REV) {
2041                                                         ast_debug(1, "Hanging up due to polarity reversal on channel %d while detecting callerid\n", p->channel);
2042                                                         p->polarity = POLARITY_IDLE;
2043                                                         ast_hangup(chan);
2044                                                         goto quit;
2045                                                 } else if (ev != ANALOG_EVENT_NONE && ev != ANALOG_EVENT_RINGBEGIN && ev != ANALOG_EVENT_RINGOFFHOOK) {
2046                                                         break;
2047                                                 }
2048                                                 if (res != 2) {
2049                                                         /* Let us detect callerid when the telco uses distinctive ring */
2050                                                         curRingData[receivedRingT] = p->ringt;
2051
2052                                                         if (p->ringt < p->ringt_base/2) {
2053                                                                 break;
2054                                                         }
2055                                                         /* Increment the ringT counter so we can match it against
2056                                                            values in chan_dahdi.conf for distinctive ring */
2057                                                         if (++receivedRingT == ARRAY_LEN(curRingData)) {
2058                                                                 break;
2059                                                         }
2060                                                 }
2061                                         }
2062
2063                                         if (ast_tvdiff_ms(ast_tvnow(), start) > timeout) {
2064                                                 break;
2065                                         }
2066
2067                                 }
2068                                 name = namebuf;
2069                                 number = numbuf;
2070
2071                                 analog_stop_cid_detect(p);
2072
2073                                 if (analog_distinctive_ring(chan, p, index, curRingData))
2074                                         goto quit;
2075
2076                                 if (res < 0) {
2077                                         ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
2078                                 }
2079                         } else
2080                                 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
2081                 }
2082                 else
2083                         cs = NULL;
2084
2085                 if (number)
2086                         ast_shrink_phone_number(number);
2087                 ast_set_callerid(chan, number, name, number);
2088
2089                 if (cs)
2090                         callerid_free(cs);
2091
2092                 analog_handle_notify_message(chan, p, flags, -1);
2093
2094                 ast_setstate(chan, AST_STATE_RING);
2095                 chan->rings = 1;
2096                 p->ringt = p->ringt_base;
2097                 res = ast_pbx_run(chan);
2098                 if (res) {
2099                         ast_hangup(chan);
2100                         ast_log(LOG_WARNING, "PBX exited non-zero\n");
2101                 }
2102                 goto quit;
2103         default:
2104                 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);
2105                 break;
2106         }
2107         res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
2108         if (res < 0)
2109                 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
2110         ast_hangup(chan);
2111 quit:
2112         analog_decrease_ss_count(p);
2113         return NULL;
2114 }
2115
2116 int analog_ss_thread_start(struct analog_pvt *p, struct ast_channel *chan)
2117 {
2118         pthread_t threadid;
2119
2120         return ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, p);
2121 }
2122
2123 static struct ast_frame *__analog_handle_event(struct analog_pvt *p, struct ast_channel *ast)
2124 {
2125         int res, x;
2126         int mysig;
2127         enum analog_sub index;
2128         char *c;
2129         pthread_t threadid;
2130         pthread_attr_t attr;
2131         struct ast_channel *chan;
2132         struct ast_frame *f;
2133         ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
2134
2135         index = analog_get_index(ast, p, 0);
2136         mysig = p->sig;
2137         if (p->outsigmod > -1)
2138                 mysig = p->outsigmod;
2139         p->subs[index].f.frametype = AST_FRAME_NULL;
2140         p->subs[index].f.subclass = 0;
2141         p->subs[index].f.datalen = 0;
2142         p->subs[index].f.samples = 0;
2143         p->subs[index].f.mallocd = 0;
2144         p->subs[index].f.offset = 0;
2145         p->subs[index].f.src = "dahdi_handle_event";
2146         p->subs[index].f.data.ptr = NULL;
2147         f = &p->subs[index].f;
2148
2149         if (index < 0)
2150                 return &p->subs[index].f;
2151
2152         if (index != ANALOG_SUB_REAL) {
2153                 ast_log(LOG_ERROR, "We got an event on a non real sub.  Fix it!\n");
2154         }
2155
2156         res = analog_get_event(p);
2157
2158         ast_debug(1, "Got event %s(%d) on channel %d (index %d)\n", analog_event2str(res), res, p->channel, index);
2159
2160         switch (res) {
2161 #ifdef ANALOG_EVENT_EC_DISABLED
2162         case ANALOG_EVENT_EC_DISABLED:
2163                 ast_verb(3, "Channel %d echo canceler disabled due to CED detection\n", p->channel);
2164                 p->echocanon = 0;
2165                 break;
2166 #endif
2167         case ANALOG_EVENT_PULSE_START:
2168                 /* Stop tone if there's a pulse start and the PBX isn't started */
2169                 if (!ast->pbx)
2170                         analog_play_tone(p, ANALOG_SUB_REAL, -1);
2171                 break;
2172         case ANALOG_EVENT_DIALCOMPLETE:
2173                 if (p->inalarm) break;
2174                 x = analog_is_dialing(p, index);
2175                 if (!x) { /* if not still dialing in driver */
2176                         analog_set_echocanceller(p, 1);
2177                         if (p->echobreak) {
2178                                 analog_train_echocanceller(p);
2179                                 ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
2180                                 p->dop.op = ANALOG_DIAL_OP_REPLACE;
2181                                 analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
2182                                 p->echobreak = 0;
2183                         } else {
2184                                 analog_set_dialing(p, 0);
2185                                 if ((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) {
2186                                         /* if thru with dialing after offhook */
2187                                         if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
2188                                                 ast_setstate(ast, AST_STATE_UP);
2189                                                 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2190                                                 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2191                                                 break;
2192                                         } else { /* if to state wait for offhook to dial rest */
2193                                                 /* we now wait for off hook */
2194                                                 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
2195                                         }
2196                                 }
2197                                 if (ast->_state == AST_STATE_DIALING) {
2198                                         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)))) {
2199                                                 ast_setstate(ast, AST_STATE_RINGING);
2200                                         } else if (!p->answeronpolarityswitch) {
2201                                                 ast_setstate(ast, AST_STATE_UP);
2202                                                 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2203                                                 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2204                                                 /* If aops=0 and hops=1, this is necessary */
2205                                                 p->polarity = POLARITY_REV;
2206                                         } else {
2207                                                 /* Start clean, so we can catch the change to REV polarity when party answers */
2208                                                 p->polarity = POLARITY_IDLE;
2209                                         }
2210                                 }
2211                         }
2212                 }
2213                 break;
2214         case ANALOG_EVENT_ALARM:
2215                 p->inalarm = 1;
2216                 analog_get_and_handle_alarms(p);
2217
2218         case ANALOG_EVENT_ONHOOK:
2219                 switch (p->sig) {
2220                 case ANALOG_SIG_FXOLS:
2221                 case ANALOG_SIG_FXOGS:
2222                 case ANALOG_SIG_FXOKS:
2223                         p->fxsoffhookstate = 0;
2224                         p->onhooktime = time(NULL);
2225                         p->msgstate = -1;
2226                         /* Check for some special conditions regarding call waiting */
2227                         if (index == ANALOG_SUB_REAL) {
2228                                 /* The normal line was hung up */
2229                                 if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
2230                                         /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
2231                                         analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
2232                                         ast_verb(3, "Channel %d still has (callwait) call, ringing phone\n", p->channel);
2233                                         analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
2234                                         analog_stop_callwait(p);
2235                                         p->owner = NULL;
2236                                         /* Don't start streaming audio yet if the incoming call isn't up yet */
2237                                         if (p->subs[ANALOG_SUB_REAL].owner->_state != AST_STATE_UP)
2238                                                 analog_set_dialing(p, 1);
2239                                         analog_ring(p);
2240                                 } else if (p->subs[ANALOG_SUB_THREEWAY].owner) {
2241                                         unsigned int mssinceflash;
2242                                         /* Here we have to retain the lock on both the main channel, the 3-way channel, and
2243                                            the private structure -- not especially easy or clean */
2244                                         while (p->subs[ANALOG_SUB_THREEWAY].owner && ast_channel_trylock(p->subs[ANALOG_SUB_THREEWAY].owner)) {
2245                                                 /* Yuck, didn't get the lock on the 3-way, gotta release everything and re-grab! */
2246                                                 analog_unlock_private(p);
2247                                                 CHANNEL_DEADLOCK_AVOIDANCE(ast);
2248                                                 /* We can grab ast and p in that order, without worry.  We should make sure
2249                                                    nothing seriously bad has happened though like some sort of bizarre double
2250                                                    masquerade! */
2251                                                 analog_lock_private(p);
2252                                                 if (p->owner != ast) {
2253                                                         ast_log(LOG_WARNING, "This isn't good...\n");
2254                                                         return NULL;
2255                                                 }
2256                                         }
2257                                         if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
2258                                                 ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
2259                                                 return NULL;
2260                                         }
2261                                         mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
2262                                         ast_debug(1, "Last flash was %d ms ago\n", mssinceflash);
2263                                         if (mssinceflash < MIN_MS_SINCE_FLASH) {
2264                                                 /* It hasn't been long enough since the last flashook.  This is probably a bounce on
2265                                                    hanging up.  Hangup both channels now */
2266                                                 if (p->subs[ANALOG_SUB_THREEWAY].owner)
2267                                                         ast_queue_hangup_with_cause(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CAUSE_NO_ANSWER);
2268                                                 ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
2269                                                 ast_debug(1, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
2270                                                 ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
2271                                         } else if ((ast->pbx) || (ast->_state == AST_STATE_UP)) {
2272                                                 if (p->transfer) {
2273                                                         /* Only attempt transfer if the phone is ringing; why transfer to busy tone eh? */
2274                                                         if (!p->transfertobusy && ast->_state == AST_STATE_BUSY) {
2275                                                                 ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
2276                                                                 /* Swap subs and dis-own channel */
2277                                                                 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
2278                                                                 p->owner = NULL;
2279                                                                 /* Ring the phone */
2280                                                                 analog_ring(p);
2281                                                         } else {
2282                                                                 if ((res = analog_attempt_transfer(p)) < 0) {
2283                                                                         ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
2284                                                                         if (p->subs[ANALOG_SUB_THREEWAY].owner)
2285                                                                                 ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
2286                                                                 } else if (res) {
2287                                                                         /* this isn't a threeway call anymore */
2288                                                                         p->subs[ANALOG_SUB_REAL].inthreeway = 0;
2289                                                                         p->subs[ANALOG_SUB_THREEWAY].inthreeway = 0;
2290
2291                                                                         /* Don't actually hang up at this point */
2292                                                                         if (p->subs[ANALOG_SUB_THREEWAY].owner)
2293                                                                                 ast_channel_unlock(&p->subs[ANALOG_SUB_THREEWAY].owner);
2294                                                                         break;
2295                                                                 }
2296                                                         }
2297                                                         /* this isn't a threeway call anymore */
2298                                                         p->subs[ANALOG_SUB_REAL].inthreeway = 0;
2299                                                         p->subs[ANALOG_SUB_THREEWAY].inthreeway = 0;
2300                                                 } else {
2301                                                         ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
2302                                                         if (p->subs[ANALOG_SUB_THREEWAY].owner)
2303                                                                 ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
2304                                                 }
2305                                         } else {
2306                                                 ast_cel_report_event(ast, AST_CEL_BLINDTRANSFER, NULL, ast->linkedid, NULL);
2307                                                 ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
2308                                                 /* Swap subs and dis-own channel */
2309                                                 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
2310                                                 p->owner = NULL;
2311                                                 /* Ring the phone */
2312                                                 analog_ring(p);
2313                                         }
2314                                 }
2315                         } else {
2316                                 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
2317                         }
2318                         /* Fall through */
2319                 default:
2320                         analog_set_echocanceller(p, 0);
2321                         return NULL;
2322                 }
2323                 break;
2324         case ANALOG_EVENT_RINGOFFHOOK:
2325                 if (p->inalarm) break;
2326                 /* for E911, its supposed to wait for offhook then dial
2327                    the second half of the dial string */
2328                 if (((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
2329                         c = strchr(p->dialdest, '/');
2330                         if (c)
2331                                 c++;
2332                         else
2333                                 c = p->dialdest;
2334                         if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
2335                         else ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
2336                         if (strlen(p->dop.dialstr) > 4) {
2337                                 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2338                                 strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2339                                 p->echorest[sizeof(p->echorest) - 1] = '\0';
2340                                 p->echobreak = 1;
2341                                 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2342                         } else
2343                                 p->echobreak = 0;
2344                         if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
2345                                 int saveerr = errno;
2346                                 analog_on_hook(p);
2347                                 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
2348                                 return NULL;
2349                         }
2350                         analog_set_dialing(p, 1);
2351                         return &p->subs[index].f;
2352                 }
2353                 switch (p->sig) {
2354                 case ANALOG_SIG_FXOLS:
2355                 case ANALOG_SIG_FXOGS:
2356                 case ANALOG_SIG_FXOKS:
2357                         p->fxsoffhookstate = 1;
2358                         switch (ast->_state) {
2359                         case AST_STATE_RINGING:
2360                                 analog_set_echocanceller(p, 1);
2361                                 analog_train_echocanceller(p);
2362                                 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2363                                 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2364                                 /* Make sure it stops ringing */
2365                                 analog_off_hook(p);
2366                                 ast_debug(1, "channel %d answered\n", p->channel);
2367                                 analog_set_dialing(p, 0);
2368                                 p->callwaitcas = 0;
2369                                 if (!ast_strlen_zero(p->dop.dialstr)) {
2370                                         /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */
2371                                         res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
2372                                         if (res < 0) {
2373                                                 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
2374                                                 p->dop.dialstr[0] = '\0';
2375                                                 return NULL;
2376                                         } else {
2377                                                 ast_debug(1, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
2378                                                 p->subs[index].f.frametype = AST_FRAME_NULL;
2379                                                 p->subs[index].f.subclass = 0;
2380                                                 analog_set_dialing(p, 1);
2381                                         }
2382                                         p->dop.dialstr[0] = '\0';
2383                                         ast_setstate(ast, AST_STATE_DIALING);
2384                                 } else
2385                                         ast_setstate(ast, AST_STATE_UP);
2386                                 return &p->subs[index].f;
2387                         case AST_STATE_DOWN:
2388                                 ast_setstate(ast, AST_STATE_RING);
2389                                 ast->rings = 1;
2390                                 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2391                                 p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
2392                                 ast_debug(1, "channel %d picked up\n", p->channel);
2393                                 return &p->subs[index].f;
2394                         case AST_STATE_UP:
2395                                 /* Make sure it stops ringing */
2396                                 analog_off_hook(p);
2397                                 /* Okay -- probably call waiting*/
2398                                 if (ast_bridged_channel(p->owner))
2399                                         ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
2400                                 break;
2401                         case AST_STATE_RESERVED:
2402                                 /* Start up dialtone */
2403                                 if (analog_has_voicemail(p))
2404                                         res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_STUTTER);
2405                                 else
2406                                         res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_DIALTONE);
2407                                 break;
2408                         default:
2409                                 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
2410                         }
2411                         break;
2412                 case ANALOG_SIG_FXSLS:
2413                 case ANALOG_SIG_FXSGS:
2414                 case ANALOG_SIG_FXSKS:
2415                         if (ast->_state == AST_STATE_RING) {
2416                                 p->ringt = p->ringt_base;
2417                         }
2418
2419                         /* Fall through */
2420                 case ANALOG_SIG_EM:
2421                 case ANALOG_SIG_EM_E1:
2422                 case ANALOG_SIG_EMWINK:
2423                 case ANALOG_SIG_FEATD:
2424                 case ANALOG_SIG_FEATDMF:
2425                 case ANALOG_SIG_FEATDMF_TA:
2426                 case ANALOG_SIG_E911:
2427                 case ANALOG_SIG_FGC_CAMA:
2428                 case ANALOG_SIG_FGC_CAMAMF:
2429                 case ANALOG_SIG_FEATB:
2430                 case ANALOG_SIG_SF:
2431                 case ANALOG_SIG_SFWINK:
2432                 case ANALOG_SIG_SF_FEATD:
2433                 case ANALOG_SIG_SF_FEATDMF:
2434                 case ANALOG_SIG_SF_FEATB:
2435                         if (ast->_state == AST_STATE_PRERING)
2436                                 ast_setstate(ast, AST_STATE_RING);
2437                         if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
2438                                 ast_debug(1, "Ring detected\n");
2439                                 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2440                                 p->subs[index].f.subclass = AST_CONTROL_RING;
2441                         } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
2442                                 ast_debug(1, "Line answered\n");
2443                                 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2444                                 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2445                                 ast_setstate(ast, AST_STATE_UP);
2446                         } else if (ast->_state != AST_STATE_RING)
2447                                 ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
2448                         break;
2449                 default:
2450                         ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
2451                 }
2452                 break;
2453 #ifdef ANALOG_EVENT_RINGBEGIN
2454         case ANALOG_EVENT_RINGBEGIN:
2455                 switch (p->sig) {
2456                 case ANALOG_SIG_FXSLS:
2457                 case ANALOG_SIG_FXSGS:
2458                 case ANALOG_SIG_FXSKS:
2459                         if (ast->_state == AST_STATE_RING) {
2460                                 p->ringt = p->ringt_base;
2461                         }
2462                         break;
2463                 }
2464                 break;
2465 #endif
2466         case ANALOG_EVENT_RINGEROFF:
2467                 if (p->inalarm) break;
2468                 ast->rings++;
2469                 if (ast->rings == p->cidrings) {
2470                         analog_send_callerid(p, 0, &p->cid);
2471                 }
2472
2473                 if (ast->rings > p->cidrings) {
2474                         p->callwaitcas = 0;
2475                 }
2476                 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2477                 p->subs[index].f.subclass = AST_CONTROL_RINGING;
2478                 break;
2479         case ANALOG_EVENT_RINGERON:
2480                 break;
2481         case ANALOG_EVENT_NOALARM:
2482                 p->inalarm = 0;
2483                 if (!p->unknown_alarm) {
2484                         ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
2485                         manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
2486                                 "Channel: %d\r\n", p->channel);
2487                 } else {
2488                         p->unknown_alarm = 0;
2489                 }
2490                 break;
2491         case ANALOG_EVENT_WINKFLASH:
2492                 if (p->inalarm) break;
2493                 /* Remember last time we got a flash-hook */
2494                 gettimeofday(&p->flashtime, NULL);
2495                 switch (mysig) {
2496                 case ANALOG_SIG_FXOLS:
2497                 case ANALOG_SIG_FXOGS:
2498                 case ANALOG_SIG_FXOKS:
2499                         ast_debug(1, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
2500                                 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));
2501
2502                         p->callwaitcas = 0;
2503
2504                         if (index != ANALOG_SUB_REAL) {
2505                                 ast_log(LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", index, p->channel);
2506                                 goto winkflashdone;
2507                         }
2508
2509                         if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
2510                                 /* Swap to call-wait */
2511                                 int previous_state = p->subs[ANALOG_SUB_CALLWAIT].owner->_state;
2512                                 if (p->subs[ANALOG_SUB_CALLWAIT].owner->_state == AST_STATE_RINGING) {
2513                                         ast_setstate(p->subs[ANALOG_SUB_CALLWAIT].owner, AST_STATE_UP);
2514                                 }
2515                                 analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_CALLWAIT);
2516                                 analog_play_tone(p, ANALOG_SUB_REAL, -1);
2517                                 p->owner = p->subs[ANALOG_SUB_REAL].owner;
2518                                 ast_debug(1, "Making %s the new owner\n", p->owner->name);
2519                                 if (previous_state == AST_STATE_RINGING) {
2520                                         ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_ANSWER);
2521                                 }
2522                                 analog_stop_callwait(p);
2523                                 /* Start music on hold if appropriate */
2524                                 if (!p->subs[ANALOG_SUB_CALLWAIT].inthreeway && ast_bridged_channel(p->subs[ANALOG_SUB_CALLWAIT].owner)) {
2525                                         ast_queue_control_data(p->subs[ANALOG_SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
2526                                                 S_OR(p->mohsuggest, NULL),
2527                                                 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2528                                 }
2529                                 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_ANSWER);
2530                                 if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner)) {
2531                                         ast_queue_control_data(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_HOLD,
2532                                                 S_OR(p->mohsuggest, NULL),
2533                                                 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2534                                 }
2535                                 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
2536                         } else if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
2537                                 char cid_num[256];
2538                                 char cid_name[256];
2539
2540                                 if (!p->threewaycalling) {
2541                                         /* Just send a flash if no 3-way calling */
2542                                         ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_FLASH);
2543                                         goto winkflashdone;
2544                                 } else if (!analog_check_for_conference(p)) {
2545                                         if (p->dahditrcallerid && p->owner) {
2546                                                 if (p->owner->cid.cid_num)
2547                                                         ast_copy_string(cid_num, p->owner->cid.cid_num, sizeof(cid_num));
2548                                                 if (p->owner->cid.cid_name)
2549                                                         ast_copy_string(cid_name, p->owner->cid.cid_name, sizeof(cid_name));
2550                                         }
2551                                         /* XXX This section needs much more error checking!!! XXX */
2552                                         /* Start a 3-way call if feasible */
2553                                         if (!((ast->pbx) ||
2554                                               (ast->_state == AST_STATE_UP) ||
2555                                               (ast->_state == AST_STATE_RING))) {
2556                                                 ast_debug(1, "Flash when call not up or ringing\n");
2557                                                         goto winkflashdone;
2558                                         }
2559                                         if (analog_alloc_sub(p, ANALOG_SUB_THREEWAY)) {
2560                                                 ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
2561                                                 goto winkflashdone;
2562                                         }
2563                                         /* Make new channel */
2564                                         chan = analog_new_ast_channel(p, AST_STATE_RESERVED, 0, ANALOG_SUB_THREEWAY, NULL);
2565                                         if (p->dahditrcallerid) {
2566                                                 if (!p->origcid_num)
2567                                                         p->origcid_num = ast_strdup(p->cid_num);
2568                                                 if (!p->origcid_name)
2569                                                         p->origcid_name = ast_strdup(p->cid_name);
2570                                                 ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
2571                                                 ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
2572                                         }
2573                                         /* Swap things around between the three-way and real call */
2574                                         analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
2575                                         /* Disable echo canceller for better dialing */
2576                                         analog_set_echocanceller(p, 0);
2577                                         res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_DIALRECALL);
2578                                         if (res)
2579                                                 ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
2580                                         p->ss_astchan = p->owner = chan;
2581                                         pthread_attr_init(&attr);
2582                                         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2583                                         if (!chan) {
2584                                                 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", p->channel);
2585                                         } else if (ast_pthread_create(&threadid, &attr, __analog_ss_thread, p)) {
2586                                                 ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
2587                                                 res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
2588                                                 analog_set_echocanceller(p, 1);
2589                                                 ast_hangup(chan);
2590                                         } else {
2591                                                 struct ast_channel *other = ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner);
2592                                                 int way3bridge = 0, cdr3way = 0;
2593
2594                                                 if (!other) {
2595                                                         other = ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner);
2596                                                 } else
2597                                                         way3bridge = 1;
2598
2599                                                 if (p->subs[ANALOG_SUB_THREEWAY].owner->cdr)
2600                                                         cdr3way = 1;
2601
2602                                                 ast_verb(3, "Started three way call on channel %d\n", p->channel);
2603                                                 /* Start music on hold if appropriate */
2604                                                 if (ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
2605                                                         ast_queue_control_data(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CONTROL_HOLD,
2606                                                                 S_OR(p->mohsuggest, NULL),
2607                                                                 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2608                                                 }
2609                                         }
2610                                         pthread_attr_destroy(&attr);
2611                                 }
2612                         } else {
2613                                 /* Already have a 3 way call */
2614                                 if (p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
2615                                         /* Call is already up, drop the last person */
2616                                         ast_debug(1, "Got flash with three way call up, dropping last call on %d\n", p->channel);
2617                                         /* If the primary call isn't answered yet, use it */
2618                                         if ((p->subs[ANALOG_SUB_REAL].owner->_state != AST_STATE_UP) && (p->subs[ANALOG_SUB_THREEWAY].owner->_state == AST_STATE_UP)) {
2619                                                 /* Swap back -- we're dropping the real 3-way that isn't finished yet*/
2620                                                 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
2621                                                 p->owner = p->subs[ANALOG_SUB_REAL].owner;
2622                                         }
2623                                         /* Drop the last call and stop the conference */
2624                                         ast_verb(3, "Dropping three-way call on %s\n", p->subs[ANALOG_SUB_THREEWAY].owner->name);
2625                                         ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
2626                                         p->subs[ANALOG_SUB_REAL].inthreeway = 0;
2627                                         p->subs[ANALOG_SUB_THREEWAY].inthreeway = 0;
2628                                 } else {
2629                                         /* Lets see what we're up to */
2630                                         if (((ast->pbx) || (ast->_state == AST_STATE_UP)) &&
2631                                             (p->transfertobusy || (ast->_state != AST_STATE_BUSY))) {
2632                                                 int otherindex = ANALOG_SUB_THREEWAY;
2633                                                 struct ast_channel *other = ast_bridged_channel(p->subs[A