Skip to content
Snippets Groups Projects
ccss.c 153 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 1999 - 2010, Digium, Inc.
     *
     * Mark Michelson <mmichelson@digium.com>
     *
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
     * This program is free software, distributed under the terms of
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    /*! \file
     * \brief Call Completion Supplementary Services implementation
     * \author Mark Michelson <mmichelson@digium.com>
     */
    
    
    /*! \li \ref ccss.c uses the configuration file \ref ccss.conf
     * \addtogroup configuration_file Configuration Files
     */
    
    /*!
     * \page ccss.conf ccss.conf
     * \verbinclude ccss.conf.sample
     */
    
    
    /*** MODULEINFO
    	<support_level>core</support_level>
     ***/
    
    
    #include "asterisk.h"
    
    #include "asterisk/astobj2.h"
    #include "asterisk/strings.h"
    #include "asterisk/ccss.h"
    #include "asterisk/channel.h"
    #include "asterisk/pbx.h"
    #include "asterisk/utils.h"
    #include "asterisk/taskprocessor.h"
    
    #include "asterisk/devicestate.h"
    
    #include "asterisk/module.h"
    #include "asterisk/app.h"
    #include "asterisk/cli.h"
    #include "asterisk/manager.h"
    #include "asterisk/causes.h"
    
    #include "asterisk/stasis_system.h"
    
    
    /*** DOCUMENTATION
    	<application name="CallCompletionRequest" language="en_US">
    		<synopsis>
    			Request call completion service for previous call
    		</synopsis>
    		<syntax />
    		<description>
    			<para>Request call completion service for a previously failed
    			call attempt.</para>
    
    			<para>This application sets the following channel variables:</para>
    			<variablelist>
    				<variable name="CC_REQUEST_RESULT">
    					<para>This is the returned status of the request.</para>
    					<value name="SUCCESS" />
    					<value name="FAIL" />
    				</variable>
    				<variable name="CC_REQUEST_REASON">
    					<para>This is the reason the request failed.</para>
    					<value name="NO_CORE_INSTANCE" />
    					<value name="NOT_GENERIC" />
    					<value name="TOO_MANY_REQUESTS" />
    					<value name="UNSPECIFIED" />
    				</variable>
    			</variablelist>
    
    		</description>
    	</application>
    	<application name="CallCompletionCancel" language="en_US">
    		<synopsis>
    			Cancel call completion service
    		</synopsis>
    		<syntax />
    		<description>
    			<para>Cancel a Call Completion Request.</para>
    
    			<para>This application sets the following channel variables:</para>
    			<variablelist>
    				<variable name="CC_CANCEL_RESULT">
    					<para>This is the returned status of the cancel.</para>
    					<value name="SUCCESS" />
    					<value name="FAIL" />
    				</variable>
    				<variable name="CC_CANCEL_REASON">
    					<para>This is the reason the cancel failed.</para>
    					<value name="NO_CORE_INSTANCE" />
    					<value name="NOT_GENERIC" />
    					<value name="UNSPECIFIED" />
    				</variable>
    			</variablelist>
    
    		</description>
    	</application>
     ***/
    
    /* These are some file-scoped variables. It would be
     * nice to define them closer to their first usage, but since
     * they are used in many places throughout the file, defining
     * them here at the top is easiest.
     */
    
    /*!
    
     * The ast_sched_context used for all generic CC timeouts
    
    static struct ast_sched_context *cc_sched_context;
    
    /*!
     * Counter used to create core IDs for CC calls. Each new
     * core ID is created by atomically adding 1 to the core_id_counter
     */
    static int core_id_counter;
    /*!
     * Taskprocessor from which all CC agent and monitor callbacks
     * are called.
     */
    static struct ast_taskprocessor *cc_core_taskprocessor;
    /*!
     * Name printed on all CC log messages.
     */
    static const char *CC_LOGGER_LEVEL_NAME = "CC";
    /*!
     * Logger level registered by the CC core.
     */
    static int cc_logger_level;
    /*!
     * Parsed configuration value for cc_max_requests
     */
    static unsigned int global_cc_max_requests;
    /*!
     * The current number of CC requests in the system
     */
    static int cc_request_count;
    
    
    static inline void *cc_ref(void *obj, const char *debug)
    {
    	ao2_t_ref(obj, +1, debug);
    	return obj;
    }
    
    static inline void *cc_unref(void *obj, const char *debug)
    {
    	ao2_t_ref(obj, -1, debug);
    	return NULL;
    }
    
    
    /*!
     * \since 1.8
     * \internal
     * \brief A structure for holding the configuration parameters
     * relating to CCSS
     */
    struct ast_cc_config_params {
    	enum ast_cc_agent_policies cc_agent_policy;
    	enum ast_cc_monitor_policies cc_monitor_policy;
    	unsigned int cc_offer_timer;
    	unsigned int ccnr_available_timer;
    	unsigned int ccbs_available_timer;
    	unsigned int cc_recall_timer;
    	unsigned int cc_max_agents;
    	unsigned int cc_max_monitors;
    	char cc_callback_macro[AST_MAX_EXTENSION];
    
    	char cc_callback_sub[AST_MAX_EXTENSION];
    
    172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    	char cc_agent_dialstring[AST_MAX_EXTENSION];
    };
    
    /*!
     * \since 1.8
     * \brief The states used in the CCSS core state machine
     *
     * For more information, see doc/CCSS_architecture.pdf
     */
    enum cc_state {
    	/*! Entered when it is determined that CCSS may be used for the call */
    	CC_AVAILABLE,
    	/*! Entered when a CCSS agent has offered CCSS to a caller */
    	CC_CALLER_OFFERED,
    	/*! Entered when a CCSS agent confirms that a caller has
    	 * requested CCSS */
    	CC_CALLER_REQUESTED,
    	/*! Entered when a CCSS monitor confirms acknowledgment of an
    	 * outbound CCSS request */
    	CC_ACTIVE,
    	/*! Entered when a CCSS monitor alerts the core that the called party
    	 * has become available */
    	CC_CALLEE_READY,
    	/*! Entered when a CCSS agent alerts the core that the calling party
    	 * may not be recalled because he is unavailable
    	 */
    	CC_CALLER_BUSY,
    	/*! Entered when a CCSS agent alerts the core that the calling party
    	 * is attempting to recall the called party
    	 */
    	CC_RECALLING,
    	/*! Entered when an application alerts the core that the calling party's
    	 * recall attempt has had a call progress response indicated
    	 */
    	CC_COMPLETE,
    	/*! Entered any time that something goes wrong during the process, thus
    	 * resulting in the failure of the attempted CCSS transaction. Note also
    	 * that cancellations of CC are treated as failures.
    	 */
    	CC_FAILED,
    };
    
    /*!
     * \brief The payload for an AST_CONTROL_CC frame
     *
     * \details
     * This contains all the necessary data regarding
     * a called device so that the CC core will be able
     * to allocate the proper monitoring resources.
     */
    struct cc_control_payload {
    	/*!
    	 * \brief The type of monitor to allocate.
    	 *
    	 * \details
    	 * The type of monitor to allocate. This is a string which corresponds
    	 * to a set of monitor callbacks registered. Examples include "generic"
    	 * and "SIP"
    	 *
    	 * \note This really should be an array of characters in case this payload
    	 * is sent accross an IAX2 link.  However, this would not make too much sense
    	 * given this type may not be recognized by the other end.
    	 * Protection may be necessary to prevent it from being transmitted.
    	 *
    	 * In addition the following other problems are also possible:
    	 * 1) Endian issues with the integers/enums stored in the config_params.
    	 * 2) Alignment padding issues for the element types.
    	 */
    	const char *monitor_type;
    	/*!
    	 * \brief Private data allocated by the callee
    	 *
    	 * \details
    	 * All channel drivers that monitor endpoints will need to allocate
    	 * data that is not usable by the CC core. In most cases, some or all
    	 * of this data is allocated at the time that the channel driver offers
    	 * CC to the caller. There are many opportunities for failures to occur
    	 * between when a channel driver offers CC and when a monitor is actually
    	 * allocated to watch the endpoint. For this reason, the channel driver
    	 * must give the core a pointer to the private data that was allocated so
    	 * that the core can call back into the channel driver to destroy it if
    	 * a failure occurs. If no private data has been allocated at the time that
    	 * CC is offered, then it is perfectly acceptable to pass NULL for this
    	 * field.
    	 */
    	void *private_data;
    	/*!
    	 * \brief Service offered by the endpoint
    	 *
    	 * \details
    	 * This indicates the type of call completion service offered by the
    	 * endpoint. This data is not crucial to the machinations of the CC core,
    	 * but it is helpful for debugging purposes.
    	 */
    	enum ast_cc_service_type service;
    	/*!
    	 * \brief Configuration parameters used by this endpoint
    	 *
    	 * \details
    	 * Each time an endpoint offers call completion, it must provide its call
    	 * completion configuration parameters. This is because settings may be different
    	 * depending on the circumstances.
    	 */
    	struct ast_cc_config_params config_params;
    	/*!
    	 * \brief ID of parent extension
    	 *
    	 * \details
    	 * This is the only datum that the CC core derives on its own and is not
    	 * provided by the offerer of CC. This provides the core with information on
    	 * which extension monitor is the most immediate parent of this device.
    	 */
    	int parent_interface_id;
    	/*!
    	 * \brief Name of device to be monitored
    	 *
    	 * \details
    	 * The device name by which this monitored endpoint will be referred in the
    	 * CC core. It is highly recommended that this device name is derived by using
    	 * the function ast_channel_get_device_name.
    	 */
    	char device_name[AST_CHANNEL_NAME];
    	/*!
    	 * \brief Recall dialstring
    	 *
    	 * \details
    	 * Certain channel drivers (DAHDI in particular) will require that a special
    	 * dialstring be used to indicate that the outgoing call is to interpreted as
    	 * a CC recall. If the channel driver has such a requirement, then this is
    	 * where that special recall dialstring is placed. If no special dialstring
    	 * is to be used, then the channel driver must provide the original dialstring
    	 * used to call this endpoint.
    	 */
    	char dialstring[AST_CHANNEL_NAME];
    };
    
    /*!
     * \brief The "tree" of interfaces that is dialed.
     *
     * \details
     * Though this is a linked list, it is logically treated
     * as a tree of monitors. Each monitor has an id and a parent_id
     * associated with it. The id is a unique ID for that monitor, and
     * the parent_id is the unique ID of the monitor's parent in the
     * tree. The tree is structured such that all of a parent's children
     * will appear after the parent in the tree. However, it cannot be
     * guaranteed exactly where after the parent the children are.
     *
     * The tree is reference counted since several threads may need
     * to use it, and it may last beyond the lifetime of a single
     * thread.
     */
    AST_LIST_HEAD(cc_monitor_tree, ast_cc_monitor);
    
    static const int CC_CORE_INSTANCES_BUCKETS = 17;
    static struct ao2_container *cc_core_instances;
    
    struct cc_core_instance {
    	/*!
    	 * Unique identifier for this instance of the CC core.
    	 */
    	int core_id;
    	/*!
    	 * The current state for this instance of the CC core.
    	 */
    	enum cc_state current_state;
    	/*!
    	 * The CC agent in use for this call
    	 */
    	struct ast_cc_agent *agent;
    	/*!
    	 * Reference to the monitor tree formed during the initial call
    	 */
    	struct cc_monitor_tree *monitors;
    };
    
    /*!
     * \internal
     * \brief Request that the core change states
     * \param state The state to which we wish to change
     * \param core_id The unique identifier for this instance of the CCSS core state machine
     * \param debug Optional message explaining the reason for the state change
     * \param ap varargs list
     * \retval 0 State change successfully queued
     * \retval -1 Unable to queue state change request
     */
    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);
    
    /*!
     * \internal
     * \brief create a new instance of the CC core and an agent for the calling channel
     *
     * This function will check to make sure that the incoming channel
     * is allowed to request CC by making sure that the incoming channel
     * has not exceeded its maximum number of allowed agents.
     *
     * Should that check pass, the core instance is created, and then the
     * agent for the channel.
     *
     * \param caller_chan The incoming channel for this particular call
     * \param called_tree A reference to the tree of called devices. The agent
     * will gain a reference to this tree as well
     * \param core_id The core_id that this core_instance will assume
     * \retval NULL Failed to create the core instance either due to memory allocation
     * errors or due to the agent count for the caller being too high
     * \retval non-NULL A reference to the newly created cc_core_instance
     */
    static struct cc_core_instance *cc_core_init_instance(struct ast_channel *caller_chan,
    		struct cc_monitor_tree *called_tree, const int core_id, struct cc_control_payload *cc_data);
    
    static const struct {
    	enum ast_cc_service_type service;
    	const char *service_string;
    } cc_service_to_string_map[] = {
    	{AST_CC_NONE, "NONE"},
    	{AST_CC_CCBS, "CCBS"},
    	{AST_CC_CCNR, "CCNR"},
    	{AST_CC_CCNL, "CCNL"},
    };
    
    static const struct {
    	enum cc_state state;
    	const char *state_string;
    } cc_state_to_string_map[] = {
    	{CC_AVAILABLE,          "CC is available"},
    	{CC_CALLER_OFFERED,     "CC offered to caller"},
    	{CC_CALLER_REQUESTED,   "CC requested by caller"},
    	{CC_ACTIVE,             "CC accepted by callee"},
    	{CC_CALLEE_READY,       "Callee has become available"},
    	{CC_CALLER_BUSY,        "Callee was ready, but caller is now unavailable"},
    	{CC_RECALLING,          "Caller is attempting to recall"},
    	{CC_COMPLETE,           "Recall complete"},
    	{CC_FAILED,             "CC has failed"},
    };
    
    static const char *cc_state_to_string(enum cc_state state)
    {
    	return cc_state_to_string_map[state].state_string;
    }
    
    static const char *cc_service_to_string(enum ast_cc_service_type service)
    {
    	return cc_service_to_string_map[service].service_string;
    }
    
    static int cc_core_instance_hash_fn(const void *obj, const int flags)
    {
    	const struct cc_core_instance *core_instance = obj;
    	return core_instance->core_id;
    }
    
    static int cc_core_instance_cmp_fn(void *obj, void *arg, int flags)
    {
    	struct cc_core_instance *core_instance1 = obj;
    	struct cc_core_instance *core_instance2 = arg;
    
    	return core_instance1->core_id == core_instance2->core_id ? CMP_MATCH | CMP_STOP : 0;
    }
    
    static struct cc_core_instance *find_cc_core_instance(const int core_id)
    {
    	struct cc_core_instance finder = {.core_id = core_id,};
    
    	return ao2_t_find(cc_core_instances, &finder, OBJ_POINTER, "Finding a core_instance");
    }
    
    struct cc_callback_helper {
    	ao2_callback_fn *function;
    	void *args;
    	const char *type;
    };
    
    static int cc_agent_callback_helper(void *obj, void *args, int flags)
    {
    	struct cc_core_instance *core_instance = obj;
    	struct cc_callback_helper *helper = args;
    
    	if (strcmp(core_instance->agent->callbacks->type, helper->type)) {
    		return 0;
    	}
    
    	return helper->function(core_instance->agent, helper->args, flags);
    }
    
    struct ast_cc_agent *ast_cc_agent_callback(int flags, ao2_callback_fn *function, void *args, const char * const type)
    {
    	struct cc_callback_helper helper = {.function = function, .args = args, .type = type};
    	struct cc_core_instance *core_instance;
    	if ((core_instance = ao2_t_callback(cc_core_instances, flags, cc_agent_callback_helper, &helper,
    					"Calling provided agent callback function"))) {
    		struct ast_cc_agent *agent = cc_ref(core_instance->agent, "An outside entity needs the agent");
    		cc_unref(core_instance, "agent callback done with the core_instance");
    		return agent;
    	}
    	return NULL;
    }
    
    enum match_flags {
    	/* Only match agents that have not yet
    	 * made a CC request
    	 */
    	MATCH_NO_REQUEST = (1 << 0),
    	/* Only match agents that have made
    	 * a CC request
    	 */
    	MATCH_REQUEST = (1 << 1),
    };
    
    /* ao2_callbacks for cc_core_instances */
    
    /*!
     * \internal
     * \brief find a core instance based on its agent
     *
     * The match flags tell whether we wish to find core instances
     * that have a monitor or core instances that do not. Core instances
     * with no monitor are core instances for which a caller has not yet
     * requested CC. Core instances with a monitor are ones for which the
     * caller has requested CC.
     */
    static int match_agent(void *obj, void *arg, void *data, int flags)
    {
    	struct cc_core_instance *core_instance = obj;
    	const char *name = arg;
    	unsigned long match_flags = *(unsigned long *)data;
    	int possible_match = 0;
    
    	if ((match_flags & MATCH_NO_REQUEST) && core_instance->current_state < CC_CALLER_REQUESTED) {
    		possible_match = 1;
    	}
    
    	if ((match_flags & MATCH_REQUEST) && core_instance->current_state >= CC_CALLER_REQUESTED) {
    		possible_match = 1;
    	}
    
    	if (!possible_match) {
    		return 0;
    	}
    
    	if (!strcmp(core_instance->agent->device_name, name)) {
    		return CMP_MATCH | CMP_STOP;
    	}
    	return 0;
    }
    
    struct count_agents_cb_data {
    	int count;
    	int core_id_exception;
    };
    
    /*!
     * \internal
     * \brief Count the number of agents a specific interface is using
     *
     * We're only concerned with the number of agents that have requested
     * CC, so we restrict our search to core instances which have a non-NULL
     * monitor pointer
     */
    static int count_agents_cb(void *obj, void *arg, void *data, int flags)
    {
    	struct cc_core_instance *core_instance = obj;
    	const char *name = arg;
    	struct count_agents_cb_data *cb_data = data;
    
    	if (cb_data->core_id_exception == core_instance->core_id) {
    		ast_log_dynamic_level(cc_logger_level, "Found agent with core_id %d but not counting it toward total\n", core_instance->core_id);
    		return 0;
    	}
    
    	if (core_instance->current_state >= CC_CALLER_REQUESTED && !strcmp(core_instance->agent->device_name, name)) {
    		cb_data->count++;
    	}
    	return 0;
    }
    
    
    /* default values mapping from cc_state to ast_dev_state */
    
    #define CC_AVAILABLE_DEVSTATE_DEFAULT        AST_DEVICE_NOT_INUSE
    #define CC_CALLER_OFFERED_DEVSTATE_DEFAULT   AST_DEVICE_NOT_INUSE
    #define CC_CALLER_REQUESTED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
    #define CC_ACTIVE_DEVSTATE_DEFAULT           AST_DEVICE_INUSE
    #define CC_CALLEE_READY_DEVSTATE_DEFAULT     AST_DEVICE_RINGING
    #define CC_CALLER_BUSY_DEVSTATE_DEFAULT      AST_DEVICE_ONHOLD
    #define CC_RECALLING_DEVSTATE_DEFAULT        AST_DEVICE_RINGING
    #define CC_COMPLETE_DEVSTATE_DEFAULT         AST_DEVICE_NOT_INUSE
    #define CC_FAILED_DEVSTATE_DEFAULT           AST_DEVICE_NOT_INUSE
    
    /*!
     * \internal
     * \brief initialization of defaults for CC_STATE to DEVICE_STATE map
     */
    static enum ast_device_state cc_state_to_devstate_map[] = {
    	[CC_AVAILABLE] =        CC_AVAILABLE_DEVSTATE_DEFAULT,
    	[CC_CALLER_OFFERED] =   CC_CALLER_OFFERED_DEVSTATE_DEFAULT,
    	[CC_CALLER_REQUESTED] = CC_CALLER_REQUESTED_DEVSTATE_DEFAULT,
    	[CC_ACTIVE] =           CC_ACTIVE_DEVSTATE_DEFAULT,
    	[CC_CALLEE_READY] =     CC_CALLEE_READY_DEVSTATE_DEFAULT,
    	[CC_CALLER_BUSY] =      CC_CALLER_BUSY_DEVSTATE_DEFAULT,
    	[CC_RECALLING] =        CC_RECALLING_DEVSTATE_DEFAULT,
    	[CC_COMPLETE] =         CC_COMPLETE_DEVSTATE_DEFAULT,
    	[CC_FAILED] =           CC_FAILED_DEVSTATE_DEFAULT,
    };
    
    /*!
    
     * \internal
    
     * \brief lookup the ast_device_state mapped to cc_state
     *
    
     * \param state
     *
    
     * \return the correponding DEVICE STATE from the cc_state_to_devstate_map
     * when passed an internal state.
     */
    static enum ast_device_state cc_state_to_devstate(enum cc_state state)
    {
    	return cc_state_to_devstate_map[state];
    }
    
    /*!
     * \internal
     * \brief Callback for devicestate providers
     *
     * \details
     * Initialize with ast_devstate_prov_add() and returns the corresponding
     * DEVICE STATE based on the current CC_STATE state machine if the requested
     * device is found and is a generic device. Returns the equivalent of
     * CC_FAILED, which defaults to NOT_INUSE, if no device is found.  NOT_INUSE would
     * indicate that there is no presence of any pending call back.
     */
    static enum ast_device_state ccss_device_state(const char *device_name)
    {
    	struct cc_core_instance *core_instance;
    	unsigned long match_flags;
    	enum ast_device_state cc_current_state;
    
    	match_flags = MATCH_NO_REQUEST;
    	core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent,
    		(char *) device_name, &match_flags,
    		"Find Core Instance for ccss_device_state reqeust.");
    	if (!core_instance) {
    		ast_log_dynamic_level(cc_logger_level,
    			"Couldn't find a core instance for caller %s\n", device_name);
    		return cc_state_to_devstate(CC_FAILED);
    	}
    
    	ast_log_dynamic_level(cc_logger_level,
    		"Core %d: Found core_instance for caller %s in state %s\n",
    		core_instance->core_id, device_name, cc_state_to_string(core_instance->current_state));
    
    	if (strcmp(core_instance->agent->callbacks->type, "generic")) {
    		ast_log_dynamic_level(cc_logger_level,
    			"Core %d: Device State is only for generic agent types.\n",
    			core_instance->core_id);
    		cc_unref(core_instance, "Unref core_instance since ccss_device_state was called with native agent");
    		return cc_state_to_devstate(CC_FAILED);
    	}
    	cc_current_state = cc_state_to_devstate(core_instance->current_state);
    	cc_unref(core_instance, "Unref core_instance done with ccss_device_state");
    	return cc_current_state;
    }
    
    /*!
     * \internal
     * \brief Notify Device State Changes from CC STATE MACHINE
     *
     * \details
     * Any time a state is changed, we call this function to notify the DEVICE STATE
     * subsystem of the change so that subscribed phones to any corresponding hints that
     * are using that state are updated.
     */
    static void ccss_notify_device_state_change(const char *device, enum cc_state state)
    {
    	enum ast_device_state devstate;
    
    	devstate = cc_state_to_devstate(state);
    
    	ast_log_dynamic_level(cc_logger_level,
    
    		"Notification of CCSS state change to '%s', device state '%s' for device '%s'\n",
    
    		cc_state_to_string(state), ast_devstate2str(devstate), device);
    
    
    	ast_devstate_changed(devstate, AST_DEVSTATE_CACHABLE, "ccss:%s", device);
    
    #define CC_OFFER_TIMER_DEFAULT			20		/* Seconds */
    #define CCNR_AVAILABLE_TIMER_DEFAULT	7200	/* Seconds */
    #define CCBS_AVAILABLE_TIMER_DEFAULT	4800	/* Seconds */
    #define CC_RECALL_TIMER_DEFAULT			20		/* Seconds */
    #define CC_MAX_AGENTS_DEFAULT			5
    #define CC_MAX_MONITORS_DEFAULT			5
    #define GLOBAL_CC_MAX_REQUESTS_DEFAULT	20
    
    static const struct ast_cc_config_params cc_default_params = {
    	.cc_agent_policy = AST_CC_AGENT_NEVER,
    	.cc_monitor_policy = AST_CC_MONITOR_NEVER,
    	.cc_offer_timer = CC_OFFER_TIMER_DEFAULT,
    	.ccnr_available_timer = CCNR_AVAILABLE_TIMER_DEFAULT,
    	.ccbs_available_timer = CCBS_AVAILABLE_TIMER_DEFAULT,
    	.cc_recall_timer = CC_RECALL_TIMER_DEFAULT,
    	.cc_max_agents = CC_MAX_AGENTS_DEFAULT,
    	.cc_max_monitors = CC_MAX_MONITORS_DEFAULT,
    	.cc_callback_macro = "",
    
    	.cc_agent_dialstring = "",
    };
    
    void ast_cc_default_config_params(struct ast_cc_config_params *params)
    {
    	*params = cc_default_params;
    }
    
    
    struct ast_cc_config_params *__ast_cc_config_params_init(const char *file, int line, const char *function)
    {
    
    	struct ast_cc_config_params *params = __ast_malloc(sizeof(*params), file, line, function);
    
    	ast_cc_default_config_params(params);
    
    	return params;
    }
    
    void ast_cc_config_params_destroy(struct ast_cc_config_params *params)
    {
    	ast_free(params);
    }
    
    static enum ast_cc_agent_policies str_to_agent_policy(const char * const value)
    {
    	if (!strcasecmp(value, "never")) {
    		return AST_CC_AGENT_NEVER;
    	} else if (!strcasecmp(value, "native")) {
    		return AST_CC_AGENT_NATIVE;
    	} else if (!strcasecmp(value, "generic")) {
    		return AST_CC_AGENT_GENERIC;
    	} else {
    		ast_log(LOG_WARNING, "%s is an invalid value for cc_agent_policy. Switching to 'never'\n", value);
    		return AST_CC_AGENT_NEVER;
    	}
    }
    
    static enum ast_cc_monitor_policies str_to_monitor_policy(const char * const value)
    {
    	if (!strcasecmp(value, "never")) {
    		return AST_CC_MONITOR_NEVER;
    	} else if (!strcasecmp(value, "native")) {
    		return AST_CC_MONITOR_NATIVE;
    	} else if (!strcasecmp(value, "generic")) {
    		return AST_CC_MONITOR_GENERIC;
    	} else if (!strcasecmp(value, "always")) {
    		return AST_CC_MONITOR_ALWAYS;
    	} else {
    		ast_log(LOG_WARNING, "%s is an invalid value for cc_monitor_policy. Switching to 'never'\n", value);
    		return AST_CC_MONITOR_NEVER;
    	}
    }
    
    static const char *agent_policy_to_str(enum ast_cc_agent_policies policy)
    {
    	switch (policy) {
    	case AST_CC_AGENT_NEVER:
    		return "never";
    	case AST_CC_AGENT_NATIVE:
    		return "native";
    	case AST_CC_AGENT_GENERIC:
    		return "generic";
    	default:
    		/* This should never happen... */
    		return "";
    	}
    }
    
    static const char *monitor_policy_to_str(enum ast_cc_monitor_policies policy)
    {
    	switch (policy) {
    	case AST_CC_MONITOR_NEVER:
    		return "never";
    	case AST_CC_MONITOR_NATIVE:
    		return "native";
    	case AST_CC_MONITOR_GENERIC:
    		return "generic";
    	case AST_CC_MONITOR_ALWAYS:
    		return "always";
    	default:
    		/* This should never happen... */
    		return "";
    	}
    }
    int ast_cc_get_param(struct ast_cc_config_params *params, const char * const name,
    		char *buf, size_t buf_len)
    {
    	const char *value = NULL;
    
    	if (!strcasecmp(name, "cc_callback_macro")) {
    		value = ast_get_cc_callback_macro(params);
    
    	} else if (!strcasecmp(name, "cc_callback_sub")) {
    		value = ast_get_cc_callback_sub(params);
    
    	} else if (!strcasecmp(name, "cc_agent_policy")) {
    		value = agent_policy_to_str(ast_get_cc_agent_policy(params));
    	} else if (!strcasecmp(name, "cc_monitor_policy")) {
    		value = monitor_policy_to_str(ast_get_cc_monitor_policy(params));
    	} else if (!strcasecmp(name, "cc_agent_dialstring")) {
    		value = ast_get_cc_agent_dialstring(params);
    	}
    
    		ast_copy_string(buf, value, buf_len);
    		return 0;
    	}
    
    	/* The rest of these are all ints of some sort and require some
    	 * snprintf-itude
    	 */
    
    	if (!strcasecmp(name, "cc_offer_timer")) {
    		snprintf(buf, buf_len, "%u", ast_get_cc_offer_timer(params));
    	} else if (!strcasecmp(name, "ccnr_available_timer")) {
    		snprintf(buf, buf_len, "%u", ast_get_ccnr_available_timer(params));
    	} else if (!strcasecmp(name, "ccbs_available_timer")) {
    		snprintf(buf, buf_len, "%u", ast_get_ccbs_available_timer(params));
    	} else if (!strcasecmp(name, "cc_max_agents")) {
    		snprintf(buf, buf_len, "%u", ast_get_cc_max_agents(params));
    	} else if (!strcasecmp(name, "cc_max_monitors")) {
    		snprintf(buf, buf_len, "%u", ast_get_cc_max_monitors(params));
    	} else if (!strcasecmp(name, "cc_recall_timer")) {
    		snprintf(buf, buf_len, "%u", ast_get_cc_recall_timer(params));
    	} else {
    		ast_log(LOG_WARNING, "%s is not a valid CC parameter. Ignoring.\n", name);
    		return -1;
    	}
    
    	return 0;
    }
    
    int ast_cc_set_param(struct ast_cc_config_params *params, const char * const name,
    		const char * const value)
    {
    	unsigned int value_as_uint;
    	if (!strcasecmp(name, "cc_agent_policy")) {
    		return ast_set_cc_agent_policy(params, str_to_agent_policy(value));
    	} else if (!strcasecmp(name, "cc_monitor_policy")) {
    		return ast_set_cc_monitor_policy(params, str_to_monitor_policy(value));
    	} else if (!strcasecmp(name, "cc_agent_dialstring")) {
    		ast_set_cc_agent_dialstring(params, value);
    	} else if (!strcasecmp(name, "cc_callback_macro")) {
    		ast_set_cc_callback_macro(params, value);
    		return 0;
    
    	} else if (!strcasecmp(name, "cc_callback_sub")) {
    		ast_set_cc_callback_sub(params, value);
    		return 0;
    
    	if (sscanf(value, "%30u", &value_as_uint) != 1) {
    
    		return -1;
    	}
    
    	if (!strcasecmp(name, "cc_offer_timer")) {
    		ast_set_cc_offer_timer(params, value_as_uint);
    	} else if (!strcasecmp(name, "ccnr_available_timer")) {
    		ast_set_ccnr_available_timer(params, value_as_uint);
    	} else if (!strcasecmp(name, "ccbs_available_timer")) {
    		ast_set_ccbs_available_timer(params, value_as_uint);
    	} else if (!strcasecmp(name, "cc_max_agents")) {
    		ast_set_cc_max_agents(params, value_as_uint);
    	} else if (!strcasecmp(name, "cc_max_monitors")) {
    		ast_set_cc_max_monitors(params, value_as_uint);
    	} else if (!strcasecmp(name, "cc_recall_timer")) {
    		ast_set_cc_recall_timer(params, value_as_uint);
    	} else {
    		ast_log(LOG_WARNING, "%s is not a valid CC parameter. Ignoring.\n", name);
    		return -1;
    	}
    
    	return 0;
    }
    
    int ast_cc_is_config_param(const char * const name)
    {
    	return (!strcasecmp(name, "cc_agent_policy") ||
    				!strcasecmp(name, "cc_monitor_policy") ||
    				!strcasecmp(name, "cc_offer_timer") ||
    				!strcasecmp(name, "ccnr_available_timer") ||
    				!strcasecmp(name, "ccbs_available_timer") ||
    				!strcasecmp(name, "cc_max_agents") ||
    				!strcasecmp(name, "cc_max_monitors") ||
    				!strcasecmp(name, "cc_callback_macro") ||
    
    				!strcasecmp(name, "cc_callback_sub") ||
    
    				!strcasecmp(name, "cc_agent_dialstring") ||
    				!strcasecmp(name, "cc_recall_timer"));
    }
    
    void ast_cc_copy_config_params(struct ast_cc_config_params *dest, const struct ast_cc_config_params *src)
    {
    	*dest = *src;
    }
    
    enum ast_cc_agent_policies ast_get_cc_agent_policy(struct ast_cc_config_params *config)
    {
    	return config->cc_agent_policy;
    }
    
    int ast_set_cc_agent_policy(struct ast_cc_config_params *config, enum ast_cc_agent_policies value)
    {
    	/* Screw C and its weak type checking for making me have to do this
    	 * validation at runtime.
    	 */
    	if (value < AST_CC_AGENT_NEVER || value > AST_CC_AGENT_GENERIC) {
    		return -1;
    	}
    	config->cc_agent_policy = value;
    	return 0;
    }
    
    enum ast_cc_monitor_policies ast_get_cc_monitor_policy(struct ast_cc_config_params *config)
    {
    	return config->cc_monitor_policy;
    }
    
    int ast_set_cc_monitor_policy(struct ast_cc_config_params *config, enum ast_cc_monitor_policies value)
    {
    	/* Screw C and its weak type checking for making me have to do this
    	 * validation at runtime.
    	 */
    	if (value < AST_CC_MONITOR_NEVER || value > AST_CC_MONITOR_ALWAYS) {
    		return -1;
    	}
    	config->cc_monitor_policy = value;
    	return 0;
    }
    
    unsigned int ast_get_cc_offer_timer(struct ast_cc_config_params *config)
    {
    	return config->cc_offer_timer;
    }
    
    void ast_set_cc_offer_timer(struct ast_cc_config_params *config, unsigned int value)
    {
    	/* 0 is an unreasonable value for any timer. Stick with the default */
    	if (value == 0) {
    		ast_log(LOG_WARNING, "0 is an invalid value for cc_offer_timer. Retaining value as %u\n", config->cc_offer_timer);
    		return;
    	}
    	config->cc_offer_timer = value;
    }
    
    unsigned int ast_get_ccnr_available_timer(struct ast_cc_config_params *config)
    {
    	return config->ccnr_available_timer;
    }
    
    void ast_set_ccnr_available_timer(struct ast_cc_config_params *config, unsigned int value)
    {
    	/* 0 is an unreasonable value for any timer. Stick with the default */
    	if (value == 0) {
    		ast_log(LOG_WARNING, "0 is an invalid value for ccnr_available_timer. Retaining value as %u\n", config->ccnr_available_timer);
    		return;
    	}
    	config->ccnr_available_timer = value;
    }
    
    unsigned int ast_get_cc_recall_timer(struct ast_cc_config_params *config)
    {
    	return config->cc_recall_timer;
    }
    
    void ast_set_cc_recall_timer(struct ast_cc_config_params *config, unsigned int value)
    {
    	/* 0 is an unreasonable value for any timer. Stick with the default */
    	if (value == 0) {
    		ast_log(LOG_WARNING, "0 is an invalid value for ccnr_available_timer. Retaining value as %u\n", config->cc_recall_timer);
    		return;
    	}
    	config->cc_recall_timer = value;
    }
    
    unsigned int ast_get_ccbs_available_timer(struct ast_cc_config_params *config)
    {
    	return config->ccbs_available_timer;
    }
    
    void ast_set_ccbs_available_timer(struct ast_cc_config_params *config, unsigned int value)
    {
    	/* 0 is an unreasonable value for any timer. Stick with the default */
    	if (value == 0) {
    		ast_log(LOG_WARNING, "0 is an invalid value for ccbs_available_timer. Retaining value as %u\n", config->ccbs_available_timer);
    		return;
    	}
    	config->ccbs_available_timer = value;
    }
    
    const char *ast_get_cc_agent_dialstring(struct ast_cc_config_params *config)
    {
    	return config->cc_agent_dialstring;
    }
    
    void ast_set_cc_agent_dialstring(struct ast_cc_config_params *config, const char *const value)
    {
    	if (ast_strlen_zero(value)) {
    		config->cc_agent_dialstring[0] = '\0';
    	} else {
    		ast_copy_string(config->cc_agent_dialstring, value, sizeof(config->cc_agent_dialstring));
    	}
    }
    
    unsigned int ast_get_cc_max_agents(struct ast_cc_config_params *config)
    {
    	return config->cc_max_agents;
    }
    
    void ast_set_cc_max_agents(struct ast_cc_config_params *config, unsigned int value)
    {
    	config->cc_max_agents = value;
    }
    
    unsigned int ast_get_cc_max_monitors(struct ast_cc_config_params *config)
    {
    	return config->cc_max_monitors;
    }
    
    void ast_set_cc_max_monitors(struct ast_cc_config_params *config, unsigned int value)
    {
    	config->cc_max_monitors = value;
    }
    
    const char *ast_get_cc_callback_macro(struct ast_cc_config_params *config)
    {
    	return config->cc_callback_macro;
    }
    
    
    const char *ast_get_cc_callback_sub(struct ast_cc_config_params *config)
    {