Skip to content
Snippets Groups Projects
ccss.c 153 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	ao2_t_link(cc_core_instances, core_instance, "Link core instance into container");
    
    	return core_instance;
    }
    
    struct cc_state_change_args {
    
    	struct cc_core_instance *core_instance;/*!< Holds reference to core instance. */
    
    	enum cc_state state;
    	int core_id;
    	char debug[1];
    };
    
    static int is_state_change_valid(enum cc_state current_state, const enum cc_state new_state, struct ast_cc_agent *agent)
    {
    	int is_valid = 0;
    	switch (new_state) {
    	case CC_AVAILABLE:
    
    		ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to state %u? That should never happen.\n",
    
    				agent->core_id, new_state);
    		break;
    	case CC_CALLER_OFFERED:
    		if (current_state == CC_AVAILABLE) {
    			is_valid = 1;
    		}
    		break;
    	case CC_CALLER_REQUESTED:
    		if (current_state == CC_CALLER_OFFERED ||
    				(current_state == CC_AVAILABLE && ast_test_flag(agent, AST_CC_AGENT_SKIP_OFFER))) {
    			is_valid = 1;
    		}
    		break;
    	case CC_ACTIVE:
    		if (current_state == CC_CALLER_REQUESTED || current_state == CC_CALLER_BUSY) {
    			is_valid = 1;
    		}
    		break;
    	case CC_CALLEE_READY:
    		if (current_state == CC_ACTIVE) {
    			is_valid = 1;
    		}
    		break;
    	case CC_CALLER_BUSY:
    		if (current_state == CC_CALLEE_READY) {
    			is_valid = 1;
    		}
    		break;
    	case CC_RECALLING:
    		if (current_state == CC_CALLEE_READY) {
    			is_valid = 1;
    		}
    		break;
    	case CC_COMPLETE:
    		if (current_state == CC_RECALLING) {
    			is_valid = 1;
    		}
    		break;
    	case CC_FAILED:
    		is_valid = 1;
    		break;
    	default:
    
    		ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to unknown state %u\n",
    
    				agent->core_id, new_state);
    		break;
    	}
    
    	return is_valid;
    }
    
    static int cc_available(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
    {
    	/* This should never happen... */
    	ast_log(LOG_WARNING, "Someone requested to change to CC_AVAILABLE? Ignoring.\n");
    	return -1;
    }
    
    static int cc_caller_offered(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
    {
    	if (core_instance->agent->callbacks->start_offer_timer(core_instance->agent)) {
    		ast_cc_failed(core_instance->core_id, "Failed to start the offer timer for %s\n",
    				core_instance->agent->device_name);
    		return -1;
    	}
    
    	cc_publish_offertimerstart(core_instance->core_id, core_instance->agent->device_name, core_instance->agent->cc_params->cc_offer_timer);
    
    	ast_log_dynamic_level(cc_logger_level, "Core %d: Started the offer timer for the agent %s!\n",
    			core_instance->core_id, core_instance->agent->device_name);
    	return 0;
    }
    
    /*!
     * \brief check if the core instance has any device monitors
     *
     * In any case where we end up removing a device monitor from the
     * list of device monitors, it is important to see what the state
     * of the list is afterwards. If we find that we only have extension
     * monitors left, then no devices are actually being monitored.
     * In such a case, we need to declare that CC has failed for this
     * call. This function helps those cases to determine if they should
     * declare failure.
     *
     * \param core_instance The core instance we are checking for the existence
     * of device monitors
     * \retval 0 No device monitors exist on this core_instance
     * \retval 1 There is still at least 1 device monitor remaining
     */
    static int has_device_monitors(struct cc_core_instance *core_instance)
    {
    	struct ast_cc_monitor *iter;
    	int res = 0;
    
    	AST_LIST_TRAVERSE(core_instance->monitors, iter, next) {
    		if (iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
    			res = 1;
    			break;
    		}
    	}
    
    	return res;
    }
    
    static void request_cc(struct cc_core_instance *core_instance)
    {
    	struct ast_cc_monitor *monitor_iter;
    	AST_LIST_LOCK(core_instance->monitors);
    	AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
    		if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
    			if (monitor_iter->callbacks->request_cc(monitor_iter, &monitor_iter->available_timer_id)) {
    				AST_LIST_REMOVE_CURRENT(next);
    				cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
    						monitor_iter->interface->device_name, 1);
    				cc_unref(monitor_iter, "request_cc failed. Unref list's reference to monitor");
    			} else {
    
    				cc_publish_requested(core_instance->core_id, core_instance->agent->device_name, monitor_iter->interface->device_name);
    
    			}
    		}
    	}
    	AST_LIST_TRAVERSE_SAFE_END;
    
    	if (!has_device_monitors(core_instance)) {
    		ast_cc_failed(core_instance->core_id, "All device monitors failed to request CC");
    	}
    	AST_LIST_UNLOCK(core_instance->monitors);
    }
    
    static int cc_caller_requested(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
    {
    	if (!ast_cc_request_is_within_limits()) {
    		ast_log(LOG_WARNING, "Cannot request CC since there is no more room for requests\n");
    
    		core_instance->agent->callbacks->respond(core_instance->agent,
    			AST_CC_AGENT_RESPONSE_FAILURE_TOO_MANY);
    
    		ast_cc_failed(core_instance->core_id, "Too many requests in the system");
    		return -1;
    	}
    	core_instance->agent->callbacks->stop_offer_timer(core_instance->agent);
    	request_cc(core_instance);
    	return 0;
    }
    
    static void unsuspend(struct cc_core_instance *core_instance)
    {
    	struct ast_cc_monitor *monitor_iter;
    	AST_LIST_LOCK(core_instance->monitors);
    	AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
    		if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
    			if (monitor_iter->callbacks->unsuspend(monitor_iter)) {
    				AST_LIST_REMOVE_CURRENT(next);
    				cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
    						monitor_iter->interface->device_name, 1);
    				cc_unref(monitor_iter, "unsuspend failed. Unref list's reference to monitor");
    			}
    		}
    	}
    	AST_LIST_TRAVERSE_SAFE_END;
    
    	if (!has_device_monitors(core_instance)) {
    		ast_cc_failed(core_instance->core_id, "All device monitors failed to unsuspend CC");
    	}
    	AST_LIST_UNLOCK(core_instance->monitors);
    }
    
    static int cc_active(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
    {
    	/* Either
    	 * 1. Callee accepted CC request, call agent's ack callback.
    	 * 2. Caller became available, call agent's stop_monitoring callback and
    	 *    call monitor's unsuspend callback.
    	 */
    	if (previous_state == CC_CALLER_REQUESTED) {
    
    		core_instance->agent->callbacks->respond(core_instance->agent,
    			AST_CC_AGENT_RESPONSE_SUCCESS);
    
    		cc_publish_requestacknowledged(core_instance->core_id, core_instance->agent->device_name);
    
    	} else if (previous_state == CC_CALLER_BUSY) {
    
    		cc_publish_callerstopmonitoring(core_instance->core_id, core_instance->agent->device_name);
    
    		unsuspend(core_instance);
    	}
    	/* Not possible for previous_state to be anything else due to the is_state_change_valid check at the beginning */
    	return 0;
    }
    
    static int cc_callee_ready(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
    {
    	core_instance->agent->callbacks->callee_available(core_instance->agent);
    	return 0;
    }
    
    static void suspend(struct cc_core_instance *core_instance)
    {
    	struct ast_cc_monitor *monitor_iter;
    	AST_LIST_LOCK(core_instance->monitors);
    	AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
    		if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
    			if (monitor_iter->callbacks->suspend(monitor_iter)) {
    				AST_LIST_REMOVE_CURRENT(next);
    				cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
    						monitor_iter->interface->device_name, 1);
    				cc_unref(monitor_iter, "suspend failed. Unref list's reference to monitor");
    			}
    		}
    	}
    	AST_LIST_TRAVERSE_SAFE_END;
    
    	if (!has_device_monitors(core_instance)) {
    		ast_cc_failed(core_instance->core_id, "All device monitors failed to suspend CC");
    	}
    	AST_LIST_UNLOCK(core_instance->monitors);
    }
    
    static int cc_caller_busy(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
    {
    	/* Callee was available, but caller was busy, call agent's begin_monitoring callback
    	 * and call monitor's suspend callback.
    	 */
    	suspend(core_instance);
    	core_instance->agent->callbacks->start_monitoring(core_instance->agent);
    
    	cc_publish_callerstartmonitoring(core_instance->core_id, core_instance->agent->device_name);
    
    	return 0;
    }
    
    static void cancel_available_timer(struct cc_core_instance *core_instance)
    {
    	struct ast_cc_monitor *monitor_iter;
    	AST_LIST_LOCK(core_instance->monitors);
    	AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
    		if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
    			if (monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id)) {
    				AST_LIST_REMOVE_CURRENT(next);
    				cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
    						monitor_iter->interface->device_name, 1);
    				cc_unref(monitor_iter, "cancel_available_timer failed. Unref list's reference to monitor");
    			}
    		}
    	}
    	AST_LIST_TRAVERSE_SAFE_END;
    
    	if (!has_device_monitors(core_instance)) {
    		ast_cc_failed(core_instance->core_id, "All device monitors failed to cancel their available timers");
    	}
    	AST_LIST_UNLOCK(core_instance->monitors);
    }
    
    static int cc_recalling(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
    {
    	/* Both caller and callee are available, call agent's recall callback
    	 */
    	cancel_available_timer(core_instance);
    
    	cc_publish_callerrecalling(core_instance->core_id, core_instance->agent->device_name);
    
    	return 0;
    }
    
    static int cc_complete(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
    {
    	/* Recall has made progress, call agent and monitor destructor functions
    	 */
    
    	cc_publish_recallcomplete(core_instance->core_id, core_instance->agent->device_name);
    
    	ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC recall has completed");
    	return 0;
    }
    
    static int cc_failed(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
    {
    
    	cc_publish_failure(core_instance->core_id, core_instance->agent->device_name, args->debug);
    
    	ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC failed");
    	return 0;
    }
    
    static int (* const state_change_funcs [])(struct cc_core_instance *, struct cc_state_change_args *, enum cc_state previous_state) = {
    	[CC_AVAILABLE] = cc_available,
    	[CC_CALLER_OFFERED] = cc_caller_offered,
    	[CC_CALLER_REQUESTED] = cc_caller_requested,
    	[CC_ACTIVE] = cc_active,
    	[CC_CALLEE_READY] = cc_callee_ready,
    	[CC_CALLER_BUSY] = cc_caller_busy,
    	[CC_RECALLING] = cc_recalling,
    	[CC_COMPLETE] = cc_complete,
    	[CC_FAILED] = cc_failed,
    };
    
    static int cc_do_state_change(void *datap)
    {
    	struct cc_state_change_args *args = datap;
    	struct cc_core_instance *core_instance;
    	enum cc_state previous_state;
    	int res;
    
    
    	ast_log_dynamic_level(cc_logger_level, "Core %d: State change to %u requested. Reason: %s\n",
    
    			args->core_id, args->state, args->debug);
    
    
    	core_instance = args->core_instance;
    
    
    	if (!is_state_change_valid(core_instance->current_state, args->state, core_instance->agent)) {
    		ast_log_dynamic_level(cc_logger_level, "Core %d: Invalid state change requested. Cannot go from %s to %s\n",
    				args->core_id, cc_state_to_string(core_instance->current_state), cc_state_to_string(args->state));
    
    		if (args->state == CC_CALLER_REQUESTED) {
    			/*
    			 * For out-of-order requests, we need to let the requester know that
    			 * we can't handle the request now.
    			 */
    			core_instance->agent->callbacks->respond(core_instance->agent,
    				AST_CC_AGENT_RESPONSE_FAILURE_INVALID);
    		}
    
    		ast_free(args);
    		cc_unref(core_instance, "Unref core instance from when it was found earlier");
    		return -1;
    	}
    
    	/* We can change to the new state now. */
    	previous_state = core_instance->current_state;
    	core_instance->current_state = args->state;
    	res = state_change_funcs[core_instance->current_state](core_instance, args, previous_state);
    
    
    	/* If state change successful then notify any device state watchers of the change */
    	if (!res && !strcmp(core_instance->agent->callbacks->type, "generic")) {
    		ccss_notify_device_state_change(core_instance->agent->device_name, core_instance->current_state);
    	}
    
    
    	ast_free(args);
    	cc_unref(core_instance, "Unref since state change has completed"); /* From ao2_find */
    	return res;
    }
    
    static int cc_request_state_change(enum cc_state state, const int core_id, const char *debug, va_list ap)
    {
    	int res;
    	int debuglen;
    	char dummy[1];
    	va_list aq;
    
    	struct cc_core_instance *core_instance;
    
    	struct cc_state_change_args *args;
    	/* This initial call to vsnprintf is simply to find what the
    	 * size of the string needs to be
    	 */
    	va_copy(aq, ap);
    	/* We add 1 to the result since vsnprintf's return does not
    	 * include the terminating null byte
    	 */
    	debuglen = vsnprintf(dummy, sizeof(dummy), debug, aq) + 1;
    	va_end(aq);
    
    	if (!(args = ast_calloc(1, sizeof(*args) + debuglen))) {
    		return -1;
    	}
    
    
    	core_instance = find_cc_core_instance(core_id);
    	if (!core_instance) {
    		ast_log_dynamic_level(cc_logger_level, "Core %d: Unable to find core instance.\n",
    			core_id);
    		ast_free(args);
    		return -1;
    	}
    
    	args->core_instance = core_instance;
    
    	args->state = state;
    	args->core_id = core_id;
    	vsnprintf(args->debug, debuglen, debug, ap);
    
    	res = ast_taskprocessor_push(cc_core_taskprocessor, cc_do_state_change, args);
    	if (res) {
    
    		cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
    
    		ast_free(args);
    	}
    	return res;
    }
    
    struct cc_recall_ds_data {
    	int core_id;
    	char ignore;
    	char nested;
    	struct cc_monitor_tree *interface_tree;
    };
    
    static void *cc_recall_ds_duplicate(void *data)
    {
    	struct cc_recall_ds_data *old_data = data;
    	struct cc_recall_ds_data *new_data = ast_calloc(1, sizeof(*new_data));
    
    	if (!new_data) {
    		return NULL;
    	}
    	new_data->interface_tree = cc_ref(old_data->interface_tree, "Bump refcount of monitor tree for recall datastore duplicate");
    	new_data->core_id = old_data->core_id;
    	new_data->nested = 1;
    	return new_data;
    }
    
    static void cc_recall_ds_destroy(void *data)
    {
    	struct cc_recall_ds_data *recall_data = data;
    	recall_data->interface_tree = cc_unref(recall_data->interface_tree, "Unref recall monitor tree");
    	ast_free(recall_data);
    }
    
    
    static const struct ast_datastore_info recall_ds_info = {
    
    	.type = "cc_recall",
    	.duplicate = cc_recall_ds_duplicate,
    	.destroy = cc_recall_ds_destroy,
    };
    
    int ast_setup_cc_recall_datastore(struct ast_channel *chan, const int core_id)
    {
    	struct ast_datastore *recall_datastore = ast_datastore_alloc(&recall_ds_info, NULL);
    	struct cc_recall_ds_data *recall_data;
    	struct cc_core_instance *core_instance;
    
    	if (!recall_datastore) {
    		return -1;
    	}
    
    	if (!(recall_data = ast_calloc(1, sizeof(*recall_data)))) {
    		ast_datastore_free(recall_datastore);
    		return -1;
    	}
    
    	if (!(core_instance = find_cc_core_instance(core_id))) {
    		ast_free(recall_data);
    		ast_datastore_free(recall_datastore);
    		return -1;
    	}
    
    	recall_data->interface_tree = cc_ref(core_instance->monitors,
    			"Bump refcount for monitor tree for recall datastore");
    	recall_data->core_id = core_id;
    	recall_datastore->data = recall_data;
    	recall_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
    	ast_channel_lock(chan);
    	ast_channel_datastore_add(chan, recall_datastore);
    	ast_channel_unlock(chan);
    	cc_unref(core_instance, "Recall datastore set up. No need for core_instance ref");
    	return 0;
    }
    
    int ast_cc_is_recall(struct ast_channel *chan, int *core_id, const char * const monitor_type)
    {
    	struct ast_datastore *recall_datastore;
    	struct cc_recall_ds_data *recall_data;
    	struct cc_monitor_tree *interface_tree;
    	char device_name[AST_CHANNEL_NAME];
    	struct ast_cc_monitor *device_monitor;
    	int core_id_candidate;
    
    	ast_assert(core_id != NULL);
    
    	*core_id = -1;
    
    	ast_channel_lock(chan);
    	if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
    		/* Obviously not a recall if the datastore isn't present */
    		ast_channel_unlock(chan);
    		return 0;
    	}
    
    	recall_data = recall_datastore->data;
    
    	if (recall_data->ignore) {
    		/* Though this is a recall, the call to this particular interface is not part of the
    		 * recall either because this is a call forward or because this is not the first
    		 * invocation of Dial during this call
    		 */
    		ast_channel_unlock(chan);
    		return 0;
    	}
    
    	if (!recall_data->nested) {
    		/* If the nested flag is not set, then this means that
    		 * the channel passed to this function is the caller making
    		 * the recall. This means that we shouldn't look through
    		 * the monitor tree for the channel because it shouldn't be
    		 * there. However, this is a recall though, so return true.
    		 */
    		*core_id = recall_data->core_id;
    		ast_channel_unlock(chan);
    		return 1;
    	}
    
    	if (ast_strlen_zero(monitor_type)) {
    		/* If someone passed a NULL or empty monitor type, then it is clear
    		 * the channel they passed in was an incoming channel, and so searching
    		 * the list of dialed interfaces is not going to be helpful. Just return
    		 * false immediately.
    		 */
    		ast_channel_unlock(chan);
    		return 0;
    	}
    
    	interface_tree = recall_data->interface_tree;
    	ast_channel_get_device_name(chan, device_name, sizeof(device_name));
    	/* We grab the value of the recall_data->core_id so that we
    	 * can unlock the channel before we start looking through the
    	 * interface list. That way we don't have to worry about a possible
    	 * clash between the channel lock and the monitor tree lock.
    	 */
    	core_id_candidate = recall_data->core_id;
    	ast_channel_unlock(chan);
    
    	/*
    	 * Now we need to find out if the channel device name
    	 * is in the list of interfaces in the called tree.
    	 */
    	AST_LIST_LOCK(interface_tree);
    	AST_LIST_TRAVERSE(interface_tree, device_monitor, next) {
    		if (!strcmp(device_monitor->interface->device_name, device_name) &&
    				!strcmp(device_monitor->interface->monitor_type, monitor_type)) {
    			/* BOOM! Device is in the tree! We have a winner! */
    			*core_id = core_id_candidate;
    			AST_LIST_UNLOCK(interface_tree);
    			return 1;
    		}
    	}
    	AST_LIST_UNLOCK(interface_tree);
    	return 0;
    }
    
    struct ast_cc_monitor *ast_cc_get_monitor_by_recall_core_id(const int core_id, const char * const device_name)
    {
    	struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
    	struct ast_cc_monitor *monitor_iter;
    
    	if (!core_instance) {
    		return NULL;
    	}
    
    	AST_LIST_LOCK(core_instance->monitors);
    	AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
    		if (!strcmp(monitor_iter->interface->device_name, device_name)) {
    			/* Found a monitor. */
    			cc_ref(monitor_iter, "Hand the requester of the monitor a reference");
    			break;
    		}
    	}
    	AST_LIST_UNLOCK(core_instance->monitors);
    	cc_unref(core_instance, "Done with core instance ref in ast_cc_get_monitor_by_recall_core_id");
    	return monitor_iter;
    }
    
    /*!
     * \internal
     * \brief uniquely append a dialstring to our CC_INTERFACES chanvar string.
     *
     * We will only append a string if it has not already appeared in our channel
     * variable earlier. We ensure that we don't erroneously match substrings by
     * adding an ampersand to the end of our potential dialstring and searching for
     * it plus the ampersand in our variable.
     *
     * It's important to note that once we have built the full CC_INTERFACES string,
     * there will be an extra ampersand at the end which must be stripped off by
     * the caller of this function.
     *
     * \param str An ast_str holding what we will add to CC_INTERFACES
     * \param dialstring A new dialstring to add
     * \retval void
     */
    
    static void cc_unique_append(struct ast_str **str, const char *dialstring)
    
    	char dialstring_search[AST_CHANNEL_NAME + 1];
    
    	if (ast_strlen_zero(dialstring)) {
    		/* No dialstring to append. */
    		return;
    	}
    
    	snprintf(dialstring_search, sizeof(dialstring_search), "%s%c", dialstring, '&');
    
    	if (strstr(ast_str_buffer(*str), dialstring_search)) {
    
    	ast_str_append(str, 0, "%s", dialstring_search);
    
    }
    
    /*!
     * \internal
     * \brief Build the CC_INTERFACES channel variable
     *
     * The method used is to traverse the child dialstrings in the
     * passed-in extension monitor, adding any that have the is_valid
     * flag set. Then, traverse the monitors, finding all children
     * of the starting extension monitor and adding their dialstrings
     * as well.
     *
     * \param starting_point The extension monitor that is the parent to all
     * monitors whose dialstrings should be added to CC_INTERFACES
     * \param str Where we will store CC_INTERFACES
     * \retval void
     */
    
    static void build_cc_interfaces_chanvar(struct ast_cc_monitor *starting_point, struct ast_str **str)
    
    {
    	struct extension_monitor_pvt *extension_pvt;
    	struct extension_child_dialstring *child_dialstring;
    	struct ast_cc_monitor *monitor_iter = starting_point;
    	int top_level_id = starting_point->id;
    
    	size_t length;
    
    	/* Init to an empty string. */
    
    	ast_str_truncate(*str, 0);
    
    
    	/* First we need to take all of the is_valid child_dialstrings from
    	 * the extension monitor we found and add them to the CC_INTERFACES
    	 * chanvar
    	 */
    	extension_pvt = starting_point->private_data;
    	AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) {
    		if (child_dialstring->is_valid) {
    			cc_unique_append(str, child_dialstring->original_dialstring);
    		}
    	}
    
    	/* And now we get the dialstrings from each of the device monitors */
    	while ((monitor_iter = AST_LIST_NEXT(monitor_iter, next))) {
    		if (monitor_iter->parent_id == top_level_id) {
    			cc_unique_append(str, monitor_iter->dialstring);
    		}
    	}
    
    	/* str will have an extra '&' tacked onto the end of it, so we need
    	 * to get rid of that.
    	 */
    
    	length = ast_str_strlen(*str);
    
    		ast_str_truncate(*str, length - 1);
    
    	}
    	if (length <= 1) {
    		/* Nothing to recall?  This should not happen. */
    		ast_log(LOG_ERROR, "CC_INTERFACES is empty. starting device_name:'%s'\n",
    			starting_point->interface->device_name);
    	}
    
    }
    
    int ast_cc_agent_set_interfaces_chanvar(struct ast_channel *chan)
    {
    	struct ast_datastore *recall_datastore;
    	struct cc_monitor_tree *interface_tree;
    	struct ast_cc_monitor *monitor;
    	struct cc_recall_ds_data *recall_data;
    	struct ast_str *str = ast_str_create(64);
    	int core_id;
    
    	if (!str) {
    		return -1;
    	}
    
    	ast_channel_lock(chan);
    	if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
    		ast_channel_unlock(chan);
    		ast_free(str);
    		return -1;
    	}
    	recall_data = recall_datastore->data;
    	interface_tree = recall_data->interface_tree;
    	core_id = recall_data->core_id;
    	ast_channel_unlock(chan);
    
    	AST_LIST_LOCK(interface_tree);
    	monitor = AST_LIST_FIRST(interface_tree);
    
    	build_cc_interfaces_chanvar(monitor, &str);
    
    	AST_LIST_UNLOCK(interface_tree);
    
    	pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str));
    	ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n",
    			core_id, ast_str_buffer(str));
    
    	ast_free(str);
    	return 0;
    }
    
    int ast_set_cc_interfaces_chanvar(struct ast_channel *chan, const char * const extension)
    {
    	struct ast_datastore *recall_datastore;
    	struct cc_monitor_tree *interface_tree;
    	struct ast_cc_monitor *monitor_iter;
    	struct cc_recall_ds_data *recall_data;
    	struct ast_str *str = ast_str_create(64);
    	int core_id;
    
    	if (!str) {
    		return -1;
    	}
    
    	ast_channel_lock(chan);
    	if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
    		ast_channel_unlock(chan);
    		ast_free(str);
    		return -1;
    	}
    	recall_data = recall_datastore->data;
    	interface_tree = recall_data->interface_tree;
    	core_id = recall_data->core_id;
    	ast_channel_unlock(chan);
    
    	AST_LIST_LOCK(interface_tree);
    	AST_LIST_TRAVERSE(interface_tree, monitor_iter, next) {
    		if (!strcmp(monitor_iter->interface->device_name, extension)) {
    			break;
    		}
    	}
    
    	if (!monitor_iter) {
    		/* We couldn't find this extension. This may be because
    		 * we have been directed into an unexpected extension because
    		 * the admin has changed a CC_INTERFACES variable at some point.
    		 */
    		AST_LIST_UNLOCK(interface_tree);
    		ast_free(str);
    		return -1;
    	}
    
    
    	build_cc_interfaces_chanvar(monitor_iter, &str);
    
    	AST_LIST_UNLOCK(interface_tree);
    
    	pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str));
    	ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n",
    			core_id, ast_str_buffer(str));
    
    	ast_free(str);
    	return 0;
    }
    
    void ast_ignore_cc(struct ast_channel *chan)
    {
    	struct ast_datastore *cc_datastore;
    	struct ast_datastore *cc_recall_datastore;
    	struct dialed_cc_interfaces *cc_interfaces;
    	struct cc_recall_ds_data *recall_cc_data;
    
    	ast_channel_lock(chan);
    	if ((cc_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
    		cc_interfaces = cc_datastore->data;
    		cc_interfaces->ignore = 1;
    	}
    
    	if ((cc_recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
    		recall_cc_data = cc_recall_datastore->data;
    		recall_cc_data->ignore = 1;
    	}
    	ast_channel_unlock(chan);
    }
    
    static __attribute__((format(printf, 2, 3))) int cc_offer(const int core_id, const char * const debug, ...)
    {
    	va_list ap;
    	int res;
    
    	va_start(ap, debug);
    	res = cc_request_state_change(CC_CALLER_OFFERED, core_id, debug, ap);
    	va_end(ap);
    	return res;
    }
    
    int ast_cc_offer(struct ast_channel *caller_chan)
    {
    	int core_id;
    	int res = -1;
    	struct ast_datastore *datastore;
    	struct dialed_cc_interfaces *cc_interfaces;
    	char cc_is_offerable;
    
    	ast_channel_lock(caller_chan);
    	if (!(datastore = ast_channel_datastore_find(caller_chan, &dialed_cc_interfaces_info, NULL))) {
    		ast_channel_unlock(caller_chan);
    		return res;
    	}
    
    	cc_interfaces = datastore->data;
    	cc_is_offerable = cc_interfaces->is_original_caller;
    	core_id = cc_interfaces->core_id;
    	ast_channel_unlock(caller_chan);
    
    	if (cc_is_offerable) {
    
    		res = cc_offer(core_id, "CC offered to caller %s", ast_channel_name(caller_chan));
    
    	}
    	return res;
    }
    
    int ast_cc_agent_accept_request(int core_id, const char * const debug, ...)
    {
    	va_list ap;
    	int res;
    
    	va_start(ap, debug);
    	res = cc_request_state_change(CC_CALLER_REQUESTED, core_id, debug, ap);
    	va_end(ap);
    	return res;
    }
    
    int ast_cc_monitor_request_acked(int core_id, const char * const debug, ...)
    {
    	va_list ap;
    	int res;
    
    	va_start(ap, debug);
    	res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap);
    	va_end(ap);
    	return res;
    }
    
    int ast_cc_monitor_callee_available(const int core_id, const char * const debug, ...)
    {
    	va_list ap;
    	int res;
    
    	va_start(ap, debug);
    	res = cc_request_state_change(CC_CALLEE_READY, core_id, debug, ap);
    	va_end(ap);
    	return res;
    }
    
    int ast_cc_agent_caller_busy(int core_id, const char * debug, ...)
    {
    	va_list ap;
    	int res;
    
    	va_start(ap, debug);
    	res = cc_request_state_change(CC_CALLER_BUSY, core_id, debug, ap);
    	va_end(ap);
    	return res;
    }
    
    int ast_cc_agent_caller_available(int core_id, const char * const debug, ...)
    {
    	va_list ap;
    	int res;
    
    	va_start(ap, debug);
    	res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap);
    	va_end(ap);
    	return res;
    }
    
    int ast_cc_agent_recalling(int core_id, const char * const debug, ...)
    {
    	va_list ap;
    	int res;
    
    	va_start(ap, debug);
    	res = cc_request_state_change(CC_RECALLING, core_id, debug, ap);
    	va_end(ap);
    	return res;
    }
    
    int ast_cc_completed(struct ast_channel *chan, const char * const debug, ...)
    {
    	struct ast_datastore *recall_datastore;
    	struct cc_recall_ds_data *recall_data;
    	int core_id;
    	va_list ap;
    	int res;
    
    	ast_channel_lock(chan);
    	if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
    		/* Silly! Why did you call this function if there's no recall DS? */
    		ast_channel_unlock(chan);
    		return -1;
    	}
    	recall_data = recall_datastore->data;
    	if (recall_data->nested || recall_data->ignore) {
    		/* If this is being called from a nested Dial, it is too
    		 * early to determine if the recall has actually completed.
    		 * The outermost dial is the only one with the authority to
    		 * declare the recall to be complete.
    		 *
    		 * Similarly, if this function has been called when the
    		 * recall has progressed beyond the first dial, this is not
    		 * a legitimate time to declare the recall to be done. In fact,
    		 * that should have been done already.
    		 */
    		ast_channel_unlock(chan);
    		return -1;
    	}
    	core_id = recall_data->core_id;
    	ast_channel_unlock(chan);
    	va_start(ap, debug);
    	res = cc_request_state_change(CC_COMPLETE, core_id, debug, ap);
    	va_end(ap);
    	return res;
    }
    
    int ast_cc_failed(int core_id, const char * const debug, ...)
    {
    	va_list ap;
    	int res;
    
    	va_start(ap, debug);
    	res = cc_request_state_change(CC_FAILED, core_id, debug, ap);
    	va_end(ap);
    	return res;
    }
    
    struct ast_cc_monitor_failure_data {
    	const char *device_name;
    	char *debug;
    	int core_id;
    };
    
    static int cc_monitor_failed(void *data)
    {
    	struct ast_cc_monitor_failure_data *failure_data = data;
    	struct cc_core_instance *core_instance;
    	struct ast_cc_monitor *monitor_iter;
    
    	core_instance = find_cc_core_instance(failure_data->core_id);
    	if (!core_instance) {
    		/* Core instance no longer exists or invalid core_id. */
    		ast_log_dynamic_level(cc_logger_level,
    			"Core %d: Could not find core instance for device %s '%s'\n",
    			failure_data->core_id, failure_data->device_name, failure_data->debug);
    		ast_free((char *) failure_data->device_name);
    		ast_free((char *) failure_data->debug);
    		ast_free(failure_data);
    		return -1;
    	}
    
    	AST_LIST_LOCK(core_instance->monitors);
    	AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
    		if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
    			if (!strcmp(monitor_iter->interface->device_name, failure_data->device_name)) {
    				AST_LIST_REMOVE_CURRENT(next);
    				cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
    						monitor_iter->interface->device_name, 1);
    				monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id);
    
    				cc_publish_monitorfailed(monitor_iter->core_id, monitor_iter->interface->device_name);
    
    				cc_unref(monitor_iter, "Monitor reported failure. Unref list's reference.");
    			}
    		}
    	}
    	AST_LIST_TRAVERSE_SAFE_END;
    
    	if (!has_device_monitors(core_instance)) {
    		ast_cc_failed(core_instance->core_id, "All monitors have failed\n");
    	}
    	AST_LIST_UNLOCK(core_instance->monitors);
    	cc_unref(core_instance, "Finished with core_instance in cc_monitor_failed\n");
    
    	ast_free((char *) failure_data->device_name);
    	ast_free((char *) failure_data->debug);
    	ast_free(failure_data);
    	return 0;
    }
    
    int ast_cc_monitor_failed(int core_id, const char *const monitor_name, const char * const debug, ...)
    {
    	struct ast_cc_monitor_failure_data *failure_data;
    	int res;
    	va_list ap;
    
    	if (!(failure_data = ast_calloc(1, sizeof(*failure_data)))) {
    		return -1;
    	}
    
    	if (!(failure_data->device_name = ast_strdup(monitor_name))) {
    		ast_free(failure_data);
    		return -1;
    	}
    
    	va_start(ap, debug);
    	if (ast_vasprintf(&failure_data->debug, debug, ap) == -1) {
    		va_end(ap);
    		ast_free((char *)failure_data->device_name);
    		ast_free(failure_data);
    		return -1;
    	}
    	va_end(ap);
    
    	failure_data->core_id = core_id;
    
    	res = ast_taskprocessor_push(cc_core_taskprocessor, cc_monitor_failed, failure_data);
    	if (res) {
    		ast_free((char *)failure_data->device_name);
    		ast_free((char *)failure_data->debug);
    		ast_free(failure_data);
    	}
    	return res;
    }
    
    static int cc_status_request(void *data)
    {
    	struct cc_core_instance *core_instance= data;
    	int res;
    
    	res = core_instance->agent->callbacks->status_request(core_instance->agent);
    	cc_unref(core_instance, "Status request finished. Unref core instance");
    	return res;
    }
    
    int ast_cc_monitor_status_request(int core_id)
    {
    	int res;