summaryrefslogtreecommitdiff
path: root/twn3_test.c
blob: 692cd3ab3df62245d0e685554b0b2778680f9066 (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "libtwn3.h"

#include <errno.h>
#include <string.h>


int main(int argc, char** argv){
	char data[1024];
	unsigned u;
	uint8_t test_block = 4, test_sector = 1;

	int device = twn3_open("/dev/ttyACM3");

	printf("Device is at fd %d\n", device);
	if(device < 0){
		printf("%s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	//check version
	if(twn3_sync_read_version(device, 0, data, sizeof(data)) >= 0){
		printf("Connected reader firmware version: %s\n", data);
	}

	//read eeprom
	for(u = 0; u < 0x15; u++){
		uint8_t regdata = 0;
		twn3_sync_read_eeprom(device, 0, u, &regdata);
		printf("Register %d has data %02X\n", u, regdata);
	}

	//write some userdata
	//twn3_sync_write_eeprom(device, 0, 19, 0xAA);

	//set ExtendID bit
	//twn3_sync_write_eeprom(device, 0, 5, 0x05);

	//restart device
	//twn3_sync_restart(device, 0);

	//store a key
	//uint8_t key[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
	//twn3_sync_storekey(device, 0, 12, key);

	//wait for a single tag
	int status;
	uint8_t type = tag_none;
	uint8_t serial[20];
	size_t serial_length = sizeof(serial);
	for(status = twn3_sync_select_first(device, 0, &type, serial, &serial_length);
			status == 0;
			status = twn3_sync_select_first(device, 0, &type, serial, &serial_length)){
		if(type != tag_none){
			printf("Tag with UID length %lu found, type %02X\n", serial_length, type);
			break;
		}
		serial_length = sizeof(serial);
	}

	printf("Authenticating for sector %d\n", test_sector);
	uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
	twn3_sync_auth(device, 0, 1, test_sector, key);
	printf("\n");

	printf("Reading block %d\n", test_block);
	twn3_sync_read(device, 0, test_block, (uint8_t*) data, 16);
	printf("Block %d data: ", test_block);
	for(u = 0; u < 16; u++){
		printf("%02x ", data[u]);
	}
	printf("\n\n");

	printf("Incrementing all bytes in block %d\n", test_block);
	for(u = 0; u < 16; u++){
		data[u]++;
	}

	printf("Writing block %d\n", test_block);
	twn3_sync_write(device, 0, test_block, (uint8_t*) data);
	printf("\n");


	close(device);
	return EXIT_SUCCESS;
}