From 6cf298a74631ef0baa37fd4601df3d0fad922d14 Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 17:18:18 -0400 Subject: [PATCH] use span name instead of id. implement termination choose for tdm. --- libs/freetdm/mod_freetdm/tdm.c | 14 ++++++++++++-- .../endpoints/mod_media_gateway/media_gateway.c | 17 +++++++++++++++-- .../mod_media_gateway/media_gateway_xml.c | 8 ++++---- .../mod_media_gateway/mod_media_gateway.h | 13 ++++++++++++- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/libs/freetdm/mod_freetdm/tdm.c b/libs/freetdm/mod_freetdm/tdm.c index a3369f1f7b..53ee192cf0 100644 --- a/libs/freetdm/mod_freetdm/tdm.c +++ b/libs/freetdm/mod_freetdm/tdm.c @@ -38,6 +38,7 @@ void ctdm_init(switch_loadable_module_interface_t *module_interface); #define kSPAN_ID "span" #define kCHAN_ID "chan" +#define kSPAN_NAME "span_name" static struct { switch_memory_pool_t *pool; @@ -97,7 +98,8 @@ static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *sessi switch_originate_flag_t flags, switch_call_cause_t *cancel_cause) { const char *szspanid = switch_event_get_header(var_event, kSPAN_ID), - *szchanid = switch_event_get_header(var_event, kCHAN_ID); + *szchanid = switch_event_get_header(var_event, kCHAN_ID), + *span_name = switch_event_get_header(var_event, kSPAN_NAME); int chan_id; int span_id; @@ -111,14 +113,22 @@ static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *sessi ctdm_private_t *tech_pvt = NULL; - if (zstr(szchanid) || zstr(szspanid)) { + if (zstr(szchanid) || (zstr(szspanid) && zstr(span_name))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Both ["kSPAN_ID"] and ["kCHAN_ID"] have to be set.\n"); goto fail; } chan_id = atoi(szchanid); span_id = atoi(szspanid); + + if (!span_id) { + if (ftdm_span_find_by_name(span_name, &span) == FTDM_SUCCESS) { + span_id = ftdm_span_get_id(span); + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find span [%s]\n"); + } + } if (!(*new_session = switch_core_session_request(ctdm.endpoint_interface, SWITCH_CALL_DIRECTION_OUTBOUND, 0, pool))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't request session.\n"); diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway.c b/src/mod/endpoints/mod_media_gateway/media_gateway.c index 8580844172..8fbd08abf5 100644 --- a/src/mod/endpoints/mod_media_gateway/media_gateway.c +++ b/src/mod/endpoints/mod_media_gateway/media_gateway.c @@ -120,7 +120,7 @@ switch_status_t megaco_activate_termination(mg_termination_t *term) } else if (term->type == MG_TERM_TDM) { switch_snprintf(dialstring, sizeof dialstring, "tdm/%s", term->name); - switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, kSPAN_ID, "%d", term->u.tdm.span); + switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, kSPAN_NAME, term->u.tdm.span_name); switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, kCHAN_ID, "%d", term->u.tdm.channel); } @@ -135,6 +135,8 @@ switch_status_t megaco_activate_termination(mg_termination_t *term) switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Termination [%s] successfully instanciated as [%s] [%s]\n", term->name, dialstring, switch_core_session_get_uuid(session)); + switch_set_flag(term, MGT_ACTIVE); + done: if (session) { switch_core_session_rwunlock(session); @@ -151,6 +153,7 @@ mg_termination_t *megaco_choose_termination(megaco_profile_t *profile, const cha mg_termination_t *term = NULL; char name[100]; int term_id; + size_t prefixlen = strlen(prefix); /* Check the termination type by prefix */ if (strncasecmp(prefix, profile->rtp_termination_id_prefix, strlen(profile->rtp_termination_id_prefix)) == 0) { @@ -158,8 +161,14 @@ mg_termination_t *megaco_choose_termination(megaco_profile_t *profile, const cha term_id = mg_rtp_request_id(profile); switch_snprintf(name, sizeof name, "%s/%d", profile->rtp_termination_id_prefix, term_id); } else { + for (term = profile->physical_terminations; term; term = term->next) { + if (!strncasecmp(prefix, term->name, prefixlen) && !switch_test_flag(term, MGT_ALLOCATED)) { + switch_set_flag(term, MGT_ALLOCATED); + return term; + } + } - return term; + return NULL; } switch_core_new_memory_pool(&pool); @@ -168,6 +177,7 @@ mg_termination_t *megaco_choose_termination(megaco_profile_t *profile, const cha term->type = termtype; term->active_events = NULL; term->profile = profile; + switch_set_flag(term, MGT_ALLOCATED); if (termtype == MG_TERM_RTP) { /* Fill in local address and reserve an rtp port */ @@ -212,6 +222,9 @@ void megaco_termination_destroy(mg_termination_t *term) term->active_events = NULL; } + switch_clear_flag(term, MGT_ALLOCATED); + switch_clear_flag(term, MGT_ACTIVE); + if (term->type == MG_TERM_RTP) { switch_core_hash_delete_wrlock(term->profile->terminations, term->name, term->profile->terminations_rwlock); switch_core_destroy_memory_pool(&term->pool); diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway_xml.c b/src/mod/endpoints/mod_media_gateway/media_gateway_xml.c index 68d546985d..f30c1d955d 100644 --- a/src/mod/endpoints/mod_media_gateway/media_gateway_xml.c +++ b/src/mod/endpoints/mod_media_gateway/media_gateway_xml.c @@ -77,8 +77,6 @@ switch_status_t config_profile(megaco_profile_t *profile, switch_bool_t reload) //const char *tech = switch_xml_attr(mg_term, "tech"); const char *channel_prefix = switch_xml_attr(mg_term, "channel-prefix"); const char *channel_map = switch_xml_attr(mg_term, "channel-map"); - const char *szspan_id = switch_xml_attr(mg_term, "span-id"); - const int span_id = !zstr(szspan_id) ? atoi(szspan_id) : 0; if (!zstr(channel_map)) { @@ -102,12 +100,14 @@ switch_status_t config_profile(megaco_profile_t *profile, switch_bool_t reload) term->type = MG_TERM_TDM; term->profile = profile; term->name = switch_core_sprintf(pool, "%s%d", prefix, j); - term->u.tdm.span = span_id; term->u.tdm.channel = j; + term->u.tdm.span_name = switch_core_strdup(pool, channel_prefix); switch_core_hash_insert_wrlock(profile->terminations, term->name, term, profile->terminations_rwlock); + term->next = profile->physical_terminations; + profile->physical_terminations = term; - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Mapped termination [%s] to freetdm span: %d chan: %d\n", term->name, term->u.tdm.span, term->u.tdm.channel); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Mapped termination [%s] to freetdm span: %s chan: %d\n", term->name, term->u.tdm.span_name, term->u.tdm.channel); } } } diff --git a/src/mod/endpoints/mod_media_gateway/mod_media_gateway.h b/src/mod/endpoints/mod_media_gateway/mod_media_gateway.h index d74fb31d1e..c1960877bd 100644 --- a/src/mod/endpoints/mod_media_gateway/mod_media_gateway.h +++ b/src/mod/endpoints/mod_media_gateway/mod_media_gateway.h @@ -83,10 +83,17 @@ typedef struct mg_context_s mg_context_t; /* TDM parameters understood by the controllable channel */ #define kSPAN_ID "span" #define kCHAN_ID "chan" +#define kSPAN_NAME "span_name" typedef struct mg_termination_s mg_termination_t; +enum { + MGT_ALLOCATED = (1 << 0), + MGT_ACTIVE = (1 << 1), + +} mg_termination_flags; + struct mg_termination_s { switch_memory_pool_t *pool; mg_termination_type_t type; @@ -96,6 +103,7 @@ struct mg_termination_s { megaco_profile_t *profile; /*!< Parent MG profile */ MgMgcoReqEvtDesc *active_events; /* !< active megaco events */ mg_termination_t *next; /*!< List for physical terminations */ + uint32_t flags; union { struct { @@ -116,8 +124,8 @@ struct mg_termination_s { } rtp; struct { - int span; int channel; + const char *span_name; } tdm; } u; }; @@ -165,6 +173,9 @@ struct megaco_profile_s { uint8_t rtpid_bitmap[MG_MAX_CONTEXTS/8]; uint32_t rtpid_next; + + mg_termination_t *physical_terminations; + switch_hash_t *terminations; switch_thread_rwlock_t *terminations_rwlock; };