| 1 |
/* |
| 2 |
* FeliCa2Money |
| 3 |
* |
| 4 |
* Copyright (C) 2001-2007 Takuya Murakami |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <vcl.h> |
| 22 |
#pragma hdrstop |
| 23 |
#include <stdio.h> |
| 24 |
#include "felicalib.h" |
| 25 |
#include "Card.h" |
| 26 |
#include "Transaction.h" |
| 27 |
#include "SfcPeep.h" |
| 28 |
#include "Nanaco.h" |
| 29 |
|
| 30 |
// |
| 31 |
// Nanaco |
| 32 |
// |
| 33 |
|
| 34 |
static int read4b(uint8 *p); |
| 35 |
static int read2b(uint8 *p); |
| 36 |
|
| 37 |
/// �R���X�g���N�^ |
| 38 |
NanacoCard::NanacoCard(void) |
| 39 |
{ |
| 40 |
Ident = "Nanaco"; |
| 41 |
CardName = "Nanaco"; |
| 42 |
} |
| 43 |
|
| 44 |
int NanacoCard::ReadCard(void) |
| 45 |
{ |
| 46 |
pasori *p = pasori_open(NULL); |
| 47 |
if (!p) { |
| 48 |
return -1; // open failed |
| 49 |
} |
| 50 |
pasori_init(p); |
| 51 |
|
| 52 |
felica *f = felica_polling(p, 0xfe00, 0, 0); |
| 53 |
if (!f) { |
| 54 |
pasori_close(p); |
| 55 |
return -1; // can't read card |
| 56 |
} |
| 57 |
|
| 58 |
// get card id |
| 59 |
int i; |
| 60 |
CardId = ""; |
| 61 |
for (i = 0; i < 16; i++) { |
| 62 |
char buf[8]; |
| 63 |
sprintf(buf, "%02x", f->IDm[i]); |
| 64 |
CardId += buf; |
| 65 |
} |
| 66 |
|
| 67 |
for (i = 0; ; i++) { |
| 68 |
uint8 data[16]; |
| 69 |
if (felica_read_without_encryption02(f, 0x564f, 0, (uint8)i, data) != 0) { |
| 70 |
break; |
| 71 |
} |
| 72 |
|
| 73 |
Transaction *t = new Transaction; |
| 74 |
|
| 75 |
switch (data[0]) { |
| 76 |
case 0x47: |
| 77 |
default: |
| 78 |
t->desc = sjis2utf8("Nanaco�x��"); |
| 79 |
t->type = T_DEBIT; |
| 80 |
break; |
| 81 |
case 0x6f: |
| 82 |
t->desc = sjis2utf8("Nanaco�`���[�W"); |
| 83 |
t->type = T_DEP; |
| 84 |
break; |
| 85 |
} |
| 86 |
|
| 87 |
// ���z |
| 88 |
t->value = read4b(data + 1); |
| 89 |
if (t->type == T_DEBIT) { |
| 90 |
t->value = - t->value; |
| 91 |
} |
| 92 |
|
| 93 |
// �c�� |
| 94 |
t->balance = read4b(data + 5); |
| 95 |
|
| 96 |
// ���t/���� |
| 97 |
int v = read4b(data + 9); |
| 98 |
t->date.year = 2000 + (v >> 21); |
| 99 |
t->date.month = (v >> 17) & 0xf; |
| 100 |
t->date.date = (v >> 12) & 0x1f; |
| 101 |
t->date.hour = (v >> 6) & 0x3f; |
| 102 |
t->date.minutes = v & 0x3f; |
| 103 |
t->date.seconds = 0; |
| 104 |
|
| 105 |
// ID |
| 106 |
t->id = read2b(data + 13); |
| 107 |
|
| 108 |
list.insert(list.begin(), t); |
| 109 |
} |
| 110 |
|
| 111 |
pasori_close(p); |
| 112 |
|
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
static int read4b(uint8 *p) |
| 117 |
{ |
| 118 |
int v; |
| 119 |
v = (*p++) << 24; |
| 120 |
v |= (*p++) << 16; |
| 121 |
v |= (*p++) << 8; |
| 122 |
v |= *p; |
| 123 |
return v; |
| 124 |
} |
| 125 |
|
| 126 |
static int read2b(uint8 *p) |
| 127 |
{ |
| 128 |
int v; |
| 129 |
v = (*p++) << 8; |
| 130 |
v |= *p; |
| 131 |
return v; |
| 132 |
} |