1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.helper.aoa;
17 
18 import com.sun.jna.Library;
19 import com.sun.jna.Pointer;
20 import com.sun.jna.ptr.PointerByReference;
21 
22 /** JNA adapter for <a href="https://libusb.info">libusb</a>. */
23 interface IUsbNative extends Library {
24 
25     /**
26      * Initialize libusb, must be called before calling any other function.
27      *
28      * @param context output location for context pointer
29      * @return 0 on success, or an error code
30      */
libusb_init(PointerByReference context)31     int libusb_init(PointerByReference context);
32 
33     /**
34      * Deinitialize libusb.
35      *
36      * @param ctx context to deinitialize
37      */
libusb_exit(Pointer ctx)38     void libusb_exit(Pointer ctx);
39 
40     /**
41      * Returns a short user-friendly description of the given error code.
42      *
43      * @param errcode error code to look up
44      * @return short description of the error code
45      */
libusb_strerror(int errcode)46     String libusb_strerror(int errcode);
47 
48     /**
49      * Returns a list of USB devices currently attached to the system.
50      *
51      * @param ctx context to operate on
52      * @param list output location for a list of devices
53      * @return number of devices, or an error code
54      */
libusb_get_device_list(Pointer ctx, PointerByReference list)55     int libusb_get_device_list(Pointer ctx, PointerByReference list);
56 
57     /**
58      * Frees a list of devices previously discovered using {@link #libusb_get_device_list}.
59      *
60      * @param list list to free
61      * @param unref_devices true to unref devices
62      */
libusb_free_device_list(Pointer list, boolean unref_devices)63     void libusb_free_device_list(Pointer list, boolean unref_devices);
64 
65     /**
66      * Open a device and obtain a device handle.
67      *
68      * @param dev device to open
69      * @param dev_handle output location for the device handle
70      * @return 0 on success, or an error code
71      */
libusb_open(Pointer dev, PointerByReference dev_handle)72     int libusb_open(Pointer dev, PointerByReference dev_handle);
73 
74     /**
75      * Close a device handle previously opened with {@link #libusb_open}.
76      *
77      * @param dev_handle device handle to close
78      */
libusb_close(Pointer dev_handle)79     void libusb_close(Pointer dev_handle);
80 
81     /**
82      * Perform a USB port reset to reinitialize a device.
83      *
84      * @param dev_handle device handle to reset
85      * @return 0 on success, or an error code
86      */
libusb_reset_device(Pointer dev_handle)87     int libusb_reset_device(Pointer dev_handle);
88 
89     /**
90      * Get the USB device descriptor for a given device.
91      *
92      * @param dev device
93      * @param desc output location for the descriptor data
94      * @return 0 on success, or an error code
95      */
libusb_get_device_descriptor(Pointer dev, byte[] desc)96     int libusb_get_device_descriptor(Pointer dev, byte[] desc);
97 
98     /**
99      * Retrieve a string descriptor in C style ASCII.
100      *
101      * @param dev_handle device handle
102      * @param desc_index index of the descriptor to retrieve
103      * @param data output buffer for ASCII string descriptor
104      * @param length size of data buffer
105      * @return number of bytes returned, or an error code
106      */
libusb_get_string_descriptor_ascii( Pointer dev_handle, byte desc_index, byte[] data, int length)107     int libusb_get_string_descriptor_ascii(
108         Pointer dev_handle, byte desc_index, byte[] data, int length);
109 
110     /**
111      * Perform a synchronous USB control transfer.
112      *
113      * @param dev_handle device handle to communicate with
114      * @param bmRequestType request type field, determines the direction of the transfer
115      * @param bRequest request field
116      * @param wValue value field
117      * @param wIndex index field
118      * @param data suitably-sized data buffer for input or output
119      * @param wLength length of the data buffer
120      * @param timeout timeout milliseconds, or 0 for unlimited
121      * @return number of bytes transferred, or an error code
122      */
libusb_control_transfer( Pointer dev_handle, byte bmRequestType, byte bRequest, short wValue, short wIndex, byte[] data, short wLength, int timeout)123     int libusb_control_transfer(
124         Pointer dev_handle,
125         byte bmRequestType,
126         byte bRequest,
127         short wValue,
128         short wIndex,
129         byte[] data,
130         short wLength,
131         int timeout);
132 }
133