aboutsummaryrefslogtreecommitdiff
path: root/telejet.c
blob: 049ff61fccd364b8609b86533c674751cdc61211 (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
/* $Id$ */
/*
 * Copyright (c) 2009 Dimitri Sokolyuk <demon@dim13.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#define	debug 0

struct	termios tio;

char	*device = "/dev/ttyS0";
char	to_kbd[] = "#TO_KBD";
char	to__pc[] = "#TO__PC";
int	minlen = 10;		/* minimal response length (if any) */

/* Ctrl-Alt-F1 */
char	caf[] = {0x14, 0x11, 0x05, 0xf0, 0x05, 0xf0, 0x11, 0xf0, 0x14};

/* Ctrl-Alt-Del */
char	cad[] = {0x14, 0x11, 0x71, 0xf0, 0x71, 0xf0, 0x11, 0xf0, 0x14};

enum	{NONE, SOFT, HARD, QUERY};

int compose(char *buf, int addr, char cmd, char *data, int dlen);
int chkresponse(char *buf, int *addr, char *data, int *dlen);
int talk(int fd, char *buf, int wlen, int rlen);
int prstr(char *data, int len);
int main(int argc, char **argv);
int opendev(char *dev);
int closedev(int fd);
unsigned short int get_crc_16(int start, char *p, int n);

int
compose(char *buf, int addr, char cmd, char *data, int dlen)
{
	char	*p;
	int	crc;

	p = buf;

	memcpy(p, to_kbd, sizeof(to_kbd) - 1);
	p += sizeof(to_kbd) - 1;
	*p++ = (addr >> 16) & 0xff;
	*p++ = (addr >> 8) & 0xff;
	*p++ = addr & 0xff;
	*p++ = dlen + 4;
	*p++ = cmd;

	while (dlen--)
		*p++ = *data++;

	crc = get_crc_16(0, buf, p - buf);
#if debug
	printf("crc: 0x%x\n", crc);
#endif
	*p++ = (crc >> 8) & 0xff;
	*p++ = crc & 0xff;

	return p - buf;
}

int
chkresponse(char *buf, int *addr, char *data, int *dlen)
{
	char	*p;
	int	crc, len;

	if (memcmp(buf, to__pc, sizeof(to__pc) - 1) != 0)
		return -1;
	
	p = buf + sizeof(to__pc) - 1;
	*addr = (*p++ & 0xff) << 16;
	*addr |= (*p++ & 0xff) << 8;
	*addr |= *p++ & 0xff;

	*dlen = *p++ - 3;
	for (len = *dlen; len--; ++data)
		*data = *p++;

	crc = (*p++ & 0xff) << 8;
	crc |= *p & 0xff;

#if debug
	printf("crc: 0x%x\n", get_crc_16(0, buf, *dlen + 11));
#endif
	if (get_crc_16(0, buf, *dlen + 11) != crc)
		return -1;
	
	return 0;
}

int
talk(int fd, char *buf, int wlen, int rlen)
{
	fd_set	readfd;
	struct	timeval tv;
	int	done = 0;
	int	expected = minlen;
	int	ret;
	int	expflag = 1;
	
	ret = write(fd, buf, wlen);
	if (ret == -1) {
		perror("write");
		return ret;
	}

	tv.tv_usec = 0;
	tv.tv_sec = 5;

	FD_ZERO(&readfd);
	FD_SET(fd, &readfd);
	while (done < expected) {
		ret = select(fd + 1, &readfd, NULL, NULL, &tv);
		if (ret == -1)
			perror("select");
		if (ret <= 0)
			return ret;
		if (FD_ISSET(fd, &readfd)) {
			ret = read(fd, buf + done, rlen - done);
			if (ret == -1) {
				perror("read");
				return ret;
			}
			done += ret;
		}
		if (expflag && done >= minlen) {
			expflag = 0;
			expected += (int)buf[minlen];
		}
#if debug
		printf("done: %d, expected: %d\n", done, expected);
#endif
	}

	return done;
}

int
prstr(char *data, int len)
{
	data += 2;
	len -= 2;

	while (len--)
		putchar(*data++);
	putchar('\n');

	return 0;
}

int
opendev(char *dev)
{
	struct	termios raw;
	int	fd;

	fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
	if (fd == -1)
		err(1, "%s", dev);
	
	tcgetattr(fd, &tio);
	memcpy(&raw, &tio, sizeof(struct termios));

	cfsetspeed(&raw, B2400);
	cfmakeraw(&raw);
#if 1
	raw.c_cc[VMIN] = minlen;	/* at least 10 bytes */
	raw.c_cc[VTIME] = 2;	/* 200 ms */
#endif
	
	if (tcsetattr(fd, TCSAFLUSH, &raw) < 0)
		err(1, "can't set raw mode");
	
	return fd;
}

int
closedev(int fd)
{
	if (tcsetattr(fd, TCSAFLUSH, &tio) < 0)
		err(1, "can't restore from raw mode");
	close(fd);

	return 0;
}

int
telejet(char *tj, int type)
{
	char	buf[256], data[256];
	int	fd, len, ret;
	int	eval = -1;
	int	addr = 0xffffff;
#if debug
	char	*p;
#endif

	if (tj)
		addr = strtol(tj, NULL, 0x10);
	else if (type != QUERY)
		return -1;

#if debug
	printf("addr: 0x%x\n", addr);
#endif

	fd = opendev("/dev/ttyS0");

	switch (type) {
	case QUERY:
		len = compose(buf, addr, 'V', NULL, 0);
		break;
	case HARD:
		/* push the button */
		len = compose(buf, addr, 'R', NULL, 0);
		break;
	case SOFT:
	default:
		/* escape from x11 to console 1 */
		len = compose(buf, addr, 'D', caf, sizeof(caf));
		talk(fd, buf, len, sizeof(buf));
		/* send tree finger */
		len = compose(buf, addr, 'D', cad, sizeof(cad));
		break;
	}

	ret = talk(fd, buf, len, sizeof(buf));
	closedev(fd);

	if (ret > 0) {
		if (chkresponse(buf, &addr, data, &len) == 0) {
			if (memcmp(data + len - 2, "OK", 2) == 0) {
				eval = 0;
#if debug
				prstr(data, len);
#endif
			}
#if debug
		} else {
			printf("chk failed, len=%d\n", ret);
			for (p = buf; ret-- > 0; ++p)
				printf("'%c'\t0x%.2x\n", *p & 0xff, *p & 0xff);
#endif
		}
	}

	return eval;
}

#if 1
void
usage()
{
	extern	char *__progname;

	fprintf(stderr, "usage: %s [-hsq] [addr]\n", __progname);

	exit(1);
}

int
main(int argc, char **argv)
{
	int	c, type = SOFT;

	while ((c = getopt(argc, argv, "f:shq")) != -1)
		switch (c) {
		case 'f':
			device = strdup(optarg);
			break;
		case 's':
			type = SOFT;
			break;
		case 'h':
			type = HARD;
			break;
		case 'q':
			type = QUERY;
			break;
		}

	argc -= optind;
	argv += optind;

	if (!argc && type != QUERY)
		usage();

	return telejet(*argv, type);
}
#endif