Asterisk - The Open Source Telephony Project  18.5.0
cel.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007 - 2009, Digium, Inc.
5  *
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.
11  *
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.
15  */
16 
17 /*!
18  * \file
19  *
20  * \brief Channel Event Logging API
21  *
22  * \author Steve Murphy <[email protected]>
23  * \author Russell Bryant <[email protected]>
24  */
25 
26 /*! \li \ref cel.c uses the configuration file \ref cel.conf
27  * \addtogroup configuration_file Configuration Files
28  */
29 
30 /*!
31  * \page cel.conf cel.conf
32  * \verbinclude cel.conf.sample
33  */
34 
35 /*** MODULEINFO
36  <support_level>core</support_level>
37  ***/
38 
39 #include "asterisk.h"
40 
41 #include "asterisk/module.h"
42 
43 #include "asterisk/channel.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/cel.h"
46 #include "asterisk/logger.h"
47 #include "asterisk/linkedlists.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/config.h"
51 #include "asterisk/cli.h"
52 #include "asterisk/astobj2.h"
56 #include "asterisk/bridge.h"
57 #include "asterisk/parking.h"
58 #include "asterisk/pickup.h"
59 #include "asterisk/core_local.h"
60 #include "asterisk/taskprocessor.h"
61 
62 /*** DOCUMENTATION
63  <configInfo name="cel" language="en_US">
64  <configFile name="cel.conf">
65  <configObject name="general">
66  <synopsis>Options that apply globally to Channel Event Logging (CEL)</synopsis>
67  <configOption name="enable">
68  <synopsis>Determines whether CEL is enabled</synopsis>
69  </configOption>
70  <configOption name="dateformat">
71  <synopsis>The format to be used for dates when logging</synopsis>
72  </configOption>
73  <configOption name="apps">
74  <synopsis>List of apps for CEL to track</synopsis>
75  <description><para>A case-insensitive, comma-separated list of applications
76  to track when one or both of APP_START and APP_END events are flagged for
77  tracking</para></description>
78  </configOption>
79  <configOption name="events">
80  <synopsis>List of events for CEL to track</synopsis>
81  <description><para>A case-sensitive, comma-separated list of event names
82  to track. These event names do not include the leading <literal>AST_CEL</literal>.
83  </para>
84  <enumlist>
85  <enum name="ALL">
86  <para>Special value which tracks all events.</para>
87  </enum>
88  <enum name="CHAN_START"/>
89  <enum name="CHAN_END"/>
90  <enum name="ANSWER"/>
91  <enum name="HANGUP"/>
92  <enum name="APP_START"/>
93  <enum name="APP_END"/>
94  <enum name="PARK_START"/>
95  <enum name="PARK_END"/>
96  <enum name="USER_DEFINED"/>
97  <enum name="BRIDGE_ENTER"/>
98  <enum name="BRIDGE_EXIT"/>
99  <enum name="BLINDTRANSFER"/>
100  <enum name="ATTENDEDTRANSFER"/>
101  <enum name="PICKUP"/>
102  <enum name="FORWARD"/>
103  <enum name="LINKEDID_END"/>
104  <enum name="LOCAL_OPTIMIZE"/>
105  </enumlist>
106  </description>
107  </configOption>
108  </configObject>
109  </configFile>
110  </configInfo>
111  ***/
112 
113 /*! Message router for state that CEL needs to know about */
115 
116 /*! Topic for CEL-specific messages */
117 static struct stasis_topic *cel_topic;
118 
119 /*! Aggregation topic for all topics CEL needs to know about */
121 
122 /*! Subscription for forwarding the channel caching topic */
124 
125 /*! Subscription for forwarding the channel caching topic */
127 
128 /*! Subscription for forwarding the parking topic */
130 
131 /*! Subscription for forwarding the CEL-specific topic */
133 
136 
137 /*! Container for CEL backend information */
138 static AO2_GLOBAL_OBJ_STATIC(cel_backends);
139 
140 /*! The number of buckets into which backend names will be hashed */
141 #define BACKEND_BUCKETS 13
142 
143 /*! Container for dial end multichannel blobs for holding on to dial statuses */
144 static AO2_GLOBAL_OBJ_STATIC(cel_dialstatus_store);
145 
146 /*!
147  * \brief Maximum possible CEL event IDs
148  * \note This limit is currently imposed by the eventset definition
149  */
150 #define CEL_MAX_EVENT_IDS 64
151 
152 /*!
153  * \brief Number of buckets for the appset container
154  */
155 #define NUM_APP_BUCKETS 97
156 
157 /*!
158  * \brief Number of buckets for the dialstatus container
159  */
160 #define NUM_DIALSTATUS_BUCKETS 251
161 
162 struct cel_linkedid {
163  /*! Number of channels with this linkedid. */
164  unsigned int count;
165  /*! Linkedid stored at end of struct. */
166  char id[0];
167 };
168 
169 /*! Container of channel references to a linkedid for CEL purposes. */
170 static AO2_GLOBAL_OBJ_STATIC(cel_linkedids);
171 
173  /*! Uniqueid of the channel */
174  char uniqueid[AST_MAX_UNIQUEID];
175  /*! The dial status */
176  char dialstatus[0];
177 };
178 
179 /*! \brief Destructor for cel_config */
180 static void cel_general_config_dtor(void *obj)
181 {
182  struct ast_cel_general_config *cfg = obj;
184  ao2_cleanup(cfg->apps);
185  cfg->apps = NULL;
186 }
187 
189 {
191 
192  if (!(cfg = ao2_alloc(sizeof(*cfg), cel_general_config_dtor))) {
193  return NULL;
194  }
195 
196  if (ast_string_field_init(cfg, 64)) {
197  return NULL;
198  }
199 
200  if (!(cfg->apps = ast_str_container_alloc(NUM_APP_BUCKETS))) {
201  return NULL;
202  }
203 
204  ao2_ref(cfg, +1);
205  return cfg;
206 }
207 
208 /*! \brief A container that holds all config-related information */
209 struct cel_config {
211 };
212 
213 
214 static AO2_GLOBAL_OBJ_STATIC(cel_configs);
215 
216 /*! \brief Destructor for cel_config */
217 static void cel_config_dtor(void *obj)
218 {
219  struct cel_config *cfg = obj;
220  ao2_cleanup(cfg->general);
221  cfg->general = NULL;
222 }
223 
224 static void *cel_config_alloc(void)
225 {
226  RAII_VAR(struct cel_config *, cfg, NULL, ao2_cleanup);
227 
228  if (!(cfg = ao2_alloc(sizeof(*cfg), cel_config_dtor))) {
229  return NULL;
230  }
231 
232  if (!(cfg->general = ast_cel_general_config_alloc())) {
233  return NULL;
234  }
235 
236  ao2_ref(cfg, +1);
237  return cfg;
238 }
239 
240 /*! \brief An aco_type structure to link the "general" category to the ast_cel_general_config type */
241 static struct aco_type general_option = {
242  .type = ACO_GLOBAL,
243  .name = "general",
244  .item_offset = offsetof(struct cel_config, general),
245  .category_match = ACO_WHITELIST_EXACT,
246  .category = "general",
247 };
248 
249 /*! Config sections used by existing modules. Do not add to this list. */
250 static const char *ignore_categories[] = {
251  "manager",
252  "radius",
253  NULL,
254 };
255 
256 static struct aco_type ignore_option = {
257  .type = ACO_IGNORE,
258  .name = "modules",
259  .category = (const char*)ignore_categories,
261 };
262 
263 /*! \brief The config file to be processed for the module. */
264 static struct aco_file cel_conf = {
265  .filename = "cel.conf", /*!< The name of the config file */
266  .types = ACO_TYPES(&general_option, &ignore_option), /*!< The mapping object types to be processed */
267 };
268 
269 static int cel_pre_apply_config(void);
270 
271 CONFIG_INFO_CORE("cel", cel_cfg_info, cel_configs, cel_config_alloc,
272  .files = ACO_FILES(&cel_conf),
273  .pre_apply_config = cel_pre_apply_config,
274 );
275 
276 static int cel_pre_apply_config(void)
277 {
278  struct cel_config *cfg = aco_pending_config(&cel_cfg_info);
279 
280  if (!cfg->general) {
281  return -1;
282  }
283 
284  if (!ao2_container_count(cfg->general->apps)) {
285  return 0;
286  }
287 
288  if (cfg->general->events & ((int64_t) 1 << AST_CEL_APP_START)) {
289  return 0;
290  }
291 
292  if (cfg->general->events & ((int64_t) 1 << AST_CEL_APP_END)) {
293  return 0;
294  }
295 
296  ast_log(LOG_ERROR, "Applications are listed to be tracked, but APP events are not tracked\n");
297  return -1;
298 }
299 
300 static struct aco_type *general_options[] = ACO_TYPES(&general_option);
301 
302 /*!
303  * \brief Map of ast_cel_event_type to strings
304  */
305 static const char * const cel_event_types[CEL_MAX_EVENT_IDS] = {
306  [AST_CEL_ALL] = "ALL",
307  [AST_CEL_CHANNEL_START] = "CHAN_START",
308  [AST_CEL_CHANNEL_END] = "CHAN_END",
309  [AST_CEL_ANSWER] = "ANSWER",
310  [AST_CEL_HANGUP] = "HANGUP",
311  [AST_CEL_APP_START] = "APP_START",
312  [AST_CEL_APP_END] = "APP_END",
313  [AST_CEL_PARK_START] = "PARK_START",
314  [AST_CEL_PARK_END] = "PARK_END",
315  [AST_CEL_USER_DEFINED] = "USER_DEFINED",
316  [AST_CEL_BRIDGE_ENTER] = "BRIDGE_ENTER",
317  [AST_CEL_BRIDGE_EXIT] = "BRIDGE_EXIT",
318  [AST_CEL_BLINDTRANSFER] = "BLINDTRANSFER",
319  [AST_CEL_ATTENDEDTRANSFER] = "ATTENDEDTRANSFER",
320  [AST_CEL_PICKUP] = "PICKUP",
321  [AST_CEL_FORWARD] = "FORWARD",
322  [AST_CEL_LINKEDID_END] = "LINKEDID_END",
323  [AST_CEL_LOCAL_OPTIMIZE] = "LOCAL_OPTIMIZE",
324 };
325 
326 struct cel_backend {
327  ast_cel_backend_cb callback; /*!< Callback for this backend */
328  char name[0]; /*!< Name of this backend */
329 };
330 
331 /*! \brief Hashing function for cel_backend */
333 
334 /*! \brief Comparator function for cel_backend */
336 
337 /*! \brief Hashing function for dialstatus container */
339 
340 /*! \brief Comparator function for dialstatus container */
342 
343 unsigned int ast_cel_check_enabled(void)
344 {
345  unsigned int enabled;
346  struct cel_config *cfg = ao2_global_obj_ref(cel_configs);
347 
348  enabled = (!cfg || !cfg->general) ? 0 : cfg->general->enable;
349  ao2_cleanup(cfg);
350  return enabled;
351 }
352 
353 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
354 {
355  unsigned int i;
356  RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
357  RAII_VAR(struct ao2_container *, backends, ao2_global_obj_ref(cel_backends), ao2_cleanup);
358  struct ao2_iterator iter;
359  char *app;
360 
361  switch (cmd) {
362  case CLI_INIT:
363  e->command = "cel show status";
364  e->usage =
365  "Usage: cel show status\n"
366  " Displays the Channel Event Logging system status.\n";
367  return NULL;
368  case CLI_GENERATE:
369  return NULL;
370  case CLI_HANDLER:
371  break;
372  }
373 
374  if (a->argc > 3) {
375  return CLI_SHOWUSAGE;
376  }
377 
378  ast_cli(a->fd, "CEL Logging: %s\n", ast_cel_check_enabled() ? "Enabled" : "Disabled");
379 
380  if (!cfg || !cfg->general || !cfg->general->enable) {
381  return CLI_SUCCESS;
382  }
383 
384  for (i = 0; i < (sizeof(cfg->general->events) * 8); i++) {
385  const char *name;
386 
387  if (!(cfg->general->events & ((int64_t) 1 << i))) {
388  continue;
389  }
390 
391  name = ast_cel_get_type_name(i);
392  if (strcasecmp(name, "Unknown")) {
393  ast_cli(a->fd, "CEL Tracking Event: %s\n", name);
394  }
395  }
396 
397  iter = ao2_iterator_init(cfg->general->apps, 0);
398  for (; (app = ao2_iterator_next(&iter)); ao2_ref(app, -1)) {
399  ast_cli(a->fd, "CEL Tracking Application: %s\n", app);
400  }
401  ao2_iterator_destroy(&iter);
402 
403  if (backends) {
404  struct cel_backend *backend;
405 
406  iter = ao2_iterator_init(backends, 0);
407  for (; (backend = ao2_iterator_next(&iter)); ao2_ref(backend, -1)) {
408  ast_cli(a->fd, "CEL Event Subscriber: %s\n", backend->name);
409  }
410  ao2_iterator_destroy(&iter);
411  }
412 
413  return CLI_SUCCESS;
414 }
415 
416 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CEL status");
417 
419 {
420  unsigned int i;
421 
422  for (i = 0; i < ARRAY_LEN(cel_event_types); i++) {
423  if (cel_event_types[i] && !strcasecmp(name, cel_event_types[i])) {
424  return i;
425  }
426  }
427 
428  ast_log(LOG_ERROR, "Unknown event name '%s'\n", name);
429  return AST_CEL_INVALID_VALUE;
430 }
431 
433 {
434  RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
435 
436  if (!cfg || !cfg->general) {
437  return 0;
438  }
439 
440  return (cfg->general->events & ((int64_t) 1 << et)) ? 1 : 0;
441 }
442 
443 static int events_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
444 {
445  struct ast_cel_general_config *cfg = obj;
446  char *events = ast_strdupa(var->value);
447  char *cur_event;
448 
449  while ((cur_event = strsep(&events, ","))) {
450  enum ast_cel_event_type event_type;
451 
452  cur_event = ast_strip(cur_event);
453  if (ast_strlen_zero(cur_event)) {
454  continue;
455  }
456 
457  event_type = ast_cel_str_to_event_type(cur_event);
458 
459  if (event_type == AST_CEL_ALL) {
460  /* All events */
461  cfg->events = (int64_t) -1;
462  } else if (event_type == AST_CEL_INVALID_VALUE) {
463  return -1;
464  } else {
465  cfg->events |= ((int64_t) 1 << event_type);
466  }
467  }
468 
469  return 0;
470 }
471 
472 static int apps_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
473 {
474  struct ast_cel_general_config *cfg = obj;
475  char *apps = ast_strdupa(var->value);
476  char *cur_app;
477 
478  while ((cur_app = strsep(&apps, ","))) {
479  cur_app = ast_strip(cur_app);
480  if (ast_strlen_zero(cur_app)) {
481  continue;
482  }
483 
484  cur_app = ast_str_to_lower(cur_app);
485  ast_str_container_add(cfg->apps, cur_app);
486  }
487 
488  return 0;
489 }
490 
492 {
493  return S_OR(cel_event_types[type], "Unknown");
494 }
495 
496 static int cel_track_app(const char *const_app)
497 {
498  RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
499  RAII_VAR(char *, app, NULL, ao2_cleanup);
500  char *app_lower;
501 
502  if (!cfg || !cfg->general) {
503  return 0;
504  }
505 
506  app_lower = ast_str_to_lower(ast_strdupa(const_app));
507  app = ao2_find(cfg->general->apps, app_lower, OBJ_SEARCH_KEY);
508  if (!app) {
509  return 0;
510  }
511 
512  return 1;
513 }
514 
515 static int cel_linkedid_ref(const char *linkedid);
516 
518  enum ast_cel_event_type event_type, const char *userdefevname,
519  struct ast_json *extra, const char *peer)
520 {
521  struct timeval eventtime = ast_tvnow();
522 
523  return ast_cel_create_event_with_time(snapshot, event_type, &eventtime,
524  userdefevname, extra, peer);
525 }
526 
528  enum ast_cel_event_type event_type, const struct timeval *event_time,
529  const char *userdefevname, struct ast_json *extra, const char *peer)
530 {
531  RAII_VAR(char *, extra_txt, NULL, ast_json_free);
532  if (extra) {
533  extra_txt = ast_json_dump_string(extra);
534  }
559 }
560 
561 static int cel_backend_send_cb(void *obj, void *arg, int flags)
562 {
563  struct cel_backend *backend = obj;
564 
565  backend->callback(arg);
566  return 0;
567 }
568 
569 static int cel_report_event(struct ast_channel_snapshot *snapshot,
570  enum ast_cel_event_type event_type, const struct timeval *event_time,
571  const char *userdefevname, struct ast_json *extra,
572  const char *peer_str)
573 {
574  struct ast_event *ev;
575  RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
576  RAII_VAR(struct ao2_container *, backends, ao2_global_obj_ref(cel_backends), ao2_cleanup);
577 
578  if (!cfg || !cfg->general || !cfg->general->enable || !backends) {
579  return 0;
580  }
581 
582  /* Record the linkedid of new channels if we are tracking LINKEDID_END even if we aren't
583  * reporting on CHANNEL_START so we can track when to send LINKEDID_END */
584  if (event_type == AST_CEL_CHANNEL_START
586  if (cel_linkedid_ref(snapshot->peer->linkedid)) {
587  return -1;
588  }
589  }
590 
591  if (!ast_cel_track_event(event_type)) {
592  return 0;
593  }
594 
595  if ((event_type == AST_CEL_APP_START || event_type == AST_CEL_APP_END)
596  && !cel_track_app(snapshot->dialplan->appl)) {
597  return 0;
598  }
599 
600  ev = ast_cel_create_event_with_time(snapshot, event_type, event_time, userdefevname, extra, peer_str);
601  if (!ev) {
602  return -1;
603  }
604 
605  /* Distribute event to backends */
607  ast_event_destroy(ev);
608 
609  return 0;
610 }
611 
612 /* called whenever a channel is destroyed or a linkedid is changed to
613  * potentially emit a CEL_LINKEDID_END event */
614 static void check_retire_linkedid(struct ast_channel_snapshot *snapshot, const struct timeval *event_time)
615 {
616  RAII_VAR(struct ao2_container *, linkedids, ao2_global_obj_ref(cel_linkedids), ao2_cleanup);
617  struct cel_linkedid *lid;
618 
619  if (!linkedids || ast_strlen_zero(snapshot->peer->linkedid)) {
620  /* The CEL module is shutdown. Abort. */
621  return;
622  }
623 
624  ao2_lock(linkedids);
625 
626  lid = ao2_find(linkedids, (void *) snapshot->peer->linkedid, OBJ_SEARCH_KEY);
627  if (!lid) {
628  ao2_unlock(linkedids);
629 
630  /*
631  * The user may have done a reload to start tracking linkedids
632  * when a call was already in progress. This is an unusual kind
633  * of change to make after starting Asterisk.
634  */
635  ast_log(LOG_ERROR, "Something weird happened, couldn't find linkedid %s\n",
636  snapshot->peer->linkedid);
637  return;
638  }
639 
640  if (!--lid->count) {
641  /* No channels use this linkedid anymore. */
642  ao2_unlink(linkedids, lid);
643  ao2_unlock(linkedids);
644 
645  cel_report_event(snapshot, AST_CEL_LINKEDID_END, event_time, NULL, NULL, NULL);
646  } else {
647  ao2_unlock(linkedids);
648  }
649  ao2_ref(lid, -1);
650 }
651 
652 /* Note that no 'chan_fixup' function is provided for this datastore type,
653  * because the channels that will use it will never be involved in masquerades.
654  */
656  .type = "CEL fabricated channel",
657  .destroy = ast_free_ptr,
658 };
659 
661 {
662  struct varshead *headp;
663  struct ast_var_t *newvariable;
664  const char *mixed_name;
665  char timebuf[30];
666  struct ast_channel *tchan;
667  struct ast_cel_event_record record = {
669  };
670  struct ast_datastore *datastore;
671  char *app_data;
672  RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
673 
674  if (!cfg || !cfg->general) {
675  return NULL;
676  }
677 
678  /* do not call ast_channel_alloc because this is not really a real channel */
679  if (!(tchan = ast_dummy_channel_alloc())) {
680  return NULL;
681  }
682 
683  headp = ast_channel_varshead(tchan);
684 
685  /* first, get the variables from the event */
686  if (ast_cel_fill_record(event, &record)) {
687  ast_channel_unref(tchan);
688  return NULL;
689  }
690 
691  /* next, fill the channel with their data */
692  mixed_name = (record.event_type == AST_CEL_USER_DEFINED)
693  ? record.user_defined_name : record.event_name;
694  if ((newvariable = ast_var_assign("eventtype", mixed_name))) {
695  AST_LIST_INSERT_HEAD(headp, newvariable, entries);
696  }
697 
698  if (ast_strlen_zero(cfg->general->date_format)) {
699  snprintf(timebuf, sizeof(timebuf), "%ld.%06ld", (long) record.event_time.tv_sec,
700  (long) record.event_time.tv_usec);
701  } else {
702  struct ast_tm tm;
703  ast_localtime(&record.event_time, &tm, NULL);
704  ast_strftime(timebuf, sizeof(timebuf), cfg->general->date_format, &tm);
705  }
706 
707  if ((newvariable = ast_var_assign("eventtime", timebuf))) {
708  AST_LIST_INSERT_HEAD(headp, newvariable, entries);
709  }
710 
711  if ((newvariable = ast_var_assign("eventenum", record.event_name))) {
712  AST_LIST_INSERT_HEAD(headp, newvariable, entries);
713  }
714  if ((newvariable = ast_var_assign("userdeftype", record.user_defined_name))) {
715  AST_LIST_INSERT_HEAD(headp, newvariable, entries);
716  }
717  if ((newvariable = ast_var_assign("eventextra", record.extra))) {
718  AST_LIST_INSERT_HEAD(headp, newvariable, entries);
719  }
720 
721  ast_channel_caller(tchan)->id.name.valid = 1;
723  ast_channel_caller(tchan)->id.number.valid = 1;
725  ast_channel_caller(tchan)->ani.number.valid = 1;
730 
731  ast_channel_exten_set(tchan, record.extension);
732  ast_channel_context_set(tchan, record.context);
733  ast_channel_name_set(tchan, record.channel_name);
735  ast_channel_accountcode_set(tchan, record.account_code);
736  ast_channel_peeraccount_set(tchan, record.peer_account);
737  ast_channel_userfield_set(tchan, record.user_field);
738 
739  if ((newvariable = ast_var_assign("BRIDGEPEER", record.peer))) {
740  AST_LIST_INSERT_HEAD(headp, newvariable, entries);
741  }
742 
743  ast_channel_amaflags_set(tchan, record.amaflag);
744 
745  /* We need to store an 'application name' and 'application
746  * data' on the channel for logging purposes, but the channel
747  * structure only provides a place to store pointers, and it
748  * expects these pointers to be pointing to data that does not
749  * need to be freed. This means that the channel's destructor
750  * does not attempt to free any storage that these pointers
751  * point to. However, we can't provide data in that form directly for
752  * these structure members. In order to ensure that these data
753  * elements have a lifetime that matches the channel's
754  * lifetime, we'll put them in a datastore attached to the
755  * channel, and set's the channel's pointers to point into the
756  * datastore. The datastore will then be automatically destroyed
757  * when the channel is destroyed.
758  */
759 
760  if (!(datastore = ast_datastore_alloc(&fabricated_channel_datastore, NULL))) {
761  ast_channel_unref(tchan);
762  return NULL;
763  }
764 
765  if (!(app_data = ast_malloc(strlen(record.application_name) + strlen(record.application_data) + 2))) {
766  ast_datastore_free(datastore);
767  ast_channel_unref(tchan);
768  return NULL;
769  }
770 
771  ast_channel_appl_set(tchan, strcpy(app_data, record.application_name));
772  ast_channel_data_set(tchan, strcpy(app_data + strlen(record.application_name) + 1,
773  record.application_data));
774 
775  datastore->data = app_data;
776  ast_channel_datastore_add(tchan, datastore);
777 
778  return tchan;
779 }
780 
781 static int cel_linkedid_ref(const char *linkedid)
782 {
783  RAII_VAR(struct ao2_container *, linkedids, ao2_global_obj_ref(cel_linkedids), ao2_cleanup);
784  struct cel_linkedid *lid;
785 
786  if (ast_strlen_zero(linkedid)) {
787  ast_log(LOG_ERROR, "The linkedid should never be empty\n");
788  return -1;
789  }
790  if (!linkedids) {
791  /* The CEL module is shutdown. Abort. */
792  return -1;
793  }
794 
795  ao2_lock(linkedids);
796  lid = ao2_find(linkedids, (void *) linkedid, OBJ_SEARCH_KEY);
797  if (!lid) {
798  /*
799  * Changes to the lid->count member are protected by the
800  * container lock so the lid object does not need its own lock.
801  */
802  lid = ao2_alloc_options(sizeof(*lid) + strlen(linkedid) + 1, NULL,
804  if (!lid) {
805  ao2_unlock(linkedids);
806  return -1;
807  }
808  strcpy(lid->id, linkedid);/* Safe */
809 
810  ao2_link(linkedids, lid);
811  }
812  ++lid->count;
813  ao2_unlock(linkedids);
814  ao2_ref(lid, -1);
815 
816  return 0;
817 }
818 
819 int ast_cel_fill_record(const struct ast_event *e, struct ast_cel_event_record *r)
820 {
822  ast_log(LOG_ERROR, "Module ABI mismatch for ast_cel_event_record. "
823  "Please ensure all modules were compiled for "
824  "this version of Asterisk.\n");
825  return -1;
826  }
827 
829 
832 
834  if (r->event_type == AST_CEL_USER_DEFINED) {
836  } else {
837  r->user_defined_name = "";
838  }
839 
858 
859  return 0;
860 }
861 
862 /*! \brief Typedef for callbacks that get called on channel snapshot updates */
864  struct ast_channel_snapshot *old_snapshot,
865  struct ast_channel_snapshot *new_snapshot,
866  const struct timeval *event_time);
867 
868 static struct cel_dialstatus *get_dialstatus(const char *uniqueid)
869 {
870  struct ao2_container *dial_statuses = ao2_global_obj_ref(cel_dialstatus_store);
871  struct cel_dialstatus *dialstatus = NULL;
872 
873  if (dial_statuses) {
874  dialstatus = ao2_find(dial_statuses, uniqueid, OBJ_SEARCH_KEY | OBJ_UNLINK);
875  ao2_ref(dial_statuses, -1);
876  }
877  return dialstatus;
878 }
879 
880 static const char *get_blob_variable(struct ast_multi_channel_blob *blob, const char *varname)
881 {
882  struct ast_json *json = ast_multi_channel_blob_get_json(blob);
883  if (!json) {
884  return NULL;
885  }
886 
887  json = ast_json_object_get(json, varname);
888  if (!json) {
889  return NULL;
890  }
891 
892  return ast_json_string_get(json);
893 }
894 
895 /*! \brief Handle channel state changes */
897  struct ast_channel_snapshot *old_snapshot,
898  struct ast_channel_snapshot *new_snapshot,
899  const struct timeval *event_time)
900 {
901  int is_hungup, was_hungup;
902 
903  if (!old_snapshot) {
904  cel_report_event(new_snapshot, AST_CEL_CHANNEL_START, event_time, NULL, NULL, NULL);
905  return;
906  }
907 
908  was_hungup = ast_test_flag(&old_snapshot->flags, AST_FLAG_DEAD) ? 1 : 0;
909  is_hungup = ast_test_flag(&new_snapshot->flags, AST_FLAG_DEAD) ? 1 : 0;
910 
911  if (!was_hungup && is_hungup) {
912  struct ast_json *extra;
913  struct cel_dialstatus *dialstatus = get_dialstatus(new_snapshot->base->uniqueid);
914 
915  extra = ast_json_pack("{s: i, s: s, s: s}",
916  "hangupcause", new_snapshot->hangup->cause,
917  "hangupsource", new_snapshot->hangup->source,
918  "dialstatus", dialstatus ? dialstatus->dialstatus : "");
919  cel_report_event(new_snapshot, AST_CEL_HANGUP, event_time, NULL, extra, NULL);
920  ast_json_unref(extra);
921  ao2_cleanup(dialstatus);
922 
923  cel_report_event(new_snapshot, AST_CEL_CHANNEL_END, event_time, NULL, NULL, NULL);
925  check_retire_linkedid(new_snapshot, event_time);
926  }
927  return;
928  }
929 
930  if (old_snapshot->state != new_snapshot->state && new_snapshot->state == AST_STATE_UP) {
931  cel_report_event(new_snapshot, AST_CEL_ANSWER, event_time, NULL, NULL, NULL);
932  return;
933  }
934 }
935 
937  struct ast_channel_snapshot *old_snapshot,
938  struct ast_channel_snapshot *new_snapshot,
939  const struct timeval *event_time)
940 {
941  if (!old_snapshot) {
942  return;
943  }
944 
945  ast_assert(!ast_strlen_zero(new_snapshot->peer->linkedid));
946  ast_assert(!ast_strlen_zero(old_snapshot->peer->linkedid));
947 
949  && strcmp(old_snapshot->peer->linkedid, new_snapshot->peer->linkedid)) {
950  cel_linkedid_ref(new_snapshot->peer->linkedid);
951  check_retire_linkedid(old_snapshot, event_time);
952  }
953 }
954 
956  struct ast_channel_snapshot *old_snapshot,
957  struct ast_channel_snapshot *new_snapshot,
958  const struct timeval *event_time)
959 {
960  if (old_snapshot && !strcmp(old_snapshot->dialplan->appl, new_snapshot->dialplan->appl)) {
961  return;
962  }
963 
964  /* old snapshot has an application, end it */
965  if (old_snapshot && !ast_strlen_zero(old_snapshot->dialplan->appl)) {
966  cel_report_event(old_snapshot, AST_CEL_APP_END, event_time, NULL, NULL, NULL);
967  }
968 
969  /* new snapshot has an application, start it */
970  if (!ast_strlen_zero(new_snapshot->dialplan->appl)) {
971  cel_report_event(new_snapshot, AST_CEL_APP_START, event_time, NULL, NULL, NULL);
972  }
973 }
974 
975 /* \brief Handlers for channel snapshot changes.
976  * \note Order of the handlers matters. Application changes must come before state
977  * changes to ensure that hangup notifications occur after application changes.
978  * Linkedid checking should always come last.
979  */
984 };
985 
987 {
988  if (!snapshot) {
989  return 0;
990  }
991  return snapshot->base->tech_properties & AST_CHAN_TP_INTERNAL;
992 }
993 
994 static void cel_snapshot_update_cb(void *data, struct stasis_subscription *sub,
995  struct stasis_message *message)
996 {
998  size_t i;
999 
1001  return;
1002  }
1003 
1004  for (i = 0; i < ARRAY_LEN(cel_channel_monitors); ++i) {
1006  }
1007 }
1008 
1010  struct ast_bridge_snapshot *bridge,
1011  struct ast_channel_snapshot *chan)
1012 {
1013  struct ast_str *peer_str = ast_str_create(32);
1014  struct ao2_iterator i;
1015  char *current_chan = NULL;
1016 
1017  if (!peer_str) {
1018  return NULL;
1019  }
1020 
1021  for (i = ao2_iterator_init(bridge->channels, 0);
1022  (current_chan = ao2_iterator_next(&i));
1023  ao2_cleanup(current_chan)) {
1024  struct ast_channel_snapshot *current_snapshot;
1025 
1026  /* Don't add the channel for which this message is being generated */
1027  if (!strcmp(current_chan, chan->base->uniqueid)) {
1028  continue;
1029  }
1030 
1031  current_snapshot = ast_channel_snapshot_get_latest(current_chan);
1032  if (!current_snapshot) {
1033  continue;
1034  }
1035 
1036  ast_str_append(&peer_str, 0, "%s,", current_snapshot->base->name);
1037  ao2_cleanup(current_snapshot);
1038  }
1040 
1041  /* Rip off the trailing comma */
1042  ast_str_truncate(peer_str, -1);
1043 
1044  return peer_str;
1045 }
1046 
1048  void *data, struct stasis_subscription *sub,
1049  struct stasis_message *message)
1050 {
1051  struct ast_bridge_blob *blob = stasis_message_data(message);
1052  struct ast_bridge_snapshot *snapshot = blob->bridge;
1053  struct ast_channel_snapshot *chan_snapshot = blob->channel;
1054  RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
1055  RAII_VAR(struct ast_str *, peer_str, NULL, ast_free);
1056 
1057  if (cel_filter_channel_snapshot(chan_snapshot)) {
1058  return;
1059  }
1060 
1061  extra = ast_json_pack("{s: s, s: s}",
1062  "bridge_id", snapshot->uniqueid,
1063  "bridge_technology", snapshot->technology);
1064  if (!extra) {
1065  return;
1066  }
1067 
1068  peer_str = cel_generate_peer_str(snapshot, chan_snapshot);
1069  if (!peer_str) {
1070  return;
1071  }
1072 
1074  NULL, extra, ast_str_buffer(peer_str));
1075 }
1076 
1078  void *data, struct stasis_subscription *sub,
1079  struct stasis_message *message)
1080 {
1081  struct ast_bridge_blob *blob = stasis_message_data(message);
1082  struct ast_bridge_snapshot *snapshot = blob->bridge;
1083  struct ast_channel_snapshot *chan_snapshot = blob->channel;
1084  RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
1085  RAII_VAR(struct ast_str *, peer_str, NULL, ast_free);
1086 
1087  if (cel_filter_channel_snapshot(chan_snapshot)) {
1088  return;
1089  }
1090 
1091  extra = ast_json_pack("{s: s, s: s}",
1092  "bridge_id", snapshot->uniqueid,
1093  "bridge_technology", snapshot->technology);
1094  if (!extra) {
1095  return;
1096  }
1097 
1098  peer_str = cel_generate_peer_str(snapshot, chan_snapshot);
1099  if (!peer_str) {
1100  return;
1101  }
1102 
1104  NULL, extra, ast_str_buffer(peer_str));
1105 }
1106 
1107 static void cel_parking_cb(
1108  void *data, struct stasis_subscription *sub,
1109  struct stasis_message *message)
1110 {
1111  struct ast_parked_call_payload *parked_payload = stasis_message_data(message);
1112  RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
1113  const char *reason = NULL;
1114 
1115  switch (parked_payload->event_type) {
1116  case PARKED_CALL:
1117  extra = ast_json_pack("{s: s, s: s}",
1118  "parker_dial_string", parked_payload->parker_dial_string,
1119  "parking_lot", parked_payload->parkinglot);
1120  if (extra) {
1122  NULL, extra, NULL);
1123  }
1124  return;
1125  case PARKED_CALL_TIMEOUT:
1126  reason = "ParkedCallTimeOut";
1127  break;
1128  case PARKED_CALL_GIVEUP:
1129  reason = "ParkedCallGiveUp";
1130  break;
1131  case PARKED_CALL_UNPARKED:
1132  reason = "ParkedCallUnparked";
1133  break;
1134  case PARKED_CALL_FAILED:
1135  reason = "ParkedCallFailed";
1136  break;
1137  case PARKED_CALL_SWAP:
1138  reason = "ParkedCallSwap";
1139  break;
1140  }
1141 
1142  if (parked_payload->retriever) {
1143  extra = ast_json_pack("{s: s, s: s}",
1144  "reason", reason ?: "",
1145  "retriever", parked_payload->retriever->base->name);
1146  } else {
1147  extra = ast_json_pack("{s: s}", "reason", reason ?: "");
1148  }
1149 
1150  if (extra) {
1152  NULL, extra, NULL);
1153  }
1154 }
1155 
1156 static void save_dialstatus(struct ast_multi_channel_blob *blob, struct ast_channel_snapshot *snapshot)
1157 {
1158  struct ao2_container *dial_statuses = ao2_global_obj_ref(cel_dialstatus_store);
1159  const char *dialstatus_string = get_blob_variable(blob, "dialstatus");
1160  struct cel_dialstatus *dialstatus;
1161  size_t dialstatus_string_len;
1162 
1163  if (!dial_statuses || ast_strlen_zero(dialstatus_string)) {
1164  ao2_cleanup(dial_statuses);
1165  return;
1166  }
1167 
1168  dialstatus = ao2_find(dial_statuses, snapshot->base->uniqueid, OBJ_SEARCH_KEY);
1169  if (dialstatus) {
1170  if (!strcasecmp(dialstatus_string, "ANSWER") && strcasecmp(dialstatus->dialstatus, "ANSWER")) {
1171  /* In the case of an answer after we already have a dial status we give
1172  * priority to the answer since the call was, well, answered. In the case of
1173  * failure dial status results we simply let the first failure be the status.
1174  */
1175  ao2_unlink(dial_statuses, dialstatus);
1176  ao2_ref(dialstatus, -1);
1177  } else {
1178  ao2_ref(dialstatus, -1);
1179  ao2_ref(dial_statuses, -1);
1180  return;
1181  }
1182  }
1183 
1184  dialstatus_string_len = strlen(dialstatus_string) + 1;
1185  dialstatus = ao2_alloc_options(sizeof(*dialstatus) + dialstatus_string_len, NULL,
1187  if (!dialstatus) {
1188  ao2_ref(dial_statuses, -1);
1189  return;
1190  }
1191 
1192  ast_copy_string(dialstatus->uniqueid, snapshot->base->uniqueid, sizeof(dialstatus->uniqueid));
1193  ast_copy_string(dialstatus->dialstatus, dialstatus_string, dialstatus_string_len);
1194 
1195  ao2_link(dial_statuses, dialstatus);
1196  ao2_ref(dialstatus, -1);
1197  ao2_ref(dial_statuses, -1);
1198 }
1199 
1201 {
1202  const char *dialstatus = get_blob_variable(blob, "dialstatus");
1203  int res = 0;
1204 
1205  if (ast_strlen_zero(dialstatus)) {
1206  res = 0;
1207  } else if (!strcasecmp(dialstatus, "CHANUNAVAIL")) {
1208  res = 1;
1209  } else if (!strcasecmp(dialstatus, "CONGESTION")) {
1210  res = 1;
1211  } else if (!strcasecmp(dialstatus, "NOANSWER")) {
1212  res = 1;
1213  } else if (!strcasecmp(dialstatus, "BUSY")) {
1214  res = 1;
1215  } else if (!strcasecmp(dialstatus, "ANSWER")) {
1216  res = 1;
1217  } else if (!strcasecmp(dialstatus, "CANCEL")) {
1218  res = 1;
1219  } else if (!strcasecmp(dialstatus, "DONTCALL")) {
1220  res = 1;
1221  } else if (!strcasecmp(dialstatus, "TORTURE")) {
1222  res = 1;
1223  } else if (!strcasecmp(dialstatus, "INVALIDARGS")) {
1224  res = 1;
1225  }
1226  return res;
1227 }
1228 
1229 static void cel_dial_cb(void *data, struct stasis_subscription *sub,
1230  struct stasis_message *message)
1231 {
1232  struct ast_multi_channel_blob *blob = stasis_message_data(message);
1233  struct ast_channel_snapshot *snapshot;
1234 
1235  snapshot = ast_multi_channel_blob_get_channel(blob, "caller");
1236  if (!snapshot || cel_filter_channel_snapshot(snapshot)) {
1237  return;
1238  }
1239 
1240  if (!ast_strlen_zero(get_blob_variable(blob, "forward"))) {
1241  struct ast_json *extra;
1242 
1243  extra = ast_json_pack("{s: s}", "forward", get_blob_variable(blob, "forward"));
1244  if (extra) {
1246  NULL, extra, NULL);
1247  ast_json_unref(extra);
1248  }
1249  }
1250 
1251  if (is_valid_dialstatus(blob)) {
1252  save_dialstatus(blob, snapshot);
1253  }
1254 }
1255 
1256 static void cel_generic_cb(
1257  void *data, struct stasis_subscription *sub,
1258  struct stasis_message *message)
1259 {
1260  struct ast_channel_blob *obj = stasis_message_data(message);
1261  int event_type = ast_json_integer_get(ast_json_object_get(obj->blob, "event_type"));
1262  struct ast_json *event_details = ast_json_object_get(obj->blob, "event_details");
1263 
1264  switch (event_type) {
1265  case AST_CEL_USER_DEFINED:
1266  {
1267  const char *event = ast_json_string_get(ast_json_object_get(event_details, "event"));
1268  struct ast_json *extra = ast_json_object_get(event_details, "extra");
1269  cel_report_event(obj->snapshot, event_type, stasis_message_timestamp(message),
1270  event, extra, NULL);
1271  break;
1272  }
1273  default:
1274  ast_log(LOG_ERROR, "Unhandled %s event blob\n", ast_cel_get_type_name(event_type));
1275  break;
1276  }
1277 }
1278 
1280  void *data, struct stasis_subscription *sub,
1281  struct stasis_message *message)
1282 {
1283  struct ast_blind_transfer_message *transfer_msg = stasis_message_data(message);
1284  struct ast_channel_snapshot *chan_snapshot = transfer_msg->transferer;
1285  struct ast_bridge_snapshot *bridge_snapshot = transfer_msg->bridge;
1286  struct ast_json *extra;
1287 
1288  if (transfer_msg->result != AST_BRIDGE_TRANSFER_SUCCESS) {
1289  return;
1290  }
1291 
1292  extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s}",
1293  "extension", transfer_msg->exten,
1294  "context", transfer_msg->context,
1295  "bridge_id", bridge_snapshot->uniqueid,
1296  "transferee_channel_name", transfer_msg->transferee ? transfer_msg->transferee->base->name : "N/A",
1297  "transferee_channel_uniqueid", transfer_msg->transferee ? transfer_msg->transferee->base->uniqueid : "N/A");
1298  if (extra) {
1300  NULL, extra, NULL);
1301  ast_json_unref(extra);
1302  }
1303 }
1304 
1306  void *data, struct stasis_subscription *sub,
1307  struct stasis_message *message)
1308 {
1309  struct ast_attended_transfer_message *xfer = stasis_message_data(message);
1310  struct ast_json *extra = NULL;
1311  struct ast_bridge_snapshot *bridge1, *bridge2;
1312  struct ast_channel_snapshot *channel1, *channel2;
1313 
1314  /* Make sure bridge1 is always non-NULL */
1315  if (!xfer->to_transferee.bridge_snapshot) {
1316  bridge1 = xfer->to_transfer_target.bridge_snapshot;
1317  bridge2 = xfer->to_transferee.bridge_snapshot;
1318  channel1 = xfer->to_transfer_target.channel_snapshot;
1319  channel2 = xfer->to_transferee.channel_snapshot;
1320  } else {
1321  bridge1 = xfer->to_transferee.bridge_snapshot;
1322  bridge2 = xfer->to_transfer_target.bridge_snapshot;
1323  channel1 = xfer->to_transferee.channel_snapshot;
1324  channel2 = xfer->to_transfer_target.channel_snapshot;
1325  }
1326 
1327  switch (xfer->dest_type) {
1329  return;
1330  /* handle these three the same */
1334  extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: s, s: s}",
1335  "bridge1_id", bridge1->uniqueid,
1336  "channel2_name", channel2->base->name,
1337  "channel2_uniqueid", channel2->base->uniqueid,
1338  "bridge2_id", bridge2->uniqueid,
1339  "transferee_channel_name", xfer->transferee ? xfer->transferee->base->name : "N/A",
1340  "transferee_channel_uniqueid", xfer->transferee ? xfer->transferee->base->uniqueid : "N/A",
1341  "transfer_target_channel_name", xfer->target ? xfer->target->base->name : "N/A",
1342  "transfer_target_channel_uniqueid", xfer->target ? xfer->target->base->uniqueid : "N/A");
1343  if (!extra) {
1344  return;
1345  }
1346  break;
1349  extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: s, s: s}",
1350  "bridge1_id", bridge1->uniqueid,
1351  "channel2_name", channel2->base->name,
1352  "channel2_uniqueid", channel2->base->uniqueid,
1353  "app", xfer->dest.app,
1354  "transferee_channel_name", xfer->transferee ? xfer->transferee->base->name : "N/A",
1355  "transferee_channel_uniqueid", xfer->transferee ? xfer->transferee->base->uniqueid : "N/A",
1356  "transfer_target_channel_name", xfer->target ? xfer->target->base->name : "N/A",
1357  "transfer_target_channel_uniqueid", xfer->target ? xfer->target->base->uniqueid : "N/A");
1358  if (!extra) {
1359  return;
1360  }
1361  break;
1362  }
1364  NULL, extra, NULL);
1365  ast_json_unref(extra);
1366 }
1367 
1368 static void cel_pickup_cb(
1369  void *data, struct stasis_subscription *sub,
1370  struct stasis_message *message)
1371 {
1372  struct ast_multi_channel_blob *obj = stasis_message_data(message);
1374  struct ast_channel_snapshot *target = ast_multi_channel_blob_get_channel(obj, "target");
1375  struct ast_json *extra;
1376 
1377  if (!channel || !target) {
1378  return;
1379  }
1380 
1381  extra = ast_json_pack("{s: s, s: s}",
1382  "pickup_channel", channel->base->name,
1383  "pickup_channel_uniqueid", channel->base->uniqueid);
1384  if (!extra) {
1385  return;
1386  }
1387 
1389  ast_json_unref(extra);
1390 }
1391 
1392 static void cel_local_cb(
1393  void *data, struct stasis_subscription *sub,
1394  struct stasis_message *message)
1395 {
1396  struct ast_multi_channel_blob *obj = stasis_message_data(message);
1397  struct ast_channel_snapshot *localone = ast_multi_channel_blob_get_channel(obj, "1");
1398  struct ast_channel_snapshot *localtwo = ast_multi_channel_blob_get_channel(obj, "2");
1399  struct ast_json *extra;
1400 
1401  if (!localone || !localtwo) {
1402  return;
1403  }
1404 
1405  extra = ast_json_pack("{s: s, s: s}",
1406  "local_two", localtwo->base->name,
1407  "local_two_uniqueid", localtwo->base->uniqueid);
1408  if (!extra) {
1409  return;
1410  }
1411 
1413  ast_json_unref(extra);
1414 }
1415 
1416 static void destroy_routes(void)
1417 {
1419  cel_state_router = NULL;
1420 }
1421 
1422 static void destroy_subscriptions(void)
1423 {
1424  ao2_cleanup(cel_aggregation_topic);
1425  cel_aggregation_topic = NULL;
1426  ao2_cleanup(cel_topic);
1427  cel_topic = NULL;
1428 
1429  cel_channel_forwarder = stasis_forward_cancel(cel_channel_forwarder);
1430  cel_bridge_forwarder = stasis_forward_cancel(cel_bridge_forwarder);
1431  cel_parking_forwarder = stasis_forward_cancel(cel_parking_forwarder);
1432  cel_cel_forwarder = stasis_forward_cancel(cel_cel_forwarder);
1433 }
1434 
1435 static int unload_module(void)
1436 {
1437  destroy_routes();
1440 
1441  ast_cli_unregister(&cli_status);
1442  aco_info_destroy(&cel_cfg_info);
1443  ao2_global_obj_release(cel_configs);
1444  ao2_global_obj_release(cel_dialstatus_store);
1445  ao2_global_obj_release(cel_linkedids);
1446  ao2_global_obj_release(cel_backends);
1447 
1448  return 0;
1449 }
1450 
1451 /*!
1452  * \brief Create the Stasis subscriptions for CEL
1453  */
1454 static int create_subscriptions(void)
1455 {
1456  cel_aggregation_topic = stasis_topic_create("cel:aggregator");
1457  if (!cel_aggregation_topic) {
1458  return -1;
1459  }
1460 
1461  cel_topic = stasis_topic_create("cel:misc");
1462  if (!cel_topic) {
1463  return -1;
1464  }
1465 
1466  cel_channel_forwarder = stasis_forward_all(
1468  cel_aggregation_topic);
1469  if (!cel_channel_forwarder) {
1470  return -1;
1471  }
1472 
1473  cel_bridge_forwarder = stasis_forward_all(
1475  cel_aggregation_topic);
1476  if (!cel_bridge_forwarder) {
1477  return -1;
1478  }
1479 
1480  cel_parking_forwarder = stasis_forward_all(
1482  cel_aggregation_topic);
1483  if (!cel_parking_forwarder) {
1484  return -1;
1485  }
1486 
1487  cel_cel_forwarder = stasis_forward_all(
1488  ast_cel_topic(),
1489  cel_aggregation_topic);
1490  if (!cel_cel_forwarder) {
1491  return -1;
1492  }
1493 
1494  return 0;
1495 }
1496 
1497 /*!
1498  * \brief Create the Stasis message router and routes for CEL
1499  */
1500 static int create_routes(void)
1501 {
1502  int ret = 0;
1503 
1504  cel_state_router = stasis_message_router_create(cel_aggregation_topic);
1505  if (!cel_state_router) {
1506  return -1;
1507  }
1508  stasis_message_router_set_congestion_limits(cel_state_router, -1,
1510 
1511  ret |= stasis_message_router_add(cel_state_router,
1514  NULL);
1515 
1516  ret |= stasis_message_router_add(cel_state_router,
1518  cel_dial_cb,
1519  NULL);
1520 
1521  ret |= stasis_message_router_add(cel_state_router,
1524  NULL);
1525 
1526  ret |= stasis_message_router_add(cel_state_router,
1529  NULL);
1530 
1531  ret |= stasis_message_router_add(cel_state_router,
1534  NULL);
1535 
1536  ret |= stasis_message_router_add(cel_state_router,
1537  cel_generic_type(),
1539  NULL);
1540 
1541  ret |= stasis_message_router_add(cel_state_router,
1544  NULL);
1545 
1546  ret |= stasis_message_router_add(cel_state_router,
1549  NULL);
1550 
1551  ret |= stasis_message_router_add(cel_state_router,
1553  cel_pickup_cb,
1554  NULL);
1555 
1556  ret |= stasis_message_router_add(cel_state_router,
1558  cel_local_cb,
1559  NULL);
1560 
1561  if (ret) {
1562  ast_log(AST_LOG_ERROR, "Failed to register for Stasis messages\n");
1563  }
1564 
1565  return ret;
1566 }
1567 
1570 
1571 static int load_module(void)
1572 {
1573  struct ao2_container *container;
1574 
1576  NUM_APP_BUCKETS, cel_linkedid_hash_fn, NULL, cel_linkedid_cmp_fn);
1577  ao2_global_obj_replace_unref(cel_linkedids, container);
1578  ao2_cleanup(container);
1579  if (!container) {
1580  return AST_MODULE_LOAD_FAILURE;
1581  }
1582 
1584  NUM_DIALSTATUS_BUCKETS, cel_dialstatus_hash_fn, NULL, cel_dialstatus_cmp_fn);
1585  ao2_global_obj_replace_unref(cel_dialstatus_store, container);
1586  ao2_cleanup(container);
1587  if (!container) {
1588  return AST_MODULE_LOAD_FAILURE;
1589  }
1590 
1592  return AST_MODULE_LOAD_FAILURE;
1593  }
1594 
1595  if (ast_cli_register(&cli_status)) {
1596  return AST_MODULE_LOAD_FAILURE;
1597  }
1598 
1600  cel_backend_hash_fn, NULL, cel_backend_cmp_fn);
1601  ao2_global_obj_replace_unref(cel_backends, container);
1602  ao2_cleanup(container);
1603  if (!container) {
1604  return AST_MODULE_LOAD_FAILURE;
1605  }
1606 
1607  if (aco_info_init(&cel_cfg_info)) {
1608  return AST_MODULE_LOAD_FAILURE;
1609  }
1610 
1611  aco_option_register(&cel_cfg_info, "enable", ACO_EXACT, general_options, "no", OPT_BOOL_T, 1, FLDSET(struct ast_cel_general_config, enable));
1612  aco_option_register(&cel_cfg_info, "dateformat", ACO_EXACT, general_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_cel_general_config, date_format));
1613  aco_option_register_custom(&cel_cfg_info, "apps", ACO_EXACT, general_options, "", apps_handler, 0);
1614  aco_option_register_custom(&cel_cfg_info, "events", ACO_EXACT, general_options, "", events_handler, 0);
1615 
1616  if (aco_process_config(&cel_cfg_info, 0)) {
1617  struct cel_config *cel_cfg = cel_config_alloc();
1618 
1619  if (!cel_cfg) {
1620  return AST_MODULE_LOAD_FAILURE;
1621  }
1622 
1623  /* We couldn't process the configuration so create a default config. */
1624  if (!aco_set_defaults(&general_option, "general", cel_cfg->general)) {
1625  ast_log(LOG_NOTICE, "Failed to process CEL configuration; using defaults\n");
1626  ao2_global_obj_replace_unref(cel_configs, cel_cfg);
1627  }
1628  ao2_ref(cel_cfg, -1);
1629  }
1630 
1631  if (create_subscriptions()) {
1632  return AST_MODULE_LOAD_FAILURE;
1633  }
1634 
1635  if (ast_cel_check_enabled() && create_routes()) {
1636  return AST_MODULE_LOAD_FAILURE;
1637  }
1638 
1639  return AST_MODULE_LOAD_SUCCESS;
1640 }
1641 
1642 static int reload_module(void)
1643 {
1644  unsigned int was_enabled = ast_cel_check_enabled();
1645  unsigned int is_enabled;
1646 
1647  if (aco_process_config(&cel_cfg_info, 1) == ACO_PROCESS_ERROR) {
1648  return -1;
1649  }
1650 
1651  is_enabled = ast_cel_check_enabled();
1652 
1653  if (!was_enabled && is_enabled) {
1654  if (create_routes()) {
1655  return -1;
1656  }
1657  } else if (was_enabled && !is_enabled) {
1658  destroy_routes();
1659  }
1660 
1661  ast_verb(3, "CEL logging %sabled.\n", is_enabled ? "en" : "dis");
1662 
1663  return 0;
1664 }
1665 
1667  enum ast_cel_event_type event_type,
1668  struct ast_json *blob)
1669 {
1670  struct ast_json *cel_blob;
1671  struct stasis_message *message;
1672 
1673  cel_blob = ast_json_pack("{s: i, s: o}",
1674  "event_type", event_type,
1675  "event_details", ast_json_ref(blob));
1676 
1678  if (message) {
1679  stasis_publish(ast_cel_topic(), message);
1680  }
1681  ao2_cleanup(message);
1682  ast_json_unref(cel_blob);
1683 }
1684 
1686 {
1687  return cel_topic;
1688 }
1689 
1691 {
1692  RAII_VAR(struct cel_config *, mod_cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
1693 
1694  if (!mod_cfg || !mod_cfg->general) {
1695  return NULL;
1696  }
1697 
1698  ao2_ref(mod_cfg->general, +1);
1699  return mod_cfg->general;
1700 }
1701 
1703 {
1704  int was_enabled;
1705  int is_enabled;
1706  struct ast_cel_general_config *cleanup_config;
1707  struct cel_config *mod_cfg = ao2_global_obj_ref(cel_configs);
1708 
1709  if (mod_cfg) {
1710  was_enabled = ast_cel_check_enabled();
1711 
1712  cleanup_config = mod_cfg->general;
1713  ao2_bump(config);
1714  mod_cfg->general = config;
1715  ao2_cleanup(cleanup_config);
1716 
1717  is_enabled = ast_cel_check_enabled();
1718  if (!was_enabled && is_enabled) {
1719  create_routes();
1720  } else if (was_enabled && !is_enabled) {
1721  destroy_routes();
1722  }
1723 
1724  ao2_ref(mod_cfg, -1);
1725  }
1726 }
1727 
1729 {
1730  struct ao2_container *backends = ao2_global_obj_ref(cel_backends);
1731 
1732  if (backends) {
1733  ao2_find(backends, name, OBJ_SEARCH_KEY | OBJ_NODATA | OBJ_UNLINK);
1734  ao2_ref(backends, -1);
1735  }
1736 
1737  return 0;
1738 }
1739 
1740 int ast_cel_backend_register(const char *name, ast_cel_backend_cb backend_callback)
1741 {
1742  RAII_VAR(struct ao2_container *, backends, ao2_global_obj_ref(cel_backends), ao2_cleanup);
1743  struct cel_backend *backend;
1744 
1745  if (!backends || ast_strlen_zero(name) || !backend_callback) {
1746  return -1;
1747  }
1748 
1749  /* The backend object is immutable so it doesn't need a lock of its own. */
1750  backend = ao2_alloc_options(sizeof(*backend) + 1 + strlen(name), NULL,
1752  if (!backend) {
1753  return -1;
1754  }
1755  strcpy(backend->name, name);/* Safe */
1756  backend->callback = backend_callback;
1757 
1758  ao2_link(backends, backend);
1759  ao2_ref(backend, -1);
1760  return 0;
1761 }
1762 
1764  .support_level = AST_MODULE_SUPPORT_CORE,
1765  .load = load_module,
1766  .unload = unload_module,
1767  .reload = reload_module,
1768  .load_pri = AST_MODPRI_CORE,
1769  .requires = "extconfig",
1770 );
enum ast_cel_event_type ast_cel_str_to_event_type(const char *name)
Get the event type from a string.
Definition: cel.c:418
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
const char * type
Definition: datastore.h:32
cel_channel_snapshot_monitor cel_channel_monitors[]
Definition: cel.c:980
const char * account_code
Definition: cel.h:161
the last channel with the given linkedid is retired
Definition: cel.h:71
struct stasis_message_type * ast_blind_transfer_type(void)
Message type for ast_blind_transfer_message.
const char * caller_id_name
Definition: cel.h:151
static const char type[]
Definition: chan_ooh323.c:109
#define NUM_APP_BUCKETS
Number of buckets for the appset container.
Definition: cel.c:155
const ast_string_field data
Helper struct for getting the fields out of a CEL event.
Definition: cel.h:136
Channel Event CID name Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:157
Channel Event app name Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:187
const char * linked_id
Definition: cel.h:164
struct ao2_container * channels
Definition: bridge.h:339
Main Channel structure associated with a channel.
struct ast_party_dialed::@246 number
Dialed/Called number.
Local proxy channel special access.
#define CEL_MAX_EVENT_IDS
Maximum possible CEL event IDs.
Definition: cel.c:150
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
An event.
Definition: event.c:81
char * str
Subscriber phone number (Malloced)
Definition: channel.h:292
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
static void cel_config_dtor(void *obj)
Destructor for cel_config.
Definition: cel.c:217
A container that holds all config-related information.
Definition: cel_custom.c:56
struct ast_channel_snapshot_base * base
union ast_attended_transfer_message::@324 dest
static struct aco_file cel_conf
The config file to be processed for the module.
Definition: cel.c:264
Asterisk main include file. File version handling, generic pbx functions.
static struct ast_cli_entry cli_status
Definition: cel.c:416
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char * str
Subscriber phone number (Malloced)
Definition: channel.h:387
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
static void cel_parking_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: cel.c:1107
struct ast_json * blob
struct ast_event * ast_cel_create_event_with_time(struct ast_channel_snapshot *snapshot, enum ast_cel_event_type event_type, const struct timeval *event_time, const char *userdefevname, struct ast_json *extra, const char *peer)
Allocate and populate a CEL event structure.
Definition: cel.c:527
struct stasis_message_type * ast_channel_entered_bridge_type(void)
Message type for channel enter bridge blob messages.
static int ast_cel_track_event(enum ast_cel_event_type et)
Definition: cel.c:432
Message representing attended transfer.
struct ast_channel_snapshot * channel
#define aco_option_register_custom(info, name, matchtype, types, default_val, handler, flags)
Register a config option.
char * config
Definition: conf2ael.c:66
static struct aco_type general_option
An aco_type structure to link the "general" category to the ast_cel_general_config type...
Definition: cel.c:241
Channel Event extra data Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:259
Call Parking API.
const ast_string_field name
#define AO2_STRING_FIELD_HASH_FN(stype, field)
Creates a hash function for a structure string field.
Definition: astobj2.h:2055
#define BACKEND_BUCKETS
Definition: cel.c:141
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2981
Registered applications container.
Definition: pbx_app.c:67
static struct stasis_forward * cel_parking_forwarder
Definition: cel.c:129
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
#define ast_test_flag(p, flag)
Definition: utils.h:63
char exten[AST_MAX_EXTENSION]
static int cel_backend_send_cb(void *obj, void *arg, int flags)
Definition: cel.c:561
static void update(int code_size, int y, int wi, int fi, int dq, int sr, int dqsez, struct g726_state *state_ptr)
Definition: codec_g726.c:367
Message published during a blind transfer.
Call Event Logging API.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_party_name name
Subscriber name.
Definition: channel.h:341
struct ast_party_id from
Who is redirecting the call (Sent to the party the call is redirected toward)
Definition: channel.h:528
void ast_channel_appl_set(struct ast_channel *chan, const char *value)
struct ast_json * blob
int stasis_message_router_add(struct stasis_message_router *router, struct stasis_message_type *message_type, stasis_subscription_cb callback, void *data)
Add a route to a message router.
static void cel_dial_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: cel.c:1229
const char * user_defined_name
Definition: cel.h:150
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1501
struct ast_channel_snapshot * snapshot
struct stasis_message_type * ast_channel_left_bridge_type(void)
Message type for channel leave bridge blob messages.
static void cel_blind_transfer_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: cel.c:1279
Call Pickup API.
static void cel_snapshot_update_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: cel.c:994
int ast_cli_unregister(struct ast_cli_entry *e)
Unregisters a command or an array of commands.
Definition: main/cli.c:2397
descriptor for a cli entry.
Definition: cli.h:171
const int argc
Definition: cli.h:160
struct stasis_message_type * ast_parked_call_type(void)
accessor for the parked call stasis message type
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
#define AST_TASKPROCESSOR_HIGH_WATER_LEVEL
Definition: taskprocessor.h:63
static int unload_module(void)
Definition: cel.c:1435
const ast_string_field rdnis
const char * application_data
Definition: cel.h:160
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
const char * application_name
Definition: cel.h:159
static struct stasis_topic * cel_aggregation_topic
Definition: cel.c:120
void ast_json_free(void *p)
Asterisk&#39;s custom JSON allocator. Exposed for use by unit tests.
Definition: json.c:52
Structure that contains a snapshot of information about a bridge.
Definition: bridge.h:322
Channel Event channel name Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:181
#define aco_option_register(info, name, matchtype, types, default_val, opt_type, flags,...)
Register a config option.
static void cel_channel_linkedid_change(struct ast_channel_snapshot *old_snapshot, struct ast_channel_snapshot *new_snapshot, const struct timeval *event_time)
Definition: cel.c:936
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
static int cel_pre_apply_config(void)
Definition: cel.c:276
A local channel optimization occurred.
Definition: cel.h:77
void stasis_message_router_unsubscribe_and_join(struct stasis_message_router *router)
Unsubscribe the router from the upstream topic, blocking until the final message has been processed...
Structure for variables, used for configurations and for channel variables.
static int events_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
Definition: cel.c:443
Channel Event UniqueID Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:211
static void cel_local_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: cel.c:1392
#define var
Definition: ast_expr2f.c:614
Channel Event context name Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:175
Structure representing a snapshot of channel state.
Channel Event app args/data Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:193
void * ast_cel_general_config_alloc(void)
Allocate a CEL configuration object.
Definition: cel.c:188
Channel Event peeraccount Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:253
Channel Event Time (micro-seconds) Used by: AST_EVENT_CEL Payload type: UINT.
Definition: event_defs.h:145
const char * extension
Definition: cel.h:156
Channel Event CID dnid Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:235
#define ast_json_dump_string(root)
Encode a JSON value to a compact string.
Definition: json.h:763
#define ast_str_container_alloc(buckets)
Allocates a hash container for bare strings.
Definition: strings.h:1312
static char date_format[6]
Definition: chan_skinny.c:209
#define STASIS_MESSAGE_TYPE_CLEANUP(name)
Boiler-plate messaging macro for cleaning up message types.
Definition: stasis.h:1523
CONFIG_INFO_CORE("cel", cel_cfg_info, cel_configs, cel_config_alloc,.files=ACO_FILES(&cel_conf),.pre_apply_config=cel_pre_apply_config,)
struct ast_json * blob
channel birth
Definition: cel.h:45
Definition: cli.h:152
if(!yyg->yy_init)
Definition: ast_expr2f.c:868
Structure for a data store type.
Definition: datastore.h:31
struct ast_channel_snapshot * target
const ast_string_field accountcode
char * str
Subscriber name (Malloced)
Definition: channel.h:265
Definition: astman.c:222
static const struct adsi_event events[]
Definition: app_adsiprog.c:85
const ast_string_field uniqueid
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
enum aco_process_status aco_process_config(struct aco_info *info, int reload)
Process a config info via the options registered with an aco_info.
#define ao2_global_obj_ref(holder)
Definition: astobj2.h:925
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
static int reload_module(void)
Definition: cel.c:1642
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
const char * caller_id_num
Definition: cel.h:152
struct ast_cel_general_config * ast_cel_get_config(void)
Obtain the current CEL configuration.
Definition: cel.c:1690
#define ast_assert(a)
Definition: utils.h:695
#define ao2_unlock(a)
Definition: astobj2.h:730
struct ast_event * ast_cel_create_event(struct ast_channel_snapshot *snapshot, enum ast_cel_event_type event_type, const char *userdefevname, struct ast_json *extra, const char *peer)
Allocate and populate a CEL event structure.
Definition: cel.c:517
Definition: muted.c:95
static void check_retire_linkedid(struct ast_channel_snapshot *snapshot, const struct timeval *event_time)
Definition: cel.c:614
static struct cel_dialstatus * get_dialstatus(const char *uniqueid)
Definition: cel.c:868
A parked call message payload.
Definition: parking.h:59
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
Structure for a data store object.
Definition: datastore.h:68
struct varshead * ast_channel_varshead(struct ast_channel *chan)
static const char *const cel_event_types[CEL_MAX_EVENT_IDS]
Map of ast_cel_event_type to strings.
Definition: cel.c:305
static struct stasis_forward * cel_channel_forwarder
Definition: cel.c:123
#define NULL
Definition: resample.c:96
STASIS_MESSAGE_TYPE_DEFN(cel_generic_type)
The representation of a single configuration file to be processed.
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
Structure representing a change of snapshot of channel state.
const char * extra
Definition: cel.h:168
enum aco_type_t type
enum ast_parked_call_event_type event_type
Definition: parking.h:62
void ast_free_ptr(void *ptr)
free() wrapper
Definition: astmm.c:1771
#define ACO_TYPES(...)
A helper macro to ensure that aco_info types always have a sentinel.
static const struct ast_datastore_info fabricated_channel_datastore
Definition: cel.c:655
struct ast_channel_snapshot_dialplan * dialplan
#define ast_verb(level,...)
Definition: logger.h:463
struct ast_bridge_channel_snapshot_pair to_transferee
int64_t events
Definition: cel.h:210
Channel Event Type Used by: AST_EVENT_CEL Payload type: UINT.
Definition: event_defs.h:133
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
struct ast_json * ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj)
Retrieve the JSON blob from a ast_multi_channel_blob. Returned ast_json is still owned by obj...
the list of registered channel types
Definition: channel.c:121
Utility functions.
Blob of data associated with a channel.
char * ast_str_truncate(struct ast_str *buf, ssize_t len)
Truncates the enclosed string to the given length.
Definition: strings.h:738
#define ast_strlen_zero(foo)
Definition: strings.h:52
Channel Event Time (seconds) Used by: AST_EVENT_CEL Payload type: UINT.
Definition: event_defs.h:139
#define ast_cli_register(e)
Registers a command or an array of commands.
Definition: cli.h:256
Channel Event CID num Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:163
#define ao2_bump(obj)
Definition: astobj2.h:491
static struct aco_type ignore_option
Definition: cel.c:256
struct ast_party_id id
Caller party ID.
Definition: channel.h:421
struct ast_bridge_channel_snapshot_pair to_transfer_target
const ast_string_field context
static void cel_attended_transfer_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: cel.c:1305
Configuration File Parser.
channel enters a bridge
Definition: cel.h:57
Channel Event extension name Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:169
struct ast_bridge_snapshot * bridge
struct stasis_topic * ast_channel_topic_all(void)
A topic which publishes the events for all channels.
#define ast_log
Definition: astobj2.c:42
#define AST_LOG_ERROR
Definition: logger.h:290
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
void(* cel_channel_snapshot_monitor)(struct ast_channel_snapshot *old_snapshot, struct ast_channel_snapshot *new_snapshot, const struct timeval *event_time)
Typedef for callbacks that get called on channel snapshot updates.
Definition: cel.c:863
const ast_string_field technology
Definition: bridge.h:336
int aco_info_init(struct aco_info *info)
Initialize an aco_info structure.
struct ast_party_id ani
Automatic Number Identification (ANI)
Definition: channel.h:428
struct ast_channel_snapshot * parkee
Definition: parking.h:60
General Asterisk PBX channel definitions.
void(* ast_cel_backend_cb)(struct ast_event *event)
CEL backend callback.
Definition: cel.h:292
void * aco_pending_config(struct aco_info *info)
Get pending config changes.
const ast_string_field appl
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
const int fd
Definition: cli.h:159
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:353
#define ast_dummy_channel_alloc()
Create a fake channel structure.
Definition: channel.h:1283
struct ast_channel * ast_cel_fabricate_channel_from_event(const struct ast_event *event)
Create a fake channel from data in a CEL event.
Definition: cel.c:660
struct ast_channel_snapshot * transferee
const struct timeval * stasis_message_timestamp(const struct stasis_message *msg)
Get the time when a message was created.
an app ends
Definition: cel.h:55
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:219
unsigned int ast_cel_check_enabled(void)
Hashing function for cel_backend.
Definition: cel.c:343
const char * context
Definition: cel.h:157
char dialstatus[0]
Definition: cel.c:176
static struct stasis_forward * cel_bridge_forwarder
Definition: cel.c:126
#define ao2_ref(o, delta)
Definition: astobj2.h:464
char uniqueid[AST_MAX_UNIQUEID]
Definition: cel.c:174
static void destroy_subscriptions(void)
Definition: cel.c:1422
uint32_t version
struct ABI version
Definition: cel.h:146
#define ao2_lock(a)
Definition: astobj2.h:718
const ast_string_field exten
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
struct ast_channel_snapshot_hangup * hangup
Channel Event Userfield Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:217
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
void ast_channel_amaflags_set(struct ast_channel *chan, enum ama_flags value)
A set of macros to manage forward-linked lists.
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
struct ao2_container * container
Definition: res_fax.c:502
struct ast_bridge_snapshot * bridge
static void cel_channel_state_change(struct ast_channel_snapshot *old_snapshot, struct ast_channel_snapshot *new_snapshot, const struct timeval *event_time)
Handle channel state changes.
Definition: cel.c:896
hangup terminates connection
Definition: cel.h:49
struct stasis_topic * stasis_topic_create(const char *name)
Create a new topic.
Definition: stasis.c:618
Blob of data associated with a bridge.
Channel Event CID RDNIS field Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:229
Channels with this particular technology are an implementation detail of Asterisk and should generall...
Definition: channel.h:972
enum aco_category_op category_match
static void cel_pickup_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: cel.c:1368
a transfer occurs
Definition: cel.h:65
static int cel_linkedid_ref(const char *linkedid)
Definition: cel.c:781
static force_inline char * ast_str_to_lower(char *str)
Convert a string to all lower-case.
Definition: strings.h:1268
Core PBX routines and definitions.
static int is_valid_dialstatus(struct ast_multi_channel_blob *blob)
Definition: cel.c:1200
#define stasis_message_router_create(topic)
a channel is parked
Definition: cel.h:61
static int cel_track_app(const char *const_app)
Definition: cel.c:496
struct stasis_topic * ast_cel_topic(void)
Get the CEL topic.
Definition: cel.c:1685
Their was an error and no changes were applied.
const char * ast_channel_uniqueid(const struct ast_channel *chan)
void ast_cel_publish_event(struct ast_channel *chan, enum ast_cel_event_type event_type, struct ast_json *blob)
Publish a CEL event.
Definition: cel.c:1666
struct ast_channel_snapshot_caller * caller
enum ast_attended_transfer_dest_type dest_type
enum ast_cel_event_type event_type
Definition: cel.h:147
struct stasis_message_type * ast_local_optimization_end_type(void)
Message type for when a local channel optimization completes.
Configuration option-handling.
struct stasis_message_type * ast_call_pickup_type(void)
accessor for call pickup message type
a transfer occurs
Definition: cel.h:67
static void * cel_config_alloc(void)
Definition: cel.c:224
struct ast_channel_snapshot * transferer
struct ast_channel_snapshot * channel_snapshot
#define LOG_ERROR
Definition: logger.h:285
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
struct ast_cel_general_config * general
Definition: cel.c:210
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
const ast_string_field dnid
Channel Event User Event Name Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:151
#define ao2_unlink(container, obj)
Definition: astobj2.h:1598
#define CLI_SHOWUSAGE
Definition: cli.h:45
static int apps_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
Definition: cel.c:472
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
void aco_info_destroy(struct aco_info *info)
Destroy an initialized aco_info struct.
#define ao2_global_obj_release(holder)
Definition: astobj2.h:865
Type for default option handler for bools (ast_true/ast_false)
const char * caller_id_rdnis
Definition: cel.h:154
void ast_cel_set_config(struct ast_cel_general_config *config)
Set the current CEL configuration.
Definition: cel.c:1702
char id[0]
Definition: cel.c:166
const char * peer
Definition: cel.h:167
static struct stasis_message_router * cel_state_router
Definition: cel.c:114
static const char * ignore_categories[]
Definition: cel.c:250
struct ast_party_dialed * ast_channel_dialed(struct ast_channel *chan)
char context[AST_MAX_CONTEXT]
static void cel_bridge_leave_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: cel.c:1077
static int cel_filter_channel_snapshot(struct ast_channel_snapshot *snapshot)
Definition: cel.c:986
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
#define LOG_NOTICE
Definition: logger.h:263
struct stasis_message_type * ast_attended_transfer_type(void)
Message type for ast_attended_transfer_message.
const ast_string_field userfield
unsigned int count
Definition: cel.c:164
struct ao2_container * apps
Definition: cel.h:214
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:710
static int create_routes(void)
Create the Stasis message router and routes for CEL.
Definition: cel.c:1500
struct ast_bridge_snapshot * bridge_snapshot
static struct stasis_topic * cel_topic
Definition: cel.c:117
channel end
Definition: cel.h:47
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic&#39;s subscribers.
Definition: stasis.c:1511
static const char name[]
Definition: cdr_mysql.c:74
#define ast_free(a)
Definition: astmm.h:182
char * command
Definition: cli.h:186
static int reload(void)
Definition: cdr_mysql.c:741
#define ast_var_assign(name, value)
Definition: chanvars.h:40
Channel Event Peer – for Things involving multiple channels, like BRIDGE Used by: AST_EVENT_CEL Payl...
Definition: event_defs.h:241
Channel Event CID ANI field Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:223
int aco_set_defaults(struct aco_type *type, const char *category, void *obj)
Set all default options of obj.
Module could not be loaded properly.
Definition: module.h:102
struct stasis_forward * stasis_forward_cancel(struct stasis_forward *forward)
Definition: stasis.c:1548
#define STRFLDSET(type,...)
Convert a struct and a list of stringfield fields to an argument list of field offsets.
struct ast_channel_snapshot * new_snapshot
enum ast_channel_state state
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
const ast_string_field uniqueid
Definition: bridge.h:336
const char * caller_id_ani
Definition: cel.h:153
An API for managing task processing threads that can be shared across modules.
const ast_string_field parkinglot
Definition: parking.h:69
const char * user_field
Definition: cel.h:166
Channel Event AMA flags Used by: AST_EVENT_CEL Payload type: UINT.
Definition: event_defs.h:199
struct stasis_message_type * ast_channel_snapshot_type(void)
Message type for ast_channel_snapshot_update.
struct ast_channel_snapshot * old_snapshot
#define AST_CEL_EVENT_RECORD_VERSION
struct ABI version
Definition: cel.h:141
struct ast_party_redirecting * ast_channel_redirecting(struct ast_channel *chan)
Support for logging to various files, console and syslog Configuration in file logger.conf.
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
struct stasis_message * ast_channel_blob_create_from_cache(const char *uniqueid, struct stasis_message_type *type, struct ast_json *blob)
Create a ast_channel_blob message, pulling channel state from the cache.
struct stasis_topic * ast_bridge_topic_all(void)
A topic which publishes the events for all bridges.
const char * usage
Definition: cli.h:177
void ast_channel_exten_set(struct ast_channel *chan, const char *value)
static int is_enabled(void)
Helper function to check if module is enabled.
Definition: res_ari.c:159
uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has an integer payload.
Definition: event.c:293
const ast_string_field ani
const char * peer_account
Definition: cel.h:162
static void save_dialstatus(struct ast_multi_channel_blob *blob, struct ast_channel_snapshot *snapshot)
Definition: cel.c:1156
static struct ast_str * cel_generate_peer_str(struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *chan)
Definition: cel.c:1009
int ast_cel_backend_unregister(const char *name)
Unregister a CEL backend.
Definition: cel.c:1728
#define CLI_SUCCESS
Definition: cli.h:44
ast_cel_backend_cb callback
Definition: cel.c:327
static int cel_report_event(struct ast_channel_snapshot *snapshot, enum ast_cel_event_type event_type, const struct timeval *event_time, const char *userdefevname, struct ast_json *extra, const char *peer_str)
Definition: cel.c:569
#define ao2_global_obj_replace_unref(holder, obj)
Definition: astobj2.h:908
A structure to hold CEL global configuration options.
Definition: cel.h:205
static void cel_channel_app_change(struct ast_channel_snapshot *old_snapshot, struct ast_channel_snapshot *new_snapshot, const struct timeval *event_time)
Definition: cel.c:955
#define ACO_FILES(...)
void * data
Definition: datastore.h:70
struct stasis_topic * ast_parking_topic(void)
accessor for the parking stasis topic
Definition: parking.c:67
#define AO2_STRING_FIELD_CMP_FN(stype, field)
Creates a compare function for a structure string field.
Definition: astobj2.h:2071
Channel Event LinkedID Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:247
char * strsep(char **str, const char *delims)
void ast_event_destroy(struct ast_event *event)
Destroy an event.
Definition: event.c:524
#define NUM_DIALSTATUS_BUCKETS
Number of buckets for the dialstatus container.
Definition: cel.c:160
struct ast_channel_snapshot * ast_channel_snapshot_get_latest(const char *uniqueid)
Obtain the latest ast_channel_snapshot from the Stasis Message Bus API cache. This is an ao2 object...
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Standard Command Line Interface.
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
struct ast_event * ast_event_new(enum ast_event_type event_type,...)
Create a new event.
Definition: event.c:402
Type information about a category-level configurable object.
const ast_string_field number
void ast_channel_context_set(struct ast_channel *chan, const char *value)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
A ringing phone is answered.
Definition: cel.h:51
struct ast_flags flags
struct ast_channel_snapshot * ast_multi_channel_blob_get_channel(struct ast_multi_channel_blob *obj, const char *role)
Retrieve a channel snapshot associated with a specific role from a ast_multi_channel_blob.
const char * filename
A multi channel blob data structure for multi_channel_blob stasis messages.
static AO2_GLOBAL_OBJ_STATIC(cel_backends)
channel out of the park
Definition: cel.h:63
const char * unique_id
Definition: cel.h:163
static void cel_bridge_enter_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: cel.c:1047
static const char * get_blob_variable(struct ast_multi_channel_blob *blob, const char *varname)
Definition: cel.c:880
struct stasis_forward * sub
Definition: res_corosync.c:240
const ast_string_field parker_dial_string
Definition: parking.h:69
Abstract JSON element (object, array, string, int, ...).
Type for default option handler for stringfields.
static void destroy_routes(void)
Definition: cel.c:1416
a user-defined event, the event name field should be set
Definition: cel.h:69
channel exits a bridge
Definition: cel.h:59
static struct stasis_forward * cel_cel_forwarder
Definition: cel.c:132
ast_cel_event_type
CEL event types.
Definition: cel.h:41
Forwarding information.
Definition: stasis.c:1531
const char * caller_id_dnid
Definition: cel.h:155
struct stasis_message_type * ast_channel_dial_type(void)
Message type for when a channel dials another channel.
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:89
const char * channel_name
Definition: cel.h:158
int ast_cel_fill_record(const struct ast_event *e, struct ast_cel_event_record *r)
Fill in an ast_cel_event_record from a CEL event.
Definition: cel.c:819
Generic container type.
unsigned char valid
TRUE if the name information is valid/present.
Definition: channel.h:280
struct stasis_message_type * cel_generic_type(void)
const char * event_name
Definition: cel.h:149
an app starts
Definition: cel.h:53
struct ast_channel_snapshot * transferee
struct stasis_forward * stasis_forward_all(struct stasis_topic *from_topic, struct stasis_topic *to_topic)
Create a subscription which forwards all messages from one topic to another.
Definition: stasis.c:1578
const char * ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has a string payload.
Definition: event.c:302
int stasis_message_router_set_congestion_limits(struct stasis_message_router *router, long low_water, long high_water)
Set the high and low alert water marks of the stasis message router.
struct ast_channel_snapshot_peer * peer
void ast_channel_name_set(struct ast_channel *chan, const char *name)
Set the channel name.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
struct ast_channel_snapshot * retriever
Definition: parking.h:61
Bridging API.
static const char app[]
Definition: app_mysql.c:62
Asterisk module definitions.
static char * handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: cel.c:353
intmax_t ast_json_integer_get(const struct ast_json *integer)
Get the value from a JSON integer.
Definition: json.c:322
int ast_cel_backend_register(const char *name, ast_cel_backend_cb backend_callback)
Register a CEL backend.
Definition: cel.c:1740
#define AST_MAX_UNIQUEID
Definition: channel.h:169
static int load_module(void)
Definition: cel.c:1571
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2390
static struct aco_type * general_options[]
Definition: cel.c:300
struct timeval event_time
Definition: cel.h:148
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:368
char name[0]
Definition: cel.c:328
a directed pickup was performed on this channel
Definition: cel.h:73
unsigned char valid
TRUE if the number information is valid/present.
Definition: channel.h:298
static void cel_general_config_dtor(void *obj)
Destructor for cel_config.
Definition: cel.c:180
enum ast_transfer_result result
this call was forwarded somewhere else
Definition: cel.h:75
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
static int create_subscriptions(void)
Create the Stasis subscriptions for CEL.
Definition: cel.c:1454
void ast_channel_data_set(struct ast_channel *chan, const char *value)
int ast_str_container_add(struct ao2_container *str_container, const char *add)
Adds a string to a string container allocated by ast_str_container_alloc.
Definition: strings.c:206
const char * ast_cel_get_type_name(enum ast_cel_event_type type)
Get the name of a CEL event type.
Definition: cel.c:491
Channel Event AccountCode Used by: AST_EVENT_CEL Payload type: STR.
Definition: event_defs.h:205
const ast_string_field name
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620
void ast_channel_internal_set_fake_ids(struct ast_channel *chan, const char *uniqueid, const char *linkedid)
Set uniqueid and linkedid string value only (not time)
static int enabled
Definition: dnsmgr.c:91
static struct test_val a
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:343
#define ao2_link(container, obj)
Definition: astobj2.h:1549
static void cel_generic_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: cel.c:1256