osmo-cc-misdn-endpoint/src/isdn/main.c

379 lines
12 KiB
C

/* osmo-cc-misdn-endpoint main
*
* (C) 2020 by Andreas Eversberg <jolly@eversberg.eu>
* All Rights Reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include "../libdebug/debug.h"
#include "../liboptions/options.h"
#include "../libg711/g711.h"
#include <mISDN/mbuffer.h>
#include "isdn.h"
#include "dss1.h"
isdn_t *isdn_ep = NULL;
int num_kanal = 1;
static char law = 'a';
static const char *portname = "0";
static int ntmode = 0;
static int ptp = 0;
static int layer1hold = 0;
static int layer2hold = 0;
static const char *channel_out = NULL;
static const char *channel_in = NULL;
static const char *timeouts = NULL;
static int tx_delay = 0;
static int tx_gain = 0;
static int rx_gain = 0;
static const char *pipeline = NULL;
static int dtmf = 1;
static int local_tones = 0;
static int debug_mISDN = 0;
static int serving_location = 1; /* private network serving local user */
#define MAX_CC_ARGS 1024
static int cc_argc = 0;
static const char *cc_argv[MAX_CC_ARGS];
static void print_usage(const char *app)
{
printf("Usage: %s [--port <misdn port>] [--nt] [<options>]\n", app);
}
static void print_help()
{
/* - - */
printf(" -h --help\n");
printf(" This help\n");
printf(" -v --verbose <level> | <level>,<category>[,<category>[,...]] | list\n");
printf(" Use 'list' to get a list of all levels and categories\n");
printf(" Verbose level: digit of debug level (default = '%d')\n", debuglevel);
printf(" Verbose level+category: level digit followed by one or more categories\n");
printf(" -> If no category is specified, all categories are selected\n");
printf(" --ulaw\n");
printf(" Use U-LAW for b-channel coding instead of alaw.\n");
printf(" -p --port <portnr> | <portname>\n");
printf(" Number or name of misdn port (see misdn_info). (Default = %s)\n", portname);
printf(" -n --nt\n");
printf(" The given port is configured as NT-mode, instead of TE-mode.\n");
printf(" -0 --ptp\n");
printf(" The given port is configured as point-to-point. PRI-Ports are always\n");
printf(" configured as point-to-point.\n");
printf(" -M --msn <msn1> [--msn <msn2> ...]\n");
printf(" Give one or multiple MSN numbers. If MSN numbers are defined, only\n");
printf(" MSN numbers that are defined are allowed. If a different MSN number\n");
printf(" is received from a phone, it is replaced by the first MSN number.\n");
printf(" This is only useful for NT-mode with point-to-mulipoint cofiguration.\n");
printf(" (Any MSN is accepted and forwarded by default.).\n");
printf(" -1 --layer-1-hold 0 | 1\n");
printf(" Keep layer 1 always active. (Default for point-to-point)\n");
printf(" -2 --layer-2-hold 0 | 1\n");
printf(" Keep layer 2 always active. (Default for point-to-point)\n");
printf(" --channel-out [force,][<number>][,...][,free][,any][,no]\n");
printf(" Channel selection list for all outgoing calls to the interface.\n");
printf(" A free channels is searched in order of appearance.\n");
printf(" force - Forces the selected port with no acceptable alternative.\n");
printf(" -> this will be automatically set for multipoint NT-mode ports\n");
printf(" <number>[,...] - List of channels to search.\n");
printf(" free - Select any free channel\n");
printf(" any - On outgoing calls, signal 'any channel acceptable'. (see DSS1)\n");
printf(" no - Signal 'no channel available' aka 'call waiting'. (see DSS1)\n");
printf(" --channel-in [<number>][,...][,free]\n");
printf(" Give list of channels to select for calls from ISDN\n");
printf(" Channel selection list for all incoming calls from the interface.\n");
printf(" A free channels is accepted if in the list.\n");
printf(" If any channel was requested, the first free channel found is selected.\n");
printf(" <number>[,...] - List of channels to accept.\n");
printf(" free - Accept any free channel\n");
printf(" --timeouts <setup>,<overlap>,<proceeding>,<alerting>,<disconnect>\n");
printf(" Alter ISDN protocol times.\n");
printf(" The default is 120 seconds for all states. Use 0 to disable.\n");
printf(" --tx-delay <ms>\n");
printf(" Give a delay in milliseconds. This is required for modem/fax. Audio\n");
printf(" toward ISDN interface is buffered with the given delay.\n");
printf(" This feature turns off the dejittering.\n");
printf(" --tx-gain <dB>\n");
printf(" Changes gain of audio towards ISDN interface. Give Gain in steps of\n");
printf(" 6 dB. (-48 .. 48)\n");
printf(" --rx-gain <dB>\n");
printf(" Changes gain of audio coming from ISDN interface. Give Gain in steps\n");
printf(" of 6 dB. (-48 .. 48)\n");
printf(" --pipeline <string>\n");
printf(" mISDN allows to use echo cancelation modules. See mISDN documentation.\n");
printf(" --dtmf 1 | 0\n");
printf(" Turns DTMF detection on or off (default is %d).\n", dtmf);
printf(" -T --local-tones german | oldgerman | american\n");
printf(" Send locally generated tones, if not provided by remote interface.\n");
printf(" -D --debug-misdn\n");
printf(" Enables mISDN stack debugging.\n");
printf(" --serving-location (see Q.931)\n");
printf(" 0 = user, 1 = private network serving local user (default=%d)\n", serving_location);
printf(" -C --cc \"<osmo-cc arg>\" [--cc ...]\n");
printf(" Pass arguments to Osmo-CC endpoint. Use '-cc help' for description.\n");
}
#define OPT_ULAW 256
#define OPT_CHANNEL_OUT 257
#define OPT_CHANNEL_IN 258
#define OPT_TIMEOUTS 259
#define OPT_TX_DELAY 260
#define OPT_TX_GAIN 261
#define OPT_RX_GAIN 262
#define OPT_PIPELINE 263
#define OPT_DTMF 264
#define OPT_SERVING 265
static void add_options(void)
{
option_add('h', "help", 0);
option_add('v', "verbose", 1);
option_add(OPT_ULAW, "ulaw", 0);
option_add('p', "port", 1);
option_add('n', "nt", 0);
option_add('0', "ptp", 0);
option_add('M', "msn", 1);
option_add('1', "layer-1-hold", 1);
option_add('2', "layer-2-hold", 1);
option_add(OPT_CHANNEL_OUT, "channel-out", 1);
option_add(OPT_CHANNEL_IN, "channel-in", 1);
option_add(OPT_TIMEOUTS, "timeouts", 1);
option_add(OPT_TX_DELAY, "tx-delay", 1);
option_add(OPT_TX_GAIN, "tx-gain", 1);
option_add(OPT_RX_GAIN, "rx-gain", 1);
option_add(OPT_PIPELINE, "pipeline", 1);
option_add(OPT_DTMF, "dtmf", 1);
option_add('T', "local-tones", 1);
option_add('D', "debug-misdn", 0);
option_add(OPT_SERVING, "serving-location", 0);
option_add('C', "cc", 1);
}
static int handle_options(int short_option, int argi, char **argv)
{
int rc;
switch (short_option) {
case 'h':
print_usage(argv[0]);
print_help();
return 0;
case 'v':
if (!strcasecmp(argv[argi], "list")) {
debug_list_cat();
return 0;
}
rc = parse_debug_opt(argv[argi]);
if (rc < 0) {
fprintf(stderr, "Failed to parse debug option, please use -h for help.\n");
return rc;
}
break;
case OPT_ULAW:
law = 'u';
break;
case 'p':
portname = strdup(argv[argi]);
break;
case 'n':
ntmode = 1;
break;
case '0':
ptp = 1;
break;
case 'm':
isdn_add_msn(isdn_ep, argv[argi]);
break;
case '1':
layer1hold = atoi(argv[argi]);
break;
case '2':
layer2hold = atoi(argv[argi]);
break;
case OPT_CHANNEL_OUT:
channel_out = strdup(argv[argi]);
break;
case OPT_CHANNEL_IN:
channel_in = strdup(argv[argi]);
break;
case OPT_TIMEOUTS:
timeouts = strdup(argv[argi]);
break;
case OPT_TX_DELAY:
tx_delay = atoi(argv[argi]);
break;
case OPT_TX_GAIN:
tx_gain = atoi(argv[argi]) / 6;
break;
case OPT_RX_GAIN:
rx_gain = atoi(argv[argi]) / 6;
break;
case OPT_PIPELINE:
pipeline = strdup(argv[argi]);
break;
case OPT_DTMF:
dtmf = atoi(argv[argi]);
break;
case 'T':
if (!strcasecmp(argv[argi], "american"))
local_tones = TONES_TYPE_AMERICAN;
else if (!strcasecmp(argv[argi], "german"))
local_tones = TONES_TYPE_GERMAN;
else if (!strcasecmp(argv[argi], "oldgerman"))
local_tones = TONES_TYPE_OLDGERMAN;
else {
fprintf(stderr, "Invalid tones type given!\n");
return -EINVAL;
}
break;
case 'D':
debug_mISDN = 1;
break;
case OPT_SERVING:
serving_location = atoi(argv[argi]);
break;
case 'C':
if (!strcasecmp(argv[argi], "help")) {
osmo_cc_help();
return 0;
}
if (cc_argc == MAX_CC_ARGS) {
fprintf(stderr, "Too many osmo-cc args!\n");
break;
}
cc_argv[cc_argc++] = strdup(argv[argi]);
break;
default:
return -EINVAL;
}
return 1;
}
static int quit = 0;
void sighandler(int sigset)
{
if (sigset == SIGHUP || sigset == SIGPIPE)
return;
fprintf(stderr, "\nSignal %d received.\n", sigset);
quit = 1;
}
static struct mi_ext_fn_s mi_fn;
static int mISDNlib_debug(const char *file, int line, const char *func, int __attribute__((unused)) level, const char *fmt, va_list va)
{
char text[256];
vsnprintf(text, sizeof(text) - 1, fmt, va);
text[sizeof(text) - 1] = '\0';
// PDEBUG(DDSS1, DEBUG_NOTICE, "libmisdn (file %s, line %d, func %s(), level %d): %s\n", file, line, func, level, text);
_printdebug(file, func, line, DMISDN, DEBUG_NOTICE, NULL, "%s", text);
return 0;
}
int main(int argc, char *argv[])
{
int argi, rc;
int layer3_initialized = 0;
g711_init();
isdn_ep = isdn_create();
if (!isdn_ep)
goto error;
cc_argv[cc_argc++] = strdup("remote auto");
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/isdn/isdn.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);
if (argi <= 0)
return argi;
/* misdn init and debug */
rc = check_mISDN_dsp();
if (rc)
goto error;
mi_fn.prt_debug = mISDNlib_debug;
init_layer3(4, &mi_fn);
layer3_initialized = 1;
mISDN_set_debug_level((debug_mISDN) ? 0xfffffeff : 0);
rc = isdn_initialize(isdn_ep, law, portname, ntmode, ptp, layer1hold, layer2hold, channel_out, channel_in, timeouts, tx_delay, tx_gain, rx_gain, pipeline, dtmf, local_tones, serving_location);
if (rc) {
PDEBUG(DISDN, DEBUG_ERROR, "mISDN initializing failed!\n");
goto error;
}
rc = isdn_open(isdn_ep);
if (rc) {
PDEBUG(DISDN, DEBUG_ERROR, "mISDN open failed!\n");
goto error;
}
rc = osmo_cc_new(&isdn_ep->cc_ep, OSMO_CC_VERSION, isdn_ep->portname, serving_location, cc_message, NULL, isdn_ep, cc_argc, cc_argv);
if (rc < 0)
goto error;
signal(SIGINT, sighandler);
signal(SIGHUP, sighandler);
signal(SIGTERM, sighandler);
signal(SIGPIPE, sighandler);
while (!quit) {
int w;
process_timer();
isdn_bchannel_work(isdn_ep);
isdn_rtp_work(isdn_ep);
do {
w = 0;
w |= osmo_cc_handle();
w |= isdn_dchannel_work(isdn_ep);
} while (w);
usleep(1000);
}
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
signal(SIGHUP, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
error:
if (isdn_ep) {
osmo_cc_delete(&isdn_ep->cc_ep);
isdn_destroy(isdn_ep);
}
if (layer3_initialized)
cleanup_layer3();
return 0;
}