From b96e619d687c745e734ecc394ed1d64cbaa4185d Mon Sep 17 00:00:00 2001 From: Leon de Rooij Date: Tue, 21 Apr 2009 13:52:43 +0200 Subject: [PATCH] freeswitch plugin: Added esl header files and modified freeswitch.c to use it --- src/esl.h | 372 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/esl_config.h | 178 ++++++++++++++++++++++++ src/esl_event.h | 293 +++++++++++++++++++++++++++++++++++++++ src/esl_oop.h | 105 ++++++++++++++ src/esl_threadmutex.h | 58 ++++++++ src/freeswitch.c | 8 +- 6 files changed, 1008 insertions(+), 6 deletions(-) create mode 100644 src/esl.h create mode 100644 src/esl_config.h create mode 100644 src/esl_event.h create mode 100644 src/esl_oop.h create mode 100644 src/esl_threadmutex.h diff --git a/src/esl.h b/src/esl.h new file mode 100644 index 00000000..ecca1908 --- /dev/null +++ b/src/esl.h @@ -0,0 +1,372 @@ +/* + * Copyright (c) 2007, Anthony Minessale II + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of the original author; nor the names of any contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ESL_H_ +#define _ESL_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* defined(__cplusplus) */ + +#define esl_copy_string(_x, _y, _z) strncpy(_x, _y, _z - 1) +#define esl_set_string(_x, _y) esl_copy_string(_x, _y, sizeof(_x)) + +typedef struct esl_event_header esl_event_header_t; +typedef struct esl_event esl_event_t; + + +typedef enum { + ESL_EVENT_TYPE_PLAIN, + ESL_EVENT_TYPE_XML +} esl_event_type_t; + +#ifdef WIN32 +#define ESL_SEQ_FWHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY +#define ESL_SEQ_FRED FOREGROUND_RED | FOREGROUND_INTENSITY +#define ESL_SEQ_FMAGEN FOREGROUND_BLUE | FOREGROUND_RED +#define ESL_SEQ_FCYAN FOREGROUND_GREEN | FOREGROUND_BLUE +#define ESL_SEQ_FGREEN FOREGROUND_GREEN +#define ESL_SEQ_FYELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY +#define ESL_SEQ_DEFAULT_COLOR ESL_SEQ_FWHITE +#else +#define ESL_SEQ_ESC "\033[" +/* Ansi Control character suffixes */ +#define ESL_SEQ_HOME_CHAR 'H' +#define ESL_SEQ_HOME_CHAR_STR "H" +#define ESL_SEQ_CLEARLINE_CHAR '1' +#define ESL_SEQ_CLEARLINE_CHAR_STR "1" +#define ESL_SEQ_CLEARLINEEND_CHAR "K" +#define ESL_SEQ_CLEARSCR_CHAR0 '2' +#define ESL_SEQ_CLEARSCR_CHAR1 'J' +#define ESL_SEQ_CLEARSCR_CHAR "2J" +#define ESL_SEQ_DEFAULT_COLOR ESL_SEQ_ESC ESL_SEQ_END_COLOR /* Reset to Default fg/bg color */ +#define ESL_SEQ_AND_COLOR ";" /* To add multiple color definitions */ +#define ESL_SEQ_END_COLOR "m" /* To end color definitions */ +/* Foreground colors values */ +#define ESL_SEQ_F_BLACK "30" +#define ESL_SEQ_F_RED "31" +#define ESL_SEQ_F_GREEN "32" +#define ESL_SEQ_F_YELLOW "33" +#define ESL_SEQ_F_BLUE "34" +#define ESL_SEQ_F_MAGEN "35" +#define ESL_SEQ_F_CYAN "36" +#define ESL_SEQ_F_WHITE "37" +/* Background colors values */ +#define ESL_SEQ_B_BLACK "40" +#define ESL_SEQ_B_RED "41" +#define ESL_SEQ_B_GREEN "42" +#define ESL_SEQ_B_YELLOW "43" +#define ESL_SEQ_B_BLUE "44" +#define ESL_SEQ_B_MAGEN "45" +#define ESL_SEQ_B_CYAN "46" +#define ESL_SEQ_B_WHITE "47" +/* Preset escape sequences - Change foreground colors only */ +#define ESL_SEQ_FBLACK ESL_SEQ_ESC ESL_SEQ_F_BLACK ESL_SEQ_END_COLOR +#define ESL_SEQ_FRED ESL_SEQ_ESC ESL_SEQ_F_RED ESL_SEQ_END_COLOR +#define ESL_SEQ_FGREEN ESL_SEQ_ESC ESL_SEQ_F_GREEN ESL_SEQ_END_COLOR +#define ESL_SEQ_FYELLOW ESL_SEQ_ESC ESL_SEQ_F_YELLOW ESL_SEQ_END_COLOR +#define ESL_SEQ_FBLUE ESL_SEQ_ESC ESL_SEQ_F_BLUE ESL_SEQ_END_COLOR +#define ESL_SEQ_FMAGEN ESL_SEQ_ESC ESL_SEQ_F_MAGEN ESL_SEQ_END_COLOR +#define ESL_SEQ_FCYAN ESL_SEQ_ESC ESL_SEQ_F_CYAN ESL_SEQ_END_COLOR +#define ESL_SEQ_FWHITE ESL_SEQ_ESC ESL_SEQ_F_WHITE ESL_SEQ_END_COLOR +#define ESL_SEQ_BBLACK ESL_SEQ_ESC ESL_SEQ_B_BLACK ESL_SEQ_END_COLOR +#define ESL_SEQ_BRED ESL_SEQ_ESC ESL_SEQ_B_RED ESL_SEQ_END_COLOR +#define ESL_SEQ_BGREEN ESL_SEQ_ESC ESL_SEQ_B_GREEN ESL_SEQ_END_COLOR +#define ESL_SEQ_BYELLOW ESL_SEQ_ESC ESL_SEQ_B_YELLOW ESL_SEQ_END_COLOR +#define ESL_SEQ_BBLUE ESL_SEQ_ESC ESL_SEQ_B_BLUE ESL_SEQ_END_COLOR +#define ESL_SEQ_BMAGEN ESL_SEQ_ESC ESL_SEQ_B_MAGEN ESL_SEQ_END_COLOR +#define ESL_SEQ_BCYAN ESL_SEQ_ESC ESL_SEQ_B_CYAN ESL_SEQ_END_COLOR +#define ESL_SEQ_BWHITE ESL_SEQ_ESC ESL_SEQ_B_WHITE ESL_SEQ_END_COLOR +/* Preset escape sequences */ +#define ESL_SEQ_HOME ESL_SEQ_ESC ESL_SEQ_HOME_CHAR_STR +#define ESL_SEQ_CLEARLINE ESL_SEQ_ESC ESL_SEQ_CLEARLINE_CHAR_STR +#define ESL_SEQ_CLEARLINEEND ESL_SEQ_ESC ESL_SEQ_CLEARLINEEND_CHAR +#define ESL_SEQ_CLEARSCR ESL_SEQ_ESC ESL_SEQ_CLEARSCR_CHAR ESL_SEQ_HOME +#endif + +#if !defined(_XOPEN_SOURCE) && !defined(__FreeBSD__) && !defined(__NetBSD__) +#define _XOPEN_SOURCE 600 +#endif + +#ifndef HAVE_STRINGS_H +#define HAVE_STRINGS_H 1 +#endif +#ifndef HAVE_SYS_SOCKET_H +#define HAVE_SYS_SOCKET_H 1 +#endif + +#ifndef __WINDOWS__ +#if defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32) +#define __WINDOWS__ +#endif +#endif + +#ifdef _MSC_VER +#ifndef __inline__ +#define __inline__ __inline +#endif +#if (_MSC_VER >= 1400) /* VC8+ */ +#ifndef _CRT_SECURE_NO_DEPRECATE +#define _CRT_SECURE_NO_DEPRECATE +#endif +#ifndef _CRT_NONSTDC_NO_DEPRECATE +#define _CRT_NONSTDC_NO_DEPRECATE +#endif +#endif +#ifndef strcasecmp +#define strcasecmp(s1, s2) _stricmp(s1, s2) +#endif +#ifndef strncasecmp +#define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n) +#endif +#ifndef snprintf +#define snprintf _snprintf +#endif +#ifndef S_IRUSR +#define S_IRUSR _S_IREAD +#endif +#ifndef S_IWUSR +#define S_IWUSR _S_IWRITE +#endif +#undef HAVE_STRINGS_H +#undef HAVE_SYS_SOCKET_H +#endif + +#include +#ifndef WIN32 +#include +#endif + +#include +#include +#include +#include +#ifndef WIN32 +#include +#include +#include +#include +#endif + +#ifdef HAVE_STRINGS_H +#include +#endif +#include + +#if (_MSC_VER >= 1400) // VC8+ +#define esl_assert(expr) assert(expr);__analysis_assume( expr ) +#endif + +#ifndef esl_assert +#define esl_assert(_x) assert(_x) +#endif + +#define esl_safe_free(_x) if (_x) free(_x); _x = NULL +#define esl_strlen_zero(s) (!s || *(s) == '\0') +#define esl_strlen_zero_buf(s) (*(s) == '\0') + +#ifdef WIN32 +#include +#include +typedef SOCKET esl_socket_t; +typedef unsigned __int64 uint64_t; +typedef unsigned __int32 uint32_t; +typedef unsigned __int16 uint16_t; +typedef unsigned __int8 uint8_t; +typedef __int64 int64_t; +typedef __int32 int32_t; +typedef __int16 int16_t; +typedef __int8 int8_t; +typedef intptr_t esl_ssize_t; +typedef int esl_filehandle_t; +#define ESL_SOCK_INVALID INVALID_SOCKET +#define strerror_r(num, buf, size) strerror_s(buf, size, num) +#if defined(ESL_DECLARE_STATIC) +#define ESL_DECLARE(type) type __stdcall +#define ESL_DECLARE_NONSTD(type) type __cdecl +#define ESL_DECLARE_DATA +#elif defined(ESL_EXPORTS) +#define ESL_DECLARE(type) __declspec(dllexport) type __stdcall +#define ESL_DECLARE_NONSTD(type) __declspec(dllexport) type __cdecl +#define ESL_DECLARE_DATA __declspec(dllexport) +#else +#define ESL_DECLARE(type) __declspec(dllimport) type __stdcall +#define ESL_DECLARE_NONSTD(type) __declspec(dllimport) type __cdecl +#define ESL_DECLARE_DATA __declspec(dllimport) +#endif +#else +#define ESL_DECLARE(type) type +#define ESL_DECLARE_NONSTD(type) type +#define ESL_DECLARE_DATA +#include +#include +#include +#include +#include +#include +#include +#define ESL_SOCK_INVALID -1 +typedef int esl_socket_t; +typedef ssize_t esl_ssize_t; +typedef int esl_filehandle_t; +#endif + +typedef int16_t esl_port_t; + +typedef enum { + ESL_SUCCESS, + ESL_FAIL, + ESL_BREAK, + ESL_DISCONNECTED +} esl_status_t; + +#include + +typedef struct { + struct sockaddr_in sockaddr; + struct hostent hostent; + char hostbuf[256]; + esl_socket_t sock; + char err[256]; + int errnum; + char header_buf[4196]; + char last_reply[1024]; + char last_sr_reply[1024]; + esl_event_t *last_event; + esl_event_t *last_sr_event; + esl_event_t *race_event; + esl_event_t *last_ievent; + esl_event_t *info_event; + int connected; + struct sockaddr_in addr; + esl_mutex_t *mutex; + int async_execute; + int event_lock; +} esl_handle_t; + +typedef enum { + ESL_TRUE = 1, + ESL_FALSE = 0 +} esl_bool_t; + +#ifndef __FUNCTION__ +#define __FUNCTION__ (const char *)__func__ +#endif + +#define ESL_PRE __FILE__, __FUNCTION__, __LINE__ +#define ESL_LOG_LEVEL_DEBUG 7 +#define ESL_LOG_LEVEL_INFO 6 +#define ESL_LOG_LEVEL_NOTICE 5 +#define ESL_LOG_LEVEL_WARNING 4 +#define ESL_LOG_LEVEL_ERROR 3 +#define ESL_LOG_LEVEL_CRIT 2 +#define ESL_LOG_LEVEL_ALERT 1 +#define ESL_LOG_LEVEL_EMERG 0 + +#define ESL_LOG_DEBUG ESL_PRE, ESL_LOG_LEVEL_DEBUG +#define ESL_LOG_INFO ESL_PRE, ESL_LOG_LEVEL_INFO +#define ESL_LOG_NOTICE ESL_PRE, ESL_LOG_LEVEL_NOTICE +#define ESL_LOG_WARNING ESL_PRE, ESL_LOG_LEVEL_WARNING +#define ESL_LOG_ERROR ESL_PRE, ESL_LOG_LEVEL_ERROR +#define ESL_LOG_CRIT ESL_PRE, ESL_LOG_LEVEL_CRIT +#define ESL_LOG_ALERT ESL_PRE, ESL_LOG_LEVEL_ALERT +#define ESL_LOG_EMERG ESL_PRE, ESL_LOG_LEVEL_EMERG +typedef void (*esl_logger_t)(const char *file, const char *func, int line, int level, const char *fmt, ...); + + +ESL_DECLARE(int) esl_vasprintf(char **ret, const char *fmt, va_list ap); + +ESL_DECLARE_DATA extern esl_logger_t esl_log; + +ESL_DECLARE(void) esl_global_set_logger(esl_logger_t logger); +ESL_DECLARE(void) esl_global_set_default_logger(int level); + +#include "esl_event.h" +#include "esl_threadmutex.h" +#include "esl_config.h" + +ESL_DECLARE(size_t) esl_url_encode(const char *url, char *buf, size_t len); +ESL_DECLARE(char *)esl_url_decode(char *s); +ESL_DECLARE(const char *)esl_stristr(const char *instr, const char *str); +ESL_DECLARE(int) esl_toupper(int c); +ESL_DECLARE(int) esl_tolower(int c); +ESL_DECLARE(int) esl_snprintf(char *buffer, size_t count, const char *fmt, ...); + + +typedef void (*esl_listen_callback_t)(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr); + +ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t socket, struct sockaddr_in *addr); +ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback); +ESL_DECLARE(esl_status_t) esl_execute(esl_handle_t *handle, const char *app, const char *arg, const char *uuid); +ESL_DECLARE(esl_status_t) esl_sendevent(esl_handle_t *handle, esl_event_t *event); + +ESL_DECLARE(esl_status_t) esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password); +ESL_DECLARE(esl_status_t) esl_disconnect(esl_handle_t *handle); +ESL_DECLARE(esl_status_t) esl_send(esl_handle_t *handle, const char *cmd); +ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_event_t **save_event); +ESL_DECLARE(esl_status_t) esl_recv_event_timed(esl_handle_t *handle, uint32_t ms, int check_q, esl_event_t **save_event); +ESL_DECLARE(esl_status_t) esl_send_recv(esl_handle_t *handle, const char *cmd); +ESL_DECLARE(esl_status_t) esl_filter(esl_handle_t *handle, const char *header, const char *value); +ESL_DECLARE(esl_status_t) esl_events(esl_handle_t *handle, esl_event_type_t etype, const char *value); + +#define esl_recv(_h) esl_recv_event(_h, 0, NULL) +#define esl_recv_timed(_h, _ms) esl_recv_event_timed(_h, _ms, 0, NULL) + +static __inline__ int esl_safe_strcasecmp(const char *s1, const char *s2) +{ + if (!(s1 && s2)) { + return 1; + } + + return strcasecmp(s1, s2); +} + +#ifdef __cplusplus +} +#endif /* defined(__cplusplus) */ + + +#endif /* defined(_ESL_H_) */ + +/* 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/esl_config.h b/src/esl_config.h new file mode 100644 index 00000000..6763cdb7 --- /dev/null +++ b/src/esl_config.h @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2007, Anthony Minessale II + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of the original author; nor the names of any contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @defgroup config Config File Parser + * @ingroup config + * This module implements a basic interface and file format parser + * + *
+ *
+ * EXAMPLE 
+ * 
+ * [category1]
+ * var1 => val1
+ * var2 => val2
+ * \# lines that begin with \# are comments
+ * \#var3 => val3
+ * 
+ * @{ + */ + +#ifndef ESL_CONFIG_H +#define ESL_CONFIG_H + +#include "esl.h" + +#ifdef __cplusplus +extern "C" { +#endif /* defined(__cplusplus) */ + + +#define ESL_URL_SEPARATOR "://" + + +#ifdef WIN32 +#define ESL_PATH_SEPARATOR "\\" +#ifndef ESL_CONFIG_DIR +#define ESL_CONFIG_DIR "c:\\openesl" +#endif +#define esl_is_file_path(file) (*(file +1) == ':' || *file == '/' || strstr(file, SWITCH_URL_SEPARATOR)) +#else +#define ESL_PATH_SEPARATOR "/" +#ifndef ESL_CONFIG_DIR +#define ESL_CONFIG_DIR "/etc/openesl" +#endif +#define esl_is_file_path(file) ((*file == '/') || strstr(file, SWITCH_URL_SEPARATOR)) +#endif + +/*! + \brief Evaluate the truthfullness of a string expression + \param expr a string expression + \return true or false +*/ +#define esl_true(expr)\ +(expr && ( !strcasecmp(expr, "yes") ||\ +!strcasecmp(expr, "on") ||\ +!strcasecmp(expr, "true") ||\ +!strcasecmp(expr, "enabled") ||\ +!strcasecmp(expr, "active") ||\ +!strcasecmp(expr, "allow") ||\ +atoi(expr))) ? 1 : 0 + +/*! + \brief Evaluate the falsefullness of a string expression + \param expr a string expression + \return true or false +*/ +#define esl_false(expr)\ +(expr && ( !strcasecmp(expr, "no") ||\ +!strcasecmp(expr, "off") ||\ +!strcasecmp(expr, "false") ||\ +!strcasecmp(expr, "disabled") ||\ +!strcasecmp(expr, "inactive") ||\ +!strcasecmp(expr, "disallow") ||\ +!atoi(expr))) ? 1 : 0 + +typedef struct esl_config esl_config_t; + +/*! \brief A simple file handle representing an open configuration file **/ +struct esl_config { + /*! FILE stream buffer to the opened file */ + FILE *file; + /*! path to the file */ + char path[512]; + /*! current category */ + char category[256]; + /*! current section */ + char section[256]; + /*! buffer of current line being read */ + char buf[1024]; + /*! current line number in file */ + int lineno; + /*! current category number in file */ + int catno; + /*! current section number in file */ + int sectno; + + int lockto; +}; + +/*! + \brief Open a configuration file + \param cfg (esl_config_t *) config handle to use + \param file_path path to the file + \return 1 (true) on success 0 (false) on failure +*/ +ESL_DECLARE(int) esl_config_open_file(esl_config_t * cfg, const char *file_path); + +/*! + \brief Close a previously opened configuration file + \param cfg (esl_config_t *) config handle to use +*/ +ESL_DECLARE(void) esl_config_close_file(esl_config_t * cfg); + +/*! + \brief Retrieve next name/value pair from configuration file + \param cfg (esl_config_t *) config handle to use + \param var pointer to aim at the new variable name + \param val pointer to aim at the new value +*/ +ESL_DECLARE(int) esl_config_next_pair(esl_config_t * cfg, char **var, char **val); + +/*! + \brief Retrieve the CAS bits from a configuration string value + \param strvalue pointer to the configuration string value (expected to be in format whatever:xxxx) + \param outbits pointer to aim at the CAS bits +*/ +ESL_DECLARE(int) esl_config_get_cas_bits(char *strvalue, unsigned char *outbits); + + +/** @} */ + +#ifdef __cplusplus +} +#endif /* defined(__cplusplus) */ + +#endif /* defined(ESL_CONFIG_H) */ + +/* 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/esl_event.h b/src/esl_event.h new file mode 100644 index 00000000..d9e7a3e4 --- /dev/null +++ b/src/esl_event.h @@ -0,0 +1,293 @@ +/* + * Copyright (c) 2007, Anthony Minessale II + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of the original author; nor the names of any contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ESL_EVENT_H +#define ESL_EVENT_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* defined(__cplusplus) */ + +typedef enum { + ESL_STACK_BOTTOM, + ESL_STACK_TOP +} esl_stack_t; + +typedef enum { + ESL_EVENT_CUSTOM, + ESL_EVENT_CLONE, + ESL_EVENT_CHANNEL_CREATE, + ESL_EVENT_CHANNEL_DESTROY, + ESL_EVENT_CHANNEL_STATE, + ESL_EVENT_CHANNEL_ANSWER, + ESL_EVENT_CHANNEL_HANGUP, + ESL_EVENT_CHANNEL_EXECUTE, + ESL_EVENT_CHANNEL_EXECUTE_COMPLETE, + ESL_EVENT_CHANNEL_BRIDGE, + ESL_EVENT_CHANNEL_UNBRIDGE, + ESL_EVENT_CHANNEL_PROGRESS, + ESL_EVENT_CHANNEL_PROGRESS_MEDIA, + ESL_EVENT_CHANNEL_OUTGOING, + ESL_EVENT_CHANNEL_PARK, + ESL_EVENT_CHANNEL_UNPARK, + ESL_EVENT_CHANNEL_APPLICATION, + ESL_EVENT_CHANNEL_ORIGINATE, + ESL_EVENT_CHANNEL_UUID, + ESL_EVENT_API, + ESL_EVENT_LOG, + ESL_EVENT_INBOUND_CHAN, + ESL_EVENT_OUTBOUND_CHAN, + ESL_EVENT_STARTUP, + ESL_EVENT_SHUTDOWN, + ESL_EVENT_PUBLISH, + ESL_EVENT_UNPUBLISH, + ESL_EVENT_TALK, + ESL_EVENT_NOTALK, + ESL_EVENT_SESSION_CRASH, + ESL_EVENT_MODULE_LOAD, + ESL_EVENT_MODULE_UNLOAD, + ESL_EVENT_DTMF, + ESL_EVENT_MESSAGE, + ESL_EVENT_PRESENCE_IN, + ESL_EVENT_NOTIFY_IN, + ESL_EVENT_PRESENCE_OUT, + ESL_EVENT_PRESENCE_PROBE, + ESL_EVENT_MESSAGE_WAITING, + ESL_EVENT_MESSAGE_QUERY, + ESL_EVENT_ROSTER, + ESL_EVENT_CODEC, + ESL_EVENT_BACKGROUND_JOB, + ESL_EVENT_DETECTED_SPEECH, + ESL_EVENT_DETECTED_TONE, + ESL_EVENT_PRIVATE_COMMAND, + ESL_EVENT_HEARTBEAT, + ESL_EVENT_TRAP, + ESL_EVENT_ADD_SCHEDULE, + ESL_EVENT_DEL_SCHEDULE, + ESL_EVENT_EXE_SCHEDULE, + ESL_EVENT_RE_SCHEDULE, + ESL_EVENT_RELOADXML, + ESL_EVENT_NOTIFY, + ESL_EVENT_SEND_MESSAGE, + ESL_EVENT_RECV_MESSAGE, + ESL_EVENT_REQUEST_PARAMS, + ESL_EVENT_CHANNEL_DATA, + ESL_EVENT_GENERAL, + ESL_EVENT_COMMAND, + ESL_EVENT_SESSION_HEARTBEAT, + ESL_EVENT_CLIENT_DISCONNECTED, + ESL_EVENT_SERVER_DISCONNECTED, + ESL_EVENT_ALL +} esl_event_types_t; + +typedef enum { + ESL_PRIORITY_NORMAL, + ESL_PRIORITY_LOW, + ESL_PRIORITY_HIGH +} esl_priority_t; + +/*! \brief An event Header */ + struct esl_event_header { + /*! the header name */ + char *name; + /*! the header value */ + char *value; + /*! hash of the header name */ + unsigned long hash; + struct esl_event_header *next; +}; + + +/*! \brief Representation of an event */ +struct esl_event { + /*! the event id (descriptor) */ + esl_event_types_t event_id; + /*! the priority of the event */ + esl_priority_t priority; + /*! the owner of the event */ + char *owner; + /*! the subclass of the event */ + char *subclass_name; + /*! the event headers */ + esl_event_header_t *headers; + /*! the event headers tail pointer */ + esl_event_header_t *last_header; + /*! the body of the event */ + char *body; + /*! user data from the subclass provider */ + void *bind_user_data; + /*! user data from the event sender */ + void *event_user_data; + /*! unique key */ + unsigned long key; + struct esl_event *next; +}; + + + +#define ESL_EVENT_SUBCLASS_ANY NULL + +/*! + \brief Create an event + \param event a NULL pointer on which to create the event + \param event_id the event id enumeration of the desired event + \param subclass_name the subclass name for custom event (only valid when event_id is ESL_EVENT_CUSTOM) + \return ESL_STATUS_SUCCESS on success +*/ +ESL_DECLARE(esl_status_t) esl_event_create_subclass(esl_event_t **event, esl_event_types_t event_id, const char *subclass_name); + +/*! + \brief Set the priority of an event + \param event the event to set the priority on + \param priority the event priority + \return ESL_STATUS_SUCCESS +*/ +ESL_DECLARE(esl_status_t) esl_event_set_priority(esl_event_t *event, esl_priority_t priority); + +/*! + \brief Retrieve a header value from an event + \param event the event to read the header from + \param header_name the name of the header to read + \return the value of the requested header +*/ +ESL_DECLARE(char *)esl_event_get_header(esl_event_t *event, const char *header_name); + +/*! + \brief Retrieve the body value from an event + \param event the event to read the body from + \return the value of the body or NULL +*/ +ESL_DECLARE(char *)esl_event_get_body(esl_event_t *event); + +/*! + \brief Add a header to an event + \param event the event to add the header to + \param stack the stack sense (stack it on the top or on the bottom) + \param header_name the name of the header to add + \param fmt the value of the header (varargs see standard sprintf family) + \return ESL_STATUS_SUCCESS if the header was added +*/ +ESL_DECLARE(esl_status_t) esl_event_add_header(esl_event_t *event, esl_stack_t stack, + const char *header_name, const char *fmt, ...); //PRINTF_FUNCTION(4, 5); + +/*! + \brief Add a string header to an event + \param event the event to add the header to + \param stack the stack sense (stack it on the top or on the bottom) + \param header_name the name of the header to add + \param data the value of the header + \return ESL_STATUS_SUCCESS if the header was added +*/ +ESL_DECLARE(esl_status_t) esl_event_add_header_string(esl_event_t *event, esl_stack_t stack, const char *header_name, const char *data); + +ESL_DECLARE(esl_status_t) esl_event_del_header(esl_event_t *event, const char *header_name); + +/*! + \brief Destroy an event + \param event pointer to the pointer to event to destroy +*/ +ESL_DECLARE(void) esl_event_destroy(esl_event_t **event); +#define esl_event_safe_destroy(_event) if (_event) esl_event_destroy(_event) + +/*! + \brief Duplicate an event + \param event a NULL pointer on which to duplicate the event + \param todup an event to duplicate + \return ESL_STATUS_SUCCESS if the event was duplicated +*/ +ESL_DECLARE(esl_status_t) esl_event_dup(esl_event_t **event, esl_event_t *todup); + +/*! + \brief Render the name of an event id enumeration + \param event the event id to render the name of + \return the rendered name +*/ +ESL_DECLARE(const char *)esl_event_name(esl_event_types_t event); + +/*! + \brief return the event id that matches a given event name + \param name the name of the event + \param type the event id to return + \return ESL_STATUS_SUCCESS if there was a match +*/ +ESL_DECLARE(esl_status_t) esl_name_event(const char *name, esl_event_types_t *type); + +/*! + \brief Render a string representation of an event sutable for printing or network transport + \param event the event to render + \param str a string pointer to point at the allocated data + \param encode url encode the headers + \return ESL_STATUS_SUCCESS if the operation was successful + \note you must free the resulting string when you are finished with it +*/ +ESL_DECLARE(esl_status_t) esl_event_serialize(esl_event_t *event, char **str, esl_bool_t encode); + +/*! + \brief Add a body to an event + \param event the event to add to body to + \param fmt optional body of the event (varargs see standard sprintf family) + \return ESL_STATUS_SUCCESS if the body was added to the event + \note the body parameter can be shadowed by the esl_event_reserve_subclass_detailed function +*/ +ESL_DECLARE(esl_status_t) esl_event_add_body(esl_event_t *event, const char *fmt, ...); + +/*! + \brief Create a new event assuming it will not be custom event and therefore hiding the unused parameters + \param event a NULL pointer on which to create the event + \param id the event id enumeration of the desired event + \return ESL_STATUS_SUCCESS on success +*/ +#define esl_event_create(event, id) esl_event_create_subclass(event, id, ESL_EVENT_SUBCLASS_ANY) + +ESL_DECLARE(const char *)esl_priority_name(esl_priority_t priority); + +///\} + +#ifdef __cplusplus +} +#endif /* defined(__cplusplus) */ + +#endif /* defined(ESL_EVENT_H) */ + +/* 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: + */ diff --git a/src/esl_oop.h b/src/esl_oop.h new file mode 100644 index 00000000..840b0ef9 --- /dev/null +++ b/src/esl_oop.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2007, Anthony Minessale II + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of the original author; nor the names of any contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ESL_OOP_H_ +#define _ESL_OOP_H_ +#include +#ifdef __cplusplus +extern "C" { +#endif + +#define this_check(x) do { if (!this) { esl_log(ESL_LOG_ERROR, "object is not initalized\n"); return x;}} while(0) +#define this_check_void() do { if (!this) { esl_log(ESL_LOG_ERROR, "object is not initalized\n"); return;}} while(0) + + +class ESLevent { + private: + esl_event_header_t *hp; + public: + esl_event_t *event; + char *serialized_string; + int mine; + + ESLevent(const char *type, const char *subclass_name = NULL); + ESLevent(esl_event_t *wrap_me, int free_me = 0); + ESLevent(ESLevent *me); + virtual ~ESLevent(); + const char *serialize(const char *format = NULL); + bool setPriority(esl_priority_t priority = ESL_PRIORITY_NORMAL); + const char *getHeader(const char *header_name); + char *getBody(void); + const char *getType(void); + bool addBody(const char *value); + bool addHeader(const char *header_name, const char *value); + bool delHeader(const char *header_name); + const char *firstHeader(void); + const char *nextHeader(void); +}; + + + +class ESLconnection { + private: + esl_handle_t handle; + ESLevent *last_event_obj; + public: + ESLconnection(const char *host, const char *port, const char *password); + ESLconnection(int socket); + virtual ~ESLconnection(); + int connected(); + ESLevent *getInfo(); + int send(const char *cmd); + ESLevent *sendRecv(const char *cmd); + ESLevent *api(const char *cmd, const char *arg = NULL); + ESLevent *bgapi(const char *cmd, const char *arg = NULL); + int sendEvent(ESLevent *send_me); + ESLevent *recvEvent(); + ESLevent *recvEventTimed(int ms); + ESLevent *filter(const char *header, const char *value); + int events(const char *etype, const char *value); + int execute(const char *app, const char *arg = NULL, const char *uuid = NULL); + int executeAsync(const char *app, const char *arg = NULL, const char *uuid = NULL); + int setAsyncExecute(const char *val); + int setEventLock(const char *val); + int disconnect(void); +}; + +void eslSetLogLevel(int level); + + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/esl_threadmutex.h b/src/esl_threadmutex.h new file mode 100644 index 00000000..e8373ef0 --- /dev/null +++ b/src/esl_threadmutex.h @@ -0,0 +1,58 @@ +/* + * Cross Platform Thread/Mutex abstraction + * Copyright(C) 2007 Michael Jerris + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so. + * + * This work is provided under this license on an "as is" basis, without warranty of any kind, + * either expressed or implied, including, without limitation, warranties that the covered code + * is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire + * risk as to the quality and performance of the covered code is with you. Should any covered + * code prove defective in any respect, you (not the initial developer or any other contributor) + * assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty + * constitutes an essential part of this license. No use of any covered code is authorized hereunder + * except under this disclaimer. + * + */ + + +#ifndef _ESL_THREADMUTEX_H +#define _ESL_THREADMUTEX_H + +#include "esl.h" + +#ifdef __cplusplus +extern "C" { +#endif /* defined(__cplusplus) */ + +typedef struct esl_mutex esl_mutex_t; +typedef struct esl_thread esl_thread_t; +typedef void *(*esl_thread_function_t) (esl_thread_t *, void *); + +ESL_DECLARE(esl_status_t) esl_thread_create_detached(esl_thread_function_t func, void *data); +esl_status_t esl_thread_create_detached_ex(esl_thread_function_t func, void *data, size_t stack_size); +void esl_thread_override_default_stacksize(size_t size); +ESL_DECLARE(esl_status_t) esl_mutex_create(esl_mutex_t **mutex); +ESL_DECLARE(esl_status_t) esl_mutex_destroy(esl_mutex_t **mutex); +ESL_DECLARE(esl_status_t) esl_mutex_lock(esl_mutex_t *mutex); +ESL_DECLARE(esl_status_t) esl_mutex_trylock(esl_mutex_t *mutex); +ESL_DECLARE(esl_status_t) esl_mutex_unlock(esl_mutex_t *mutex); + +#ifdef __cplusplus +} +#endif /* defined(__cplusplus) */ + +#endif /* defined(_ESL_THREADMUTEX_H) */ + +/* 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/freeswitch.c b/src/freeswitch.c index c666e8bc..4e67dc61 100644 --- a/src/freeswitch.c +++ b/src/freeswitch.c @@ -25,8 +25,8 @@ /* #include "utils_match.h" -#include */ +#include #define FREESWITCH_DEF_HOST "127.0.0.1" #define FREESWITCH_DEF_PORT "8021" @@ -87,18 +87,14 @@ static int freeswitch_read (void) if (password == NULL) password = FREESWITCH_DEF_PASSWORD; -/* esl_handle_t handle = {{0}}; - esl_connect(&handle, host, port, password); + esl_connect(&handle, host, atoi(port), password); esl_send_recv(&handle, "api show channels\n\n"); -*/ // DO YOUR THING HERE TO PARSE &handle -/* esl_disconnect(&handle); -*/ DEBUG ("FreeSWITCH SUBMIT: res-public fs_channels 3 5"); -- 2.11.0