freetdm: ftmod_zt - refactored zt_channel_next_event(), zt_next_event() and created zt_channel_process_event()

This commit is contained in:
Arnaldo Pereira 2010-12-23 16:05:48 -02:00
parent 4098b4e289
commit a797a93feb
1 changed files with 72 additions and 121 deletions

View File

@ -993,27 +993,14 @@ FIO_SPAN_POLL_EVENT_FUNCTION(zt_poll_event)
} }
/** /**
* \brief Retrieves an event from a ftdm channel * \brief Process an event from a ftdmchan and set the proper OOB event_id. The channel must be locked.
* \param ftdmchan Channel to retrieve event from * \param fchan Channel to retrieve event from
* \param event FreeTDM event to return * \param event_id Pointer to OOB event id
* \return Success or failure * \param zt_event_id Zaptel event id
* \return FTDM_SUCCESS or FTDM_FAIL
*/ */
FIO_CHANNEL_NEXT_EVENT_FUNCTION(zt_channel_next_event) static __inline__ ftdm_status_t zt_channel_process_event(ftdm_channel_t *fchan, ftdm_oob_event_t *event_id, zt_event_t zt_event_id)
{ {
uint32_t i, event_id = FTDM_OOB_INVALID;
zt_event_t zt_event_id = 0;
ftdm_span_t *span = ftdmchan->span;
if (ftdm_test_flag(ftdmchan, FTDM_CHANNEL_EVENT)) {
ftdm_clear_flag(ftdmchan, FTDM_CHANNEL_EVENT);
}
if (ioctl(ftdmchan->sockfd, codes.GETEVENT, &zt_event_id) == -1) {
ftdm_log_chan(ftdmchan, FTDM_LOG_ERROR, "Failed retrieving event from channel: %s\n",
strerror(errno));
return FTDM_FAIL;
}
switch(zt_event_id) { switch(zt_event_id) {
case ZT_EVENT_RINGEROFF: case ZT_EVENT_RINGEROFF:
{ {
@ -1027,63 +1014,92 @@ FIO_CHANNEL_NEXT_EVENT_FUNCTION(zt_channel_next_event)
break; break;
case ZT_EVENT_RINGBEGIN: case ZT_EVENT_RINGBEGIN:
{ {
event_id = FTDM_OOB_RING_START; *event_id = FTDM_OOB_RING_START;
} }
break; break;
case ZT_EVENT_ONHOOK: case ZT_EVENT_ONHOOK:
{ {
event_id = FTDM_OOB_ONHOOK; *event_id = FTDM_OOB_ONHOOK;
} }
break; break;
case ZT_EVENT_WINKFLASH: case ZT_EVENT_WINKFLASH:
{ {
if (ftdmchan->state == FTDM_CHANNEL_STATE_DOWN || ftdmchan->state == FTDM_CHANNEL_STATE_DIALING) { if (fchan->state == FTDM_CHANNEL_STATE_DOWN || fchan->state == FTDM_CHANNEL_STATE_DIALING) {
event_id = FTDM_OOB_WINK; *event_id = FTDM_OOB_WINK;
} else { } else {
event_id = FTDM_OOB_FLASH; *event_id = FTDM_OOB_FLASH;
} }
} }
break; break;
case ZT_EVENT_RINGOFFHOOK: case ZT_EVENT_RINGOFFHOOK:
{ {
if (ftdmchan->type == FTDM_CHAN_TYPE_FXS || (ftdmchan->type == FTDM_CHAN_TYPE_EM && ftdmchan->state != FTDM_CHANNEL_STATE_UP)) { if (fchan->type == FTDM_CHAN_TYPE_FXS || (fchan->type == FTDM_CHAN_TYPE_EM && fchan->state != FTDM_CHANNEL_STATE_UP)) {
ftdm_set_flag(ftdmchan, FTDM_CHANNEL_OFFHOOK); ftdm_set_flag_locked(fchan, FTDM_CHANNEL_OFFHOOK);
event_id = FTDM_OOB_OFFHOOK; *event_id = FTDM_OOB_OFFHOOK;
} else if (ftdmchan->type == FTDM_CHAN_TYPE_FXO) { } else if (fchan->type == FTDM_CHAN_TYPE_FXO) {
event_id = FTDM_OOB_RING_START; *event_id = FTDM_OOB_RING_START;
} else { } else {
event_id = FTDM_OOB_NOOP; *event_id = FTDM_OOB_NOOP;
} }
} }
break; break;
case ZT_EVENT_ALARM: case ZT_EVENT_ALARM:
{ {
event_id = FTDM_OOB_ALARM_TRAP; *event_id = FTDM_OOB_ALARM_TRAP;
} }
break; break;
case ZT_EVENT_NOALARM: case ZT_EVENT_NOALARM:
{ {
event_id = FTDM_OOB_ALARM_CLEAR; *event_id = FTDM_OOB_ALARM_CLEAR;
} }
break; break;
case ZT_EVENT_BITSCHANGED: case ZT_EVENT_BITSCHANGED:
{ {
event_id = FTDM_OOB_CAS_BITS_CHANGE; *event_id = FTDM_OOB_CAS_BITS_CHANGE;
int bits = 0; int bits = 0;
int err = ioctl(ftdmchan->sockfd, codes.GETRXBITS, &bits); int err = ioctl(fchan->sockfd, codes.GETRXBITS, &bits);
if (err) { if (err) {
return FTDM_FAIL; return FTDM_FAIL;
} }
ftdmchan->rx_cas_bits = bits; fchan->rx_cas_bits = bits;
} }
break; break;
default: default:
{ {
ftdm_log_chan(ftdmchan, FTDM_LOG_WARNING, "Unhandled event %d for %d:%d\n", zt_event_id, span->span_id, i); ftdm_log_chan(fchan, FTDM_LOG_WARNING, "Unhandled event %d\n", zt_event_id);
event_id = FTDM_OOB_INVALID; *event_id = FTDM_OOB_INVALID;
} }
break; break;
} }
return FTDM_SUCCESS;
}
/**
* \brief Retrieves an event from a ftdm channel
* \param ftdmchan Channel to retrieve event from
* \param event FreeTDM event to return
* \return Success or failure
*/
FIO_CHANNEL_NEXT_EVENT_FUNCTION(zt_channel_next_event)
{
uint32_t event_id = FTDM_OOB_INVALID;
zt_event_t zt_event_id = 0;
ftdm_span_t *span = ftdmchan->span;
if (ftdm_test_flag(ftdmchan, FTDM_CHANNEL_EVENT)) {
ftdm_clear_flag(ftdmchan, FTDM_CHANNEL_EVENT);
}
if (ioctl(ftdmchan->sockfd, codes.GETEVENT, &zt_event_id) == -1) {
ftdm_log_chan(ftdmchan, FTDM_LOG_ERROR, "Failed retrieving event from channel: %s\n",
strerror(errno));
return FTDM_FAIL;
}
if ((zt_channel_process_event(ftdmchan, &event_id, zt_event_id)) != FTDM_SUCCESS) {
ftdm_log_chan_msg(ftdmchan, FTDM_LOG_ERROR, "Failed to process event from channel\n");
return FTDM_FAIL;
}
ftdmchan->last_event_time = 0; ftdmchan->last_event_time = 0;
span->event_header.e_type = FTDM_EVENT_OOB; span->event_header.e_type = FTDM_EVENT_OOB;
@ -1105,91 +1121,26 @@ FIO_SPAN_NEXT_EVENT_FUNCTION(zt_next_event)
zt_event_t zt_event_id = 0; zt_event_t zt_event_id = 0;
for(i = 1; i <= span->chan_count; i++) { for(i = 1; i <= span->chan_count; i++) {
if (ftdm_test_flag(span->channels[i], FTDM_CHANNEL_EVENT)) { ftdm_channel_t *fchan = span->channels[i];
ftdm_clear_flag(span->channels[i], FTDM_CHANNEL_EVENT); if (ftdm_test_flag(fchan, FTDM_CHANNEL_EVENT)) {
if (ioctl(span->channels[i]->sockfd, codes.GETEVENT, &zt_event_id) == -1) { ftdm_clear_flag(fchan, FTDM_CHANNEL_EVENT);
snprintf(span->last_error, sizeof(span->last_error), "%s", strerror(errno));
return FTDM_FAIL;
}
switch(zt_event_id) {
case ZT_EVENT_RINGEROFF:
{
return FTDM_FAIL;
}
break;
case ZT_EVENT_RINGERON:
{
return FTDM_FAIL;
}
break;
case ZT_EVENT_RINGBEGIN:
{
event_id = FTDM_OOB_RING_START;
}
break;
case ZT_EVENT_ONHOOK:
{
event_id = FTDM_OOB_ONHOOK;
}
break;
case ZT_EVENT_WINKFLASH:
{
if (span->channels[i]->state == FTDM_CHANNEL_STATE_DOWN || span->channels[i]->state == FTDM_CHANNEL_STATE_DIALING) {
event_id = FTDM_OOB_WINK;
} else {
event_id = FTDM_OOB_FLASH;
}
}
break;
case ZT_EVENT_RINGOFFHOOK:
{
if (span->channels[i]->type == FTDM_CHAN_TYPE_FXS || (span->channels[i]->type == FTDM_CHAN_TYPE_EM && span->channels[i]->state != FTDM_CHANNEL_STATE_UP)) {
ftdm_set_flag_locked(span->channels[i], FTDM_CHANNEL_OFFHOOK);
event_id = FTDM_OOB_OFFHOOK;
} else if (span->channels[i]->type == FTDM_CHAN_TYPE_FXO) {
event_id = FTDM_OOB_RING_START;
} else {
event_id = FTDM_OOB_NOOP;
}
}
break;
case ZT_EVENT_ALARM:
{
event_id = FTDM_OOB_ALARM_TRAP;
}
break;
case ZT_EVENT_NOALARM:
{
event_id = FTDM_OOB_ALARM_CLEAR;
}
break;
case ZT_EVENT_BITSCHANGED:
{
event_id = FTDM_OOB_CAS_BITS_CHANGE;
int bits = 0;
int err = ioctl(span->channels[i]->sockfd, codes.GETRXBITS, &bits);
if (err) {
return FTDM_FAIL;
}
span->channels[i]->rx_cas_bits = bits;
}
break;
default:
{
ftdm_log(FTDM_LOG_WARNING, "Unhandled event %d for %d:%d\n", zt_event_id, span->span_id, i);
event_id = FTDM_OOB_INVALID;
}
break;
}
span->channels[i]->last_event_time = 0;
span->event_header.e_type = FTDM_EVENT_OOB;
span->event_header.enum_id = event_id;
span->event_header.channel = span->channels[i];
*event = &span->event_header;
return FTDM_SUCCESS;
} }
if (ioctl(fchan->sockfd, codes.GETEVENT, &zt_event_id) == -1) {
snprintf(span->last_error, sizeof(span->last_error), "%s", strerror(errno));
return FTDM_FAIL;
}
if ((zt_channel_process_event(fchan, &event_id, zt_event_id)) != FTDM_SUCCESS) {
ftdm_log_chan_msg(fchan, FTDM_LOG_ERROR, "Failed to process event from channel\n");
return FTDM_FAIL;
}
fchan->last_event_time = 0;
span->event_header.e_type = FTDM_EVENT_OOB;
span->event_header.enum_id = event_id;
span->event_header.channel = fchan;
*event = &span->event_header;
return FTDM_SUCCESS;
} }
return FTDM_FAIL; return FTDM_FAIL;