summaryrefslogtreecommitdiff
path: root/libtwn3.c
blob: e9a70bccff5bbf81ecf15aa027617217962e02a5 (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
#include <stdio.h>
#include <stdint.h>

#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define DEFAULT_NODE 0xFF

#ifdef DEBUG
	#define DBG(x) (x)
	static void xxd(uint8_t* buf, size_t len){
		size_t n;
		for(n = 0; n < len; n++){
			printf("%02X ", buf[n]);
		}
	}
#else
	#define DBG(x)
#endif

#include "libtwn3.h"

#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

#pragma pack(push, 1)
typedef struct /*_twn3_hdr_t*/ {
	uint8_t stx; //start of transmission, fixed 0x02
	uint8_t station;
	uint8_t length; //bytes in data segment
} twn3_hdr_t;

typedef struct /*_twn3_trl_t*/ {
	uint8_t bcc; //xor over all bytes between stx and etx
	uint8_t etx; //fixed 0x03
} twn3_trl_t;
#pragma pack(pop)

int twn3_open(char* device){
	int fd = open(device, O_RDWR | O_CLOEXEC);
	struct termios tty;
	uint8_t recv_buf[1024];
	ssize_t bytes;

	if(fd < 0){
		return -1;
	}

	if(tcgetattr(fd, &tty)){
		return -1;
	}

	cfsetospeed(&tty, B9600);
	cfsetispeed(&tty, B9600);
	tty.c_iflag = IGNBRK | IGNPAR;
	tty.c_oflag = 0;
	tty.c_cflag |= CS8;

	tty.c_lflag = 0;
	tty.c_cc[VMIN] = 0;
	tty.c_cc[VTIME] = 3;

	if(tcsetattr(fd, TCSAFLUSH, &tty)){
		return -1;
	}

	tcflush(fd, TCIOFLUSH);

	//the default config is for the reader to start in continuous read mode
	//send two cancel commands to return to a known mode, at least one should produce '?'
	twn3_send_command(fd, 0, (uint8_t*) "..", 2);
	//then, read until the timeout elapses to clear the buffer.
	//this is necessary because the default protocol does not have clear message boundaries
	bytes = twn3_receive_response(fd, RX_FLAG_UNTIL_TIMEOUT, recv_buf, sizeof(recv_buf));
	if(bytes <= 3 || recv_buf[bytes - 3] != '?'
			|| recv_buf[bytes - 2] != '\r'
			|| recv_buf[bytes - 1] != '\n'){
		DBG(printf("Invalid initial response (%ld bytes): ", bytes));
		DBG(xxd(recv_buf, bytes));
		DBG(printf("\n"));
		close(fd);
		return -1;
	}

	return fd;
}

ssize_t twn3_send_command(int fd, uint8_t flags, uint8_t* data, size_t len){
	size_t n;
	twn3_hdr_t hdr = {
		.stx = 0x02,
		.station = DEFAULT_NODE,
		.length = len
	};
	twn3_trl_t trl = {
		.etx = 0x03
	};

	DBG(printf("Out: "));
	if(flags & TX_FLAG_BINARY){
		//calculate checksum
		trl.bcc = hdr.station ^ hdr.length;
		for(n = 0; n < len; n++){
			trl.bcc ^= data[n];
		}

		DBG(xxd((uint8_t*) &hdr, sizeof(hdr)));

		write(fd, &hdr, sizeof(hdr));
	}

	DBG(xxd(data, len));
	write(fd, data, len);

	if(flags & TX_FLAG_BINARY){
		DBG(xxd((uint8_t*) &trl, sizeof(trl)));
		write(fd, &trl, sizeof(trl));
	}
	DBG(printf("\n"));
	return 0;
}

ssize_t twn3_receive_response(int fd, uint8_t flags, uint8_t* response, size_t max_length){
	uint8_t recv_buffer[1024] = "";
	ssize_t status, bytes = 0;

	//TODO handle buffer overrun
	//TODO handle binary protocol

	while(1){
		status = read(fd, recv_buffer + bytes, sizeof(recv_buffer) - bytes);
		if(status < 0){
			return -1;
		}
		if(status){
			bytes += status;
			if(!(flags & RX_FLAG_UNTIL_TIMEOUT) && bytes > 2){
				//check if the last two buffer bytes are a newline
				if(recv_buffer[bytes - 2] == '\r'
						&& recv_buffer[bytes - 1] == '\n'){
					break;
				}
			}
		}
		else if(flags & RX_FLAG_UNTIL_TIMEOUT){
			break;
		}
	}

	DBG(printf("In%s: ", (flags & RX_FLAG_UNTIL_TIMEOUT) ? " (timeout)" : ""));
	DBG(xxd(recv_buffer, bytes));
	DBG(printf("\n"));
	if(response && max_length){
		memcpy(response, recv_buffer, MIN(bytes, max_length));
	}
	return bytes;
}

ssize_t twn3_sync_command(int fd, uint8_t flags, uint8_t* cmd, size_t cmd_len, uint8_t* response, size_t max_length){
	twn3_send_command(fd, flags, cmd, cmd_len);
	return twn3_receive_response(fd, flags, response, max_length);
}

int twn3_sync_restart(int fd, uint8_t flags){
	//TODO stop auto-started continuous mode
	ssize_t bytes = twn3_sync_command(fd, flags, (uint8_t*) "x", 1, NULL, 0);
	return (bytes >= 2) ? 0 : -1;
}

int twn3_sync_read_version(int fd, uint8_t flags, char* version, size_t max_length){
	ssize_t bytes = twn3_sync_command(fd, flags, (uint8_t*) "v", 1, (uint8_t*) version, max_length);
	if(bytes >= max_length){
		version[max_length] = 0;
	}
	else{
		//strip newlines
		while(bytes > 0 && !isprint(version[bytes - 1])){
			version[bytes - 1] = 0;
			bytes--;
		}
	}
	return bytes;
}

int twn3_sync_read_eeprom(int fd, uint8_t flags, uint8_t reg, uint8_t* value){
	char data[6];
	ssize_t bytes = 0;
	snprintf(data, sizeof(data) - 1, "re%02X", reg);
	
	bytes = twn3_sync_command(fd, flags, (uint8_t*) data, 4, (uint8_t*) data, 4);
	if(bytes > 0){
		data[2] = 0;
		if(value){
			*value = strtoul(data, NULL, 16);
		}
		return 0;
	}
	return -1;
}

int twn3_sync_write_eeprom(int fd, uint8_t flags, uint8_t reg, uint8_t value){
	char data[8];
	ssize_t bytes = 0;
	snprintf(data, sizeof(data) - 1, "we%02X%02X", reg, value);

	if(reg > 0x14){
		return -1;
	}

	bytes = twn3_sync_command(fd, flags, (uint8_t*) data, 6, (uint8_t*) data, 4);
	//failure may return `F\r\n`, which is also valid hex - genius!
	if(bytes > 0 && data[1] != '\r'){
		data[2] = 0;
		bytes = strtoul(data, NULL, 16);
		return (bytes == value) ? 0 : -1;
	}
	return -1;
}

int twn3_sync_storekey(int fd, uint8_t flags, uint8_t keyid, uint8_t key[6]){
	char data[20];
	snprintf(data, sizeof(data) - 1, "wm%02X%02X%02X%02X%02X%02X%02X",
			keyid, key[0], key[1],
			key[2], key[3],
			key[4], key[5]);

	if(keyid > 31){
		return -1;
	}

	twn3_sync_command(fd, flags, (uint8_t*) data, 16, NULL, 0);
	return 0;
}

int twn3_sync_antenna(int fd, uint8_t flags, uint8_t enable){
	uint8_t data[5];
	ssize_t bytes = 0;

	if(enable){
		bytes = twn3_sync_command(fd, flags, (uint8_t*) "pon", 3, data, sizeof(data));
	}
	else{
		bytes = twn3_sync_command(fd, flags, (uint8_t*) "poff", 4, data, sizeof(data));
	}

	return (bytes > 2 && data[0] == 'P') ? 0 : -1;
}

int twn3_sync_select_first(int fd, uint8_t flags, uint8_t* type, uint8_t* serial, size_t* length);
int twn3_sync_list(int fd, uint8_t flags, uint8_t* type, uint8_t* serial, size_t* length);
int twn3_sync_select(int fd, uint8_t flags, uint8_t* type, uint8_t* serial, size_t* length);
int twn3_sync_auth(int fd, uint8_t flags, uint8_t key_a, uint8_t block, uint8_t key[6]);
int twn3_sync_auth_stored(int fd, uint8_t flags, uint8_t key_a, uint8_t block, uint8_t key_id);

static inline uint8_t hex_nibble(char in){
	if(in <= 39){
		return in & 0x0F;
	}
	if(in <= 46){
		return in - 55;
	}
	return in - 87;
}

int twn3_sync_read(int fd, uint8_t flags, uint8_t block, uint8_t* values, size_t max_len){
	char data[40];
	ssize_t bytes;
	unsigned u;
	snprintf(data, sizeof(data) - 1, "r%02X", block);

	bytes = twn3_sync_command(fd, flags, (uint8_t*) data, 3, (uint8_t*) data, sizeof(data));
	if(bytes > 0){
		if(bytes > 10){
			//convert ascii hex representation
			for(u = 0; u < max_len && bytes > 2 * u; u++){
				if(!isxdigit(data[u * 2]) || !isxdigit(data[(u * 2) + 1])){
					return -1;
				}

				values[u] = (hex_nibble(data[u * 2]) << 4) | hex_nibble(data[(u * 2) + 1]);
			}
			return u;
		}
		else{
			return 0;
		}
	}
	return -1;
}

int twn3_sync_write(int fd, uint8_t flags, uint8_t block, uint8_t value[16]){
	char data[40];
	ssize_t bytes;
	snprintf(data, sizeof(data) - 1, "w%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
			block,
			value[0], value[1], value[2], value[3],
			value[4], value[5], value[6], value[7],
			value[8], value[9], value[10], value[11],
			value[12], value[13], value[14], value[15]);

	bytes = twn3_sync_command(fd, flags, (uint8_t*) data, 19, (uint8_t*) data, sizeof(data));
	if(bytes > 0){
		if(data[1] != '\r'){
			return 0;
		}
		return 1;
	}
	return -1;
}