mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-11-11 20:17:05 +00:00
ftmod_libpri: Major rework of runtime thread, lpwrap event loop, restart b-channel handling and some new features.
Add per b-channel private data (ftdm_libpri_b_data_t), move the q.931 libpri call pointer into it and use channel->call_data to make it accessible. Use ftdm_channel_wait() instead of select() to wait for I/O events on the D-Channel and rewrite the timeout handling, adding support for custom timers that can be added to the b-channel private data and started/stopped with lpwrap_timer_start()/_stop(). The (sorted) list of active lpwrap timers is protected by the timer_mutex, which is initialized in the now unified lpwrap_init_pri() (see below) and destroyed in the new lpwrap_destroy_pri() function. Unify lpwrap_init_pri() and lpwrap_init_bri(). Use the span type to select BRI/PRI and Point-to-Point vs. Multipoint settings. Use the new custom lpwrap timer API to implement T302 (aka overlap receiving digit timeout). The default T302 timeout is 3000 miliseconds and can be set via the "digit_timeout" or "t302" span configuration parameter (0 = disable timer, valid range = 3000 ... 30000 miliseconds). The lpwrap_pri_init() changes made it neccessarry to rewrite the span runtime thread event loop. The d-channel initialization and libpri event handler registration have been moved out of the event loop. The b-channel restart handling in the event loop has been left as-is, pending a further clean up in the future. Rename on_info() to on_information() and add a new on_keypad_digit() event handler to convert incoming KEYPAD DIGIT messages to channel DTMF events (only useful on BRI PTMP NT-mode). The channel restart handling has been improved to distinguish between locally triggered and restarts caused by the remote end (= incoming RESTART messages). Restarts on the D-Channel now use pri_restart(), causing a Q.921 reset on the span. Large patch, i'd have loved to split this into smaller pieces and actually test some of the bits more toroughly (restart handling), but some people are f*cking annoying, so here it is. Signed-off-by: Stefan Knoblich <stkn@openisdn.net>
This commit is contained in:
1 parent
f3cd4f41a0
commit
f012b7abf8
4 files changed
+604
-335
No files matched your search
@@ -36,51 +36,7 @@
|
||||
#include "private/ftdm_core.h"
|
||||
#include "lpwrap_pri.h"
|
||||
|
||||
#ifndef HAVE_GETTIMEOFDAY
|
||||
#ifdef WIN32
|
||||
#include <mmsystem.h>
|
||||
|
||||
static __inline int gettimeofday(struct timeval *tp, void *nothing)
|
||||
{
|
||||
#ifdef WITHOUT_MM_LIB
|
||||
SYSTEMTIME st;
|
||||
time_t tt;
|
||||
struct tm tmtm;
|
||||
/* mktime converts local to UTC */
|
||||
GetLocalTime (&st);
|
||||
tmtm.tm_sec = st.wSecond;
|
||||
tmtm.tm_min = st.wMinute;
|
||||
tmtm.tm_hour = st.wHour;
|
||||
tmtm.tm_mday = st.wDay;
|
||||
tmtm.tm_mon = st.wMonth - 1;
|
||||
tmtm.tm_year = st.wYear - 1900; tmtm.tm_isdst = -1;
|
||||
tt = mktime (&tmtm);
|
||||
tp->tv_sec = tt;
|
||||
tp->tv_usec = st.wMilliseconds * 1000;
|
||||
#else
|
||||
/**
|
||||
** The earlier time calculations using GetLocalTime
|
||||
** had a time resolution of 10ms.The timeGetTime, part
|
||||
** of multimedia apis offer a better time resolution
|
||||
** of 1ms.Need to link against winmm.lib for this
|
||||
**/
|
||||
unsigned long Ticks = 0;
|
||||
unsigned long Sec =0;
|
||||
unsigned long Usec = 0;
|
||||
Ticks = timeGetTime();
|
||||
|
||||
Sec = Ticks/1000;
|
||||
Usec = (Ticks - (Sec*1000))*1000;
|
||||
tp->tv_sec = Sec;
|
||||
tp->tv_usec = Usec;
|
||||
#endif /* WITHOUT_MM_LIB */
|
||||
(void)nothing;
|
||||
return 0;
|
||||
}
|
||||
#endif /* WIN32 */
|
||||
#endif /* HAVE_GETTIMEOFDAY */
|
||||
|
||||
static struct lpwrap_pri_event_list LPWRAP_PRI_EVENT_LIST[] = {
|
||||
static struct lpwrap_pri_event_list LPWRAP_PRI_EVENT_LIST[LPWRAP_PRI_EVENT_MAX] = {
|
||||
{0, LPWRAP_PRI_EVENT_ANY, "ANY"},
|
||||
{1, LPWRAP_PRI_EVENT_DCHAN_UP, "DCHAN_UP"},
|
||||
{2, LPWRAP_PRI_EVENT_DCHAN_DOWN, "DCHAN_DOWN"},
|
||||
@@ -103,8 +59,6 @@ static struct lpwrap_pri_event_list LPWRAP_PRI_EVENT_LIST[] = {
|
||||
{19, LPWRAP_PRI_EVENT_IO_FAIL, "IO_FAIL"},
|
||||
};
|
||||
|
||||
#define LINE "--------------------------------------------------------------------------------"
|
||||
|
||||
const char *lpwrap_pri_event_str(lpwrap_pri_event_t event_id)
|
||||
{
|
||||
if (event_id < 0 || event_id >= LPWRAP_PRI_EVENT_MAX)
|
||||
@@ -170,6 +124,10 @@ static int __pri_lpwrap_write(struct pri *pri, void *buf, int buflen)
|
||||
return (int)buflen;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Unified init function for BRI + PRI libpri spans
|
||||
*/
|
||||
int lpwrap_init_pri(struct lpwrap_pri *spri, ftdm_span_t *span, ftdm_channel_t *dchan, int swtype, int node, int debug)
|
||||
{
|
||||
int ret = -1;
|
||||
@@ -179,115 +137,255 @@ int lpwrap_init_pri(struct lpwrap_pri *spri, ftdm_span_t *span, ftdm_channel_t *
|
||||
spri->span = span;
|
||||
|
||||
if (!spri->dchan) {
|
||||
ftdm_log(FTDM_LOG_ERROR, "No D-Channel available, unable to create PRI\n");
|
||||
ftdm_log(FTDM_LOG_ERROR, "No D-Channel available, unable to create BRI/PRI\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((spri->pri = pri_new_cb(spri->dchan->sockfd, node, swtype, __pri_lpwrap_read, __pri_lpwrap_write, spri))) {
|
||||
unsigned char buf[4] = { 0 };
|
||||
size_t buflen = sizeof(buf), len = 0;
|
||||
|
||||
pri_set_debug(spri->pri, debug);
|
||||
#ifdef HAVE_LIBPRI_AOC
|
||||
pri_aoc_events_enable(spri->pri, 1);
|
||||
#endif
|
||||
ftdm_channel_write(spri->dchan, buf, buflen, &len);
|
||||
|
||||
ret = 0;
|
||||
} else {
|
||||
ftdm_log(FTDM_LOG_ERROR, "Unable to create PRI\n");
|
||||
if (ftdm_mutex_create(&spri->timer_mutex) != FTDM_SUCCESS) {
|
||||
ftdm_log(FTDM_LOG_ERROR, "Failed to create timer list mutex\n");
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int lpwrap_init_bri(struct lpwrap_pri *spri, ftdm_span_t *span, ftdm_channel_t *dchan, int swtype, int node, int ptp, int debug)
|
||||
{
|
||||
int ret = -1;
|
||||
|
||||
switch (ftdm_span_get_trunk_type(span)) {
|
||||
case FTDM_TRUNK_E1:
|
||||
case FTDM_TRUNK_J1:
|
||||
case FTDM_TRUNK_T1:
|
||||
spri->pri = pri_new_cb(spri->dchan->sockfd, node, swtype, __pri_lpwrap_read, __pri_lpwrap_write, spri);
|
||||
break;
|
||||
#ifdef HAVE_LIBPRI_BRI
|
||||
memset(spri, 0, sizeof(struct lpwrap_pri));
|
||||
spri->dchan = dchan;
|
||||
spri->span = span;
|
||||
|
||||
if (!spri->dchan) {
|
||||
ftdm_log(FTDM_LOG_ERROR, "No D-Channel available, unable to create BRI\n");
|
||||
case FTDM_TRUNK_BRI:
|
||||
spri->pri = pri_new_bri_cb(spri->dchan->sockfd, 1, node, swtype, __pri_lpwrap_read, __pri_lpwrap_write, spri);
|
||||
break;
|
||||
case FTDM_TRUNK_BRI_PTMP:
|
||||
spri->pri = pri_new_bri_cb(spri->dchan->sockfd, 0, node, swtype, __pri_lpwrap_read, __pri_lpwrap_write, spri);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
ftdm_log(FTDM_LOG_CRIT, "Invalid/unsupported trunk type '%s'\n",
|
||||
ftdm_span_get_trunk_type_str(span));
|
||||
ftdm_mutex_destroy(&spri->timer_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((spri->pri = pri_new_bri_cb(spri->dchan->sockfd, ptp, node, swtype, __pri_lpwrap_read, __pri_lpwrap_write, spri))) {
|
||||
unsigned char buf[4] = { 0 };
|
||||
size_t buflen = sizeof(buf), len = 0;
|
||||
|
||||
if (spri->pri) {
|
||||
pri_set_debug(spri->pri, debug);
|
||||
#ifdef HAVE_LIBPRI_AOC
|
||||
pri_aoc_events_enable(spri->pri, 1);
|
||||
#endif
|
||||
ftdm_channel_write(spri->dchan, buf, buflen, &len);
|
||||
|
||||
ret = 0;
|
||||
} else {
|
||||
ftdm_log(FTDM_LOG_ERROR, "Unable to create BRI\n");
|
||||
ftdm_log(FTDM_LOG_CRIT, "Unable to create BRI/PRI\n");
|
||||
ftdm_mutex_destroy(&spri->timer_mutex);
|
||||
}
|
||||
#else
|
||||
ftdm_log(FTDM_LOG_ERROR, "Installed libpri version (%s) has no BRI support\n",
|
||||
pri_get_version());
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int lpwrap_one_loop(struct lpwrap_pri *spri)
|
||||
#define timeval_to_ms(x) \
|
||||
(((ftdm_time_t)(x)->tv_sec * 1000) + (ftdm_time_t)((x)->tv_usec / 1000))
|
||||
|
||||
int lpwrap_start_timer(struct lpwrap_pri *spri, struct lpwrap_timer *timer, const uint32_t timeout_ms, timeout_handler callback)
|
||||
{
|
||||
fd_set rfds, efds;
|
||||
struct timeval now = {0,0}, *next = NULL;
|
||||
struct lpwrap_timer **prev, *cur;
|
||||
|
||||
if (!spri || !timer || timer->timeout)
|
||||
return -1;
|
||||
|
||||
ftdm_log_chan(spri->dchan, FTDM_LOG_DEBUG, "-- Starting timer %p with timeout %u ms\n",
|
||||
timer, timeout_ms);
|
||||
|
||||
timer->timeout = ftdm_current_time_in_ms() + timeout_ms;
|
||||
timer->callback = callback;
|
||||
timer->next = NULL;
|
||||
|
||||
ftdm_mutex_lock(spri->timer_mutex);
|
||||
|
||||
for (prev = &spri->timer_list, cur = spri->timer_list; cur; prev = &(*prev)->next, cur = cur->next) {
|
||||
if (cur->timeout < timer->timeout) {
|
||||
*prev = timer;
|
||||
timer->next = cur;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!cur) {
|
||||
*prev = timer;
|
||||
}
|
||||
|
||||
ftdm_mutex_unlock(spri->timer_mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lpwrap_stop_timer(struct lpwrap_pri *spri, struct lpwrap_timer *timer)
|
||||
{
|
||||
struct lpwrap_timer **prev, *cur;
|
||||
|
||||
if (!spri || !timer)
|
||||
return -1;
|
||||
|
||||
if (!timer->timeout)
|
||||
return 0;
|
||||
|
||||
ftdm_log_chan(spri->dchan, FTDM_LOG_DEBUG, "-- Stopping timer %p\n", timer);
|
||||
|
||||
ftdm_mutex_lock(spri->timer_mutex);
|
||||
|
||||
for (prev = &spri->timer_list, cur = spri->timer_list; cur; prev = &(*prev)->next, cur = cur->next) {
|
||||
if (cur == timer) {
|
||||
*prev = cur->next;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ftdm_mutex_unlock(spri->timer_mutex);
|
||||
|
||||
timer->next = NULL;
|
||||
timer->timeout = 0;
|
||||
timer->callback = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct lpwrap_timer *lpwrap_timer_next(struct lpwrap_pri *spri)
|
||||
{
|
||||
return spri ? spri->timer_list : NULL;
|
||||
}
|
||||
|
||||
static int lpwrap_run_expired(struct lpwrap_pri *spri, ftdm_time_t now_ms)
|
||||
{
|
||||
struct lpwrap_timer *expired_list = NULL;
|
||||
struct lpwrap_timer **prev, *cur;
|
||||
|
||||
if (!spri || !spri->timer_list)
|
||||
return 0;
|
||||
|
||||
ftdm_mutex_lock(spri->timer_mutex);
|
||||
|
||||
/* Move all timers to expired list */
|
||||
expired_list = spri->timer_list;
|
||||
|
||||
for (prev = &expired_list, cur = expired_list; cur; prev = &(*prev)->next, cur = cur->next) {
|
||||
if (cur->timeout > now_ms) {
|
||||
*prev = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Move non-expired timer to front of timer_list (or clear list if there are none) */
|
||||
spri->timer_list = cur;
|
||||
|
||||
ftdm_mutex_unlock(spri->timer_mutex);
|
||||
|
||||
/* fire callbacks */
|
||||
while ((cur = expired_list)) {
|
||||
expired_list = cur->next;
|
||||
if (cur->callback)
|
||||
cur->callback(spri, cur);
|
||||
/* stop timer */
|
||||
cur->next = NULL;
|
||||
cur->timeout = 0;
|
||||
cur->callback = NULL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#define LPWRAP_MAX_TIMEOUT_MS 100
|
||||
#define LPWRAP_MAX_ERRORS 2
|
||||
|
||||
int lpwrap_run_pri_once(struct lpwrap_pri *spri)
|
||||
{
|
||||
struct timeval *next = NULL;
|
||||
struct lpwrap_timer *timer = NULL;
|
||||
pri_event *event = NULL;
|
||||
event_handler handler;
|
||||
int sel;
|
||||
ftdm_wait_flag_t flags;
|
||||
ftdm_time_t now_ms, next_ms, timeout_ms, tmp_ms;
|
||||
int ret;
|
||||
|
||||
if (spri->on_loop) {
|
||||
if ((sel = spri->on_loop(spri)) < 0) {
|
||||
return sel;
|
||||
if ((ret = spri->on_loop(spri)) < 0)
|
||||
return FTDM_FAIL;
|
||||
}
|
||||
|
||||
/* Default timeout when no scheduled events are pending */
|
||||
timeout_ms = LPWRAP_MAX_TIMEOUT_MS;
|
||||
next_ms = 0;
|
||||
now_ms = ftdm_current_time_in_ms();
|
||||
|
||||
/*
|
||||
* Get the next scheduled timer from libpri to set the maximum timeout,
|
||||
* but limit it to MAX_TIMEOUT_MS (100ms).
|
||||
*/
|
||||
if ((next = pri_schedule_next(spri->pri))) {
|
||||
next_ms = timeval_to_ms(next);
|
||||
if (now_ms >= next_ms) {
|
||||
/* Already late, handle timeout */
|
||||
timeout_ms = 0;
|
||||
} else {
|
||||
/* Calculate new timeout and limit it to MAX_TIMEOUT_MS miliseconds */
|
||||
tmp_ms = ftdm_min(next_ms - now_ms, LPWRAP_MAX_TIMEOUT_MS);
|
||||
timeout_ms = ftdm_min(timeout_ms, tmp_ms);
|
||||
}
|
||||
}
|
||||
|
||||
if (spri->errs >= 2) {
|
||||
spri->errs = 0;
|
||||
return -1;
|
||||
/*
|
||||
* Next lpwrap_timer timeout
|
||||
*/
|
||||
if ((timer = lpwrap_timer_next(spri))) {
|
||||
if (now_ms >= timer->timeout) {
|
||||
/* Already late, handle timeout */
|
||||
timeout_ms = 0;
|
||||
} else {
|
||||
/* Calculate new timeout and limit it to MAX_TIMEOUT_MS miliseconds */
|
||||
tmp_ms = ftdm_min(timer->timeout - now_ms, LPWRAP_MAX_TIMEOUT_MS);
|
||||
timeout_ms = ftdm_min(timeout_ms, tmp_ms);
|
||||
}
|
||||
}
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_ZERO(&efds);
|
||||
/* */
|
||||
if (timeout_ms > 0) {
|
||||
flags = FTDM_READ | FTDM_EVENTS;
|
||||
ret = ftdm_channel_wait(spri->dchan, &flags, timeout_ms);
|
||||
|
||||
#ifdef _MSC_VER
|
||||
//Windows macro for FD_SET includes a warning C4127: conditional expression is constant
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable:4127)
|
||||
#endif
|
||||
if (spri->flags & LPWRAP_PRI_ABORT)
|
||||
return FTDM_SUCCESS;
|
||||
|
||||
FD_SET(pri_fd(spri->pri), &rfds);
|
||||
FD_SET(pri_fd(spri->pri), &efds);
|
||||
if (ret == FTDM_TIMEOUT) {
|
||||
now_ms = ftdm_current_time_in_ms();
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
now.tv_sec = 0;
|
||||
now.tv_usec = 100000;
|
||||
|
||||
sel = select(pri_fd(spri->pri) + 1, &rfds, NULL, &efds, &now);
|
||||
if (!sel) {
|
||||
if ((next = pri_schedule_next(spri->pri))) {
|
||||
gettimeofday(&now, NULL);
|
||||
if (now.tv_sec >= next->tv_sec && (now.tv_usec >= next->tv_usec || next->tv_usec <= 100000)) {
|
||||
//ftdm_log(FTDM_LOG_DEBUG, "Check event\n");
|
||||
if (next) {
|
||||
if (next_ms < now_ms) {
|
||||
ftdm_log_chan(spri->dchan, FTDM_LOG_DEBUG, "pri timer %d ms late\n",
|
||||
(int)(now_ms - next_ms));
|
||||
}
|
||||
event = pri_schedule_run(spri->pri);
|
||||
}
|
||||
if (timer) {
|
||||
if (timer->timeout < now_ms) {
|
||||
ftdm_log_chan(spri->dchan, FTDM_LOG_DEBUG, "lpwrap timer %d ms late\n",
|
||||
(int)(now_ms - timer->timeout));
|
||||
}
|
||||
lpwrap_run_expired(spri, now_ms);
|
||||
}
|
||||
} else if (flags & (FTDM_READ | FTDM_EVENTS)) {
|
||||
event = pri_check_event(spri->pri);
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* Scheduled event has already expired, handle it immediately
|
||||
*/
|
||||
if (next) {
|
||||
event = pri_schedule_run(spri->pri);
|
||||
}
|
||||
if (timer) {
|
||||
lpwrap_run_expired(spri, now_ms);
|
||||
}
|
||||
} else if (sel > 0) {
|
||||
event = pri_check_event(spri->pri);
|
||||
}
|
||||
|
||||
if (spri->flags & LPWRAP_PRI_ABORT)
|
||||
return FTDM_SUCCESS;
|
||||
|
||||
if (event) {
|
||||
event_handler handler;
|
||||
|
||||
/* 0 is catchall event handler */
|
||||
if (event->e < 0 || event->e >= LPWRAP_PRI_EVENT_MAX) {
|
||||
handler = spri->eventmap[0];
|
||||
@@ -303,28 +401,47 @@ int lpwrap_one_loop(struct lpwrap_pri *spri)
|
||||
ftdm_log(FTDM_LOG_CRIT, "No event handler found for event %d.\n", event->e);
|
||||
}
|
||||
}
|
||||
return sel;
|
||||
|
||||
return FTDM_SUCCESS;
|
||||
}
|
||||
|
||||
int lpwrap_run_pri(struct lpwrap_pri *spri)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
for (;;) {
|
||||
if ((ret = lpwrap_one_loop(spri)) < 0) {
|
||||
#ifndef WIN32 //This needs to be adressed fror WIN32 still
|
||||
if (errno == EINTR){
|
||||
/* Igonore an interrupted system call */
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
ftdm_log(FTDM_LOG_CRIT, "Error = %i [%s]\n", ret, strerror(errno));
|
||||
while (!(spri->flags & LPWRAP_PRI_ABORT)) {
|
||||
ret = lpwrap_run_pri_once(spri);
|
||||
if (ret) {
|
||||
ftdm_log(FTDM_LOG_ERROR, "Error = %d, [%s]\n",
|
||||
ret, strerror(errno));
|
||||
spri->errs++;
|
||||
} else {
|
||||
spri->errs = 0;
|
||||
}
|
||||
if (!ftdm_running())
|
||||
break;
|
||||
if (spri->errs >= LPWRAP_MAX_ERRORS) {
|
||||
ftdm_log(FTDM_LOG_CRIT, "Too many errors on span, restarting\n");
|
||||
spri->errs = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int lpwrap_stop_pri(struct lpwrap_pri *spri)
|
||||
{
|
||||
spri->flags |= LPWRAP_PRI_ABORT;
|
||||
return FTDM_SUCCESS;
|
||||
}
|
||||
|
||||
int lpwrap_destroy_pri(struct lpwrap_pri *spri)
|
||||
{
|
||||
if (spri->timer_mutex)
|
||||
ftdm_mutex_destroy(&spri->timer_mutex);
|
||||
return FTDM_SUCCESS;
|
||||
}
|
||||
|
||||
/* For Emacs:
|
||||
* Local Variables:
|
||||
* mode:c
|
||||
|
||||
Reference in new issue
Block a user