diff --git a/libs/freetdm/Makefile.am b/libs/freetdm/Makefile.am index b322d65138..fb14ff075c 100644 --- a/libs/freetdm/Makefile.am +++ b/libs/freetdm/Makefile.am @@ -36,7 +36,7 @@ moddir = @modinstdir@ libdir = @libdir@ library_includedir = $(PREFIX)/include -INCS = -I$(FT_SRCDIR)/$(SRC)/include -I$(FT_SRCDIR)/$(SRC)/isdn/include +INCS = -I$(FT_SRCDIR)/$(SRC)/include -I$(FT_SRCDIR)/$(SRC)/isdn/include -I$(FT_SRCDIR)/$(SRC)/include/private if HAVE_SCTP INCS += -I$(FT_SRCDIR)/$(SRC)/ftmod/ftmod_sangoma_boost endif @@ -76,23 +76,9 @@ $(SRC)/ftdm_dso.c \ $(SRC)/ftdm_cpu_monitor.c library_include_HEADERS = \ -$(SRC)/include/fsk.h \ -$(SRC)/include/g711.h \ -$(SRC)/include/hashtable.h \ -$(SRC)/include/hashtable_itr.h \ -$(SRC)/include/hashtable_private.h \ -$(SRC)/include/libteletone_detect.h \ -$(SRC)/include/libteletone_generate.h \ -$(SRC)/include/libteletone.h \ $(SRC)/include/freetdm.h \ -$(SRC)/include/sangoma_tdm_api.h \ -$(SRC)/include/uart.h \ -$(SRC)/include/ftdm_buffer.h \ -$(SRC)/include/ftdm_config.h \ $(SRC)/include/ftdm_threadmutex.h \ -$(SRC)/include/ftdm_dso.h \ -$(SRC)/include/ftdm_types.h \ -$(SRC)/include/ftdm_cpu_monitor.h +$(SRC)/include/ftdm_os.h lib_LTLIBRARIES = libfreetdm.la libfreetdm_la_CFLAGS = $(AM_CFLAGS) $(MY_CFLAGS) @@ -275,4 +261,4 @@ install-data-local: $(mkinstalldirs) $(DESTDIR)$(PREFIX) $(mkinstalldirs) $(DESTDIR)@confdir@ @[ -f "$(DESTDIR)@confdir@/freetdm.conf" ] || ( cp conf/*.conf $(DESTDIR)@confdir@) - @echo OpenFTDM Installed + @echo FreeTDM Installed diff --git a/libs/freetdm/src/fsk.c b/libs/freetdm/src/fsk.c index 60a4722a4c..9e6f97287e 100644 --- a/libs/freetdm/src/fsk.c +++ b/libs/freetdm/src/fsk.c @@ -32,7 +32,7 @@ * * 2005 03 20 R. Krten created */ -#include +#include #include #include #include diff --git a/libs/freetdm/src/ftdm_buffer.c b/libs/freetdm/src/ftdm_buffer.c index 2f08e7bbee..000cddd3f9 100644 --- a/libs/freetdm/src/ftdm_buffer.c +++ b/libs/freetdm/src/ftdm_buffer.c @@ -31,7 +31,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "freetdm.h" +#include "private/ftdm_core.h" #include "ftdm_buffer.h" static unsigned buffer_id = 0; diff --git a/libs/freetdm/src/ftdm_callerid.c b/libs/freetdm/src/ftdm_callerid.c index 16dd5c8ed7..d3223502f4 100644 --- a/libs/freetdm/src/ftdm_callerid.c +++ b/libs/freetdm/src/ftdm_callerid.c @@ -1,4 +1,4 @@ -#include "freetdm.h" +#include "private/ftdm_core.h" #include "fsk.h" #include "uart.h" diff --git a/libs/freetdm/src/ftdm_config.c b/libs/freetdm/src/ftdm_config.c index 5f9895d04b..4ba2e19c67 100644 --- a/libs/freetdm/src/ftdm_config.c +++ b/libs/freetdm/src/ftdm_config.c @@ -35,8 +35,7 @@ * Moises Silva */ -#include "freetdm.h" -#include "ftdm_config.h" +#include "private/ftdm_core.h" int ftdm_config_open_file(ftdm_config_t *cfg, const char *file_path) { diff --git a/libs/freetdm/src/ftdm_cpu_monitor.c b/libs/freetdm/src/ftdm_cpu_monitor.c index b02f5a0d1f..4583f07714 100644 --- a/libs/freetdm/src/ftdm_cpu_monitor.c +++ b/libs/freetdm/src/ftdm_cpu_monitor.c @@ -50,7 +50,7 @@ #include #endif -#include "freetdm.h" +#include "private/ftdm_core.h" #include "ftdm_cpu_monitor.h" struct ftdm_cpu_monitor_stats { diff --git a/libs/freetdm/src/ftdm_dso.c b/libs/freetdm/src/ftdm_dso.c index a0981ebdd9..ea5056130a 100644 --- a/libs/freetdm/src/ftdm_dso.c +++ b/libs/freetdm/src/ftdm_dso.c @@ -17,7 +17,7 @@ * */ -#include "freetdm.h" +#include "private/ftdm_core.h" #include "ftdm_dso.h" #include #include diff --git a/libs/freetdm/src/ftdm_io.c b/libs/freetdm/src/ftdm_io.c index cac4f15f16..e72f177d67 100644 --- a/libs/freetdm/src/ftdm_io.c +++ b/libs/freetdm/src/ftdm_io.c @@ -40,7 +40,7 @@ #define _GNU_SOURCE #ifndef WIN32 #endif -#include "freetdm.h" +#include "private/ftdm_core.h" #include #ifdef WIN32 #include @@ -1592,11 +1592,72 @@ FT_DECLARE(ftdm_status_t) ftdm_channel_open(uint32_t span_id, uint32_t chan_id, return status; } -FT_DECLARE(ftdm_status_t) ftdm_channel_outgoing_call(ftdm_channel_t *ftdmchan) +FT_DECLARE(uint32_t) ftdm_channel_get_id(const ftdm_channel_t *ftdmchan) +{ + return ftdmchan->chan_id; +} + +FT_DECLARE(uint32_t) ftdm_channel_get_ph_id(const ftdm_channel_t *ftdmchan) +{ + return ftdmchan->physical_chan_id; +} + +FT_DECLARE(uint32_t) ftdm_channel_get_span_id(const ftdm_channel_t *ftdmchan) +{ + return ftdmchan->span_id; +} + +FT_DECLARE(uint32_t) ftdm_span_get_id(const ftdm_span_t *span) +{ + return span->span_id; +} + +FT_DECLARE(const char *) ftdm_span_get_name(const ftdm_span_t *span) +{ + return span->name; +} + +FT_DECLARE(ftdm_bool_t) ftdm_channel_call_active(const ftdm_channel_t *ftdmchan) +{ + return (ftdmchan->state == FTDM_CHANNEL_STATE_UP) ? FTDM_TRUE : FTDM_FALSE; +} + +FT_DECLARE(ftdm_status_t) ftdm_channel_call_answer(ftdm_channel_t *ftdmchan) +{ + ftdm_set_state_locked_wait(ftdmchan, FTDM_CHANNEL_STATE_UP); + return FTDM_SUCCESS; +} + +FT_DECLARE(ftdm_status_t) ftdm_channel_call_hangup(ftdm_channel_t *ftdmchan) +{ + ftdm_set_state_locked_wait(ftdmchan, FTDM_CHANNEL_STATE_HANGUP); + return FTDM_SUCCESS; +} + +FT_DECLARE(const char *) ftdm_channel_get_last_error(const ftdm_channel_t *ftdmchan) +{ + return ftdmchan->last_error; +} + +FT_DECLARE(ftdm_status_t) ftdm_channel_call_indicate(ftdm_channel_t *ftdmchan, ftdm_channel_indication_t indication) +{ + switch (indication) { + case FTDM_CHANNEL_INDICATE_RING: + ftdm_set_state_locked_wait(ftdmchan, FTDM_CHANNEL_STATE_RING); + case FTDM_CHANNEL_INDICATE_PROGRESS: + ftdm_set_state_locked_wait(ftdmchan, FTDM_CHANNEL_STATE_PROGRESS_MEDIA); + default: + ftdm_log(FTDM_LOG_DEBUG, "Do not know how to indicate %d\n", indication); + return FTDM_FAIL; + } + return FTDM_SUCCESS; +} + +FT_DECLARE(ftdm_status_t) ftdm_channel_call_place(ftdm_channel_t *ftdmchan) { ftdm_status_t status; - assert(ftdmchan != NULL); + ftdm_assert(ftdmchan != NULL, "null channel"); if (ftdmchan->span->outgoing_call) { if ((status = ftdmchan->span->outgoing_call(ftdmchan)) == FTDM_SUCCESS) { @@ -1604,9 +1665,8 @@ FT_DECLARE(ftdm_status_t) ftdm_channel_outgoing_call(ftdm_channel_t *ftdmchan) } return status; } else { - ftdm_log(FTDM_LOG_ERROR, "outgoing_call method not implemented!\n"); + ftdm_log(FTDM_LOG_ERROR, "outgoing_call method not implemented in this span!\n"); } - return FTDM_FAIL; } diff --git a/libs/freetdm/src/ftdm_queue.c b/libs/freetdm/src/ftdm_queue.c index 60ee74737d..75d18033da 100644 --- a/libs/freetdm/src/ftdm_queue.c +++ b/libs/freetdm/src/ftdm_queue.c @@ -32,7 +32,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "freetdm.h" +#include "private/ftdm_core.h" static ftdm_status_t ftdm_std_queue_create(ftdm_queue_t **outqueue, ftdm_size_t capacity); static ftdm_status_t ftdm_std_queue_enqueue(ftdm_queue_t *queue, void *obj); diff --git a/libs/freetdm/src/ftdm_threadmutex.c b/libs/freetdm/src/ftdm_threadmutex.c index 1eb34a81f8..1ef9a876ea 100644 --- a/libs/freetdm/src/ftdm_threadmutex.c +++ b/libs/freetdm/src/ftdm_threadmutex.c @@ -26,7 +26,7 @@ #define _WIN32_WINNT 0x0400 #endif -#include "freetdm.h" +#include "private/ftdm_core.h" #include "ftdm_threadmutex.h" #ifdef WIN32 diff --git a/libs/freetdm/src/ftmod/ftmod_analog/ftmod_analog.c b/libs/freetdm/src/ftmod/ftmod_analog/ftmod_analog.c index c7ad6b9870..efdfcb50f4 100644 --- a/libs/freetdm/src/ftmod/ftmod_analog/ftmod_analog.c +++ b/libs/freetdm/src/ftmod/ftmod_analog/ftmod_analog.c @@ -31,7 +31,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "freetdm.h" +#include "private/ftdm_core.h" #include "ftdm_analog.h" #ifndef localtime_r diff --git a/libs/freetdm/src/ftmod/ftmod_analog_em/ftmod_analog_em.c b/libs/freetdm/src/ftmod/ftmod_analog_em/ftmod_analog_em.c index 146bcb9ea4..46ef0b2d87 100644 --- a/libs/freetdm/src/ftmod/ftmod_analog_em/ftmod_analog_em.c +++ b/libs/freetdm/src/ftmod/ftmod_analog_em/ftmod_analog_em.c @@ -36,7 +36,7 @@ * */ -#include "freetdm.h" +#include "private/ftdm_core.h" #include "ftdm_analog_em.h" #ifndef localtime_r diff --git a/libs/freetdm/src/ftmod/ftmod_r2/ftmod_r2.c b/libs/freetdm/src/ftmod/ftmod_r2/ftmod_r2.c index 85fe09f7bc..a3396ae0ad 100644 --- a/libs/freetdm/src/ftmod/ftmod_r2/ftmod_r2.c +++ b/libs/freetdm/src/ftmod/ftmod_r2/ftmod_r2.c @@ -33,7 +33,7 @@ #include #include -#include "freetdm.h" +#include "private/ftdm_core.h" /* debug thread count for r2 legs */ static ftdm_mutex_t* g_thread_count_mutex; diff --git a/libs/freetdm/src/ftmod/ftmod_sangoma_boost/ftmod_sangoma_boost.c b/libs/freetdm/src/ftmod/ftmod_sangoma_boost/ftmod_sangoma_boost.c index 424c6d45a8..0b1081a841 100644 --- a/libs/freetdm/src/ftmod/ftmod_sangoma_boost/ftmod_sangoma_boost.c +++ b/libs/freetdm/src/ftmod/ftmod_sangoma_boost/ftmod_sangoma_boost.c @@ -43,7 +43,7 @@ On __WINDOWS__ platform this code works with sigmod ONLY, don't try to make sens I basically ifdef out everything that the compiler complained about */ -#include "freetdm.h" +#include "private/ftdm_core.h" #include "sangoma_boost_client.h" #include "ftdm_sangoma_boost.h" #ifdef HAVE_SYS_SELECT_H diff --git a/libs/freetdm/src/ftmod/ftmod_sangoma_boost/sangoma_boost_interface.h b/libs/freetdm/src/ftmod/ftmod_sangoma_boost/sangoma_boost_interface.h index d7ad6de84f..f6130db444 100644 --- a/libs/freetdm/src/ftmod/ftmod_sangoma_boost/sangoma_boost_interface.h +++ b/libs/freetdm/src/ftmod/ftmod_sangoma_boost/sangoma_boost_interface.h @@ -35,7 +35,7 @@ #ifndef SANGOMA_BOOST_INTERFACE_H #define SANGOMA_BOOST_INTERFACE_H -#include "freetdm.h" +#include "private/ftdm_core.h" #ifdef __cplusplus extern "C" { diff --git a/libs/freetdm/src/ftmod/ftmod_skel/ftmod_skel.c b/libs/freetdm/src/ftmod/ftmod_skel/ftmod_skel.c index 62c4942e7c..ee06a0716d 100644 --- a/libs/freetdm/src/ftmod/ftmod_skel/ftmod_skel.c +++ b/libs/freetdm/src/ftmod/ftmod_skel/ftmod_skel.c @@ -32,7 +32,7 @@ */ -#include "freetdm.h" +#include "private/ftdm_core.h" //#include "ftdm_skel.h" static FIO_CONFIGURE_FUNCTION(skel_configure) diff --git a/libs/freetdm/src/ftmod/ftmod_wanpipe/ftmod_wanpipe.c b/libs/freetdm/src/ftmod/ftmod_wanpipe/ftmod_wanpipe.c index b941e641de..7d192d59c5 100644 --- a/libs/freetdm/src/ftmod/ftmod_wanpipe/ftmod_wanpipe.c +++ b/libs/freetdm/src/ftmod/ftmod_wanpipe/ftmod_wanpipe.c @@ -42,7 +42,7 @@ #include #include #endif -#include "freetdm.h" +#include "private/ftdm_core.h" #ifndef __WINDOWS__ #include #include diff --git a/libs/freetdm/src/ftmod/ftmod_zt/ftmod_zt.c b/libs/freetdm/src/ftmod/ftmod_zt/ftmod_zt.c index 1394a9f91f..5a8cf1760e 100644 --- a/libs/freetdm/src/ftmod/ftmod_zt/ftmod_zt.c +++ b/libs/freetdm/src/ftmod/ftmod_zt/ftmod_zt.c @@ -32,7 +32,7 @@ */ -#include "freetdm.h" +#include "private/ftdm_core.h" #include "ftmod_zt.h" /** diff --git a/libs/freetdm/src/hashtable.c b/libs/freetdm/src/hashtable.c index 1f08abc2b8..aced5ec413 100644 --- a/libs/freetdm/src/hashtable.c +++ b/libs/freetdm/src/hashtable.c @@ -31,7 +31,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "freetdm.h" +#include "private/ftdm_core.h" #include "hashtable.h" #include "hashtable_private.h" #include diff --git a/libs/freetdm/src/hashtable_itr.c b/libs/freetdm/src/hashtable_itr.c index a31f5b44c5..7885be1609 100644 --- a/libs/freetdm/src/hashtable_itr.c +++ b/libs/freetdm/src/hashtable_itr.c @@ -31,7 +31,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "freetdm.h" +#include "private/ftdm_core.h" #include "hashtable.h" #include "hashtable_private.h" #include "hashtable_itr.h" diff --git a/libs/freetdm/src/include/freetdm.h b/libs/freetdm/src/include/freetdm.h index 34442aa95a..1a021eaa91 100644 --- a/libs/freetdm/src/include/freetdm.h +++ b/libs/freetdm/src/include/freetdm.h @@ -40,290 +40,40 @@ #define FREETDM_H -#if !defined(_XOPEN_SOURCE) && !defined(__FreeBSD__) -#define _XOPEN_SOURCE 600 -#endif +#include "ftdm_declare.h" -#ifndef HAVE_STRINGS_H -#define HAVE_STRINGS_H 1 -#endif -#ifndef HAVE_SYS_SOCKET_H -#define HAVE_SYS_SOCKET_H 1 -#endif +typedef enum { + FTDM_SUCCESS, + FTDM_FAIL, + FTDM_MEMERR, + FTDM_TIMEOUT, + FTDM_NOTIMPL, + FTDM_CHECKSUM_ERROR, + FTDM_STATUS_COUNT, + FTDM_BREAK +} ftdm_status_t; -#ifndef __WINDOWS__ -#if defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32) || defined(_WIN64) -#define __WINDOWS__ -#endif -#endif - -#ifdef _MSC_VER -#if defined(FT_DECLARE_STATIC) -#define FT_DECLARE(type) type __stdcall -#define FT_DECLARE_NONSTD(type) type __cdecl -#define FT_DECLARE_DATA -#elif defined(FREETDM_EXPORTS) -#define FT_DECLARE(type) __declspec(dllexport) type __stdcall -#define FT_DECLARE_NONSTD(type) __declspec(dllexport) type __cdecl -#define FT_DECLARE_DATA __declspec(dllexport) -#else -#define FT_DECLARE(type) __declspec(dllimport) type __stdcall -#define FT_DECLARE_NONSTD(type) __declspec(dllimport) type __cdecl -#define FT_DECLARE_DATA __declspec(dllimport) -#endif -#define EX_DECLARE_DATA __declspec(dllexport) -#else -#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(HAVE_VISIBILITY) -#define FT_DECLARE(type) __attribute__((visibility("default"))) type -#define FT_DECLARE_NONSTD(type) __attribute__((visibility("default"))) type -#define FT_DECLARE_DATA __attribute__((visibility("default"))) -#else -#define FT_DECLARE(type) type -#define FT_DECLARE_NONSTD(type) type -#define FT_DECLARE_DATA -#endif -#define EX_DECLARE_DATA -#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 -/* disable warning for zero length array in a struct */ -/* this will cause errors on c99 and ansi compliant compilers and will need to be fixed in the wanpipe header files */ -#pragma warning(disable:4706) -#pragma comment(lib, "Winmm") -#endif - -#define FTDM_THREAD_STACKSIZE 240 * 1024 -#define FTDM_ENUM_NAMES(_NAME, _STRINGS) static const char * _NAME [] = { _STRINGS , NULL }; -#define FTDM_STR2ENUM_P(_FUNC1, _FUNC2, _TYPE) FT_DECLARE(_TYPE) _FUNC1 (const char *name); FT_DECLARE(const char *) _FUNC2 (_TYPE type); -#define FTDM_STR2ENUM(_FUNC1, _FUNC2, _TYPE, _STRINGS, _MAX) \ - FT_DECLARE(_TYPE) _FUNC1 (const char *name) \ - { \ - int i; \ - _TYPE t = _MAX ; \ - \ - for (i = 0; i < _MAX ; i++) { \ - if (!strcasecmp(name, _STRINGS[i])) { \ - t = (_TYPE) i; \ - break; \ - } \ - } \ - \ - return t; \ - } \ - FT_DECLARE(const char *) _FUNC2 (_TYPE type) \ - { \ - if (type > _MAX) { \ - type = _MAX; \ - } \ - return _STRINGS[(int)type]; \ - } \ - -#define ftdm_true(expr) \ - (expr && ( !strcasecmp(expr, "yes") || \ - !strcasecmp(expr, "on") || \ - !strcasecmp(expr, "true") || \ - !strcasecmp(expr, "enabled") || \ - !strcasecmp(expr, "active") || \ - atoi(expr))) ? 1 : 0 - - -#include -#ifndef __WINDOWS__ -#include -#endif - -#include -#include -#include -#ifdef HAVE_STRINGS_H -#include -#endif -#include -#include "ftdm_types.h" -#include "hashtable.h" -#include "ftdm_config.h" -#include "g711.h" -#include "libteletone.h" -#include "ftdm_buffer.h" -#include "ftdm_threadmutex.h" +/* Thread/Mutex OS abstraction */ +#include "ftdm_os.h" #ifdef __cplusplus extern "C" { #endif -#ifdef __WINDOWS__ -#define ftdm_sleep(x) Sleep(x) -#else -#define ftdm_sleep(x) usleep(x * 1000) -#endif - -#ifdef NDEBUG -#undef assert -#define assert(_Expression) ((void)(_Expression)) -#endif - -#define FTDM_MAX_CHANNELS_PHYSICAL_SPAN 32 -#define FTDM_MAX_PHYSICAL_SPANS_PER_LOGICAL_SPAN 32 -#define FTDM_MAX_CHANNELS_SPAN FTDM_MAX_CHANNELS_PHYSICAL_SPAN * FTDM_MAX_PHYSICAL_SPANS_PER_LOGICAL_SPAN -#define FTDM_MAX_SPANS_INTERFACE 128 - -#define FTDM_MAX_CHANNELS_GROUP 1024 -#define FTDM_MAX_GROUPS_INTERFACE FTDM_MAX_SPANS_INTERFACE - #define FTDM_MAX_NAME_STR_SZ 80 #define FTDM_MAX_NUMBER_STR_SZ 20 -#define GOTO_STATUS(label,st) status = st; goto label ; - -#define ftdm_copy_string(x,y,z) strncpy(x, y, z - 1) -#define ftdm_set_string(x,y) strncpy(x, y, sizeof(x)-1) -#define ftdm_strlen_zero(s) (!s || *s == '\0') -#define ftdm_strlen_zero_buf(s) (*s == '\0') - - -#define ftdm_channel_test_feature(obj, flag) ((obj)->features & flag) -#define ftdm_channel_set_feature(obj, flag) (obj)->features |= (flag) -#define ftdm_channel_clear_feature(obj, flag) (obj)->features &= ~(flag) -#define ftdm_channel_set_member_locked(obj, _m, _v) ftdm_mutex_lock(obj->mutex); obj->_m = _v; ftdm_mutex_unlock(obj->mutex) - -/*! - \brief Test for the existance of a flag on an arbitary object - \command obj the object to test - \command flag the or'd list of flags to test - \return true value if the object has the flags defined -*/ -#define ftdm_test_flag(obj, flag) ((obj)->flags & flag) -#define ftdm_test_pflag(obj, flag) ((obj)->pflags & flag) -#define ftdm_test_sflag(obj, flag) ((obj)->sflags & flag) - - -#define ftdm_set_alarm_flag(obj, flag) (obj)->alarm_flags |= (flag) -#define ftdm_clear_alarm_flag(obj, flag) (obj)->alarm_flags &= ~(flag) -#define ftdm_test_alarm_flag(obj, flag) ((obj)->alarm_flags & flag) - -/*! - \brief Set a flag on an arbitrary object - \command obj the object to set the flags on - \command flag the or'd list of flags to set -*/ -#define ftdm_set_flag(obj, flag) (obj)->flags |= (flag) -#define ftdm_set_flag_locked(obj, flag) assert(obj->mutex != NULL); \ - ftdm_mutex_lock(obj->mutex); \ - (obj)->flags |= (flag); \ - ftdm_mutex_unlock(obj->mutex); - -#define ftdm_set_pflag(obj, flag) (obj)->pflags |= (flag) -#define ftdm_set_pflag_locked(obj, flag) assert(obj->mutex != NULL); \ - ftdm_mutex_lock(obj->mutex); \ - (obj)->pflags |= (flag); \ - ftdm_mutex_unlock(obj->mutex); - -#define ftdm_set_sflag(obj, flag) (obj)->sflags |= (flag) -#define ftdm_set_sflag_locked(obj, flag) assert(obj->mutex != NULL); \ - ftdm_mutex_lock(obj->mutex); \ - (obj)->sflags |= (flag); \ - ftdm_mutex_unlock(obj->mutex); - -/*! - \brief Clear a flag on an arbitrary object while locked - \command obj the object to test - \command flag the or'd list of flags to clear -*/ -#define ftdm_clear_flag(obj, flag) (obj)->flags &= ~(flag) - -#define ftdm_clear_flag_locked(obj, flag) assert(obj->mutex != NULL); ftdm_mutex_lock(obj->mutex); (obj)->flags &= ~(flag); ftdm_mutex_unlock(obj->mutex); - -#define ftdm_clear_pflag(obj, flag) (obj)->pflags &= ~(flag) - -#define ftdm_clear_pflag_locked(obj, flag) assert(obj->mutex != NULL); ftdm_mutex_lock(obj->mutex); (obj)->pflags &= ~(flag); ftdm_mutex_unlock(obj->mutex); - -#define ftdm_clear_sflag(obj, flag) (obj)->sflags &= ~(flag) - -#define ftdm_clear_sflag_locked(obj, flag) assert(obj->mutex != NULL); ftdm_mutex_lock(obj->mutex); (obj)->sflags &= ~(flag); ftdm_mutex_unlock(obj->mutex); - - -#define ftdm_set_state_locked(obj, s) if ( obj->state == s ) { \ - ftdm_log(FTDM_LOG_WARNING, "Why bother changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(obj->state), ftdm_channel_state2str(s)); \ - } else if (ftdm_test_flag(obj, FTDM_CHANNEL_READY)) { \ - ftdm_channel_state_t st = obj->state; \ - ftdm_channel_set_state(obj, s, 1); \ - if (obj->state == s) ftdm_log(FTDM_LOG_DEBUG, "Changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(st), ftdm_channel_state2str(s)); \ - else ftdm_log(FTDM_LOG_WARNING, "VETO Changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(st), ftdm_channel_state2str(s)); \ - } - -#define ftdm_set_state(obj, s) if ( obj->state == s ) { \ - ftdm_log(FTDM_LOG_WARNING, "Why bother changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(obj->state), ftdm_channel_state2str(s)); \ - } else if (ftdm_test_flag(obj, FTDM_CHANNEL_READY)) { \ - ftdm_channel_state_t st = obj->state; \ - ftdm_channel_set_state(obj, s, 0); \ - if (obj->state == s) ftdm_log(FTDM_LOG_DEBUG, "Changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(st), ftdm_channel_state2str(s)); \ - else ftdm_log(FTDM_LOG_WARNING, "VETO Changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(st), ftdm_channel_state2str(s)); \ - } - -#ifdef _MSC_VER -/* The while(0) below throws a conditional expression is constant warning */ -#pragma warning(disable:4127) -#endif - -#define ftdm_set_state_locked_wait(obj, s) \ - do { \ - int __safety = 100; \ - ftdm_set_state_locked(obj, s); \ - while(__safety-- && ftdm_test_flag(obj, FTDM_CHANNEL_STATE_CHANGE)) { \ - ftdm_sleep(10); \ - } \ - if(!__safety) { \ - ftdm_log(FTDM_LOG_CRIT, "State change not completed\n"); \ - } \ - } while(0); - -#define ftdm_wait_for_flag_cleared(obj, flag, time) \ - do { \ - int __safety = time; \ - while(__safety-- && ftdm_test_flag(obj, flag)) { \ - ftdm_mutex_unlock(obj->mutex); \ - ftdm_sleep(10); \ - ftdm_mutex_lock(obj->mutex); \ - } \ - if(!__safety) { \ - ftdm_log(FTDM_LOG_CRIT, "flag %d was never cleared\n", flag); \ - } \ - } while(0); - -#define ftdm_set_state_wait(obj, s) \ - do { \ - ftdm_channel_set_state(obj, s, 0); \ - ftdm_wait_for_flag_cleared(obj, FTDM_CHANNEL_STATE_CHANGE, 100); \ - } while(0); +typedef enum { + FTDM_TOP_DOWN, + FTDM_BOTTOM_UP +} ftdm_direction_t; +typedef enum { + FTDM_EVENT_NONE, + FTDM_EVENT_DTMF, + FTDM_EVENT_OOB, + FTDM_EVENT_COUNT +} ftdm_event_type_t; typedef enum { FTDM_STATE_CHANGE_FAIL, @@ -331,52 +81,6 @@ typedef enum { FTDM_STATE_CHANGE_SAME, } ftdm_state_change_result_t; -#define ftdm_set_state_r(obj, s, l, r) if ( obj->state == s ) { \ - if (s != FTDM_CHANNEL_STATE_HANGUP) ftdm_log(FTDM_LOG_WARNING, "Why bother changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(obj->state), ftdm_channel_state2str(s)); r = FTDM_STATE_CHANGE_SAME; \ - } else if (ftdm_test_flag(obj, FTDM_CHANNEL_READY)) { \ - int st = obj->state; \ - r = (ftdm_channel_set_state(obj, s, l) == FTDM_SUCCESS) ? FTDM_STATE_CHANGE_SUCCESS : FTDM_STATE_CHANGE_FAIL; \ - if (obj->state == s) {ftdm_log(FTDM_LOG_DEBUG, "Changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(st), ftdm_channel_state2str(s));} \ - else ftdm_log(FTDM_LOG_WARNING, "VETO Changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(st), ftdm_channel_state2str(s)); \ - } - - -#define ftdm_is_dtmf(key) ((key > 47 && key < 58) || (key > 64 && key < 69) || (key > 96 && key < 101) || key == 35 || key == 42 || key == 87 || key == 119) - -/*! - \brief Copy flags from one arbitrary object to another - \command dest the object to copy the flags to - \command src the object to copy the flags from - \command flags the flags to copy -*/ -#define ftdm_copy_flags(dest, src, flags) (dest)->flags &= ~(flags); (dest)->flags |= ((src)->flags & (flags)) - -struct ftdm_stream_handle { - ftdm_stream_handle_write_function_t write_function; - ftdm_stream_handle_raw_write_function_t raw_write_function; - void *data; - void *end; - ftdm_size_t data_size; - ftdm_size_t data_len; - ftdm_size_t alloc_len; - ftdm_size_t alloc_chunk; -}; - - -FT_DECLARE_NONSTD(ftdm_status_t) ftdm_console_stream_raw_write(ftdm_stream_handle_t *handle, uint8_t *data, ftdm_size_t datalen); -FT_DECLARE_NONSTD(ftdm_status_t) ftdm_console_stream_write(ftdm_stream_handle_t *handle, const char *fmt, ...); - -#define FTDM_CMD_CHUNK_LEN 1024 -#define FTDM_STANDARD_STREAM(s) memset(&s, 0, sizeof(s)); s.data = ftdm_malloc(FTDM_CMD_CHUNK_LEN); \ - assert(s.data); \ - memset(s.data, 0, FTDM_CMD_CHUNK_LEN); \ - s.end = s.data; \ - s.data_size = FTDM_CMD_CHUNK_LEN; \ - s.write_function = ftdm_console_stream_write; \ - s.raw_write_function = ftdm_console_stream_raw_write; \ - s.alloc_len = FTDM_CMD_CHUNK_LEN; \ - s.alloc_chunk = FTDM_CMD_CHUNK_LEN - struct ftdm_event { ftdm_event_type_t e_type; uint32_t enum_id; @@ -384,7 +88,23 @@ struct ftdm_event { void *data; }; -typedef struct ftdm_queue ftdm_queue_t; +typedef enum { + FTDM_CHAN_TYPE_B, + FTDM_CHAN_TYPE_DQ921, + FTDM_CHAN_TYPE_DQ931, + FTDM_CHAN_TYPE_FXS, + FTDM_CHAN_TYPE_FXO, + FTDM_CHAN_TYPE_EM, + FTDM_CHAN_TYPE_CAS, + FTDM_CHAN_TYPE_COUNT +} ftdm_chan_type_t; +#define CHAN_TYPE_STRINGS "B", "DQ921", "DQ931", "FXS", "FXO", "EM", "CAS", "INVALID" +FTDM_STR2ENUM_P(ftdm_str2ftdm_chan_type, ftdm_chan_type2str, ftdm_chan_type_t) + +#define FTDM_IS_VOICE_CHANNEL(ftdm_chan) ((ftdm_chan)->type != FTDM_CHAN_TYPE_DQ921 && (ftdm_chan)->type != FTDM_CHAN_TYPE_DQ931) + +typedef void (*ftdm_logger_t)(const char *file, const char *func, int line, int level, const char *fmt, ...); + typedef ftdm_status_t (*ftdm_queue_create_func_t)(ftdm_queue_t **queue, ftdm_size_t capacity); typedef ftdm_status_t (*ftdm_queue_enqueue_func_t)(ftdm_queue_t *queue, void *obj); typedef void *(*ftdm_queue_dequeue_func_t)(ftdm_queue_t *queue); @@ -399,85 +119,6 @@ typedef struct ftdm_queue_handler { ftdm_queue_get_interrupt_func_t get_interrupt; ftdm_queue_destroy_func_t destroy; } ftdm_queue_handler_t; -FT_DECLARE_DATA extern ftdm_queue_handler_t g_ftdm_queue_handler; - -/*! brief create a new queue */ -#define ftdm_queue_create(queue, capacity) g_ftdm_queue_handler.create(queue, capacity) - -/*! Enqueue an object */ -#define ftdm_queue_enqueue(queue, obj) g_ftdm_queue_handler.enqueue(queue, obj) - -/*! dequeue an object from the queue */ -#define ftdm_queue_dequeue(queue) g_ftdm_queue_handler.dequeue(queue) - -/*! wait ms milliseconds for a queue to have available objects, -1 to wait forever */ -#define ftdm_queue_wait(queue, ms) g_ftdm_queue_handler.wait(queue, ms) - -/*! get the internal interrupt object (to wait for elements to be added from the outside bypassing ftdm_queue_wait) */ -#define ftdm_queue_get_interrupt(queue, ms) g_ftdm_queue_handler.get_interrupt(queue, ms) - -/*! destroy the queue */ -#define ftdm_queue_destroy(queue) g_ftdm_queue_handler.destroy(queue) - - -#define FTDM_TOKEN_STRLEN 128 -#define FTDM_MAX_TOKENS 10 - -static __inline__ char *ftdm_clean_string(char *s) -{ - char *p; - - for (p = s; p && *p; p++) { - uint8_t x = (uint8_t) *p; - if (x < 32 || x > 127) { - *p = ' '; - } - } - - return s; -} - -struct ftdm_bitstream { - uint8_t *data; - uint32_t datalen; - uint32_t byte_index; - uint8_t bit_index; - int8_t endian; - uint8_t top; - uint8_t bot; - uint8_t ss; - uint8_t ssv; -}; - -struct ftdm_fsk_data_state { - dsp_fsk_handle_t *fsk1200_handle; - uint8_t init; - uint8_t *buf; - size_t bufsize; - ftdm_size_t blen; - ftdm_size_t bpos; - ftdm_size_t dlen; - ftdm_size_t ppos; - int checksum; -}; - -struct ftdm_fsk_modulator { - teletone_dds_state_t dds; - ftdm_bitstream_t bs; - uint32_t carrier_bits_start; - uint32_t carrier_bits_stop; - uint32_t chan_sieze_bits; - uint32_t bit_factor; - uint32_t bit_accum; - uint32_t sample_counter; - int32_t samples_per_bit; - int32_t est_bytes; - fsk_modem_types_t modem_type; - ftdm_fsk_data_state_t *fsk_data; - ftdm_fsk_write_sample_t write_sample_callback; - void *user_data; - int16_t sample_buffer[64]; -}; /** * Type Of Number (TON) @@ -519,7 +160,7 @@ typedef enum { FTDM_CALLER_STATE_FAIL } ftdm_caller_state_t; -struct ftdm_caller_data { +typedef struct ftdm_caller_data { char cid_date[8]; char cid_name[80]; ftdm_number_t cid_num; @@ -537,92 +178,33 @@ struct ftdm_caller_data { uint32_t flags; ftdm_caller_state_t call_state; uint32_t chan_id; -}; +} ftdm_caller_data_t; typedef enum { - FTDM_TYPE_NONE, - FTDM_TYPE_SPAN = 0xFF, - FTDM_TYPE_CHANNEL -} ftdm_data_type_t; - -/* 2^8 table size, one for each byte value */ -#define FTDM_GAINS_TABLE_SIZE 256 -struct ftdm_channel { - ftdm_data_type_t data_type; - uint32_t span_id; - uint32_t chan_id; - uint32_t physical_span_id; - uint32_t physical_chan_id; - uint32_t rate; - uint32_t extra_id; - ftdm_chan_type_t type; - ftdm_socket_t sockfd; - uint32_t flags; - uint32_t pflags; - uint32_t sflags; - ftdm_alarm_flag_t alarm_flags; - ftdm_channel_feature_t features; - ftdm_codec_t effective_codec; - ftdm_codec_t native_codec; - uint32_t effective_interval; - uint32_t native_interval; - uint32_t packet_len; - ftdm_channel_state_t state; - ftdm_channel_state_t last_state; - ftdm_channel_state_t init_state; - ftdm_mutex_t *mutex; - teletone_dtmf_detect_state_t dtmf_detect; - uint32_t buffer_delay; - ftdm_event_t event_header; - char last_error[256]; - fio_event_cb_t event_callback; - uint32_t skip_read_frames; - ftdm_buffer_t *dtmf_buffer; - ftdm_buffer_t *gen_dtmf_buffer; - ftdm_buffer_t *pre_buffer; - ftdm_buffer_t *digit_buffer; - ftdm_buffer_t *fsk_buffer; - ftdm_mutex_t *pre_buffer_mutex; - uint32_t dtmf_on; - uint32_t dtmf_off; - char *dtmf_hangup_buf; - teletone_generation_session_t tone_session; - ftdm_time_t last_event_time; - ftdm_time_t ring_time; - char tokens[FTDM_MAX_TOKENS+1][FTDM_TOKEN_STRLEN]; - uint8_t needed_tones[FTDM_TONEMAP_INVALID]; - uint8_t detected_tones[FTDM_TONEMAP_INVALID]; - ftdm_tonemap_t last_detected_tone; - uint32_t token_count; - char chan_name[128]; - char chan_number[32]; - ftdm_filehandle_t fds[2]; - ftdm_fsk_data_state_t fsk; - uint8_t fsk_buf[80]; - uint32_t ring_count; - void *mod_data; - void *call_data; - struct ftdm_caller_data caller_data; - struct ftdm_span *span; - struct ftdm_io_interface *fio; - ftdm_hash_t *variable_hash; - unsigned char rx_cas_bits; - uint32_t pre_buffer_size; - uint8_t rxgain_table[FTDM_GAINS_TABLE_SIZE]; - uint8_t txgain_table[FTDM_GAINS_TABLE_SIZE]; - float rxgain; - float txgain; -}; - -struct ftdm_channel_config { - char name[FTDM_MAX_NAME_STR_SZ]; - char number[FTDM_MAX_NUMBER_STR_SZ]; - char group_name[FTDM_MAX_NAME_STR_SZ]; - ftdm_chan_type_t type; - float rxgain; - float txgain; -}; - + FTDM_SIGEVENT_START, + FTDM_SIGEVENT_STOP, + FTDM_SIGEVENT_TRANSFER, + FTDM_SIGEVENT_ANSWER, + FTDM_SIGEVENT_UP, + FTDM_SIGEVENT_FLASH, + FTDM_SIGEVENT_PROGRESS, + FTDM_SIGEVENT_PROGRESS_MEDIA, + FTDM_SIGEVENT_NOTIFY, + FTDM_SIGEVENT_TONE_DETECTED, + FTDM_SIGEVENT_ALARM_TRAP, + FTDM_SIGEVENT_ALARM_CLEAR, + FTDM_SIGEVENT_MISC, + FTDM_SIGEVENT_COLLECTED_DIGIT, + FTDM_SIGEVENT_ADD_CALL, + FTDM_SIGEVENT_RESTART, + /* Signaling status changed (D-chan up, down, R2 blocked etc) */ + FTDM_SIGEVENT_SIGSTATUS_CHANGED, + FTDM_SIGEVENT_INVALID +} ftdm_signal_event_t; +#define SIGNAL_STRINGS "START", "STOP", "TRANSFER", "ANSWER", "UP", "FLASH", "PROGRESS", \ + "PROGRESS_MEDIA", "NOTIFY", "TONE_DETECTED", "ALARM_TRAP", "ALARM_CLEAR", "MISC", \ + "COLLECTED_DIGIT", "ADD_CALL", "RESTART", "SIGLINK_CHANGED", "INVALID" +FTDM_STR2ENUM_P(ftdm_str2ftdm_signal_event, ftdm_signal_event2str, ftdm_signal_event_t) struct ftdm_sigmsg { ftdm_signal_event_t event_id; @@ -633,63 +215,88 @@ struct ftdm_sigmsg { uint32_t raw_data_len; }; - -struct ftdm_span { - ftdm_data_type_t data_type; - char *name; - uint32_t span_id; - uint32_t chan_count; - ftdm_span_flag_t flags; - struct ftdm_io_interface *fio; - fio_event_cb_t event_callback; - ftdm_mutex_t *mutex; - ftdm_trunk_type_t trunk_type; - ftdm_analog_start_type_t start_type; - ftdm_signal_type_t signal_type; - void *signal_data; - fio_signal_cb_t signal_cb; - ftdm_event_t event_header; - char last_error[256]; - char tone_map[FTDM_TONEMAP_INVALID+1][FTDM_TONEMAP_LEN]; - teletone_tone_map_t tone_detect_map[FTDM_TONEMAP_INVALID+1]; - teletone_multi_tone_t tone_finder[FTDM_TONEMAP_INVALID+1]; - ftdm_channel_t *channels[FTDM_MAX_CHANNELS_SPAN+1]; - fio_channel_outgoing_call_t outgoing_call; - fio_channel_set_sig_status_t set_channel_sig_status; - fio_channel_get_sig_status_t get_channel_sig_status; - fio_span_set_sig_status_t set_span_sig_status; - fio_span_get_sig_status_t get_span_sig_status; - fio_channel_request_t channel_request; - ftdm_span_start_t start; - ftdm_span_stop_t stop; - void *mod_data; - char *type; - char *dtmf_hangup; - size_t dtmf_hangup_len; - ftdm_state_map_t *state_map; - ftdm_caller_data_t default_caller_data; - ftdm_queue_t *pendingchans; - struct ftdm_span *next; -}; - -struct ftdm_group { - char *name; - uint32_t group_id; - uint32_t chan_count; - ftdm_channel_t *channels[FTDM_MAX_CHANNELS_GROUP]; - uint32_t last_used_index; - ftdm_mutex_t *mutex; - struct ftdm_group *next; -}; - -FT_DECLARE_DATA extern ftdm_logger_t ftdm_log; - typedef enum { FTDM_CRASH_NEVER = 0, FTDM_CRASH_ON_ASSERT } ftdm_crash_policy_t; -FT_DECLARE_DATA extern ftdm_crash_policy_t g_ftdm_crash_policy; +/*! + \brief Signaling status on a given span or specific channel on protocols that support it + */ +typedef enum { + /* The signaling link is down (no d-chans up in the span/group, MFC-R2 bit pattern unidentified) */ + FTDM_SIG_STATE_DOWN, + /* The signaling link is suspended (MFC-R2 bit pattern blocked, ss7 blocked?) */ + FTDM_SIG_STATE_SUSPENDED, + /* The signaling link is ready and calls can be placed */ + FTDM_SIG_STATE_UP, + /* Invalid status */ + FTDM_SIG_STATE_INVALID +} ftdm_signaling_status_t; +#define SIGSTATUS_STRINGS "DOWN", "SUSPENDED", "UP", "INVALID" +FTDM_STR2ENUM_P(ftdm_str2ftdm_signaling_status, ftdm_signaling_status2str, ftdm_signaling_status_t) + +typedef enum { + FTDM_NO_FLAGS = 0, + FTDM_READ = (1 << 0), + FTDM_WRITE = (1 << 1), + FTDM_EVENTS = (1 << 2) +} ftdm_wait_flag_t; + +typedef struct ftdm_conf_parameter { + const char *var; + const char *val; +} ftdm_conf_parameter_t; + +typedef enum { + FTDM_COMMAND_NOOP, + FTDM_COMMAND_SET_INTERVAL, + FTDM_COMMAND_GET_INTERVAL, + FTDM_COMMAND_SET_CODEC, + FTDM_COMMAND_GET_CODEC, + FTDM_COMMAND_SET_NATIVE_CODEC, + FTDM_COMMAND_GET_NATIVE_CODEC, + FTDM_COMMAND_ENABLE_DTMF_DETECT, + FTDM_COMMAND_DISABLE_DTMF_DETECT, + FTDM_COMMAND_SEND_DTMF, + FTDM_COMMAND_SET_DTMF_ON_PERIOD, + FTDM_COMMAND_GET_DTMF_ON_PERIOD, + FTDM_COMMAND_SET_DTMF_OFF_PERIOD, + FTDM_COMMAND_GET_DTMF_OFF_PERIOD, + FTDM_COMMAND_GENERATE_RING_ON, + FTDM_COMMAND_GENERATE_RING_OFF, + FTDM_COMMAND_OFFHOOK, + FTDM_COMMAND_ONHOOK, + FTDM_COMMAND_FLASH, + FTDM_COMMAND_WINK, + FTDM_COMMAND_ENABLE_PROGRESS_DETECT, + FTDM_COMMAND_DISABLE_PROGRESS_DETECT, + FTDM_COMMAND_TRACE_INPUT, + FTDM_COMMAND_TRACE_OUTPUT, + FTDM_COMMAND_ENABLE_CALLERID_DETECT, + FTDM_COMMAND_DISABLE_CALLERID_DETECT, + FTDM_COMMAND_ENABLE_ECHOCANCEL, + FTDM_COMMAND_DISABLE_ECHOCANCEL, + FTDM_COMMAND_ENABLE_ECHOTRAIN, + FTDM_COMMAND_DISABLE_ECHOTRAIN, + FTDM_COMMAND_SET_CAS_BITS, + FTDM_COMMAND_GET_CAS_BITS, + FTDM_COMMAND_SET_RX_GAIN, + FTDM_COMMAND_GET_RX_GAIN, + FTDM_COMMAND_SET_TX_GAIN, + FTDM_COMMAND_GET_TX_GAIN, + FTDM_COMMAND_FLUSH_TX_BUFFERS, + FTDM_COMMAND_FLUSH_RX_BUFFERS, + FTDM_COMMAND_FLUSH_BUFFERS, + FTDM_COMMAND_SET_PRE_BUFFER_SIZE, + FTDM_COMMAND_SET_LINK_STATUS, + FTDM_COMMAND_GET_LINK_STATUS, + FTDM_COMMAND_ENABLE_LOOP, + FTDM_COMMAND_DISABLE_LOOP, + FTDM_COMMAND_COUNT +} ftdm_command_t; + + typedef void *(*ftdm_malloc_func_t)(void *pool, ftdm_size_t len); typedef void *(*ftdm_calloc_func_t)(void *pool, ftdm_size_t elements, ftdm_size_t len); @@ -703,7 +310,93 @@ typedef struct ftdm_memory_handler { ftdm_free_func_t free; } ftdm_memory_handler_t; -FT_DECLARE_DATA extern ftdm_memory_handler_t g_ftdm_mem_handler; +#define FIO_CHANNEL_REQUEST_ARGS (ftdm_span_t *span, uint32_t chan_id, ftdm_direction_t direction, ftdm_caller_data_t *caller_data, ftdm_channel_t **ftdmchan) +#define FIO_CHANNEL_OUTGOING_CALL_ARGS (ftdm_channel_t *ftdmchan) +#define FIO_CHANNEL_SET_SIG_STATUS_ARGS (ftdm_channel_t *ftdmchan, ftdm_signaling_status_t status) +#define FIO_CHANNEL_GET_SIG_STATUS_ARGS (ftdm_channel_t *ftdmchan, ftdm_signaling_status_t *status) +#define FIO_SPAN_SET_SIG_STATUS_ARGS (ftdm_span_t *span, ftdm_signaling_status_t status) +#define FIO_SPAN_GET_SIG_STATUS_ARGS (ftdm_span_t *span, ftdm_signaling_status_t *status) +#define FIO_SPAN_POLL_EVENT_ARGS (ftdm_span_t *span, uint32_t ms) +#define FIO_SPAN_NEXT_EVENT_ARGS (ftdm_span_t *span, ftdm_event_t **event) +#define FIO_SIGNAL_CB_ARGS (ftdm_sigmsg_t *sigmsg) +#define FIO_EVENT_CB_ARGS (ftdm_channel_t *ftdmchan, ftdm_event_t *event) +#define FIO_CONFIGURE_SPAN_ARGS (ftdm_span_t *span, const char *str, ftdm_chan_type_t type, char *name, char *number) +#define FIO_CONFIGURE_ARGS (const char *category, const char *var, const char *val, int lineno) +#define FIO_OPEN_ARGS (ftdm_channel_t *ftdmchan) +#define FIO_CLOSE_ARGS (ftdm_channel_t *ftdmchan) +#define FIO_CHANNEL_DESTROY_ARGS (ftdm_channel_t *ftdmchan) +#define FIO_SPAN_DESTROY_ARGS (ftdm_span_t *span) +#define FIO_COMMAND_ARGS (ftdm_channel_t *ftdmchan, ftdm_command_t command, void *obj) +#define FIO_WAIT_ARGS (ftdm_channel_t *ftdmchan, ftdm_wait_flag_t *flags, int32_t to) +#define FIO_GET_ALARMS_ARGS (ftdm_channel_t *ftdmchan) +#define FIO_READ_ARGS (ftdm_channel_t *ftdmchan, void *data, ftdm_size_t *datalen) +#define FIO_WRITE_ARGS (ftdm_channel_t *ftdmchan, void *data, ftdm_size_t *datalen) +#define FIO_IO_LOAD_ARGS (ftdm_io_interface_t **fio) +#define FIO_IO_UNLOAD_ARGS (void) +#define FIO_SIG_LOAD_ARGS (void) +#define FIO_SIG_CONFIGURE_ARGS (ftdm_span_t *span, fio_signal_cb_t sig_cb, va_list ap) +#define FIO_CONFIGURE_SPAN_SIGNALING_ARGS (ftdm_span_t *span, fio_signal_cb_t sig_cb, ftdm_conf_parameter_t *ftdm_parameters) +#define FIO_SIG_UNLOAD_ARGS (void) +#define FIO_API_ARGS (ftdm_stream_handle_t *stream, const char *data) + +typedef ftdm_status_t (*fio_channel_request_t) FIO_CHANNEL_REQUEST_ARGS ; +typedef ftdm_status_t (*fio_channel_outgoing_call_t) FIO_CHANNEL_OUTGOING_CALL_ARGS ; +typedef ftdm_status_t (*fio_channel_set_sig_status_t) FIO_CHANNEL_SET_SIG_STATUS_ARGS; +typedef ftdm_status_t (*fio_channel_get_sig_status_t) FIO_CHANNEL_GET_SIG_STATUS_ARGS; +typedef ftdm_status_t (*fio_span_set_sig_status_t) FIO_SPAN_SET_SIG_STATUS_ARGS; +typedef ftdm_status_t (*fio_span_get_sig_status_t) FIO_SPAN_GET_SIG_STATUS_ARGS; +typedef ftdm_status_t (*fio_span_poll_event_t) FIO_SPAN_POLL_EVENT_ARGS ; +typedef ftdm_status_t (*fio_span_next_event_t) FIO_SPAN_NEXT_EVENT_ARGS ; +typedef ftdm_status_t (*fio_signal_cb_t) FIO_SIGNAL_CB_ARGS ; +typedef ftdm_status_t (*fio_event_cb_t) FIO_EVENT_CB_ARGS ; +typedef ftdm_status_t (*fio_configure_span_t) FIO_CONFIGURE_SPAN_ARGS ; +typedef ftdm_status_t (*fio_configure_t) FIO_CONFIGURE_ARGS ; +typedef ftdm_status_t (*fio_open_t) FIO_OPEN_ARGS ; +typedef ftdm_status_t (*fio_close_t) FIO_CLOSE_ARGS ; +typedef ftdm_status_t (*fio_channel_destroy_t) FIO_CHANNEL_DESTROY_ARGS ; +typedef ftdm_status_t (*fio_span_destroy_t) FIO_SPAN_DESTROY_ARGS ; +typedef ftdm_status_t (*fio_get_alarms_t) FIO_GET_ALARMS_ARGS ; +typedef ftdm_status_t (*fio_command_t) FIO_COMMAND_ARGS ; +typedef ftdm_status_t (*fio_wait_t) FIO_WAIT_ARGS ; +typedef ftdm_status_t (*fio_read_t) FIO_READ_ARGS ; +typedef ftdm_status_t (*fio_write_t) FIO_WRITE_ARGS ; +typedef ftdm_status_t (*fio_io_load_t) FIO_IO_LOAD_ARGS ; +typedef ftdm_status_t (*fio_sig_load_t) FIO_SIG_LOAD_ARGS ; +typedef ftdm_status_t (*fio_sig_configure_t) FIO_SIG_CONFIGURE_ARGS ; +typedef ftdm_status_t (*fio_configure_span_signaling_t) FIO_CONFIGURE_SPAN_SIGNALING_ARGS ; +typedef ftdm_status_t (*fio_io_unload_t) FIO_IO_UNLOAD_ARGS ; +typedef ftdm_status_t (*fio_sig_unload_t) FIO_SIG_UNLOAD_ARGS ; +typedef ftdm_status_t (*fio_api_t) FIO_API_ARGS ; + + +#define FIO_CHANNEL_REQUEST_FUNCTION(name) ftdm_status_t name FIO_CHANNEL_REQUEST_ARGS +#define FIO_CHANNEL_OUTGOING_CALL_FUNCTION(name) ftdm_status_t name FIO_CHANNEL_OUTGOING_CALL_ARGS +#define FIO_CHANNEL_SET_SIG_STATUS_FUNCTION(name) ftdm_status_t name FIO_CHANNEL_SET_SIG_STATUS_ARGS +#define FIO_CHANNEL_GET_SIG_STATUS_FUNCTION(name) ftdm_status_t name FIO_CHANNEL_GET_SIG_STATUS_ARGS +#define FIO_SPAN_SET_SIG_STATUS_FUNCTION(name) ftdm_status_t name FIO_SPAN_SET_SIG_STATUS_ARGS +#define FIO_SPAN_GET_SIG_STATUS_FUNCTION(name) ftdm_status_t name FIO_SPAN_GET_SIG_STATUS_ARGS +#define FIO_SPAN_POLL_EVENT_FUNCTION(name) ftdm_status_t name FIO_SPAN_POLL_EVENT_ARGS +#define FIO_SPAN_NEXT_EVENT_FUNCTION(name) ftdm_status_t name FIO_SPAN_NEXT_EVENT_ARGS +#define FIO_SIGNAL_CB_FUNCTION(name) ftdm_status_t name FIO_SIGNAL_CB_ARGS +#define FIO_EVENT_CB_FUNCTION(name) ftdm_status_t name FIO_EVENT_CB_ARGS +#define FIO_CONFIGURE_SPAN_FUNCTION(name) ftdm_status_t name FIO_CONFIGURE_SPAN_ARGS +#define FIO_CONFIGURE_FUNCTION(name) ftdm_status_t name FIO_CONFIGURE_ARGS +#define FIO_OPEN_FUNCTION(name) ftdm_status_t name FIO_OPEN_ARGS +#define FIO_CLOSE_FUNCTION(name) ftdm_status_t name FIO_CLOSE_ARGS +#define FIO_CHANNEL_DESTROY_FUNCTION(name) ftdm_status_t name FIO_CHANNEL_DESTROY_ARGS +#define FIO_SPAN_DESTROY_FUNCTION(name) ftdm_status_t name FIO_SPAN_DESTROY_ARGS +#define FIO_GET_ALARMS_FUNCTION(name) ftdm_status_t name FIO_GET_ALARMS_ARGS +#define FIO_COMMAND_FUNCTION(name) ftdm_status_t name FIO_COMMAND_ARGS +#define FIO_WAIT_FUNCTION(name) ftdm_status_t name FIO_WAIT_ARGS +#define FIO_READ_FUNCTION(name) ftdm_status_t name FIO_READ_ARGS +#define FIO_WRITE_FUNCTION(name) ftdm_status_t name FIO_WRITE_ARGS +#define FIO_IO_LOAD_FUNCTION(name) ftdm_status_t name FIO_IO_LOAD_ARGS +#define FIO_SIG_LOAD_FUNCTION(name) ftdm_status_t name FIO_SIG_LOAD_ARGS +#define FIO_SIG_CONFIGURE_FUNCTION(name) ftdm_status_t name FIO_SIG_CONFIGURE_ARGS +#define FIO_CONFIGURE_SPAN_SIGNALING_FUNCTION(name) ftdm_status_t name FIO_CONFIGURE_SPAN_SIGNALING_ARGS +#define FIO_IO_UNLOAD_FUNCTION(name) ftdm_status_t name FIO_IO_UNLOAD_ARGS +#define FIO_SIG_UNLOAD_FUNCTION(name) ftdm_status_t name FIO_SIG_UNLOAD_ARGS +#define FIO_API_FUNCTION(name) ftdm_status_t name FIO_API_ARGS struct ftdm_io_interface { const char *name; @@ -723,61 +416,52 @@ struct ftdm_io_interface { fio_api_t api; }; +typedef enum { + FTDM_CODEC_ULAW = 0, + FTDM_CODEC_ALAW = 8, + FTDM_CODEC_SLIN = 10, + FTDM_CODEC_NONE = (1 << 30) +} ftdm_codec_t; + +typedef enum { + FTDM_CHANNEL_INDICATE_RING, + FTDM_CHANNEL_INDICATE_PROCEED, + FTDM_CHANNEL_INDICATE_PROGRESS, + FTDM_CHANNEL_INDICATE_BUSY, +} ftdm_channel_indication_t; + +typedef enum { + FTDM_FALSE, + FTDM_TRUE +} ftdm_bool_t; + /*! \brief Override the default queue handler */ FT_DECLARE(ftdm_status_t) ftdm_global_set_queue_handler(ftdm_queue_handler_t *handler); -/*! \brief Duplicate string */ -FT_DECLARE(char *) ftdm_strdup(const char *str); -FT_DECLARE(char *) ftdm_strndup(const char *str, ftdm_size_t inlen); +FT_DECLARE(ftdm_status_t) ftdm_channel_call_answer(ftdm_channel_t *ftdmchan); +FT_DECLARE(ftdm_status_t) ftdm_channel_call_place(ftdm_channel_t *ftdmchan); +FT_DECLARE(ftdm_status_t) ftdm_channel_call_indicate(ftdm_channel_t *ftdmchan, ftdm_channel_indication_t indication); +FT_DECLARE(ftdm_status_t) ftdm_channel_call_hangup(ftdm_channel_t *ftdmchan); +FT_DECLARE(ftdm_bool_t) ftdm_channel_call_active(const ftdm_channel_t *ftdmchan); -FT_DECLARE(ftdm_size_t) ftdm_fsk_modulator_generate_bit(ftdm_fsk_modulator_t *fsk_trans, int8_t bit, int16_t *buf, ftdm_size_t buflen); -FT_DECLARE(int32_t) ftdm_fsk_modulator_generate_carrier_bits(ftdm_fsk_modulator_t *fsk_trans, uint32_t bits); -FT_DECLARE(void) ftdm_fsk_modulator_generate_chan_sieze(ftdm_fsk_modulator_t *fsk_trans); -FT_DECLARE(void) ftdm_fsk_modulator_send_data(ftdm_fsk_modulator_t *fsk_trans); -#define ftdm_fsk_modulator_send_all(_it) ftdm_fsk_modulator_generate_chan_sieze(_it); \ - ftdm_fsk_modulator_generate_carrier_bits(_it, _it->carrier_bits_start); \ - ftdm_fsk_modulator_send_data(_it); \ - ftdm_fsk_modulator_generate_carrier_bits(_it, _it->carrier_bits_stop) - -FT_DECLARE(ftdm_status_t) ftdm_fsk_modulator_init(ftdm_fsk_modulator_t *fsk_trans, - fsk_modem_types_t modem_type, - uint32_t sample_rate, - ftdm_fsk_data_state_t *fsk_data, - float db_level, - uint32_t carrier_bits_start, - uint32_t carrier_bits_stop, - uint32_t chan_sieze_bits, - ftdm_fsk_write_sample_t write_sample_callback, - void *user_data); -FT_DECLARE(int8_t) ftdm_bitstream_get_bit(ftdm_bitstream_t *bsp); -FT_DECLARE(void) ftdm_bitstream_init(ftdm_bitstream_t *bsp, uint8_t *data, uint32_t datalen, ftdm_endian_t endian, uint8_t ss); -FT_DECLARE(ftdm_status_t) ftdm_fsk_data_parse(ftdm_fsk_data_state_t *state, ftdm_size_t *type, char **data, ftdm_size_t *len); -FT_DECLARE(ftdm_status_t) ftdm_fsk_demod_feed(ftdm_fsk_data_state_t *state, int16_t *data, size_t samples); -FT_DECLARE(ftdm_status_t) ftdm_fsk_demod_destroy(ftdm_fsk_data_state_t *state); -FT_DECLARE(int) ftdm_fsk_demod_init(ftdm_fsk_data_state_t *state, int rate, uint8_t *buf, size_t bufsize); -FT_DECLARE(ftdm_status_t) ftdm_fsk_data_init(ftdm_fsk_data_state_t *state, uint8_t *data, uint32_t datalen); -FT_DECLARE(ftdm_status_t) ftdm_fsk_data_add_mdmf(ftdm_fsk_data_state_t *state, ftdm_mdmf_type_t type, const uint8_t *data, uint32_t datalen); -FT_DECLARE(ftdm_status_t) ftdm_fsk_data_add_checksum(ftdm_fsk_data_state_t *state); -FT_DECLARE(ftdm_status_t) ftdm_fsk_data_add_sdmf(ftdm_fsk_data_state_t *state, const char *date, char *number); -FT_DECLARE(ftdm_status_t) ftdm_channel_outgoing_call(ftdm_channel_t *ftdmchan); FT_DECLARE(ftdm_status_t) ftdm_channel_set_sig_status(ftdm_channel_t *ftdmchan, ftdm_signaling_status_t status); FT_DECLARE(ftdm_status_t) ftdm_channel_get_sig_status(ftdm_channel_t *ftdmchan, ftdm_signaling_status_t *status); FT_DECLARE(ftdm_status_t) ftdm_span_set_sig_status(ftdm_span_t *span, ftdm_signaling_status_t status); FT_DECLARE(ftdm_status_t) ftdm_span_get_sig_status(ftdm_span_t *span, ftdm_signaling_status_t *status); + FT_DECLARE(void) ftdm_channel_rotate_tokens(ftdm_channel_t *ftdmchan); FT_DECLARE(void) ftdm_channel_clear_detected_tones(ftdm_channel_t *ftdmchan); FT_DECLARE(void) ftdm_channel_clear_needed_tones(ftdm_channel_t *ftdmchan); + +FT_DECLARE(const char *) ftdm_channel_get_last_error(const ftdm_channel_t *ftdmchan); FT_DECLARE(ftdm_status_t) ftdm_channel_get_alarms(ftdm_channel_t *ftdmchan); -FT_DECLARE(ftdm_status_t) ftdm_channel_send_fsk_data(ftdm_channel_t *ftdmchan, ftdm_fsk_data_state_t *fsk_data, float db_level); FT_DECLARE(ftdm_status_t) ftdm_channel_clear_token(ftdm_channel_t *ftdmchan, const char *token); FT_DECLARE(void) ftdm_channel_replace_token(ftdm_channel_t *ftdmchan, const char *old_token, const char *new_token); FT_DECLARE(ftdm_status_t) ftdm_channel_add_token(ftdm_channel_t *ftdmchan, char *token, int end); -FT_DECLARE(ftdm_status_t) ftdm_channel_set_state(ftdm_channel_t *ftdmchan, ftdm_channel_state_t state, int lock); -FT_DECLARE(ftdm_status_t) ftdm_span_load_tones(ftdm_span_t *span, const char *mapname); + FT_DECLARE(ftdm_size_t) ftdm_channel_dequeue_dtmf(ftdm_channel_t *ftdmchan, char *dtmf, ftdm_size_t len); FT_DECLARE(ftdm_status_t) ftdm_channel_queue_dtmf(ftdm_channel_t *ftdmchan, const char *dtmf); FT_DECLARE(void) ftdm_channel_flush_dtmf(ftdm_channel_t *ftdmchan); -FT_DECLARE(ftdm_time_t) ftdm_current_time_in_ms(void); FT_DECLARE(ftdm_status_t) ftdm_span_poll_event(ftdm_span_t *span, uint32_t ms); FT_DECLARE(ftdm_status_t) ftdm_span_next_event(ftdm_span_t *span, ftdm_event_t **event); FT_DECLARE(ftdm_status_t) ftdm_span_find(uint32_t id, ftdm_span_t **span); @@ -798,16 +482,16 @@ FT_DECLARE(ftdm_status_t) ftdm_group_channel_use_count(ftdm_group_t *group, uint FT_DECLARE(ftdm_status_t) ftdm_channel_open_by_span(uint32_t span_id, ftdm_direction_t direction, ftdm_caller_data_t *caller_data, ftdm_channel_t **ftdmchan); FT_DECLARE(ftdm_status_t) ftdm_channel_open_by_group(uint32_t group_id, ftdm_direction_t direction, ftdm_caller_data_t *caller_data, ftdm_channel_t **ftdmchan); FT_DECLARE(ftdm_status_t) ftdm_channel_close(ftdm_channel_t **ftdmchan); -FT_DECLARE(ftdm_status_t) ftdm_channel_done(ftdm_channel_t *ftdmchan); -FT_DECLARE(ftdm_status_t) ftdm_channel_use(ftdm_channel_t *ftdmchan); FT_DECLARE(ftdm_status_t) ftdm_channel_command(ftdm_channel_t *ftdmchan, ftdm_command_t command, void *obj); FT_DECLARE(ftdm_status_t) ftdm_channel_wait(ftdm_channel_t *ftdmchan, ftdm_wait_flag_t *flags, int32_t to); FT_DECLARE(ftdm_status_t) ftdm_channel_read(ftdm_channel_t *ftdmchan, void *data, ftdm_size_t *datalen); -FT_DECLARE(void) ftdm_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t divisor); FT_DECLARE(ftdm_status_t) ftdm_channel_write(ftdm_channel_t *ftdmchan, void *data, ftdm_size_t datasize, ftdm_size_t *datalen); FT_DECLARE(ftdm_status_t) ftdm_channel_add_var(ftdm_channel_t *ftdmchan, const char *var_name, const char *value); FT_DECLARE(const char *) ftdm_channel_get_var(ftdm_channel_t *ftdmchan, const char *var_name); FT_DECLARE(ftdm_status_t) ftdm_channel_clear_vars(ftdm_channel_t *ftdmchan); +FT_DECLARE(uint32_t) ftdm_channel_get_span_id(const ftdm_channel_t *ftdmchan); +FT_DECLARE(uint32_t) ftdm_channel_get_id(const ftdm_channel_t *ftdmchan); +FT_DECLARE(uint32_t) ftdm_channel_get_ph_id(const ftdm_channel_t *ftdmchan); FT_DECLARE(ftdm_status_t) ftdm_global_init(void); FT_DECLARE(ftdm_status_t) ftdm_global_configuration(void); FT_DECLARE(ftdm_status_t) ftdm_global_destroy(void); @@ -815,37 +499,27 @@ FT_DECLARE(ftdm_status_t) ftdm_global_set_memory_handler(ftdm_memory_handler_t * FT_DECLARE(void) ftdm_global_set_crash_policy(ftdm_crash_policy_t policy); FT_DECLARE(void) ftdm_global_set_logger(ftdm_logger_t logger); FT_DECLARE(void) ftdm_global_set_default_logger(int level); -FT_DECLARE(uint32_t) ftdm_separate_string(char *buf, char delim, char **array, int arraylen); -FT_DECLARE(void) print_bits(uint8_t *b, int bl, char *buf, int blen, int e, uint8_t ss); -FT_DECLARE(void) print_hex_bytes(uint8_t *data, ftdm_size_t dlen, char *buf, ftdm_size_t blen); -FT_DECLARE_NONSTD(int) ftdm_hash_equalkeys(void *k1, void *k2); -FT_DECLARE_NONSTD(uint32_t) ftdm_hash_hashfromstring(void *ky); -FT_DECLARE(uint32_t) ftdm_running(void); -FT_DECLARE(ftdm_status_t) ftdm_channel_complete_state(ftdm_channel_t *ftdmchan); +FT_DECLARE(ftdm_bool_t) ftdm_running(void); FT_DECLARE(ftdm_status_t) ftdm_channel_init(ftdm_channel_t *ftdmchan); -FT_DECLARE(int) ftdm_load_modules(void); -FT_DECLARE(ftdm_status_t) ftdm_unload_modules(void); FT_DECLARE(ftdm_status_t) ftdm_configure_span(const char *type, ftdm_span_t *span, fio_signal_cb_t sig_cb, ...); FT_DECLARE(ftdm_status_t) ftdm_configure_span_signaling(const char *type, ftdm_span_t *span, fio_signal_cb_t sig_cb, ftdm_conf_parameter_t *parameters); FT_DECLARE(ftdm_status_t) ftdm_span_start(ftdm_span_t *span); FT_DECLARE(ftdm_status_t) ftdm_span_stop(ftdm_span_t *span); -FT_DECLARE(ftdm_status_t) ftdm_span_send_signal(ftdm_span_t *span, ftdm_sigmsg_t *sigmsg); -FT_DECLARE(char *) ftdm_build_dso_path(const char *name, char *path, ftdm_size_t len); FT_DECLARE(ftdm_status_t) ftdm_global_add_io_interface(ftdm_io_interface_t *io_interface); -FT_DECLARE(int) ftdm_load_module(const char *name); -FT_DECLARE(int) ftdm_load_module_assume(const char *name); FT_DECLARE(ftdm_status_t) ftdm_span_find_by_name(const char *name, ftdm_span_t **span); +FT_DECLARE(uint32_t) ftdm_span_get_id(const ftdm_span_t *span); +FT_DECLARE(const char *) ftdm_span_get_name(const ftdm_span_t *span); FT_DECLARE(char *) ftdm_api_execute(const char *type, const char *cmd); -FT_DECLARE(int) ftdm_vasprintf(char **ret, const char *fmt, va_list ap); FT_DECLARE(ftdm_status_t) ftdm_channel_set_caller_data(ftdm_channel_t *ftdmchan, ftdm_caller_data_t *caller_data); FT_DECLARE(void) ftdm_cpu_monitor_disable(void); FT_DECLARE(ftdm_status_t) ftdm_conf_node_create(const char *name, ftdm_conf_node_t **node, ftdm_conf_node_t *parent); FT_DECLARE(ftdm_status_t) ftdm_conf_node_add_param(ftdm_conf_node_t *node, const char *param, const char *val); FT_DECLARE(ftdm_status_t) ftdm_conf_node_destroy(ftdm_conf_node_t *node); - - FT_DECLARE(ftdm_status_t) ftdm_configure_span_channels(ftdm_span_t *span, const char *str, ftdm_channel_config_t *chan_config, unsigned *configured); +#define FIO_CODEC_ARGS (void *data, ftdm_size_t max, ftdm_size_t *datalen) +#define FIO_CODEC_FUNCTION(name) FT_DECLARE_NONSTD(ftdm_status_t) name FIO_CODEC_ARGS +typedef ftdm_status_t (*fio_codec_t) FIO_CODEC_ARGS ; FIO_CODEC_FUNCTION(fio_slin2ulaw); FIO_CODEC_FUNCTION(fio_ulaw2slin); FIO_CODEC_FUNCTION(fio_slin2alaw); @@ -853,129 +527,29 @@ FIO_CODEC_FUNCTION(fio_alaw2slin); FIO_CODEC_FUNCTION(fio_ulaw2alaw); FIO_CODEC_FUNCTION(fio_alaw2ulaw); -#ifdef DEBUG_LOCKS -#define ftdm_mutex_lock(_x) printf("++++++lock %s:%d\n", __FILE__, __LINE__) && _ftdm_mutex_lock(_x) -#define ftdm_mutex_trylock(_x) printf("++++++try %s:%d\n", __FILE__, __LINE__) && _ftdm_mutex_trylock(_x) -#define ftdm_mutex_unlock(_x) printf("------unlock %s:%d\n", __FILE__, __LINE__) && _ftdm_mutex_unlock(_x) -#else -#define ftdm_mutex_lock(_x) _ftdm_mutex_lock(_x) -#define ftdm_mutex_trylock(_x) _ftdm_mutex_trylock(_x) -#define ftdm_mutex_unlock(_x) _ftdm_mutex_unlock(_x) -#endif -/*! - \brief Assert condition -*/ -#define ftdm_assert(assertion, msg) \ - if (!(assertion)) { \ - ftdm_log(FTDM_LOG_CRIT, msg); \ - if (g_ftdm_crash_policy & FTDM_CRASH_ON_ASSERT) { \ - ftdm_abort(); \ - } \ - } +FT_DECLARE_DATA extern ftdm_logger_t ftdm_log; -/*! - \brief Assert condition and return -*/ -#define ftdm_assert_return(assertion, retval, msg) \ - if (!(assertion)) { \ - ftdm_log(FTDM_LOG_CRIT, msg); \ - if (g_ftdm_crash_policy & FTDM_CRASH_ON_ASSERT) { \ - ftdm_abort(); \ - } else { \ - return retval; \ - } \ - } +#define FTDM_PRE __FILE__, __FUNCTION__, __LINE__ +#define FTDM_LOG_LEVEL_DEBUG 7 +#define FTDM_LOG_LEVEL_INFO 6 +#define FTDM_LOG_LEVEL_NOTICE 5 +#define FTDM_LOG_LEVEL_WARNING 4 +#define FTDM_LOG_LEVEL_ERROR 3 +#define FTDM_LOG_LEVEL_CRIT 2 +#define FTDM_LOG_LEVEL_ALERT 1 +#define FTDM_LOG_LEVEL_EMERG 0 -/*! - \brief Allocate uninitialized memory - \command chunksize the chunk size -*/ -#define ftdm_malloc(chunksize) g_ftdm_mem_handler.malloc(g_ftdm_mem_handler.pool, chunksize) +#define FTDM_LOG_DEBUG FTDM_PRE, FTDM_LOG_LEVEL_DEBUG +#define FTDM_LOG_INFO FTDM_PRE, FTDM_LOG_LEVEL_INFO +#define FTDM_LOG_NOTICE FTDM_PRE, FTDM_LOG_LEVEL_NOTICE +#define FTDM_LOG_WARNING FTDM_PRE, FTDM_LOG_LEVEL_WARNING +#define FTDM_LOG_ERROR FTDM_PRE, FTDM_LOG_LEVEL_ERROR +#define FTDM_LOG_CRIT FTDM_PRE, FTDM_LOG_LEVEL_CRIT +#define FTDM_LOG_ALERT FTDM_PRE, FTDM_LOG_LEVEL_ALERT +#define FTDM_LOG_EMERG FTDM_PRE, FTDM_LOG_LEVEL_EMERG -/*! - \brief Reallocates memory - \command buff the buffer - \command chunksize the chunk size -*/ -#define ftdm_realloc(buff, chunksize) g_ftdm_mem_handler.realloc(g_ftdm_mem_handler.pool, buff, chunksize) - -/*! - \brief Allocate initialized memory - \command chunksize the chunk size -*/ -#define ftdm_calloc(elements, chunksize) g_ftdm_mem_handler.calloc(g_ftdm_mem_handler.pool, elements, chunksize) - -/*! - \brief Free chunk of memory - \command chunksize the chunk size -*/ -#define ftdm_free(chunk) g_ftdm_mem_handler.free(g_ftdm_mem_handler.pool, chunk) - -/*! - \brief Free a pointer and set it to NULL unless it already is NULL - \command it the pointer -*/ -#define ftdm_safe_free(it) if (it) { ftdm_free(it); it = NULL; } - -/*! - \brief Socket the given socket - \command it the socket -*/ -#define ftdm_socket_close(it) if (it > -1) { close(it); it = -1;} - -#define ftdm_array_len(array) sizeof(array)/sizeof(array[0]) - -static __inline__ void ftdm_abort(void) -{ -#ifdef __cplusplus - ::abort(); -#else - abort(); -#endif -} - -static __inline__ void ftdm_set_state_all(ftdm_span_t *span, ftdm_channel_state_t state) -{ - uint32_t j; - ftdm_mutex_lock(span->mutex); - for(j = 1; j <= span->chan_count; j++) { - ftdm_set_state_locked((span->channels[j]), state); - } - ftdm_mutex_unlock(span->mutex); -} - -static __inline__ int ftdm_check_state_all(ftdm_span_t *span, ftdm_channel_state_t state) -{ - uint32_t j; - for(j = 1; j <= span->chan_count; j++) { - if (span->channels[j]->state != state || ftdm_test_flag(span->channels[j], FTDM_CHANNEL_STATE_CHANGE)) { - return 0; - } - } - - return 1; -} - -static __inline__ void ftdm_set_flag_all(ftdm_span_t *span, uint32_t flag) -{ - uint32_t j; - ftdm_mutex_lock(span->mutex); - for(j = 1; j <= span->chan_count; j++) { - ftdm_set_flag_locked((span->channels[j]), flag); - } - ftdm_mutex_unlock(span->mutex); -} - -static __inline__ void ftdm_clear_flag_all(ftdm_span_t *span, uint32_t flag) -{ - uint32_t j; - ftdm_mutex_lock(span->mutex); - for(j = 1; j <= span->chan_count; j++) { - ftdm_clear_flag_locked((span->channels[j]), flag); - } - ftdm_mutex_unlock(span->mutex); -} +#define FTDM_TAG_END NULL #ifdef __cplusplus } /* extern C */ diff --git a/libs/freetdm/src/include/ftdm_declare.h b/libs/freetdm/src/include/ftdm_declare.h new file mode 100644 index 0000000000..b12852d854 --- /dev/null +++ b/libs/freetdm/src/include/ftdm_declare.h @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2010, Sangoma Technologies + * Moises Silva + * 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 __FTDM_DECLARE_H__ +#define __FTDM_DECLARE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* ######## start utility macros not to be used by customers, but needed in this header, may be move to another header ############ */ + +#ifndef __WINDOWS__ +#if defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32) || defined(_WIN64) +#define __WINDOWS__ +#endif +#endif + +#ifdef _MSC_VER +#if defined(FT_DECLARE_STATIC) +#define FT_DECLARE(type) type __stdcall +#define FT_DECLARE_NONSTD(type) type __cdecl +#define FT_DECLARE_DATA +#elif defined(FREETDM_EXPORTS) +#define FT_DECLARE(type) __declspec(dllexport) type __stdcall +#define FT_DECLARE_NONSTD(type) __declspec(dllexport) type __cdecl +#define FT_DECLARE_DATA __declspec(dllexport) +#else +#define FT_DECLARE(type) __declspec(dllimport) type __stdcall +#define FT_DECLARE_NONSTD(type) __declspec(dllimport) type __cdecl +#define FT_DECLARE_DATA __declspec(dllimport) +#endif +#define EX_DECLARE_DATA __declspec(dllexport) +#else +#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(HAVE_VISIBILITY) +#define FT_DECLARE(type) __attribute__((visibility("default"))) type +#define FT_DECLARE_NONSTD(type) __attribute__((visibility("default"))) type +#define FT_DECLARE_DATA __attribute__((visibility("default"))) +#else +#define FT_DECLARE(type) type +#define FT_DECLARE_NONSTD(type) type +#define FT_DECLARE_DATA +#endif +#define EX_DECLARE_DATA +#endif + +#define FTDM_STR2ENUM_P(_FUNC1, _FUNC2, _TYPE) FT_DECLARE(_TYPE) _FUNC1 (const char *name); FT_DECLARE(const char *) _FUNC2 (_TYPE type); +#define FTDM_STR2ENUM(_FUNC1, _FUNC2, _TYPE, _STRINGS, _MAX) \ + FT_DECLARE(_TYPE) _FUNC1 (const char *name) \ + { \ + int i; \ + _TYPE t = _MAX ; \ + \ + for (i = 0; i < _MAX ; i++) { \ + if (!strcasecmp(name, _STRINGS[i])) { \ + t = (_TYPE) i; \ + break; \ + } \ + } \ + \ + return t; \ + } \ + FT_DECLARE(const char *) _FUNC2 (_TYPE type) \ + { \ + if (type > _MAX) { \ + type = _MAX; \ + } \ + return _STRINGS[(int)type]; \ + } \ + +#ifdef WIN32 +#include +#define FTDM_INVALID_SOCKET INVALID_HANDLE_VALUE +typedef HANDLE ftdm_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; +#else +#define FTDM_INVALID_SOCKET -1 +typedef int ftdm_socket_t; +#include +#include +#include +#endif + +typedef struct ftdm_channel ftdm_channel_t; +typedef struct ftdm_span ftdm_span_t; +typedef struct ftdm_event ftdm_event_t; +typedef struct ftdm_conf_node ftdm_conf_node_t; +typedef struct ftdm_group ftdm_group_t; +typedef size_t ftdm_size_t; +typedef struct ftdm_sigmsg ftdm_sigmsg_t; +typedef struct ftdm_io_interface ftdm_io_interface_t; +typedef struct ftdm_stream_handle ftdm_stream_handle_t; +typedef struct ftdm_queue ftdm_queue_t; + +#ifdef __cplusplus +} /* extern C */ +#endif + +#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: + */ diff --git a/libs/freetdm/src/include/ftdm_os.h b/libs/freetdm/src/include/ftdm_os.h new file mode 100644 index 0000000000..78f484fa28 --- /dev/null +++ b/libs/freetdm/src/include/ftdm_os.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2010, Sangoma Technologies + * Moises Silva + * 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 __FTDM_OS_H__ +#define __FTDM_OS_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ftdm_declare.h" +#include "ftdm_threadmutex.h" + +#include + +#ifndef __WINDOWS__ +#include +#endif + +#ifdef __WINDOWS__ +#define ftdm_sleep(x) Sleep(x) +#else +#define ftdm_sleep(x) usleep(x * 1000) +#endif + +#define ftdm_copy_string(x,y,z) strncpy(x, y, z - 1) +#define ftdm_set_string(x,y) strncpy(x, y, sizeof(x)-1) +#define ftdm_strlen_zero(s) (!s || *s == '\0') +#define ftdm_strlen_zero_buf(s) (*s == '\0') + +#ifdef __cplusplus +} /* extern C */ +#endif + +#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: + */ diff --git a/libs/freetdm/src/include/ftdm_threadmutex.h b/libs/freetdm/src/include/ftdm_threadmutex.h index f67d2840fe..4d0030e260 100644 --- a/libs/freetdm/src/include/ftdm_threadmutex.h +++ b/libs/freetdm/src/include/ftdm_threadmutex.h @@ -30,6 +30,7 @@ #ifdef __cplusplus extern "C" { #endif + typedef struct ftdm_mutex ftdm_mutex_t; typedef struct ftdm_thread ftdm_thread_t; typedef struct ftdm_interrupt ftdm_interrupt_t; @@ -38,11 +39,20 @@ typedef void *(*ftdm_thread_function_t) (ftdm_thread_t *, void *); FT_DECLARE(ftdm_status_t) ftdm_thread_create_detached(ftdm_thread_function_t func, void *data); FT_DECLARE(ftdm_status_t) ftdm_thread_create_detached_ex(ftdm_thread_function_t func, void *data, ftdm_size_t stack_size); FT_DECLARE(void) ftdm_thread_override_default_stacksize(ftdm_size_t size); + + FT_DECLARE(ftdm_status_t) ftdm_mutex_create(ftdm_mutex_t **mutex); FT_DECLARE(ftdm_status_t) ftdm_mutex_destroy(ftdm_mutex_t **mutex); + +#define ftdm_mutex_lock(_x) _ftdm_mutex_lock(_x) FT_DECLARE(ftdm_status_t) _ftdm_mutex_lock(ftdm_mutex_t *mutex); + +#define ftdm_mutex_trylock(_x) _ftdm_mutex_trylock(_x) FT_DECLARE(ftdm_status_t) _ftdm_mutex_trylock(ftdm_mutex_t *mutex); + +#define ftdm_mutex_unlock(_x) _ftdm_mutex_unlock(_x) FT_DECLARE(ftdm_status_t) _ftdm_mutex_unlock(ftdm_mutex_t *mutex); + FT_DECLARE(ftdm_status_t) ftdm_interrupt_create(ftdm_interrupt_t **cond, ftdm_socket_t device); FT_DECLARE(ftdm_status_t) ftdm_interrupt_destroy(ftdm_interrupt_t **cond); FT_DECLARE(ftdm_status_t) ftdm_interrupt_signal(ftdm_interrupt_t *cond); diff --git a/libs/freetdm/src/include/fsk.h b/libs/freetdm/src/include/private/fsk.h similarity index 100% rename from libs/freetdm/src/include/fsk.h rename to libs/freetdm/src/include/private/fsk.h diff --git a/libs/freetdm/src/include/ftdm_buffer.h b/libs/freetdm/src/include/private/ftdm_buffer.h similarity index 100% rename from libs/freetdm/src/include/ftdm_buffer.h rename to libs/freetdm/src/include/private/ftdm_buffer.h diff --git a/libs/freetdm/src/include/ftdm_config.h b/libs/freetdm/src/include/private/ftdm_config.h similarity index 100% rename from libs/freetdm/src/include/ftdm_config.h rename to libs/freetdm/src/include/private/ftdm_config.h diff --git a/libs/freetdm/src/include/private/ftdm_core.h b/libs/freetdm/src/include/private/ftdm_core.h new file mode 100644 index 0000000000..13cfda2672 --- /dev/null +++ b/libs/freetdm/src/include/private/ftdm_core.h @@ -0,0 +1,697 @@ +/* + * Copyright (c) 2010, Sangoma Technologies + * Moises Silva + * 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 __PRIVATE_FTDM_CORE__ +#define __PRIVATE_FTDM_CORE__ + +#if !defined(_XOPEN_SOURCE) && !defined(__FreeBSD__) +#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 + +#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 +/* disable warning for zero length array in a struct */ +/* this will cause errors on c99 and ansi compliant compilers and will need to be fixed in the wanpipe header files */ +#pragma warning(disable:4706) +#pragma comment(lib, "Winmm") +#endif + +#define FTDM_THREAD_STACKSIZE 240 * 1024 +#define FTDM_ENUM_NAMES(_NAME, _STRINGS) static const char * _NAME [] = { _STRINGS , NULL }; + +#define ftdm_true(expr) \ + (expr && ( !strcasecmp(expr, "yes") || \ + !strcasecmp(expr, "on") || \ + !strcasecmp(expr, "true") || \ + !strcasecmp(expr, "enabled") || \ + !strcasecmp(expr, "active") || \ + atoi(expr))) ? 1 : 0 + + + +#include +#ifndef __WINDOWS__ +#include +#endif + +#include +#include +#include +#ifdef HAVE_STRINGS_H +#include +#endif +#include + +#include "freetdm.h" +#include "ftdm_types.h" +#include "hashtable.h" +#include "ftdm_config.h" +#include "g711.h" +#include "libteletone.h" +#include "ftdm_buffer.h" +#include "ftdm_threadmutex.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define FTDM_MAX_CHANNELS_PHYSICAL_SPAN 32 +#define FTDM_MAX_PHYSICAL_SPANS_PER_LOGICAL_SPAN 32 +#define FTDM_MAX_CHANNELS_SPAN FTDM_MAX_CHANNELS_PHYSICAL_SPAN * FTDM_MAX_PHYSICAL_SPANS_PER_LOGICAL_SPAN +#define FTDM_MAX_SPANS_INTERFACE 128 + +#define FTDM_MAX_CHANNELS_GROUP 1024 +#define FTDM_MAX_GROUPS_INTERFACE FTDM_MAX_SPANS_INTERFACE + +#define GOTO_STATUS(label,st) status = st; goto label ; + +#define ftdm_copy_string(x,y,z) strncpy(x, y, z - 1) +#define ftdm_set_string(x,y) strncpy(x, y, sizeof(x)-1) +#define ftdm_strlen_zero(s) (!s || *s == '\0') +#define ftdm_strlen_zero_buf(s) (*s == '\0') + + +#define ftdm_channel_test_feature(obj, flag) ((obj)->features & flag) +#define ftdm_channel_set_feature(obj, flag) (obj)->features |= (flag) +#define ftdm_channel_clear_feature(obj, flag) (obj)->features &= ~(flag) +#define ftdm_channel_set_member_locked(obj, _m, _v) ftdm_mutex_lock(obj->mutex); obj->_m = _v; ftdm_mutex_unlock(obj->mutex) + +/*! + \brief Test for the existance of a flag on an arbitary object + \command obj the object to test + \command flag the or'd list of flags to test + \return true value if the object has the flags defined +*/ +#define ftdm_test_flag(obj, flag) ((obj)->flags & flag) +#define ftdm_test_pflag(obj, flag) ((obj)->pflags & flag) +#define ftdm_test_sflag(obj, flag) ((obj)->sflags & flag) + +#define ftdm_set_alarm_flag(obj, flag) (obj)->alarm_flags |= (flag) +#define ftdm_clear_alarm_flag(obj, flag) (obj)->alarm_flags &= ~(flag) +#define ftdm_test_alarm_flag(obj, flag) ((obj)->alarm_flags & flag) + +/*! + \brief Set a flag on an arbitrary object + \command obj the object to set the flags on + \command flag the or'd list of flags to set +*/ +#define ftdm_set_flag(obj, flag) (obj)->flags |= (flag) +#define ftdm_set_flag_locked(obj, flag) assert(obj->mutex != NULL); \ + ftdm_mutex_lock(obj->mutex); \ + (obj)->flags |= (flag); \ + ftdm_mutex_unlock(obj->mutex); + +#define ftdm_set_pflag(obj, flag) (obj)->pflags |= (flag) +#define ftdm_set_pflag_locked(obj, flag) assert(obj->mutex != NULL); \ + ftdm_mutex_lock(obj->mutex); \ + (obj)->pflags |= (flag); \ + ftdm_mutex_unlock(obj->mutex); + +#define ftdm_set_sflag(obj, flag) (obj)->sflags |= (flag) +#define ftdm_set_sflag_locked(obj, flag) assert(obj->mutex != NULL); \ + ftdm_mutex_lock(obj->mutex); \ + (obj)->sflags |= (flag); \ + ftdm_mutex_unlock(obj->mutex); + +/*! + \brief Clear a flag on an arbitrary object while locked + \command obj the object to test + \command flag the or'd list of flags to clear +*/ +#define ftdm_clear_flag(obj, flag) (obj)->flags &= ~(flag) + +#define ftdm_clear_flag_locked(obj, flag) assert(obj->mutex != NULL); ftdm_mutex_lock(obj->mutex); (obj)->flags &= ~(flag); ftdm_mutex_unlock(obj->mutex); + +#define ftdm_clear_pflag(obj, flag) (obj)->pflags &= ~(flag) + +#define ftdm_clear_pflag_locked(obj, flag) assert(obj->mutex != NULL); ftdm_mutex_lock(obj->mutex); (obj)->pflags &= ~(flag); ftdm_mutex_unlock(obj->mutex); + +#define ftdm_clear_sflag(obj, flag) (obj)->sflags &= ~(flag) + +#define ftdm_clear_sflag_locked(obj, flag) assert(obj->mutex != NULL); ftdm_mutex_lock(obj->mutex); (obj)->sflags &= ~(flag); ftdm_mutex_unlock(obj->mutex); + + +#define ftdm_set_state_locked(obj, s) if ( obj->state == s ) { \ + ftdm_log(FTDM_LOG_WARNING, "Why bother changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(obj->state), ftdm_channel_state2str(s)); \ + } else if (ftdm_test_flag(obj, FTDM_CHANNEL_READY)) { \ + ftdm_channel_state_t st = obj->state; \ + ftdm_channel_set_state(obj, s, 1); \ + if (obj->state == s) ftdm_log(FTDM_LOG_DEBUG, "Changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(st), ftdm_channel_state2str(s)); \ + else ftdm_log(FTDM_LOG_WARNING, "VETO Changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(st), ftdm_channel_state2str(s)); \ + } + +#define ftdm_set_state(obj, s) if ( obj->state == s ) { \ + ftdm_log(FTDM_LOG_WARNING, "Why bother changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(obj->state), ftdm_channel_state2str(s)); \ + } else if (ftdm_test_flag(obj, FTDM_CHANNEL_READY)) { \ + ftdm_channel_state_t st = obj->state; \ + ftdm_channel_set_state(obj, s, 0); \ + if (obj->state == s) ftdm_log(FTDM_LOG_DEBUG, "Changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(st), ftdm_channel_state2str(s)); \ + else ftdm_log(FTDM_LOG_WARNING, "VETO Changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(st), ftdm_channel_state2str(s)); \ + } + +#ifdef _MSC_VER +/* The while(0) below throws a conditional expression is constant warning */ +#pragma warning(disable:4127) +#endif + +#define ftdm_set_state_locked_wait(obj, s) \ + do { \ + int __safety = 100; \ + ftdm_set_state_locked(obj, s); \ + while(__safety-- && ftdm_test_flag(obj, FTDM_CHANNEL_STATE_CHANGE)) { \ + ftdm_sleep(10); \ + } \ + if(!__safety) { \ + ftdm_log(FTDM_LOG_CRIT, "State change not completed\n"); \ + } \ + } while(0); + +#define ftdm_wait_for_flag_cleared(obj, flag, time) \ + do { \ + int __safety = time; \ + while(__safety-- && ftdm_test_flag(obj, flag)) { \ + ftdm_mutex_unlock(obj->mutex); \ + ftdm_sleep(10); \ + ftdm_mutex_lock(obj->mutex); \ + } \ + if(!__safety) { \ + ftdm_log(FTDM_LOG_CRIT, "flag %d was never cleared\n", flag); \ + } \ + } while(0); + +#define ftdm_set_state_wait(obj, s) \ + do { \ + ftdm_channel_set_state(obj, s, 0); \ + ftdm_wait_for_flag_cleared(obj, FTDM_CHANNEL_STATE_CHANGE, 100); \ + } while(0); + + +#define ftdm_set_state_r(obj, s, l, r) if ( obj->state == s ) { \ + if (s != FTDM_CHANNEL_STATE_HANGUP) ftdm_log(FTDM_LOG_WARNING, "Why bother changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(obj->state), ftdm_channel_state2str(s)); r = FTDM_STATE_CHANGE_SAME; \ + } else if (ftdm_test_flag(obj, FTDM_CHANNEL_READY)) { \ + int st = obj->state; \ + r = (ftdm_channel_set_state(obj, s, l) == FTDM_SUCCESS) ? FTDM_STATE_CHANGE_SUCCESS : FTDM_STATE_CHANGE_FAIL; \ + if (obj->state == s) {ftdm_log(FTDM_LOG_DEBUG, "Changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(st), ftdm_channel_state2str(s));} \ + else ftdm_log(FTDM_LOG_WARNING, "VETO Changing state on %d:%d from %s to %s\n", obj->span_id, obj->chan_id, ftdm_channel_state2str(st), ftdm_channel_state2str(s)); \ + } + + +#define ftdm_is_dtmf(key) ((key > 47 && key < 58) || (key > 64 && key < 69) || (key > 96 && key < 101) || key == 35 || key == 42 || key == 87 || key == 119) + +/*! + \brief Copy flags from one arbitrary object to another + \command dest the object to copy the flags to + \command src the object to copy the flags from + \command flags the flags to copy +*/ +#define ftdm_copy_flags(dest, src, flags) (dest)->flags &= ~(flags); (dest)->flags |= ((src)->flags & (flags)) + +struct ftdm_stream_handle { + ftdm_stream_handle_write_function_t write_function; + ftdm_stream_handle_raw_write_function_t raw_write_function; + void *data; + void *end; + ftdm_size_t data_size; + ftdm_size_t data_len; + ftdm_size_t alloc_len; + ftdm_size_t alloc_chunk; +}; + +FT_DECLARE_NONSTD(ftdm_status_t) ftdm_console_stream_raw_write(ftdm_stream_handle_t *handle, uint8_t *data, ftdm_size_t datalen); +FT_DECLARE_NONSTD(ftdm_status_t) ftdm_console_stream_write(ftdm_stream_handle_t *handle, const char *fmt, ...); + +#define FTDM_CMD_CHUNK_LEN 1024 +#define FTDM_STANDARD_STREAM(s) memset(&s, 0, sizeof(s)); s.data = ftdm_malloc(FTDM_CMD_CHUNK_LEN); \ + assert(s.data); \ + memset(s.data, 0, FTDM_CMD_CHUNK_LEN); \ + s.end = s.data; \ + s.data_size = FTDM_CMD_CHUNK_LEN; \ + s.write_function = ftdm_console_stream_write; \ + s.raw_write_function = ftdm_console_stream_raw_write; \ + s.alloc_len = FTDM_CMD_CHUNK_LEN; \ + s.alloc_chunk = FTDM_CMD_CHUNK_LEN + +/*! brief create a new queue */ +#define ftdm_queue_create(queue, capacity) g_ftdm_queue_handler.create(queue, capacity) + +/*! Enqueue an object */ +#define ftdm_queue_enqueue(queue, obj) g_ftdm_queue_handler.enqueue(queue, obj) + +/*! dequeue an object from the queue */ +#define ftdm_queue_dequeue(queue) g_ftdm_queue_handler.dequeue(queue) + +/*! wait ms milliseconds for a queue to have available objects, -1 to wait forever */ +#define ftdm_queue_wait(queue, ms) g_ftdm_queue_handler.wait(queue, ms) + +/*! get the internal interrupt object (to wait for elements to be added from the outside bypassing ftdm_queue_wait) */ +#define ftdm_queue_get_interrupt(queue, ms) g_ftdm_queue_handler.get_interrupt(queue, ms) + +/*! destroy the queue */ +#define ftdm_queue_destroy(queue) g_ftdm_queue_handler.destroy(queue) + +FT_DECLARE_DATA extern ftdm_queue_handler_t g_ftdm_queue_handler; + +#define FTDM_TOKEN_STRLEN 128 +#define FTDM_MAX_TOKENS 10 + +static __inline__ char *ftdm_clean_string(char *s) +{ + char *p; + + for (p = s; p && *p; p++) { + uint8_t x = (uint8_t) *p; + if (x < 32 || x > 127) { + *p = ' '; + } + } + + return s; +} + +struct ftdm_bitstream { + uint8_t *data; + uint32_t datalen; + uint32_t byte_index; + uint8_t bit_index; + int8_t endian; + uint8_t top; + uint8_t bot; + uint8_t ss; + uint8_t ssv; +}; + +struct ftdm_fsk_data_state { + dsp_fsk_handle_t *fsk1200_handle; + uint8_t init; + uint8_t *buf; + size_t bufsize; + ftdm_size_t blen; + ftdm_size_t bpos; + ftdm_size_t dlen; + ftdm_size_t ppos; + int checksum; +}; + +struct ftdm_fsk_modulator { + teletone_dds_state_t dds; + ftdm_bitstream_t bs; + uint32_t carrier_bits_start; + uint32_t carrier_bits_stop; + uint32_t chan_sieze_bits; + uint32_t bit_factor; + uint32_t bit_accum; + uint32_t sample_counter; + int32_t samples_per_bit; + int32_t est_bytes; + fsk_modem_types_t modem_type; + ftdm_fsk_data_state_t *fsk_data; + ftdm_fsk_write_sample_t write_sample_callback; + void *user_data; + int16_t sample_buffer[64]; +}; + + +typedef enum { + FTDM_TYPE_NONE, + FTDM_TYPE_SPAN = 0xFF, + FTDM_TYPE_CHANNEL +} ftdm_data_type_t; + +/* 2^8 table size, one for each byte (sample) value */ +#define FTDM_GAINS_TABLE_SIZE 256 +struct ftdm_channel { + ftdm_data_type_t data_type; + uint32_t span_id; + uint32_t chan_id; + uint32_t physical_span_id; + uint32_t physical_chan_id; + uint32_t rate; + uint32_t extra_id; + ftdm_chan_type_t type; + ftdm_socket_t sockfd; + uint32_t flags; + uint32_t pflags; + uint32_t sflags; + ftdm_alarm_flag_t alarm_flags; + ftdm_channel_feature_t features; + ftdm_codec_t effective_codec; + ftdm_codec_t native_codec; + uint32_t effective_interval; + uint32_t native_interval; + uint32_t packet_len; + ftdm_channel_state_t state; + ftdm_channel_state_t last_state; + ftdm_channel_state_t init_state; + ftdm_mutex_t *mutex; + teletone_dtmf_detect_state_t dtmf_detect; + uint32_t buffer_delay; + ftdm_event_t event_header; + char last_error[256]; + fio_event_cb_t event_callback; + uint32_t skip_read_frames; + ftdm_buffer_t *dtmf_buffer; + ftdm_buffer_t *gen_dtmf_buffer; + ftdm_buffer_t *pre_buffer; + ftdm_buffer_t *digit_buffer; + ftdm_buffer_t *fsk_buffer; + ftdm_mutex_t *pre_buffer_mutex; + uint32_t dtmf_on; + uint32_t dtmf_off; + char *dtmf_hangup_buf; + teletone_generation_session_t tone_session; + ftdm_time_t last_event_time; + ftdm_time_t ring_time; + char tokens[FTDM_MAX_TOKENS+1][FTDM_TOKEN_STRLEN]; + uint8_t needed_tones[FTDM_TONEMAP_INVALID]; + uint8_t detected_tones[FTDM_TONEMAP_INVALID]; + ftdm_tonemap_t last_detected_tone; + uint32_t token_count; + char chan_name[128]; + char chan_number[32]; + ftdm_filehandle_t fds[2]; + ftdm_fsk_data_state_t fsk; + uint8_t fsk_buf[80]; + uint32_t ring_count; + void *mod_data; + void *call_data; + struct ftdm_caller_data caller_data; + struct ftdm_span *span; + struct ftdm_io_interface *fio; + ftdm_hash_t *variable_hash; + unsigned char rx_cas_bits; + uint32_t pre_buffer_size; + uint8_t rxgain_table[FTDM_GAINS_TABLE_SIZE]; + uint8_t txgain_table[FTDM_GAINS_TABLE_SIZE]; + float rxgain; + float txgain; +}; + +struct ftdm_span { + ftdm_data_type_t data_type; + char *name; + uint32_t span_id; + uint32_t chan_count; + ftdm_span_flag_t flags; + struct ftdm_io_interface *fio; + fio_event_cb_t event_callback; + ftdm_mutex_t *mutex; + ftdm_trunk_type_t trunk_type; + ftdm_analog_start_type_t start_type; + ftdm_signal_type_t signal_type; + void *signal_data; + fio_signal_cb_t signal_cb; + ftdm_event_t event_header; + char last_error[256]; + char tone_map[FTDM_TONEMAP_INVALID+1][FTDM_TONEMAP_LEN]; + teletone_tone_map_t tone_detect_map[FTDM_TONEMAP_INVALID+1]; + teletone_multi_tone_t tone_finder[FTDM_TONEMAP_INVALID+1]; + ftdm_channel_t *channels[FTDM_MAX_CHANNELS_SPAN+1]; + fio_channel_outgoing_call_t outgoing_call; + fio_channel_set_sig_status_t set_channel_sig_status; + fio_channel_get_sig_status_t get_channel_sig_status; + fio_span_set_sig_status_t set_span_sig_status; + fio_span_get_sig_status_t get_span_sig_status; + fio_channel_request_t channel_request; + ftdm_span_start_t start; + ftdm_span_stop_t stop; + void *mod_data; + char *type; + char *dtmf_hangup; + size_t dtmf_hangup_len; + ftdm_state_map_t *state_map; + ftdm_caller_data_t default_caller_data; + ftdm_queue_t *pendingchans; + struct ftdm_span *next; +}; + +struct ftdm_group { + char *name; + uint32_t group_id; + uint32_t chan_count; + ftdm_channel_t *channels[FTDM_MAX_CHANNELS_GROUP]; + uint32_t last_used_index; + ftdm_mutex_t *mutex; + struct ftdm_group *next; +}; + +FT_DECLARE_DATA extern ftdm_crash_policy_t g_ftdm_crash_policy; + +FT_DECLARE_DATA extern ftdm_memory_handler_t g_ftdm_mem_handler; + +/*! \brief Duplicate string */ +FT_DECLARE(char *) ftdm_strdup(const char *str); +FT_DECLARE(char *) ftdm_strndup(const char *str, ftdm_size_t inlen); + +FT_DECLARE(ftdm_size_t) ftdm_fsk_modulator_generate_bit(ftdm_fsk_modulator_t *fsk_trans, int8_t bit, int16_t *buf, ftdm_size_t buflen); +FT_DECLARE(int32_t) ftdm_fsk_modulator_generate_carrier_bits(ftdm_fsk_modulator_t *fsk_trans, uint32_t bits); +FT_DECLARE(void) ftdm_fsk_modulator_generate_chan_sieze(ftdm_fsk_modulator_t *fsk_trans); +FT_DECLARE(void) ftdm_fsk_modulator_send_data(ftdm_fsk_modulator_t *fsk_trans); +#define ftdm_fsk_modulator_send_all(_it) ftdm_fsk_modulator_generate_chan_sieze(_it); \ + ftdm_fsk_modulator_generate_carrier_bits(_it, _it->carrier_bits_start); \ + ftdm_fsk_modulator_send_data(_it); \ + ftdm_fsk_modulator_generate_carrier_bits(_it, _it->carrier_bits_stop) + +FT_DECLARE(ftdm_status_t) ftdm_fsk_modulator_init(ftdm_fsk_modulator_t *fsk_trans, + fsk_modem_types_t modem_type, + uint32_t sample_rate, + ftdm_fsk_data_state_t *fsk_data, + float db_level, + uint32_t carrier_bits_start, + uint32_t carrier_bits_stop, + uint32_t chan_sieze_bits, + ftdm_fsk_write_sample_t write_sample_callback, + void *user_data); +FT_DECLARE(int8_t) ftdm_bitstream_get_bit(ftdm_bitstream_t *bsp); +FT_DECLARE(void) ftdm_bitstream_init(ftdm_bitstream_t *bsp, uint8_t *data, uint32_t datalen, ftdm_endian_t endian, uint8_t ss); +FT_DECLARE(ftdm_status_t) ftdm_fsk_data_parse(ftdm_fsk_data_state_t *state, ftdm_size_t *type, char **data, ftdm_size_t *len); +FT_DECLARE(ftdm_status_t) ftdm_fsk_demod_feed(ftdm_fsk_data_state_t *state, int16_t *data, size_t samples); +FT_DECLARE(ftdm_status_t) ftdm_fsk_demod_destroy(ftdm_fsk_data_state_t *state); +FT_DECLARE(int) ftdm_fsk_demod_init(ftdm_fsk_data_state_t *state, int rate, uint8_t *buf, size_t bufsize); +FT_DECLARE(ftdm_status_t) ftdm_fsk_data_init(ftdm_fsk_data_state_t *state, uint8_t *data, uint32_t datalen); +FT_DECLARE(ftdm_status_t) ftdm_fsk_data_add_mdmf(ftdm_fsk_data_state_t *state, ftdm_mdmf_type_t type, const uint8_t *data, uint32_t datalen); +FT_DECLARE(ftdm_status_t) ftdm_fsk_data_add_checksum(ftdm_fsk_data_state_t *state); +FT_DECLARE(ftdm_status_t) ftdm_fsk_data_add_sdmf(ftdm_fsk_data_state_t *state, const char *date, char *number); +FT_DECLARE(ftdm_status_t) ftdm_channel_send_fsk_data(ftdm_channel_t *ftdmchan, ftdm_fsk_data_state_t *fsk_data, float db_level); + +FT_DECLARE(ftdm_status_t) ftdm_channel_set_state(ftdm_channel_t *ftdmchan, ftdm_channel_state_t state, int lock); + +FT_DECLARE(ftdm_status_t) ftdm_span_load_tones(ftdm_span_t *span, const char *mapname); +FT_DECLARE(ftdm_time_t) ftdm_current_time_in_ms(void); +FT_DECLARE(ftdm_status_t) ftdm_channel_done(ftdm_channel_t *ftdmchan); + +FT_DECLARE(ftdm_status_t) ftdm_channel_use(ftdm_channel_t *ftdmchan); + +FT_DECLARE(void) ftdm_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t divisor); + +FT_DECLARE(uint32_t) ftdm_separate_string(char *buf, char delim, char **array, int arraylen); +FT_DECLARE(void) print_bits(uint8_t *b, int bl, char *buf, int blen, int e, uint8_t ss); +FT_DECLARE(void) print_hex_bytes(uint8_t *data, ftdm_size_t dlen, char *buf, ftdm_size_t blen); + +FT_DECLARE_NONSTD(int) ftdm_hash_equalkeys(void *k1, void *k2); +FT_DECLARE_NONSTD(uint32_t) ftdm_hash_hashfromstring(void *ky); + +FT_DECLARE(ftdm_status_t) ftdm_channel_complete_state(ftdm_channel_t *ftdmchan); + +FT_DECLARE(int) ftdm_load_modules(void); + +FT_DECLARE(ftdm_status_t) ftdm_unload_modules(void); + +FT_DECLARE(ftdm_status_t) ftdm_span_send_signal(ftdm_span_t *span, ftdm_sigmsg_t *sigmsg); + +FT_DECLARE(char *) ftdm_build_dso_path(const char *name, char *path, ftdm_size_t len); + +FT_DECLARE(int) ftdm_load_module(const char *name); +FT_DECLARE(int) ftdm_load_module_assume(const char *name); +FT_DECLARE(int) ftdm_vasprintf(char **ret, const char *fmt, va_list ap); + +FIO_CODEC_FUNCTION(fio_slin2ulaw); +FIO_CODEC_FUNCTION(fio_ulaw2slin); +FIO_CODEC_FUNCTION(fio_slin2alaw); +FIO_CODEC_FUNCTION(fio_alaw2slin); +FIO_CODEC_FUNCTION(fio_ulaw2alaw); +FIO_CODEC_FUNCTION(fio_alaw2ulaw); + +/*! + \brief Assert condition +*/ +#define ftdm_assert(assertion, msg) \ + if (!(assertion)) { \ + ftdm_log(FTDM_LOG_CRIT, msg); \ + if (g_ftdm_crash_policy & FTDM_CRASH_ON_ASSERT) { \ + ftdm_abort(); \ + } \ + } + +/*! + \brief Assert condition and return +*/ +#define ftdm_assert_return(assertion, retval, msg) \ + if (!(assertion)) { \ + ftdm_log(FTDM_LOG_CRIT, msg); \ + if (g_ftdm_crash_policy & FTDM_CRASH_ON_ASSERT) { \ + ftdm_abort(); \ + } else { \ + return retval; \ + } \ + } + +/*! + \brief Allocate uninitialized memory + \command chunksize the chunk size +*/ +#define ftdm_malloc(chunksize) g_ftdm_mem_handler.malloc(g_ftdm_mem_handler.pool, chunksize) + +/*! + \brief Reallocates memory + \command buff the buffer + \command chunksize the chunk size +*/ +#define ftdm_realloc(buff, chunksize) g_ftdm_mem_handler.realloc(g_ftdm_mem_handler.pool, buff, chunksize) + +/*! + \brief Allocate initialized memory + \command chunksize the chunk size +*/ +#define ftdm_calloc(elements, chunksize) g_ftdm_mem_handler.calloc(g_ftdm_mem_handler.pool, elements, chunksize) + +/*! + \brief Free chunk of memory + \command chunksize the chunk size +*/ +#define ftdm_free(chunk) g_ftdm_mem_handler.free(g_ftdm_mem_handler.pool, chunk) + +/*! + \brief Free a pointer and set it to NULL unless it already is NULL + \command it the pointer +*/ +#define ftdm_safe_free(it) if (it) { ftdm_free(it); it = NULL; } + +/*! + \brief Socket the given socket + \command it the socket +*/ +#define ftdm_socket_close(it) if (it > -1) { close(it); it = -1;} + +#define ftdm_array_len(array) sizeof(array)/sizeof(array[0]) + +static __inline__ void ftdm_abort(void) +{ +#ifdef __cplusplus + ::abort(); +#else + abort(); +#endif +} + +static __inline__ void ftdm_set_state_all(ftdm_span_t *span, ftdm_channel_state_t state) +{ + uint32_t j; + ftdm_mutex_lock(span->mutex); + for(j = 1; j <= span->chan_count; j++) { + ftdm_set_state_locked((span->channels[j]), state); + } + ftdm_mutex_unlock(span->mutex); +} + +static __inline__ int ftdm_check_state_all(ftdm_span_t *span, ftdm_channel_state_t state) +{ + uint32_t j; + for(j = 1; j <= span->chan_count; j++) { + if (span->channels[j]->state != state || ftdm_test_flag(span->channels[j], FTDM_CHANNEL_STATE_CHANGE)) { + return 0; + } + } + + return 1; +} + +static __inline__ void ftdm_set_flag_all(ftdm_span_t *span, uint32_t flag) +{ + uint32_t j; + ftdm_mutex_lock(span->mutex); + for(j = 1; j <= span->chan_count; j++) { + ftdm_set_flag_locked((span->channels[j]), flag); + } + ftdm_mutex_unlock(span->mutex); +} + +static __inline__ void ftdm_clear_flag_all(ftdm_span_t *span, uint32_t flag) +{ + uint32_t j; + ftdm_mutex_lock(span->mutex); + for(j = 1; j <= span->chan_count; j++) { + ftdm_clear_flag_locked((span->channels[j]), flag); + } + ftdm_mutex_unlock(span->mutex); +} + +#ifdef __cplusplus +} +#endif + +#endif /* endif __PRIVATE_FTDM_CORE__ */ diff --git a/libs/freetdm/src/include/ftdm_cpu_monitor.h b/libs/freetdm/src/include/private/ftdm_cpu_monitor.h similarity index 100% rename from libs/freetdm/src/include/ftdm_cpu_monitor.h rename to libs/freetdm/src/include/private/ftdm_cpu_monitor.h diff --git a/libs/freetdm/src/include/ftdm_dso.h b/libs/freetdm/src/include/private/ftdm_dso.h similarity index 100% rename from libs/freetdm/src/include/ftdm_dso.h rename to libs/freetdm/src/include/private/ftdm_dso.h diff --git a/libs/freetdm/src/include/ftdm_m3ua.h b/libs/freetdm/src/include/private/ftdm_m3ua.h similarity index 100% rename from libs/freetdm/src/include/ftdm_m3ua.h rename to libs/freetdm/src/include/private/ftdm_m3ua.h diff --git a/libs/freetdm/src/include/private/ftdm_types.h b/libs/freetdm/src/include/private/ftdm_types.h new file mode 100644 index 0000000000..90ace21b6a --- /dev/null +++ b/libs/freetdm/src/include/private/ftdm_types.h @@ -0,0 +1,451 @@ +/* + * 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. + * + * Contributors: + * + * Moises Silva + * + */ + +#ifndef FTDM_TYPES_H +#define FTDM_TYPES_H + +#include "freetdm.h" + +#include "fsk.h" + +#ifdef WIN32 +typedef intptr_t ftdm_ssize_t; +typedef int ftdm_filehandle_t; +#else +#include +#include +#include +typedef ssize_t ftdm_ssize_t; +typedef int ftdm_filehandle_t; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#define FTDM_COMMAND_OBJ_INT *((int *)obj) +#define FTDM_COMMAND_OBJ_CHAR_P (char *)obj +#define FTDM_COMMAND_OBJ_FLOAT *(float *)obj +#define FTDM_FSK_MOD_FACTOR 0x10000 +#define FTDM_DEFAULT_DTMF_ON 250 +#define FTDM_DEFAULT_DTMF_OFF 50 + +#define FTDM_END -1 +#define FTDM_ANY_STATE -1 + +typedef uint64_t ftdm_time_t; + +typedef enum { + FTDM_ENDIAN_BIG = 1, + FTDM_ENDIAN_LITTLE = -1 +} ftdm_endian_t; + +typedef enum { + FTDM_CID_TYPE_SDMF = 0x04, + FTDM_CID_TYPE_MDMF = 0x80 +} ftdm_cid_type_t; + +typedef enum { + MDMF_DATETIME = 1, + MDMF_PHONE_NUM = 2, + MDMF_DDN = 3, + MDMF_NO_NUM = 4, + MDMF_PHONE_NAME = 7, + MDMF_NO_NAME = 8, + MDMF_ALT_ROUTE = 9, + MDMF_INVALID = 10 +} ftdm_mdmf_type_t; +#define MDMF_STRINGS "X", "DATETIME", "PHONE_NUM", "DDN", "NO_NUM", "X", "X", "PHONE_NAME", "NO_NAME", "ALT_ROUTE", "INVALID" +FTDM_STR2ENUM_P(ftdm_str2ftdm_mdmf_type, ftdm_mdmf_type2str, ftdm_mdmf_type_t) + +#define FTDM_TONEMAP_LEN 128 +typedef enum { + FTDM_TONEMAP_NONE, + FTDM_TONEMAP_DIAL, + FTDM_TONEMAP_RING, + FTDM_TONEMAP_BUSY, + FTDM_TONEMAP_FAIL1, + FTDM_TONEMAP_FAIL2, + FTDM_TONEMAP_FAIL3, + FTDM_TONEMAP_ATTN, + FTDM_TONEMAP_CALLWAITING_CAS, + FTDM_TONEMAP_CALLWAITING_SAS, + FTDM_TONEMAP_CALLWAITING_ACK, + FTDM_TONEMAP_INVALID +} ftdm_tonemap_t; +#define TONEMAP_STRINGS "NONE", "DIAL", "RING", "BUSY", "FAIL1", "FAIL2", "FAIL3", "ATTN", "CALLWAITING-CAS", "CALLWAITING-SAS", "CALLWAITING-ACK", "INVALID" +FTDM_STR2ENUM_P(ftdm_str2ftdm_tonemap, ftdm_tonemap2str, ftdm_tonemap_t) + +typedef enum { + FTDM_TRUNK_E1, + FTDM_TRUNK_T1, + FTDM_TRUNK_J1, + FTDM_TRUNK_BRI, + FTDM_TRUNK_BRI_PTMP, + FTDM_TRUNK_FXO, + FTDM_TRUNK_FXS, + FTDM_TRUNK_EM, + FTDM_TRUNK_NONE +} ftdm_trunk_type_t; +#define TRUNK_STRINGS "E1", "T1", "J1", "BRI", "BRI_PTMP", "FXO", "FXS", "EM", "NONE" +FTDM_STR2ENUM_P(ftdm_str2ftdm_trunk_type, ftdm_trunk_type2str, ftdm_trunk_type_t) + +typedef enum { + FTDM_ANALOG_START_KEWL, + FTDM_ANALOG_START_LOOP, + FTDM_ANALOG_START_GROUND, + FTDM_ANALOG_START_WINK, + FTDM_ANALOG_START_NA +} ftdm_analog_start_type_t; +#define START_TYPE_STRINGS "KEWL", "LOOP", "GROUND", "WINK", "NA" +FTDM_STR2ENUM_P(ftdm_str2ftdm_analog_start_type, ftdm_analog_start_type2str, ftdm_analog_start_type_t) + +typedef enum { + FTDM_OOB_ONHOOK, + FTDM_OOB_OFFHOOK, + FTDM_OOB_WINK, + FTDM_OOB_FLASH, + FTDM_OOB_RING_START, + FTDM_OOB_RING_STOP, + FTDM_OOB_ALARM_TRAP, + FTDM_OOB_ALARM_CLEAR, + FTDM_OOB_NOOP, + FTDM_OOB_CAS_BITS_CHANGE, + FTDM_OOB_INVALID +} ftdm_oob_event_t; +#define OOB_STRINGS "DTMF", "ONHOOK", "OFFHOOK", "WINK", "FLASH", "RING_START", "RING_STOP", "ALARM_TRAP", "ALARM_CLEAR", "NOOP", "CAS_BITS_CHANGE", "INVALID" +FTDM_STR2ENUM_P(ftdm_str2ftdm_oob_event, ftdm_oob_event2str, ftdm_oob_event_t) + +typedef enum { + FTDM_ALARM_NONE = 0, + FTDM_ALARM_RECOVER = (1 << 0), + FTDM_ALARM_LOOPBACK = (1 << 2), + FTDM_ALARM_YELLOW = (1 << 3), + FTDM_ALARM_RED = (1 << 4), + FTDM_ALARM_BLUE = (1 << 5), + FTDM_ALARM_NOTOPEN = ( 1 << 6), + FTDM_ALARM_AIS = ( 1 << 7), + FTDM_ALARM_RAI = ( 1 << 8), + FTDM_ALARM_GENERAL = ( 1 << 30) +} ftdm_alarm_flag_t; + +typedef enum { + FTDM_SIGTYPE_NONE, + FTDM_SIGTYPE_ISDN, + FTDM_SIGTYPE_RBS, + FTDM_SIGTYPE_ANALOG, + FTDM_SIGTYPE_SANGOMABOOST, + FTDM_SIGTYPE_M3UA, + FTDM_SIGTYPE_R2 +} ftdm_signal_type_t; + +typedef enum { + FTDM_TONE_DTMF = (1 << 0) +} ftdm_tone_type_t; + +typedef enum { + FTDM_SPAN_CONFIGURED = (1 << 0), + FTDM_SPAN_READY = (1 << 1), + FTDM_SPAN_STATE_CHANGE = (1 << 2), + FTDM_SPAN_SUSPENDED = (1 << 3), + FTDM_SPAN_IN_THREAD = (1 << 4), + FTDM_SPAN_STOP_THREAD = (1 << 5), + FTDM_SPAN_USE_CHAN_QUEUE = (1 << 6), + FTDM_SPAN_SUGGEST_CHAN_ID = (1 << 7), +} ftdm_span_flag_t; + +typedef enum { + FTDM_CHANNEL_FEATURE_DTMF_DETECT = (1 << 0), + FTDM_CHANNEL_FEATURE_DTMF_GENERATE = (1 << 1), + FTDM_CHANNEL_FEATURE_CODECS = (1 << 2), + FTDM_CHANNEL_FEATURE_INTERVAL = (1 << 3), + FTDM_CHANNEL_FEATURE_CALLERID = (1 << 4), + FTDM_CHANNEL_FEATURE_PROGRESS = (1 << 5) +} ftdm_channel_feature_t; + +typedef enum { + FTDM_CHANNEL_STATE_DOWN, + FTDM_CHANNEL_STATE_HOLD, + FTDM_CHANNEL_STATE_SUSPENDED, + FTDM_CHANNEL_STATE_DIALTONE, + FTDM_CHANNEL_STATE_COLLECT, + FTDM_CHANNEL_STATE_RING, + FTDM_CHANNEL_STATE_BUSY, + FTDM_CHANNEL_STATE_ATTN, + FTDM_CHANNEL_STATE_GENRING, + FTDM_CHANNEL_STATE_DIALING, + FTDM_CHANNEL_STATE_GET_CALLERID, + FTDM_CHANNEL_STATE_CALLWAITING, + FTDM_CHANNEL_STATE_RESTART, + FTDM_CHANNEL_STATE_PROGRESS, + FTDM_CHANNEL_STATE_PROGRESS_MEDIA, + FTDM_CHANNEL_STATE_UP, + FTDM_CHANNEL_STATE_IDLE, + FTDM_CHANNEL_STATE_TERMINATING, + FTDM_CHANNEL_STATE_CANCEL, + FTDM_CHANNEL_STATE_HANGUP, + FTDM_CHANNEL_STATE_HANGUP_COMPLETE, + FTDM_CHANNEL_STATE_IN_LOOP, + FTDM_CHANNEL_STATE_INVALID +} ftdm_channel_state_t; +#define CHANNEL_STATE_STRINGS "DOWN", "HOLD", "SUSPENDED", "DIALTONE", "COLLECT", \ + "RING", "BUSY", "ATTN", "GENRING", "DIALING", "GET_CALLERID", "CALLWAITING", \ + "RESTART", "PROGRESS", "PROGRESS_MEDIA", "UP", "IDLE", "TERMINATING", "CANCEL", \ + "HANGUP", "HANGUP_COMPLETE", "IN_LOOP", "INVALID" +FTDM_STR2ENUM_P(ftdm_str2ftdm_channel_state, ftdm_channel_state2str, ftdm_channel_state_t) + +typedef enum { + FTDM_CHANNEL_CONFIGURED = (1 << 0), + FTDM_CHANNEL_READY = (1 << 1), + FTDM_CHANNEL_OPEN = (1 << 2), + FTDM_CHANNEL_DTMF_DETECT = (1 << 3), + FTDM_CHANNEL_SUPRESS_DTMF = (1 << 4), + FTDM_CHANNEL_TRANSCODE = (1 << 5), + FTDM_CHANNEL_BUFFER = (1 << 6), + FTDM_CHANNEL_EVENT = (1 << 7), + FTDM_CHANNEL_INTHREAD = (1 << 8), + FTDM_CHANNEL_WINK = (1 << 9), + FTDM_CHANNEL_FLASH = (1 << 10), + FTDM_CHANNEL_STATE_CHANGE = (1 << 11), + FTDM_CHANNEL_HOLD = (1 << 12), + FTDM_CHANNEL_INUSE = (1 << 13), + FTDM_CHANNEL_OFFHOOK = (1 << 14), + FTDM_CHANNEL_RINGING = (1 << 15), + FTDM_CHANNEL_PROGRESS_DETECT = (1 << 16), + FTDM_CHANNEL_CALLERID_DETECT = (1 << 17), + FTDM_CHANNEL_OUTBOUND = (1 << 18), + FTDM_CHANNEL_SUSPENDED = (1 << 19), + FTDM_CHANNEL_3WAY = (1 << 20), + FTDM_CHANNEL_PROGRESS = (1 << 21), + FTDM_CHANNEL_MEDIA = (1 << 22), + FTDM_CHANNEL_ANSWERED = (1 << 23), + FTDM_CHANNEL_MUTE = (1 << 24), + FTDM_CHANNEL_USE_RX_GAIN = (1 << 25), + FTDM_CHANNEL_USE_TX_GAIN = (1 << 26), + FTDM_CHANNEL_IN_ALARM = (1 << 27), +} ftdm_channel_flag_t; +#if defined(__cplusplus) && defined(WIN32) + // fix C2676 +__inline__ ftdm_channel_flag_t operator|=(ftdm_channel_flag_t a, int32_t b) { + a = (ftdm_channel_flag_t)(a | b); + return a; +} +__inline__ ftdm_channel_flag_t operator&=(ftdm_channel_flag_t a, int32_t b) { + a = (ftdm_channel_flag_t)(a & b); + return a; +} +#endif + +typedef enum { + ZSM_NONE, + ZSM_UNACCEPTABLE, + ZSM_ACCEPTABLE +} ftdm_state_map_type_t; + +typedef enum { + ZSD_INBOUND, + ZSD_OUTBOUND, +} ftdm_state_direction_t; + +#define FTDM_MAP_NODE_SIZE 512 +#define FTDM_MAP_MAX FTDM_CHANNEL_STATE_INVALID+2 + +struct ftdm_state_map_node { + ftdm_state_direction_t direction; + ftdm_state_map_type_t type; + ftdm_channel_state_t check_states[FTDM_MAP_MAX]; + ftdm_channel_state_t states[FTDM_MAP_MAX]; +}; +typedef struct ftdm_state_map_node ftdm_state_map_node_t; + +struct ftdm_state_map { + ftdm_state_map_node_t nodes[FTDM_MAP_NODE_SIZE]; +}; +typedef struct ftdm_state_map ftdm_state_map_t; + +typedef enum ftdm_channel_hw_link_status { + FTDM_HW_LINK_DISCONNECTED = 0, + FTDM_HW_LINK_CONNECTED +} ftdm_channel_hw_link_status_t; + +typedef ftdm_status_t (*ftdm_stream_handle_raw_write_function_t) (ftdm_stream_handle_t *handle, uint8_t *data, ftdm_size_t datalen); +typedef ftdm_status_t (*ftdm_stream_handle_write_function_t) (ftdm_stream_handle_t *handle, const char *fmt, ...); + +#include "ftdm_dso.h" + +#define FTDM_NODE_NAME_SIZE 50 +struct ftdm_conf_node { + /* node name */ + char name[FTDM_NODE_NAME_SIZE]; + + /* total slots for parameters */ + unsigned int t_parameters; + + /* current number of parameters */ + unsigned int n_parameters; + + /* array of parameters */ + ftdm_conf_parameter_t *parameters; + + /* first node child */ + struct ftdm_conf_node *child; + + /* next node sibling */ + struct ftdm_conf_node *next; + + /* my parent if any */ + struct ftdm_conf_node *parent; +}; + +typedef struct ftdm_module { + char name[256]; + fio_io_load_t io_load; + fio_io_unload_t io_unload; + fio_sig_load_t sig_load; + fio_sig_configure_t sig_configure; + fio_sig_unload_t sig_unload; + /*! + \brief configure a given span signaling + \see sig_configure + This is just like sig_configure but receives + an an ftdm_conf_node_t instead + I'd like to deprecate sig_configure and move + all modules to use configure_span_signaling + */ + fio_configure_span_signaling_t configure_span_signaling; + ftdm_dso_lib_t lib; + char path[256]; +} ftdm_module_t; + +#ifndef __FUNCTION__ +#define __FUNCTION__ (const char *)__func__ +#endif + +typedef struct ftdm_fsk_data_state ftdm_fsk_data_state_t; +typedef int (*ftdm_fsk_data_decoder_t)(ftdm_fsk_data_state_t *state); +typedef ftdm_status_t (*ftdm_fsk_write_sample_t)(int16_t *buf, ftdm_size_t buflen, void *user_data); +typedef struct hashtable ftdm_hash_t; +typedef struct hashtable_iterator ftdm_hash_iterator_t; +typedef struct key ftdm_hash_key_t; +typedef struct value ftdm_hash_val_t; +typedef struct ftdm_bitstream ftdm_bitstream_t; +typedef struct ftdm_fsk_modulator ftdm_fsk_modulator_t; +typedef ftdm_status_t (*ftdm_span_start_t)(ftdm_span_t *span); +typedef ftdm_status_t (*ftdm_span_stop_t)(ftdm_span_t *span); + +typedef enum { + FTDM_CAUSE_NONE = 0, + FTDM_CAUSE_UNALLOCATED = 1, + FTDM_CAUSE_NO_ROUTE_TRANSIT_NET = 2, + FTDM_CAUSE_NO_ROUTE_DESTINATION = 3, + FTDM_CAUSE_CHANNEL_UNACCEPTABLE = 6, + FTDM_CAUSE_CALL_AWARDED_DELIVERED = 7, + FTDM_CAUSE_NORMAL_CLEARING = 16, + FTDM_CAUSE_USER_BUSY = 17, + FTDM_CAUSE_NO_USER_RESPONSE = 18, + FTDM_CAUSE_NO_ANSWER = 19, + FTDM_CAUSE_SUBSCRIBER_ABSENT = 20, + FTDM_CAUSE_CALL_REJECTED = 21, + FTDM_CAUSE_NUMBER_CHANGED = 22, + FTDM_CAUSE_REDIRECTION_TO_NEW_DESTINATION = 23, + FTDM_CAUSE_EXCHANGE_ROUTING_ERROR = 25, + FTDM_CAUSE_DESTINATION_OUT_OF_ORDER = 27, + FTDM_CAUSE_INVALID_NUMBER_FORMAT = 28, + FTDM_CAUSE_FACILITY_REJECTED = 29, + FTDM_CAUSE_RESPONSE_TO_STATUS_ENQUIRY = 30, + FTDM_CAUSE_NORMAL_UNSPECIFIED = 31, + FTDM_CAUSE_NORMAL_CIRCUIT_CONGESTION = 34, + FTDM_CAUSE_NETWORK_OUT_OF_ORDER = 38, + FTDM_CAUSE_NORMAL_TEMPORARY_FAILURE = 41, + FTDM_CAUSE_SWITCH_CONGESTION = 42, + FTDM_CAUSE_ACCESS_INFO_DISCARDED = 43, + FTDM_CAUSE_REQUESTED_CHAN_UNAVAIL = 44, + FTDM_CAUSE_PRE_EMPTED = 45, + FTDM_CAUSE_FACILITY_NOT_SUBSCRIBED = 50, + FTDM_CAUSE_OUTGOING_CALL_BARRED = 52, + FTDM_CAUSE_INCOMING_CALL_BARRED = 54, + FTDM_CAUSE_BEARERCAPABILITY_NOTAUTH = 57, + FTDM_CAUSE_BEARERCAPABILITY_NOTAVAIL = 58, + FTDM_CAUSE_SERVICE_UNAVAILABLE = 63, + FTDM_CAUSE_BEARERCAPABILITY_NOTIMPL = 65, + FTDM_CAUSE_CHAN_NOT_IMPLEMENTED = 66, + FTDM_CAUSE_FACILITY_NOT_IMPLEMENTED = 69, + FTDM_CAUSE_SERVICE_NOT_IMPLEMENTED = 79, + FTDM_CAUSE_INVALID_CALL_REFERENCE = 81, + FTDM_CAUSE_INCOMPATIBLE_DESTINATION = 88, + FTDM_CAUSE_INVALID_MSG_UNSPECIFIED = 95, + FTDM_CAUSE_MANDATORY_IE_MISSING = 96, + FTDM_CAUSE_MESSAGE_TYPE_NONEXIST = 97, + FTDM_CAUSE_WRONG_MESSAGE = 98, + FTDM_CAUSE_IE_NONEXIST = 99, + FTDM_CAUSE_INVALID_IE_CONTENTS = 100, + FTDM_CAUSE_WRONG_CALL_STATE = 101, + FTDM_CAUSE_RECOVERY_ON_TIMER_EXPIRE = 102, + FTDM_CAUSE_MANDATORY_IE_LENGTH_ERROR = 103, + FTDM_CAUSE_PROTOCOL_ERROR = 111, + FTDM_CAUSE_INTERWORKING = 127, + FTDM_CAUSE_SUCCESS = 142, + FTDM_CAUSE_ORIGINATOR_CANCEL = 487, + FTDM_CAUSE_CRASH = 500, + FTDM_CAUSE_SYSTEM_SHUTDOWN = 501, + FTDM_CAUSE_LOSE_RACE = 502, + FTDM_CAUSE_MANAGER_REQUEST = 503, + FTDM_CAUSE_BLIND_TRANSFER = 600, + FTDM_CAUSE_ATTENDED_TRANSFER = 601, + FTDM_CAUSE_ALLOTTED_TIMEOUT = 602, + FTDM_CAUSE_USER_CHALLENGE = 603, + FTDM_CAUSE_MEDIA_TIMEOUT = 604 +} ftdm_call_cause_t; + +#ifdef __cplusplus +} +#endif + +#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: + */ + diff --git a/libs/freetdm/src/include/g711.h b/libs/freetdm/src/include/private/g711.h similarity index 100% rename from libs/freetdm/src/include/g711.h rename to libs/freetdm/src/include/private/g711.h diff --git a/libs/freetdm/src/include/hashtable.h b/libs/freetdm/src/include/private/hashtable.h similarity index 100% rename from libs/freetdm/src/include/hashtable.h rename to libs/freetdm/src/include/private/hashtable.h diff --git a/libs/freetdm/src/include/hashtable_itr.h b/libs/freetdm/src/include/private/hashtable_itr.h similarity index 100% rename from libs/freetdm/src/include/hashtable_itr.h rename to libs/freetdm/src/include/private/hashtable_itr.h diff --git a/libs/freetdm/src/include/hashtable_private.h b/libs/freetdm/src/include/private/hashtable_private.h similarity index 100% rename from libs/freetdm/src/include/hashtable_private.h rename to libs/freetdm/src/include/private/hashtable_private.h diff --git a/libs/freetdm/src/include/libteletone.h b/libs/freetdm/src/include/private/libteletone.h similarity index 100% rename from libs/freetdm/src/include/libteletone.h rename to libs/freetdm/src/include/private/libteletone.h diff --git a/libs/freetdm/src/include/libteletone_detect.h b/libs/freetdm/src/include/private/libteletone_detect.h similarity index 100% rename from libs/freetdm/src/include/libteletone_detect.h rename to libs/freetdm/src/include/private/libteletone_detect.h diff --git a/libs/freetdm/src/include/libteletone_generate.h b/libs/freetdm/src/include/private/libteletone_generate.h similarity index 100% rename from libs/freetdm/src/include/libteletone_generate.h rename to libs/freetdm/src/include/private/libteletone_generate.h diff --git a/libs/freetdm/src/include/sangoma_tdm_api.h b/libs/freetdm/src/include/private/sangoma_tdm_api.h similarity index 100% rename from libs/freetdm/src/include/sangoma_tdm_api.h rename to libs/freetdm/src/include/private/sangoma_tdm_api.h diff --git a/libs/freetdm/src/include/uart.h b/libs/freetdm/src/include/private/uart.h similarity index 100% rename from libs/freetdm/src/include/uart.h rename to libs/freetdm/src/include/private/uart.h diff --git a/libs/freetdm/src/libteletone_generate.c b/libs/freetdm/src/libteletone_generate.c index f79c615a36..089268bc7b 100644 --- a/libs/freetdm/src/libteletone_generate.c +++ b/libs/freetdm/src/libteletone_generate.c @@ -34,7 +34,7 @@ */ #include -#include "freetdm.h" +#include "private/ftdm_core.h" #define SMAX 32767 #define SMIN -32768 diff --git a/libs/freetdm/src/testanalog.c b/libs/freetdm/src/testanalog.c index 52b79f16bc..ab575e6c58 100644 --- a/libs/freetdm/src/testanalog.c +++ b/libs/freetdm/src/testanalog.c @@ -1,3 +1,6 @@ +#include +#include +#include #include "freetdm.h" @@ -12,10 +15,10 @@ static void *test_call(ftdm_thread_t *me, void *obj) ftdm_log(FTDM_LOG_DEBUG, "answer call and start echo test\n"); - ftdm_set_state_locked(chan, FTDM_CHANNEL_STATE_UP); + ftdm_channel_call_answer(chan); ftdm_channel_command(chan, FTDM_COMMAND_SEND_DTMF, number); - while (chan->state == FTDM_CHANNEL_STATE_UP) { + while (ftdm_channel_call_active(chan)) { ftdm_wait_flag_t flags = FTDM_READ; if (ftdm_channel_wait(chan, &flags, -1) == FTDM_FAIL) { @@ -32,8 +35,8 @@ static void *test_call(ftdm_thread_t *me, void *obj) } } - if (chan->state == FTDM_CHANNEL_STATE_UP) { - ftdm_set_state_locked(chan, FTDM_CHANNEL_STATE_BUSY); + if (ftdm_channel_call_active(chan)) { + ftdm_channel_call_indicate(chan, FTDM_CHANNEL_INDICATE_BUSY); } ftdm_log(FTDM_LOG_DEBUG, "call over\n"); @@ -47,7 +50,7 @@ static FIO_SIGNAL_CB_FUNCTION(on_signal) switch(sigmsg->event_id) { case FTDM_SIGEVENT_START: - ftdm_set_state_locked(sigmsg->channel, FTDM_CHANNEL_STATE_RING); + ftdm_channel_call_indicate(sigmsg->channel, FTDM_CHANNEL_INDICATE_RING); ftdm_log(FTDM_LOG_DEBUG, "launching thread and indicating ring\n"); ftdm_thread_create_detached(test_call, sigmsg->channel); break; @@ -101,7 +104,7 @@ int main(int argc, char *argv[]) "tonemap", "us", "digit_timeout", &digit_timeout, "max_dialstr", &max_dialstr, - TAG_END + FTDM_TAG_END ) != FTDM_SUCCESS) { ftdm_log(FTDM_LOG_ERROR, "Error configuring FreeTDM span\n"); goto done; diff --git a/libs/freetdm/src/testapp.c b/libs/freetdm/src/testapp.c index 0cb2822712..4dfa712f81 100644 --- a/libs/freetdm/src/testapp.c +++ b/libs/freetdm/src/testapp.c @@ -1,4 +1,5 @@ #include "freetdm.h" +#include int main(int argc, char *argv[]) { @@ -7,6 +8,7 @@ int main(int argc, char *argv[]) unsigned ms = 20; ftdm_codec_t codec = FTDM_CODEC_SLIN; unsigned runs = 1; + int spanid, chanid; if (ftdm_global_init() != FTDM_SUCCESS) { @@ -20,7 +22,9 @@ int main(int argc, char *argv[]) //if (ftdm_channel_open_any("wanpipe", 0, FTDM_TOP_DOWN, &chan) == FTDM_SUCCESS) { if (ftdm_channel_open(1, 1, &chan) == FTDM_SUCCESS) { int x = 0; - printf("opened channel %d:%d\n", chan->span_id, chan->chan_id); + spanid = ftdm_channel_get_span_id(chan); + chanid = ftdm_channel_get_id(chan); + printf("opened channel %d:%d\n", spanid, chanid); #if 1 if (ftdm_channel_command(chan, FTDM_COMMAND_SET_INTERVAL, &ms) == FTDM_SUCCESS) { @@ -28,7 +32,7 @@ int main(int argc, char *argv[]) ftdm_channel_command(chan, FTDM_COMMAND_GET_INTERVAL, &ms); printf("interval set to %u\n", ms); } else { - printf("set interval failed [%s]\n", chan->last_error); + printf("set interval failed [%s]\n", ftdm_channel_get_last_error(chan)); } #endif if (ftdm_channel_command(chan, FTDM_COMMAND_SET_CODEC, &codec) == FTDM_SUCCESS) { @@ -36,7 +40,7 @@ int main(int argc, char *argv[]) ftdm_channel_command(chan, FTDM_COMMAND_GET_CODEC, &codec); printf("codec set to %u\n", codec); } else { - printf("set codec failed [%s]\n", chan->last_error); + printf("set codec failed [%s]\n", ftdm_channel_get_last_error(chan)); } for(x = 0; x < 25; x++) { @@ -45,22 +49,22 @@ int main(int argc, char *argv[]) ftdm_wait_flag_t flags = FTDM_READ; if (ftdm_channel_wait(chan, &flags, -1) == FTDM_FAIL) { - printf("wait FAIL! %u [%s]\n", (unsigned)len, chan->last_error); + printf("wait FAIL! %u [%s]\n", (unsigned)len, ftdm_channel_get_last_error(chan)); } if (flags & FTDM_READ) { if (ftdm_channel_read(chan, buf, &len) == FTDM_SUCCESS) { printf("READ: %u\n", (unsigned)len); } else { - printf("READ FAIL! %u [%s]\n", (unsigned)len, chan->last_error); + printf("READ FAIL! %u [%s]\n", (unsigned)len, ftdm_channel_get_last_error(chan)); break; } } else { - printf("wait fail [%s]\n", chan->last_error); + printf("wait fail [%s]\n", ftdm_channel_get_last_error(chan)); } } ftdm_channel_close(&chan); } else { - printf("open fail [%s]\n", chan->last_error); + printf("open fail [%s]\n", ftdm_channel_get_last_error(chan)); } if(--runs) { diff --git a/libs/freetdm/src/testboost.c b/libs/freetdm/src/testboost.c index 152cae35ec..93d7d894af 100644 --- a/libs/freetdm/src/testboost.c +++ b/libs/freetdm/src/testboost.c @@ -1,4 +1,5 @@ #include "freetdm.h" +#include static FIO_SIGNAL_CB_FUNCTION(on_signal) { diff --git a/libs/freetdm/src/testcid.c b/libs/freetdm/src/testcid.c index 59086d003d..e6a0116d4a 100644 --- a/libs/freetdm/src/testcid.c +++ b/libs/freetdm/src/testcid.c @@ -1,4 +1,5 @@ -#include "freetdm.h" +#include "private/ftdm_core.h" + ftdm_status_t my_write_sample(int16_t *buf, ftdm_size_t buflen, void *user_data); struct helper { diff --git a/libs/freetdm/src/testpri.c b/libs/freetdm/src/testpri.c index 42afe2ffd3..eb498f3b80 100644 --- a/libs/freetdm/src/testpri.c +++ b/libs/freetdm/src/testpri.c @@ -1,5 +1,11 @@ #include "freetdm.h" +#include +#include +#include +#include +#include #include +#include static int THREADS[4][31] = { {0} }; static int R = 0; @@ -12,18 +18,20 @@ static void *channel_run(ftdm_thread_t *me, void *obj) ftdm_channel_t *ftdmchan = obj; int fd = -1; short buf[160]; + int spanid = ftdm_channel_get_span_id(ftdmchan); + int chanid = ftdm_channel_get_id(ftdmchan); ftdm_mutex_lock(mutex); T++; ftdm_mutex_unlock(mutex); - ftdm_set_state_locked_wait(ftdmchan, FTDM_CHANNEL_STATE_UP); + ftdm_channel_call_answer(ftdmchan); if ((fd = open("test.raw", O_RDONLY, 0)) < 0) { goto end; } - while(R == 1 && THREADS[ftdmchan->span_id][ftdmchan->chan_id] == 1) { + while(R == 1 && THREADS[spanid][chanid] == 1) { ssize_t bytes = read(fd, buf, sizeof(buf)); size_t bbytes; @@ -44,9 +52,9 @@ static void *channel_run(ftdm_thread_t *me, void *obj) end: - ftdm_set_state_locked_wait(ftdmchan, FTDM_CHANNEL_STATE_HANGUP); + ftdm_channel_call_hangup(ftdmchan); - THREADS[ftdmchan->span_id][ftdmchan->chan_id] = 0; + THREADS[spanid][chanid] = 0; ftdm_mutex_lock(mutex); T = 0; @@ -57,17 +65,19 @@ static void *channel_run(ftdm_thread_t *me, void *obj) static FIO_SIGNAL_CB_FUNCTION(on_signal) { - ftdm_log(FTDM_LOG_DEBUG, "got sig %d:%d [%s]\n", sigmsg->channel->span_id, sigmsg->channel->chan_id, ftdm_signal_event2str(sigmsg->event_id)); + int spanid = ftdm_channel_get_span_id(sigmsg->channel); + int chanid = ftdm_channel_get_id(sigmsg->channel); + ftdm_log(FTDM_LOG_DEBUG, "got sig %d:%d [%s]\n", spanid, chanid, ftdm_signal_event2str(sigmsg->event_id)); switch(sigmsg->event_id) { case FTDM_SIGEVENT_STOP: - THREADS[sigmsg->channel->span_id][sigmsg->channel->chan_id] = -1; + THREADS[spanid][chanid] = -1; break; case FTDM_SIGEVENT_START: - if (!THREADS[sigmsg->channel->span_id][sigmsg->channel->chan_id]) { - THREADS[sigmsg->channel->span_id][sigmsg->channel->chan_id] = 1; + if (!THREADS[spanid][chanid]) { + THREADS[spanid][chanid] = 1; ftdm_thread_create_detached(channel_run, sigmsg->channel); } @@ -125,7 +135,7 @@ int main(int argc, char *argv[]) "l1", "alaw", "debug", NULL, "opts", 0, - TAG_END) == FTDM_SUCCESS) { + FTDM_TAG_END) == FTDM_SUCCESS) { ftdm_span_start(span); diff --git a/libs/freetdm/src/testr2.c b/libs/freetdm/src/testr2.c index 5eb80dfe7a..8ceb527e58 100644 --- a/libs/freetdm/src/testr2.c +++ b/libs/freetdm/src/testr2.c @@ -1,12 +1,14 @@ #include "freetdm.h" #include +#include static int R = 0; static ftdm_mutex_t *mutex = NULL; static FIO_SIGNAL_CB_FUNCTION(on_r2_signal) { - ftdm_log(FTDM_LOG_DEBUG, "Got R2 channel sig [%s] in channel\n", ftdm_signal_event2str(sigmsg->event_id), sigmsg->channel->physical_chan_id); + int chanid = ftdm_channel_get_ph_id(sigmsg->channel); + ftdm_log(FTDM_LOG_DEBUG, "Got R2 channel sig [%s] in channel\n", ftdm_signal_event2str(sigmsg->event_id), chanid); return FTDM_SUCCESS; } @@ -49,7 +51,7 @@ int main(int argc, char *argv[]) "max_ani", 10, "max_dnis", 4, "logging", "all", - TAG_END) == FTDM_SUCCESS) { + FTDM_TAG_END) == FTDM_SUCCESS) { ftdm_span_start(span); diff --git a/libs/freetdm/src/testsangomaboost.c b/libs/freetdm/src/testsangomaboost.c index 8c888633f1..7dbe32e83d 100644 --- a/libs/freetdm/src/testsangomaboost.c +++ b/libs/freetdm/src/testsangomaboost.c @@ -46,6 +46,9 @@ #include #include "freetdm.h" +#include +#include +#include /* arbitrary limit for max calls in this sample program */ @@ -137,16 +140,20 @@ static void release_timers(ftdm_channel_t *channel) /* hangup the call */ static void send_hangup(ftdm_channel_t *channel) { - ftdm_log(FTDM_LOG_NOTICE, "-- Requesting hangup in channel %d:%d\n", channel->span_id, channel->chan_id); - ftdm_set_state_locked(channel, FTDM_CHANNEL_STATE_HANGUP); + int spanid = ftdm_channel_get_span_id(channel); + int chanid = ftdm_channel_get_id(channel); + ftdm_log(FTDM_LOG_NOTICE, "-- Requesting hangup in channel %d:%d\n", spanid, chanid); + ftdm_channel_call_hangup(channel); } /* send answer for an incoming call */ static void send_answer(ftdm_channel_t *channel) { /* we move the channel signaling state machine to UP (answered) */ - ftdm_log(FTDM_LOG_NOTICE, "-- Requesting answer in channel %d:%d\n", channel->span_id, channel->chan_id); - ftdm_set_state_locked(channel, FTDM_CHANNEL_STATE_UP); + int spanid = ftdm_channel_get_span_id(channel); + int chanid = ftdm_channel_get_id(channel); + ftdm_log(FTDM_LOG_NOTICE, "-- Requesting answer in channel %d:%d\n", spanid, chanid); + ftdm_channel_call_answer(channel); schedule_timer(channel, HANGUP_TIMER, send_hangup); } @@ -154,8 +161,10 @@ static void send_answer(ftdm_channel_t *channel) static void send_progress(ftdm_channel_t *channel) { /* we move the channel signaling state machine to UP (answered) */ - ftdm_log(FTDM_LOG_NOTICE, "-- Requesting progress\n", channel->span_id, channel->chan_id); - ftdm_set_state_locked(channel, FTDM_CHANNEL_STATE_PROGRESS); + int spanid = ftdm_channel_get_span_id(channel); + int chanid = ftdm_channel_get_id(channel); + ftdm_log(FTDM_LOG_NOTICE, "-- Requesting progress\n", spanid, chanid); + ftdm_channel_call_indicate(channel, FTDM_CHANNEL_INDICATE_PROGRESS); schedule_timer(channel, ANSWER_TIMER, send_answer); } @@ -196,7 +205,7 @@ static FIO_SIGNAL_CB_FUNCTION(on_signaling_event) /* release any timer for this channel */ release_timers(sigmsg->channel); /* acknowledge the hangup */ - ftdm_set_state_locked(sigmsg->channel, FTDM_CHANNEL_STATE_HANGUP); + ftdm_channel_call_hangup(sigmsg->channel); break; default: ftdm_log(FTDM_LOG_WARNING, "Unhandled event %s in channel %d:%d\n", ftdm_signal_event2str(sigmsg->event_id), @@ -223,7 +232,7 @@ static void place_call(const ftdm_span_t *span, const char *number) * it is also an option to use ftdm_channel_open_by_group to let freetdm hunt * an available channel in a given group instead of per span * */ - status = ftdm_channel_open_by_span(span->span_id, FTDM_TOP_DOWN, &caller_data, &ftdmchan); + status = ftdm_channel_open_by_span(ftdm_span_get_id(span), FTDM_TOP_DOWN, &caller_data, &ftdmchan); if (status != FTDM_SUCCESS) { ftdm_log(FTDM_LOG_ERROR, "Failed to originate call\n"); return; @@ -232,9 +241,9 @@ static void place_call(const ftdm_span_t *span, const char *number) g_outgoing_channel = ftdmchan; /* set the caller data for the outgoing channel */ - memcpy(&ftdmchan->caller_data, &caller_data, sizeof(caller_data)); + ftdm_channel_set_caller_data(ftdmchan, &caller_data); - status = ftdm_channel_outgoing_call(ftdmchan); + status = ftdm_channel_call_place(ftdmchan); if (status != FTDM_SUCCESS) { ftdm_log(FTDM_LOG_ERROR, "Failed to originate call\n"); return; @@ -331,7 +340,7 @@ int main(int argc, char *argv[]) /* send the configuration values down to the stack */ if (ftdm_configure_span_signaling("sangoma_boost", span, on_signaling_event, parameters) != FTDM_SUCCESS) { - fprintf(stderr, "Error configuring sangoma_boost signaling abstraction in span %s\n", span->name); + fprintf(stderr, "Error configuring sangoma_boost signaling abstraction in span %s\n", ftdm_span_get_name(span)); goto done; } diff --git a/libs/freetdm/src/testtones.c b/libs/freetdm/src/testtones.c index c4cc327772..d5d5d548bf 100644 --- a/libs/freetdm/src/testtones.c +++ b/libs/freetdm/src/testtones.c @@ -1,4 +1,4 @@ -#include "freetdm.h" +#include "private/ftdm_core.h" struct ttmp { int fd; diff --git a/libs/freetdm/src/uart.c b/libs/freetdm/src/uart.c index d042496a86..7211d9c643 100644 --- a/libs/freetdm/src/uart.c +++ b/libs/freetdm/src/uart.c @@ -32,7 +32,7 @@ * 2005 06 11 R. Krten created */ -#include +#include #include #include #include