2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007 - 2009, Digium, Inc.
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
20 * \brief Channel Event Logging API
22 * \author Steve Murphy <murf@digium.com>
23 * \author Russell Bryant <russell@digium.com>
25 * \todo Do thorough testing of all transfer methods to ensure that BLINDTRANSFER,
26 * ATTENDEDTRANSFER, BRIDGE_START, and BRIDGE_END events are all reported
30 /*! \li \ref cel.c uses the configuration file \ref cel.conf
31 * \addtogroup configuration_file Configuration Files
35 * \page cel.conf cel.conf
36 * \verbinclude cel.conf.sample
40 <support_level>core</support_level>
45 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
47 #include "asterisk/_private.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/pbx.h"
51 #include "asterisk/cel.h"
52 #include "asterisk/logger.h"
53 #include "asterisk/linkedlists.h"
54 #include "asterisk/utils.h"
55 #include "asterisk/config.h"
56 #include "asterisk/config_options.h"
57 #include "asterisk/cli.h"
58 #include "asterisk/astobj2.h"
59 #include "asterisk/stasis_message_router.h"
60 #include "asterisk/stasis_channels.h"
61 #include "asterisk/stasis_bridging.h"
62 #include "asterisk/bridging.h"
63 #include "asterisk/parking.h"
66 <configInfo name="cel" language="en_US">
67 <configFile name="cel.conf">
68 <configObject name="general">
69 <synopsis>Options that apply globally to Channel Event Logging (CEL)</synopsis>
70 <configOption name="enable">
71 <synopsis>Determines whether CEL is enabled</synopsis>
73 <configOption name="dateformat">
74 <synopsis>The format to be used for dates when logging</synopsis>
76 <configOption name="apps">
77 <synopsis>List of apps for CEL to track</synopsis>
78 <description><para>A case-insensitive, comma-separated list of applications
79 to track when one or both of APP_START and APP_END events are flagged for
80 tracking</para></description>
82 <configOption name="events">
83 <synopsis>List of events for CEL to track</synopsis>
84 <description><para>A case-sensitive, comma-separated list of event names
85 to track. These event names do not include the leading <literal>AST_CEL</literal>.
89 <para>Special value which tracks all events.</para>
91 <enum name="CHAN_START"/>
92 <enum name="CHAN_END"/>
95 <enum name="APP_START"/>
96 <enum name="APP_END"/>
97 <enum name="BRIDGE_START"/>
98 <enum name="BRIDGE_END"/>
99 <enum name="BRIDGE_UPDATE"/>
100 <enum name="BRIDGE_TO_CONF"/>
101 <enum name="CONF_START"/>
102 <enum name="CONF_END"/>
103 <enum name="PARK_START"/>
104 <enum name="PARK_END"/>
105 <enum name="TRANSFER"/>
106 <enum name="USER_DEFINED"/>
107 <enum name="CONF_ENTER"/>
108 <enum name="CONF_EXIT"/>
109 <enum name="BLINDTRANSFER"/>
110 <enum name="ATTENDEDTRANSFER"/>
111 <enum name="PICKUP"/>
112 <enum name="FORWARD"/>
113 <enum name="3WAY_START"/>
114 <enum name="3WAY_END"/>
115 <enum name="HOOKFLASH"/>
116 <enum name="LINKEDID_END"/>
126 /*! Message router for state that CEL needs to know about */
127 static struct stasis_message_router *cel_state_router;
129 /*! Aggregation topic for all topics CEL needs to know about */
130 static struct stasis_topic *cel_state_topic;
132 /*! Subscription for forwarding the channel caching topic */
133 static struct stasis_subscription *cel_channel_forwarder;
135 /*! Subscription for forwarding the channel caching topic */
136 static struct stasis_subscription *cel_bridge_forwarder;
138 /*! Subscription for forwarding the parking topic */
139 static struct stasis_subscription *cel_parking_forwarder;
141 /*! Container for primary channel/bridge ID listing for 2 party bridges */
142 static struct ao2_container *bridge_primaries;
144 /*! The number of buckets into which primary channel uniqueids will be hashed */
145 #define BRIDGE_PRIMARY_BUCKETS 251
147 /*! Container for dial end multichannel blobs for holding on to dial statuses */
148 static struct ao2_container *cel_dialstatus_store;
151 * \brief Maximum possible CEL event IDs
152 * \note This limit is currently imposed by the eventset definition
154 #define CEL_MAX_EVENT_IDS 64
157 * \brief Number of buckets for the appset container
159 #define NUM_APP_BUCKETS 97
162 * \brief Number of buckets for the dialstatus container
164 #define NUM_DIALSTATUS_BUCKETS 251
167 * \brief Container of Asterisk application names
169 * The apps in this container are the applications that were specified
170 * in the configuration as applications that CEL events should be generated
171 * for when they start and end on a channel.
173 static struct ao2_container *linkedids;
175 /*! \brief A structure to hold global configuration-related options */
176 struct cel_general_config {
177 AST_DECLARE_STRING_FIELDS(
178 AST_STRING_FIELD(date_format); /*!< The desired date format for logging */
180 int enable; /*!< Whether CEL is enabled */
181 int64_t events; /*!< The events to be logged */
182 struct ao2_container *apps; /*!< The apps for which to log app start and end events */
185 /*! \brief Destructor for cel_config */
186 static void cel_general_config_dtor(void *obj)
188 struct cel_general_config *cfg = obj;
189 ast_string_field_free_memory(cfg);
190 ao2_cleanup(cfg->apps);
194 static void *cel_general_config_alloc(void)
196 RAII_VAR(struct cel_general_config *, cfg, NULL, ao2_cleanup);
198 if (!(cfg = ao2_alloc(sizeof(*cfg), cel_general_config_dtor))) {
202 if (ast_string_field_init(cfg, 64)) {
206 if (!(cfg->apps = ast_str_container_alloc(NUM_APP_BUCKETS))) {
214 /*! \brief A container that holds all config-related information */
216 struct cel_general_config *general;
220 static AO2_GLOBAL_OBJ_STATIC(cel_configs);
222 /*! \brief Destructor for cel_config */
223 static void cel_config_dtor(void *obj)
225 struct cel_config *cfg = obj;
226 ao2_cleanup(cfg->general);
230 static void *cel_config_alloc(void)
232 RAII_VAR(struct cel_config *, cfg, NULL, ao2_cleanup);
234 if (!(cfg = ao2_alloc(sizeof(*cfg), cel_config_dtor))) {
238 if (!(cfg->general = cel_general_config_alloc())) {
246 /*! \brief An aco_type structure to link the "general" category to the cel_general_config type */
247 static struct aco_type general_option = {
250 .item_offset = offsetof(struct cel_config, general),
251 .category_match = ACO_WHITELIST,
252 .category = "^general$",
255 /*! \brief The config file to be processed for the module. */
256 static struct aco_file cel_conf = {
257 .filename = "cel.conf", /*!< The name of the config file */
258 .types = ACO_TYPES(&general_option), /*!< The mapping object types to be processed */
259 .skip_category = "(^manager$|^radius$)", /*!< Config sections used by existing modules. Do not add to this list. */
262 static int cel_pre_apply_config(void);
264 CONFIG_INFO_CORE("cel", cel_cfg_info, cel_configs, cel_config_alloc,
265 .files = ACO_FILES(&cel_conf),
266 .pre_apply_config = cel_pre_apply_config,
269 static int cel_pre_apply_config(void)
271 struct cel_config *cfg = aco_pending_config(&cel_cfg_info);
277 if (!ao2_container_count(cfg->general->apps)) {
281 if (cfg->general->events & ((int64_t) 1 << AST_CEL_APP_START)) {
285 if (cfg->general->events & ((int64_t) 1 << AST_CEL_APP_END)) {
289 ast_log(LOG_ERROR, "Applications are listed to be tracked, but APP events are not tracked\n");
293 static struct aco_type *general_options[] = ACO_TYPES(&general_option);
296 * \brief Map of ast_cel_event_type to strings
298 static const char * const cel_event_types[CEL_MAX_EVENT_IDS] = {
300 [AST_CEL_CHANNEL_START] = "CHAN_START",
301 [AST_CEL_CHANNEL_END] = "CHAN_END",
302 [AST_CEL_ANSWER] = "ANSWER",
303 [AST_CEL_HANGUP] = "HANGUP",
304 [AST_CEL_APP_START] = "APP_START",
305 [AST_CEL_APP_END] = "APP_END",
306 [AST_CEL_BRIDGE_START] = "BRIDGE_START",
307 [AST_CEL_BRIDGE_END] = "BRIDGE_END",
308 [AST_CEL_BRIDGE_UPDATE] = "BRIDGE_UPDATE",
309 [AST_CEL_BRIDGE_TO_CONF] = "BRIDGE_TO_CONF",
310 [AST_CEL_CONF_START] = "CONF_START",
311 [AST_CEL_CONF_END] = "CONF_END",
312 [AST_CEL_PARK_START] = "PARK_START",
313 [AST_CEL_PARK_END] = "PARK_END",
314 [AST_CEL_TRANSFER] = "TRANSFER",
315 [AST_CEL_USER_DEFINED] = "USER_DEFINED",
316 [AST_CEL_CONF_ENTER] = "CONF_ENTER",
317 [AST_CEL_CONF_EXIT] = "CONF_EXIT",
318 [AST_CEL_BLINDTRANSFER] = "BLINDTRANSFER",
319 [AST_CEL_ATTENDEDTRANSFER] = "ATTENDEDTRANSFER",
320 [AST_CEL_PICKUP] = "PICKUP",
321 [AST_CEL_FORWARD] = "FORWARD",
322 [AST_CEL_3WAY_START] = "3WAY_START",
323 [AST_CEL_3WAY_END] = "3WAY_END",
324 [AST_CEL_HOOKFLASH] = "HOOKFLASH",
325 [AST_CEL_LINKEDID_END] = "LINKEDID_END",
328 struct bridge_assoc {
329 AST_DECLARE_STRING_FIELDS(
330 AST_STRING_FIELD(bridge_id); /*!< UniqueID of the bridge */
331 AST_STRING_FIELD(secondary_name); /*!< UniqueID of the secondary/dialed channel */
333 struct ast_channel_snapshot *primary_snapshot; /*!< The snapshot for the initiating channel in the bridge */
334 int track_as_conf; /*!< Whether this bridge will be treated like a conference in CEL terms */
337 static void bridge_assoc_dtor(void *obj)
339 struct bridge_assoc *assoc = obj;
340 ast_string_field_free_memory(assoc);
341 ao2_cleanup(assoc->primary_snapshot);
342 assoc->primary_snapshot = NULL;
345 static struct bridge_assoc *bridge_assoc_alloc(struct ast_channel_snapshot *primary, const char *bridge_id, const char *secondary_name)
347 RAII_VAR(struct bridge_assoc *, assoc, ao2_alloc(sizeof(*assoc), bridge_assoc_dtor), ao2_cleanup);
348 if (!primary || !assoc || ast_string_field_init(assoc, 64)) {
352 ast_string_field_set(assoc, bridge_id, bridge_id);
353 ast_string_field_set(assoc, secondary_name, secondary_name);
355 assoc->primary_snapshot = primary;
356 ao2_ref(primary, +1);
362 static int add_bridge_primary(struct ast_channel_snapshot *primary, const char *bridge_id, const char *secondary_name)
364 RAII_VAR(struct bridge_assoc *, assoc, bridge_assoc_alloc(primary, bridge_id, secondary_name), ao2_cleanup);
369 ao2_link(bridge_primaries, assoc);
373 static void remove_bridge_primary(const char *channel_id)
375 ao2_find(bridge_primaries, channel_id, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK | OBJ_KEY);
378 /*! \brief Hashing function for bridge_assoc */
379 static int bridge_assoc_hash(const void *obj, int flags)
381 const struct bridge_assoc *assoc = obj;
382 const char *uniqueid = obj;
383 if (!(flags & OBJ_KEY)) {
384 uniqueid = assoc->primary_snapshot->uniqueid;
387 return ast_str_hash(uniqueid);
390 /*! \brief Comparator function for bridge_assoc */
391 static int bridge_assoc_cmp(void *obj, void *arg, int flags)
393 struct bridge_assoc *assoc1 = obj, *assoc2 = arg;
394 const char *assoc2_id = arg, *assoc1_id = assoc1->primary_snapshot->uniqueid;
395 if (!(flags & OBJ_KEY)) {
396 assoc2_id = assoc2->primary_snapshot->uniqueid;
399 return !strcmp(assoc1_id, assoc2_id) ? CMP_MATCH | CMP_STOP : 0;
402 static const char *get_caller_uniqueid(struct ast_multi_channel_blob *blob)
404 struct ast_channel_snapshot *caller = ast_multi_channel_blob_get_channel(blob, "caller");
409 return caller->uniqueid;
412 /*! \brief Hashing function for dialstatus container */
413 static int dialstatus_hash(const void *obj, int flags)
415 struct ast_multi_channel_blob *blob = (void *) obj;
416 const char *uniqueid = obj;
417 if (!(flags & OBJ_KEY)) {
418 uniqueid = get_caller_uniqueid(blob);
421 return ast_str_hash(uniqueid);
424 /*! \brief Comparator function for dialstatus container */
425 static int dialstatus_cmp(void *obj, void *arg, int flags)
427 struct ast_multi_channel_blob *blob1 = obj, *blob2 = arg;
428 const char *blob2_id = arg, *blob1_id = get_caller_uniqueid(blob1);
429 if (!(flags & OBJ_KEY)) {
430 blob2_id = get_caller_uniqueid(blob2);
433 return !strcmp(blob1_id, blob2_id) ? CMP_MATCH | CMP_STOP : 0;
437 * \brief Map of ast_cel_ama_flags to strings
439 static const char * const cel_ama_flags[AST_CEL_AMA_FLAG_TOTAL] = {
440 [AST_CEL_AMA_FLAG_NONE] = "NONE",
441 [AST_CEL_AMA_FLAG_OMIT] = "OMIT",
442 [AST_CEL_AMA_FLAG_BILLING] = "BILLING",
443 [AST_CEL_AMA_FLAG_DOCUMENTATION] = "DOCUMENTATION",
446 unsigned int ast_cel_check_enabled(void)
448 RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
450 if (!cfg || !cfg->general) {
454 return cfg->general->enable;
457 static int print_app(void *obj, void *arg, int flags)
459 struct ast_cli_args *a = arg;
461 ast_cli(a->fd, "CEL Tracking Application: %s\n", (const char *) obj);
466 static void print_cel_sub(const struct ast_event *event, void *data)
468 struct ast_cli_args *a = data;
470 ast_cli(a->fd, "CEL Event Subscriber: %s\n",
471 ast_event_get_ie_str(event, AST_EVENT_IE_DESCRIPTION));
474 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
477 struct ast_event_sub *sub;
478 RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
480 if (!cfg || !cfg->general) {
486 e->command = "cel show status";
488 "Usage: cel show status\n"
489 " Displays the Channel Event Logging system status.\n";
498 return CLI_SHOWUSAGE;
501 ast_cli(a->fd, "CEL Logging: %s\n", ast_cel_check_enabled() ? "Enabled" : "Disabled");
503 if (!cfg->general->enable) {
507 for (i = 0; i < (sizeof(cfg->general->events) * 8); i++) {
510 if (!(cfg->general->events & ((int64_t) 1 << i))) {
514 name = ast_cel_get_type_name(i);
515 if (strcasecmp(name, "Unknown")) {
516 ast_cli(a->fd, "CEL Tracking Event: %s\n", name);
520 ao2_callback(cfg->general->apps, OBJ_NODATA, print_app, a);
522 if (!(sub = ast_event_subscribe_new(AST_EVENT_SUB, print_cel_sub, a))) {
525 ast_event_sub_append_ie_uint(sub, AST_EVENT_IE_EVENTTYPE, AST_EVENT_CEL);
526 ast_event_report_subs(sub);
527 ast_event_sub_destroy(sub);
533 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CEL status");
535 enum ast_cel_event_type ast_cel_str_to_event_type(const char *name)
539 for (i = 0; i < ARRAY_LEN(cel_event_types); i++) {
540 if (!cel_event_types[i]) {
544 if (!strcasecmp(name, cel_event_types[i])) {
552 static int ast_cel_track_event(enum ast_cel_event_type et)
554 RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
556 if (!cfg || !cfg->general) {
560 return (cfg->general->events & ((int64_t) 1 << et));
563 static int events_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
565 struct cel_general_config *cfg = obj;
566 char *events = ast_strdupa(var->value);
569 while ((cur_event = strsep(&events, ","))) {
570 enum ast_cel_event_type event_type;
572 cur_event = ast_strip(cur_event);
573 if (ast_strlen_zero(cur_event)) {
577 event_type = ast_cel_str_to_event_type(cur_event);
579 if (event_type == 0) {
581 cfg->events = (int64_t) -1;
582 } else if (event_type == -1) {
583 ast_log(LOG_ERROR, "Unknown event name '%s'\n", cur_event);
586 cfg->events |= ((int64_t) 1 << event_type);
593 static int apps_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
595 struct cel_general_config *cfg = obj;
596 char *apps = ast_strdupa(var->value);
599 while ((cur_app = strsep(&apps, ","))) {
600 cur_app = ast_strip(cur_app);
601 if (ast_strlen_zero(cur_app)) {
605 cur_app = ast_str_to_lower(cur_app);
606 ast_str_container_add(cfg->apps, cur_app);
612 static int do_reload(void)
614 if (aco_process_config(&cel_cfg_info, 1)) {
618 ast_verb(3, "CEL logging %sabled.\n", ast_cel_check_enabled() ? "en" : "dis");
623 const char *ast_cel_get_type_name(enum ast_cel_event_type type)
625 return S_OR(cel_event_types[type], "Unknown");
628 const char *ast_cel_get_ama_flag_name(enum ast_cel_ama_flag flag)
630 if (flag < 0 || flag >= ARRAY_LEN(cel_ama_flags)) {
631 ast_log(LOG_WARNING, "Invalid AMA flag: %d\n", flag);
635 return S_OR(cel_ama_flags[flag], "Unknown");
638 static int cel_track_app(const char *const_app)
640 RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
641 RAII_VAR(char *, app, NULL, ao2_cleanup);
644 if (!cfg || !cfg->general) {
648 app_lower = ast_str_to_lower(ast_strdupa(const_app));
649 app = ao2_find(cfg->general->apps, app_lower, OBJ_KEY);
657 static int report_event_snapshot(struct ast_channel_snapshot *snapshot,
658 enum ast_cel_event_type event_type, const char *userdefevname,
659 const char *extra, const char *peer2_name)
661 struct timeval eventtime;
662 struct ast_event *ev;
663 char *linkedid = ast_strdupa(snapshot->linkedid);
664 const char *peer_name = peer2_name;
665 RAII_VAR(struct bridge_assoc *, assoc, NULL, ao2_cleanup);
666 RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
668 if (!cfg || !cfg->general) {
672 if (!cfg->general->enable) {
676 if (ast_strlen_zero(peer_name)) {
677 assoc = ao2_find(bridge_primaries, snapshot->uniqueid, OBJ_KEY);
679 peer_name = assoc->secondary_name;
683 /* Record the linkedid of new channels if we are tracking LINKEDID_END even if we aren't
684 * reporting on CHANNEL_START so we can track when to send LINKEDID_END */
685 if (ast_cel_track_event(AST_CEL_LINKEDID_END) && event_type == AST_CEL_CHANNEL_START && linkedid) {
686 if (ast_cel_linkedid_ref(linkedid)) {
691 if (!ast_cel_track_event(event_type)) {
695 if ((event_type == AST_CEL_APP_START || event_type == AST_CEL_APP_END)
696 && !cel_track_app(snapshot->appl)) {
700 eventtime = ast_tvnow();
702 ev = ast_event_new(AST_EVENT_CEL,
703 AST_EVENT_IE_CEL_EVENT_TYPE, AST_EVENT_IE_PLTYPE_UINT, event_type,
704 AST_EVENT_IE_CEL_EVENT_TIME, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_sec,
705 AST_EVENT_IE_CEL_EVENT_TIME_USEC, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_usec,
706 AST_EVENT_IE_CEL_USEREVENT_NAME, AST_EVENT_IE_PLTYPE_STR, S_OR(userdefevname, ""),
707 AST_EVENT_IE_CEL_CIDNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_name,
708 AST_EVENT_IE_CEL_CIDNUM, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_number,
709 AST_EVENT_IE_CEL_CIDANI, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_ani,
710 AST_EVENT_IE_CEL_CIDRDNIS, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_rdnis,
711 AST_EVENT_IE_CEL_CIDDNID, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_dnid,
712 AST_EVENT_IE_CEL_EXTEN, AST_EVENT_IE_PLTYPE_STR, snapshot->exten,
713 AST_EVENT_IE_CEL_CONTEXT, AST_EVENT_IE_PLTYPE_STR, snapshot->context,
714 AST_EVENT_IE_CEL_CHANNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->name,
715 AST_EVENT_IE_CEL_APPNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->appl,
716 AST_EVENT_IE_CEL_APPDATA, AST_EVENT_IE_PLTYPE_STR, snapshot->data,
717 AST_EVENT_IE_CEL_AMAFLAGS, AST_EVENT_IE_PLTYPE_UINT, snapshot->amaflags,
718 AST_EVENT_IE_CEL_ACCTCODE, AST_EVENT_IE_PLTYPE_STR, snapshot->accountcode,
719 AST_EVENT_IE_CEL_PEERACCT, AST_EVENT_IE_PLTYPE_STR, snapshot->peeraccount,
720 AST_EVENT_IE_CEL_UNIQUEID, AST_EVENT_IE_PLTYPE_STR, snapshot->uniqueid,
721 AST_EVENT_IE_CEL_LINKEDID, AST_EVENT_IE_PLTYPE_STR, snapshot->linkedid,
722 AST_EVENT_IE_CEL_USERFIELD, AST_EVENT_IE_PLTYPE_STR, snapshot->userfield,
723 AST_EVENT_IE_CEL_EXTRA, AST_EVENT_IE_PLTYPE_STR, S_OR(extra, ""),
724 AST_EVENT_IE_CEL_PEER, AST_EVENT_IE_PLTYPE_STR, S_OR(peer_name, ""),
727 if (ev && ast_event_queue(ev)) {
728 ast_event_destroy(ev);
735 /* called whenever a channel is destroyed or a linkedid is changed to
736 * potentially emit a CEL_LINKEDID_END event */
737 static void check_retire_linkedid(struct ast_channel_snapshot *snapshot)
741 /* make sure we need to do all this work */
743 if (ast_strlen_zero(snapshot->linkedid) || !ast_cel_track_event(AST_CEL_LINKEDID_END)) {
747 if (!(lid = ao2_find(linkedids, (void *) snapshot->linkedid, OBJ_POINTER))) {
748 ast_log(LOG_ERROR, "Something weird happened, couldn't find linkedid %s\n", snapshot->linkedid);
752 /* We have a ref for each channel with this linkedid, the link and the above find, so if
753 * before unreffing the channel we have a refcount of 3, we're done. Unlink and report. */
754 if (ao2_ref(lid, -1) == 3) {
755 ast_str_container_remove(linkedids, lid);
756 report_event_snapshot(snapshot, AST_CEL_LINKEDID_END, NULL, NULL, NULL);
761 /* Note that no 'chan_fixup' function is provided for this datastore type,
762 * because the channels that will use it will never be involved in masquerades.
764 static const struct ast_datastore_info fabricated_channel_datastore = {
765 .type = "CEL fabricated channel",
766 .destroy = ast_free_ptr,
769 struct ast_channel *ast_cel_fabricate_channel_from_event(const struct ast_event *event)
771 struct varshead *headp;
772 struct ast_var_t *newvariable;
773 const char *mixed_name;
775 struct ast_channel *tchan;
776 struct ast_cel_event_record record = {
777 .version = AST_CEL_EVENT_RECORD_VERSION,
779 struct ast_datastore *datastore;
781 RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
783 if (!cfg || !cfg->general) {
787 /* do not call ast_channel_alloc because this is not really a real channel */
788 if (!(tchan = ast_dummy_channel_alloc())) {
792 headp = ast_channel_varshead(tchan);
794 /* first, get the variables from the event */
795 if (ast_cel_fill_record(event, &record)) {
796 ast_channel_unref(tchan);
800 /* next, fill the channel with their data */
801 mixed_name = (record.event_type == AST_CEL_USER_DEFINED)
802 ? record.user_defined_name : record.event_name;
803 if ((newvariable = ast_var_assign("eventtype", mixed_name))) {
804 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
807 if (ast_strlen_zero(cfg->general->date_format)) {
808 snprintf(timebuf, sizeof(timebuf), "%ld.%06ld", (long) record.event_time.tv_sec,
809 (long) record.event_time.tv_usec);
812 ast_localtime(&record.event_time, &tm, NULL);
813 ast_strftime(timebuf, sizeof(timebuf), cfg->general->date_format, &tm);
816 if ((newvariable = ast_var_assign("eventtime", timebuf))) {
817 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
820 if ((newvariable = ast_var_assign("eventenum", record.event_name))) {
821 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
823 if ((newvariable = ast_var_assign("userdeftype", record.user_defined_name))) {
824 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
826 if ((newvariable = ast_var_assign("eventextra", record.extra))) {
827 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
830 ast_channel_caller(tchan)->id.name.valid = 1;
831 ast_channel_caller(tchan)->id.name.str = ast_strdup(record.caller_id_name);
832 ast_channel_caller(tchan)->id.number.valid = 1;
833 ast_channel_caller(tchan)->id.number.str = ast_strdup(record.caller_id_num);
834 ast_channel_caller(tchan)->ani.number.valid = 1;
835 ast_channel_caller(tchan)->ani.number.str = ast_strdup(record.caller_id_ani);
836 ast_channel_redirecting(tchan)->from.number.valid = 1;
837 ast_channel_redirecting(tchan)->from.number.str = ast_strdup(record.caller_id_rdnis);
838 ast_channel_dialed(tchan)->number.str = ast_strdup(record.caller_id_dnid);
840 ast_channel_exten_set(tchan, record.extension);
841 ast_channel_context_set(tchan, record.context);
842 ast_channel_name_set(tchan, record.channel_name);
843 ast_channel_uniqueid_set(tchan, record.unique_id);
844 ast_channel_linkedid_set(tchan, record.linked_id);
845 ast_channel_accountcode_set(tchan, record.account_code);
846 ast_channel_peeraccount_set(tchan, record.peer_account);
847 ast_channel_userfield_set(tchan, record.user_field);
849 if ((newvariable = ast_var_assign("BRIDGEPEER", record.peer))) {
850 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
853 ast_channel_amaflags_set(tchan, record.amaflag);
855 /* We need to store an 'application name' and 'application
856 * data' on the channel for logging purposes, but the channel
857 * structure only provides a place to store pointers, and it
858 * expects these pointers to be pointing to data that does not
859 * need to be freed. This means that the channel's destructor
860 * does not attempt to free any storage that these pointers
861 * point to. However, we can't provide data in that form directly for
862 * these structure members. In order to ensure that these data
863 * elements have a lifetime that matches the channel's
864 * lifetime, we'll put them in a datastore attached to the
865 * channel, and set's the channel's pointers to point into the
866 * datastore. The datastore will then be automatically destroyed
867 * when the channel is destroyed.
870 if (!(datastore = ast_datastore_alloc(&fabricated_channel_datastore, NULL))) {
871 ast_channel_unref(tchan);
875 if (!(app_data = ast_malloc(strlen(record.application_name) + strlen(record.application_data) + 2))) {
876 ast_datastore_free(datastore);
877 ast_channel_unref(tchan);
881 ast_channel_appl_set(tchan, strcpy(app_data, record.application_name));
882 ast_channel_data_set(tchan, strcpy(app_data + strlen(record.application_name) + 1,
883 record.application_data));
885 datastore->data = app_data;
886 ast_channel_datastore_add(tchan, datastore);
891 int ast_cel_linkedid_ref(const char *linkedid)
895 if (ast_strlen_zero(linkedid)) {
896 ast_log(LOG_ERROR, "The linkedid should never be empty\n");
900 if (!(lid = ao2_find(linkedids, (void *) linkedid, OBJ_POINTER))) {
901 if (!(lid = ao2_alloc(strlen(linkedid) + 1, NULL))) {
904 strcpy(lid, linkedid);
905 if (!ao2_link(linkedids, lid)) {
909 /* Leave both the link and the alloc refs to show a count of 1 + the link */
911 /* If we've found, go ahead and keep the ref to increment count of how many channels
912 * have this linkedid. We'll clean it up in check_retire */
916 int ast_cel_report_event(struct ast_channel *chan, enum ast_cel_event_type event_type,
917 const char *userdefevname, const char *extra, struct ast_channel *peer2)
919 struct timeval eventtime;
920 struct ast_event *ev;
921 const char *peername = "";
922 struct ast_channel *peer;
923 char *linkedid = ast_strdupa(ast_channel_linkedid(chan));
925 if (!ast_cel_check_enabled()) {
929 /* Record the linkedid of new channels if we are tracking LINKEDID_END even if we aren't
930 * reporting on CHANNEL_START so we can track when to send LINKEDID_END */
931 if (ast_cel_track_event(AST_CEL_LINKEDID_END) && event_type == AST_CEL_CHANNEL_START && linkedid) {
932 if (ast_cel_linkedid_ref(linkedid)) {
937 if (!ast_cel_track_event(event_type)) {
941 if ((event_type == AST_CEL_APP_START || event_type == AST_CEL_APP_END)
942 && !cel_track_app(ast_channel_appl(chan))) {
946 ast_channel_lock(chan);
947 peer = ast_bridged_channel(chan);
949 ast_channel_ref(peer);
951 ast_channel_unlock(chan);
954 ast_channel_lock(peer);
955 peername = ast_strdupa(ast_channel_name(peer));
956 ast_channel_unlock(peer);
958 ast_channel_lock(peer2);
959 peername = ast_strdupa(ast_channel_name(peer2));
960 ast_channel_unlock(peer2);
963 if (!userdefevname) {
971 eventtime = ast_tvnow();
973 ast_channel_lock(chan);
975 ev = ast_event_new(AST_EVENT_CEL,
976 AST_EVENT_IE_CEL_EVENT_TYPE, AST_EVENT_IE_PLTYPE_UINT, event_type,
977 AST_EVENT_IE_CEL_EVENT_TIME, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_sec,
978 AST_EVENT_IE_CEL_EVENT_TIME_USEC, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_usec,
979 AST_EVENT_IE_CEL_USEREVENT_NAME, AST_EVENT_IE_PLTYPE_STR, userdefevname,
980 AST_EVENT_IE_CEL_CIDNAME, AST_EVENT_IE_PLTYPE_STR,
981 S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, ""),
982 AST_EVENT_IE_CEL_CIDNUM, AST_EVENT_IE_PLTYPE_STR,
983 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, ""),
984 AST_EVENT_IE_CEL_CIDANI, AST_EVENT_IE_PLTYPE_STR,
985 S_COR(ast_channel_caller(chan)->ani.number.valid, ast_channel_caller(chan)->ani.number.str, ""),
986 AST_EVENT_IE_CEL_CIDRDNIS, AST_EVENT_IE_PLTYPE_STR,
987 S_COR(ast_channel_redirecting(chan)->from.number.valid, ast_channel_redirecting(chan)->from.number.str, ""),
988 AST_EVENT_IE_CEL_CIDDNID, AST_EVENT_IE_PLTYPE_STR,
989 S_OR(ast_channel_dialed(chan)->number.str, ""),
990 AST_EVENT_IE_CEL_EXTEN, AST_EVENT_IE_PLTYPE_STR, ast_channel_exten(chan),
991 AST_EVENT_IE_CEL_CONTEXT, AST_EVENT_IE_PLTYPE_STR, ast_channel_context(chan),
992 AST_EVENT_IE_CEL_CHANNAME, AST_EVENT_IE_PLTYPE_STR, ast_channel_name(chan),
993 AST_EVENT_IE_CEL_APPNAME, AST_EVENT_IE_PLTYPE_STR, S_OR(ast_channel_appl(chan), ""),
994 AST_EVENT_IE_CEL_APPDATA, AST_EVENT_IE_PLTYPE_STR, S_OR(ast_channel_data(chan), ""),
995 AST_EVENT_IE_CEL_AMAFLAGS, AST_EVENT_IE_PLTYPE_UINT, ast_channel_amaflags(chan),
996 AST_EVENT_IE_CEL_ACCTCODE, AST_EVENT_IE_PLTYPE_STR, ast_channel_accountcode(chan),
997 AST_EVENT_IE_CEL_PEERACCT, AST_EVENT_IE_PLTYPE_STR, ast_channel_peeraccount(chan),
998 AST_EVENT_IE_CEL_UNIQUEID, AST_EVENT_IE_PLTYPE_STR, ast_channel_uniqueid(chan),
999 AST_EVENT_IE_CEL_LINKEDID, AST_EVENT_IE_PLTYPE_STR, ast_channel_linkedid(chan),
1000 AST_EVENT_IE_CEL_USERFIELD, AST_EVENT_IE_PLTYPE_STR, ast_channel_userfield(chan),
1001 AST_EVENT_IE_CEL_EXTRA, AST_EVENT_IE_PLTYPE_STR, extra,
1002 AST_EVENT_IE_CEL_PEER, AST_EVENT_IE_PLTYPE_STR, peername,
1005 ast_channel_unlock(chan);
1008 peer = ast_channel_unref(peer);
1011 if (ev && ast_event_queue(ev)) {
1012 ast_event_destroy(ev);
1019 int ast_cel_fill_record(const struct ast_event *e, struct ast_cel_event_record *r)
1021 if (r->version != AST_CEL_EVENT_RECORD_VERSION) {
1022 ast_log(LOG_ERROR, "Module ABI mismatch for ast_cel_event_record. "
1023 "Please ensure all modules were compiled for "
1024 "this version of Asterisk.\n");
1028 r->event_type = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TYPE);
1030 r->event_time.tv_sec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME);
1031 r->event_time.tv_usec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME_USEC);
1033 r->event_name = ast_cel_get_type_name(r->event_type);
1034 if (r->event_type == AST_CEL_USER_DEFINED) {
1035 r->user_defined_name = ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USEREVENT_NAME);
1037 r->user_defined_name = "";
1040 r->caller_id_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNAME), "");
1041 r->caller_id_num = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNUM), "");
1042 r->caller_id_ani = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDANI), "");
1043 r->caller_id_rdnis = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDRDNIS), "");
1044 r->caller_id_dnid = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDDNID), "");
1045 r->extension = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTEN), "");
1046 r->context = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CONTEXT), "");
1047 r->channel_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CHANNAME), "");
1048 r->application_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPNAME), "");
1049 r->application_data = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPDATA), "");
1050 r->account_code = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_ACCTCODE), "");
1051 r->peer_account = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_ACCTCODE), "");
1052 r->unique_id = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_UNIQUEID), "");
1053 r->linked_id = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_LINKEDID), "");
1054 r->amaflag = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_AMAFLAGS);
1055 r->user_field = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USERFIELD), "");
1056 r->peer = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_PEER), "");
1057 r->extra = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTRA), "");
1062 /*! \brief Typedef for callbacks that get called on channel snapshot updates */
1063 typedef void (*cel_channel_snapshot_monitor)(
1064 struct ast_channel_snapshot *old_snapshot,
1065 struct ast_channel_snapshot *new_snapshot);
1067 static struct ast_multi_channel_blob *get_dialstatus_blob(const char *uniqueid)
1069 return ao2_find(cel_dialstatus_store, uniqueid, OBJ_KEY | OBJ_UNLINK);
1072 static const char *get_caller_dialstatus(struct ast_multi_channel_blob *blob)
1074 struct ast_json *json = ast_multi_channel_blob_get_json(blob);
1079 json = ast_json_object_get(json, "dialstatus");
1084 return ast_json_string_get(json);
1087 /*! \brief Handle channel state changes */
1088 static void cel_channel_state_change(
1089 struct ast_channel_snapshot *old_snapshot,
1090 struct ast_channel_snapshot *new_snapshot)
1092 int is_hungup, was_hungup;
1094 if (!new_snapshot) {
1095 report_event_snapshot(old_snapshot, AST_CEL_CHANNEL_END, NULL, NULL, NULL);
1096 check_retire_linkedid(old_snapshot);
1100 if (!old_snapshot) {
1101 report_event_snapshot(new_snapshot, AST_CEL_CHANNEL_START, NULL, NULL, NULL);
1105 was_hungup = ast_test_flag(&old_snapshot->flags, AST_FLAG_ZOMBIE) ? 1 : 0;
1106 is_hungup = ast_test_flag(&new_snapshot->flags, AST_FLAG_ZOMBIE) ? 1 : 0;
1108 if (!was_hungup && is_hungup) {
1109 RAII_VAR(struct ast_str *, extra_str, ast_str_create(128), ast_free);
1110 RAII_VAR(struct ast_multi_channel_blob *, blob, get_dialstatus_blob(new_snapshot->uniqueid), ao2_cleanup);
1111 const char *dialstatus = "";
1112 if (blob && !ast_strlen_zero(get_caller_dialstatus(blob))) {
1113 dialstatus = get_caller_dialstatus(blob);
1115 ast_str_set(&extra_str, 0, "%d,%s,%s",
1116 new_snapshot->hangupcause,
1117 new_snapshot->hangupsource,
1119 report_event_snapshot(new_snapshot, AST_CEL_HANGUP, NULL, ast_str_buffer(extra_str), NULL);
1123 if (old_snapshot->state != new_snapshot->state && new_snapshot->state == AST_STATE_UP) {
1124 report_event_snapshot(new_snapshot, AST_CEL_ANSWER, NULL, NULL, NULL);
1129 static void cel_channel_linkedid_change(
1130 struct ast_channel_snapshot *old_snapshot,
1131 struct ast_channel_snapshot *new_snapshot)
1133 if (!old_snapshot || !new_snapshot) {
1137 if (strcmp(old_snapshot->linkedid, new_snapshot->linkedid)) {
1138 check_retire_linkedid(old_snapshot);
1142 static void cel_channel_app_change(
1143 struct ast_channel_snapshot *old_snapshot,
1144 struct ast_channel_snapshot *new_snapshot)
1146 if (new_snapshot && old_snapshot
1147 && !strcmp(old_snapshot->appl, new_snapshot->appl)) {
1151 /* old snapshot has an application, end it */
1152 if (old_snapshot && !ast_strlen_zero(old_snapshot->appl)) {
1153 report_event_snapshot(old_snapshot, AST_CEL_APP_END, NULL, NULL, NULL);
1156 /* new snapshot has an application, start it */
1157 if (new_snapshot && !ast_strlen_zero(new_snapshot->appl)) {
1158 report_event_snapshot(new_snapshot, AST_CEL_APP_START, NULL, NULL, NULL);
1162 cel_channel_snapshot_monitor cel_channel_monitors[] = {
1163 cel_channel_state_change,
1164 cel_channel_app_change,
1165 cel_channel_linkedid_change,
1168 static void update_bridge_primary(struct ast_channel_snapshot *snapshot)
1170 RAII_VAR(struct bridge_assoc *, assoc, NULL, ao2_cleanup);
1176 assoc = ao2_find(bridge_primaries, snapshot->uniqueid, OBJ_KEY);
1181 ao2_cleanup(assoc->primary_snapshot);
1182 ao2_ref(snapshot, +1);
1183 assoc->primary_snapshot = snapshot;
1186 static int bridge_match_cb(void *obj, void *arg, int flags)
1188 struct bridge_assoc *assoc = obj;
1189 char *bridge_id = arg;
1190 ast_assert(flags & OBJ_KEY);
1191 if (!strcmp(bridge_id, assoc->bridge_id)) {
1197 static struct bridge_assoc *find_bridge_primary_by_bridge_id(const char *bridge_id)
1199 char *dup_id = ast_strdupa(bridge_id);
1200 return ao2_callback(bridge_primaries, OBJ_KEY, bridge_match_cb, dup_id);
1203 static void clear_bridge_primary(const char *bridge_id)
1205 char *dup_id = ast_strdupa(bridge_id);
1206 ao2_callback(bridge_primaries, OBJ_KEY | OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, bridge_match_cb, dup_id);
1209 static void cel_snapshot_update_cb(void *data, struct stasis_subscription *sub,
1210 struct stasis_topic *topic,
1211 struct stasis_message *message)
1213 struct stasis_cache_update *update = stasis_message_data(message);
1214 if (ast_channel_snapshot_type() == update->type) {
1215 struct ast_channel_snapshot *old_snapshot;
1216 struct ast_channel_snapshot *new_snapshot;
1219 old_snapshot = stasis_message_data(update->old_snapshot);
1220 new_snapshot = stasis_message_data(update->new_snapshot);
1222 update_bridge_primary(new_snapshot);
1224 for (i = 0; i < ARRAY_LEN(cel_channel_monitors); ++i) {
1225 cel_channel_monitors[i](old_snapshot, new_snapshot);
1227 } else if (ast_bridge_snapshot_type() == update->type) {
1228 RAII_VAR(struct bridge_assoc *, assoc, NULL, ao2_cleanup);
1229 struct ast_bridge_snapshot *old_snapshot;
1230 struct ast_bridge_snapshot *new_snapshot;
1232 update = stasis_message_data(message);
1234 old_snapshot = stasis_message_data(update->old_snapshot);
1235 new_snapshot = stasis_message_data(update->new_snapshot);
1237 if (!old_snapshot) {
1241 if (!new_snapshot) {
1242 clear_bridge_primary(old_snapshot->uniqueid);
1246 if (old_snapshot->capabilities == new_snapshot->capabilities) {
1250 /* handle 1:1/native -> multimix */
1251 if ((old_snapshot->capabilities & (AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE))
1252 && (new_snapshot->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX)) {
1253 assoc = find_bridge_primary_by_bridge_id(new_snapshot->uniqueid);
1255 /* this bridge will no longer be treated like a bridge, so mark the bridge_assoc as such */
1256 assoc->track_as_conf = 1;
1257 report_event_snapshot(assoc->primary_snapshot, AST_CEL_BRIDGE_TO_CONF, NULL, NULL, assoc->secondary_name);
1261 /* handle multimix -> 1:1/native */
1262 if ((old_snapshot->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX)
1263 && (new_snapshot->capabilities & (AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE))) {
1264 struct ao2_iterator i;
1265 RAII_VAR(char *, channel_id, NULL, ao2_cleanup);
1266 RAII_VAR(struct ast_channel_snapshot *, chan_snapshot, NULL, ao2_cleanup);
1268 assoc = find_bridge_primary_by_bridge_id(new_snapshot->uniqueid);
1270 assoc->track_as_conf = 1;
1274 /* get the first item in the container */
1275 i = ao2_iterator_init(new_snapshot->channels, 0);
1276 while ((channel_id = ao2_iterator_next(&i))) {
1279 ao2_iterator_destroy(&i);
1281 /* create a bridge_assoc for this bridge and mark it as being tracked appropriately */
1282 chan_snapshot = ast_channel_snapshot_get_latest(channel_id);
1283 ast_assert(chan_snapshot != NULL);
1284 assoc = bridge_assoc_alloc(chan_snapshot, new_snapshot->uniqueid, chan_snapshot->name);
1288 assoc->track_as_conf = 1;
1290 ao2_link(bridge_primaries, assoc);
1296 static void cel_bridge_enter_cb(
1297 void *data, struct stasis_subscription *sub,
1298 struct stasis_topic *topic,
1299 struct stasis_message *message)
1301 struct ast_bridge_blob *blob = stasis_message_data(message);
1302 struct ast_bridge_snapshot *snapshot = blob->bridge;
1303 struct ast_channel_snapshot *chan_snapshot = blob->channel;
1305 if (snapshot->capabilities & (AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE)) {
1306 RAII_VAR(struct bridge_assoc *, assoc, find_bridge_primary_by_bridge_id(snapshot->uniqueid), ao2_cleanup);
1307 if (assoc && assoc->track_as_conf) {
1308 report_event_snapshot(chan_snapshot, AST_CEL_CONF_ENTER, NULL, NULL, NULL);
1312 if (ao2_container_count(snapshot->channels) == 2) {
1313 struct ao2_iterator i;
1314 RAII_VAR(char *, channel_id, NULL, ao2_cleanup);
1315 RAII_VAR(struct ast_channel_snapshot *, latest_primary, NULL, ao2_cleanup);
1317 /* get the name of the channel in the container we don't already know the name of */
1318 i = ao2_iterator_init(snapshot->channels, 0);
1319 while ((channel_id = ao2_iterator_next(&i))) {
1320 if (strcmp(channel_id, chan_snapshot->uniqueid)) {
1323 ao2_cleanup(channel_id);
1326 ao2_iterator_destroy(&i);
1328 latest_primary = ast_channel_snapshot_get_latest(channel_id);
1329 add_bridge_primary(latest_primary, snapshot->uniqueid, chan_snapshot->name);
1330 report_event_snapshot(latest_primary, AST_CEL_BRIDGE_START, NULL, NULL, chan_snapshot->name);
1332 } else if (snapshot->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
1333 report_event_snapshot(chan_snapshot, AST_CEL_CONF_ENTER, NULL, NULL, NULL);
1337 static void cel_bridge_leave_cb(
1338 void *data, struct stasis_subscription *sub,
1339 struct stasis_topic *topic,
1340 struct stasis_message *message)
1342 struct ast_bridge_blob *blob = stasis_message_data(message);
1343 struct ast_bridge_snapshot *snapshot = blob->bridge;
1344 struct ast_channel_snapshot *chan_snapshot = blob->channel;
1346 if (snapshot->capabilities & (AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE)) {
1347 RAII_VAR(struct bridge_assoc *, assoc,
1348 find_bridge_primary_by_bridge_id(snapshot->uniqueid),
1355 if (assoc->track_as_conf) {
1356 report_event_snapshot(chan_snapshot, AST_CEL_CONF_EXIT, NULL, NULL, NULL);
1360 if (ao2_container_count(snapshot->channels) == 1) {
1361 report_event_snapshot(assoc->primary_snapshot, AST_CEL_BRIDGE_END, NULL, NULL, assoc->secondary_name);
1362 remove_bridge_primary(assoc->primary_snapshot->uniqueid);
1365 } else if (snapshot->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
1366 report_event_snapshot(chan_snapshot, AST_CEL_CONF_EXIT, NULL, NULL, NULL);
1370 static void cel_parking_cb(
1371 void *data, struct stasis_subscription *sub,
1372 struct stasis_topic *topic,
1373 struct stasis_message *message)
1375 struct ast_parked_call_payload *parked_payload = stasis_message_data(message);
1377 switch (parked_payload->event_type) {
1379 report_event_snapshot(parked_payload->parkee, AST_CEL_PARK_START, NULL,
1380 parked_payload->parkinglot,
1381 S_COR(parked_payload->parker, parked_payload->parker->name, NULL));
1383 case PARKED_CALL_TIMEOUT:
1384 report_event_snapshot(parked_payload->parkee, AST_CEL_PARK_END, NULL, "ParkedCallTimeOut", NULL);
1386 case PARKED_CALL_GIVEUP:
1387 report_event_snapshot(parked_payload->parkee, AST_CEL_PARK_END, NULL, "ParkedCallGiveUp", NULL);
1389 case PARKED_CALL_UNPARKED:
1390 report_event_snapshot(parked_payload->parkee, AST_CEL_PARK_END, NULL, "ParkedCallUnparked", NULL);
1392 case PARKED_CALL_FAILED:
1393 report_event_snapshot(parked_payload->parkee, AST_CEL_PARK_END, NULL, "ParkedCallFailed", NULL);
1398 static void save_dialstatus(struct ast_multi_channel_blob *blob)
1400 ao2_link(cel_dialstatus_store, blob);
1403 static void cel_dial_cb(void *data, struct stasis_subscription *sub,
1404 struct stasis_topic *topic,
1405 struct stasis_message *message)
1407 struct ast_multi_channel_blob *blob = stasis_message_data(message);
1409 if (!get_caller_uniqueid(blob)) {
1413 if (ast_strlen_zero(get_caller_dialstatus(blob))) {
1417 save_dialstatus(blob);
1420 static void ast_cel_engine_term(void)
1422 stasis_message_router_unsubscribe_and_join(cel_state_router);
1423 cel_state_router = NULL;
1424 ao2_cleanup(cel_state_topic);
1425 cel_state_topic = NULL;
1426 cel_channel_forwarder = stasis_unsubscribe_and_join(cel_channel_forwarder);
1427 cel_bridge_forwarder = stasis_unsubscribe_and_join(cel_bridge_forwarder);
1428 cel_parking_forwarder = stasis_unsubscribe_and_join(cel_parking_forwarder);
1429 ao2_cleanup(linkedids);
1431 ast_cli_unregister(&cli_status);
1432 ao2_cleanup(bridge_primaries);
1433 bridge_primaries = NULL;
1436 int ast_cel_engine_init(void)
1439 if (!(linkedids = ast_str_container_alloc(NUM_APP_BUCKETS))) {
1443 if (!(cel_dialstatus_store = ao2_container_alloc(NUM_DIALSTATUS_BUCKETS, dialstatus_hash, dialstatus_cmp))) {
1447 if (aco_info_init(&cel_cfg_info)) {
1451 aco_option_register(&cel_cfg_info, "enable", ACO_EXACT, general_options, "no", OPT_BOOL_T, 1, FLDSET(struct cel_general_config, enable));
1452 aco_option_register(&cel_cfg_info, "dateformat", ACO_EXACT, general_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct cel_general_config, date_format));
1453 aco_option_register_custom(&cel_cfg_info, "apps", ACO_EXACT, general_options, "", apps_handler, 0);
1454 aco_option_register_custom(&cel_cfg_info, "events", ACO_EXACT, general_options, "", events_handler, 0);
1456 if (aco_process_config(&cel_cfg_info, 0)) {
1460 if (ast_cli_register(&cli_status)) {
1464 bridge_primaries = ao2_container_alloc(BRIDGE_PRIMARY_BUCKETS, bridge_assoc_hash, bridge_assoc_cmp);
1465 if (!bridge_primaries) {
1469 cel_state_topic = stasis_topic_create("cel_state_topic");
1470 if (!cel_state_topic) {
1474 cel_channel_forwarder = stasis_forward_all(
1475 stasis_caching_get_topic(ast_channel_topic_all_cached()),
1477 if (!cel_channel_forwarder) {
1481 cel_bridge_forwarder = stasis_forward_all(
1482 stasis_caching_get_topic(ast_bridge_topic_all_cached()),
1484 if (!cel_bridge_forwarder) {
1488 cel_parking_forwarder = stasis_forward_all(
1489 ast_parking_topic(),
1491 if (!cel_parking_forwarder) {
1495 cel_state_router = stasis_message_router_create(cel_state_topic);
1496 if (!cel_state_router) {
1500 ret |= stasis_message_router_add(cel_state_router,
1501 stasis_cache_update_type(),
1502 cel_snapshot_update_cb,
1505 ret |= stasis_message_router_add(cel_state_router,
1506 ast_channel_dial_type(),
1510 ret |= stasis_message_router_add(cel_state_router,
1511 ast_channel_entered_bridge_type(),
1512 cel_bridge_enter_cb,
1515 ret |= stasis_message_router_add(cel_state_router,
1516 ast_channel_left_bridge_type(),
1517 cel_bridge_leave_cb,
1520 ret |= stasis_message_router_add(cel_state_router,
1521 ast_parked_call_type(),
1525 /* If somehow we failed to add any routes, just shut down the whole
1526 * thing and fail it.
1529 ast_cel_engine_term();
1533 ast_register_cleanup(ast_cel_engine_term);
1538 int ast_cel_engine_reload(void)