]> git.ipfire.org Git - thirdparty/freeswitch.git/blob - src/switch_ivr_originate.c
99c70991bdeed8cd45c9bef1a6db4cc1d51443ea
[thirdparty/freeswitch.git] / src / switch_ivr_originate.c
1 /*
2 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3 * Copyright (C) 2005-2021, Anthony Minessale II <anthm@freeswitch.org>
4 *
5 * Version: MPL 1.1
6 *
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
16 *
17 * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18 *
19 * The Initial Developer of the Original Code is
20 * Anthony Minessale II <anthm@freeswitch.org>
21 * Portions created by the Initial Developer are Copyright (C)
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Anthony Minessale II <anthm@freeswitch.org>
27 * Michael Jerris <mike@jerris.com>
28 * Travis Cross <tc@traviscross.com>
29 *
30 * switch_ivr_originate.c -- IVR Library (originate)
31 *
32 */
33
34 #include <switch.h>
35 #define QUOTED_ESC_COMMA 1
36 #define UNQUOTED_ESC_COMMA 2
37
38 static const switch_state_handler_table_t originate_state_handlers;
39
40 static switch_status_t originate_on_consume_media_transmit(switch_core_session_t *session)
41 {
42 switch_channel_t *channel = switch_core_session_get_channel(session);
43
44 if (!switch_channel_test_flag(channel, CF_PROXY_MODE) && switch_channel_test_flag(channel, CF_CONSUME_ON_ORIGINATE)) {
45 while (switch_channel_test_flag(channel, CF_ORIGINATING) &&
46 switch_channel_get_state(channel) == CS_CONSUME_MEDIA && !switch_channel_test_flag(channel, CF_TAGGED)) {
47 if (!switch_channel_media_ready(channel)) {
48 switch_yield(10000);
49 } else {
50 switch_ivr_sleep(session, 10, SWITCH_FALSE, NULL);
51 }
52 switch_ivr_parse_all_messages(session);
53 }
54 }
55
56 switch_channel_clear_state_handler(channel, &originate_state_handlers);
57
58 return SWITCH_STATUS_FALSE;
59 }
60
61 static switch_status_t originate_on_routing(switch_core_session_t *session)
62 {
63 switch_channel_t *channel = switch_core_session_get_channel(session);
64
65 if (switch_channel_get_state(channel) == CS_ROUTING) {
66 /* put the channel in a passive state until it is answered */
67 switch_channel_set_state(channel, CS_CONSUME_MEDIA);
68 }
69
70 return SWITCH_STATUS_FALSE;
71 }
72
73 static const switch_state_handler_table_t originate_state_handlers = {
74 /*.on_init */ NULL,
75 /*.on_routing */ originate_on_routing,
76 /*.on_execute */ NULL,
77 /*.on_hangup */ NULL,
78 /*.on_exchange_media */ NULL,
79 /*.on_soft_execute */ originate_on_consume_media_transmit,
80 /*.on_consume_media */ originate_on_consume_media_transmit
81 };
82
83 #define MAX_PEERS 128
84
85 struct switch_dial_handle_s;
86
87
88 struct switch_dial_leg_list_s {
89 int leg_idx;
90 switch_dial_leg_t *legs[MAX_PEERS];
91 struct switch_dial_handle_s *handle;
92 };
93
94 struct switch_dial_leg_s {
95 char *dial_string;
96 switch_event_t *leg_vars;
97 struct switch_dial_handle_s *handle;
98 struct switch_dial_leg_s *next;
99 };
100
101 struct switch_dial_handle_s {
102 int is_sub;
103 int leg_list_idx;
104 switch_dial_leg_list_t *leg_lists[MAX_PEERS];
105 switch_event_t *global_vars;
106 switch_memory_pool_t *pool;
107 };
108
109 struct switch_dial_handle_list_s {
110 int handle_idx;
111 switch_dial_handle_t *handles[MAX_PEERS];
112 switch_event_t *global_vars;
113 switch_memory_pool_t *pool;
114 };
115
116 static switch_status_t switch_dial_handle_dup(switch_dial_handle_t **handle, switch_dial_handle_t *todup);
117
118 typedef struct {
119 switch_core_session_t *down_session;
120 switch_core_session_t *peer_session;
121 switch_channel_t *peer_channel;
122 switch_caller_profile_t *caller_profile;
123 uint8_t ring_ready;
124 uint8_t early_media;
125 uint8_t answered;
126 uint8_t tagged;
127 uint8_t array_pos;
128 uint32_t per_channel_timelimit_sec;
129 uint32_t per_channel_progress_timelimit_sec;
130 uint32_t per_channel_delay_start;
131 } originate_status_t;
132
133
134 typedef struct {
135 switch_core_session_t *session;
136 int32_t idx;
137 uint32_t hups;
138 char *file;
139 char *error_file;
140 int confirm_timeout;
141 int confirm_read_timeout;
142 char key[80];
143 uint8_t early_ok;
144 uint8_t ring_ready;
145 uint8_t instant_ringback;
146 uint8_t sent_ring;
147 uint8_t progress;
148 uint8_t return_ring_ready;
149 uint8_t monitor_early_media_ring;
150 uint8_t monitor_early_media_fail;
151 uint8_t gen_ringback;
152 uint8_t ignore_early_media;
153 uint8_t ignore_ring_ready;
154 int monitor_early_media_ring_count;
155 int monitor_early_media_ring_total;
156 switch_bool_t cancel_timeout;
157 int continue_on_timeout;
158 int ringback_ok;
159 int sending_ringback;
160 int bridge_early_media;
161 switch_thread_t *ethread;
162 switch_caller_profile_t *caller_profile_override;
163 switch_bool_t check_vars;
164 switch_memory_pool_t *pool;
165 originate_status_t originate_status[MAX_PEERS];// = { {0} };
166 } originate_global_t;
167
168
169
170 typedef enum {
171 IDX_XFER = -5,
172 IDX_KEY_CANCEL = -4,
173 IDX_TIMEOUT = -3,
174 IDX_CANCEL = -2,
175 IDX_NADA = -1
176 } abort_t;
177
178 struct key_collect {
179 char *key;
180 char *file;
181 char *error_file;
182 int confirm_read_timeout;
183 switch_core_session_t *session;
184 };
185
186 static void *SWITCH_THREAD_FUNC collect_thread_run(switch_thread_t *thread, void *obj)
187 {
188 struct key_collect *collect = (struct key_collect *) obj;
189 switch_channel_t *channel = NULL;
190 char buf[10] = SWITCH_BLANK_STRING;
191 switch_application_interface_t *application_interface = NULL;
192
193 if (!collect->session) {
194 return NULL;
195 }
196
197 channel = switch_core_session_get_channel(collect->session);
198 if (switch_core_session_read_lock(collect->session) != SWITCH_STATUS_SUCCESS) {
199 return NULL;
200 }
201
202 switch_ivr_sleep(collect->session, 0, SWITCH_TRUE, NULL);
203
204 if (!strcasecmp(collect->key, "exec")) {
205 char *data;
206 char *app_name, *app_data;
207
208 if (!(data = collect->file)) {
209 goto wbreak;
210 }
211
212 app_name = data;
213
214 if ((app_data = strchr(app_name, ' '))) {
215 *app_data++ = '\0';
216 }
217
218 if ((application_interface = switch_loadable_module_get_application_interface(app_name)) == 0) {
219 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(collect->session), SWITCH_LOG_ERROR, "Invalid Application %s\n", app_name);
220 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
221 goto wbreak;
222 }
223
224 if (!application_interface->application_function) {
225 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(collect->session), SWITCH_LOG_ERROR, "No Function for %s\n", app_name);
226 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
227 goto wbreak;
228 }
229
230 switch_core_session_exec(collect->session, application_interface, app_data);
231
232 if (switch_channel_up_nosig(channel)) {
233 switch_channel_set_flag(channel, CF_WINNER);
234 switch_channel_set_variable(channel, "group_dial_status", "winner");
235 }
236
237 goto wbreak;
238 }
239
240 if (!switch_channel_up_nosig(channel)) {
241 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
242 goto wbreak;
243 }
244
245 while (switch_channel_ready(channel)) {
246 switch_size_t len = strlen(collect->key);
247 const char *file = collect->file;
248 switch_status_t status;
249
250 memset(buf, 0, sizeof(buf));
251
252 if (zstr(file)) {
253 file = "silence";
254 }
255
256 status = switch_ivr_read(collect->session,
257 (uint32_t)len,
258 (uint32_t)len,
259 file, NULL, buf, sizeof(buf), collect->confirm_read_timeout, NULL, 0);
260
261
262 if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK && status != SWITCH_STATUS_TOO_SMALL) {
263 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(collect->session), SWITCH_LOG_ERROR, "%s Error Playing File!\n",
264 switch_channel_get_name(channel));
265 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
266 }
267
268 if (!strcmp(collect->key, buf)) {
269 switch_channel_set_flag(channel, CF_WINNER);
270 switch_channel_set_variable(channel, "group_dial_status", "winner");
271 goto wbreak;
272 } else if (collect->error_file) {
273 switch_ivr_play_file(collect->session, NULL, collect->error_file, NULL);
274 }
275 }
276
277 wbreak:
278
279 switch_core_session_rwunlock(collect->session);
280
281 UNPROTECT_INTERFACE(application_interface);
282
283 return NULL;
284 }
285
286 static void launch_collect_thread(struct key_collect *collect)
287 {
288 switch_thread_t *thread;
289 switch_threadattr_t *thd_attr = NULL;
290
291 switch_threadattr_create(&thd_attr, switch_core_session_get_pool(collect->session));
292 switch_threadattr_detach_set(thd_attr, 1);
293 switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
294 switch_thread_create(&thread, thd_attr, collect_thread_run, collect, switch_core_session_get_pool(collect->session));
295 }
296
297 static int check_per_channel_timeouts(originate_global_t *oglobals,
298 int max, time_t start, switch_call_cause_t *force_reason)
299 {
300 int x = 0, i, delayed_channels = 0, active_channels = 0;
301 uint32_t early_exit_time = 0, delayed_min = 0;
302
303 time_t elapsed = switch_epoch_time_now(NULL) - start;
304
305 for (i = 0; i < max; i++) {
306 if (oglobals->originate_status[i].peer_channel && switch_channel_get_state(oglobals->originate_status[i].peer_channel) != CS_DESTROY &&
307 switch_channel_get_state(oglobals->originate_status[i].peer_channel) != CS_REPORTING) {
308 if (oglobals->originate_status[i].per_channel_delay_start) {
309 delayed_channels++;
310 } else {
311 active_channels++;
312 }
313 }
314 }
315
316 if (active_channels == 0 && delayed_channels) {
317 for (i = 0; i < max; i++) {
318 if (oglobals->originate_status[i].peer_channel && oglobals->originate_status[i].per_channel_delay_start &&
319 (!delayed_min || delayed_min > oglobals->originate_status[i].per_channel_delay_start)) {
320 delayed_min = oglobals->originate_status[i].per_channel_delay_start;
321 }
322 }
323
324 early_exit_time = delayed_min - (uint32_t)(switch_time_t) elapsed;
325 }
326
327 for (i = 0; i < max; i++) {
328 if (oglobals->originate_status[i].peer_channel && oglobals->originate_status[i].per_channel_delay_start &&
329 (elapsed > oglobals->originate_status[i].per_channel_delay_start || active_channels == 0)) {
330 if (active_channels == 0) {
331 if (oglobals->originate_status[i].per_channel_timelimit_sec) {
332 if (oglobals->originate_status[i].per_channel_timelimit_sec > early_exit_time) {
333 /* IN theory this check is not needed ( should just be if !0 then -= with no else), if its not 0 it should always be greater.... */
334 oglobals->originate_status[i].per_channel_timelimit_sec -= early_exit_time;
335 } else {
336 oglobals->originate_status[i].per_channel_timelimit_sec = 1;
337 }
338 }
339
340 if (oglobals->originate_status[i].per_channel_progress_timelimit_sec) {
341 if (oglobals->originate_status[i].per_channel_progress_timelimit_sec > early_exit_time) {
342 /* IN theory this check is not needed ( should just be if !0 then -= with no else), if its not 0 it should always be greater.... */
343 oglobals->originate_status[i].per_channel_progress_timelimit_sec -= early_exit_time;
344 } else {
345 oglobals->originate_status[i].per_channel_progress_timelimit_sec = 1;
346 }
347 }
348
349 oglobals->originate_status[i].per_channel_delay_start -= delayed_min;
350 } else {
351 oglobals->originate_status[i].per_channel_delay_start = 0;
352 }
353
354 if (!oglobals->originate_status[i].per_channel_delay_start) {
355 switch_channel_clear_flag(oglobals->originate_status[i].peer_channel, CF_BLOCK_STATE);
356 }
357 }
358
359 if (oglobals->originate_status[i].peer_channel && switch_channel_up_nosig(oglobals->originate_status[i].peer_channel)) {
360 if (oglobals->originate_status[i].per_channel_progress_timelimit_sec && elapsed > oglobals->originate_status[i].per_channel_progress_timelimit_sec &&
361 !(switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_RING_READY) ||
362 switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_ANSWERED) ||
363 (!oglobals->monitor_early_media_ring && switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_EARLY_MEDIA))
364 )
365 ) {
366 switch_channel_hangup(oglobals->originate_status[i].peer_channel, SWITCH_CAUSE_PROGRESS_TIMEOUT);
367 *force_reason = SWITCH_CAUSE_PROGRESS_TIMEOUT;
368 x++;
369 }
370 if (oglobals->originate_status[i].per_channel_timelimit_sec && elapsed > oglobals->originate_status[i].per_channel_timelimit_sec) {
371 switch_channel_hangup(oglobals->originate_status[i].peer_channel, SWITCH_CAUSE_ALLOTTED_TIMEOUT);
372 x++;
373 }
374 }
375 }
376
377 return x;
378 }
379
380
381 static switch_bool_t monitor_callback(switch_core_session_t *session, const char *app, const char *data)
382 {
383 if (app) {
384 switch_channel_t *channel = switch_core_session_get_channel(session);
385 if (!strcmp(app, "fail")) {
386 const char *bd = switch_channel_get_variable(channel, "monitor_fail_dispo");
387 if (!bd) {
388 bd = "monitor_early_media_fail";
389 }
390 switch_channel_set_variable(channel, "DIALSTATUS", "BUSY");
391 switch_channel_set_variable(channel, "originate_disposition", bd);
392 switch_channel_hangup(channel, data ? switch_channel_str2cause(data) : SWITCH_CAUSE_USER_BUSY);
393 } else if (!strcmp(app, "ring")) {
394 originate_global_t *oglobals = (originate_global_t *) switch_channel_get_private(channel, "_oglobals_");
395 const char *bd = switch_channel_get_variable(channel, "monitor_ring_dispo");
396 if (!bd) {
397 bd = "monitor_early_media_ring";
398 }
399 switch_channel_set_variable(channel, "originate_disposition", bd);
400 switch_channel_set_variable(channel, "DIALSTATUS", "EARLY");
401
402 if (oglobals) {
403 if (oglobals->monitor_early_media_ring_total && ++oglobals->monitor_early_media_ring_count < oglobals->monitor_early_media_ring_total) {
404 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Ring %d/%d\n",
405 oglobals->monitor_early_media_ring_count, oglobals->monitor_early_media_ring_total);
406 return SWITCH_TRUE;
407 }
408
409 switch_channel_set_private(channel, "_oglobals_", NULL);
410
411 if (!oglobals->progress) {
412 oglobals->progress = 1;
413 }
414
415 if (!oglobals->ring_ready && !oglobals->ignore_ring_ready) {
416 oglobals->ring_ready = 1;
417 }
418
419 if (!oglobals->ignore_early_media && !oglobals->early_ok) {
420 oglobals->early_ok = 1;
421 }
422 }
423 }
424 }
425
426 return SWITCH_FALSE;
427 }
428
429 static void inherit_codec(switch_channel_t *caller_channel, switch_core_session_t *session)
430 {
431 const char *var = switch_channel_get_variable(caller_channel, "inherit_codec");
432 switch_channel_t *channel = switch_core_session_get_channel(session);
433
434 if (!zstr(var) && !strcasecmp(var, "passthru")) {
435 switch_channel_set_variable(caller_channel, "absolute_codec_string", switch_channel_get_variable(channel, "ep_codec_string"));
436 } else if (switch_true(var)) {
437 switch_codec_implementation_t impl = { 0 };
438 switch_codec_implementation_t video_impl = { 0 };
439 char tmp[128] = "";
440
441 if (switch_core_session_get_read_impl(session, &impl) == SWITCH_STATUS_SUCCESS) {
442 const char *ep = switch_channel_get_variable(caller_channel, "ep_codec_string");
443
444 if (switch_core_session_get_video_read_impl(session, &video_impl) == SWITCH_STATUS_SUCCESS) {
445 switch_snprintf(tmp, sizeof(tmp), "%s@%uh@%ui,%s",
446 impl.iananame, impl.samples_per_second, (uint32_t)impl.microseconds_per_packet / 1000,
447 video_impl.iananame);
448 } else {
449 switch_snprintf(tmp, sizeof(tmp), "%s@%uh@%ui",
450 impl.iananame, impl.samples_per_second, (uint32_t)impl.microseconds_per_packet / 1000);
451 }
452
453 if (ep && switch_stristr(impl.iananame, ep)) {
454 switch_channel_set_variable(caller_channel, "absolute_codec_string", tmp);
455 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "Setting codec string on %s to %s\n",
456 switch_channel_get_name(caller_channel), tmp);
457 } else {
458 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "Codec string %s not supported on %s, skipping inheritance\n",
459 tmp, switch_channel_get_name(caller_channel));
460 }
461 } else {
462 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_WARNING,
463 "Error inheriting codec. Channel %s has no read codec yet.\n",
464 switch_channel_get_name(channel));
465 }
466
467 }
468 }
469
470 static uint8_t check_channel_status(originate_global_t *oglobals, uint32_t len, switch_call_cause_t *force_reason, time_t start)
471 {
472
473 uint32_t i;
474 uint8_t rval = 0;
475 switch_channel_t *caller_channel = NULL;
476 int pindex = -1;
477 char bug_key[256] = "";
478 int send_ringback = 0;
479 uint8_t ring_ready_val = 0;
480 int pickups_without_timelimit = 0;
481
482 oglobals->hups = 0;
483 oglobals->idx = IDX_NADA;
484
485
486 if (oglobals->session) {
487 caller_channel = switch_core_session_get_channel(oglobals->session);
488 if (switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) {
489 caller_channel = NULL;
490 }
491 }
492
493
494 for (i = 0; i < len; i++) {
495 if (oglobals->originate_status[i].peer_channel && switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_CHANNEL_SWAP)) {
496 const char *key = switch_channel_get_variable(oglobals->originate_status[i].peer_channel, "channel_swap_uuid");
497 switch_core_session_t *swap_session, *old_session;
498
499 if ((swap_session = switch_core_session_locate(key))) {
500 switch_channel_clear_flag(oglobals->originate_status[i].peer_channel, CF_CHANNEL_SWAP);
501 switch_channel_hangup(oglobals->originate_status[i].peer_channel, SWITCH_CAUSE_PICKED_OFF);
502
503 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(oglobals->originate_status[i].peer_channel), SWITCH_LOG_DEBUG, "Swapping %s for %s\n",
504 switch_core_session_get_name(swap_session), switch_channel_get_name(oglobals->originate_status[i].peer_channel));
505
506
507 old_session = oglobals->originate_status[i].peer_session;
508 oglobals->originate_status[i].peer_session = swap_session;
509 oglobals->originate_status[i].peer_channel = switch_core_session_get_channel(oglobals->originate_status[i].peer_session);
510 oglobals->originate_status[i].caller_profile = switch_channel_get_caller_profile(oglobals->originate_status[i].peer_channel);
511 switch_channel_set_flag(oglobals->originate_status[i].peer_channel, CF_ORIGINATING);
512
513 switch_channel_answer(oglobals->originate_status[i].peer_channel);
514
515 switch_channel_set_variable(oglobals->originate_status[i].peer_channel, "picked_up_uuid", switch_core_session_get_uuid(old_session));
516 switch_channel_execute_on(oglobals->originate_status[i].peer_channel, "execute_on_pickup");
517 switch_channel_api_on(oglobals->originate_status[i].peer_channel, "api_on_pickup");
518
519 switch_core_session_rwunlock(old_session);
520 break;
521 }
522 }
523 }
524
525 for (i = 0; i < len; i++) {
526 switch_channel_state_t state;
527
528 if (oglobals->originate_status[i].tagged && oglobals->originate_status[i].peer_session) {
529 switch_channel_t *channel = switch_core_session_get_channel(oglobals->originate_status[i].peer_session);
530 uint32_t j;
531
532 if (switch_channel_down_nosig(channel)) {
533 switch_call_cause_t cause = switch_channel_get_cause(channel);
534
535 for (j = 0; j < len; j++) {
536 channel = switch_core_session_get_channel(oglobals->originate_status[j].peer_session);
537 switch_channel_hangup(channel, cause);
538 }
539 oglobals->hups = len;
540 rval = 0;
541 goto end;
542 }
543 }
544
545
546 if (oglobals->originate_status[i].peer_channel && switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_PICKUP)) {
547 if (oglobals->originate_status[i].per_channel_timelimit_sec == 0) {
548 pickups_without_timelimit++;
549 }
550 }
551
552 if (!(oglobals->originate_status[i].peer_channel && oglobals->originate_status[i].peer_session)) {
553 oglobals->hups++;
554 continue;
555 }
556
557 if ((ring_ready_val = (uint8_t)switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_RING_READY))) {
558 if (!oglobals->originate_status[i].ring_ready) {
559 oglobals->originate_status[i].ring_ready = ring_ready_val;
560 }
561
562 if (oglobals->sending_ringback == 1) {
563 send_ringback++;
564 pindex = (uint32_t) i;
565 } else {
566 if (!oglobals->ring_ready) {
567 oglobals->ring_ready = ring_ready_val;
568 if (caller_channel && !oglobals->ignore_ring_ready) {
569 if (len == 1) {
570 switch_channel_pass_callee_id(oglobals->originate_status[0].peer_channel, caller_channel);
571 }
572 switch_channel_ring_ready_value(caller_channel, ring_ready_val);
573 oglobals->sent_ring = ring_ready_val;
574 }
575 }
576 }
577 }
578
579 if (switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_EARLY_MEDIA)) {
580
581 if (oglobals->ignore_early_media == 3 && oglobals->bridge_early_media == -1) {
582 oglobals->bridge_early_media = i;
583 oglobals->ringback_ok = 1;
584 }
585
586 if (oglobals->sending_ringback == 1) {
587 send_ringback++;
588 pindex = (uint32_t) i;
589 } else if (!oglobals->sent_ring && oglobals->ignore_early_media == 2 && len == 1 && caller_channel && !oglobals->ignore_ring_ready) {
590 switch_channel_pass_callee_id(oglobals->originate_status[0].peer_channel, caller_channel);
591 switch_channel_ring_ready(caller_channel);
592 oglobals->sent_ring = 1;
593 }
594
595 if (!oglobals->originate_status[i].early_media) {
596 oglobals->originate_status[i].early_media = 1;
597 if (oglobals->early_ok) {
598 pindex = i;
599 }
600
601 if (oglobals->monitor_early_media_fail) {
602 const char *var = switch_channel_get_variable(oglobals->originate_status[i].peer_channel, "monitor_early_media_fail");
603 if (!zstr(var)) {
604 char *fail_array[128] = { 0 };
605 int fail_count = 0;
606 char *fail_data = strdup(var);
607 int fx;
608 int y = 0;
609
610 switch_assert(fail_data);
611 fail_count = switch_separate_string(fail_data, '!', fail_array, (sizeof(fail_array) / sizeof(fail_array[0])));
612
613 for (fx = 0; fx < fail_count; fx++) {
614 char *cause = fail_array[fx];
615 int hits = 2;
616 char *p, *q;
617
618 if (!(p = strchr(cause, ':'))) {
619 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
620 continue;
621 }
622 *p++ = '\0';
623
624
625 if (!p) {
626 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
627 continue;
628 }
629
630
631 if (!(hits = atoi(p))) {
632 hits = 2;
633 }
634
635
636 if (!(p = strchr(p, ':'))) {
637 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
638 continue;
639 }
640 *p++ = '\0';
641
642 if (!p) {
643 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
644 continue;
645 }
646
647 for (q = p; q && *q; q++) {
648 if (*q == '+') {
649 *q = ',';
650 }
651 }
652 switch_snprintf(bug_key, sizeof(bug_key), "monitor_early_media_fail_%d", ++y);
653 switch_ivr_tone_detect_session(oglobals->originate_status[i].peer_session, bug_key, p, "r", 0, hits, "fail", cause, monitor_callback);
654
655 }
656
657 switch_safe_free(fail_data);
658
659 }
660 }
661
662 if (oglobals->monitor_early_media_ring) {
663 const char *var = switch_channel_get_variable(oglobals->originate_status[i].peer_channel, "monitor_early_media_ring");
664 const char *var_total = switch_channel_get_variable(oglobals->originate_status[i].peer_channel, "monitor_early_media_ring_total");
665 if (!zstr(var)) {
666 char *ring_array[128] = { 0 };
667 int ring_count = 0;
668 char *ring_data = strdup(var);
669 int fx;
670 int y = 0;
671
672 switch_assert(ring_data);
673 ring_count = switch_separate_string(ring_data, '!', ring_array, (sizeof(ring_array) / sizeof(ring_array[0])));
674
675 for (fx = 0; fx < ring_count; fx++) {
676 int hits = 2;
677 char *p = ring_array[fx], *q;
678
679 if (!p) {
680 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
681 continue;
682 }
683
684 if (!(hits = atoi(p))) {
685 hits = 2;
686 }
687
688 if (!(p = strchr(p, ':'))) {
689 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
690 continue;
691 }
692 *p++ = '\0';
693
694 if (!p) {
695 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
696 continue;
697 }
698
699 for (q = p; q && *q; q++) {
700 if (*q == '+') {
701 *q = ',';
702 }
703 }
704
705 switch_channel_set_private(oglobals->originate_status[i].peer_channel, "_oglobals_", oglobals);
706 switch_snprintf(bug_key, sizeof(bug_key), "monitor_early_media_ring_%d", ++y);
707 switch_ivr_tone_detect_session(oglobals->originate_status[i].peer_session, bug_key, p, "r", 0, hits, "ring", NULL, monitor_callback);
708
709 }
710
711 if (var_total) {
712 int tmp = atoi(var_total);
713 if (tmp > 0 && tmp < 100) {
714 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->originate_status[i].peer_session), SWITCH_LOG_DEBUG,
715 "%s setting ring total to %d\n", switch_channel_get_name(oglobals->originate_status[i].peer_channel), tmp);
716 oglobals->monitor_early_media_ring_total = tmp;
717 }
718 }
719
720 switch_safe_free(ring_data);
721
722 }
723 }
724 }
725
726 if (!oglobals->monitor_early_media_ring) {
727
728 if (!oglobals->progress) {
729 oglobals->progress = 1;
730 }
731
732 if (!oglobals->ring_ready && !oglobals->ignore_ring_ready) {
733 oglobals->ring_ready = 1;
734
735 }
736 }
737 }
738
739 if (!switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_PARK) &&
740 !switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_CONSUME_ON_ORIGINATE)) {
741 if (switch_core_session_messages_waiting(oglobals->originate_status[i].peer_session)) {
742 if (switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_THREAD_SLEEPING)) {
743 switch_core_session_wake_session_thread(oglobals->originate_status[i].peer_session);
744 } else {
745 switch_ivr_parse_all_events(oglobals->originate_status[i].peer_session);
746 }
747 }
748 }
749
750 if (switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_EARLY_OK)) {
751 if (!oglobals->early_ok) {
752 oglobals->early_ok = 1;
753 }
754 switch_channel_clear_flag(oglobals->originate_status[i].peer_channel, CF_EARLY_OK);
755 }
756
757 if (caller_channel && switch_channel_test_flag(caller_channel, CF_EARLY_OK)) {
758 if (!oglobals->early_ok) {
759 oglobals->early_ok = 1;
760 }
761 switch_channel_clear_flag(caller_channel, CF_EARLY_OK);
762 }
763
764 state = switch_channel_get_state(oglobals->originate_status[i].peer_channel);
765 if (state >= CS_HANGUP || state == CS_RESET || switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_TRANSFER) ||
766 switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_REDIRECT) ||
767 switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_BRIDGED) ||
768 !switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_ORIGINATING)
769 ) {
770 (oglobals->hups)++;
771 if (switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_PICKUP)) {
772 if (oglobals->originate_status[i].per_channel_timelimit_sec == 0) {
773 pickups_without_timelimit--;
774 }
775 }
776 } else if ((switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_ANSWERED) ||
777 (oglobals->early_ok && switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_EARLY_MEDIA)) ||
778 (oglobals->ring_ready && oglobals->return_ring_ready && len == 1 &&
779 switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_RING_READY))
780 )
781 && !switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_TAGGED)
782 ) {
783
784 const char *group_confirm_key = switch_channel_get_variable(oglobals->originate_status[i].peer_channel, "group_confirm_key");
785 const char *group_confirm_file = switch_channel_get_variable(oglobals->originate_status[i].peer_channel, "group_confirm_file");
786
787 if (!zstr(oglobals->key) || !zstr(group_confirm_key)) {
788 struct key_collect *collect;
789 const char *group_confirm_timeout = switch_channel_get_variable(oglobals->originate_status[i].peer_channel, "group_confirm_timeout");
790 int extend_timeout = 0;
791 int cancel_timeout = 0;
792 if (group_confirm_timeout && switch_is_number(group_confirm_timeout)) {
793 // leg var overrides global group_confirm_timeout
794 extend_timeout = atoi(group_confirm_timeout);
795 if (extend_timeout == 0) {
796 cancel_timeout = 1;
797 }
798 } else {
799 extend_timeout = oglobals->confirm_timeout;
800 }
801
802 if (extend_timeout > 0) {
803 /* extend timeout for this leg only */
804 time_t elapsed = switch_epoch_time_now(NULL) - start;
805 oglobals->originate_status[i].per_channel_progress_timelimit_sec = elapsed + extend_timeout;
806 oglobals->originate_status[i].per_channel_timelimit_sec = elapsed + extend_timeout;
807 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "elapsed %" TIME_T_FMT ", timelimit extended to %u\n", elapsed, oglobals->originate_status[i].per_channel_timelimit_sec);
808 } else if (oglobals->cancel_timeout || cancel_timeout) {
809 /* cancel timeout for this leg only */
810 oglobals->originate_status[i].per_channel_progress_timelimit_sec = 0;
811 oglobals->originate_status[i].per_channel_timelimit_sec = 0;
812 }
813
814 if ((collect = switch_core_session_alloc(oglobals->originate_status[i].peer_session, sizeof(*collect)))) {
815 switch_channel_set_flag(oglobals->originate_status[i].peer_channel, CF_TAGGED);
816 if (!zstr(group_confirm_key)) {
817 collect->key = switch_core_session_strdup(oglobals->originate_status[i].peer_session, group_confirm_key);
818 } else {
819 collect->key = switch_core_session_strdup(oglobals->originate_status[i].peer_session, oglobals->key);
820 }
821
822 if (!zstr(group_confirm_file)) {
823 collect->file = switch_core_session_strdup(oglobals->originate_status[i].peer_session, group_confirm_file);
824 } else if (!zstr(oglobals->file)) {
825 collect->file = switch_core_session_strdup(oglobals->originate_status[i].peer_session, oglobals->file);
826 }
827 if (!zstr(oglobals->error_file)) {
828 collect->error_file = switch_core_session_strdup(oglobals->originate_status[i].peer_session, oglobals->error_file);
829 }
830
831 if (oglobals->confirm_read_timeout) {
832 collect->confirm_read_timeout = oglobals->confirm_read_timeout;
833 } else {
834 collect->confirm_read_timeout = 5000;
835 }
836
837 switch_channel_audio_sync(oglobals->originate_status[i].peer_channel);
838 collect->session = oglobals->originate_status[i].peer_session;
839 launch_collect_thread(collect);
840 }
841 } else {
842 oglobals->idx = i;
843 pindex = (uint32_t) i;
844 rval = 0;
845 goto end;
846
847 }
848 } else if (switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_WINNER)) {
849 /* unset group_confirm variables */
850 switch_channel_set_variable(oglobals->originate_status[i].peer_channel, "group_confirm_key", NULL);
851 switch_channel_set_variable(oglobals->originate_status[i].peer_channel, "group_confirm_file", NULL);
852 switch_channel_set_variable(oglobals->originate_status[i].peer_channel, "group_confirm_error_file", NULL);
853 switch_channel_set_variable(oglobals->originate_status[i].peer_channel, "group_confirm_cancel_timeout", NULL);
854 switch_channel_set_variable(oglobals->originate_status[i].peer_channel, "group_confirm_read_timeout", NULL);
855 oglobals->idx = i;
856 rval = 0;
857 pindex = (uint32_t) i;
858 goto end;
859 }
860 }
861
862 if (oglobals->hups > 0 && oglobals->hups + pickups_without_timelimit == len) {
863 /* only pickup channels with no timelimit remain */
864 rval = 0;
865 } else {
866 rval = 1;
867 }
868
869 end:
870
871 if (rval == 0 && pickups_without_timelimit) {
872 for (i = 0; i < len; i++) {
873 if (oglobals->originate_status[i].peer_channel && switch_channel_test_flag(oglobals->originate_status[i].peer_channel, CF_PICKUP) &&
874 switch_channel_up(oglobals->originate_status[i].peer_channel)) {
875 switch_channel_hangup(oglobals->originate_status[i].peer_channel, SWITCH_CAUSE_NO_PICKUP);
876 }
877 }
878 }
879
880
881 if (pindex > -1 && caller_channel && switch_channel_ready(caller_channel) && !switch_channel_media_ready(caller_channel) &&
882 switch_channel_media_ready(oglobals->originate_status[pindex].peer_channel)) {
883 inherit_codec(caller_channel, oglobals->originate_status[pindex].peer_session);
884 }
885
886 if (send_ringback) {
887 oglobals->sending_ringback++;
888 }
889
890 return rval;
891
892 }
893
894 struct ringback {
895 switch_buffer_t *audio_buffer;
896 teletone_generation_session_t ts;
897 switch_file_handle_t fhb;
898 switch_file_handle_t *fh;
899 int silence;
900 uint8_t asis;
901 int channels;
902 void *mux_buf;
903 int mux_buflen;
904 };
905
906 typedef struct ringback ringback_t;
907
908 static int teletone_handler(teletone_generation_session_t *ts, teletone_tone_map_t *map)
909 {
910 ringback_t *tto = ts->user_data;
911 int wrote;
912 void *buf;
913 int buflen;
914
915 if (!tto) {
916 return -1;
917 }
918 wrote = teletone_mux_tones(ts, map);
919 if (wrote <= 0) {
920 return -1;
921 }
922
923 if (tto->channels != 1) {
924 if (tto->mux_buflen < wrote * 2 * tto->channels) {
925 tto->mux_buflen = wrote * 2 * tto->channels;
926 tto->mux_buf = realloc(tto->mux_buf, tto->mux_buflen);
927 }
928 memcpy(tto->mux_buf, ts->buffer, wrote * 2);
929 switch_mux_channels((int16_t *) tto->mux_buf, wrote, 1, tto->channels);
930 buf = tto->mux_buf;
931 buflen = wrote * 2 * tto->channels;
932 } else {
933 buf = ts->buffer;
934 buflen = wrote * 2;
935 }
936
937 switch_buffer_write(tto->audio_buffer, buf, buflen);
938
939 return 0;
940 }
941
942
943 SWITCH_DECLARE(switch_status_t) switch_ivr_wait_for_answer(switch_core_session_t *session, switch_core_session_t *peer_session)
944 {
945 switch_channel_t *caller_channel = NULL;
946 switch_channel_t *peer_channel = switch_core_session_get_channel(peer_session);
947 const char *ringback_data = NULL;
948 switch_frame_t write_frame = { 0 };
949 switch_codec_t write_codec = { 0 };
950 switch_codec_t *read_codec = switch_core_session_get_read_codec(session);
951 uint8_t pass = 0;
952 ringback_t ringback = { 0 };
953 switch_frame_t *read_frame = NULL;
954 switch_status_t status = SWITCH_STATUS_SUCCESS;
955 int timelimit = SWITCH_DEFAULT_TIMEOUT;
956 const char *var;
957 switch_time_t start = 0;
958 const char *cancel_key = NULL;
959 switch_channel_state_t wait_state = 0;
960
961 switch_assert(peer_channel);
962
963 if (switch_channel_get_state(peer_channel) == CS_RESET) {
964 switch_channel_set_state(peer_channel, CS_SOFT_EXECUTE);
965 }
966
967 if (session) {
968 caller_channel = switch_core_session_get_channel(session);
969 }
970
971 if ((switch_channel_test_flag(peer_channel, CF_ANSWERED) || switch_channel_test_flag(peer_channel, CF_EARLY_MEDIA))) {
972 goto end;
973 }
974
975 switch_zmalloc(write_frame.data, SWITCH_RECOMMENDED_BUFFER_SIZE);
976 write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
977
978 if (caller_channel && (var = switch_channel_get_variable(caller_channel, SWITCH_CALL_TIMEOUT_VARIABLE))) {
979 timelimit = atoi(var);
980 if (timelimit < 0) {
981 timelimit = SWITCH_DEFAULT_TIMEOUT;
982 }
983 }
984
985 timelimit *= 1000000;
986 start = switch_micro_time_now();
987
988 if (caller_channel) {
989 cancel_key = switch_channel_get_variable(caller_channel, "origination_cancel_key");
990
991 if (switch_channel_test_flag(caller_channel, CF_ANSWERED)) {
992 ringback_data = switch_channel_get_variable(caller_channel, "transfer_ringback");
993 }
994
995 if (!ringback_data) {
996 ringback_data = switch_channel_get_variable(caller_channel, "ringback");
997 }
998
999 if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE) || switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA)) {
1000 ringback_data = NULL;
1001 } else if (zstr(ringback_data)) {
1002 if ((var = switch_channel_get_variable(caller_channel, SWITCH_SEND_SILENCE_WHEN_IDLE_VARIABLE))) {
1003 int sval = atoi(var);
1004
1005 if (sval) {
1006 ringback_data = switch_core_session_sprintf(session, "silence:%d", sval);
1007 }
1008 }
1009 }
1010 }
1011
1012
1013 if (read_codec && ringback_data) {
1014 if (switch_is_file_path(ringback_data)) {
1015 if (!(strrchr(ringback_data, '.') || strstr(ringback_data, SWITCH_URL_SEPARATOR))) {
1016 ringback.asis++;
1017 }
1018 }
1019
1020
1021
1022 if (!ringback.asis) {
1023 if ((pass = (uint8_t) switch_test_flag(read_codec, SWITCH_CODEC_FLAG_PASSTHROUGH))) {
1024 goto no_ringback;
1025 }
1026
1027 if (switch_core_codec_init(&write_codec,
1028 "L16",
1029 NULL,
1030 NULL,
1031 read_codec->implementation->actual_samples_per_second,
1032 read_codec->implementation->microseconds_per_packet / 1000,
1033 read_codec->implementation->number_of_channels, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
1034 switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
1035 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec Error!\n");
1036 if (caller_channel) {
1037 switch_channel_hangup(caller_channel, SWITCH_CAUSE_BEARERCAPABILITY_NOTIMPL);
1038 }
1039 read_codec = NULL;
1040 goto done;
1041 } else {
1042 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
1043 "Raw Codec Activation Success L16@%uhz 1 channel %dms\n",
1044 read_codec->implementation->actual_samples_per_second, read_codec->implementation->microseconds_per_packet / 1000);
1045
1046 write_frame.codec = &write_codec;
1047 write_frame.datalen = read_codec->implementation->decoded_bytes_per_packet;
1048 write_frame.samples = write_frame.datalen / 2;
1049 memset(write_frame.data, 255, write_frame.datalen);
1050 switch_core_session_set_read_codec(session, &write_codec);
1051 }
1052 }
1053
1054 if (switch_channel_test_flag(caller_channel, CF_DISABLE_RINGBACK)) {
1055 ringback_data = NULL;
1056 }
1057
1058 if (ringback_data) {
1059 char *tmp_data = NULL;
1060
1061 if (switch_is_file_path(ringback_data)) {
1062 char *ext;
1063
1064 if (ringback.asis) {
1065 write_frame.codec = read_codec;
1066 ext = read_codec->implementation->iananame;
1067 tmp_data = switch_mprintf("%s.%s", ringback_data, ext);
1068 ringback_data = tmp_data;
1069 }
1070
1071 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Play Ringback File [%s]\n", ringback_data);
1072
1073 ringback.fhb.channels = read_codec->implementation->number_of_channels;
1074 ringback.fhb.samplerate = read_codec->implementation->actual_samples_per_second;
1075 if (switch_core_file_open(&ringback.fhb,
1076 ringback_data,
1077 read_codec->implementation->number_of_channels,
1078 read_codec->implementation->actual_samples_per_second,
1079 SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
1080 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error Playing File\n");
1081 switch_safe_free(tmp_data);
1082 goto done;
1083 }
1084 ringback.fh = &ringback.fhb;
1085 } else {
1086 if (!strncasecmp(ringback_data, "silence", 7)) {
1087 const char *p = ringback_data + 7;
1088 if (*p == ':') {
1089 p++;
1090 if (p) {
1091 ringback.silence = atoi(p);
1092 }
1093 }
1094 SWITCH_IVR_VERIFY_SILENCE_DIVISOR(ringback.silence);
1095 } else {
1096 switch_buffer_create_dynamic(&ringback.audio_buffer, 512, 1024, 0);
1097 switch_buffer_set_loops(ringback.audio_buffer, -1);
1098
1099 teletone_init_session(&ringback.ts, 0, teletone_handler, &ringback);
1100 ringback.ts.rate = read_codec->implementation->actual_samples_per_second;
1101 ringback.channels = read_codec->implementation->number_of_channels;
1102 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Play Ringback Tone [%s]\n", ringback_data);
1103 if (teletone_run(&ringback.ts, ringback_data)) {
1104 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error Playing Tone\n");
1105 teletone_destroy_session(&ringback.ts);
1106 switch_buffer_destroy(&ringback.audio_buffer);
1107 ringback_data = NULL;
1108 }
1109 }
1110 }
1111 switch_safe_free(tmp_data);
1112 }
1113 }
1114
1115 no_ringback:
1116
1117 if (caller_channel) {
1118 wait_state = switch_channel_get_state(caller_channel);
1119 }
1120
1121 while (switch_channel_ready(peer_channel) && !switch_channel_media_ready(peer_channel)) {
1122 int diff = (int) (switch_micro_time_now() - start);
1123
1124 switch_ivr_parse_all_messages(session);
1125
1126 if (caller_channel && cancel_key) {
1127 if (switch_channel_has_dtmf(caller_channel)) {
1128 switch_dtmf_t dtmf = { 0, 0 };
1129 if (switch_channel_dequeue_dtmf(caller_channel, &dtmf) == SWITCH_STATUS_SUCCESS) {
1130 if (dtmf.digit == *cancel_key) {
1131 status = SWITCH_STATUS_FALSE;
1132 goto done;
1133 }
1134 }
1135 }
1136 }
1137
1138 if (caller_channel && switch_channel_get_state(caller_channel) != wait_state) {
1139 goto done;
1140 }
1141
1142 if (diff > timelimit) {
1143 status = SWITCH_STATUS_TIMEOUT;
1144 goto done;
1145 }
1146
1147 if (switch_channel_media_ready(caller_channel)) {
1148 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1149 if (!SWITCH_READ_ACCEPTABLE(status)) {
1150 break;
1151 }
1152 } else {
1153 read_frame = NULL;
1154 }
1155
1156 if (read_frame && !pass) {
1157
1158 if (!write_frame.codec) {
1159 status = SWITCH_STATUS_FALSE;
1160 break;
1161 }
1162
1163 if (ringback.fh) {
1164 switch_size_t mlen, olen;
1165 unsigned int pos = 0;
1166
1167 if (ringback.asis) {
1168 mlen = write_frame.codec->implementation->encoded_bytes_per_packet;
1169 } else {
1170 mlen = write_frame.codec->implementation->samples_per_packet;
1171 }
1172
1173 olen = mlen;
1174 //if (ringback.fh->resampler && ringback.fh->resampler->rfactor > 1) {
1175 //olen = (switch_size_t) (olen * ringback.fh->resampler->rfactor);
1176 //}
1177 switch_core_file_read(ringback.fh, write_frame.data, &olen);
1178
1179 if (olen == 0) {
1180 olen = mlen;
1181 ringback.fh->speed = 0;
1182 switch_core_file_seek(ringback.fh, &pos, 0, SEEK_SET);
1183 switch_core_file_read(ringback.fh, write_frame.data, &olen);
1184 if (olen == 0) {
1185 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_ERROR,
1186 "Failure to read or re-read after seeking to beginning on file [%s]\n", ringback.fh->file_path);
1187 break;
1188 }
1189 }
1190 write_frame.datalen = (uint32_t) (ringback.asis ? olen : olen * 2 * ringback.fh->channels);
1191 } else if (ringback.audio_buffer) {
1192 if ((write_frame.datalen = (uint32_t) switch_buffer_read_loop(ringback.audio_buffer,
1193 write_frame.data,
1194 write_frame.codec->implementation->decoded_bytes_per_packet)) <= 0) {
1195 break;
1196 }
1197 } else if (ringback.silence) {
1198 write_frame.datalen = write_frame.codec->implementation->decoded_bytes_per_packet;
1199 switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.datalen / 2,
1200 write_frame.codec->implementation->number_of_channels, ringback.silence);
1201 }
1202
1203 if ((ringback.fh || ringback.silence || ringback.audio_buffer) && write_frame.codec && write_frame.datalen) {
1204 if (switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
1205 break;
1206 }
1207 }
1208 } else {
1209 switch_cond_next();
1210 }
1211 }
1212
1213 done:
1214
1215 if (ringback.fh) {
1216 switch_core_file_close(ringback.fh);
1217 ringback.fh = NULL;
1218 } else if (ringback.audio_buffer) {
1219 teletone_destroy_session(&ringback.ts);
1220 switch_safe_free(ringback.mux_buf);
1221 switch_buffer_destroy(&ringback.audio_buffer);
1222 }
1223
1224
1225 switch_ivr_parse_all_events(session);
1226
1227 switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
1228
1229 if (switch_core_codec_ready(&write_codec)) {
1230 switch_core_codec_destroy(&write_codec);
1231 }
1232
1233 switch_safe_free(write_frame.data);
1234
1235 end:
1236
1237 if (!switch_channel_media_ready(peer_channel)) {
1238 if (switch_channel_up_nosig(peer_channel)) {
1239 switch_channel_hangup(peer_channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
1240 }
1241 status = SWITCH_STATUS_FALSE;
1242 }
1243
1244 if (switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) {
1245 switch_channel_state_t peer_state = switch_channel_get_state(peer_channel);
1246
1247 while (switch_channel_ready(peer_channel) && switch_channel_get_state(peer_channel) == peer_state) {
1248 switch_ivr_parse_all_messages(session);
1249 switch_channel_ready(caller_channel);
1250 switch_yield(20000);
1251 }
1252 }
1253
1254 if (caller_channel && !switch_channel_up_nosig(caller_channel)) {
1255 status = SWITCH_STATUS_FALSE;
1256 }
1257
1258 return status;
1259 }
1260
1261 SWITCH_DECLARE(void) switch_process_import(switch_core_session_t *session, switch_channel_t *peer_channel, const char *varname, const char *prefix)
1262 {
1263 const char *import, *val;
1264 switch_channel_t *caller_channel;
1265
1266 switch_assert(session && peer_channel);
1267 caller_channel = switch_core_session_get_channel(session);
1268
1269 if ((import = switch_channel_get_variable(caller_channel, varname))) {
1270 char *mydata = switch_core_session_strdup(session, import);
1271 int i, argc;
1272 char *argv[64] = { 0 };
1273
1274 if ((argc = switch_separate_string(mydata, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
1275 for (i = 0; i < argc; i++) {
1276 if ((val = switch_channel_get_variable(peer_channel, argv[i]))) {
1277 if (prefix) {
1278 char *var = switch_mprintf("%s%s", prefix, argv[i]);
1279 switch_channel_set_variable(caller_channel, var, val);
1280 free(var);
1281 } else {
1282 switch_channel_set_variable(caller_channel, argv[i], val);
1283 }
1284 }
1285 }
1286 }
1287 }
1288 }
1289
1290
1291 static switch_status_t setup_ringback(originate_global_t *oglobals, originate_status_t *originate_status, int len,
1292 const char *ringback_data, ringback_t *ringback, switch_frame_t *write_frame, switch_codec_t *write_codec)
1293 {
1294 switch_status_t status = SWITCH_STATUS_SUCCESS;
1295 switch_channel_t *caller_channel = switch_core_session_get_channel(oglobals->session);
1296 switch_codec_t *read_codec = NULL;
1297 char *tmp_data = NULL;
1298
1299 if (!switch_channel_test_flag(caller_channel, CF_ANSWERED)
1300 && !switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)) {
1301 if (oglobals->bridge_early_media > -1 && len == 1 && originate_status[0].peer_session &&
1302 switch_channel_media_ready(originate_status[0].peer_channel)) {
1303 inherit_codec(caller_channel, originate_status[0].peer_session);
1304 }
1305 if ((status = switch_channel_pre_answer(caller_channel)) != SWITCH_STATUS_SUCCESS) {
1306 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "%s Media Establishment Failed.\n",
1307 switch_channel_get_name(caller_channel));
1308 switch_goto_status(SWITCH_STATUS_BREAK, end);
1309 }
1310 }
1311
1312 if ((read_codec = switch_core_session_get_read_codec(oglobals->session))) {
1313 if (ringback_data && switch_is_file_path(ringback_data)) {
1314 if (!(strrchr(ringback_data, '.') || strstr(ringback_data, SWITCH_URL_SEPARATOR))) {
1315 ringback->asis++;
1316 }
1317 } else if (oglobals->bridge_early_media > -1 && zstr(ringback_data) && len == 1 && originate_status[0].peer_session) {
1318 switch_codec_implementation_t read_impl = { 0 }, write_impl = { 0 };
1319
1320 if (switch_channel_ready(originate_status[0].peer_channel)
1321 && switch_core_session_get_read_impl(originate_status[0].peer_session, &read_impl) == SWITCH_STATUS_SUCCESS
1322 && switch_core_session_get_write_impl(oglobals->session, &write_impl) == SWITCH_STATUS_SUCCESS) {
1323 if (read_impl.impl_id == write_impl.impl_id &&
1324 read_impl.microseconds_per_packet == write_impl.microseconds_per_packet &&
1325 read_impl.actual_samples_per_second == write_impl.actual_samples_per_second) {
1326 ringback->asis++;
1327 write_frame->codec = switch_core_session_get_write_codec(originate_status[0].peer_session);
1328 write_frame->datalen = write_frame->codec->implementation->decoded_bytes_per_packet;
1329 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "bridge_early_media: passthrough enabled\n");
1330 } else {
1331 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "bridge_early_media: codecs don't match (%s@%uh@%di / %s@%uh@%di)\n",
1332 read_impl.iananame, read_impl.actual_samples_per_second, read_impl.microseconds_per_packet / 1000,
1333 write_impl.iananame, write_impl.actual_samples_per_second, write_impl.microseconds_per_packet / 1000);
1334 }
1335 }
1336 }
1337
1338 if (!ringback->asis) {
1339 switch_codec_implementation_t peer_read_impl = { 0 };
1340
1341 if (switch_test_flag(read_codec, SWITCH_CODEC_FLAG_PASSTHROUGH)) {
1342 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_WARNING, "%s Ringback not supported in passthrough codec mode.\n",
1343 switch_channel_get_name(caller_channel));
1344 switch_goto_status(SWITCH_STATUS_GENERR, end);
1345 }
1346
1347 if (oglobals->bridge_early_media > -1 && zstr(ringback_data) && len == 1 && originate_status[0].peer_session) {
1348 switch_core_session_get_read_impl(originate_status[0].peer_session, &peer_read_impl);
1349 } else {
1350 switch_core_session_get_read_impl(oglobals->session, &peer_read_impl);
1351 }
1352
1353 if (switch_core_codec_init(write_codec,
1354 "L16",
1355 NULL,
1356 NULL,
1357 peer_read_impl.actual_samples_per_second,
1358 peer_read_impl.microseconds_per_packet / 1000,
1359 peer_read_impl.number_of_channels, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
1360 switch_core_session_get_pool(oglobals->session)) == SWITCH_STATUS_SUCCESS) {
1361
1362
1363 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->session), SWITCH_LOG_DEBUG,
1364 "Raw Codec Activation Success L16@%uhz %d channel %dms\n",
1365 peer_read_impl.actual_samples_per_second, peer_read_impl.number_of_channels, peer_read_impl.microseconds_per_packet / 1000);
1366 write_frame->codec = write_codec;
1367 write_frame->datalen = peer_read_impl.decoded_bytes_per_packet;
1368 write_frame->samples = write_frame->datalen / 2;
1369 memset(write_frame->data, 255, write_frame->datalen);
1370 switch_core_session_set_read_codec(oglobals->session, write_codec);
1371 } else {
1372 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_ERROR, "Codec Error!\n");
1373 switch_channel_hangup(caller_channel, SWITCH_CAUSE_BEARERCAPABILITY_NOTIMPL);
1374 read_codec = NULL;
1375 switch_goto_status(SWITCH_STATUS_BREAK, end);
1376 }
1377 }
1378
1379 oglobals->gen_ringback = 1;
1380
1381 if (zstr(ringback_data)) {
1382 switch_goto_status(SWITCH_STATUS_SUCCESS, end);
1383 }
1384
1385 if (switch_is_file_path(ringback_data)) {
1386 char *ext;
1387
1388 if (ringback->asis) {
1389 write_frame->codec = read_codec;
1390 ext = read_codec->implementation->iananame;
1391 tmp_data = switch_mprintf("%s.%s", ringback_data, ext);
1392 ringback_data = tmp_data;
1393 }
1394
1395 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->session), SWITCH_LOG_DEBUG, "Play Ringback File [%s]\n", ringback_data);
1396
1397 if (switch_test_flag((&ringback->fhb), SWITCH_FILE_OPEN)) {
1398 switch_core_file_close(&ringback->fhb);
1399 }
1400
1401
1402 ringback->fhb.channels = read_codec->implementation->number_of_channels;
1403 ringback->fhb.samplerate = read_codec->implementation->actual_samples_per_second;
1404 if (switch_core_file_open(&ringback->fhb,
1405 ringback_data,
1406 read_codec->implementation->number_of_channels,
1407 read_codec->implementation->actual_samples_per_second,
1408 SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
1409 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->session), SWITCH_LOG_ERROR, "Error Playing File\n");
1410 switch_safe_free(tmp_data);
1411 switch_goto_status(SWITCH_STATUS_GENERR, end);
1412 //switch_goto_status(SWITCH_STATUS_FALSE, end);
1413 }
1414 ringback->fh = &ringback->fhb;
1415
1416 } else if (!strncasecmp(ringback_data, "silence", 7)) {
1417 const char *c = ringback_data + 7;
1418 if (*c == ':') {
1419 c++;
1420 if (c) {
1421 ringback->silence = atoi(c);
1422 }
1423 }
1424 SWITCH_IVR_VERIFY_SILENCE_DIVISOR(ringback->silence);
1425 } else {
1426 if (ringback->audio_buffer) {
1427 switch_buffer_destroy(&ringback->audio_buffer);
1428 teletone_destroy_session(&ringback->ts);
1429 }
1430
1431 switch_buffer_create_dynamic(&ringback->audio_buffer, 512, 1024, 0);
1432 switch_buffer_set_loops(ringback->audio_buffer, -1);
1433
1434 teletone_init_session(&ringback->ts, 0, teletone_handler, ringback);
1435 ringback->ts.rate = read_codec->implementation->actual_samples_per_second;
1436 ringback->channels = read_codec->implementation->number_of_channels;
1437 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->session), SWITCH_LOG_DEBUG, "Play Ringback Tone [%s]\n", ringback_data);
1438 /* ringback->ts.debug = 1;
1439 ringback->ts.debug_stream = switch_core_get_console(); */
1440
1441 if (teletone_run(&ringback->ts, ringback_data)) {
1442 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->session), SWITCH_LOG_ERROR, "Error Playing Tone\n");
1443 teletone_destroy_session(&ringback->ts);
1444 switch_buffer_destroy(&ringback->audio_buffer);
1445 switch_goto_status(SWITCH_STATUS_GENERR, end);
1446 }
1447 }
1448 }
1449
1450 end:
1451
1452 switch_safe_free(tmp_data);
1453
1454 return status;
1455
1456 }
1457
1458
1459
1460
1461 typedef struct {
1462 switch_core_session_t *session;
1463 switch_core_session_t *bleg;
1464 switch_call_cause_t cause;
1465 switch_call_cause_t cancel_cause;
1466 const char *bridgeto;
1467 uint32_t timelimit_sec;
1468 const switch_state_handler_table_t *table;
1469 const char *cid_name_override;
1470 const char *cid_num_override;
1471 switch_caller_profile_t *caller_profile_override;
1472 switch_event_t *ovars;
1473 switch_originate_flag_t flags;
1474 switch_status_t status;
1475 int done;
1476 switch_thread_t *thread;
1477 switch_mutex_t *mutex;
1478 switch_mutex_t *fence_mutex;
1479 switch_dial_handle_t *dh;
1480 } enterprise_originate_handle_t;
1481
1482
1483 struct ent_originate_ringback {
1484 switch_core_session_t *session;
1485 int running;
1486 const char *ringback_data;
1487 switch_thread_t *thread;
1488 };
1489
1490 static void *SWITCH_THREAD_FUNC enterprise_originate_thread(switch_thread_t *thread, void *obj)
1491 {
1492 enterprise_originate_handle_t *handle = (enterprise_originate_handle_t *) obj;
1493 switch_status_t status;
1494
1495 switch_mutex_lock(handle->fence_mutex);
1496 handle->done = 0;
1497 switch_mutex_unlock(handle->fence_mutex);
1498
1499 status = switch_ivr_originate(NULL, &handle->bleg, &handle->cause,
1500 handle->bridgeto, handle->timelimit_sec,
1501 handle->table,
1502 handle->cid_name_override,
1503 handle->cid_num_override,
1504 handle->caller_profile_override,
1505 handle->ovars,
1506 handle->flags,
1507 &handle->cancel_cause,
1508 handle->dh);
1509
1510 switch_mutex_lock(handle->fence_mutex);
1511 handle->status = status;
1512 handle->done = 1;
1513 switch_mutex_unlock(handle->fence_mutex);
1514
1515 switch_mutex_lock(handle->mutex);
1516 switch_mutex_unlock(handle->mutex);
1517
1518 if (handle->done != 2) {
1519 if (handle->status == SWITCH_STATUS_SUCCESS && handle->bleg) {
1520 switch_channel_t *channel = switch_core_session_get_channel(handle->bleg);
1521
1522 switch_channel_set_variable(channel, "group_dial_status", "loser");
1523 switch_channel_hangup(channel, SWITCH_CAUSE_LOSE_RACE);
1524 switch_core_session_rwunlock(handle->bleg);
1525 }
1526 }
1527
1528 return NULL;
1529 }
1530
1531 static void *SWITCH_THREAD_FUNC enterprise_originate_ringback_thread(switch_thread_t *thread, void *obj)
1532 {
1533 struct ent_originate_ringback *rb_data = (struct ent_originate_ringback *) obj;
1534 switch_core_session_t *session = rb_data->session;
1535 switch_channel_t *channel;
1536 switch_status_t status = SWITCH_STATUS_FALSE;
1537
1538 if (switch_core_session_read_lock(session) != SWITCH_STATUS_SUCCESS) {
1539 return NULL;
1540 }
1541
1542 channel = switch_core_session_get_channel(session);
1543
1544 while (rb_data->running && switch_channel_ready(channel)) {
1545 switch_ivr_parse_all_messages(session);
1546 if (status != SWITCH_STATUS_BREAK) {
1547 if (zstr(rb_data->ringback_data) || !strcasecmp(rb_data->ringback_data, "silence")) {
1548 status = switch_ivr_collect_digits_callback(session, NULL, 0, 0);
1549 } else if (switch_is_file_path(rb_data->ringback_data)) {
1550 status = switch_ivr_play_file(session, NULL, rb_data->ringback_data, NULL);
1551 } else {
1552 status = switch_ivr_gentones(session, rb_data->ringback_data, 0, NULL);
1553 }
1554 }
1555
1556 if (status == SWITCH_STATUS_BREAK) {
1557 switch_channel_set_flag(channel, CF_NOT_READY);
1558 }
1559 }
1560 switch_core_session_rwunlock(session);
1561
1562 rb_data->running = 0;
1563 return NULL;
1564 }
1565
1566
1567 SWITCH_DECLARE(switch_status_t) switch_ivr_enterprise_originate(switch_core_session_t *session,
1568 switch_core_session_t **bleg,
1569 switch_call_cause_t *cause,
1570 const char *bridgeto,
1571 uint32_t timelimit_sec,
1572 const switch_state_handler_table_t *table,
1573 const char *cid_name_override,
1574 const char *cid_num_override,
1575 switch_caller_profile_t *caller_profile_override,
1576 switch_event_t *ovars, switch_originate_flag_t flags,
1577 switch_call_cause_t *cancel_cause,
1578 switch_dial_handle_list_t *hl)
1579 {
1580 int x_argc = 0;
1581 char *x_argv[MAX_PEERS] = { 0 };
1582 enterprise_originate_handle_t *hp = NULL, handles[MAX_PEERS] = { {0} };
1583 int i;
1584 switch_caller_profile_t *cp = NULL;
1585 switch_channel_t *channel = NULL;
1586 char *data = NULL;
1587 switch_status_t status = SWITCH_STATUS_FALSE;
1588 switch_threadattr_t *thd_attr = NULL;
1589 int running = 0, over = 0;
1590 switch_status_t tstatus = SWITCH_STATUS_FALSE;
1591 switch_memory_pool_t *pool;
1592 switch_event_header_t *hi = NULL;
1593 struct ent_originate_ringback rb_data = { 0 };
1594 const char *ringback_data = NULL;
1595 switch_event_t *var_event = NULL;
1596 int getcause = 1;
1597
1598 *cause = SWITCH_CAUSE_SUCCESS;
1599
1600 switch_core_new_memory_pool(&pool);
1601
1602 if (zstr(bridgeto) && (!hl || hl->handle_idx == 0)) {
1603 *cause = SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
1604 getcause = 0;
1605 switch_goto_status(SWITCH_STATUS_FALSE, end);
1606 }
1607
1608 if (!hl) {
1609 data = switch_core_strdup(pool, bridgeto);
1610 }
1611
1612 if (session) {
1613 switch_caller_profile_t *cpp = NULL;
1614 channel = switch_core_session_get_channel(session);
1615 if ((cpp = switch_channel_get_caller_profile(channel))) {
1616 cp = switch_caller_profile_dup(pool, cpp);
1617 }
1618 }
1619
1620 if (ovars) {
1621 var_event = ovars;
1622 } else {
1623 if (switch_event_create_plain(&var_event, SWITCH_EVENT_CHANNEL_DATA) != SWITCH_STATUS_SUCCESS) {
1624 abort();
1625 }
1626 }
1627
1628 if (session) {
1629 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "ent_originate_aleg_uuid", switch_core_session_get_uuid(session));
1630 }
1631
1632 if (channel) {
1633 const char *tmp_var = NULL;
1634
1635 switch_channel_process_export(channel, NULL, var_event, SWITCH_EXPORT_VARS_VARIABLE);
1636
1637 if ((tmp_var = switch_channel_get_variable(channel, "effective_ani"))) {
1638 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_ani", tmp_var);
1639 }
1640
1641 if ((tmp_var = switch_channel_get_variable(channel, "effective_aniii"))) {
1642 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_aniii", tmp_var);
1643 }
1644
1645 if ((tmp_var = switch_channel_get_variable(channel, "effective_caller_id_name"))) {
1646 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_caller_id_name", tmp_var);
1647 }
1648
1649 if ((tmp_var = switch_channel_get_variable(channel, "effective_caller_id_number"))) {
1650 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_caller_id_number", tmp_var);
1651 }
1652 }
1653
1654 /* strip leading spaces */
1655 while (data && *data && *data == ' ') {
1656 data++;
1657 }
1658
1659 /* extract channel variables, allowing multiple sets of braces */
1660 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Parsing ultra-global variables\n");
1661 while (data && *data == '<') {
1662 char *parsed = NULL;
1663
1664 if (switch_event_create_brackets(data, '<', '>', ',', &var_event, &parsed, SWITCH_FALSE) != SWITCH_STATUS_SUCCESS || !parsed) {
1665 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Parse Error!\n");
1666 switch_goto_status(SWITCH_STATUS_GENERR, done);
1667 }
1668
1669 data = parsed;
1670 }
1671 if (hl && hl->global_vars) {
1672 switch_event_merge(var_event, hl->global_vars);
1673 }
1674
1675 /* strip leading spaces (again) */
1676 while (data && *data && *data == ' ') {
1677 data++;
1678 }
1679
1680 if (ovars && ovars != var_event) {
1681 for (hi = ovars->headers; hi; hi = hi->next) {
1682 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, hi->name, hi->value);
1683 }
1684 }
1685
1686 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "ignore_early_media", "true");
1687
1688 if (data) {
1689 if (!(x_argc = switch_separate_string_string(data, SWITCH_ENT_ORIGINATE_DELIM, x_argv, MAX_PEERS))) {
1690 *cause = SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
1691 getcause = 0;
1692 switch_goto_status(SWITCH_STATUS_FALSE, end);
1693 }
1694 } else if (hl) {
1695 x_argc = hl->handle_idx;
1696 }
1697
1698 switch_threadattr_create(&thd_attr, pool);
1699 switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
1700
1701 for (i = 0; i < x_argc; i++) {
1702 handles[i].session = session;
1703 handles[i].bleg = NULL;
1704 handles[i].cause = 0;
1705 handles[i].cancel_cause = 0;
1706 handles[i].bridgeto = x_argv[i];
1707 handles[i].timelimit_sec = timelimit_sec;
1708 handles[i].table = table;
1709 handles[i].cid_name_override = cid_name_override;
1710 handles[i].cid_num_override = cid_num_override;
1711 handles[i].caller_profile_override = cp;
1712 switch_event_dup(&handles[i].ovars, var_event);
1713 handles[i].flags = flags;
1714 if (hl) {
1715 switch_dial_handle_dup(&handles[i].dh, hl->handles[i]);
1716 }
1717 switch_mutex_init(&handles[i].mutex, SWITCH_MUTEX_NESTED, pool);
1718 switch_mutex_init(&handles[i].fence_mutex, SWITCH_MUTEX_NESTED, pool);
1719 switch_mutex_lock(handles[i].mutex);
1720 switch_thread_create(&handles[i].thread, thd_attr, enterprise_originate_thread, &handles[i], pool);
1721 }
1722
1723 if (channel && !switch_channel_test_flag(channel, CF_PROXY_MODE) && !switch_channel_test_flag(channel, CF_PROXY_MEDIA)) {
1724 if (switch_channel_test_flag(channel, CF_ANSWERED)) {
1725 ringback_data = switch_channel_get_variable(channel, "transfer_ringback");
1726 }
1727
1728 if (!ringback_data) {
1729 ringback_data = switch_channel_get_variable(channel, "ringback");
1730 }
1731
1732 if (ringback_data || switch_channel_media_ready(channel)) {
1733 rb_data.ringback_data = ringback_data;
1734 rb_data.session = session;
1735 rb_data.running = 1;
1736 if (!switch_channel_media_ready(channel)) {
1737 if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
1738 goto done;
1739 }
1740 }
1741 switch_thread_create(&rb_data.thread, thd_attr, enterprise_originate_ringback_thread, &rb_data, pool);
1742 }
1743 }
1744
1745
1746 for (;;) {
1747 running = 0;
1748 over = 0;
1749
1750 if (channel && !switch_channel_ready(channel)) {
1751 break;
1752 }
1753
1754 if (cancel_cause && *cancel_cause > 0) {
1755 break;
1756 }
1757
1758 for (i = 0; i < x_argc; i++) {
1759
1760 switch_mutex_lock(handles[i].fence_mutex);
1761 if (handles[i].done == 0) {
1762 running++;
1763 } else if (handles[i].done == 1) {
1764 if (handles[i].status == SWITCH_STATUS_SUCCESS) {
1765 handles[i].done = 2;
1766 hp = &handles[i];
1767 switch_mutex_unlock(handles[i].fence_mutex);
1768 goto done;
1769 } else {
1770 handles[i].done = -1;
1771 }
1772 } else {
1773 over++;
1774 }
1775
1776 switch_mutex_unlock(handles[i].fence_mutex);
1777
1778 switch_yield(10000);
1779 }
1780
1781 if (!running || over == x_argc) {
1782 break;
1783 }
1784 }
1785
1786
1787 done:
1788
1789 if (hp) {
1790 *cause = hp->cause;
1791 getcause = 0;
1792 status = hp->status;
1793 *bleg = hp->bleg;
1794 if (*bleg) {
1795 switch_channel_t *bchan = switch_core_session_get_channel(*bleg);
1796 switch_caller_profile_t *cloned_profile;
1797
1798 if (session) {
1799 cloned_profile = switch_caller_profile_clone(*bleg, cp);
1800 switch_channel_set_originator_caller_profile(bchan, cloned_profile);
1801
1802 cloned_profile = switch_caller_profile_clone(session, switch_channel_get_caller_profile(bchan));
1803 switch_channel_set_originatee_caller_profile(channel, cloned_profile);
1804 }
1805 }
1806 switch_mutex_unlock(hp->mutex);
1807 switch_thread_join(&tstatus, hp->thread);
1808 switch_event_destroy(&hp->ovars);
1809 switch_dial_handle_destroy(&hp->dh);
1810 }
1811
1812 for (i = 0; i < x_argc; i++) {
1813 if (hp == &handles[i]) {
1814 continue;
1815 }
1816
1817 if (cancel_cause && *cancel_cause > 0) {
1818 handles[i].cancel_cause = *cancel_cause;
1819 } else {
1820 /* Was this call taken by another destination? */
1821 if (hp != NULL && hp->cause == SWITCH_CAUSE_SUCCESS) {
1822 /* Yes, the race was lost */
1823 handles[i].cancel_cause = SWITCH_CAUSE_LOSE_RACE;
1824 } else {
1825 /* No, something else happened, probably Originator Cancel */
1826 handles[i].cancel_cause = SWITCH_CAUSE_ORIGINATOR_CANCEL;
1827 }
1828 }
1829 }
1830
1831 for (i = 0; i < x_argc; i++) {
1832
1833 if (hp == &handles[i]) {
1834 continue;
1835 }
1836
1837 if (getcause && channel && handles[i].cause && handles[i].cause != SWITCH_CAUSE_SUCCESS) {
1838 switch_channel_handle_cause(channel, handles[i].cause);
1839 }
1840
1841 switch_mutex_unlock(handles[i].mutex);
1842
1843 if (getcause && *cause != handles[i].cause && handles[i].cause != SWITCH_CAUSE_LOSE_RACE && handles[i].cause != SWITCH_CAUSE_NO_PICKUP) {
1844 *cause = handles[i].cause;
1845 getcause++;
1846 }
1847
1848 switch_thread_join(&tstatus, handles[i].thread);
1849 switch_event_destroy(&handles[i].ovars);
1850 switch_dial_handle_destroy(&handles[i].dh);
1851 }
1852
1853 if (channel && rb_data.thread) {
1854 switch_channel_set_flag(channel, CF_NOT_READY);
1855 switch_thread_join(&tstatus, rb_data.thread);
1856 switch_channel_clear_flag(channel, CF_NOT_READY);
1857 }
1858
1859
1860 end:
1861
1862 if (getcause == 1 && *cause == SWITCH_CAUSE_SUCCESS) {
1863 *cause = SWITCH_CAUSE_NO_ANSWER;
1864 }
1865
1866 if (channel) {
1867 if (*cause == SWITCH_CAUSE_SUCCESS) {
1868 switch_channel_set_variable(channel, "originate_disposition", "success");
1869 } else {
1870 switch_channel_set_variable(channel, "originate_disposition", "failure");
1871 switch_channel_set_variable(channel, "hangup_cause", switch_channel_cause2str(*cause));
1872 }
1873 }
1874
1875
1876 if (var_event && var_event != ovars) {
1877 switch_event_destroy(&var_event);
1878 }
1879
1880 switch_core_destroy_memory_pool(&pool);
1881
1882 return status;
1883
1884 }
1885
1886 struct early_state {
1887 originate_global_t *oglobals;
1888 switch_mutex_t *mutex;
1889 switch_buffer_t *buffer;
1890 int ready;
1891 ringback_t *ringback;
1892 int ttl;
1893 };
1894 typedef struct early_state early_state_t;
1895
1896
1897 static void *SWITCH_THREAD_FUNC early_thread_run(switch_thread_t *thread, void *obj)
1898 {
1899 early_state_t *state = (early_state_t *) obj;
1900 originate_status_t originate_status[MAX_PEERS] = { {0} };
1901 uint8_t array_pos = 0;
1902 int16_t mux_data[SWITCH_RECOMMENDED_BUFFER_SIZE / 2] = { 0 };
1903 int32_t sample;
1904 switch_codec_t read_codecs[MAX_PEERS] = { {0} };
1905 int i, x, ready = 0, answered = 0, ring_ready = 0;
1906 int16_t *data;
1907 uint32_t datalen = 0;
1908 switch_status_t status;
1909 switch_frame_t *read_frame = NULL;
1910 switch_codec_implementation_t read_impl = { 0 };
1911
1912 for (i = 0; i < MAX_PEERS && i < state->ttl; i++) {
1913 switch_core_session_t *session = state->oglobals->originate_status[i].peer_session;
1914 switch_channel_t *channel = NULL;
1915
1916 if (session) channel = switch_core_session_get_channel(session);
1917
1918 if (!session || !channel || !switch_channel_up(channel)) {
1919 continue;
1920 }
1921
1922 if (switch_core_session_read_lock(session) == SWITCH_STATUS_SUCCESS) {
1923 originate_status[array_pos].peer_session = session;
1924 originate_status[array_pos].peer_channel = channel;
1925 originate_status[array_pos].array_pos = (uint8_t) i;
1926 array_pos++;
1927 }
1928 }
1929
1930 if (state->oglobals->session) {
1931 switch_core_session_get_read_impl(state->oglobals->session, &read_impl);
1932 }
1933
1934 while (state->ready) {
1935 datalen = 0;
1936 memset(mux_data, 0, sizeof(mux_data));
1937 ready = 0;
1938 answered = 0;
1939
1940 for (array_pos = 0; array_pos < MAX_PEERS && originate_status[array_pos].peer_session; array_pos++) {
1941 switch_core_session_t *session = originate_status[array_pos].peer_session;
1942 switch_channel_t *channel = originate_status[array_pos].peer_channel;
1943 i = originate_status[array_pos].array_pos;
1944
1945 if (!session || !channel || !switch_channel_up(channel)) {
1946 continue;
1947 }
1948
1949 if (switch_channel_media_ready(channel)) {
1950 ready++;
1951
1952 if (switch_channel_test_flag(channel, CF_RING_READY)) {
1953 ring_ready = 1;
1954 state->oglobals->bridge_early_media = -1;
1955 state->oglobals->ignore_early_media = 1;
1956 }
1957
1958 if (switch_channel_test_flag(channel, CF_ANSWERED)) {
1959 answered++;
1960 }
1961
1962 if (!state->ringback->asis) {
1963 if (!switch_core_codec_ready((&read_codecs[i]))) {
1964 if (switch_core_codec_init(&read_codecs[i],
1965 "L16",
1966 NULL,
1967 NULL,
1968 read_impl.actual_samples_per_second,
1969 read_impl.microseconds_per_packet / 1000,
1970 read_impl.number_of_channels, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
1971 switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
1972 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec Error!\n");
1973 }
1974 switch_core_session_set_read_codec(session, NULL);
1975 switch_core_session_set_read_codec(session, &read_codecs[i]);
1976 }
1977 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1978 if (SWITCH_READ_ACCEPTABLE(status) && !switch_test_flag(read_frame, SFF_CNG)) {
1979 data = (int16_t *) read_frame->data;
1980 if (datalen < read_frame->datalen) {
1981 datalen = read_frame->datalen;
1982 }
1983 for (x = 0; x < (int) read_frame->datalen / 2; x++) {
1984 sample = data[x] + mux_data[x];
1985 switch_normalize_to_16bit(sample);
1986 mux_data[x] = (int16_t) sample;
1987 }
1988 }
1989 } else {
1990 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1991 if (SWITCH_READ_ACCEPTABLE(status) && !switch_test_flag(read_frame, SFF_CNG)) {
1992 datalen = read_frame->datalen;
1993 }
1994 break;
1995 }
1996 }
1997 }
1998
1999 if (!ready || answered || ring_ready) {
2000 break;
2001 }
2002
2003 if (!datalen) {
2004 continue;
2005 }
2006
2007 if (state->ringback->asis) {
2008 uint16_t flen = (uint16_t)datalen;
2009 switch_mutex_lock(state->mutex);
2010 switch_buffer_write(state->buffer, &flen, sizeof(uint16_t));
2011 switch_buffer_write(state->buffer, read_frame->data, datalen);
2012 switch_mutex_unlock(state->mutex);
2013 } else {
2014 switch_mutex_lock(state->mutex);
2015 switch_buffer_write(state->buffer, mux_data, datalen);
2016 switch_mutex_unlock(state->mutex);
2017 }
2018 }
2019
2020
2021 for (array_pos = 0; array_pos < MAX_PEERS && originate_status[array_pos].peer_session; array_pos++) {
2022 switch_core_session_t *session = originate_status[array_pos].peer_session;
2023 switch_channel_t *channel = originate_status[array_pos].peer_channel;
2024 i = originate_status[array_pos].array_pos;
2025
2026 if (!session) continue;
2027
2028 if (switch_core_codec_ready((&read_codecs[i]))) {
2029 switch_core_session_set_read_codec(session, NULL);
2030 switch_core_codec_destroy(&read_codecs[i]);
2031 }
2032
2033 if (switch_channel_up_nosig(channel)) {
2034 switch_core_session_reset(session, SWITCH_FALSE, SWITCH_TRUE);
2035 }
2036
2037 switch_core_session_rwunlock(session);
2038 }
2039
2040 if (!ring_ready) {
2041 state->oglobals->early_ok = 1;
2042 }
2043
2044 return NULL;
2045 }
2046
2047 #define peer_eligible(_peer) (_peer && !(switch_channel_test_flag(_peer, CF_TRANSFER) || \
2048 switch_channel_test_flag(_peer, CF_REDIRECT) || \
2049 switch_channel_test_flag(_peer, CF_BRIDGED) || \
2050 switch_channel_get_state(_peer) == CS_RESET || \
2051 !switch_channel_test_flag(_peer, CF_ORIGINATING)))
2052
2053 static void wait_for_cause(switch_channel_t *channel)
2054 {
2055 int sanity = 5;
2056
2057 while (--sanity > 0 && peer_eligible(channel) && switch_channel_get_cause(channel) == SWITCH_CAUSE_NONE) {
2058 switch_yield(10000);
2059 }
2060 }
2061
2062
2063
2064 SWITCH_DECLARE(switch_status_t) switch_ivr_originate(switch_core_session_t *session,
2065 switch_core_session_t **bleg,
2066 switch_call_cause_t *cause,
2067 const char *bridgeto,
2068 uint32_t timelimit_sec,
2069 const switch_state_handler_table_t *table,
2070 const char *cid_name_override,
2071 const char *cid_num_override,
2072 switch_caller_profile_t *caller_profile_override,
2073 switch_event_t *ovars, switch_originate_flag_t flags,
2074 switch_call_cause_t *cancel_cause,
2075 switch_dial_handle_t *dh)
2076 {
2077 //originate_status_t originate_status[MAX_PEERS] = { {0} };
2078 switch_originate_flag_t dftflags = SOF_NONE, myflags;
2079 char *pipe_names[MAX_PEERS] = { 0 };
2080 char *data = NULL;
2081 switch_status_t status = SWITCH_STATUS_SUCCESS;
2082 switch_channel_t *caller_channel = NULL;
2083 char *peer_names[MAX_PEERS] = { 0 };
2084 switch_event_t *peer_vars[MAX_PEERS] = { 0 };
2085 switch_core_session_t *new_session = NULL, *peer_session = NULL;
2086 switch_caller_profile_t *new_profile = NULL, *caller_caller_profile;
2087 char *chan_type = NULL, *chan_data;
2088 switch_channel_t *peer_channel = NULL;
2089 ringback_t ringback = { 0 };
2090 time_t start, global_start;
2091 switch_time_t last_retry_start = 0;
2092 switch_frame_t *read_frame = NULL;
2093 int r = 0, i, and_argc = 0, or_argc = 0;
2094 int32_t sleep_ms = 1000, try = 0, retries = 1, retry_timelimit_sec = 0;
2095 int32_t min_retry_period_ms = sleep_ms;
2096 switch_codec_t write_codec = { 0 };
2097 switch_frame_t write_frame = { 0 };
2098 char *odata, *var;
2099 switch_call_cause_t reason = SWITCH_CAUSE_NONE;
2100 switch_call_cause_t force_reason = SWITCH_CAUSE_NONE;
2101 uint8_t to = 0;
2102 char *var_val;
2103 const char *ringback_data = NULL;
2104 switch_event_t *var_event = NULL;
2105 int8_t fail_on_single_reject = 0;
2106 int8_t hangup_on_single_reject = 0;
2107 char *fail_on_single_reject_var = NULL;
2108 char *loop_data = NULL;
2109 uint32_t progress_timelimit_sec = 0;
2110 const char *cid_tmp, *lc;
2111 originate_global_t oglobals = { 0 };
2112 int cdr_total = 0;
2113 int local_clobber = 0;
2114 const char *cancel_key = NULL;
2115 const char *holding = NULL;
2116 const char *soft_holding = NULL;
2117 early_state_t early_state = { 0 };
2118 int read_packet = 0;
2119 int check_reject = 1;
2120 switch_codec_implementation_t read_impl = { 0 };
2121 const char *ani_override = NULL;
2122 const char *aniii_override = NULL;
2123 const char *ent_aleg_uuid = NULL;
2124 switch_core_session_t *a_session = session, *l_session = NULL;
2125 char *event_string;
2126
2127 if (!bridgeto || dh) {
2128 bridgeto = "";
2129 }
2130
2131 if (session) {
2132 caller_channel = switch_core_session_get_channel(session);
2133
2134 if (switch_false(switch_channel_get_variable(caller_channel, "preserve_originated_vars"))) {
2135 switch_channel_set_variable(caller_channel, "originated_legs", NULL);
2136 switch_channel_set_variable(caller_channel, "originate_causes", NULL);
2137 }
2138 }
2139
2140
2141 if (strstr(bridgeto, SWITCH_ENT_ORIGINATE_DELIM)) {
2142 return switch_ivr_enterprise_originate(session, bleg, cause, bridgeto, timelimit_sec, table, cid_name_override, cid_num_override,
2143 caller_profile_override, ovars, flags, cancel_cause, NULL);
2144 }
2145
2146 oglobals.check_vars = SWITCH_TRUE;
2147 oglobals.ringback_ok = 1;
2148 oglobals.bridge_early_media = -1;
2149 oglobals.file = NULL;
2150 oglobals.error_file = NULL;
2151 switch_core_new_memory_pool(&oglobals.pool);
2152
2153 if (caller_profile_override) {
2154 oglobals.caller_profile_override = switch_caller_profile_dup(oglobals.pool, caller_profile_override);
2155 } else if (session) {
2156 switch_caller_profile_t *cp = switch_channel_get_caller_profile(caller_channel);
2157
2158 if (cp) {
2159 oglobals.caller_profile_override = switch_caller_profile_dup(oglobals.pool, cp);
2160 }
2161 }
2162
2163 if (session) {
2164 const char *to_var, *bypass_media = NULL, *proxy_media = NULL;
2165 switch_channel_set_flag(caller_channel, CF_ORIGINATOR);
2166 oglobals.session = session;
2167
2168 switch_channel_execute_on(caller_channel, SWITCH_CHANNEL_EXECUTE_ON_PRE_ORIGINATE_VARIABLE);
2169 switch_channel_api_on(caller_channel, SWITCH_CHANNEL_API_ON_PRE_ORIGINATE_VARIABLE);
2170
2171 switch_core_session_get_read_impl(session, &read_impl);
2172
2173 if ((to_var = switch_channel_get_variable(caller_channel, SWITCH_CALL_TIMEOUT_VARIABLE))) {
2174 timelimit_sec = atoi(to_var);
2175 }
2176
2177 proxy_media = switch_channel_get_variable(caller_channel, SWITCH_PROXY_MEDIA_VARIABLE);
2178 bypass_media = switch_channel_get_variable(caller_channel, SWITCH_BYPASS_MEDIA_VARIABLE);
2179
2180 if (!zstr(proxy_media)) {
2181 if (switch_true(proxy_media)) {
2182 switch_channel_set_flag(caller_channel, CF_PROXY_MEDIA);
2183 } else if (switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA)) {
2184 switch_channel_clear_flag(caller_channel, CF_PROXY_MEDIA);
2185 }
2186 }
2187
2188 if (bypass_media && switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA) && !switch_channel_test_flag(caller_channel, CF_ANSWERED)) {
2189 switch_core_session_message_t msg = { 0 };
2190
2191 msg.message_id = SWITCH_MESSAGE_INDICATE_CLEAR_PROGRESS;
2192 msg.from = __FILE__;
2193 switch_core_session_receive_message(session, &msg);
2194 }
2195
2196
2197 if (!zstr(bypass_media) && !switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA)) {
2198 if (switch_true(bypass_media)) {
2199 switch_channel_set_flag(caller_channel, CF_PROXY_MODE);
2200 } else if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE)) {
2201 if (!switch_channel_test_flag(caller_channel, CF_ANSWERED) && switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)) {
2202 switch_channel_set_variable(caller_channel, SWITCH_B_SDP_VARIABLE, NULL);
2203 switch_channel_answer(caller_channel);
2204 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
2205 "Must answer channel %s due to SIP PARADOX\n", switch_channel_get_name(caller_channel));
2206 }
2207 switch_channel_set_variable(caller_channel, SWITCH_B_SDP_VARIABLE, NULL);
2208 switch_ivr_media(switch_core_session_get_uuid(session), SMF_NONE);
2209 }
2210 }
2211
2212 if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE) && switch_channel_test_flag(caller_channel, CF_ANSWERED)) {
2213 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
2214 "Channel is already up, delaying proxy mode 'till both legs are answered.\n");
2215 switch_channel_set_variable(caller_channel, "bypass_media_after_bridge", "true");
2216 switch_channel_set_variable(caller_channel, SWITCH_BYPASS_MEDIA_VARIABLE, NULL);
2217 switch_channel_clear_flag(caller_channel, CF_PROXY_MODE);
2218 }
2219 }
2220
2221 if (timelimit_sec <= 0) {
2222 timelimit_sec = SWITCH_DEFAULT_TIMEOUT;
2223 }
2224
2225
2226 oglobals.idx = IDX_NADA;
2227 oglobals.early_ok = 1;
2228
2229 *bleg = NULL;
2230
2231 switch_zmalloc(write_frame.data, SWITCH_RECOMMENDED_BUFFER_SIZE);
2232 write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
2233
2234 odata = strdup(bridgeto);
2235
2236 if (!odata) {
2237 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Memory Error!\n");
2238 status = SWITCH_STATUS_MEMERR;
2239 goto done;
2240 }
2241
2242 data = odata;
2243
2244
2245 /* Some channel are created from an originating channel and some aren't so not all outgoing calls have a way to get params
2246 so we will normalize dialstring params and channel variables (when there is an originator) into an event that we
2247 will use as a pseudo hash to consult for params as needed.
2248 */
2249
2250 if (ovars) {
2251 var_event = ovars;
2252 } else {
2253 if (switch_event_create_plain(&var_event, SWITCH_EVENT_CHANNEL_DATA) != SWITCH_STATUS_SUCCESS) {
2254 abort();
2255 }
2256 }
2257
2258 ent_aleg_uuid = switch_event_get_header(var_event, "ent_originate_aleg_uuid");
2259
2260 if (caller_channel) {
2261 switch_channel_process_export(caller_channel, NULL, var_event, SWITCH_EXPORT_VARS_VARIABLE);
2262 }
2263
2264 /* strip leading spaces */
2265 while (data && *data && *data == ' ') {
2266 data++;
2267 }
2268
2269 if ((ovars && switch_true(switch_event_get_header(ovars,"origination_nested_vars"))) ||
2270 (caller_channel && switch_true(switch_channel_get_variable(caller_channel, "origination_nested_vars")))
2271 || switch_true(switch_core_get_variable("origination_nested_vars")) || switch_stristr("origination_nested_vars=true", data)) {
2272 oglobals.check_vars = SWITCH_FALSE;
2273 }
2274
2275 if (dh) {
2276 switch_event_t *vp = switch_dial_handle_get_global_vars(dh);
2277 if (vp) {
2278 if (var_event && var_event != ovars) {
2279 switch_event_destroy(&var_event);
2280 }
2281 switch_event_dup(&var_event, vp);
2282 }
2283 }
2284
2285 /* extract channel variables, allowing multiple sets of braces */
2286 if (*data == '<') {
2287 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Parsing ultra-global variables\n");
2288 while (*data == '<') {
2289 char *parsed = NULL;
2290
2291 if (switch_event_create_brackets(data, '<', '>', ',', &var_event, &parsed, SWITCH_FALSE) != SWITCH_STATUS_SUCCESS || !parsed) {
2292 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Parse Error!\n");
2293 switch_goto_status(SWITCH_STATUS_GENERR, done);
2294 }
2295
2296 data = parsed;
2297 }
2298 }
2299
2300 /* extract channel variables, allowing multiple sets of braces */
2301 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Parsing global variables\n");
2302 while (*data == '{') {
2303 char *parsed = NULL;
2304
2305 if (switch_event_create_brackets(data, '{', '}', ',', &var_event, &parsed, SWITCH_FALSE) != SWITCH_STATUS_SUCCESS || !parsed) {
2306 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Parse Error!\n");
2307 switch_goto_status(SWITCH_STATUS_GENERR, done);
2308 }
2309
2310 data = parsed;
2311 }
2312
2313 if (dh && var_event && switch_event_serialize(var_event, &event_string, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
2314 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Global Vars\n======================\n%s\n", event_string);
2315 switch_safe_free(event_string);
2316 }
2317
2318 /* strip leading spaces (again) */
2319 while (data && *data && *data == ' ') {
2320 data++;
2321 }
2322
2323 if (zstr(data) && !dh) {
2324 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "No origination URL specified!\n");
2325 status = SWITCH_STATUS_GENERR;
2326 goto done;
2327 }
2328
2329 if (!(flags & SOF_NOBLOCK) && var_event && (var = switch_event_get_header(var_event, "originate_delay_start"))) {
2330 int tmp = atoi(var);
2331 if (tmp > 0) {
2332 while (tmp && (!cancel_cause || *cancel_cause == 0)) {
2333 switch_cond_next();
2334 tmp--;
2335 }
2336 }
2337 }
2338
2339 if (oglobals.session) {
2340 switch_event_header_t *hi;
2341 const char *cdr_total_var;
2342 const char *cdr_var;
2343 const char *json_cdr_var;
2344
2345 if (switch_channel_var_true(caller_channel, "originate_xfer_zombie")) {
2346 switch_channel_set_flag(caller_channel, CF_XFER_ZOMBIE);
2347 oglobals.early_ok = 0;
2348 oglobals.ignore_early_media = 1;
2349 }
2350
2351 if ((cdr_var = switch_channel_get_variable(caller_channel, "failed_xml_cdr_prefix"))) {
2352 char buf[128] = "";
2353 switch_snprintf(buf, sizeof(buf), "%s_total", cdr_var);
2354 if ((cdr_total_var = switch_channel_get_variable(caller_channel, buf))) {
2355 int tmp = atoi(cdr_total_var);
2356 if (tmp > 0) {
2357 cdr_total = tmp;
2358 }
2359 }
2360 }
2361
2362 if ((json_cdr_var = switch_channel_get_variable(caller_channel, "failed_json_cdr_prefix"))) {
2363 char buf[128] = "";
2364 switch_snprintf(buf, sizeof(buf), "%s_total", json_cdr_var);
2365 if ((cdr_total_var = switch_channel_get_variable(caller_channel, buf))) {
2366 int tmp = atoi(cdr_total_var);
2367 if (tmp > 0) {
2368 cdr_total = tmp;
2369 }
2370 }
2371 }
2372
2373 /* Copy all the missing applicable channel variables from A-leg into the event */
2374 if ((hi = switch_channel_variable_first(caller_channel))) {
2375 for (; hi; hi = hi->next) {
2376 int ok = 0;
2377 if (!strcasecmp((char *) hi->name, "group_confirm_key")) {
2378 ok = 1;
2379 } else if (!strcasecmp((char *) hi->name, "group_confirm_file")) {
2380 ok = 1;
2381 } else if (!strcasecmp((char *) hi->name, "group_confirm_read_timeout")) {
2382 ok = 1;
2383 } else if (!strcasecmp((char *) hi->name, "group_confirm_cancel_timeout")) {
2384 ok = 1;
2385 } else if (!strcasecmp((char *) hi->name, "group_confirm_timeout")) {
2386 ok = 1;
2387 } else if (!strcasecmp((char *) hi->name, "forked_dial")) {
2388 ok = 1;
2389 } else if (!strcasecmp((char *) hi->name, "fail_on_single_reject")) {
2390 ok = 1;
2391 } else if (!strcasecmp((char *) hi->name, "hangup_on_single_reject")) {
2392 ok = 1;
2393 } else if (!strcasecmp((char *) hi->name, "ignore_early_media")) {
2394 ok = 1;
2395 } else if (!strcasecmp((char *) hi->name, "bridge_early_media")) {
2396 ok = 1;
2397 } else if (!strcasecmp((char *) hi->name, "originate_continue_on_timeout")) {
2398 ok = 1;
2399 } else if (!strcasecmp((char *) hi->name, "ignore_ring_ready")) {
2400 ok = 1;
2401 } else if (!strcasecmp((char *) hi->name, "monitor_early_media_ring")) {
2402 ok = 1;
2403 } else if (!strcasecmp((char *) hi->name, "monitor_early_media_ring_total")) {
2404 ok = 1;
2405 } else if (!strcasecmp((char *) hi->name, "monitor_early_media_fail")) {
2406 ok = 1;
2407 } else if (!strcasecmp((char *) hi->name, "return_ring_ready")) {
2408 ok = 1;
2409 } else if (!strcasecmp((char *) hi->name, "ring_ready")) {
2410 ok = 1;
2411 } else if (!strcasecmp((char *) hi->name, "instant_ringback")) {
2412 ok = 1;
2413 } else if (!strcasecmp((char *) hi->name, "progress_timeout")) {
2414 ok = 1;
2415 } else if (!strcasecmp((char *) hi->name, "language")) {
2416 ok = 1;
2417 }
2418
2419 if (ok && !switch_event_get_header(var_event, hi->name)) {
2420 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, (char *) hi->name, (char *) hi->value);
2421 }
2422 }
2423 switch_channel_variable_last(caller_channel);
2424 }
2425 /*
2426 if ((hi = switch_channel_variable_first(caller_channel))) {
2427 for (; hi; hi = switch_core_hash_next(&hi)) {
2428 switch_core_hash_this(hi, &vvar, NULL, &vval);
2429 if (vvar && vval) {
2430 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, (void *) vvar, (char *) vval);
2431 }
2432 }
2433 switch_channel_variable_last(caller_channel);
2434 }
2435 */
2436 }
2437
2438 if (caller_channel) { /* ringback is only useful when there is an originator */
2439 ringback_data = NULL;
2440 cancel_key = switch_channel_get_variable(caller_channel, "origination_cancel_key");
2441
2442 if (switch_channel_test_flag(caller_channel, CF_ANSWERED)) {
2443 ringback_data = switch_channel_get_variable(caller_channel, "transfer_ringback");
2444 }
2445
2446 if (!ringback_data) {
2447 ringback_data = switch_channel_get_variable(caller_channel, "ringback");
2448 }
2449
2450 switch_channel_set_variable(caller_channel, "originate_disposition", "failure");
2451 switch_channel_set_variable(caller_channel, "DIALSTATUS", "INVALIDARGS");
2452
2453 if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE) || switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA)) {
2454 ringback_data = NULL;
2455 }
2456 }
2457 #if 0
2458 /* changing behaviour ignore_early_media=true must also be explicitly set for previous behaviour */
2459 if (ringback_data) {
2460 oglobals.early_ok = 0;
2461 }
2462 #endif
2463
2464 if ((var = switch_event_get_header(var_event, "group_confirm_timeout"))) {
2465 // has precedence over group_confirm_cancel_timeout
2466 if (switch_is_number(var)) {
2467 oglobals.confirm_timeout = atoi(var);
2468 if (oglobals.confirm_timeout == 0) {
2469 oglobals.cancel_timeout = SWITCH_TRUE;
2470 }
2471 }
2472 } else if (switch_true(switch_event_get_header(var_event, "group_confirm_cancel_timeout"))) {
2473 oglobals.cancel_timeout = SWITCH_TRUE;
2474 }
2475
2476 if ((var = switch_event_get_header(var_event, "group_confirm_early_ok"))) {
2477 oglobals.early_ok = switch_true(var);
2478 }
2479
2480 if ((var = switch_event_get_header(var_event, "group_confirm_key"))) {
2481 switch_copy_string(oglobals.key, var, sizeof(oglobals.key));
2482 if ((var = switch_event_get_header(var_event, "group_confirm_file"))) {
2483 oglobals.file = strdup(var);
2484 }
2485 if ((var = switch_event_get_header(var_event, "group_confirm_error_file"))) {
2486 oglobals.error_file = strdup(var);
2487 }
2488 if ((var = switch_event_get_header(var_event, "group_confirm_read_timeout"))) {
2489 int tmp = atoi(var);
2490
2491 if (tmp >= 0) {
2492 oglobals.confirm_read_timeout = tmp;
2493 }
2494
2495 }
2496 }
2497 /* When using the AND operator, the fail_on_single_reject flag may be set in order to indicate that a single
2498 rejections should terminate the attempt rather than a timeout, answer, or rejection by all.
2499 If the value is set to 'true' any fail cause will end the attempt otherwise it can contain a comma (,) separated
2500 list of cause names which should be considered fatal
2501 */
2502 if ((var = switch_event_get_header(var_event, "hangup_on_single_reject"))) {
2503 hangup_on_single_reject = switch_true(var);
2504 }
2505
2506 if ((var = switch_event_get_header(var_event, "fail_on_single_reject")) || hangup_on_single_reject) {
2507 if (var) {
2508 fail_on_single_reject_var = strdup(var);
2509 }
2510
2511 if (switch_true(var)) {
2512 fail_on_single_reject = 1;
2513 } else {
2514 fail_on_single_reject = -1;
2515 }
2516 }
2517
2518 if ((!zstr(oglobals.file)) && (!strcmp(oglobals.file, "undef"))) {
2519 switch_safe_free(oglobals.file);
2520 oglobals.file = NULL;
2521 }
2522 if ((!zstr(oglobals.error_file)) && (!strcmp(oglobals.error_file, "undef"))) {
2523 switch_safe_free(oglobals.error_file);
2524 oglobals.error_file = NULL;
2525 }
2526
2527 if ((var_val = switch_event_get_header(var_event, "bridge_early_media"))) {
2528 if (switch_true(var_val)) {
2529 oglobals.early_ok = 0;
2530 oglobals.ignore_early_media = 3;
2531 }
2532 }
2533
2534 if ((var_val = switch_event_get_header(var_event, "ignore_early_media"))) {
2535 if (switch_true(var_val)) {
2536 oglobals.early_ok = 0;
2537 oglobals.ignore_early_media = 1;
2538 } else if (!strcmp(var_val, "consume")) {
2539 oglobals.early_ok = 0;
2540 oglobals.ignore_early_media = 4;
2541 } else if (!strcmp(var_val, "ring_ready")) {
2542 oglobals.early_ok = 0;
2543 oglobals.ignore_early_media = 2;
2544 }
2545 }
2546
2547 if ((var_val = switch_event_get_header(var_event, "originate_continue_on_timeout")) && switch_true(var_val)) {
2548 oglobals.continue_on_timeout = 1;
2549 }
2550
2551 if ((var_val = switch_event_get_header(var_event, "ignore_ring_ready")) && switch_true(var_val)) {
2552 oglobals.ignore_ring_ready = 1;
2553 }
2554
2555 if ((var_val = switch_event_get_header(var_event, "monitor_early_media_ring")) && switch_true(var_val)) {
2556 oglobals.early_ok = 0;
2557 oglobals.monitor_early_media_ring = 1;
2558 }
2559
2560 if ((var_val = switch_event_get_header(var_event, "monitor_early_media_fail")) && switch_true(var_val)) {
2561 oglobals.early_ok = 0;
2562 oglobals.monitor_early_media_fail = 1;
2563 }
2564
2565 if ((var_val = switch_event_get_header(var_event, "return_ring_ready")) && switch_true(var_val)) {
2566 oglobals.return_ring_ready = 1;
2567 }
2568
2569 if ((var_val = switch_event_get_header(var_event, "ring_ready")) && switch_true(var_val)) {
2570 oglobals.ring_ready = 1;
2571 }
2572
2573 if ((var_val = switch_event_get_header(var_event, "instant_ringback")) && switch_true(var_val)) {
2574 oglobals.instant_ringback = 1;
2575 }
2576
2577 if ((var_val = switch_event_get_header(var_event, "originate_timeout"))) {
2578 int tmp = atoi(var_val);
2579 if (tmp > 0) {
2580 timelimit_sec = (uint32_t) tmp;
2581 }
2582 }
2583
2584 if ((var_val = switch_event_get_header(var_event, "progress_timeout"))) {
2585 int tmp = atoi(var_val);
2586 if (tmp > 0) {
2587 progress_timelimit_sec = (uint32_t) tmp;
2588 }
2589 }
2590
2591 if ((var_val = switch_event_get_header(var_event, "originate_retry_timeout")) && switch_true(var_val)) {
2592 int32_t tmp;
2593 tmp = atoi(var_val);
2594 if (tmp > 0) {
2595 retry_timelimit_sec = tmp;
2596 } else {
2597 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
2598 "Invalid originate_retry_timeout setting of %s ignored, value must be > 0\n", var_val);
2599 }
2600 }
2601
2602 if ((var_val = switch_event_get_header(var_event, "originate_retries")) && switch_true(var_val)) {
2603 int32_t tmp;
2604 tmp = atoi(var_val);
2605 /* allow large number of retries if timeout is set */
2606 if (tmp > 0 && (retry_timelimit_sec > 0 || tmp < 101)) {
2607 retries = tmp;
2608 } else {
2609 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
2610 "Invalid originate_retries setting of %d ignored, value must be between 1 and 100\n", tmp);
2611 }
2612 }
2613
2614 if ((var_val = switch_event_get_header(var_event, "originate_retry_sleep_ms")) && switch_true(var_val)) {
2615 int32_t tmp;
2616 tmp = atoi(var_val);
2617 if (tmp >= 500 && tmp <= 60000) {
2618 sleep_ms = tmp;
2619 } else {
2620 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
2621 "Invalid originate_retry_sleep_ms setting of %d ignored, value must be between 500 and 60000\n", tmp);
2622 }
2623 }
2624
2625 if ((var_val = switch_event_get_header(var_event, "originate_retry_min_period_ms")) && switch_true(var_val)) {
2626 int32_t tmp;
2627 tmp = atoi(var_val);
2628 if (tmp >= 500 && tmp <= 300000) {
2629 min_retry_period_ms = tmp;
2630 } else {
2631 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
2632 "Invalid originate_retry_min_period_ms setting of %d ignored, value must be between 500 and 300000\n", tmp);
2633 }
2634 }
2635
2636 /* variable to force ANI / ANIII */
2637 ani_override = switch_event_get_header(var_event, "origination_ani");
2638 aniii_override = switch_event_get_header(var_event, "origination_aniii");
2639
2640 if ((cid_tmp = switch_event_get_header(var_event, "origination_caller_id_name"))) {
2641 cid_name_override = cid_tmp;
2642 }
2643
2644 if (cid_name_override) {
2645 if (!cid_tmp) {
2646 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_caller_id_name", cid_name_override);
2647 }
2648 } else {
2649 cid_name_override = switch_event_get_header(var_event, "origination_caller_id_name");
2650 }
2651
2652 if ((cid_tmp = switch_event_get_header(var_event, "origination_caller_id_number"))) {
2653 cid_num_override = cid_tmp;
2654 }
2655
2656 if (cid_num_override) {
2657 if (!cid_tmp) {
2658 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_caller_id_number", cid_num_override);
2659 }
2660 } else {
2661 cid_num_override = switch_event_get_header(var_event, "origination_caller_id_number");
2662 }
2663
2664 if (flags & SOF_NO_LIMITS) {
2665 dftflags |= SOF_NO_LIMITS;
2666 }
2667
2668 if (ani_override) {
2669 dftflags |= SOF_NO_EFFECTIVE_ANI;
2670 }
2671
2672 if (aniii_override) {
2673 dftflags |= SOF_NO_EFFECTIVE_ANIII;
2674 }
2675
2676 if (cid_num_override) {
2677 dftflags |= SOF_NO_EFFECTIVE_CID_NUM;
2678 }
2679
2680 if (cid_name_override) {
2681 dftflags |= SOF_NO_EFFECTIVE_CID_NAME;
2682 }
2683
2684 if (!progress_timelimit_sec) {
2685 progress_timelimit_sec = timelimit_sec;
2686 }
2687
2688 switch_epoch_time_now(&global_start);
2689 last_retry_start = switch_micro_time_now();
2690
2691 for (try = 0; try < retries; try++) {
2692
2693 if (try > 0) {
2694 int64_t elapsed = switch_epoch_time_now(NULL) - global_start;
2695
2696 /* check if retry time limit has been exceeded */
2697 if (retry_timelimit_sec > 0) {
2698 if (elapsed > retry_timelimit_sec) {
2699 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE,
2700 "Elapsed = %"SWITCH_INT64_T_FMT", originate retry timeout.\n", elapsed);
2701 break;
2702 } else if (cancel_cause && *cancel_cause != 0) {
2703 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Originate cancelled\n");
2704 break;
2705 } else {
2706 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
2707 "Elapsed = %"SWITCH_INT64_T_FMT", originate retry not timed out yet\n", elapsed);
2708 }
2709 }
2710
2711 /* don't allow retry to start too quickly since last attempt */
2712 if (min_retry_period_ms > sleep_ms) {
2713 int64_t retry_sleep_ms = min_retry_period_ms - sleep_ms - ((switch_micro_time_now() - last_retry_start) / 1000);
2714 if (retry_sleep_ms > 0 && retry_sleep_ms <= 300000) {
2715 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Minimum retry period has not elapsed yet, waiting %"SWITCH_INT64_T_FMT" ms\n", retry_sleep_ms);
2716 if (caller_channel) {
2717 switch_ivr_sleep(oglobals.session, retry_sleep_ms, SWITCH_TRUE, NULL);
2718 if (!switch_channel_ready(caller_channel)) {
2719 status = SWITCH_STATUS_FALSE;
2720 goto done;
2721 }
2722 } else {
2723 switch_yield(retry_sleep_ms * 1000);
2724 }
2725 }
2726 }
2727 }
2728
2729 switch_safe_free(loop_data);
2730 loop_data = strdup(data);
2731 switch_assert(loop_data);
2732
2733 if (dh) {
2734 or_argc = switch_dial_handle_get_total(dh);
2735 } else {
2736 or_argc = switch_separate_string(loop_data, '|', pipe_names, (sizeof(pipe_names) / sizeof(pipe_names[0])));
2737 }
2738
2739 if ((flags & SOF_NOBLOCK) && or_argc > 1) {
2740 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Only calling the first element in the list in this mode.\n");
2741 or_argc = 1;
2742 }
2743
2744 if (or_argc <= 0) {
2745 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Nothing to do\n");
2746 goto outer_for;
2747 }
2748
2749 for (r = 0; r < or_argc && (!cancel_cause || *cancel_cause == 0); r++) {
2750 char *p, *end = NULL;
2751 int q = 0, alt = 0;
2752
2753 check_reject = 1;
2754
2755 oglobals.hups = 0;
2756
2757 reason = SWITCH_CAUSE_NONE;
2758 memset(peer_names, 0, sizeof(peer_names));
2759 peer_session = NULL;
2760 memset(oglobals.originate_status, 0, sizeof(oglobals.originate_status));
2761 new_profile = NULL;
2762 new_session = NULL;
2763 chan_type = NULL;
2764 chan_data = NULL;
2765 peer_channel = NULL;
2766 start = 0;
2767 read_frame = NULL;
2768 oglobals.ringback_ok = 1;
2769 var = NULL;
2770 to = 0;
2771 oglobals.sent_ring = 0;
2772 oglobals.progress = 0;
2773 myflags = dftflags;
2774
2775
2776 if (try > 0) {
2777 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Originate attempt %d/%d in %d ms\n", try + 1, retries,
2778 sleep_ms);
2779 if (caller_channel) {
2780 switch_ivr_sleep(oglobals.session, sleep_ms, SWITCH_TRUE, NULL);
2781 if (!switch_channel_ready(caller_channel)) {
2782 status = SWITCH_STATUS_FALSE;
2783 /* set try and retries to 0 */
2784 try = 0;
2785 retries = 0;
2786 goto done;
2787 }
2788 } else {
2789 switch_yield(sleep_ms * 1000);
2790 }
2791 }
2792
2793 if (r == 0) {
2794 last_retry_start = switch_micro_time_now();
2795 }
2796
2797 if (!dh) {
2798 p = pipe_names[r];
2799
2800 while (p && *p) {
2801 if (!end && *p == '[') {
2802 end = switch_find_end_paren(p, '[', ']');
2803 if (*(p+1) == '^' && *(p + 2) == '^') {
2804 alt = 1;
2805 } else {
2806 alt = 0;
2807 }
2808 q = 0;
2809 }
2810
2811 if (*p == '\'') {
2812 q = !q;
2813 }
2814
2815 if (end && p < end && *p == ',' && *(p-1) != '\\') {
2816
2817 if (q || alt) {
2818 *p = QUOTED_ESC_COMMA;
2819 } else {
2820 *p = UNQUOTED_ESC_COMMA;
2821 }
2822 }
2823
2824 if (p == end) {
2825 end = NULL;
2826 }
2827
2828 p++;
2829 }
2830
2831 and_argc = switch_separate_string(pipe_names[r], ',', peer_names, (sizeof(peer_names) / sizeof(peer_names[0])));
2832 } else {
2833 and_argc = switch_dial_handle_get_peers(dh, r, peer_names, MAX_PEERS);
2834 switch_dial_handle_get_vars(dh, r, peer_vars, MAX_PEERS);
2835 }
2836
2837 if ((flags & SOF_NOBLOCK) && and_argc > 1) {
2838 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Only calling the first element in the list in this mode.\n");
2839 and_argc = 1;
2840 }
2841
2842 for (i = 0; i < and_argc; i++) {
2843 const char *current_variable;
2844 switch_event_t *local_var_event = NULL, *originate_var_event = NULL;
2845
2846 end = NULL;
2847
2848 if (!(chan_type = peer_names[i])) {
2849 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Empty dial string\n");
2850 switch_goto_status(SWITCH_STATUS_FALSE, done);
2851 }
2852
2853
2854 /* strip leading spaces */
2855 while (chan_type && *chan_type && *chan_type == ' ') {
2856 chan_type++;
2857 }
2858
2859 /* extract channel variables, allowing multiple sets of braces */
2860
2861 if (*chan_type == '[') {
2862 switch_event_create_plain(&local_var_event, SWITCH_EVENT_CHANNEL_DATA);
2863 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Parsing session specific variables\n");
2864 }
2865
2866 while (*chan_type == '[') {
2867 char *parsed = NULL;
2868 char *bend = switch_find_end_paren(chan_type, '[', ']');
2869
2870 for (p = chan_type + 1; p && p < bend && *p; p++) {
2871 if (*p == QUOTED_ESC_COMMA) {
2872 *p = ',';
2873 }
2874 }
2875
2876 if (switch_event_create_brackets(chan_type, '[', ']', UNQUOTED_ESC_COMMA,
2877 &local_var_event, &parsed, SWITCH_FALSE) != SWITCH_STATUS_SUCCESS || !parsed) {
2878 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Parse Error!\n");
2879 switch_goto_status(SWITCH_STATUS_GENERR, done);
2880 }
2881
2882 if (chan_type == parsed) {
2883 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Parse Error!\n");
2884 switch_goto_status(SWITCH_STATUS_GENERR, done);
2885 } else {
2886 chan_type = parsed;
2887 }
2888 }
2889
2890 if (peer_vars[i]) {
2891 if (local_var_event) {
2892 switch_event_merge(local_var_event, peer_vars[i]);
2893 } else {
2894 switch_event_dup(&local_var_event, peer_vars[i]);
2895 }
2896
2897 if (dh && local_var_event && switch_event_serialize(local_var_event, &event_string, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
2898 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Local Vars for %s\n======================\n%s\n",
2899 peer_names[i], event_string);
2900 switch_safe_free(event_string);
2901 }
2902 }
2903
2904 /* strip leading spaces (again) */
2905 while (chan_type && *chan_type && *chan_type == ' ') {
2906 chan_type++;
2907 }
2908
2909 if ((chan_data = strchr(chan_type, '/')) != 0) {
2910 *chan_data = '\0';
2911 chan_data++;
2912 }
2913
2914 if (oglobals.session) {
2915 if (!switch_channel_ready(caller_channel)) {
2916 status = SWITCH_STATUS_FALSE;
2917 if (local_var_event) switch_event_destroy(&local_var_event);
2918 goto done;
2919 }
2920
2921 if ((caller_caller_profile = oglobals.caller_profile_override)) {
2922 new_profile = switch_caller_profile_dup(oglobals.pool, caller_caller_profile);
2923 } else {
2924 new_profile = switch_caller_profile_new(oglobals.pool,
2925 NULL,
2926 NULL,
2927 cid_name_override, cid_num_override, NULL, ani_override, aniii_override, NULL, __FILE__, NULL, chan_data);
2928 }
2929
2930 new_profile->uuid = SWITCH_BLANK_STRING;
2931 new_profile->chan_name = SWITCH_BLANK_STRING;
2932 new_profile->destination_number = switch_core_strdup(new_profile->pool, chan_data);
2933
2934 if (ani_override) {
2935 new_profile->ani = switch_core_strdup(new_profile->pool, ani_override);
2936 }
2937 if (aniii_override) {
2938 new_profile->aniii = switch_core_strdup(new_profile->pool, aniii_override);
2939 }
2940 if (cid_name_override) {
2941 new_profile->caller_id_name = switch_core_strdup(new_profile->pool, cid_name_override);
2942 }
2943 if (cid_num_override) {
2944 new_profile->caller_id_number = switch_core_strdup(new_profile->pool, cid_num_override);
2945 }
2946 } else {
2947 if (oglobals.caller_profile_override) {
2948 new_profile = switch_caller_profile_dup(oglobals.pool, oglobals.caller_profile_override);
2949 new_profile->destination_number = switch_core_strdup(new_profile->pool, switch_str_nil(chan_data));
2950 new_profile->uuid = SWITCH_BLANK_STRING;
2951 new_profile->chan_name = SWITCH_BLANK_STRING;
2952 } else {
2953 if (!cid_name_override) {
2954 cid_name_override = SWITCH_DEFAULT_CLID_NAME;
2955 }
2956 if (!cid_num_override) {
2957 cid_num_override = SWITCH_DEFAULT_CLID_NUMBER;
2958 }
2959
2960 new_profile = switch_caller_profile_new(oglobals.pool,
2961 NULL,
2962 NULL,
2963 cid_name_override, cid_num_override, NULL, ani_override, aniii_override, NULL, __FILE__, NULL, chan_data);
2964 }
2965 }
2966
2967 if (zstr(new_profile->destination_number)) {
2968 new_profile->destination_number = switch_core_strdup(new_profile->pool, "service");
2969 }
2970
2971 new_profile->callee_id_name = switch_core_strdup(new_profile->pool, "Outbound Call");
2972 new_profile->callee_id_number = switch_sanitize_number(switch_core_strdup(new_profile->pool, new_profile->destination_number));
2973
2974 oglobals.originate_status[i].caller_profile = NULL;
2975 oglobals.originate_status[i].peer_channel = NULL;
2976 oglobals.originate_status[i].peer_session = NULL;
2977
2978 new_session = NULL;
2979
2980 if (and_argc > 1 || or_argc > 1) {
2981 myflags |= SOF_FORKED_DIAL;
2982 }
2983
2984 if (var_event) {
2985 const char *vvar;
2986 if ((vvar = switch_event_get_header(var_event, "forked_dial")) && switch_true(vvar)) {
2987 myflags |= SOF_FORKED_DIAL;
2988 }
2989 if ((vvar = switch_event_get_header(var_event, "no_throttle_limits")) && switch_true(vvar)) {
2990 myflags |= SOF_NO_LIMITS;
2991 }
2992 }
2993
2994
2995 /* Valid in both {} and [] with [] taking precedence */
2996
2997 /* make a special var event with mixture of the {} and the [] vars to pass down as global vars to the outgoing channel
2998 so if something like the user channel does another originate our options will be passed down properly
2999 */
3000
3001 switch_event_dup(&originate_var_event, var_event);
3002
3003 if (local_var_event) {
3004 switch_event_merge(originate_var_event, local_var_event);
3005 }
3006
3007 if ((current_variable = switch_event_get_header(originate_var_event, "origination_ani"))) {
3008 new_profile->ani = switch_core_strdup(new_profile->pool, current_variable);
3009 myflags |= SOF_NO_EFFECTIVE_ANI;
3010 }
3011
3012 if ((current_variable = switch_event_get_header(originate_var_event, "origination_aniii"))) {
3013 new_profile->aniii = switch_core_strdup(new_profile->pool, current_variable);
3014 myflags |= SOF_NO_EFFECTIVE_ANIII;
3015 }
3016
3017 if ((current_variable = switch_event_get_header(originate_var_event, "origination_caller_id_number"))) {
3018 new_profile->caller_id_number = switch_core_strdup(new_profile->pool, current_variable);
3019 myflags |= SOF_NO_EFFECTIVE_CID_NUM;
3020 }
3021
3022 if ((current_variable = switch_event_get_header(originate_var_event, "origination_caller_id_name"))) {
3023 new_profile->caller_id_name = switch_core_strdup(new_profile->pool, current_variable);
3024 myflags |= SOF_NO_EFFECTIVE_CID_NAME;
3025 }
3026
3027 if ((current_variable = switch_event_get_header(originate_var_event, "origination_privacy"))) {
3028 new_profile->flags = SWITCH_CPF_NONE;
3029
3030 if (switch_stristr("screen", current_variable)) {
3031 switch_set_flag(new_profile, SWITCH_CPF_SCREEN);
3032 }
3033
3034 if (switch_stristr("hide_name", current_variable)) {
3035 switch_set_flag(new_profile, SWITCH_CPF_HIDE_NAME);
3036 }
3037
3038 if (switch_stristr("hide_number", current_variable)) {
3039 switch_set_flag(new_profile, SWITCH_CPF_HIDE_NUMBER);
3040 }
3041 }
3042
3043 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "originate_early_media", oglobals.early_ok ? "true" : "false");
3044
3045
3046 if (caller_channel && switch_true(switch_channel_get_variable(caller_channel, "push_channel_name"))) {
3047 char *new_name = switch_core_session_sprintf(session, "%s__B", switch_channel_get_name(caller_channel));
3048 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_channel_name", new_name);
3049 new_name = switch_core_session_sprintf(session, "_%s", switch_channel_get_name(caller_channel));
3050 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "sip_h_X-FS-Channel-Name", new_name);
3051 }
3052
3053
3054 reason = switch_core_session_outgoing_channel(oglobals.session, originate_var_event, chan_type,
3055 new_profile, &new_session, NULL, myflags, cancel_cause);
3056 switch_event_destroy(&originate_var_event);
3057
3058 if (reason != SWITCH_CAUSE_SUCCESS) {
3059 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Cannot create outgoing channel of type [%s] cause: [%s]\n",
3060 chan_type, switch_channel_cause2str(reason));
3061 if (local_var_event) switch_event_destroy(&local_var_event);
3062
3063 if (fail_on_single_reject_var) {
3064 const char *cause_str = switch_channel_cause2str(reason);
3065 int neg = *fail_on_single_reject_var == '!';
3066 int pos = !!switch_stristr(cause_str, fail_on_single_reject_var);
3067
3068 if (neg) {
3069 pos = !pos;
3070 }
3071
3072 check_reject = 0;
3073
3074 if (fail_on_single_reject == 1 || pos) {
3075 force_reason = reason;
3076 status = SWITCH_STATUS_FALSE;
3077 goto outer_for;
3078 }
3079 }
3080 continue;
3081 }
3082
3083 if (switch_core_session_read_lock(new_session) != SWITCH_STATUS_SUCCESS) {
3084 status = SWITCH_STATUS_FALSE;
3085 if (local_var_event) switch_event_destroy(&local_var_event);
3086 goto done;
3087 }
3088
3089 oglobals.originate_status[i].peer_channel = switch_core_session_get_channel(new_session);
3090 oglobals.originate_status[i].caller_profile = switch_channel_get_caller_profile(oglobals.originate_status[i].peer_channel);
3091 oglobals.originate_status[i].peer_session = new_session;
3092
3093 switch_channel_set_flag(oglobals.originate_status[i].peer_channel, CF_ORIGINATING);
3094
3095 if (caller_channel) {
3096 switch_channel_set_variable(oglobals.originate_status[i].peer_channel, "call_uuid", switch_channel_get_variable(caller_channel, "call_uuid"));
3097 }
3098
3099
3100 if (local_var_event) {
3101 const char *device_id = switch_event_get_header(local_var_event, "device_id");
3102 switch_channel_set_profile_var(oglobals.originate_status[i].peer_channel, "device_id", device_id);
3103 }
3104
3105 if ((lc = switch_event_get_header(var_event, "local_var_clobber"))) {
3106 local_clobber = switch_true(lc);
3107 }
3108
3109 if (switch_channel_test_flag(oglobals.originate_status[i].peer_channel, CF_NO_PRESENCE)) {
3110 if (var_event) {
3111 switch_event_del_header(var_event, "presence_id");
3112 }
3113 if (local_var_event) {
3114 switch_event_del_header(local_var_event, "presence_id");
3115 }
3116 }
3117
3118
3119 if (local_clobber) {
3120 if (var_event) {
3121 switch_event_header_t *header;
3122 /* install the vars from the {} params */
3123 for (header = var_event->headers; header; header = header->next) {
3124 switch_channel_set_variable_var_check(oglobals.originate_status[i].peer_channel, header->name, header->value, oglobals.check_vars);
3125 }
3126 }
3127 }
3128
3129 /* copy local originate vars to the channel */
3130 if (local_var_event) {
3131 switch_event_header_t *header;
3132 for (header = local_var_event->headers; header; header = header->next) {
3133 switch_channel_set_variable_var_check(oglobals.originate_status[i].peer_channel, header->name, header->value, oglobals.check_vars);
3134 }
3135 switch_event_destroy(&local_var_event);
3136 }
3137
3138 if (!local_clobber) {
3139 if (var_event) {
3140 switch_event_header_t *header;
3141 /* install the vars from the {} params */
3142 for (header = var_event->headers; header; header = header->next) {
3143 switch_channel_set_variable_var_check(oglobals.originate_status[i].peer_channel, header->name, header->value, oglobals.check_vars);
3144 }
3145 }
3146 }
3147
3148 if (oglobals.originate_status[i].peer_channel) {
3149 const char *vvar;
3150
3151 if (switch_true(switch_channel_get_variable(oglobals.originate_status[i].peer_channel, "leg_required"))) {
3152 oglobals.originate_status[i].tagged = 1;
3153 }
3154
3155 if ((vvar = switch_channel_get_variable(oglobals.originate_status[i].peer_channel, "origination_channel_name"))) {
3156 switch_channel_set_name(oglobals.originate_status[i].peer_channel, vvar);
3157 }
3158
3159 if ((vvar = switch_channel_get_variable(oglobals.originate_status[i].peer_channel, "origination_callee_id_name"))) {
3160 switch_channel_set_profile_var(oglobals.originate_status[i].peer_channel, "callee_id_name", vvar);
3161 }
3162
3163 if ((vvar = switch_channel_get_variable(oglobals.originate_status[i].peer_channel, "origination_callee_id_number"))) {
3164 switch_channel_set_profile_var(oglobals.originate_status[i].peer_channel, "callee_id_number", vvar);
3165 }
3166
3167 if ((vvar = switch_channel_get_variable(oglobals.originate_status[i].peer_channel, "leg_timeout"))) {
3168 int val = atoi(vvar);
3169
3170 if (val > 0) {
3171 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s Setting leg timeout to %d\n",
3172 switch_channel_get_name(oglobals.originate_status[i].peer_channel), val);
3173 oglobals.originate_status[i].per_channel_timelimit_sec = (uint32_t) val;
3174 }
3175 }
3176
3177 if ((vvar = switch_channel_get_variable(oglobals.originate_status[i].peer_channel, "leg_progress_timeout"))) {
3178 int val = atoi(vvar);
3179 if (val > 0) {
3180 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s Setting leg progress timeout to %d\n",
3181 switch_channel_get_name(oglobals.originate_status[i].peer_channel), val);
3182 oglobals.originate_status[i].per_channel_progress_timelimit_sec = (uint32_t) val;
3183 }
3184 }
3185
3186 if ((vvar = switch_channel_get_variable(oglobals.originate_status[i].peer_channel, "leg_delay_start"))) {
3187 int val = atoi(vvar);
3188 if (val > 0) {
3189 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s Setting leg delay start to %d\n",
3190 switch_channel_get_name(oglobals.originate_status[i].peer_channel), val);
3191 oglobals.originate_status[i].per_channel_delay_start = (uint32_t) val;
3192
3193 if (oglobals.originate_status[i].per_channel_progress_timelimit_sec != 0) {
3194 oglobals.originate_status[i].per_channel_progress_timelimit_sec += oglobals.originate_status[i].per_channel_delay_start;
3195 }
3196
3197 if (oglobals.originate_status[i].per_channel_timelimit_sec != 0) {
3198 oglobals.originate_status[i].per_channel_timelimit_sec += oglobals.originate_status[i].per_channel_delay_start;
3199 }
3200 }
3201 }
3202
3203 if (!zstr(ent_aleg_uuid)) {
3204 l_session = switch_core_session_locate(ent_aleg_uuid);
3205 a_session = l_session;
3206 }
3207
3208 if (a_session) {
3209 switch_channel_t *channel = switch_core_session_get_channel(a_session);
3210 char *val =
3211 switch_core_session_sprintf(a_session, "%s;%s;%s",
3212 switch_core_session_get_uuid(oglobals.originate_status[i].peer_session),
3213 switch_str_nil(switch_channel_get_variable(oglobals.originate_status[i].peer_channel, "callee_id_name")),
3214 switch_str_nil(switch_channel_get_variable(oglobals.originate_status[i].peer_channel, "callee_id_number")));
3215
3216
3217 switch_channel_set_variable(oglobals.originate_status[i].peer_channel, "originating_leg_uuid", switch_core_session_get_uuid(a_session));
3218
3219 switch_channel_add_variable_var_check(channel, "originated_legs", val, SWITCH_FALSE, SWITCH_STACK_PUSH);
3220
3221 }
3222
3223 if (l_session) {
3224 switch_core_session_rwunlock(l_session);
3225 l_session = NULL;
3226 }
3227
3228 switch_channel_set_variable(oglobals.originate_status[i].peer_channel, "originate_endpoint", chan_type);
3229 switch_channel_execute_on(oglobals.originate_status[i].peer_channel, SWITCH_CHANNEL_EXECUTE_ON_ORIGINATE_VARIABLE);
3230 switch_channel_api_on(oglobals.originate_status[i].peer_channel, SWITCH_CHANNEL_API_ON_ORIGINATE_VARIABLE);
3231 }
3232
3233 if (table) {
3234 switch_channel_add_state_handler(oglobals.originate_status[i].peer_channel, table);
3235 }
3236
3237 if (oglobals.monitor_early_media_ring || oglobals.monitor_early_media_fail || oglobals.ignore_early_media == 4) {
3238 switch_channel_set_flag(oglobals.originate_status[i].peer_channel, CF_CONSUME_ON_ORIGINATE);
3239 }
3240
3241 switch_channel_add_state_handler(oglobals.originate_status[i].peer_channel, &originate_state_handlers);
3242
3243 if ((flags & SOF_NOBLOCK) && oglobals.originate_status[i].peer_session) {
3244 status = SWITCH_STATUS_SUCCESS;
3245 *bleg = oglobals.originate_status[i].peer_session;
3246 *cause = SWITCH_CAUSE_SUCCESS;
3247 goto outer_for;
3248 }
3249
3250 if (!switch_core_session_running(oglobals.originate_status[i].peer_session)) {
3251 if (oglobals.originate_status[i].per_channel_delay_start) {
3252 switch_channel_set_flag(oglobals.originate_status[i].peer_channel, CF_BLOCK_STATE);
3253 }
3254 switch_core_session_thread_launch(oglobals.originate_status[i].peer_session);
3255 }
3256 }
3257
3258 switch_epoch_time_now(&start);
3259
3260 for (;;) {
3261 uint32_t valid_channels = 0;
3262 for (i = 0; i < and_argc; i++) {
3263 int state;
3264 time_t elapsed;
3265
3266 if (!oglobals.originate_status[i].peer_channel) {
3267 continue;
3268 }
3269
3270 state = switch_channel_get_state(oglobals.originate_status[i].peer_channel);
3271
3272 if (state < CS_HANGUP) {
3273 valid_channels++;
3274 } else {
3275 continue;
3276 }
3277
3278 if (state >= CS_ROUTING) {
3279 goto endfor1;
3280 }
3281
3282 if (caller_channel && !switch_channel_ready(caller_channel)) {
3283 goto notready;
3284 }
3285
3286 elapsed = switch_epoch_time_now(NULL) - start;
3287
3288 if (elapsed > (time_t) timelimit_sec) {
3289 to++;
3290 oglobals.idx = IDX_TIMEOUT;
3291 goto notready;
3292 }
3293
3294 if (!oglobals.sent_ring && !oglobals.ignore_ring_ready &&
3295 !oglobals.progress && (progress_timelimit_sec && elapsed > (time_t) progress_timelimit_sec)) {
3296 to++;
3297 oglobals.idx = IDX_TIMEOUT;
3298 if (force_reason == SWITCH_CAUSE_NONE) {
3299 force_reason = SWITCH_CAUSE_PROGRESS_TIMEOUT;
3300 }
3301 goto notready;
3302 }
3303
3304 switch_cond_next();
3305 }
3306
3307 check_per_channel_timeouts(&oglobals, and_argc, start, &force_reason);
3308
3309
3310 if (valid_channels == 0) {
3311 status = SWITCH_STATUS_GENERR;
3312 goto done;
3313 }
3314
3315 }
3316
3317 endfor1:
3318
3319 if (caller_channel) {
3320 if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE) ||
3321 switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA) || switch_channel_test_flag(caller_channel, CF_DISABLE_RINGBACK)) {
3322 ringback_data = NULL;
3323 }
3324 }
3325
3326
3327 #if 0
3328 /* changing behaviour ignore_early_media=true must also be explicitly set for previous behaviour */
3329 if (ringback_data) {
3330 oglobals.early_ok = 0;
3331 }
3332 #endif
3333
3334 if (ringback_data) {
3335 oglobals.sending_ringback = 1;
3336 } else {
3337 oglobals.ringback_ok = 0;
3338 }
3339
3340 if (caller_channel) {
3341 soft_holding = switch_channel_get_variable(caller_channel, SWITCH_SOFT_HOLDING_UUID_VARIABLE);
3342 }
3343
3344 while ((!caller_channel || switch_channel_ready(caller_channel) || switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) &&
3345 check_channel_status(&oglobals, and_argc, &force_reason, start)) {
3346 time_t elapsed = switch_epoch_time_now(NULL) - start;
3347 read_packet = 0;
3348
3349 if (cancel_cause && *cancel_cause > 0) {
3350 if (force_reason == SWITCH_CAUSE_NONE) {
3351 force_reason = *cancel_cause;
3352 }
3353 oglobals.idx = IDX_CANCEL;
3354 goto notready;
3355 }
3356
3357 check_per_channel_timeouts(&oglobals, and_argc, start, &force_reason);
3358
3359 if (oglobals.session) {
3360 switch_ivr_parse_all_events(oglobals.session);
3361 }
3362
3363 if (!oglobals.sent_ring && !oglobals.progress && (progress_timelimit_sec && elapsed > (time_t) progress_timelimit_sec)) {
3364 oglobals.idx = IDX_TIMEOUT;
3365 if (force_reason == SWITCH_CAUSE_NONE) {
3366 force_reason = SWITCH_CAUSE_PROGRESS_TIMEOUT;
3367 }
3368 goto notready;
3369 }
3370
3371 if ((to = (uint8_t) (elapsed >= (time_t) timelimit_sec)) || (fail_on_single_reject && oglobals.hups)) {
3372 int ok = 0;
3373
3374 if (fail_on_single_reject_var) {
3375 if (!switch_true(fail_on_single_reject_var)) {
3376 ok = 1;
3377
3378 for (i = 0; i < and_argc; i++) {
3379 switch_channel_t *pchannel;
3380 const char *cause_str;
3381
3382 if (!oglobals.originate_status[i].peer_session) {
3383 continue;
3384 }
3385 pchannel = switch_core_session_get_channel(oglobals.originate_status[i].peer_session);
3386
3387 if (switch_channel_down_nosig(pchannel)) {
3388 int neg, pos;
3389 cause_str = switch_channel_cause2str(switch_channel_get_cause(pchannel));
3390 neg = *fail_on_single_reject_var == '!';
3391 pos = !!switch_stristr(cause_str, fail_on_single_reject_var);
3392
3393 if (neg) {
3394 pos = !pos;
3395 }
3396
3397 if (pos) {
3398 ok = 0;
3399 break;
3400 }
3401 }
3402 }
3403 }
3404 }
3405 if (!ok) {
3406 oglobals.idx = IDX_TIMEOUT;
3407 goto notready;
3408 }
3409 }
3410
3411 /* read from the channel while we wait if the audio is up on it */
3412 if (oglobals.session &&
3413 !switch_channel_test_flag(caller_channel, CF_PROXY_MODE) &&
3414 !switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA) &&
3415 //!switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE) &&
3416 switch_channel_up(caller_channel) &&
3417 (oglobals.ringback_ok
3418 || (switch_channel_test_flag(caller_channel, CF_ANSWERED) || switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)))) {
3419
3420 switch_status_t tstatus = SWITCH_STATUS_SUCCESS;
3421 int silence = 0;
3422
3423 if (caller_channel && cancel_key) {
3424 if (switch_channel_has_dtmf(caller_channel)) {
3425 switch_dtmf_t dtmf = { 0, 0 };
3426 if (switch_channel_dequeue_dtmf(caller_channel, &dtmf) == SWITCH_STATUS_SUCCESS) {
3427 if (dtmf.digit == *cancel_key) {
3428 oglobals.idx = IDX_KEY_CANCEL;
3429 goto notready;
3430 }
3431 }
3432 }
3433 }
3434
3435 if (switch_channel_media_ready(caller_channel)) {
3436 tstatus = switch_core_session_read_frame(oglobals.session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
3437 if (!SWITCH_READ_ACCEPTABLE(tstatus)) {
3438 if (soft_holding) {
3439 switch_channel_set_flag(caller_channel, CF_XFER_ZOMBIE);
3440 }
3441
3442 if (switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) {
3443 goto do_continue;
3444 }
3445 break;
3446 }
3447
3448 read_packet++;
3449 } else {
3450 read_frame = NULL;
3451 }
3452
3453
3454 if (oglobals.ringback_ok && (oglobals.ring_ready || oglobals.instant_ringback ||
3455 oglobals.sending_ringback > 1 || oglobals.bridge_early_media > -1)) {
3456 if (oglobals.ringback_ok == 1) {
3457 switch_status_t rst;
3458
3459 rst = setup_ringback(&oglobals, oglobals.originate_status, and_argc, ringback_data, &ringback, &write_frame, &write_codec);
3460
3461 if (oglobals.bridge_early_media > -1) {
3462 switch_threadattr_t *thd_attr = NULL;
3463 switch_threadattr_create(&thd_attr, switch_core_session_get_pool(session));
3464 switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
3465 early_state.oglobals = &oglobals;
3466 //early_state.originate_status = oglobals.originate_status;
3467 early_state.ready = 1;
3468 early_state.ringback = &ringback;
3469 early_state.ttl = and_argc;
3470 switch_mutex_init(&early_state.mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
3471 switch_buffer_create_dynamic(&early_state.buffer, 1024, 1024, 0);
3472 switch_thread_create(&oglobals.ethread, thd_attr, early_thread_run, &early_state, switch_core_session_get_pool(session));
3473 }
3474
3475
3476 switch (rst) {
3477 case SWITCH_STATUS_SUCCESS:
3478 oglobals.ringback_ok++;
3479 break;
3480 case SWITCH_STATUS_FALSE:
3481 goto notready;
3482 break;
3483 case SWITCH_STATUS_BREAK:
3484 status = SWITCH_STATUS_FALSE;
3485 goto done;
3486 break;
3487 default:
3488 ringback_data = NULL;
3489 oglobals.ringback_ok = 0;
3490 oglobals.sending_ringback = 0;
3491 break;
3492 }
3493
3494 goto do_continue;
3495 }
3496
3497 if (oglobals.bridge_early_media > -1) {
3498 write_frame.datalen = 0;
3499 switch_mutex_lock(early_state.mutex);
3500 if (ringback.asis) {
3501 uint16_t mlen;
3502 switch_size_t buflen = switch_buffer_inuse(early_state.buffer);
3503 if (buflen > sizeof(uint16_t)) {
3504 switch_buffer_peek(early_state.buffer, &mlen, sizeof(uint16_t));
3505 if (buflen >= (mlen + sizeof(uint16_t))) {
3506 switch_buffer_toss(early_state.buffer, sizeof(uint16_t));
3507 write_frame.datalen = (uint32_t)switch_buffer_read(early_state.buffer, write_frame.data, mlen);
3508 }
3509 }
3510 } else {
3511 if (write_frame.codec && switch_buffer_inuse(early_state.buffer) >= write_frame.codec->implementation->decoded_bytes_per_packet) {
3512 write_frame.datalen = (uint32_t)switch_buffer_read(early_state.buffer, write_frame.data,
3513 write_frame.codec->implementation->decoded_bytes_per_packet);
3514 }
3515 }
3516 switch_mutex_unlock(early_state.mutex);
3517 } else if (ringback.fh) {
3518 switch_size_t mlen, olen;
3519 unsigned int pos = 0;
3520
3521 if (ringback.asis) {
3522 mlen = write_frame.codec->implementation->encoded_bytes_per_packet;
3523 } else {
3524 mlen = write_frame.codec->implementation->samples_per_packet;
3525 }
3526
3527 olen = mlen;
3528
3529 //if (ringback.fh->resampler && ringback.fh->resampler->rfactor > 1) {
3530 //olen = (switch_size_t) (olen * ringback.fh->resampler->rfactor);
3531 //}
3532
3533 switch_core_file_read(ringback.fh, write_frame.data, &olen);
3534
3535 if (olen == 0) {
3536 olen = mlen;
3537 ringback.fh->speed = 0;
3538 switch_core_file_seek(ringback.fh, &pos, 0, SEEK_SET);
3539 switch_core_file_read(ringback.fh, write_frame.data, &olen);
3540 if (olen == 0) {
3541 break;
3542 }
3543 }
3544 write_frame.datalen = (uint32_t) (ringback.asis ? olen : olen * 2 * ringback.fh->channels);
3545 write_frame.samples = (uint32_t) olen;
3546
3547 } else if (ringback.audio_buffer) {
3548 if ((write_frame.datalen = (uint32_t) switch_buffer_read_loop(ringback.audio_buffer,
3549 write_frame.data,
3550 write_frame.codec->implementation->decoded_bytes_per_packet)) <=
3551 0) {
3552
3553 if (soft_holding) {
3554 switch_channel_set_flag(caller_channel, CF_XFER_ZOMBIE);
3555 goto do_continue;
3556 }
3557
3558 break;
3559 }
3560 } else if (ringback.silence) {
3561 silence = ringback.silence;
3562 }
3563 } else {
3564 silence = 600;
3565 }
3566
3567 if ((ringback.fh || silence || ringback.audio_buffer || oglobals.bridge_early_media > -1) && write_frame.codec && write_frame.codec->implementation && write_frame.datalen) {
3568 if (silence) {
3569 write_frame.datalen = read_impl.decoded_bytes_per_packet;
3570 switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.datalen / 2, write_frame.codec->implementation->number_of_channels, silence);
3571 }
3572
3573 if (switch_core_session_write_frame(oglobals.session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
3574 if (soft_holding) {
3575 switch_channel_set_flag(caller_channel, CF_XFER_ZOMBIE);
3576 }
3577 if (switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) {
3578 goto do_continue;
3579 }
3580 break;
3581 }
3582 }
3583
3584 }
3585
3586 do_continue:
3587
3588 if (!read_packet) {
3589 switch_yield(20000);
3590 }
3591 }
3592
3593 notready:
3594
3595 if (caller_channel) {
3596 holding = switch_channel_get_variable(caller_channel, SWITCH_HOLDING_UUID_VARIABLE);
3597 switch_channel_set_variable(caller_channel, SWITCH_HOLDING_UUID_VARIABLE, NULL);
3598
3599 if (soft_holding && switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) {
3600 holding = soft_holding;
3601 soft_holding = NULL;
3602 switch_channel_set_variable(caller_channel, SWITCH_SOFT_HOLDING_UUID_VARIABLE, NULL);
3603 }
3604 }
3605
3606 if (caller_channel && !switch_channel_ready(caller_channel) && !switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) {
3607 oglobals.idx = IDX_CANCEL;
3608 }
3609
3610 if (oglobals.session && (ringback_data || !(switch_channel_test_flag(caller_channel, CF_PROXY_MODE) ||
3611 switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA)))) {
3612 switch_core_session_reset(oglobals.session, SWITCH_FALSE, SWITCH_TRUE);
3613 }
3614
3615 if (holding) {
3616 if (oglobals.idx > IDX_NADA) {
3617 peer_session = oglobals.originate_status[oglobals.idx].peer_session;
3618 peer_channel = oglobals.originate_status[oglobals.idx].peer_channel;
3619 oglobals.originate_status[oglobals.idx].peer_channel = NULL;
3620 } else if (and_argc == 1) {
3621 peer_session = oglobals.originate_status[0].peer_session;
3622 peer_channel = oglobals.originate_status[0].peer_channel;
3623 oglobals.originate_status[0].peer_channel = NULL;
3624 } else {
3625 for (i = 0; i < and_argc; i++) {
3626 if (!peer_eligible(oglobals.originate_status[i].peer_channel)) {
3627 continue;
3628 }
3629 if (switch_channel_media_ready(oglobals.originate_status[i].peer_channel)) {
3630 peer_session = oglobals.originate_status[i].peer_session;
3631 peer_channel = oglobals.originate_status[i].peer_channel;
3632 oglobals.originate_status[i].peer_channel = NULL;
3633 goto end_search;
3634 }
3635 }
3636 for (i = 0; i < and_argc; i++) {
3637 if (!peer_eligible(oglobals.originate_status[i].peer_channel)) {
3638 continue;
3639 }
3640 if (switch_channel_up_nosig(oglobals.originate_status[i].peer_channel)) {
3641 peer_session = oglobals.originate_status[i].peer_session;
3642 peer_channel = oglobals.originate_status[i].peer_channel;
3643 oglobals.originate_status[i].peer_channel = NULL;
3644 break;
3645 }
3646 }
3647 }
3648
3649 end_search:
3650
3651 if (peer_channel && switch_channel_down_nosig(peer_channel)) {
3652 switch_core_session_rwunlock(peer_session);
3653 peer_session = NULL;
3654 peer_channel = NULL;
3655
3656 }
3657
3658 if (oglobals.idx == IDX_TIMEOUT || to || oglobals.idx == IDX_KEY_CANCEL || oglobals.idx == IDX_CANCEL ||
3659 (!peer_session && oglobals.idx == IDX_NADA)) {
3660 const char *dest = NULL;
3661 const char *context = NULL;
3662 const char *dialplan = NULL;
3663 switch_core_session_t *holding_session;
3664
3665 if (caller_channel) {
3666 if (zstr(context)) {
3667 context = switch_channel_get_variable(caller_channel, "context");
3668 }
3669 if (zstr(dialplan)) {
3670 dialplan = switch_channel_get_variable(caller_channel, "dialplan");
3671 }
3672 }
3673
3674 if (zstr(context)) {
3675 context = "default";
3676 }
3677
3678 if (zstr(context)) {
3679 dialplan = "XML";
3680 }
3681
3682 if ((holding_session = switch_core_session_locate(holding))) {
3683 switch_channel_t *holding_channel = switch_core_session_get_channel(holding_session);
3684 switch_status_t mstatus = SWITCH_STATUS_FALSE;
3685
3686 if (caller_channel) {
3687 if ((mstatus = switch_channel_caller_extension_masquerade(caller_channel, holding_channel, 0)) == SWITCH_STATUS_SUCCESS) {
3688 switch_channel_restart(holding_channel);
3689 }
3690 }
3691
3692 if (mstatus != SWITCH_STATUS_SUCCESS) {
3693 if (peer_channel) {
3694 dest = switch_channel_get_variable(peer_channel, "destination_number");
3695 context = switch_channel_get_variable(peer_channel, "context");
3696 dialplan = switch_channel_get_variable(peer_channel, "dialplan");
3697 } else if (caller_channel) {
3698 dest = switch_channel_get_variable(caller_channel, "destination_number");
3699 }
3700 if (dest) {
3701 switch_ivr_session_transfer(holding_session, dest, dialplan, context);
3702 }
3703 }
3704
3705 switch_core_session_rwunlock(holding_session);
3706 holding = NULL;
3707 holding_session = NULL;
3708 }
3709
3710 if (peer_channel) {
3711 switch_channel_hangup(peer_channel, SWITCH_CAUSE_ATTENDED_TRANSFER);
3712 switch_core_session_rwunlock(peer_session);
3713 }
3714 if (force_reason == SWITCH_CAUSE_NONE) {
3715 force_reason = SWITCH_CAUSE_ATTENDED_TRANSFER;
3716 }
3717 } else if (zstr(soft_holding)) {
3718
3719 if (peer_channel && switch_channel_ready(peer_channel)) {
3720 switch_core_session_t *holding_session;
3721
3722 if (force_reason == SWITCH_CAUSE_NONE) {
3723 force_reason = SWITCH_CAUSE_ATTENDED_TRANSFER;
3724 }
3725
3726 if ((holding_session = switch_core_session_locate(holding))) {
3727 switch_channel_set_variable(switch_core_session_get_channel(holding_session), SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE, "true");
3728 switch_core_session_rwunlock(holding_session);
3729 }
3730 switch_channel_set_flag(peer_channel, CF_LAZY_ATTENDED_TRANSFER);
3731 switch_ivr_uuid_bridge(holding, switch_core_session_get_uuid(peer_session));
3732 holding = NULL;
3733 oglobals.idx = IDX_XFER;
3734 if (caller_channel && switch_channel_up_nosig(caller_channel) && !switch_channel_test_flag(caller_channel, CF_INTERCEPTED)) {
3735 switch_channel_hangup(caller_channel, SWITCH_CAUSE_ATTENDED_TRANSFER);
3736 }
3737 caller_channel = NULL;
3738 oglobals.session = NULL;
3739 session = NULL;
3740 switch_core_session_rwunlock(peer_session);
3741 } else {
3742 switch_core_session_t *holding_session;
3743
3744 if ((holding_session = switch_core_session_locate(holding))) {
3745 switch_channel_t *holding_channel = switch_core_session_get_channel(holding_session);
3746
3747 if (caller_channel && switch_channel_ready(caller_channel)) {
3748 switch_channel_set_variable(holding_channel, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE, "true");
3749 switch_ivr_uuid_bridge(holding, switch_core_session_get_uuid(session));
3750 holding = NULL;
3751 } else {
3752 switch_channel_hangup(holding_channel, SWITCH_CAUSE_NORMAL_UNSPECIFIED);
3753 }
3754 switch_core_session_rwunlock(holding_session);
3755 }
3756 }
3757 }
3758
3759 peer_session = NULL;
3760 peer_channel = NULL;
3761 }
3762
3763 for (i = 0; i < and_argc; i++) {
3764 if (!peer_eligible(oglobals.originate_status[i].peer_channel)) {
3765 continue;
3766 }
3767
3768 if (i != oglobals.idx) {
3769 holding = NULL;
3770
3771 if (oglobals.idx == IDX_TIMEOUT || to) {
3772 reason = SWITCH_CAUSE_NO_ANSWER;
3773 } else {
3774 if (oglobals.idx == IDX_CANCEL) {
3775 reason = SWITCH_CAUSE_ORIGINATOR_CANCEL;
3776 } else {
3777 if (and_argc > 1) {
3778 reason = SWITCH_CAUSE_LOSE_RACE;
3779 } else if (!switch_channel_ready(oglobals.originate_status[i].peer_channel)) {
3780 wait_for_cause(oglobals.originate_status[i].peer_channel);
3781 if (switch_channel_down_nosig(oglobals.originate_status[i].peer_channel)) {
3782 reason = switch_channel_get_cause(oglobals.originate_status[i].peer_channel);
3783 }
3784 } else {
3785 reason = SWITCH_CAUSE_NO_ANSWER;
3786 }
3787 }
3788 }
3789 if (switch_channel_up_nosig(oglobals.originate_status[i].peer_channel)) {
3790 if (caller_channel && i == 0) {
3791 holding = switch_channel_get_variable(caller_channel, SWITCH_SOFT_HOLDING_UUID_VARIABLE);
3792 switch_channel_set_variable(caller_channel, SWITCH_SOFT_HOLDING_UUID_VARIABLE, NULL);
3793 }
3794
3795 if (holding && oglobals.idx != IDX_TIMEOUT && oglobals.idx != IDX_KEY_CANCEL && oglobals.idx < 0) {
3796 switch_core_session_t *holding_session;
3797
3798 if ((holding_session = switch_core_session_locate(holding))) {
3799 switch_channel_t *holding_channel = switch_core_session_get_channel(holding_session);
3800
3801 switch_channel_set_variable(holding_channel, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE, "true");
3802
3803 if (caller_channel && switch_true(switch_channel_get_variable(caller_channel, "recording_follow_transfer"))) {
3804 switch_ivr_transfer_recordings(session, oglobals.originate_status[i].peer_session);
3805 }
3806
3807 if (switch_true(switch_channel_get_variable(holding_channel, "recording_follow_transfer"))) {
3808 switch_ivr_transfer_recordings(holding_session, oglobals.originate_status[i].peer_session);
3809 }
3810
3811 switch_core_session_rwunlock(holding_session);
3812 }
3813 switch_channel_set_flag(oglobals.originate_status[i].peer_channel, CF_LAZY_ATTENDED_TRANSFER);
3814 switch_ivr_uuid_bridge(holding, switch_core_session_get_uuid(oglobals.originate_status[i].peer_session));
3815 holding = NULL;
3816 } else {
3817 if (force_reason == SWITCH_CAUSE_LOSE_RACE || reason == SWITCH_CAUSE_LOSE_RACE) {
3818 switch_channel_set_variable(oglobals.originate_status[i].peer_channel, "group_dial_status", "loser");
3819 }
3820 switch_channel_hangup(oglobals.originate_status[i].peer_channel, force_reason ? force_reason : reason);
3821 }
3822 }
3823 }
3824 }
3825
3826
3827
3828 if (oglobals.idx > IDX_NADA) {
3829 if ((peer_session = oglobals.originate_status[oglobals.idx].peer_session)) {
3830 peer_channel = switch_core_session_get_channel(oglobals.originate_status[oglobals.idx].peer_session);
3831 }
3832 } else {
3833 status = SWITCH_STATUS_FALSE;
3834 if (caller_channel && peer_channel) {
3835 switch_process_import(oglobals.session, peer_channel, "import", NULL);
3836 }
3837 peer_channel = NULL;
3838 goto done;
3839 }
3840
3841 if (caller_channel) {
3842
3843 if (switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE) && !switch_channel_up(caller_channel)) {
3844 if (switch_channel_media_up(peer_channel)) {
3845 oglobals.idx = IDX_XFER;
3846 reason = force_reason = SWITCH_CAUSE_ATTENDED_TRANSFER;
3847 switch_channel_execute_on(peer_channel, "execute_on_orphaned_bleg");
3848 switch_channel_api_on(peer_channel, "api_on_orphaned_bleg");
3849 }
3850 } else if (switch_channel_test_flag(peer_channel, CF_ANSWERED)) {
3851 switch_channel_pass_callee_id(peer_channel, caller_channel);
3852 if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE)) {
3853 status = SWITCH_STATUS_SUCCESS;
3854 } else {
3855 status = switch_channel_answer(caller_channel);
3856 }
3857 } else if (switch_channel_test_flag(peer_channel, CF_EARLY_MEDIA)) {
3858 if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE)) {
3859 status = SWITCH_STATUS_SUCCESS;
3860 } else {
3861 switch_channel_pass_callee_id(peer_channel, caller_channel);
3862 status = switch_channel_pre_answer(caller_channel);
3863 }
3864 } else {
3865 status = SWITCH_STATUS_SUCCESS;
3866 }
3867
3868 if (status != SWITCH_STATUS_SUCCESS) {
3869 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(peer_channel), SWITCH_LOG_DEBUG, "%s Media Establishment Failed.\n",
3870 switch_channel_get_name(caller_channel));
3871 switch_channel_hangup(peer_channel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION);
3872 }
3873 }
3874
3875 if (switch_channel_test_flag(peer_channel, CF_ANSWERED) ||
3876 (oglobals.early_ok && switch_channel_test_flag(peer_channel, CF_EARLY_MEDIA)) ||
3877 (oglobals.return_ring_ready && switch_channel_test_flag(peer_channel, CF_RING_READY))
3878 ) {
3879 *bleg = peer_session;
3880
3881 if (oglobals.monitor_early_media_ring || oglobals.monitor_early_media_fail) {
3882 switch_ivr_stop_tone_detect_session(peer_session);
3883 switch_channel_set_private(peer_channel, "_oglobals_", NULL);
3884 }
3885
3886 status = SWITCH_STATUS_SUCCESS;
3887 } else {
3888 status = SWITCH_STATUS_FALSE;
3889 }
3890
3891 done:
3892
3893 *cause = SWITCH_CAUSE_NONE;
3894
3895 if (caller_channel && !switch_channel_ready(caller_channel)) {
3896 status = SWITCH_STATUS_FALSE;
3897 }
3898
3899 if (status == SWITCH_STATUS_SUCCESS) {
3900 if (caller_channel) {
3901 switch_channel_set_variable(caller_channel, "originate_disposition", "call accepted");
3902 if (peer_channel) {
3903 switch_process_import(oglobals.session, peer_channel, "import", NULL);
3904
3905 if (switch_channel_test_flag(peer_channel, CF_ANSWERED)) {
3906 switch_channel_set_variable(caller_channel, "DIALSTATUS", "EARLY");
3907 } else {
3908 switch_channel_set_variable(caller_channel, "DIALSTATUS", "ANSWER");
3909 }
3910
3911 }
3912 }
3913 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals.session), SWITCH_LOG_DEBUG, "Originate Resulted in Success: [%s] Peer UUID: %s\n",
3914 switch_channel_get_name(peer_channel), switch_channel_get_uuid(peer_channel));
3915 *cause = SWITCH_CAUSE_SUCCESS;
3916
3917 } else {
3918 const char *cdr_var = NULL;
3919 const char *json_cdr_var = NULL;
3920
3921 switch_xml_t cdr = NULL;
3922 cJSON *json_cdr = NULL;
3923
3924 char *json_text;
3925 char *xml_text;
3926 char buf[128] = "", buf2[128] = "";
3927
3928 if (caller_channel) {
3929 cdr_var = switch_channel_get_variable(caller_channel, "failed_xml_cdr_prefix");
3930 }
3931
3932 if (caller_channel) {
3933 json_cdr_var = switch_channel_get_variable(caller_channel, "failed_json_cdr_prefix");
3934 }
3935
3936 if (peer_channel) {
3937 wait_for_cause(peer_channel);
3938 *cause = switch_channel_get_cause(peer_channel);
3939 } else {
3940 for (i = 0; i < and_argc; i++) {
3941 if (!oglobals.originate_status[i].peer_channel) {
3942 continue;
3943 }
3944 *cause = switch_channel_get_cause(oglobals.originate_status[i].peer_channel);
3945 break;
3946 }
3947 }
3948
3949 if (cdr_var) {
3950 for (i = 0; i < and_argc; i++) {
3951 switch_channel_t *channel;
3952
3953 if (!oglobals.originate_status[i].peer_session) {
3954 continue;
3955 }
3956
3957 channel = switch_core_session_get_channel(oglobals.originate_status[i].peer_session);
3958
3959 switch_channel_wait_for_state_timeout(channel, CS_REPORTING, 5000);
3960
3961 if (!switch_channel_test_flag(channel, CF_TIMESTAMP_SET)) {
3962 switch_channel_set_timestamps(channel);
3963 }
3964
3965 if (switch_ivr_generate_xml_cdr(oglobals.originate_status[i].peer_session, &cdr) == SWITCH_STATUS_SUCCESS) {
3966 if ((xml_text = switch_xml_toxml(cdr, SWITCH_FALSE))) {
3967 switch_snprintf(buf, sizeof(buf), "%s_%d", cdr_var, ++cdr_total);
3968 switch_channel_set_variable(caller_channel, buf, xml_text);
3969 switch_safe_free(xml_text);
3970 }
3971 switch_xml_free(cdr);
3972 cdr = NULL;
3973 }
3974
3975 }
3976 switch_snprintf(buf, sizeof(buf), "%s_total", cdr_var);
3977 switch_snprintf(buf2, sizeof(buf2), "%d", cdr_total ? cdr_total : 0);
3978 switch_channel_set_variable(caller_channel, buf, buf2);
3979 }
3980
3981 if (json_cdr_var) {
3982 for (i = 0; i < and_argc; i++) {
3983 switch_channel_t *channel;
3984
3985 if (!oglobals.originate_status[i].peer_session) {
3986 continue;
3987 }
3988
3989 channel = switch_core_session_get_channel(oglobals.originate_status[i].peer_session);
3990
3991 switch_channel_wait_for_state_timeout(channel, CS_REPORTING, 5000);
3992
3993 if (!switch_channel_test_flag(channel, CF_TIMESTAMP_SET)) {
3994 switch_channel_set_timestamps(channel);
3995 }
3996
3997 if (switch_ivr_generate_json_cdr(oglobals.originate_status[i].peer_session, &json_cdr, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
3998 json_text = cJSON_PrintUnformatted(json_cdr);
3999 switch_snprintf(buf, sizeof(buf), "%s_%d", json_cdr_var, ++cdr_total);
4000 switch_channel_set_variable(caller_channel, buf, json_text);
4001 // switch_safe_free(json_text);
4002 cJSON_Delete(json_cdr);
4003 json_cdr = NULL;
4004 }
4005
4006 }
4007 switch_snprintf(buf, sizeof(buf), "%s_total", json_cdr_var);
4008 switch_snprintf(buf2, sizeof(buf2), "%d", cdr_total ? cdr_total : 0);
4009 switch_channel_set_variable(caller_channel, buf, buf2);
4010 }
4011
4012 if (caller_channel && switch_channel_test_flag(caller_channel, CF_INTERCEPTED)) {
4013 *cause = SWITCH_CAUSE_PICKED_OFF;
4014 }
4015
4016 if (!*cause) {
4017 if (reason) {
4018 *cause = reason;
4019 } else if (caller_channel) {
4020 *cause = switch_channel_get_cause(caller_channel);
4021 } else {
4022 *cause = SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
4023 for (i = 0; i < and_argc; i++) {
4024 if (!peer_eligible(oglobals.originate_status[i].peer_channel)) {
4025 continue;
4026 }
4027
4028 wait_for_cause(oglobals.originate_status[i].peer_channel);
4029
4030 if (switch_channel_down_nosig(oglobals.originate_status[i].peer_channel)) {
4031 *cause = switch_channel_get_cause(oglobals.originate_status[i].peer_channel);
4032 break;
4033 }
4034
4035 }
4036 }
4037 }
4038
4039 if (*cause == SWITCH_CAUSE_SUCCESS || *cause == SWITCH_CAUSE_NONE) {
4040 *cause = SWITCH_CAUSE_ORIGINATOR_CANCEL;
4041 }
4042
4043 if (oglobals.idx == IDX_CANCEL) {
4044 *cause = SWITCH_CAUSE_ORIGINATOR_CANCEL;
4045 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals.session), SWITCH_LOG_DEBUG,
4046 "Originate Cancelled by originator termination Cause: %d [%s]\n", *cause, switch_channel_cause2str(*cause));
4047
4048 } else if (oglobals.idx == IDX_TIMEOUT) {
4049 *cause = SWITCH_CAUSE_NO_ANSWER;
4050 } else {
4051 if (oglobals.idx == IDX_XFER) {
4052 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals.session), SWITCH_LOG_DEBUG,
4053 "Originate Resulted in Attended Transfer Cause: %d [%s]\n", *cause, switch_channel_cause2str(*cause));
4054 } else {
4055
4056 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals.session), SWITCH_LOG_DEBUG,
4057 "Originate Resulted in Error Cause: %d [%s]\n", *cause, switch_channel_cause2str(*cause));
4058 }
4059 }
4060 }
4061
4062 if (caller_channel) {
4063 switch_channel_set_variable(caller_channel, "originate_disposition", switch_channel_cause2str(*cause));
4064
4065 switch (*cause) {
4066 case SWITCH_CAUSE_ORIGINATOR_CANCEL:
4067 switch_channel_set_variable(caller_channel, "DIALSTATUS", "CANCEL");
4068 break;
4069 case SWITCH_CAUSE_USER_BUSY:
4070 switch_channel_set_variable(caller_channel, "DIALSTATUS", "BUSY");
4071 break;
4072 case SWITCH_CAUSE_NO_ANSWER:
4073 switch_channel_set_variable(caller_channel, "DIALSTATUS", "NOANSWER");
4074 break;
4075 case SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER:
4076 case SWITCH_CAUSE_INVALID_PROFILE:
4077 switch_channel_set_variable(caller_channel, "DIALSTATUS", "INVALIDARGS");
4078 break;
4079 case SWITCH_CAUSE_CALL_REJECTED:
4080 switch_channel_set_variable(caller_channel, "DIALSTATUS", "DONTCALL");
4081 break;
4082 default:
4083 switch_channel_set_variable(caller_channel, "DIALSTATUS", switch_channel_cause2str(*cause));
4084 break;
4085 }
4086 }
4087
4088 early_state.ready = 0;
4089
4090 if (oglobals.ethread) {
4091 switch_status_t st;
4092 switch_thread_join(&st, oglobals.ethread);
4093 }
4094
4095 if (early_state.buffer) {
4096 switch_buffer_destroy(&early_state.buffer);
4097 }
4098
4099 if (ringback.fh) {
4100 switch_core_file_close(ringback.fh);
4101 ringback.fh = NULL;
4102 } else if (ringback.audio_buffer) {
4103 teletone_destroy_session(&ringback.ts);
4104 switch_safe_free(ringback.mux_buf);
4105 switch_buffer_destroy(&ringback.audio_buffer);
4106 }
4107
4108 if (oglobals.session) {
4109 switch_core_session_reset(oglobals.session, SWITCH_FALSE, SWITCH_TRUE);
4110 }
4111
4112 if (switch_core_codec_ready(&write_codec)) {
4113 switch_core_codec_destroy(&write_codec);
4114 }
4115
4116 for (i = 0; i < and_argc; i++) {
4117 switch_channel_state_t state;
4118 switch_core_session_t *peer_session;
4119 char *val;
4120
4121 if (!oglobals.originate_status[i].peer_channel) {
4122 continue;
4123 }
4124
4125 if (session) {
4126 val = switch_core_session_sprintf(oglobals.originate_status[i].peer_session, "%s;%s",
4127 switch_core_session_get_uuid(oglobals.originate_status[i].peer_session),
4128 switch_channel_cause2str(switch_channel_get_cause(oglobals.originate_status[i].peer_channel)));
4129
4130 switch_channel_add_variable_var_check(switch_core_session_get_channel(session), "originate_causes", val, SWITCH_FALSE, SWITCH_STACK_PUSH);
4131 }
4132
4133 if (status == SWITCH_STATUS_SUCCESS) {
4134 switch_channel_clear_flag(oglobals.originate_status[i].peer_channel, CF_ORIGINATING);
4135 if (bleg && *bleg && *bleg == oglobals.originate_status[i].peer_session) {
4136 continue;
4137 }
4138 } else if ((state = switch_channel_get_state(oglobals.originate_status[i].peer_channel)) < CS_HANGUP &&
4139 switch_channel_test_flag(oglobals.originate_status[i].peer_channel, CF_ORIGINATING)) {
4140 if (!(state == CS_RESET || switch_channel_test_flag(oglobals.originate_status[i].peer_channel, CF_TRANSFER) ||
4141 switch_channel_test_flag(oglobals.originate_status[i].peer_channel, CF_REDIRECT) ||
4142 switch_channel_test_flag(oglobals.originate_status[i].peer_channel, CF_BRIDGED))) {
4143 if (caller_channel && switch_channel_test_flag(caller_channel, CF_INTERCEPTED)) {
4144 switch_channel_set_flag(oglobals.originate_status[i].peer_channel, CF_INTERCEPT);
4145 }
4146 switch_channel_hangup(oglobals.originate_status[i].peer_channel, *cause);
4147 }
4148 }
4149 switch_channel_clear_flag(oglobals.originate_status[i].peer_channel, CF_ORIGINATING);
4150
4151 peer_session = oglobals.originate_status[i].peer_session;
4152 oglobals.originate_status[i].down_session = oglobals.originate_status[i].peer_session;
4153 oglobals.originate_status[i].peer_session = NULL;
4154 oglobals.originate_status[i].peer_channel = NULL;
4155
4156 switch_core_session_rwunlock(peer_session);
4157 }
4158
4159 if (status == SWITCH_STATUS_SUCCESS || oglobals.idx == IDX_XFER) {
4160 goto outer_for;
4161 } else {
4162 int ok = 1;
4163
4164 if (fail_on_single_reject && check_reject && !switch_true(fail_on_single_reject_var)) {
4165 for (i = 0; i < and_argc; i++) {
4166 switch_channel_t *pchannel;
4167 const char *cause_str;
4168
4169 if (!oglobals.originate_status[i].down_session) {
4170 continue;
4171 }
4172
4173 pchannel = switch_core_session_get_channel(oglobals.originate_status[i].down_session);
4174 wait_for_cause(pchannel);
4175
4176 if (switch_channel_down_nosig(pchannel)) {
4177 int neg, pos;
4178
4179 cause_str = switch_channel_cause2str(switch_channel_get_cause(pchannel));
4180
4181 neg = *fail_on_single_reject_var == '!';
4182 pos = !!switch_stristr(cause_str, fail_on_single_reject_var);
4183
4184 if (neg) {
4185 pos = !pos;
4186 }
4187
4188 if (pos) {
4189 ok = 0;
4190 break;
4191 }
4192 }
4193 }
4194 }
4195
4196 if (!ok) {
4197 goto outer_for;
4198 }
4199
4200 if (to && !oglobals.continue_on_timeout) {
4201 goto outer_for;
4202 }
4203 }
4204 }
4205 }
4206 outer_for:
4207 switch_safe_free(loop_data);
4208 switch_safe_free(odata);
4209 switch_safe_free(oglobals.file);
4210 switch_safe_free(oglobals.error_file);
4211
4212 if (bleg && status != SWITCH_STATUS_SUCCESS) {
4213 *bleg = NULL;
4214 }
4215
4216 if (bleg && !*bleg && status == SWITCH_STATUS_SUCCESS) {
4217 status = SWITCH_STATUS_FALSE;
4218 }
4219
4220 if (bleg && *bleg) {
4221 switch_channel_t *bchan = switch_core_session_get_channel(*bleg);
4222
4223 if (session && caller_channel) {
4224 switch_caller_profile_t *cloned_profile, *peer_profile = switch_channel_get_caller_profile(switch_core_session_get_channel(*bleg));
4225
4226 if (peer_profile) {
4227 if ((cloned_profile = switch_caller_profile_clone(session, peer_profile)) != 0) {
4228 switch_channel_set_originatee_caller_profile(caller_channel, cloned_profile);
4229 }
4230 }
4231
4232 switch_channel_set_variable(caller_channel, SWITCH_SIGNAL_BOND_VARIABLE, switch_core_session_get_uuid(*bleg));
4233 // Now main SWITCH_SIGNAL_BOND_VARIABLE is populated, don't need this one anymore...
4234 switch_channel_set_variable(caller_channel, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE, NULL);
4235 }
4236
4237
4238 switch_channel_execute_on(bchan, SWITCH_CHANNEL_EXECUTE_ON_POST_ORIGINATE_VARIABLE);
4239 switch_channel_api_on(bchan, SWITCH_CHANNEL_API_ON_POST_ORIGINATE_VARIABLE);
4240
4241
4242 while(switch_channel_state_change_pending(bchan)) {
4243 switch_cond_next();
4244 }
4245
4246 switch_channel_audio_sync(bchan);
4247
4248 if (caller_channel) {
4249 switch_channel_audio_sync(caller_channel);
4250 }
4251 }
4252
4253 if (oglobals.session) {
4254 switch_ivr_parse_all_events(oglobals.session);
4255 }
4256
4257 if (oglobals.session && status == SWITCH_STATUS_SUCCESS) {
4258 switch_ivr_sleep(oglobals.session, 0, SWITCH_TRUE, NULL);
4259 }
4260
4261 if (var_event && var_event != ovars) {
4262 switch_event_destroy(&var_event);
4263 }
4264
4265 switch_safe_free(write_frame.data);
4266 switch_safe_free(fail_on_single_reject_var);
4267
4268 if (force_reason != SWITCH_CAUSE_NONE) {
4269 *cause = force_reason;
4270 }
4271
4272 if (caller_channel) {
4273
4274 switch_channel_execute_on(caller_channel, SWITCH_CHANNEL_EXECUTE_ON_POST_ORIGINATE_VARIABLE);
4275 switch_channel_api_on(caller_channel, SWITCH_CHANNEL_API_ON_POST_ORIGINATE_VARIABLE);
4276
4277 switch_channel_clear_flag(caller_channel, CF_ORIGINATOR);
4278 switch_channel_clear_flag(caller_channel, CF_XFER_ZOMBIE);
4279
4280 if (hangup_on_single_reject) {
4281 switch_channel_hangup(caller_channel, *cause);
4282 }
4283 }
4284
4285
4286 switch_core_destroy_memory_pool(&oglobals.pool);
4287
4288 return status;
4289 }
4290
4291 SWITCH_DECLARE(switch_status_t) switch_dial_handle_list_create(switch_dial_handle_list_t **hl)
4292 {
4293 switch_dial_handle_list_t *hlP = NULL;
4294 switch_memory_pool_t *pool = NULL;
4295
4296 switch_core_new_memory_pool(&pool);
4297 switch_assert(pool);
4298
4299 hlP = switch_core_alloc(pool, sizeof(*hlP));
4300 switch_assert(hlP);
4301
4302 hlP->pool = pool;
4303
4304 *hl = hlP;
4305
4306 return SWITCH_STATUS_SUCCESS;
4307 }
4308
4309 static switch_status_t switch_dial_handle_list_add_handle(switch_dial_handle_list_t *hl, switch_dial_handle_t *handle)
4310 {
4311 if (hl->handle_idx < MAX_PEERS && handle) {
4312 hl->handles[hl->handle_idx++] = handle;
4313 return SWITCH_STATUS_SUCCESS;
4314 }
4315 return SWITCH_STATUS_FALSE;
4316 }
4317
4318 SWITCH_DECLARE(switch_status_t) switch_dial_handle_list_create_handle(switch_dial_handle_list_t *hl, switch_dial_handle_t **handle)
4319 {
4320 switch_dial_handle_t *hp = NULL;
4321 if (hl->handle_idx < MAX_PEERS && switch_dial_handle_create(&hp) == SWITCH_STATUS_SUCCESS && hp) {
4322 hl->handles[hl->handle_idx++] = hp;
4323 *handle = hp;
4324 return SWITCH_STATUS_SUCCESS;
4325 }
4326 return SWITCH_STATUS_FALSE;
4327 }
4328
4329 SWITCH_DECLARE(void) switch_dial_handle_list_destroy(switch_dial_handle_list_t **hl)
4330 {
4331 switch_dial_handle_list_t *hlP = *hl;
4332 switch_memory_pool_t *pool = NULL;
4333
4334 *hl = NULL;
4335
4336 if (hlP) {
4337 int i;
4338 for (i = 0; i < hlP->handle_idx; i++) {
4339 switch_dial_handle_destroy(&hlP->handles[i]);
4340 }
4341
4342 switch_event_destroy(&hlP->global_vars);
4343 pool = hlP->pool;
4344 hlP = NULL;
4345 switch_core_destroy_memory_pool(&pool);
4346 }
4347 }
4348
4349 SWITCH_DECLARE(void) switch_dial_handle_list_add_global_var(switch_dial_handle_list_t *hl, const char *var, const char *val)
4350 {
4351 switch_assert(hl);
4352
4353 if (!hl->global_vars) {
4354 switch_event_create_plain(&hl->global_vars, SWITCH_EVENT_CHANNEL_DATA);
4355 }
4356
4357 switch_event_add_header_string(hl->global_vars, SWITCH_STACK_BOTTOM, var, val);
4358 }
4359
4360 SWITCH_DECLARE(void) switch_dial_handle_list_add_global_var_printf(switch_dial_handle_list_t *hl, const char *var, const char *fmt, ...)
4361 {
4362 int ret = 0;
4363 char *data = NULL;
4364 va_list ap;
4365
4366 va_start(ap, fmt);
4367 ret = switch_vasprintf(&data, fmt, ap);
4368 va_end(ap);
4369
4370 if (ret == -1) {
4371 abort();
4372 }
4373
4374 switch_dial_handle_list_add_global_var(hl, var, data);
4375 free(data);
4376 }
4377
4378 static switch_status_t switch_dial_handle_dup(switch_dial_handle_t **handle, switch_dial_handle_t *todup)
4379 {
4380 int i;
4381 switch_dial_handle_t *hp;
4382
4383 if (!todup || !handle) {
4384 return SWITCH_STATUS_FALSE;
4385 }
4386
4387 *handle = NULL;
4388
4389 switch_dial_handle_create(&hp);
4390 switch_assert(hp);
4391
4392 for (i = 0; i < todup->leg_list_idx; i++) {
4393 int j;
4394 switch_dial_leg_list_t *ll_todup = todup->leg_lists[i];
4395 switch_dial_leg_list_t *ll = NULL;
4396 switch_dial_handle_add_leg_list(hp, &ll);
4397 for (j = 0; j < ll_todup->leg_idx; j++) {
4398 switch_dial_leg_t *leg;
4399 switch_dial_leg_t *leg_todup = ll_todup->legs[j];
4400 switch_dial_leg_list_add_leg(ll, &leg, leg_todup->dial_string);
4401 if (leg_todup->leg_vars) {
4402 switch_event_dup(&leg->leg_vars, leg_todup->leg_vars);
4403 }
4404 }
4405 }
4406
4407 if (todup->global_vars) {
4408 switch_event_dup(&hp->global_vars, todup->global_vars);
4409 }
4410
4411 hp->is_sub = todup->is_sub;
4412
4413 *handle = hp;
4414
4415 return SWITCH_STATUS_SUCCESS;
4416 }
4417
4418 SWITCH_DECLARE(switch_status_t) switch_dial_handle_create(switch_dial_handle_t **handle)
4419 {
4420 switch_dial_handle_t *hp;
4421 switch_memory_pool_t *pool = NULL;
4422
4423 switch_core_new_memory_pool(&pool);
4424 switch_assert(pool);
4425
4426 hp = switch_core_alloc(pool, sizeof(*hp));
4427 switch_assert(hp);
4428
4429 hp->pool = pool;
4430
4431 *handle = hp;
4432
4433 return SWITCH_STATUS_SUCCESS;
4434 }
4435
4436 SWITCH_DECLARE(void) switch_dial_handle_destroy(switch_dial_handle_t **handle)
4437 {
4438 switch_dial_handle_t *hp = *handle;
4439 switch_memory_pool_t *pool = NULL;
4440
4441 *handle = NULL;
4442
4443 if (hp) {
4444 int i, j;
4445
4446 for (i = 0; i < hp->leg_list_idx; i++) {
4447 for(j = 0; j < hp->leg_lists[i]->leg_idx; j++) {
4448 switch_event_destroy(&hp->leg_lists[i]->legs[j]->leg_vars);
4449 }
4450 }
4451
4452 switch_event_destroy(&hp->global_vars);
4453 pool = hp->pool;
4454 hp = NULL;
4455 switch_core_destroy_memory_pool(&pool);
4456 }
4457 }
4458
4459 SWITCH_DECLARE(void) switch_dial_handle_add_leg_list(switch_dial_handle_t *handle, switch_dial_leg_list_t **leg_listP)
4460 {
4461 switch_dial_leg_list_t *leg_list;
4462
4463 switch_assert(handle);
4464
4465 leg_list = switch_core_alloc(handle->pool, sizeof(*leg_list));
4466 leg_list->handle = handle;
4467
4468 handle->leg_lists[handle->leg_list_idx++] = leg_list;
4469
4470 *leg_listP = leg_list;
4471 }
4472
4473 SWITCH_DECLARE(void) switch_dial_leg_list_add_leg_printf(switch_dial_leg_list_t *parent, switch_dial_leg_t **legP, const char *fmt, ...)
4474 {
4475 int ret = 0;
4476 char *data = NULL;
4477 va_list ap;
4478
4479 va_start(ap, fmt);
4480 ret = switch_vasprintf(&data, fmt, ap);
4481 va_end(ap);
4482
4483 if (ret == -1) {
4484 abort();
4485 }
4486
4487 switch_dial_leg_list_add_leg(parent, legP, data);
4488 free(data);
4489 }
4490
4491 SWITCH_DECLARE(void) switch_dial_leg_list_add_leg(switch_dial_leg_list_t *parent, switch_dial_leg_t **legP, const char *dial_string)
4492 {
4493 switch_dial_leg_t *leg;
4494
4495 switch_assert(parent);
4496
4497 leg = switch_core_alloc(parent->handle->pool, sizeof(*leg));
4498 leg->handle = parent->handle;
4499 leg->dial_string = switch_core_strdup(parent->handle->pool, dial_string);
4500
4501 parent->legs[parent->leg_idx++] = leg;
4502
4503 if (legP) {
4504 *legP = leg;
4505 }
4506 }
4507
4508 SWITCH_DECLARE(void) switch_dial_handle_add_global_var(switch_dial_handle_t *handle, const char *var, const char *val)
4509 {
4510 switch_assert(handle);
4511
4512 if (!handle->global_vars) {
4513 switch_event_create_plain(&handle->global_vars, SWITCH_EVENT_CHANNEL_DATA);
4514 }
4515
4516 switch_event_add_header_string(handle->global_vars, SWITCH_STACK_BOTTOM, var, val);
4517 }
4518
4519 SWITCH_DECLARE(void) switch_dial_handle_add_global_var_printf(switch_dial_handle_t *handle, const char *var, const char *fmt, ...)
4520 {
4521 int ret = 0;
4522 char *data = NULL;
4523 va_list ap;
4524
4525 va_start(ap, fmt);
4526 ret = switch_vasprintf(&data, fmt, ap);
4527 va_end(ap);
4528
4529 if (ret == -1) {
4530 abort();
4531 }
4532
4533 switch_dial_handle_add_global_var(handle, var, data);
4534 free(data);
4535 }
4536
4537 SWITCH_DECLARE(switch_status_t) switch_dial_handle_add_leg_var(switch_dial_leg_t *leg, const char *var, const char *val)
4538 {
4539 if (!leg) return SWITCH_STATUS_NOTFOUND;
4540
4541 if (!leg->leg_vars) {
4542 switch_event_create_plain(&leg->leg_vars, SWITCH_EVENT_CHANNEL_DATA);
4543 }
4544
4545 switch_event_add_header_string(leg->leg_vars, SWITCH_STACK_BOTTOM, var, val);
4546
4547 return SWITCH_STATUS_SUCCESS;
4548
4549 }
4550
4551 SWITCH_DECLARE(switch_status_t) switch_dial_handle_add_leg_var_printf(switch_dial_leg_t *leg, const char *var, const char *fmt, ...)
4552 {
4553 int ret = 0;
4554 char *data = NULL;
4555 va_list ap;
4556 switch_status_t status;
4557
4558 va_start(ap, fmt);
4559 ret = switch_vasprintf(&data, fmt, ap);
4560 va_end(ap);
4561
4562 if (ret == -1) {
4563 abort();
4564 }
4565
4566 status = switch_dial_handle_add_leg_var(leg, var, data);
4567
4568 free(data);
4569
4570 return status;
4571 }
4572
4573 SWITCH_DECLARE(int) switch_dial_handle_get_total(switch_dial_handle_t *handle)
4574 {
4575 return handle->leg_list_idx;
4576 }
4577
4578 SWITCH_DECLARE(int) switch_dial_handle_get_peers(switch_dial_handle_t *handle, int idx, char **array, int max)
4579 {
4580 int i, j = 0;
4581
4582 if (!handle->leg_lists[idx]) return 0;
4583
4584 for (i = 0; i < max && handle->leg_lists[idx]->legs[i]; i++) {
4585 array[j++] = handle->leg_lists[idx]->legs[i]->dial_string;
4586 }
4587
4588 return j;
4589 }
4590
4591
4592 SWITCH_DECLARE(int) switch_dial_handle_get_vars(switch_dial_handle_t *handle, int idx, switch_event_t **array, int max)
4593 {
4594 int i, j = 0;
4595
4596 if (!handle->leg_lists[idx]) return 0;
4597
4598 for (i = 0; i < max && handle->leg_lists[idx]->legs[i]; i++) {
4599 array[j++] = handle->leg_lists[idx]->legs[i]->leg_vars;
4600 }
4601
4602 return j;
4603 }
4604
4605
4606 SWITCH_DECLARE(switch_event_t *) switch_dial_handle_get_global_vars(switch_dial_handle_t *handle)
4607 {
4608 switch_assert(handle);
4609
4610 return handle->global_vars;
4611 }
4612
4613 SWITCH_DECLARE(switch_event_t *) switch_dial_leg_get_vars(switch_dial_leg_t *leg)
4614 {
4615 switch_assert(leg);
4616
4617 return leg->leg_vars;
4618 }
4619
4620 SWITCH_DECLARE(const char *) switch_dial_leg_get_var(switch_dial_leg_t *leg, const char *key)
4621 {
4622 switch_assert(leg);
4623
4624 return switch_event_get_header(leg->leg_vars, key);
4625 }
4626
4627 static switch_status_t vars_serialize_json_obj(switch_event_t *event, cJSON **json)
4628 {
4629 switch_event_header_t *hp;
4630 *json = cJSON_CreateObject();
4631 for (hp = event->headers; hp; hp = hp->next) {
4632 if (hp->name && hp->value) {
4633 cJSON_AddItemToObject(*json, hp->name, cJSON_CreateString(hp->value));
4634 }
4635 }
4636 return SWITCH_STATUS_SUCCESS;
4637 }
4638
4639
4640 static switch_status_t leg_serialize_json_obj(switch_dial_leg_t *leg, cJSON **json)
4641 {
4642 cJSON *vars_json = NULL;
4643 *json = cJSON_CreateObject();
4644 if (leg->dial_string) {
4645 cJSON_AddStringToObject(*json, "dial_string", leg->dial_string);
4646 }
4647 if (leg->leg_vars && vars_serialize_json_obj(leg->leg_vars, &vars_json) == SWITCH_STATUS_SUCCESS && vars_json) {
4648 cJSON_AddItemToObject(*json, "vars", vars_json);
4649 }
4650 return SWITCH_STATUS_SUCCESS;
4651 }
4652
4653
4654 static switch_status_t leg_list_serialize_json_obj(switch_dial_leg_list_t *ll, cJSON **json)
4655 {
4656 int i;
4657 cJSON *legs_json = cJSON_CreateArray();
4658 *json = cJSON_CreateObject();
4659 cJSON_AddItemToObject(*json, "legs", legs_json);
4660 for (i = 0; i < ll->leg_idx; i++) {
4661 switch_dial_leg_t *leg = ll->legs[i];
4662 cJSON *leg_json = NULL;
4663 if (leg_serialize_json_obj(leg, &leg_json) == SWITCH_STATUS_SUCCESS && leg_json) {
4664 cJSON_AddItemToArray(legs_json, leg_json);
4665 }
4666 }
4667 return SWITCH_STATUS_SUCCESS;
4668 }
4669
4670
4671 SWITCH_DECLARE(switch_status_t) switch_dial_handle_serialize_json_obj(switch_dial_handle_t *handle, cJSON **json)
4672 {
4673 int i;
4674 cJSON *global_vars_json = NULL;
4675 cJSON *leg_lists_json = NULL;
4676 if (!handle) {
4677 return SWITCH_STATUS_FALSE;
4678 }
4679 *json = cJSON_CreateObject();
4680 if (handle->global_vars && vars_serialize_json_obj(handle->global_vars, &global_vars_json) == SWITCH_STATUS_SUCCESS && global_vars_json) {
4681 cJSON_AddItemToObject(*json, "vars", global_vars_json);
4682 }
4683
4684 leg_lists_json = cJSON_CreateArray();
4685 cJSON_AddItemToObject(*json, "leg_lists", leg_lists_json);
4686 for (i = 0; i < handle->leg_list_idx; i++) {
4687 switch_dial_leg_list_t *ll = handle->leg_lists[i];
4688 cJSON *leg_list_json = NULL;
4689 if (leg_list_serialize_json_obj(ll, &leg_list_json) == SWITCH_STATUS_SUCCESS && leg_list_json) {
4690 cJSON_AddItemToArray(leg_lists_json, leg_list_json);
4691 }
4692 }
4693
4694 return SWITCH_STATUS_SUCCESS;
4695 }
4696
4697
4698 SWITCH_DECLARE(switch_status_t) switch_dial_handle_serialize_json(switch_dial_handle_t *handle, char **str)
4699 {
4700 cJSON *json = NULL;
4701 if (switch_dial_handle_serialize_json_obj(handle, &json) == SWITCH_STATUS_SUCCESS && json) {
4702 *str = cJSON_PrintUnformatted(json);
4703 cJSON_Delete(json);
4704 return SWITCH_STATUS_SUCCESS;
4705 }
4706 return SWITCH_STATUS_FALSE;
4707 }
4708
4709
4710 SWITCH_DECLARE(switch_status_t) switch_dial_handle_create_json_obj(switch_dial_handle_t **handle, cJSON *json)
4711 {
4712 cJSON *vars_json = NULL;
4713 cJSON *var_json = NULL;
4714 cJSON *leg_lists_json = NULL;
4715 if (!json) {
4716 return SWITCH_STATUS_FALSE;
4717 }
4718 switch_dial_handle_create(handle);
4719
4720 leg_lists_json = cJSON_GetObjectItem(json, "leg_lists");
4721 if (leg_lists_json && leg_lists_json->type == cJSON_Array) {
4722 cJSON *leg_list_json = NULL;
4723 cJSON_ArrayForEach(leg_list_json, leg_lists_json) {
4724 cJSON *legs_json = cJSON_GetObjectItem(leg_list_json, "legs");
4725 cJSON *leg_json = NULL;
4726 switch_dial_leg_list_t *ll = NULL;
4727 if (!legs_json || legs_json->type != cJSON_Array) {
4728 continue;
4729 }
4730 switch_dial_handle_add_leg_list(*handle, &ll);
4731 cJSON_ArrayForEach(leg_json, legs_json) {
4732 switch_dial_leg_t *leg = NULL;
4733 const char *dial_string = NULL;
4734 if (!leg_json || leg_json->type != cJSON_Object) {
4735 continue;
4736 }
4737 dial_string = cJSON_GetObjectCstr(leg_json, "dial_string");
4738 if (!dial_string) {
4739 continue;
4740 }
4741 switch_dial_leg_list_add_leg(ll, &leg, dial_string);
4742
4743 vars_json = cJSON_GetObjectItem(leg_json, "vars");
4744 if (vars_json && vars_json->type == cJSON_Object) {
4745 cJSON_ArrayForEach(var_json, vars_json) {
4746 if (!var_json || var_json->type != cJSON_String || !var_json->valuestring || !var_json->string) {
4747 continue;
4748 }
4749 switch_dial_handle_add_leg_var(leg, var_json->string, var_json->valuestring);
4750 }
4751 }
4752 }
4753 }
4754 }
4755
4756 vars_json = cJSON_GetObjectItem(json, "vars");
4757 if (vars_json && vars_json->type == cJSON_Object) {
4758 cJSON_ArrayForEach(var_json, vars_json) {
4759 if (!var_json || var_json->type != cJSON_String || !var_json->valuestring || !var_json->string) {
4760 continue;
4761 }
4762 switch_dial_handle_add_global_var(*handle, var_json->string, var_json->valuestring);
4763 }
4764 }
4765 return SWITCH_STATUS_SUCCESS;
4766 }
4767
4768
4769 SWITCH_DECLARE(switch_status_t) switch_dial_handle_create_json(switch_dial_handle_t **handle, const char *handle_string)
4770 {
4771 switch_status_t status;
4772 cJSON *handle_json = NULL;
4773
4774 if (zstr(handle_string)) {
4775 return SWITCH_STATUS_FALSE;
4776 }
4777
4778 handle_json = cJSON_Parse(handle_string);
4779 if (!handle_json) {
4780 return SWITCH_STATUS_FALSE;
4781 }
4782
4783 status = switch_dial_handle_create_json_obj(handle, handle_json);
4784 cJSON_Delete(handle_json);
4785 return status;
4786 }
4787
4788
4789 SWITCH_DECLARE(switch_status_t) switch_dial_handle_list_serialize_json_obj(switch_dial_handle_list_t *hl, cJSON **json)
4790 {
4791 int i;
4792 cJSON *global_vars_json = NULL;
4793 cJSON *handles_json = NULL;
4794 if (!hl) {
4795 return SWITCH_STATUS_FALSE;
4796 }
4797 *json = cJSON_CreateObject();
4798 if (hl->global_vars && vars_serialize_json_obj(hl->global_vars, &global_vars_json) == SWITCH_STATUS_SUCCESS && global_vars_json) {
4799 cJSON_AddItemToObject(*json, "vars", global_vars_json);
4800 }
4801
4802 handles_json = cJSON_CreateArray();
4803 cJSON_AddItemToObject(*json, "handles", handles_json);
4804 for (i = 0; i < hl->handle_idx; i++) {
4805 switch_dial_handle_t *handle = hl->handles[i];
4806 cJSON *handle_json = NULL;
4807 if (switch_dial_handle_serialize_json_obj(handle, &handle_json) == SWITCH_STATUS_SUCCESS && handle_json) {
4808 cJSON_AddItemToArray(handles_json, handle_json);
4809 }
4810 }
4811
4812 return SWITCH_STATUS_SUCCESS;
4813 }
4814
4815
4816 SWITCH_DECLARE(switch_status_t) switch_dial_handle_list_serialize_json(switch_dial_handle_list_t *hl, char **str)
4817 {
4818 cJSON *json = NULL;
4819 if (switch_dial_handle_list_serialize_json_obj(hl, &json) == SWITCH_STATUS_SUCCESS && json) {
4820 *str = cJSON_PrintUnformatted(json);
4821 cJSON_Delete(json);
4822 return SWITCH_STATUS_SUCCESS;
4823 }
4824 return SWITCH_STATUS_FALSE;
4825 }
4826
4827
4828 SWITCH_DECLARE(switch_status_t) switch_dial_handle_list_create_json_obj(switch_dial_handle_list_t **hl, cJSON *handle_list_json)
4829 {
4830 cJSON *handle_json = NULL;
4831 cJSON *handles_json = NULL;
4832 cJSON *vars_json = NULL;
4833
4834 *hl = NULL;
4835
4836 handles_json = cJSON_GetObjectItem(handle_list_json, "handles");
4837 if (!handles_json || !cJSON_IsArray(handles_json)) {
4838 return SWITCH_STATUS_FALSE;
4839 }
4840 switch_dial_handle_list_create(hl);
4841 switch_assert(*hl);
4842 for (handle_json = handles_json->child; handle_json; handle_json = handle_json->next) {
4843 switch_dial_handle_t *handle = NULL;
4844 if (switch_dial_handle_create_json_obj(&handle, handle_json) == SWITCH_STATUS_SUCCESS && handle) {
4845 if (switch_dial_handle_list_add_handle(*hl, handle) != SWITCH_STATUS_SUCCESS) {
4846 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Not adding remaining dial handles: exceeded limit of %d handles\n", MAX_PEERS);
4847 switch_dial_handle_destroy(&handle);
4848 break;
4849 }
4850 } else {
4851 char *handle_json_str = cJSON_PrintUnformatted(handle_json);
4852 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to create dial handle: %s\n", handle_json_str);
4853 switch_safe_free(handle_json_str);
4854 }
4855 }
4856
4857 if ((*hl)->handle_idx == 0) {
4858 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to create dial handle list: no handles added!\n");
4859 switch_dial_handle_list_destroy(hl);
4860 return SWITCH_STATUS_FALSE;
4861 }
4862
4863 vars_json = cJSON_GetObjectItem(handle_list_json, "vars");
4864 if (vars_json && vars_json->type == cJSON_Object) {
4865 cJSON *var_json = NULL;
4866 cJSON_ArrayForEach(var_json, vars_json) {
4867 if (!var_json || var_json->type != cJSON_String || !var_json->valuestring || !var_json->string) {
4868 continue;
4869 }
4870 switch_dial_handle_list_add_global_var(*hl, var_json->string, var_json->valuestring);
4871 }
4872 }
4873
4874 return SWITCH_STATUS_SUCCESS;
4875 }
4876
4877
4878 SWITCH_DECLARE(switch_status_t) switch_dial_handle_list_create_json(switch_dial_handle_list_t **hl, const char *handle_list_string)
4879 {
4880 switch_status_t status;
4881 cJSON *handle_list_json = NULL;
4882
4883 if (zstr(handle_list_string)) {
4884 return SWITCH_STATUS_FALSE;
4885 }
4886
4887 handle_list_json = cJSON_Parse(handle_list_string);
4888 if (!handle_list_json) {
4889 return SWITCH_STATUS_FALSE;
4890 }
4891
4892 status = switch_dial_handle_list_create_json_obj(hl, handle_list_json);
4893 cJSON_Delete(handle_list_json);
4894 return status;
4895 }
4896
4897
4898 static switch_status_t o_bridge_on_dtmf(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen)
4899 {
4900 char *str = (char *) buf;
4901
4902 if (str && input && itype == SWITCH_INPUT_TYPE_DTMF) {
4903 switch_dtmf_t *dtmf = (switch_dtmf_t *) input;
4904 if (strchr(str, dtmf->digit)) {
4905 return SWITCH_STATUS_BREAK;
4906 }
4907 }
4908 return SWITCH_STATUS_SUCCESS;
4909 }
4910
4911
4912 SWITCH_DECLARE(switch_status_t) switch_ivr_enterprise_orig_and_bridge(switch_core_session_t *session, const char *data, switch_dial_handle_list_t *hl, switch_call_cause_t *cause)
4913 {
4914 switch_channel_t *caller_channel = switch_core_session_get_channel(session);
4915 switch_core_session_t *peer_session = NULL;
4916 switch_status_t status = SWITCH_STATUS_FALSE;
4917 int fail = 0;
4918
4919 if ((status = switch_ivr_enterprise_originate(session,
4920 &peer_session,
4921 cause, data, 0, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, hl)) != SWITCH_STATUS_SUCCESS) {
4922 fail = 1;
4923 }
4924
4925
4926 if (fail) {
4927 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Originate Failed. Cause: %s\n", switch_channel_cause2str(*cause));
4928
4929 switch_channel_set_variable(caller_channel, "originate_failed_cause", switch_channel_cause2str(*cause));
4930
4931 switch_channel_handle_cause(caller_channel, *cause);
4932
4933 return status;
4934 } else {
4935 switch_channel_t *peer_channel = switch_core_session_get_channel(peer_session);
4936
4937 if (switch_true(switch_channel_get_variable(caller_channel, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE)) ||
4938 switch_true(switch_channel_get_variable(peer_channel, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE))) {
4939 switch_channel_set_flag(caller_channel, CF_BYPASS_MEDIA_AFTER_BRIDGE);
4940 }
4941
4942 if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE)) {
4943 switch_ivr_signal_bridge(session, peer_session);
4944 } else {
4945 char *a_key = (char *) switch_channel_get_variable(caller_channel, "bridge_terminate_key");
4946 char *b_key = (char *) switch_channel_get_variable(peer_channel, "bridge_terminate_key");
4947 int ok = 0;
4948 switch_input_callback_function_t func = NULL;
4949
4950 if (a_key) {
4951 a_key = switch_core_session_strdup(session, a_key);
4952 ok++;
4953 }
4954 if (b_key) {
4955 b_key = switch_core_session_strdup(session, b_key);
4956 ok++;
4957 }
4958 if (ok) {
4959 func = o_bridge_on_dtmf;
4960 } else {
4961 a_key = NULL;
4962 b_key = NULL;
4963 }
4964
4965 switch_ivr_multi_threaded_bridge(session, peer_session, func, a_key, b_key);
4966 }
4967
4968 switch_core_session_rwunlock(peer_session);
4969 }
4970
4971 return status;
4972 }
4973
4974
4975 SWITCH_DECLARE(switch_status_t) switch_ivr_orig_and_bridge(switch_core_session_t *session, const char *data, switch_dial_handle_t *dh, switch_call_cause_t *cause)
4976 {
4977 switch_channel_t *caller_channel = switch_core_session_get_channel(session);
4978 switch_core_session_t *peer_session = NULL;
4979 switch_status_t status = SWITCH_STATUS_FALSE;
4980 int fail = 0;
4981
4982 if ((status = switch_ivr_originate(session,
4983 &peer_session,
4984 cause, data, 0, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, dh)) != SWITCH_STATUS_SUCCESS) {
4985 fail = 1;
4986 }
4987
4988
4989 if (fail) {
4990 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Originate Failed. Cause: %s\n", switch_channel_cause2str(*cause));
4991
4992 switch_channel_set_variable(caller_channel, "originate_failed_cause", switch_channel_cause2str(*cause));
4993
4994 switch_channel_handle_cause(caller_channel, *cause);
4995
4996 return status;
4997 } else {
4998 switch_channel_t *peer_channel = switch_core_session_get_channel(peer_session);
4999
5000 if (switch_true(switch_channel_get_variable(caller_channel, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE)) ||
5001 switch_true(switch_channel_get_variable(peer_channel, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE))) {
5002 switch_channel_set_flag(caller_channel, CF_BYPASS_MEDIA_AFTER_BRIDGE);
5003 }
5004
5005 if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE)) {
5006 switch_ivr_signal_bridge(session, peer_session);
5007 } else {
5008 char *a_key = (char *) switch_channel_get_variable(caller_channel, "bridge_terminate_key");
5009 char *b_key = (char *) switch_channel_get_variable(peer_channel, "bridge_terminate_key");
5010 int ok = 0;
5011 switch_input_callback_function_t func = NULL;
5012
5013 if (a_key) {
5014 a_key = switch_core_session_strdup(session, a_key);
5015 ok++;
5016 }
5017 if (b_key) {
5018 b_key = switch_core_session_strdup(session, b_key);
5019 ok++;
5020 }
5021 if (ok) {
5022 func = o_bridge_on_dtmf;
5023 } else {
5024 a_key = NULL;
5025 b_key = NULL;
5026 }
5027
5028 switch_ivr_multi_threaded_bridge(session, peer_session, func, a_key, b_key);
5029 }
5030
5031 switch_core_session_rwunlock(peer_session);
5032 }
5033
5034 return status;
5035 }
5036
5037
5038
5039 /* For Emacs:
5040 * Local Variables:
5041 * mode:c
5042 * indent-tabs-mode:t
5043 * tab-width:4
5044 * c-basic-offset:4
5045 * End:
5046 * For VIM:
5047 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
5048 */