| 1 |
/* |
| 2 |
* FeliCa2Money |
| 3 |
* |
| 4 |
* Copyright (C) 2001-2008 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 |
using System; |
| 22 |
using System.Collections.Generic; |
| 23 |
using System.Text; |
| 24 |
using FelicaLib; |
| 25 |
|
| 26 |
namespace FeliCa2Money |
| 27 |
{ |
| 28 |
// Card クラス |
| 29 |
abstract class Card |
| 30 |
{ |
| 31 |
protected string org; // 組織名 |
| 32 |
protected int bankId = 0; // 銀行番号 |
| 33 |
protected string branchId = "0"; // 支店番号 |
| 34 |
protected string accountId = ""; // 口座番号 |
| 35 |
protected string cardName; // カード名 |
| 36 |
|
| 37 |
public abstract List<Transaction> ReadCard(); |
| 38 |
|
| 39 |
public string Org |
| 40 |
{ |
| 41 |
get { return this.org; } |
| 42 |
} |
| 43 |
|
| 44 |
public int BankId |
| 45 |
{ |
| 46 |
get { return this.bankId; } |
| 47 |
} |
| 48 |
public string BranchId |
| 49 |
{ |
| 50 |
get { return this.branchId; } |
| 51 |
} |
| 52 |
public string CardName |
| 53 |
{ |
| 54 |
get { return this.cardName; } |
| 55 |
} |
| 56 |
|
| 57 |
public string AccountId |
| 58 |
{ |
| 59 |
set { this.accountId = value; } |
| 60 |
get { return this.accountId; } |
| 61 |
} |
| 62 |
|
| 63 |
// タブ区切りの分解 (SFCPeep用) |
| 64 |
protected string[] ParseLine(string line) |
| 65 |
{ |
| 66 |
return line.Split('\t'); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// FeliCa カードクラス |
| 71 |
abstract class CardWithFelicaLib : Card, IDisposable |
| 72 |
{ |
| 73 |
protected int systemCode; // システムコード |
| 74 |
protected int serviceCode; // サービスコード |
| 75 |
protected bool needReverse; // レコード順序を逆転するかどうか |
| 76 |
protected int blocksPerTransaction = 1; // 1トランザクションあたりのブロック数 |
| 77 |
protected int maxTransactions = 100; // 最大トランザクション数 |
| 78 |
|
| 79 |
// カード ID 取得 |
| 80 |
public abstract void analyzeCardId(Felica f); |
| 81 |
|
| 82 |
// Transaction 解析 |
| 83 |
public abstract bool analyzeTransaction(Transaction t, byte[] data); |
| 84 |
|
| 85 |
// カード読み込み |
| 86 |
public override List<Transaction> ReadCard() |
| 87 |
{ |
| 88 |
List<Transaction> list = new List<Transaction>(); |
| 89 |
|
| 90 |
using (Felica f = new Felica()) |
| 91 |
{ |
| 92 |
f.Polling(systemCode); |
| 93 |
analyzeCardId(f); |
| 94 |
|
| 95 |
for (int i = 0; i < maxTransactions; i++) |
| 96 |
{ |
| 97 |
byte[] data = new byte[16 * blocksPerTransaction]; |
| 98 |
byte[] block = null; |
| 99 |
|
| 100 |
for (int j = 0; j < blocksPerTransaction; j++) |
| 101 |
{ |
| 102 |
block = f.ReadWithoutEncryption(serviceCode, i * blocksPerTransaction + j); |
| 103 |
if (block == null) |
| 104 |
{ |
| 105 |
break; |
| 106 |
} |
| 107 |
|
| 108 |
block.CopyTo(data, j * 16); |
| 109 |
} |
| 110 |
if (block == null) |
| 111 |
{ |
| 112 |
break; |
| 113 |
} |
| 114 |
|
| 115 |
Transaction t = new Transaction(); |
| 116 |
|
| 117 |
// データが全0かどうかチェック |
| 118 |
int x = 0; |
| 119 |
foreach (int xx in data) |
| 120 |
{ |
| 121 |
x |= xx; |
| 122 |
} |
| 123 |
if (x == 0) |
| 124 |
{ |
| 125 |
// データが全0なら無視(空エントリ) |
| 126 |
t.Invalidate(); |
| 127 |
} |
| 128 |
|
| 129 |
// トランザクション解析 |
| 130 |
else if (!analyzeTransaction(t, data)) |
| 131 |
{ |
| 132 |
t.Invalidate(); |
| 133 |
} |
| 134 |
list.Add(t); |
| 135 |
} |
| 136 |
} |
| 137 |
if (needReverse) |
| 138 |
{ |
| 139 |
list.Reverse(); |
| 140 |
} |
| 141 |
PostProcess(list); |
| 142 |
|
| 143 |
return list; |
| 144 |
} |
| 145 |
|
| 146 |
protected virtual void PostProcess(List<Transaction> list) |
| 147 |
{ |
| 148 |
// do nothing |
| 149 |
} |
| 150 |
|
| 151 |
public virtual void Dispose() |
| 152 |
{ |
| 153 |
} |
| 154 |
|
| 155 |
// 複数バイト読み込み (big endian) |
| 156 |
protected int read2b(byte[] b, int pos) |
| 157 |
{ |
| 158 |
int ret = b[pos] << 8 | b[pos + 1]; |
| 159 |
return ret; |
| 160 |
} |
| 161 |
|
| 162 |
protected int read3b(byte[] b, int pos) |
| 163 |
{ |
| 164 |
int ret = b[pos] << 16 | b[pos + 1] << 8 | b[pos + 2]; |
| 165 |
return ret; |
| 166 |
} |
| 167 |
|
| 168 |
protected int read4b(byte[] b, int pos) |
| 169 |
{ |
| 170 |
int ret = b[pos] << 24 | b[pos + 1] << 16 | b[pos + 2] << 8 | b[pos + 3]; |
| 171 |
return ret; |
| 172 |
} |
| 173 |
|
| 174 |
// little endian |
| 175 |
protected int read2l(byte[] b, int pos) |
| 176 |
{ |
| 177 |
int ret = b[pos + 1] << 8 | b[pos]; |
| 178 |
return ret; |
| 179 |
} |
| 180 |
} |
| 181 |
} |