forked from usb4java/libusb4java
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDeviceList.c
60 lines (48 loc) · 1.42 KB
/
DeviceList.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
/*
* Copyright (C) 2013 Klaus Reimer ([email protected])
* See LICENSE.md file for copying conditions
*/
#include "DeviceList.h"
#include "Device.h"
void setDeviceList(JNIEnv* env, libusb_device* const *list, jint size, jobject object)
{
jclass cls;
jfieldID field;
SET_POINTER(env, list, object, "deviceListPointer");
cls = (*env)->GetObjectClass(env, object);
field = (*env)->GetFieldID(env, cls, "size", "I");
(*env)->SetIntField(env, object, field, size);
}
libusb_device** unwrapDeviceList(JNIEnv* env, jobject list)
{
return (libusb_device **) unwrapPointer(env, list, "deviceListPointer");
}
void resetDeviceList(JNIEnv* env, jobject object)
{
jclass cls;
jfieldID field;
RESET_POINTER(env, object, "deviceListPointer");
cls = (*env)->GetObjectClass(env, object);
field = (*env)->GetFieldID(env, cls, "size", "I");
(*env)->SetIntField(env, object, field, 0);
}
/**
* Device get(index)
*/
JNIEXPORT jobject JNICALL METHOD_NAME(DeviceList, get)
(
JNIEnv *env, jobject this, jint index
)
{
jclass cls;
libusb_device* const *list;
jfieldID field;
int size;
list = unwrapDeviceList(env, this);
if (!list) return NULL;
cls = (*env)->GetObjectClass(env, this);
field = (*env)->GetFieldID(env, cls, "size", "I");
size = (*env)->GetIntField(env, this, field);
if (index < 0 || index >= size) return NULL;
return wrapDevice(env, list[index]);
}