Index: channels/chan_zap.c =================================================================== --- channels/chan_zap.c (revision 72600) +++ channels/chan_zap.c (working copy) @@ -3775,6 +3775,11 @@ ast_setstate(ast, AST_STATE_UP); p->subs[index].f.frametype = AST_FRAME_CONTROL; p->subs[index].f.subclass = AST_CONTROL_ANSWER; + /* If aops=0 and hops=1, this is necessary */ + p->polarity=POLARITY_REV; + } else { + /* Start clean, so we can catch the change to REV polarity when party answers */ + p->polarity=POLARITY_IDLE; } } } @@ -4066,11 +4071,15 @@ /* If we get a ring then we cannot be in * reversed polarity. So we reset to idle */ - if (option_debug) + /* This isn't working in Spain. We first + get a PR, then 1 or more RINGS. Setting + polarity to IDLE breaks remote hangup + detection */ + /*if (option_debug) ast_log(LOG_DEBUG, "Setting IDLE polarity due " "to ring. Old polarity was %d\n", p->polarity); - p->polarity = POLARITY_IDLE; + p->polarity = POLARITY_IDLE;*/ /* Fall through */ case SIG_EM: @@ -6362,6 +6371,15 @@ if (i & ZT_IOMUX_SIGEVENT) { res = zt_get_event(p->subs[index].zfd); ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res)); + /* If we get a PR event, they hung up while processing calerid */ + ast_log(LOG_DEBUG, "JM4: chan %d, pol=%d, aonp=%d, honp=%d, pdelay=%d, tv=%d\n", p->channel, p->polarity, p->answeronpolarityswitch, p->hanguponpolarityswitch, p->polarityonanswerdelay, ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) ); + if ( res == ZT_EVENT_POLARITY && p->hanguponpolarityswitch && p->polarity == POLARITY_REV ) { + ast_log(LOG_DEBUG, "Hanging up due to polarity reversal on channel %d while detecting callerid\n", p->channel); + p->polarity=POLARITY_IDLE; + callerid_free(cs); + ast_hangup(chan); + return NULL; + } res = 0; /* Let us detect callerid when the telco uses distinctive ring */ @@ -6724,6 +6742,13 @@ case SIG_FXSLS: case SIG_FXSKS: case SIG_FXSGS: + /* We have already got a PR before the channel was + created, but it wasn't handled. We need polarity + to be REV for remote hangup detection to work. + At least in Spain */ + if (i->hanguponpolarityswitch) + i->polarity = POLARITY_REV; + if (i->cid_start == CID_START_POLARITY) { i->polarity = POLARITY_REV; ast_verbose(VERBOSE_PREFIX_2 "Starting post polarity " @@ -10686,6 +10711,14 @@ ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno); else confp->chan.amaflags = y; + } else if (!strcasecmp(v->name, "polarityonanswerdelay")) { + confp->chan.polarityonanswerdelay = atoi(v->value); + } else if (!strcasecmp(v->name, "answeronpolarityswitch")) { + confp->chan.answeronpolarityswitch = ast_true(v->value); + } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) { + confp->chan.hanguponpolarityswitch = ast_true(v->value); + } else if (!strcasecmp(v->name, "sendcalleridafter")) { + confp->chan.sendcalleridafter = atoi(v->value); } else if (!reload){ if (!strcasecmp(v->name, "signalling")) { confp->chan.outsigmod = -1; @@ -11095,14 +11128,6 @@ } } close(ctlfd); - } else if (!strcasecmp(v->name, "polarityonanswerdelay")) { - confp->chan.polarityonanswerdelay = atoi(v->value); - } else if (!strcasecmp(v->name, "answeronpolarityswitch")) { - confp->chan.answeronpolarityswitch = ast_true(v->value); - } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) { - confp->chan.hanguponpolarityswitch = ast_true(v->value); - } else if (!strcasecmp(v->name, "sendcalleridafter")) { - confp->chan.sendcalleridafter = atoi(v->value); } else if (!strcasecmp(v->name, "defaultcic")) { ast_copy_string(defaultcic, v->value, sizeof(defaultcic)); } else if (!strcasecmp(v->name, "defaultozz")) {