summary refs log tree commit diff
path: root/bin/msr606.c
blob: 1f2acbf4ac62bd81908e0cb3cdffb33f5220d2c4 (plain) (blame)
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
/* Copyright (C) 2018  Curtis McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <err.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <termios.h>
#include <unistd.h>

static int tty = -1;

static void ttyWrite(const uint8_t *ptr, size_t size) {
	ssize_t ret = write(tty, ptr, size);
	if (ret < 0) err(EX_IOERR, "write");
}
static void ttyRead(uint8_t *ptr, size_t size) {
	while (size) {
		ssize_t ret = read(tty, ptr, size);
		if (ret < 0) err(EX_IOERR, "read");
		ptr += ret;
		size -= ret;
	}
}

static void reset(void) {
	uint8_t buf[] = { 0x1B, 'a' };
	ttyWrite(buf, 2);
}

static void test(void) {
	uint8_t buf[3] = { 0x1B, 'e' };
	ttyWrite(buf, 2);
	ttyRead(buf, 2);
	if (buf[1] != 'y') errx(EX_PROTOCOL, "test fail %hhX", buf[1]);

	buf[1] = 0x87;
	ttyWrite(buf, 2);
	ttyRead(buf, 2);
	if (buf[1] != '0') errx(EX_PROTOCOL, "ram fail %hhX", buf[1]);

	buf[1] = 't';
	ttyWrite(buf, 2);
	ttyRead(buf, 3);
	printf("model: %c%c\n", buf[1], buf[2]);

	buf[1] = 'v';
	ttyWrite(buf, 2);
	ttyRead(buf, 2);
	printf("firmware: %hhu\n", buf[1]);
}

static void sensorTest(void) {
	uint8_t buf[] = { 0x1B, 0x86 };
	ttyWrite(buf, 2);
	ttyRead(buf, 2);
	if (buf[1] != '0') errx(EX_PROTOCOL, "sensor fail %hhX", buf[1]);
}

static char led;
static void setLed(void) {
	uint8_t buf[2] = { 0x1B };
	switch (led) {
		break; case 'a': buf[1] = 0x82;
		break; case 'g': buf[1] = 0x83;
		break; case 'y': buf[1] = 0x84;
		break; case 'r': buf[1] = 0x85;
		break; default:  buf[1] = 0x81;
	}
	ttyWrite(buf, 2);
}

static void status(void) {
	uint8_t buf[2];
	ttyRead(buf, 2);
	switch (buf[1]) {
		break; case '0': return;
		break; case '1': errx(EX_DATAERR, "read/write error");
		break; case '2': errx(EX_DATAERR, "command format error");
		break; case '4': errx(EX_DATAERR, "invalid command");
		break; case '9': errx(EX_DATAERR, "invalid card swipe");
		break; default:  errx(EX_PROTOCOL, "status fail %hhX", buf[1]);
	}
}

static void readCard(void) {
	uint8_t buf[] = { 0x1B, 'r' };
	ttyWrite(buf, 2);
	ttyRead(buf, 2);
	if (buf[1] != 's') errx(EX_PROTOCOL, "read fail %hhX", buf[1]);
	for (;;) {
		ttyRead(buf, 1);
		if (buf[0] == '?') {
			ttyRead(buf, 1);
			if (buf[0] == 0x1C) break;
			printf("?");
		} else {
			printf("%c", buf[0]);
		}
	}
	status();
}

static void writeCard(void) {
	uint8_t buf[] = { 0x1B, 'w', 0x1B, 's' };
	ttyWrite(buf, 4);
	ssize_t size;
	while (0 < (size = read(STDIN_FILENO, buf, 1))) {
		ttyWrite(buf, 1);
	}
	if (size < 0) err(EX_IOERR, "(stdin)");
	buf[0] = '?';
	buf[1] = 0x1C;
	ttyWrite(buf, 2);
	status();
}

static char erase;
static void eraseCard(void) {
	uint8_t buf[] = { 0x1B, 'c', erase - '0' };
	ttyWrite(buf, 3);
	ttyRead(buf, 2);
	if (buf[1] != '0') errx(EX_DATAERR, "erase fail %hhX", buf[1]);
}

static void setHiCo(void) {
	uint8_t buf[] = { 0x1B, 'x' };
	ttyWrite(buf, 2);
	ttyRead(buf, 2);
	if (buf[1] != '0') errx(EX_PROTOCOL, "hi-co fail %hhX", buf[1]);
}
static void setLoCo(void) {
	uint8_t buf[] = { 0x1B, 'y' };
	ttyWrite(buf, 2);
	ttyRead(buf, 2);
	if (buf[1] != '0') errx(EX_PROTOCOL, "lo-co fail %hhX", buf[1]);
}
static void getCo(void) {
	uint8_t buf[] = { 0x1B, 'd' };
	ttyWrite(buf, 2);
	ttyRead(buf, 2);
	if (buf[1] == 'h') printf("hi-co\n");
	else if (buf[1] == 'l') printf("lo-co\n");
	else errx(EX_PROTOCOL, "get co fail %hhX", buf[1]);
}

int main(int argc, char *argv[]) {
	const char *file = "/dev/ttyUSB0";
	void (*func)(void) = test;

	int opt;
	while (0 < (opt = getopt(argc, argv, "L:RTce:f:hlrtw"))) {
		switch (opt) {
			break; case 'L': led = optarg[0]; func = setLed;
			break; case 'R': func = reset;
			break; case 'T': func = sensorTest;
			break; case 'c': func = getCo;
			break; case 'e': erase = optarg[0]; func = eraseCard;
			break; case 'f': file = optarg;
			break; case 'h': func = setHiCo;
			break; case 'l': func = setLoCo;
			break; case 'r': func = readCard;
			break; case 't': func = test;
			break; case 'w': func = writeCard;
			break; default: return EX_USAGE;
		}
	}

	tty = open(file, O_RDWR);
	if (tty < 0) err(EX_NOINPUT, "%s", file);

	struct termios attr;
	int error = tcgetattr(tty, &attr);
	if (error) err(EX_IOERR, "tcgetattr");

	cfmakeraw(&attr);
	error = tcsetattr(tty, TCSANOW, &attr);
	if (error) err(EX_IOERR, "tcsetattr");

	func();
}