@@ -37,9 +37,9 @@ namespace GLogiK
37
37
38
38
using namespace NSGKUtils ;
39
39
40
- libusb_context * libusb ::pContext = nullptr ;
41
- uint8_t libusb ::counter = 0 ;
42
- bool libusb ::status = false ;
40
+ libusb_context * USBInit ::pContext = nullptr ;
41
+ uint8_t USBInit ::counter = 0 ;
42
+ bool USBInit ::status = false ;
43
43
44
44
/*
45
45
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
@@ -51,40 +51,56 @@ bool libusb::status = false;
51
51
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
52
52
*/
53
53
54
- libusb::libusb (void )
54
+ USBInit::USBInit (void )
55
55
{
56
- libusb::counter++;
57
-
58
- if ( ! libusb::status ) {
56
+ if ( ! USBInit::status ) {
59
57
#if DEBUGGING_ON
60
58
LOG (DEBUG3) << " initializing libusb" ;
61
59
#endif
62
- int ret = libusb_init ( &(libusb ::pContext) );
60
+ int ret = libusb_init ( &(USBInit ::pContext) );
63
61
if ( this ->USBError (ret) ) {
64
62
throw GLogiKExcept (" libusb initialization failure" );
65
63
}
66
64
67
- libusb ::status = true ;
65
+ USBInit ::status = true ;
68
66
}
67
+
68
+ USBInit::counter++;
69
69
}
70
70
71
- libusb ::~libusb ( )
71
+ USBInit ::~USBInit ( void )
72
72
{
73
- libusb ::counter--;
73
+ USBInit ::counter--;
74
74
75
- if (libusb ::status and libusb ::counter == 0 ) {
75
+ if (USBInit ::status and USBInit ::counter == 0 ) {
76
76
#if DEBUGGING_ON
77
77
LOG (DEBUG3) << " closing libusb" ;
78
78
#endif
79
- libusb_exit (libusb ::pContext);
80
- libusb ::status = false ;
79
+ libusb_exit (USBInit ::pContext);
80
+ USBInit ::status = false ;
81
81
}
82
82
}
83
83
84
- void libusb::openUSBDevice (USBDevice & device)
84
+ int USBInit::USBError (int errorCode) noexcept
85
+ {
86
+ switch (errorCode) {
87
+ case LIBUSB_SUCCESS:
88
+ break ;
89
+ default :
90
+ std::ostringstream buffer (std::ios_base::app);
91
+ buffer << " libusb error (" << libusb_error_name (errorCode) << " ) : "
92
+ << libusb_strerror ( (libusb_error)errorCode );
93
+ GKSysLog (LOG_ERR, ERROR, buffer.str ());
94
+ break ;
95
+ }
96
+
97
+ return errorCode;
98
+ }
99
+
100
+ void USBInit::seekUSBDevice (USBDevice & device)
85
101
{
86
102
libusb_device **list;
87
- int numDevices = libusb_get_device_list (libusb ::pContext, &(list));
103
+ int numDevices = libusb_get_device_list (USBInit ::pContext, &(list));
88
104
if ( numDevices < 0 ) {
89
105
this ->USBError (numDevices);
90
106
throw GLogiKExcept (" error getting USB devices list" );
@@ -107,18 +123,25 @@ void libusb::openUSBDevice(USBDevice & device)
107
123
throw GLogiKExcept (buffer.str ());
108
124
}
109
125
126
+ libusb_free_device_list (list, 1 );
127
+ }
128
+
129
+ /* -- -- -- */
130
+
131
+ void libusb::openUSBDevice (USBDevice & device)
132
+ {
133
+ /* throws on failure */
134
+ this ->seekUSBDevice (device);
135
+
110
136
int ret = libusb_open ( device._pUSBDevice , &(device._pUSBDeviceHandle ) );
111
137
if ( this ->USBError (ret) ) {
112
- libusb_free_device_list (list, 1 );
113
138
throw GLogiKExcept (" opening device failure" );
114
139
}
115
140
116
141
#if DEBUGGING_ON
117
142
LOG (DEBUG3) << device.getID () << " opened USB device" ;
118
143
#endif
119
144
120
- libusb_free_device_list (list, 1 );
121
-
122
145
try {
123
146
this ->setUSBDeviceActiveConfiguration (device);
124
147
this ->findUSBDeviceInterface (device);
@@ -275,22 +298,6 @@ int libusb::performLCDScreenInterruptTransfer(
275
298
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
276
299
*/
277
300
278
- int libusb::USBError (int errorCode) noexcept
279
- {
280
- switch (errorCode) {
281
- case LIBUSB_SUCCESS:
282
- break ;
283
- default :
284
- std::ostringstream buffer (std::ios_base::app);
285
- buffer << " libusb error (" << libusb_error_name (errorCode) << " ) : "
286
- << libusb_strerror ( (libusb_error)errorCode );
287
- GKSysLog (LOG_ERR, ERROR, buffer.str ());
288
- break ;
289
- }
290
-
291
- return errorCode;
292
- }
293
-
294
301
void libusb::releaseUSBDeviceInterfaces (USBDevice & device) noexcept
295
302
{
296
303
int ret = 0 ;
0 commit comments