-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathUSB_Interface.c
More file actions
120 lines (102 loc) · 3.05 KB
/
USB_Interface.c
File metadata and controls
120 lines (102 loc) · 3.05 KB
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
/*
* Copyright (C) 2011 Klaus Reimer (k@ailis.de)
* See COPYING file for copying conditions
*/
/**
* @name USB_Interface
*
* Native methods for the USB_Interface class.
*
* @author Klaus Reimer <k@ailis.de>
*/
#include <stdint.h>
#include <jni.h>
#include <usb.h>
#include "usb4java.h"
#include "USB_Interface_Descriptor.h"
/**
* Creates and returns a new USB interface wrapper object.
*
* @param env
* The JNI environment.
* @param iface
* The USB interface.
* @return The USB interface wrapper object.
*/
jobject wrap_usb_interface(JNIEnv *env, struct usb_interface *iface)
{
if (!iface) return NULL;
jclass cls = (*env)->FindClass(env, PACKAGE_DIR"/USB_Interface");
if (cls == NULL) return NULL;
jmethodID constructor = (*env)->GetMethodID(env, cls, "<init>",
"(Ljava/nio/ByteBuffer;)V");
if (constructor == NULL) return NULL;
jobject buffer = (*env)->NewDirectByteBuffer(env, iface, 0);
return (*env)->NewObject(env, cls, constructor, buffer);
}
/**
* Returns the wrapped USB interface object from the specified wrapper object.
*
* @param env
* The JNI environment.
* @param obj
* The USB interface wrapper object.
* @return The USB interface object.
*/
struct usb_interface *unwrap_usb_interface(JNIEnv *env, jobject obj)
{
jclass cls = (*env)->GetObjectClass(env, obj);
jfieldID field = (*env)->GetFieldID(env, cls, "iface",
"Ljava/nio/ByteBuffer;");
jobject buffer = (*env)->GetObjectField(env, obj, field);
return (struct usb_interface *) (*env)->GetDirectBufferAddress(env, buffer);
}
/**
* Creates and returns an array with USB interface wrapper objects.
*
* @param env
* The JNI environment
* @param num_interfaces
* The number of interfaces
* @param interfaces
* The interfaces to wrap.
* @return The array with the USB interface wrappers.
*/
jobjectArray wrap_usb_interfaces(JNIEnv *env, uint8_t num_interfaces,
struct usb_interface *interfaces)
{
int i;
jobjectArray array = (jobjectArray) (*env)->NewObjectArray(env,
num_interfaces, (*env)->FindClass(env, PACKAGE_DIR"/USB_Interface"),
NULL);
for (i = 0; i < num_interfaces; i++)
(*env)->SetObjectArrayElement(env, array, i,
wrap_usb_interface(env, &interfaces[i]));
return array;
}
/**
* Returns the number of available interface descriptors.
*
* @return The number of available interface descriptors.
*/
JNIEXPORT jshort JNICALL METHOD_NAME(USB_1Interface, num_1altsetting)
(
JNIEnv *env, jobject this
)
{
return (jshort) unwrap_usb_interface(env, this)->num_altsetting;
}
/**
* Returns the array with all available interface descriptors.
*
* @return The array with the interface descriptors.
*/
JNIEXPORT jobjectArray JNICALL METHOD_NAME(USB_1Interface, altsetting)
(
JNIEnv *env, jobject this
)
{
struct usb_interface *interface = unwrap_usb_interface(env, this);
return wrap_usb_interface_descriptors(env, interface->num_altsetting,
interface->altsetting);
}