|
| 1 | +/* |
| 2 | + * |
| 3 | + * This file is part of GLogiK project. |
| 4 | + * GLogiK, daemon to handle special features on gaming keyboards |
| 5 | + * Copyright (C) 2016-2021 Fabrice Delliaux <[email protected]> |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +#include "lib/utils/utils.hpp" |
| 23 | + |
| 24 | +#include "usbinit.hpp" |
| 25 | + |
| 26 | +namespace GLogiK |
| 27 | +{ |
| 28 | + |
| 29 | +using namespace NSGKUtils; |
| 30 | + |
| 31 | +libusb_context * USBInit::pContext = nullptr; |
| 32 | +uint8_t USBInit::counter = 0; |
| 33 | +bool USBInit::status = false; |
| 34 | + |
| 35 | +/* |
| 36 | + * --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- |
| 37 | + * --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- |
| 38 | + * |
| 39 | + * === protected === protected === protected === protected === |
| 40 | + * |
| 41 | + * --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- |
| 42 | + * --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- |
| 43 | + */ |
| 44 | + |
| 45 | +USBInit::USBInit(void) |
| 46 | +{ |
| 47 | + if( ! USBInit::status ) { |
| 48 | +#if DEBUGGING_ON |
| 49 | + LOG(DEBUG3) << "initializing libusb"; |
| 50 | +#endif |
| 51 | + int ret = libusb_init( &(USBInit::pContext) ); |
| 52 | + if ( this->USBError(ret) ) { |
| 53 | + throw GLogiKExcept("libusb initialization failure"); |
| 54 | + } |
| 55 | + |
| 56 | + USBInit::status = true; |
| 57 | + } |
| 58 | + |
| 59 | + USBInit::counter++; |
| 60 | +} |
| 61 | + |
| 62 | +USBInit::~USBInit(void) |
| 63 | +{ |
| 64 | + USBInit::counter--; |
| 65 | + |
| 66 | + if (USBInit::status and USBInit::counter == 0) { |
| 67 | +#if DEBUGGING_ON |
| 68 | + LOG(DEBUG3) << "closing libusb"; |
| 69 | +#endif |
| 70 | + libusb_exit(USBInit::pContext); |
| 71 | + USBInit::status = false; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +int USBInit::USBError(int errorCode) noexcept |
| 76 | +{ |
| 77 | + switch(errorCode) { |
| 78 | + case LIBUSB_SUCCESS: |
| 79 | + break; |
| 80 | + default: |
| 81 | + std::ostringstream buffer(std::ios_base::app); |
| 82 | + buffer << "libusb error (" << libusb_error_name(errorCode) << ") : " |
| 83 | + << libusb_strerror( (libusb_error)errorCode ); |
| 84 | + GKSysLog(LOG_ERR, ERROR, buffer.str()); |
| 85 | + break; |
| 86 | + } |
| 87 | + |
| 88 | + return errorCode; |
| 89 | +} |
| 90 | + |
| 91 | +void USBInit::seekUSBDevice(USBDevice & device) |
| 92 | +{ |
| 93 | + libusb_device **list; |
| 94 | + int numDevices = libusb_get_device_list(USBInit::pContext, &(list)); |
| 95 | + if( numDevices < 0 ) { |
| 96 | + this->USBError(numDevices); |
| 97 | + throw GLogiKExcept("error getting USB devices list"); |
| 98 | + } |
| 99 | + |
| 100 | + for (int i = 0; i < numDevices; ++i) { |
| 101 | + device._pUSBDevice = list[i]; |
| 102 | + if( libusb_get_bus_number(device._pUSBDevice) == device.getBus() and |
| 103 | + libusb_get_device_address(device._pUSBDevice) == device.getNum() ) { |
| 104 | + break; |
| 105 | + } |
| 106 | + device._pUSBDevice = nullptr; |
| 107 | + } |
| 108 | + |
| 109 | + if( device._pUSBDevice == nullptr ) { |
| 110 | + std::ostringstream buffer(std::ios_base::app); |
| 111 | + buffer << "libusb cannot find device " << device.getNum() |
| 112 | + << " on bus " << device.getBus(); |
| 113 | + libusb_free_device_list(list, 1); |
| 114 | + throw GLogiKExcept(buffer.str()); |
| 115 | + } |
| 116 | + |
| 117 | + libusb_free_device_list(list, 1); |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace GLogiK |
| 121 | + |
0 commit comments