diff --git a/Makefile.am b/Makefile.am index ef9a2ef712..1c50d05e0e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -58,6 +58,7 @@ src/switch_stun.c\ src/switch_log.c\ src/switch_xml.c\ libs/stfu/stfu.c\ +src/switch_cpp.cpp\ libs/libteletone/src/libteletone_detect.c\ libs/libteletone/src/libteletone_generate.c @@ -90,6 +91,7 @@ src/include/switch_rtp.h\ src/include/switch_stun.h\ src/include/switch_log.h\ src/include/switch_xml.h\ +src/include/switch_cpp.h\ libs/libteletone/src/libteletone_detect.h\ libs/libteletone/src/libteletone_generate.h\ libs/libteletone/src/libteletone.h diff --git a/src/include/switch_cpp.h b/src/include/switch_cpp.h new file mode 100644 index 0000000000..0d6f4bb605 --- /dev/null +++ b/src/include/switch_cpp.h @@ -0,0 +1,55 @@ +#ifndef SWITCH_CPP_H +#define SWITCH_CPP_H + +SWITCH_BEGIN_EXTERN_C +#include + +void console_log(char *level_str, char *msg); +void console_clean_log(char *msg); +char *api_execute(char *cmd, char *arg); +void api_reply_delete(char *reply); + +class CoreSession { + private: + char *uuid; + char *tts_name; + char *voice_name; + switch_input_args_t args; + switch_input_args_t *ap; + public: + CoreSession(char *uuid); + CoreSession(switch_core_session_t *session); + ~CoreSession(); + switch_core_session_t *session; + switch_channel_t *channel; + int answer(); + int preAnswer(); + void hangup(char *cause); + void setVariable(char *var, char *val); + void getVariable(char *var, char *val); + int playFile(char *file, char *timer_name); + void setDTMFCallback(switch_input_callback_function_t cb, void *buf, uint32_t buflen); + int speakText(char *text); + void set_tts_parms(char *tts_name, char *voice_name); + int getDigits(char *dtmf_buf, int len, char *terminators, char *terminator, int timeout); + int transfer(char *extensions, char *dialplan, char *context); + int playAndgetDigits(int min_digits, int max_digits, int max_tries, int timeout, char *terminators, + char *audio_files, char *bad_input_audio_files, char *dtmf_buf, char *digits_regex); + void execute(char *app, char *data); + protected: +}; + + +SWITCH_END_EXTERN_C +#endif +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab: + */ + diff --git a/src/switch_cpp.cpp b/src/switch_cpp.cpp new file mode 100644 index 0000000000..0236c890d3 --- /dev/null +++ b/src/switch_cpp.cpp @@ -0,0 +1,163 @@ +#include +#include + +#define sanity_check(x) do { if (!session) { switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "session is not initalized\n"); return x;}} while(0) +#define init_vars() do { session = NULL; channel = NULL; uuid = NULL; tts_name = NULL; voice_name = NULL; memset(&args, 0, sizeof(args)); ap = NULL;} while(0) + +CoreSession::CoreSession(char *nuuid) +{ + init_vars(); + uuid = strdup(nuuid); + if (session = switch_core_session_locate(uuid)) { + channel = switch_core_session_get_channel(session); + } +} + +CoreSession::CoreSession(switch_core_session_t *session) +{ + init_vars(); + channel = switch_core_session_get_channel(session); + switch_core_session_read_lock(session); +} + +CoreSession::~CoreSession() +{ + + if (session) { + switch_core_session_rwunlock(session); + } + + switch_safe_free(uuid); + switch_safe_free(tts_name); + switch_safe_free(voice_name); +} + +int CoreSession::answer() +{ + switch_status_t status; + + sanity_check(-1); + status = switch_channel_answer(channel); + return status == SWITCH_STATUS_SUCCESS ? 1 : 0; +} + +int CoreSession::preAnswer() +{ + switch_status_t status; + sanity_check(-1); + switch_channel_pre_answer(channel); + return status == SWITCH_STATUS_SUCCESS ? 1 : 0; +} + +void CoreSession::hangup(char *cause) +{ + sanity_check(); + switch_channel_hangup(channel, switch_channel_str2cause(cause)); +} + +void CoreSession::setVariable(char *var, char *val) +{ + sanity_check(); + switch_channel_set_variable(channel, var, val); +} + +void CoreSession::getVariable(char *var, char *val) +{ + sanity_check(); + switch_channel_get_variable(channel, var); +} + +void CoreSession::execute(char *app, char *data) +{ + const switch_application_interface_t *application_interface; + sanity_check(); + + if ((application_interface = switch_loadable_module_get_application_interface(app))) { + switch_core_session_exec(session, application_interface, data); + } +} + +int CoreSession::playFile(char *file, char *timer_name) +{ + switch_status_t status; + sanity_check(-1); + if (switch_strlen_zero(timer_name)) { + timer_name = NULL; + } + + status = switch_ivr_play_file(session, NULL, file, ap); + return status == SWITCH_STATUS_SUCCESS ? 1 : 0; +} + +void CoreSession::setDTMFCallback(switch_input_callback_function_t cb, void *buf, uint32_t buflen) +{ + sanity_check(); + if (cb) { + args.buf = buf; + args.buflen = buflen; + args.input_callback = cb; + ap = &args; + } else { + memset(&args, 0, sizeof(args)); + ap = NULL; + } +} + +int CoreSession::speakText(char *text) +{ + switch_status_t status; + switch_codec_t *codec; + + sanity_check(-1); + codec = switch_core_session_get_read_codec(session); + status = switch_ivr_speak_text(session, tts_name, voice_name, codec->implementation->samples_per_second, text, ap); + return status == SWITCH_STATUS_SUCCESS ? 1 : 0; +} + +void CoreSession::set_tts_parms(char *tts_name_p, char *voice_name_p) +{ + sanity_check(); + switch_safe_free(tts_name); + switch_safe_free(voice_name); + tts_name = strdup(tts_name_p); + voice_name = strdup(voice_name_p); +} + +int CoreSession::getDigits(char *dtmf_buf, int len, char *terminators, char *terminator, int timeout) +{ + switch_status_t status; + sanity_check(-1); + status = switch_ivr_collect_digits_count(session, dtmf_buf,(uint32_t) len,(uint32_t) len, terminators, terminator, (uint32_t) timeout); + return status == SWITCH_STATUS_SUCCESS ? 1 : 0; +} + +int CoreSession::transfer(char *extension, char *dialplan, char *context) +{ + switch_status_t status; + sanity_check(-1); + status = switch_ivr_session_transfer(session, extension, dialplan, context); + return status == SWITCH_STATUS_SUCCESS ? 1 : 0; +} + +int CoreSession::playAndgetDigits(int min_digits, int max_digits, int max_tries, int timeout, char *terminators, + char *audio_files, char *bad_input_audio_files, char *dtmf_buf, char *digits_regex) +{ + switch_status_t status; + sanity_check(-1); + status = switch_play_and_get_digits( session, (uint32_t) min_digits,(uint32_t) max_digits, + (uint32_t) max_tries, (uint32_t) timeout, + terminators, audio_files, bad_input_audio_files, dtmf_buf, 128, digits_regex); + return status == SWITCH_STATUS_SUCCESS ? 1 : 0; +} + + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab: + */