Index: channels/chan_mgcp.c =================================================================== --- channels/chan_mgcp.c (revisiĆ³n: 219899) +++ channels/chan_mgcp.c (copia de trabajo) @@ -70,6 +70,7 @@ #include "asterisk/stringfields.h" #include "asterisk/abstract_jb.h" #include "asterisk/event.h" +#include "asterisk/chanvars.h" /* * Define to work around buggy dlink MGCP phone firmware which @@ -363,6 +364,7 @@ /* struct ast_rtp *rtp; */ /* struct sockaddr_in tmpdest; */ /* message go the the endpoint and not the channel so they stay here */ + struct ast_variable *chanvars; /*!< Variables to set for channel created by user */ struct mgcp_endpoint *next; struct mgcp_gateway *parent; }; @@ -430,6 +432,8 @@ static int mgcp_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration); static int mgcp_devicestate(void *data); static void add_header_offhook(struct mgcp_subchannel *sub, struct mgcp_request *resp); +static struct ast_variable *add_var(const char *buf, struct ast_variable *list); +static struct ast_variable *copy_vars(struct ast_variable *src); static const struct ast_channel_tech mgcp_tech = { .type = "MGCP", @@ -1038,6 +1042,8 @@ struct mgcp_gateway *mg; struct mgcp_endpoint *me; int hasendpoints = 0; + struct ast_variable * v = NULL; + switch (cmd) { case CLI_INIT: @@ -1058,9 +1064,12 @@ me = mg->endpoints; ast_cli(a->fd, "Gateway '%s' at %s (%s)\n", mg->name, mg->addr.sin_addr.s_addr ? ast_inet_ntoa(mg->addr.sin_addr) : ast_inet_ntoa(mg->defaddr.sin_addr), mg->dynamic ? "Dynamic" : "Static"); while(me) { - /* Don't show wilcard endpoint */ - if (strcmp(me->name, mg->wcardep) != 0) - ast_cli(a->fd, " -- '%s@%s in '%s' is %s\n", me->name, mg->name, me->context, me->sub->owner ? "active" : "idle"); + ast_cli(a->fd, " -- '%s@%s in '%s' is %s\n", me->name, mg->name, me->context, me->sub->owner ? "active" : "idle"); + if (me->chanvars) { + ast_cli(a->fd, " Variables:\n"); + for (v = me->chanvars ; v ; v = v->next) + ast_cli(a->fd, " %s = %s\n", v->name, v->value); + } hasendpoints = 1; me = me->next; } @@ -1469,6 +1478,7 @@ static struct ast_channel *mgcp_new(struct mgcp_subchannel *sub, int state, const char *linkedid) { struct ast_channel *tmp; + struct ast_variable *v = NULL; struct mgcp_endpoint *i = sub->parent; int fmt; @@ -1517,6 +1527,13 @@ if (!i->adsi) tmp->adsicpe = AST_ADSI_UNAVAILABLE; tmp->priority = 1; + + /* Set channel variables for this call from configuration */ + for (v = i->chanvars ; v ; v = v->next) { + char valuebuf[1024]; + pbx_builtin_setvar_helper(tmp, v->name, ast_get_encoded_str(v->value, valuebuf, sizeof(valuebuf))); + } + if (sub->rtp) ast_jb_configure(tmp, &global_jbconf); if (state != AST_STATE_DOWN) { @@ -3548,6 +3565,8 @@ struct mgcp_gateway *gw; struct mgcp_endpoint *e; struct mgcp_subchannel *sub; + struct ast_variable *chanvars = NULL; + /*char txident[80];*/ int i=0, y=0; int gw_reload = 0; @@ -3648,6 +3667,14 @@ } else { amaflags = y; } + + } else if (!strcasecmp(v->name, "setvar")) { + chanvars = add_var(v->value, chanvars); + } else if (!strcasecmp(v->name, "clearvars")) { + if (chanvars) { + ast_variables_destroy(chanvars); + chanvars = NULL; + } } else if (!strcasecmp(v->name, "musiconhold")) { ast_copy_string(musicclass, v->value, sizeof(musicclass)); } else if (!strcasecmp(v->name, "parkinglot")) { @@ -3757,6 +3784,7 @@ e->onhooktime = time(NULL); /* ASSUME we're onhook */ e->hookstate = MGCP_ONHOOK; + e->chanvars = copy_vars(chanvars); if (!ep_reload) { /*snprintf(txident, sizeof(txident), "%08lx", ast_random());*/ for (i = 0; i < MAX_SUBS; i++) { @@ -3858,6 +3886,16 @@ e->slowsequence = slowsequence; e->transfer = transfer; e->threewaycalling = threewaycalling; + + /* If we already have a valid chanvars, it's not a new endpoint (it's a reload), + so first, free previous mem + */ + if (e->chanvars) { + ast_variables_destroy(e->chanvars); + e->chanvars = NULL; + } + e->chanvars = copy_vars(chanvars); + if (!ep_reload) { e->onhooktime = time(NULL); /* ASSUME we're onhook */ @@ -3920,6 +3958,11 @@ ast_mutex_destroy(&gw->msgs_lock); ast_free(gw); } + + if (chanvars) { + ast_variables_destroy(chanvars); + chanvars = NULL; + } return NULL; } gw->defaddr.sin_family = AF_INET; @@ -3932,6 +3975,10 @@ if (ast_ouraddrfor(&gw->addr.sin_addr, &gw->ourip)) memcpy(&gw->ourip, &__ourip, sizeof(gw->ourip)); + if (chanvars) { + ast_variables_destroy(chanvars); + chanvars = NULL; + } return (gw_reload ? NULL : gw); } @@ -4007,7 +4054,12 @@ if (e->mwi_event_sub) ast_event_unsubscribe(e->mwi_event_sub); - + + if (e->chanvars) { + ast_variables_destroy(e->chanvars); + e->chanvars = NULL; + } + ast_mutex_destroy(&e->lock); ast_mutex_destroy(&e->rqnt_queue_lock); ast_mutex_destroy(&e->cmd_queue_lock); @@ -4067,6 +4119,38 @@ ast_mutex_unlock(&gatelock); } +static struct ast_variable *add_var(const char *buf, struct ast_variable *list) +{ + struct ast_variable *tmpvar = NULL; + char *varname = ast_strdupa(buf), *varval = NULL; + + if ((varval = strchr(varname, '='))) { + *varval++ = '\0'; + if ((tmpvar = ast_variable_new(varname, varval, ""))) { + tmpvar->next = list; + list = tmpvar; + } + } + return list; +} + +/*! \brief + * duplicate a list of channel variables, \return the copy. + */ +static struct ast_variable *copy_vars(struct ast_variable *src) +{ + struct ast_variable *res = NULL, *tmp, *v = NULL; + + for (v = src ; v ; v = v->next) { + if ((tmp = ast_variable_new(v->name, v->value, v->file))) { + tmp->next = res; + res = tmp; + } + } + return res; +} + + static int reload_config(int reload) { struct ast_config *cfg;