dahdi_cfg.c 46.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Configuration program for DAHDI Telephony Interface
 *
 * Written by Mark Spencer <markster@digium.com>
 * Based on previous works, designs, and architectures conceived and
 * written by Jim Dixon <jim@lambdatel.com>.
 *
 * Copyright (C) 2001 Jim Dixon / Zapata Telephony.
 * Copyright (C) 2001-2008 Digium, Inc.
 *
 * All rights reserved.
 *
 * Primary Author: Mark Spencer <markster@digium.com>
 * Radio Support by Jim Dixon <jim@lambdatel.com>
 */

/*
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2 as published by the
 * Free Software Foundation. See the LICENSE file included with
 * this program for more details.
 */

#include <stdio.h> 
#include <getopt.h>
32
#include <signal.h>
33 34 35 36 37 38
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
39 40
#include <sys/stat.h>
#include <semaphore.h>
41
#include <errno.h>
42 43
#include <dirent.h>
#include <stdbool.h>
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

#include <dahdi/user.h>
#include "tonezone.h"
#include "dahdi_tools_version.h"

#define CONFIG_FILENAME "/etc/dahdi/system.conf"
#define MASTER_DEVICE   "/dev/dahdi/ctl"

#define NUM_SPANS DAHDI_MAX_SPANS

#define NUM_TONES 15

/*! A sanity check for the timing parameter of the span. 
 *
 * Note that each driver using it is still responsible for validating
 * that value.
 */
#define MAX_TIMING 255 

/* Assume no more than 1024 dynamics */
#define NUM_DYNAMIC	1024

static int lineno=0;

static FILE *cf;

static char *filename=CONFIG_FILENAME;

int rxtones[NUM_TONES + 1],rxtags[NUM_TONES + 1],txtones[NUM_TONES + 1];
int bursttime = 0, debouncetime = 0, invertcor = 0, exttone = 0, corthresh = 0;
int txgain = 0, rxgain = 0, deemp = 0, preemp = 0;

int corthreshes[] = {3125,6250,9375,12500,15625,18750,21875,25000,0} ;

static int toneindex = 1;

#define DEBUG_READER (1 << 0)
#define DEBUG_PARSER (1 << 1)
#define DEBUG_APPLY  (1 << 2)
static int debug = 0;

static int errcnt = 0;

static int deftonezone = -1;

static struct dahdi_lineconfig lc[DAHDI_MAX_SPANS];

static struct dahdi_chanconfig cc[DAHDI_MAX_CHANNELS];

93 94 95 96 97
static int only_span = 0;
static int restrict_channels = 0;
static int selected_channels[DAHDI_MAX_CHANNELS];
static int declared_spans[DAHDI_MAX_SPANS];

98 99 100 101 102 103 104 105 106 107 108 109
static struct dahdi_attach_echocan ae[DAHDI_MAX_CHANNELS];

static struct dahdi_dynamic_span zds[NUM_DYNAMIC];

static const char *sig[DAHDI_MAX_CHANNELS];		/* Signalling */

static int slineno[DAHDI_MAX_CHANNELS];	/* Line number where signalling specified */

static int fiftysixkhdlc[DAHDI_MAX_CHANNELS];

static int spans=0;

110
static int dry_run = 0;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

static int verbose = 0;

static int force = 0;

static int stopmode = 0;

static int numdynamic = 0;

static char zonestoload[DAHDI_TONE_ZONE_MAX][10];

static int numzones = 0;

static int fd = -1;

static const char *lbostr[] = {
"0 db (CSU)/0-133 feet (DSX-1)",
"133-266 feet (DSX-1)",
"266-399 feet (DSX-1)",
"399-533 feet (DSX-1)",
"533-655 feet (DSX-1)",
"-7.5db (CSU)",
"-15db (CSU)",
"-22.5db (CSU)"
};

static const char *laws[] = {
	"Default",
	"Mu-law",
	"A-law"
};

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static bool _are_all_spans_assigned(const char *device_path)
{
	char attribute[1024];
	int res;
	FILE *fp;
	int span_count;
	DIR *dirp;
	struct dirent *dirent;

	snprintf(attribute, sizeof(attribute) - 1,
		 "%s/span_count", device_path);
	fp = fopen(attribute, "r");
	if (NULL == fp) {
		fprintf(stderr, "Failed to open '%s'.\n", attribute);
		return false;
	}
	res = fscanf(fp, "%d", &span_count);
	fclose(fp);

	if (EOF == res) {
		fprintf(stderr, "Failed to read '%s'.\n", attribute);
		return false;
	}

	dirp = opendir(device_path);
	while (span_count) {
		dirent = readdir(dirp);
		if (NULL == dirent)
			break;
		if (!strncmp("span-", dirent->d_name, 5)) {
			--span_count;
		}
	}
	closedir(dirp);
	return (span_count > 0) ? false : true;
}

/**
 * are_all_spans_assigned - Look in sysfs to see if all spans for a device are assigned.
 *
 * Returns true if there are $span_count child spans of all devices, or false
 *  otherwise.
 */
static bool are_all_spans_assigned(void)
{
	DIR *dirp;
	struct dirent *dirent;
	bool res = true;
	char device_path[1024];

	dirp = opendir("/sys/bus/dahdi_devices/devices");
	if (!dirp) {
		/* If we cannot open dahdi_devices, either dahdi isn't loaded,
		 * or we're using an older version of DAHDI that doesn't use
		 * sysfs. */
		return true;
	}

	while (true && res) {

		dirent = readdir(dirp);
		if (NULL == dirent)
			break;

		if (!strcmp(dirent->d_name, ".") ||
		    !strcmp(dirent->d_name, ".."))
			continue;

		snprintf(device_path, sizeof(device_path)-1,
			 "/sys/bus/dahdi_devices/devices/%s", dirent->d_name);
		res = _are_all_spans_assigned(device_path);
	}

	closedir(dirp);
	errno = 0;
	return res;
}

221 222 223 224 225 226 227 228 229 230 231 232 233
static bool wait_for_all_spans_assigned(unsigned long timeout_sec)
{
	bool all_assigned = are_all_spans_assigned();
	unsigned int timeout = 10*timeout_sec;

	while (!all_assigned && --timeout) {
		usleep(100000);
		all_assigned = are_all_spans_assigned();
	}

	return all_assigned;
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
static const char *sigtype_to_str(const int sig)
{
	switch (sig) {
	case 0:
		return "Unused";
	case DAHDI_SIG_EM:
		return "E & M";
	case DAHDI_SIG_EM_E1:
		return "E & M E1";
	case DAHDI_SIG_FXSLS:
		return "FXS Loopstart";
	case DAHDI_SIG_FXSGS:
		return "FXS Groundstart";
	case DAHDI_SIG_FXSKS:
		return "FXS Kewlstart";
	case DAHDI_SIG_FXOLS:
		return "FXO Loopstart";
	case DAHDI_SIG_FXOGS:
		return "FXO Groundstart";
	case DAHDI_SIG_FXOKS:
		return "FXO Kewlstart";
	case DAHDI_SIG_CAS:
		return "CAS / User";
	case DAHDI_SIG_DACS:
		return "DACS";
	case DAHDI_SIG_DACS_RBS:
		return "DACS w/RBS";
	case DAHDI_SIG_CLEAR:
		return "Clear channel";
	case DAHDI_SIG_SLAVE:
		return "Slave channel";
	case DAHDI_SIG_HDLCRAW:
		return "Raw HDLC";
	case DAHDI_SIG_HDLCNET:
		return "Network HDLC";
	case DAHDI_SIG_HDLCFCS:
		return "HDLC with FCS check";
	case DAHDI_SIG_HARDHDLC:
		return "Hardware assisted D-channel";
	case DAHDI_SIG_MTP2:
		return "MTP2";
	default:
		return "Unknown";
	}
}

static void clear_fields()
{

	memset(rxtones,0,sizeof(rxtones));
	memset(rxtags,0,sizeof(rxtags));
	memset(txtones,0,sizeof(txtones));
	bursttime = 0;
	debouncetime = 0;
	invertcor = 0;
	exttone = 0;
	txgain = 0;
	rxgain = 0;
	deemp = 0;
	preemp = 0;
}

static int error(char *fmt, ...) __attribute__ ((format(printf, 1, 2)));

static int error(char *fmt, ...)
{
	int res;
	static int shown=0;
	va_list ap;
	if (!shown) {
		fprintf(stderr, "Notice: Configuration file is %s\n", filename);
		shown++;
	}
	res = fprintf(stderr, "line %d: ", lineno);
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	errcnt++;
	return res;
}

static char *trim(char *buf)
{
	size_t len;

	while (*buf && (*buf < 33)) {
		buf++;
	}

	len = strlen(buf);

	while (len && buf[len-1] < 33) {
		buf[--len] = '\0';
	}

	return buf;
}

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
static int skip_channel(int x)
{
	if (restrict_channels) {
		if (!selected_channels[x])
			return 1;
	} else {
		if (only_span && !declared_spans[only_span]) {
			fprintf(stderr,
				"Error: analog span %d given to '-S', without '-C' restriction.\n",
				only_span);
			exit(1);
		}
	}
	return 0;
}

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
static int parseargs(char *input, char *output[], int maxargs, char sep)
{
	char *c;
	int pos=0;
	c = input;
	output[pos++] = c;
	while(*c) {
		while(*c && (*c != sep)) c++;
		if (*c) {
			*c = '\0';
			c++;
			while(*c && (*c < 33)) c++;
			if (*c)  {
				if (pos >= maxargs)
					return -1;
				output[pos] = c;
				trim(output[pos]);
				pos++;
				output[pos] = NULL;
				/* Return error if we have too many */
			} else
				return pos;
		}
	}
	return pos;
}

int dspanconfig(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int chans;
	int timing;
381
	res = parseargs(args, realargs, 4, ',');
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	if (res != 4) {
		error("Incorrect number of arguments to 'dynamic' (should be <driver>,<address>,<num channels>, <timing>)\n");
		return -1;
	}
	res = sscanf(realargs[2], "%d", &chans);
	if ((res == 1) && (chans < 1))
		res = -1;
	if (res != 1) {
		error("Invalid number of channels '%s', should be a number > 0.\n", realargs[2]);
		return -1;
	}

	res = sscanf(realargs[3], "%d", &timing);
	if ((res == 1) && (timing < 0))
		res = -1;
	if (res != 1) {
		error("Invalid timing '%s', should be a number > 0.\n", realargs[3]);
		return -1;
	}


	dahdi_copy_string(zds[numdynamic].driver, realargs[0], sizeof(zds[numdynamic].driver));
	dahdi_copy_string(zds[numdynamic].addr, realargs[1], sizeof(zds[numdynamic].addr));
	zds[numdynamic].numchans = chans;
	zds[numdynamic].timing = timing;
	
	numdynamic++;
	return 0;
}

int spanconfig(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int argc;
	int span;
	int timing;
	int i;
	argc = res = parseargs(args, realargs, 9, ',');
	if ((res < 5) || (res > 9)) {
		error("Incorrect number of arguments to 'span' (should be <spanno>,<timing>,<lbo>,<framing>,<coding>[, crc4 | yellow [, yellow]])\n");
		return -1;
	}
	res = sscanf(realargs[0], "%d", &span);
	if (res != 1) {
		error("Span number should be a valid span number, not '%s'\n", realargs[0]);
		return -1;
	}
430
	declared_spans[span] = 1;
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
	res = sscanf(realargs[1], "%d", &timing);
	if ((res != 1) || (timing < 0) || (timing > MAX_TIMING)) {
		error("Timing should be a number from 0 to %d, not '%s'\n", 
				MAX_TIMING,  realargs[1]);
		return -1;
	}
	res = sscanf(realargs[2], "%d", &lc[spans].lbo);
	if (res != 1) {
		error("Line build-out (LBO) should be a number from 0 to 7 (usually 0) not '%s'\n", realargs[2]);
		return -1;
	}
	if ((lc[spans].lbo < 0) || (lc[spans].lbo > 7)) {
		error("Line build-out should be in the range 0 to 7, not %d\n", lc[spans].lbo);
		return -1;
	}
	if (!strcasecmp(realargs[3], "d4")) {
		lc[spans].lineconfig |= DAHDI_CONFIG_D4;
		lc[spans].lineconfig &= ~DAHDI_CONFIG_ESF;
		lc[spans].lineconfig &= ~DAHDI_CONFIG_CCS;
	} else if (!strcasecmp(realargs[3], "esf")) {
		lc[spans].lineconfig |= DAHDI_CONFIG_ESF;
		lc[spans].lineconfig &= ~DAHDI_CONFIG_D4;
		lc[spans].lineconfig &= ~DAHDI_CONFIG_CCS;
	} else if (!strcasecmp(realargs[3], "ccs")) {
		lc[spans].lineconfig |= DAHDI_CONFIG_CCS;
		lc[spans].lineconfig &= ~(DAHDI_CONFIG_ESF | DAHDI_CONFIG_D4);
	} else if (!strcasecmp(realargs[3], "cas")) {
		lc[spans].lineconfig &= ~DAHDI_CONFIG_CCS;
		lc[spans].lineconfig &= ~(DAHDI_CONFIG_ESF | DAHDI_CONFIG_D4);
	} else {
		error("Framing(T1)/Signalling(E1) must be one of 'd4', 'esf', 'cas' or 'ccs', not '%s'\n", realargs[3]);
		return -1;
	}
	if (!strcasecmp(realargs[4], "ami")) {
		lc[spans].lineconfig &= ~(DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_HDB3);
		lc[spans].lineconfig |= DAHDI_CONFIG_AMI;
	} else if (!strcasecmp(realargs[4], "b8zs")) {
		lc[spans].lineconfig |= DAHDI_CONFIG_B8ZS;
		lc[spans].lineconfig &= ~(DAHDI_CONFIG_AMI | DAHDI_CONFIG_HDB3);
	} else if (!strcasecmp(realargs[4], "hdb3")) {
		lc[spans].lineconfig |= DAHDI_CONFIG_HDB3;
		lc[spans].lineconfig &= ~(DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS);
	} else {
		error("Coding must be one of 'ami', 'b8zs' or 'hdb3', not '%s'\n", realargs[4]);
		return -1;
	}
	for (i = 5; i < argc; i++) {
		if (!strcasecmp(realargs[i], "yellow"))
			lc[spans].lineconfig |= DAHDI_CONFIG_NOTOPEN;
		else if (!strcasecmp(realargs[i], "crc4"))
			lc[spans].lineconfig |= DAHDI_CONFIG_CRC4;
		else if (!strcasecmp(realargs[i], "nt"))
			lc[spans].lineconfig |= DAHDI_CONFIG_NTTE;
		else if (!strcasecmp(realargs[i], "te"))
			lc[spans].lineconfig &= ~DAHDI_CONFIG_NTTE;
		else if (!strcasecmp(realargs[i], "term"))
			lc[spans].lineconfig |= DAHDI_CONFIG_TERM;
		else {
			error("Remaining arguments may be any of: 'yellow', 'crc4', 'nt', 'te', 'term', not '%s'\n", realargs[i]);
			return -1;
		}

	}
	lc[spans].span = span;
	lc[spans].sync = timing;
	/* Valid span */
	spans++;
	return 0;
}

int apply_channels(int chans[], char *argstr)
{
	char *args[DAHDI_MAX_CHANNELS+1];
	char *range[3];
	int res,x, res2,y;
	int chan;
	int start, finish;
	char argcopy[256];
	res = parseargs(argstr, args, DAHDI_MAX_CHANNELS, ',');
	if (res < 0) {
		error("Too many arguments...  Max is %d\n", DAHDI_MAX_CHANNELS);
		return -1;
	}
	for (x=0;x<res;x++) {
		if (strchr(args[x], '-')) {
			/* It's a range */
			dahdi_copy_string(argcopy, args[x], sizeof(argcopy));
			res2 = parseargs(argcopy, range, 2, '-');
			if (res2 != 2) {
				error("Syntax error in range '%s'.  Should be <val1>-<val2>.\n", args[x]);
				return -1;
			}
			res2 =sscanf(range[0], "%d", &start);
			if (res2 != 1) {
				error("Syntax error.  Start of range '%s' should be a number from 1 to %d\n", args[x], DAHDI_MAX_CHANNELS - 1);
				return -1;
			} else if ((start < 1) || (start >= DAHDI_MAX_CHANNELS)) {
				error("Start of range '%s' must be between 1 and %d (not '%d')\n", args[x], DAHDI_MAX_CHANNELS - 1, start);
				return -1;
			}
			res2 =sscanf(range[1], "%d", &finish);
			if (res2 != 1) {
				error("Syntax error.  End of range '%s' should be a number from 1 to %d\n", args[x], DAHDI_MAX_CHANNELS - 1);
				return -1;
			} else if ((finish < 1) || (finish >= DAHDI_MAX_CHANNELS)) {
				error("end of range '%s' must be between 1 and %d (not '%d')\n", args[x], DAHDI_MAX_CHANNELS - 1, finish);
				return -1;
			}
			if (start > finish) {
				error("Range '%s' should start before it ends\n", args[x]);
				return -1;
			}
			for (y=start;y<=finish;y++)
				chans[y]=1;
		} else {
			/* It's a single channel */
			res2 =sscanf(args[x], "%d", &chan);
			if (res2 != 1) {
				error("Syntax error.  Channel should be a number from 1 to %d, not '%s'\n", DAHDI_MAX_CHANNELS - 1, args[x]);
				return -1;
			} else if ((chan < 1) || (chan >= DAHDI_MAX_CHANNELS)) {
				error("Channel must be between 1 and %d (not '%d')\n", DAHDI_MAX_CHANNELS - 1, chan);
				return -1;
			}
			chans[chan]=1;
		}		
	}
	return res;
}

int parse_idle(int *i, char *s)
{
	char a,b,c,d;
	if (s) {
		if (sscanf(s, "%c%c%c%c", &a,&b,&c,&d) == 4) {
			if (((a == '0') || (a == '1')) && ((b == '0') || (b == '1')) && ((c == '0') || (c == '1')) && ((d == '0') || (d == '1'))) {
				*i = 0;
				if (a == '1') 
					*i |= DAHDI_ABIT;
				if (b == '1')
					*i |= DAHDI_BBIT;
				if (c == '1')
					*i |= DAHDI_CBIT;
				if (d == '1')
					*i |= DAHDI_DBIT;
				return 0;
			}
		}
	}
	error("CAS Signalling requires idle definition in the form ':xxxx' at the end of the channel definition, where xxxx represent the a, b, c, and d bits\n");
	return -1;
}

static int parse_channel(char *channel, int *startchan)
{
	if (!channel || (sscanf(channel, "%d", startchan) != 1) || 
		(*startchan < 1)) {
		error("DACS requires a starting channel in the form ':x' where x is the channel\n");
		return -1;
	}
	return 0;
}

static int chanconfig(char *keyword, char *args)
{
	int chans[DAHDI_MAX_CHANNELS];
	int res = 0;
	int x;
	int master=0;
	int dacschan = 0;
	char *idle;
	bzero(chans, sizeof(chans));
	strtok(args, ":");
	idle = strtok(NULL, ":");
	if (!strcasecmp(keyword, "dacs") || !strcasecmp(keyword, "dacsrbs")) {
		res = parse_channel(idle, &dacschan);
	}
	if (!res)
		res = apply_channels(chans, args);
	if (res <= 0)
		return -1;
	for (x=1;x<DAHDI_MAX_CHANNELS;x++) {
		if (chans[x]) {
			if (slineno[x]) {
				error("Channel %d already configured as '%s' at line %d\n", x, sig[x], slineno[x]);
				continue;
			}
			if ((!strcasecmp(keyword, "dacs") || !strcasecmp(keyword, "dacsrbs")) && slineno[dacschan]) {
				error("DACS Destination channel %d already configured as '%s' at line %d\n", dacschan, sig[dacschan], slineno[dacschan]);
				continue;
			} else {
				cc[dacschan].chan = dacschan;
				cc[dacschan].master = dacschan;
				slineno[dacschan] = lineno;
			}
			cc[x].chan = x;
			cc[x].master = x;
			slineno[x] = lineno;
			if (!strcasecmp(keyword, "e&m")) {
				cc[x].sigtype = DAHDI_SIG_EM;
				sig[x] = sigtype_to_str(cc[x].sigtype);
			} else if (!strcasecmp(keyword, "e&me1")) {
				cc[x].sigtype = DAHDI_SIG_EM_E1;
				sig[x] = sigtype_to_str(cc[x].sigtype);
			} else if (!strcasecmp(keyword, "fxsls")) {
				cc[x].sigtype = DAHDI_SIG_FXSLS;
				sig[x] = sigtype_to_str(cc[x].sigtype);
			} else if (!strcasecmp(keyword, "fxsgs")) {
				cc[x].sigtype = DAHDI_SIG_FXSGS;
				sig[x] = sigtype_to_str(cc[x].sigtype);
			} else if (!strcasecmp(keyword, "fxsks")) {
				cc[x].sigtype = DAHDI_SIG_FXSKS;
				sig[x] = sigtype_to_str(cc[x].sigtype);
			} else if (!strcasecmp(keyword, "fxols")) {
				cc[x].sigtype = DAHDI_SIG_FXOLS;
				sig[x] = sigtype_to_str(cc[x].sigtype);
			} else if (!strcasecmp(keyword, "fxogs")) {
				cc[x].sigtype = DAHDI_SIG_FXOGS;
				sig[x] = sigtype_to_str(cc[x].sigtype);
			} else if (!strcasecmp(keyword, "fxoks")) {
				cc[x].sigtype = DAHDI_SIG_FXOKS;
				sig[x] = sigtype_to_str(cc[x].sigtype);
			} else if (!strcasecmp(keyword, "cas") || !strcasecmp(keyword, "user")) {
				if (parse_idle(&cc[x].idlebits, idle))
					return -1;
				cc[x].sigtype = DAHDI_SIG_CAS;
				sig[x] = sigtype_to_str(cc[x].sigtype);
			} else if (!strcasecmp(keyword, "dacs")) {
				/* Setup channel for monitor */
				cc[x].idlebits = dacschan;
				cc[x].sigtype = DAHDI_SIG_DACS;
				sig[x] = sigtype_to_str(cc[x].sigtype);
				/* Setup inverse */
				cc[dacschan].idlebits = x;
				cc[dacschan].sigtype = DAHDI_SIG_DACS;
				sig[x] = sigtype_to_str(cc[dacschan].sigtype);
				dacschan++;
			} else if (!strcasecmp(keyword, "dacsrbs")) {
				/* Setup channel for monitor */
				cc[x].idlebits = dacschan;
				cc[x].sigtype = DAHDI_SIG_DACS_RBS;
				sig[x] = sigtype_to_str(cc[x].sigtype);
				/* Setup inverse */
				cc[dacschan].idlebits = x;
				cc[dacschan].sigtype = DAHDI_SIG_DACS_RBS;
				sig[x] = sigtype_to_str(cc[dacschan].sigtype);
				dacschan++;
			} else if (!strcasecmp(keyword, "unused")) {
				cc[x].sigtype = 0;
				sig[x] = sigtype_to_str(cc[x].sigtype);
			} else if (!strcasecmp(keyword, "indclear") || !strcasecmp(keyword, "bchan")) {
				cc[x].sigtype = DAHDI_SIG_CLEAR;
				sig[x] = sigtype_to_str(cc[x].sigtype);
			} else if (!strcasecmp(keyword, "clear")) {
				sig[x] = sigtype_to_str(DAHDI_SIG_CLEAR);
				if (master) {
					cc[x].sigtype = DAHDI_SIG_SLAVE;
					cc[x].master = master;
				} else {
					cc[x].sigtype = DAHDI_SIG_CLEAR;
					master = x;
				}
			} else if (!strcasecmp(keyword, "rawhdlc")) {
				sig[x] = sigtype_to_str(DAHDI_SIG_HDLCRAW);
				if (master) {
					cc[x].sigtype = DAHDI_SIG_SLAVE;
					cc[x].master = master;
				} else {
					cc[x].sigtype = DAHDI_SIG_HDLCRAW;
					master = x;
				}
			} else if (!strcasecmp(keyword, "nethdlc")) {
				sig[x] = sigtype_to_str(DAHDI_SIG_HDLCNET);
				memset(cc[x].netdev_name, 0, sizeof(cc[x].netdev_name));
				if (master) {
					cc[x].sigtype = DAHDI_SIG_SLAVE;
					cc[x].master = master;
				} else {
					cc[x].sigtype = DAHDI_SIG_HDLCNET;
					if (idle) {
					    dahdi_copy_string(cc[x].netdev_name, idle, sizeof(cc[x].netdev_name));
					}
					master = x;
				}
			} else if (!strcasecmp(keyword, "fcshdlc")) {
				sig[x] = sigtype_to_str(DAHDI_SIG_HDLCFCS);
				if (master) {
					cc[x].sigtype = DAHDI_SIG_SLAVE;
					cc[x].master = master;
				} else {
					cc[x].sigtype = DAHDI_SIG_HDLCFCS;
					master = x;
				}
			} else if (!strcasecmp(keyword, "dchan")) {
				sig[x] = "D-channel";
				cc[x].sigtype = DAHDI_SIG_HDLCFCS;
			} else if (!strcasecmp(keyword, "hardhdlc")) {
				sig[x] = "Hardware assisted D-channel";
				cc[x].sigtype = DAHDI_SIG_HARDHDLC;
			} else if (!strcasecmp(keyword, "mtp2")) {
				sig[x] = "MTP2";
				cc[x].sigtype = DAHDI_SIG_MTP2;
			} else {
				fprintf(stderr, "Huh? (%s)\n", keyword);
			}
736 737 738 739 740 741 742 743

			if (cc[x].sigtype != DAHDI_SIG_CAS &&
			    cc[x].sigtype != DAHDI_SIG_DACS &&
			    cc[x].sigtype != DAHDI_SIG_DACS_RBS) {
				if (NULL != idle) {
					fprintf(stderr, "WARNING: idlebits are not valid on %s channels.\n", sig[x]);
				}
			}
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
		}
	}
	return 0;
}

static int setlaw(char *keyword, char *args)
{
	int res;
	int law;
	int x;
	int chans[DAHDI_MAX_CHANNELS];

	bzero(chans, sizeof(chans));
	res = apply_channels(chans, args);
	if (res <= 0)
		return -1;
	if (!strcasecmp(keyword, "alaw")) {
		law = DAHDI_LAW_ALAW;
	} else if (!strcasecmp(keyword, "mulaw")) {
		law = DAHDI_LAW_MULAW;
	} else if (!strcasecmp(keyword, "deflaw")) {
		law = DAHDI_LAW_DEFAULT;
	} else {
		fprintf(stderr, "Huh??? Don't know about '%s' law\n", keyword);
		return -1;
	}
	for (x=0;x<DAHDI_MAX_CHANNELS;x++) {
		if (chans[x])
			cc[x].deflaw = law;
	}
	return 0;
}

static int setfiftysixkhdlc(char *keyword, char *args)
{
	int res;

	res = apply_channels(fiftysixkhdlc, args);
	if (res <= 0)
		return -1;

	return 0;
}

788
static int apply_fiftysix(void)
789 790 791 792 793 794
{
	int x;
	int rate;
	int chanfd;

	for (x = 1; x < DAHDI_MAX_CHANNELS; x++) {
795
		if (skip_channel(x) || !cc[x].sigtype)
796
			continue;
797

798 799 800 801 802
		chanfd = open("/dev/dahdi/channel", O_RDWR);
		if (chanfd == -1) {
			fprintf(stderr, 
			    "Couldn't open /dev/dahdi/channel: %s\n", 
			    strerror(errno));
803
			return -1;	
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
		}

		if (ioctl(chanfd, DAHDI_SPECIFY, &x)) {
			close(chanfd);
			continue;
		}

		if (fiftysixkhdlc[x]) {
			printf("Setting channel %d to 56K mode (only valid on HDLC channels)\n", x);
			rate = 56;
		} else {
			rate = 64;
		}

		if (ioctl(chanfd, DAHDI_HDLC_RATE, &rate)) {
			fprintf(stderr, "Error setting HDLC rate\n");
			exit(-1);
		}
		close(chanfd);
	}
824
	return 0;
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
}

static int setechocan(char *keyword, char *args)
{
	int res;
	int chans[DAHDI_MAX_CHANNELS] = { 0, };
	char *echocan, *chanlist;
	unsigned int x;

	echocan = strtok(args, ",");

	while ((chanlist = strtok(NULL, ","))) {
		res = apply_channels(chans, chanlist);
		if (res <= 0) {
			return -1;
		}
	}

	for (x = 0; x < DAHDI_MAX_CHANNELS; x++) {
		if (chans[x]) {
			dahdi_copy_string(ae[x].echocan, echocan, sizeof(ae[x].echocan));
		}
	}

	return 0;
}

static int registerzone(char *keyword, char *args)
{
	if (numzones >= DAHDI_TONE_ZONE_MAX) {
		error("Too many tone zones specified\n");
		return 0;
	}
	dahdi_copy_string(zonestoload[numzones++], args, sizeof(zonestoload[0]));
	return 0;
}

static int defaultzone(char *keyword, char *args)
{
	struct tone_zone *z;
	if (!(z = tone_zone_find(args))) {
		error("No such tone zone known: %s\n", args);
		return 0;
	}
	deftonezone = z->zone;
	return 0;
}

#if 0
static int unimplemented(char *keyword, char *args)
{
	fprintf(stderr, "Warning: '%s' is not yet implemented\n", keyword);
	return 0;
}
#endif


/* Radio functions */

int ctcss(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int rxtone;
	int rxtag;
	int txtone;
	int isdcs = 0;
892
	res = parseargs(args, realargs, 3, ',');
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
	if (res != 3) {
		error("Incorrect number of arguments to 'ctcss' (should be <rxtone>,<rxtag>,<txtone>)\n");
		return -1;
	}
	res = sscanf(realargs[0], "%d", &rxtone);
	if ((res == 1) && (rxtone < 1))
		res = -1;
	if (res != 1) {
		error("Invalid rxtone '%s', should be a number > 0.\n", realargs[0]);
		return -1;
	}
	res = sscanf(realargs[1], "%d", &rxtag);
	if ((res == 1) && (rxtag < 0))
		res = -1;
	if (res != 1) {
		error("Invalid rxtag '%s', should be a number > 0.\n", realargs[1]);
		return -1;
	}
	if ((*realargs[2] == 'D') || (*realargs[2] == 'd'))
	{
		realargs[2]++;
		isdcs = 0x8000;
	}
	res = sscanf(realargs[2], "%d", &txtone);
	if ((res == 1) && (rxtag < 0))
		res = -1;
	if (res != 1) {
		error("Invalid txtone '%s', should be a number > 0.\n", realargs[2]);
		return -1;
	}

	if (toneindex >= NUM_TONES)
	{
		error("Cannot specify more then %d CTCSS tones\n",NUM_TONES);
		return -1;
	}
	rxtones[toneindex] = rxtone;
	rxtags[toneindex] = rxtag;
	txtones[toneindex] = txtone | isdcs;
	toneindex++;
	return 0;
}

int dcsrx(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int rxtone;
941
	res = parseargs(args, realargs, 1, ',');
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
	if (res != 1) {
		error("Incorrect number of arguments to 'dcsrx' (should be <rxtone>)\n");
		return -1;
	}
	res = sscanf(realargs[0], "%d", &rxtone);
	if ((res == 1) && (rxtone < 1))
		res = -1;
	if (res != 1) {
		error("Invalid rxtone '%s', should be a number > 0.\n", realargs[0]);
		return -1;
	}

	rxtones[0] = rxtone;
	return 0;
}

int tx(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int txtone;
	int isdcs = 0;
964
	res = parseargs(args, realargs, 1, ',');
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
	if (res != 1) {
		error("Incorrect number of arguments to 'tx' (should be <txtone>)\n");
		return -1;
	}
	if ((*realargs[0] == 'D') || (*realargs[0] == 'd'))
	{
		realargs[0]++;
		isdcs = 0x8000;
	}
	res = sscanf(realargs[0], "%d", &txtone);
	if ((res == 1) && (txtone < 1))
		res = -1;
	if (res != 1) {
		error("Invalid tx (tone) '%s', should be a number > 0.\n", realargs[0]);
		return -1;
	}

	txtones[0] = txtone | isdcs;
	return 0;
}

int debounce_time(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int val;
991
	res = parseargs(args, realargs, 1, ',');
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	if (res != 1) {
		error("Incorrect number of arguments to 'debouncetime' (should be <value>)\n");
		return -1;
	}
	res = sscanf(realargs[0], "%d", &val);
	if ((res == 1) && (val < 1))
		res = -1;
	if (res != 1) {
		error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
		return -1;
	}

	debouncetime = val;
	return 0;
}

int burst_time(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int val;
1013
	res = parseargs(args, realargs, 1, ',');
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	if (res != 1) {
		error("Incorrect number of arguments to 'bursttime' (should be <value>)\n");
		return -1;
	}
	res = sscanf(realargs[0], "%d", &val);
	if ((res == 1) && (val < 1))
		res = -1;
	if (res != 1) {
		error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
		return -1;
	}

	bursttime = val;
	return 0;
}

int tx_gain(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int val;
1035
	res = parseargs(args, realargs, 1, ',');
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	if (res != 1) {
		error("Incorrect number of arguments to 'txgain' (should be <value>)\n");
		return -1;
	}
	res = sscanf(realargs[0], "%d", &val);
	if (res != 1) {
		error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
		return -1;
	}

	txgain = val;
	return 0;
}

int rx_gain(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int val;
1055
	res = parseargs(args, realargs, 1, ',');
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	if (res != 1) {
		error("Incorrect number of arguments to 'rxgain' (should be <value>)\n");
		return -1;
	}
	res = sscanf(realargs[0], "%d", &val);
	if (res != 1) {
		error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
		return -1;
	}

	rxgain = val;
	return 0;
}

int de_emp(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int val;
1075
	res = parseargs(args, realargs, 1, ',');
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	if (res != 1) {
		error("Incorrect number of arguments to 'de-emp' (should be <value>)\n");
		return -1;
	}
	res = sscanf(realargs[0], "%d", &val);
	if ((res == 1) && (val < 1))
		res = -1;
	if (res != 1) {
		error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
		return -1;
	}

	deemp = val;
	return 0;
}

int pre_emp(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int val;
1097
	res = parseargs(args, realargs, 1, ',');
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	if (res != 1) {
		error("Incorrect number of arguments to 'pre_emp' (should be <value>)\n");
		return -1;
	}
	res = sscanf(realargs[0], "%d", &val);
	if ((res == 1) && (val < 1))
		res = -1;
	if (res != 1) {
		error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
		return -1;
	}

	preemp = val;
	return 0;
}

int invert_cor(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int val;
1119
	res = parseargs(args, realargs, 1, ',');
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	if (res != 1) {
		error("Incorrect number of arguments to 'invertcor' (should be <value>)\n");
		return -1;
	}
	if ((*realargs[0] == 'y') || (*realargs[0] == 'Y')) val = 1;
	else if ((*realargs[0] == 'n') || (*realargs[0] == 'N')) val = 0;
	else
	{
		res = sscanf(realargs[0], "%d", &val);
		if ((res == 1) && (val < 0))
			res = -1;
		if (res != 1) {
			error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
			return -1;
		}
	}
	invertcor = (val > 0);
	return 0;
}

int ext_tone(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int val;
1145
	res = parseargs(args, realargs, 1, ',');
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
	if (res != 1) {
		error("Incorrect number of arguments to 'exttone' (should be <value>)\n");
		return -1;
	}
	if ((*realargs[0] == 'y') || (*realargs[0] == 'Y')) val = 1;
	else if ((*realargs[0] == 'n') || (*realargs[0] == 'N')) val = 0;
	else if ((*realargs[0] == 'i') || (*realargs[0] == 'I')) val = 2;
	else
	{
		res = sscanf(realargs[0], "%d", &val);
		if ((res == 1) && (val < 0))
			res = -1;
		if (val > 2) res = -1;
		if (res != 1) {
			error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
			return -1;
		}
	}
	exttone = val;
	return 0;
}

int cor_thresh(char *keyword, char *args)
{
	static char *realargs[10];
	int res;
	int val;
	int x = 0;
1174
	res = parseargs(args, realargs, 1, ',');
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
	if (res != 1) {
		error("Incorrect number of arguments to 'corthresh' (should be <value>)\n");
		return -1;
	}
	res = sscanf(realargs[0], "%d", &val);
	if ((res == 1) && (val < 1))
		res = -1;
	for(x = 0; corthreshes[x]; x++)
	{
		if (corthreshes[x] == val) break;
	}
	if (!corthreshes[x]) res = -1;
	if (res != 1) {
		error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
		return -1;
	}
	corthresh = x + 1;
	return 0;
}

static int rad_chanconfig(char *keyword, char *args)
{
	int chans[DAHDI_MAX_CHANNELS];
	int res = 0;
	int x,i,n;
	struct dahdi_radio_param p;
1201
	int chanfd;
1202 1203 1204 1205 1206 1207 1208 1209

	toneindex = 1;
	bzero(chans, sizeof(chans));
	res = apply_channels(chans, args);
	if (res <= 0)
		return -1;
	for (x=1;x<DAHDI_MAX_CHANNELS;x++) {
		if (chans[x]) {
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
			const char *CHANNEL_FILENAME = "/dev/dahdi/channel";
			chanfd = open(CHANNEL_FILENAME, O_RDWR);
			if (-1 == chanfd) {
				error("Failed to open '%s'.\n", CHANNEL_FILENAME);
				exit(-1);
			}

			res = ioctl(chanfd, DAHDI_SPECIFY, &x);
			if (res) {
				error("Failed to open channel %d.\n", x);
				close(chanfd);
				continue;
			}
1223
			p.radpar = DAHDI_RADPAR_NUMTONES;
1224
			if (ioctl(chanfd,DAHDI_RADIO_GETPARAM,&p) == -1)
1225 1226 1227 1228 1229 1230 1231
				n = 0;
			else
				n = p.data;

			if (n)
			{
				p.radpar = DAHDI_RADPAR_INITTONE;
1232
				if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1) {
1233 1234 1235 1236 1237 1238 1239 1240 1241
					error("Cannot init tones for channel %d\n",x);
				}
				if (!rxtones[0]) for(i = 1; i <= n; i++)
				{
					if (rxtones[i])
					{
						p.radpar = DAHDI_RADPAR_RXTONE;
						p.index = i;
						p.data = rxtones[i];
1242
						if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1243 1244 1245 1246 1247 1248 1249
							error("Cannot set rxtone on channel %d\n",x);
					}
					if (rxtags[i])
					{
						p.radpar = DAHDI_RADPAR_RXTONECLASS;
						p.index = i;
						p.data = rxtags[i];
1250
						if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1251 1252 1253 1254 1255 1256 1257
							error("Cannot set rxtag on channel %d\n",x);
					}
					if (txtones[i])
					{
						p.radpar = DAHDI_RADPAR_TXTONE;
						p.index = i;
						p.data = txtones[i];
1258
						if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1259 1260 1261 1262 1263 1264 1265 1266
							error("Cannot set txtone on channel %d\n",x);
					}
				} else { /* if we may have DCS receive */
					if (rxtones[0])
					{
						p.radpar = DAHDI_RADPAR_RXTONE;
						p.index = 0;
						p.data = rxtones[0];
1267
						if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1268 1269 1270 1271 1272 1273 1274 1275
							error("Cannot set DCS rxtone on channel %d\n",x);
					}
				}
				if (txtones[0])
				{
					p.radpar = DAHDI_RADPAR_TXTONE;
					p.index = 0;
					p.data = txtones[0];
1276
					if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1277 1278 1279 1280 1281 1282 1283
						error("Cannot set default txtone on channel %d\n",x);
				}
			}
			if (debouncetime)
			{
				p.radpar = DAHDI_RADPAR_DEBOUNCETIME;
				p.data = debouncetime;
1284
				if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1285 1286 1287 1288 1289 1290
					error("Cannot set debouncetime on channel %d\n",x);
			}
			if (bursttime)
			{
				p.radpar = DAHDI_RADPAR_BURSTTIME;
				p.data = bursttime;
1291
				if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1292 1293 1294 1295
					error("Cannot set bursttime on channel %d\n",x);
			}
			p.radpar = DAHDI_RADPAR_DEEMP;
			p.data = deemp;
1296
			ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p);
1297 1298
			p.radpar = DAHDI_RADPAR_PREEMP;
			p.data = preemp;
1299
			ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p);
1300 1301
			p.radpar = DAHDI_RADPAR_TXGAIN;
			p.data = txgain;
1302
			ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p);
1303 1304
			p.radpar = DAHDI_RADPAR_RXGAIN;
			p.data = rxgain;
1305
			ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p);
1306 1307
			p.radpar = DAHDI_RADPAR_INVERTCOR;
			p.data = invertcor;
1308
			ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p);
1309 1310
			p.radpar = DAHDI_RADPAR_EXTRXTONE;
			p.data = exttone;
1311
			ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p);
1312 1313 1314 1315
			if (corthresh)
			{
				p.radpar = DAHDI_RADPAR_CORTHRESH;
				p.data = corthresh - 1;
1316
				if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1317 1318
					error("Cannot set corthresh on channel %d\n",x);
			}
1319 1320

			close(chanfd);
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
		}
	}
	clear_fields();
	return 0;
}

/* End Radio functions */

static void printconfig(int fd)
{
	int x,y;
	int ps;
	int configs=0;
	struct dahdi_versioninfo vi;

	strcpy(vi.version, "Unknown");
	strcpy(vi.echo_canceller, "Unknown");

	if (ioctl(fd, DAHDI_GETVERSION, &vi))
		error("Unable to read DAHDI version information.\n");

	printf("\nDAHDI Version: %s\n"
	       "Echo Canceller(s): %s\n"
	       "Configuration\n"
	       "======================\n\n", vi.version, vi.echo_canceller);
	for (x = 0; x < spans; x++) {
1347
		if (only_span && only_span != lc[x].span)
1348
			continue;
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
		printf("SPAN %d: %3s/%4s Build-out: %s\n",
		       lc[x].span,
		       (lc[x].lineconfig & DAHDI_CONFIG_D4 ? "D4" :
			lc[x].lineconfig & DAHDI_CONFIG_ESF ? "ESF" :
			lc[x].lineconfig & DAHDI_CONFIG_CCS ? "CCS" : "CAS"),
		       (lc[x].lineconfig & DAHDI_CONFIG_AMI ? "AMI" :
			lc[x].lineconfig & DAHDI_CONFIG_B8ZS ? "B8ZS" :
			lc[x].lineconfig & DAHDI_CONFIG_HDB3 ? "HDB3" : "???"),
		       lbostr[lc[x].lbo]);
	}
	for (x=0;x<numdynamic;x++) {
		printf("Dynamic span %d: driver %s, addr %s, channels %d, timing %d\n",
		       x +1, zds[x].driver, zds[x].addr, zds[x].numchans, zds[x].timing);
	}
	if (verbose > 1) {
		printf("\nChannel map:\n\n");
		for (x=1;x<DAHDI_MAX_CHANNELS;x++) {
1366 1367
			if (skip_channel(x))
				continue;
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
			if ((cc[x].sigtype != DAHDI_SIG_SLAVE) && (cc[x].sigtype)) {
				configs++;
				ps = 0;
				if ((cc[x].sigtype & __DAHDI_SIG_DACS) == __DAHDI_SIG_DACS)
					printf("Channel %02d %s to %02d", x, sig[x], cc[x].idlebits);
				else {
					printf("Channel %02d: %s (%s)", x, sig[x], laws[cc[x].deflaw]);
					printf(" (Echo Canceler: %s)", ae[x].echocan[0] ? ae[x].echocan : "none");
					for (y=1;y<DAHDI_MAX_CHANNELS;y++) {
						if (cc[y].master == x)  {
							printf("%s%02d", ps++ ? " " : " (Slaves: ", y);
						}
					}
				}
				if (ps)
					printf(")\n");
				else
					printf("\n");
			} else
				if (cc[x].sigtype) configs++;
		}
	} else {
		for (x=1;x<DAHDI_MAX_CHANNELS;x++) {
1391 1392
			if (skip_channel(x))
				continue;
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
			if (cc[x].sigtype)
				configs++;
		}
	}
	printf("\n%d channels to configure.\n\n", configs);
}

static struct handler {
	char *keyword;
	int (*func)(char *keyword, char *args);
} handlers[] = {
	{ "span", spanconfig },
	{ "dynamic", dspanconfig },
	{ "loadzone", registerzone },
	{ "defaultzone", defaultzone },
	{ "e&m", chanconfig },
	{ "e&me1", chanconfig },
	{ "fxsls", chanconfig },
	{ "fxsgs", chanconfig },
	{ "fxsks", chanconfig },
	{ "fxols", chanconfig },
	{ "fxogs", chanconfig },
	{ "fxoks", chanconfig },
	{ "rawhdlc", chanconfig },
	{ "nethdlc", chanconfig },
	{ "fcshdlc", chanconfig },
	{ "hardhdlc", chanconfig },
	{ "mtp2", chanconfig },
	{ "dchan", chanconfig },
	{ "bchan", chanconfig },
	{ "indclear", chanconfig },
	{ "clear", chanconfig },
	{ "unused", chanconfig },
	{ "cas", chanconfig },
	{ "dacs", chanconfig },
	{ "dacsrbs", chanconfig },
	{ "user", chanconfig },
	{ "alaw", setlaw },
	{ "mulaw", setlaw },
	{ "deflaw", setlaw },
	{ "ctcss", ctcss },
	{ "dcsrx", dcsrx },
	{ "rxdcs", dcsrx },
	{ "tx", tx },
	{ "debouncetime", debounce_time },
	{ "bursttime", burst_time },
	{ "exttone", ext_tone },
	{ "invertcor", invert_cor },
	{ "corthresh", cor_thresh },
	{ "rxgain", rx_gain },
	{ "txgain", tx_gain },
	{ "deemp", de_emp },
	{ "preemp", pre_emp },
	{ "channel", rad_chanconfig },
	{ "channels", rad_chanconfig },
	{ "echocanceller", setechocan },
	{ "56k", setfiftysixkhdlc },
};

static char *readline()
{
	static char buf[256];
	char *c;
	do {
		if (!fgets(buf, sizeof(buf), cf)) 
			return NULL;
		/* Strip comments */
		c = strchr(buf, '#');
		if (c)
			*c = '\0';
		trim(buf);
		lineno++;
	} while (!strlen(buf));
	return buf;
}

static void usage(char *argv0, int exitcode)
{
	char *c;
	c = strrchr(argv0, '/');
	if (!c)
		c = argv0;
	else
		c++;
	fprintf(stderr, "%s\n", dahdi_tools_version);
	fprintf(stderr, 
		"Usage: %s [options]\n"
		"    Valid options are:\n"
		"  -c <filename>     -- Use <filename> instead of " CONFIG_FILENAME "\n"
		"  -d [level]        -- Generate debugging output. (Default level is 1.)\n"
		"  -f                -- Always reconfigure every channel\n"
		"  -h                -- Generate this help statement\n"
		"  -s                -- Shutdown spans only\n"
		"  -t                -- Test mode only, do not apply\n"
1487 1488
		"  -C <chan_list>    -- Only configure specified channels\n"
		"  -S <spanno>       -- Only configure specified span\n"
1489 1490 1491 1492 1493
		"  -v                -- Verbose (more -v's means more verbose)\n"
	,c);
	exit(exitcode);
}

1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
static int chan_restrict(char *str)
{
	if (apply_channels(selected_channels, str) < 0)
		return 0;
	restrict_channels = 1;
	return 1;
}

static int span_restrict(char *str)
{
	long	spanno;
	char	*endptr;

	spanno = strtol(str, &endptr, 10);
	if (endptr == str) {
		fprintf(stderr, "Missing valid span number after '-S'\n");
		return 0;
	}
	if (*endptr != '\0') {
		fprintf(stderr, "Extra garbage after span number in '-S'\n");
		return 0;
	}
	only_span = spanno;
	return 1;
}

1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
static const char *SEM_NAME = "dahdi_cfg";
static sem_t *lock = SEM_FAILED;

static void signal_handler(int signal)
{
	if (SEM_FAILED != lock) {
		sem_unlink(SEM_NAME);
	}
	/* The default handler should have been restored before this handler was
	 * called, so we can let the "normal" processing finish the cleanup. */
	raise(signal);
}

1533 1534 1535 1536 1537 1538
int main(int argc, char *argv[])
{
	int c;
	char *buf;
	char *key, *value;
	int x,found;
1539
	int exit_code = 0;
1540
	struct sigaction act;
1541

1542
	while((c = getopt(argc, argv, "fthc:vsd::C:S:")) != -1) {
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
		switch(c) {
		case 'c':
			filename=optarg;
			break;
		case 'h':
			usage(argv[0], 0);
			break;
		case '?':
			usage(argv[0], 1);
			break;
		case 'v':
			verbose++;
			break;
		case 'f':
			force++;
			break;
		case 't':
1560
			dry_run = 1;
1561 1562 1563 1564
			break;
		case 's':
			stopmode = 1;
			break;
1565 1566 1567 1568 1569 1570 1571 1572
		case 'C':
			if (!chan_restrict(optarg))
				usage(argv[0], 1);
			break;
		case 'S':
			if (!span_restrict(optarg))
				usage(argv[0], 1);
			break;
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
		case 'd':
			if (optarg)
				debug = atoi(optarg);
			else
				debug = 1;	
			break;
		}
	}
	
	if (verbose) {
		fprintf(stderr, "%s\n", dahdi_tools_version);
	}

1586 1587 1588 1589
	if (!restrict_channels && only_span) {
		error("-S requires -C\n");
		goto finish;
	}
1590
	if (!restrict_channels && !only_span) {
1591
		bool all_assigned = wait_for_all_spans_assigned(5);
1592

1593
		if (!all_assigned) {
1594 1595 1596 1597 1598
			fprintf(stderr,
				"Timeout waiting for all spans to be assigned.\n");
		}
	}

1599 1600 1601 1602 1603
	if (fd == -1) fd = open(MASTER_DEVICE, O_RDWR);
	if (fd < 0) {
		error("Unable to open master device '%s'\n", MASTER_DEVICE);
		goto finish;
	}
1604 1605 1606 1607
	if (strcmp(filename, "-") == 0)
		cf = fdopen(STDIN_FILENO, "r");
	else
		cf = fopen(filename, "r");
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
	if (cf) {
		while((buf = readline())) {
			if (*buf == 10) /* skip new line */
				continue;

			if (debug & DEBUG_READER) 
				fprintf(stderr, "Line %d: %s\n", lineno, buf);

			if ((value = strchr(buf, '='))) {
				*value++ = '\0';
				value = trim(value);
				key = trim(buf);
			}

			if (!value || !*value || !*key) {
				error("Syntax error. Should be <keyword>=<value>\n");
				continue;
			}

			if (debug & DEBUG_PARSER)
				fprintf(stderr, "Keyword: [%s], Value: [%s]\n", key, value);

			found = 0;
			for (x = 0; x < sizeof(handlers) / sizeof(handlers[0]); x++) {
				if (!strcasecmp(key, handlers[x].keyword)) {
					found++;
					handlers[x].func(key, value);
					break;
				}
			}

			if (!found) 
				error("Unknown keyword '%s'\n", key);
		}
		if (debug & DEBUG_READER)
			fprintf(stderr, "<End of File>\n");
		/* fclose(cf); // causes seg fault (double free) */
	} else {
		error("Unable to open configuration file '%s'\n", filename);
	}

finish:
	if (errcnt) {
		fprintf(stderr, "\n%d error(s) detected\n\n", errcnt);
		exit(1);
	}
	if (verbose) {
		printconfig(fd);
	}

1658 1659
	if (dry_run)
		exit(0);
1660 1661 1662 1663 1664
	
	if (debug & DEBUG_APPLY) {
		printf("About to open Master device\n");
		fflush(stdout);
	}
1665

1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
	sigemptyset(&act.sa_mask);
	act.sa_handler = signal_handler;
	act.sa_flags = SA_RESETHAND;

	if (sigaction(SIGTERM, &act, NULL) == -1) {
		perror("Failed to install SIGTERM handler.");
		exit(1);
	}
	if (sigaction(SIGINT, &act, NULL) == -1) {
		perror("Failed to install SIGINT handler.");
		exit(1);
	}

1679 1680
	lock = sem_open(SEM_NAME, O_CREAT, O_RDWR, 1);
	if (SEM_FAILED == lock) {
1681
		perror("Unable to create 'dahdi_cfg' mutex");
1682 1683 1684 1685 1686
		exit_code = 1;
		goto release_sem;
	}

	if (-1 == sem_wait(lock)) {
1687
		perror("Failed to wait for 'dahdi_cfg' mutex");
1688
		exit_code = 1;
1689
		goto unlink_sem;
1690 1691
	}

1692 1693 1694 1695 1696
	if (!restrict_channels && !only_span) {
		for (x=0;x<numdynamic;x++) {
			/* destroy them all */
			ioctl(fd, DAHDI_DYNAMIC_DESTROY, &zds[x]);
		}
1697
	}
1698

1699 1700
	if (stopmode) {
		for (x=0;x<spans;x++) {
1701
			if (only_span && lc[x].span != only_span)
1702
				continue;
1703 1704 1705
			if (ioctl(fd, DAHDI_SHUTDOWN, &lc[x].span)) {
				fprintf(stderr, "DAHDI shutdown failed: %s\n", strerror(errno));
				close(fd);
1706 1707
				exit_code = 1;
				goto release_sem;
1708 1709
			}
		}
1710 1711
		exit_code = 1;
		goto release_sem;
1712 1713
	}
	for (x=0;x<spans;x++) {
1714
		if (only_span && lc[x].span != only_span)
1715
			continue;
1716 1717 1718
		if (ioctl(fd, DAHDI_SPANCONFIG, lc + x)) {
			fprintf(stderr, "DAHDI_SPANCONFIG failed on span %d: %s (%d)\n", lc[x].span, strerror(errno), errno);
			close(fd);
1719 1720
			exit_code = 1;
			goto release_sem;
1721 1722
		}
	}
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738

	if (!restrict_channels && !only_span) {

		sem_post(lock);

		for (x=0;x<numdynamic;x++) {
			if (ioctl(fd, DAHDI_DYNAMIC_CREATE, &zds[x])) {
				fprintf(stderr, "DAHDI dynamic span creation failed: %s\n", strerror(errno));
				close(fd);
				exit_code = 1;
				goto release_sem;
			}
			wait_for_all_spans_assigned(1);
		}

		if (-1 == sem_wait(lock)) {
1739
			perror("Failed to wait for 'dahdi_cfg' mutex after creating dynamic spans");
1740
			exit_code = 1;
1741
			goto unlink_sem;
1742 1743
		}
	}
1744

1745 1746 1747 1748
	for (x=1;x<DAHDI_MAX_CHANNELS;x++) {
		struct dahdi_params current_state;
		int master;
		int needupdate = force;
1749 1750 1751 1752 1753 1754 1755 1756

		if (skip_channel(x)) {
			if (debug & DEBUG_APPLY) {
				printf("Skip device %d\n", x);
				fflush(stdout);
			}
			continue;
		}
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
		if (debug & DEBUG_APPLY) {
			printf("Configuring device %d\n", x);
			fflush(stdout);
		}
		if (!cc[x].sigtype)
			continue;
		
		if (!needupdate) {
			memset(&current_state, 0, sizeof(current_state));
			current_state.channo = cc[x].chan | DAHDI_GET_PARAMS_RETURN_MASTER;
			if (ioctl(fd, DAHDI_GET_PARAMS, &current_state))
				needupdate = 1;
		}
		
		if (!needupdate) {
			master = current_state.channo >> 16;
			
			if (cc[x].sigtype != current_state.sigtype) {
				needupdate++;
				if (verbose > 1)
					printf("Changing signalling on channel %d from %s to %s\n",
					       cc[x].chan, sigtype_to_str(current_state.sigtype),
					       sigtype_to_str(cc[x].sigtype));
			}
			
			if ((cc[x].deflaw != DAHDI_LAW_DEFAULT) && (cc[x].deflaw != current_state.curlaw)) {
				needupdate++;
				if (verbose > 1)
					printf("Changing law on channel %d from %s to %s\n",
					       cc[x].chan, laws[current_state.curlaw],
					       laws[cc[x].deflaw]);
			}
			
			if (cc[x].master != master) {
				needupdate++;
				if (verbose > 1)
					printf("Changing master of channel %d from %d to %d\n",
					       cc[x].chan, master,
					       cc[x].master);
			}
			
			if (cc[x].idlebits != current_state.idlebits) {
				needupdate++;
				if (verbose > 1)
					printf("Changing idle bits of channel %d from %d to %d\n",
					       cc[x].chan, current_state.idlebits,
					       cc[x].idlebits);
			}
		}
		
		if (needupdate && ioctl(fd, DAHDI_CHANCONFIG, &cc[x])) {
			fprintf(stderr, "DAHDI_CHANCONFIG failed on channel %d: %s (%d)\n", x, strerror(errno), errno);
			if (errno == EINVAL) {
				/* give helpful suggestions on signaling errors */
				fprintf(stderr, "Selected signaling not "
						"supported\n");
				fprintf(stderr, "Possible causes:\n");
				switch(cc[x].sigtype) {
				case DAHDI_SIG_FXOKS:
				case DAHDI_SIG_FXOLS:
				case DAHDI_SIG_FXOGS:
					fprintf(stderr, "\tFXO signaling is "
						"being used on a FXO interface"
						" (use a FXS signaling variant"
						")\n");
					fprintf(stderr, "\tRBS signaling is "
						"being used on a E1 CCS span"
						"\n");
					break;
				case DAHDI_SIG_FXSKS:
				case DAHDI_SIG_FXSLS:
				case DAHDI_SIG_FXSGS:
					fprintf(stderr, "\tFXS signaling is "
						"being used on a FXS interface"
						" (use a FXO signaling variant"
						")\n");
					fprintf(stderr, "\tRBS signaling is "
						"being used on a E1 CCS span"
						"\n");
					break;
				case DAHDI_SIG_EM:
					fprintf(stderr, "\te&m signaling is "
						"being used on a E1 line (use"
						" e&me1)\n");
					break;
				case DAHDI_SIG_EM_E1:
					fprintf(stderr, "\te&me1 signaling is "
						"being used on a T1 line (use "
						"e&m)\n");
					fprintf(stderr, "\tRBS signaling is "
						"being used on a E1 CCS span"
						"\n");
					break;
				case DAHDI_SIG_HARDHDLC:
					fprintf(stderr, "\thardhdlc is being "
						"used on a TE12x (use dchan)\n"
						);
					break;
				case DAHDI_SIG_HDLCFCS:
					fprintf(stderr, "\tdchan is being used"
						" on a BRI span (use hardhdlc)"
						"\n");
					break;
				default:
					break;
				}
				fprintf(stderr, "\tSignaling is being assigned"
					" to channel 16 of an E1 CAS span\n");
			}
			close(fd);
1867 1868
			exit_code = 1;
			goto release_sem;
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
		}

		ae[x].chan = x;
		if (verbose) {
			printf("Setting echocan for channel %d to %s\n", ae[x].chan, ae[x].echocan[0] ? ae[x].echocan : "none");
		}

		if (ioctl(fd, DAHDI_ATTACH_ECHOCAN, &ae[x])) {
			fprintf(stderr, "DAHDI_ATTACH_ECHOCAN failed on channel %d: %s (%d)\n", x, strerror(errno), errno);
			close(fd);
1879 1880
			exit_code = 1;
			goto release_sem;
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
		}
	}
	if (0 == numzones) {
		/* Default to the us zone if one wasn't specified. */
		dahdi_copy_string(zonestoload[numzones++], "us", sizeof(zonestoload[0]));
		deftonezone = 0;
	}

	for (x=0;x<numzones;x++) {
		if (debug & DEBUG_APPLY) {
			printf("Loading tone zone for %s\n", zonestoload[x]);
			fflush(stdout);
		}
		if (tone_zone_register(fd, zonestoload[x])) {
			if (errno != EBUSY)
				error("Unable to register tone zone '%s'\n", zonestoload[x]);
		}
	}
	if (debug & DEBUG_APPLY) {
		printf("Doing startup\n");
		fflush(stdout);
	}
	if (deftonezone > -1) {
		if (ioctl(fd, DAHDI_DEFAULTZONE, &deftonezone)) {
			fprintf(stderr, "DAHDI_DEFAULTZONE failed: %s (%d)\n", strerror(errno), errno);
			close(fd);
1907 1908
			exit_code = 1;
			goto release_sem;
1909 1910 1911
		}
	}
	for (x=0;x<spans;x++) {
1912
		if (only_span && lc[x].span != only_span)
1913
			continue;
1914 1915 1916
		if (ioctl(fd, DAHDI_STARTUP, &lc[x].span)) {
			fprintf(stderr, "DAHDI startup failed: %s\n", strerror(errno));
			close(fd);
1917 1918
			exit_code = 1;
			goto release_sem;
1919 1920
		}
	}
1921 1922 1923
	exit_code = apply_fiftysix();

release_sem:
1924
	if (SEM_FAILED != lock)
1925
		sem_post(lock);
1926 1927 1928

unlink_sem:
	if (SEM_FAILED != lock)
1929 1930
		sem_unlink(SEM_NAME);
	exit(exit_code);
1931
}