Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / i4b / isdnphone / isdn.c
1 /*
2  * Copyright (c) 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  *      isdnphone - isdn (i4b) handling
28  *      ===============================
29  *
30  *      $Id: isdn.c,v 1.4 1999/12/13 21:25:26 hm Exp $
31  *
32  * $FreeBSD: src/usr.sbin/i4b/isdnphone/isdn.c,v 1.1.2.1 2001/08/01 17:45:06 obrien Exp $
33  *
34  *      last edit-date: [Mon Dec 13 21:53:05 1999]
35  *
36  *---------------------------------------------------------------------------*/
37
38 #include "defs.h"
39
40 /*---------------------------------------------------------------------------*
41  *      dialer init
42  *---------------------------------------------------------------------------*/
43 int
44 init_dial(char *device)
45 {
46         int ret;
47         
48         if((ret = open(device, O_RDWR)) < 0)
49         {
50                 fprintf(stderr, "unable to open %s: %s\n", device, strerror(errno));
51                 return(-1);
52         }
53         return(ret);
54 }
55
56 /*---------------------------------------------------------------------------*
57  *      i4bteld data available handler
58  *---------------------------------------------------------------------------*/
59 void
60 dial_hdlr(void)
61 {
62         char result;
63
64         if((read (dialerfd, &result, 1) < 0))
65         {
66                 fatal("read failed: %s", strerror(errno));
67         }
68
69         switch(result)
70         {
71                 case RSP_CONN:
72                         newstate(ST_ACTIVE);
73                         message("connected to remote!");
74                         break;
75                         
76                 case RSP_BUSY:
77                         message("remote is busy!");
78                         break;
79
80                 case RSP_HUP:
81                         newstate(ST_IDLE);              
82                         message("disconnected from remote!");
83                         break;
84
85                 case RSP_NOA:
86                         message("no answer from remote!");
87                         break;
88
89                 default:
90                         message("unknown response = 0x%2x!", result);
91                         break;
92         }
93 }
94
95 /*---------------------------------------------------------------------------*
96  *      telephone init
97  *---------------------------------------------------------------------------*/
98 int
99 init_tel(char *device)
100 {
101         int ret;
102         int format;
103
104         if(play_fmt == AFMT_MU_LAW)
105                 format = CVT_ALAW2ULAW;
106         else
107                 format = CVT_NONE;              
108         
109         if((ret = open(device, O_RDWR)) < 0)
110                 fatal("unable to open %s: %s\n", device, strerror(errno));
111
112         if((ioctl(ret, I4B_TEL_SETAUDIOFMT, &format)) < 0)
113                 fatal("ioctl I4B_TEL_SETAUDIOFMT failed: %s", strerror(errno));
114
115         return(ret);
116 }
117                 
118 /*---------------------------------------------------------------------------*
119  *      dial number
120  *---------------------------------------------------------------------------*/
121 void
122 do_dial(char *number)
123 {
124         char commandbuffer[80]; 
125         sprintf(commandbuffer, "D%s", number);
126
127         if((write(dialerfd, commandbuffer, strlen(commandbuffer))) < 0)
128         {
129                 fatal("write commandbuffer failed: %s", strerror(errno));
130         }
131 }
132
133 /*---------------------------------------------------------------------------*
134  *      hangup
135  *---------------------------------------------------------------------------*/
136 void
137 do_hangup(void)
138 {
139         char commandbuffer[80]; 
140
141         if(state == ST_IDLE)
142         {
143                 message("tried hangup while ST_IDLE");
144                 return;
145         }
146         
147         sprintf(commandbuffer, "H");
148
149         if((write(dialerfd, commandbuffer, strlen(commandbuffer))) < 0)
150         {
151                 fatal("write commandbuffer failed: %s", strerror(errno));
152         }
153 }
154
155 /*---------------------------------------------------------------------------*
156  *      i4btel speech data available handler
157  *---------------------------------------------------------------------------*/
158 void
159 tel_hdlr(void)
160 {
161         unsigned char buffer[BCH_MAX_DATALEN];
162         int ret;
163
164         ret = read(telfd, buffer, BCH_MAX_DATALEN);
165
166         if(ret < 0)
167         {
168                 fatal("read telfd failed: %s", strerror(errno));
169         }
170
171         debug("tel_hdlr: read %d bytes\n", ret);
172
173         if(ret > 0)
174         {
175                 audiowrite(ret, buffer);
176         }
177 }
178
179 /*---------------------------------------------------------------------------*
180  *      write audio data to ISDN
181  *---------------------------------------------------------------------------*/
182 void
183 telwrite(int len, unsigned char *buf)
184 {
185         if((write(telfd, buf, len)) < 0)
186         {
187                 fatal("write tel failed: %s", strerror(errno));
188         }
189 }
190
191 /* EOF */