Develop and Download Open Source Software

Browse Subversion Repository

Contents of /tags/FeliCa2Money-2.4/FelicaLib.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 150 - (show annotations) (download)
Sun Mar 16 10:36:07 2008 UTC (16 years ago) by tmurakam
File size: 3596 byte(s)
ver 2.4 tag

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Runtime.InteropServices;
5
6 namespace FelicaLib
7 {
8 // システムコード
9 enum SystemCode : int
10 {
11 Any = 0xffff, // ANY
12 Common = 0xfe00, // 共通領域
13 Cyberne = 0x0003, // サイバネ領域
14
15 Edy = 0xfe00, // Edy (=共通領域)
16 Suica = 0x0003, // Suica (=サイバネ領域)
17 }
18
19 public class Felica : IDisposable
20 {
21 [DllImport("felicalib.dll")]
22 private extern static IntPtr pasori_open(String dummy);
23 [DllImport("felicalib.dll")]
24 private extern static void pasori_close(IntPtr p);
25 [DllImport("felicalib.dll")]
26 private extern static int pasori_init(IntPtr p);
27 [DllImport("felicalib.dll")]
28 private extern static IntPtr felica_polling(IntPtr p, ushort systemcode, byte rfu, byte time_slot);
29 [DllImport("felicalib.dll")]
30 private extern static void felica_free(IntPtr f);
31 [DllImport("felicalib.dll")]
32 private extern static void felica_getidm(IntPtr f, byte[] data);
33 [DllImport("felicalib.dll")]
34 private extern static void felica_getpmm(IntPtr f, byte[] data);
35 [DllImport("felicalib.dll")]
36 private extern static int felica_read_without_encryption02(IntPtr f, int servicecode, int mode, byte addr, byte[] data);
37
38 private IntPtr pasorip = IntPtr.Zero;
39 private IntPtr felicap = IntPtr.Zero;
40
41 public Felica()
42 {
43 pasorip = pasori_open(null);
44 if (pasorip == IntPtr.Zero)
45 {
46 throw new Exception("felicalib.dll を開けません");
47 }
48 if (pasori_init(pasorip) != 0)
49 {
50 throw new Exception("PaSoRi に接続できません");
51 }
52 }
53
54 public void Dispose()
55 {
56 if (pasorip != IntPtr.Zero)
57 {
58 pasori_close(pasorip);
59 pasorip = IntPtr.Zero;
60 }
61 }
62
63 ~Felica()
64 {
65 Dispose();
66 }
67
68 public void Polling(int systemcode)
69 {
70 felica_free(felicap);
71
72 felicap = felica_polling(pasorip, (ushort)systemcode, 0, 0);
73 if (felicap == IntPtr.Zero)
74 {
75 throw new Exception("カード読み取り失敗");
76 }
77 }
78
79 public byte[] IDm()
80 {
81 if (felicap == IntPtr.Zero)
82 {
83 throw new Exception("no polling executed.");
84 }
85
86 byte[] buf = new byte[8];
87 felica_getidm(felicap, buf);
88 return buf;
89 }
90
91 public byte[] PMm()
92 {
93 if (felicap == IntPtr.Zero)
94 {
95 throw new Exception("no polling executed.");
96 }
97
98 byte[] buf = new byte[8];
99 felica_getpmm(felicap, buf);
100 return buf;
101 }
102
103 public byte[] ReadWithoutEncryption(int servicecode, int addr)
104 {
105 if (felicap == IntPtr.Zero)
106 {
107 throw new Exception("no polling executed.");
108 }
109
110 byte[] data = new byte[16];
111 int ret = felica_read_without_encryption02(felicap, servicecode, 0, (byte)addr, data);
112 if (ret != 0)
113 {
114 return null;
115 }
116 return data;
117 }
118 }
119 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26