summaryrefslogtreecommitdiffstats
path: root/share/examples/isdn/v21/v21modem.c
blob: 2707cdb392b5c6be95ed0934f1873b31f0271d4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
 * ----------------------------------------------------------------------------
 *
 * This is a V.21 modem for ISDN4BSD.
 *
 * $FreeBSD$
 */

#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <err.h>
#include <sys/ioccom.h>
#include <errno.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <libutil.h>

#include <i4b/i4b_tel_ioctl.h>

static void create_session(void);
static void input_byte(int byte, int stopbit);
static void sample(int vol, int* tones);
static void tonedetect(unsigned char *ptr, int count);
static void uart(int bit);

static int dcd;			/* Carrier on ? */
static int ptyfd = -1;		/* PTY filedescriptor */
static int telfd = -1;		/* I4BTEL filedescriptor */

/*
 * Alaw to Linear [-32767..32767] conversion
 */

static int a2l[256] = { -5504, -5248, -6016, -5760, -4480, -4224,
-4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040,
-6784, -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
-3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, -22016,
-20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208,
-29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008,
-10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592,
-16128, -15616, -13056, -12544, -14080, -13568, -344, -328, -376,
-360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392,
-440, -424, -88, -72, -120, -104, -24, -8, -56, -40, -216, -200,
-248, -232, -152, -136, -184, -168, -1376, -1312, -1504, -1440,
-1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632,
-1568, -1760, -1696, -688, -656, -752, -720, -560, -528, -624,
-592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504, 5248,
6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528,
6272, 7040, 6784, 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016, 20992, 24064,
23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232,
26112, 25088, 28160, 27136, 11008, 10496, 12032, 11520, 8960, 8448,
9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
344, 328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408,
392, 440, 424, 88, 72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232,
152, 136, 184, 168, 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688, 656, 752, 720,
560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848 };

/*
 * A High Q Tone detector
 */
		
#define NTONES	2		/* Number of tones to detect */
#define SCALE	4096		/* Scaling factor */
#define EXPAVG	14		/* Exponential Average factor */
#define POLERAD	3885		/* pole_radius ^ 2 * SCALE */

/* Table of "-cos(2 * PI * frequency / sample_rate) * SCALE" */
static int p[NTONES] = {
	-2941,			/* 980 Hz */
	-2460			/* 1180 Hz */
};

static void
tonedetect(unsigned char *ptr, int count)
{
	int i, j;
	int y;
	int c, d, f, n;
	static int k[NTONES], h[NTONES];
	static int tones[NTONES];
	static int amplitude;


	for (i = 0; i < count; i++) {
		y = a2l[*ptr++];
		if (y > 0)
			amplitude += (y - amplitude) / EXPAVG;
		else
			amplitude += (-y - amplitude) / EXPAVG;

		for(j = 0; j < NTONES; j++) {
			c = (POLERAD * (y - k[j])) / SCALE;
			d = y + c;
			f = (p[j] * (d - h[j])) / SCALE;
			n = y - k[j] - c;
			if (n < 0)
				n = -n;
			k[j] = h[j] + f;
			h[j] = f + d;
			tones[j] += (n - tones[j]) / EXPAVG;
		}
		sample(amplitude, tones);
	}
}

/*
 * Taste each sample, detect (loss off) carrier, and feed uart
 */

#define NCARRIER	1000	/* Samples of carrier for detection */

static void
sample(int vol, int* tones)
{
	static int carrier;
	
	if ((tones[0] + tones[1]) > vol * 3/2) {	/* XXX */
		if (carrier < NCARRIER)
			carrier ++;
	} else {
		if (carrier > 0)
			carrier --;
	}

	if (!dcd && carrier > NCARRIER / 2) {
		syslog(LOG_ERR, "CARRIER ON");
		dcd = 1;
	} else if (dcd && carrier < NCARRIER / 2) {
		syslog(LOG_ERR, "CARRIER OFF");
		dcd = 0;
	}

	if (!dcd)
		return;
		
	if (tones[0] > tones[1]) {
		uart(1);
	} else {
		uart(0);
	}
}

/*
 * A UART in software
 */

#define BITCENTER	13	/* Middle of a bit: 8000/300/2 */
static int bitsample[] = {	/* table of sampling points */
	BITCENTER,
	BITCENTER + 27,
	BITCENTER + 54,
	BITCENTER + 80,
	BITCENTER + 107,
	BITCENTER + 134,
	BITCENTER + 160,
	BITCENTER + 187,
	BITCENTER + 214,
	BITCENTER + 240
};

static void
uart(int bit)
{
	static int n, v, j;

	if (n == 0 && bit == 1)	
		return;		/* Waiting for start bit */
	if (n == 0) {
		j = 0;		/* Begin start bit */
		v = 0;
		n++;
	} else if (j == 0 && bit && n > bitsample[j]) {
		n = 0;		/* Gone by middle of start bit */
	} else if (n > bitsample[j]) {
		j++;		/* Sample point */
		if (j == 10) {
			n = 0;
			input_byte(v, bit);
		} else {
			v = v / 2 + 128 * bit;
			n++;
		}
	} else {
		n++;
	}
}

/*
 * Send a byte using kenrnel tone generation support
 */

static void
output_byte(int val)
{
	struct i4b_tel_tones tt;
	int i;

	i = 0;
	tt.frequency[i] = 1850; tt.duration[i++] = 27;

	tt.frequency[i] = val &   1 ? 1650 : 1850; tt.duration[i++] = 27;
	tt.frequency[i] = val &   2 ? 1650 : 1850; tt.duration[i++] = 26;
	tt.frequency[i] = val &   4 ? 1650 : 1850; tt.duration[i++] = 27;
	tt.frequency[i] = val &   8 ? 1650 : 1850; tt.duration[i++] = 27;
	tt.frequency[i] = val &  16 ? 1650 : 1850; tt.duration[i++] = 26;
	tt.frequency[i] = val &  32 ? 1650 : 1850; tt.duration[i++] = 27;
	tt.frequency[i] = val &  64 ? 1650 : 1850; tt.duration[i++] = 27;
	tt.frequency[i] = val & 128 ? 1650 : 1850; tt.duration[i++] = 26;

	tt.frequency[i] = 1650; tt.duration[i++] = 27;
	tt.frequency[i] = 1650; tt.duration[i++] = 0;

	i = ioctl(telfd, I4B_TEL_TONES, &tt);
	if (i != 0 && errno != EAGAIN) {
		syslog(LOG_ERR, "%d: *** %d/%d ***", __LINE__, i, errno);
		exit(0);
	}
}

/*
 * Create Session
 */

static void
create_session(void)
{
	int i;
	char buf[100];

	i = forkpty(&ptyfd, buf, 0, 0);
	if (i == 0) {
		execl("/usr/libexec/getty", "getty", "std.300", "-",
		    (char *)NULL);
		syslog(LOG_ERR, "exec getty %d", errno);
		exit(2);
	} else if (i < 0) {
		syslog(LOG_ERR, "forkpty failed %d", errno);
		exit(2);
	}
	syslog(LOG_ERR, "pty %s", buf);
}

static void 
input_byte(int byte, int stopbit)
{
	u_char c;
	int i;
	static int first;
	static u_char buf[80];

	if (!stopbit)
		return;
	c = byte;
	/*
	 * I have no idea why, but my TB2500 modem sends a sequence of
	 * 28 bytes after carrier is established at the link level, but
	 * before it is acceptted at the logical level.
	 *
	 *  [16100214010201060100000000ff0201020301080402400010034510]
	 *
	 * Unfortunately this contains a ^D which kills getty.
	 * The following code swallows this sequence, assuming that it
	 * is always the same length and always start with 0x16.
	 *
	 */
	if (first == 0 && c == 0x16) {
		sprintf(buf, "%02x", c);
		first = 27;
		return;
	} else if (first == 0) {
		first = -1;
		dcd = 2;
		return;
	}
	if (first > 0) {
		sprintf(buf + strlen(buf), "%02x", c);
		first--;
		if (!first) {
			syslog(LOG_NOTICE, "Got magic [%s]", buf);
			*buf = 0;
		}
		return;
	}
	if (ptyfd != -1 && dcd) {
		i = write(ptyfd, &c, 1);
		if (i != 1 && errno != EAGAIN) {
		syslog(LOG_ERR, "%d: *** %d/%d ***", __LINE__, i, errno);
			exit(0);
		}
	}
}

int
main(int argc, char **argv)
{
	char *device = "/dev/tel0";
	u_char ibuf[2048];
	int ii, io;
	int i, maxfd;
	struct i4b_tel_tones tt;
	fd_set rfd, wfd, efd;

	openlog("v21modem", LOG_PID, LOG_DAEMON);
	/* Find our device name */
	for (i = 0; i < argc; i++)
		if (!strcmp(argv[i], "-D"))
			device = argv[i + 1];
	telfd = open(device, O_RDWR, 0);
	if (telfd < 0) {
		syslog(LOG_ERR, "open %s: %m", device);
		exit (0);
	}
	syslog(LOG_NOTICE, "Running on %s", device);

	/* Output V.25 tone and carrier */
	i = 0;
	tt.frequency[i] =    0; tt.duration[i++] = 1000;
	tt.frequency[i] = 2100; tt.duration[i++] = 2*8000;
	tt.frequency[i] =    0; tt.duration[i++] = 400;
	tt.frequency[i] = 1650; tt.duration[i++] = 1;
	tt.frequency[i] = 1650; tt.duration[i++] = 0;
	tt.frequency[i] =    0; tt.duration[i++] = 0;
	i = ioctl(telfd, I4B_TEL_TONES, &tt);
	if (i < 0) {
		syslog(LOG_ERR, "hangup");
		exit(0);
	}

	create_session();

	/* Wait for carrier */
	do {
		ii = read(telfd, ibuf, sizeof ibuf);
		tonedetect(ibuf, ii);
	} while (ii > 0 && dcd != 2);
	if (ii < 0) {
		syslog(LOG_ERR, "hangup");
		exit(0);
	}

	maxfd = ptyfd;
	if (telfd > maxfd)
		maxfd = telfd;
	maxfd += 1;
	do {
		FD_ZERO(&rfd);
		FD_SET(telfd, &rfd);
		FD_SET(ptyfd, &rfd);
		FD_ZERO(&wfd);
		FD_ZERO(&efd);
		FD_SET(telfd, &efd);
		FD_SET(ptyfd, &efd);
		i = select(maxfd, &rfd, &wfd, &efd, NULL);
		if (FD_ISSET(telfd, &rfd)) {
			ii = read(telfd, ibuf, sizeof ibuf);
			if (ii > 0)
				tonedetect(ibuf, ii);
			else
				syslog(LOG_ERR, "hangup");
		}
		if (FD_ISSET(ptyfd, &rfd)) {
			io = read(ptyfd, ibuf, 1);
			if (io == 1)
				output_byte(*ibuf);
			else if (io == 0) {
				syslog(LOG_ERR, "Session EOF");
				exit(0);
			}
			
		}
		if (FD_ISSET(telfd, &efd)) {
			syslog(LOG_ERR, "Exception TELFD");
			exit (0);
		}
		if (FD_ISSET(ptyfd, &efd)) {
			syslog(LOG_ERR, "Exception PTYFD");
			exit (0);
		}
	} while (dcd);
	syslog(LOG_ERR, "Carrier Lost");
	exit(0);
}
OpenPOWER on IntegriCloud