Skip to content

Commit 81e5176

Browse files
committed
Initiate USBInit class.
1 parent 786b188 commit 81e5176

File tree

3 files changed

+62
-44
lines changed

3 files changed

+62
-44
lines changed

src/bin/daemon/USBDevice.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class USBDevice
6666

6767
#if GKLIBUSB
6868
private:
69+
friend class USBInit;
6970
friend class libusb;
7071

7172
std::mutex _libUSBMutex;

src/bin/daemon/libusb.cpp

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ namespace GLogiK
3737

3838
using namespace NSGKUtils;
3939

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;
4343

4444
/*
4545
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
@@ -51,40 +51,56 @@ bool libusb::status = false;
5151
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
5252
*/
5353

54-
libusb::libusb(void)
54+
USBInit::USBInit(void)
5555
{
56-
libusb::counter++;
57-
58-
if( ! libusb::status ) {
56+
if( ! USBInit::status ) {
5957
#if DEBUGGING_ON
6058
LOG(DEBUG3) << "initializing libusb";
6159
#endif
62-
int ret = libusb_init( &(libusb::pContext) );
60+
int ret = libusb_init( &(USBInit::pContext) );
6361
if ( this->USBError(ret) ) {
6462
throw GLogiKExcept("libusb initialization failure");
6563
}
6664

67-
libusb::status = true;
65+
USBInit::status = true;
6866
}
67+
68+
USBInit::counter++;
6969
}
7070

71-
libusb::~libusb()
71+
USBInit::~USBInit(void)
7272
{
73-
libusb::counter--;
73+
USBInit::counter--;
7474

75-
if (libusb::status and libusb::counter == 0) {
75+
if (USBInit::status and USBInit::counter == 0) {
7676
#if DEBUGGING_ON
7777
LOG(DEBUG3) << "closing libusb";
7878
#endif
79-
libusb_exit(libusb::pContext);
80-
libusb::status = false;
79+
libusb_exit(USBInit::pContext);
80+
USBInit::status = false;
8181
}
8282
}
8383

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)
85101
{
86102
libusb_device **list;
87-
int numDevices = libusb_get_device_list(libusb::pContext, &(list));
103+
int numDevices = libusb_get_device_list(USBInit::pContext, &(list));
88104
if( numDevices < 0 ) {
89105
this->USBError(numDevices);
90106
throw GLogiKExcept("error getting USB devices list");
@@ -107,18 +123,25 @@ void libusb::openUSBDevice(USBDevice & device)
107123
throw GLogiKExcept(buffer.str());
108124
}
109125

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+
110136
int ret = libusb_open( device._pUSBDevice, &(device._pUSBDeviceHandle) );
111137
if( this->USBError(ret) ) {
112-
libusb_free_device_list(list, 1);
113138
throw GLogiKExcept("opening device failure");
114139
}
115140

116141
#if DEBUGGING_ON
117142
LOG(DEBUG3) << device.getID() << " opened USB device";
118143
#endif
119144

120-
libusb_free_device_list(list, 1);
121-
122145
try {
123146
this->setUSBDeviceActiveConfiguration(device);
124147
this->findUSBDeviceInterface(device);
@@ -275,22 +298,6 @@ int libusb::performLCDScreenInterruptTransfer(
275298
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
276299
*/
277300

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-
294301
void libusb::releaseUSBDeviceInterfaces(USBDevice & device) noexcept
295302
{
296303
int ret = 0;

src/bin/daemon/libusb.hpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,30 @@
3131
namespace GLogiK
3232
{
3333

34+
class USBInit
35+
{
36+
public:
37+
protected:
38+
USBInit(void);
39+
~USBInit(void);
40+
41+
int USBError(int errorCode) noexcept;
42+
void seekUSBDevice(USBDevice & device);
43+
44+
private:
45+
static libusb_context * pContext;
46+
static uint8_t counter; /* initialized drivers counter */
47+
static bool status; /* is libusb initialized ? */
48+
};
49+
3450
class libusb
51+
: private USBInit
3552
{
3653
public:
3754

3855
protected:
39-
libusb(void);
40-
~libusb(void);
56+
libusb(void) = default;
57+
~libusb(void) = default;
4158

4259
void openUSBDevice(USBDevice & device);
4360
void closeUSBDevice(USBDevice & device) noexcept;
@@ -61,19 +78,12 @@ class libusb
6178
);
6279

6380
private:
64-
static libusb_context * pContext;
65-
static uint8_t counter; /* initialized drivers counter */
66-
static bool status; /* is libusb initialized ? */
67-
6881
void setUSBDeviceActiveConfiguration(USBDevice & device);
6982
void findUSBDeviceInterface(USBDevice & device);
7083
void releaseUSBDeviceInterfaces(USBDevice & device) noexcept;
7184

7285
void detachKernelDriverFromUSBDeviceInterface(USBDevice & device, int numInt);
7386
void attachUSBDeviceInterfacesToKernelDrivers(USBDevice & device) noexcept;
74-
75-
int USBError(int errorCode) noexcept;
76-
7787
};
7888

7989
} // namespace GLogiK

0 commit comments

Comments
 (0)