--- asterisk-1.2.0/channels/chan_zap.c 2005-11-11 18:00:07.000000000 +0000 +++ asterisk-1.2.0-polaritypatch/channels/chan_zap.c 2005-11-27 11:54:38.000000000 +0000 @@ -334,6 +334,10 @@ /*! \brief Whether we hang up on a Polarity Switch event */ static int hanguponpolarityswitch = 0; +/*! \brief Whether we reset polarity to IDLE on a RING event. Needed in Sweeden. Breaks in Spain */ +static int resetpolarityonring = 0; + + /*! \brief How long (ms) to ignore Polarity Switch events after we answer a call */ static int polarityonanswerdelay = 600; @@ -574,6 +578,7 @@ unsigned int priexclusive:1; unsigned int pulse:1; unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */ + unsigned int resetpolarityonring:1; unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */ unsigned int threewaycalling:1; unsigned int transfer:1; @@ -3494,6 +3499,7 @@ pthread_t threadid; pthread_attr_t attr; struct ast_channel *chan; + int polarityswitchalreadyhandled; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); @@ -3863,10 +3869,12 @@ /* If we get a ring then we cannot be in * reversed polarity. So we reset to idle */ + if (p->resetpolarityonring) { ast_log(LOG_DEBUG, "Setting IDLE polarity due " "to ring. Old polarity was %d\n", p->polarity); p->polarity = POLARITY_IDLE; + } /* Fall through */ case SIG_EM: @@ -4190,6 +4198,7 @@ * mark the channel as UP or if this is an indication * of remote end disconnect. */ + polarityswitchalreadyhandled=0; if (p->polarity == POLARITY_IDLE) { p->polarity = POLARITY_REV; if (p->answeronpolarityswitch && @@ -4197,12 +4206,14 @@ (ast->_state == AST_STATE_RINGING))) { ast_log(LOG_DEBUG, "Answering on polarity switch!\n"); ast_setstate(p->owner, AST_STATE_UP); + polarityswitchalreadyhandled=1; + gettimeofday(&p->polaritydelaytv, NULL); } else ast_log(LOG_DEBUG, "Ignore switch to REVERSED Polarity on channel %d, state %d\n", p->channel, ast->_state); } /* Removed else statement from here as it was preventing hangups from ever happening*/ /* Added AST_STATE_RING in if statement below to deal with calling party hangups that take place when ringing */ - if(p->hanguponpolarityswitch && + if(!polarityswitchalreadyhandled && p->hanguponpolarityswitch && (p->polarityonanswerdelay > 0) && (p->polarity == POLARITY_REV) && ((ast->_state == AST_STATE_UP) || (ast->_state == AST_STATE_RING)) ) { @@ -4216,7 +4227,7 @@ } else { ast_log(LOG_DEBUG, "Polarity Reversal detected but NOT hanging up (too close to answer event) on channel %d, state %d\n", p->channel, ast->_state); } - } else { + } else if (!polarityswitchalreadyhandled) { p->polarity = POLARITY_IDLE; ast_log(LOG_DEBUG, "Ignoring Polarity switch to IDLE on channel %d, state %d\n", p->channel, ast->_state); } @@ -7186,6 +7197,7 @@ tmp->polarityonanswerdelay = polarityonanswerdelay; tmp->answeronpolarityswitch = answeronpolarityswitch; tmp->hanguponpolarityswitch = hanguponpolarityswitch; + tmp->resetpolarityonring = resetpolarityonring; tmp->sendcalleridafter = sendcalleridafter; } @@ -10805,6 +10817,8 @@ answeronpolarityswitch = ast_true(v->value); } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) { hanguponpolarityswitch = ast_true(v->value); + } else if (!strcasecmp(v->name, "resetpolarityonring")) { + resetpolarityonring = ast_true(v->value); } else if (!strcasecmp(v->name, "sendcalleridafter")) { sendcalleridafter = atoi(v->value); } else if (!strcasecmp(v->name, "defaultcic")) {