Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / i4b / isdntelctl / main.c
1 /*
2  * Copyright (c) 1997, 1999 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      isdntelctl - i4b set telephone interface options
28  *      ------------------------------------------------
29  *
30  *      $Id: main.c,v 1.12 1999/12/13 21:25:26 hm Exp $
31  *
32  * $FreeBSD: src/usr.sbin/i4b/isdntelctl/main.c,v 1.8.2.2 2001/08/01 17:45:07 obrien Exp $
33  *
34  *      last edit-date: [Mon Dec 13 21:54:50 1999]
35  *
36  *---------------------------------------------------------------------------*/
37
38 #include <stdio.h>
39 #include <signal.h>
40 #include <errno.h>
41 #include <paths.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <ctype.h>
47 #include <sys/stat.h>
48 #include <sys/wait.h>
49 #include <sys/ioctl.h>
50 #include <sys/types.h>
51 #include <sys/time.h>
52
53 #include <machine/i4b_ioctl.h>
54 #include <machine/i4b_tel_ioctl.h>
55
56 static void usage ( void );
57
58 #define I4BTELDEVICE    "/dev/i4btel"
59
60 int opt_get = 0;
61 int opt_unit = 0;
62 int opt_U = 0;
63 int opt_A = 0;
64 int opt_C = 0;
65 int opt_N = 0;
66
67 /*---------------------------------------------------------------------------*
68  *      program entry
69  *---------------------------------------------------------------------------*/
70 int
71 main(int argc, char **argv)
72 {
73         int c;
74         int ret;
75         int telfd;
76         char namebuffer[128];
77         
78         while ((c = getopt(argc, argv, "cgu:AUN")) != -1)
79         {
80                 switch(c)
81                 {
82                         case 'c':
83                                 opt_C = 1;
84                                 break;
85
86                         case 'g':
87                                 opt_get = 1;
88                                 break;
89
90                         case 'u':
91                                 opt_unit = atoi(optarg);
92                                 if(opt_unit < 0 || opt_unit > 9)
93                                         usage();
94                                 break;
95
96                         case 'A':
97                                 opt_A = 1;
98                                 break;
99
100                         case 'U':
101                                 opt_U = 1;
102                                 break;
103
104                         case 'N':
105                                 opt_N = 1;
106                                 break;
107
108                         case '?':
109                         default:
110                                 usage();
111                                 break;
112                 }
113         }
114
115         if(opt_get == 0 && opt_N == 0 && opt_U == 0 && opt_A == 0 && opt_C == 0)
116         {
117                 opt_get = 1;
118         }
119
120         if((opt_get + opt_N + opt_U + opt_A + opt_C) > 1)
121         {
122                 usage();
123         }
124
125         sprintf(namebuffer,"%s%d", I4BTELDEVICE, opt_unit);
126         
127         if((telfd = open(namebuffer, O_RDWR)) < 0)
128         {
129                 fprintf(stderr, "isdntelctl: cannot open %s: %s\n", namebuffer, strerror(errno));
130                 exit(1);
131         }
132
133         if(opt_get)
134         {
135                 int format;
136                 
137                 if((ret = ioctl(telfd, I4B_TEL_GETAUDIOFMT, &format)) < 0)
138                 {
139                         fprintf(stderr, "ioctl I4B_TEL_GETAUDIOFMT failed: %s", strerror(errno));
140                         exit(1);
141                 }
142
143                 if(format == CVT_NONE)
144                 {
145                         printf("device %s does not do A-law/u-law format conversion\n", namebuffer);
146                 }
147                 else if(format == CVT_ALAW2ULAW)
148                 {
149                         printf("device %s does ISDN: A-law -> user: u-law format conversion\n", namebuffer);
150                 }
151                 else if(format == CVT_ULAW2ALAW)
152                 {
153                         printf("device %s does ISDN: u-law -> user: A-law format conversion\n", namebuffer);
154                 }
155                 else
156                 {
157                         printf("ERROR, device %s uses unknown format %d!\n", namebuffer, format);
158                 }
159                 exit(0);
160         }
161
162         if(opt_A)
163         {
164                 int format = CVT_ALAW2ULAW;
165                 
166                 if((ret = ioctl(telfd, I4B_TEL_SETAUDIOFMT, &format)) < 0)
167                 {
168                         fprintf(stderr, "ioctl I4B_TEL_SETAUDIOFMT failed: %s", strerror(errno));
169                         exit(1);
170                 }
171                 exit(0);
172         }
173
174         if(opt_U)
175         {
176                 int format = CVT_ULAW2ALAW;
177                 
178                 if((ret = ioctl(telfd, I4B_TEL_SETAUDIOFMT, &format)) < 0)
179                 {
180                         fprintf(stderr, "ioctl I4B_TEL_SETAUDIOFMT failed: %s", strerror(errno));
181                         exit(1);
182                 }
183                 exit(0);
184         }
185         if(opt_N)
186         {
187                 int format = CVT_NONE;
188                 
189                 if((ret = ioctl(telfd, I4B_TEL_SETAUDIOFMT, &format)) < 0)
190                 {
191                         fprintf(stderr, "ioctl I4B_TEL_SETAUDIOFMT failed: %s", strerror(errno));
192                         exit(1);
193                 }
194                 exit(0);
195         }
196         if(opt_C)
197         {
198                 int dummy;
199                 if((ret = ioctl(telfd, I4B_TEL_EMPTYINPUTQUEUE, &dummy)) < 0)
200                 {
201                         fprintf(stderr, "ioctl I4B_TEL_EMPTYINPUTQUEUE failed: %s", strerror(errno));
202                         exit(1);
203                 }
204                 exit(0);
205         }
206         return(0);
207 }
208         
209 /*---------------------------------------------------------------------------*
210  *      usage display and exit
211  *---------------------------------------------------------------------------*/
212 static void
213 usage(void)
214 {
215         fprintf(stderr, "\n");
216         fprintf(stderr, "isdntelctl - %si4btel control, version %d.%d.%d (%s %s)\n", _PATH_DEV, VERSION, REL, STEP, __DATE__, __TIME__);
217         fprintf(stderr, "usage: isdntelctl -c -g -u <unit> -A -N -U\n");
218         fprintf(stderr, "       -c            clear input queue\n");
219         fprintf(stderr, "       -g            get current settings\n");
220         fprintf(stderr, "       -u unit       specify unit number\n");  
221         fprintf(stderr, "       -A            set conversion ISDN: A-law -> user: u-law\n");
222         fprintf(stderr, "       -U            set conversion ISDN: u-law -> user: A-law\n");
223         fprintf(stderr, "       -N            set conversion to no A-law/u-law conversion\n");
224         fprintf(stderr, "\n");
225         exit(1);
226 }
227
228 /* EOF */