2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2010, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 * \brief Call Completion Supplementary Services implementation
21 * \author Mark Michelson <mmichelson@digium.com>
26 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
28 #include "asterisk/astobj2.h"
29 #include "asterisk/strings.h"
30 #include "asterisk/ccss.h"
31 #include "asterisk/channel.h"
32 #include "asterisk/pbx.h"
33 #include "asterisk/utils.h"
34 #include "asterisk/taskprocessor.h"
35 #include "asterisk/event.h"
36 #include "asterisk/devicestate.h"
37 #include "asterisk/module.h"
38 #include "asterisk/app.h"
39 #include "asterisk/cli.h"
40 #include "asterisk/manager.h"
41 #include "asterisk/causes.h"
44 <application name="CallCompletionRequest" language="en_US">
46 Request call completion service for previous call
50 <para>Request call completion service for a previously failed
52 <para>This application sets the following channel variables:</para>
54 <variable name="CC_REQUEST_RESULT">
55 <para>This is the returned status of the request.</para>
56 <value name="SUCCESS" />
59 <variable name="CC_REQUEST_REASON">
60 <para>This is the reason the request failed.</para>
61 <value name="NO_CORE_INSTANCE" />
62 <value name="NOT_GENERIC" />
63 <value name="TOO_MANY_REQUESTS" />
64 <value name="UNSPECIFIED" />
69 <application name="CallCompletionCancel" language="en_US">
71 Cancel call completion service
75 <para>Cancel a Call Completion Request.</para>
76 <para>This application sets the following channel variables:</para>
78 <variable name="CC_CANCEL_RESULT">
79 <para>This is the returned status of the cancel.</para>
80 <value name="SUCCESS" />
83 <variable name="CC_CANCEL_REASON">
84 <para>This is the reason the cancel failed.</para>
85 <value name="NO_CORE_INSTANCE" />
86 <value name="NOT_GENERIC" />
87 <value name="UNSPECIFIED" />
94 /* These are some file-scoped variables. It would be
95 * nice to define them closer to their first usage, but since
96 * they are used in many places throughout the file, defining
97 * them here at the top is easiest.
101 * The ast_sched_context used for all generic CC timeouts
103 static struct ast_sched_context *cc_sched_context;
105 * Counter used to create core IDs for CC calls. Each new
106 * core ID is created by atomically adding 1 to the core_id_counter
108 static int core_id_counter;
110 * Taskprocessor from which all CC agent and monitor callbacks
113 static struct ast_taskprocessor *cc_core_taskprocessor;
115 * Name printed on all CC log messages.
117 static const char *CC_LOGGER_LEVEL_NAME = "CC";
119 * Logger level registered by the CC core.
121 static int cc_logger_level;
123 * Parsed configuration value for cc_max_requests
125 static unsigned int global_cc_max_requests;
127 * The current number of CC requests in the system
129 static int cc_request_count;
131 static inline void *cc_ref(void *obj, const char *debug)
133 ao2_t_ref(obj, +1, debug);
137 static inline void *cc_unref(void *obj, const char *debug)
139 ao2_t_ref(obj, -1, debug);
146 * \brief A structure for holding the configuration parameters
149 struct ast_cc_config_params {
150 enum ast_cc_agent_policies cc_agent_policy;
151 enum ast_cc_monitor_policies cc_monitor_policy;
152 unsigned int cc_offer_timer;
153 unsigned int ccnr_available_timer;
154 unsigned int ccbs_available_timer;
155 unsigned int cc_recall_timer;
156 unsigned int cc_max_agents;
157 unsigned int cc_max_monitors;
158 char cc_callback_macro[AST_MAX_EXTENSION];
159 char cc_agent_dialstring[AST_MAX_EXTENSION];
164 * \brief The states used in the CCSS core state machine
166 * For more information, see doc/CCSS_architecture.pdf
169 /*! Entered when it is determined that CCSS may be used for the call */
171 /*! Entered when a CCSS agent has offered CCSS to a caller */
173 /*! Entered when a CCSS agent confirms that a caller has
176 /*! Entered when a CCSS monitor confirms acknowledgment of an
177 * outbound CCSS request */
179 /*! Entered when a CCSS monitor alerts the core that the called party
180 * has become available */
182 /*! Entered when a CCSS agent alerts the core that the calling party
183 * may not be recalled because he is unavailable
186 /*! Entered when a CCSS agent alerts the core that the calling party
187 * is attempting to recall the called party
190 /*! Entered when an application alerts the core that the calling party's
191 * recall attempt has had a call progress response indicated
194 /*! Entered any time that something goes wrong during the process, thus
195 * resulting in the failure of the attempted CCSS transaction. Note also
196 * that cancellations of CC are treated as failures.
202 * \brief The payload for an AST_CONTROL_CC frame
205 * This contains all the necessary data regarding
206 * a called device so that the CC core will be able
207 * to allocate the proper monitoring resources.
209 struct cc_control_payload {
211 * \brief The type of monitor to allocate.
214 * The type of monitor to allocate. This is a string which corresponds
215 * to a set of monitor callbacks registered. Examples include "generic"
218 * \note This really should be an array of characters in case this payload
219 * is sent accross an IAX2 link. However, this would not make too much sense
220 * given this type may not be recognized by the other end.
221 * Protection may be necessary to prevent it from being transmitted.
223 * In addition the following other problems are also possible:
224 * 1) Endian issues with the integers/enums stored in the config_params.
225 * 2) Alignment padding issues for the element types.
227 const char *monitor_type;
229 * \brief Private data allocated by the callee
232 * All channel drivers that monitor endpoints will need to allocate
233 * data that is not usable by the CC core. In most cases, some or all
234 * of this data is allocated at the time that the channel driver offers
235 * CC to the caller. There are many opportunities for failures to occur
236 * between when a channel driver offers CC and when a monitor is actually
237 * allocated to watch the endpoint. For this reason, the channel driver
238 * must give the core a pointer to the private data that was allocated so
239 * that the core can call back into the channel driver to destroy it if
240 * a failure occurs. If no private data has been allocated at the time that
241 * CC is offered, then it is perfectly acceptable to pass NULL for this
246 * \brief Service offered by the endpoint
249 * This indicates the type of call completion service offered by the
250 * endpoint. This data is not crucial to the machinations of the CC core,
251 * but it is helpful for debugging purposes.
253 enum ast_cc_service_type service;
255 * \brief Configuration parameters used by this endpoint
258 * Each time an endpoint offers call completion, it must provide its call
259 * completion configuration parameters. This is because settings may be different
260 * depending on the circumstances.
262 struct ast_cc_config_params config_params;
264 * \brief ID of parent extension
267 * This is the only datum that the CC core derives on its own and is not
268 * provided by the offerer of CC. This provides the core with information on
269 * which extension monitor is the most immediate parent of this device.
271 int parent_interface_id;
273 * \brief Name of device to be monitored
276 * The device name by which this monitored endpoint will be referred in the
277 * CC core. It is highly recommended that this device name is derived by using
278 * the function ast_channel_get_device_name.
280 char device_name[AST_CHANNEL_NAME];
282 * \brief Recall dialstring
285 * Certain channel drivers (DAHDI in particular) will require that a special
286 * dialstring be used to indicate that the outgoing call is to interpreted as
287 * a CC recall. If the channel driver has such a requirement, then this is
288 * where that special recall dialstring is placed. If no special dialstring
289 * is to be used, then the channel driver must provide the original dialstring
290 * used to call this endpoint.
292 char dialstring[AST_CHANNEL_NAME];
296 * \brief The "tree" of interfaces that is dialed.
299 * Though this is a linked list, it is logically treated
300 * as a tree of monitors. Each monitor has an id and a parent_id
301 * associated with it. The id is a unique ID for that monitor, and
302 * the parent_id is the unique ID of the monitor's parent in the
303 * tree. The tree is structured such that all of a parent's children
304 * will appear after the parent in the tree. However, it cannot be
305 * guaranteed exactly where after the parent the children are.
307 * The tree is reference counted since several threads may need
308 * to use it, and it may last beyond the lifetime of a single
311 AST_LIST_HEAD(cc_monitor_tree, ast_cc_monitor);
313 static const int CC_CORE_INSTANCES_BUCKETS = 17;
314 static struct ao2_container *cc_core_instances;
316 struct cc_core_instance {
318 * Unique identifier for this instance of the CC core.
322 * The current state for this instance of the CC core.
324 enum cc_state current_state;
326 * The CC agent in use for this call
328 struct ast_cc_agent *agent;
330 * Reference to the monitor tree formed during the initial call
332 struct cc_monitor_tree *monitors;
337 * \brief Request that the core change states
338 * \param state The state to which we wish to change
339 * \param core_id The unique identifier for this instance of the CCSS core state machine
340 * \param debug Optional message explaining the reason for the state change
341 * \param ap varargs list
342 * \retval 0 State change successfully queued
343 * \retval -1 Unable to queue state change request
345 static int __attribute__((format(printf, 3, 0))) cc_request_state_change(enum cc_state state, const int core_id, const char *debug, va_list ap);
349 * \brief create a new instance of the CC core and an agent for the calling channel
351 * This function will check to make sure that the incoming channel
352 * is allowed to request CC by making sure that the incoming channel
353 * has not exceeded its maximum number of allowed agents.
355 * Should that check pass, the core instance is created, and then the
356 * agent for the channel.
358 * \param caller_chan The incoming channel for this particular call
359 * \param called_tree A reference to the tree of called devices. The agent
360 * will gain a reference to this tree as well
361 * \param core_id The core_id that this core_instance will assume
362 * \retval NULL Failed to create the core instance either due to memory allocation
363 * errors or due to the agent count for the caller being too high
364 * \retval non-NULL A reference to the newly created cc_core_instance
366 static struct cc_core_instance *cc_core_init_instance(struct ast_channel *caller_chan,
367 struct cc_monitor_tree *called_tree, const int core_id, struct cc_control_payload *cc_data);
369 static const struct {
370 enum ast_cc_service_type service;
371 const char *service_string;
372 } cc_service_to_string_map[] = {
373 {AST_CC_NONE, "NONE"},
374 {AST_CC_CCBS, "CCBS"},
375 {AST_CC_CCNR, "CCNR"},
376 {AST_CC_CCNL, "CCNL"},
379 static const struct {
381 const char *state_string;
382 } cc_state_to_string_map[] = {
383 {CC_AVAILABLE, "CC is available"},
384 {CC_CALLER_OFFERED, "CC offered to caller"},
385 {CC_CALLER_REQUESTED, "CC requested by caller"},
386 {CC_ACTIVE, "CC accepted by callee"},
387 {CC_CALLEE_READY, "Callee has become available"},
388 {CC_CALLER_BUSY, "Callee was ready, but caller is now unavailable"},
389 {CC_RECALLING, "Caller is attempting to recall"},
390 {CC_COMPLETE, "Recall complete"},
391 {CC_FAILED, "CC has failed"},
394 static const char *cc_state_to_string(enum cc_state state)
396 return cc_state_to_string_map[state].state_string;
399 static const char *cc_service_to_string(enum ast_cc_service_type service)
401 return cc_service_to_string_map[service].service_string;
404 static int cc_core_instance_hash_fn(const void *obj, const int flags)
406 const struct cc_core_instance *core_instance = obj;
407 return core_instance->core_id;
410 static int cc_core_instance_cmp_fn(void *obj, void *arg, int flags)
412 struct cc_core_instance *core_instance1 = obj;
413 struct cc_core_instance *core_instance2 = arg;
415 return core_instance1->core_id == core_instance2->core_id ? CMP_MATCH | CMP_STOP : 0;
418 static struct cc_core_instance *find_cc_core_instance(const int core_id)
420 struct cc_core_instance finder = {.core_id = core_id,};
422 return ao2_t_find(cc_core_instances, &finder, OBJ_POINTER, "Finding a core_instance");
425 struct cc_callback_helper {
426 ao2_callback_fn *function;
431 static int cc_agent_callback_helper(void *obj, void *args, int flags)
433 struct cc_core_instance *core_instance = obj;
434 struct cc_callback_helper *helper = args;
436 if (strcmp(core_instance->agent->callbacks->type, helper->type)) {
440 return helper->function(core_instance->agent, helper->args, flags);
443 struct ast_cc_agent *ast_cc_agent_callback(int flags, ao2_callback_fn *function, void *args, const char * const type)
445 struct cc_callback_helper helper = {.function = function, .args = args, .type = type};
446 struct cc_core_instance *core_instance;
447 if ((core_instance = ao2_t_callback(cc_core_instances, flags, cc_agent_callback_helper, &helper,
448 "Calling provided agent callback function"))) {
449 struct ast_cc_agent *agent = cc_ref(core_instance->agent, "An outside entity needs the agent");
450 cc_unref(core_instance, "agent callback done with the core_instance");
457 /* Only match agents that have not yet
460 MATCH_NO_REQUEST = (1 << 0),
461 /* Only match agents that have made
464 MATCH_REQUEST = (1 << 1),
467 /* ao2_callbacks for cc_core_instances */
471 * \brief find a core instance based on its agent
473 * The match flags tell whether we wish to find core instances
474 * that have a monitor or core instances that do not. Core instances
475 * with no monitor are core instances for which a caller has not yet
476 * requested CC. Core instances with a monitor are ones for which the
477 * caller has requested CC.
479 static int match_agent(void *obj, void *arg, void *data, int flags)
481 struct cc_core_instance *core_instance = obj;
482 const char *name = arg;
483 unsigned long match_flags = *(unsigned long *)data;
484 int possible_match = 0;
486 if ((match_flags & MATCH_NO_REQUEST) && core_instance->current_state < CC_CALLER_REQUESTED) {
490 if ((match_flags & MATCH_REQUEST) && core_instance->current_state >= CC_CALLER_REQUESTED) {
494 if (!possible_match) {
498 if (!strcmp(core_instance->agent->device_name, name)) {
499 return CMP_MATCH | CMP_STOP;
504 struct count_agents_cb_data {
506 int core_id_exception;
511 * \brief Count the number of agents a specific interface is using
513 * We're only concerned with the number of agents that have requested
514 * CC, so we restrict our search to core instances which have a non-NULL
517 static int count_agents_cb(void *obj, void *arg, void *data, int flags)
519 struct cc_core_instance *core_instance = obj;
520 const char *name = arg;
521 struct count_agents_cb_data *cb_data = data;
523 if (cb_data->core_id_exception == core_instance->core_id) {
524 ast_log_dynamic_level(cc_logger_level, "Found agent with core_id %d but not counting it toward total\n", core_instance->core_id);
528 if (core_instance->current_state >= CC_CALLER_REQUESTED && !strcmp(core_instance->agent->device_name, name)) {
534 /* default values mapping from cc_state to ast_dev_state */
536 #define CC_AVAILABLE_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
537 #define CC_CALLER_OFFERED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
538 #define CC_CALLER_REQUESTED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
539 #define CC_ACTIVE_DEVSTATE_DEFAULT AST_DEVICE_INUSE
540 #define CC_CALLEE_READY_DEVSTATE_DEFAULT AST_DEVICE_RINGING
541 #define CC_CALLER_BUSY_DEVSTATE_DEFAULT AST_DEVICE_ONHOLD
542 #define CC_RECALLING_DEVSTATE_DEFAULT AST_DEVICE_RINGING
543 #define CC_COMPLETE_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
544 #define CC_FAILED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
548 * \brief initialization of defaults for CC_STATE to DEVICE_STATE map
550 static enum ast_device_state cc_state_to_devstate_map[] = {
551 [CC_AVAILABLE] = CC_AVAILABLE_DEVSTATE_DEFAULT,
552 [CC_CALLER_OFFERED] = CC_CALLER_OFFERED_DEVSTATE_DEFAULT,
553 [CC_CALLER_REQUESTED] = CC_CALLER_REQUESTED_DEVSTATE_DEFAULT,
554 [CC_ACTIVE] = CC_ACTIVE_DEVSTATE_DEFAULT,
555 [CC_CALLEE_READY] = CC_CALLEE_READY_DEVSTATE_DEFAULT,
556 [CC_CALLER_BUSY] = CC_CALLER_BUSY_DEVSTATE_DEFAULT,
557 [CC_RECALLING] = CC_RECALLING_DEVSTATE_DEFAULT,
558 [CC_COMPLETE] = CC_COMPLETE_DEVSTATE_DEFAULT,
559 [CC_FAILED] = CC_FAILED_DEVSTATE_DEFAULT,
564 * \brief lookup the ast_device_state mapped to cc_state
566 * \return the correponding DEVICE STATE from the cc_state_to_devstate_map
567 * when passed an internal state.
569 static enum ast_device_state cc_state_to_devstate(enum cc_state state)
571 return cc_state_to_devstate_map[state];
576 * \brief Callback for devicestate providers
579 * Initialize with ast_devstate_prov_add() and returns the corresponding
580 * DEVICE STATE based on the current CC_STATE state machine if the requested
581 * device is found and is a generic device. Returns the equivalent of
582 * CC_FAILED, which defaults to NOT_INUSE, if no device is found. NOT_INUSE would
583 * indicate that there is no presence of any pending call back.
585 static enum ast_device_state ccss_device_state(const char *device_name)
587 struct cc_core_instance *core_instance;
588 unsigned long match_flags;
589 enum ast_device_state cc_current_state;
591 match_flags = MATCH_NO_REQUEST;
592 core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent,
593 (char *) device_name, &match_flags,
594 "Find Core Instance for ccss_device_state reqeust.");
595 if (!core_instance) {
596 ast_log_dynamic_level(cc_logger_level,
597 "Couldn't find a core instance for caller %s\n", device_name);
598 return cc_state_to_devstate(CC_FAILED);
601 ast_log_dynamic_level(cc_logger_level,
602 "Core %d: Found core_instance for caller %s in state %s\n",
603 core_instance->core_id, device_name, cc_state_to_string(core_instance->current_state));
605 if (strcmp(core_instance->agent->callbacks->type, "generic")) {
606 ast_log_dynamic_level(cc_logger_level,
607 "Core %d: Device State is only for generic agent types.\n",
608 core_instance->core_id);
609 cc_unref(core_instance, "Unref core_instance since ccss_device_state was called with native agent");
610 return cc_state_to_devstate(CC_FAILED);
612 cc_current_state = cc_state_to_devstate(core_instance->current_state);
613 cc_unref(core_instance, "Unref core_instance done with ccss_device_state");
614 return cc_current_state;
619 * \brief Notify Device State Changes from CC STATE MACHINE
622 * Any time a state is changed, we call this function to notify the DEVICE STATE
623 * subsystem of the change so that subscribed phones to any corresponding hints that
624 * are using that state are updated.
626 static void ccss_notify_device_state_change(const char *device, enum cc_state state)
628 enum ast_device_state devstate;
630 devstate = cc_state_to_devstate(state);
632 ast_log_dynamic_level(cc_logger_level,
633 "Notification of CCSS state change to '%s', device state '%s' for device '%s'\n",
634 cc_state_to_string(state), ast_devstate2str(devstate), device);
636 ast_devstate_changed(devstate, "ccss:%s", device);
639 #define CC_OFFER_TIMER_DEFAULT 20 /* Seconds */
640 #define CCNR_AVAILABLE_TIMER_DEFAULT 7200 /* Seconds */
641 #define CCBS_AVAILABLE_TIMER_DEFAULT 4800 /* Seconds */
642 #define CC_RECALL_TIMER_DEFAULT 20 /* Seconds */
643 #define CC_MAX_AGENTS_DEFAULT 5
644 #define CC_MAX_MONITORS_DEFAULT 5
645 #define GLOBAL_CC_MAX_REQUESTS_DEFAULT 20
647 static const struct ast_cc_config_params cc_default_params = {
648 .cc_agent_policy = AST_CC_AGENT_NEVER,
649 .cc_monitor_policy = AST_CC_MONITOR_NEVER,
650 .cc_offer_timer = CC_OFFER_TIMER_DEFAULT,
651 .ccnr_available_timer = CCNR_AVAILABLE_TIMER_DEFAULT,
652 .ccbs_available_timer = CCBS_AVAILABLE_TIMER_DEFAULT,
653 .cc_recall_timer = CC_RECALL_TIMER_DEFAULT,
654 .cc_max_agents = CC_MAX_AGENTS_DEFAULT,
655 .cc_max_monitors = CC_MAX_MONITORS_DEFAULT,
656 .cc_callback_macro = "",
657 .cc_agent_dialstring = "",
660 void ast_cc_default_config_params(struct ast_cc_config_params *params)
662 *params = cc_default_params;
665 struct ast_cc_config_params *__ast_cc_config_params_init(const char *file, int line, const char *function)
667 #if defined(__AST_DEBUG_MALLOC)
668 struct ast_cc_config_params *params = __ast_malloc(sizeof(*params), file, line, function);
670 struct ast_cc_config_params *params = ast_malloc(sizeof(*params));
677 ast_cc_default_config_params(params);
681 void ast_cc_config_params_destroy(struct ast_cc_config_params *params)
686 static enum ast_cc_agent_policies str_to_agent_policy(const char * const value)
688 if (!strcasecmp(value, "never")) {
689 return AST_CC_AGENT_NEVER;
690 } else if (!strcasecmp(value, "native")) {
691 return AST_CC_AGENT_NATIVE;
692 } else if (!strcasecmp(value, "generic")) {
693 return AST_CC_AGENT_GENERIC;
695 ast_log(LOG_WARNING, "%s is an invalid value for cc_agent_policy. Switching to 'never'\n", value);
696 return AST_CC_AGENT_NEVER;
700 static enum ast_cc_monitor_policies str_to_monitor_policy(const char * const value)
702 if (!strcasecmp(value, "never")) {
703 return AST_CC_MONITOR_NEVER;
704 } else if (!strcasecmp(value, "native")) {
705 return AST_CC_MONITOR_NATIVE;
706 } else if (!strcasecmp(value, "generic")) {
707 return AST_CC_MONITOR_GENERIC;
708 } else if (!strcasecmp(value, "always")) {
709 return AST_CC_MONITOR_ALWAYS;
711 ast_log(LOG_WARNING, "%s is an invalid value for cc_monitor_policy. Switching to 'never'\n", value);
712 return AST_CC_MONITOR_NEVER;
716 static const char *agent_policy_to_str(enum ast_cc_agent_policies policy)
719 case AST_CC_AGENT_NEVER:
721 case AST_CC_AGENT_NATIVE:
723 case AST_CC_AGENT_GENERIC:
726 /* This should never happen... */
731 static const char *monitor_policy_to_str(enum ast_cc_monitor_policies policy)
734 case AST_CC_MONITOR_NEVER:
736 case AST_CC_MONITOR_NATIVE:
738 case AST_CC_MONITOR_GENERIC:
740 case AST_CC_MONITOR_ALWAYS:
743 /* This should never happen... */
747 int ast_cc_get_param(struct ast_cc_config_params *params, const char * const name,
748 char *buf, size_t buf_len)
750 const char *value = NULL;
752 if (!strcasecmp(name, "cc_callback_macro")) {
753 value = ast_get_cc_callback_macro(params);
754 } else if (!strcasecmp(name, "cc_agent_policy")) {
755 value = agent_policy_to_str(ast_get_cc_agent_policy(params));
756 } else if (!strcasecmp(name, "cc_monitor_policy")) {
757 value = monitor_policy_to_str(ast_get_cc_monitor_policy(params));
758 } else if (!strcasecmp(name, "cc_agent_dialstring")) {
759 value = ast_get_cc_agent_dialstring(params);
762 ast_copy_string(buf, value, buf_len);
766 /* The rest of these are all ints of some sort and require some
770 if (!strcasecmp(name, "cc_offer_timer")) {
771 snprintf(buf, buf_len, "%u", ast_get_cc_offer_timer(params));
772 } else if (!strcasecmp(name, "ccnr_available_timer")) {
773 snprintf(buf, buf_len, "%u", ast_get_ccnr_available_timer(params));
774 } else if (!strcasecmp(name, "ccbs_available_timer")) {
775 snprintf(buf, buf_len, "%u", ast_get_ccbs_available_timer(params));
776 } else if (!strcasecmp(name, "cc_max_agents")) {
777 snprintf(buf, buf_len, "%u", ast_get_cc_max_agents(params));
778 } else if (!strcasecmp(name, "cc_max_monitors")) {
779 snprintf(buf, buf_len, "%u", ast_get_cc_max_monitors(params));
780 } else if (!strcasecmp(name, "cc_recall_timer")) {
781 snprintf(buf, buf_len, "%u", ast_get_cc_recall_timer(params));
783 ast_log(LOG_WARNING, "%s is not a valid CC parameter. Ignoring.\n", name);
790 int ast_cc_set_param(struct ast_cc_config_params *params, const char * const name,
791 const char * const value)
793 unsigned int value_as_uint;
794 if (!strcasecmp(name, "cc_agent_policy")) {
795 return ast_set_cc_agent_policy(params, str_to_agent_policy(value));
796 } else if (!strcasecmp(name, "cc_monitor_policy")) {
797 return ast_set_cc_monitor_policy(params, str_to_monitor_policy(value));
798 } else if (!strcasecmp(name, "cc_agent_dialstring")) {
799 ast_set_cc_agent_dialstring(params, value);
800 } else if (!strcasecmp(name, "cc_callback_macro")) {
801 ast_set_cc_callback_macro(params, value);
805 if (!sscanf(value, "%30u", &value_as_uint) == 1) {
809 if (!strcasecmp(name, "cc_offer_timer")) {
810 ast_set_cc_offer_timer(params, value_as_uint);
811 } else if (!strcasecmp(name, "ccnr_available_timer")) {
812 ast_set_ccnr_available_timer(params, value_as_uint);
813 } else if (!strcasecmp(name, "ccbs_available_timer")) {
814 ast_set_ccbs_available_timer(params, value_as_uint);
815 } else if (!strcasecmp(name, "cc_max_agents")) {
816 ast_set_cc_max_agents(params, value_as_uint);
817 } else if (!strcasecmp(name, "cc_max_monitors")) {
818 ast_set_cc_max_monitors(params, value_as_uint);
819 } else if (!strcasecmp(name, "cc_recall_timer")) {
820 ast_set_cc_recall_timer(params, value_as_uint);
822 ast_log(LOG_WARNING, "%s is not a valid CC parameter. Ignoring.\n", name);
829 int ast_cc_is_config_param(const char * const name)
831 return (!strcasecmp(name, "cc_agent_policy") ||
832 !strcasecmp(name, "cc_monitor_policy") ||
833 !strcasecmp(name, "cc_offer_timer") ||
834 !strcasecmp(name, "ccnr_available_timer") ||
835 !strcasecmp(name, "ccbs_available_timer") ||
836 !strcasecmp(name, "cc_max_agents") ||
837 !strcasecmp(name, "cc_max_monitors") ||
838 !strcasecmp(name, "cc_callback_macro") ||
839 !strcasecmp(name, "cc_agent_dialstring") ||
840 !strcasecmp(name, "cc_recall_timer"));
843 void ast_cc_copy_config_params(struct ast_cc_config_params *dest, const struct ast_cc_config_params *src)
848 enum ast_cc_agent_policies ast_get_cc_agent_policy(struct ast_cc_config_params *config)
850 return config->cc_agent_policy;
853 int ast_set_cc_agent_policy(struct ast_cc_config_params *config, enum ast_cc_agent_policies value)
855 /* Screw C and its weak type checking for making me have to do this
856 * validation at runtime.
858 if (value < AST_CC_AGENT_NEVER || value > AST_CC_AGENT_GENERIC) {
861 config->cc_agent_policy = value;
865 enum ast_cc_monitor_policies ast_get_cc_monitor_policy(struct ast_cc_config_params *config)
867 return config->cc_monitor_policy;
870 int ast_set_cc_monitor_policy(struct ast_cc_config_params *config, enum ast_cc_monitor_policies value)
872 /* Screw C and its weak type checking for making me have to do this
873 * validation at runtime.
875 if (value < AST_CC_MONITOR_NEVER || value > AST_CC_MONITOR_ALWAYS) {
878 config->cc_monitor_policy = value;
882 unsigned int ast_get_cc_offer_timer(struct ast_cc_config_params *config)
884 return config->cc_offer_timer;
887 void ast_set_cc_offer_timer(struct ast_cc_config_params *config, unsigned int value)
889 /* 0 is an unreasonable value for any timer. Stick with the default */
891 ast_log(LOG_WARNING, "0 is an invalid value for cc_offer_timer. Retaining value as %u\n", config->cc_offer_timer);
894 config->cc_offer_timer = value;
897 unsigned int ast_get_ccnr_available_timer(struct ast_cc_config_params *config)
899 return config->ccnr_available_timer;
902 void ast_set_ccnr_available_timer(struct ast_cc_config_params *config, unsigned int value)
904 /* 0 is an unreasonable value for any timer. Stick with the default */
906 ast_log(LOG_WARNING, "0 is an invalid value for ccnr_available_timer. Retaining value as %u\n", config->ccnr_available_timer);
909 config->ccnr_available_timer = value;
912 unsigned int ast_get_cc_recall_timer(struct ast_cc_config_params *config)
914 return config->cc_recall_timer;
917 void ast_set_cc_recall_timer(struct ast_cc_config_params *config, unsigned int value)
919 /* 0 is an unreasonable value for any timer. Stick with the default */
921 ast_log(LOG_WARNING, "0 is an invalid value for ccnr_available_timer. Retaining value as %u\n", config->cc_recall_timer);
924 config->cc_recall_timer = value;
927 unsigned int ast_get_ccbs_available_timer(struct ast_cc_config_params *config)
929 return config->ccbs_available_timer;
932 void ast_set_ccbs_available_timer(struct ast_cc_config_params *config, unsigned int value)
934 /* 0 is an unreasonable value for any timer. Stick with the default */
936 ast_log(LOG_WARNING, "0 is an invalid value for ccbs_available_timer. Retaining value as %u\n", config->ccbs_available_timer);
939 config->ccbs_available_timer = value;
942 const char *ast_get_cc_agent_dialstring(struct ast_cc_config_params *config)
944 return config->cc_agent_dialstring;
947 void ast_set_cc_agent_dialstring(struct ast_cc_config_params *config, const char *const value)
949 if (ast_strlen_zero(value)) {
950 config->cc_agent_dialstring[0] = '\0';
952 ast_copy_string(config->cc_agent_dialstring, value, sizeof(config->cc_agent_dialstring));
956 unsigned int ast_get_cc_max_agents(struct ast_cc_config_params *config)
958 return config->cc_max_agents;
961 void ast_set_cc_max_agents(struct ast_cc_config_params *config, unsigned int value)
963 config->cc_max_agents = value;
966 unsigned int ast_get_cc_max_monitors(struct ast_cc_config_params *config)
968 return config->cc_max_monitors;
971 void ast_set_cc_max_monitors(struct ast_cc_config_params *config, unsigned int value)
973 config->cc_max_monitors = value;
976 const char *ast_get_cc_callback_macro(struct ast_cc_config_params *config)
978 return config->cc_callback_macro;
981 void ast_set_cc_callback_macro(struct ast_cc_config_params *config, const char * const value)
983 if (ast_strlen_zero(value)) {
984 config->cc_callback_macro[0] = '\0';
986 ast_copy_string(config->cc_callback_macro, value, sizeof(config->cc_callback_macro));
990 struct cc_monitor_backend {
991 AST_LIST_ENTRY(cc_monitor_backend) next;
992 const struct ast_cc_monitor_callbacks *callbacks;
995 AST_RWLIST_HEAD_STATIC(cc_monitor_backends, cc_monitor_backend);
997 int ast_cc_monitor_register(const struct ast_cc_monitor_callbacks *callbacks)
999 struct cc_monitor_backend *backend = ast_calloc(1, sizeof(*backend));
1005 backend->callbacks = callbacks;
1007 AST_RWLIST_WRLOCK(&cc_monitor_backends);
1008 AST_RWLIST_INSERT_TAIL(&cc_monitor_backends, backend, next);
1009 AST_RWLIST_UNLOCK(&cc_monitor_backends);
1013 static const struct ast_cc_monitor_callbacks *find_monitor_callbacks(const char * const type)
1015 struct cc_monitor_backend *backend;
1016 const struct ast_cc_monitor_callbacks *callbacks = NULL;
1018 AST_RWLIST_RDLOCK(&cc_monitor_backends);
1019 AST_RWLIST_TRAVERSE(&cc_monitor_backends, backend, next) {
1020 if (!strcmp(backend->callbacks->type, type)) {
1021 ast_log_dynamic_level(cc_logger_level, "Returning monitor backend %s\n", backend->callbacks->type);
1022 callbacks = backend->callbacks;
1026 AST_RWLIST_UNLOCK(&cc_monitor_backends);
1030 void ast_cc_monitor_unregister(const struct ast_cc_monitor_callbacks *callbacks)
1032 struct cc_monitor_backend *backend;
1033 AST_RWLIST_WRLOCK(&cc_monitor_backends);
1034 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&cc_monitor_backends, backend, next) {
1035 if (backend->callbacks == callbacks) {
1036 AST_RWLIST_REMOVE_CURRENT(next);
1041 AST_RWLIST_TRAVERSE_SAFE_END;
1042 AST_RWLIST_UNLOCK(&cc_monitor_backends);
1045 struct cc_agent_backend {
1046 AST_LIST_ENTRY(cc_agent_backend) next;
1047 const struct ast_cc_agent_callbacks *callbacks;
1050 AST_RWLIST_HEAD_STATIC(cc_agent_backends, cc_agent_backend);
1052 int ast_cc_agent_register(const struct ast_cc_agent_callbacks *callbacks)
1054 struct cc_agent_backend *backend = ast_calloc(1, sizeof(*backend));
1060 backend->callbacks = callbacks;
1061 AST_RWLIST_WRLOCK(&cc_agent_backends);
1062 AST_RWLIST_INSERT_TAIL(&cc_agent_backends, backend, next);
1063 AST_RWLIST_UNLOCK(&cc_agent_backends);
1067 void ast_cc_agent_unregister(const struct ast_cc_agent_callbacks *callbacks)
1069 struct cc_agent_backend *backend;
1070 AST_RWLIST_WRLOCK(&cc_agent_backends);
1071 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&cc_agent_backends, backend, next) {
1072 if (backend->callbacks == callbacks) {
1073 AST_RWLIST_REMOVE_CURRENT(next);
1078 AST_RWLIST_TRAVERSE_SAFE_END;
1079 AST_RWLIST_UNLOCK(&cc_agent_backends);
1082 static const struct ast_cc_agent_callbacks *find_agent_callbacks(struct ast_channel *chan)
1084 struct cc_agent_backend *backend;
1085 const struct ast_cc_agent_callbacks *callbacks = NULL;
1086 struct ast_cc_config_params *cc_params;
1089 cc_params = ast_channel_get_cc_config_params(chan);
1093 switch (ast_get_cc_agent_policy(cc_params)) {
1094 case AST_CC_AGENT_GENERIC:
1095 ast_copy_string(type, "generic", sizeof(type));
1097 case AST_CC_AGENT_NATIVE:
1098 ast_channel_get_cc_agent_type(chan, type, sizeof(type));
1101 ast_log_dynamic_level(cc_logger_level, "Not returning agent callbacks since this channel is configured not to have a CC agent\n");
1105 AST_RWLIST_RDLOCK(&cc_agent_backends);
1106 AST_RWLIST_TRAVERSE(&cc_agent_backends, backend, next) {
1107 if (!strcmp(backend->callbacks->type, type)) {
1108 ast_log_dynamic_level(cc_logger_level, "Returning agent backend %s\n", backend->callbacks->type);
1109 callbacks = backend->callbacks;
1113 AST_RWLIST_UNLOCK(&cc_agent_backends);
1119 * \brief Determine if the given device state is considered available by generic CCSS.
1122 * \param state Device state to test.
1124 * \return TRUE if the given device state is considered available by generic CCSS.
1126 static int cc_generic_is_device_available(enum ast_device_state state)
1128 return state == AST_DEVICE_NOT_INUSE || state == AST_DEVICE_UNKNOWN;
1131 static int cc_generic_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id);
1132 static int cc_generic_monitor_suspend(struct ast_cc_monitor *monitor);
1133 static int cc_generic_monitor_unsuspend(struct ast_cc_monitor *monitor);
1134 static int cc_generic_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id);
1135 static void cc_generic_monitor_destructor(void *private_data);
1137 static struct ast_cc_monitor_callbacks generic_monitor_cbs = {
1139 .request_cc = cc_generic_monitor_request_cc,
1140 .suspend = cc_generic_monitor_suspend,
1141 .unsuspend = cc_generic_monitor_unsuspend,
1142 .cancel_available_timer = cc_generic_monitor_cancel_available_timer,
1143 .destructor = cc_generic_monitor_destructor,
1146 struct ao2_container *generic_monitors;
1148 struct generic_monitor_instance {
1152 AST_LIST_ENTRY(generic_monitor_instance) next;
1155 struct generic_monitor_instance_list {
1156 const char *device_name;
1157 enum ast_device_state current_state;
1158 /* If there are multiple instances monitoring the
1159 * same device and one should fail, we need to know
1160 * whether to signal that the device can be recalled.
1161 * The problem is that the device state is not enough
1162 * to check. If a caller has requested CCNR, then the
1163 * fact that the device is available does not indicate
1164 * that the device is ready to be recalled. Instead, as
1165 * soon as one instance of the monitor becomes available
1166 * for a recall, we mark the entire list as being fit
1167 * for recall. If a CCNR request comes in, then we will
1168 * have to mark the list as unfit for recall since this
1169 * is a clear indicator that the person at the monitored
1170 * device has gone away and is actuall not fit to be
1174 struct ast_event_sub *sub;
1175 AST_LIST_HEAD_NOLOCK(, generic_monitor_instance) list;
1179 * \brief private data for generic device monitor
1181 struct generic_monitor_pvt {
1183 * We need the device name during destruction so we
1184 * can find the appropriate item to destroy.
1186 const char *device_name;
1188 * We need the core ID for similar reasons. Once we
1189 * find the appropriate item in our ao2_container, we
1190 * need to remove the appropriate cc_monitor from the
1196 static int generic_monitor_hash_fn(const void *obj, const int flags)
1198 const struct generic_monitor_instance_list *generic_list = obj;
1199 return ast_str_hash(generic_list->device_name);
1202 static int generic_monitor_cmp_fn(void *obj, void *arg, int flags)
1204 const struct generic_monitor_instance_list *generic_list1 = obj;
1205 const struct generic_monitor_instance_list *generic_list2 = arg;
1207 return !strcmp(generic_list1->device_name, generic_list2->device_name) ? CMP_MATCH | CMP_STOP : 0;
1210 static struct generic_monitor_instance_list *find_generic_monitor_instance_list(const char * const device_name)
1212 struct generic_monitor_instance_list finder = {.device_name = device_name};
1214 return ao2_t_find(generic_monitors, &finder, OBJ_POINTER, "Finding generic monitor instance list");
1217 static void generic_monitor_instance_list_destructor(void *obj)
1219 struct generic_monitor_instance_list *generic_list = obj;
1220 struct generic_monitor_instance *generic_instance;
1222 generic_list->sub = ast_event_unsubscribe(generic_list->sub);
1223 while ((generic_instance = AST_LIST_REMOVE_HEAD(&generic_list->list, next))) {
1224 ast_free(generic_instance);
1226 ast_free((char *)generic_list->device_name);
1229 static void generic_monitor_devstate_cb(const struct ast_event *event, void *userdata);
1230 static struct generic_monitor_instance_list *create_new_generic_list(struct ast_cc_monitor *monitor)
1232 struct generic_monitor_instance_list *generic_list = ao2_t_alloc(sizeof(*generic_list),
1233 generic_monitor_instance_list_destructor, "allocate generic monitor instance list");
1235 if (!generic_list) {
1239 if (!(generic_list->device_name = ast_strdup(monitor->interface->device_name))) {
1240 cc_unref(generic_list, "Failed to strdup the monitor's device name");
1244 if (!(generic_list->sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE,
1245 generic_monitor_devstate_cb, "Requesting CC", NULL,
1246 AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, monitor->interface->device_name,
1247 AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_EXISTS,
1248 AST_EVENT_IE_END))) {
1249 cc_unref(generic_list, "Failed to subscribe to device state");
1252 generic_list->current_state = ast_device_state(monitor->interface->device_name);
1253 ao2_t_link(generic_monitors, generic_list, "linking new generic monitor instance list");
1254 return generic_list;
1257 struct generic_tp_cb_data {
1258 const char *device_name;
1259 enum ast_device_state new_state;
1262 static int generic_monitor_devstate_tp_cb(void *data)
1264 struct generic_tp_cb_data *gtcd = data;
1265 enum ast_device_state new_state = gtcd->new_state;
1266 enum ast_device_state previous_state = gtcd->new_state;
1267 const char *monitor_name = gtcd->device_name;
1268 struct generic_monitor_instance_list *generic_list;
1269 struct generic_monitor_instance *generic_instance;
1271 if (!(generic_list = find_generic_monitor_instance_list(monitor_name))) {
1272 /* The most likely cause for this is that we destroyed the monitor in the
1273 * time between subscribing to its device state and the time this executes.
1274 * Not really a big deal.
1276 ast_free((char *) gtcd->device_name);
1281 if (generic_list->current_state == new_state) {
1282 /* The device state hasn't actually changed, so we don't really care */
1283 cc_unref(generic_list, "Kill reference of generic list in devstate taskprocessor callback");
1284 ast_free((char *) gtcd->device_name);
1289 previous_state = generic_list->current_state;
1290 generic_list->current_state = new_state;
1292 if (cc_generic_is_device_available(new_state) &&
1293 (previous_state == AST_DEVICE_INUSE || previous_state == AST_DEVICE_UNAVAILABLE ||
1294 previous_state == AST_DEVICE_BUSY)) {
1295 AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
1296 if (!generic_instance->is_suspended && generic_instance->monitoring) {
1297 generic_instance->monitoring = 0;
1298 generic_list->fit_for_recall = 1;
1299 ast_cc_monitor_callee_available(generic_instance->core_id, "Generic monitored party has become available");
1304 cc_unref(generic_list, "Kill reference of generic list in devstate taskprocessor callback");
1305 ast_free((char *) gtcd->device_name);
1310 static void generic_monitor_devstate_cb(const struct ast_event *event, void *userdata)
1312 /* Wow, it's cool that we've picked up on a state change, but we really want
1313 * the actual work to be done in the core's taskprocessor execution thread
1314 * so that all monitor operations can be serialized. Locks?! We don't need
1315 * no steenkin' locks!
1317 struct generic_tp_cb_data *gtcd = ast_calloc(1, sizeof(*gtcd));
1323 if (!(gtcd->device_name = ast_strdup(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE)))) {
1327 gtcd->new_state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
1329 if (ast_taskprocessor_push(cc_core_taskprocessor, generic_monitor_devstate_tp_cb, gtcd)) {
1330 ast_free((char *)gtcd->device_name);
1335 int ast_cc_available_timer_expire(const void *data)
1337 struct ast_cc_monitor *monitor = (struct ast_cc_monitor *) data;
1339 monitor->available_timer_id = -1;
1340 res = ast_cc_monitor_failed(monitor->core_id, monitor->interface->device_name, "Available timer expired for monitor");
1341 cc_unref(monitor, "Unref reference from scheduler\n");
1345 static int cc_generic_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id)
1347 struct generic_monitor_instance_list *generic_list;
1348 struct generic_monitor_instance *generic_instance;
1349 struct generic_monitor_pvt *gen_mon_pvt;
1350 enum ast_cc_service_type service = monitor->service_offered;
1353 /* First things first. Native channel drivers will have their private data allocated
1354 * at the time that they tell the core that they can offer CC. Generic is quite a bit
1355 * different, and we wait until this point to allocate our private data.
1357 if (!(gen_mon_pvt = ast_calloc(1, sizeof(*gen_mon_pvt)))) {
1361 if (!(gen_mon_pvt->device_name = ast_strdup(monitor->interface->device_name))) {
1362 ast_free(gen_mon_pvt);
1366 gen_mon_pvt->core_id = monitor->core_id;
1368 monitor->private_data = gen_mon_pvt;
1370 if (!(generic_list = find_generic_monitor_instance_list(monitor->interface->device_name))) {
1371 if (!(generic_list = create_new_generic_list(monitor))) {
1376 if (!(generic_instance = ast_calloc(1, sizeof(*generic_instance)))) {
1377 /* The generic monitor destructor will take care of the appropriate
1380 cc_unref(generic_list, "Generic monitor instance failed to allocate");
1383 generic_instance->core_id = monitor->core_id;
1384 generic_instance->monitoring = 1;
1385 AST_LIST_INSERT_TAIL(&generic_list->list, generic_instance, next);
1386 when = service == AST_CC_CCBS ? ast_get_ccbs_available_timer(monitor->interface->config_params) :
1387 ast_get_ccnr_available_timer(monitor->interface->config_params);
1389 *available_timer_id = ast_sched_add(cc_sched_context, when * 1000,
1390 ast_cc_available_timer_expire, cc_ref(monitor, "Give the scheduler a monitor reference"));
1391 if (*available_timer_id == -1) {
1392 cc_unref(monitor, "Failed to schedule available timer. (monitor)");
1393 cc_unref(generic_list, "Failed to schedule available timer. (generic_list)");
1396 /* If the new instance was created as CCNR, then that means this device is not currently
1397 * fit for recall even if it previously was.
1399 if (service == AST_CC_CCNR || service == AST_CC_CCNL) {
1400 generic_list->fit_for_recall = 0;
1402 ast_cc_monitor_request_acked(monitor->core_id, "Generic monitor for %s subscribed to device state.",
1403 monitor->interface->device_name);
1404 cc_unref(generic_list, "Finished with monitor instance reference in request cc callback");
1408 static int cc_generic_monitor_suspend(struct ast_cc_monitor *monitor)
1410 struct generic_monitor_instance_list *generic_list;
1411 struct generic_monitor_instance *generic_instance;
1412 enum ast_device_state state = ast_device_state(monitor->interface->device_name);
1414 if (!(generic_list = find_generic_monitor_instance_list(monitor->interface->device_name))) {
1418 /* First we need to mark this particular monitor as being suspended. */
1419 AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
1420 if (generic_instance->core_id == monitor->core_id) {
1421 generic_instance->is_suspended = 1;
1426 /* If the device being suspended is currently in use, then we don't need to
1427 * take any further actions
1429 if (!cc_generic_is_device_available(state)) {
1430 cc_unref(generic_list, "Device is in use. Nothing to do. Unref generic list.");
1434 /* If the device is not in use, though, then it may be possible to report the
1435 * device's availability using a different monitor which is monitoring the
1439 AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
1440 if (!generic_instance->is_suspended) {
1441 ast_cc_monitor_callee_available(generic_instance->core_id, "Generic monitored party has become available");
1445 cc_unref(generic_list, "Done with generic list in suspend callback");
1449 static int cc_generic_monitor_unsuspend(struct ast_cc_monitor *monitor)
1451 struct generic_monitor_instance *generic_instance;
1452 struct generic_monitor_instance_list *generic_list = find_generic_monitor_instance_list(monitor->interface->device_name);
1453 enum ast_device_state state = ast_device_state(monitor->interface->device_name);
1455 if (!generic_list) {
1458 /* If the device is currently available, we can immediately announce
1461 if (cc_generic_is_device_available(state)) {
1462 ast_cc_monitor_callee_available(monitor->core_id, "Generic monitored party has become available");
1465 /* In addition, we need to mark this generic_monitor_instance as not being suspended anymore */
1466 AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
1467 if (generic_instance->core_id == monitor->core_id) {
1468 generic_instance->is_suspended = 0;
1469 generic_instance->monitoring = 1;
1473 cc_unref(generic_list, "Done with generic list in cc_generic_monitor_unsuspend");
1477 static int cc_generic_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id)
1479 ast_assert(sched_id != NULL);
1481 if (*sched_id == -1) {
1485 ast_log_dynamic_level(cc_logger_level, "Core %d: Canceling generic monitor available timer for monitor %s\n",
1486 monitor->core_id, monitor->interface->device_name);
1487 if (!ast_sched_del(cc_sched_context, *sched_id)) {
1488 cc_unref(monitor, "Remove scheduler's reference to the monitor");
1494 static void cc_generic_monitor_destructor(void *private_data)
1496 struct generic_monitor_pvt *gen_mon_pvt = private_data;
1497 struct generic_monitor_instance_list *generic_list;
1498 struct generic_monitor_instance *generic_instance;
1500 if (!private_data) {
1501 /* If the private data is NULL, that means that the monitor hasn't even
1502 * been created yet, but that the destructor was called. While this sort
1503 * of behavior is useful for native monitors, with a generic one, there is
1504 * nothing in particular to do.
1509 ast_log_dynamic_level(cc_logger_level, "Core %d: Destroying generic monitor %s\n",
1510 gen_mon_pvt->core_id, gen_mon_pvt->device_name);
1512 if (!(generic_list = find_generic_monitor_instance_list(gen_mon_pvt->device_name))) {
1513 /* If there's no generic list, that means that the monitor is being destroyed
1514 * before we actually got to request CC. Not a biggie. Same in the situation
1515 * below if the list traversal should complete without finding an entry.
1517 ast_free((char *)gen_mon_pvt->device_name);
1518 ast_free(gen_mon_pvt);
1522 AST_LIST_TRAVERSE_SAFE_BEGIN(&generic_list->list, generic_instance, next) {
1523 if (generic_instance->core_id == gen_mon_pvt->core_id) {
1524 AST_LIST_REMOVE_CURRENT(next);
1525 ast_free(generic_instance);
1529 AST_LIST_TRAVERSE_SAFE_END;
1531 if (AST_LIST_EMPTY(&generic_list->list)) {
1532 /* No more monitors with this device name exist. Time to unlink this
1533 * list from the container
1535 ao2_t_unlink(generic_monitors, generic_list, "Generic list is empty. Unlink it from the container");
1537 /* There are still instances for this particular device. The situation
1538 * may be that we were attempting a CC recall and a failure occurred, perhaps
1539 * on the agent side. If a failure happens here and the device being monitored
1540 * is available, then we need to signal on the first unsuspended instance that
1541 * the device is available for recall.
1544 /* First things first. We don't even want to consider this action if
1545 * the device in question isn't available right now.
1547 if (generic_list->fit_for_recall
1548 && cc_generic_is_device_available(generic_list->current_state)) {
1549 AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
1550 if (!generic_instance->is_suspended && generic_instance->monitoring) {
1551 ast_cc_monitor_callee_available(generic_instance->core_id, "Signaling generic monitor "
1552 "availability due to other instance's failure.");
1558 cc_unref(generic_list, "Done with generic list in generic monitor destructor");
1559 ast_free((char *)gen_mon_pvt->device_name);
1560 ast_free(gen_mon_pvt);
1563 static void cc_interface_destroy(void *data)
1565 struct ast_cc_interface *interface = data;
1566 ast_log_dynamic_level(cc_logger_level, "Destroying cc interface %s\n", interface->device_name);
1567 ast_cc_config_params_destroy(interface->config_params);
1571 * \brief Data regarding an extension monitor's child's dialstrings
1574 * In developing CCSS, we had most aspects of its operation finished,
1575 * but there was one looming problem that we had failed to get right.
1576 * In our design document, we stated that when a CC recall occurs, all
1577 * endpoints that had been dialed originally would be called back.
1578 * Unfortunately, our implementation only allowed for devices which had
1579 * active monitors to inhabit the CC_INTERFACES channel variable, thus
1580 * making the automated recall only call monitored devices.
1582 * Devices that were not CC-capable, or devices which failed CC at some
1583 * point during the process would not make it into the CC_INTERFACES
1584 * channel variable. This struct is meant as a remedy for the problem.
1586 struct extension_child_dialstring {
1588 * \brief the original dialstring used to call a particular device
1591 * When someone dials a particular endpoint, the dialstring used in
1592 * the dialplan is copied into this buffer. What's important here is
1593 * that this is the ORIGINAL dialstring, not the dialstring saved on
1594 * a device monitor. The dialstring on a device monitor is what should
1595 * be used when recalling that device. The two dialstrings may not be
1598 * By keeping a copy of the original dialstring used, we can fall back
1599 * to using it if the device either does not ever offer CC or if the
1600 * device at some point fails for some reason, such as a timer expiration.
1602 char original_dialstring[AST_CHANNEL_NAME];
1604 * \brief The name of the device being dialed
1607 * This serves mainly as a key when searching for a particular dialstring.
1608 * For instance, let's say that we have called device SIP/400@somepeer. This
1609 * device offers call completion, but then due to some unforeseen circumstance,
1610 * this device backs out and makes CC unavailable. When that happens, we need
1611 * to find the dialstring that corresponds to that device, and we use the
1612 * stored device name as a way to find it.
1614 * \note There is one particular case where the device name stored here
1615 * will be empty. This is the case where we fail to request a channel, but we
1616 * still can make use of generic call completion. In such a case, since we never
1617 * were able to request the channel, we can't find what its device name is. In
1618 * this case, however, it is not important because the dialstring is guaranteed
1619 * to be the same both here and in the device monitor.
1621 char device_name[AST_CHANNEL_NAME];
1623 * \brief Is this structure valid for use in CC_INTERFACES?
1626 * When this structure is first created, all information stored here is planned
1627 * to be used, so we set the is_valid flag. However, if a device offers call
1628 * completion, it will potentially have its own dialstring to use for the recall,
1629 * so we find this structure and clear the is_valid flag. By clearing the is_valid
1630 * flag, we won't try to populate the CC_INTERFACES variable with the dialstring
1631 * stored in this struct. Now, if later, the device which had offered CC should fail,
1632 * perhaps due to a timer expiration, then we need to re-set the is_valid flag. This
1633 * way, we still will end up placing a call to the device again, and the dialstring
1634 * used will be the same as was originally used.
1637 AST_LIST_ENTRY(extension_child_dialstring) next;
1641 * \brief Private data for an extension monitor
1643 struct extension_monitor_pvt {
1644 AST_LIST_HEAD_NOLOCK(, extension_child_dialstring) child_dialstrings;
1647 static void cc_extension_monitor_destructor(void *private_data)
1649 struct extension_monitor_pvt *extension_pvt = private_data;
1650 struct extension_child_dialstring *child_dialstring;
1652 /* This shouldn't be possible, but I'm paranoid */
1653 if (!extension_pvt) {
1657 while ((child_dialstring = AST_LIST_REMOVE_HEAD(&extension_pvt->child_dialstrings, next))) {
1658 ast_free(child_dialstring);
1660 ast_free(extension_pvt);
1663 static void cc_monitor_destroy(void *data)
1665 struct ast_cc_monitor *monitor = data;
1666 /* During the monitor creation process, it is possible for this
1667 * function to be called prior to when callbacks are assigned
1668 * to the monitor. Also, extension monitors do not have callbacks
1669 * assigned to them, so we wouldn't want to segfault when we try
1670 * to destroy one of them.
1672 ast_log_dynamic_level(cc_logger_level, "Core %d: Calling destructor for monitor %s\n",
1673 monitor->core_id, monitor->interface->device_name);
1674 if (monitor->interface->monitor_class == AST_CC_EXTENSION_MONITOR) {
1675 cc_extension_monitor_destructor(monitor->private_data);
1677 if (monitor->callbacks) {
1678 monitor->callbacks->destructor(monitor->private_data);
1680 cc_unref(monitor->interface, "Unreffing tree's reference to interface");
1681 ast_free(monitor->dialstring);
1684 static void cc_interface_tree_destroy(void *data)
1686 struct cc_monitor_tree *cc_interface_tree = data;
1687 struct ast_cc_monitor *monitor;
1688 while ((monitor = AST_LIST_REMOVE_HEAD(cc_interface_tree, next))) {
1689 if (monitor->callbacks) {
1690 monitor->callbacks->cancel_available_timer(monitor, &monitor->available_timer_id);
1692 cc_unref(monitor, "Destroying all monitors");
1694 AST_LIST_HEAD_DESTROY(cc_interface_tree);
1698 * This counter is used for assigning unique ids
1699 * to CC-enabled dialed interfaces.
1701 static int dialed_cc_interface_counter;
1705 * \brief data stored in CC datastore
1707 * The datastore creates a list of interfaces that were
1708 * dialed, including both extensions and devices. In addition
1709 * to the intrinsic data of the tree, some extra information
1710 * is needed for use by app_dial.
1712 struct dialed_cc_interfaces {
1714 * This value serves a dual-purpose. When dial starts, if the
1715 * dialed_cc_interfaces datastore currently exists on the calling
1716 * channel, then the dial_parent_id will serve as a means of
1717 * letting the new extension cc_monitor we create know
1718 * who his parent is. This value will be the extension
1719 * cc_monitor that dialed the local channel that resulted
1720 * in the new Dial app being called.
1722 * In addition, once an extension cc_monitor is created,
1723 * the dial_parent_id will be changed to the id of that newly
1724 * created interface. This way, device interfaces created from
1725 * receiving AST_CONTROL_CC frames can use this field to determine
1726 * who their parent extension interface should be.
1728 unsigned int dial_parent_id;
1730 * Identifier for the potential CC request that may be made
1731 * based on this call. Even though an instance of the core may
1732 * not be made (since the caller may not request CC), we allocate
1733 * a new core_id at the beginning of the call so that recipient
1734 * channel drivers can have the information handy just in case
1735 * the caller does end up requesting CC.
1739 * When a new Dial application is started, and the datastore
1740 * already exists on the channel, we can determine if we
1741 * should be adding any new interface information to tree.
1745 * When it comes time to offer CC to the caller, we only want to offer
1746 * it to the original incoming channel. For nested Dials and outbound
1747 * channels, it is incorrect to attempt such a thing. This flag indicates
1748 * if the channel to which this datastore is attached may be legally
1749 * offered CC when the call is finished.
1751 char is_original_caller;
1753 * Reference-counted "tree" of interfaces.
1755 struct cc_monitor_tree *interface_tree;
1760 * \brief Destructor function for cc_interfaces datastore
1762 * This function will free the actual datastore and drop
1763 * the refcount for the monitor tree by one. In cases
1764 * where CC can actually be used, this unref will not
1765 * result in the destruction of the monitor tree, because
1766 * the CC core will still have a reference.
1768 * \param data The dialed_cc_interfaces struct to destroy
1770 static void dialed_cc_interfaces_destroy(void *data)
1772 struct dialed_cc_interfaces *cc_interfaces = data;
1773 cc_unref(cc_interfaces->interface_tree, "Unref dial's ref to monitor tree");
1774 ast_free(cc_interfaces);
1779 * \brief Duplicate callback for cc_interfaces datastore
1781 * Integers are copied by value, but the monitor tree
1782 * is done via a shallow copy and a bump of the refcount.
1783 * This way, sub-Dials will be appending interfaces onto
1784 * the same list as this call to Dial.
1786 * \param data The old dialed_cc_interfaces we want to copy
1787 * \retval NULL Could not allocate memory for new dialed_cc_interfaces
1788 * \retval non-NULL The new copy of the dialed_cc_interfaces
1790 static void *dialed_cc_interfaces_duplicate(void *data)
1792 struct dialed_cc_interfaces *old_cc_interfaces = data;
1793 struct dialed_cc_interfaces *new_cc_interfaces = ast_calloc(1, sizeof(*new_cc_interfaces));
1794 if (!new_cc_interfaces) {
1797 new_cc_interfaces->ignore = old_cc_interfaces->ignore;
1798 new_cc_interfaces->dial_parent_id = old_cc_interfaces->dial_parent_id;
1799 new_cc_interfaces->is_original_caller = 0;
1800 cc_ref(old_cc_interfaces->interface_tree, "New ref due to duplication of monitor tree");
1801 new_cc_interfaces->core_id = old_cc_interfaces->core_id;
1802 new_cc_interfaces->interface_tree = old_cc_interfaces->interface_tree;
1803 return new_cc_interfaces;
1808 * \brief information regarding the dialed_cc_interfaces datastore
1810 * The dialed_cc_interfaces datastore is responsible for keeping track
1811 * of what CC-enabled interfaces have been dialed by the caller. For
1812 * more information regarding the actual structure of the tree, see
1813 * the documentation provided in include/asterisk/ccss.h
1815 static const struct ast_datastore_info dialed_cc_interfaces_info = {
1816 .type = "Dial CC Interfaces",
1817 .duplicate = dialed_cc_interfaces_duplicate,
1818 .destroy = dialed_cc_interfaces_destroy,
1821 static struct extension_monitor_pvt *extension_monitor_pvt_init(void)
1823 struct extension_monitor_pvt *ext_pvt = ast_calloc(1, sizeof(*ext_pvt));
1827 AST_LIST_HEAD_INIT_NOLOCK(&ext_pvt->child_dialstrings);
1831 void ast_cc_extension_monitor_add_dialstring(struct ast_channel *incoming, const char * const dialstring, const char * const device_name)
1833 struct ast_datastore *cc_datastore;
1834 struct dialed_cc_interfaces *cc_interfaces;
1835 struct ast_cc_monitor *monitor;
1836 struct extension_monitor_pvt *extension_pvt;
1837 struct extension_child_dialstring *child_dialstring;
1838 struct cc_monitor_tree *interface_tree;
1841 ast_channel_lock(incoming);
1842 if (!(cc_datastore = ast_channel_datastore_find(incoming, &dialed_cc_interfaces_info, NULL))) {
1843 ast_channel_unlock(incoming);
1847 cc_interfaces = cc_datastore->data;
1848 interface_tree = cc_interfaces->interface_tree;
1849 id = cc_interfaces->dial_parent_id;
1850 ast_channel_unlock(incoming);
1852 AST_LIST_LOCK(interface_tree);
1853 AST_LIST_TRAVERSE(interface_tree, monitor, next) {
1854 if (monitor->id == id) {
1860 AST_LIST_UNLOCK(interface_tree);
1864 extension_pvt = monitor->private_data;
1865 if (!(child_dialstring = ast_calloc(1, sizeof(*child_dialstring)))) {
1866 AST_LIST_UNLOCK(interface_tree);
1869 ast_copy_string(child_dialstring->original_dialstring, dialstring, sizeof(child_dialstring->original_dialstring));
1870 ast_copy_string(child_dialstring->device_name, device_name, sizeof(child_dialstring->device_name));
1871 child_dialstring->is_valid = 1;
1872 AST_LIST_INSERT_TAIL(&extension_pvt->child_dialstrings, child_dialstring, next);
1873 AST_LIST_UNLOCK(interface_tree);
1876 static void cc_extension_monitor_change_is_valid(struct cc_core_instance *core_instance, unsigned int parent_id, const char * const device_name, int is_valid)
1878 struct ast_cc_monitor *monitor_iter;
1879 struct extension_monitor_pvt *extension_pvt;
1880 struct extension_child_dialstring *child_dialstring;
1882 AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
1883 if (monitor_iter->id == parent_id) {
1888 if (!monitor_iter) {
1891 extension_pvt = monitor_iter->private_data;
1893 AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) {
1894 if (!strcmp(child_dialstring->device_name, device_name)) {
1895 child_dialstring->is_valid = is_valid;
1903 * \brief Allocate and initialize an "extension" interface for CC purposes
1905 * When app_dial starts, this function is called in order to set up the
1906 * information about the extension in which this Dial is occurring. Any
1907 * devices dialed will have this particular cc_monitor as a parent.
1909 * \param exten Extension from which Dial is occurring
1910 * \param context Context to which exten belongs
1911 * \param parent_id What should we set the parent_id of this interface to?
1912 * \retval NULL Memory allocation failure
1913 * \retval non-NULL The newly-created cc_monitor for the extension
1915 static struct ast_cc_monitor *cc_extension_monitor_init(const char * const exten, const char * const context, const unsigned int parent_id)
1917 struct ast_str *str = ast_str_alloca(2 * AST_MAX_EXTENSION);
1918 struct ast_cc_interface *cc_interface;
1919 struct ast_cc_monitor *monitor;
1921 ast_str_set(&str, 0, "%s@%s", exten, context);
1923 if (!(cc_interface = ao2_t_alloc(sizeof(*cc_interface) + ast_str_strlen(str), cc_interface_destroy,
1924 "Allocating new ast_cc_interface"))) {
1928 if (!(monitor = ao2_t_alloc(sizeof(*monitor), cc_monitor_destroy, "Allocating new ast_cc_monitor"))) {
1929 cc_unref(cc_interface, "failed to allocate the monitor, so unref the interface");
1933 if (!(monitor->private_data = extension_monitor_pvt_init())) {
1934 cc_unref(monitor, "Failed to initialize extension monitor private data. uref monitor");
1935 cc_unref(cc_interface, "Failed to initialize extension monitor private data. unref cc_interface");
1938 monitor->id = ast_atomic_fetchadd_int(&dialed_cc_interface_counter, +1);
1939 monitor->parent_id = parent_id;
1940 cc_interface->monitor_type = "extension";
1941 cc_interface->monitor_class = AST_CC_EXTENSION_MONITOR;
1942 strcpy(cc_interface->device_name, ast_str_buffer(str));
1943 monitor->interface = cc_interface;
1944 ast_log_dynamic_level(cc_logger_level, "Created an extension cc interface for '%s' with id %d and parent %d\n", cc_interface->device_name, monitor->id, monitor->parent_id);
1950 * \brief allocate dialed_cc_interfaces datastore and initialize fields
1952 * This function is called when Situation 1 occurs in ast_cc_call_init.
1953 * See that function for more information on what Situation 1 is.
1955 * In this particular case, we have to do a lot of memory allocation in order
1956 * to create the datastore, the data for the datastore, the tree of interfaces
1957 * that we'll be adding to, and the initial extension interface for this Dial
1960 * \param chan The channel onto which the datastore should be added.
1961 * \retval -1 An error occurred
1964 static int cc_interfaces_datastore_init(struct ast_channel *chan) {
1965 struct dialed_cc_interfaces *interfaces;
1966 struct ast_cc_monitor *monitor;
1967 struct ast_datastore *dial_cc_datastore;
1969 /*XXX This may be a bit controversial. In an attempt to not allocate
1970 * extra resources, I make sure that a future request will be within
1971 * limits. The problem here is that it is reasonable to think that
1972 * even if we're not within the limits at this point, we may be by
1973 * the time the requestor will have made his request. This may be
1974 * deleted at some point.
1976 if (!ast_cc_request_is_within_limits()) {
1980 if (!(interfaces = ast_calloc(1, sizeof(*interfaces)))) {
1984 if (!(monitor = cc_extension_monitor_init(S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)), S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), 0))) {
1985 ast_free(interfaces);
1989 if (!(dial_cc_datastore = ast_datastore_alloc(&dialed_cc_interfaces_info, NULL))) {
1990 cc_unref(monitor, "Could not allocate the dialed interfaces datastore. Unreffing monitor");
1991 ast_free(interfaces);
1995 if (!(interfaces->interface_tree = ao2_t_alloc(sizeof(*interfaces->interface_tree), cc_interface_tree_destroy,
1996 "Allocate monitor tree"))) {
1997 ast_datastore_free(dial_cc_datastore);
1998 cc_unref(monitor, "Could not allocate monitor tree on dialed interfaces datastore. Unreffing monitor");
1999 ast_free(interfaces);
2003 /* Finally, all that allocation is done... */
2004 AST_LIST_HEAD_INIT(interfaces->interface_tree);
2005 AST_LIST_INSERT_TAIL(interfaces->interface_tree, monitor, next);
2006 cc_ref(monitor, "List's reference to extension monitor");
2007 dial_cc_datastore->data = interfaces;
2008 dial_cc_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
2009 interfaces->dial_parent_id = monitor->id;
2010 interfaces->core_id = monitor->core_id = ast_atomic_fetchadd_int(&core_id_counter, +1);
2011 interfaces->is_original_caller = 1;
2012 ast_channel_lock(chan);
2013 ast_channel_datastore_add(chan, dial_cc_datastore);
2014 ast_channel_unlock(chan);
2015 cc_unref(monitor, "Unreffing allocation's reference");
2021 * \brief Call a monitor's destructor before the monitor has been allocated
2024 * \param monitor_type The type of monitor callbacks to use when calling the destructor
2025 * \param private_data Data allocated by a channel driver that must be freed
2028 * I'll admit, this is a bit evil.
2030 * When a channel driver determines that it can offer a call completion service to
2031 * a caller, it is very likely that the channel driver will need to allocate some
2032 * data so that when the time comes to request CC, the channel driver will have the
2033 * necessary data at hand.
2035 * The problem is that there are many places where failures may occur before the monitor
2036 * has been properly allocated and had its callbacks assigned to it. If one of these
2037 * failures should occur, then we still need to let the channel driver know that it
2038 * must destroy the data that it allocated.
2042 static void call_destructor_with_no_monitor(const char * const monitor_type, void *private_data)
2044 const struct ast_cc_monitor_callbacks *monitor_callbacks = find_monitor_callbacks(monitor_type);
2046 if (!monitor_callbacks) {
2050 monitor_callbacks->destructor(private_data);
2055 * \brief Allocate and intitialize a device cc_monitor
2057 * For all intents and purposes, this is the same as
2058 * cc_extension_monitor_init, except that there is only
2059 * a single parameter used for naming the interface.
2061 * This function is called when handling AST_CONTROL_CC frames.
2062 * The device has reported that CC is possible, so we add it
2063 * to the interface_tree.
2065 * Note that it is not necessarily erroneous to add the same
2066 * device to the tree twice. If the same device is called by
2067 * two different extension during the same call, then
2068 * that is a legitimate situation. Of course, I'm pretty sure
2069 * the dialed_interfaces global datastore will not allow that
2072 * \param device_name The name of the device being added to the tree
2073 * \param dialstring The dialstring used to dial the device being added
2074 * \param parent_id The parent of this new tree node.
2075 * \retval NULL Memory allocation failure
2076 * \retval non-NULL The new ast_cc_interface created.
2078 static struct ast_cc_monitor *cc_device_monitor_init(const char * const device_name, const char * const dialstring, const struct cc_control_payload *cc_data, int core_id)
2080 struct ast_cc_interface *cc_interface;
2081 struct ast_cc_monitor *monitor;
2082 size_t device_name_len = strlen(device_name);
2083 int parent_id = cc_data->parent_interface_id;
2085 if (!(cc_interface = ao2_t_alloc(sizeof(*cc_interface) + device_name_len, cc_interface_destroy,
2086 "Allocating new ast_cc_interface"))) {
2090 if (!(cc_interface->config_params = ast_cc_config_params_init())) {
2091 cc_unref(cc_interface, "Failed to allocate config params, unref interface");
2095 if (!(monitor = ao2_t_alloc(sizeof(*monitor), cc_monitor_destroy, "Allocating new ast_cc_monitor"))) {
2096 cc_unref(cc_interface, "Failed to allocate monitor, unref interface");
2100 if (!(monitor->dialstring = ast_strdup(dialstring))) {
2101 cc_unref(monitor, "Failed to copy dialable name. Unref monitor");
2102 cc_unref(cc_interface, "Failed to copy dialable name");
2106 if (!(monitor->callbacks = find_monitor_callbacks(cc_data->monitor_type))) {
2107 cc_unref(monitor, "Failed to find monitor callbacks. Unref monitor");
2108 cc_unref(cc_interface, "Failed to find monitor callbacks");
2112 strcpy(cc_interface->device_name, device_name);
2113 monitor->id = ast_atomic_fetchadd_int(&dialed_cc_interface_counter, +1);
2114 monitor->parent_id = parent_id;
2115 monitor->core_id = core_id;
2116 monitor->service_offered = cc_data->service;
2117 monitor->private_data = cc_data->private_data;
2118 cc_interface->monitor_type = cc_data->monitor_type;
2119 cc_interface->monitor_class = AST_CC_DEVICE_MONITOR;
2120 monitor->interface = cc_interface;
2121 monitor->available_timer_id = -1;
2122 ast_cc_copy_config_params(cc_interface->config_params, &cc_data->config_params);
2123 ast_log_dynamic_level(cc_logger_level, "Core %d: Created a device cc interface for '%s' with id %d and parent %d\n",
2124 monitor->core_id, cc_interface->device_name, monitor->id, monitor->parent_id);
2130 * Unless we are ignoring CC for some reason, we will always
2131 * call this function when we read an AST_CONTROL_CC frame
2132 * from an outbound channel.
2134 * This function will call cc_device_monitor_init to
2135 * create the new cc_monitor for the device from which
2136 * we read the frame. In addition, the new device will be added
2137 * to the monitor tree on the dialed_cc_interfaces datastore
2138 * on the inbound channel.
2140 * If this is the first AST_CONTROL_CC frame that we have handled
2141 * for this call, then we will also initialize the CC core for
2144 void ast_handle_cc_control_frame(struct ast_channel *inbound, struct ast_channel *outbound, void *frame_data)
2148 struct ast_cc_monitor *monitor;
2149 struct ast_datastore *cc_datastore;
2150 struct dialed_cc_interfaces *cc_interfaces;
2151 struct cc_control_payload *cc_data = frame_data;
2152 struct cc_core_instance *core_instance;
2154 device_name = cc_data->device_name;
2155 dialstring = cc_data->dialstring;
2157 ast_channel_lock(inbound);
2158 if (!(cc_datastore = ast_channel_datastore_find(inbound, &dialed_cc_interfaces_info, NULL))) {
2159 ast_log(LOG_WARNING, "Unable to retrieve CC datastore while processing CC frame from '%s'. CC services will be unavailable.\n", device_name);
2160 ast_channel_unlock(inbound);
2161 call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
2165 cc_interfaces = cc_datastore->data;
2167 if (cc_interfaces->ignore) {
2168 ast_channel_unlock(inbound);
2169 call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
2173 if (!cc_interfaces->is_original_caller) {
2174 /* If the is_original_caller is not set on the *inbound* channel, then
2175 * it must be a local channel. As such, we do not want to create a core instance
2176 * or an agent for the local channel. Instead, we want to pass this along to the
2177 * other side of the local channel so that the original caller can benefit.
2179 ast_channel_unlock(inbound);
2180 ast_indicate_data(inbound, AST_CONTROL_CC, cc_data, sizeof(*cc_data));
2184 core_instance = find_cc_core_instance(cc_interfaces->core_id);
2185 if (!core_instance) {
2186 core_instance = cc_core_init_instance(inbound, cc_interfaces->interface_tree,
2187 cc_interfaces->core_id, cc_data);
2188 if (!core_instance) {
2189 cc_interfaces->ignore = 1;
2190 ast_channel_unlock(inbound);
2191 call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
2196 ast_channel_unlock(inbound);
2198 /* Yeah this kind of sucks, but luckily most people
2199 * aren't dialing thousands of interfaces on every call
2201 * This traversal helps us to not create duplicate monitors in
2202 * case a device queues multiple CC control frames.
2204 AST_LIST_LOCK(cc_interfaces->interface_tree);
2205 AST_LIST_TRAVERSE(cc_interfaces->interface_tree, monitor, next) {
2206 if (!strcmp(monitor->interface->device_name, device_name)) {
2207 ast_log_dynamic_level(cc_logger_level, "Core %d: Device %s sent us multiple CC control frames. Ignoring those beyond the first.\n",
2208 core_instance->core_id, device_name);
2209 AST_LIST_UNLOCK(cc_interfaces->interface_tree);
2210 cc_unref(core_instance, "Returning early from ast_handle_cc_control_frame. Unref core_instance");
2211 call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
2215 AST_LIST_UNLOCK(cc_interfaces->interface_tree);
2217 if (!(monitor = cc_device_monitor_init(device_name, dialstring, cc_data, core_instance->core_id))) {
2218 ast_log(LOG_WARNING, "Unable to create CC device interface for '%s'. CC services will be unavailable on this interface.\n", device_name);
2219 cc_unref(core_instance, "Returning early from ast_handle_cc_control_frame. Unref core_instance");
2220 call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
2224 AST_LIST_LOCK(cc_interfaces->interface_tree);
2225 cc_ref(monitor, "monitor tree's reference to the monitor");
2226 AST_LIST_INSERT_TAIL(cc_interfaces->interface_tree, monitor, next);
2227 AST_LIST_UNLOCK(cc_interfaces->interface_tree);
2229 cc_extension_monitor_change_is_valid(core_instance, monitor->parent_id, monitor->interface->device_name, 0);
2231 manager_event(EVENT_FLAG_CC, "CCAvailable",
2235 cc_interfaces->core_id, device_name, cc_service_to_string(cc_data->service)
2238 cc_unref(core_instance, "Done with core_instance after handling CC control frame");
2239 cc_unref(monitor, "Unref reference from allocating monitor");
2242 int ast_cc_call_init(struct ast_channel *chan, int *ignore_cc)
2244 /* There are three situations to deal with here:
2246 * 1. The channel does not have a dialed_cc_interfaces datastore on
2247 * it. This means that this is the first time that Dial has
2248 * been called. We need to create/initialize the datastore.
2250 * 2. The channel does have a cc_interface datastore on it and
2251 * the "ignore" indicator is 0. This means that a Local channel
2252 * was called by a "parent" dial. We can check the datastore's
2253 * parent field to see who the root of this particular dial tree
2256 * 3. The channel does have a cc_interface datastore on it and
2257 * the "ignore" indicator is 1. This means that a second Dial call
2258 * is being made from an extension. In this case, we do not
2259 * want to make any additions/modifications to the datastore. We
2260 * will instead set a flag to indicate that CCSS is completely
2261 * disabled for this Dial attempt.
2264 struct ast_datastore *cc_interfaces_datastore;
2265 struct dialed_cc_interfaces *interfaces;
2266 struct ast_cc_monitor *monitor;
2267 struct ast_cc_config_params *cc_params;
2269 ast_channel_lock(chan);
2271 cc_params = ast_channel_get_cc_config_params(chan);
2273 ast_channel_unlock(chan);
2276 if (ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_NEVER) {
2277 /* We can't offer CC to this caller anyway, so don't bother with CC on this call
2280 ast_channel_unlock(chan);
2281 ast_log_dynamic_level(cc_logger_level, "Agent policy for %s is 'never'. CC not possible\n", ast_channel_name(chan));
2285 if (!(cc_interfaces_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
2286 /* Situation 1 has occurred */
2287 ast_channel_unlock(chan);
2288 return cc_interfaces_datastore_init(chan);
2290 interfaces = cc_interfaces_datastore->data;
2291 ast_channel_unlock(chan);
2293 if (interfaces->ignore) {
2294 /* Situation 3 has occurred */
2296 ast_log_dynamic_level(cc_logger_level, "Datastore is present with ignore flag set. Ignoring CC offers on this call\n");
2300 /* Situation 2 has occurred */
2301 if (!(monitor = cc_extension_monitor_init(S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)),
2302 S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), interfaces->dial_parent_id))) {
2305 monitor->core_id = interfaces->core_id;
2306 AST_LIST_LOCK(interfaces->interface_tree);
2307 cc_ref(monitor, "monitor tree's reference to the monitor");
2308 AST_LIST_INSERT_TAIL(interfaces->interface_tree, monitor, next);
2309 AST_LIST_UNLOCK(interfaces->interface_tree);
2310 interfaces->dial_parent_id = monitor->id;
2311 cc_unref(monitor, "Unref monitor's allocation reference");
2315 int ast_cc_request_is_within_limits(void)
2317 return cc_request_count < global_cc_max_requests;
2320 int ast_cc_get_current_core_id(struct ast_channel *chan)
2322 struct ast_datastore *datastore;
2323 struct dialed_cc_interfaces *cc_interfaces;
2326 ast_channel_lock(chan);
2327 if (!(datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
2328 ast_channel_unlock(chan);
2332 cc_interfaces = datastore->data;
2333 core_id_return = cc_interfaces->ignore ? -1 : cc_interfaces->core_id;
2334 ast_channel_unlock(chan);
2335 return core_id_return;
2339 static long count_agents(const char * const caller, const int core_id_exception)
2341 struct count_agents_cb_data data = {.core_id_exception = core_id_exception,};
2343 ao2_t_callback_data(cc_core_instances, OBJ_NODATA, count_agents_cb, (char *)caller, &data, "Counting agents");
2344 ast_log_dynamic_level(cc_logger_level, "Counted %d agents\n", data.count);
2348 static void kill_duplicate_offers(char *caller)
2350 unsigned long match_flags = MATCH_NO_REQUEST;
2351 struct ao2_iterator *dups_iter;
2354 * Must remove the ref that was in cc_core_instances outside of
2355 * the container lock to prevent deadlock.
2357 dups_iter = ao2_t_callback_data(cc_core_instances, OBJ_MULTIPLE | OBJ_UNLINK,
2358 match_agent, caller, &match_flags, "Killing duplicate offers");
2360 /* Now actually unref any duplicate offers by simply destroying the iterator. */
2361 ao2_iterator_destroy(dups_iter);
2365 static void check_callback_sanity(const struct ast_cc_agent_callbacks *callbacks)
2367 ast_assert(callbacks->init != NULL);
2368 ast_assert(callbacks->start_offer_timer != NULL);
2369 ast_assert(callbacks->stop_offer_timer != NULL);
2370 ast_assert(callbacks->respond != NULL);
2371 ast_assert(callbacks->status_request != NULL);
2372 ast_assert(callbacks->start_monitoring != NULL);
2373 ast_assert(callbacks->callee_available != NULL);
2374 ast_assert(callbacks->destructor != NULL);
2377 static void agent_destroy(void *data)
2379 struct ast_cc_agent *agent = data;
2381 if (agent->callbacks) {
2382 agent->callbacks->destructor(agent);
2384 ast_cc_config_params_destroy(agent->cc_params);
2387 static struct ast_cc_agent *cc_agent_init(struct ast_channel *caller_chan,
2388 const char * const caller_name, const int core_id,
2389 struct cc_monitor_tree *interface_tree)
2391 struct ast_cc_agent *agent;
2392 struct ast_cc_config_params *cc_params;
2394 if (!(agent = ao2_t_alloc(sizeof(*agent) + strlen(caller_name), agent_destroy,
2395 "Allocating new ast_cc_agent"))) {
2399 agent->core_id = core_id;
2400 strcpy(agent->device_name, caller_name);
2402 cc_params = ast_channel_get_cc_config_params(caller_chan);
2404 cc_unref(agent, "Could not get channel config params.");
2407 if (!(agent->cc_params = ast_cc_config_params_init())) {
2408 cc_unref(agent, "Could not init agent config params.");
2411 ast_cc_copy_config_params(agent->cc_params, cc_params);
2413 if (!(agent->callbacks = find_agent_callbacks(caller_chan))) {
2414 cc_unref(agent, "Could not find agent callbacks.");
2417 check_callback_sanity(agent->callbacks);
2419 if (agent->callbacks->init(agent, caller_chan)) {
2420 cc_unref(agent, "Agent init callback failed.");
2423 ast_log_dynamic_level(cc_logger_level, "Core %d: Created an agent for caller %s\n",
2424 agent->core_id, agent->device_name);
2428 /* Generic agent callbacks */
2429 static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan);
2430 static int cc_generic_agent_start_offer_timer(struct ast_cc_agent *agent);
2431 static int cc_generic_agent_stop_offer_timer(struct ast_cc_agent *agent);
2432 static void cc_generic_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason);
2433 static int cc_generic_agent_status_request(struct ast_cc_agent *agent);
2434 static int cc_generic_agent_stop_ringing(struct ast_cc_agent *agent);
2435 static int cc_generic_agent_start_monitoring(struct ast_cc_agent *agent);
2436 static int cc_generic_agent_recall(struct ast_cc_agent *agent);
2437 static void cc_generic_agent_destructor(struct ast_cc_agent *agent);
2439 static struct ast_cc_agent_callbacks generic_agent_callbacks = {
2441 .init = cc_generic_agent_init,
2442 .start_offer_timer = cc_generic_agent_start_offer_timer,
2443 .stop_offer_timer = cc_generic_agent_stop_offer_timer,
2444 .respond = cc_generic_agent_respond,
2445 .status_request = cc_generic_agent_status_request,
2446 .stop_ringing = cc_generic_agent_stop_ringing,
2447 .start_monitoring = cc_generic_agent_start_monitoring,
2448 .callee_available = cc_generic_agent_recall,
2449 .destructor = cc_generic_agent_destructor,
2452 struct cc_generic_agent_pvt {
2454 * Subscription to device state
2456 * Used in the CC_CALLER_BUSY state. The
2457 * generic agent will subscribe to the
2458 * device state of the caller in order to
2459 * determine when we may move on
2461 struct ast_event_sub *sub;
2463 * Scheduler id of offer timer.
2469 * When we re-call the caller, we need
2470 * to provide this information to
2471 * ast_request_and_dial so that the
2472 * information will be present in the
2473 * call to the callee
2475 char cid_num[AST_CHANNEL_NAME];
2479 * See the description of cid_num.
2480 * The same applies here, except this
2481 * is the caller's name.
2483 char cid_name[AST_CHANNEL_NAME];
2487 * The original extension dialed. This is used
2488 * so that when performing a recall, we can
2489 * call the proper extension.
2491 char exten[AST_CHANNEL_NAME];
2495 * The original context dialed. This is used
2496 * so that when performaing a recall, we can
2497 * call into the proper context
2499 char context[AST_CHANNEL_NAME];
2502 static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan)
2504 struct cc_generic_agent_pvt *generic_pvt = ast_calloc(1, sizeof(*generic_pvt));
2510 generic_pvt->offer_timer_id = -1;
2511 if (chan->caller.id.number.valid && chan->caller.id.number.str) {
2512 ast_copy_string(generic_pvt->cid_num, chan->caller.id.number.str, sizeof(generic_pvt->cid_num));
2514 if (chan->caller.id.name.valid && chan->caller.id.name.str) {
2515 ast_copy_string(generic_pvt->cid_name, chan->caller.id.name.str, sizeof(generic_pvt->cid_name));
2517 ast_copy_string(generic_pvt->exten, S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)), sizeof(generic_pvt->exten));
2518 ast_copy_string(generic_pvt->context, S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), sizeof(generic_pvt->context));
2519 agent->private_data = generic_pvt;
2520 ast_set_flag(agent, AST_CC_AGENT_SKIP_OFFER);
2524 static int offer_timer_expire(const void *data)
2526 struct ast_cc_agent *agent = (struct ast_cc_agent *) data;
2527 struct cc_generic_agent_pvt *agent_pvt = agent->private_data;
2528 ast_log_dynamic_level(cc_logger_level, "Core %d: Queuing change request because offer timer has expired.\n",
2530 agent_pvt->offer_timer_id = -1;
2531 ast_cc_failed(agent->core_id, "Generic agent %s offer timer expired", agent->device_name);
2532 cc_unref(agent, "Remove scheduler's reference to the agent");
2536 static int cc_generic_agent_start_offer_timer(struct ast_cc_agent *agent)
2540 struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
2542 ast_assert(cc_sched_context != NULL);
2543 ast_assert(agent->cc_params != NULL);
2545 when = ast_get_cc_offer_timer(agent->cc_params) * 1000;
2546 ast_log_dynamic_level(cc_logger_level, "Core %d: About to schedule offer timer expiration for %d ms\n",
2547 agent->core_id, when);
2548 if ((sched_id = ast_sched_add(cc_sched_context, when, offer_timer_expire, cc_ref(agent, "Give scheduler an agent ref"))) == -1) {
2551 generic_pvt->offer_timer_id = sched_id;
2555 static int cc_generic_agent_stop_offer_timer(struct ast_cc_agent *agent)
2557 struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
2559 if (generic_pvt->offer_timer_id != -1) {
2560 if (!ast_sched_del(cc_sched_context, generic_pvt->offer_timer_id)) {
2561 cc_unref(agent, "Remove scheduler's reference to the agent");
2563 generic_pvt->offer_timer_id = -1;
2568 static void cc_generic_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason)
2570 /* The generic agent doesn't have to do anything special to
2571 * acknowledge a CC request. Just return.
2576 static int cc_generic_agent_status_request(struct ast_cc_agent *agent)
2578 ast_cc_agent_status_response(agent->core_id, ast_device_state(agent->device_name));
2582 static int cc_generic_agent_stop_ringing(struct ast_cc_agent *agent)
2584 struct ast_channel *recall_chan = ast_channel_get_by_name_prefix(agent->device_name, strlen(agent->device_name));
2590 ast_softhangup(recall_chan, AST_SOFTHANGUP_EXPLICIT);
2594 static int generic_agent_devstate_unsubscribe(void *data)
2596 struct ast_cc_agent *agent = data;
2597 struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
2599 if (generic_pvt->sub != NULL) {
2600 generic_pvt->sub = ast_event_unsubscribe(generic_pvt->sub);
2602 cc_unref(agent, "Done unsubscribing from devstate");
2606 static void generic_agent_devstate_cb(const struct ast_event *event, void *userdata)
2608 struct ast_cc_agent *agent = userdata;
2609 enum ast_device_state new_state;
2611 new_state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
2612 if (!cc_generic_is_device_available(new_state)) {
2613 /* Not interested in this new state of the device. It is still busy. */
2617 /* We can't unsubscribe from device state events here because it causes a deadlock */
2618 if (ast_taskprocessor_push(cc_core_taskprocessor, generic_agent_devstate_unsubscribe,
2619 cc_ref(agent, "ref agent for device state unsubscription"))) {
2620 cc_unref(agent, "Unref agent unsubscribing from devstate failed");
2622 ast_cc_agent_caller_available(agent->core_id, "%s is no longer busy", agent->device_name);
2625 static int cc_generic_agent_start_monitoring(struct ast_cc_agent *agent)
2627 struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
2628 struct ast_str *str = ast_str_alloca(128);
2630 ast_assert(generic_pvt->sub == NULL);
2631 ast_str_set(&str, 0, "Agent monitoring %s device state since it is busy\n",
2632 agent->device_name);
2634 if (!(generic_pvt->sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE,
2635 generic_agent_devstate_cb, ast_str_buffer(str), agent,
2636 AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, agent->device_name,
2637 AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_EXISTS,
2638 AST_EVENT_IE_END))) {
2644 static void *generic_recall(void *data)
2646 struct ast_cc_agent *agent = data;
2647 struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
2648 const char *interface = S_OR(ast_get_cc_agent_dialstring(agent->cc_params), ast_strdupa(agent->device_name));
2652 struct ast_channel *chan;
2653 const char *callback_macro = ast_get_cc_callback_macro(agent->cc_params);
2654 unsigned int recall_timer = ast_get_cc_recall_timer(agent->cc_params) * 1000;
2655 struct ast_format tmp_fmt;
2656 struct ast_format_cap *tmp_cap = ast_format_cap_alloc_nolock();
2663 if ((target = strchr(interface, '/'))) {
2667 ast_format_cap_add(tmp_cap, ast_format_set(&tmp_fmt, AST_FORMAT_SLINEAR, 0));
2668 if (!(chan = ast_request_and_dial(tech, tmp_cap, NULL, target, recall_timer, &reason, generic_pvt->cid_num, generic_pvt->cid_name))) {
2669 /* Hmm, no channel. Sucks for you, bud.
2671 ast_log_dynamic_level(cc_logger_level, "Core %d: Failed to call back %s for reason %d\n",
2672 agent->core_id, agent->device_name, reason);
2673 ast_cc_failed(agent->core_id, "Failed to call back device %s/%s", tech, target);
2674 ast_format_cap_destroy(tmp_cap);
2677 ast_format_cap_destroy(tmp_cap);
2679 /* We have a channel. It's time now to set up the datastore of recalled CC interfaces.
2680 * This will be a common task for all recall functions. If it were possible, I'd have
2681 * the core do it automatically, but alas I cannot. Instead, I will provide a public
2682 * function to do so.
2684 ast_setup_cc_recall_datastore(chan, agent->core_id);
2685 ast_cc_agent_set_interfaces_chanvar(chan);
2687 ast_channel_exten_set(chan, generic_pvt->exten);
2688 ast_channel_context_set(chan, generic_pvt->context);
2689 ast_channel_priority_set(chan, 1);
2691 pbx_builtin_setvar_helper(chan, "CC_EXTEN", generic_pvt->exten);
2692 pbx_builtin_setvar_helper(chan, "CC_CONTEXT", generic_pvt->context);
2694 if (!ast_strlen_zero(callback_macro)) {
2695 ast_log_dynamic_level(cc_logger_level, "Core %d: There's a callback macro configured for agent %s\n",
2696 agent->core_id, agent->device_name);
2697 if (ast_app_run_macro(NULL, chan, callback_macro, NULL)) {
2698 ast_cc_failed(agent->core_id, "Callback macro to %s failed. Maybe a hangup?", agent->device_name);
2703 ast_cc_agent_recalling(agent->core_id, "Generic agent %s is recalling", agent->device_name);
2704 ast_pbx_start(chan);
2708 static int cc_generic_agent_recall(struct ast_cc_agent *agent)
2711 enum ast_device_state current_state = ast_device_state(agent->device_name);
2713 if (!cc_generic_is_device_available(current_state)) {
2714 /* We can't try to contact the device right now because he's not available
2715 * Let the core know he's busy.
2717 ast_cc_agent_caller_busy(agent->core_id, "Generic agent caller %s is busy", agent->device_name);
2720 ast_pthread_create_detached_background(&clotho, NULL, generic_recall, agent);
2724 static void cc_generic_agent_destructor(struct ast_cc_agent *agent)
2726 struct cc_generic_agent_pvt *agent_pvt = agent->private_data;
2729 /* The agent constructor probably failed. */
2733 cc_generic_agent_stop_offer_timer(agent);
2734 if (agent_pvt->sub) {
2735 agent_pvt->sub = ast_event_unsubscribe(agent_pvt->sub);
2738 ast_free(agent_pvt);
2741 static void cc_core_instance_destructor(void *data)
2743 struct cc_core_instance *core_instance = data;
2744 ast_log_dynamic_level(cc_logger_level, "Core %d: Destroying core instance\n", core_instance->core_id);
2745 if (core_instance->agent) {
2746 cc_unref(core_instance->agent, "Core instance is done with the agent now");
2748 if (core_instance->monitors) {
2749 core_instance->monitors = cc_unref(core_instance->monitors, "Core instance is done with interface list");
2753 static struct cc_core_instance *cc_core_init_instance(struct ast_channel *caller_chan,
2754 struct cc_monitor_tree *called_tree, const int core_id, struct cc_control_payload *cc_data)
2756 char caller[AST_CHANNEL_NAME];
2757 struct cc_core_instance *core_instance;
2758 struct ast_cc_config_params *cc_params;
2762 ast_channel_get_device_name(caller_chan, caller, sizeof(caller));
2763 cc_params = ast_channel_get_cc_config_params(caller_chan);
2765 ast_log_dynamic_level(cc_logger_level, "Could not get CC parameters for %s\n",
2769 /* First, we need to kill off other pending CC offers from caller. If the caller is going
2770 * to request a CC service, it may only be for the latest call he made.
2772 if (ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_GENERIC) {
2773 kill_duplicate_offers(caller);
2776 ast_cc_is_recall(caller_chan, &recall_core_id, NULL);
2777 agent_count = count_agents(caller, recall_core_id);
2778 if (agent_count >= ast_get_cc_max_agents(cc_params)) {
2779 ast_log_dynamic_level(cc_logger_level, "Caller %s already has the maximum number of agents configured\n", caller);
2783 /* Generic agents can only have a single outstanding CC request per caller. */
2784 if (agent_count > 0 && ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_GENERIC) {
2785 ast_log_dynamic_level(cc_logger_level, "Generic agents can only have a single outstanding request\n");
2789 /* Next, we need to create the core instance for this call */
2790 if (!(core_instance = ao2_t_alloc(sizeof(*core_instance), cc_core_instance_destructor, "Creating core instance for CC"))) {
2794 core_instance->core_id = core_id;
2795 if (!(core_instance->agent = cc_agent_init(caller_chan, caller, core_instance->core_id, called_tree))) {
2796 cc_unref(core_instance, "Couldn't allocate agent, unref core_instance");
2800 core_instance->monitors = cc_ref(called_tree, "Core instance getting ref to monitor tree");
2802 ao2_t_link(cc_core_instances, core_instance, "Link core instance into container");
2804 return core_instance;
2807 struct cc_state_change_args {
2808 struct cc_core_instance *core_instance;/*!< Holds reference to core instance. */
2809 enum cc_state state;
2814 static int is_state_change_valid(enum cc_state current_state, const enum cc_state new_state, struct ast_cc_agent *agent)
2817 switch (new_state) {
2819 ast_log_dynamic_level(cc_logger_level, "Core %d: Asked to change to state %d? That should never happen.\n",
2820 agent->core_id, new_state);
2822 case CC_CALLER_OFFERED:
2823 if (current_state == CC_AVAILABLE) {
2827 case CC_CALLER_REQUESTED:
2828 if (current_state == CC_CALLER_OFFERED ||
2829 (current_state == CC_AVAILABLE && ast_test_flag(agent, AST_CC_AGENT_SKIP_OFFER))) {
2834 if (current_state == CC_CALLER_REQUESTED || current_state == CC_CALLER_BUSY) {
2838 case CC_CALLEE_READY:
2839 if (current_state == CC_ACTIVE) {
2843 case CC_CALLER_BUSY:
2844 if (current_state == CC_CALLEE_READY) {
2849 if (current_state == CC_CALLEE_READY) {
2854 if (current_state == CC_RECALLING) {
2862 ast_log_dynamic_level(cc_logger_level, "Core %d: Asked to change to unknown state %d\n",
2863 agent->core_id, new_state);
2870 static int cc_available(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
2872 /* This should never happen... */
2873 ast_log(LOG_WARNING, "Someone requested to change to CC_AVAILABLE? Ignoring.\n");
2877 static int cc_caller_offered(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
2879 if (core_instance->agent->callbacks->start_offer_timer(core_instance->agent)) {
2880 ast_cc_failed(core_instance->core_id, "Failed to start the offer timer for %s\n",
2881 core_instance->agent->device_name);
2884 manager_event(EVENT_FLAG_CC, "CCOfferTimerStart",
2888 core_instance->core_id, core_instance->agent->device_name, core_instance->agent->cc_params->cc_offer_timer);
2889 ast_log_dynamic_level(cc_logger_level, "Core %d: Started the offer timer for the agent %s!\n",
2890 core_instance->core_id, core_instance->agent->device_name);
2895 * \brief check if the core instance has any device monitors
2897 * In any case where we end up removing a device monitor from the
2898 * list of device monitors, it is important to see what the state
2899 * of the list is afterwards. If we find that we only have extension
2900 * monitors left, then no devices are actually being monitored.
2901 * In such a case, we need to declare that CC has failed for this
2902 * call. This function helps those cases to determine if they should
2905 * \param core_instance The core instance we are checking for the existence
2906 * of device monitors
2907 * \retval 0 No device monitors exist on this core_instance
2908 * \retval 1 There is still at least 1 device monitor remaining
2910 static int has_device_monitors(struct cc_core_instance *core_instance)
2912 struct ast_cc_monitor *iter;
2915 AST_LIST_TRAVERSE(core_instance->monitors, iter, next) {
2916 if (iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
2925 static void request_cc(struct cc_core_instance *core_instance)
2927 struct ast_cc_monitor *monitor_iter;
2928 AST_LIST_LOCK(core_instance->monitors);
2929 AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
2930 if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
2931 if (monitor_iter->callbacks->request_cc(monitor_iter, &monitor_iter->available_timer_id)) {
2932 AST_LIST_REMOVE_CURRENT(next);
2933 cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
2934 monitor_iter->interface->device_name, 1);
2935 cc_unref(monitor_iter, "request_cc failed. Unref list's reference to monitor");
2937 manager_event(EVENT_FLAG_CC, "CCRequested",
2941 core_instance->core_id, core_instance->agent->device_name, monitor_iter->interface->device_name);
2945 AST_LIST_TRAVERSE_SAFE_END;
2947 if (!has_device_monitors(core_instance)) {
2948 ast_cc_failed(core_instance->core_id, "All device monitors failed to request CC");
2950 AST_LIST_UNLOCK(core_instance->monitors);
2953 static int cc_caller_requested(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
2955 if (!ast_cc_request_is_within_limits()) {
2956 ast_log(LOG_WARNING, "Cannot request CC since there is no more room for requests\n");
2957 core_instance->agent->callbacks->respond(core_instance->agent,
2958 AST_CC_AGENT_RESPONSE_FAILURE_TOO_MANY);
2959 ast_cc_failed(core_instance->core_id, "Too many requests in the system");
2962 core_instance->agent->callbacks->stop_offer_timer(core_instance->agent);
2963 request_cc(core_instance);
2967 static void unsuspend(struct cc_core_instance *core_instance)
2969 struct ast_cc_monitor *monitor_iter;
2970 AST_LIST_LOCK(core_instance->monitors);
2971 AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
2972 if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
2973 if (monitor_iter->callbacks->unsuspend(monitor_iter)) {
2974 AST_LIST_REMOVE_CURRENT(next);
2975 cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
2976 monitor_iter->interface->device_name, 1);
2977 cc_unref(monitor_iter, "unsuspend failed. Unref list's reference to monitor");
2981 AST_LIST_TRAVERSE_SAFE_END;
2983 if (!has_device_monitors(core_instance)) {
2984 ast_cc_failed(core_instance->core_id, "All device monitors failed to unsuspend CC");
2986 AST_LIST_UNLOCK(core_instance->monitors);
2989 static int cc_active(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
2992 * 1. Callee accepted CC request, call agent's ack callback.
2993 * 2. Caller became available, call agent's stop_monitoring callback and
2994 * call monitor's unsuspend callback.
2996 if (previous_state == CC_CALLER_REQUESTED) {
2997 core_instance->agent->callbacks->respond(core_instance->agent,
2998 AST_CC_AGENT_RESPONSE_SUCCESS);
2999 manager_event(EVENT_FLAG_CC, "CCRequestAcknowledged",
3002 core_instance->core_id, core_instance->agent->device_name);
3003 } else if (previous_state == CC_CALLER_BUSY) {
3004 manager_event(EVENT_FLAG_CC, "CCCallerStopMonitoring",
3007 core_instance->core_id, core_instance->agent->device_name);
3008 unsuspend(core_instance);
3010 /* Not possible for previous_state to be anything else due to the is_state_change_valid check at the beginning */
3014 static int cc_callee_ready(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
3016 core_instance->agent->callbacks->callee_available(core_instance->agent);
3020 static void suspend(struct cc_core_instance *core_instance)
3022 struct ast_cc_monitor *monitor_iter;
3023 AST_LIST_LOCK(core_instance->monitors);
3024 AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
3025 if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
3026 if (monitor_iter->callbacks->suspend(monitor_iter)) {
3027 AST_LIST_REMOVE_CURRENT(next);
3028 cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
3029 monitor_iter->interface->device_name, 1);
3030 cc_unref(monitor_iter, "suspend failed. Unref list's reference to monitor");
3034 AST_LIST_TRAVERSE_SAFE_END;
3036 if (!has_device_monitors(core_instance)) {
3037 ast_cc_failed(core_instance->core_id, "All device monitors failed to suspend CC");
3039 AST_LIST_UNLOCK(core_instance->monitors);
3042 static int cc_caller_busy(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
3044 /* Callee was available, but caller was busy, call agent's begin_monitoring callback
3045 * and call monitor's suspend callback.
3047 suspend(core_instance);
3048 core_instance->agent->callbacks->start_monitoring(core_instance->agent);
3049 manager_event(EVENT_FLAG_CC, "CCCallerStartMonitoring",
3052 core_instance->core_id, core_instance->agent->device_name);
3056 static void cancel_available_timer(struct cc_core_instance *core_instance)
3058 struct ast_cc_monitor *monitor_iter;
3059 AST_LIST_LOCK(core_instance->monitors);
3060 AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
3061 if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
3062 if (monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id)) {
3063 AST_LIST_REMOVE_CURRENT(next);
3064 cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
3065 monitor_iter->interface->device_name, 1);
3066 cc_unref(monitor_iter, "cancel_available_timer failed. Unref list's reference to monitor");
3070 AST_LIST_TRAVERSE_SAFE_END;
3072 if (!has_device_monitors(core_instance)) {
3073 ast_cc_failed(core_instance->core_id, "All device monitors failed to cancel their available timers");
3075 AST_LIST_UNLOCK(core_instance->monitors);
3078 static int cc_recalling(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
3080 /* Both caller and callee are available, call agent's recall callback
3082 cancel_available_timer(core_instance);
3083 manager_event(EVENT_FLAG_CC, "CCCallerRecalling",
3086 core_instance->core_id, core_instance->agent->device_name);
3090 static int cc_complete(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
3092 /* Recall has made progress, call agent and monitor destructor functions
3094 manager_event(EVENT_FLAG_CC, "CCRecallComplete",
3097 core_instance->core_id, core_instance->agent->device_name);
3098 ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC recall has completed");
3102 static int cc_failed(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
3104 manager_event(EVENT_FLAG_CC, "CCFailure",
3108 core_instance->core_id, core_instance->agent->device_name, args->debug);
3109 ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC failed");
3113 static int (* const state_change_funcs [])(struct cc_core_instance *, struct cc_state_change_args *, enum cc_state previous_state) = {
3114 [CC_AVAILABLE] = cc_available,
3115 [CC_CALLER_OFFERED] = cc_caller_offered,
3116 [CC_CALLER_REQUESTED] = cc_caller_requested,
3117 [CC_ACTIVE] = cc_active,
3118 [CC_CALLEE_READY] = cc_callee_ready,
3119 [CC_CALLER_BUSY] = cc_caller_busy,
3120 [CC_RECALLING] = cc_recalling,
3121 [CC_COMPLETE] = cc_complete,
3122 [CC_FAILED] = cc_failed,
3125 static int cc_do_state_change(void *datap)
3127 struct cc_state_change_args *args = datap;
3128 struct cc_core_instance *core_instance;
3129 enum cc_state previous_state;
3132 ast_log_dynamic_level(cc_logger_level, "Core %d: State change to %d requested. Reason: %s\n",
3133 args->core_id, args->state, args->debug);
3135 core_instance = args->core_instance;
3137 if (!is_state_change_valid(core_instance->current_state, args->state, core_instance->agent)) {
3138 ast_log_dynamic_level(cc_logger_level, "Core %d: Invalid state change requested. Cannot go from %s to %s\n",
3139 args->core_id, cc_state_to_string(core_instance->current_state), cc_state_to_string(args->state));
3140 if (args->state == CC_CALLER_REQUESTED) {
3142 * For out-of-order requests, we need to let the requester know that
3143 * we can't handle the request now.
3145 core_instance->agent->callbacks->respond(core_instance->agent,
3146 AST_CC_AGENT_RESPONSE_FAILURE_INVALID);
3149 cc_unref(core_instance, "Unref core instance from when it was found earlier");
3153 /* We can change to the new state now. */
3154 previous_state = core_instance->current_state;
3155 core_instance->current_state = args->state;
3156 res = state_change_funcs[core_instance->current_state](core_instance, args, previous_state);
3158 /* If state change successful then notify any device state watchers of the change */
3159 if (!res && !strcmp(core_instance->agent->callbacks->type, "generic")) {
3160 ccss_notify_device_state_change(core_instance->agent->device_name, core_instance->current_state);
3164 cc_unref(core_instance, "Unref since state change has completed"); /* From ao2_find */
3168 static int cc_request_state_change(enum cc_state state, const int core_id, const char *debug, va_list ap)
3174 struct cc_core_instance *core_instance;
3175 struct cc_state_change_args *args;
3176 /* This initial call to vsnprintf is simply to find what the
3177 * size of the string needs to be
3180 /* We add 1 to the result since vsnprintf's return does not
3181 * include the terminating null byte
3183 debuglen = vsnprintf(dummy, sizeof(dummy), debug, aq) + 1;
3186 if (!(args = ast_calloc(1, sizeof(*args) + debuglen))) {
3190 core_instance = find_cc_core_instance(core_id);
3191 if (!core_instance) {
3192 ast_log_dynamic_level(cc_logger_level, "Core %d: Unable to find core instance.\n",
3198 args->core_instance = core_instance;
3199 args->state = state;
3200 args->core_id = core_id;
3201 vsnprintf(args->debug, debuglen, debug, ap);
3203 res = ast_taskprocessor_push(cc_core_taskprocessor, cc_do_state_change, args);
3205 cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
3211 struct cc_recall_ds_data {
3215 struct cc_monitor_tree *interface_tree;
3218 static void *cc_recall_ds_duplicate(void *data)
3220 struct cc_recall_ds_data *old_data = data;
3221 struct cc_recall_ds_data *new_data = ast_calloc(1, sizeof(*new_data));
3226 new_data->interface_tree = cc_ref(old_data->interface_tree, "Bump refcount of monitor tree for recall datastore duplicate");
3227 new_data->core_id = old_data->core_id;
3228 new_data->nested = 1;
3232 static void cc_recall_ds_destroy(void *data)
3234 struct cc_recall_ds_data *recall_data = data;
3235 recall_data->interface_tree = cc_unref(recall_data->interface_tree, "Unref recall monitor tree");
3236 ast_free(recall_data);
3239 static struct ast_datastore_info recall_ds_info = {
3240 .type = "cc_recall",
3241 .duplicate = cc_recall_ds_duplicate,
3242 .destroy = cc_recall_ds_destroy,
3245 int ast_setup_cc_recall_datastore(struct ast_channel *chan, const int core_id)
3247 struct ast_datastore *recall_datastore = ast_datastore_alloc(&recall_ds_info, NULL);
3248 struct cc_recall_ds_data *recall_data;
3249 struct cc_core_instance *core_instance;
3251 if (!recall_datastore) {
3255 if (!(recall_data = ast_calloc(1, sizeof(*recall_data)))) {
3256 ast_datastore_free(recall_datastore);
3260 if (!(core_instance = find_cc_core_instance(core_id))) {
3261 ast_free(recall_data);
3262 ast_datastore_free(recall_datastore);
3266 recall_data->interface_tree = cc_ref(core_instance->monitors,
3267 "Bump refcount for monitor tree for recall datastore");
3268 recall_data->core_id = core_id;
3269 recall_datastore->data = recall_data;
3270 recall_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
3271 ast_channel_lock(chan);
3272 ast_channel_datastore_add(chan, recall_datastore);
3273 ast_channel_unlock(chan);
3274 cc_unref(core_instance, "Recall datastore set up. No need for core_instance ref");
3278 int ast_cc_is_recall(struct ast_channel *chan, int *core_id, const char * const monitor_type)
3280 struct ast_datastore *recall_datastore;
3281 struct cc_recall_ds_data *recall_data;
3282 struct cc_monitor_tree *interface_tree;
3283 char device_name[AST_CHANNEL_NAME];
3284 struct ast_cc_monitor *device_monitor;
3285 int core_id_candidate;
3287 ast_assert(core_id != NULL);
3291 ast_channel_lock(chan);
3292 if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
3293 /* Obviously not a recall if the datastore isn't present */
3294 ast_channel_unlock(chan);
3298 recall_data = recall_datastore->data;
3300 if (recall_data->ignore) {
3301 /* Though this is a recall, the call to this particular interface is not part of the
3302 * recall either because this is a call forward or because this is not the first
3303 * invocation of Dial during this call
3305 ast_channel_unlock(chan);
3309 if (!recall_data->nested) {
3310 /* If the nested flag is not set, then this means that
3311 * the channel passed to this function is the caller making
3312 * the recall. This means that we shouldn't look through
3313 * the monitor tree for the channel because it shouldn't be
3314 * there. However, this is a recall though, so return true.
3316 *core_id = recall_data->core_id;
3317 ast_channel_unlock(chan);
3321 if (ast_strlen_zero(monitor_type)) {
3322 /* If someone passed a NULL or empty monitor type, then it is clear
3323 * the channel they passed in was an incoming channel, and so searching
3324 * the list of dialed interfaces is not going to be helpful. Just return
3325 * false immediately.
3327 ast_channel_unlock(chan);
3331 interface_tree = recall_data->interface_tree;
3332 ast_channel_get_device_name(chan, device_name, sizeof(device_name));
3333 /* We grab the value of the recall_data->core_id so that we
3334 * can unlock the channel before we start looking through the
3335 * interface list. That way we don't have to worry about a possible
3336 * clash between the channel lock and the monitor tree lock.
3338 core_id_candidate = recall_data->core_id;
3339 ast_channel_unlock(chan);
3342 * Now we need to find out if the channel device name
3343 * is in the list of interfaces in the called tree.
3345 AST_LIST_LOCK(interface_tree);
3346 AST_LIST_TRAVERSE(interface_tree, device_monitor, next) {
3347 if (!strcmp(device_monitor->interface->device_name, device_name) &&
3348 !strcmp(device_monitor->interface->monitor_type, monitor_type)) {
3349 /* BOOM! Device is in the tree! We have a winner! */
3350 *core_id = core_id_candidate;
3351 AST_LIST_UNLOCK(interface_tree);
3355 AST_LIST_UNLOCK(interface_tree);
3359 struct ast_cc_monitor *ast_cc_get_monitor_by_recall_core_id(const int core_id, const char * const device_name)
3361 struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
3362 struct ast_cc_monitor *monitor_iter;
3364 if (!core_instance) {
3368 AST_LIST_LOCK(core_instance->monitors);
3369 AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {