From 747e0905d2e734c46c406472969ebbf3d1a42879 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 4 Mar 2008 18:55:16 +0000 Subject: [PATCH] add read app git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@7784 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_ivr.h | 10 +++ .../applications/mod_dptools/mod_dptools.c | 65 +++++++++++++++++++ src/switch_ivr.c | 2 +- src/switch_ivr_play_say.c | 61 ++++++++++++++++- 4 files changed, 136 insertions(+), 2 deletions(-) diff --git a/src/include/switch_ivr.h b/src/include/switch_ivr.h index 75d12c7167..99bac572eb 100644 --- a/src/include/switch_ivr.h +++ b/src/include/switch_ivr.h @@ -735,6 +735,16 @@ SWITCH_DECLARE(void) switch_ivr_intercept_session(switch_core_session_t *session SWITCH_DECLARE(void) switch_ivr_park_session(switch_core_session_t *session); SWITCH_DECLARE(switch_status_t) switch_ivr_wait_for_answer(switch_core_session_t *session, switch_core_session_t *peer_session); +SWITCH_DECLARE(switch_status_t) switch_ivr_read(switch_core_session_t *session, + uint32_t min_digits, + uint32_t max_digits, + const char *prompt_audio_file, + const char *var_name, + char *digit_buffer, + switch_size_t digit_buffer_length, + uint32_t timeout, + const char *valid_terminators); + /** @} */ SWITCH_END_EXTERN_C diff --git a/src/mod/applications/mod_dptools/mod_dptools.c b/src/mod/applications/mod_dptools/mod_dptools.c index 35973a74f8..ec3ccf2d24 100644 --- a/src/mod/applications/mod_dptools/mod_dptools.c +++ b/src/mod/applications/mod_dptools/mod_dptools.c @@ -1057,6 +1057,70 @@ SWITCH_STANDARD_APP(speak_function) switch_ivr_speak_text(session, engine, voice, text, &args); } + +SWITCH_STANDARD_APP(read_function) +{ + char *mydata; + char *argv[6] = { 0 }; + int argc; + int32_t min_digits = 0; + int32_t max_digits = 0; + int timeout = 1000; + char digit_buffer[128] = ""; + const char *prompt_audio_file = NULL; + const char *var_name = NULL; + const char *valid_terminators = NULL; + + if (!switch_strlen_zero(data) && (mydata = switch_core_session_strdup(session, data))) { + argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))); + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No file specified.\n"); + return; + } + + min_digits = atoi(argv[0]); + + if (argc > 1) { + max_digits = atoi(argv[1]); + } + + if (argc > 2) { + prompt_audio_file = argv[2]; + } + + if (argc > 3) { + var_name = argv[3]; + } + + if (argc > 4) { + timeout = atoi(argv[4]); + } + + if (argc > 5) { + valid_terminators = argv[5]; + } + + if (min_digits <= 1) { + min_digits = 1; + } + + if (max_digits < min_digits) { + max_digits = min_digits; + } + + if (timeout <= 1000) { + timeout = 1000; + } + + if (switch_strlen_zero(valid_terminators)) { + valid_terminators = "#"; + } + + switch_ivr_read(session, min_digits, max_digits, prompt_audio_file, var_name, digit_buffer, sizeof(digit_buffer), timeout, valid_terminators); + +} + + SWITCH_STANDARD_APP(playback_function) { switch_input_args_t args = { 0 }; @@ -1550,6 +1614,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_dptools_load) SWITCH_ADD_APP(app_interface, "park_state", "Park State", "Park State", park_state_function, "", SAF_NONE); SWITCH_ADD_APP(app_interface, "gentones", "Generate Tones", "Generate tones to the channel", gentones_function, "[|]", SAF_NONE); SWITCH_ADD_APP(app_interface, "playback", "Playback File", "Playback a file to the channel", playback_function, "", SAF_NONE); + SWITCH_ADD_APP(app_interface, "read", "Read Digits", "Read Digits", read_function, " ", SAF_NONE); SWITCH_ADD_APP(app_interface, "stop_record_session", "Stop Record Session", STOP_SESS_REC_DESC, stop_record_session_function, "", SAF_NONE); SWITCH_ADD_APP(app_interface, "record_session", "Record Session", SESS_REC_DESC, record_session_function, "", SAF_NONE); SWITCH_ADD_APP(app_interface, "record", "Record File", "Record a file from the channels input", record_function, " [] [] []", SAF_NONE); diff --git a/src/switch_ivr.c b/src/switch_ivr.c index 4f0d1204cb..bb935f268e 100644 --- a/src/switch_ivr.c +++ b/src/switch_ivr.c @@ -647,7 +647,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_count(switch_core_sess digit_started = switch_timestamp_now(); } - for (y = 0; y < maxdigits; y++) { + for (y = 0; y <= maxdigits; y++) { if (switch_channel_dequeue_dtmf(channel, &dtmf) != SWITCH_STATUS_SUCCESS) { break; } diff --git a/src/switch_ivr_play_say.c b/src/switch_ivr_play_say.c index c50d572e9c..292316f5f5 100644 --- a/src/switch_ivr_play_say.c +++ b/src/switch_ivr_play_say.c @@ -887,7 +887,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess if (args->input_callback) { status = args->input_callback(session, (void *)&dtmf, SWITCH_INPUT_TYPE_DTMF, args->buf, args->buflen); } else { - switch_copy_string((char *) args->buf, (void *)&dtmf, args->buflen); + *((char *)args->buf) = dtmf.digit; status = SWITCH_STATUS_BREAK; } } @@ -1081,6 +1081,65 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess return status; } +SWITCH_DECLARE(switch_status_t) switch_ivr_read(switch_core_session_t *session, + uint32_t min_digits, + uint32_t max_digits, + const char *prompt_audio_file, + const char *var_name, + char *digit_buffer, + switch_size_t digit_buffer_length, + uint32_t timeout, + const char *valid_terminators) +{ + switch_channel_t *channel; + switch_input_args_t args = { 0 }; + switch_status_t status = SWITCH_STATUS_SUCCESS; + char terminator; + int len; + + switch_assert(session); + + channel = switch_core_session_get_channel(session); + + if (digit_buffer_length < min_digits || digit_buffer_length < max_digits) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Buffer too small!\n"); + return SWITCH_STATUS_FALSE; + } + + switch_channel_pre_answer(channel); + + memset(digit_buffer, 0, digit_buffer_length); + args.buf = digit_buffer; + args.buflen = digit_buffer_length; + + if (!switch_strlen_zero(prompt_audio_file)) { + status = switch_ivr_play_file(session, NULL, prompt_audio_file, &args); + } + + if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) { + goto end; + } + + len = strlen(digit_buffer); + + if (len < min_digits && len < max_digits) { + args.buf = digit_buffer + len; + args.buflen = digit_buffer_length - len; + status = switch_ivr_collect_digits_count(session, digit_buffer, digit_buffer_length, max_digits, valid_terminators, &terminator, timeout, 0, 0); + } + + + end: + + if (var_name && !switch_strlen_zero(digit_buffer)) { + switch_channel_set_variable(channel, var_name, digit_buffer); + } + + return status; + +} + + SWITCH_DECLARE(switch_status_t) switch_play_and_get_digits(switch_core_session_t *session, uint32_t min_digits, uint32_t max_digits,