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