ccss.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2010, Digium, Inc.
00005  *
00006  * Mark Michelson <mmichelson@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  * \brief Call Completion Supplementary Services implementation
00021  * \author Mark Michelson <mmichelson@digium.com>
00022  */
00023 
00024 /*! \li \ref ccss.c uses the configuration file \ref ccss.conf
00025  * \addtogroup configuration_file Configuration Files
00026  */
00027 
00028 /*!
00029  * \page ccss.conf ccss.conf
00030  * \verbinclude ccss.conf.sample
00031  */
00032 
00033 /*** MODULEINFO
00034    <support_level>core</support_level>
00035  ***/
00036 
00037 #include "asterisk.h"
00038 
00039 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 434115 $")
00040 
00041 #include "asterisk/astobj2.h"
00042 #include "asterisk/strings.h"
00043 #include "asterisk/ccss.h"
00044 #include "asterisk/channel.h"
00045 #include "asterisk/pbx.h"
00046 #include "asterisk/utils.h"
00047 #include "asterisk/taskprocessor.h"
00048 #include "asterisk/devicestate.h"
00049 #include "asterisk/module.h"
00050 #include "asterisk/app.h"
00051 #include "asterisk/cli.h"
00052 #include "asterisk/manager.h"
00053 #include "asterisk/causes.h"
00054 #include "asterisk/stasis_system.h"
00055 #include "asterisk/format_cache.h"
00056 
00057 /*** DOCUMENTATION
00058    <application name="CallCompletionRequest" language="en_US">
00059       <synopsis>
00060          Request call completion service for previous call
00061       </synopsis>
00062       <syntax />
00063       <description>
00064          <para>Request call completion service for a previously failed
00065          call attempt.</para>
00066          <para>This application sets the following channel variables:</para>
00067          <variablelist>
00068             <variable name="CC_REQUEST_RESULT">
00069                <para>This is the returned status of the request.</para>
00070                <value name="SUCCESS" />
00071                <value name="FAIL" />
00072             </variable>
00073             <variable name="CC_REQUEST_REASON">
00074                <para>This is the reason the request failed.</para>
00075                <value name="NO_CORE_INSTANCE" />
00076                <value name="NOT_GENERIC" />
00077                <value name="TOO_MANY_REQUESTS" />
00078                <value name="UNSPECIFIED" />
00079             </variable>
00080          </variablelist>
00081       </description>
00082    </application>
00083    <application name="CallCompletionCancel" language="en_US">
00084       <synopsis>
00085          Cancel call completion service
00086       </synopsis>
00087       <syntax />
00088       <description>
00089          <para>Cancel a Call Completion Request.</para>
00090          <para>This application sets the following channel variables:</para>
00091          <variablelist>
00092             <variable name="CC_CANCEL_RESULT">
00093                <para>This is the returned status of the cancel.</para>
00094                <value name="SUCCESS" />
00095                <value name="FAIL" />
00096             </variable>
00097             <variable name="CC_CANCEL_REASON">
00098                <para>This is the reason the cancel failed.</para>
00099                <value name="NO_CORE_INSTANCE" />
00100                <value name="NOT_GENERIC" />
00101                <value name="UNSPECIFIED" />
00102             </variable>
00103          </variablelist>
00104       </description>
00105    </application>
00106  ***/
00107 
00108 /* These are some file-scoped variables. It would be
00109  * nice to define them closer to their first usage, but since
00110  * they are used in many places throughout the file, defining
00111  * them here at the top is easiest.
00112  */
00113 
00114 /*!
00115  * The ast_sched_context used for all generic CC timeouts
00116  */
00117 static struct ast_sched_context *cc_sched_context;
00118 /*!
00119  * Counter used to create core IDs for CC calls. Each new
00120  * core ID is created by atomically adding 1 to the core_id_counter
00121  */
00122 static int core_id_counter;
00123 /*!
00124  * Taskprocessor from which all CC agent and monitor callbacks
00125  * are called.
00126  */
00127 static struct ast_taskprocessor *cc_core_taskprocessor;
00128 /*!
00129  * Name printed on all CC log messages.
00130  */
00131 static const char *CC_LOGGER_LEVEL_NAME = "CC";
00132 /*!
00133  * Logger level registered by the CC core.
00134  */
00135 static int cc_logger_level;
00136 /*!
00137  * Parsed configuration value for cc_max_requests
00138  */
00139 static unsigned int global_cc_max_requests;
00140 /*!
00141  * The current number of CC requests in the system
00142  */
00143 static int cc_request_count;
00144 
00145 static inline void *cc_ref(void *obj, const char *debug)
00146 {
00147    ao2_t_ref(obj, +1, debug);
00148    return obj;
00149 }
00150 
00151 static inline void *cc_unref(void *obj, const char *debug)
00152 {
00153    ao2_t_ref(obj, -1, debug);
00154    return NULL;
00155 }
00156 
00157 /*!
00158  * \since 1.8
00159  * \internal
00160  * \brief A structure for holding the configuration parameters
00161  * relating to CCSS
00162  */
00163 struct ast_cc_config_params {
00164    enum ast_cc_agent_policies cc_agent_policy;
00165    enum ast_cc_monitor_policies cc_monitor_policy;
00166    unsigned int cc_offer_timer;
00167    unsigned int ccnr_available_timer;
00168    unsigned int ccbs_available_timer;
00169    unsigned int cc_recall_timer;
00170    unsigned int cc_max_agents;
00171    unsigned int cc_max_monitors;
00172    char cc_callback_macro[AST_MAX_EXTENSION];
00173    char cc_callback_sub[AST_MAX_EXTENSION];
00174    char cc_agent_dialstring[AST_MAX_EXTENSION];
00175 };
00176 
00177 /*!
00178  * \since 1.8
00179  * \brief The states used in the CCSS core state machine
00180  *
00181  * For more information, see doc/CCSS_architecture.pdf
00182  */
00183 enum cc_state {
00184    /*! Entered when it is determined that CCSS may be used for the call */
00185    CC_AVAILABLE,
00186    /*! Entered when a CCSS agent has offered CCSS to a caller */
00187    CC_CALLER_OFFERED,
00188    /*! Entered when a CCSS agent confirms that a caller has
00189     * requested CCSS */
00190    CC_CALLER_REQUESTED,
00191    /*! Entered when a CCSS monitor confirms acknowledgment of an
00192     * outbound CCSS request */
00193    CC_ACTIVE,
00194    /*! Entered when a CCSS monitor alerts the core that the called party
00195     * has become available */
00196    CC_CALLEE_READY,
00197    /*! Entered when a CCSS agent alerts the core that the calling party
00198     * may not be recalled because he is unavailable
00199     */
00200    CC_CALLER_BUSY,
00201    /*! Entered when a CCSS agent alerts the core that the calling party
00202     * is attempting to recall the called party
00203     */
00204    CC_RECALLING,
00205    /*! Entered when an application alerts the core that the calling party's
00206     * recall attempt has had a call progress response indicated
00207     */
00208    CC_COMPLETE,
00209    /*! Entered any time that something goes wrong during the process, thus
00210     * resulting in the failure of the attempted CCSS transaction. Note also
00211     * that cancellations of CC are treated as failures.
00212     */
00213    CC_FAILED,
00214 };
00215 
00216 /*!
00217  * \brief The payload for an AST_CONTROL_CC frame
00218  *
00219  * \details
00220  * This contains all the necessary data regarding
00221  * a called device so that the CC core will be able
00222  * to allocate the proper monitoring resources.
00223  */
00224 struct cc_control_payload {
00225    /*!
00226     * \brief The type of monitor to allocate.
00227     *
00228     * \details
00229     * The type of monitor to allocate. This is a string which corresponds
00230     * to a set of monitor callbacks registered. Examples include "generic"
00231     * and "SIP"
00232     *
00233     * \note This really should be an array of characters in case this payload
00234     * is sent accross an IAX2 link.  However, this would not make too much sense
00235     * given this type may not be recognized by the other end.
00236     * Protection may be necessary to prevent it from being transmitted.
00237     *
00238     * In addition the following other problems are also possible:
00239     * 1) Endian issues with the integers/enums stored in the config_params.
00240     * 2) Alignment padding issues for the element types.
00241     */
00242    const char *monitor_type;
00243    /*!
00244     * \brief Private data allocated by the callee
00245     *
00246     * \details
00247     * All channel drivers that monitor endpoints will need to allocate
00248     * data that is not usable by the CC core. In most cases, some or all
00249     * of this data is allocated at the time that the channel driver offers
00250     * CC to the caller. There are many opportunities for failures to occur
00251     * between when a channel driver offers CC and when a monitor is actually
00252     * allocated to watch the endpoint. For this reason, the channel driver
00253     * must give the core a pointer to the private data that was allocated so
00254     * that the core can call back into the channel driver to destroy it if
00255     * a failure occurs. If no private data has been allocated at the time that
00256     * CC is offered, then it is perfectly acceptable to pass NULL for this
00257     * field.
00258     */
00259    void *private_data;
00260    /*!
00261     * \brief Service offered by the endpoint
00262     *
00263     * \details
00264     * This indicates the type of call completion service offered by the
00265     * endpoint. This data is not crucial to the machinations of the CC core,
00266     * but it is helpful for debugging purposes.
00267     */
00268    enum ast_cc_service_type service;
00269    /*!
00270     * \brief Configuration parameters used by this endpoint
00271     *
00272     * \details
00273     * Each time an endpoint offers call completion, it must provide its call
00274     * completion configuration parameters. This is because settings may be different
00275     * depending on the circumstances.
00276     */
00277    struct ast_cc_config_params config_params;
00278    /*!
00279     * \brief ID of parent extension
00280     *
00281     * \details
00282     * This is the only datum that the CC core derives on its own and is not
00283     * provided by the offerer of CC. This provides the core with information on
00284     * which extension monitor is the most immediate parent of this device.
00285     */
00286    int parent_interface_id;
00287    /*!
00288     * \brief Name of device to be monitored
00289     *
00290     * \details
00291     * The device name by which this monitored endpoint will be referred in the
00292     * CC core. It is highly recommended that this device name is derived by using
00293     * the function ast_channel_get_device_name.
00294     */
00295    char device_name[AST_CHANNEL_NAME];
00296    /*!
00297     * \brief Recall dialstring
00298     *
00299     * \details
00300     * Certain channel drivers (DAHDI in particular) will require that a special
00301     * dialstring be used to indicate that the outgoing call is to interpreted as
00302     * a CC recall. If the channel driver has such a requirement, then this is
00303     * where that special recall dialstring is placed. If no special dialstring
00304     * is to be used, then the channel driver must provide the original dialstring
00305     * used to call this endpoint.
00306     */
00307    char dialstring[AST_CHANNEL_NAME];
00308 };
00309 
00310 /*!
00311  * \brief The "tree" of interfaces that is dialed.
00312  *
00313  * \details
00314  * Though this is a linked list, it is logically treated
00315  * as a tree of monitors. Each monitor has an id and a parent_id
00316  * associated with it. The id is a unique ID for that monitor, and
00317  * the parent_id is the unique ID of the monitor's parent in the
00318  * tree. The tree is structured such that all of a parent's children
00319  * will appear after the parent in the tree. However, it cannot be
00320  * guaranteed exactly where after the parent the children are.
00321  *
00322  * The tree is reference counted since several threads may need
00323  * to use it, and it may last beyond the lifetime of a single
00324  * thread.
00325  */
00326 AST_LIST_HEAD(cc_monitor_tree, ast_cc_monitor);
00327 
00328 static const int CC_CORE_INSTANCES_BUCKETS = 17;
00329 static struct ao2_container *cc_core_instances;
00330 
00331 struct cc_core_instance {
00332    /*!
00333     * Unique identifier for this instance of the CC core.
00334     */
00335    int core_id;
00336    /*!
00337     * The current state for this instance of the CC core.
00338     */
00339    enum cc_state current_state;
00340    /*!
00341     * The CC agent in use for this call
00342     */
00343    struct ast_cc_agent *agent;
00344    /*!
00345     * Reference to the monitor tree formed during the initial call
00346     */
00347    struct cc_monitor_tree *monitors;
00348 };
00349 
00350 /*!
00351  * \internal
00352  * \brief Request that the core change states
00353  * \param state The state to which we wish to change
00354  * \param core_id The unique identifier for this instance of the CCSS core state machine
00355  * \param debug Optional message explaining the reason for the state change
00356  * \param ap varargs list
00357  * \retval 0 State change successfully queued
00358  * \retval -1 Unable to queue state change request
00359  */
00360 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);
00361 
00362 /*!
00363  * \internal
00364  * \brief create a new instance of the CC core and an agent for the calling channel
00365  *
00366  * This function will check to make sure that the incoming channel
00367  * is allowed to request CC by making sure that the incoming channel
00368  * has not exceeded its maximum number of allowed agents.
00369  *
00370  * Should that check pass, the core instance is created, and then the
00371  * agent for the channel.
00372  *
00373  * \param caller_chan The incoming channel for this particular call
00374  * \param called_tree A reference to the tree of called devices. The agent
00375  * will gain a reference to this tree as well
00376  * \param core_id The core_id that this core_instance will assume
00377  * \retval NULL Failed to create the core instance either due to memory allocation
00378  * errors or due to the agent count for the caller being too high
00379  * \retval non-NULL A reference to the newly created cc_core_instance
00380  */
00381 static struct cc_core_instance *cc_core_init_instance(struct ast_channel *caller_chan,
00382       struct cc_monitor_tree *called_tree, const int core_id, struct cc_control_payload *cc_data);
00383 
00384 static const struct {
00385    enum ast_cc_service_type service;
00386    const char *service_string;
00387 } cc_service_to_string_map[] = {
00388    {AST_CC_NONE, "NONE"},
00389    {AST_CC_CCBS, "CCBS"},
00390    {AST_CC_CCNR, "CCNR"},
00391    {AST_CC_CCNL, "CCNL"},
00392 };
00393 
00394 static const struct {
00395    enum cc_state state;
00396    const char *state_string;
00397 } cc_state_to_string_map[] = {
00398    {CC_AVAILABLE,          "CC is available"},
00399    {CC_CALLER_OFFERED,     "CC offered to caller"},
00400    {CC_CALLER_REQUESTED,   "CC requested by caller"},
00401    {CC_ACTIVE,             "CC accepted by callee"},
00402    {CC_CALLEE_READY,       "Callee has become available"},
00403    {CC_CALLER_BUSY,        "Callee was ready, but caller is now unavailable"},
00404    {CC_RECALLING,          "Caller is attempting to recall"},
00405    {CC_COMPLETE,           "Recall complete"},
00406    {CC_FAILED,             "CC has failed"},
00407 };
00408 
00409 static const char *cc_state_to_string(enum cc_state state)
00410 {
00411    return cc_state_to_string_map[state].state_string;
00412 }
00413 
00414 static const char *cc_service_to_string(enum ast_cc_service_type service)
00415 {
00416    return cc_service_to_string_map[service].service_string;
00417 }
00418 
00419 static int cc_core_instance_hash_fn(const void *obj, const int flags)
00420 {
00421    const struct cc_core_instance *core_instance = obj;
00422    return core_instance->core_id;
00423 }
00424 
00425 static int cc_core_instance_cmp_fn(void *obj, void *arg, int flags)
00426 {
00427    struct cc_core_instance *core_instance1 = obj;
00428    struct cc_core_instance *core_instance2 = arg;
00429 
00430    return core_instance1->core_id == core_instance2->core_id ? CMP_MATCH | CMP_STOP : 0;
00431 }
00432 
00433 static struct cc_core_instance *find_cc_core_instance(const int core_id)
00434 {
00435    struct cc_core_instance finder = {.core_id = core_id,};
00436 
00437    return ao2_t_find(cc_core_instances, &finder, OBJ_POINTER, "Finding a core_instance");
00438 }
00439 
00440 struct cc_callback_helper {
00441    ao2_callback_fn *function;
00442    void *args;
00443    const char *type;
00444 };
00445 
00446 static int cc_agent_callback_helper(void *obj, void *args, int flags)
00447 {
00448    struct cc_core_instance *core_instance = obj;
00449    struct cc_callback_helper *helper = args;
00450 
00451    if (strcmp(core_instance->agent->callbacks->type, helper->type)) {
00452       return 0;
00453    }
00454 
00455    return helper->function(core_instance->agent, helper->args, flags);
00456 }
00457 
00458 struct ast_cc_agent *ast_cc_agent_callback(int flags, ao2_callback_fn *function, void *args, const char * const type)
00459 {
00460    struct cc_callback_helper helper = {.function = function, .args = args, .type = type};
00461    struct cc_core_instance *core_instance;
00462    if ((core_instance = ao2_t_callback(cc_core_instances, flags, cc_agent_callback_helper, &helper,
00463                "Calling provided agent callback function"))) {
00464       struct ast_cc_agent *agent = cc_ref(core_instance->agent, "An outside entity needs the agent");
00465       cc_unref(core_instance, "agent callback done with the core_instance");
00466       return agent;
00467    }
00468    return NULL;
00469 }
00470 
00471 enum match_flags {
00472    /* Only match agents that have not yet
00473     * made a CC request
00474     */
00475    MATCH_NO_REQUEST = (1 << 0),
00476    /* Only match agents that have made
00477     * a CC request
00478     */
00479    MATCH_REQUEST = (1 << 1),
00480 };
00481 
00482 /* ao2_callbacks for cc_core_instances */
00483 
00484 /*!
00485  * \internal
00486  * \brief find a core instance based on its agent
00487  *
00488  * The match flags tell whether we wish to find core instances
00489  * that have a monitor or core instances that do not. Core instances
00490  * with no monitor are core instances for which a caller has not yet
00491  * requested CC. Core instances with a monitor are ones for which the
00492  * caller has requested CC.
00493  */
00494 static int match_agent(void *obj, void *arg, void *data, int flags)
00495 {
00496    struct cc_core_instance *core_instance = obj;
00497    const char *name = arg;
00498    unsigned long match_flags = *(unsigned long *)data;
00499    int possible_match = 0;
00500 
00501    if ((match_flags & MATCH_NO_REQUEST) && core_instance->current_state < CC_CALLER_REQUESTED) {
00502       possible_match = 1;
00503    }
00504 
00505    if ((match_flags & MATCH_REQUEST) && core_instance->current_state >= CC_CALLER_REQUESTED) {
00506       possible_match = 1;
00507    }
00508 
00509    if (!possible_match) {
00510       return 0;
00511    }
00512 
00513    if (!strcmp(core_instance->agent->device_name, name)) {
00514       return CMP_MATCH | CMP_STOP;
00515    }
00516    return 0;
00517 }
00518 
00519 struct count_agents_cb_data {
00520    int count;
00521    int core_id_exception;
00522 };
00523 
00524 /*!
00525  * \internal
00526  * \brief Count the number of agents a specific interface is using
00527  *
00528  * We're only concerned with the number of agents that have requested
00529  * CC, so we restrict our search to core instances which have a non-NULL
00530  * monitor pointer
00531  */
00532 static int count_agents_cb(void *obj, void *arg, void *data, int flags)
00533 {
00534    struct cc_core_instance *core_instance = obj;
00535    const char *name = arg;
00536    struct count_agents_cb_data *cb_data = data;
00537 
00538    if (cb_data->core_id_exception == core_instance->core_id) {
00539       ast_log_dynamic_level(cc_logger_level, "Found agent with core_id %d but not counting it toward total\n", core_instance->core_id);
00540       return 0;
00541    }
00542 
00543    if (core_instance->current_state >= CC_CALLER_REQUESTED && !strcmp(core_instance->agent->device_name, name)) {
00544       cb_data->count++;
00545    }
00546    return 0;
00547 }
00548 
00549 /* default values mapping from cc_state to ast_dev_state */
00550 
00551 #define CC_AVAILABLE_DEVSTATE_DEFAULT        AST_DEVICE_NOT_INUSE
00552 #define CC_CALLER_OFFERED_DEVSTATE_DEFAULT   AST_DEVICE_NOT_INUSE
00553 #define CC_CALLER_REQUESTED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
00554 #define CC_ACTIVE_DEVSTATE_DEFAULT           AST_DEVICE_INUSE
00555 #define CC_CALLEE_READY_DEVSTATE_DEFAULT     AST_DEVICE_RINGING
00556 #define CC_CALLER_BUSY_DEVSTATE_DEFAULT      AST_DEVICE_ONHOLD
00557 #define CC_RECALLING_DEVSTATE_DEFAULT        AST_DEVICE_RINGING
00558 #define CC_COMPLETE_DEVSTATE_DEFAULT         AST_DEVICE_NOT_INUSE
00559 #define CC_FAILED_DEVSTATE_DEFAULT           AST_DEVICE_NOT_INUSE
00560 
00561 /*!
00562  * \internal
00563  * \brief initialization of defaults for CC_STATE to DEVICE_STATE map
00564  */
00565 static enum ast_device_state cc_state_to_devstate_map[] = {
00566    [CC_AVAILABLE] =        CC_AVAILABLE_DEVSTATE_DEFAULT,
00567    [CC_CALLER_OFFERED] =   CC_CALLER_OFFERED_DEVSTATE_DEFAULT,
00568    [CC_CALLER_REQUESTED] = CC_CALLER_REQUESTED_DEVSTATE_DEFAULT,
00569    [CC_ACTIVE] =           CC_ACTIVE_DEVSTATE_DEFAULT,
00570    [CC_CALLEE_READY] =     CC_CALLEE_READY_DEVSTATE_DEFAULT,
00571    [CC_CALLER_BUSY] =      CC_CALLER_BUSY_DEVSTATE_DEFAULT,
00572    [CC_RECALLING] =        CC_RECALLING_DEVSTATE_DEFAULT,
00573    [CC_COMPLETE] =         CC_COMPLETE_DEVSTATE_DEFAULT,
00574    [CC_FAILED] =           CC_FAILED_DEVSTATE_DEFAULT,
00575 };
00576 
00577 /*!
00578  * \internal
00579  * \brief lookup the ast_device_state mapped to cc_state
00580  *
00581  * \param state
00582  *
00583  * \return the correponding DEVICE STATE from the cc_state_to_devstate_map
00584  * when passed an internal state.
00585  */
00586 static enum ast_device_state cc_state_to_devstate(enum cc_state state)
00587 {
00588    return cc_state_to_devstate_map[state];
00589 }
00590 
00591 /*!
00592  * \internal
00593  * \brief Callback for devicestate providers
00594  *
00595  * \details
00596  * Initialize with ast_devstate_prov_add() and returns the corresponding
00597  * DEVICE STATE based on the current CC_STATE state machine if the requested
00598  * device is found and is a generic device. Returns the equivalent of
00599  * CC_FAILED, which defaults to NOT_INUSE, if no device is found.  NOT_INUSE would
00600  * indicate that there is no presence of any pending call back.
00601  */
00602 static enum ast_device_state ccss_device_state(const char *device_name)
00603 {
00604    struct cc_core_instance *core_instance;
00605    unsigned long match_flags;
00606    enum ast_device_state cc_current_state;
00607 
00608    match_flags = MATCH_NO_REQUEST;
00609    core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent,
00610       (char *) device_name, &match_flags,
00611       "Find Core Instance for ccss_device_state reqeust.");
00612    if (!core_instance) {
00613       ast_log_dynamic_level(cc_logger_level,
00614          "Couldn't find a core instance for caller %s\n", device_name);
00615       return cc_state_to_devstate(CC_FAILED);
00616    }
00617 
00618    ast_log_dynamic_level(cc_logger_level,
00619       "Core %d: Found core_instance for caller %s in state %s\n",
00620       core_instance->core_id, device_name, cc_state_to_string(core_instance->current_state));
00621 
00622    if (strcmp(core_instance->agent->callbacks->type, "generic")) {
00623       ast_log_dynamic_level(cc_logger_level,
00624          "Core %d: Device State is only for generic agent types.\n",
00625          core_instance->core_id);
00626       cc_unref(core_instance, "Unref core_instance since ccss_device_state was called with native agent");
00627       return cc_state_to_devstate(CC_FAILED);
00628    }
00629    cc_current_state = cc_state_to_devstate(core_instance->current_state);
00630    cc_unref(core_instance, "Unref core_instance done with ccss_device_state");
00631    return cc_current_state;
00632 }
00633 
00634 /*!
00635  * \internal
00636  * \brief Notify Device State Changes from CC STATE MACHINE
00637  *
00638  * \details
00639  * Any time a state is changed, we call this function to notify the DEVICE STATE
00640  * subsystem of the change so that subscribed phones to any corresponding hints that
00641  * are using that state are updated.
00642  */
00643 static void ccss_notify_device_state_change(const char *device, enum cc_state state)
00644 {
00645    enum ast_device_state devstate;
00646 
00647    devstate = cc_state_to_devstate(state);
00648 
00649    ast_log_dynamic_level(cc_logger_level,
00650       "Notification of CCSS state change to '%s', device state '%s' for device '%s'\n",
00651       cc_state_to_string(state), ast_devstate2str(devstate), device);
00652 
00653    ast_devstate_changed(devstate, AST_DEVSTATE_CACHABLE, "ccss:%s", device);
00654 }
00655 
00656 #define CC_OFFER_TIMER_DEFAULT         20    /* Seconds */
00657 #define CCNR_AVAILABLE_TIMER_DEFAULT   7200  /* Seconds */
00658 #define CCBS_AVAILABLE_TIMER_DEFAULT   4800  /* Seconds */
00659 #define CC_RECALL_TIMER_DEFAULT        20    /* Seconds */
00660 #define CC_MAX_AGENTS_DEFAULT       5
00661 #define CC_MAX_MONITORS_DEFAULT        5
00662 #define GLOBAL_CC_MAX_REQUESTS_DEFAULT 20
00663 
00664 static const struct ast_cc_config_params cc_default_params = {
00665    .cc_agent_policy = AST_CC_AGENT_NEVER,
00666    .cc_monitor_policy = AST_CC_MONITOR_NEVER,
00667    .cc_offer_timer = CC_OFFER_TIMER_DEFAULT,
00668    .ccnr_available_timer = CCNR_AVAILABLE_TIMER_DEFAULT,
00669    .ccbs_available_timer = CCBS_AVAILABLE_TIMER_DEFAULT,
00670    .cc_recall_timer = CC_RECALL_TIMER_DEFAULT,
00671    .cc_max_agents = CC_MAX_AGENTS_DEFAULT,
00672    .cc_max_monitors = CC_MAX_MONITORS_DEFAULT,
00673    .cc_callback_macro = "",
00674    .cc_callback_sub = "",
00675    .cc_agent_dialstring = "",
00676 };
00677 
00678 void ast_cc_default_config_params(struct ast_cc_config_params *params)
00679 {
00680    *params = cc_default_params;
00681 }
00682 
00683 struct ast_cc_config_params *__ast_cc_config_params_init(const char *file, int line, const char *function)
00684 {
00685 #if defined(__AST_DEBUG_MALLOC)
00686    struct ast_cc_config_params *params = __ast_malloc(sizeof(*params), file, line, function);
00687 #else
00688    struct ast_cc_config_params *params = ast_malloc(sizeof(*params));
00689 #endif
00690 
00691    if (!params) {
00692       return NULL;
00693    }
00694 
00695    ast_cc_default_config_params(params);
00696    return params;
00697 }
00698 
00699 void ast_cc_config_params_destroy(struct ast_cc_config_params *params)
00700 {
00701    ast_free(params);
00702 }
00703 
00704 static enum ast_cc_agent_policies str_to_agent_policy(const char * const value)
00705 {
00706    if (!strcasecmp(value, "never")) {
00707       return AST_CC_AGENT_NEVER;
00708    } else if (!strcasecmp(value, "native")) {
00709       return AST_CC_AGENT_NATIVE;
00710    } else if (!strcasecmp(value, "generic")) {
00711       return AST_CC_AGENT_GENERIC;
00712    } else {
00713       ast_log(LOG_WARNING, "%s is an invalid value for cc_agent_policy. Switching to 'never'\n", value);
00714       return AST_CC_AGENT_NEVER;
00715    }
00716 }
00717 
00718 static enum ast_cc_monitor_policies str_to_monitor_policy(const char * const value)
00719 {
00720    if (!strcasecmp(value, "never")) {
00721       return AST_CC_MONITOR_NEVER;
00722    } else if (!strcasecmp(value, "native")) {
00723       return AST_CC_MONITOR_NATIVE;
00724    } else if (!strcasecmp(value, "generic")) {
00725       return AST_CC_MONITOR_GENERIC;
00726    } else if (!strcasecmp(value, "always")) {
00727       return AST_CC_MONITOR_ALWAYS;
00728    } else {
00729       ast_log(LOG_WARNING, "%s is an invalid value for cc_monitor_policy. Switching to 'never'\n", value);
00730       return AST_CC_MONITOR_NEVER;
00731    }
00732 }
00733 
00734 static const char *agent_policy_to_str(enum ast_cc_agent_policies policy)
00735 {
00736    switch (policy) {
00737    case AST_CC_AGENT_NEVER:
00738       return "never";
00739    case AST_CC_AGENT_NATIVE:
00740       return "native";
00741    case AST_CC_AGENT_GENERIC:
00742       return "generic";
00743    default:
00744       /* This should never happen... */
00745       return "";
00746    }
00747 }
00748 
00749 static const char *monitor_policy_to_str(enum ast_cc_monitor_policies policy)
00750 {
00751    switch (policy) {
00752    case AST_CC_MONITOR_NEVER:
00753       return "never";
00754    case AST_CC_MONITOR_NATIVE:
00755       return "native";
00756    case AST_CC_MONITOR_GENERIC:
00757       return "generic";
00758    case AST_CC_MONITOR_ALWAYS:
00759       return "always";
00760    default:
00761       /* This should never happen... */
00762       return "";
00763    }
00764 }
00765 int ast_cc_get_param(struct ast_cc_config_params *params, const char * const name,
00766       char *buf, size_t buf_len)
00767 {
00768    const char *value = NULL;
00769 
00770    if (!strcasecmp(name, "cc_callback_macro")) {
00771       value = ast_get_cc_callback_macro(params);
00772    } else if (!strcasecmp(name, "cc_callback_sub")) {
00773       value = ast_get_cc_callback_sub(params);
00774    } else if (!strcasecmp(name, "cc_agent_policy")) {
00775       value = agent_policy_to_str(ast_get_cc_agent_policy(params));
00776    } else if (!strcasecmp(name, "cc_monitor_policy")) {
00777       value = monitor_policy_to_str(ast_get_cc_monitor_policy(params));
00778    } else if (!strcasecmp(name, "cc_agent_dialstring")) {
00779       value = ast_get_cc_agent_dialstring(params);
00780    }
00781    if (value) {
00782       ast_copy_string(buf, value, buf_len);
00783       return 0;
00784    }
00785 
00786    /* The rest of these are all ints of some sort and require some
00787     * snprintf-itude
00788     */
00789 
00790    if (!strcasecmp(name, "cc_offer_timer")) {
00791       snprintf(buf, buf_len, "%u", ast_get_cc_offer_timer(params));
00792    } else if (!strcasecmp(name, "ccnr_available_timer")) {
00793       snprintf(buf, buf_len, "%u", ast_get_ccnr_available_timer(params));
00794    } else if (!strcasecmp(name, "ccbs_available_timer")) {
00795       snprintf(buf, buf_len, "%u", ast_get_ccbs_available_timer(params));
00796    } else if (!strcasecmp(name, "cc_max_agents")) {
00797       snprintf(buf, buf_len, "%u", ast_get_cc_max_agents(params));
00798    } else if (!strcasecmp(name, "cc_max_monitors")) {
00799       snprintf(buf, buf_len, "%u", ast_get_cc_max_monitors(params));
00800    } else if (!strcasecmp(name, "cc_recall_timer")) {
00801       snprintf(buf, buf_len, "%u", ast_get_cc_recall_timer(params));
00802    } else {
00803       ast_log(LOG_WARNING, "%s is not a valid CC parameter. Ignoring.\n", name);
00804       return -1;
00805    }
00806 
00807    return 0;
00808 }
00809 
00810 int ast_cc_set_param(struct ast_cc_config_params *params, const char * const name,
00811       const char * const value)
00812 {
00813    unsigned int value_as_uint;
00814    if (!strcasecmp(name, "cc_agent_policy")) {
00815       return ast_set_cc_agent_policy(params, str_to_agent_policy(value));
00816    } else if (!strcasecmp(name, "cc_monitor_policy")) {
00817       return ast_set_cc_monitor_policy(params, str_to_monitor_policy(value));
00818    } else if (!strcasecmp(name, "cc_agent_dialstring")) {
00819       ast_set_cc_agent_dialstring(params, value);
00820    } else if (!strcasecmp(name, "cc_callback_macro")) {
00821       ast_set_cc_callback_macro(params, value);
00822       return 0;
00823    } else if (!strcasecmp(name, "cc_callback_sub")) {
00824       ast_set_cc_callback_sub(params, value);
00825       return 0;
00826    }
00827 
00828    if (sscanf(value, "%30u", &value_as_uint) != 1) {
00829       return -1;
00830    }
00831 
00832    if (!strcasecmp(name, "cc_offer_timer")) {
00833       ast_set_cc_offer_timer(params, value_as_uint);
00834    } else if (!strcasecmp(name, "ccnr_available_timer")) {
00835       ast_set_ccnr_available_timer(params, value_as_uint);
00836    } else if (!strcasecmp(name, "ccbs_available_timer")) {
00837       ast_set_ccbs_available_timer(params, value_as_uint);
00838    } else if (!strcasecmp(name, "cc_max_agents")) {
00839       ast_set_cc_max_agents(params, value_as_uint);
00840    } else if (!strcasecmp(name, "cc_max_monitors")) {
00841       ast_set_cc_max_monitors(params, value_as_uint);
00842    } else if (!strcasecmp(name, "cc_recall_timer")) {
00843       ast_set_cc_recall_timer(params, value_as_uint);
00844    } else {
00845       ast_log(LOG_WARNING, "%s is not a valid CC parameter. Ignoring.\n", name);
00846       return -1;
00847    }
00848 
00849    return 0;
00850 }
00851 
00852 int ast_cc_is_config_param(const char * const name)
00853 {
00854    return (!strcasecmp(name, "cc_agent_policy") ||
00855             !strcasecmp(name, "cc_monitor_policy") ||
00856             !strcasecmp(name, "cc_offer_timer") ||
00857             !strcasecmp(name, "ccnr_available_timer") ||
00858             !strcasecmp(name, "ccbs_available_timer") ||
00859             !strcasecmp(name, "cc_max_agents") ||
00860             !strcasecmp(name, "cc_max_monitors") ||
00861             !strcasecmp(name, "cc_callback_macro") ||
00862             !strcasecmp(name, "cc_callback_sub") ||
00863             !strcasecmp(name, "cc_agent_dialstring") ||
00864             !strcasecmp(name, "cc_recall_timer"));
00865 }
00866 
00867 void ast_cc_copy_config_params(struct ast_cc_config_params *dest, const struct ast_cc_config_params *src)
00868 {
00869    *dest = *src;
00870 }
00871 
00872 enum ast_cc_agent_policies ast_get_cc_agent_policy(struct ast_cc_config_params *config)
00873 {
00874    return config->cc_agent_policy;
00875 }
00876 
00877 int ast_set_cc_agent_policy(struct ast_cc_config_params *config, enum ast_cc_agent_policies value)
00878 {
00879    /* Screw C and its weak type checking for making me have to do this
00880     * validation at runtime.
00881     */
00882    if (value < AST_CC_AGENT_NEVER || value > AST_CC_AGENT_GENERIC) {
00883       return -1;
00884    }
00885    config->cc_agent_policy = value;
00886    return 0;
00887 }
00888 
00889 enum ast_cc_monitor_policies ast_get_cc_monitor_policy(struct ast_cc_config_params *config)
00890 {
00891    return config->cc_monitor_policy;
00892 }
00893 
00894 int ast_set_cc_monitor_policy(struct ast_cc_config_params *config, enum ast_cc_monitor_policies value)
00895 {
00896    /* Screw C and its weak type checking for making me have to do this
00897     * validation at runtime.
00898     */
00899    if (value < AST_CC_MONITOR_NEVER || value > AST_CC_MONITOR_ALWAYS) {
00900       return -1;
00901    }
00902    config->cc_monitor_policy = value;
00903    return 0;
00904 }
00905 
00906 unsigned int ast_get_cc_offer_timer(struct ast_cc_config_params *config)
00907 {
00908    return config->cc_offer_timer;
00909 }
00910 
00911 void ast_set_cc_offer_timer(struct ast_cc_config_params *config, unsigned int value)
00912 {
00913    /* 0 is an unreasonable value for any timer. Stick with the default */
00914    if (value == 0) {
00915       ast_log(LOG_WARNING, "0 is an invalid value for cc_offer_timer. Retaining value as %u\n", config->cc_offer_timer);
00916       return;
00917    }
00918    config->cc_offer_timer = value;
00919 }
00920 
00921 unsigned int ast_get_ccnr_available_timer(struct ast_cc_config_params *config)
00922 {
00923    return config->ccnr_available_timer;
00924 }
00925 
00926 void ast_set_ccnr_available_timer(struct ast_cc_config_params *config, unsigned int value)
00927 {
00928    /* 0 is an unreasonable value for any timer. Stick with the default */
00929    if (value == 0) {
00930       ast_log(LOG_WARNING, "0 is an invalid value for ccnr_available_timer. Retaining value as %u\n", config->ccnr_available_timer);
00931       return;
00932    }
00933    config->ccnr_available_timer = value;
00934 }
00935 
00936 unsigned int ast_get_cc_recall_timer(struct ast_cc_config_params *config)
00937 {
00938    return config->cc_recall_timer;
00939 }
00940 
00941 void ast_set_cc_recall_timer(struct ast_cc_config_params *config, unsigned int value)
00942 {
00943    /* 0 is an unreasonable value for any timer. Stick with the default */
00944    if (value == 0) {
00945       ast_log(LOG_WARNING, "0 is an invalid value for ccnr_available_timer. Retaining value as %u\n", config->cc_recall_timer);
00946       return;
00947    }
00948    config->cc_recall_timer = value;
00949 }
00950 
00951 unsigned int ast_get_ccbs_available_timer(struct ast_cc_config_params *config)
00952 {
00953    return config->ccbs_available_timer;
00954 }
00955 
00956 void ast_set_ccbs_available_timer(struct ast_cc_config_params *config, unsigned int value)
00957 {
00958    /* 0 is an unreasonable value for any timer. Stick with the default */
00959    if (value == 0) {
00960       ast_log(LOG_WARNING, "0 is an invalid value for ccbs_available_timer. Retaining value as %u\n", config->ccbs_available_timer);
00961       return;
00962    }
00963    config->ccbs_available_timer = value;
00964 }
00965 
00966 const char *ast_get_cc_agent_dialstring(struct ast_cc_config_params *config)
00967 {
00968    return config->cc_agent_dialstring;
00969 }
00970 
00971 void ast_set_cc_agent_dialstring(struct ast_cc_config_params *config, const char *const value)
00972 {
00973    if (ast_strlen_zero(value)) {
00974       config->cc_agent_dialstring[0] = '\0';
00975    } else {
00976       ast_copy_string(config->cc_agent_dialstring, value, sizeof(config->cc_agent_dialstring));
00977    }
00978 }
00979 
00980 unsigned int ast_get_cc_max_agents(struct ast_cc_config_params *config)
00981 {
00982    return config->cc_max_agents;
00983 }
00984 
00985 void ast_set_cc_max_agents(struct ast_cc_config_params *config, unsigned int value)
00986 {
00987    config->cc_max_agents = value;
00988 }
00989 
00990 unsigned int ast_get_cc_max_monitors(struct ast_cc_config_params *config)
00991 {
00992    return config->cc_max_monitors;
00993 }
00994 
00995 void ast_set_cc_max_monitors(struct ast_cc_config_params *config, unsigned int value)
00996 {
00997    config->cc_max_monitors = value;
00998 }
00999 
01000 const char *ast_get_cc_callback_macro(struct ast_cc_config_params *config)
01001 {
01002    return config->cc_callback_macro;
01003 }
01004 
01005 const char *ast_get_cc_callback_sub(struct ast_cc_config_params *config)
01006 {
01007    return config->cc_callback_sub;
01008 }
01009 
01010 void ast_set_cc_callback_macro(struct ast_cc_config_params *config, const char * const value)
01011 {
01012    ast_log(LOG_WARNING, "Usage of cc_callback_macro is deprecated.  Please use cc_callback_sub instead.\n");
01013    if (ast_strlen_zero(value)) {
01014       config->cc_callback_macro[0] = '\0';
01015    } else {
01016       ast_copy_string(config->cc_callback_macro, value, sizeof(config->cc_callback_macro));
01017    }
01018 }
01019 
01020 void ast_set_cc_callback_sub(struct ast_cc_config_params *config, const char * const value)
01021 {
01022    if (ast_strlen_zero(value)) {
01023       config->cc_callback_sub[0] = '\0';
01024    } else {
01025       ast_copy_string(config->cc_callback_sub, value, sizeof(config->cc_callback_sub));
01026    }
01027 }
01028 
01029 static int cc_publish(struct stasis_message_type *message_type, int core_id, struct ast_json *extras)
01030 {
01031    RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
01032    RAII_VAR(struct ast_json_payload *, payload, NULL, ao2_cleanup);
01033    RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
01034 
01035    if (!message_type) {
01036       return -1;
01037    }
01038 
01039    blob = ast_json_pack("{s: i}",
01040       "core_id", core_id);
01041    if (!blob) {
01042       return -1;
01043    }
01044 
01045    if (extras) {
01046       ast_json_object_update(blob, extras);
01047    }
01048 
01049    if (!(payload = ast_json_payload_create(blob))) {
01050       return -1;
01051    }
01052 
01053    if (!(message = stasis_message_create(message_type, payload))) {
01054       return -1;
01055    }
01056 
01057    stasis_publish(ast_system_topic(), message);
01058 
01059    return 0;
01060 }
01061 
01062 static void cc_publish_available(int core_id, const char *callee, const char *service)
01063 {
01064    RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
01065 
01066    extras = ast_json_pack("{s: s, s: s}",
01067       "callee", callee,
01068       "service", service);
01069 
01070    cc_publish(ast_cc_available_type(), core_id, extras);
01071 }
01072 
01073 static void cc_publish_offertimerstart(int core_id, const char *caller, unsigned int expires)
01074 {
01075    RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
01076 
01077    extras = ast_json_pack("{s: s, s: i}",
01078       "caller", caller,
01079       "expires", expires);
01080 
01081    cc_publish(ast_cc_offertimerstart_type(), core_id, extras);
01082 }
01083 
01084 static void cc_publish_requested(int core_id, const char *caller, const char *callee)
01085 {
01086    RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
01087 
01088    extras = ast_json_pack("{s: s, s: s}",
01089       "caller", caller,
01090       "callee", callee);
01091 
01092    cc_publish(ast_cc_requested_type(), core_id, extras);
01093 }
01094 
01095 static void cc_publish_requestacknowledged(int core_id, const char *caller)
01096 {
01097    RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
01098 
01099    extras = ast_json_pack("{s: s}",
01100       "caller", caller);
01101 
01102    cc_publish(ast_cc_requestacknowledged_type(), core_id, extras);
01103 }
01104 
01105 static void cc_publish_callerstopmonitoring(int core_id, const char *caller)
01106 {
01107    RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
01108 
01109    extras = ast_json_pack("{s: s}",
01110       "caller", caller);
01111 
01112    cc_publish(ast_cc_callerstopmonitoring_type(), core_id, extras);
01113 }
01114 
01115 static void cc_publish_callerstartmonitoring(int core_id, const char *caller)
01116 {
01117    RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
01118 
01119    extras = ast_json_pack("{s: s}",
01120       "caller", caller);
01121 
01122    cc_publish(ast_cc_callerstartmonitoring_type(), core_id, extras);
01123 }
01124 
01125 static void cc_publish_callerrecalling(int core_id, const char *caller)
01126 {
01127    RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
01128 
01129    extras = ast_json_pack("{s: s}",
01130       "caller", caller);
01131 
01132    cc_publish(ast_cc_callerrecalling_type(), core_id, extras);
01133 }
01134 
01135 static void cc_publish_recallcomplete(int core_id, const char *caller)
01136 {
01137    RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
01138 
01139    extras = ast_json_pack("{s: s}",
01140       "caller", caller);
01141 
01142    cc_publish(ast_cc_recallcomplete_type(), core_id, extras);
01143 }
01144 
01145 static void cc_publish_failure(int core_id, const char *caller, const char *reason)
01146 {
01147    RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
01148 
01149    extras = ast_json_pack("{s: s, s: s}",
01150       "caller", caller,
01151       "reason", reason);
01152 
01153    cc_publish(ast_cc_failure_type(), core_id, extras);
01154 }
01155 
01156 static void cc_publish_monitorfailed(int core_id, const char *callee)
01157 {
01158    RAII_VAR(struct ast_json *, extras, NULL, ast_json_unref);
01159 
01160    extras = ast_json_pack("{s: s}",
01161       "callee", callee);
01162 
01163    cc_publish(ast_cc_monitorfailed_type(), core_id, extras);
01164 }
01165 
01166 struct cc_monitor_backend {
01167    AST_LIST_ENTRY(cc_monitor_backend) next;
01168    const struct ast_cc_monitor_callbacks *callbacks;
01169 };
01170 
01171 AST_RWLIST_HEAD_STATIC(cc_monitor_backends, cc_monitor_backend);
01172 
01173 int ast_cc_monitor_register(const struct ast_cc_monitor_callbacks *callbacks)
01174 {
01175    struct cc_monitor_backend *backend = ast_calloc(1, sizeof(*backend));
01176 
01177    if (!backend) {
01178       return -1;
01179    }
01180 
01181    backend->callbacks = callbacks;
01182 
01183    AST_RWLIST_WRLOCK(&cc_monitor_backends);
01184    AST_RWLIST_INSERT_TAIL(&cc_monitor_backends, backend, next);
01185    AST_RWLIST_UNLOCK(&cc_monitor_backends);
01186    return 0;
01187 }
01188 
01189 static const struct ast_cc_monitor_callbacks *find_monitor_callbacks(const char * const type)
01190 {
01191    struct cc_monitor_backend *backend;
01192    const struct ast_cc_monitor_callbacks *callbacks = NULL;
01193 
01194    AST_RWLIST_RDLOCK(&cc_monitor_backends);
01195    AST_RWLIST_TRAVERSE(&cc_monitor_backends, backend, next) {
01196       if (!strcmp(backend->callbacks->type, type)) {
01197          ast_log_dynamic_level(cc_logger_level, "Returning monitor backend %s\n", backend->callbacks->type);
01198          callbacks = backend->callbacks;
01199          break;
01200       }
01201    }
01202    AST_RWLIST_UNLOCK(&cc_monitor_backends);
01203    return callbacks;
01204 }
01205 
01206 void ast_cc_monitor_unregister(const struct ast_cc_monitor_callbacks *callbacks)
01207 {
01208    struct cc_monitor_backend *backend;
01209    AST_RWLIST_WRLOCK(&cc_monitor_backends);
01210    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&cc_monitor_backends, backend, next) {
01211       if (backend->callbacks == callbacks) {
01212          AST_RWLIST_REMOVE_CURRENT(next);
01213          ast_free(backend);
01214          break;
01215       }
01216    }
01217    AST_RWLIST_TRAVERSE_SAFE_END;
01218    AST_RWLIST_UNLOCK(&cc_monitor_backends);
01219 }
01220 
01221 struct cc_agent_backend {
01222    AST_LIST_ENTRY(cc_agent_backend) next;
01223    const struct ast_cc_agent_callbacks *callbacks;
01224 };
01225 
01226 AST_RWLIST_HEAD_STATIC(cc_agent_backends, cc_agent_backend);
01227 
01228 int ast_cc_agent_register(const struct ast_cc_agent_callbacks *callbacks)
01229 {
01230    struct cc_agent_backend *backend = ast_calloc(1, sizeof(*backend));
01231 
01232    if (!backend) {
01233       return -1;
01234    }
01235 
01236    backend->callbacks = callbacks;
01237    AST_RWLIST_WRLOCK(&cc_agent_backends);
01238    AST_RWLIST_INSERT_TAIL(&cc_agent_backends, backend, next);
01239    AST_RWLIST_UNLOCK(&cc_agent_backends);
01240    return 0;
01241 }
01242 
01243 void ast_cc_agent_unregister(const struct ast_cc_agent_callbacks *callbacks)
01244 {
01245    struct cc_agent_backend *backend;
01246    AST_RWLIST_WRLOCK(&cc_agent_backends);
01247    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&cc_agent_backends, backend, next) {
01248       if (backend->callbacks == callbacks) {
01249          AST_RWLIST_REMOVE_CURRENT(next);
01250          ast_free(backend);
01251          break;
01252       }
01253    }
01254    AST_RWLIST_TRAVERSE_SAFE_END;
01255    AST_RWLIST_UNLOCK(&cc_agent_backends);
01256 }
01257 
01258 static const struct ast_cc_agent_callbacks *find_agent_callbacks(struct ast_channel *chan)
01259 {
01260    struct cc_agent_backend *backend;
01261    const struct ast_cc_agent_callbacks *callbacks = NULL;
01262    struct ast_cc_config_params *cc_params;
01263    char type[32];
01264 
01265    cc_params = ast_channel_get_cc_config_params(chan);
01266    if (!cc_params) {
01267       return NULL;
01268    }
01269    switch (ast_get_cc_agent_policy(cc_params)) {
01270    case AST_CC_AGENT_GENERIC:
01271       ast_copy_string(type, "generic", sizeof(type));
01272       break;
01273    case AST_CC_AGENT_NATIVE:
01274       ast_channel_get_cc_agent_type(chan, type, sizeof(type));
01275       break;
01276    default:
01277       ast_log_dynamic_level(cc_logger_level, "Not returning agent callbacks since this channel is configured not to have a CC agent\n");
01278       return NULL;
01279    }
01280 
01281    AST_RWLIST_RDLOCK(&cc_agent_backends);
01282    AST_RWLIST_TRAVERSE(&cc_agent_backends, backend, next) {
01283       if (!strcmp(backend->callbacks->type, type)) {
01284          ast_log_dynamic_level(cc_logger_level, "Returning agent backend %s\n", backend->callbacks->type);
01285          callbacks = backend->callbacks;
01286          break;
01287       }
01288    }
01289    AST_RWLIST_UNLOCK(&cc_agent_backends);
01290    return callbacks;
01291 }
01292 
01293 /*!
01294  * \internal
01295  * \brief Determine if the given device state is considered available by generic CCSS.
01296  * \since 1.8
01297  *
01298  * \param state Device state to test.
01299  *
01300  * \return TRUE if the given device state is considered available by generic CCSS.
01301  */
01302 static int cc_generic_is_device_available(enum ast_device_state state)
01303 {
01304    return state == AST_DEVICE_NOT_INUSE || state == AST_DEVICE_UNKNOWN;
01305 }
01306 
01307 static int cc_generic_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id);
01308 static int cc_generic_monitor_suspend(struct ast_cc_monitor *monitor);
01309 static int cc_generic_monitor_unsuspend(struct ast_cc_monitor *monitor);
01310 static int cc_generic_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id);
01311 static void cc_generic_monitor_destructor(void *private_data);
01312 
01313 static struct ast_cc_monitor_callbacks generic_monitor_cbs = {
01314    .type = "generic",
01315    .request_cc = cc_generic_monitor_request_cc,
01316    .suspend = cc_generic_monitor_suspend,
01317    .unsuspend = cc_generic_monitor_unsuspend,
01318    .cancel_available_timer = cc_generic_monitor_cancel_available_timer,
01319    .destructor = cc_generic_monitor_destructor,
01320 };
01321 
01322 struct ao2_container *generic_monitors;
01323 
01324 struct generic_monitor_instance {
01325    int core_id;
01326    int is_suspended;
01327    int monitoring;
01328    AST_LIST_ENTRY(generic_monitor_instance) next;
01329 };
01330 
01331 struct generic_monitor_instance_list {
01332    const char *device_name;
01333    enum ast_device_state current_state;
01334    /* If there are multiple instances monitoring the
01335     * same device and one should fail, we need to know
01336     * whether to signal that the device can be recalled.
01337     * The problem is that the device state is not enough
01338     * to check. If a caller has requested CCNR, then the
01339     * fact that the device is available does not indicate
01340     * that the device is ready to be recalled. Instead, as
01341     * soon as one instance of the monitor becomes available
01342     * for a recall, we mark the entire list as being fit
01343     * for recall. If a CCNR request comes in, then we will
01344     * have to mark the list as unfit for recall since this
01345     * is a clear indicator that the person at the monitored
01346     * device has gone away and is actuall not fit to be
01347     * recalled
01348     */
01349    int fit_for_recall;
01350    struct stasis_subscription *sub;
01351    AST_LIST_HEAD_NOLOCK(, generic_monitor_instance) list;
01352 };
01353 
01354 /*!
01355  * \brief private data for generic device monitor
01356  */
01357 struct generic_monitor_pvt {
01358    /*!
01359     * We need the device name during destruction so we
01360     * can find the appropriate item to destroy.
01361     */
01362    const char *device_name;
01363    /*!
01364     * We need the core ID for similar reasons. Once we
01365     * find the appropriate item in our ao2_container, we
01366     * need to remove the appropriate cc_monitor from the
01367     * list of monitors.
01368     */
01369    int core_id;
01370 };
01371 
01372 static int generic_monitor_hash_fn(const void *obj, const int flags)
01373 {
01374    const struct generic_monitor_instance_list *generic_list = obj;
01375    return ast_str_hash(generic_list->device_name);
01376 }
01377 
01378 static int generic_monitor_cmp_fn(void *obj, void *arg, int flags)
01379 {
01380    const struct generic_monitor_instance_list *generic_list1 = obj;
01381    const struct generic_monitor_instance_list *generic_list2 = arg;
01382 
01383    return !strcmp(generic_list1->device_name, generic_list2->device_name) ? CMP_MATCH | CMP_STOP : 0;
01384 }
01385 
01386 static struct generic_monitor_instance_list *find_generic_monitor_instance_list(const char * const device_name)
01387 {
01388    struct generic_monitor_instance_list finder = {0};
01389    char *uppertech = ast_strdupa(device_name);
01390    ast_tech_to_upper(uppertech);
01391    finder.device_name = uppertech;
01392 
01393    return ao2_t_find(generic_monitors, &finder, OBJ_POINTER, "Finding generic monitor instance list");
01394 }
01395 
01396 static void generic_monitor_instance_list_destructor(void *obj)
01397 {
01398    struct generic_monitor_instance_list *generic_list = obj;
01399    struct generic_monitor_instance *generic_instance;
01400 
01401    generic_list->sub = stasis_unsubscribe(generic_list->sub);
01402    while ((generic_instance = AST_LIST_REMOVE_HEAD(&generic_list->list, next))) {
01403       ast_free(generic_instance);
01404    }
01405    ast_free((char *)generic_list->device_name);
01406 }
01407 
01408 static void generic_monitor_devstate_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg);
01409 static struct generic_monitor_instance_list *create_new_generic_list(struct ast_cc_monitor *monitor)
01410 {
01411    struct generic_monitor_instance_list *generic_list = ao2_t_alloc(sizeof(*generic_list),
01412          generic_monitor_instance_list_destructor, "allocate generic monitor instance list");
01413    char * device_name;
01414    struct stasis_topic *device_specific_topic;
01415 
01416    if (!generic_list) {
01417       return NULL;
01418    }
01419 
01420    if (!(device_name = ast_strdup(monitor->interface->device_name))) {
01421       cc_unref(generic_list, "Failed to strdup the monitor's device name");
01422       return NULL;
01423    }
01424    ast_tech_to_upper(device_name);
01425    generic_list->device_name = device_name;
01426 
01427    device_specific_topic = ast_device_state_topic(device_name);
01428    if (!device_specific_topic) {
01429       return NULL;
01430    }
01431 
01432    if (!(generic_list->sub = stasis_subscribe(device_specific_topic, generic_monitor_devstate_cb, NULL))) {
01433       cc_unref(generic_list, "Failed to subscribe to device state");
01434       return NULL;
01435    }
01436    generic_list->current_state = ast_device_state(monitor->interface->device_name);
01437    ao2_t_link(generic_monitors, generic_list, "linking new generic monitor instance list");
01438    return generic_list;
01439 }
01440 
01441 static int generic_monitor_devstate_tp_cb(void *data)
01442 {
01443    RAII_VAR(struct ast_device_state_message *, dev_state, data, ao2_cleanup);
01444    enum ast_device_state new_state = dev_state->state;
01445    enum ast_device_state previous_state;
01446    struct generic_monitor_instance_list *generic_list;
01447    struct generic_monitor_instance *generic_instance;
01448 
01449    if (!(generic_list = find_generic_monitor_instance_list(dev_state->device))) {
01450       /* The most likely cause for this is that we destroyed the monitor in the
01451        * time between subscribing to its device state and the time this executes.
01452        * Not really a big deal.
01453        */
01454       return 0;
01455    }
01456 
01457    if (generic_list->current_state == new_state) {
01458       /* The device state hasn't actually changed, so we don't really care */
01459       cc_unref(generic_list, "Kill reference of generic list in devstate taskprocessor callback");
01460       return 0;
01461    }
01462 
01463    previous_state = generic_list->current_state;
01464    generic_list->current_state = new_state;
01465 
01466    if (cc_generic_is_device_available(new_state) &&
01467          (previous_state == AST_DEVICE_INUSE || previous_state == AST_DEVICE_UNAVAILABLE ||
01468           previous_state == AST_DEVICE_BUSY)) {
01469       AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
01470          if (!generic_instance->is_suspended && generic_instance->monitoring) {
01471             generic_instance->monitoring = 0;
01472             generic_list->fit_for_recall = 1;
01473             ast_cc_monitor_callee_available(generic_instance->core_id, "Generic monitored party has become available");
01474             break;
01475          }
01476       }
01477    }
01478    cc_unref(generic_list, "Kill reference of generic list in devstate taskprocessor callback");
01479    return 0;
01480 }
01481 
01482 static void generic_monitor_devstate_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
01483 {
01484    /* Wow, it's cool that we've picked up on a state change, but we really want
01485     * the actual work to be done in the core's taskprocessor execution thread
01486     * so that all monitor operations can be serialized. Locks?! We don't need
01487     * no steenkin' locks!
01488     */
01489    struct ast_device_state_message *dev_state;
01490    if (ast_device_state_message_type() != stasis_message_type(msg)) {
01491       return;
01492    }
01493 
01494    dev_state = stasis_message_data(msg);
01495    if (dev_state->eid) {
01496       /* ignore non-aggregate states */
01497       return;
01498    }
01499 
01500    ao2_t_ref(dev_state, +1, "Bumping dev_state ref for cc_core_taskprocessor");
01501    if (ast_taskprocessor_push(cc_core_taskprocessor, generic_monitor_devstate_tp_cb, dev_state)) {
01502       ao2_cleanup(dev_state);
01503       return;
01504    }
01505 }
01506 
01507 int ast_cc_available_timer_expire(const void *data)
01508 {
01509    struct ast_cc_monitor *monitor = (struct ast_cc_monitor *) data;
01510    int res;
01511    monitor->available_timer_id = -1;
01512    res = ast_cc_monitor_failed(monitor->core_id, monitor->interface->device_name, "Available timer expired for monitor");
01513    cc_unref(monitor, "Unref reference from scheduler\n");
01514    return res;
01515 }
01516 
01517 static int cc_generic_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id)
01518 {
01519    struct generic_monitor_instance_list *generic_list;
01520    struct generic_monitor_instance *generic_instance;
01521    struct generic_monitor_pvt *gen_mon_pvt;
01522    enum ast_cc_service_type service = monitor->service_offered;
01523    int when;
01524 
01525    /* First things first. Native channel drivers will have their private data allocated
01526     * at the time that they tell the core that they can offer CC. Generic is quite a bit
01527     * different, and we wait until this point to allocate our private data.
01528     */
01529    if (!(gen_mon_pvt = ast_calloc(1, sizeof(*gen_mon_pvt)))) {
01530       return -1;
01531    }
01532 
01533    if (!(gen_mon_pvt->device_name = ast_strdup(monitor->interface->device_name))) {
01534       ast_free(gen_mon_pvt);
01535       return -1;
01536    }
01537 
01538    gen_mon_pvt->core_id = monitor->core_id;
01539 
01540    monitor->private_data = gen_mon_pvt;
01541 
01542    if (!(generic_list = find_generic_monitor_instance_list(monitor->interface->device_name))) {
01543       if (!(generic_list = create_new_generic_list(monitor))) {
01544          return -1;
01545       }
01546    }
01547 
01548    if (!(generic_instance = ast_calloc(1, sizeof(*generic_instance)))) {
01549       /* The generic monitor destructor will take care of the appropriate
01550        * deallocations
01551        */
01552       cc_unref(generic_list, "Generic monitor instance failed to allocate");
01553       return -1;
01554    }
01555    generic_instance->core_id = monitor->core_id;
01556    generic_instance->monitoring = 1;
01557    AST_LIST_INSERT_TAIL(&generic_list->list, generic_instance, next);
01558    when = service == AST_CC_CCBS ? ast_get_ccbs_available_timer(monitor->interface->config_params) :
01559       ast_get_ccnr_available_timer(monitor->interface->config_params);
01560 
01561    *available_timer_id = ast_sched_add(cc_sched_context, when * 1000,
01562          ast_cc_available_timer_expire, cc_ref(monitor, "Give the scheduler a monitor reference"));
01563    if (*available_timer_id == -1) {
01564       cc_unref(monitor, "Failed to schedule available timer. (monitor)");
01565       cc_unref(generic_list, "Failed to schedule available timer. (generic_list)");
01566       return -1;
01567    }
01568    /* If the new instance was created as CCNR, then that means this device is not currently
01569     * fit for recall even if it previously was.
01570     */
01571    if (service == AST_CC_CCNR || service == AST_CC_CCNL) {
01572       generic_list->fit_for_recall = 0;
01573    }
01574    ast_cc_monitor_request_acked(monitor->core_id, "Generic monitor for %s subscribed to device state.",
01575          monitor->interface->device_name);
01576    cc_unref(generic_list, "Finished with monitor instance reference in request cc callback");
01577    return 0;
01578 }
01579 
01580 static int cc_generic_monitor_suspend(struct ast_cc_monitor *monitor)
01581 {
01582    struct generic_monitor_instance_list *generic_list;
01583    struct generic_monitor_instance *generic_instance;
01584    enum ast_device_state state = ast_device_state(monitor->interface->device_name);
01585 
01586    if (!(generic_list = find_generic_monitor_instance_list(monitor->interface->device_name))) {
01587       return -1;
01588    }
01589 
01590    /* First we need to mark this particular monitor as being suspended. */
01591    AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
01592       if (generic_instance->core_id == monitor->core_id) {
01593          generic_instance->is_suspended = 1;
01594          break;
01595       }
01596    }
01597 
01598    /* If the device being suspended is currently in use, then we don't need to
01599     * take any further actions
01600     */
01601    if (!cc_generic_is_device_available(state)) {
01602       cc_unref(generic_list, "Device is in use. Nothing to do. Unref generic list.");
01603       return 0;
01604    }
01605 
01606    /* If the device is not in use, though, then it may be possible to report the
01607     * device's availability using a different monitor which is monitoring the
01608     * same device
01609     */
01610 
01611    AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
01612       if (!generic_instance->is_suspended) {
01613          ast_cc_monitor_callee_available(generic_instance->core_id, "Generic monitored party has become available");
01614          break;
01615       }
01616    }
01617    cc_unref(generic_list, "Done with generic list in suspend callback");
01618    return 0;
01619 }
01620 
01621 static int cc_generic_monitor_unsuspend(struct ast_cc_monitor *monitor)
01622 {
01623    struct generic_monitor_instance *generic_instance;
01624    struct generic_monitor_instance_list *generic_list = find_generic_monitor_instance_list(monitor->interface->device_name);
01625    enum ast_device_state state = ast_device_state(monitor->interface->device_name);
01626 
01627    if (!generic_list) {
01628       return -1;
01629    }
01630    /* If the device is currently available, we can immediately announce
01631     * its availability
01632     */
01633    if (cc_generic_is_device_available(state)) {
01634       ast_cc_monitor_callee_available(monitor->core_id, "Generic monitored party has become available");
01635    }
01636 
01637    /* In addition, we need to mark this generic_monitor_instance as not being suspended anymore */
01638    AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
01639       if (generic_instance->core_id == monitor->core_id) {
01640          generic_instance->is_suspended = 0;
01641          generic_instance->monitoring = 1;
01642          break;
01643       }
01644    }
01645    cc_unref(generic_list, "Done with generic list in cc_generic_monitor_unsuspend");
01646    return 0;
01647 }
01648 
01649 static int cc_generic_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id)
01650 {
01651    ast_assert(sched_id != NULL);
01652 
01653    if (*sched_id == -1) {
01654       return 0;
01655    }
01656 
01657    ast_log_dynamic_level(cc_logger_level, "Core %d: Canceling generic monitor available timer for monitor %s\n",
01658          monitor->core_id, monitor->interface->device_name);
01659    if (!ast_sched_del(cc_sched_context, *sched_id)) {
01660       cc_unref(monitor, "Remove scheduler's reference to the monitor");
01661    }
01662    *sched_id = -1;
01663    return 0;
01664 }
01665 
01666 static void cc_generic_monitor_destructor(void *private_data)
01667 {
01668    struct generic_monitor_pvt *gen_mon_pvt = private_data;
01669    struct generic_monitor_instance_list *generic_list;
01670    struct generic_monitor_instance *generic_instance;
01671 
01672    if (!private_data) {
01673       /* If the private data is NULL, that means that the monitor hasn't even
01674        * been created yet, but that the destructor was called. While this sort
01675        * of behavior is useful for native monitors, with a generic one, there is
01676        * nothing in particular to do.
01677        */
01678       return;
01679    }
01680 
01681    ast_log_dynamic_level(cc_logger_level, "Core %d: Destroying generic monitor %s\n",
01682          gen_mon_pvt->core_id, gen_mon_pvt->device_name);
01683 
01684    if (!(generic_list = find_generic_monitor_instance_list(gen_mon_pvt->device_name))) {
01685       /* If there's no generic list, that means that the monitor is being destroyed
01686        * before we actually got to request CC. Not a biggie. Same in the situation
01687        * below if the list traversal should complete without finding an entry.
01688        */
01689       ast_free((char *)gen_mon_pvt->device_name);
01690       ast_free(gen_mon_pvt);
01691       return;
01692    }
01693 
01694    AST_LIST_TRAVERSE_SAFE_BEGIN(&generic_list->list, generic_instance, next) {
01695       if (generic_instance->core_id == gen_mon_pvt->core_id) {
01696          AST_LIST_REMOVE_CURRENT(next);
01697          ast_free(generic_instance);
01698          break;
01699       }
01700    }
01701    AST_LIST_TRAVERSE_SAFE_END;
01702 
01703    if (AST_LIST_EMPTY(&generic_list->list)) {
01704       /* No more monitors with this device name exist. Time to unlink this
01705        * list from the container
01706        */
01707       ao2_t_unlink(generic_monitors, generic_list, "Generic list is empty. Unlink it from the container");
01708    } else {
01709       /* There are still instances for this particular device. The situation
01710        * may be that we were attempting a CC recall and a failure occurred, perhaps
01711        * on the agent side. If a failure happens here and the device being monitored
01712        * is available, then we need to signal on the first unsuspended instance that
01713        * the device is available for recall.
01714        */
01715 
01716       /* First things first. We don't even want to consider this action if
01717        * the device in question isn't available right now.
01718        */
01719       if (generic_list->fit_for_recall
01720          && cc_generic_is_device_available(generic_list->current_state)) {
01721          AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
01722             if (!generic_instance->is_suspended && generic_instance->monitoring) {
01723                ast_cc_monitor_callee_available(generic_instance->core_id, "Signaling generic monitor "
01724                      "availability due to other instance's failure.");
01725                break;
01726             }
01727          }
01728       }
01729    }
01730    cc_unref(generic_list, "Done with generic list in generic monitor destructor");
01731    ast_free((char *)gen_mon_pvt->device_name);
01732    ast_free(gen_mon_pvt);
01733 }
01734 
01735 static void cc_interface_destroy(void *data)
01736 {
01737    struct ast_cc_interface *interface = data;
01738    ast_log_dynamic_level(cc_logger_level, "Destroying cc interface %s\n", interface->device_name);
01739    ast_cc_config_params_destroy(interface->config_params);
01740 }
01741 
01742 /*!
01743  * \brief Data regarding an extension monitor's child's dialstrings
01744  *
01745  * \details
01746  * In developing CCSS, we had most aspects of its operation finished,
01747  * but there was one looming problem that we had failed to get right.
01748  * In our design document, we stated that when a CC recall occurs, all
01749  * endpoints that had been dialed originally would be called back.
01750  * Unfortunately, our implementation only allowed for devices which had
01751  * active monitors to inhabit the CC_INTERFACES channel variable, thus
01752  * making the automated recall only call monitored devices.
01753  *
01754  * Devices that were not CC-capable, or devices which failed CC at some
01755  * point during the process would not make it into the CC_INTERFACES
01756  * channel variable. This struct is meant as a remedy for the problem.
01757  */
01758 struct extension_child_dialstring {
01759    /*!
01760     * \brief the original dialstring used to call a particular device
01761     *
01762     * \details
01763     * When someone dials a particular endpoint, the dialstring used in
01764     * the dialplan is copied into this buffer. What's important here is
01765     * that this is the ORIGINAL dialstring, not the dialstring saved on
01766     * a device monitor. The dialstring on a device monitor is what should
01767     * be used when recalling that device. The two dialstrings may not be
01768     * the same.
01769     *
01770     * By keeping a copy of the original dialstring used, we can fall back
01771     * to using it if the device either does not ever offer CC or if the
01772     * device at some point fails for some reason, such as a timer expiration.
01773     */
01774    char original_dialstring[AST_CHANNEL_NAME];
01775    /*!
01776     * \brief The name of the device being dialed
01777     *
01778     * \details
01779     * This serves mainly as a key when searching for a particular dialstring.
01780     * For instance, let's say that we have called device SIP/400\@somepeer. This
01781     * device offers call completion, but then due to some unforeseen circumstance,
01782     * this device backs out and makes CC unavailable. When that happens, we need
01783     * to find the dialstring that corresponds to that device, and we use the
01784     * stored device name as a way to find it.
01785     *
01786     * \note There is one particular case where the device name stored here
01787     * will be empty. This is the case where we fail to request a channel, but we
01788     * still can make use of generic call completion. In such a case, since we never
01789     * were able to request the channel, we can't find what its device name is. In
01790     * this case, however, it is not important because the dialstring is guaranteed
01791     * to be the same both here and in the device monitor.
01792     */
01793    char device_name[AST_CHANNEL_NAME];
01794    /*!
01795     * \brief Is this structure valid for use in CC_INTERFACES?
01796     *
01797     * \details
01798     * When this structure is first created, all information stored here is planned
01799     * to be used, so we set the is_valid flag. However, if a device offers call
01800     * completion, it will potentially have its own dialstring to use for the recall,
01801     * so we find this structure and clear the is_valid flag. By clearing the is_valid
01802     * flag, we won't try to populate the CC_INTERFACES variable with the dialstring
01803     * stored in this struct. Now, if later, the device which had offered CC should fail,
01804     * perhaps due to a timer expiration, then we need to re-set the is_valid flag. This
01805     * way, we still will end up placing a call to the device again, and the dialstring
01806     * used will be the same as was originally used.
01807     */
01808    int is_valid;
01809    AST_LIST_ENTRY(extension_child_dialstring) next;
01810 };
01811 
01812 /*!
01813  * \brief Private data for an extension monitor
01814  */
01815 struct extension_monitor_pvt {
01816    AST_LIST_HEAD_NOLOCK(, extension_child_dialstring) child_dialstrings;
01817 };
01818 
01819 static void cc_extension_monitor_destructor(void *private_data)
01820 {
01821    struct extension_monitor_pvt *extension_pvt = private_data;
01822    struct extension_child_dialstring *child_dialstring;
01823 
01824    /* This shouldn't be possible, but I'm paranoid */
01825    if (!extension_pvt) {
01826       return;
01827    }
01828 
01829    while ((child_dialstring = AST_LIST_REMOVE_HEAD(&extension_pvt->child_dialstrings, next))) {
01830       ast_free(child_dialstring);
01831    }
01832    ast_free(extension_pvt);
01833 }
01834 
01835 static void cc_monitor_destroy(void *data)
01836 {
01837    struct ast_cc_monitor *monitor = data;
01838    /* During the monitor creation process, it is possible for this
01839     * function to be called prior to when callbacks are assigned
01840     * to the monitor. Also, extension monitors do not have callbacks
01841     * assigned to them, so we wouldn't want to segfault when we try
01842     * to destroy one of them.
01843     */
01844    ast_log_dynamic_level(cc_logger_level, "Core %d: Calling destructor for monitor %s\n",
01845          monitor->core_id, monitor->interface->device_name);
01846    if (monitor->interface->monitor_class == AST_CC_EXTENSION_MONITOR) {
01847       cc_extension_monitor_destructor(monitor->private_data);
01848    }
01849    if (monitor->callbacks) {
01850       monitor->callbacks->destructor(monitor->private_data);
01851    }
01852    cc_unref(monitor->interface, "Unreffing tree's reference to interface");
01853    ast_free(monitor->dialstring);
01854 }
01855 
01856 static void cc_interface_tree_destroy(void *data)
01857 {
01858    struct cc_monitor_tree *cc_interface_tree = data;
01859    struct ast_cc_monitor *monitor;
01860    while ((monitor = AST_LIST_REMOVE_HEAD(cc_interface_tree, next))) {
01861       if (monitor->callbacks) {
01862          monitor->callbacks->cancel_available_timer(monitor, &monitor->available_timer_id);
01863       }
01864       cc_unref(monitor, "Destroying all monitors");
01865    }
01866    AST_LIST_HEAD_DESTROY(cc_interface_tree);
01867 }
01868 
01869 /*!
01870  * This counter is used for assigning unique ids
01871  * to CC-enabled dialed interfaces.
01872  */
01873 static int dialed_cc_interface_counter;
01874 
01875 /*!
01876  * \internal
01877  * \brief data stored in CC datastore
01878  *
01879  * The datastore creates a list of interfaces that were
01880  * dialed, including both extensions and devices. In addition
01881  * to the intrinsic data of the tree, some extra information
01882  * is needed for use by app_dial.
01883  */
01884 struct dialed_cc_interfaces {
01885    /*!
01886     * This value serves a dual-purpose. When dial starts, if the
01887     * dialed_cc_interfaces datastore currently exists on the calling
01888     * channel, then the dial_parent_id will serve as a means of
01889     * letting the new extension cc_monitor we create know
01890     * who his parent is. This value will be the extension
01891     * cc_monitor that dialed the local channel that resulted
01892     * in the new Dial app being called.
01893     *
01894     * In addition, once an extension cc_monitor is created,
01895     * the dial_parent_id will be changed to the id of that newly
01896     * created interface. This way, device interfaces created from
01897     * receiving AST_CONTROL_CC frames can use this field to determine
01898     * who their parent extension interface should be.
01899     */
01900    unsigned int dial_parent_id;
01901    /*!
01902     * Identifier for the potential CC request that may be made
01903     * based on this call. Even though an instance of the core may
01904     * not be made (since the caller may not request CC), we allocate
01905     * a new core_id at the beginning of the call so that recipient
01906     * channel drivers can have the information handy just in case
01907     * the caller does end up requesting CC.
01908     */
01909    int core_id;
01910    /*!
01911     * When a new Dial application is started, and the datastore
01912     * already exists on the channel, we can determine if we
01913     * should be adding any new interface information to tree.
01914     */
01915    char ignore;
01916    /*!
01917     * When it comes time to offer CC to the caller, we only want to offer
01918     * it to the original incoming channel. For nested Dials and outbound
01919     * channels, it is incorrect to attempt such a thing. This flag indicates
01920     * if the channel to which this datastore is attached may be legally
01921     * offered CC when the call is finished.
01922     */
01923    char is_original_caller;
01924    /*!
01925     * Reference-counted "tree" of interfaces.
01926     */
01927    struct cc_monitor_tree *interface_tree;
01928 };
01929 
01930 /*!
01931  * \internal
01932  * \brief Destructor function for cc_interfaces datastore
01933  *
01934  * This function will free the actual datastore and drop
01935  * the refcount for the monitor tree by one. In cases
01936  * where CC can actually be used, this unref will not
01937  * result in the destruction of the monitor tree, because
01938  * the CC core will still have a reference.
01939  *
01940  * \param data The dialed_cc_interfaces struct to destroy
01941  */
01942 static void dialed_cc_interfaces_destroy(void *data)
01943 {
01944    struct dialed_cc_interfaces *cc_interfaces = data;
01945    cc_unref(cc_interfaces->interface_tree, "Unref dial's ref to monitor tree");
01946    ast_free(cc_interfaces);
01947 }
01948 
01949 /*!
01950  * \internal
01951  * \brief Duplicate callback for cc_interfaces datastore
01952  *
01953  * Integers are copied by value, but the monitor tree
01954  * is done via a shallow copy and a bump of the refcount.
01955  * This way, sub-Dials will be appending interfaces onto
01956  * the same list as this call to Dial.
01957  *
01958  * \param data The old dialed_cc_interfaces we want to copy
01959  * \retval NULL Could not allocate memory for new dialed_cc_interfaces
01960  * \retval non-NULL The new copy of the dialed_cc_interfaces
01961  */
01962 static void *dialed_cc_interfaces_duplicate(void *data)
01963 {
01964    struct dialed_cc_interfaces *old_cc_interfaces = data;
01965    struct dialed_cc_interfaces *new_cc_interfaces = ast_calloc(1, sizeof(*new_cc_interfaces));
01966    if (!new_cc_interfaces) {
01967       return NULL;
01968    }
01969    new_cc_interfaces->ignore = old_cc_interfaces->ignore;
01970    new_cc_interfaces->dial_parent_id = old_cc_interfaces->dial_parent_id;
01971    new_cc_interfaces->is_original_caller = 0;
01972    cc_ref(old_cc_interfaces->interface_tree, "New ref due to duplication of monitor tree");
01973    new_cc_interfaces->core_id = old_cc_interfaces->core_id;
01974    new_cc_interfaces->interface_tree = old_cc_interfaces->interface_tree;
01975    return new_cc_interfaces;
01976 }
01977 
01978 /*!
01979  * \internal
01980  * \brief information regarding the dialed_cc_interfaces datastore
01981  *
01982  * The dialed_cc_interfaces datastore is responsible for keeping track
01983  * of what CC-enabled interfaces have been dialed by the caller. For
01984  * more information regarding the actual structure of the tree, see
01985  * the documentation provided in include/asterisk/ccss.h
01986  */
01987 static const struct ast_datastore_info dialed_cc_interfaces_info = {
01988    .type = "Dial CC Interfaces",
01989    .duplicate = dialed_cc_interfaces_duplicate,
01990    .destroy = dialed_cc_interfaces_destroy,
01991 };
01992 
01993 static struct extension_monitor_pvt *extension_monitor_pvt_init(void)
01994 {
01995    struct extension_monitor_pvt *ext_pvt = ast_calloc(1, sizeof(*ext_pvt));
01996    if (!ext_pvt) {
01997       return NULL;
01998    }
01999    AST_LIST_HEAD_INIT_NOLOCK(&ext_pvt->child_dialstrings);
02000    return ext_pvt;
02001 }
02002 
02003 void ast_cc_extension_monitor_add_dialstring(struct ast_channel *incoming, const char * const dialstring, const char * const device_name)
02004 {
02005    struct ast_datastore *cc_datastore;
02006    struct dialed_cc_interfaces *cc_interfaces;
02007    struct ast_cc_monitor *monitor;
02008    struct extension_monitor_pvt *extension_pvt;
02009    struct extension_child_dialstring *child_dialstring;
02010    struct cc_monitor_tree *interface_tree;
02011    int id;
02012 
02013    ast_channel_lock(incoming);
02014    if (!(cc_datastore = ast_channel_datastore_find(incoming, &dialed_cc_interfaces_info, NULL))) {
02015       ast_channel_unlock(incoming);
02016       return;
02017    }
02018 
02019    cc_interfaces = cc_datastore->data;
02020    interface_tree = cc_interfaces->interface_tree;
02021    id = cc_interfaces->dial_parent_id;
02022    ast_channel_unlock(incoming);
02023 
02024    AST_LIST_LOCK(interface_tree);
02025    AST_LIST_TRAVERSE(interface_tree, monitor, next) {
02026       if (monitor->id == id) {
02027          break;
02028       }
02029    }
02030 
02031    if (!monitor) {
02032       AST_LIST_UNLOCK(interface_tree);
02033       return;
02034    }
02035 
02036    extension_pvt = monitor->private_data;
02037    if (!(child_dialstring = ast_calloc(1, sizeof(*child_dialstring)))) {
02038       AST_LIST_UNLOCK(interface_tree);
02039       return;
02040    }
02041    ast_copy_string(child_dialstring->original_dialstring, dialstring, sizeof(child_dialstring->original_dialstring));
02042    ast_copy_string(child_dialstring->device_name, device_name, sizeof(child_dialstring->device_name));
02043    child_dialstring->is_valid = 1;
02044    AST_LIST_INSERT_TAIL(&extension_pvt->child_dialstrings, child_dialstring, next);
02045    AST_LIST_UNLOCK(interface_tree);
02046 }
02047 
02048 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)
02049 {
02050    struct ast_cc_monitor *monitor_iter;
02051    struct extension_monitor_pvt *extension_pvt;
02052    struct extension_child_dialstring *child_dialstring;
02053 
02054    AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
02055       if (monitor_iter->id == parent_id) {
02056          break;
02057       }
02058    }
02059 
02060    if (!monitor_iter) {
02061       return;
02062    }
02063    extension_pvt = monitor_iter->private_data;
02064 
02065    AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) {
02066       if (!strcmp(child_dialstring->device_name, device_name)) {
02067          child_dialstring->is_valid = is_valid;
02068          break;
02069       }
02070    }
02071 }
02072 
02073 /*!
02074  * \internal
02075  * \brief Allocate and initialize an "extension" interface for CC purposes
02076  *
02077  * When app_dial starts, this function is called in order to set up the
02078  * information about the extension in which this Dial is occurring. Any
02079  * devices dialed will have this particular cc_monitor as a parent.
02080  *
02081  * \param exten Extension from which Dial is occurring
02082  * \param context Context to which exten belongs
02083  * \param parent_id What should we set the parent_id of this interface to?
02084  * \retval NULL Memory allocation failure
02085  * \retval non-NULL The newly-created cc_monitor for the extension
02086  */
02087 static struct ast_cc_monitor *cc_extension_monitor_init(const char * const exten, const char * const context, const unsigned int parent_id)
02088 {
02089    struct ast_str *str = ast_str_alloca(2 * AST_MAX_EXTENSION);
02090    struct ast_cc_interface *cc_interface;
02091    struct ast_cc_monitor *monitor;
02092 
02093    ast_str_set(&str, 0, "%s@%s", exten, context);
02094 
02095    if (!(cc_interface = ao2_t_alloc(sizeof(*cc_interface) + ast_str_strlen(str), cc_interface_destroy,
02096                "Allocating new ast_cc_interface"))) {
02097       return NULL;
02098    }
02099 
02100    if (!(monitor = ao2_t_alloc(sizeof(*monitor), cc_monitor_destroy, "Allocating new ast_cc_monitor"))) {
02101       cc_unref(cc_interface, "failed to allocate the monitor, so unref the interface");
02102       return NULL;
02103    }
02104 
02105    if (!(monitor->private_data = extension_monitor_pvt_init())) {
02106       cc_unref(monitor, "Failed to initialize extension monitor private data. uref monitor");
02107       cc_unref(cc_interface, "Failed to initialize extension monitor private data. unref cc_interface");
02108    }
02109 
02110    monitor->id = ast_atomic_fetchadd_int(&dialed_cc_interface_counter, +1);
02111    monitor->parent_id = parent_id;
02112    cc_interface->monitor_type = "extension";
02113    cc_interface->monitor_class = AST_CC_EXTENSION_MONITOR;
02114    strcpy(cc_interface->device_name, ast_str_buffer(str));
02115    monitor->interface = cc_interface;
02116    ast_log_dynamic_level(cc_logger_level, "Created an extension cc interface for '%s' with id %u and parent %u\n", cc_interface->device_name, monitor->id, monitor->parent_id);
02117    return monitor;
02118 }
02119 
02120 /*!
02121  * \internal
02122  * \brief allocate dialed_cc_interfaces datastore and initialize fields
02123  *
02124  * This function is called when Situation 1 occurs in ast_cc_call_init.
02125  * See that function for more information on what Situation 1 is.
02126  *
02127  * In this particular case, we have to do a lot of memory allocation in order
02128  * to create the datastore, the data for the datastore, the tree of interfaces
02129  * that we'll be adding to, and the initial extension interface for this Dial
02130  * attempt.
02131  *
02132  * \param chan The channel onto which the datastore should be added.
02133  * \retval -1 An error occurred
02134  * \retval 0 Success
02135  */
02136 static int cc_interfaces_datastore_init(struct ast_channel *chan) {
02137    struct dialed_cc_interfaces *interfaces;
02138    struct ast_cc_monitor *monitor;
02139    struct ast_datastore *dial_cc_datastore;
02140 
02141    /*XXX This may be a bit controversial. In an attempt to not allocate
02142     * extra resources, I make sure that a future request will be within
02143     * limits. The problem here is that it is reasonable to think that
02144     * even if we're not within the limits at this point, we may be by
02145     * the time the requestor will have made his request. This may be
02146     * deleted at some point.
02147     */
02148    if (!ast_cc_request_is_within_limits()) {
02149       return 0;
02150    }
02151 
02152    if (!(interfaces = ast_calloc(1, sizeof(*interfaces)))) {
02153       return -1;
02154    }
02155 
02156    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))) {
02157       ast_free(interfaces);
02158       return -1;
02159    }
02160 
02161    if (!(dial_cc_datastore = ast_datastore_alloc(&dialed_cc_interfaces_info, NULL))) {
02162       cc_unref(monitor, "Could not allocate the dialed interfaces datastore. Unreffing monitor");
02163       ast_free(interfaces);
02164       return -1;
02165    }
02166 
02167    if (!(interfaces->interface_tree = ao2_t_alloc(sizeof(*interfaces->interface_tree), cc_interface_tree_destroy,
02168                "Allocate monitor tree"))) {
02169       ast_datastore_free(dial_cc_datastore);
02170       cc_unref(monitor, "Could not allocate monitor tree on dialed interfaces datastore. Unreffing monitor");
02171       ast_free(interfaces);
02172       return -1;
02173    }
02174 
02175    /* Finally, all that allocation is done... */
02176    AST_LIST_HEAD_INIT(interfaces->interface_tree);
02177    AST_LIST_INSERT_TAIL(interfaces->interface_tree, monitor, next);
02178    cc_ref(monitor, "List's reference to extension monitor");
02179    dial_cc_datastore->data = interfaces;
02180    dial_cc_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
02181    interfaces->dial_parent_id = monitor->id;
02182    interfaces->core_id = monitor->core_id = ast_atomic_fetchadd_int(&core_id_counter, +1);
02183    interfaces->is_original_caller = 1;
02184    ast_channel_lock(chan);
02185    ast_channel_datastore_add(chan, dial_cc_datastore);
02186    ast_channel_unlock(chan);
02187    cc_unref(monitor, "Unreffing allocation's reference");
02188    return 0;
02189 }
02190 
02191 /*!
02192  * \internal
02193  * \brief  Call a monitor's destructor before the monitor has been allocated
02194  * \since 1.8
02195  *
02196  * \param monitor_type The type of monitor callbacks to use when calling the destructor
02197  * \param private_data Data allocated by a channel driver that must be freed
02198  *
02199  * \details
02200  * I'll admit, this is a bit evil.
02201  *
02202  * When a channel driver determines that it can offer a call completion service to
02203  * a caller, it is very likely that the channel driver will need to allocate some
02204  * data so that when the time comes to request CC, the channel driver will have the
02205  * necessary data at hand.
02206  *
02207  * The problem is that there are many places where failures may occur before the monitor
02208  * has been properly allocated and had its callbacks assigned to it. If one of these
02209  * failures should occur, then we still need to let the channel driver know that it
02210  * must destroy the data that it allocated.
02211  *
02212  * \return Nothing
02213  */
02214 static void call_destructor_with_no_monitor(const char * const monitor_type, void *private_data)
02215 {
02216    const struct ast_cc_monitor_callbacks *monitor_callbacks = find_monitor_callbacks(monitor_type);
02217 
02218    if (!monitor_callbacks) {
02219       return;
02220    }
02221 
02222    monitor_callbacks->destructor(private_data);
02223 }
02224 
02225 /*!
02226  * \internal
02227  * \brief Allocate and intitialize a device cc_monitor
02228  *
02229  * For all intents and purposes, this is the same as
02230  * cc_extension_monitor_init, except that there is only
02231  * a single parameter used for naming the interface.
02232  *
02233  * This function is called when handling AST_CONTROL_CC frames.
02234  * The device has reported that CC is possible, so we add it
02235  * to the interface_tree.
02236  *
02237  * Note that it is not necessarily erroneous to add the same
02238  * device to the tree twice. If the same device is called by
02239  * two different extension during the same call, then
02240  * that is a legitimate situation. Of course, I'm pretty sure
02241  * the dialed_interfaces global datastore will not allow that
02242  * to happen anyway.
02243  *
02244  * \param device_name The name of the device being added to the tree
02245  * \param dialstring The dialstring used to dial the device being added
02246  * \param parent_id The parent of this new tree node.
02247  * \retval NULL Memory allocation failure
02248  * \retval non-NULL The new ast_cc_interface created.
02249  */
02250 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)
02251 {
02252    struct ast_cc_interface *cc_interface;
02253    struct ast_cc_monitor *monitor;
02254    size_t device_name_len = strlen(device_name);
02255    int parent_id = cc_data->parent_interface_id;
02256 
02257    if (!(cc_interface = ao2_t_alloc(sizeof(*cc_interface) + device_name_len, cc_interface_destroy,
02258                "Allocating new ast_cc_interface"))) {
02259       return NULL;
02260    }
02261 
02262    if (!(cc_interface->config_params = ast_cc_config_params_init())) {
02263       cc_unref(cc_interface, "Failed to allocate config params, unref interface");
02264       return NULL;
02265    }
02266 
02267    if (!(monitor = ao2_t_alloc(sizeof(*monitor), cc_monitor_destroy, "Allocating new ast_cc_monitor"))) {
02268       cc_unref(cc_interface, "Failed to allocate monitor, unref interface");
02269       return NULL;
02270    }
02271 
02272    if (!(monitor->dialstring = ast_strdup(dialstring))) {
02273       cc_unref(monitor, "Failed to copy dialable name. Unref monitor");
02274       cc_unref(cc_interface, "Failed to copy dialable name");
02275       return NULL;
02276    }
02277 
02278    if (!(monitor->callbacks = find_monitor_callbacks(cc_data->monitor_type))) {
02279       cc_unref(monitor, "Failed to find monitor callbacks. Unref monitor");
02280       cc_unref(cc_interface, "Failed to find monitor callbacks");
02281       return NULL;
02282    }
02283 
02284    strcpy(cc_interface->device_name, device_name);
02285    monitor->id = ast_atomic_fetchadd_int(&dialed_cc_interface_counter, +1);
02286    monitor->parent_id = parent_id;
02287    monitor->core_id = core_id;
02288    monitor->service_offered = cc_data->service;
02289    monitor->private_data = cc_data->private_data;
02290    cc_interface->monitor_type = cc_data->monitor_type;
02291    cc_interface->monitor_class = AST_CC_DEVICE_MONITOR;
02292    monitor->interface = cc_interface;
02293    monitor->available_timer_id = -1;
02294    ast_cc_copy_config_params(cc_interface->config_params, &cc_data->config_params);
02295    ast_log_dynamic_level(cc_logger_level, "Core %d: Created a device cc interface for '%s' with id %u and parent %u\n",
02296          monitor->core_id, cc_interface->device_name, monitor->id, monitor->parent_id);
02297    return monitor;
02298 }
02299 
02300 /*!
02301  * \details
02302  * Unless we are ignoring CC for some reason, we will always
02303  * call this function when we read an AST_CONTROL_CC frame
02304  * from an outbound channel.
02305  *
02306  * This function will call cc_device_monitor_init to
02307  * create the new cc_monitor for the device from which
02308  * we read the frame. In addition, the new device will be added
02309  * to the monitor tree on the dialed_cc_interfaces datastore
02310  * on the inbound channel.
02311  *
02312  * If this is the first AST_CONTROL_CC frame that we have handled
02313  * for this call, then we will also initialize the CC core for
02314  * this call.
02315  */
02316 void ast_handle_cc_control_frame(struct ast_channel *inbound, struct ast_channel *outbound, void *frame_data)
02317 {
02318    char *device_name;
02319    char *dialstring;
02320    struct ast_cc_monitor *monitor;
02321    struct ast_datastore *cc_datastore;
02322    struct dialed_cc_interfaces *cc_interfaces;
02323    struct cc_control_payload *cc_data = frame_data;
02324    struct cc_core_instance *core_instance;
02325 
02326    device_name = cc_data->device_name;
02327    dialstring = cc_data->dialstring;
02328 
02329    ast_channel_lock(inbound);
02330    if (!(cc_datastore = ast_channel_datastore_find(inbound, &dialed_cc_interfaces_info, NULL))) {
02331       ast_log(LOG_WARNING, "Unable to retrieve CC datastore while processing CC frame from '%s'. CC services will be unavailable.\n", device_name);
02332       ast_channel_unlock(inbound);
02333       call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
02334       return;
02335    }
02336 
02337    cc_interfaces = cc_datastore->data;
02338 
02339    if (cc_interfaces->ignore) {
02340       ast_channel_unlock(inbound);
02341       call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
02342       return;
02343    }
02344 
02345    if (!cc_interfaces->is_original_caller) {
02346       /* If the is_original_caller is not set on the *inbound* channel, then
02347        * it must be a local channel. As such, we do not want to create a core instance
02348        * or an agent for the local channel. Instead, we want to pass this along to the
02349        * other side of the local channel so that the original caller can benefit.
02350        */
02351       ast_channel_unlock(inbound);
02352       ast_indicate_data(inbound, AST_CONTROL_CC, cc_data, sizeof(*cc_data));
02353       return;
02354    }
02355 
02356    core_instance = find_cc_core_instance(cc_interfaces->core_id);
02357    if (!core_instance) {
02358       core_instance = cc_core_init_instance(inbound, cc_interfaces->interface_tree,
02359          cc_interfaces->core_id, cc_data);
02360       if (!core_instance) {
02361          cc_interfaces->ignore = 1;
02362          ast_channel_unlock(inbound);
02363          call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
02364          return;
02365       }
02366    }
02367 
02368    ast_channel_unlock(inbound);
02369 
02370    /* Yeah this kind of sucks, but luckily most people
02371     * aren't dialing thousands of interfaces on every call
02372     *
02373     * This traversal helps us to not create duplicate monitors in
02374     * case a device queues multiple CC control frames.
02375     */
02376    AST_LIST_LOCK(cc_interfaces->interface_tree);
02377    AST_LIST_TRAVERSE(cc_interfaces->interface_tree, monitor, next) {
02378       if (!strcmp(monitor->interface->device_name, device_name)) {
02379          ast_log_dynamic_level(cc_logger_level, "Core %d: Device %s sent us multiple CC control frames. Ignoring those beyond the first.\n",
02380                core_instance->core_id, device_name);
02381          AST_LIST_UNLOCK(cc_interfaces->interface_tree);
02382          cc_unref(core_instance, "Returning early from ast_handle_cc_control_frame. Unref core_instance");
02383          call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
02384          return;
02385       }
02386    }
02387    AST_LIST_UNLOCK(cc_interfaces->interface_tree);
02388 
02389    if (!(monitor = cc_device_monitor_init(device_name, dialstring, cc_data, core_instance->core_id))) {
02390       ast_log(LOG_WARNING, "Unable to create CC device interface for '%s'. CC services will be unavailable on this interface.\n", device_name);
02391       cc_unref(core_instance, "Returning early from ast_handle_cc_control_frame. Unref core_instance");
02392       call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
02393       return;
02394    }
02395 
02396    AST_LIST_LOCK(cc_interfaces->interface_tree);
02397    cc_ref(monitor, "monitor tree's reference to the monitor");
02398    AST_LIST_INSERT_TAIL(cc_interfaces->interface_tree, monitor, next);
02399    AST_LIST_UNLOCK(cc_interfaces->interface_tree);
02400 
02401    cc_extension_monitor_change_is_valid(core_instance, monitor->parent_id, monitor->interface->device_name, 0);
02402 
02403    cc_publish_available(cc_interfaces->core_id, device_name, cc_service_to_string(cc_data->service));
02404 
02405    cc_unref(core_instance, "Done with core_instance after handling CC control frame");
02406    cc_unref(monitor, "Unref reference from allocating monitor");
02407 }
02408 
02409 int ast_cc_call_init(struct ast_channel *chan, int *ignore_cc)
02410 {
02411    /* There are three situations to deal with here:
02412     *
02413     * 1. The channel does not have a dialed_cc_interfaces datastore on
02414     * it. This means that this is the first time that Dial has
02415     * been called. We need to create/initialize the datastore.
02416     *
02417     * 2. The channel does have a cc_interface datastore on it and
02418     * the "ignore" indicator is 0. This means that a Local channel
02419     * was called by a "parent" dial. We can check the datastore's
02420     * parent field to see who the root of this particular dial tree
02421     * is.
02422     *
02423     * 3. The channel does have a cc_interface datastore on it and
02424     * the "ignore" indicator is 1. This means that a second Dial call
02425     * is being made from an extension. In this case, we do not
02426     * want to make any additions/modifications to the datastore. We
02427     * will instead set a flag to indicate that CCSS is completely
02428     * disabled for this Dial attempt.
02429     */
02430 
02431    struct ast_datastore *cc_interfaces_datastore;
02432    struct dialed_cc_interfaces *interfaces;
02433    struct ast_cc_monitor *monitor;
02434    struct ast_cc_config_params *cc_params;
02435 
02436    ast_channel_lock(chan);
02437 
02438    cc_params = ast_channel_get_cc_config_params(chan);
02439    if (!cc_params) {
02440       ast_channel_unlock(chan);
02441       return -1;
02442    }
02443    if (ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_NEVER) {
02444       /* We can't offer CC to this caller anyway, so don't bother with CC on this call
02445        */
02446       *ignore_cc = 1;
02447       ast_channel_unlock(chan);
02448       ast_log_dynamic_level(cc_logger_level, "Agent policy for %s is 'never'. CC not possible\n", ast_channel_name(chan));
02449       return 0;
02450    }
02451 
02452    if (!(cc_interfaces_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
02453       /* Situation 1 has occurred */
02454       ast_channel_unlock(chan);
02455       return cc_interfaces_datastore_init(chan);
02456    }
02457    interfaces = cc_interfaces_datastore->data;
02458    ast_channel_unlock(chan);
02459 
02460    if (interfaces->ignore) {
02461       /* Situation 3 has occurred */
02462       *ignore_cc = 1;
02463       ast_log_dynamic_level(cc_logger_level, "Datastore is present with ignore flag set. Ignoring CC offers on this call\n");
02464       return 0;
02465    }
02466 
02467    /* Situation 2 has occurred */
02468    if (!(monitor = cc_extension_monitor_init(S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)),
02469          S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), interfaces->dial_parent_id))) {
02470       return -1;
02471    }
02472    monitor->core_id = interfaces->core_id;
02473    AST_LIST_LOCK(interfaces->interface_tree);
02474    cc_ref(monitor, "monitor tree's reference to the monitor");
02475    AST_LIST_INSERT_TAIL(interfaces->interface_tree, monitor, next);
02476    AST_LIST_UNLOCK(interfaces->interface_tree);
02477    interfaces->dial_parent_id = monitor->id;
02478    cc_unref(monitor, "Unref monitor's allocation reference");
02479    return 0;
02480 }
02481 
02482 int ast_cc_request_is_within_limits(void)
02483 {
02484    return cc_request_count < global_cc_max_requests;
02485 }
02486 
02487 int ast_cc_get_current_core_id(struct ast_channel *chan)
02488 {
02489    struct ast_datastore *datastore;
02490    struct dialed_cc_interfaces *cc_interfaces;
02491    int core_id_return;
02492 
02493    ast_channel_lock(chan);
02494    if (!(datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
02495       ast_channel_unlock(chan);
02496       return -1;
02497    }
02498 
02499    cc_interfaces = datastore->data;
02500    core_id_return = cc_interfaces->ignore ? -1 : cc_interfaces->core_id;
02501    ast_channel_unlock(chan);
02502    return core_id_return;
02503 
02504 }
02505 
02506 static long count_agents(const char * const caller, const int core_id_exception)
02507 {
02508    struct count_agents_cb_data data = {.core_id_exception = core_id_exception,};
02509 
02510    ao2_t_callback_data(cc_core_instances, OBJ_NODATA, count_agents_cb, (char *)caller, &data, "Counting agents");
02511    ast_log_dynamic_level(cc_logger_level, "Counted %d agents\n", data.count);
02512    return data.count;
02513 }
02514 
02515 static void kill_duplicate_offers(char *caller)
02516 {
02517    unsigned long match_flags = MATCH_NO_REQUEST;
02518    struct ao2_iterator *dups_iter;
02519 
02520    /*
02521     * Must remove the ref that was in cc_core_instances outside of
02522     * the container lock to prevent deadlock.
02523     */
02524    dups_iter = ao2_t_callback_data(cc_core_instances, OBJ_MULTIPLE | OBJ_UNLINK,
02525       match_agent, caller, &match_flags, "Killing duplicate offers");
02526    if (dups_iter) {
02527       /* Now actually unref any duplicate offers by simply destroying the iterator. */
02528       ao2_iterator_destroy(dups_iter);
02529    }
02530 }
02531 
02532 static void check_callback_sanity(const struct ast_cc_agent_callbacks *callbacks)
02533 {
02534    ast_assert(callbacks->init != NULL);
02535    ast_assert(callbacks->start_offer_timer != NULL);
02536    ast_assert(callbacks->stop_offer_timer != NULL);
02537    ast_assert(callbacks->respond != NULL);
02538    ast_assert(callbacks->status_request != NULL);
02539    ast_assert(callbacks->start_monitoring != NULL);
02540    ast_assert(callbacks->callee_available != NULL);
02541    ast_assert(callbacks->destructor != NULL);
02542 }
02543 
02544 static void agent_destroy(void *data)
02545 {
02546    struct ast_cc_agent *agent = data;
02547 
02548    if (agent->callbacks) {
02549       agent->callbacks->destructor(agent);
02550    }
02551    ast_cc_config_params_destroy(agent->cc_params);
02552 }
02553 
02554 static struct ast_cc_agent *cc_agent_init(struct ast_channel *caller_chan,
02555       const char * const caller_name, const int core_id,
02556       struct cc_monitor_tree *interface_tree)
02557 {
02558    struct ast_cc_agent *agent;
02559    struct ast_cc_config_params *cc_params;
02560 
02561    if (!(agent = ao2_t_alloc(sizeof(*agent) + strlen(caller_name), agent_destroy,
02562                "Allocating new ast_cc_agent"))) {
02563       return NULL;
02564    }
02565 
02566    agent->core_id = core_id;
02567    strcpy(agent->device_name, caller_name);
02568 
02569    cc_params = ast_channel_get_cc_config_params(caller_chan);
02570    if (!cc_params) {
02571       cc_unref(agent, "Could not get channel config params.");
02572       return NULL;
02573    }
02574    if (!(agent->cc_params = ast_cc_config_params_init())) {
02575       cc_unref(agent, "Could not init agent config params.");
02576       return NULL;
02577    }
02578    ast_cc_copy_config_params(agent->cc_params, cc_params);
02579 
02580    if (!(agent->callbacks = find_agent_callbacks(caller_chan))) {
02581       cc_unref(agent, "Could not find agent callbacks.");
02582       return NULL;
02583    }
02584    check_callback_sanity(agent->callbacks);
02585 
02586    if (agent->callbacks->init(agent, caller_chan)) {
02587       cc_unref(agent, "Agent init callback failed.");
02588       return NULL;
02589    }
02590    ast_log_dynamic_level(cc_logger_level, "Core %u: Created an agent for caller %s\n",
02591          agent->core_id, agent->device_name);
02592    return agent;
02593 }
02594 
02595 /* Generic agent callbacks */
02596 static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan);
02597 static int cc_generic_agent_start_offer_timer(struct ast_cc_agent *agent);
02598 static int cc_generic_agent_stop_offer_timer(struct ast_cc_agent *agent);
02599 static void cc_generic_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason);
02600 static int cc_generic_agent_status_request(struct ast_cc_agent *agent);
02601 static int cc_generic_agent_stop_ringing(struct ast_cc_agent *agent);
02602 static int cc_generic_agent_start_monitoring(struct ast_cc_agent *agent);
02603 static int cc_generic_agent_recall(struct ast_cc_agent *agent);
02604 static void cc_generic_agent_destructor(struct ast_cc_agent *agent);
02605 
02606 static struct ast_cc_agent_callbacks generic_agent_callbacks = {
02607    .type = "generic",
02608    .init = cc_generic_agent_init,
02609    .start_offer_timer = cc_generic_agent_start_offer_timer,
02610    .stop_offer_timer = cc_generic_agent_stop_offer_timer,
02611    .respond = cc_generic_agent_respond,
02612    .status_request = cc_generic_agent_status_request,
02613    .stop_ringing = cc_generic_agent_stop_ringing,
02614    .start_monitoring = cc_generic_agent_start_monitoring,
02615    .callee_available = cc_generic_agent_recall,
02616    .destructor = cc_generic_agent_destructor,
02617 };
02618 
02619 struct cc_generic_agent_pvt {
02620    /*!
02621     * Subscription to device state
02622     *
02623     * Used in the CC_CALLER_BUSY state. The
02624     * generic agent will subscribe to the
02625     * device state of the caller in order to
02626     * determine when we may move on
02627     */
02628    struct stasis_subscription *sub;
02629    /*!
02630     * Scheduler id of offer timer.
02631     */
02632    int offer_timer_id;
02633    /*!
02634     * Caller ID number
02635     *
02636     * When we re-call the caller, we need
02637     * to provide this information to
02638     * ast_request_and_dial so that the
02639     * information will be present in the
02640     * call to the callee
02641     */
02642    char cid_num[AST_CHANNEL_NAME];
02643    /*!
02644     * Caller ID name
02645     *
02646     * See the description of cid_num.
02647     * The same applies here, except this
02648     * is the caller's name.
02649     */
02650    char cid_name[AST_CHANNEL_NAME];
02651    /*!
02652     * Extension dialed
02653     *
02654     * The original extension dialed. This is used
02655     * so that when performing a recall, we can
02656     * call the proper extension.
02657     */
02658    char exten[AST_CHANNEL_NAME];
02659    /*!
02660     * Context dialed
02661     *
02662     * The original context dialed. This is used
02663     * so that when performaing a recall, we can
02664     * call into the proper context
02665     */
02666    char context[AST_CHANNEL_NAME];
02667 };
02668 
02669 static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan)
02670 {
02671    struct cc_generic_agent_pvt *generic_pvt = ast_calloc(1, sizeof(*generic_pvt));
02672 
02673    if (!generic_pvt) {
02674       return -1;
02675    }
02676 
02677    generic_pvt->offer_timer_id = -1;
02678    if (ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str) {
02679       ast_copy_string(generic_pvt->cid_num, ast_channel_caller(chan)->id.number.str, sizeof(generic_pvt->cid_num));
02680    }
02681    if (ast_channel_caller(chan)->id.name.valid && ast_channel_caller(chan)->id.name.str) {
02682       ast_copy_string(generic_pvt->cid_name, ast_channel_caller(chan)->id.name.str, sizeof(generic_pvt->cid_name));
02683    }
02684    ast_copy_string(generic_pvt->exten, S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)), sizeof(generic_pvt->exten));
02685    ast_copy_string(generic_pvt->context, S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), sizeof(generic_pvt->context));
02686    agent->private_data = generic_pvt;
02687    ast_set_flag(agent, AST_CC_AGENT_SKIP_OFFER);
02688    return 0;
02689 }
02690 
02691 static int offer_timer_expire(const void *data)
02692 {
02693    struct ast_cc_agent *agent = (struct ast_cc_agent *) data;
02694    struct cc_generic_agent_pvt *agent_pvt = agent->private_data;
02695    ast_log_dynamic_level(cc_logger_level, "Core %u: Queuing change request because offer timer has expired.\n",
02696          agent->core_id);
02697    agent_pvt->offer_timer_id = -1;
02698    ast_cc_failed(agent->core_id, "Generic agent %s offer timer expired", agent->device_name);
02699    cc_unref(agent, "Remove scheduler's reference to the agent");
02700    return 0;
02701 }
02702 
02703 static int cc_generic_agent_start_offer_timer(struct ast_cc_agent *agent)
02704 {
02705    int when;
02706    int sched_id;
02707    struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
02708 
02709    ast_assert(cc_sched_context != NULL);
02710    ast_assert(agent->cc_params != NULL);
02711 
02712    when = ast_get_cc_offer_timer(agent->cc_params) * 1000;
02713    ast_log_dynamic_level(cc_logger_level, "Core %u: About to schedule offer timer expiration for %d ms\n",
02714          agent->core_id, when);
02715    if ((sched_id = ast_sched_add(cc_sched_context, when, offer_timer_expire, cc_ref(agent, "Give scheduler an agent ref"))) == -1) {
02716       return -1;
02717    }
02718    generic_pvt->offer_timer_id = sched_id;
02719    return 0;
02720 }
02721 
02722 static int cc_generic_agent_stop_offer_timer(struct ast_cc_agent *agent)
02723 {
02724    struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
02725 
02726    if (generic_pvt->offer_timer_id != -1) {
02727       if (!ast_sched_del(cc_sched_context, generic_pvt->offer_timer_id)) {
02728          cc_unref(agent, "Remove scheduler's reference to the agent");
02729       }
02730       generic_pvt->offer_timer_id = -1;
02731    }
02732    return 0;
02733 }
02734 
02735 static void cc_generic_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason)
02736 {
02737    /* The generic agent doesn't have to do anything special to
02738     * acknowledge a CC request. Just return.
02739     */
02740    return;
02741 }
02742 
02743 static int cc_generic_agent_status_request(struct ast_cc_agent *agent)
02744 {
02745    ast_cc_agent_status_response(agent->core_id, ast_device_state(agent->device_name));
02746    return 0;
02747 }
02748 
02749 static int cc_generic_agent_stop_ringing(struct ast_cc_agent *agent)
02750 {
02751    struct ast_channel *recall_chan = ast_channel_get_by_name_prefix(agent->device_name, strlen(agent->device_name));
02752 
02753    if (!recall_chan) {
02754       return 0;
02755    }
02756 
02757    ast_softhangup(recall_chan, AST_SOFTHANGUP_EXPLICIT);
02758    return 0;
02759 }
02760 
02761 static void generic_agent_devstate_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
02762 {
02763    struct ast_cc_agent *agent = userdata;
02764    enum ast_device_state new_state;
02765    struct ast_device_state_message *dev_state;
02766    struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
02767 
02768    if (stasis_subscription_final_message(sub, msg)) {
02769       cc_unref(agent, "Done holding ref for subscription");
02770       return;
02771    } else if (ast_device_state_message_type() != stasis_message_type(msg)) {
02772       return;
02773    }
02774 
02775    dev_state = stasis_message_data(msg);
02776    if (dev_state->eid) {
02777       /* ignore non-aggregate states */
02778       return;
02779    }
02780 
02781    new_state = dev_state->state;
02782    if (!cc_generic_is_device_available(new_state)) {
02783       /* Not interested in this new state of the device.  It is still busy. */
02784       return;
02785    }
02786 
02787    generic_pvt->sub = stasis_unsubscribe(sub);
02788    ast_cc_agent_caller_available(agent->core_id, "%s is no longer busy", agent->device_name);
02789 }
02790 
02791 static int cc_generic_agent_start_monitoring(struct ast_cc_agent *agent)
02792 {
02793    struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
02794    struct ast_str *str = ast_str_alloca(128);
02795    struct stasis_topic *device_specific_topic;
02796 
02797    ast_assert(generic_pvt->sub == NULL);
02798    ast_str_set(&str, 0, "Agent monitoring %s device state since it is busy\n",
02799       agent->device_name);
02800 
02801    device_specific_topic = ast_device_state_topic(agent->device_name);
02802    if (!device_specific_topic) {
02803       return -1;
02804    }
02805 
02806    if (!(generic_pvt->sub = stasis_subscribe(device_specific_topic, generic_agent_devstate_cb, agent))) {
02807       return -1;
02808    }
02809    cc_ref(agent, "Ref agent for subscription");
02810    return 0;
02811 }
02812 
02813 static void *generic_recall(void *data)
02814 {
02815    struct ast_cc_agent *agent = data;
02816    struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
02817    const char *interface = S_OR(ast_get_cc_agent_dialstring(agent->cc_params), ast_strdupa(agent->device_name));
02818    const char *tech;
02819    char *target;
02820    int reason;
02821    struct ast_channel *chan;
02822    const char *callback_macro = ast_get_cc_callback_macro(agent->cc_params);
02823    const char *callback_sub = ast_get_cc_callback_sub(agent->cc_params);
02824    unsigned int recall_timer = ast_get_cc_recall_timer(agent->cc_params) * 1000;
02825    struct ast_format_cap *tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
02826 
02827    if (!tmp_cap) {
02828       return NULL;
02829    }
02830 
02831    tech = interface;
02832    if ((target = strchr(interface, '/'))) {
02833       *target++ = '\0';
02834    }
02835 
02836    ast_format_cap_append(tmp_cap, ast_format_slin, 0);
02837    if (!(chan = ast_request_and_dial(tech, tmp_cap, NULL, NULL, target, recall_timer, &reason, generic_pvt->cid_num, generic_pvt->cid_name))) {
02838       /* Hmm, no channel. Sucks for you, bud.
02839        */
02840       ast_log_dynamic_level(cc_logger_level, "Core %u: Failed to call back %s for reason %d\n",
02841             agent->core_id, agent->device_name, reason);
02842       ast_cc_failed(agent->core_id, "Failed to call back device %s/%s", tech, target);
02843       ao2_ref(tmp_cap, -1);
02844       return NULL;
02845    }
02846    ao2_ref(tmp_cap, -1);
02847    
02848    /* We have a channel. It's time now to set up the datastore of recalled CC interfaces.
02849     * This will be a common task for all recall functions. If it were possible, I'd have
02850     * the core do it automatically, but alas I cannot. Instead, I will provide a public
02851     * function to do so.
02852     */
02853    ast_setup_cc_recall_datastore(chan, agent->core_id);
02854    ast_cc_agent_set_interfaces_chanvar(chan);
02855 
02856    ast_channel_exten_set(chan, generic_pvt->exten);
02857    ast_channel_context_set(chan, generic_pvt->context);
02858    ast_channel_priority_set(chan, 1);
02859 
02860    pbx_builtin_setvar_helper(chan, "CC_EXTEN", generic_pvt->exten);
02861    pbx_builtin_setvar_helper(chan, "CC_CONTEXT", generic_pvt->context);
02862 
02863    if (!ast_strlen_zero(callback_macro)) {
02864       ast_log_dynamic_level(cc_logger_level, "Core %u: There's a callback macro configured for agent %s\n",
02865             agent->core_id, agent->device_name);
02866       if (ast_app_exec_macro(NULL, chan, callback_macro)) {
02867          ast_cc_failed(agent->core_id, "Callback macro to %s failed. Maybe a hangup?", agent->device_name);
02868          ast_hangup(chan);
02869          return NULL;
02870       }
02871    }
02872 
02873    if (!ast_strlen_zero(callback_sub)) {
02874       ast_log_dynamic_level(cc_logger_level, "Core %u: There's a callback subroutine configured for agent %s\n",
02875             agent->core_id, agent->device_name);
02876       if (ast_app_exec_sub(NULL, chan, callback_sub, 0)) {
02877          ast_cc_failed(agent->core_id, "Callback subroutine to %s failed. Maybe a hangup?", agent->device_name);
02878          ast_hangup(chan);
02879          return NULL;
02880       }
02881    }
02882    if (ast_pbx_start(chan)) {
02883       ast_cc_failed(agent->core_id, "PBX failed to start for %s.", agent->device_name);
02884       ast_hangup(chan);
02885       return NULL;
02886    }
02887    ast_cc_agent_recalling(agent->core_id, "Generic agent %s is recalling",
02888       agent->device_name);
02889    return NULL;
02890 }
02891 
02892 static int cc_generic_agent_recall(struct ast_cc_agent *agent)
02893 {
02894    pthread_t clotho;
02895    enum ast_device_state current_state = ast_device_state(agent->device_name);
02896 
02897    if (!cc_generic_is_device_available(current_state)) {
02898       /* We can't try to contact the device right now because he's not available
02899        * Let the core know he's busy.
02900        */
02901       ast_cc_agent_caller_busy(agent->core_id, "Generic agent caller %s is busy", agent->device_name);
02902       return 0;
02903    }
02904    ast_pthread_create_detached_background(&clotho, NULL, generic_recall, agent);
02905    return 0;
02906 }
02907 
02908 static void cc_generic_agent_destructor(struct ast_cc_agent *agent)
02909 {
02910    struct cc_generic_agent_pvt *agent_pvt = agent->private_data;
02911 
02912    if (!agent_pvt) {
02913       /* The agent constructor probably failed. */
02914       return;
02915    }
02916 
02917    cc_generic_agent_stop_offer_timer(agent);
02918    if (agent_pvt->sub) {
02919       agent_pvt->sub = stasis_unsubscribe(agent_pvt->sub);
02920    }
02921 
02922    ast_free(agent_pvt);
02923 }
02924 
02925 static void cc_core_instance_destructor(void *data)
02926 {
02927    struct cc_core_instance *core_instance = data;
02928    ast_log_dynamic_level(cc_logger_level, "Core %d: Destroying core instance\n", core_instance->core_id);
02929    if (core_instance->agent) {
02930       cc_unref(core_instance->agent, "Core instance is done with the agent now");
02931    }
02932    if (core_instance->monitors) {
02933       core_instance->monitors = cc_unref(core_instance->monitors, "Core instance is done with interface list");
02934    }
02935 }
02936 
02937 static struct cc_core_instance *cc_core_init_instance(struct ast_channel *caller_chan,
02938       struct cc_monitor_tree *called_tree, const int core_id, struct cc_control_payload *cc_data)
02939 {
02940    char caller[AST_CHANNEL_NAME];
02941    struct cc_core_instance *core_instance;
02942    struct ast_cc_config_params *cc_params;
02943    long agent_count;
02944    int recall_core_id;
02945 
02946    ast_channel_get_device_name(caller_chan, caller, sizeof(caller));
02947    cc_params = ast_channel_get_cc_config_params(caller_chan);
02948    if (!cc_params) {
02949       ast_log_dynamic_level(cc_logger_level, "Could not get CC parameters for %s\n",
02950          caller);
02951       return NULL;
02952    }
02953    /* First, we need to kill off other pending CC offers from caller. If the caller is going
02954     * to request a CC service, it may only be for the latest call he made.
02955     */
02956    if (ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_GENERIC) {
02957       kill_duplicate_offers(caller);
02958    }
02959 
02960    ast_cc_is_recall(caller_chan, &recall_core_id, NULL);
02961    agent_count = count_agents(caller, recall_core_id);
02962    if (agent_count >= ast_get_cc_max_agents(cc_params)) {
02963       ast_log_dynamic_level(cc_logger_level, "Caller %s already has the maximum number of agents configured\n", caller);
02964       return NULL;
02965    }
02966 
02967    /* Generic agents can only have a single outstanding CC request per caller. */
02968    if (agent_count > 0 && ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_GENERIC) {
02969       ast_log_dynamic_level(cc_logger_level, "Generic agents can only have a single outstanding request\n");
02970       return NULL;
02971    }
02972 
02973    /* Next, we need to create the core instance for this call */
02974    if (!(core_instance = ao2_t_alloc(sizeof(*core_instance), cc_core_instance_destructor, "Creating core instance for CC"))) {
02975       return NULL;
02976    }
02977 
02978    core_instance->core_id = core_id;
02979    if (!(core_instance->agent = cc_agent_init(caller_chan, caller, core_instance->core_id, called_tree))) {
02980       cc_unref(core_instance, "Couldn't allocate agent, unref core_instance");
02981       return NULL;
02982    }
02983 
02984    core_instance->monitors = cc_ref(called_tree, "Core instance getting ref to monitor tree");
02985 
02986    ao2_t_link(cc_core_instances, core_instance, "Link core instance into container");
02987 
02988    return core_instance;
02989 }
02990 
02991 struct cc_state_change_args {
02992    struct cc_core_instance *core_instance;/*!< Holds reference to core instance. */
02993    enum cc_state state;
02994    int core_id;
02995    char debug[1];
02996 };
02997 
02998 static int is_state_change_valid(enum cc_state current_state, const enum cc_state new_state, struct ast_cc_agent *agent)
02999 {
03000    int is_valid = 0;
03001    switch (new_state) {
03002    case CC_AVAILABLE:
03003       ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to state %u? That should never happen.\n",
03004             agent->core_id, new_state);
03005       break;
03006    case CC_CALLER_OFFERED:
03007       if (current_state == CC_AVAILABLE) {
03008          is_valid = 1;
03009       }
03010       break;
03011    case CC_CALLER_REQUESTED:
03012       if (current_state == CC_CALLER_OFFERED ||
03013             (current_state == CC_AVAILABLE && ast_test_flag(agent, AST_CC_AGENT_SKIP_OFFER))) {
03014          is_valid = 1;
03015       }
03016       break;
03017    case CC_ACTIVE:
03018       if (current_state == CC_CALLER_REQUESTED || current_state == CC_CALLER_BUSY) {
03019          is_valid = 1;
03020       }
03021       break;
03022    case CC_CALLEE_READY:
03023       if (current_state == CC_ACTIVE) {
03024          is_valid = 1;
03025       }
03026       break;
03027    case CC_CALLER_BUSY:
03028       if (current_state == CC_CALLEE_READY) {
03029          is_valid = 1;
03030       }
03031       break;
03032    case CC_RECALLING:
03033       if (current_state == CC_CALLEE_READY) {
03034          is_valid = 1;
03035       }
03036       break;
03037    case CC_COMPLETE:
03038       if (current_state == CC_RECALLING) {
03039          is_valid = 1;
03040       }
03041       break;
03042    case CC_FAILED:
03043       is_valid = 1;
03044       break;
03045    default:
03046       ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to unknown state %u\n",
03047             agent->core_id, new_state);
03048       break;
03049    }
03050 
03051    return is_valid;
03052 }
03053 
03054 static int cc_available(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
03055 {
03056    /* This should never happen... */
03057    ast_log(LOG_WARNING, "Someone requested to change to CC_AVAILABLE? Ignoring.\n");
03058    return -1;
03059 }
03060 
03061 static int cc_caller_offered(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
03062 {
03063    if (core_instance->agent->callbacks->start_offer_timer(core_instance->agent)) {
03064       ast_cc_failed(core_instance->core_id, "Failed to start the offer timer for %s\n",
03065             core_instance->agent->device_name);
03066       return -1;
03067    }
03068    cc_publish_offertimerstart(core_instance->core_id, core_instance->agent->device_name, core_instance->agent->cc_params->cc_offer_timer);
03069    ast_log_dynamic_level(cc_logger_level, "Core %d: Started the offer timer for the agent %s!\n",
03070          core_instance->core_id, core_instance->agent->device_name);
03071    return 0;
03072 }
03073 
03074 /*!
03075  * \brief check if the core instance has any device monitors
03076  *
03077  * In any case where we end up removing a device monitor from the
03078  * list of device monitors, it is important to see what the state
03079  * of the list is afterwards. If we find that we only have extension
03080  * monitors left, then no devices are actually being monitored.
03081  * In such a case, we need to declare that CC has failed for this
03082  * call. This function helps those cases to determine if they should
03083  * declare failure.
03084  *
03085  * \param core_instance The core instance we are checking for the existence
03086  * of device monitors
03087  * \retval 0 No device monitors exist on this core_instance
03088  * \retval 1 There is still at least 1 device monitor remaining
03089  */
03090 static int has_device_monitors(struct cc_core_instance *core_instance)
03091 {
03092    struct ast_cc_monitor *iter;
03093    int res = 0;
03094 
03095    AST_LIST_TRAVERSE(core_instance->monitors, iter, next) {
03096       if (iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
03097          res = 1;
03098          break;
03099       }
03100    }
03101 
03102    return res;
03103 }
03104 
03105 static void request_cc(struct cc_core_instance *core_instance)
03106 {
03107    struct ast_cc_monitor *monitor_iter;
03108    AST_LIST_LOCK(core_instance->monitors);
03109    AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
03110       if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
03111          if (monitor_iter->callbacks->request_cc(monitor_iter, &monitor_iter->available_timer_id)) {
03112             AST_LIST_REMOVE_CURRENT(next);
03113             cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
03114                   monitor_iter->interface->device_name, 1);
03115             cc_unref(monitor_iter, "request_cc failed. Unref list's reference to monitor");
03116          } else {
03117             cc_publish_requested(core_instance->core_id, core_instance->agent->device_name, monitor_iter->interface->device_name);
03118          }
03119       }
03120    }
03121    AST_LIST_TRAVERSE_SAFE_END;
03122 
03123    if (!has_device_monitors(core_instance)) {
03124       ast_cc_failed(core_instance->core_id, "All device monitors failed to request CC");
03125    }
03126    AST_LIST_UNLOCK(core_instance->monitors);
03127 }
03128 
03129 static int cc_caller_requested(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
03130 {
03131    if (!ast_cc_request_is_within_limits()) {
03132       ast_log(LOG_WARNING, "Cannot request CC since there is no more room for requests\n");
03133       core_instance->agent->callbacks->respond(core_instance->agent,
03134          AST_CC_AGENT_RESPONSE_FAILURE_TOO_MANY);
03135       ast_cc_failed(core_instance->core_id, "Too many requests in the system");
03136       return -1;
03137    }
03138    core_instance->agent->callbacks->stop_offer_timer(core_instance->agent);
03139    request_cc(core_instance);
03140    return 0;
03141 }
03142 
03143 static void unsuspend(struct cc_core_instance *core_instance)
03144 {
03145    struct ast_cc_monitor *monitor_iter;
03146    AST_LIST_LOCK(core_instance->monitors);
03147    AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
03148       if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
03149          if (monitor_iter->callbacks->unsuspend(monitor_iter)) {
03150             AST_LIST_REMOVE_CURRENT(next);
03151             cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
03152                   monitor_iter->interface->device_name, 1);
03153             cc_unref(monitor_iter, "unsuspend failed. Unref list's reference to monitor");
03154          }
03155       }
03156    }
03157    AST_LIST_TRAVERSE_SAFE_END;
03158 
03159    if (!has_device_monitors(core_instance)) {
03160       ast_cc_failed(core_instance->core_id, "All device monitors failed to unsuspend CC");
03161    }
03162    AST_LIST_UNLOCK(core_instance->monitors);
03163 }
03164 
03165 static int cc_active(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
03166 {
03167    /* Either
03168     * 1. Callee accepted CC request, call agent's ack callback.
03169     * 2. Caller became available, call agent's stop_monitoring callback and
03170     *    call monitor's unsuspend callback.
03171     */
03172    if (previous_state == CC_CALLER_REQUESTED) {
03173       core_instance->agent->callbacks->respond(core_instance->agent,
03174          AST_CC_AGENT_RESPONSE_SUCCESS);
03175       cc_publish_requestacknowledged(core_instance->core_id, core_instance->agent->device_name);
03176    } else if (previous_state == CC_CALLER_BUSY) {
03177       cc_publish_callerstopmonitoring(core_instance->core_id, core_instance->agent->device_name);
03178       unsuspend(core_instance);
03179    }
03180    /* Not possible for previous_state to be anything else due to the is_state_change_valid check at the beginning */
03181    return 0;
03182 }
03183 
03184 static int cc_callee_ready(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
03185 {
03186    core_instance->agent->callbacks->callee_available(core_instance->agent);
03187    return 0;
03188 }
03189 
03190 static void suspend(struct cc_core_instance *core_instance)
03191 {
03192    struct ast_cc_monitor *monitor_iter;
03193    AST_LIST_LOCK(core_instance->monitors);
03194    AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
03195       if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
03196          if (monitor_iter->callbacks->suspend(monitor_iter)) {
03197             AST_LIST_REMOVE_CURRENT(next);
03198             cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
03199                   monitor_iter->interface->device_name, 1);
03200             cc_unref(monitor_iter, "suspend failed. Unref list's reference to monitor");
03201          }
03202       }
03203    }
03204    AST_LIST_TRAVERSE_SAFE_END;
03205 
03206    if (!has_device_monitors(core_instance)) {
03207       ast_cc_failed(core_instance->core_id, "All device monitors failed to suspend CC");
03208    }
03209    AST_LIST_UNLOCK(core_instance->monitors);
03210 }
03211 
03212 static int cc_caller_busy(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
03213 {
03214    /* Callee was available, but caller was busy, call agent's begin_monitoring callback
03215     * and call monitor's suspend callback.
03216     */
03217    suspend(core_instance);
03218    core_instance->agent->callbacks->start_monitoring(core_instance->agent);
03219    cc_publish_callerstartmonitoring(core_instance->core_id, core_instance->agent->device_name);
03220    return 0;
03221 }
03222 
03223 static void cancel_available_timer(struct cc_core_instance *core_instance)
03224 {
03225    struct ast_cc_monitor *monitor_iter;
03226    AST_LIST_LOCK(core_instance->monitors);
03227    AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
03228       if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
03229          if (monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id)) {
03230             AST_LIST_REMOVE_CURRENT(next);
03231             cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
03232                   monitor_iter->interface->device_name, 1);
03233             cc_unref(monitor_iter, "cancel_available_timer failed. Unref list's reference to monitor");
03234          }
03235       }
03236    }
03237    AST_LIST_TRAVERSE_SAFE_END;
03238 
03239    if (!has_device_monitors(core_instance)) {
03240       ast_cc_failed(core_instance->core_id, "All device monitors failed to cancel their available timers");
03241    }
03242    AST_LIST_UNLOCK(core_instance->monitors);
03243 }
03244 
03245 static int cc_recalling(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
03246 {
03247    /* Both caller and callee are available, call agent's recall callback
03248     */
03249    cancel_available_timer(core_instance);
03250    cc_publish_callerrecalling(core_instance->core_id, core_instance->agent->device_name);
03251    return 0;
03252 }
03253 
03254 static int cc_complete(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
03255 {
03256    /* Recall has made progress, call agent and monitor destructor functions
03257     */
03258    cc_publish_recallcomplete(core_instance->core_id, core_instance->agent->device_name);
03259    ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC recall has completed");
03260    return 0;
03261 }
03262 
03263 static int cc_failed(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
03264 {
03265    cc_publish_failure(core_instance->core_id, core_instance->agent->device_name, args->debug);
03266    ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC failed");
03267    return 0;
03268 }
03269 
03270 static int (* const state_change_funcs [])(struct cc_core_instance *, struct cc_state_change_args *, enum cc_state previous_state) = {
03271    [CC_AVAILABLE] = cc_available,
03272    [CC_CALLER_OFFERED] = cc_caller_offered,
03273    [CC_CALLER_REQUESTED] = cc_caller_requested,
03274    [CC_ACTIVE] = cc_active,
03275    [CC_CALLEE_READY] = cc_callee_ready,
03276    [CC_CALLER_BUSY] = cc_caller_busy,
03277    [CC_RECALLING] = cc_recalling,
03278    [CC_COMPLETE] = cc_complete,
03279    [CC_FAILED] = cc_failed,
03280 };
03281 
03282 static int cc_do_state_change(void *datap)
03283 {
03284    struct cc_state_change_args *args = datap;
03285    struct cc_core_instance *core_instance;
03286    enum cc_state previous_state;
03287    int res;
03288 
03289    ast_log_dynamic_level(cc_logger_level, "Core %d: State change to %u requested. Reason: %s\n",
03290          args->core_id, args->state, args->debug);
03291 
03292    core_instance = args->core_instance;
03293 
03294    if (!is_state_change_valid(core_instance->current_state, args->state, core_instance->agent)) {
03295       ast_log_dynamic_level(cc_logger_level, "Core %d: Invalid state change requested. Cannot go from %s to %s\n",
03296             args->core_id, cc_state_to_string(core_instance->current_state), cc_state_to_string(args->state));
03297       if (args->state == CC_CALLER_REQUESTED) {
03298          /*
03299           * For out-of-order requests, we need to let the requester know that
03300           * we can't handle the request now.
03301           */
03302          core_instance->agent->callbacks->respond(core_instance->agent,
03303             AST_CC_AGENT_RESPONSE_FAILURE_INVALID);
03304       }
03305       ast_free(args);
03306       cc_unref(core_instance, "Unref core instance from when it was found earlier");
03307       return -1;
03308    }
03309 
03310    /* We can change to the new state now. */
03311    previous_state = core_instance->current_state;
03312    core_instance->current_state = args->state;
03313    res = state_change_funcs[core_instance->current_state](core_instance, args, previous_state);
03314 
03315    /* If state change successful then notify any device state watchers of the change */
03316    if (!res && !strcmp(core_instance->agent->callbacks->type, "generic")) {
03317       ccss_notify_device_state_change(core_instance->agent->device_name, core_instance->current_state);
03318    }
03319 
03320    ast_free(args);
03321    cc_unref(core_instance, "Unref since state change has completed"); /* From ao2_find */
03322    return res;
03323 }
03324 
03325 static int cc_request_state_change(enum cc_state state, const int core_id, const char *debug, va_list ap)
03326 {
03327    int res;
03328    int debuglen;
03329    char dummy[1];
03330    va_list aq;
03331    struct cc_core_instance *core_instance;
03332    struct cc_state_change_args *args;
03333    /* This initial call to vsnprintf is simply to find what the
03334     * size of the string needs to be
03335     */
03336    va_copy(aq, ap);
03337    /* We add 1 to the result since vsnprintf's return does not
03338     * include the terminating null byte
03339     */
03340    debuglen = vsnprintf(dummy, sizeof(dummy), debug, aq) + 1;
03341    va_end(aq);
03342 
03343    if (!(args = ast_calloc(1, sizeof(*args) + debuglen))) {
03344       return -1;
03345    }
03346 
03347    core_instance = find_cc_core_instance(core_id);
03348    if (!core_instance) {
03349       ast_log_dynamic_level(cc_logger_level, "Core %d: Unable to find core instance.\n",
03350          core_id);
03351       ast_free(args);
03352       return -1;
03353    }
03354 
03355    args->core_instance = core_instance;
03356    args->state = state;
03357    args->core_id = core_id;
03358    vsnprintf(args->debug, debuglen, debug, ap);
03359 
03360    res = ast_taskprocessor_push(cc_core_taskprocessor, cc_do_state_change, args);
03361    if (res) {
03362       cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
03363       ast_free(args);
03364    }
03365    return res;
03366 }
03367 
03368 struct cc_recall_ds_data {
03369    int core_id;
03370    char ignore;
03371    char nested;
03372    struct cc_monitor_tree *interface_tree;
03373 };
03374 
03375 static void *cc_recall_ds_duplicate(void *data)
03376 {
03377    struct cc_recall_ds_data *old_data = data;
03378    struct cc_recall_ds_data *new_data = ast_calloc(1, sizeof(*new_data));
03379 
03380    if (!new_data) {
03381       return NULL;
03382    }
03383    new_data->interface_tree = cc_ref(old_data->interface_tree, "Bump refcount of monitor tree for recall datastore duplicate");
03384    new_data->core_id = old_data->core_id;
03385    new_data->nested = 1;
03386    return new_data;
03387 }
03388 
03389 static void cc_recall_ds_destroy(void *data)
03390 {
03391    struct cc_recall_ds_data *recall_data = data;
03392    recall_data->interface_tree = cc_unref(recall_data->interface_tree, "Unref recall monitor tree");
03393    ast_free(recall_data);
03394 }
03395 
03396 static const struct ast_datastore_info recall_ds_info = {
03397    .type = "cc_recall",
03398    .duplicate = cc_recall_ds_duplicate,
03399    .destroy = cc_recall_ds_destroy,
03400 };
03401 
03402 int ast_setup_cc_recall_datastore(struct ast_channel *chan, const int core_id)
03403 {
03404    struct ast_datastore *recall_datastore = ast_datastore_alloc(&recall_ds_info, NULL);
03405    struct cc_recall_ds_data *recall_data;
03406    struct cc_core_instance *core_instance;
03407 
03408    if (!recall_datastore) {
03409       return -1;
03410    }
03411 
03412    if (!(recall_data = ast_calloc(1, sizeof(*recall_data)))) {
03413       ast_datastore_free(recall_datastore);
03414       return -1;
03415    }
03416 
03417    if (!(core_instance = find_cc_core_instance(core_id))) {
03418       ast_free(recall_data);
03419       ast_datastore_free(recall_datastore);
03420       return -1;
03421    }
03422 
03423    recall_data->interface_tree = cc_ref(core_instance->monitors,
03424          "Bump refcount for monitor tree for recall datastore");
03425    recall_data->core_id = core_id;
03426    recall_datastore->data = recall_data;
03427    recall_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
03428    ast_channel_lock(chan);
03429    ast_channel_datastore_add(chan, recall_datastore);
03430    ast_channel_unlock(chan);
03431    cc_unref(core_instance, "Recall datastore set up. No need for core_instance ref");
03432    return 0;
03433 }
03434 
03435 int ast_cc_is_recall(struct ast_channel *chan, int *core_id, const char * const monitor_type)
03436 {
03437    struct ast_datastore *recall_datastore;
03438    struct cc_recall_ds_data *recall_data;
03439    struct cc_monitor_tree *interface_tree;
03440    char device_name[AST_CHANNEL_NAME];
03441    struct ast_cc_monitor *device_monitor;
03442    int core_id_candidate;
03443 
03444    ast_assert(core_id != NULL);
03445 
03446    *core_id = -1;
03447 
03448    ast_channel_lock(chan);
03449    if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
03450       /* Obviously not a recall if the datastore isn't present */
03451       ast_channel_unlock(chan);
03452       return 0;
03453    }
03454 
03455    recall_data = recall_datastore->data;
03456 
03457    if (recall_data->ignore) {
03458       /* Though this is a recall, the call to this particular interface is not part of the
03459        * recall either because this is a call forward or because this is not the first
03460        * invocation of Dial during this call
03461        */
03462       ast_channel_unlock(chan);
03463       return 0;
03464    }
03465 
03466    if (!recall_data->nested) {
03467       /* If the nested flag is not set, then this means that
03468        * the channel passed to this function is the caller making
03469        * the recall. This means that we shouldn't look through
03470        * the monitor tree for the channel because it shouldn't be
03471        * there. However, this is a recall though, so return true.
03472        */
03473       *core_id = recall_data->core_id;
03474       ast_channel_unlock(chan);
03475       return 1;
03476    }
03477 
03478    if (ast_strlen_zero(monitor_type)) {
03479       /* If someone passed a NULL or empty monitor type, then it is clear
03480        * the channel they passed in was an incoming channel, and so searching
03481        * the list of dialed interfaces is not going to be helpful. Just return
03482        * false immediately.
03483        */
03484       ast_channel_unlock(chan);
03485       return 0;
03486    }
03487 
03488    interface_tree = recall_data->interface_tree;
03489    ast_channel_get_device_name(chan, device_name, sizeof(device_name));
03490    /* We grab the value of the recall_data->core_id so that we
03491     * can unlock the channel before we start looking through the
03492     * interface list. That way we don't have to worry about a possible
03493     * clash between the channel lock and the monitor tree lock.
03494     */
03495    core_id_candidate = recall_data->core_id;
03496    ast_channel_unlock(chan);
03497 
03498    /*
03499     * Now we need to find out if the channel device name
03500     * is in the list of interfaces in the called tree.
03501     */
03502    AST_LIST_LOCK(interface_tree);
03503    AST_LIST_TRAVERSE(interface_tree, device_monitor, next) {
03504       if (!strcmp(device_monitor->interface->device_name, device_name) &&
03505             !strcmp(device_monitor->interface->monitor_type, monitor_type)) {
03506          /* BOOM! Device is in the tree! We have a winner! */
03507          *core_id = core_id_candidate;
03508          AST_LIST_UNLOCK(interface_tree);
03509          return 1;
03510       }
03511    }
03512    AST_LIST_UNLOCK(interface_tree);
03513    return 0;
03514 }
03515 
03516 struct ast_cc_monitor *ast_cc_get_monitor_by_recall_core_id(const int core_id, const char * const device_name)
03517 {
03518    struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
03519    struct ast_cc_monitor *monitor_iter;
03520 
03521    if (!core_instance) {
03522       return NULL;
03523    }
03524 
03525    AST_LIST_LOCK(core_instance->monitors);
03526    AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
03527       if (!strcmp(monitor_iter->interface->device_name, device_name)) {
03528          /* Found a monitor. */
03529          cc_ref(monitor_iter, "Hand the requester of the monitor a reference");
03530          break;
03531       }
03532    }
03533    AST_LIST_UNLOCK(core_instance->monitors);
03534    cc_unref(core_instance, "Done with core instance ref in ast_cc_get_monitor_by_recall_core_id");
03535    return monitor_iter;
03536 }
03537 
03538 /*!
03539  * \internal
03540  * \brief uniquely append a dialstring to our CC_INTERFACES chanvar string.
03541  *
03542  * We will only append a string if it has not already appeared in our channel
03543  * variable earlier. We ensure that we don't erroneously match substrings by
03544  * adding an ampersand to the end of our potential dialstring and searching for
03545  * it plus the ampersand in our variable.
03546  *
03547  * It's important to note that once we have built the full CC_INTERFACES string,
03548  * there will be an extra ampersand at the end which must be stripped off by
03549  * the caller of this function.
03550  *
03551  * \param str An ast_str holding what we will add to CC_INTERFACES
03552  * \param dialstring A new dialstring to add
03553  * \retval void
03554  */
03555 static void cc_unique_append(struct ast_str **str, const char *dialstring)
03556 {
03557    char dialstring_search[AST_CHANNEL_NAME];
03558 
03559    if (ast_strlen_zero(dialstring)) {
03560       /* No dialstring to append. */
03561       return;
03562    }
03563    snprintf(dialstring_search, sizeof(dialstring_search), "%s%c", dialstring, '&');
03564    if (strstr(ast_str_buffer(*str), dialstring_search)) {
03565       return;
03566    }
03567    ast_str_append(str, 0, "%s", dialstring_search);
03568 }
03569 
03570 /*!
03571  * \internal
03572  * \brief Build the CC_INTERFACES channel variable
03573  *
03574  * The method used is to traverse the child dialstrings in the
03575  * passed-in extension monitor, adding any that have the is_valid
03576  * flag set. Then, traverse the monitors, finding all children
03577  * of the starting extension monitor and adding their dialstrings
03578  * as well.
03579  *
03580  * \param starting_point The extension monitor that is the parent to all
03581  * monitors whose dialstrings should be added to CC_INTERFACES
03582  * \param str Where we will store CC_INTERFACES
03583  * \retval void
03584  */
03585 static void build_cc_interfaces_chanvar(struct ast_cc_monitor *starting_point, struct ast_str **str)
03586 {
03587    struct extension_monitor_pvt *extension_pvt;
03588    struct extension_child_dialstring *child_dialstring;
03589    struct ast_cc_monitor *monitor_iter = starting_point;
03590    int top_level_id = starting_point->id;
03591    size_t length;
03592 
03593    /* Init to an empty string. */
03594    ast_str_truncate(*str, 0);
03595 
03596    /* First we need to take all of the is_valid child_dialstrings from
03597     * the extension monitor we found and add them to the CC_INTERFACES
03598     * chanvar
03599     */
03600    extension_pvt = starting_point->private_data;
03601    AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) {
03602       if (child_dialstring->is_valid) {
03603          cc_unique_append(str, child_dialstring->original_dialstring);
03604       }
03605    }
03606 
03607    /* And now we get the dialstrings from each of the device monitors */
03608    while ((monitor_iter = AST_LIST_NEXT(monitor_iter, next))) {
03609       if (monitor_iter->parent_id == top_level_id) {
03610          cc_unique_append(str, monitor_iter->dialstring);
03611       }
03612    }
03613 
03614    /* str will have an extra '&' tacked onto the end of it, so we need
03615     * to get rid of that.
03616     */
03617    length = ast_str_strlen(*str);
03618    if (length) {
03619       ast_str_truncate(*str, length - 1);
03620    }
03621    if (length <= 1) {
03622       /* Nothing to recall?  This should not happen. */
03623       ast_log(LOG_ERROR, "CC_INTERFACES is empty. starting device_name:'%s'\n",
03624          starting_point->interface->device_name);
03625    }
03626 }
03627 
03628 int ast_cc_agent_set_interfaces_chanvar(struct ast_channel *chan)
03629 {
03630    struct ast_datastore *recall_datastore;
03631    struct cc_monitor_tree *interface_tree;
03632    struct ast_cc_monitor *monitor;
03633    struct cc_recall_ds_data *recall_data;
03634    struct ast_str *str = ast_str_create(64);
03635    int core_id;
03636 
03637    if (!str) {
03638       return -1;
03639    }
03640 
03641    ast_channel_lock(chan);
03642    if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
03643       ast_channel_unlock(chan);
03644       ast_free(str);
03645       return -1;
03646    }
03647    recall_data = recall_datastore->data;
03648    interface_tree = recall_data->interface_tree;
03649    core_id = recall_data->core_id;
03650    ast_channel_unlock(chan);
03651 
03652    AST_LIST_LOCK(interface_tree);
03653    monitor = AST_LIST_FIRST(interface_tree);
03654    build_cc_interfaces_chanvar(monitor, &str);
03655    AST_LIST_UNLOCK(interface_tree);
03656 
03657    pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str));
03658    ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n",
03659          core_id, ast_str_buffer(str));
03660 
03661    ast_free(str);
03662    return 0;
03663 }
03664 
03665 int ast_set_cc_interfaces_chanvar(struct ast_channel *chan, const char * const extension)
03666 {
03667    struct ast_datastore *recall_datastore;
03668    struct cc_monitor_tree *interface_tree;
03669    struct ast_cc_monitor *monitor_iter;
03670    struct cc_recall_ds_data *recall_data;
03671    struct ast_str *str = ast_str_create(64);
03672    int core_id;
03673 
03674    if (!str) {
03675       return -1;
03676    }
03677 
03678    ast_channel_lock(chan);
03679    if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
03680       ast_channel_unlock(chan);
03681       ast_free(str);
03682       return -1;
03683    }
03684    recall_data = recall_datastore->data;
03685    interface_tree = recall_data->interface_tree;
03686    core_id = recall_data->core_id;
03687    ast_channel_unlock(chan);
03688 
03689    AST_LIST_LOCK(interface_tree);
03690    AST_LIST_TRAVERSE(interface_tree, monitor_iter, next) {
03691       if (!strcmp(monitor_iter->interface->device_name, extension)) {
03692          break;
03693       }
03694    }
03695 
03696    if (!monitor_iter) {
03697       /* We couldn't find this extension. This may be because
03698        * we have been directed into an unexpected extension because
03699        * the admin has changed a CC_INTERFACES variable at some point.
03700        */
03701       AST_LIST_UNLOCK(interface_tree);
03702       ast_free(str);
03703       return -1;
03704    }
03705 
03706    build_cc_interfaces_chanvar(monitor_iter, &str);
03707    AST_LIST_UNLOCK(interface_tree);
03708 
03709    pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str));
03710    ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n",
03711          core_id, ast_str_buffer(str));
03712 
03713    ast_free(str);
03714    return 0;
03715 }
03716 
03717 void ast_ignore_cc(struct ast_channel *chan)
03718 {
03719    struct ast_datastore *cc_datastore;
03720    struct ast_datastore *cc_recall_datastore;
03721    struct dialed_cc_interfaces *cc_interfaces;
03722    struct cc_recall_ds_data *recall_cc_data;
03723 
03724    ast_channel_lock(chan);
03725    if ((cc_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
03726       cc_interfaces = cc_datastore->data;
03727       cc_interfaces->ignore = 1;
03728    }
03729 
03730    if ((cc_recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
03731       recall_cc_data = cc_recall_datastore->data;
03732       recall_cc_data->ignore = 1;
03733    }
03734    ast_channel_unlock(chan);
03735 }
03736 
03737 static __attribute__((format(printf, 2, 3))) int cc_offer(const int core_id, const char * const debug, ...)
03738 {
03739    va_list ap;
03740    int res;
03741 
03742    va_start(ap, debug);
03743    res = cc_request_state_change(CC_CALLER_OFFERED, core_id, debug, ap);
03744    va_end(ap);
03745    return res;
03746 }
03747 
03748 int ast_cc_offer(struct ast_channel *caller_chan)
03749 {
03750    int core_id;
03751    int res = -1;
03752    struct ast_datastore *datastore;
03753    struct dialed_cc_interfaces *cc_interfaces;
03754    char cc_is_offerable;
03755 
03756    ast_channel_lock(caller_chan);
03757    if (!(datastore = ast_channel_datastore_find(caller_chan, &dialed_cc_interfaces_info, NULL))) {
03758       ast_channel_unlock(caller_chan);
03759       return res;
03760    }
03761 
03762    cc_interfaces = datastore->data;
03763    cc_is_offerable = cc_interfaces->is_original_caller;
03764    core_id = cc_interfaces->core_id;
03765    ast_channel_unlock(caller_chan);
03766 
03767    if (cc_is_offerable) {
03768       res = cc_offer(core_id, "CC offered to caller %s", ast_channel_name(caller_chan));
03769    }
03770    return res;
03771 }
03772 
03773 int ast_cc_agent_accept_request(int core_id, const char * const debug, ...)
03774 {
03775    va_list ap;
03776    int res;
03777 
03778    va_start(ap, debug);
03779    res = cc_request_state_change(CC_CALLER_REQUESTED, core_id, debug, ap);
03780    va_end(ap);
03781    return res;
03782 }
03783 
03784 int ast_cc_monitor_request_acked(int core_id, const char * const debug, ...)
03785 {
03786    va_list ap;
03787    int res;
03788 
03789    va_start(ap, debug);
03790    res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap);
03791    va_end(ap);
03792    return res;
03793 }
03794 
03795 int ast_cc_monitor_callee_available(const int core_id, const char * const debug, ...)
03796 {
03797    va_list ap;
03798    int res;
03799 
03800    va_start(ap, debug);
03801    res = cc_request_state_change(CC_CALLEE_READY, core_id, debug, ap);
03802    va_end(ap);
03803    return res;
03804 }
03805 
03806 int ast_cc_agent_caller_busy(int core_id, const char * debug, ...)
03807 {
03808    va_list ap;
03809    int res;
03810 
03811    va_start(ap, debug);
03812    res = cc_request_state_change(CC_CALLER_BUSY, core_id, debug, ap);
03813    va_end(ap);
03814    return res;
03815 }
03816 
03817 int ast_cc_agent_caller_available(int core_id, const char * const debug, ...)
03818 {
03819    va_list ap;
03820    int res;
03821 
03822    va_start(ap, debug);
03823    res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap);
03824    va_end(ap);
03825    return res;
03826 }
03827 
03828 int ast_cc_agent_recalling(int core_id, const char * const debug, ...)
03829 {
03830    va_list ap;
03831    int res;
03832 
03833    va_start(ap, debug);
03834    res = cc_request_state_change(CC_RECALLING, core_id, debug, ap);
03835    va_end(ap);
03836    return res;
03837 }
03838 
03839 int ast_cc_completed(struct ast_channel *chan, const char * const debug, ...)
03840 {
03841    struct ast_datastore *recall_datastore;
03842    struct cc_recall_ds_data *recall_data;
03843    int core_id;
03844    va_list ap;
03845    int res;
03846 
03847    ast_channel_lock(chan);
03848    if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
03849       /* Silly! Why did you call this function if there's no recall DS? */
03850       ast_channel_unlock(chan);
03851       return -1;
03852    }
03853    recall_data = recall_datastore->data;
03854    if (recall_data->nested || recall_data->ignore) {
03855       /* If this is being called from a nested Dial, it is too
03856        * early to determine if the recall has actually completed.
03857        * The outermost dial is the only one with the authority to
03858        * declare the recall to be complete.
03859        *
03860        * Similarly, if this function has been called when the
03861        * recall has progressed beyond the first dial, this is not
03862        * a legitimate time to declare the recall to be done. In fact,
03863        * that should have been done already.
03864        */
03865       ast_channel_unlock(chan);
03866       return -1;
03867    }
03868    core_id = recall_data->core_id;
03869    ast_channel_unlock(chan);
03870    va_start(ap, debug);
03871    res = cc_request_state_change(CC_COMPLETE, core_id, debug, ap);
03872    va_end(ap);
03873    return res;
03874 }
03875 
03876 int ast_cc_failed(int core_id, const char * const debug, ...)
03877 {
03878    va_list ap;
03879    int res;
03880 
03881    va_start(ap, debug);
03882    res = cc_request_state_change(CC_FAILED, core_id, debug, ap);
03883    va_end(ap);
03884    return res;
03885 }
03886 
03887 struct ast_cc_monitor_failure_data {
03888    const char *device_name;
03889    char *debug;
03890    int core_id;
03891 };
03892 
03893 static int cc_monitor_failed(void *data)
03894 {
03895    struct ast_cc_monitor_failure_data *failure_data = data;
03896    struct cc_core_instance *core_instance;
03897    struct ast_cc_monitor *monitor_iter;
03898 
03899    core_instance = find_cc_core_instance(failure_data->core_id);
03900    if (!core_instance) {
03901       /* Core instance no longer exists or invalid core_id. */
03902       ast_log_dynamic_level(cc_logger_level,
03903          "Core %d: Could not find core instance for device %s '%s'\n",
03904          failure_data->core_id, failure_data->device_name, failure_data->debug);
03905       ast_free((char *) failure_data->device_name);
03906       ast_free((char *) failure_data->debug);
03907       ast_free(failure_data);
03908       return -1;
03909    }
03910 
03911    AST_LIST_LOCK(core_instance->monitors);
03912    AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
03913       if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
03914          if (!strcmp(monitor_iter->interface->device_name, failure_data->device_name)) {
03915             AST_LIST_REMOVE_CURRENT(next);
03916             cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
03917                   monitor_iter->interface->device_name, 1);
03918             monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id);
03919             cc_publish_monitorfailed(monitor_iter->core_id, monitor_iter->interface->device_name);
03920             cc_unref(monitor_iter, "Monitor reported failure. Unref list's reference.");
03921          }
03922       }
03923    }
03924    AST_LIST_TRAVERSE_SAFE_END;
03925 
03926    if (!has_device_monitors(core_instance)) {
03927       ast_cc_failed(core_instance->core_id, "All monitors have failed\n");
03928    }
03929    AST_LIST_UNLOCK(core_instance->monitors);
03930    cc_unref(core_instance, "Finished with core_instance in cc_monitor_failed\n");
03931 
03932    ast_free((char *) failure_data->device_name);
03933    ast_free((char *) failure_data->debug);
03934    ast_free(failure_data);
03935    return 0;
03936 }
03937 
03938 int ast_cc_monitor_failed(int core_id, const char *const monitor_name, const char * const debug, ...)
03939 {
03940    struct ast_cc_monitor_failure_data *failure_data;
03941    int res;
03942    va_list ap;
03943 
03944    if (!(failure_data = ast_calloc(1, sizeof(*failure_data)))) {
03945       return -1;
03946    }
03947 
03948    if (!(failure_data->device_name = ast_strdup(monitor_name))) {
03949       ast_free(failure_data);
03950       return -1;
03951    }
03952 
03953    va_start(ap, debug);
03954    if (ast_vasprintf(&failure_data->debug, debug, ap) == -1) {
03955       va_end(ap);
03956       ast_free((char *)failure_data->device_name);
03957       ast_free(failure_data);
03958       return -1;
03959    }
03960    va_end(ap);
03961 
03962    failure_data->core_id = core_id;
03963 
03964    res = ast_taskprocessor_push(cc_core_taskprocessor, cc_monitor_failed, failure_data);
03965    if (res) {
03966       ast_free((char *)failure_data->device_name);
03967       ast_free((char *)failure_data->debug);
03968       ast_free(failure_data);
03969    }
03970    return res;
03971 }
03972 
03973 static int cc_status_request(void *data)
03974 {
03975    struct cc_core_instance *core_instance= data;
03976    int res;
03977 
03978    res = core_instance->agent->callbacks->status_request(core_instance->agent);
03979    cc_unref(core_instance, "Status request finished. Unref core instance");
03980    return res;
03981 }
03982 
03983 int ast_cc_monitor_status_request(int core_id)
03984 {
03985    int res;
03986    struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
03987 
03988    if (!core_instance) {
03989       return -1;
03990    }
03991 
03992    res = ast_taskprocessor_push(cc_core_taskprocessor, cc_status_request, core_instance);
03993    if (res) {
03994       cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
03995    }
03996    return res;
03997 }
03998 
03999 static int cc_stop_ringing(void *data)
04000 {
04001    struct cc_core_instance *core_instance = data;
04002    int res = 0;
04003 
04004    if (core_instance->agent->callbacks->stop_ringing) {
04005       res = core_instance->agent->callbacks->stop_ringing(core_instance->agent);
04006    }
04007    /* If an agent is being asked to stop ringing, then he needs to be prepared if for
04008     * whatever reason he needs to be called back again. The proper state to be in to
04009     * detect such a circumstance is the CC_ACTIVE state.
04010     *
04011     * We get to this state using the slightly unintuitive method of calling
04012     * ast_cc_monitor_request_acked because it gets us to the proper state.
04013     */
04014    ast_cc_monitor_request_acked(core_instance->core_id, "Agent %s asked to stop ringing. Be prepared to be recalled again.",
04015          core_instance->agent->device_name);
04016    cc_unref(core_instance, "Stop ringing finished. Unref core_instance");
04017    return res;
04018 }
04019 
04020 int ast_cc_monitor_stop_ringing(int core_id)
04021 {
04022    int res;
04023    struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
04024 
04025    if (!core_instance) {
04026       return -1;
04027    }
04028 
04029    res = ast_taskprocessor_push(cc_core_taskprocessor, cc_stop_ringing, core_instance);
04030    if (res) {
04031       cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
04032    }
04033    return res;
04034 }
04035 
04036 static int cc_party_b_free(void *data)
04037 {
04038    struct cc_core_instance *core_instance = data;
04039    int res = 0;
04040 
04041    if (core_instance->agent->callbacks->party_b_free) {
04042       res = core_instance->agent->callbacks->party_b_free(core_instance->agent);
04043    }
04044    cc_unref(core_instance, "Party B free finished. Unref core_instance");
04045    return res;
04046 }
04047 
04048 int ast_cc_monitor_party_b_free(int core_id)
04049 {
04050    int res;
04051    struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
04052 
04053    if (!core_instance) {
04054       return -1;
04055    }
04056 
04057    res = ast_taskprocessor_push(cc_core_taskprocessor, cc_party_b_free, core_instance);
04058    if (res) {
04059       cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
04060    }
04061    return res;
04062 }
04063 
04064 struct cc_status_response_args {
04065    struct cc_core_instance *core_instance;
04066    enum ast_device_state devstate;
04067 };
04068 
04069 static int cc_status_response(void *data)
04070 {
04071    struct cc_status_response_args *args = data;
04072    struct cc_core_instance *core_instance = args->core_instance;
04073    struct ast_cc_monitor *monitor_iter;
04074    enum ast_device_state devstate = args->devstate;
04075 
04076    ast_free(args);
04077 
04078    AST_LIST_LOCK(core_instance->monitors);
04079    AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
04080       if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR &&
04081             monitor_iter->callbacks->status_response) {
04082          monitor_iter->callbacks->status_response(monitor_iter, devstate);
04083       }
04084    }
04085    AST_LIST_UNLOCK(core_instance->monitors);
04086    cc_unref(core_instance, "Status response finished. Unref core instance");
04087    return 0;
04088 }
04089 
04090 int ast_cc_agent_status_response(int core_id, enum ast_device_state devstate)
04091 {
04092    struct cc_status_response_args *args;
04093    struct cc_core_instance *core_instance;
04094    int res;
04095 
04096    args = ast_calloc(1, sizeof(*args));
04097    if (!args) {
04098       return -1;
04099    }
04100 
04101    core_instance = find_cc_core_instance(core_id);
04102    if (!core_instance) {
04103       ast_free(args);
04104       return -1;
04105    }
04106 
04107    args->core_instance = core_instance;
04108    args->devstate = devstate;
04109 
04110    res = ast_taskprocessor_push(cc_core_taskprocessor, cc_status_response, args);
04111    if (res) {
04112       cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
04113       ast_free(args);
04114    }
04115    return res;
04116 }
04117 
04118 static int cc_build_payload(struct ast_channel *chan, struct ast_cc_config_params *cc_params,
04119    const char *monitor_type, const char * const device_name, const char * dialstring,
04120    enum ast_cc_service_type service, void *private_data, struct cc_control_payload *payload)
04121 {
04122    struct ast_datastore *datastore;
04123    struct dialed_cc_interfaces *cc_interfaces;
04124    int dial_parent_id;
04125 
04126    ast_channel_lock(chan);
04127    datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL);
04128    if (!datastore) {
04129       ast_channel_unlock(chan);
04130       return -1;
04131    }
04132    cc_interfaces = datastore->data;
04133    dial_parent_id = cc_interfaces->dial_parent_id;
04134    ast_channel_unlock(chan);
04135 
04136    payload->monitor_type = monitor_type;
04137    payload->private_data = private_data;
04138    payload->service = service;
04139    ast_cc_copy_config_params(&payload->config_params, cc_params);
04140    payload->parent_interface_id = dial_parent_id;
04141    ast_copy_string(payload->device_name, device_name, sizeof(payload->device_name));
04142    ast_copy_string(payload->dialstring, dialstring, sizeof(payload->dialstring));
04143    return 0;
04144 }
04145 
04146 int ast_queue_cc_frame(struct ast_channel *chan, const char *monitor_type,
04147       const char * const dialstring, enum ast_cc_service_type service, void *private_data)
04148 {
04149    struct ast_frame frame = {0,};
04150    char device_name[AST_CHANNEL_NAME];
04151    int retval;
04152    struct ast_cc_config_params *cc_params;
04153 
04154    cc_params = ast_channel_get_cc_config_params(chan);
04155    if (!cc_params) {
04156       return -1;
04157    }
04158    ast_channel_get_device_name(chan, device_name, sizeof(device_name));
04159    if (ast_cc_monitor_count(device_name, monitor_type) >= ast_get_cc_max_monitors(cc_params)) {
04160       ast_log(LOG_NOTICE, "Not queuing a CC frame for device %s since it already has its maximum monitors allocated\n", device_name);
04161       return -1;
04162    }
04163 
04164    if (ast_cc_build_frame(chan, cc_params, monitor_type, device_name, dialstring, service, private_data, &frame)) {
04165       /* Frame building failed. We can't use this. */
04166       return -1;
04167    }
04168    retval = ast_queue_frame(chan, &frame);
04169    ast_frfree(&frame);
04170    return retval;
04171 }
04172 
04173 int ast_cc_build_frame(struct ast_channel *chan, struct ast_cc_config_params *cc_params,
04174    const char *monitor_type, const char * const device_name,
04175    const char * const dialstring, enum ast_cc_service_type service, void *private_data,
04176    struct ast_frame *frame)
04177 {
04178    struct cc_control_payload *payload = ast_calloc(1, sizeof(*payload));
04179 
04180    if (!payload) {
04181       return -1;
04182    }
04183    if (cc_build_payload(chan, cc_params, monitor_type, device_name, dialstring, service, private_data, payload)) {
04184       /* Something screwed up, we can't make a frame with this */
04185       ast_free(payload);
04186       return -1;
04187    }
04188    frame->frametype = AST_FRAME_CONTROL;
04189    frame->subclass.integer = AST_CONTROL_CC;
04190    frame->data.ptr = payload;
04191    frame->datalen = sizeof(*payload);
04192    frame->mallocd = AST_MALLOCD_DATA;
04193    return 0;
04194 }
04195 
04196 void ast_cc_call_failed(struct ast_channel *incoming, struct ast_channel *outgoing, const char * const dialstring)
04197 {
04198    char device_name[AST_CHANNEL_NAME];
04199    struct cc_control_payload payload;
04200    struct ast_cc_config_params *cc_params;
04201 
04202    if (ast_channel_hangupcause(outgoing) != AST_CAUSE_BUSY && ast_channel_hangupcause(outgoing) != AST_CAUSE_CONGESTION) {
04203       /* It doesn't make sense to try to offer CCBS to the caller if the reason for ast_call
04204        * failing is something other than busy or congestion
04205        */
04206       return;
04207    }
04208 
04209    cc_params = ast_channel_get_cc_config_params(outgoing);
04210    if (!cc_params) {
04211       return;
04212    }
04213    if (ast_get_cc_monitor_policy(cc_params) != AST_CC_MONITOR_GENERIC) {
04214       /* This sort of CCBS only works if using generic CC. For native, we would end up sending
04215        * a CC request for a non-existent call. The far end will reject this every time
04216        */
04217       return;
04218    }
04219 
04220    ast_channel_get_device_name(outgoing, device_name, sizeof(device_name));
04221    if (cc_build_payload(outgoing, cc_params, AST_CC_GENERIC_MONITOR_TYPE, device_name,
04222       dialstring, AST_CC_CCBS, NULL, &payload)) {
04223       /* Something screwed up, we can't make a frame with this */
04224       return;
04225    }
04226    ast_handle_cc_control_frame(incoming, outgoing, &payload);
04227 }
04228 
04229 void ast_cc_busy_interface(struct ast_channel *inbound, struct ast_cc_config_params *cc_params,
04230    const char *monitor_type, const char * const device_name, const char * const dialstring, void *private_data)
04231 {
04232    struct cc_control_payload payload;
04233    if (cc_build_payload(inbound, cc_params, monitor_type, device_name, dialstring, AST_CC_CCBS, private_data, &payload)) {
04234       /* Something screwed up. Don't try to handle this payload */
04235       call_destructor_with_no_monitor(monitor_type, private_data);
04236       return;
04237    }
04238    ast_handle_cc_control_frame(inbound, NULL, &payload);
04239 }
04240 
04241 int ast_cc_callback(struct ast_channel *inbound, const char * const tech, const char * const dest, ast_cc_callback_fn callback)
04242 {
04243    const struct ast_channel_tech *chantech = ast_get_channel_tech(tech);
04244 
04245    if (chantech && chantech->cc_callback) {
04246       chantech->cc_callback(inbound, dest, callback);
04247    }
04248 
04249    return 0;
04250 }
04251 
04252 static const char *ccreq_app = "CallCompletionRequest";
04253 
04254 static int ccreq_exec(struct ast_channel *chan, const char *data)
04255 {
04256    struct cc_core_instance *core_instance;
04257    char device_name[AST_CHANNEL_NAME];
04258    unsigned long match_flags;
04259    int res;
04260 
04261    ast_channel_get_device_name(chan, device_name, sizeof(device_name));
04262 
04263    match_flags = MATCH_NO_REQUEST;
04264    if (!(core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent, device_name, &match_flags, "Find core instance for CallCompletionRequest"))) {
04265       ast_log_dynamic_level(cc_logger_level, "Couldn't find a core instance for caller %s\n", device_name);
04266       pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL");
04267       pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "NO_CORE_INSTANCE");
04268       return 0;
04269    }
04270 
04271    ast_log_dynamic_level(cc_logger_level, "Core %d: Found core_instance for caller %s\n",
04272          core_instance->core_id, device_name);
04273 
04274    if (strcmp(core_instance->agent->callbacks->type, "generic")) {
04275       ast_log_dynamic_level(cc_logger_level, "Core %d: CallCompletionRequest is only for generic agent types.\n",
04276             core_instance->core_id);
04277       pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL");
04278       pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "NOT_GENERIC");
04279       cc_unref(core_instance, "Unref core_instance since CallCompletionRequest was called with native agent");
04280       return 0;
04281    }
04282 
04283    if (!ast_cc_request_is_within_limits()) {
04284       ast_log_dynamic_level(cc_logger_level, "Core %d: CallCompletionRequest failed. Too many requests in the system\n",
04285             core_instance->core_id);
04286       ast_cc_failed(core_instance->core_id, "Too many CC requests\n");
04287       pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL");
04288       pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "TOO_MANY_REQUESTS");
04289       cc_unref(core_instance, "Unref core_instance since too many CC requests");
04290       return 0;
04291    }
04292 
04293    res = ast_cc_agent_accept_request(core_instance->core_id, "CallCompletionRequest called by caller %s for core_id %d", device_name, core_instance->core_id);
04294    pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", res ? "FAIL" : "SUCCESS");
04295    if (res) {
04296       pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "UNSPECIFIED");
04297    }
04298 
04299    cc_unref(core_instance, "Done with CallCompletionRequest");
04300    return 0;
04301 }
04302 
04303 static const char *cccancel_app = "CallCompletionCancel";
04304 
04305 static int cccancel_exec(struct ast_channel *chan, const char *data)
04306 {
04307    struct cc_core_instance *core_instance;
04308    char device_name[AST_CHANNEL_NAME];
04309    unsigned long match_flags;
04310    int res;
04311 
04312    ast_channel_get_device_name(chan, device_name, sizeof(device_name));
04313 
04314    match_flags = MATCH_REQUEST;
04315    if (!(core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent, device_name, &match_flags, "Find core instance for CallCompletionCancel"))) {
04316       ast_log_dynamic_level(cc_logger_level, "Cannot find CC transaction to cancel for caller %s\n", device_name);
04317       pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", "FAIL");
04318       pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "NO_CORE_INSTANCE");
04319       return 0;
04320    }
04321 
04322    if (strcmp(core_instance->agent->callbacks->type, "generic")) {
04323       ast_log(LOG_WARNING, "CallCompletionCancel may only be used for calles with a generic agent\n");
04324       cc_unref(core_instance, "Unref core instance found during CallCompletionCancel");
04325       pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", "FAIL");
04326       pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "NOT_GENERIC");
04327       return 0;
04328    }
04329    res = ast_cc_failed(core_instance->core_id, "Call completion request Cancelled for core ID %d by caller %s",
04330          core_instance->core_id, device_name);
04331    cc_unref(core_instance, "Unref core instance found during CallCompletionCancel");
04332    pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", res ? "FAIL" : "SUCCESS");
04333    if (res) {
04334       pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "UNSPECIFIED");
04335    }
04336    return 0;
04337 }
04338 
04339 struct count_monitors_cb_data {
04340    const char *device_name;
04341    const char *monitor_type;
04342    int count;
04343 };
04344 
04345 static int count_monitors_cb(void *obj, void *arg, int flags)
04346 {
04347    struct cc_core_instance *core_instance = obj;
04348    struct count_monitors_cb_data *cb_data = arg;
04349    const char *device_name = cb_data->device_name;
04350    const char *monitor_type = cb_data->monitor_type;
04351    struct ast_cc_monitor *monitor_iter;
04352 
04353    AST_LIST_LOCK(core_instance->monitors);
04354    AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
04355       if (!strcmp(monitor_iter->interface->device_name, device_name) &&
04356             !strcmp(monitor_iter->interface->monitor_type, monitor_type)) {
04357          cb_data->count++;
04358          break;
04359       }
04360    }
04361    AST_LIST_UNLOCK(core_instance->monitors);
04362    return 0;
04363 }
04364 
04365 int ast_cc_monitor_count(const char * const name, const char * const type)
04366 {
04367    struct count_monitors_cb_data data = {.device_name = name, .monitor_type = type,};
04368 
04369    ao2_t_callback(cc_core_instances, OBJ_NODATA, count_monitors_cb, &data, "Counting agents");
04370    ast_log_dynamic_level(cc_logger_level, "Counted %d monitors\n", data.count);
04371    return data.count;
04372 }
04373 
04374 static void initialize_cc_max_requests(void)
04375 {
04376    struct ast_config *cc_config;
04377    const char *cc_max_requests_str;
04378    struct ast_flags config_flags = {0,};
04379    char *endptr;
04380 
04381    cc_config = ast_config_load2("ccss.conf", "ccss", config_flags);
04382    if (!cc_config || cc_config == CONFIG_STATUS_FILEINVALID) {
04383       ast_log(LOG_WARNING, "Could not find valid ccss.conf file. Using cc_max_requests default\n");
04384       global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT;
04385       return;
04386    }
04387 
04388    if (!(cc_max_requests_str = ast_variable_retrieve(cc_config, "general", "cc_max_requests"))) {
04389       ast_config_destroy(cc_config);
04390       global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT;
04391       return;
04392    }
04393 
04394    global_cc_max_requests = strtol(cc_max_requests_str, &endptr, 10);
04395 
04396    if (!ast_strlen_zero(endptr)) {
04397       ast_log(LOG_WARNING, "Invalid input given for cc_max_requests. Using default\n");
04398       global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT;
04399    }
04400 
04401    ast_config_destroy(cc_config);
04402    return;
04403 }
04404 
04405 /*!
04406  * \internal
04407  * \brief helper function to parse and configure each devstate map
04408  */
04409 static void initialize_cc_devstate_map_helper(struct ast_config *cc_config, enum cc_state state, const char *cc_setting)
04410 {
04411    const char *cc_devstate_str;
04412    enum ast_device_state this_devstate;
04413 
04414    if ((cc_devstate_str = ast_variable_retrieve(cc_config, "general", cc_setting))) {
04415       this_devstate = ast_devstate_val(cc_devstate_str);
04416       if (this_devstate != AST_DEVICE_UNKNOWN) {
04417          cc_state_to_devstate_map[state] = this_devstate;
04418       }
04419    }
04420 }
04421 
04422 /*!
04423  * \internal
04424  * \brief initializes cc_state_to_devstate_map from ccss.conf
04425  *
04426  * \details
04427  * The cc_state_to_devstate_map[] is already initialized with all the
04428  * default values. This will update that structure with any changes
04429  * from the ccss.conf file. The configuration parameters in ccss.conf
04430  * should use any valid device state form that is recognized by
04431  * ast_devstate_val() function.
04432  */
04433 static void initialize_cc_devstate_map(void)
04434 {
04435    struct ast_config *cc_config;
04436    struct ast_flags config_flags = { 0, };
04437 
04438    cc_config = ast_config_load2("ccss.conf", "ccss", config_flags);
04439    if (!cc_config || cc_config == CONFIG_STATUS_FILEINVALID) {
04440       ast_log(LOG_WARNING,
04441          "Could not find valid ccss.conf file. Using cc_[state]_devstate defaults\n");
04442       return;
04443    }
04444 
04445    initialize_cc_devstate_map_helper(cc_config, CC_AVAILABLE, "cc_available_devstate");
04446    initialize_cc_devstate_map_helper(cc_config, CC_CALLER_OFFERED, "cc_caller_offered_devstate");
04447    initialize_cc_devstate_map_helper(cc_config, CC_CALLER_REQUESTED, "cc_caller_requested_devstate");
04448    initialize_cc_devstate_map_helper(cc_config, CC_ACTIVE, "cc_active_devstate");
04449    initialize_cc_devstate_map_helper(cc_config, CC_CALLEE_READY, "cc_callee_ready_devstate");
04450    initialize_cc_devstate_map_helper(cc_config, CC_CALLER_BUSY, "cc_caller_busy_devstate");
04451    initialize_cc_devstate_map_helper(cc_config, CC_RECALLING, "cc_recalling_devstate");
04452    initialize_cc_devstate_map_helper(cc_config, CC_COMPLETE, "cc_complete_devstate");
04453    initialize_cc_devstate_map_helper(cc_config, CC_FAILED, "cc_failed_devstate");
04454 
04455    ast_config_destroy(cc_config);
04456 }
04457 
04458 static void cc_cli_print_monitor_stats(struct ast_cc_monitor *monitor, int fd, int parent_id)
04459 {
04460    struct ast_cc_monitor *child_monitor_iter = monitor;
04461    if (!monitor) {
04462       return;
04463    }
04464 
04465    ast_cli(fd, "\t\t|-->%s", monitor->interface->device_name);
04466    if (monitor->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
04467       ast_cli(fd, "(%s)", cc_service_to_string(monitor->service_offered));
04468    }
04469    ast_cli(fd, "\n");
04470 
04471    while ((child_monitor_iter = AST_LIST_NEXT(child_monitor_iter, next))) {
04472       if (child_monitor_iter->parent_id == monitor->id) {
04473          cc_cli_print_monitor_stats(child_monitor_iter, fd, child_monitor_iter->id);
04474       }
04475    }
04476 }
04477 
04478 static int print_stats_cb(void *obj, void *arg, int flags)
04479 {
04480    int *cli_fd = arg;
04481    struct cc_core_instance *core_instance = obj;
04482 
04483    ast_cli(*cli_fd, "%d\t\t%s\t\t%s\n", core_instance->core_id, core_instance->agent->device_name,
04484          cc_state_to_string(core_instance->current_state));
04485    AST_LIST_LOCK(core_instance->monitors);
04486    cc_cli_print_monitor_stats(AST_LIST_FIRST(core_instance->monitors), *cli_fd, 0);
04487    AST_LIST_UNLOCK(core_instance->monitors);
04488    return 0;
04489 }
04490 
04491 static int cc_cli_output_status(void *data)
04492 {
04493    int *cli_fd = data;
04494    int count = ao2_container_count(cc_core_instances);
04495 
04496    if (!count) {
04497       ast_cli(*cli_fd, "There are currently no active call completion transactions\n");
04498    } else {
04499       ast_cli(*cli_fd, "%d Call completion transactions\n", count);
04500       ast_cli(*cli_fd, "Core ID\t\tCaller\t\t\t\tStatus\n");
04501       ast_cli(*cli_fd, "----------------------------------------------------------------------------\n");
04502       ao2_t_callback(cc_core_instances, OBJ_NODATA, print_stats_cb, cli_fd, "Printing stats to CLI");
04503    }
04504    ast_free(cli_fd);
04505    return 0;
04506 }
04507 
04508 static char *handle_cc_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04509 {
04510    int *cli_fd;
04511 
04512    switch (cmd) {
04513    case CLI_INIT:
04514       e->command = "cc report status";
04515       e->usage =
04516          "Usage: cc report status\n"
04517          "       Report the current status of any ongoing CC transactions\n";
04518       return NULL;
04519    case CLI_GENERATE:
04520       return NULL;
04521    }
04522 
04523    if (a->argc != 3) {
04524       return CLI_SHOWUSAGE;
04525    }
04526 
04527    cli_fd = ast_malloc(sizeof(*cli_fd));
04528    if (!cli_fd) {
04529       return CLI_FAILURE;
04530    }
04531 
04532    *cli_fd = a->fd;
04533 
04534    if (ast_taskprocessor_push(cc_core_taskprocessor, cc_cli_output_status, cli_fd)) {
04535       ast_free(cli_fd);
04536       return CLI_FAILURE;
04537    }
04538    return CLI_SUCCESS;
04539 }
04540 
04541 static int kill_cores(void *obj, void *arg, int flags)
04542 {
04543    int *core_id = arg;
04544    struct cc_core_instance *core_instance = obj;
04545 
04546    if (!core_id || (core_instance->core_id == *core_id)) {
04547       ast_cc_failed(core_instance->core_id, "CC transaction canceled administratively\n");
04548    }
04549    return 0;
04550 }
04551 
04552 static char *complete_core_id(const char *line, const char *word, int pos, int state)
04553 {
04554    int which = 0;
04555    int wordlen = strlen(word);
04556    char *ret = NULL;
04557    struct ao2_iterator core_iter = ao2_iterator_init(cc_core_instances, 0);
04558    struct cc_core_instance *core_instance;
04559 
04560    for (; (core_instance = ao2_t_iterator_next(&core_iter, "Next core instance"));
04561          cc_unref(core_instance, "CLI tab completion iteration")) {
04562       char core_id_str[20];
04563       snprintf(core_id_str, sizeof(core_id_str), "%d", core_instance->core_id);
04564       if (!strncmp(word, core_id_str, wordlen) && ++which > state) {
04565          ret = ast_strdup(core_id_str);
04566          cc_unref(core_instance, "Found a matching core ID for CLI tab-completion");
04567          break;
04568       }
04569    }
04570    ao2_iterator_destroy(&core_iter);
04571 
04572    return ret;
04573 }
04574 
04575 static char *handle_cc_kill(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04576 {
04577    static const char * const option[] = { "core", "all", NULL };
04578 
04579    switch (cmd) {
04580    case CLI_INIT:
04581       e->command = "cc cancel";
04582       e->usage =
04583          "Usage: cc cancel can be used in two ways.\n"
04584          "       1. 'cc cancel core [core ID]' will cancel the CC transaction with\n"
04585          "          core ID equal to the specified core ID.\n"
04586          "       2. 'cc cancel all' will cancel all active CC transactions.\n";
04587       return NULL;
04588    case CLI_GENERATE:
04589       if (a->pos == 2) {
04590          return ast_cli_complete(a->word, option, a->n);
04591       }
04592       if (a->pos == 3) {
04593          return complete_core_id(a->line, a->word, a->pos, a->n);
04594       }
04595       return NULL;
04596    }
04597 
04598    if (a->argc == 4) {
04599       int core_id;
04600       char *endptr;
04601       if (strcasecmp(a->argv[2], "core")) {
04602          return CLI_SHOWUSAGE;
04603       }
04604       core_id = strtol(a->argv[3], &endptr, 10);
04605       if ((errno != 0 && core_id == 0) || (endptr == a->argv[3])) {
04606          return CLI_SHOWUSAGE;
04607       }
04608       ao2_t_callback(cc_core_instances, OBJ_NODATA, kill_cores, &core_id, "CLI Killing Core Id");
04609    } else if (a->argc == 3) {
04610       if (strcasecmp(a->argv[2], "all")) {
04611          return CLI_SHOWUSAGE;
04612       }
04613       ao2_t_callback(cc_core_instances, OBJ_NODATA, kill_cores, NULL, "CLI Killing all CC cores");
04614    } else {
04615       return CLI_SHOWUSAGE;
04616    }
04617 
04618    return CLI_SUCCESS;
04619 }
04620 
04621 static struct ast_cli_entry cc_cli[] = {
04622    AST_CLI_DEFINE(handle_cc_status, "Reports CC stats"),
04623    AST_CLI_DEFINE(handle_cc_kill, "Kill a CC transaction"),
04624 };
04625 
04626 static void cc_shutdown(void)
04627 {
04628    ast_devstate_prov_del("ccss");
04629    ast_cc_agent_unregister(&generic_agent_callbacks);
04630    ast_cc_monitor_unregister(&generic_monitor_cbs);
04631    ast_unregister_application(cccancel_app);
04632    ast_unregister_application(ccreq_app);
04633    ast_logger_unregister_level(CC_LOGGER_LEVEL_NAME);
04634    ast_cli_unregister_multiple(cc_cli, ARRAY_LEN(cc_cli));
04635 
04636    if (cc_sched_context) {
04637       ast_sched_context_destroy(cc_sched_context);
04638       cc_sched_context = NULL;
04639    }
04640    if (cc_core_taskprocessor) {
04641       cc_core_taskprocessor = ast_taskprocessor_unreference(cc_core_taskprocessor);
04642    }
04643    /* Note that core instances must be destroyed prior to the generic_monitors */
04644    if (cc_core_instances) {
04645       ao2_t_ref(cc_core_instances, -1, "Unref cc_core_instances container in cc_shutdown");
04646       cc_core_instances = NULL;
04647    }
04648    if (generic_monitors) {
04649       ao2_t_ref(generic_monitors, -1, "Unref generic_monitor container in cc_shutdown");
04650       generic_monitors = NULL;
04651    }
04652 }
04653 
04654 int ast_cc_init(void)
04655 {
04656    int res;
04657 
04658    if (!(cc_core_instances = ao2_t_container_alloc(CC_CORE_INSTANCES_BUCKETS,
04659                cc_core_instance_hash_fn, cc_core_instance_cmp_fn,
04660                "Create core instance container"))) {
04661       return -1;
04662    }
04663    if (!(generic_monitors = ao2_t_container_alloc(CC_CORE_INSTANCES_BUCKETS,
04664                generic_monitor_hash_fn, generic_monitor_cmp_fn,
04665                "Create generic monitor container"))) {
04666       return -1;
04667    }
04668    if (!(cc_core_taskprocessor = ast_taskprocessor_get("CCSS core", TPS_REF_DEFAULT))) {
04669       return -1;
04670    }
04671    if (!(cc_sched_context = ast_sched_context_create())) {
04672       return -1;
04673    }
04674    if (ast_sched_start_thread(cc_sched_context)) {
04675       return -1;
04676    }
04677    res = ast_register_application2(ccreq_app, ccreq_exec, NULL, NULL, NULL);
04678    res |= ast_register_application2(cccancel_app, cccancel_exec, NULL, NULL, NULL);
04679    res |= ast_cc_monitor_register(&generic_monitor_cbs);
04680    res |= ast_cc_agent_register(&generic_agent_callbacks);
04681 
04682    ast_cli_register_multiple(cc_cli, ARRAY_LEN(cc_cli));
04683    cc_logger_level = ast_logger_register_level(CC_LOGGER_LEVEL_NAME);
04684    dialed_cc_interface_counter = 1;
04685    initialize_cc_max_requests();
04686 
04687    /* Read the map and register the device state callback for generic agents */
04688    initialize_cc_devstate_map();
04689    res |= ast_devstate_prov_add("ccss", ccss_device_state);
04690 
04691    ast_register_cleanup(cc_shutdown);
04692 
04693    return res;
04694 }

Generated on Thu Apr 16 06:27:17 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6