Develop and Download Open Source Software

Browse Subversion Repository

Contents of /branches/FeliCa2Money-1/felicalib.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 204 - (show annotations) (download) (as text)
Sun May 18 04:21:26 2008 UTC (15 years, 10 months ago) by tmurakam
File MIME type: text/x-csrc
File size: 7780 byte(s)
Move older version (ver 1.x)

1 /*
2 felicalib - FeliCa access wrapper library
3
4 Copyright (c) 2007, Takuya Murakami, All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
8 met:
9
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16
17 3. Neither the name of the project nor the names of its contributors
18 may be used to endorse or promote products derived from this software
19 without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 /**
34 @file felicalib.c
35
36 felicalib �{��
37 */
38
39 #include "felicalib.h"
40 #include "felicaint.h"
41
42 /**
43 @brief PaSoRi ���I�[�v������
44 @param[in] dummy �_�~�[ (libpasori ����������������)
45 @retval pasori �n���h��
46
47 felica.dll �����[�h�A����������
48 */
49 pasori *pasori_open(char *dummy)
50 {
51 pasori *p;
52
53 p = (pasori *)malloc(sizeof(pasori));
54
55 /* open felica.dll */
56 p->hInstDLL = LoadLibrary(_T("C:\\Program Files\\Common Files\\Sony Shared\\FeliCaLibrary\\felica.dll"));
57 if (p->hInstDLL == NULL) {
58 free(p);
59 return NULL;
60 }
61
62 /* resolve function pointers */
63 #define resolve_entry(f) p->f = (f ## _t)GetProcAddress(p->hInstDLL, #f)
64 resolve_entry(initialize_library);
65 resolve_entry(dispose_library);
66 resolve_entry(open_reader_writer_auto);
67 resolve_entry(close_reader_writer);
68 resolve_entry(polling_and_get_card_information);
69 resolve_entry(polling_and_request_system_code);
70 resolve_entry(polling_and_search_service_code);
71 resolve_entry(read_block_without_encryption);
72
73 if (!p->initialize_library()) {
74 free(p);
75 return NULL;
76 }
77 return p;
78 }
79
80 /**
81 @brief PaSoRi �n���h�����N���[�Y����
82 @param[in] p pasori�n���h��
83
84 felica.dll ����������
85 */
86 void pasori_close(pasori *p)
87 {
88 p->dispose_library();
89 FreeLibrary(p->hInstDLL);
90 }
91
92 /**
93 @brief PaSoRi ������������
94 @param[in] p pasori�n���h��
95 @return �G���[�R�[�h
96 */
97 int pasori_init(pasori *p)
98 {
99 if (!p->open_reader_writer_auto()) {
100 return -1;
101 }
102 return 0;
103 }
104
105 static felica *alloc_felica(pasori *p, uint16 systemcode)
106 {
107 felica *f = (felica *)malloc(sizeof(felica));
108 f->p = p;
109 f->systemcode = H2NS(systemcode);
110
111 return f;
112 }
113
114 /**
115 @brief FeliCa ���|�[�����O����
116 @param[in] p pasori�n���h��
117 @param[in] systemcode �V�X�e���R�[�h
118 @param[in] RFU RFU (�g�p������)
119 @param[in] timeslot �^�C���X���b�g
120 @return felica�n���h��
121 */
122 felica *felica_polling(pasori *p, uint16 systemcode, uint8 RFU, uint8 timeslot)
123 {
124 felica *f;
125 POLLING polling;
126 uint16 sc;
127 uint8 number_of_cards = 0;
128 CARD_INFO card_info;
129
130 f = alloc_felica(p, systemcode);
131
132 sc = H2NS(systemcode);
133 polling.system_code = (uint8 *)≻
134 polling.time_slot = timeslot;
135
136 card_info.card_idm = f->IDm;
137 card_info.card_pmm = f->PMm;
138
139 if (!p->polling_and_get_card_information(&polling, &number_of_cards, &card_info) ||
140 number_of_cards == 0) {
141 free(f);
142 return NULL;
143 }
144
145 return f;
146 }
147
148 /**
149 @brief �������������������u���b�N����������
150 @param[in] f felica�n���h��
151 @param[in] servicecode �T�[�r�X�R�[�h
152 @param[in] mode ���[�h(�g�p������)
153 @param[in] addr �u���b�N����
154 @param[out] data �f�[�^(16�o�C�g)
155 @return �G���[�R�[�h
156
157 �T�[�r�X�R�[�h�A�u���b�N�������w�������u���b�N�����������B
158 �V�X�e���R�[�h�� felica_polling ���w�������������g�p�������B
159 */
160 int felica_read_without_encryption02(felica *f, int servicecode, int mode, uint8 addr, uint8 *data)
161 {
162 INSTR_READ_BLOCK irb;
163 OUTSTR_READ_BLOCK orb;
164
165 uint8 service_code_list[2];
166 uint8 block_list[2];
167 uint8 status_flag1, status_flag2;
168 uint8 result_number_of_blocks = 0;
169
170 service_code_list[0] = servicecode & 0xff;
171 service_code_list[1] = servicecode >> 8;
172 block_list[0] = 0x80;
173 block_list[1] = addr;
174
175 irb.card_idm = f->IDm;
176 irb.number_of_services = 1;
177 irb.service_code_list = service_code_list;
178 irb.number_of_blocks = 1;
179 irb.block_list = block_list;
180
181 orb.status_flag_1 = &status_flag1;
182 orb.status_flag_2 = &status_flag2;
183 orb.result_number_of_blocks = &result_number_of_blocks;
184 orb.block_data = data;
185
186 if (!f->p->read_block_without_encryption(&irb, &orb)) {
187 return -1;
188 }
189 if (status_flag1 != 0) {
190 return -1;
191 }
192 return 0;
193 }
194
195 /*------------- ���������� libpasori ���������� (����) ------------*/
196
197 /**
198 @brief �V�X�e���R�[�h������
199 @param[in] p pasori�n���h��
200 @return felica�n���h��
201
202 �������Afelica�\������ num_system_code/system_code ���i�[�������B
203 �����A�V�X�e���R�[�h���G���f�B�A�����t���i�[���������������������������B
204 */
205 felica * felica_enum_systemcode(pasori *p)
206 {
207 felica *f;
208 POLLING polling;
209 CARD_INFO card_info;
210 INSTR_REQ_SYSTEM_CODE irs;
211 OUTSTR_REQ_SYSTEM_CODE ors;
212
213 f = alloc_felica(p, POLLING_ANY);
214
215 polling.system_code = (uint8 *)&f->systemcode;
216 polling.time_slot = 0;
217
218 card_info.card_idm = f->IDm;
219 card_info.card_pmm = f->PMm;
220
221 irs.card_idm = f->IDm;
222
223 ors.system_code_list = (uint8 *)f->system_code;
224
225 if (!f->p->polling_and_request_system_code(&polling, &irs, &card_info, &ors)) {
226 free(f);
227 return NULL;
228 }
229
230 f->num_system_code = ors.number_of_system_codes;
231 return f;
232 }
233
234 /**
235 @brief �T�[�r�X/�G���A�R�[�h������
236 @param[in] p pasori�n���h��
237 @param[in] systemcode �V�X�e���R�[�h
238 @return felica�n���h��
239
240 �������Afelica�\������ num_area_code/area_code/end_service_code ������
241 num_service_code/service_code ���i�[�������B
242 */
243 felica * felica_enum_service(pasori *p, uint16 systemcode)
244 {
245 felica *f;
246
247 POLLING polling;
248 CARD_INFO card_info;
249 INSTR_SEARCH_SERVICE iss;
250 OUTSTR_SEARCH_SERVICE oss;
251
252 f = alloc_felica(p, systemcode);
253
254 polling.system_code = (uint8 *)&f->systemcode;
255 polling.time_slot = 0;
256
257 card_info.card_idm = f->IDm;
258 card_info.card_pmm = f->PMm;
259
260 iss.buffer_size_of_area_codes = MAX_AREA_CODE;
261 iss.buffer_size_of_service_codes = MAX_SERVICE_CODE;
262 iss.offset_of_area_service_index = 0;
263
264 oss.num_area_codes = 10;
265 oss.num_service_codes = 10;
266 oss.service_code_list = (uint8 *)f->service_code;
267 oss.area_code_list = (uint8 *)f->area_code;
268 oss.end_service_code_list = (uint8 *)f->end_service_code;
269
270 if (!f->p->polling_and_search_service_code(&polling, &iss, &card_info, &oss)) {
271 free(f);
272 return NULL;
273 }
274
275 f->num_area_code = oss.num_area_codes;
276 f->num_service_code = oss.num_service_codes;
277
278 return f;
279 }

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