dahdi_cfg: minor: Rename fo_real to dry_run
[dahdi/tools.git] / dahdi_cfg.c
1 /*
2  * Configuration program for DAHDI Telephony Interface
3  *
4  * Written by Mark Spencer <markster@digium.com>
5  * Based on previous works, designs, and architectures conceived and
6  * written by Jim Dixon <jim@lambdatel.com>.
7  *
8  * Copyright (C) 2001 Jim Dixon / Zapata Telephony.
9  * Copyright (C) 2001-2008 Digium, Inc.
10  *
11  * All rights reserved.
12  *
13  * Primary Author: Mark Spencer <markster@digium.com>
14  * Radio Support by Jim Dixon <jim@lambdatel.com>
15  */
16
17 /*
18  * See http://www.asterisk.org for more information about
19  * the Asterisk project. Please do not directly contact
20  * any of the maintainers of this project for assistance;
21  * the project provides a web site, mailing lists and IRC
22  * channels for your use.
23  *
24  * This program is free software, distributed under the terms of
25  * the GNU General Public License Version 2 as published by the
26  * Free Software Foundation. See the LICENSE file included with
27  * this program for more details.
28  */
29
30 #include <stdio.h> 
31 #include <getopt.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <sys/ioctl.h>
38 #include <fcntl.h>
39 #include <sys/stat.h>
40 #include <semaphore.h>
41 #include <errno.h>
42 #include <dirent.h>
43 #include <stdbool.h>
44
45 #include <dahdi/user.h>
46 #include "tonezone.h"
47 #include "dahdi_tools_version.h"
48
49 #define CONFIG_FILENAME "/etc/dahdi/system.conf"
50 #define MASTER_DEVICE   "/dev/dahdi/ctl"
51
52 #define NUM_SPANS DAHDI_MAX_SPANS
53
54 #define NUM_TONES 15
55
56 /*! A sanity check for the timing parameter of the span. 
57  *
58  * Note that each driver using it is still responsible for validating
59  * that value.
60  */
61 #define MAX_TIMING 255 
62
63 /* Assume no more than 1024 dynamics */
64 #define NUM_DYNAMIC     1024
65
66 static int lineno=0;
67
68 static FILE *cf;
69
70 static char *filename=CONFIG_FILENAME;
71
72 int rxtones[NUM_TONES + 1],rxtags[NUM_TONES + 1],txtones[NUM_TONES + 1];
73 int bursttime = 0, debouncetime = 0, invertcor = 0, exttone = 0, corthresh = 0;
74 int txgain = 0, rxgain = 0, deemp = 0, preemp = 0;
75
76 int corthreshes[] = {3125,6250,9375,12500,15625,18750,21875,25000,0} ;
77
78 static int toneindex = 1;
79
80 #define DEBUG_READER (1 << 0)
81 #define DEBUG_PARSER (1 << 1)
82 #define DEBUG_APPLY  (1 << 2)
83 static int debug = 0;
84
85 static int errcnt = 0;
86
87 static int deftonezone = -1;
88
89 static struct dahdi_lineconfig lc[DAHDI_MAX_SPANS];
90
91 static struct dahdi_chanconfig cc[DAHDI_MAX_CHANNELS];
92
93 static int current_span = 0;
94 static int only_span = 0;
95 static int restrict_channels = 0;
96 static int selected_channels[DAHDI_MAX_CHANNELS];
97 static int chan2span[DAHDI_MAX_CHANNELS];
98 static int declared_spans[DAHDI_MAX_SPANS];
99
100 static struct dahdi_attach_echocan ae[DAHDI_MAX_CHANNELS];
101
102 static struct dahdi_dynamic_span zds[NUM_DYNAMIC];
103
104 static const char *sig[DAHDI_MAX_CHANNELS];             /* Signalling */
105
106 static int slineno[DAHDI_MAX_CHANNELS]; /* Line number where signalling specified */
107
108 static int fiftysixkhdlc[DAHDI_MAX_CHANNELS];
109
110 static int spans=0;
111
112 static int dry_run = 0;
113
114 static int verbose = 0;
115
116 static int force = 0;
117
118 static int stopmode = 0;
119
120 static int numdynamic = 0;
121
122 static char zonestoload[DAHDI_TONE_ZONE_MAX][10];
123
124 static int numzones = 0;
125
126 static int fd = -1;
127
128 static const char *lbostr[] = {
129 "0 db (CSU)/0-133 feet (DSX-1)",
130 "133-266 feet (DSX-1)",
131 "266-399 feet (DSX-1)",
132 "399-533 feet (DSX-1)",
133 "533-655 feet (DSX-1)",
134 "-7.5db (CSU)",
135 "-15db (CSU)",
136 "-22.5db (CSU)"
137 };
138
139 static const char *laws[] = {
140         "Default",
141         "Mu-law",
142         "A-law"
143 };
144
145 static bool _are_all_spans_assigned(const char *device_path)
146 {
147         char attribute[1024];
148         int res;
149         FILE *fp;
150         int span_count;
151         DIR *dirp;
152         struct dirent *dirent;
153
154         snprintf(attribute, sizeof(attribute) - 1,
155                  "%s/span_count", device_path);
156         fp = fopen(attribute, "r");
157         if (NULL == fp) {
158                 fprintf(stderr, "Failed to open '%s'.\n", attribute);
159                 return false;
160         }
161         res = fscanf(fp, "%d", &span_count);
162         fclose(fp);
163
164         if (EOF == res) {
165                 fprintf(stderr, "Failed to read '%s'.\n", attribute);
166                 return false;
167         }
168
169         dirp = opendir(device_path);
170         while (span_count) {
171                 dirent = readdir(dirp);
172                 if (NULL == dirent)
173                         break;
174                 if (!strncmp("span-", dirent->d_name, 5)) {
175                         --span_count;
176                 }
177         }
178         closedir(dirp);
179         return (span_count > 0) ? false : true;
180 }
181
182 /**
183  * are_all_spans_assigned - Look in sysfs to see if all spans for a device are assigned.
184  *
185  * Returns true if there are $span_count child spans of all devices, or false
186  *  otherwise.
187  */
188 static bool are_all_spans_assigned(void)
189 {
190         DIR *dirp;
191         struct dirent *dirent;
192         bool res = true;
193         char device_path[1024];
194
195         dirp = opendir("/sys/bus/dahdi_devices/devices");
196         if (!dirp) {
197                 /* If we cannot open dahdi_devices, either dahdi isn't loaded,
198                  * or we're using an older version of DAHDI that doesn't use
199                  * sysfs. */
200                 return true;
201         }
202
203         while (true && res) {
204
205                 dirent = readdir(dirp);
206                 if (NULL == dirent)
207                         break;
208
209                 if (!strcmp(dirent->d_name, ".") ||
210                     !strcmp(dirent->d_name, ".."))
211                         continue;
212
213                 snprintf(device_path, sizeof(device_path)-1,
214                          "/sys/bus/dahdi_devices/devices/%s", dirent->d_name);
215                 res = _are_all_spans_assigned(device_path);
216         }
217
218         closedir(dirp);
219         errno = 0;
220         return res;
221 }
222
223 static bool wait_for_all_spans_assigned(unsigned long timeout_sec)
224 {
225         bool all_assigned = are_all_spans_assigned();
226         unsigned int timeout = 10*timeout_sec;
227
228         while (!all_assigned && --timeout) {
229                 usleep(100000);
230                 all_assigned = are_all_spans_assigned();
231         }
232
233         return all_assigned;
234 }
235
236 static const char *sigtype_to_str(const int sig)
237 {
238         switch (sig) {
239         case 0:
240                 return "Unused";
241         case DAHDI_SIG_EM:
242                 return "E & M";
243         case DAHDI_SIG_EM_E1:
244                 return "E & M E1";
245         case DAHDI_SIG_FXSLS:
246                 return "FXS Loopstart";
247         case DAHDI_SIG_FXSGS:
248                 return "FXS Groundstart";
249         case DAHDI_SIG_FXSKS:
250                 return "FXS Kewlstart";
251         case DAHDI_SIG_FXOLS:
252                 return "FXO Loopstart";
253         case DAHDI_SIG_FXOGS:
254                 return "FXO Groundstart";
255         case DAHDI_SIG_FXOKS:
256                 return "FXO Kewlstart";
257         case DAHDI_SIG_CAS:
258                 return "CAS / User";
259         case DAHDI_SIG_DACS:
260                 return "DACS";
261         case DAHDI_SIG_DACS_RBS:
262                 return "DACS w/RBS";
263         case DAHDI_SIG_CLEAR:
264                 return "Clear channel";
265         case DAHDI_SIG_SLAVE:
266                 return "Slave channel";
267         case DAHDI_SIG_HDLCRAW:
268                 return "Raw HDLC";
269         case DAHDI_SIG_HDLCNET:
270                 return "Network HDLC";
271         case DAHDI_SIG_HDLCFCS:
272                 return "HDLC with FCS check";
273         case DAHDI_SIG_HARDHDLC:
274                 return "Hardware assisted D-channel";
275         case DAHDI_SIG_MTP2:
276                 return "MTP2";
277         default:
278                 return "Unknown";
279         }
280 }
281
282 static void clear_fields()
283 {
284
285         memset(rxtones,0,sizeof(rxtones));
286         memset(rxtags,0,sizeof(rxtags));
287         memset(txtones,0,sizeof(txtones));
288         bursttime = 0;
289         debouncetime = 0;
290         invertcor = 0;
291         exttone = 0;
292         txgain = 0;
293         rxgain = 0;
294         deemp = 0;
295         preemp = 0;
296 }
297
298 static int error(char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
299
300 static int error(char *fmt, ...)
301 {
302         int res;
303         static int shown=0;
304         va_list ap;
305         if (!shown) {
306                 fprintf(stderr, "Notice: Configuration file is %s\n", filename);
307                 shown++;
308         }
309         res = fprintf(stderr, "line %d: ", lineno);
310         va_start(ap, fmt);
311         vfprintf(stderr, fmt, ap);
312         va_end(ap);
313         errcnt++;
314         return res;
315 }
316
317 static char *trim(char *buf)
318 {
319         size_t len;
320
321         while (*buf && (*buf < 33)) {
322                 buf++;
323         }
324
325         len = strlen(buf);
326
327         while (len && buf[len-1] < 33) {
328                 buf[--len] = '\0';
329         }
330
331         return buf;
332 }
333
334 static int skip_channel(int x)
335 {
336         int     spanno = chan2span[x];
337
338         if (restrict_channels) {
339                 if (!selected_channels[x])
340                         return 1;
341                 /* sanity check */
342                 if (only_span) {
343                         if (spanno != 0 && only_span != spanno) {
344                                 fprintf(stderr,
345                                         "Only span %d. Skip selected channel %d from span %d\n",
346                                         only_span, x, spanno);
347                                 return 1;
348                         }
349                 }
350         } else {
351                 if (only_span && !declared_spans[only_span]) {
352                         fprintf(stderr,
353                                 "Error: analog span %d given to '-S', without '-C' restriction.\n",
354                                 only_span);
355                         exit(1);
356                 }
357                 if (only_span && only_span != spanno)
358                         return 1;
359         }
360         return 0;
361 }
362
363 static int parseargs(char *input, char *output[], int maxargs, char sep)
364 {
365         char *c;
366         int pos=0;
367         c = input;
368         output[pos++] = c;
369         while(*c) {
370                 while(*c && (*c != sep)) c++;
371                 if (*c) {
372                         *c = '\0';
373                         c++;
374                         while(*c && (*c < 33)) c++;
375                         if (*c)  {
376                                 if (pos >= maxargs)
377                                         return -1;
378                                 output[pos] = c;
379                                 trim(output[pos]);
380                                 pos++;
381                                 output[pos] = NULL;
382                                 /* Return error if we have too many */
383                         } else
384                                 return pos;
385                 }
386         }
387         return pos;
388 }
389
390 int dspanconfig(char *keyword, char *args)
391 {
392         static char *realargs[10];
393         int res;
394         int chans;
395         int timing;
396         res = parseargs(args, realargs, 4, ',');
397         if (res != 4) {
398                 error("Incorrect number of arguments to 'dynamic' (should be <driver>,<address>,<num channels>, <timing>)\n");
399                 return -1;
400         }
401         res = sscanf(realargs[2], "%d", &chans);
402         if ((res == 1) && (chans < 1))
403                 res = -1;
404         if (res != 1) {
405                 error("Invalid number of channels '%s', should be a number > 0.\n", realargs[2]);
406                 return -1;
407         }
408
409         res = sscanf(realargs[3], "%d", &timing);
410         if ((res == 1) && (timing < 0))
411                 res = -1;
412         if (res != 1) {
413                 error("Invalid timing '%s', should be a number > 0.\n", realargs[3]);
414                 return -1;
415         }
416
417
418         dahdi_copy_string(zds[numdynamic].driver, realargs[0], sizeof(zds[numdynamic].driver));
419         dahdi_copy_string(zds[numdynamic].addr, realargs[1], sizeof(zds[numdynamic].addr));
420         zds[numdynamic].numchans = chans;
421         zds[numdynamic].timing = timing;
422         
423         numdynamic++;
424         return 0;
425 }
426
427 int spanconfig(char *keyword, char *args)
428 {
429         static char *realargs[10];
430         int res;
431         int argc;
432         int span;
433         int timing;
434         int i;
435         argc = res = parseargs(args, realargs, 9, ',');
436         if ((res < 5) || (res > 9)) {
437                 error("Incorrect number of arguments to 'span' (should be <spanno>,<timing>,<lbo>,<framing>,<coding>[, crc4 | yellow [, yellow]])\n");
438                 return -1;
439         }
440         res = sscanf(realargs[0], "%d", &span);
441         if (res != 1) {
442                 error("Span number should be a valid span number, not '%s'\n", realargs[0]);
443                 return -1;
444         }
445         current_span = span;
446         declared_spans[span] = 1;
447         res = sscanf(realargs[1], "%d", &timing);
448         if ((res != 1) || (timing < 0) || (timing > MAX_TIMING)) {
449                 error("Timing should be a number from 0 to %d, not '%s'\n", 
450                                 MAX_TIMING,  realargs[1]);
451                 return -1;
452         }
453         res = sscanf(realargs[2], "%d", &lc[spans].lbo);
454         if (res != 1) {
455                 error("Line build-out (LBO) should be a number from 0 to 7 (usually 0) not '%s'\n", realargs[2]);
456                 return -1;
457         }
458         if ((lc[spans].lbo < 0) || (lc[spans].lbo > 7)) {
459                 error("Line build-out should be in the range 0 to 7, not %d\n", lc[spans].lbo);
460                 return -1;
461         }
462         if (!strcasecmp(realargs[3], "d4")) {
463                 lc[spans].lineconfig |= DAHDI_CONFIG_D4;
464                 lc[spans].lineconfig &= ~DAHDI_CONFIG_ESF;
465                 lc[spans].lineconfig &= ~DAHDI_CONFIG_CCS;
466         } else if (!strcasecmp(realargs[3], "esf")) {
467                 lc[spans].lineconfig |= DAHDI_CONFIG_ESF;
468                 lc[spans].lineconfig &= ~DAHDI_CONFIG_D4;
469                 lc[spans].lineconfig &= ~DAHDI_CONFIG_CCS;
470         } else if (!strcasecmp(realargs[3], "ccs")) {
471                 lc[spans].lineconfig |= DAHDI_CONFIG_CCS;
472                 lc[spans].lineconfig &= ~(DAHDI_CONFIG_ESF | DAHDI_CONFIG_D4);
473         } else if (!strcasecmp(realargs[3], "cas")) {
474                 lc[spans].lineconfig &= ~DAHDI_CONFIG_CCS;
475                 lc[spans].lineconfig &= ~(DAHDI_CONFIG_ESF | DAHDI_CONFIG_D4);
476         } else {
477                 error("Framing(T1)/Signalling(E1) must be one of 'd4', 'esf', 'cas' or 'ccs', not '%s'\n", realargs[3]);
478                 return -1;
479         }
480         if (!strcasecmp(realargs[4], "ami")) {
481                 lc[spans].lineconfig &= ~(DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_HDB3);
482                 lc[spans].lineconfig |= DAHDI_CONFIG_AMI;
483         } else if (!strcasecmp(realargs[4], "b8zs")) {
484                 lc[spans].lineconfig |= DAHDI_CONFIG_B8ZS;
485                 lc[spans].lineconfig &= ~(DAHDI_CONFIG_AMI | DAHDI_CONFIG_HDB3);
486         } else if (!strcasecmp(realargs[4], "hdb3")) {
487                 lc[spans].lineconfig |= DAHDI_CONFIG_HDB3;
488                 lc[spans].lineconfig &= ~(DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS);
489         } else {
490                 error("Coding must be one of 'ami', 'b8zs' or 'hdb3', not '%s'\n", realargs[4]);
491                 return -1;
492         }
493         for (i = 5; i < argc; i++) {
494                 if (!strcasecmp(realargs[i], "yellow"))
495                         lc[spans].lineconfig |= DAHDI_CONFIG_NOTOPEN;
496                 else if (!strcasecmp(realargs[i], "crc4"))
497                         lc[spans].lineconfig |= DAHDI_CONFIG_CRC4;
498                 else if (!strcasecmp(realargs[i], "nt"))
499                         lc[spans].lineconfig |= DAHDI_CONFIG_NTTE;
500                 else if (!strcasecmp(realargs[i], "te"))
501                         lc[spans].lineconfig &= ~DAHDI_CONFIG_NTTE;
502                 else if (!strcasecmp(realargs[i], "term"))
503                         lc[spans].lineconfig |= DAHDI_CONFIG_TERM;
504                 else {
505                         error("Remaining arguments may be any of: 'yellow', 'crc4', 'nt', 'te', 'term', not '%s'\n", realargs[i]);
506                         return -1;
507                 }
508
509         }
510         lc[spans].span = span;
511         lc[spans].sync = timing;
512         /* Valid span */
513         spans++;
514         return 0;
515 }
516
517 int apply_channels(int chans[], char *argstr)
518 {
519         char *args[DAHDI_MAX_CHANNELS+1];
520         char *range[3];
521         int res,x, res2,y;
522         int chan;
523         int start, finish;
524         char argcopy[256];
525         res = parseargs(argstr, args, DAHDI_MAX_CHANNELS, ',');
526         if (res < 0) {
527                 error("Too many arguments...  Max is %d\n", DAHDI_MAX_CHANNELS);
528                 return -1;
529         }
530         for (x=0;x<res;x++) {
531                 if (strchr(args[x], '-')) {
532                         /* It's a range */
533                         dahdi_copy_string(argcopy, args[x], sizeof(argcopy));
534                         res2 = parseargs(argcopy, range, 2, '-');
535                         if (res2 != 2) {
536                                 error("Syntax error in range '%s'.  Should be <val1>-<val2>.\n", args[x]);
537                                 return -1;
538                         }
539                         res2 =sscanf(range[0], "%d", &start);
540                         if (res2 != 1) {
541                                 error("Syntax error.  Start of range '%s' should be a number from 1 to %d\n", args[x], DAHDI_MAX_CHANNELS - 1);
542                                 return -1;
543                         } else if ((start < 1) || (start >= DAHDI_MAX_CHANNELS)) {
544                                 error("Start of range '%s' must be between 1 and %d (not '%d')\n", args[x], DAHDI_MAX_CHANNELS - 1, start);
545                                 return -1;
546                         }
547                         res2 =sscanf(range[1], "%d", &finish);
548                         if (res2 != 1) {
549                                 error("Syntax error.  End of range '%s' should be a number from 1 to %d\n", args[x], DAHDI_MAX_CHANNELS - 1);
550                                 return -1;
551                         } else if ((finish < 1) || (finish >= DAHDI_MAX_CHANNELS)) {
552                                 error("end of range '%s' must be between 1 and %d (not '%d')\n", args[x], DAHDI_MAX_CHANNELS - 1, finish);
553                                 return -1;
554                         }
555                         if (start > finish) {
556                                 error("Range '%s' should start before it ends\n", args[x]);
557                                 return -1;
558                         }
559                         for (y=start;y<=finish;y++)
560                                 chans[y]=1;
561                 } else {
562                         /* It's a single channel */
563                         res2 =sscanf(args[x], "%d", &chan);
564                         if (res2 != 1) {
565                                 error("Syntax error.  Channel should be a number from 1 to %d, not '%s'\n", DAHDI_MAX_CHANNELS - 1, args[x]);
566                                 return -1;
567                         } else if ((chan < 1) || (chan >= DAHDI_MAX_CHANNELS)) {
568                                 error("Channel must be between 1 and %d (not '%d')\n", DAHDI_MAX_CHANNELS - 1, chan);
569                                 return -1;
570                         }
571                         chans[chan]=1;
572                 }               
573         }
574         return res;
575 }
576
577 int parse_idle(int *i, char *s)
578 {
579         char a,b,c,d;
580         if (s) {
581                 if (sscanf(s, "%c%c%c%c", &a,&b,&c,&d) == 4) {
582                         if (((a == '0') || (a == '1')) && ((b == '0') || (b == '1')) && ((c == '0') || (c == '1')) && ((d == '0') || (d == '1'))) {
583                                 *i = 0;
584                                 if (a == '1') 
585                                         *i |= DAHDI_ABIT;
586                                 if (b == '1')
587                                         *i |= DAHDI_BBIT;
588                                 if (c == '1')
589                                         *i |= DAHDI_CBIT;
590                                 if (d == '1')
591                                         *i |= DAHDI_DBIT;
592                                 return 0;
593                         }
594                 }
595         }
596         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");
597         return -1;
598 }
599
600 static int parse_channel(char *channel, int *startchan)
601 {
602         if (!channel || (sscanf(channel, "%d", startchan) != 1) || 
603                 (*startchan < 1)) {
604                 error("DACS requires a starting channel in the form ':x' where x is the channel\n");
605                 return -1;
606         }
607         return 0;
608 }
609
610 static int chanconfig(char *keyword, char *args)
611 {
612         int chans[DAHDI_MAX_CHANNELS];
613         int res = 0;
614         int x;
615         int master=0;
616         int dacschan = 0;
617         char *idle;
618         int is_digital;
619         bzero(chans, sizeof(chans));
620         strtok(args, ":");
621         idle = strtok(NULL, ":");
622         if (!strcasecmp(keyword, "dacs") || !strcasecmp(keyword, "dacsrbs")) {
623                 res = parse_channel(idle, &dacschan);
624         }
625         if (!res)
626                 res = apply_channels(chans, args);
627         if (res <= 0)
628                 return -1;
629         for (x=1;x<DAHDI_MAX_CHANNELS;x++) {
630                 is_digital = 0;
631                 if (chans[x]) {
632                         if (slineno[x]) {
633                                 error("Channel %d already configured as '%s' at line %d\n", x, sig[x], slineno[x]);
634                                 continue;
635                         }
636                         if ((!strcasecmp(keyword, "dacs") || !strcasecmp(keyword, "dacsrbs")) && slineno[dacschan]) {
637                                 error("DACS Destination channel %d already configured as '%s' at line %d\n", dacschan, sig[dacschan], slineno[dacschan]);
638                                 continue;
639                         } else {
640                                 cc[dacschan].chan = dacschan;
641                                 cc[dacschan].master = dacschan;
642                                 slineno[dacschan] = lineno;
643                         }
644                         cc[x].chan = x;
645                         cc[x].master = x;
646                         slineno[x] = lineno;
647                         if (!strcasecmp(keyword, "e&m")) {
648                                 cc[x].sigtype = DAHDI_SIG_EM;
649                                 sig[x] = sigtype_to_str(cc[x].sigtype);
650                         } else if (!strcasecmp(keyword, "e&me1")) {
651                                 cc[x].sigtype = DAHDI_SIG_EM_E1;
652                                 sig[x] = sigtype_to_str(cc[x].sigtype);
653                         } else if (!strcasecmp(keyword, "fxsls")) {
654                                 cc[x].sigtype = DAHDI_SIG_FXSLS;
655                                 sig[x] = sigtype_to_str(cc[x].sigtype);
656                         } else if (!strcasecmp(keyword, "fxsgs")) {
657                                 cc[x].sigtype = DAHDI_SIG_FXSGS;
658                                 sig[x] = sigtype_to_str(cc[x].sigtype);
659                         } else if (!strcasecmp(keyword, "fxsks")) {
660                                 cc[x].sigtype = DAHDI_SIG_FXSKS;
661                                 sig[x] = sigtype_to_str(cc[x].sigtype);
662                         } else if (!strcasecmp(keyword, "fxols")) {
663                                 cc[x].sigtype = DAHDI_SIG_FXOLS;
664                                 sig[x] = sigtype_to_str(cc[x].sigtype);
665                         } else if (!strcasecmp(keyword, "fxogs")) {
666                                 cc[x].sigtype = DAHDI_SIG_FXOGS;
667                                 sig[x] = sigtype_to_str(cc[x].sigtype);
668                         } else if (!strcasecmp(keyword, "fxoks")) {
669                                 cc[x].sigtype = DAHDI_SIG_FXOKS;
670                                 sig[x] = sigtype_to_str(cc[x].sigtype);
671                         } else if (!strcasecmp(keyword, "cas") || !strcasecmp(keyword, "user")) {
672                                 if (parse_idle(&cc[x].idlebits, idle))
673                                         return -1;
674                                 cc[x].sigtype = DAHDI_SIG_CAS;
675                                 sig[x] = sigtype_to_str(cc[x].sigtype);
676                                 is_digital = 1;
677                         } else if (!strcasecmp(keyword, "dacs")) {
678                                 /* Setup channel for monitor */
679                                 cc[x].idlebits = dacschan;
680                                 cc[x].sigtype = DAHDI_SIG_DACS;
681                                 sig[x] = sigtype_to_str(cc[x].sigtype);
682                                 /* Setup inverse */
683                                 cc[dacschan].idlebits = x;
684                                 cc[dacschan].sigtype = DAHDI_SIG_DACS;
685                                 sig[x] = sigtype_to_str(cc[dacschan].sigtype);
686                                 dacschan++;
687                                 is_digital = 1;
688                         } else if (!strcasecmp(keyword, "dacsrbs")) {
689                                 /* Setup channel for monitor */
690                                 cc[x].idlebits = dacschan;
691                                 cc[x].sigtype = DAHDI_SIG_DACS_RBS;
692                                 sig[x] = sigtype_to_str(cc[x].sigtype);
693                                 /* Setup inverse */
694                                 cc[dacschan].idlebits = x;
695                                 cc[dacschan].sigtype = DAHDI_SIG_DACS_RBS;
696                                 sig[x] = sigtype_to_str(cc[dacschan].sigtype);
697                                 is_digital = 1;
698                                 dacschan++;
699                         } else if (!strcasecmp(keyword, "unused")) {
700                                 cc[x].sigtype = 0;
701                                 sig[x] = sigtype_to_str(cc[x].sigtype);
702                         } else if (!strcasecmp(keyword, "indclear") || !strcasecmp(keyword, "bchan")) {
703                                 cc[x].sigtype = DAHDI_SIG_CLEAR;
704                                 sig[x] = sigtype_to_str(cc[x].sigtype);
705                                 is_digital = 1;
706                         } else if (!strcasecmp(keyword, "clear")) {
707                                 sig[x] = sigtype_to_str(DAHDI_SIG_CLEAR);
708                                 if (master) {
709                                         cc[x].sigtype = DAHDI_SIG_SLAVE;
710                                         cc[x].master = master;
711                                 } else {
712                                         cc[x].sigtype = DAHDI_SIG_CLEAR;
713                                         master = x;
714                                 }
715                                 is_digital = 1;
716                         } else if (!strcasecmp(keyword, "rawhdlc")) {
717                                 sig[x] = sigtype_to_str(DAHDI_SIG_HDLCRAW);
718                                 if (master) {
719                                         cc[x].sigtype = DAHDI_SIG_SLAVE;
720                                         cc[x].master = master;
721                                 } else {
722                                         cc[x].sigtype = DAHDI_SIG_HDLCRAW;
723                                         master = x;
724                                 }
725                                 is_digital = 1;
726                         } else if (!strcasecmp(keyword, "nethdlc")) {
727                                 sig[x] = sigtype_to_str(DAHDI_SIG_HDLCNET);
728                                 memset(cc[x].netdev_name, 0, sizeof(cc[x].netdev_name));
729                                 if (master) {
730                                         cc[x].sigtype = DAHDI_SIG_SLAVE;
731                                         cc[x].master = master;
732                                 } else {
733                                         cc[x].sigtype = DAHDI_SIG_HDLCNET;
734                                         if (idle) {
735                                             dahdi_copy_string(cc[x].netdev_name, idle, sizeof(cc[x].netdev_name));
736                                         }
737                                         master = x;
738                                 }
739                                 is_digital = 1;
740                         } else if (!strcasecmp(keyword, "fcshdlc")) {
741                                 sig[x] = sigtype_to_str(DAHDI_SIG_HDLCFCS);
742                                 if (master) {
743                                         cc[x].sigtype = DAHDI_SIG_SLAVE;
744                                         cc[x].master = master;
745                                 } else {
746                                         cc[x].sigtype = DAHDI_SIG_HDLCFCS;
747                                         master = x;
748                                 }
749                                 is_digital = 1;
750                         } else if (!strcasecmp(keyword, "dchan")) {
751                                 sig[x] = "D-channel";
752                                 cc[x].sigtype = DAHDI_SIG_HDLCFCS;
753                                 is_digital = 1;
754                         } else if (!strcasecmp(keyword, "hardhdlc")) {
755                                 sig[x] = "Hardware assisted D-channel";
756                                 cc[x].sigtype = DAHDI_SIG_HARDHDLC;
757                                 is_digital = 1;
758                         } else if (!strcasecmp(keyword, "mtp2")) {
759                                 sig[x] = "MTP2";
760                                 cc[x].sigtype = DAHDI_SIG_MTP2;
761                                 is_digital = 1;
762                         } else {
763                                 fprintf(stderr, "Huh? (%s)\n", keyword);
764                         }
765
766                         if (cc[x].sigtype != DAHDI_SIG_CAS &&
767                             cc[x].sigtype != DAHDI_SIG_DACS &&
768                             cc[x].sigtype != DAHDI_SIG_DACS_RBS) {
769                                 if (NULL != idle) {
770                                         fprintf(stderr, "WARNING: idlebits are not valid on %s channels.\n", sig[x]);
771                                 }
772                         }
773
774                         if (is_digital) 
775                                 chan2span[x] = current_span;
776                         else
777                                 current_span = 0;
778                 }
779         }
780         return 0;
781 }
782
783 static int setlaw(char *keyword, char *args)
784 {
785         int res;
786         int law;
787         int x;
788         int chans[DAHDI_MAX_CHANNELS];
789
790         bzero(chans, sizeof(chans));
791         res = apply_channels(chans, args);
792         if (res <= 0)
793                 return -1;
794         if (!strcasecmp(keyword, "alaw")) {
795                 law = DAHDI_LAW_ALAW;
796         } else if (!strcasecmp(keyword, "mulaw")) {
797                 law = DAHDI_LAW_MULAW;
798         } else if (!strcasecmp(keyword, "deflaw")) {
799                 law = DAHDI_LAW_DEFAULT;
800         } else {
801                 fprintf(stderr, "Huh??? Don't know about '%s' law\n", keyword);
802                 return -1;
803         }
804         for (x=0;x<DAHDI_MAX_CHANNELS;x++) {
805                 if (chans[x])
806                         cc[x].deflaw = law;
807         }
808         return 0;
809 }
810
811 static int setfiftysixkhdlc(char *keyword, char *args)
812 {
813         int res;
814
815         res = apply_channels(fiftysixkhdlc, args);
816         if (res <= 0)
817                 return -1;
818
819         return 0;
820 }
821
822 static int apply_fiftysix(void)
823 {
824         int x;
825         int rate;
826         int chanfd;
827
828         for (x = 1; x < DAHDI_MAX_CHANNELS; x++) {
829                 if (skip_channel(x))
830                         continue;
831                 chanfd = open("/dev/dahdi/channel", O_RDWR);
832                 if (chanfd == -1) {
833                         fprintf(stderr, 
834                             "Couldn't open /dev/dahdi/channel: %s\n", 
835                             strerror(errno));
836                         return -1;      
837                 }
838
839                 if (ioctl(chanfd, DAHDI_SPECIFY, &x)) {
840                         close(chanfd);
841                         continue;
842                 }
843
844                 if (fiftysixkhdlc[x]) {
845                         printf("Setting channel %d to 56K mode (only valid on HDLC channels)\n", x);
846                         rate = 56;
847                 } else {
848                         rate = 64;
849                 }
850
851                 if (ioctl(chanfd, DAHDI_HDLC_RATE, &rate)) {
852                         fprintf(stderr, "Error setting HDLC rate\n");
853                         exit(-1);
854                 }
855                 close(chanfd);
856         }
857         return 0;
858 }
859
860 static int setechocan(char *keyword, char *args)
861 {
862         int res;
863         int chans[DAHDI_MAX_CHANNELS] = { 0, };
864         char *echocan, *chanlist;
865         unsigned int x;
866
867         echocan = strtok(args, ",");
868
869         while ((chanlist = strtok(NULL, ","))) {
870                 res = apply_channels(chans, chanlist);
871                 if (res <= 0) {
872                         return -1;
873                 }
874         }
875
876         for (x = 0; x < DAHDI_MAX_CHANNELS; x++) {
877                 if (chans[x]) {
878                         dahdi_copy_string(ae[x].echocan, echocan, sizeof(ae[x].echocan));
879                 }
880         }
881
882         return 0;
883 }
884
885 static int registerzone(char *keyword, char *args)
886 {
887         if (numzones >= DAHDI_TONE_ZONE_MAX) {
888                 error("Too many tone zones specified\n");
889                 return 0;
890         }
891         dahdi_copy_string(zonestoload[numzones++], args, sizeof(zonestoload[0]));
892         return 0;
893 }
894
895 static int defaultzone(char *keyword, char *args)
896 {
897         struct tone_zone *z;
898         if (!(z = tone_zone_find(args))) {
899                 error("No such tone zone known: %s\n", args);
900                 return 0;
901         }
902         deftonezone = z->zone;
903         return 0;
904 }
905
906 #if 0
907 static int unimplemented(char *keyword, char *args)
908 {
909         fprintf(stderr, "Warning: '%s' is not yet implemented\n", keyword);
910         return 0;
911 }
912 #endif
913
914
915 /* Radio functions */
916
917 int ctcss(char *keyword, char *args)
918 {
919         static char *realargs[10];
920         int res;
921         int rxtone;
922         int rxtag;
923         int txtone;
924         int isdcs = 0;
925         res = parseargs(args, realargs, 3, ',');
926         if (res != 3) {
927                 error("Incorrect number of arguments to 'ctcss' (should be <rxtone>,<rxtag>,<txtone>)\n");
928                 return -1;
929         }
930         res = sscanf(realargs[0], "%d", &rxtone);
931         if ((res == 1) && (rxtone < 1))
932                 res = -1;
933         if (res != 1) {
934                 error("Invalid rxtone '%s', should be a number > 0.\n", realargs[0]);
935                 return -1;
936         }
937         res = sscanf(realargs[1], "%d", &rxtag);
938         if ((res == 1) && (rxtag < 0))
939                 res = -1;
940         if (res != 1) {
941                 error("Invalid rxtag '%s', should be a number > 0.\n", realargs[1]);
942                 return -1;
943         }
944         if ((*realargs[2] == 'D') || (*realargs[2] == 'd'))
945         {
946                 realargs[2]++;
947                 isdcs = 0x8000;
948         }
949         res = sscanf(realargs[2], "%d", &txtone);
950         if ((res == 1) && (rxtag < 0))
951                 res = -1;
952         if (res != 1) {
953                 error("Invalid txtone '%s', should be a number > 0.\n", realargs[2]);
954                 return -1;
955         }
956
957         if (toneindex >= NUM_TONES)
958         {
959                 error("Cannot specify more then %d CTCSS tones\n",NUM_TONES);
960                 return -1;
961         }
962         rxtones[toneindex] = rxtone;
963         rxtags[toneindex] = rxtag;
964         txtones[toneindex] = txtone | isdcs;
965         toneindex++;
966         return 0;
967 }
968
969 int dcsrx(char *keyword, char *args)
970 {
971         static char *realargs[10];
972         int res;
973         int rxtone;
974         res = parseargs(args, realargs, 1, ',');
975         if (res != 1) {
976                 error("Incorrect number of arguments to 'dcsrx' (should be <rxtone>)\n");
977                 return -1;
978         }
979         res = sscanf(realargs[0], "%d", &rxtone);
980         if ((res == 1) && (rxtone < 1))
981                 res = -1;
982         if (res != 1) {
983                 error("Invalid rxtone '%s', should be a number > 0.\n", realargs[0]);
984                 return -1;
985         }
986
987         rxtones[0] = rxtone;
988         return 0;
989 }
990
991 int tx(char *keyword, char *args)
992 {
993         static char *realargs[10];
994         int res;
995         int txtone;
996         int isdcs = 0;
997         res = parseargs(args, realargs, 1, ',');
998         if (res != 1) {
999                 error("Incorrect number of arguments to 'tx' (should be <txtone>)\n");
1000                 return -1;
1001         }
1002         if ((*realargs[0] == 'D') || (*realargs[0] == 'd'))
1003         {
1004                 realargs[0]++;
1005                 isdcs = 0x8000;
1006         }
1007         res = sscanf(realargs[0], "%d", &txtone);
1008         if ((res == 1) && (txtone < 1))
1009                 res = -1;
1010         if (res != 1) {
1011                 error("Invalid tx (tone) '%s', should be a number > 0.\n", realargs[0]);
1012                 return -1;
1013         }
1014
1015         txtones[0] = txtone | isdcs;
1016         return 0;
1017 }
1018
1019 int debounce_time(char *keyword, char *args)
1020 {
1021         static char *realargs[10];
1022         int res;
1023         int val;
1024         res = parseargs(args, realargs, 1, ',');
1025         if (res != 1) {
1026                 error("Incorrect number of arguments to 'debouncetime' (should be <value>)\n");
1027                 return -1;
1028         }
1029         res = sscanf(realargs[0], "%d", &val);
1030         if ((res == 1) && (val < 1))
1031                 res = -1;
1032         if (res != 1) {
1033                 error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
1034                 return -1;
1035         }
1036
1037         debouncetime = val;
1038         return 0;
1039 }
1040
1041 int burst_time(char *keyword, char *args)
1042 {
1043         static char *realargs[10];
1044         int res;
1045         int val;
1046         res = parseargs(args, realargs, 1, ',');
1047         if (res != 1) {
1048                 error("Incorrect number of arguments to 'bursttime' (should be <value>)\n");
1049                 return -1;
1050         }
1051         res = sscanf(realargs[0], "%d", &val);
1052         if ((res == 1) && (val < 1))
1053                 res = -1;
1054         if (res != 1) {
1055                 error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
1056                 return -1;
1057         }
1058
1059         bursttime = val;
1060         return 0;
1061 }
1062
1063 int tx_gain(char *keyword, char *args)
1064 {
1065         static char *realargs[10];
1066         int res;
1067         int val;
1068         res = parseargs(args, realargs, 1, ',');
1069         if (res != 1) {
1070                 error("Incorrect number of arguments to 'txgain' (should be <value>)\n");
1071                 return -1;
1072         }
1073         res = sscanf(realargs[0], "%d", &val);
1074         if (res != 1) {
1075                 error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
1076                 return -1;
1077         }
1078
1079         txgain = val;
1080         return 0;
1081 }
1082
1083 int rx_gain(char *keyword, char *args)
1084 {
1085         static char *realargs[10];
1086         int res;
1087         int val;
1088         res = parseargs(args, realargs, 1, ',');
1089         if (res != 1) {
1090                 error("Incorrect number of arguments to 'rxgain' (should be <value>)\n");
1091                 return -1;
1092         }
1093         res = sscanf(realargs[0], "%d", &val);
1094         if (res != 1) {
1095                 error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
1096                 return -1;
1097         }
1098
1099         rxgain = val;
1100         return 0;
1101 }
1102
1103 int de_emp(char *keyword, char *args)
1104 {
1105         static char *realargs[10];
1106         int res;
1107         int val;
1108         res = parseargs(args, realargs, 1, ',');
1109         if (res != 1) {
1110                 error("Incorrect number of arguments to 'de-emp' (should be <value>)\n");
1111                 return -1;
1112         }
1113         res = sscanf(realargs[0], "%d", &val);
1114         if ((res == 1) && (val < 1))
1115                 res = -1;
1116         if (res != 1) {
1117                 error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
1118                 return -1;
1119         }
1120
1121         deemp = val;
1122         return 0;
1123 }
1124
1125 int pre_emp(char *keyword, char *args)
1126 {
1127         static char *realargs[10];
1128         int res;
1129         int val;
1130         res = parseargs(args, realargs, 1, ',');
1131         if (res != 1) {
1132                 error("Incorrect number of arguments to 'pre_emp' (should be <value>)\n");
1133                 return -1;
1134         }
1135         res = sscanf(realargs[0], "%d", &val);
1136         if ((res == 1) && (val < 1))
1137                 res = -1;
1138         if (res != 1) {
1139                 error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
1140                 return -1;
1141         }
1142
1143         preemp = val;
1144         return 0;
1145 }
1146
1147 int invert_cor(char *keyword, char *args)
1148 {
1149         static char *realargs[10];
1150         int res;
1151         int val;
1152         res = parseargs(args, realargs, 1, ',');
1153         if (res != 1) {
1154                 error("Incorrect number of arguments to 'invertcor' (should be <value>)\n");
1155                 return -1;
1156         }
1157         if ((*realargs[0] == 'y') || (*realargs[0] == 'Y')) val = 1;
1158         else if ((*realargs[0] == 'n') || (*realargs[0] == 'N')) val = 0;
1159         else
1160         {
1161                 res = sscanf(realargs[0], "%d", &val);
1162                 if ((res == 1) && (val < 0))
1163                         res = -1;
1164                 if (res != 1) {
1165                         error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
1166                         return -1;
1167                 }
1168         }
1169         invertcor = (val > 0);
1170         return 0;
1171 }
1172
1173 int ext_tone(char *keyword, char *args)
1174 {
1175         static char *realargs[10];
1176         int res;
1177         int val;
1178         res = parseargs(args, realargs, 1, ',');
1179         if (res != 1) {
1180                 error("Incorrect number of arguments to 'exttone' (should be <value>)\n");
1181                 return -1;
1182         }
1183         if ((*realargs[0] == 'y') || (*realargs[0] == 'Y')) val = 1;
1184         else if ((*realargs[0] == 'n') || (*realargs[0] == 'N')) val = 0;
1185         else if ((*realargs[0] == 'i') || (*realargs[0] == 'I')) val = 2;
1186         else
1187         {
1188                 res = sscanf(realargs[0], "%d", &val);
1189                 if ((res == 1) && (val < 0))
1190                         res = -1;
1191                 if (val > 2) res = -1;
1192                 if (res != 1) {
1193                         error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
1194                         return -1;
1195                 }
1196         }
1197         exttone = val;
1198         return 0;
1199 }
1200
1201 int cor_thresh(char *keyword, char *args)
1202 {
1203         static char *realargs[10];
1204         int res;
1205         int val;
1206         int x = 0;
1207         res = parseargs(args, realargs, 1, ',');
1208         if (res != 1) {
1209                 error("Incorrect number of arguments to 'corthresh' (should be <value>)\n");
1210                 return -1;
1211         }
1212         res = sscanf(realargs[0], "%d", &val);
1213         if ((res == 1) && (val < 1))
1214                 res = -1;
1215         for(x = 0; corthreshes[x]; x++)
1216         {
1217                 if (corthreshes[x] == val) break;
1218         }
1219         if (!corthreshes[x]) res = -1;
1220         if (res != 1) {
1221                 error("Invalid value '%s', should be a number > 0.\n", realargs[0]);
1222                 return -1;
1223         }
1224         corthresh = x + 1;
1225         return 0;
1226 }
1227
1228 static int rad_chanconfig(char *keyword, char *args)
1229 {
1230         int chans[DAHDI_MAX_CHANNELS];
1231         int res = 0;
1232         int x,i,n;
1233         struct dahdi_radio_param p;
1234         int chanfd;
1235
1236         toneindex = 1;
1237         bzero(chans, sizeof(chans));
1238         res = apply_channels(chans, args);
1239         if (res <= 0)
1240                 return -1;
1241         for (x=1;x<DAHDI_MAX_CHANNELS;x++) {
1242                 if (chans[x]) {
1243                         const char *CHANNEL_FILENAME = "/dev/dahdi/channel";
1244                         chanfd = open(CHANNEL_FILENAME, O_RDWR);
1245                         if (-1 == chanfd) {
1246                                 error("Failed to open '%s'.\n", CHANNEL_FILENAME);
1247                                 exit(-1);
1248                         }
1249
1250                         res = ioctl(chanfd, DAHDI_SPECIFY, &x);
1251                         if (res) {
1252                                 error("Failed to open channel %d.\n", x);
1253                                 close(chanfd);
1254                                 continue;
1255                         }
1256                         p.radpar = DAHDI_RADPAR_NUMTONES;
1257                         if (ioctl(chanfd,DAHDI_RADIO_GETPARAM,&p) == -1)
1258                                 n = 0;
1259                         else
1260                                 n = p.data;
1261
1262                         if (n)
1263                         {
1264                                 p.radpar = DAHDI_RADPAR_INITTONE;
1265                                 if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1) {
1266                                         error("Cannot init tones for channel %d\n",x);
1267                                 }
1268                                 if (!rxtones[0]) for(i = 1; i <= n; i++)
1269                                 {
1270                                         if (rxtones[i])
1271                                         {
1272                                                 p.radpar = DAHDI_RADPAR_RXTONE;
1273                                                 p.index = i;
1274                                                 p.data = rxtones[i];
1275                                                 if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1276                                                         error("Cannot set rxtone on channel %d\n",x);
1277                                         }
1278                                         if (rxtags[i])
1279                                         {
1280                                                 p.radpar = DAHDI_RADPAR_RXTONECLASS;
1281                                                 p.index = i;
1282                                                 p.data = rxtags[i];
1283                                                 if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1284                                                         error("Cannot set rxtag on channel %d\n",x);
1285                                         }
1286                                         if (txtones[i])
1287                                         {
1288                                                 p.radpar = DAHDI_RADPAR_TXTONE;
1289                                                 p.index = i;
1290                                                 p.data = txtones[i];
1291                                                 if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1292                                                         error("Cannot set txtone on channel %d\n",x);
1293                                         }
1294                                 } else { /* if we may have DCS receive */
1295                                         if (rxtones[0])
1296                                         {
1297                                                 p.radpar = DAHDI_RADPAR_RXTONE;
1298                                                 p.index = 0;
1299                                                 p.data = rxtones[0];
1300                                                 if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1301                                                         error("Cannot set DCS rxtone on channel %d\n",x);
1302                                         }
1303                                 }
1304                                 if (txtones[0])
1305                                 {
1306                                         p.radpar = DAHDI_RADPAR_TXTONE;
1307                                         p.index = 0;
1308                                         p.data = txtones[0];
1309                                         if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1310                                                 error("Cannot set default txtone on channel %d\n",x);
1311                                 }
1312                         }
1313                         if (debouncetime)
1314                         {
1315                                 p.radpar = DAHDI_RADPAR_DEBOUNCETIME;
1316                                 p.data = debouncetime;
1317                                 if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1318                                         error("Cannot set debouncetime on channel %d\n",x);
1319                         }
1320                         if (bursttime)
1321                         {
1322                                 p.radpar = DAHDI_RADPAR_BURSTTIME;
1323                                 p.data = bursttime;
1324                                 if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1325                                         error("Cannot set bursttime on channel %d\n",x);
1326                         }
1327                         p.radpar = DAHDI_RADPAR_DEEMP;
1328                         p.data = deemp;
1329                         ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p);
1330                         p.radpar = DAHDI_RADPAR_PREEMP;
1331                         p.data = preemp;
1332                         ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p);
1333                         p.radpar = DAHDI_RADPAR_TXGAIN;
1334                         p.data = txgain;
1335                         ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p);
1336                         p.radpar = DAHDI_RADPAR_RXGAIN;
1337                         p.data = rxgain;
1338                         ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p);
1339                         p.radpar = DAHDI_RADPAR_INVERTCOR;
1340                         p.data = invertcor;
1341                         ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p);
1342                         p.radpar = DAHDI_RADPAR_EXTRXTONE;
1343                         p.data = exttone;
1344                         ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p);
1345                         if (corthresh)
1346                         {
1347                                 p.radpar = DAHDI_RADPAR_CORTHRESH;
1348                                 p.data = corthresh - 1;
1349                                 if (ioctl(chanfd,DAHDI_RADIO_SETPARAM,&p) == -1)
1350                                         error("Cannot set corthresh on channel %d\n",x);
1351                         }
1352
1353                         close(chanfd);
1354                 }
1355         }
1356         clear_fields();
1357         return 0;
1358 }
1359
1360 /* End Radio functions */
1361
1362 static void printconfig(int fd)
1363 {
1364         int x,y;
1365         int ps;
1366         int configs=0;
1367         struct dahdi_versioninfo vi;
1368
1369         strcpy(vi.version, "Unknown");
1370         strcpy(vi.echo_canceller, "Unknown");
1371
1372         if (ioctl(fd, DAHDI_GETVERSION, &vi))
1373                 error("Unable to read DAHDI version information.\n");
1374
1375         printf("\nDAHDI Version: %s\n"
1376                "Echo Canceller(s): %s\n"
1377                "Configuration\n"
1378                "======================\n\n", vi.version, vi.echo_canceller);
1379         for (x = 0; x < spans; x++) {
1380                 if (only_span && only_span != lc[x].span)
1381                         continue;
1382                 printf("SPAN %d: %3s/%4s Build-out: %s\n",
1383                        lc[x].span,
1384                        (lc[x].lineconfig & DAHDI_CONFIG_D4 ? "D4" :
1385                         lc[x].lineconfig & DAHDI_CONFIG_ESF ? "ESF" :
1386                         lc[x].lineconfig & DAHDI_CONFIG_CCS ? "CCS" : "CAS"),
1387                        (lc[x].lineconfig & DAHDI_CONFIG_AMI ? "AMI" :
1388                         lc[x].lineconfig & DAHDI_CONFIG_B8ZS ? "B8ZS" :
1389                         lc[x].lineconfig & DAHDI_CONFIG_HDB3 ? "HDB3" : "???"),
1390                        lbostr[lc[x].lbo]);
1391         }
1392         for (x=0;x<numdynamic;x++) {
1393                 printf("Dynamic span %d: driver %s, addr %s, channels %d, timing %d\n",
1394                        x +1, zds[x].driver, zds[x].addr, zds[x].numchans, zds[x].timing);
1395         }
1396         if (verbose > 1) {
1397                 printf("\nChannel map:\n\n");
1398                 for (x=1;x<DAHDI_MAX_CHANNELS;x++) {
1399                         if (skip_channel(x))
1400                                 continue;
1401                         if ((cc[x].sigtype != DAHDI_SIG_SLAVE) && (cc[x].sigtype)) {
1402                                 configs++;
1403                                 ps = 0;
1404                                 if ((cc[x].sigtype & __DAHDI_SIG_DACS) == __DAHDI_SIG_DACS)
1405                                         printf("Channel %02d %s to %02d", x, sig[x], cc[x].idlebits);
1406                                 else {
1407                                         printf("Channel %02d: %s (%s)", x, sig[x], laws[cc[x].deflaw]);
1408                                         printf(" (Echo Canceler: %s)", ae[x].echocan[0] ? ae[x].echocan : "none");
1409                                         for (y=1;y<DAHDI_MAX_CHANNELS;y++) {
1410                                                 if (cc[y].master == x)  {
1411                                                         printf("%s%02d", ps++ ? " " : " (Slaves: ", y);
1412                                                 }
1413                                         }
1414                                 }
1415                                 if (ps)
1416                                         printf(")\n");
1417                                 else
1418                                         printf("\n");
1419                         } else
1420                                 if (cc[x].sigtype) configs++;
1421                 }
1422         } else {
1423                 for (x=1;x<DAHDI_MAX_CHANNELS;x++) {
1424                         if (skip_channel(x))
1425                                 continue;
1426                         if (cc[x].sigtype)
1427                                 configs++;
1428                 }
1429         }
1430         printf("\n%d channels to configure.\n\n", configs);
1431 }
1432
1433 static struct handler {
1434         char *keyword;
1435         int (*func)(char *keyword, char *args);
1436 } handlers[] = {
1437         { "span", spanconfig },
1438         { "dynamic", dspanconfig },
1439         { "loadzone", registerzone },
1440         { "defaultzone", defaultzone },
1441         { "e&m", chanconfig },
1442         { "e&me1", chanconfig },
1443         { "fxsls", chanconfig },
1444         { "fxsgs", chanconfig },
1445         { "fxsks", chanconfig },
1446         { "fxols", chanconfig },
1447         { "fxogs", chanconfig },
1448         { "fxoks", chanconfig },
1449         { "rawhdlc", chanconfig },
1450         { "nethdlc", chanconfig },
1451         { "fcshdlc", chanconfig },
1452         { "hardhdlc", chanconfig },
1453         { "mtp2", chanconfig },
1454         { "dchan", chanconfig },
1455         { "bchan", chanconfig },
1456         { "indclear", chanconfig },
1457         { "clear", chanconfig },
1458         { "unused", chanconfig },
1459         { "cas", chanconfig },
1460         { "dacs", chanconfig },
1461         { "dacsrbs", chanconfig },
1462         { "user", chanconfig },
1463         { "alaw", setlaw },
1464         { "mulaw", setlaw },
1465         { "deflaw", setlaw },
1466         { "ctcss", ctcss },
1467         { "dcsrx", dcsrx },
1468         { "rxdcs", dcsrx },
1469         { "tx", tx },
1470         { "debouncetime", debounce_time },
1471         { "bursttime", burst_time },
1472         { "exttone", ext_tone },
1473         { "invertcor", invert_cor },
1474         { "corthresh", cor_thresh },
1475         { "rxgain", rx_gain },
1476         { "txgain", tx_gain },
1477         { "deemp", de_emp },
1478         { "preemp", pre_emp },
1479         { "channel", rad_chanconfig },
1480         { "channels", rad_chanconfig },
1481         { "echocanceller", setechocan },
1482         { "56k", setfiftysixkhdlc },
1483 };
1484
1485 static char *readline()
1486 {
1487         static char buf[256];
1488         char *c;
1489         do {
1490                 if (!fgets(buf, sizeof(buf), cf)) 
1491                         return NULL;
1492                 /* Strip comments */
1493                 c = strchr(buf, '#');
1494                 if (c)
1495                         *c = '\0';
1496                 trim(buf);
1497                 lineno++;
1498         } while (!strlen(buf));
1499         return buf;
1500 }
1501
1502 static void usage(char *argv0, int exitcode)
1503 {
1504         char *c;
1505         c = strrchr(argv0, '/');
1506         if (!c)
1507                 c = argv0;
1508         else
1509                 c++;
1510         fprintf(stderr, "%s\n", dahdi_tools_version);
1511         fprintf(stderr, 
1512                 "Usage: %s [options]\n"
1513                 "    Valid options are:\n"
1514                 "  -c <filename>     -- Use <filename> instead of " CONFIG_FILENAME "\n"
1515                 "  -d [level]        -- Generate debugging output. (Default level is 1.)\n"
1516                 "  -f                -- Always reconfigure every channel\n"
1517                 "  -h                -- Generate this help statement\n"
1518                 "  -s                -- Shutdown spans only\n"
1519                 "  -t                -- Test mode only, do not apply\n"
1520                 "  -C <chan_list>    -- Only configure specified channels\n"
1521                 "  -S <spanno>       -- Only configure specified span\n"
1522                 "  -v                -- Verbose (more -v's means more verbose)\n"
1523         ,c);
1524         exit(exitcode);
1525 }
1526
1527 static int chan_restrict(char *str)
1528 {
1529         if (apply_channels(selected_channels, str) < 0)
1530                 return 0;
1531         restrict_channels = 1;
1532         return 1;
1533 }
1534
1535 static int span_restrict(char *str)
1536 {
1537         long    spanno;
1538         char    *endptr;
1539
1540         spanno = strtol(str, &endptr, 10);
1541         if (endptr == str) {
1542                 fprintf(stderr, "Missing valid span number after '-S'\n");
1543                 return 0;
1544         }
1545         if (*endptr != '\0') {
1546                 fprintf(stderr, "Extra garbage after span number in '-S'\n");
1547                 return 0;
1548         }
1549         only_span = spanno;
1550         return 1;
1551 }
1552
1553 static const char *SEM_NAME = "dahdi_cfg";
1554 static sem_t *lock = SEM_FAILED;
1555
1556 static void signal_handler(int signal)
1557 {
1558         if (SEM_FAILED != lock) {
1559                 sem_unlink(SEM_NAME);
1560         }
1561         /* The default handler should have been restored before this handler was
1562          * called, so we can let the "normal" processing finish the cleanup. */
1563         raise(signal);
1564 }
1565
1566 int main(int argc, char *argv[])
1567 {
1568         int c;
1569         char *buf;
1570         char *key, *value;
1571         int x,found;
1572         int exit_code = 0;
1573         struct sigaction act;
1574
1575         while((c = getopt(argc, argv, "fthc:vsd::C:S:")) != -1) {
1576                 switch(c) {
1577                 case 'c':
1578                         filename=optarg;
1579                         break;
1580                 case 'h':
1581                         usage(argv[0], 0);
1582                         break;
1583                 case '?':
1584                         usage(argv[0], 1);
1585                         break;
1586                 case 'v':
1587                         verbose++;
1588                         break;
1589                 case 'f':
1590                         force++;
1591                         break;
1592                 case 't':
1593                         dry_run = 1;
1594                         break;
1595                 case 's':
1596                         stopmode = 1;
1597                         break;
1598                 case 'C':
1599                         if (!chan_restrict(optarg))
1600                                 usage(argv[0], 1);
1601                         break;
1602                 case 'S':
1603                         if (!span_restrict(optarg))
1604                                 usage(argv[0], 1);
1605                         break;
1606                 case 'd':
1607                         if (optarg)
1608                                 debug = atoi(optarg);
1609                         else
1610                                 debug = 1;      
1611                         break;
1612                 }
1613         }
1614         
1615         if (verbose) {
1616                 fprintf(stderr, "%s\n", dahdi_tools_version);
1617         }
1618
1619         if (!restrict_channels && !only_span) {
1620                 bool all_assigned = wait_for_all_spans_assigned(5);
1621
1622                 if (!all_assigned) {
1623                         fprintf(stderr,
1624                                 "Timeout waiting for all spans to be assigned.\n");
1625                 }
1626         }
1627
1628         if (fd == -1) fd = open(MASTER_DEVICE, O_RDWR);
1629         if (fd < 0) {
1630                 error("Unable to open master device '%s'\n", MASTER_DEVICE);
1631                 goto finish;
1632         }
1633         if (strcmp(filename, "-") == 0)
1634                 cf = fdopen(STDIN_FILENO, "r");
1635         else
1636                 cf = fopen(filename, "r");
1637         if (cf) {
1638                 while((buf = readline())) {
1639                         if (*buf == 10) /* skip new line */
1640                                 continue;
1641
1642                         if (debug & DEBUG_READER) 
1643                                 fprintf(stderr, "Line %d: %s\n", lineno, buf);
1644
1645                         if ((value = strchr(buf, '='))) {
1646                                 *value++ = '\0';
1647                                 value = trim(value);
1648                                 key = trim(buf);
1649                         }
1650
1651                         if (!value || !*value || !*key) {
1652                                 error("Syntax error. Should be <keyword>=<value>\n");
1653                                 continue;
1654                         }
1655
1656                         if (debug & DEBUG_PARSER)
1657                                 fprintf(stderr, "Keyword: [%s], Value: [%s]\n", key, value);
1658
1659                         found = 0;
1660                         for (x = 0; x < sizeof(handlers) / sizeof(handlers[0]); x++) {
1661                                 if (!strcasecmp(key, handlers[x].keyword)) {
1662                                         found++;
1663                                         handlers[x].func(key, value);
1664                                         break;
1665                                 }
1666                         }
1667
1668                         if (!found) 
1669                                 error("Unknown keyword '%s'\n", key);
1670                 }
1671                 if (debug & DEBUG_READER)
1672                         fprintf(stderr, "<End of File>\n");
1673                 /* fclose(cf); // causes seg fault (double free) */
1674         } else {
1675                 error("Unable to open configuration file '%s'\n", filename);
1676         }
1677
1678 finish:
1679         if (errcnt) {
1680                 fprintf(stderr, "\n%d error(s) detected\n\n", errcnt);
1681                 exit(1);
1682         }
1683         if (verbose) {
1684                 printconfig(fd);
1685         }
1686
1687         if (dry_run)
1688                 exit(0);
1689         
1690         if (debug & DEBUG_APPLY) {
1691                 printf("About to open Master device\n");
1692                 fflush(stdout);
1693         }
1694
1695         sigemptyset(&act.sa_mask);
1696         act.sa_handler = signal_handler;
1697         act.sa_flags = SA_RESETHAND;
1698
1699         if (sigaction(SIGTERM, &act, NULL) == -1) {
1700                 perror("Failed to install SIGTERM handler.");
1701                 exit(1);
1702         }
1703         if (sigaction(SIGINT, &act, NULL) == -1) {
1704                 perror("Failed to install SIGINT handler.");
1705                 exit(1);
1706         }
1707
1708         lock = sem_open(SEM_NAME, O_CREAT, O_RDWR, 1);
1709         if (SEM_FAILED == lock) {
1710                 perror("Unable to create 'dahdi_cfg' mutex");
1711                 exit_code = 1;
1712                 goto release_sem;
1713         }
1714
1715         if (-1 == sem_wait(lock)) {
1716                 perror("Failed to wait for 'dahdi_cfg' mutex");
1717                 exit_code = 1;
1718                 goto unlink_sem;
1719         }
1720
1721         if (!restrict_channels && !only_span) {
1722                 for (x=0;x<numdynamic;x++) {
1723                         /* destroy them all */
1724                         ioctl(fd, DAHDI_DYNAMIC_DESTROY, &zds[x]);
1725                 }
1726         }
1727
1728         if (stopmode) {
1729                 for (x=0;x<spans;x++) {
1730                         if (only_span && lc[x].span != only_span)
1731                                 continue;
1732                         if (ioctl(fd, DAHDI_SHUTDOWN, &lc[x].span)) {
1733                                 fprintf(stderr, "DAHDI shutdown failed: %s\n", strerror(errno));
1734                                 close(fd);
1735                                 exit_code = 1;
1736                                 goto release_sem;
1737                         }
1738                 }
1739                 exit_code = 1;
1740                 goto release_sem;
1741         }
1742         for (x=0;x<spans;x++) {
1743                 if (only_span && lc[x].span != only_span)
1744                         continue;
1745                 if (ioctl(fd, DAHDI_SPANCONFIG, lc + x)) {
1746                         fprintf(stderr, "DAHDI_SPANCONFIG failed on span %d: %s (%d)\n", lc[x].span, strerror(errno), errno);
1747                         close(fd);
1748                         exit_code = 1;
1749                         goto release_sem;
1750                 }
1751         }
1752
1753         if (!restrict_channels && !only_span) {
1754
1755                 sem_post(lock);
1756
1757                 for (x=0;x<numdynamic;x++) {
1758                         if (ioctl(fd, DAHDI_DYNAMIC_CREATE, &zds[x])) {
1759                                 fprintf(stderr, "DAHDI dynamic span creation failed: %s\n", strerror(errno));
1760                                 close(fd);
1761                                 exit_code = 1;
1762                                 goto release_sem;
1763                         }
1764                         wait_for_all_spans_assigned(1);
1765                 }
1766
1767                 if (-1 == sem_wait(lock)) {
1768                         perror("Failed to wait for 'dahdi_cfg' mutex after creating dynamic spans");
1769                         exit_code = 1;
1770                         goto unlink_sem;
1771                 }
1772         }
1773
1774         for (x=1;x<DAHDI_MAX_CHANNELS;x++) {
1775                 struct dahdi_params current_state;
1776                 int master;
1777                 int needupdate = force;
1778
1779                 if (skip_channel(x)) {
1780                         if (debug & DEBUG_APPLY) {
1781                                 printf("Skip device %d\n", x);
1782                                 fflush(stdout);
1783                         }
1784                         continue;
1785                 }
1786                 if (debug & DEBUG_APPLY) {
1787                         printf("Configuring device %d\n", x);
1788                         fflush(stdout);
1789                 }
1790                 if (!cc[x].sigtype)
1791                         continue;
1792                 
1793                 if (!needupdate) {
1794                         memset(&current_state, 0, sizeof(current_state));
1795                         current_state.channo = cc[x].chan | DAHDI_GET_PARAMS_RETURN_MASTER;
1796                         if (ioctl(fd, DAHDI_GET_PARAMS, &current_state))
1797                                 needupdate = 1;
1798                 }
1799                 
1800                 if (!needupdate) {
1801                         master = current_state.channo >> 16;
1802                         
1803                         if (cc[x].sigtype != current_state.sigtype) {
1804                                 needupdate++;
1805                                 if (verbose > 1)
1806                                         printf("Changing signalling on channel %d from %s to %s\n",
1807                                                cc[x].chan, sigtype_to_str(current_state.sigtype),
1808                                                sigtype_to_str(cc[x].sigtype));
1809                         }
1810                         
1811                         if ((cc[x].deflaw != DAHDI_LAW_DEFAULT) && (cc[x].deflaw != current_state.curlaw)) {
1812                                 needupdate++;
1813                                 if (verbose > 1)
1814                                         printf("Changing law on channel %d from %s to %s\n",
1815                                                cc[x].chan, laws[current_state.curlaw],
1816                                                laws[cc[x].deflaw]);
1817                         }
1818                         
1819                         if (cc[x].master != master) {
1820                                 needupdate++;
1821                                 if (verbose > 1)
1822                                         printf("Changing master of channel %d from %d to %d\n",
1823                                                cc[x].chan, master,
1824                                                cc[x].master);
1825                         }
1826                         
1827                         if (cc[x].idlebits != current_state.idlebits) {
1828                                 needupdate++;
1829                                 if (verbose > 1)
1830                                         printf("Changing idle bits of channel %d from %d to %d\n",
1831                                                cc[x].chan, current_state.idlebits,
1832                                                cc[x].idlebits);
1833                         }
1834                 }
1835                 
1836                 if (needupdate && ioctl(fd, DAHDI_CHANCONFIG, &cc[x])) {
1837                         fprintf(stderr, "DAHDI_CHANCONFIG failed on channel %d: %s (%d)\n", x, strerror(errno), errno);
1838                         if (errno == EINVAL) {
1839                                 /* give helpful suggestions on signaling errors */
1840                                 fprintf(stderr, "Selected signaling not "
1841                                                 "supported\n");
1842                                 fprintf(stderr, "Possible causes:\n");
1843                                 switch(cc[x].sigtype) {
1844                                 case DAHDI_SIG_FXOKS:
1845                                 case DAHDI_SIG_FXOLS:
1846                                 case DAHDI_SIG_FXOGS:
1847                                         fprintf(stderr, "\tFXO signaling is "
1848                                                 "being used on a FXO interface"
1849                                                 " (use a FXS signaling variant"
1850                                                 ")\n");
1851                                         fprintf(stderr, "\tRBS signaling is "
1852                                                 "being used on a E1 CCS span"
1853                                                 "\n");
1854                                         break;
1855                                 case DAHDI_SIG_FXSKS:
1856                                 case DAHDI_SIG_FXSLS:
1857                                 case DAHDI_SIG_FXSGS:
1858                                         fprintf(stderr, "\tFXS signaling is "
1859                                                 "being used on a FXS interface"
1860                                                 " (use a FXO signaling variant"
1861                                                 ")\n");
1862                                         fprintf(stderr, "\tRBS signaling is "
1863                                                 "being used on a E1 CCS span"
1864                                                 "\n");
1865                                         break;
1866                                 case DAHDI_SIG_EM:
1867                                         fprintf(stderr, "\te&m signaling is "
1868                                                 "being used on a E1 line (use"
1869                                                 " e&me1)\n");
1870                                         break;
1871                                 case DAHDI_SIG_EM_E1:
1872                                         fprintf(stderr, "\te&me1 signaling is "
1873                                                 "being used on a T1 line (use "
1874                                                 "e&m)\n");
1875                                         fprintf(stderr, "\tRBS signaling is "
1876                                                 "being used on a E1 CCS span"
1877                                                 "\n");
1878                                         break;
1879                                 case DAHDI_SIG_HARDHDLC:
1880                                         fprintf(stderr, "\thardhdlc is being "
1881                                                 "used on a TE12x (use dchan)\n"
1882                                                 );
1883                                         break;
1884                                 case DAHDI_SIG_HDLCFCS:
1885                                         fprintf(stderr, "\tdchan is being used"
1886                                                 " on a BRI span (use hardhdlc)"
1887                                                 "\n");
1888                                         break;
1889                                 default:
1890                                         break;
1891                                 }
1892                                 fprintf(stderr, "\tSignaling is being assigned"
1893                                         " to channel 16 of an E1 CAS span\n");
1894                         }
1895                         close(fd);
1896                         exit_code = 1;
1897                         goto release_sem;
1898                 }
1899
1900                 ae[x].chan = x;
1901                 if (verbose) {
1902                         printf("Setting echocan for channel %d to %s\n", ae[x].chan, ae[x].echocan[0] ? ae[x].echocan : "none");
1903                 }
1904
1905                 if (ioctl(fd, DAHDI_ATTACH_ECHOCAN, &ae[x])) {
1906                         fprintf(stderr, "DAHDI_ATTACH_ECHOCAN failed on channel %d: %s (%d)\n", x, strerror(errno), errno);
1907                         close(fd);
1908                         exit_code = 1;
1909                         goto release_sem;
1910                 }
1911         }
1912         if (0 == numzones) {
1913                 /* Default to the us zone if one wasn't specified. */
1914                 dahdi_copy_string(zonestoload[numzones++], "us", sizeof(zonestoload[0]));
1915                 deftonezone = 0;
1916         }
1917
1918         for (x=0;x<numzones;x++) {
1919                 if (debug & DEBUG_APPLY) {
1920                         printf("Loading tone zone for %s\n", zonestoload[x]);
1921                         fflush(stdout);
1922                 }
1923                 if (tone_zone_register(fd, zonestoload[x])) {
1924                         if (errno != EBUSY)
1925                                 error("Unable to register tone zone '%s'\n", zonestoload[x]);
1926                 }
1927         }
1928         if (debug & DEBUG_APPLY) {
1929                 printf("Doing startup\n");
1930                 fflush(stdout);
1931         }
1932         if (deftonezone > -1) {
1933                 if (ioctl(fd, DAHDI_DEFAULTZONE, &deftonezone)) {
1934                         fprintf(stderr, "DAHDI_DEFAULTZONE failed: %s (%d)\n", strerror(errno), errno);
1935                         close(fd);
1936                         exit_code = 1;
1937                         goto release_sem;
1938                 }
1939         }
1940         for (x=0;x<spans;x++) {
1941                 if (only_span && lc[x].span != only_span)
1942                         continue;
1943                 if (ioctl(fd, DAHDI_STARTUP, &lc[x].span)) {
1944                         fprintf(stderr, "DAHDI startup failed: %s\n", strerror(errno));
1945                         close(fd);
1946                         exit_code = 1;
1947                         goto release_sem;
1948                 }
1949         }
1950         exit_code = apply_fiftysix();
1951
1952 release_sem:
1953         if (SEM_FAILED != lock)
1954                 sem_post(lock);
1955
1956 unlink_sem:
1957         if (SEM_FAILED != lock)
1958                 sem_unlink(SEM_NAME);
1959         exit(exit_code);
1960 }