Newer
Older
ao2_t_link(cc_core_instances, core_instance, "Link core instance into container");
return core_instance;
}
struct cc_state_change_args {
struct cc_core_instance *core_instance;/*!< Holds reference to core instance. */
enum cc_state state;
int core_id;
char debug[1];
};
static int is_state_change_valid(enum cc_state current_state, const enum cc_state new_state, struct ast_cc_agent *agent)
{
int is_valid = 0;
switch (new_state) {
case CC_AVAILABLE:
ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to state %u? That should never happen.\n",
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
agent->core_id, new_state);
break;
case CC_CALLER_OFFERED:
if (current_state == CC_AVAILABLE) {
is_valid = 1;
}
break;
case CC_CALLER_REQUESTED:
if (current_state == CC_CALLER_OFFERED ||
(current_state == CC_AVAILABLE && ast_test_flag(agent, AST_CC_AGENT_SKIP_OFFER))) {
is_valid = 1;
}
break;
case CC_ACTIVE:
if (current_state == CC_CALLER_REQUESTED || current_state == CC_CALLER_BUSY) {
is_valid = 1;
}
break;
case CC_CALLEE_READY:
if (current_state == CC_ACTIVE) {
is_valid = 1;
}
break;
case CC_CALLER_BUSY:
if (current_state == CC_CALLEE_READY) {
is_valid = 1;
}
break;
case CC_RECALLING:
if (current_state == CC_CALLEE_READY) {
is_valid = 1;
}
break;
case CC_COMPLETE:
if (current_state == CC_RECALLING) {
is_valid = 1;
}
break;
case CC_FAILED:
is_valid = 1;
break;
default:
ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to unknown state %u\n",
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
agent->core_id, new_state);
break;
}
return is_valid;
}
static int cc_available(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
{
/* This should never happen... */
ast_log(LOG_WARNING, "Someone requested to change to CC_AVAILABLE? Ignoring.\n");
return -1;
}
static int cc_caller_offered(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
{
if (core_instance->agent->callbacks->start_offer_timer(core_instance->agent)) {
ast_cc_failed(core_instance->core_id, "Failed to start the offer timer for %s\n",
core_instance->agent->device_name);
return -1;
}
cc_publish_offertimerstart(core_instance->core_id, core_instance->agent->device_name, core_instance->agent->cc_params->cc_offer_timer);
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
ast_log_dynamic_level(cc_logger_level, "Core %d: Started the offer timer for the agent %s!\n",
core_instance->core_id, core_instance->agent->device_name);
return 0;
}
/*!
* \brief check if the core instance has any device monitors
*
* In any case where we end up removing a device monitor from the
* list of device monitors, it is important to see what the state
* of the list is afterwards. If we find that we only have extension
* monitors left, then no devices are actually being monitored.
* In such a case, we need to declare that CC has failed for this
* call. This function helps those cases to determine if they should
* declare failure.
*
* \param core_instance The core instance we are checking for the existence
* of device monitors
* \retval 0 No device monitors exist on this core_instance
* \retval 1 There is still at least 1 device monitor remaining
*/
static int has_device_monitors(struct cc_core_instance *core_instance)
{
struct ast_cc_monitor *iter;
int res = 0;
AST_LIST_TRAVERSE(core_instance->monitors, iter, next) {
if (iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
res = 1;
break;
}
}
return res;
}
static void request_cc(struct cc_core_instance *core_instance)
{
struct ast_cc_monitor *monitor_iter;
AST_LIST_LOCK(core_instance->monitors);
AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
if (monitor_iter->callbacks->request_cc(monitor_iter, &monitor_iter->available_timer_id)) {
AST_LIST_REMOVE_CURRENT(next);
cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
monitor_iter->interface->device_name, 1);
cc_unref(monitor_iter, "request_cc failed. Unref list's reference to monitor");
} else {
cc_publish_requested(core_instance->core_id, core_instance->agent->device_name, monitor_iter->interface->device_name);
}
}
}
AST_LIST_TRAVERSE_SAFE_END;
if (!has_device_monitors(core_instance)) {
ast_cc_failed(core_instance->core_id, "All device monitors failed to request CC");
}
AST_LIST_UNLOCK(core_instance->monitors);
}
static int cc_caller_requested(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
{
if (!ast_cc_request_is_within_limits()) {
ast_log(LOG_WARNING, "Cannot request CC since there is no more room for requests\n");
core_instance->agent->callbacks->respond(core_instance->agent,
AST_CC_AGENT_RESPONSE_FAILURE_TOO_MANY);
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
ast_cc_failed(core_instance->core_id, "Too many requests in the system");
return -1;
}
core_instance->agent->callbacks->stop_offer_timer(core_instance->agent);
request_cc(core_instance);
return 0;
}
static void unsuspend(struct cc_core_instance *core_instance)
{
struct ast_cc_monitor *monitor_iter;
AST_LIST_LOCK(core_instance->monitors);
AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
if (monitor_iter->callbacks->unsuspend(monitor_iter)) {
AST_LIST_REMOVE_CURRENT(next);
cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
monitor_iter->interface->device_name, 1);
cc_unref(monitor_iter, "unsuspend failed. Unref list's reference to monitor");
}
}
}
AST_LIST_TRAVERSE_SAFE_END;
if (!has_device_monitors(core_instance)) {
ast_cc_failed(core_instance->core_id, "All device monitors failed to unsuspend CC");
}
AST_LIST_UNLOCK(core_instance->monitors);
}
static int cc_active(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
{
/* Either
* 1. Callee accepted CC request, call agent's ack callback.
* 2. Caller became available, call agent's stop_monitoring callback and
* call monitor's unsuspend callback.
*/
if (previous_state == CC_CALLER_REQUESTED) {
core_instance->agent->callbacks->respond(core_instance->agent,
AST_CC_AGENT_RESPONSE_SUCCESS);
cc_publish_requestacknowledged(core_instance->core_id, core_instance->agent->device_name);
} else if (previous_state == CC_CALLER_BUSY) {
cc_publish_callerstopmonitoring(core_instance->core_id, core_instance->agent->device_name);
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
unsuspend(core_instance);
}
/* Not possible for previous_state to be anything else due to the is_state_change_valid check at the beginning */
return 0;
}
static int cc_callee_ready(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
{
core_instance->agent->callbacks->callee_available(core_instance->agent);
return 0;
}
static void suspend(struct cc_core_instance *core_instance)
{
struct ast_cc_monitor *monitor_iter;
AST_LIST_LOCK(core_instance->monitors);
AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
if (monitor_iter->callbacks->suspend(monitor_iter)) {
AST_LIST_REMOVE_CURRENT(next);
cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
monitor_iter->interface->device_name, 1);
cc_unref(monitor_iter, "suspend failed. Unref list's reference to monitor");
}
}
}
AST_LIST_TRAVERSE_SAFE_END;
if (!has_device_monitors(core_instance)) {
ast_cc_failed(core_instance->core_id, "All device monitors failed to suspend CC");
}
AST_LIST_UNLOCK(core_instance->monitors);
}
static int cc_caller_busy(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
{
/* Callee was available, but caller was busy, call agent's begin_monitoring callback
* and call monitor's suspend callback.
*/
suspend(core_instance);
core_instance->agent->callbacks->start_monitoring(core_instance->agent);
cc_publish_callerstartmonitoring(core_instance->core_id, core_instance->agent->device_name);
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
return 0;
}
static void cancel_available_timer(struct cc_core_instance *core_instance)
{
struct ast_cc_monitor *monitor_iter;
AST_LIST_LOCK(core_instance->monitors);
AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
if (monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id)) {
AST_LIST_REMOVE_CURRENT(next);
cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
monitor_iter->interface->device_name, 1);
cc_unref(monitor_iter, "cancel_available_timer failed. Unref list's reference to monitor");
}
}
}
AST_LIST_TRAVERSE_SAFE_END;
if (!has_device_monitors(core_instance)) {
ast_cc_failed(core_instance->core_id, "All device monitors failed to cancel their available timers");
}
AST_LIST_UNLOCK(core_instance->monitors);
}
static int cc_recalling(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
{
/* Both caller and callee are available, call agent's recall callback
*/
cancel_available_timer(core_instance);
cc_publish_callerrecalling(core_instance->core_id, core_instance->agent->device_name);
return 0;
}
static int cc_complete(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
{
/* Recall has made progress, call agent and monitor destructor functions
*/
cc_publish_recallcomplete(core_instance->core_id, core_instance->agent->device_name);
ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC recall has completed");
return 0;
}
static int cc_failed(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
{
cc_publish_failure(core_instance->core_id, core_instance->agent->device_name, args->debug);
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC failed");
return 0;
}
static int (* const state_change_funcs [])(struct cc_core_instance *, struct cc_state_change_args *, enum cc_state previous_state) = {
[CC_AVAILABLE] = cc_available,
[CC_CALLER_OFFERED] = cc_caller_offered,
[CC_CALLER_REQUESTED] = cc_caller_requested,
[CC_ACTIVE] = cc_active,
[CC_CALLEE_READY] = cc_callee_ready,
[CC_CALLER_BUSY] = cc_caller_busy,
[CC_RECALLING] = cc_recalling,
[CC_COMPLETE] = cc_complete,
[CC_FAILED] = cc_failed,
};
static int cc_do_state_change(void *datap)
{
struct cc_state_change_args *args = datap;
struct cc_core_instance *core_instance;
enum cc_state previous_state;
int res;
ast_log_dynamic_level(cc_logger_level, "Core %d: State change to %u requested. Reason: %s\n",
args->core_id, args->state, args->debug);
core_instance = args->core_instance;
if (!is_state_change_valid(core_instance->current_state, args->state, core_instance->agent)) {
ast_log_dynamic_level(cc_logger_level, "Core %d: Invalid state change requested. Cannot go from %s to %s\n",
args->core_id, cc_state_to_string(core_instance->current_state), cc_state_to_string(args->state));
if (args->state == CC_CALLER_REQUESTED) {
/*
* For out-of-order requests, we need to let the requester know that
* we can't handle the request now.
*/
core_instance->agent->callbacks->respond(core_instance->agent,
AST_CC_AGENT_RESPONSE_FAILURE_INVALID);
}
ast_free(args);
cc_unref(core_instance, "Unref core instance from when it was found earlier");
return -1;
}
/* We can change to the new state now. */
previous_state = core_instance->current_state;
core_instance->current_state = args->state;
res = state_change_funcs[core_instance->current_state](core_instance, args, previous_state);
/* If state change successful then notify any device state watchers of the change */
if (!res && !strcmp(core_instance->agent->callbacks->type, "generic")) {
ccss_notify_device_state_change(core_instance->agent->device_name, core_instance->current_state);
}
ast_free(args);
cc_unref(core_instance, "Unref since state change has completed"); /* From ao2_find */
return res;
}
static int cc_request_state_change(enum cc_state state, const int core_id, const char *debug, va_list ap)
{
int res;
int debuglen;
char dummy[1];
va_list aq;
struct cc_core_instance *core_instance;
struct cc_state_change_args *args;
/* This initial call to vsnprintf is simply to find what the
* size of the string needs to be
*/
va_copy(aq, ap);
/* We add 1 to the result since vsnprintf's return does not
* include the terminating null byte
*/
debuglen = vsnprintf(dummy, sizeof(dummy), debug, aq) + 1;
va_end(aq);
if (!(args = ast_calloc(1, sizeof(*args) + debuglen))) {
return -1;
}
core_instance = find_cc_core_instance(core_id);
if (!core_instance) {
ast_log_dynamic_level(cc_logger_level, "Core %d: Unable to find core instance.\n",
core_id);
ast_free(args);
return -1;
}
args->core_instance = core_instance;
args->state = state;
args->core_id = core_id;
vsnprintf(args->debug, debuglen, debug, ap);
res = ast_taskprocessor_push(cc_core_taskprocessor, cc_do_state_change, args);
if (res) {
cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
ast_free(args);
}
return res;
}
struct cc_recall_ds_data {
int core_id;
char ignore;
char nested;
struct cc_monitor_tree *interface_tree;
};
static void *cc_recall_ds_duplicate(void *data)
{
struct cc_recall_ds_data *old_data = data;
struct cc_recall_ds_data *new_data = ast_calloc(1, sizeof(*new_data));
if (!new_data) {
return NULL;
}
new_data->interface_tree = cc_ref(old_data->interface_tree, "Bump refcount of monitor tree for recall datastore duplicate");
new_data->core_id = old_data->core_id;
new_data->nested = 1;
return new_data;
}
static void cc_recall_ds_destroy(void *data)
{
struct cc_recall_ds_data *recall_data = data;
recall_data->interface_tree = cc_unref(recall_data->interface_tree, "Unref recall monitor tree");
ast_free(recall_data);
}
static const struct ast_datastore_info recall_ds_info = {
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
.type = "cc_recall",
.duplicate = cc_recall_ds_duplicate,
.destroy = cc_recall_ds_destroy,
};
int ast_setup_cc_recall_datastore(struct ast_channel *chan, const int core_id)
{
struct ast_datastore *recall_datastore = ast_datastore_alloc(&recall_ds_info, NULL);
struct cc_recall_ds_data *recall_data;
struct cc_core_instance *core_instance;
if (!recall_datastore) {
return -1;
}
if (!(recall_data = ast_calloc(1, sizeof(*recall_data)))) {
ast_datastore_free(recall_datastore);
return -1;
}
if (!(core_instance = find_cc_core_instance(core_id))) {
ast_free(recall_data);
ast_datastore_free(recall_datastore);
return -1;
}
recall_data->interface_tree = cc_ref(core_instance->monitors,
"Bump refcount for monitor tree for recall datastore");
recall_data->core_id = core_id;
recall_datastore->data = recall_data;
recall_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
ast_channel_lock(chan);
ast_channel_datastore_add(chan, recall_datastore);
ast_channel_unlock(chan);
cc_unref(core_instance, "Recall datastore set up. No need for core_instance ref");
return 0;
}
int ast_cc_is_recall(struct ast_channel *chan, int *core_id, const char * const monitor_type)
{
struct ast_datastore *recall_datastore;
struct cc_recall_ds_data *recall_data;
struct cc_monitor_tree *interface_tree;
char device_name[AST_CHANNEL_NAME];
struct ast_cc_monitor *device_monitor;
int core_id_candidate;
ast_assert(core_id != NULL);
*core_id = -1;
ast_channel_lock(chan);
if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
/* Obviously not a recall if the datastore isn't present */
ast_channel_unlock(chan);
return 0;
}
recall_data = recall_datastore->data;
if (recall_data->ignore) {
/* Though this is a recall, the call to this particular interface is not part of the
* recall either because this is a call forward or because this is not the first
* invocation of Dial during this call
*/
ast_channel_unlock(chan);
return 0;
}
if (!recall_data->nested) {
/* If the nested flag is not set, then this means that
* the channel passed to this function is the caller making
* the recall. This means that we shouldn't look through
* the monitor tree for the channel because it shouldn't be
* there. However, this is a recall though, so return true.
*/
*core_id = recall_data->core_id;
ast_channel_unlock(chan);
return 1;
}
if (ast_strlen_zero(monitor_type)) {
/* If someone passed a NULL or empty monitor type, then it is clear
* the channel they passed in was an incoming channel, and so searching
* the list of dialed interfaces is not going to be helpful. Just return
* false immediately.
*/
ast_channel_unlock(chan);
return 0;
}
interface_tree = recall_data->interface_tree;
ast_channel_get_device_name(chan, device_name, sizeof(device_name));
/* We grab the value of the recall_data->core_id so that we
* can unlock the channel before we start looking through the
* interface list. That way we don't have to worry about a possible
* clash between the channel lock and the monitor tree lock.
*/
core_id_candidate = recall_data->core_id;
ast_channel_unlock(chan);
/*
* Now we need to find out if the channel device name
* is in the list of interfaces in the called tree.
*/
AST_LIST_LOCK(interface_tree);
AST_LIST_TRAVERSE(interface_tree, device_monitor, next) {
if (!strcmp(device_monitor->interface->device_name, device_name) &&
!strcmp(device_monitor->interface->monitor_type, monitor_type)) {
/* BOOM! Device is in the tree! We have a winner! */
*core_id = core_id_candidate;
AST_LIST_UNLOCK(interface_tree);
return 1;
}
}
AST_LIST_UNLOCK(interface_tree);
return 0;
}
struct ast_cc_monitor *ast_cc_get_monitor_by_recall_core_id(const int core_id, const char * const device_name)
{
struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
struct ast_cc_monitor *monitor_iter;
if (!core_instance) {
return NULL;
}
AST_LIST_LOCK(core_instance->monitors);
AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
if (!strcmp(monitor_iter->interface->device_name, device_name)) {
/* Found a monitor. */
cc_ref(monitor_iter, "Hand the requester of the monitor a reference");
break;
}
}
AST_LIST_UNLOCK(core_instance->monitors);
cc_unref(core_instance, "Done with core instance ref in ast_cc_get_monitor_by_recall_core_id");
return monitor_iter;
}
/*!
* \internal
* \brief uniquely append a dialstring to our CC_INTERFACES chanvar string.
*
* We will only append a string if it has not already appeared in our channel
* variable earlier. We ensure that we don't erroneously match substrings by
* adding an ampersand to the end of our potential dialstring and searching for
* it plus the ampersand in our variable.
*
* It's important to note that once we have built the full CC_INTERFACES string,
* there will be an extra ampersand at the end which must be stripped off by
* the caller of this function.
*
* \param str An ast_str holding what we will add to CC_INTERFACES
* \param dialstring A new dialstring to add
* \retval void
*/
static void cc_unique_append(struct ast_str **str, const char *dialstring)
char dialstring_search[AST_CHANNEL_NAME + 1];
if (ast_strlen_zero(dialstring)) {
/* No dialstring to append. */
return;
}
snprintf(dialstring_search, sizeof(dialstring_search), "%s%c", dialstring, '&');
if (strstr(ast_str_buffer(*str), dialstring_search)) {
ast_str_append(str, 0, "%s", dialstring_search);
}
/*!
* \internal
* \brief Build the CC_INTERFACES channel variable
*
* The method used is to traverse the child dialstrings in the
* passed-in extension monitor, adding any that have the is_valid
* flag set. Then, traverse the monitors, finding all children
* of the starting extension monitor and adding their dialstrings
* as well.
*
* \param starting_point The extension monitor that is the parent to all
* monitors whose dialstrings should be added to CC_INTERFACES
* \param str Where we will store CC_INTERFACES
* \retval void
*/
static void build_cc_interfaces_chanvar(struct ast_cc_monitor *starting_point, struct ast_str **str)
{
struct extension_monitor_pvt *extension_pvt;
struct extension_child_dialstring *child_dialstring;
struct ast_cc_monitor *monitor_iter = starting_point;
int top_level_id = starting_point->id;
size_t length;
/* Init to an empty string. */
ast_str_truncate(*str, 0);
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
/* First we need to take all of the is_valid child_dialstrings from
* the extension monitor we found and add them to the CC_INTERFACES
* chanvar
*/
extension_pvt = starting_point->private_data;
AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) {
if (child_dialstring->is_valid) {
cc_unique_append(str, child_dialstring->original_dialstring);
}
}
/* And now we get the dialstrings from each of the device monitors */
while ((monitor_iter = AST_LIST_NEXT(monitor_iter, next))) {
if (monitor_iter->parent_id == top_level_id) {
cc_unique_append(str, monitor_iter->dialstring);
}
}
/* str will have an extra '&' tacked onto the end of it, so we need
* to get rid of that.
*/
length = ast_str_strlen(*str);
ast_str_truncate(*str, length - 1);
}
if (length <= 1) {
/* Nothing to recall? This should not happen. */
ast_log(LOG_ERROR, "CC_INTERFACES is empty. starting device_name:'%s'\n",
starting_point->interface->device_name);
}
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
}
int ast_cc_agent_set_interfaces_chanvar(struct ast_channel *chan)
{
struct ast_datastore *recall_datastore;
struct cc_monitor_tree *interface_tree;
struct ast_cc_monitor *monitor;
struct cc_recall_ds_data *recall_data;
struct ast_str *str = ast_str_create(64);
int core_id;
if (!str) {
return -1;
}
ast_channel_lock(chan);
if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
ast_channel_unlock(chan);
ast_free(str);
return -1;
}
recall_data = recall_datastore->data;
interface_tree = recall_data->interface_tree;
core_id = recall_data->core_id;
ast_channel_unlock(chan);
AST_LIST_LOCK(interface_tree);
monitor = AST_LIST_FIRST(interface_tree);
build_cc_interfaces_chanvar(monitor, &str);
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
AST_LIST_UNLOCK(interface_tree);
pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str));
ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n",
core_id, ast_str_buffer(str));
ast_free(str);
return 0;
}
int ast_set_cc_interfaces_chanvar(struct ast_channel *chan, const char * const extension)
{
struct ast_datastore *recall_datastore;
struct cc_monitor_tree *interface_tree;
struct ast_cc_monitor *monitor_iter;
struct cc_recall_ds_data *recall_data;
struct ast_str *str = ast_str_create(64);
int core_id;
if (!str) {
return -1;
}
ast_channel_lock(chan);
if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
ast_channel_unlock(chan);
ast_free(str);
return -1;
}
recall_data = recall_datastore->data;
interface_tree = recall_data->interface_tree;
core_id = recall_data->core_id;
ast_channel_unlock(chan);
AST_LIST_LOCK(interface_tree);
AST_LIST_TRAVERSE(interface_tree, monitor_iter, next) {
if (!strcmp(monitor_iter->interface->device_name, extension)) {
break;
}
}
if (!monitor_iter) {
/* We couldn't find this extension. This may be because
* we have been directed into an unexpected extension because
* the admin has changed a CC_INTERFACES variable at some point.
*/
AST_LIST_UNLOCK(interface_tree);
ast_free(str);
return -1;
}
build_cc_interfaces_chanvar(monitor_iter, &str);
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
AST_LIST_UNLOCK(interface_tree);
pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str));
ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n",
core_id, ast_str_buffer(str));
ast_free(str);
return 0;
}
void ast_ignore_cc(struct ast_channel *chan)
{
struct ast_datastore *cc_datastore;
struct ast_datastore *cc_recall_datastore;
struct dialed_cc_interfaces *cc_interfaces;
struct cc_recall_ds_data *recall_cc_data;
ast_channel_lock(chan);
if ((cc_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
cc_interfaces = cc_datastore->data;
cc_interfaces->ignore = 1;
}
if ((cc_recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
recall_cc_data = cc_recall_datastore->data;
recall_cc_data->ignore = 1;
}
ast_channel_unlock(chan);
}
static __attribute__((format(printf, 2, 3))) int cc_offer(const int core_id, const char * const debug, ...)
{
va_list ap;
int res;
va_start(ap, debug);
res = cc_request_state_change(CC_CALLER_OFFERED, core_id, debug, ap);
va_end(ap);
return res;
}
int ast_cc_offer(struct ast_channel *caller_chan)
{
int core_id;
int res = -1;
struct ast_datastore *datastore;
struct dialed_cc_interfaces *cc_interfaces;
char cc_is_offerable;
ast_channel_lock(caller_chan);
if (!(datastore = ast_channel_datastore_find(caller_chan, &dialed_cc_interfaces_info, NULL))) {
ast_channel_unlock(caller_chan);
return res;
}
cc_interfaces = datastore->data;
cc_is_offerable = cc_interfaces->is_original_caller;
core_id = cc_interfaces->core_id;
ast_channel_unlock(caller_chan);
if (cc_is_offerable) {
res = cc_offer(core_id, "CC offered to caller %s", ast_channel_name(caller_chan));
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
}
return res;
}
int ast_cc_agent_accept_request(int core_id, const char * const debug, ...)
{
va_list ap;
int res;
va_start(ap, debug);
res = cc_request_state_change(CC_CALLER_REQUESTED, core_id, debug, ap);
va_end(ap);
return res;
}
int ast_cc_monitor_request_acked(int core_id, const char * const debug, ...)
{
va_list ap;
int res;
va_start(ap, debug);
res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap);
va_end(ap);
return res;
}
int ast_cc_monitor_callee_available(const int core_id, const char * const debug, ...)
{
va_list ap;
int res;
va_start(ap, debug);
res = cc_request_state_change(CC_CALLEE_READY, core_id, debug, ap);
va_end(ap);
return res;
}
int ast_cc_agent_caller_busy(int core_id, const char * debug, ...)
{
va_list ap;
int res;
va_start(ap, debug);
res = cc_request_state_change(CC_CALLER_BUSY, core_id, debug, ap);
va_end(ap);
return res;
}
int ast_cc_agent_caller_available(int core_id, const char * const debug, ...)
{
va_list ap;
int res;
va_start(ap, debug);
res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap);
va_end(ap);
return res;
}
int ast_cc_agent_recalling(int core_id, const char * const debug, ...)
{
va_list ap;
int res;
va_start(ap, debug);
res = cc_request_state_change(CC_RECALLING, core_id, debug, ap);
va_end(ap);
return res;
}
int ast_cc_completed(struct ast_channel *chan, const char * const debug, ...)
{
struct ast_datastore *recall_datastore;
struct cc_recall_ds_data *recall_data;
int core_id;
va_list ap;
int res;
ast_channel_lock(chan);
if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
/* Silly! Why did you call this function if there's no recall DS? */
ast_channel_unlock(chan);
return -1;
}
recall_data = recall_datastore->data;
if (recall_data->nested || recall_data->ignore) {
/* If this is being called from a nested Dial, it is too
* early to determine if the recall has actually completed.
* The outermost dial is the only one with the authority to
* declare the recall to be complete.
*
* Similarly, if this function has been called when the
* recall has progressed beyond the first dial, this is not
* a legitimate time to declare the recall to be done. In fact,
* that should have been done already.
*/
ast_channel_unlock(chan);
return -1;
}
core_id = recall_data->core_id;
ast_channel_unlock(chan);
va_start(ap, debug);
res = cc_request_state_change(CC_COMPLETE, core_id, debug, ap);
va_end(ap);
return res;
}
int ast_cc_failed(int core_id, const char * const debug, ...)
{
va_list ap;
int res;
va_start(ap, debug);
res = cc_request_state_change(CC_FAILED, core_id, debug, ap);
va_end(ap);
return res;
}
struct ast_cc_monitor_failure_data {
const char *device_name;
char *debug;
int core_id;
};
static int cc_monitor_failed(void *data)
{
struct ast_cc_monitor_failure_data *failure_data = data;
struct cc_core_instance *core_instance;
struct ast_cc_monitor *monitor_iter;
core_instance = find_cc_core_instance(failure_data->core_id);
if (!core_instance) {
/* Core instance no longer exists or invalid core_id. */
ast_log_dynamic_level(cc_logger_level,
"Core %d: Could not find core instance for device %s '%s'\n",
failure_data->core_id, failure_data->device_name, failure_data->debug);
ast_free((char *) failure_data->device_name);
ast_free((char *) failure_data->debug);
ast_free(failure_data);
return -1;
}
AST_LIST_LOCK(core_instance->monitors);
AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
if (!strcmp(monitor_iter->interface->device_name, failure_data->device_name)) {
AST_LIST_REMOVE_CURRENT(next);
cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
monitor_iter->interface->device_name, 1);
monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id);
cc_publish_monitorfailed(monitor_iter->core_id, monitor_iter->interface->device_name);
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
cc_unref(monitor_iter, "Monitor reported failure. Unref list's reference.");
}
}
}
AST_LIST_TRAVERSE_SAFE_END;
if (!has_device_monitors(core_instance)) {
ast_cc_failed(core_instance->core_id, "All monitors have failed\n");
}
AST_LIST_UNLOCK(core_instance->monitors);
cc_unref(core_instance, "Finished with core_instance in cc_monitor_failed\n");
ast_free((char *) failure_data->device_name);
ast_free((char *) failure_data->debug);
ast_free(failure_data);
return 0;
}
int ast_cc_monitor_failed(int core_id, const char *const monitor_name, const char * const debug, ...)
{
struct ast_cc_monitor_failure_data *failure_data;
int res;
va_list ap;
if (!(failure_data = ast_calloc(1, sizeof(*failure_data)))) {
return -1;
}
if (!(failure_data->device_name = ast_strdup(monitor_name))) {
ast_free(failure_data);
return -1;
}
va_start(ap, debug);
if (ast_vasprintf(&failure_data->debug, debug, ap) == -1) {
va_end(ap);
ast_free((char *)failure_data->device_name);
ast_free(failure_data);
return -1;
}
va_end(ap);
failure_data->core_id = core_id;
res = ast_taskprocessor_push(cc_core_taskprocessor, cc_monitor_failed, failure_data);
if (res) {
ast_free((char *)failure_data->device_name);
ast_free((char *)failure_data->debug);
ast_free(failure_data);
}
return res;
}
static int cc_status_request(void *data)
{
struct cc_core_instance *core_instance= data;
int res;
res = core_instance->agent->callbacks->status_request(core_instance->agent);
cc_unref(core_instance, "Status request finished. Unref core instance");
return res;
}
int ast_cc_monitor_status_request(int core_id)
{
int res;