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