ftmod_libpri: Initial support for BRI and various cleanups + double locking fix

Fixes:
- Fix double locking in check_state(): remove ftdm_mutex_(un)lock calls.

New:
- Add lpwrap_init_bri() to initialize libpri in BRI mode
- Use trunk type to determine PRI/BRI and PTP/PTMP mode (no additional configuration required).
- Improve libpri detection code in configure.ac, check whether libpri is usable
  and if BRI support is available (needs more work)

Cleanups:
- Whitespace and indentation cleanups
- Rename str2* functions to parse_*
- Simplify libpri_configure_span() option parsing and bail out on error
- Add "ftdm libpri help" command that displays usage information
- Coding style cleanups and other (minor) changes

Known issues:
- BRI PTMP TE Q.921 link does not go beyond TEI ASSIGNED state,
  SABME requests are ignored by NT (EuroISDN DTAG, DAHDI-2.3.0.1, vzaphfc)

Signed-off-by: Stefan Knoblich <s.knoblich@axsentis.de>
This commit is contained in:
Stefan Knoblich 2010-11-06 16:08:07 +01:00
parent 6787710b00
commit 3d5ccf055d
4 changed files with 314 additions and 244 deletions

View File

@ -144,21 +144,6 @@ fi
## ##
AC_MSG_RESULT([${as_nl}<<>> Modules and optional features]) AC_MSG_RESULT([${as_nl}<<>> Modules and optional features])
##
# libpri (TODO: add checks)
#
HAVE_LIBPRI="no"
AC_ARG_WITH([libpri],
[AS_HELP_STRING([--with-libpri], [Install ftmod_libpri])],
[case "${withval}" in
no) enable_libpri="no" ;;
*) enable_libpri="yes" ;;
esac],
[enable_libpri="no"]
)
HAVE_LIBPRI="${enable_libpri}"
AM_CONDITIONAL([HAVE_LIBPRI],[test "${enable_libpri}" = "yes"])
## ##
# pritap (TODO: add checks) # pritap (TODO: add checks)
# #
@ -193,6 +178,50 @@ AC_CHECK_LIB([openr2], [openr2_context_set_io_type], [HAVE_OPENR2="yes"])
AC_MSG_RESULT([checking whether to build ftmod_r2... ${HAVE_OPENR2}]) AC_MSG_RESULT([checking whether to build ftmod_r2... ${HAVE_OPENR2}])
AM_CONDITIONAL([HAVE_OPENR2], [test "${HAVE_OPENR2}" = "yes"]) AM_CONDITIONAL([HAVE_OPENR2], [test "${HAVE_OPENR2}" = "yes"])
##
# Digium libpri (TODO: add checks)
#
HAVE_LIBPRI="no"
AC_ARG_WITH([libpri],
[AS_HELP_STRING([--with-libpri], [Install ftmod_libpri])],
[case "${withval}" in
no) enable_libpri="no" ;;
*) enable_libpri="yes" ;;
esac],
[enable_libpri="no"]
)
if test "${enable_libpri}" != "no"
then
AC_MSG_RESULT([${as_nl}<<>> Digium libpri])
save_LIBS="${LIBS}"
LIBS="${LIBS} -lpri"
AC_MSG_CHECKING([whether libpri is usable])
AC_LINK_IFELSE(
[AC_LANG_PROGRAM(
[#include <libpri.h>],
[const char *version = pri_get_version();]
)],
[AC_MSG_RESULT([yes])],
[AC_MSG_RESULT([no])
AC_MSG_ERROR([libpri is not installed or unusable (see config.log for details)])]
)
AC_MSG_CHECKING([whether libpri has BRI support])
AC_LINK_IFELSE(
[AC_LANG_PROGRAM(
[extern void pri_new_bri_cb(void);],
[pri_new_bri_cb();]
)],
[AC_DEFINE([HAVE_LIBPRI_BRI], [1], [libpri has bri support])
AC_MSG_RESULT([yes])],
[AC_MSG_RESULT([no])]
)
LIBS="${save_LIBS}"
fi
HAVE_LIBPRI="${enable_libpri}"
AM_CONDITIONAL([HAVE_LIBPRI],[test "${enable_libpri}" = "yes"])
## ##
# Sangoma Wanpipe # Sangoma Wanpipe
# #

View File

@ -143,34 +143,6 @@ static void s_pri_message(struct pri *pri, char *s)
ftdm_log(FTDM_LOG_DEBUG, "%s", s); ftdm_log(FTDM_LOG_DEBUG, "%s", s);
} }
/**
* \brief Parses an option string to flags
* \param in String to parse for configuration options
* \return Flags
*/
static uint32_t parse_opts(const char *in)
{
uint32_t flags = 0;
if (!in) {
return 0;
}
if (strstr(in, "suggest_channel")) {
flags |= FTMOD_LIBPRI_OPT_SUGGEST_CHANNEL;
}
if (strstr(in, "omit_display")) {
flags |= FTMOD_LIBPRI_OPT_OMIT_DISPLAY_IE;
}
if (strstr(in, "omit_redirecting_number")) {
flags |= FTMOD_LIBPRI_OPT_OMIT_REDIRECTING_NUMBER_IE;
}
return flags;
}
/** /**
* \brief Parses a debug string to flags * \brief Parses a debug string to flags
* \param in Debug string to parse for * \param in Debug string to parse for
@ -187,43 +159,33 @@ static int parse_debug(const char *in)
if (strstr(in, "q921_raw")) { if (strstr(in, "q921_raw")) {
flags |= PRI_DEBUG_Q921_RAW; flags |= PRI_DEBUG_Q921_RAW;
} }
if (strstr(in, "q921_dump")) { if (strstr(in, "q921_dump")) {
flags |= PRI_DEBUG_Q921_DUMP; flags |= PRI_DEBUG_Q921_DUMP;
} }
if (strstr(in, "q921_state")) { if (strstr(in, "q921_state")) {
flags |= PRI_DEBUG_Q921_STATE; flags |= PRI_DEBUG_Q921_STATE;
} }
if (strstr(in, "config")) { if (strstr(in, "config")) {
flags |= PRI_DEBUG_CONFIG; flags |= PRI_DEBUG_CONFIG;
} }
if (strstr(in, "q931_dump")) { if (strstr(in, "q931_dump")) {
flags |= PRI_DEBUG_Q931_DUMP; flags |= PRI_DEBUG_Q931_DUMP;
} }
if (strstr(in, "q931_state")) { if (strstr(in, "q931_state")) {
flags |= PRI_DEBUG_Q931_STATE; flags |= PRI_DEBUG_Q931_STATE;
} }
if (strstr(in, "q931_anomaly")) { if (strstr(in, "q931_anomaly")) {
flags |= PRI_DEBUG_Q931_ANOMALY; flags |= PRI_DEBUG_Q931_ANOMALY;
} }
if (strstr(in, "apdu")) { if (strstr(in, "apdu")) {
flags |= PRI_DEBUG_APDU; flags |= PRI_DEBUG_APDU;
} }
if (strstr(in, "aoc")) { if (strstr(in, "aoc")) {
flags |= PRI_DEBUG_AOC; flags |= PRI_DEBUG_AOC;
} }
if (strstr(in, "all")) { if (strstr(in, "all")) {
flags |= PRI_DEBUG_ALL; flags |= PRI_DEBUG_ALL;
} }
if (strstr(in, "none")) { if (strstr(in, "none")) {
flags = 0; flags = 0;
} }
@ -231,9 +193,13 @@ static int parse_debug(const char *in)
return flags; return flags;
} }
static ftdm_status_t ftdm_libpri_start(ftdm_span_t *span);
static ftdm_io_interface_t ftdm_libpri_interface; static ftdm_io_interface_t ftdm_libpri_interface;
static ftdm_status_t ftdm_libpri_start(ftdm_span_t *span); static const char *ftdm_libpri_usage =
"Usage:\n"
"libpri kill <span>\n"
"libpri debug <span> <level>\n";
/** /**
* \brief API function to kill or debug a libpri span * \brief API function to kill or debug a libpri span
@ -251,6 +217,13 @@ static FIO_API_FUNCTION(ftdm_libpri_api)
argc = ftdm_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0]))); argc = ftdm_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
} }
if (argc == 1) {
if (!strcasecmp(argv[0], "help") || !strcasecmp(argv[0], "usage")) {
stream->write_function(stream, ftdm_libpri_usage);
goto done;
}
}
if (argc == 2) { if (argc == 2) {
if (!strcasecmp(argv[0], "kill")) { if (!strcasecmp(argv[0], "kill")) {
int span_id = atoi(argv[1]); int span_id = atoi(argv[1]);
@ -295,16 +268,15 @@ static FIO_API_FUNCTION(ftdm_libpri_api)
} }
} }
stream->write_function(stream, "%s: -ERR invalid command.\n", __FILE__); stream->write_function(stream, "%s: -ERR invalid command.\n", __FILE__);
done: done:
ftdm_safe_free(mycmd); ftdm_safe_free(mycmd);
return FTDM_SUCCESS; return FTDM_SUCCESS;
} }
/** /**
* \brief Loads libpri IO module * \brief Loads libpri IO module
* \param fio FreeTDM IO interface * \param fio FreeTDM IO interface
@ -313,10 +285,10 @@ static FIO_API_FUNCTION(ftdm_libpri_api)
static FIO_IO_LOAD_FUNCTION(ftdm_libpri_io_init) static FIO_IO_LOAD_FUNCTION(ftdm_libpri_io_init)
{ {
assert(fio != NULL); assert(fio != NULL);
memset(&ftdm_libpri_interface, 0, sizeof(ftdm_libpri_interface));
memset(&ftdm_libpri_interface, 0, sizeof(ftdm_libpri_interface));
ftdm_libpri_interface.name = "libpri"; ftdm_libpri_interface.name = "libpri";
ftdm_libpri_interface.api = ftdm_libpri_api; ftdm_libpri_interface.api = &ftdm_libpri_api;
*fio = &ftdm_libpri_interface; *fio = &ftdm_libpri_interface;
@ -445,8 +417,6 @@ static ftdm_state_map_t isdn_state_map = {
{FTDM_CHANNEL_STATE_UP, FTDM_END}, {FTDM_CHANNEL_STATE_UP, FTDM_END},
{FTDM_CHANNEL_STATE_HANGUP, FTDM_CHANNEL_STATE_TERMINATING, FTDM_END}, {FTDM_CHANNEL_STATE_HANGUP, FTDM_CHANNEL_STATE_TERMINATING, FTDM_END},
}, },
} }
}; };
@ -462,11 +432,9 @@ static __inline__ void state_advance(ftdm_channel_t *ftdmchan)
ftdm_sigmsg_t sig; ftdm_sigmsg_t sig;
q931_call *call = (q931_call *) ftdmchan->call_data; q931_call *call = (q931_call *) ftdmchan->call_data;
ftdm_log(FTDM_LOG_DEBUG, "%d:%d STATE [%s]\n", ftdm_log(FTDM_LOG_DEBUG, "%d:%d STATE [%s]\n",
ftdmchan->span_id, ftdmchan->chan_id, ftdm_channel_state2str(ftdmchan->state)); ftdmchan->span_id, ftdmchan->chan_id, ftdm_channel_state2str(ftdmchan->state));
#if 0 #if 0
if (!ftdm_test_flag(ftdmchan, FTDM_CHANNEL_OUTBOUND) && !call) { if (!ftdm_test_flag(ftdmchan, FTDM_CHANNEL_OUTBOUND) && !call) {
ftdm_log(FTDM_LOG_WARNING, "NO CALL!!!!\n"); ftdm_log(FTDM_LOG_WARNING, "NO CALL!!!!\n");
@ -562,7 +530,6 @@ static __inline__ void state_advance(ftdm_channel_t *ftdmchan)
return; return;
} }
dp = ftdmchan->caller_data.dnis.type; dp = ftdmchan->caller_data.dnis.type;
switch(dp) { switch(dp) {
case FTDM_TON_NATIONAL: case FTDM_TON_NATIONAL:
@ -584,11 +551,14 @@ static __inline__ void state_advance(ftdm_channel_t *ftdmchan)
pri_sr_set_channel(sr, ftdmchan->chan_id, 0, 0); pri_sr_set_channel(sr, ftdmchan->chan_id, 0, 0);
pri_sr_set_bearer(sr, 0, isdn_data->l1); pri_sr_set_bearer(sr, 0, isdn_data->l1);
pri_sr_set_called(sr, ftdmchan->caller_data.dnis.digits, dp, 1); pri_sr_set_called(sr, ftdmchan->caller_data.dnis.digits, dp, 1);
pri_sr_set_caller(sr, ftdmchan->caller_data.cid_num.digits, (isdn_data->opts & FTMOD_LIBPRI_OPT_OMIT_DISPLAY_IE ? NULL : ftdmchan->caller_data.cid_name), pri_sr_set_caller(sr, ftdmchan->caller_data.cid_num.digits,
dp, (ftdmchan->caller_data.pres != 1 ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_PROHIB_USER_NUMBER_NOT_SCREENED)); (isdn_data->opts & FTMOD_LIBPRI_OPT_OMIT_DISPLAY_IE ? NULL : ftdmchan->caller_data.cid_name),
dp,
(ftdmchan->caller_data.pres != 1 ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_PROHIB_USER_NUMBER_NOT_SCREENED));
if (!(isdn_data->opts & FTMOD_LIBPRI_OPT_OMIT_REDIRECTING_NUMBER_IE)) { if (!(isdn_data->opts & FTMOD_LIBPRI_OPT_OMIT_REDIRECTING_NUMBER_IE)) {
pri_sr_set_redirecting(sr, ftdmchan->caller_data.cid_num.digits, dp, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL); pri_sr_set_redirecting(sr, ftdmchan->caller_data.cid_num.digits, dp,
PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
} }
if (pri_setup(isdn_data->spri.pri, call, sr)) { if (pri_setup(isdn_data->spri.pri, call, sr)) {
@ -635,15 +605,17 @@ static __inline__ void check_state(ftdm_span_t *span)
{ {
if (ftdm_test_flag(span, FTDM_SPAN_STATE_CHANGE)) { if (ftdm_test_flag(span, FTDM_SPAN_STATE_CHANGE)) {
uint32_t j; uint32_t j;
ftdm_clear_flag_locked(span, FTDM_SPAN_STATE_CHANGE); ftdm_clear_flag_locked(span, FTDM_SPAN_STATE_CHANGE);
for (j = 1; j <= span->chan_count; j++) { for (j = 1; j <= span->chan_count; j++) {
if (ftdm_test_flag((span->channels[j]), FTDM_CHANNEL_STATE_CHANGE)) { if (ftdm_test_flag((span->channels[j]), FTDM_CHANNEL_STATE_CHANGE)) {
ftdm_mutex_lock(span->channels[j]->mutex);
ftdm_channel_lock(span->channels[j]); ftdm_channel_lock(span->channels[j]);
ftdm_clear_flag((span->channels[j]), FTDM_CHANNEL_STATE_CHANGE); ftdm_clear_flag((span->channels[j]), FTDM_CHANNEL_STATE_CHANGE);
state_advance(span->channels[j]); state_advance(span->channels[j]);
ftdm_channel_complete_state(span->channels[j]); ftdm_channel_complete_state(span->channels[j]);
ftdm_mutex_unlock(span->channels[j]->mutex);
ftdm_channel_unlock(span->channels[j]); ftdm_channel_unlock(span->channels[j]);
} }
} }
@ -659,7 +631,6 @@ static __inline__ void check_state(ftdm_span_t *span)
*/ */
static int on_info(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_event *pevent) static int on_info(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_event *pevent)
{ {
ftdm_log(FTDM_LOG_DEBUG, "number is: %s\n", pevent->ring.callednum); ftdm_log(FTDM_LOG_DEBUG, "number is: %s\n", pevent->ring.callednum);
if (strlen(pevent->ring.callednum) > 3) { if (strlen(pevent->ring.callednum) > 3) {
ftdm_log(FTDM_LOG_DEBUG, "final number is: %s\n", pevent->ring.callednum); ftdm_log(FTDM_LOG_DEBUG, "final number is: %s\n", pevent->ring.callednum);
@ -733,7 +704,6 @@ static int on_answer(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_even
ftdm_set_state_locked(ftdmchan, FTDM_CHANNEL_STATE_UP); ftdm_set_state_locked(ftdmchan, FTDM_CHANNEL_STATE_UP);
} else { } else {
ftdm_log(FTDM_LOG_DEBUG, "-- Answer on channel %d:%d %s but it's not in use?\n", spri->span->span_id, pevent->answer.channel, ftdmchan->chan_id); ftdm_log(FTDM_LOG_DEBUG, "-- Answer on channel %d:%d %s but it's not in use?\n", spri->span->span_id, pevent->answer.channel, ftdmchan->chan_id);
} }
return 0; return 0;
@ -822,7 +792,6 @@ static int on_ring(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_event
goto done; goto done;
} }
ftdm_log(FTDM_LOG_NOTICE, "-- Ring on channel %d:%d (from %s to %s)\n", spri->span->span_id, pevent->ring.channel, ftdm_log(FTDM_LOG_NOTICE, "-- Ring on channel %d:%d (from %s to %s)\n", spri->span->span_id, pevent->ring.channel,
pevent->ring.callingnum, pevent->ring.callednum); pevent->ring.callingnum, pevent->ring.callednum);
@ -848,7 +817,6 @@ static int on_ring(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_event
done: done:
//switch_mutex_unlock(globals.channel_mutex); //switch_mutex_unlock(globals.channel_mutex);
return ret; return ret;
} }
@ -861,8 +829,13 @@ static int on_ring(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_event
static __inline__ ftdm_status_t process_event(ftdm_span_t *span, ftdm_event_t *event) static __inline__ ftdm_status_t process_event(ftdm_span_t *span, ftdm_event_t *event)
{ {
ftdm_alarm_flag_t alarmbits; ftdm_alarm_flag_t alarmbits;
ftdm_log(FTDM_LOG_DEBUG, "EVENT [%s][%d][%d:%d] STATE [%s]\n", ftdm_log(FTDM_LOG_DEBUG, "EVENT [%s][%d][%d:%d] STATE [%s]\n",
ftdm_oob_event2str(event->enum_id), event->enum_id, event->channel->span_id, event->channel->chan_id, ftdm_channel_state2str(event->channel->state)); ftdm_oob_event2str(event->enum_id),
event->enum_id,
event->channel->span_id,
event->channel->chan_id,
ftdm_channel_state2str(event->channel->state));
switch(event->enum_id) { switch(event->enum_id) {
case FTDM_OOB_ALARM_TRAP: case FTDM_OOB_ALARM_TRAP:
@ -873,10 +846,8 @@ static __inline__ ftdm_status_t process_event(ftdm_span_t *span, ftdm_event_t *e
} }
} }
ftdm_set_flag(event->channel, FTDM_CHANNEL_SUSPENDED); ftdm_set_flag(event->channel, FTDM_CHANNEL_SUSPENDED);
ftdm_channel_get_alarms(event->channel, &alarmbits); ftdm_channel_get_alarms(event->channel, &alarmbits);
ftdm_log(FTDM_LOG_WARNING, "channel %d:%d (%d:%d) has alarms! [%s]\n", ftdm_log(FTDM_LOG_WARNING, "channel %d:%d (%d:%d) has alarms! [%s]\n",
event->channel->span_id, event->channel->chan_id, event->channel->span_id, event->channel->chan_id,
@ -886,7 +857,6 @@ static __inline__ ftdm_status_t process_event(ftdm_span_t *span, ftdm_event_t *e
break; break;
case FTDM_OOB_ALARM_CLEAR: case FTDM_OOB_ALARM_CLEAR:
{ {
ftdm_log(FTDM_LOG_WARNING, "channel %d:%d (%d:%d) alarms Cleared!\n", event->channel->span_id, event->channel->chan_id, ftdm_log(FTDM_LOG_WARNING, "channel %d:%d (%d:%d) alarms Cleared!\n", event->channel->span_id, event->channel->chan_id,
event->channel->physical_span_id, event->channel->physical_chan_id); event->channel->physical_span_id, event->channel->physical_chan_id);
@ -905,14 +875,11 @@ static __inline__ ftdm_status_t process_event(ftdm_span_t *span, ftdm_event_t *e
*/ */
static __inline__ void check_events(ftdm_span_t *span) static __inline__ void check_events(ftdm_span_t *span)
{ {
ftdm_status_t status; switch (ftdm_span_poll_event(span, 5)) {
status = ftdm_span_poll_event(span, 5);
switch(status) {
case FTDM_SUCCESS: case FTDM_SUCCESS:
{ {
ftdm_event_t *event; ftdm_event_t *event;
while (ftdm_span_next_event(span, &event) == FTDM_SUCCESS) { while (ftdm_span_next_event(span, &event) == FTDM_SUCCESS) {
if (event->enum_id == FTDM_OOB_NOOP) { if (event->enum_id == FTDM_OOB_NOOP) {
continue; continue;
@ -923,12 +890,12 @@ static __inline__ void check_events(ftdm_span_t *span)
} }
} }
break; break;
case FTDM_FAIL: case FTDM_FAIL:
{
ftdm_log(FTDM_LOG_DEBUG, "Event Failure! %d\n", ftdm_running()); ftdm_log(FTDM_LOG_DEBUG, "Event Failure! %d\n", ftdm_running());
ftdm_sleep(2000); ftdm_sleep(2000);
}
break; break;
default: default:
break; break;
} }
@ -949,8 +916,6 @@ static int check_flags(lpwrap_pri_t *spri)
check_state(span); check_state(span);
check_events(span); check_events(span);
return 0; return 0;
} }
@ -998,19 +963,23 @@ static int on_dchan_up(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_ev
ftdm_channel_t *ftdmchan = NULL; ftdm_channel_t *ftdmchan = NULL;
ftdm_sigmsg_t sig; ftdm_sigmsg_t sig;
int i; int i;
ftdm_log(FTDM_LOG_INFO, "Span %d D-Chan UP!\n", spri->span->span_id); ftdm_log(FTDM_LOG_INFO, "Span %d D-Chan UP!\n", spri->span->span_id);
ftdm_set_flag(spri, LPWRAP_PRI_READY); ftdm_set_flag(spri, LPWRAP_PRI_READY);
ftdm_set_state_all(spri->span, FTDM_CHANNEL_STATE_RESTART); ftdm_set_state_all(spri->span, FTDM_CHANNEL_STATE_RESTART);
ftdm_log(FTDM_LOG_NOTICE, "%d:Signaling link status changed to %s\n", spri->span->span_id, ftdm_signaling_status2str(status)); ftdm_log(FTDM_LOG_NOTICE, "%d:Signaling link status changed to %s\n", spri->span->span_id, ftdm_signaling_status2str(status));
for (i = 1; i <= spri->span->chan_count; i++) { for (i = 1; i <= spri->span->chan_count; i++) {
ftdmchan = spri->span->channels[i]; ftdmchan = spri->span->channels[i];
memset(&sig, 0, sizeof(sig)); memset(&sig, 0, sizeof(sig));
sig.chan_id = ftdmchan->chan_id; sig.chan_id = ftdmchan->chan_id;
sig.span_id = ftdmchan->span_id; sig.span_id = ftdmchan->span_id;
sig.channel = ftdmchan; sig.channel = ftdmchan;
sig.event_id = FTDM_SIGEVENT_SIGSTATUS_CHANGED; sig.event_id = FTDM_SIGEVENT_SIGSTATUS_CHANGED;
sig.raw_data = &status; sig.raw_data = &status;
ftdm_span_send_signal(spri->span, &sig); ftdm_span_send_signal(spri->span, &sig);
} }
} }
@ -1031,20 +1000,23 @@ static int on_dchan_down(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_
ftdm_channel_t *ftdmchan = NULL; ftdm_channel_t *ftdmchan = NULL;
ftdm_sigmsg_t sig; ftdm_sigmsg_t sig;
int i; int i;
ftdm_log(FTDM_LOG_INFO, "Span %d D-Chan DOWN!\n", spri->span->span_id); ftdm_log(FTDM_LOG_INFO, "Span %d D-Chan DOWN!\n", spri->span->span_id);
ftdm_clear_flag(spri, LPWRAP_PRI_READY); ftdm_clear_flag(spri, LPWRAP_PRI_READY);
ftdm_set_state_all(spri->span, FTDM_CHANNEL_STATE_RESTART); ftdm_set_state_all(spri->span, FTDM_CHANNEL_STATE_RESTART);
ftdm_log(FTDM_LOG_NOTICE, "%d:Signaling link status changed to %s\n", spri->span->span_id, ftdm_signaling_status2str(status)); ftdm_log(FTDM_LOG_NOTICE, "%d:Signaling link status changed to %s\n", spri->span->span_id, ftdm_signaling_status2str(status));
for (i = 1; i <= spri->span->chan_count; i++) { for (i = 1; i <= spri->span->chan_count; i++) {
ftdmchan = spri->span->channels[i]; ftdmchan = spri->span->channels[i];
memset(&sig, 0, sizeof(sig)); memset(&sig, 0, sizeof(sig));
sig.chan_id = ftdmchan->chan_id; sig.chan_id = ftdmchan->chan_id;
sig.span_id = ftdmchan->span_id; sig.span_id = ftdmchan->span_id;
sig.channel = ftdmchan; sig.channel = ftdmchan;
sig.event_id = FTDM_SIGEVENT_SIGSTATUS_CHANGED; sig.event_id = FTDM_SIGEVENT_SIGSTATUS_CHANGED;
sig.raw_data = &status; sig.raw_data = &status;
ftdm_span_send_signal(spri->span, &sig); ftdm_span_send_signal(spri->span, &sig);
} }
} }
@ -1061,7 +1033,6 @@ static int on_dchan_down(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_
*/ */
static int on_anything(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_event *pevent) static int on_anything(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_event *pevent)
{ {
ftdm_log(FTDM_LOG_DEBUG, "Caught Event span %d %u (%s)\n", spri->span->span_id, event_type, lpwrap_pri_event_str(event_type)); ftdm_log(FTDM_LOG_DEBUG, "Caught Event span %d %u (%s)\n", spri->span->span_id, event_type, lpwrap_pri_event_str(event_type));
return 0; return 0;
} }
@ -1075,7 +1046,6 @@ static int on_anything(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_ev
*/ */
static int on_io_fail(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_event *pevent) static int on_io_fail(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_event *pevent)
{ {
ftdm_log(FTDM_LOG_DEBUG, "Caught Event span %d %u (%s)\n", spri->span->span_id, event_type, lpwrap_pri_event_str(event_type)); ftdm_log(FTDM_LOG_DEBUG, "Caught Event span %d %u (%s)\n", spri->span->span_id, event_type, lpwrap_pri_event_str(event_type));
return 0; return 0;
} }
@ -1084,20 +1054,24 @@ static int on_io_fail(lpwrap_pri_t *spri, lpwrap_pri_event_t event_type, pri_eve
* \brief Main thread function for libpri span (monitor) * \brief Main thread function for libpri span (monitor)
* \param me Current thread * \param me Current thread
* \param obj Span to run in this thread * \param obj Span to run in this thread
*
* \todo Move all init stuff outside of loop or into ftdm_libpri_configure_span()
*/ */
static void *ftdm_libpri_run(ftdm_thread_t *me, void *obj) static void *ftdm_libpri_run(ftdm_thread_t *me, void *obj)
{ {
ftdm_span_t *span = (ftdm_span_t *) obj; ftdm_span_t *span = (ftdm_span_t *) obj;
ftdm_libpri_data_t *isdn_data = span->signal_data; ftdm_libpri_data_t *isdn_data = span->signal_data;
int i, x = 0;
int down = 0; int down = 0;
int got_d = 0; int got_d = 0;
int res = 0;
ftdm_set_flag(span, FTDM_SPAN_IN_THREAD); ftdm_set_flag(span, FTDM_SPAN_IN_THREAD);
while (ftdm_running() && !ftdm_test_flag(span, FTDM_SPAN_STOP_THREAD)) { while (ftdm_running() && !ftdm_test_flag(span, FTDM_SPAN_STOP_THREAD)) {
if (!got_d) { if (!got_d) {
for(i = 1; i <= span->chan_count; i++) { int i, x;
for (i = 1, x = 0; i <= span->chan_count; i++) {
if (span->channels[i]->type == FTDM_CHAN_TYPE_DQ921) { if (span->channels[i]->type == FTDM_CHAN_TYPE_DQ921) {
if (ftdm_channel_open(span->span_id, i, &isdn_data->dchan) == FTDM_SUCCESS) { if (ftdm_channel_open(span->span_id, i, &isdn_data->dchan) == FTDM_SUCCESS) {
ftdm_log(FTDM_LOG_DEBUG, "opening d-channel #%d %d:%d\n", x, isdn_data->dchan->span_id, isdn_data->dchan->chan_id); ftdm_log(FTDM_LOG_DEBUG, "opening d-channel #%d %d:%d\n", x, isdn_data->dchan->span_id, isdn_data->dchan->chan_id);
@ -1110,22 +1084,33 @@ static void *ftdm_libpri_run(ftdm_thread_t *me, void *obj)
} }
} }
} }
if (!got_d) { if (!got_d) {
ftdm_log(FTDM_LOG_ERROR, "Failed to get a D-channel in span %d\n", span->span_id); ftdm_log(FTDM_LOG_ERROR, "Failed to get a D-channel in span %d\n", span->span_id);
break; break;
} }
/* Initialize libpri trunk */
switch (span->trunk_type) {
case FTDM_TRUNK_E1:
case FTDM_TRUNK_T1:
case FTDM_TRUNK_J1:
res = lpwrap_init_pri(&isdn_data->spri, span, isdn_data->dchan,
isdn_data->pswitch, isdn_data->node, isdn_data->debug);
break;
case FTDM_TRUNK_BRI:
res = lpwrap_init_bri(&isdn_data->spri, span, isdn_data->dchan,
isdn_data->pswitch, isdn_data->node, 0, isdn_data->debug);
break;
case FTDM_TRUNK_BRI_PTMP:
res = lpwrap_init_bri(&isdn_data->spri, span, isdn_data->dchan,
isdn_data->pswitch, isdn_data->node, 1, isdn_data->debug);
break;
default:
snprintf(span->last_error, sizeof(span->last_error), "Invalid trunk type");
goto out;
}
if (lpwrap_init_pri(&isdn_data->spri, if (res == 0) {
span, // span
isdn_data->dchan, // dchan
isdn_data->pswitch,
isdn_data->node,
isdn_data->debug) < 0) {
snprintf(span->last_error, sizeof(span->last_error), "PRI init FAIL!");
} else {
LPWRAP_MAP_PRI_EVENT(isdn_data->spri, LPWRAP_PRI_EVENT_ANY, on_anything); LPWRAP_MAP_PRI_EVENT(isdn_data->spri, LPWRAP_PRI_EVENT_ANY, on_anything);
LPWRAP_MAP_PRI_EVENT(isdn_data->spri, LPWRAP_PRI_EVENT_RING, on_ring); LPWRAP_MAP_PRI_EVENT(isdn_data->spri, LPWRAP_PRI_EVENT_RING, on_ring);
LPWRAP_MAP_PRI_EVENT(isdn_data->spri, LPWRAP_PRI_EVENT_RINGING, on_ringing); LPWRAP_MAP_PRI_EVENT(isdn_data->spri, LPWRAP_PRI_EVENT_RINGING, on_ringing);
@ -1148,8 +1133,10 @@ static void *ftdm_libpri_run(ftdm_thread_t *me, void *obj)
isdn_data->spri.on_loop = check_flags; isdn_data->spri.on_loop = check_flags;
isdn_data->spri.private_info = span; isdn_data->spri.private_info = span;
lpwrap_run_pri(&isdn_data->spri);
lpwrap_run_pri(&isdn_data->spri);
} else {
snprintf(span->last_error, sizeof(span->last_error), "PRI init FAIL!");
} }
if (!ftdm_running() || ftdm_test_flag(span, FTDM_SPAN_STOP_THREAD)) { if (!ftdm_running() || ftdm_test_flag(span, FTDM_SPAN_STOP_THREAD)) {
@ -1172,7 +1159,7 @@ static void *ftdm_libpri_run(ftdm_thread_t *me, void *obj)
down++; down++;
ftdm_sleep(5000); ftdm_sleep(5000);
} }
out:
ftdm_log(FTDM_LOG_DEBUG, "PRI thread ended on span %d\n", span->span_id); ftdm_log(FTDM_LOG_DEBUG, "PRI thread ended on span %d\n", span->span_id);
ftdm_clear_flag(span, FTDM_SPAN_IN_THREAD); ftdm_clear_flag(span, FTDM_SPAN_IN_THREAD);
@ -1197,11 +1184,15 @@ static ftdm_status_t ftdm_libpri_stop(ftdm_span_t *span)
} }
ftdm_set_state_all(span, FTDM_CHANNEL_STATE_RESTART); ftdm_set_state_all(span, FTDM_CHANNEL_STATE_RESTART);
check_state(span); check_state(span);
ftdm_set_flag(span, FTDM_SPAN_STOP_THREAD); ftdm_set_flag(span, FTDM_SPAN_STOP_THREAD);
while (ftdm_test_flag(span, FTDM_SPAN_IN_THREAD)) { while (ftdm_test_flag(span, FTDM_SPAN_IN_THREAD)) {
ftdm_sleep(100); ftdm_sleep(100);
} }
check_state(span); check_state(span);
return FTDM_SUCCESS; return FTDM_SUCCESS;
@ -1241,7 +1232,7 @@ static ftdm_status_t ftdm_libpri_start(ftdm_span_t *span)
* \param node Node string to convert * \param node Node string to convert
* \return -1 on failure, node value on success * \return -1 on failure, node value on success
*/ */
static int str2node(char *node) static int parse_node(const char *node)
{ {
if (!strcasecmp(node, "cpe") || !strcasecmp(node, "user")) if (!strcasecmp(node, "cpe") || !strcasecmp(node, "user"))
return PRI_CPE; return PRI_CPE;
@ -1255,7 +1246,7 @@ static int str2node(char *node)
* \param swtype Swtype string to convert * \param swtype Swtype string to convert
* \return Switch value * \return Switch value
*/ */
static int str2switch(char *swtype) static int parse_switch(const char *swtype)
{ {
if (!strcasecmp(swtype, "ni1")) if (!strcasecmp(swtype, "ni1"))
return PRI_SWITCH_NI1; return PRI_SWITCH_NI1;
@ -1273,6 +1264,7 @@ static int str2switch(char *swtype)
return PRI_SWITCH_GR303_EOC; return PRI_SWITCH_GR303_EOC;
if (!strcasecmp(swtype, "gr303tmc")) if (!strcasecmp(swtype, "gr303tmc"))
return PRI_SWITCH_GR303_TMC; return PRI_SWITCH_GR303_TMC;
return PRI_SWITCH_DMS100; return PRI_SWITCH_DMS100;
} }
@ -1281,7 +1273,7 @@ static int str2switch(char *swtype)
* \param l1 L1 string to convert * \param l1 L1 string to convert
* \return L1 value * \return L1 value
*/ */
static int str2l1(char *l1) static int parse_l1(const char *l1)
{ {
if (!strcasecmp(l1, "alaw")) if (!strcasecmp(l1, "alaw"))
return PRI_LAYER_1_ALAW; return PRI_LAYER_1_ALAW;
@ -1294,7 +1286,7 @@ static int str2l1(char *l1)
* \param dp DP string to convert * \param dp DP string to convert
* \return DP value * \return DP value
*/ */
static int str2dp(char *dp) static int parse_numplan(const char *dp)
{ {
if (!strcasecmp(dp, "international")) if (!strcasecmp(dp, "international"))
return PRI_INTERNATIONAL_ISDN; return PRI_INTERNATIONAL_ISDN;
@ -1310,6 +1302,32 @@ static int str2dp(char *dp)
return PRI_UNKNOWN; return PRI_UNKNOWN;
} }
/**
* \brief Parses an option string to flags
* \param in String to parse for configuration options
* \return Flags
*/
static uint32_t parse_opts(const char *in)
{
uint32_t flags = 0;
if (!in) {
return 0;
}
if (strstr(in, "suggest_channel")) {
flags |= FTMOD_LIBPRI_OPT_SUGGEST_CHANNEL;
}
if (strstr(in, "omit_display")) {
flags |= FTMOD_LIBPRI_OPT_OMIT_DISPLAY_IE;
}
if (strstr(in, "omit_redirecting_number")) {
flags |= FTMOD_LIBPRI_OPT_OMIT_REDIRECTING_NUMBER_IE;
}
return flags;
}
/** /**
* \brief Initialises a libpri span from configuration variables * \brief Initialises a libpri span from configuration variables
* \param span Span to configure * \param span Span to configure
@ -1323,7 +1341,6 @@ static FIO_SIG_CONFIGURE_FUNCTION(ftdm_libpri_configure_span)
//ftdm_channel_t *dchans[2] = {0}; //ftdm_channel_t *dchans[2] = {0};
ftdm_libpri_data_t *isdn_data; ftdm_libpri_data_t *isdn_data;
char *var, *val; char *var, *val;
char *debug = NULL;
if (span->trunk_type >= FTDM_TRUNK_NONE) { if (span->trunk_type >= FTDM_TRUNK_NONE) {
ftdm_log(FTDM_LOG_WARNING, "Invalid trunk type '%s' defaulting to T1.\n", ftdm_trunk_type2str(span->trunk_type)); ftdm_log(FTDM_LOG_WARNING, "Invalid trunk type '%s' defaulting to T1.\n", ftdm_trunk_type2str(span->trunk_type));
@ -1367,43 +1384,35 @@ static FIO_SIG_CONFIGURE_FUNCTION(ftdm_libpri_configure_span)
} }
while ((var = va_arg(ap, char *))) { while ((var = va_arg(ap, char *))) {
val = va_arg(ap, char *);
if (!val) {
ftdm_log(FTDM_LOG_ERROR, "Parameter '%s' has no value\n", var);
return FTDM_FAIL;
}
if (!strcasecmp(var, "node")) { if (!strcasecmp(var, "node")) {
int node; if ((isdn_data->node = parse_node(val)) == -1) {
if (!(val = va_arg(ap, char *))) { ftdm_log(FTDM_LOG_ERROR, "Unknown node type '%s', defaulting to CPE mode\n", val);
break; isdn_data->node = PRI_CPE;
} }
node = str2node(val);
if (-1 == node) {
ftdm_log(FTDM_LOG_ERROR, "Unknown node type %s, defaulting to CPE mode\n", val);
node = PRI_CPE;
} }
isdn_data->node = node; else if (!strcasecmp(var, "switch")) {
} else if (!strcasecmp(var, "switch")) { isdn_data->pswitch = parse_switch(val);
if (!(val = va_arg(ap, char *))) {
break;
}
isdn_data->pswitch = str2switch(val);
} else if (!strcasecmp(var, "opts")) {
if (!(val = va_arg(ap, char *))) {
break;
} }
else if (!strcasecmp(var, "opts")) {
isdn_data->opts = parse_opts(val); isdn_data->opts = parse_opts(val);
} else if (!strcasecmp(var, "dp")) {
if (!(val = va_arg(ap, char *))) {
break;
} }
isdn_data->dp = str2dp(val); else if (!strcasecmp(var, "dp")) {
} else if (!strcasecmp(var, "l1")) { isdn_data->dp = parse_numplan(val);
if (!(val = va_arg(ap, char *))) {
break;
} }
isdn_data->l1 = str2l1(val); else if (!strcasecmp(var, "l1")) {
} else if (!strcasecmp(var, "debug")) { isdn_data->l1 = parse_l1(val);
if (!(val = va_arg(ap, char *))) {
break;
} }
debug = val; else if (!strcasecmp(var, "debug")) {
} else { isdn_data->debug = parse_debug(val);
}
else {
snprintf(span->last_error, sizeof(span->last_error), "Unknown parameter [%s]", var); snprintf(span->last_error, sizeof(span->last_error), "Unknown parameter [%s]", var);
return FTDM_FAIL; return FTDM_FAIL;
} }
@ -1416,13 +1425,12 @@ static FIO_SIG_CONFIGURE_FUNCTION(ftdm_libpri_configure_span)
//isdn_data->dchans[1] = dchans[1]; //isdn_data->dchans[1] = dchans[1];
//isdn_data->dchan = isdn_data->dchans[0]; //isdn_data->dchan = isdn_data->dchans[0];
isdn_data->debug = parse_debug(debug);
span->signal_data = isdn_data; span->signal_data = isdn_data;
span->signal_type = FTDM_SIGTYPE_ISDN; span->signal_type = FTDM_SIGTYPE_ISDN;
span->outgoing_call = isdn_outgoing_call; span->outgoing_call = isdn_outgoing_call;
span->state_map = &isdn_state_map;
span->get_channel_sig_status = isdn_get_channel_sig_status; span->get_channel_sig_status = isdn_get_channel_sig_status;
span->get_span_sig_status = isdn_get_span_sig_status; span->get_span_sig_status = isdn_get_span_sig_status;
@ -1431,8 +1439,6 @@ static FIO_SIG_CONFIGURE_FUNCTION(ftdm_libpri_configure_span)
ftdm_set_flag(span, FTDM_SPAN_SUGGEST_CHAN_ID); ftdm_set_flag(span, FTDM_SPAN_SUGGEST_CHAN_ID);
} }
span->state_map = &isdn_state_map;
return FTDM_SUCCESS; return FTDM_SUCCESS;
} }
@ -1448,7 +1454,6 @@ ftdm_module_t ftdm_module = {
NULL NULL
}; };
/* For Emacs: /* For Emacs:
* Local Variables: * Local Variables:
* mode:c * mode:c

View File

@ -173,21 +173,56 @@ int lpwrap_init_pri(struct lpwrap_pri *spri, ftdm_span_t *span, ftdm_channel_t *
int ret = -1; int ret = -1;
memset(spri, 0, sizeof(struct lpwrap_pri)); memset(spri, 0, sizeof(struct lpwrap_pri));
spri->dchan = dchan; spri->dchan = dchan;
spri->span = span; spri->span = span;
if (spri->dchan && (spri->pri = pri_new_cb(spri->dchan->sockfd, node, swtype, __pri_lpwrap_read, __pri_lpwrap_write, spri))){ if (!spri->dchan) {
unsigned char buf[4] = { 0 }; ftdm_log(FTDM_LOG_ERROR, "No D-Channel available, unable to create PRI\n");
size_t buflen = sizeof(buf), len = 0; return ret;
pri_set_debug(spri->pri, debug);
ret = 0;
ftdm_channel_write(spri->dchan, buf, buflen, &len);
} else {
fprintf(stderr, "Unable to create PRI\n");
} }
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);
ftdm_channel_write(spri->dchan, buf, buflen, &len);
ret = 0;
} else {
ftdm_log(FTDM_LOG_ERROR, "Unable to create PRI\n");
}
return ret;
}
int lpwrap_init_bri(struct lpwrap_pri *spri, ftdm_span_t *span, ftdm_channel_t *dchan, int swtype, int node, int ptmp, int debug)
{
int ret = -1;
#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");
return ret;
}
if ((spri->pri = pri_new_bri_cb(spri->dchan->sockfd, ptmp, 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);
ftdm_channel_write(spri->dchan, buf, buflen, &len);
ret = 0;
} else {
ftdm_log(FTDM_LOG_ERROR, "Unable to create BRI\n");
}
#else
ftdm_log(FTDM_LOG_ERROR, "Installed libpri version (%s) has no BRI support\n",
pri_get_version());
#endif
return ret; return ret;
} }
@ -300,4 +335,3 @@ int lpwrap_run_pri(struct lpwrap_pri *spri)
* For VIM: * For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4: * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
*/ */

View File

@ -121,6 +121,8 @@ struct lpwrap_pri_event_list {
const char *lpwrap_pri_event_str(lpwrap_pri_event_t event_id); const char *lpwrap_pri_event_str(lpwrap_pri_event_t event_id);
int lpwrap_one_loop(struct lpwrap_pri *spri); int lpwrap_one_loop(struct lpwrap_pri *spri);
int lpwrap_init_pri(struct lpwrap_pri *spri, ftdm_span_t *span, ftdm_channel_t *dchan, int swtype, int node, int debug); int lpwrap_init_pri(struct lpwrap_pri *spri, ftdm_span_t *span, ftdm_channel_t *dchan, int swtype, int node, int debug);
int lpwrap_init_bri(struct lpwrap_pri *spri, ftdm_span_t *span, ftdm_channel_t *dchan, int swtype, int node, int ptmp, int debug);
int lpwrap_run_pri(struct lpwrap_pri *spri); int lpwrap_run_pri(struct lpwrap_pri *spri);
#define lpwrap_run_bri(x) lpwrap_run_pri(x)
#endif #endif