Kernel tree reorganization stage 2: Major cvs repository work.
[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  * $DragonFly: src/usr.sbin/i4b/isdntelctl/main.c,v 1.3 2003/08/08 04:18:45 dillon Exp $
34  *
35  *      last edit-date: [Mon Dec 13 21:54:50 1999]
36  *
37  *---------------------------------------------------------------------------*/
38
39 #include <stdio.h>
40 #include <signal.h>
41 #include <errno.h>
42 #include <paths.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <ctype.h>
48 #include <sys/stat.h>
49 #include <sys/wait.h>
50 #include <sys/ioctl.h>
51 #include <sys/types.h>
52 #include <sys/time.h>
53
54 #include <i4b_machine/i4b_ioctl.h>
55 #include <i4b_machine/i4b_tel_ioctl.h>
56
57 static void usage ( void );
58
59 #define I4BTELDEVICE    "/dev/i4btel"
60
61 int opt_get = 0;
62 int opt_unit = 0;
63 int opt_U = 0;
64 int opt_A = 0;
65 int opt_C = 0;
66 int opt_N = 0;
67
68 /*---------------------------------------------------------------------------*
69  *      program entry
70  *---------------------------------------------------------------------------*/
71 int
72 main(int argc, char **argv)
73 {
74         int c;
75         int ret;
76         int telfd;
77         char namebuffer[128];
78         
79         while ((c = getopt(argc, argv, "cgu:AUN")) != -1)
80         {
81                 switch(c)
82                 {
83                         case 'c':
84                                 opt_C = 1;
85                                 break;
86
87                         case 'g':
88                                 opt_get = 1;
89                                 break;
90
91                         case 'u':
92                                 opt_unit = atoi(optarg);
93                                 if(opt_unit < 0 || opt_unit > 9)
94                                         usage();
95                                 break;
96
97                         case 'A':
98                                 opt_A = 1;
99                                 break;
100
101                         case 'U':
102                                 opt_U = 1;
103                                 break;
104
105                         case 'N':
106                                 opt_N = 1;
107                                 break;
108
109                         case '?':
110                         default:
111                                 usage();
112                                 break;
113                 }
114         }
115
116         if(opt_get == 0 && opt_N == 0 && opt_U == 0 && opt_A == 0 && opt_C == 0)
117         {
118                 opt_get = 1;
119         }
120
121         if((opt_get + opt_N + opt_U + opt_A + opt_C) > 1)
122         {
123                 usage();
124         }
125
126         sprintf(namebuffer,"%s%d", I4BTELDEVICE, opt_unit);
127         
128         if((telfd = open(namebuffer, O_RDWR)) < 0)
129         {
130                 fprintf(stderr, "isdntelctl: cannot open %s: %s\n", namebuffer, strerror(errno));
131                 exit(1);
132         }
133
134         if(opt_get)
135         {
136                 int format;
137                 
138                 if((ret = ioctl(telfd, I4B_TEL_GETAUDIOFMT, &format)) < 0)
139                 {
140                         fprintf(stderr, "ioctl I4B_TEL_GETAUDIOFMT failed: %s", strerror(errno));
141                         exit(1);
142                 }
143
144                 if(format == CVT_NONE)
145                 {
146                         printf("device %s does not do A-law/u-law format conversion\n", namebuffer);
147                 }
148                 else if(format == CVT_ALAW2ULAW)
149                 {
150                         printf("device %s does ISDN: A-law -> user: u-law format conversion\n", namebuffer);
151                 }
152                 else if(format == CVT_ULAW2ALAW)
153                 {
154                         printf("device %s does ISDN: u-law -> user: A-law format conversion\n", namebuffer);
155                 }
156                 else
157                 {
158                         printf("ERROR, device %s uses unknown format %d!\n", namebuffer, format);
159                 }
160                 exit(0);
161         }
162
163         if(opt_A)
164         {
165                 int format = CVT_ALAW2ULAW;
166                 
167                 if((ret = ioctl(telfd, I4B_TEL_SETAUDIOFMT, &format)) < 0)
168                 {
169                         fprintf(stderr, "ioctl I4B_TEL_SETAUDIOFMT failed: %s", strerror(errno));
170                         exit(1);
171                 }
172                 exit(0);
173         }
174
175         if(opt_U)
176         {
177                 int format = CVT_ULAW2ALAW;
178                 
179                 if((ret = ioctl(telfd, I4B_TEL_SETAUDIOFMT, &format)) < 0)
180                 {
181                         fprintf(stderr, "ioctl I4B_TEL_SETAUDIOFMT failed: %s", strerror(errno));
182                         exit(1);
183                 }
184                 exit(0);
185         }
186         if(opt_N)
187         {
188                 int format = CVT_NONE;
189                 
190                 if((ret = ioctl(telfd, I4B_TEL_SETAUDIOFMT, &format)) < 0)
191                 {
192                         fprintf(stderr, "ioctl I4B_TEL_SETAUDIOFMT failed: %s", strerror(errno));
193                         exit(1);
194                 }
195                 exit(0);
196         }
197         if(opt_C)
198         {
199                 int dummy;
200                 if((ret = ioctl(telfd, I4B_TEL_EMPTYINPUTQUEUE, &dummy)) < 0)
201                 {
202                         fprintf(stderr, "ioctl I4B_TEL_EMPTYINPUTQUEUE failed: %s", strerror(errno));
203                         exit(1);
204                 }
205                 exit(0);
206         }
207         return(0);
208 }
209         
210 /*---------------------------------------------------------------------------*
211  *      usage display and exit
212  *---------------------------------------------------------------------------*/
213 static void
214 usage(void)
215 {
216         fprintf(stderr, "\n");
217         fprintf(stderr, "isdntelctl - %si4btel control, version %d.%d.%d (%s %s)\n", _PATH_DEV, VERSION, REL, STEP, __DATE__, __TIME__);
218         fprintf(stderr, "usage: isdntelctl -c -g -u <unit> -A -N -U\n");
219         fprintf(stderr, "       -c            clear input queue\n");
220         fprintf(stderr, "       -g            get current settings\n");
221         fprintf(stderr, "       -u unit       specify unit number\n");  
222         fprintf(stderr, "       -A            set conversion ISDN: A-law -> user: u-law\n");
223         fprintf(stderr, "       -U            set conversion ISDN: u-law -> user: A-law\n");
224         fprintf(stderr, "       -N            set conversion to no A-law/u-law conversion\n");
225         fprintf(stderr, "\n");
226         exit(1);
227 }
228
229 /* EOF */