-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyg_devaccess_pasori.c
299 lines (249 loc) · 5.75 KB
/
cyg_devaccess_pasori.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <string.h>
#include <sys/types.h>
#include "libusb-1.0/libusb.h"
#include "hk_devaccess.h"
// デバッグ設定
#define HKNFCRW_ENABLE_DEBUG
#ifdef HKNFCRW_ENABLE_DEBUG
//#define DBG_WRITEDATA
//#define DBG_READDATA
#include <stdio.h>
#define LOGI printf
#define LOGE printf
#define printf printf
#else
#define LOGI(...)
#define LOGE(...)
#define printf(...)
#endif //HKNFCRW_ENABLE_DEBUG
static const uint16_t VID = 0x054c;
static const uint16_t PID = 0x02e1;
static const unsigned int TIMEOUT = 65535 * 2;
static libusb_context* m_pContext;
static libusb_device_handle* m_pHandle;
static uint8_t m_EndPntIn = 0xff;
static uint8_t m_EndPntOut = 0xff;
static int m_ReadSize;
static uint8_t m_ReadBuf[256];
static int m_ReadPtr;
static bool usb_open();
static void usb_close();
static uint8_t usb_write(const uint8_t *pData, uint8_t size);
static uint8_t usb_read(uint8_t *pData, uint8_t size);
static bool usb_open()
{
//printf("%s\n", __PRETTY_FUNCTION__);
if(m_pContext) {
// open済み
//printf("already opened\n");
return true;
}
int r;
//printf("--init...\n");
r = libusb_init(&m_pContext);
if (r < 0) {
LOGE("cannot init\n");
return false;
}
//printf(" ==> done!\n");
// libusb_set_debug(NULL, 3);
#if 0
libusb_device **devs;
ssize_t cnt = libusb_get_device_list(m_pContext, &devs);
if (cnt < 0) {
LOGE("cannot list\n");
usb_close();
return false;
}
libusb_device *dev;
int i = 0;
while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
LOGE("failed to get device descriptor\n");
i = -1;
break;
}
if((desc.idVendor == VID) && (desc.idProduct == PID)) {
i = 0;
break;
}
}
if(i != 0) {
LOGE("cannot find\n");
usb_close();
return false;
}
r = libusb_open(dev, &m_pHandle);
if(r != 0) {
LOGE("cannot open : %d\n", r);
usb_close();
return false;
}
libusb_free_device_list(devs, 1);
#else
//printf("--open...\n");
m_pHandle = libusb_open_device_with_vid_pid(NULL, VID, PID);
if(m_pHandle == NULL) {
LOGE("cannot open\n");
usb_close();
return false;
}
//printf(" ==>done!\n");
libusb_device *dev;
dev = libusb_get_device(m_pHandle);
#endif
struct libusb_config_descriptor* pConfig;
r = libusb_get_config_descriptor(dev, 0, &pConfig);
if(r != 0) {
LOGE("cannot getdesc : %d\n", r);
usb_close();
return false;
}
//printf("--find\n");
int inf;
int alt;
int desc;
for(inf = 0; inf < pConfig->bNumInterfaces; inf++) {
const struct libusb_interface* inter = &pConfig->interface[inf];
for(alt = 0; alt < inter->num_altsetting; alt++) {
const struct libusb_interface_descriptor* idesc = &(inter->altsetting[alt]);
for(desc = 0; desc < idesc->bNumEndpoints; desc++) {
const struct libusb_endpoint_descriptor* EndPnt = &(idesc->endpoint[desc]);
if(EndPnt->bEndpointAddress & LIBUSB_ENDPOINT_IN) {
//printf("find IN\n");
m_EndPntIn = EndPnt->bEndpointAddress;
}
else {
//printf("find OUT\n");
m_EndPntOut = EndPnt->bEndpointAddress;
}
}
}
}
r = libusb_claim_interface(m_pHandle, 0);
if(r != 0) {
LOGE("cannot claim\n");
usb_close();
return false;
}
//Debug Out
libusb_set_debug(m_pContext, 3);
//printf("open!\n");
return true;
}
static void usb_close()
{
if(m_pHandle) {
libusb_release_interface(m_pHandle, 0);
libusb_close(m_pHandle);
m_pHandle = NULL;
}
if(m_pContext) {
libusb_exit(m_pContext);
m_pContext = NULL;
}
printf("%s\n", __PRETTY_FUNCTION__);
}
static uint8_t usb_write(const uint8_t *pData, uint8_t size)
{
if(m_pHandle == NULL) {
return 0;
}
int transferred = 0;
int r = libusb_bulk_transfer(m_pHandle, m_EndPntOut, (uint8_t*)pData, size, &transferred, TIMEOUT);
if(r) {
LOGE("err : %d / transferred : %d\n", r, transferred);
}
return transferred;
}
static uint8_t usb_read(uint8_t *pData, uint8_t size)
{
if(m_pHandle == NULL) {
return 0;
}
if(m_ReadSize) {
memcpy(pData, &m_ReadBuf[m_ReadPtr], size);
m_ReadSize -= size;
m_ReadPtr += size;
return size;
}
m_ReadPtr = 0;
int r = libusb_bulk_transfer(m_pHandle, m_EndPntIn, m_ReadBuf, sizeof(m_ReadBuf), &m_ReadSize, TIMEOUT);
if(r) {
LOGE("err : %d / transferred : %d\n", r, m_ReadSize);
}
memcpy(pData, &m_ReadBuf[m_ReadPtr], size);
m_ReadSize -= size;
m_ReadPtr = size;
return size;
}
/**
* ポートオープン
*
* @retval true オープン成功
*/
bool hk_nfcrw_open(void)
{
return usb_open();
}
/**
* ポートクローズ
*/
void hk_nfcrw_close(void)
{
usb_close();
}
/**
* ポート送信
*
* @param[in] data 送信データ
* @param[in] len dataの長さ
* @return 送信したサイズ
*/
uint16_t hk_nfcrw_write(const uint8_t* data, uint16_t len)
{
#ifdef DBG_READDATA
int i;
printf("---------------------\n");
for(i=0; i<len; i++) {
printf("[W]%02x\n", data[i]);
}
#endif
return usb_write(data, len);
}
/**
* 受信
*
* @param[out] data 受信バッファ
* @param[in] len 受信サイズ
*
* @return 受信したサイズ
*
* @attention - len分のデータを受信するか失敗するまで処理がブロックされる。
*/
uint16_t hk_nfcrw_read(uint8_t* data, uint16_t len)
{
uint16_t ret_len = 0;
ret_len = usb_read(data, len);
#ifdef DBG_READDATA
int i;
printf("---------------------\n");
for(i=0; i<ret_len; i++) {
printf("[R]%02x\n", data[i]);
}
#endif
return ret_len;
}
/**
* ポート受信タイムアウト時間設定
*
* タイムアウト処理が可能な場合、受信タイムアウト時間を設定する。
* タイムアウトがない場合は、何も処理しないし、#hk_nfcrw_read()にも影響はない。
*
* @param[in] msec タイムアウト時間(ミリ秒)。0のときはタイムアウト解除。
*/
void hk_nfcrw_read_timeout(uint16_t msec)
{
}