Initial import from FreeBSD RELENG_4:
[dragonfly.git] / share / examples / isdn / v21 / v21modem.c
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * This is a V.21 modem for ISDN4BSD.
10  *
11  * $FreeBSD: src/share/examples/isdn/v21/v21modem.c,v 1.2.2.1 2001/08/10 14:59:48 obrien Exp $
12  */
13
14 #include <string.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <fcntl.h>
19 #include <err.h>
20 #include <sys/ioccom.h>
21 #include <errno.h>
22 #include <syslog.h>
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <termios.h>
26 #include <libutil.h>
27
28 #include <machine/i4b_tel_ioctl.h>
29
30 static void create_session(void);
31 static void input_byte(int byte, int stopbit);
32 static void sample(int vol, int* tones);
33 static void tonedetect(unsigned char *ptr, int count);
34 static void uart(int bit);
35
36 static int dcd;                 /* Carrier on ? */
37 static int ptyfd = -1;          /* PTY filedescriptor */
38 static int telfd = -1;          /* I4BTEL filedescriptor */
39
40 /*
41  * Alaw to Linear [-32767..32767] conversion
42  */
43
44 static int a2l[256] = { -5504, -5248, -6016, -5760, -4480, -4224,
45 -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040,
46 -6784, -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
47 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, -22016,
48 -20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208,
49 -29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008,
50 -10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592,
51 -16128, -15616, -13056, -12544, -14080, -13568, -344, -328, -376,
52 -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392,
53 -440, -424, -88, -72, -120, -104, -24, -8, -56, -40, -216, -200,
54 -248, -232, -152, -136, -184, -168, -1376, -1312, -1504, -1440,
55 -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632,
56 -1568, -1760, -1696, -688, -656, -752, -720, -560, -528, -624,
57 -592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504, 5248,
58 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528,
59 6272, 7040, 6784, 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
60 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016, 20992, 24064,
61 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232,
62 26112, 25088, 28160, 27136, 11008, 10496, 12032, 11520, 8960, 8448,
63 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
64 344, 328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408,
65 392, 440, 424, 88, 72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232,
66 152, 136, 184, 168, 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
67 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688, 656, 752, 720,
68 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848 };
69
70 /*
71  * A High Q Tone detector
72  */
73                 
74 #define NTONES  2               /* Number of tones to detect */
75 #define SCALE   4096            /* Scaling factor */
76 #define EXPAVG  14              /* Exponential Average factor */
77 #define POLERAD 3885            /* pole_radius ^ 2 * SCALE */
78
79 /* Table of "-cos(2 * PI * frequency / sample_rate) * SCALE" */
80 static int p[NTONES] = {
81         -2941,                  /* 980 Hz */
82         -2460                   /* 1180 Hz */
83 };
84
85 static void
86 tonedetect(unsigned char *ptr, int count)
87 {
88         int i, j;
89         int y;
90         int c, d, f, n;
91         static int k[NTONES], h[NTONES];
92         static int tones[NTONES];
93         static int amplitude;
94
95
96         for (i = 0; i < count; i++) {
97                 y = a2l[*ptr++];
98                 if (y > 0)
99                         amplitude += (y - amplitude) / EXPAVG;
100                 else
101                         amplitude += (-y - amplitude) / EXPAVG;
102
103                 for(j = 0; j < NTONES; j++) {
104                         c = (POLERAD * (y - k[j])) / SCALE;
105                         d = y + c;
106                         f = (p[j] * (d - h[j])) / SCALE;
107                         n = y - k[j] - c;
108                         if (n < 0)
109                                 n = -n;
110                         k[j] = h[j] + f;
111                         h[j] = f + d;
112                         tones[j] += (n - tones[j]) / EXPAVG;
113                 }
114                 sample(amplitude, tones);
115         }
116 }
117
118 /*
119  * Taste each sample, detect (loss off) carrier, and feed uart
120  */
121
122 #define NCARRIER        1000    /* Samples of carrier for detection */
123
124 static void
125 sample(int vol, int* tones)
126 {
127         static int carrier;
128         
129         if ((tones[0] + tones[1]) > vol * 3/2) {        /* XXX */
130                 if (carrier < NCARRIER)
131                         carrier ++;
132         } else {
133                 if (carrier > 0)
134                         carrier --;
135         }
136
137         if (!dcd && carrier > NCARRIER / 2) {
138                 syslog(LOG_ERR, "CARRIER ON");
139                 dcd = 1;
140         } else if (dcd && carrier < NCARRIER / 2) {
141                 syslog(LOG_ERR, "CARRIER OFF");
142                 dcd = 0;
143         }
144
145         if (!dcd)
146                 return;
147                 
148         if (tones[0] > tones[1]) {
149                 uart(1);
150         } else {
151                 uart(0);
152         }
153 }
154
155 /*
156  * A UART in software
157  */
158
159 #define BITCENTER       13      /* Middle of a bit: 8000/300/2 */
160 static int bitsample[] = {      /* table of sampling points */
161         BITCENTER,
162         BITCENTER + 27,
163         BITCENTER + 54,
164         BITCENTER + 80,
165         BITCENTER + 107,
166         BITCENTER + 134,
167         BITCENTER + 160,
168         BITCENTER + 187,
169         BITCENTER + 214,
170         BITCENTER + 240
171 };
172
173 static void
174 uart(int bit)
175 {
176         static int n, v, j;
177
178         if (n == 0 && bit == 1) 
179                 return;         /* Waiting for start bit */
180         if (n == 0) {
181                 j = 0;          /* Begin start bit */
182                 v = 0;
183                 n++;
184         } else if (j == 0 && bit && n > bitsample[j]) {
185                 n = 0;          /* Gone by middle of start bit */
186         } else if (n > bitsample[j]) {
187                 j++;            /* Sample point */
188                 if (j == 10) {
189                         n = 0;
190                         input_byte(v, bit);
191                 } else {
192                         v = v / 2 + 128 * bit;
193                         n++;
194                 }
195         } else {
196                 n++;
197         }
198 }
199
200 /*
201  * Send a byte using kenrnel tone generation support
202  */
203
204 static void
205 output_byte(int val)
206 {
207         struct i4b_tel_tones tt;
208         int i;
209
210         i = 0;
211         tt.frequency[i] = 1850; tt.duration[i++] = 27;
212
213         tt.frequency[i] = val &   1 ? 1650 : 1850; tt.duration[i++] = 27;
214         tt.frequency[i] = val &   2 ? 1650 : 1850; tt.duration[i++] = 26;
215         tt.frequency[i] = val &   4 ? 1650 : 1850; tt.duration[i++] = 27;
216         tt.frequency[i] = val &   8 ? 1650 : 1850; tt.duration[i++] = 27;
217         tt.frequency[i] = val &  16 ? 1650 : 1850; tt.duration[i++] = 26;
218         tt.frequency[i] = val &  32 ? 1650 : 1850; tt.duration[i++] = 27;
219         tt.frequency[i] = val &  64 ? 1650 : 1850; tt.duration[i++] = 27;
220         tt.frequency[i] = val & 128 ? 1650 : 1850; tt.duration[i++] = 26;
221
222         tt.frequency[i] = 1650; tt.duration[i++] = 27;
223         tt.frequency[i] = 1650; tt.duration[i++] = 0;
224
225         i = ioctl(telfd, I4B_TEL_TONES, &tt);
226         if (i != 0 && errno != EAGAIN) {
227                 syslog(LOG_ERR, "%d: *** %d/%d ***", __LINE__, i, errno);
228                 exit(0);
229         }
230 }
231
232 /*
233  * Create Session
234  */
235
236 static void
237 create_session(void)
238 {
239         int i;
240         char buf[100];
241
242         i = forkpty(&ptyfd, buf, 0, 0);
243         if (i == 0) {
244                 execl("/usr/libexec/getty", "getty", "std.300", "-",
245                     (char *)NULL);
246                 syslog(LOG_ERR, "exec getty %d", errno);
247                 exit(2);
248         } else if (i < 0) {
249                 syslog(LOG_ERR, "forkpty failed %d", errno);
250                 exit(2);
251         }
252         syslog(LOG_ERR, "pty %s", buf);
253 }
254
255 static void 
256 input_byte(int byte, int stopbit)
257 {
258         u_char c;
259         int i;
260         static int first;
261         static u_char buf[80];
262
263         if (!stopbit)
264                 return;
265         c = byte;
266         /*
267          * I have no idea why, but my TB2500 modem sends a sequence of
268          * 28 bytes after carrier is established at the link level, but
269          * before it is acceptted at the logical level.
270          *
271          *  [16100214010201060100000000ff0201020301080402400010034510]
272          *
273          * Unfortunately this contains a ^D which kills getty.
274          * The following code swallows this sequence, assuming that it
275          * is always the same length and always start with 0x16.
276          *
277          */
278         if (first == 0 && c == 0x16) {
279                 sprintf(buf, "%02x", c);
280                 first = 27;
281                 return;
282         } else if (first == 0) {
283                 first = -1;
284                 dcd = 2;
285                 return;
286         }
287         if (first > 0) {
288                 sprintf(buf + strlen(buf), "%02x", c);
289                 first--;
290                 if (!first) {
291                         syslog(LOG_NOTICE, "Got magic [%s]", buf);
292                         *buf = 0;
293                 }
294                 return;
295         }
296         if (ptyfd != -1 && dcd) {
297                 i = write(ptyfd, &c, 1);
298                 if (i != 1 && errno != EAGAIN) {
299                 syslog(LOG_ERR, "%d: *** %d/%d ***", __LINE__, i, errno);
300                         exit(0);
301                 }
302         }
303 }
304
305 int
306 main(int argc, char **argv)
307 {
308         char *device = "/dev/tel0";
309         u_char ibuf[2048];
310         int ii, io;
311         int i, maxfd;
312         struct i4b_tel_tones tt;
313         fd_set rfd, wfd, efd;
314
315         openlog("v21modem", LOG_PID, LOG_DAEMON);
316         /* Find our device name */
317         for (i = 0; i < argc; i++)
318                 if (!strcmp(argv[i], "-D"))
319                         device = argv[i + 1];
320         telfd = open(device, O_RDWR, 0);
321         if (telfd < 0) {
322                 syslog(LOG_ERR, "open %s: %m", device);
323                 exit (0);
324         }
325         syslog(LOG_NOTICE, "Running on %s", device);
326
327         /* Output V.25 tone and carrier */
328         i = 0;
329         tt.frequency[i] =    0; tt.duration[i++] = 1000;
330         tt.frequency[i] = 2100; tt.duration[i++] = 2*8000;
331         tt.frequency[i] =    0; tt.duration[i++] = 400;
332         tt.frequency[i] = 1650; tt.duration[i++] = 1;
333         tt.frequency[i] = 1650; tt.duration[i++] = 0;
334         tt.frequency[i] =    0; tt.duration[i++] = 0;
335         i = ioctl(telfd, I4B_TEL_TONES, &tt);
336         if (i < 0) {
337                 syslog(LOG_ERR, "hangup");
338                 exit(0);
339         }
340
341         create_session();
342
343         /* Wait for carrier */
344         do {
345                 ii = read(telfd, ibuf, sizeof ibuf);
346                 tonedetect(ibuf, ii);
347         } while (ii > 0 && dcd != 2);
348         if (ii < 0) {
349                 syslog(LOG_ERR, "hangup");
350                 exit(0);
351         }
352
353         maxfd = ptyfd;
354         if (telfd > maxfd)
355                 maxfd = telfd;
356         maxfd += 1;
357         do {
358                 FD_ZERO(&rfd);
359                 FD_SET(telfd, &rfd);
360                 FD_SET(ptyfd, &rfd);
361                 FD_ZERO(&wfd);
362                 FD_ZERO(&efd);
363                 FD_SET(telfd, &efd);
364                 FD_SET(ptyfd, &efd);
365                 i = select(maxfd, &rfd, &wfd, &efd, NULL);
366                 if (FD_ISSET(telfd, &rfd)) {
367                         ii = read(telfd, ibuf, sizeof ibuf);
368                         if (ii > 0)
369                                 tonedetect(ibuf, ii);
370                         else
371                                 syslog(LOG_ERR, "hangup");
372                 }
373                 if (FD_ISSET(ptyfd, &rfd)) {
374                         io = read(ptyfd, ibuf, 1);
375                         if (io == 1)
376                                 output_byte(*ibuf);
377                         else if (io == 0) {
378                                 syslog(LOG_ERR, "Session EOF");
379                                 exit(0);
380                         }
381                         
382                 }
383                 if (FD_ISSET(telfd, &efd)) {
384                         syslog(LOG_ERR, "Exception TELFD");
385                         exit (0);
386                 }
387                 if (FD_ISSET(ptyfd, &efd)) {
388                         syslog(LOG_ERR, "Exception PTYFD");
389                         exit (0);
390                 }
391         } while (dcd);
392         syslog(LOG_ERR, "Carrier Lost");
393         exit(0);
394 }