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