summaryrefslogtreecommitdiff
path: root/easytool/easycard.c
blob: 26a1430521e0b4114b00acffe424bc165f86d71e (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
/* A reverse-engineered implementation of the EasyCard data format */

/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

/* System includes */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <netinet/in.h>

/* libnfc includes */
#include <nfc/nfc-types.h>
#include <nfc/mifaretag.h>

#include "mifare_classic.h"

/* Easycard specific includes */
#include "easycard.h"

time_t easy_timestamp2time(const uint8_t *easy_ts)
{
	return (easy_ts[2] << 16 | easy_ts[1] << 8 | easy_ts[0]) << 8;
}

/* apply a telta (positive or negative) to a EasyCard log record */
int easy_update_log_rec(struct easy_log_rec *elr, int16_t delta)
{
	int32_t sum = elr->amount + delta;
	int32_t remaining = elr->remaining = delta;

	if ((sum < 0 || sum > 0xffff) ||
	    (remaining < 0 || remaining > 0xffff))
		return -ERANGE;

	elr->amount = sum;
	elr->remaining = remaining;

	return 0;
}

/* apply a delta to the 'sum of day' record in Sector 15 Block 2 */
int easy_update_sum_of_day(struct easy_sect15blk2 *s15b2, int16_t delta)
{
	int32_t sum = s15b2->sum_of_day + delta;

	if (sum < 0 || sum > 0xffff)
		return -ERANGE;

	s15b2->sum_of_day = sum;

	return 0;
}

static char tsbuf[64];
char *easy_asc_timestamp(const uint8_t *timestamp)
{
	time_t t_time = easy_timestamp2time(timestamp);
	struct tm *t_tm = gmtime(&t_time);
	memset(tsbuf, 0, sizeof(tsbuf));
	snprintf(tsbuf, sizeof(tsbuf), "%4u-%02u-%02u %02u:%02u",
		t_tm->tm_year+1900, t_tm->tm_mon+1, t_tm->tm_mday,
		t_tm->tm_hour, t_tm->tm_min);
	return tsbuf;
}

static const struct mfcl_addr easy_trans_log_addrs[] = {
	{ .sector = 2,	.block = 2, },

	/* first block of sector 3 is not a log record */
	{ .sector = 3,	.block = 1, },
	{ .sector = 3,	.block = 2, },

	{ .sector = 4,	.block = 0, },
	{ .sector = 4,	.block = 1, },
	{ .sector = 4,	.block = 2, },

	{ .sector = 5,	.block = 0, },
	{ .sector = 5,	.block = 1, },
	{ .sector = 5,	.block = 2, },
};

void easy_dump_log_rec(const struct easy_log_rec *elr)
{
	/* Skip empty records */
	if (!elr->timestamp)
		return;

	printf("%s | %02x | %4u NTD | %4u NTD | %-15s",
		easy_asc_timestamp(elr->timestamp),
		elr->trans_id, elr->amount, elr->remaining,
		get_value_string(easy_tt_names, elr->trans_type));
	switch (elr->trans_type) {
	case EASY_TT_MRT_ENTER:
	case EASY_TT_MRT_REENTER:
	case EASY_TT_MRT_EXIT:
	case EASY_TT_RECHARGE:
		printf(" | %s\n",
			get_value_string(taipei_mrt_stn_id,
					 elr->station_code));
		break;
	default:
		printf("\n");
	}
}

void easy_dump_log(mifare_tag *mft)
{
	unsigned int i;

	/*     "2010-01-01 01:01 | TID|  Amount  |  Balance |  */
	puts("Timestamp        | TID|  Amount  |  Balance | Type            | MRT Station");
	puts("---------------------------------------------------------------------------");

	for (i = 0; i < ARRAY_SIZE(easy_trans_log_addrs); i++) {
		unsigned int sect = easy_trans_log_addrs[i].sector;
		unsigned int block = easy_trans_log_addrs[i].block;
		unsigned int block_base = sect * 4;
		void *data = mft->amb[block_base+block].mbd.abtData;

#if 0
		printf("Dumping Sector %u Block %u (0x%x)\n", sect, block,
			(block_base+sect)*16);
#endif
		easy_dump_log_rec(data);
	}
}
personal git repositories of Harald Welte. Your mileage may vary