@@ -107,27 +107,23 @@ private void createContextAndInitLibUSB() throws Exception{
107
107
108
108
returningValue = LibUsb .init (contextNS );
109
109
if (returningValue != LibUsb .SUCCESS )
110
- throw new Exception ("libusb initialization\n Returned : " +UsbErrorCodes .getErrCode (returningValue ));
110
+ throw new Exception ("LibUSB initialization failed : " +UsbErrorCodes .getErrCode (returningValue ));
111
111
}
112
112
113
113
private void getDeviceList () throws Exception {
114
114
deviceList = new DeviceList ();
115
115
returningValue = LibUsb .getDeviceList (contextNS , deviceList );
116
116
if (returningValue < 0 )
117
- throw new Exception ("Get device list\n Returned : " +UsbErrorCodes .getErrCode (returningValue ));
117
+ throw new Exception ("Can't get device list: " +UsbErrorCodes .getErrCode (returningValue ));
118
118
}
119
119
120
120
private void findDevice () throws Exception {
121
121
// Searching for NS in devices: looking for NS
122
122
DeviceDescriptor descriptor ;
123
123
124
124
for (Device device : deviceList ){
125
- descriptor = new DeviceDescriptor ();
126
- returningValue = LibUsb .getDeviceDescriptor (device , descriptor );
127
- if (returningValue != LibUsb .SUCCESS ){
128
- this .freeDeviceList ();
129
- throw new Exception ("Read file descriptors for USB devices\n Returned: " +UsbErrorCodes .getErrCode (returningValue ));
130
- }
125
+ descriptor = getDeviceDescriptor (device );
126
+
131
127
if ((descriptor .idVendor () == VENDOR_ID ) && descriptor .idProduct () == PRODUCT_ID ){
132
128
deviceNS = device ;
133
129
break ;
@@ -139,6 +135,18 @@ private void findDevice() throws Exception{
139
135
}
140
136
}
141
137
138
+ private DeviceDescriptor getDeviceDescriptor (Device device ) throws Exception {
139
+ DeviceDescriptor descriptor = new DeviceDescriptor ();
140
+
141
+ returningValue = LibUsb .getDeviceDescriptor (device , descriptor );
142
+
143
+ if (returningValue != LibUsb .SUCCESS ){
144
+ this .freeDeviceList ();
145
+ throw new Exception ("Get USB device descriptor failure: " +UsbErrorCodes .getErrCode (returningValue ));
146
+ }
147
+ return descriptor ;
148
+ }
149
+
142
150
private void openDevice () throws Exception {
143
151
// Handle NS device
144
152
handlerNS = new DeviceHandle ();
@@ -150,15 +158,15 @@ private void openDevice() throws Exception{
150
158
handlerNS = null ; // Avoid issue on close();
151
159
if (returningValue == LibUsb .ERROR_ACCESS ) {
152
160
throw new Exception (String .format (
153
- "Open NS USB device\n Returned : %s\n " +
161
+ "Can't open NS USB device: %s\n " +
154
162
"Double check that you have administrator privileges (you're 'root') or check 'udev' rules set for this user (linux only)!\n \n " +
155
163
"Steps to set 'udev' rules:\n " +
156
164
"root # vim /etc/udev/rules.d/99-NS" + ((RCM_VID == VENDOR_ID ) ? "RCM" : "" ) + ".rules\n " +
157
165
"SUBSYSTEM==\" usb\" , ATTRS{idVendor}==\" %04x\" , ATTRS{idProduct}==\" %04x\" , GROUP=\" plugdev\" \n " +
158
166
"root # udevadm control --reload-rules && udevadm trigger\n " , UsbErrorCodes .getErrCode (returningValue ), VENDOR_ID , PRODUCT_ID ));
159
167
}
160
168
else
161
- throw new Exception ("Open NS USB device\n Returned : " +UsbErrorCodes .getErrCode (returningValue ));
169
+ throw new Exception ("Can't open NS USB device: " +UsbErrorCodes .getErrCode (returningValue ));
162
170
}
163
171
164
172
private void freeDeviceList (){
@@ -182,13 +190,13 @@ private void resetDevice(){
182
190
private void setConfiguration (int configuration ) throws Exception {
183
191
returningValue = LibUsb .setConfiguration (handlerNS , configuration );
184
192
if (returningValue != LibUsb .SUCCESS )
185
- throw new Exception ("Set active configuration to device \n Returned : " +UsbErrorCodes .getErrCode (returningValue ));
193
+ throw new Exception ("Unable to set active configuration on device : " +UsbErrorCodes .getErrCode (returningValue ));
186
194
}
187
195
private void claimInterface () throws Exception {
188
196
// Claim interface
189
197
returningValue = LibUsb .claimInterface (handlerNS , DEFAULT_INTERFACE );
190
198
if (returningValue != LibUsb .SUCCESS )
191
- throw new Exception ("Claim interface\n Returned : " +UsbErrorCodes .getErrCode (returningValue ));
199
+ throw new Exception ("Claim interface failure : " +UsbErrorCodes .getErrCode (returningValue ));
192
200
}
193
201
194
202
/**
@@ -222,18 +230,16 @@ public void close(){
222
230
// Try to release interface
223
231
returningValue = LibUsb .releaseInterface (handlerNS , DEFAULT_INTERFACE );
224
232
225
- if (returningValue != LibUsb .SUCCESS )
226
- logPrinter .print ("Release interface\n Returned: " +returningValue +" (sometimes it's not an issue)" , EMsgType .WARNING );
227
- else
228
- logPrinter .print ("Release interface" , EMsgType .PASS );
233
+ if (returningValue != LibUsb .SUCCESS ) {
234
+ logPrinter .print ("Release interface failure: " +
235
+ UsbErrorCodes .getErrCode (returningValue ) +
236
+ " (sometimes it's not an issue)" , EMsgType .WARNING );
237
+ }
229
238
230
239
LibUsb .close (handlerNS );
231
- logPrinter .print ("Requested handler close" , EMsgType .INFO );
232
240
}
233
241
// Close context in the end
234
- if (contextNS != null ) {
242
+ if (contextNS != null )
235
243
LibUsb .exit (contextNS );
236
- logPrinter .print ("Requested context close" , EMsgType .INFO );
237
- }
238
244
}
239
245
}
0 commit comments