Thu Aug 30 20:43:00 2012

Asterisk developer's documentation


cel.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007 - 2009, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*!
00018  * \file
00019  *
00020  * \brief Channel Event Logging API
00021  *
00022  * \author Steve Murphy <murf@digium.com>
00023  * \author Russell Bryant <russell@digium.com>
00024  *
00025  * \todo Do thorough testing of all transfer methods to ensure that BLINDTRANSFER,
00026  *       ATTENDEDTRANSFER, BRIDGE_START, and BRIDGE_END events are all reported
00027  *       as expected.
00028  */
00029 
00030 /*** MODULEINFO
00031    <support_level>core</support_level>
00032  ***/
00033 
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
00037 
00038 #include "asterisk/_private.h"
00039 
00040 #include "asterisk/channel.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/cel.h"
00043 #include "asterisk/logger.h"
00044 #include "asterisk/linkedlists.h"
00045 #include "asterisk/utils.h"
00046 #include "asterisk/config.h"
00047 #include "asterisk/cli.h"
00048 #include "asterisk/astobj2.h"
00049 
00050 /*! Is the CEL subsystem enabled ? */
00051 static unsigned char cel_enabled;
00052 
00053 /*! \brief CEL is off by default */
00054 #define CEL_ENABLED_DEFAULT      0
00055 
00056 /*! 
00057  * \brief which events we want to track 
00058  *
00059  * \note bit field, up to 64 events
00060  */
00061 static int64_t eventset;
00062 
00063 /*!
00064  * \brief Maximum possible CEL event IDs
00065  * \note This limit is currently imposed by the eventset definition
00066  */
00067 #define CEL_MAX_EVENT_IDS 64
00068 
00069 /*! 
00070  * \brief Track no events by default.
00071  */
00072 #define CEL_DEFAULT_EVENTS 0
00073 
00074 /*!
00075  * \brief Number of buckets for the appset container
00076  */
00077 #define NUM_APP_BUCKETS    97
00078 
00079 /*!
00080  * \brief Container of Asterisk application names
00081  *
00082  * The apps in this container are the applications that were specified
00083  * in the configuration as applications that CEL events should be generated
00084  * for when they start and end on a channel.
00085  */
00086 static struct ao2_container *appset;
00087 static struct ao2_container *linkedids;
00088 
00089 /*!
00090  * \brief Configured date format for event timestamps
00091  */
00092 static char cel_dateformat[256];
00093 
00094 /*!
00095  * \brief Map of ast_cel_event_type to strings
00096  */
00097 static const char * const cel_event_types[CEL_MAX_EVENT_IDS] = {
00098    [0]                        = "ALL",
00099    [AST_CEL_CHANNEL_START]    = "CHAN_START",
00100    [AST_CEL_CHANNEL_END]      = "CHAN_END",
00101    [AST_CEL_ANSWER]           = "ANSWER",
00102    [AST_CEL_HANGUP]           = "HANGUP",
00103    [AST_CEL_APP_START]        = "APP_START",
00104    [AST_CEL_APP_END]          = "APP_END",
00105    [AST_CEL_BRIDGE_START]     = "BRIDGE_START",
00106    [AST_CEL_BRIDGE_END]       = "BRIDGE_END",
00107    [AST_CEL_BRIDGE_UPDATE]    = "BRIDGE_UPDATE",
00108    [AST_CEL_CONF_START]       = "CONF_START",
00109    [AST_CEL_CONF_END]         = "CONF_END",
00110    [AST_CEL_PARK_START]       = "PARK_START",
00111    [AST_CEL_PARK_END]         = "PARK_END",
00112    [AST_CEL_TRANSFER]         = "TRANSFER",
00113    [AST_CEL_USER_DEFINED]     = "USER_DEFINED",
00114    [AST_CEL_CONF_ENTER]       = "CONF_ENTER",
00115    [AST_CEL_CONF_EXIT]        = "CONF_EXIT",
00116    [AST_CEL_BLINDTRANSFER]    = "BLINDTRANSFER",
00117    [AST_CEL_ATTENDEDTRANSFER] = "ATTENDEDTRANSFER",
00118    [AST_CEL_PICKUP]           = "PICKUP",
00119    [AST_CEL_FORWARD]          = "FORWARD",
00120    [AST_CEL_3WAY_START]       = "3WAY_START",
00121    [AST_CEL_3WAY_END]         = "3WAY_END",
00122    [AST_CEL_HOOKFLASH]        = "HOOKFLASH",
00123    [AST_CEL_LINKEDID_END]     = "LINKEDID_END",
00124 };
00125 
00126 /*!
00127  * \brief Map of ast_cel_ama_flags to strings
00128  */
00129 static const char * const cel_ama_flags[AST_CEL_AMA_FLAG_TOTAL] = {
00130    [AST_CEL_AMA_FLAG_NONE]          = "NONE",
00131    [AST_CEL_AMA_FLAG_OMIT]          = "OMIT",
00132    [AST_CEL_AMA_FLAG_BILLING]       = "BILLING",
00133    [AST_CEL_AMA_FLAG_DOCUMENTATION] = "DOCUMENTATION",
00134 };
00135 
00136 unsigned int ast_cel_check_enabled(void)
00137 {
00138    return cel_enabled;
00139 }
00140 
00141 static int print_app(void *obj, void *arg, int flags)
00142 {
00143    struct ast_cli_args *a = arg;
00144 
00145    ast_cli(a->fd, "CEL Tracking Application: %s\n", (const char *) obj);
00146 
00147    return 0;
00148 }
00149 
00150 static void print_cel_sub(const struct ast_event *event, void *data)
00151 {
00152    struct ast_cli_args *a = data;
00153 
00154    ast_cli(a->fd, "CEL Event Subscriber: %s\n",
00155          ast_event_get_ie_str(event, AST_EVENT_IE_DESCRIPTION));
00156 }
00157 
00158 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00159 {
00160    unsigned int i;
00161    struct ast_event_sub *sub;
00162 
00163    switch (cmd) {
00164    case CLI_INIT:
00165       e->command = "cel show status";
00166       e->usage =
00167          "Usage: cel show status\n"
00168          "       Displays the Channel Event Logging system status.\n";
00169       return NULL;
00170    case CLI_GENERATE:
00171       return NULL;
00172    case CLI_HANDLER:
00173       break;
00174    }
00175 
00176    if (a->argc > 3) {
00177       return CLI_SHOWUSAGE;
00178    }
00179 
00180    ast_cli(a->fd, "CEL Logging: %s\n", cel_enabled ? "Enabled" : "Disabled");
00181 
00182    if (!cel_enabled) {
00183       return CLI_SUCCESS;
00184    }
00185 
00186    for (i = 0; i < (sizeof(eventset) * 8); i++) {
00187       const char *name;
00188 
00189       if (!(eventset & ((int64_t) 1 << i))) {
00190          continue;
00191       }
00192 
00193       name = ast_cel_get_type_name(i);
00194       if (strcasecmp(name, "Unknown")) {
00195          ast_cli(a->fd, "CEL Tracking Event: %s\n", name);
00196       }
00197    }
00198 
00199    ao2_callback(appset, OBJ_NODATA, print_app, a);
00200 
00201    if (!(sub = ast_event_subscribe_new(AST_EVENT_SUB, print_cel_sub, a))) {
00202       return CLI_FAILURE;
00203    }
00204    ast_event_sub_append_ie_uint(sub, AST_EVENT_IE_EVENTTYPE, AST_EVENT_CEL);
00205    ast_event_report_subs(sub);
00206    ast_event_sub_destroy(sub);
00207    sub = NULL;
00208 
00209    return CLI_SUCCESS;
00210 }
00211 
00212 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CEL status");
00213 
00214 enum ast_cel_event_type ast_cel_str_to_event_type(const char *name)
00215 {
00216    unsigned int i;
00217 
00218    for (i = 0; i < ARRAY_LEN(cel_event_types); i++) {
00219       if (!cel_event_types[i]) {
00220          continue;
00221       }
00222 
00223       if (!strcasecmp(name, cel_event_types[i])) {
00224          return i;
00225       }
00226    }
00227 
00228    return -1;
00229 }
00230 
00231 static int ast_cel_track_event(enum ast_cel_event_type et)
00232 {
00233    return (eventset & ((int64_t) 1 << et));
00234 }
00235 
00236 static void parse_events(const char *val)
00237 {
00238    char *events = ast_strdupa(val);
00239    char *cur_event;
00240 
00241    while ((cur_event = strsep(&events, ","))) {
00242       enum ast_cel_event_type event_type;
00243 
00244       cur_event = ast_strip(cur_event);
00245       if (ast_strlen_zero(cur_event)) {
00246          continue;
00247       }
00248 
00249       event_type = ast_cel_str_to_event_type(cur_event);
00250 
00251       if (event_type == 0) {
00252          /* All events */
00253          eventset = (int64_t) -1;
00254       } else if (event_type == -1) {
00255          ast_log(LOG_WARNING, "Unknown event name '%s'\n",
00256                cur_event);
00257       } else {
00258          eventset |= ((int64_t) 1 << event_type);
00259       }
00260    }
00261 }
00262 
00263 static void parse_apps(const char *val)
00264 {
00265    char *apps = ast_strdupa(val);
00266    char *cur_app;
00267 
00268    if (!ast_cel_track_event(AST_CEL_APP_START) && !ast_cel_track_event(AST_CEL_APP_END)) {
00269       ast_log(LOG_WARNING, "An apps= config line, but not tracking APP events\n");
00270       return;
00271    }
00272 
00273    while ((cur_app = strsep(&apps, ","))) {
00274       char *app;
00275 
00276       cur_app = ast_strip(cur_app);
00277       if (ast_strlen_zero(cur_app)) {
00278          continue;
00279       }
00280 
00281       if (!(app = ao2_alloc(strlen(cur_app) + 1, NULL))) {
00282          continue;
00283       }
00284       strcpy(app, cur_app);
00285 
00286       ao2_link(appset, app);
00287       ao2_ref(app, -1);
00288       app = NULL;
00289    }
00290 }
00291 
00292 AST_MUTEX_DEFINE_STATIC(reload_lock);
00293 
00294 static int do_reload(void)
00295 {
00296    struct ast_config *config;
00297    const char *enabled_value;
00298    const char *val;
00299    int res = 0;
00300    struct ast_flags config_flags = { 0, };
00301    const char *s;
00302 
00303    ast_mutex_lock(&reload_lock);
00304 
00305    /* Reset all settings before reloading configuration */
00306    cel_enabled = CEL_ENABLED_DEFAULT;
00307    eventset = CEL_DEFAULT_EVENTS;
00308    *cel_dateformat = '\0';
00309    ao2_callback(appset, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
00310 
00311    config = ast_config_load2("cel.conf", "cel", config_flags);
00312 
00313    if (config == CONFIG_STATUS_FILEMISSING) {
00314       config = NULL;
00315       goto return_cleanup;
00316    }
00317 
00318    if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
00319       cel_enabled = ast_true(enabled_value);
00320    }
00321 
00322    if (!cel_enabled) {
00323       goto return_cleanup;
00324    }
00325 
00326    /* get the date format for logging */
00327    if ((s = ast_variable_retrieve(config, "general", "dateformat"))) {
00328       ast_copy_string(cel_dateformat, s, sizeof(cel_dateformat));
00329    }
00330 
00331    if ((val = ast_variable_retrieve(config, "general", "events"))) {
00332       parse_events(val);
00333    }
00334 
00335    if ((val = ast_variable_retrieve(config, "general", "apps"))) {
00336       parse_apps(val);
00337    }
00338 
00339 return_cleanup:
00340    ast_verb(3, "CEL logging %sabled.\n", cel_enabled ? "en" : "dis");
00341 
00342    ast_mutex_unlock(&reload_lock);
00343 
00344    if (config) {
00345       ast_config_destroy(config);
00346    }
00347 
00348    return res;
00349 }
00350 
00351 const char *ast_cel_get_type_name(enum ast_cel_event_type type)
00352 {
00353    return S_OR(cel_event_types[type], "Unknown");
00354 }
00355 
00356 const char *ast_cel_get_ama_flag_name(enum ast_cel_ama_flag flag)
00357 {
00358    if (flag < 0 || flag >= ARRAY_LEN(cel_ama_flags)) {
00359       ast_log(LOG_WARNING, "Invalid AMA flag: %d\n", flag);
00360       return "Unknown";
00361    }
00362 
00363    return S_OR(cel_ama_flags[flag], "Unknown");
00364 }
00365 
00366 /* called whenever a channel is destroyed or a linkedid is changed to
00367  * potentially emit a CEL_LINKEDID_END event */
00368 void ast_cel_check_retire_linkedid(struct ast_channel *chan)
00369 {
00370    const char *linkedid = chan->linkedid;
00371    char *lid;
00372 
00373    /* make sure we need to do all this work */
00374 
00375    if (ast_strlen_zero(linkedid) || !ast_cel_track_event(AST_CEL_LINKEDID_END)) {
00376       return;
00377    }
00378 
00379    if (!(lid = ao2_find(linkedids, (void *) linkedid, OBJ_POINTER))) {
00380       ast_log(LOG_ERROR, "Something weird happened, couldn't find linkedid %s\n", linkedid);
00381       return;
00382    }
00383 
00384    /* We have a ref for each channel with this linkedid, the link and the above find, so if
00385     * before unreffing the channel we have a refcount of 3, we're done. Unlink and report. */
00386    if (ao2_ref(lid, -1) == 3) {
00387       ao2_unlink(linkedids, lid);
00388       ast_cel_report_event(chan, AST_CEL_LINKEDID_END, NULL, NULL, NULL);
00389    }
00390    ao2_ref(lid, -1);
00391 }
00392 
00393 struct ast_channel *ast_cel_fabricate_channel_from_event(const struct ast_event *event)
00394 {
00395    struct varshead *headp;
00396    struct ast_var_t *newvariable;
00397    char timebuf[30];
00398    struct ast_channel *tchan;
00399    struct ast_cel_event_record record = {
00400       .version = AST_CEL_EVENT_RECORD_VERSION,
00401    };
00402 
00403    /* do not call ast_channel_alloc because this is not really a real channel */
00404    if (!(tchan = ast_dummy_channel_alloc())) {
00405       return NULL;
00406    }
00407 
00408    headp = &tchan->varshead;
00409 
00410    /* first, get the variables from the event */
00411    if (ast_cel_fill_record(event, &record)) {
00412       ast_channel_unref(tchan);
00413       return NULL;
00414    }
00415 
00416    /* next, fill the channel with their data */
00417    if ((newvariable = ast_var_assign("eventtype", record.event_name))) {
00418       AST_LIST_INSERT_HEAD(headp, newvariable, entries);
00419    }
00420 
00421    if (ast_strlen_zero(cel_dateformat)) {
00422       snprintf(timebuf, sizeof(timebuf), "%ld.%06ld", (long) record.event_time.tv_sec,
00423             (long) record.event_time.tv_usec);
00424    } else {
00425       struct ast_tm tm;
00426       ast_localtime(&record.event_time, &tm, NULL);
00427       ast_strftime(timebuf, sizeof(timebuf), cel_dateformat, &tm);
00428    }
00429 
00430    if ((newvariable = ast_var_assign("eventtime", timebuf))) {
00431       AST_LIST_INSERT_HEAD(headp, newvariable, entries);
00432    }
00433 
00434    if ((newvariable = ast_var_assign("userdeftype", record.user_defined_name))) {
00435       AST_LIST_INSERT_HEAD(headp, newvariable, entries);
00436    }
00437    if ((newvariable = ast_var_assign("eventextra", record.extra))) {
00438       AST_LIST_INSERT_HEAD(headp, newvariable, entries);
00439    }
00440 
00441    tchan->caller.id.name.valid = 1;
00442    tchan->caller.id.name.str = ast_strdup(record.caller_id_name);
00443    tchan->caller.id.number.valid = 1;
00444    tchan->caller.id.number.str = ast_strdup(record.caller_id_num);
00445    tchan->caller.ani.number.valid = 1;
00446    tchan->caller.ani.number.str = ast_strdup(record.caller_id_ani);
00447    tchan->redirecting.from.number.valid = 1;
00448    tchan->redirecting.from.number.str = ast_strdup(record.caller_id_rdnis);
00449    tchan->dialed.number.str = ast_strdup(record.caller_id_dnid);
00450 
00451    ast_copy_string(tchan->exten, record.extension, sizeof(tchan->exten));
00452    ast_copy_string(tchan->context, record.context, sizeof(tchan->context));
00453    ast_string_field_set(tchan, name, record.channel_name);
00454    ast_string_field_set(tchan, uniqueid, record.unique_id);
00455    ast_string_field_set(tchan, linkedid, record.linked_id);
00456    ast_string_field_set(tchan, accountcode, record.account_code);
00457    ast_string_field_set(tchan, peeraccount, record.peer_account);
00458    ast_string_field_set(tchan, userfield, record.user_field);
00459 
00460    if ((newvariable = ast_var_assign("BRIDGEPEER", record.peer))) {
00461       AST_LIST_INSERT_HEAD(headp, newvariable, entries);
00462    }
00463 
00464    tchan->appl = ast_strdup(record.application_name);
00465    tchan->data = ast_strdup(record.application_data);
00466    tchan->amaflags = record.amaflag;
00467 
00468    return tchan;
00469 }
00470 
00471 int ast_cel_linkedid_ref(const char *linkedid)
00472 {
00473    char *lid;
00474 
00475    if (ast_strlen_zero(linkedid)) {
00476       ast_log(LOG_ERROR, "The linkedid should never be empty\n");
00477       return -1;
00478    }
00479 
00480    if (!(lid = ao2_find(linkedids, (void *) linkedid, OBJ_POINTER))) {
00481       if (!(lid = ao2_alloc(strlen(linkedid) + 1, NULL))) {
00482          return -1;
00483       }
00484       strcpy(lid, linkedid);
00485       if (!ao2_link(linkedids, lid)) {
00486          ao2_ref(lid, -1);
00487          return -1;
00488       }
00489       /* Leave both the link and the alloc refs to show a count of 1 + the link */
00490    }
00491    /* If we've found, go ahead and keep the ref to increment count of how many channels
00492     * have this linkedid. We'll clean it up in check_retire */
00493    return 0;
00494 }
00495 
00496 int ast_cel_report_event(struct ast_channel *chan, enum ast_cel_event_type event_type,
00497       const char *userdefevname, const char *extra, struct ast_channel *peer2)
00498 {
00499    struct timeval eventtime;
00500    struct ast_event *ev;
00501    const char *peername = "";
00502    struct ast_channel *peer;
00503 
00504    /* Make sure a reload is not occurring while we're checking to see if this
00505     * is an event that we care about.  We could lose an important event in this
00506     * process otherwise. */
00507    ast_mutex_lock(&reload_lock);
00508 
00509    /* Record the linkedid of new channels if we are tracking LINKEDID_END even if we aren't
00510     * reporting on CHANNEL_START so we can track when to send LINKEDID_END */
00511    if (cel_enabled && ast_cel_track_event(AST_CEL_LINKEDID_END) && event_type == AST_CEL_CHANNEL_START && chan->linkedid) {
00512       if (ast_cel_linkedid_ref(chan->linkedid)) {
00513          ast_mutex_unlock(&reload_lock);
00514          return -1;
00515       }
00516    }
00517 
00518    if (!cel_enabled || !ast_cel_track_event(event_type)) {
00519       ast_mutex_unlock(&reload_lock);
00520       return 0;
00521    }
00522 
00523    if (event_type == AST_CEL_APP_START || event_type == AST_CEL_APP_END) {
00524       char *app;
00525       if (!(app = ao2_find(appset, (char *) chan->appl, OBJ_POINTER))) {
00526          ast_mutex_unlock(&reload_lock);
00527          return 0;
00528       }
00529       ao2_ref(app, -1);
00530    }
00531 
00532    ast_mutex_unlock(&reload_lock);
00533 
00534    ast_channel_lock(chan);
00535    peer = ast_bridged_channel(chan);
00536    if (peer) {
00537       ast_channel_ref(peer);
00538    }
00539    ast_channel_unlock(chan);
00540 
00541    if (peer) {
00542       ast_channel_lock(peer);
00543       peername = ast_strdupa(peer->name);
00544       ast_channel_unlock(peer);
00545    } else if (peer2) {
00546       ast_channel_lock(peer2);
00547       peername = ast_strdupa(peer2->name);
00548       ast_channel_unlock(peer2);
00549    }
00550 
00551    if (!userdefevname) {
00552       userdefevname = "";
00553    }
00554 
00555    if (!extra) {
00556       extra = "";
00557    }
00558 
00559    eventtime = ast_tvnow();
00560 
00561    ast_channel_lock(chan);
00562 
00563    ev = ast_event_new(AST_EVENT_CEL,
00564       AST_EVENT_IE_CEL_EVENT_TYPE, AST_EVENT_IE_PLTYPE_UINT, event_type,
00565       AST_EVENT_IE_CEL_EVENT_TIME, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_sec,
00566       AST_EVENT_IE_CEL_EVENT_TIME_USEC, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_usec,
00567       AST_EVENT_IE_CEL_USEREVENT_NAME, AST_EVENT_IE_PLTYPE_STR, userdefevname,
00568       AST_EVENT_IE_CEL_CIDNAME, AST_EVENT_IE_PLTYPE_STR,
00569          S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, ""),
00570       AST_EVENT_IE_CEL_CIDNUM, AST_EVENT_IE_PLTYPE_STR,
00571          S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, ""),
00572       AST_EVENT_IE_CEL_CIDANI, AST_EVENT_IE_PLTYPE_STR,
00573          S_COR(chan->caller.ani.number.valid, chan->caller.ani.number.str, ""),
00574       AST_EVENT_IE_CEL_CIDRDNIS, AST_EVENT_IE_PLTYPE_STR,
00575          S_COR(chan->redirecting.from.number.valid, chan->redirecting.from.number.str, ""),
00576       AST_EVENT_IE_CEL_CIDDNID, AST_EVENT_IE_PLTYPE_STR,
00577          S_OR(chan->dialed.number.str, ""),
00578       AST_EVENT_IE_CEL_EXTEN, AST_EVENT_IE_PLTYPE_STR, chan->exten,
00579       AST_EVENT_IE_CEL_CONTEXT, AST_EVENT_IE_PLTYPE_STR, chan->context,
00580       AST_EVENT_IE_CEL_CHANNAME, AST_EVENT_IE_PLTYPE_STR, chan->name,
00581       AST_EVENT_IE_CEL_APPNAME, AST_EVENT_IE_PLTYPE_STR, S_OR(chan->appl, ""),
00582       AST_EVENT_IE_CEL_APPDATA, AST_EVENT_IE_PLTYPE_STR, S_OR(chan->data, ""),
00583       AST_EVENT_IE_CEL_AMAFLAGS, AST_EVENT_IE_PLTYPE_UINT, chan->amaflags,
00584       AST_EVENT_IE_CEL_ACCTCODE, AST_EVENT_IE_PLTYPE_STR, chan->accountcode,
00585       AST_EVENT_IE_CEL_PEERACCT, AST_EVENT_IE_PLTYPE_STR, chan->peeraccount,
00586       AST_EVENT_IE_CEL_UNIQUEID, AST_EVENT_IE_PLTYPE_STR, chan->uniqueid,
00587       AST_EVENT_IE_CEL_LINKEDID, AST_EVENT_IE_PLTYPE_STR, chan->linkedid,
00588       AST_EVENT_IE_CEL_USERFIELD, AST_EVENT_IE_PLTYPE_STR, chan->userfield,
00589       AST_EVENT_IE_CEL_EXTRA, AST_EVENT_IE_PLTYPE_STR, extra,
00590       AST_EVENT_IE_CEL_PEER, AST_EVENT_IE_PLTYPE_STR, peername,
00591       AST_EVENT_IE_END);
00592 
00593    ast_channel_unlock(chan);
00594 
00595    if (peer) {
00596       peer = ast_channel_unref(peer);
00597    }
00598 
00599    if (ev && ast_event_queue(ev)) {
00600       ast_event_destroy(ev);
00601       return -1;
00602    }
00603 
00604    return 0;
00605 }
00606 
00607 int ast_cel_fill_record(const struct ast_event *e, struct ast_cel_event_record *r)
00608 {
00609    if (r->version != AST_CEL_EVENT_RECORD_VERSION) {
00610       ast_log(LOG_ERROR, "Module ABI mismatch for ast_cel_event_record.  "
00611             "Please ensure all modules were compiled for "
00612             "this version of Asterisk.\n");
00613       return -1;
00614    }
00615 
00616    r->event_type = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TYPE);
00617 
00618    r->event_time.tv_sec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME);
00619    r->event_time.tv_usec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME_USEC);
00620 
00621    r->user_defined_name = "";
00622 
00623    if (r->event_type == AST_CEL_USER_DEFINED) {
00624       r->user_defined_name = ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USEREVENT_NAME);
00625       r->event_name = r->user_defined_name;
00626    } else {
00627       r->event_name = ast_cel_get_type_name(r->event_type);
00628    }
00629 
00630    r->caller_id_name   = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNAME), "");
00631    r->caller_id_num    = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNUM), "");
00632    r->caller_id_ani    = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDANI), "");
00633    r->caller_id_rdnis  = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDRDNIS), "");
00634    r->caller_id_dnid   = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDDNID), "");
00635    r->extension        = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTEN), "");
00636    r->context          = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CONTEXT), "");
00637    r->channel_name     = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CHANNAME), "");
00638    r->application_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPNAME), "");
00639    r->application_data = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPDATA), "");
00640    r->account_code     = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_ACCTCODE), "");
00641    r->peer_account     = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_ACCTCODE), "");
00642    r->unique_id        = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_UNIQUEID), "");
00643    r->linked_id        = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_LINKEDID), "");
00644    r->amaflag          = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_AMAFLAGS);
00645    r->user_field       = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USERFIELD), "");
00646    r->peer             = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_PEER), "");
00647    r->extra            = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTRA), "");
00648 
00649    return 0;
00650 }
00651 
00652 static int app_hash(const void *obj, const int flags)
00653 {
00654    return ast_str_case_hash((const char *) obj);
00655 }
00656 
00657 static int app_cmp(void *obj, void *arg, int flags)
00658 {
00659    const char *app1 = obj, *app2 = arg;
00660 
00661    return !strcasecmp(app1, app2) ? CMP_MATCH | CMP_STOP : 0;
00662 }
00663 
00664 #define lid_hash app_hash
00665 #define lid_cmp app_cmp
00666 
00667 static void ast_cel_engine_term(void)
00668 {
00669    if (appset) {
00670       ao2_ref(appset, -1);
00671       appset = NULL;
00672    }
00673 }
00674 
00675 int ast_cel_engine_init(void)
00676 {
00677    if (!(appset = ao2_container_alloc(NUM_APP_BUCKETS, app_hash, app_cmp))) {
00678       return -1;
00679    }
00680    if (!(linkedids = ao2_container_alloc(NUM_APP_BUCKETS, lid_hash, lid_cmp))) {
00681       ao2_ref(appset, -1);
00682       return -1;
00683    }
00684 
00685    if (do_reload() || ast_cli_register(&cli_status)) {
00686       ao2_ref(appset, -1);
00687       appset = NULL;
00688       ao2_ref(linkedids, -1);
00689       linkedids = NULL;
00690       return -1;
00691    }
00692 
00693    ast_register_atexit(ast_cel_engine_term);
00694 
00695    return 0;
00696 }
00697 
00698 int ast_cel_engine_reload(void)
00699 {
00700    return do_reload();
00701 }
00702 

Generated on 30 Aug 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1