2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2009, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Analog signaling module
23 * \author Matthew Fredrickson <creslin@digium.com>
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"
43 #include "sig_analog.h"
45 #define POLARITY_IDLE 0
46 #define POLARITY_REV 1
47 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
48 static int analog_matchdigittimeout = 3000;
49 static int analog_gendigittimeout = 8000;
50 static int analog_firstdigittimeout = 16000;
51 static char analog_defaultcic[64] = "";
52 static char analog_defaultozz[64] = "";
55 enum analog_sigtype sigtype;
56 const char const *name;
58 { ANALOG_SIG_FXOLS, "fxo_ls" },
59 { ANALOG_SIG_FXOKS, "fxo_ks" },
60 { ANALOG_SIG_FXOGS, "fxo_gs" },
61 { ANALOG_SIG_FXSLS, "fxs_ls" },
62 { ANALOG_SIG_FXSKS, "fxs_ks" },
63 { ANALOG_SIG_FXSGS, "fxs_gs" },
64 { ANALOG_SIG_EMWINK, "em_w" },
65 { ANALOG_SIG_EM, "em" },
66 { ANALOG_SIG_EM_E1, "em_e1" },
67 { ANALOG_SIG_FEATD, "featd" },
68 { ANALOG_SIG_FEATDMF, "featdmf" },
69 { ANALOG_SIG_FEATDMF_TA, "featdmf_ta" },
70 { ANALOG_SIG_FEATB, "featb" },
71 { ANALOG_SIG_FGC_CAMA, "fgccama" },
72 { ANALOG_SIG_FGC_CAMAMF, "fgccamamf" },
73 { ANALOG_SIG_SF, "sf" },
74 { ANALOG_SIG_SFWINK, "sf_w" },
75 { ANALOG_SIG_SF_FEATD, "sf_featd" },
76 { ANALOG_SIG_SF_FEATDMF, "sf_featdmf" },
77 { ANALOG_SIG_SF_FEATB, "sf_featb" },
78 { ANALOG_SIG_E911, "e911" },
82 unsigned int cid_type;
83 const char const *name;
85 { CID_SIG_BELL, "bell" },
86 { CID_SIG_V23, "v23" },
87 { CID_SIG_V23_JP, "v23_jp" },
88 { CID_SIG_DTMF, "dtmf" },
89 /* "smdi" is intentionally not supported here, as there is a much better
90 * way to do this in the dialplan now. */
93 #define ISTRUNK(p) ((p->sig == ANALOG_SIG_FXSLS) || (p->sig == ANALOG_SIG_FXSKS) || \
94 (p->sig == ANALOG_SIG_FXSGS))
96 enum analog_sigtype analog_str_to_sigtype(const char *name)
100 for (i = 0; i < ARRAY_LEN(sigtypes); i++) {
101 if (!strcasecmp(sigtypes[i].name, name)) {
102 return sigtypes[i].sigtype;
109 const char *analog_sigtype_to_str(enum analog_sigtype sigtype)
113 for (i = 0; i < ARRAY_LEN(sigtypes); i++) {
114 if (sigtype == sigtypes[i].sigtype) {
115 return sigtypes[i].name;
122 unsigned int analog_str_to_cidtype(const char *name)
126 for (i = 0; i < ARRAY_LEN(cidtypes); i++) {
127 if (!strcasecmp(cidtypes[i].name, name)) {
128 return cidtypes[i].cid_type;
135 const char *analog_cidtype_to_str(unsigned int cid_type)
139 for (i = 0; i < ARRAY_LEN(cidtypes); i++) {
140 if (cid_type == cidtypes[i].cid_type) {
141 return cidtypes[i].name;
148 static int analog_start_cid_detect(struct analog_pvt *p, int cid_signalling)
150 if (p->calls->start_cid_detect)
151 return p->calls->start_cid_detect(p->chan_pvt, cid_signalling);
156 static int analog_stop_cid_detect(struct analog_pvt *p)
158 if (p->calls->stop_cid_detect)
159 return p->calls->stop_cid_detect(p->chan_pvt);
164 static int analog_get_callerid(struct analog_pvt *p, char *name, char *number, enum analog_event *ev, size_t timeout)
166 if (p->calls->get_callerid)
167 return p->calls->get_callerid(p->chan_pvt, name, number, ev, timeout);
172 static int analog_get_event(struct analog_pvt *p)
174 if (p->calls->get_event)
175 return p->calls->get_event(p->chan_pvt);
180 static int analog_wait_event(struct analog_pvt *p)
182 if (p->calls->wait_event)
183 return p->calls->wait_event(p->chan_pvt);
188 enum analog_cid_start analog_str_to_cidstart(const char *value)
190 if (!strcasecmp(value, "ring")) {
191 return ANALOG_CID_START_RING;
192 } else if (!strcasecmp(value, "polarity")) {
193 return ANALOG_CID_START_POLARITY;
194 } else if (!strcasecmp(value, "polarity_in")) {
195 return ANALOG_CID_START_POLARITY_IN;
201 const char *analog_cidstart_to_str(enum analog_cid_start cid_start)
204 case ANALOG_CID_START_RING:
206 case ANALOG_CID_START_POLARITY:
208 case ANALOG_CID_START_POLARITY_IN:
209 return "Polarity_In";
215 static char *analog_event2str(enum analog_event event)
219 case ANALOG_EVENT_DIALCOMPLETE:
220 res = "ANALOG_EVENT_DIALCOMPLETE";
222 case ANALOG_EVENT_WINKFLASH:
223 res = "ANALOG_EVENT_WINKFLASH";
225 case ANALOG_EVENT_ONHOOK:
226 res = "ANALOG_EVENT_ONHOOK";
228 case ANALOG_EVENT_RINGOFFHOOK:
229 res = "ANALOG_EVENT_RINGOFFHOOK";
231 case ANALOG_EVENT_ALARM:
232 res = "ANALOG_EVENT_ALARM";
234 case ANALOG_EVENT_NOALARM:
235 res = "ANALOG_EVENT_NOALARM";
237 case ANALOG_EVENT_HOOKCOMPLETE:
238 res = "ANALOG_EVENT_HOOKCOMPLETE";
240 case ANALOG_EVENT_POLARITY:
241 res = "ANALOG_EVENT_POLARITY";
243 case ANALOG_EVENT_RINGERON:
244 res = "ANALOG_EVENT_RINGERON";
246 case ANALOG_EVENT_RINGEROFF:
247 res = "ANALOG_EVENT_RINGEROFF";
249 case ANALOG_EVENT_RINGBEGIN:
250 res = "ANALOG_EVENT_RINGBEGIN";
252 case ANALOG_EVENT_PULSE_START:
253 res = "ANALOG_EVENT_PULSE_START";
255 case ANALOG_EVENT_NEONMWI_ACTIVE:
256 res = "ANALOG_EVENT_NEONMWI_ACTIVE";
258 case ANALOG_EVENT_NEONMWI_INACTIVE:
259 res = "ANALOG_EVENT_NEONMWI_INACTIVE";
262 res = "UNKNOWN/OTHER";
269 static void analog_swap_subs(struct analog_pvt *p, enum analog_sub a, enum analog_sub b)
272 struct ast_channel *towner;
274 ast_debug(1, "Swapping %d and %d\n", a, b);
276 towner = p->subs[a].owner;
277 tinthreeway = p->subs[a].inthreeway;
279 p->subs[a].owner = p->subs[b].owner;
280 p->subs[a].inthreeway = p->subs[b].inthreeway;
282 p->subs[b].owner = towner;
283 p->subs[b].inthreeway = tinthreeway;
285 if (p->calls->swap_subs)
286 p->calls->swap_subs(p->chan_pvt, a, p->subs[a].owner, b, p->subs[b].owner);
290 static int analog_alloc_sub(struct analog_pvt *p, enum analog_sub x)
292 if (p->calls->allocate_sub) {
294 res = p->calls->allocate_sub(p->chan_pvt, x);
296 p->subs[x].allocd = 1;
303 static int analog_unalloc_sub(struct analog_pvt *p, enum analog_sub x)
305 p->subs[x].allocd = 0;
306 p->subs[x].owner = NULL;
307 if (p->calls->unallocate_sub)
308 return p->calls->unallocate_sub(p->chan_pvt, x);
313 static int analog_send_callerid(struct analog_pvt *p, int cwcid, struct ast_callerid *cid)
315 ast_debug(1, "Sending callerid. CID_NAME: '%s' CID_NUM: '%s'\n", cid->cid_name, cid->cid_num);
321 if (p->calls->send_callerid)
322 return p->calls->send_callerid(p->chan_pvt, cwcid, cid);
327 static int analog_get_index(struct ast_channel *ast, struct analog_pvt *p, int nullok)
330 if (p->subs[ANALOG_SUB_REAL].owner == ast)
331 res = ANALOG_SUB_REAL;
332 else if (p->subs[ANALOG_SUB_CALLWAIT].owner == ast)
333 res = ANALOG_SUB_CALLWAIT;
334 else if (p->subs[ANALOG_SUB_THREEWAY].owner == ast)
335 res = ANALOG_SUB_THREEWAY;
339 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
344 static int analog_dsp_reset_and_flush_digits(struct analog_pvt *p)
346 if (p->calls->dsp_reset_and_flush_digits)
347 return p->calls->dsp_reset_and_flush_digits(p->chan_pvt);
349 /* Return 0 since I think this is unnecessary to do in most cases it is used. Mostly only for ast_dsp */
353 static int analog_play_tone(struct analog_pvt *p, enum analog_sub sub, enum analog_tone tone)
355 if (p->calls->play_tone)
356 return p->calls->play_tone(p->chan_pvt, sub, tone);
361 static struct ast_channel * analog_new_ast_channel(struct analog_pvt *p, int state, int startpbx, enum analog_sub sub, const struct ast_channel *requestor)
363 struct ast_channel *c;
365 if (p->calls->new_ast_channel)
366 c = p->calls->new_ast_channel(p->chan_pvt, state, startpbx, sub, requestor);
370 p->subs[sub].owner = c;
376 static int analog_set_echocanceller(struct analog_pvt *p, int enable)
378 if (p->calls->set_echocanceller)
379 return p->calls->set_echocanceller(p->chan_pvt, enable);
384 static int analog_train_echocanceller(struct analog_pvt *p)
386 if (p->calls->train_echocanceller)
387 return p->calls->train_echocanceller(p->chan_pvt);
392 static int analog_is_off_hook(struct analog_pvt *p)
394 if (p->calls->is_off_hook)
395 return p->calls->is_off_hook(p->chan_pvt);
400 static int analog_ring(struct analog_pvt *p)
403 return p->calls->ring(p->chan_pvt);
408 static int analog_flash(struct analog_pvt *p)
411 return p->calls->flash(p->chan_pvt);
416 static int analog_start(struct analog_pvt *p)
419 return p->calls->start(p->chan_pvt);
424 static int analog_dial_digits(struct analog_pvt *p, enum analog_sub sub, struct analog_dialoperation *dop)
426 if (p->calls->dial_digits)
427 return p->calls->dial_digits(p->chan_pvt, sub, dop);
432 static int analog_on_hook(struct analog_pvt *p)
434 if (p->calls->on_hook)
435 return p->calls->on_hook(p->chan_pvt);
440 static int analog_check_for_conference(struct analog_pvt *p)
442 if (p->calls->check_for_conference)
443 return p->calls->check_for_conference(p->chan_pvt);
448 static void analog_all_subchannels_hungup(struct analog_pvt *p)
450 if (p->calls->all_subchannels_hungup)
451 p->calls->all_subchannels_hungup(p->chan_pvt);
454 static void analog_unlock_private(struct analog_pvt *p)
456 if (p->calls->unlock_private)
457 p->calls->unlock_private(p->chan_pvt);
460 static void analog_lock_private(struct analog_pvt *p)
462 if (p->calls->lock_private)
463 p->calls->lock_private(p->chan_pvt);
466 static int analog_off_hook(struct analog_pvt *p)
468 if (p->calls->off_hook)
469 return p->calls->off_hook(p->chan_pvt);
474 static int analog_dsp_set_digitmode(struct analog_pvt *p, enum analog_dsp_digitmode mode)
476 if (p->calls->dsp_set_digitmode)
477 return p->calls->dsp_set_digitmode(p->chan_pvt, mode);
482 static void analog_cb_handle_dtmfup(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub analog_index, struct ast_frame **dest)
484 if (p->calls->handle_dtmfup)
485 p->calls->handle_dtmfup(p->chan_pvt, ast, analog_index, dest);
488 static int analog_wink(struct analog_pvt *p, enum analog_sub index)
491 return p->calls->wink(p->chan_pvt, index);
496 static int analog_has_voicemail(struct analog_pvt *p)
498 if (p->calls->has_voicemail)
499 return p->calls->has_voicemail(p->chan_pvt);
504 static int analog_is_dialing(struct analog_pvt *p, enum analog_sub index)
506 if (p->calls->is_dialing)
507 return p->calls->is_dialing(p->chan_pvt, index);
512 static int analog_attempt_transfer(struct analog_pvt *p)
514 /* In order to transfer, we need at least one of the channels to
515 actually be in a call bridge. We can't conference two applications
516 together (but then, why would we want to?) */
517 if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner)) {
518 /* The three-way person we're about to transfer to could still be in MOH, so
519 stop if now if appropriate */
520 if (ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner))
521 ast_queue_control(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CONTROL_UNHOLD);
522 if (p->subs[ANALOG_SUB_REAL].owner->_state == AST_STATE_RINGING) {
523 ast_indicate(ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner), AST_CONTROL_RINGING);
525 if (p->subs[ANALOG_SUB_THREEWAY].owner->_state == AST_STATE_RING) {
526 analog_play_tone(p, ANALOG_SUB_THREEWAY, ANALOG_TONE_RINGTONE);
528 if (!p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
529 ast_cel_report_event(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CEL_ATTENDEDTRANSFER, NULL, p->subs[ANALOG_SUB_THREEWAY].owner->linkedid, NULL);
531 if (ast_channel_masquerade(p->subs[ANALOG_SUB_THREEWAY].owner, ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner))) {
532 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
533 ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner)->name, p->subs[ANALOG_SUB_THREEWAY].owner->name);
536 /* Orphan the channel after releasing the lock */
537 ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
538 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
539 } else if (ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
540 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
541 if (p->subs[ANALOG_SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
542 ast_indicate(ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner), AST_CONTROL_RINGING);
544 if (p->subs[ANALOG_SUB_REAL].owner->_state == AST_STATE_RING) {
545 analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_RINGTONE);
547 ast_cel_report_event(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CEL_BLINDTRANSFER, NULL, p->subs[ANALOG_SUB_THREEWAY].owner->linkedid, NULL);
548 if (ast_channel_masquerade(p->subs[ANALOG_SUB_REAL].owner, ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner))) {
549 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
550 ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)->name, p->subs[ANALOG_SUB_REAL].owner->name);
553 /* Three-way is now the REAL */
554 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
555 ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner); /* unlock REAL because THREEWAY has become REAL */
556 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
557 /* Tell the caller not to hangup */
560 ast_debug(1, "Neither %s nor %s are in a bridge, nothing to transfer\n",
561 p->subs[ANALOG_SUB_REAL].owner->name, p->subs[ANALOG_SUB_THREEWAY].owner->name);
562 ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
568 static int analog_update_conf(struct analog_pvt *p)
573 /* Start with the obvious, general stuff */
574 for (x = 0; x < 3; x++) {
575 /* Look for three way calls */
576 if ((p->subs[x].allocd) && p->subs[x].inthreeway) {
577 if (p->calls->conf_add)
578 p->calls->conf_add(p->chan_pvt, x);
581 if (p->calls->conf_del)
582 p->calls->conf_del(p->chan_pvt, x);
585 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
587 if (p->calls->complete_conference_update)
588 p->calls->complete_conference_update(p->chan_pvt, needconf);
592 struct ast_channel * analog_request(struct analog_pvt *p, int *callwait, const struct ast_channel *requestor)
594 ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
595 *callwait = (p->owner != NULL);
598 if (analog_alloc_sub(p, ANALOG_SUB_CALLWAIT)) {
599 ast_log(LOG_ERROR, "Unable to alloc subchannel\n");
604 return analog_new_ast_channel(p, AST_STATE_RESERVED, 0, p->owner ? ANALOG_SUB_CALLWAIT : ANALOG_SUB_REAL, requestor);
607 int analog_available(struct analog_pvt *p, int channelmatch, ast_group_t groupmatch, int *busy, int *channelmatched, int *groupmatched)
611 ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
612 /* We're at least busy at this point */
614 if ((p->sig == ANALOG_SIG_FXOKS) || (p->sig == ANALOG_SIG_FXOLS) || (p->sig == ANALOG_SIG_FXOGS))
617 /* If do not disturb, definitely not */
620 /* If guard time, definitely not */
621 if (p->guardtime && (time(NULL) < p->guardtime))
624 /* If no owner definitely available */
626 if (p->sig == ANALOG_SIG_FXSLS)
629 offhook = analog_is_off_hook(p);
631 if ((p->sig == ANALOG_SIG_FXSKS) || (p->sig == ANALOG_SIG_FXSGS)) {
632 /* When "onhook" that means no battery on the line, and thus
633 it is out of service..., if it's on a TDM card... If it's a channel
634 bank, there is no telling... */
639 } else if (offhook) {
640 ast_debug(1, "Channel %d off hook, can't use\n", p->channel);
641 /* Not available when the other end is off hook */
647 /* If it's not an FXO, forget about call wait */
648 if ((p->sig != ANALOG_SIG_FXOKS) && (p->sig != ANALOG_SIG_FXOLS) && (p->sig != ANALOG_SIG_FXOGS))
651 if (!p->callwaiting) {
652 /* If they don't have call waiting enabled, then for sure they're unavailable at this point */
656 if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
657 /* If there is already a call waiting call, then we can't take a second one */
661 if ((p->owner->_state != AST_STATE_UP) &&
662 ((p->owner->_state != AST_STATE_RINGING) || p->outgoing)) {
663 /* If the current call is not up, then don't allow the call */
666 if ((p->subs[ANALOG_SUB_THREEWAY].owner) && (!p->subs[ANALOG_SUB_THREEWAY].inthreeway)) {
667 /* Can't take a call wait when the three way calling hasn't been merged yet. */
674 static int analog_stop_callwait(struct analog_pvt *p)
676 if (p->callwaitingcallerid)
679 if (p->calls->stop_callwait)
680 return p->calls->stop_callwait(p->chan_pvt);
685 static int analog_callwait(struct analog_pvt *p)
687 if (p->callwaitingcallerid)
689 if (p->calls->callwait)
690 return p->calls->callwait(p->chan_pvt);
695 static void analog_set_cadence(struct analog_pvt *p, struct ast_channel *chan)
697 if (p->calls->set_cadence) {
698 return p->calls->set_cadence(p->chan_pvt, &p->cidrings, chan);
702 static void analog_set_dialing(struct analog_pvt *p, int flag)
705 if (p->calls->set_dialing) {
706 return p->calls->set_dialing(p->chan_pvt, flag);
710 int analog_call(struct analog_pvt *p, struct ast_channel *ast, char *rdest, int timeout)
712 int res, index,mysig;
714 char dest[256]; /* must be same length as p->dialdest */
716 ast_log(LOG_DEBUG, "CALLING CID_NAME: %s CID_NUM:: %s\n", ast->connected.id.name, ast->connected.id.number);
718 ast_copy_string(dest, rdest, sizeof(dest));
719 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
721 if ((ast->_state == AST_STATE_BUSY)) {
722 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_BUSY);
726 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
727 ast_log(LOG_WARNING, "analog_call called on %s, neither down nor reserved\n", ast->name);
734 if (p->outsigmod > -1)
735 mysig = p->outsigmod;
738 case ANALOG_SIG_FXOLS:
739 case ANALOG_SIG_FXOGS:
740 case ANALOG_SIG_FXOKS:
741 if (p->owner == ast) {
742 /* Normal ring, on hook */
744 /* Don't send audio while on hook, until the call is answered */
745 analog_set_dialing(p, 1);
746 analog_set_cadence(p, ast); /* and set p->cidrings */
748 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
749 c = strchr(dest, '/');
752 if (c && (strlen(c) < p->stripmsd)) {
753 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
757 p->dop.op = ANALOG_DIAL_OP_REPLACE;
758 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
759 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
761 p->dop.dialstr[0] = '\0';
764 if (analog_ring(p)) {
765 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
768 analog_set_dialing(p, 1);
770 if (ast->connected.id.number)
771 ast_copy_string(p->callwait_num, ast->connected.id.number, sizeof(p->callwait_num));
773 p->callwait_num[0] = '\0';
774 if (ast->connected.id.name)
775 ast_copy_string(p->callwait_name, ast->connected.id.name, sizeof(p->callwait_name));
777 p->callwait_name[0] = '\0';
779 /* Call waiting tone instead */
780 if (analog_callwait(p)) {
784 if (analog_play_tone(p, ANALOG_SUB_CALLWAIT, ANALOG_TONE_RINGTONE))
785 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
788 n = ast->connected.id.name;
789 l = ast->connected.id.number;
791 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
793 p->lastcid_num[0] = '\0';
795 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
797 p->lastcid_name[0] = '\0';
799 if (p->use_callerid) {
801 p->cid.cid_name = p->lastcid_name;
802 p->cid.cid_num = p->lastcid_num;
805 ast_setstate(ast, AST_STATE_RINGING);
806 index = analog_get_index(ast, p, 0);
808 ast_queue_control(p->subs[index].owner, AST_CONTROL_RINGING);
811 case ANALOG_SIG_FXSLS:
812 case ANALOG_SIG_FXSGS:
813 case ANALOG_SIG_FXSKS:
814 if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
815 ast_debug(1, "Ignore possible polarity reversal on line seizure\n");
816 p->polaritydelaytv = ast_tvnow();
819 case ANALOG_SIG_EMWINK:
821 case ANALOG_SIG_EM_E1:
822 case ANALOG_SIG_FEATD:
823 case ANALOG_SIG_FEATDMF:
824 case ANALOG_SIG_E911:
825 case ANALOG_SIG_FGC_CAMA:
826 case ANALOG_SIG_FGC_CAMAMF:
827 case ANALOG_SIG_FEATB:
828 case ANALOG_SIG_SFWINK:
830 case ANALOG_SIG_SF_FEATD:
831 case ANALOG_SIG_SF_FEATDMF:
832 case ANALOG_SIG_FEATDMF_TA:
833 case ANALOG_SIG_SF_FEATB:
834 c = strchr(dest, '/');
839 if (strlen(c) < p->stripmsd) {
840 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
843 res = analog_start(p);
845 if (errno != EINPROGRESS) {
849 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
850 p->dop.op = ANALOG_DIAL_OP_REPLACE;
855 case ANALOG_SIG_FEATD:
856 l = ast->connected.id.number;
858 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
860 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
862 case ANALOG_SIG_FEATDMF:
863 l = ast->connected.id.number;
865 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
867 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
869 case ANALOG_SIG_FEATDMF_TA:
871 const char *cic = "", *ozz = "";
873 /* If you have to go through a Tandem Access point you need to use this */
875 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
877 ozz = analog_defaultozz;
878 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
880 cic = analog_defaultcic;
883 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
886 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
887 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
891 case ANALOG_SIG_E911:
892 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
894 case ANALOG_SIG_FGC_CAMA:
895 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
897 case ANALOG_SIG_FGC_CAMAMF:
898 case ANALOG_SIG_FEATB:
899 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
903 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
905 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
909 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
910 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
911 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
912 p->echorest[sizeof(p->echorest) - 1] = '\0';
914 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
918 if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
922 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
926 ast_debug(1, "Deferring dialing...\n");
927 analog_set_dialing(p, 1);
928 if (ast_strlen_zero(c))
930 ast_setstate(ast, AST_STATE_DIALING);
933 ast_debug(1, "not yet implemented\n");
939 int analog_hangup(struct analog_pvt *p, struct ast_channel *ast)
944 ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
945 if (!ast->tech_pvt) {
946 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
950 index = analog_get_index(ast, p, 1);
953 if (p->origcid_num) {
954 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
955 free(p->origcid_num);
956 p->origcid_num = NULL;
958 if (p->origcid_name) {
959 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
960 free(p->origcid_name);
961 p->origcid_name = NULL;
964 analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
966 ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
967 p->channel, index, p->subs[ANALOG_SUB_REAL].allocd, p->subs[ANALOG_SUB_CALLWAIT].allocd, p->subs[ANALOG_SUB_THREEWAY].allocd);
969 /* Real channel, do some fixup */
970 p->subs[index].owner = NULL;
971 p->subs[index].needcallerid = 0;
972 p->polarity = POLARITY_IDLE;
973 if (index == ANALOG_SUB_REAL) {
974 if (p->subs[ANALOG_SUB_CALLWAIT].allocd && p->subs[ANALOG_SUB_THREEWAY].allocd) {
975 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
976 if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
977 /* We had flipped over to answer a callwait and now it's gone */
978 ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
979 /* Move to the call-wait, but un-own us until they flip back. */
980 analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
981 analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
984 /* The three way hung up, but we still have a call wait */
985 ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
986 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
987 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
988 if (p->subs[ANALOG_SUB_REAL].inthreeway) {
989 /* This was part of a three way call. Immediately make way for
991 ast_debug(1, "Call was complete, setting owner to former third call\n");
992 p->owner = p->subs[ANALOG_SUB_REAL].owner;
994 /* This call hasn't been completed yet... Set owner to NULL */
995 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
998 p->subs[ANALOG_SUB_REAL].inthreeway = 0;
1000 } else if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
1001 /* Move to the call-wait and switch back to them. */
1002 analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
1003 analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
1004 p->owner = p->subs[ANALOG_SUB_REAL].owner;
1005 if (p->owner->_state != AST_STATE_UP)
1006 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_ANSWER);
1007 if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner))
1008 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
1009 } else if (p->subs[ANALOG_SUB_THREEWAY].allocd) {
1010 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
1011 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1012 if (p->subs[ANALOG_SUB_REAL].inthreeway) {
1013 /* This was part of a three way call. Immediately make way for
1015 ast_debug(1, "Call was complete, setting owner to former third call\n");
1016 p->owner = p->subs[ANALOG_SUB_REAL].owner;
1018 /* This call hasn't been completed yet... Set owner to NULL */
1019 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
1022 p->subs[ANALOG_SUB_REAL].inthreeway = 0;
1024 } else if (index == ANALOG_SUB_CALLWAIT) {
1025 /* Ditch the holding callwait call, and immediately make it availabe */
1026 if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
1027 /* This is actually part of a three way, placed on hold. Place the third part
1028 on music on hold now */
1029 if (p->subs[ANALOG_SUB_THREEWAY].owner && ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
1030 ast_queue_control_data(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CONTROL_HOLD,
1031 S_OR(p->mohsuggest, NULL),
1032 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
1034 p->subs[ANALOG_SUB_THREEWAY].inthreeway = 0;
1035 /* Make it the call wait now */
1036 analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
1037 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1039 analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
1040 } else if (index == ANALOG_SUB_THREEWAY) {
1041 if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
1042 /* The other party of the three way call is currently in a call-wait state.
1043 Start music on hold for them, and take the main guy out of the third call */
1044 if (p->subs[ANALOG_SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[ANALOG_SUB_CALLWAIT].owner)) {
1045 ast_queue_control_data(p->subs[ANALOG_SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
1046 S_OR(p->mohsuggest, NULL),
1047 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
1049 p->subs[ANALOG_SUB_CALLWAIT].inthreeway = 0;
1051 p->subs[ANALOG_SUB_REAL].inthreeway = 0;
1052 /* If this was part of a three way call index, let us make
1053 another three way call */
1054 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1056 /* This wasn't any sort of call, but how are we an index? */
1057 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
1061 if (!p->subs[ANALOG_SUB_REAL].owner && !p->subs[ANALOG_SUB_CALLWAIT].owner && !p->subs[ANALOG_SUB_THREEWAY].owner) {
1065 p->onhooktime = time(NULL);
1068 /* Perform low level hangup if no owner left */
1069 res = analog_on_hook(p);
1071 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
1074 case ANALOG_SIG_FXOGS:
1075 case ANALOG_SIG_FXOLS:
1076 case ANALOG_SIG_FXOKS:
1077 /* If they're off hook, try playing congestion */
1078 if (analog_is_off_hook(p))
1079 analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
1081 analog_play_tone(p, ANALOG_SUB_REAL, -1);
1083 case ANALOG_SIG_FXSGS:
1084 case ANALOG_SIG_FXSLS:
1085 case ANALOG_SIG_FXSKS:
1086 /* Make sure we're not made available for at least two seconds assuming
1087 we were actually used for an inbound or outbound call. */
1088 if (ast->_state != AST_STATE_RESERVED) {
1089 time(&p->guardtime);
1094 analog_play_tone(p, ANALOG_SUB_REAL, -1);
1098 analog_set_echocanceller(p, 0);
1101 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
1102 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
1104 p->callwaiting = p->permcallwaiting;
1105 p->hidecallerid = p->permhidecallerid;
1106 analog_set_dialing(p, 0);
1107 analog_update_conf(p);
1108 analog_all_subchannels_hungup(p);
1111 analog_stop_callwait(p);
1112 ast->tech_pvt = NULL;
1114 ast_verb(3, "Hanging up on '%s'\n", ast->name);
1119 int analog_answer(struct analog_pvt *p, struct ast_channel *ast)
1123 int oldstate = ast->_state;
1124 ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
1125 ast_setstate(ast, AST_STATE_UP);
1126 index = analog_get_index(ast, p, 1);
1128 index = ANALOG_SUB_REAL;
1130 case ANALOG_SIG_FXSLS:
1131 case ANALOG_SIG_FXSGS:
1132 case ANALOG_SIG_FXSKS:
1136 case ANALOG_SIG_EM_E1:
1137 case ANALOG_SIG_EMWINK:
1138 case ANALOG_SIG_FEATD:
1139 case ANALOG_SIG_FEATDMF:
1140 case ANALOG_SIG_FEATDMF_TA:
1141 case ANALOG_SIG_E911:
1142 case ANALOG_SIG_FGC_CAMA:
1143 case ANALOG_SIG_FGC_CAMAMF:
1144 case ANALOG_SIG_FEATB:
1146 case ANALOG_SIG_SFWINK:
1147 case ANALOG_SIG_SF_FEATD:
1148 case ANALOG_SIG_SF_FEATDMF:
1149 case ANALOG_SIG_SF_FEATB:
1150 case ANALOG_SIG_FXOLS:
1151 case ANALOG_SIG_FXOGS:
1152 case ANALOG_SIG_FXOKS:
1153 /* Pick up the line */
1154 ast_debug(1, "Took %s off hook\n", ast->name);
1155 if (p->hanguponpolarityswitch) {
1156 gettimeofday(&p->polaritydelaytv, NULL);
1158 res = analog_off_hook(p);
1159 analog_play_tone(p, index, -1);
1160 analog_set_dialing(p, 0);
1161 if ((index == ANALOG_SUB_REAL) && p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
1162 if (oldstate == AST_STATE_RINGING) {
1163 ast_debug(1, "Finally swapping real and threeway\n");
1164 analog_play_tone(p, ANALOG_SUB_THREEWAY, -1);
1165 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
1166 p->owner = p->subs[ANALOG_SUB_REAL].owner;
1169 if ((p->sig == ANALOG_SIG_FXSLS) || (p->sig == ANALOG_SIG_FXSKS) || (p->sig == ANALOG_SIG_FXSGS)) {
1170 analog_set_echocanceller(p, 1);
1171 analog_train_echocanceller(p);
1175 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
1178 ast_setstate(ast, AST_STATE_UP);
1182 static int analog_handles_digit(struct ast_frame *f)
1184 char subclass = toupper(f->subclass);
1207 void analog_handle_dtmfup(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub index, struct ast_frame **dest)
1209 struct ast_frame *f = *dest;
1211 if (p->callwaitcas) {
1212 if ((f->subclass == 'A') || (f->subclass == 'D')) {
1213 ast_log(LOG_ERROR, "Got some DTMF, but it's for the CAS\n");
1214 p->cid.cid_name = p->callwait_name;
1215 p->cid.cid_num = p->callwait_num;
1216 analog_send_callerid(p, 1, &p->cid);
1218 if (analog_handles_digit(f))
1220 p->subs[index].f.frametype = AST_FRAME_NULL;
1221 p->subs[index].f.subclass = 0;
1222 *dest = &p->subs[index].f;
1224 analog_cb_handle_dtmfup(p, ast, index, dest);
1228 static int analog_my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
1232 *str = 0; /* start with empty output buffer */
1235 /* Wait for the first digit (up to specified ms). */
1236 c = ast_waitfordigit(chan, ms);
1237 /* if timeout, hangup or error, return as such */
1242 if (strchr(term, c))
1247 static int analog_handle_notify_message(struct ast_channel *chan, struct analog_pvt *p, int cid_flags, int neon_mwievent)
1249 if (p->calls->handle_notify_message) {
1250 p->calls->handle_notify_message(chan, p->chan_pvt, cid_flags, neon_mwievent);
1257 static int analog_increase_ss_count(struct analog_pvt *p)
1259 if (p->calls->increase_ss_count) {
1260 p->calls->increase_ss_count();
1266 static int analog_decrease_ss_count(struct analog_pvt *p)
1268 if (p->calls->decrease_ss_count) {
1269 p->calls->decrease_ss_count();
1275 static int analog_distinctive_ring(struct ast_channel *chan, struct analog_pvt *p, int idx, int *ringdata)
1277 if (p->calls->distinctive_ring) {
1278 return p->calls->distinctive_ring(chan, p->chan_pvt, idx, ringdata);
1284 static int analog_set_linear_mode(struct analog_pvt *p, int index, int linear_mode)
1286 if (p->calls->set_linear_mode) {
1287 return p->calls->set_linear_mode(p->chan_pvt, index, linear_mode);
1292 static void analog_get_and_handle_alarms(struct analog_pvt *p)
1294 if (p->calls->get_and_handle_alarms)
1295 return p->calls->get_and_handle_alarms(p->chan_pvt);
1298 static void *analog_get_bridged_channel(struct analog_pvt *p, struct ast_channel *chan)
1300 if (p->calls->get_sigpvt_bridged_channel)
1301 return p->calls->get_sigpvt_bridged_channel;
1306 static int analog_get_sub_fd(struct analog_pvt *p, enum analog_sub sub)
1308 if (p->calls->get_sub_fd) {
1309 return p->calls->get_sub_fd(p->chan_pvt, sub);
1314 #define ANALOG_NEED_MFDETECT(p) (((p)->sig == ANALOG_SIG_FEATDMF) || ((p)->sig == ANALOG_SIG_FEATDMF_TA) || ((p)->sig == ANALOG_SIG_E911) || ((p)->sig == ANALOG_SIG_FGC_CAMA) || ((p)->sig == ANALOG_SIG_FGC_CAMAMF) || ((p)->sig == ANALOG_SIG_FEATB))
1316 static void *__analog_ss_thread(void *data)
1318 struct analog_pvt *p = data;
1319 struct ast_channel *chan = p->ss_astchan;
1320 char exten[AST_MAX_EXTENSION] = "";
1321 char exten2[AST_MAX_EXTENSION] = "";
1324 char namebuf[ANALOG_MAX_CID];
1325 char numbuf[ANALOG_MAX_CID];
1326 struct callerid_state *cs = NULL;
1327 char *name = NULL, *number = NULL;
1336 analog_increase_ss_count(p);
1338 ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
1340 /* in the bizarre case where the channel has become a zombie before we
1341 even get started here, abort safely
1344 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
1349 ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
1350 index = analog_get_index(chan, p, 1);
1352 ast_log(LOG_WARNING, "Huh?\n");
1356 analog_dsp_reset_and_flush_digits(p);
1358 case ANALOG_SIG_FEATD:
1359 case ANALOG_SIG_FEATDMF:
1360 case ANALOG_SIG_FEATDMF_TA:
1361 case ANALOG_SIG_E911:
1362 case ANALOG_SIG_FGC_CAMAMF:
1363 case ANALOG_SIG_FEATB:
1364 case ANALOG_SIG_EMWINK:
1365 case ANALOG_SIG_SF_FEATD:
1366 case ANALOG_SIG_SF_FEATDMF:
1367 case ANALOG_SIG_SF_FEATB:
1368 case ANALOG_SIG_SFWINK:
1369 if (analog_wink(p, index))
1373 case ANALOG_SIG_EM_E1:
1375 case ANALOG_SIG_FGC_CAMA:
1376 res = analog_play_tone(p, index, -1);
1378 analog_dsp_reset_and_flush_digits(p);
1380 if (ANALOG_NEED_MFDETECT(p)) {
1381 analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
1383 analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1385 memset(dtmfbuf, 0, sizeof(dtmfbuf));
1386 /* Wait for the first digit only if immediate=no */
1388 /* Wait for the first digit (up to 5 seconds). */
1389 res = ast_waitfordigit(chan, 5000);
1393 /* save first char */
1396 case ANALOG_SIG_FEATD:
1397 case ANALOG_SIG_SF_FEATD:
1398 res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
1400 res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
1402 analog_dsp_reset_and_flush_digits(p);
1404 case ANALOG_SIG_FEATDMF_TA:
1405 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1407 analog_dsp_reset_and_flush_digits(p);
1408 if (analog_wink(p, index)) goto quit;
1410 /* Wait for the first digit (up to 5 seconds). */
1411 res = ast_waitfordigit(chan, 5000);
1412 if (res <= 0) break;
1414 /* fall through intentionally */
1415 case ANALOG_SIG_FEATDMF:
1416 case ANALOG_SIG_E911:
1417 case ANALOG_SIG_FGC_CAMAMF:
1418 case ANALOG_SIG_SF_FEATDMF:
1419 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1420 /* if international caca, do it again to get real ANO */
1421 if ((p->sig == ANALOG_SIG_FEATDMF) && (dtmfbuf[1] != '0') && (strlen(dtmfbuf) != 14))
1423 if (analog_wink(p, index)) goto quit;
1425 /* Wait for the first digit (up to 5 seconds). */
1426 res = ast_waitfordigit(chan, 5000);
1427 if (res <= 0) break;
1429 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1432 /* if E911, take off hook */
1433 if (p->sig == ANALOG_SIG_E911)
1435 res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
1438 analog_dsp_reset_and_flush_digits(p);
1440 case ANALOG_SIG_FEATB:
1441 case ANALOG_SIG_SF_FEATB:
1442 res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
1444 analog_dsp_reset_and_flush_digits(p);
1446 case ANALOG_SIG_EMWINK:
1447 /* if we received a '*', we are actually receiving Feature Group D
1448 dial syntax, so use that mode; otherwise, fall through to normal
1452 res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
1454 res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
1456 analog_dsp_reset_and_flush_digits(p);
1460 /* If we got the first digit, get the rest */
1462 dtmfbuf[len] = '\0';
1463 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
1464 if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
1465 timeout = analog_matchdigittimeout;
1467 timeout = analog_gendigittimeout;
1469 res = ast_waitfordigit(chan, timeout);
1471 ast_debug(1, "waitfordigit returned < 0...\n");
1475 dtmfbuf[len++] = res;
1476 dtmfbuf[len] = '\0';
1485 ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
1488 } else if (res < 0) {
1489 ast_debug(1, "Got hung up before digits finished\n");
1494 if (p->sig == ANALOG_SIG_FGC_CAMA) {
1497 if (ast_safe_sleep(chan,1000) == -1) {
1502 analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
1503 res = analog_my_getsigstr(chan, anibuf, "#", 10000);
1504 if ((res > 0) && (strlen(anibuf) > 2)) {
1505 if (anibuf[strlen(anibuf) - 1] == '#')
1506 anibuf[strlen(anibuf) - 1] = 0;
1507 ast_set_callerid(chan, anibuf + 2, NULL, anibuf + 2);
1509 analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1512 ast_copy_string(exten, dtmfbuf, sizeof(exten));
1513 if (ast_strlen_zero(exten))
1514 ast_copy_string(exten, "s", sizeof(exten));
1515 if (p->sig == ANALOG_SIG_FEATD || p->sig == ANALOG_SIG_EMWINK) {
1516 /* Look for Feature Group D on all E&M Wink and Feature Group D trunks */
1517 if (exten[0] == '*') {
1519 ast_copy_string(exten2, exten, sizeof(exten2));
1520 /* Parse out extension and callerid */
1522 s1 = strsep(&stringp, "*");
1523 s2 = strsep(&stringp, "*");
1525 if (!ast_strlen_zero(p->cid_num))
1526 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
1528 ast_set_callerid(chan, s1, NULL, s1);
1529 ast_copy_string(exten, s2, sizeof(exten));
1531 ast_copy_string(exten, s1, sizeof(exten));
1532 } else if (p->sig == ANALOG_SIG_FEATD)
1533 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
1535 if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
1536 if (exten[0] == '*') {
1538 ast_copy_string(exten2, exten, sizeof(exten2));
1539 /* Parse out extension and callerid */
1541 s1 = strsep(&stringp, "#");
1542 s2 = strsep(&stringp, "#");
1544 if (!ast_strlen_zero(p->cid_num))
1545 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
1548 ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
1549 ast_copy_string(exten, s2 + 1, sizeof(exten));
1551 ast_copy_string(exten, s1 + 2, sizeof(exten));
1553 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
1555 if ((p->sig == ANALOG_SIG_E911) || (p->sig == ANALOG_SIG_FGC_CAMAMF)) {
1556 if (exten[0] == '*') {
1558 ast_copy_string(exten2, exten, sizeof(exten2));
1559 /* Parse out extension and callerid */
1561 s1 = strsep(&stringp, "#");
1562 s2 = strsep(&stringp, "#");
1563 if (s2 && (*(s2 + 1) == '0')) {
1565 ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
1567 if (s1) ast_copy_string(exten, s1, sizeof(exten));
1568 else ast_copy_string(exten, "911", sizeof(exten));
1570 ast_log(LOG_WARNING, "Got a non-E911/FGC CAMA input on channel %d. Assuming E&M Wink instead\n", p->channel);
1572 if (p->sig == ANALOG_SIG_FEATB) {
1573 if (exten[0] == '*') {
1575 ast_copy_string(exten2, exten, sizeof(exten2));
1576 /* Parse out extension and callerid */
1578 s1 = strsep(&stringp, "#");
1579 ast_copy_string(exten, exten2 + 1, sizeof(exten));
1581 ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d. Assuming E&M Wink instead\n", p->channel);
1583 if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
1584 analog_wink(p, index);
1585 /* some switches require a minimum guard time between
1586 the last FGD wink and something that answers
1587 immediately. This ensures it */
1588 if (ast_safe_sleep(chan,100)) goto quit;
1590 analog_set_echocanceller(p, 1);
1592 analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
1594 if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) {
1595 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
1596 analog_dsp_reset_and_flush_digits(p);
1597 res = ast_pbx_run(chan);
1599 ast_log(LOG_WARNING, "PBX exited non-zero\n");
1600 res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
1604 ast_verb(3, "Unknown extension '%s' in context '%s' requested\n", exten, chan->context);
1606 res = analog_play_tone(p, index, ANALOG_TONE_INFO);
1608 ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
1611 res = ast_streamfile(chan, "ss-noservice", chan->language);
1613 ast_waitstream(chan, "");
1614 res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
1619 case ANALOG_SIG_FXOLS:
1620 case ANALOG_SIG_FXOGS:
1621 case ANALOG_SIG_FXOKS:
1622 /* Read the first digit */
1623 timeout = analog_firstdigittimeout;
1624 /* If starting a threeway call, never timeout on the first digit so someone
1625 can use flash-hook as a "hold" feature */
1626 if (p->subs[ANALOG_SUB_THREEWAY].owner)
1628 while (len < AST_MAX_EXTENSION-1) {
1629 /* Read digit unless it's supposed to be immediate, in which case the
1630 only answer is 's' */
1634 res = ast_waitfordigit(chan, timeout);
1637 ast_debug(1, "waitfordigit returned < 0...\n");
1638 res = analog_play_tone(p, index, -1);
1645 if (!ast_ignore_pattern(chan->context, exten))
1646 analog_play_tone(p, index, -1);
1648 analog_play_tone(p, index, ANALOG_TONE_DIALTONE);
1649 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) {
1650 if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
1652 /* Record this as the forwarding extension */
1653 ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
1654 ast_verb(3, "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
1655 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1659 res = analog_play_tone(p, index, -1);
1661 memset(exten, 0, sizeof(exten));
1662 res = analog_play_tone(p, index, ANALOG_TONE_DIALTONE);
1666 res = analog_play_tone(p, index, -1);
1667 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
1668 if (!ast_strlen_zero(p->cid_num)) {
1669 if (!p->hidecallerid)
1670 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
1672 ast_set_callerid(chan, NULL, NULL, p->cid_num);
1674 if (!ast_strlen_zero(p->cid_name)) {
1675 if (!p->hidecallerid)
1676 ast_set_callerid(chan, NULL, p->cid_name, NULL);
1678 ast_setstate(chan, AST_STATE_RING);
1679 analog_set_echocanceller(p, 1);
1680 res = ast_pbx_run(chan);
1682 ast_log(LOG_WARNING, "PBX exited non-zero\n");
1683 res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
1688 /* It's a match, but they just typed a digit, and there is an ambiguous match,
1689 so just set the timeout to analog_matchdigittimeout and wait some more */
1690 timeout = analog_matchdigittimeout;
1692 } else if (res == 0) {
1693 ast_debug(1, "not enough digits (and no ambiguous match)...\n");
1694 res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
1695 analog_wait_event(p);
1698 } else if (p->callwaiting && !strcmp(exten, "*70")) {
1699 ast_verb(3, "Disabling call waiting on %s\n", chan->name);
1700 /* Disable call waiting if enabled */
1702 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1704 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
1705 chan->name, strerror(errno));
1708 memset(exten, 0, sizeof(exten));
1709 timeout = analog_firstdigittimeout;
1711 } else if (!strcmp(exten,ast_pickup_ext())) {
1712 /* Scan all channels and see if there are any
1713 * ringing channels that have call groups
1714 * that equal this channels pickup group
1716 if (index == ANALOG_SUB_REAL) {
1717 /* Switch us from Third call to Call Wait */
1718 if (p->subs[ANALOG_SUB_THREEWAY].owner) {
1719 /* If you make a threeway call and the *8# a call, it should actually
1720 look like a callwait */
1721 analog_alloc_sub(p, ANALOG_SUB_CALLWAIT);
1722 analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
1723 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1725 analog_set_echocanceller(p, 1);
1726 if (ast_pickup_call(chan)) {
1727 ast_debug(1, "No call pickup possible...\n");
1728 res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
1729 analog_wait_event(p);
1734 ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n");
1738 } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
1739 ast_verb(3, "Disabling Caller*ID on %s\n", chan->name);
1740 /* Disable Caller*ID if enabled */
1741 p->hidecallerid = 1;
1742 if (chan->cid.cid_num)
1743 free(chan->cid.cid_num);
1744 chan->cid.cid_num = NULL;
1745 if (chan->cid.cid_name)
1746 free(chan->cid.cid_name);
1747 chan->cid.cid_name = NULL;
1748 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1750 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
1751 chan->name, strerror(errno));
1754 memset(exten, 0, sizeof(exten));
1755 timeout = analog_firstdigittimeout;
1756 } else if (p->callreturn && !strcmp(exten, "*69")) {
1758 if (!ast_strlen_zero(p->lastcid_num)) {
1759 res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language);
1762 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1764 } else if (!strcmp(exten, "*78")) {
1765 /* Do not disturb */
1766 ast_verb(3, "Enabled DND on channel %d\n", p->channel);
1767 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
1768 "Channel: DAHDI/%d\r\n"
1769 "Status: enabled\r\n", p->channel);
1770 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1773 memset(exten, 0, sizeof(exten));
1775 } else if (!strcmp(exten, "*79")) {
1776 /* Do not disturb */
1777 ast_verb(3, "Disabled DND on channel %d\n", p->channel);
1778 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
1779 "Channel: DAHDI/%d\r\n"
1780 "Status: disabled\r\n", p->channel);
1781 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1784 memset(exten, 0, sizeof(exten));
1786 } else if (p->cancallforward && !strcmp(exten, "*72")) {
1787 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1789 memset(exten, 0, sizeof(exten));
1791 } else if (p->cancallforward && !strcmp(exten, "*73")) {
1792 ast_verb(3, "Cancelling call forwarding on channel %d\n", p->channel);
1793 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1794 memset(p->call_forward, 0, sizeof(p->call_forward));
1796 memset(exten, 0, sizeof(exten));
1798 } else if ((p->transfer || p->canpark) && !strcmp(exten, ast_parking_ext()) &&
1799 p->subs[ANALOG_SUB_THREEWAY].owner &&
1800 ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
1801 /* This is a three way call, the main call being a real channel,
1802 and we're parking the first call. */
1803 ast_masq_park_call(ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner), chan, 0, NULL);
1804 ast_verb(3, "Parking call to '%s'\n", chan->name);
1806 } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
1807 ast_verb(3, "Blacklisting number %s\n", p->lastcid_num);
1808 res = ast_db_put("blacklist", p->lastcid_num, "1");
1810 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1811 memset(exten, 0, sizeof(exten));
1814 } else if (p->hidecallerid && !strcmp(exten, "*82")) {
1815 ast_verb(3, "Enabling Caller*ID on %s\n", chan->name);
1816 /* Enable Caller*ID if enabled */
1817 p->hidecallerid = 0;
1818 if (chan->cid.cid_num)
1819 free(chan->cid.cid_num);
1820 chan->cid.cid_num = NULL;
1821 if (chan->cid.cid_name)
1822 free(chan->cid.cid_name);
1823 chan->cid.cid_name = NULL;
1824 ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
1825 res = analog_play_tone(p, index, ANALOG_TONE_DIALRECALL);
1827 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
1828 chan->name, strerror(errno));
1831 memset(exten, 0, sizeof(exten));
1832 timeout = analog_firstdigittimeout;
1833 } else if (!strcmp(exten, "*0")) {
1834 struct ast_channel *nbridge = p->subs[ANALOG_SUB_THREEWAY].owner;
1835 struct analog_pvt *pbridge = NULL;
1836 /* set up the private struct of the bridged one, if any */
1837 if (nbridge && ast_bridged_channel(nbridge))
1838 pbridge = analog_get_bridged_channel(p, nbridge);
1839 if (nbridge && pbridge &&
1840 (nbridge->tech == p->chan_tech) &&
1841 (ast_bridged_channel(nbridge)->tech == p->chan_tech) &&
1843 /* Clear out the dial buffer */
1844 p->dop.dialstr[0] = '\0';
1845 /* flash hookswitch */
1846 if ((analog_flash(p) == -1) && (errno != EINPROGRESS)) {
1847 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
1848 nbridge->name, strerror(errno));
1850 analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
1851 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1852 p->owner = p->subs[ANALOG_SUB_REAL].owner;
1853 if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner))
1854 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
1858 analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
1859 analog_wait_event(p);
1860 analog_play_tone(p, index, -1);
1861 analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
1862 analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
1863 p->owner = p->subs[ANALOG_SUB_REAL].owner;
1867 } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
1868 ((exten[0] != '*') || (strlen(exten) > 2))) {
1869 ast_debug(1, "Can't match %s from '%s' in context %s\n", exten, chan->cid.cid_num ? chan->cid.cid_num : "<Unknown Caller>", chan->context);
1873 timeout = analog_gendigittimeout;
1874 if (len && !ast_ignore_pattern(chan->context, exten))
1875 analog_play_tone(p, index, -1);
1878 case ANALOG_SIG_FXSLS:
1879 case ANALOG_SIG_FXSGS:
1880 case ANALOG_SIG_FXSKS:
1882 /* If we want caller id, we're in a prering state due to a polarity reversal
1883 * and we're set to use a polarity reversal to trigger the start of caller id,
1884 * grab the caller id and wait for ringing to start... */
1885 if (p->use_callerid && (chan->_state == AST_STATE_PRERING && (p->cid_start == ANALOG_CID_START_POLARITY || p->cid_start == ANALOG_CID_START_POLARITY_IN))) {
1886 /* If set to use DTMF CID signalling, listen for DTMF */
1887 if (p->cid_signalling == CID_SIG_DTMF) {
1890 ast_debug(1, "Receiving DTMF cid on "
1891 "channel %s\n", chan->name);
1893 analog_set_linear_mode(p, index, 0);
1897 struct ast_frame *f;
1898 res = ast_waitfor(chan, res);
1900 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
1901 "Exiting simple switch\n");
1905 if (!(f = ast_read(chan)))
1907 if (f->frametype == AST_FRAME_DTMF) {
1908 dtmfbuf[i++] = f->subclass;
1909 ast_debug(1, "CID got digit '%c'\n", f->subclass);
1913 if (chan->_state == AST_STATE_RING ||
1914 chan->_state == AST_STATE_RINGING)
1915 break; /* Got ring */
1919 analog_set_linear_mode(p, index, 1);
1921 /* Got cid and ring. */
1922 ast_debug(1, "CID got string '%s'\n", dtmfbuf);
1923 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
1924 ast_debug(1, "CID is '%s', flags %d\n",
1926 /* If first byte is NULL, we have no cid */
1927 if (!ast_strlen_zero(dtmfcid))
1932 /* If set to use V23 Signalling, launch our FSK gubbins and listen for it */
1933 } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) {
1934 int timeout = 10000; /* Ten seconds */
1935 struct timeval start = ast_tvnow();
1936 enum analog_event ev;
1941 if (!analog_start_cid_detect(p, p->cid_signalling)) {
1943 res = analog_get_callerid(p, namebuf, numbuf, &ev, timeout - ast_tvdiff_ms(ast_tvnow(), start));
1950 if (p->cid_signalling == CID_SIG_V23_JP) {
1951 if (ev == ANALOG_EVENT_RINGBEGIN) {
1956 ev = ANALOG_EVENT_NONE;
1961 if (ast_tvdiff_ms(ast_tvnow(), start) > timeout)
1968 analog_stop_cid_detect(p);
1970 if (p->cid_signalling == CID_SIG_V23_JP) {
1971 res = analog_on_hook(p);
1975 /* Finished with Caller*ID, now wait for a ring to make sure there really is a call coming */
1980 struct ast_frame *f;
1981 res = ast_waitfor(chan, res);
1983 ast_log(LOG_WARNING, "CID timed out waiting for ring. "
1984 "Exiting simple switch\n");
1988 if (!(f = ast_read(chan))) {
1989 ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n");
1994 if (chan->_state == AST_STATE_RING ||
1995 chan->_state == AST_STATE_RINGING)
1996 break; /* Got ring */
1999 if (analog_distinctive_ring(chan, p, index, NULL))
2003 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
2006 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
2009 ast_log(LOG_WARNING, "Channel %s in prering "
2010 "state, but I have nothing to do. "
2011 "Terminating simple switch, should be "
2012 "restarted by the actual ring.\n",
2017 } else if (p->use_callerid && p->cid_start == ANALOG_CID_START_RING) {
2018 int timeout = 10000; /* Ten seconds */
2019 struct timeval start = ast_tvnow();
2020 enum analog_event ev;
2021 int curRingData[3] = { 0 };
2022 int receivedRingT = 0;
2027 if (!analog_start_cid_detect(p, p->cid_signalling)) {
2029 res = analog_get_callerid(p, namebuf, numbuf, &ev, timeout - ast_tvdiff_ms(ast_tvnow(), start));
2035 if (res == 1 || res == 2) {
2036 if (ev == ANALOG_EVENT_POLARITY && p->hanguponpolarityswitch && p->polarity == POLARITY_REV) {
2037 ast_debug(1, "Hanging up due to polarity reversal on channel %d while detecting callerid\n", p->channel);
2038 p->polarity = POLARITY_IDLE;
2041 } else if (ev != ANALOG_EVENT_NONE && ev != ANALOG_EVENT_RINGBEGIN && ev != ANALOG_EVENT_RINGOFFHOOK) {
2045 /* Let us detect callerid when the telco uses distinctive ring */
2046 curRingData[receivedRingT] = p->ringt;
2048 if (p->ringt < p->ringt_base/2) {
2051 /* Increment the ringT counter so we can match it against
2052 values in chan_dahdi.conf for distinctive ring */
2053 if (++receivedRingT == ARRAY_LEN(curRingData)) {
2059 if (ast_tvdiff_ms(ast_tvnow(), start) > timeout) {
2067 analog_stop_cid_detect(p);
2069 if (analog_distinctive_ring(chan, p, index, curRingData))
2073 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
2076 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
2082 ast_shrink_phone_number(number);
2083 ast_set_callerid(chan, number, name, number);
2088 analog_handle_notify_message(chan, p, flags, -1);
2090 ast_setstate(chan, AST_STATE_RING);
2092 p->ringt = p->ringt_base;
2093 res = ast_pbx_run(chan);
2096 ast_log(LOG_WARNING, "PBX exited non-zero\n");
2100 ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", analog_sigtype_to_str(p->sig), p->channel);
2101 res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
2103 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
2105 res = analog_play_tone(p, index, ANALOG_TONE_CONGESTION);
2107 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
2110 analog_decrease_ss_count(p);
2114 int analog_ss_thread_start(struct analog_pvt *p, struct ast_channel *chan)
2117 return ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, chan);
2120 static struct ast_frame *__analog_handle_event(struct analog_pvt *p, struct ast_channel *ast)
2124 enum analog_sub index;
2127 pthread_attr_t attr;
2128 struct ast_channel *chan;
2129 struct ast_frame *f;
2130 ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
2132 index = analog_get_index(ast, p, 0);
2134 if (p->outsigmod > -1)
2135 mysig = p->outsigmod;
2136 p->subs[index].f.frametype = AST_FRAME_NULL;
2137 p->subs[index].f.subclass = 0;
2138 p->subs[index].f.datalen = 0;
2139 p->subs[index].f.samples = 0;
2140 p->subs[index].f.mallocd = 0;
2141 p->subs[index].f.offset = 0;
2142 p->subs[index].f.src = "dahdi_handle_event";
2143 p->subs[index].f.data.ptr = NULL;
2144 f = &p->subs[index].f;
2147 return &p->subs[index].f;
2149 if (index != ANALOG_SUB_REAL) {
2150 ast_log(LOG_ERROR, "We got an event on a non real sub. Fix it!\n");
2153 res = analog_get_event(p);
2155 ast_debug(1, "Got event %s(%d) on channel %d (index %d)\n", analog_event2str(res), res, p->channel, index);
2158 #ifdef ANALOG_EVENT_EC_DISABLED
2159 case ANALOG_EVENT_EC_DISABLED:
2160 ast_verb(3, "Channel %d echo canceler disabled due to CED detection\n", p->channel);
2164 case ANALOG_EVENT_PULSE_START:
2165 /* Stop tone if there's a pulse start and the PBX isn't started */
2167 analog_play_tone(p, ANALOG_SUB_REAL, -1);
2169 case ANALOG_EVENT_DIALCOMPLETE:
2170 if (p->inalarm) break;
2171 x = analog_is_dialing(p, index);
2172 if (!x) { /* if not still dialing in driver */
2173 analog_set_echocanceller(p, 1);
2175 analog_train_echocanceller(p);
2176 ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
2177 p->dop.op = ANALOG_DIAL_OP_REPLACE;
2178 analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
2181 analog_set_dialing(p, 0);
2182 if ((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) {
2183 /* if thru with dialing after offhook */
2184 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
2185 ast_setstate(ast, AST_STATE_UP);
2186 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2187 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2189 } else { /* if to state wait for offhook to dial rest */
2190 /* we now wait for off hook */
2191 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
2194 if (ast->_state == AST_STATE_DIALING) {
2195 if ((!p->dialednone && ((mysig == ANALOG_SIG_EM) || (mysig == ANALOG_SIG_EM_E1) || (mysig == ANALOG_SIG_EMWINK) || (mysig == ANALOG_SIG_FEATD) || (mysig == ANALOG_SIG_FEATDMF_TA) || (mysig == ANALOG_SIG_FEATDMF) || (mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF) || (mysig == ANALOG_SIG_FEATB) || (mysig == ANALOG_SIG_SF) || (mysig == ANALOG_SIG_SFWINK) || (mysig == ANALOG_SIG_SF_FEATD) || (mysig == ANALOG_SIG_SF_FEATDMF) || (mysig == ANALOG_SIG_SF_FEATB)))) {
2196 ast_setstate(ast, AST_STATE_RINGING);
2197 } else if (!p->answeronpolarityswitch) {
2198 ast_setstate(ast, AST_STATE_UP);
2199 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2200 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2201 /* If aops=0 and hops=1, this is necessary */
2202 p->polarity = POLARITY_REV;
2204 /* Start clean, so we can catch the change to REV polarity when party answers */
2205 p->polarity = POLARITY_IDLE;
2211 case ANALOG_EVENT_ALARM:
2213 analog_get_and_handle_alarms(p);
2215 case ANALOG_EVENT_ONHOOK:
2217 case ANALOG_SIG_FXOLS:
2218 case ANALOG_SIG_FXOGS:
2219 case ANALOG_SIG_FXOKS:
2220 p->fxsoffhookstate = 0;
2221 p->onhooktime = time(NULL);
2223 /* Check for some special conditions regarding call waiting */
2224 if (index == ANALOG_SUB_REAL) {
2225 /* The normal line was hung up */
2226 if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
2227 /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
2228 analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
2229 ast_verb(3, "Channel %d still has (callwait) call, ringing phone\n", p->channel);
2230 analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
2231 analog_stop_callwait(p);
2233 /* Don't start streaming audio yet if the incoming call isn't up yet */
2234 if (p->subs[ANALOG_SUB_REAL].owner->_state != AST_STATE_UP)
2235 analog_set_dialing(p, 1);
2237 } else if (p->subs[ANALOG_SUB_THREEWAY].owner) {
2238 unsigned int mssinceflash;
2239 /* Here we have to retain the lock on both the main channel, the 3-way channel, and
2240 the private structure -- not especially easy or clean */
2241 while (p->subs[ANALOG_SUB_THREEWAY].owner && ast_channel_trylock(p->subs[ANALOG_SUB_THREEWAY].owner)) {
2242 /* Yuck, didn't get the lock on the 3-way, gotta release everything and re-grab! */
2243 analog_unlock_private(p);
2244 CHANNEL_DEADLOCK_AVOIDANCE(ast);
2245 /* We can grab ast and p in that order, without worry. We should make sure
2246 nothing seriously bad has happened though like some sort of bizarre double
2248 analog_lock_private(p);
2249 if (p->owner != ast) {
2250 ast_log(LOG_WARNING, "This isn't good...\n");
2254 if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
2255 ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
2258 mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
2259 ast_debug(1, "Last flash was %d ms ago\n", mssinceflash);
2260 if (mssinceflash < MIN_MS_SINCE_FLASH) {
2261 /* It hasn't been long enough since the last flashook. This is probably a bounce on
2262 hanging up. Hangup both channels now */
2263 if (p->subs[ANALOG_SUB_THREEWAY].owner)
2264 ast_queue_hangup_with_cause(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CAUSE_NO_ANSWER);
2265 ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
2266 ast_debug(1, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
2267 ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
2268 } else if ((ast->pbx) || (ast->_state == AST_STATE_UP)) {
2270 /* Only attempt transfer if the phone is ringing; why transfer to busy tone eh? */
2271 if (!p->transfertobusy && ast->_state == AST_STATE_BUSY) {
2272 ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
2273 /* Swap subs and dis-own channel */
2274 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
2276 /* Ring the phone */
2279 if ((res = analog_attempt_transfer(p)) < 0) {
2280 ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
2281 if (p->subs[ANALOG_SUB_THREEWAY].owner)
2282 ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
2284 /* this isn't a threeway call anymore */
2285 p->subs[ANALOG_SUB_REAL].inthreeway = 0;
2286 p->subs[ANALOG_SUB_THREEWAY].inthreeway = 0;
2288 /* Don't actually hang up at this point */
2289 if (p->subs[ANALOG_SUB_THREEWAY].owner)
2290 ast_channel_unlock(&p->subs[ANALOG_SUB_THREEWAY].owner);
2294 /* this isn't a threeway call anymore */
2295 p->subs[ANALOG_SUB_REAL].inthreeway = 0;
2296 p->subs[ANALOG_SUB_THREEWAY].inthreeway = 0;
2298 ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
2299 if (p->subs[ANALOG_SUB_THREEWAY].owner)
2300 ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
2303 ast_cel_report_event(ast, AST_CEL_BLINDTRANSFER, NULL, ast->linkedid, NULL);
2304 ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
2305 /* Swap subs and dis-own channel */
2306 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
2308 /* Ring the phone */
2313 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
2317 analog_set_echocanceller(p, 0);
2321 case ANALOG_EVENT_RINGOFFHOOK:
2322 if (p->inalarm) break;
2323 /* for E911, its supposed to wait for offhook then dial
2324 the second half of the dial string */
2325 if (((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
2326 c = strchr(p->dialdest, '/');
2331 if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
2332 else ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
2333 if (strlen(p->dop.dialstr) > 4) {
2334 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2335 strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2336 p->echorest[sizeof(p->echorest) - 1] = '\0';
2338 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2341 if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
2342 int saveerr = errno;
2344 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
2347 analog_set_dialing(p, 1);
2348 return &p->subs[index].f;
2351 case ANALOG_SIG_FXOLS:
2352 case ANALOG_SIG_FXOGS:
2353 case ANALOG_SIG_FXOKS:
2354 p->fxsoffhookstate = 1;
2355 switch (ast->_state) {
2356 case AST_STATE_RINGING:
2357 analog_set_echocanceller(p, 1);
2358 analog_train_echocanceller(p);
2359 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2360 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2361 /* Make sure it stops ringing */
2363 ast_debug(1, "channel %d answered\n", p->channel);
2364 analog_set_dialing(p, 0);
2366 if (!ast_strlen_zero(p->dop.dialstr)) {
2367 /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */
2368 res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
2370 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
2371 p->dop.dialstr[0] = '\0';
2374 ast_debug(1, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
2375 p->subs[index].f.frametype = AST_FRAME_NULL;
2376 p->subs[index].f.subclass = 0;
2377 analog_set_dialing(p, 1);
2379 p->dop.dialstr[0] = '\0';
2380 ast_setstate(ast, AST_STATE_DIALING);
2382 ast_setstate(ast, AST_STATE_UP);
2383 return &p->subs[index].f;
2384 case AST_STATE_DOWN:
2385 ast_setstate(ast, AST_STATE_RING);
2387 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2388 p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
2389 ast_debug(1, "channel %d picked up\n", p->channel);
2390 return &p->subs[index].f;
2392 /* Make sure it stops ringing */
2394 /* Okay -- probably call waiting*/
2395 if (ast_bridged_channel(p->owner))
2396 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
2398 case AST_STATE_RESERVED:
2399 /* Start up dialtone */
2400 if (analog_has_voicemail(p))
2401 res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_STUTTER);
2403 res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_DIALTONE);
2406 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
2409 case ANALOG_SIG_FXSLS:
2410 case ANALOG_SIG_FXSGS:
2411 case ANALOG_SIG_FXSKS:
2412 if (ast->_state == AST_STATE_RING) {
2413 p->ringt = p->ringt_base;
2418 case ANALOG_SIG_EM_E1:
2419 case ANALOG_SIG_EMWINK:
2420 case ANALOG_SIG_FEATD:
2421 case ANALOG_SIG_FEATDMF:
2422 case ANALOG_SIG_FEATDMF_TA:
2423 case ANALOG_SIG_E911:
2424 case ANALOG_SIG_FGC_CAMA:
2425 case ANALOG_SIG_FGC_CAMAMF:
2426 case ANALOG_SIG_FEATB:
2428 case ANALOG_SIG_SFWINK:
2429 case ANALOG_SIG_SF_FEATD:
2430 case ANALOG_SIG_SF_FEATDMF:
2431 case ANALOG_SIG_SF_FEATB:
2432 if (ast->_state == AST_STATE_PRERING)
2433 ast_setstate(ast, AST_STATE_RING);
2434 if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
2435 ast_debug(1, "Ring detected\n");
2436 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2437 p->subs[index].f.subclass = AST_CONTROL_RING;
2438 } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
2439 ast_debug(1, "Line answered\n");
2440 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2441 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2442 ast_setstate(ast, AST_STATE_UP);
2443 } else if (ast->_state != AST_STATE_RING)
2444 ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
2447 ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
2450 #ifdef ANALOG_EVENT_RINGBEGIN
2451 case ANALOG_EVENT_RINGBEGIN:
2453 case ANALOG_SIG_FXSLS:
2454 case ANALOG_SIG_FXSGS:
2455 case ANALOG_SIG_FXSKS:
2456 if (ast->_state == AST_STATE_RING) {
2457 p->ringt = p->ringt_base;
2463 case ANALOG_EVENT_RINGEROFF:
2464 if (p->inalarm) break;
2466 if (ast->rings == p->cidrings) {
2467 analog_send_callerid(p, 0, &p->cid);
2470 if (ast->rings > p->cidrings) {
2473 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2474 p->subs[index].f.subclass = AST_CONTROL_RINGING;
2476 case ANALOG_EVENT_RINGERON:
2478 case ANALOG_EVENT_NOALARM:
2480 if (!p->unknown_alarm) {
2481 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
2482 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
2483 "Channel: %d\r\n", p->channel);
2485 p->unknown_alarm = 0;
2488 case ANALOG_EVENT_WINKFLASH:
2489 if (p->inalarm) break;
2490 /* Remember last time we got a flash-hook */
2491 gettimeofday(&p->flashtime, NULL);
2493 case ANALOG_SIG_FXOLS:
2494 case ANALOG_SIG_FXOGS:
2495 case ANALOG_SIG_FXOKS:
2496 ast_debug(1, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
2497 index, analog_get_sub_fd(p, ANALOG_SUB_REAL), analog_get_sub_fd(p, ANALOG_SUB_CALLWAIT), analog_get_sub_fd(p, ANALOG_SUB_THREEWAY));
2501 if (index != ANALOG_SUB_REAL) {
2502 ast_log(LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", index, p->channel);
2506 if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
2507 /* Swap to call-wait */
2508 int previous_state = p->subs[ANALOG_SUB_CALLWAIT].owner->_state;
2509 if (p->subs[ANALOG_SUB_CALLWAIT].owner->_state == AST_STATE_RINGING) {
2510 ast_setstate(p->subs[ANALOG_SUB_CALLWAIT].owner, AST_STATE_UP);
2512 analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_CALLWAIT);
2513 analog_play_tone(p, ANALOG_SUB_REAL, -1);
2514 p->owner = p->subs[ANALOG_SUB_REAL].owner;
2515 ast_debug(1, "Making %s the new owner\n", p->owner->name);
2516 if (previous_state == AST_STATE_RINGING) {
2517 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_ANSWER);
2519 analog_stop_callwait(p);
2520 /* Start music on hold if appropriate */
2521 if (!p->subs[ANALOG_SUB_CALLWAIT].inthreeway && ast_bridged_channel(p->subs[ANALOG_SUB_CALLWAIT].owner)) {
2522 ast_queue_control_data(p->subs[ANALOG_SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
2523 S_OR(p->mohsuggest, NULL),
2524 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2526 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_ANSWER);
2527 if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner)) {
2528 ast_queue_control_data(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_HOLD,
2529 S_OR(p->mohsuggest, NULL),
2530 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2532 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
2533 } else if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
2537 if (!p->threewaycalling) {
2538 /* Just send a flash if no 3-way calling */
2539 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_FLASH);
2541 } else if (!analog_check_for_conference(p)) {
2542 if (p->dahditrcallerid && p->owner) {
2543 if (p->owner->cid.cid_num)
2544 ast_copy_string(cid_num, p->owner->cid.cid_num, sizeof(cid_num));
2545 if (p->owner->cid.cid_name)
2546 ast_copy_string(cid_name, p->owner->cid.cid_name, sizeof(cid_name));
2548 /* XXX This section needs much more error checking!!! XXX */
2549 /* Start a 3-way call if feasible */
2551 (ast->_state == AST_STATE_UP) ||
2552 (ast->_state == AST_STATE_RING))) {
2553 ast_debug(1, "Flash when call not up or ringing\n");
2556 if (analog_alloc_sub(p, ANALOG_SUB_THREEWAY)) {
2557 ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
2560 /* Make new channel */
2561 chan = analog_new_ast_channel(p, AST_STATE_RESERVED, 0, ANALOG_SUB_THREEWAY, NULL);
2562 if (p->dahditrcallerid) {
2563 if (!p->origcid_num)
2564 p->origcid_num = ast_strdup(p->cid_num);
2565 if (!p->origcid_name)
2566 p->origcid_name = ast_strdup(p->cid_name);
2567 ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
2568 ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
2570 /* Swap things around between the three-way and real call */
2571 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
2572 /* Disable echo canceller for better dialing */
2573 analog_set_echocanceller(p, 0);
2574 res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_DIALRECALL);
2576 ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
2577 p->ss_astchan = p->owner = chan;
2578 pthread_attr_init(&attr);
2579 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2581 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", p->channel);
2582 } else if (ast_pthread_create(&threadid, &attr, __analog_ss_thread, p)) {
2583 ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
2584 res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
2585 analog_set_echocanceller(p, 1);
2588 struct ast_channel *other = ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner);
2589 int way3bridge = 0, cdr3way = 0;
2592 other = ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner);
2596 if (p->subs[ANALOG_SUB_THREEWAY].owner->cdr)
2599 ast_verb(3, "Started three way call on channel %d\n", p->channel);
2600 /* Start music on hold if appropriate */
2601 if (ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
2602 ast_queue_control_data(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CONTROL_HOLD,
2603 S_OR(p->mohsuggest, NULL),
2604 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2607 pthread_attr_destroy(&attr);
2610 /* Already have a 3 way call */
2611 if (p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
2612 /* Call is already up, drop the last person */
2613 ast_debug(1, "Got flash with three way call up, dropping last call on %d\n", p->channel);
2614 /* If the primary call isn't answered yet, use it */
2615 if ((p->subs[ANALOG_SUB_REAL].owner->_state != AST_STATE_UP) && (p->subs[ANALOG_SUB_THREEWAY].owner->_state == AST_STATE_UP)) {
2616 /* Swap back -- we're dropping the real 3-way that isn't finished yet*/
2617 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
2618 p->owner = p->subs[ANALOG_SUB_REAL].owner;
2620 /* Drop the last call and stop the conference */
2621 ast_verb(3, "Dropping three-way call on %s\n", p->subs[ANALOG_SUB_THREEWAY].owner->name);
2622 ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
2623 p->subs[ANALOG_SUB_REAL].inthreeway = 0;
2624 p->subs[ANALOG_SUB_THREEWAY].inthreeway = 0;
2626 /* Lets see what we're up to */
2627 if (((ast->pbx) || (ast->_state == AST_STATE_UP)) &&
2628 (p->transfertobusy || (ast->_state != AST_STATE_BUSY))) {
2629 int otherindex = ANALOG_SUB_THREEWAY;
2630 struct ast_channel *other = ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner);
2631 int way3bridge = 0, cdr3way = 0;
2634 other = ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner);
2638 if (p->subs[ANALOG_SUB_THREEWAY].owner->cdr)
2641 ast_verb(3, "Building conference on call on %s and %s\n", p->subs[ANALOG_SUB_THREEWAY].owner->name, p->subs[ANALOG_SUB_REAL].owner->name);
2642 /* Put them in the threeway, and flip */
2643 p->subs[ANALOG_SUB_THREEWAY].inthreeway = 1;
2644 p->subs[ANALOG_SUB_REAL].inthreeway = 1;
2645 if (ast->_state == AST_STATE_UP) {
2646 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
2647 otherindex = ANALOG_SUB_REAL;
2649 if (p->subs[otherindex].owner && ast_bridged_channel(p->subs[otherindex].owner))
2650 ast_queue_control(p->subs[otherindex].owner, AST_CONTROL_UNHOLD);
2651 p->owner = p->subs[ANALOG_SUB_REAL].owner;
2652 if (ast->_state == AST_STATE_RINGING) {
2653 ast_debug(1, "Enabling ringtone on real and threeway\n");
2654 analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_RINGTONE);
2655 analog_play_tone(p, ANALOG_SUB_THREEWAY, ANALOG_TONE_RINGTONE);
2658 ast_verb(3, "Dumping incomplete call on on %s\n", p->subs[ANALOG_SUB_THREEWAY].owner->name);
2659 analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
2660 ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
2661 p->owner = p->subs[ANALOG_SUB_REAL].owner;
2662 if (p->subs[ANALOG_SUB_REAL].owner && ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner))
2663 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
2664 analog_set_echocanceller(p, 1);
2669 analog_update_conf(p);
2672 case ANALOG_SIG_EM_E1:
2673 case ANALOG_SIG_FEATD:
2675 case ANALOG_SIG_SFWINK:
2676 case ANALOG_SIG_SF_FEATD:
2677 case ANALOG_SIG_FXSLS:
2678 case ANALOG_SIG_FXSGS:
2680 ast_debug(1, "Ignoring wink on channel %d\n", p->channel);
2682 ast_debug(1, "Got wink in weird state %d on channel %d\n", ast->_state, p->channel);
2684 case ANALOG_SIG_FEATDMF_TA:
2685 switch (p->whichwink) {
2687 ast_debug(1, "ANI2 set to '%d' and ANI is '%s'\n", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
2688 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
2691 ast_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr));
2694 ast_log(LOG_WARNING, "Received unexpected wink on channel of type ANALOG_SIG_FEATDMF_TA\n");
2699 case ANALOG_SIG_FEATDMF:
2700 case ANALOG_SIG_E911:
2701 case ANALOG_SIG_FGC_CAMAMF:
2702 case ANALOG_SIG_FGC_CAMA:
2703 case ANALOG_SIG_FEATB:
2704 case ANALOG_SIG_SF_FEATDMF:
2705 case ANALOG_SIG_SF_FEATB:
2706 case ANALOG_SIG_EMWINK:
2707 /* FGD MF and EMWINK *Must* wait for wink */
2708 if (!ast_strlen_zero(p->dop.dialstr)) {
2709 res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
2711 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
2712 p->dop.dialstr[0] = '\0';
2715 ast_debug(1, "Sent deferred digit string on channel %d: %s\n", p->channel, p->dop.dialstr);
2717 p->dop.dialstr[0] = '\0';
2720 ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
2723 case ANALOG_EVENT_HOOKCOMPLETE:
2724 if (p->inalarm) break;
2726 case ANALOG_SIG_FXSLS: /* only interesting for FXS */
2727 case ANALOG_SIG_FXSGS:
2728 case ANALOG_SIG_FXSKS:
2730 case ANALOG_SIG_EM_E1:
2731 case ANALOG_SIG_EMWINK:
2732 case ANALOG_SIG_FEATD:
2734 case ANALOG_SIG_SFWINK:
2735 case ANALOG_SIG_SF_FEATD:
2736 if (!ast_strlen_zero(p->dop.dialstr)) {
2737 res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
2739 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
2740 p->dop.dialstr[0] = '\0';
2743 ast_debug(1, "Sent deferred digit string on channel %d: %s\n", p->channel, p->dop.dialstr);
2745 p->dop.dialstr[0] = '\0';
2746 p->dop.op = ANALOG_DIAL_OP_REPLACE;
2748 case ANALOG_SIG_FEATDMF:
2749 case ANALOG_SIG_FEATDMF_TA:
2750 case ANALOG_SIG_E911:
2751 case ANALOG_SIG_FGC_CAMA:
2752 case ANALOG_SIG_FGC_CAMAMF:
2753 case ANALOG_SIG_FEATB:
2754 case ANALOG_SIG_SF_FEATDMF:
2755 case ANALOG_SIG_SF_FEATB:
2756 ast_debug(1, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
2762 case ANALOG_EVENT_POLARITY:
2764 * If we get a Polarity Switch event, this could be
2765 * due to line seizure, remote end connect or remote end disconnect.
2767 * Check to see if we should change the polarity state and
2768 * mark the channel as UP or if this is an indication
2769 * of remote end disconnect.
2772 if (p->polarityonanswerdelay > 0) {
2773 /* check if event is not too soon after OffHook or Answer */
2775 if (ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
2776 switch (ast->_state) {
2777 case AST_STATE_DIALING: /*!< Digits (or equivalent) have been dialed */
2778 case AST_STATE_RINGING: /*!< Remote end is ringing */
2779 if (p->answeronpolarityswitch) {
2780 ast_debug(1, "Answering on polarity switch! channel %d\n", p->channel);
2781 ast_setstate(p->owner, AST_STATE_UP);
2782 p->polarity = POLARITY_REV;
2783 if (p->hanguponpolarityswitch) {
2784 p->polaritydelaytv = ast_tvnow();
2787 ast_debug(1, "Ignore Answer on polarity switch, channel %d\n", p->channel);
2790 case AST_STATE_UP: /*!< Line is up */
2791 case AST_STATE_RING: /*!< Line is ringing */
2792 if (p->hanguponpolarityswitch) {
2793 ast_debug(1, "HangingUp on polarity switch! channel %d\n", p->channel);
2794 ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
2795 p->polarity = POLARITY_IDLE;
2797 ast_debug(1, "Ignore Hangup on polarity switch, channel %d\n", p->channel);
2801 case AST_STATE_DOWN: /*!< Channel is down and available */
2802 case AST_STATE_RESERVED: /*!< Channel is down, but reserved */
2803 case AST_STATE_OFFHOOK: /*!< Channel is off hook */
2804 case AST_STATE_BUSY: /*!< Line is busy */
2805 case AST_STATE_DIALING_OFFHOOK: /*!< Digits (or equivalent) have been dialed while offhook */
2806 case AST_STATE_PRERING: /*!< Channel has detected an incoming call and is waiting for ring */
2808 if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
2809 ast_debug(1, "Ignoring Polarity switch on channel %d, state %d\n", p->channel, ast->_state);
2814 /* event is too soon after OffHook or Answer */
2815 switch (ast->_state) {
2816 case AST_STATE_DIALING: /*!< Digits (or equivalent) have been dialed */
2817 case AST_STATE_RINGING: /*!< Remote end is ringing */
2818 if (p->answeronpolarityswitch) {
2819 ast_debug(1, "Polarity switch detected but NOT answering (too close to OffHook event) on channel %d, state %d\n", p->channel, ast->_state);
2823 case AST_STATE_UP: /*!< Line is up */
2824 case AST_STATE_RING: /*!< Line is ringing */
2825 if (p->hanguponpolarityswitch) {
2826 ast_debug(1, "Polarity switch detected but NOT hanging up (too close to Answer event) on channel %d, state %d\n", p->channel, ast->_state);
2831 if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
2832 ast_debug(1, "Polarity switch detected (too close to previous event) on channel %d, state %d\n", p->channel, ast->_state);
2838 /* Added more log_debug information below to provide a better indication of what is going on */
2839 ast_debug(1, "Polarity Reversal event occured - DEBUG 2: channel %d, state %d, pol= %d, aonp= %d, honp= %d, pdelay= %d, tv= %d\n", p->channel, ast->_state, p->polarity, p->answeronpolarityswitch, p->hanguponpolarityswitch, p->polarityonanswerdelay, ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) );
2842 ast_debug(1, "Dunno what to do with event %d on channel %d\n", res, p->channel);
2844 return &p->subs[index].f;
2847 struct ast_frame *analog_exception(struct analog_pvt *p, struct ast_channel *ast)
2852 struct ast_frame *f;
2854 ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
2856 index = analog_get_index(ast, p, 1);
2858 p->subs[index].f.frametype = AST_FRAME_NULL;
2859 p->subs[index].f.datalen = 0;
2860 p->subs[index].f.samples = 0;
2861 p->subs[index].f.mallocd = 0;
2862 p->subs[index].f.offset = 0;
2863 p->subs[index].f.subclass = 0;
2864 p->subs[index].f.delivery = ast_tv(0,0);
2865 p->subs[index].f.src = "dahdi_exception";
2866 p->subs[index].f.data.ptr = NULL;
2870 /* If nobody owns us, absorb the event appropriately, otherwise
2871 we loop indefinitely. This occurs when, during call waiting, the
2872 other end hangs up our channel so that it no longer exists, but we
2873 have neither FLASH'd nor ONHOOK'd to signify our desire to
2874 change to the other channel. */
2875 res = analog_get_event(p);
2877 /* Switch to real if there is one and this isn't something really silly... */
2878 if ((res != ANALOG_EVENT_RINGEROFF) && (res != ANALOG_EVENT_RINGERON) &&
2879 (res != ANALOG_EVENT_HOOKCOMPLETE)) {
2880 ast_debug(1, "Restoring owner of channel %d on event %d\n", p->channel, res);
2881 p->owner = p->subs[ANALOG_SUB_REAL].owner;
2882 if (p->owner && ast_bridged_channel(p->owner))
2883 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
2886 case ANALOG_EVENT_ONHOOK:
2887 analog_set_echocanceller(p, 0);
2889 ast_verb(3, "Channel %s still has call, ringing phone\n", p->owner->name);
2891 analog_stop_callwait(p);
2893 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
2894 analog_update_conf(p);
2896 case ANALOG_EVENT_RINGOFFHOOK:
2897 analog_set_echocanceller(p, 1);
2899 if (p->owner && (p->owner->_state == AST_STATE_RINGING)) {
2900 ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_ANSWER);
2901 analog_set_dialing(p, 0);
2904 case ANALOG_EVENT_HOOKCOMPLETE:
2905 case ANALOG_EVENT_RINGERON:
2906 case ANALOG_EVENT_RINGEROFF:
2909 case ANALOG_EVENT_WINKFLASH:
2910 gettimeofday(&p->flashtime, NULL);
2912 ast_verb(3, "Channel %d flashed to other channel %s\n", p->channel, p->owner->name);
2913 if (p->owner->_state != AST_STATE_UP) {
2914 /* Answer if necessary */
2915 usedindex = analog_get_index(p->owner, p, 0);
2916 if (usedindex > -1) {
2917 ast_queue_control(p->subs[usedindex].owner, AST_CONTROL_ANSWER);
2919 ast_setstate(p->owner, AST_STATE_UP);
2921 analog_stop_callwait(p);
2922 if (ast_bridged_channel(p->owner))
2923 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);