Skip to content

Commit

Permalink
UsbHidDevice: add connect() with pre opened hid device
Browse files Browse the repository at this point in the history
Signed-off-by: Norbert Takacs <norberttak@gmail.com>
  • Loading branch information
norberttak committed Mar 31, 2024
1 parent 3ae99f7 commit d5d639e
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/core/UsbHidDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,25 @@ int UsbHidDevice::connect()
ref_count++;

if (dev_info->next) {
Logger(TLogLevel::logWARNING) << "found more than one device with vid=" << vid << " pid=" << pid << " Only the first device is used now. serial=" << dev_info->serial_number << std::endl;
Logger(TLogLevel::logWARNING) << "found more than one device with vid=" << vid << " pid=" << pid << " Only the first device is used now" << std::endl;
}

Logger(TLogLevel::logDEBUG) << "device opened: vid=" << vid << " pid=" << pid << " product=" << dev_info->product_string << " serial=" << dev_info->serial_number << std::endl;
Logger(TLogLevel::logDEBUG) << "device opened: vid=" << vid << " pid=" << pid << std::endl;

hid_free_enumeration(dev_info);

return EXIT_SUCCESS;
}

int UsbHidDevice::connect(hid_device* _device_handle)
{
ref_count++;
device_handle = _device_handle;
Logger(TLogLevel::logDEBUG) << "device connect: vid=" << vid << " pid=" << pid << std::endl;

return EXIT_SUCCESS;
}

void UsbHidDevice::start()
{
Logger(TLogLevel::logDEBUG) << "UsbHidDevice::start" << std::endl;
Expand Down
1 change: 1 addition & 0 deletions src/core/UsbHidDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class UsbHidDevice : public Device
unsigned short vid = 0;
unsigned short pid = 0;
int connect();
int connect(hid_device* _device_handle);
virtual void start();
virtual void stop(int time_out);
void release();
Expand Down
21 changes: 21 additions & 0 deletions src/devices/arduino-homecockpit/ArduinoHomeCockpit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,27 @@ int ArduinoHomeCockpit::read_board_configuration(std::string file_name, unsigned
return exit_status;
}

int ArduinoHomeCockpit::connect(hid_device* _device_handle)
{
if (_device_handle == NULL)
{
if (UsbHidDevice::connect() != EXIT_SUCCESS)
{
Logger(TLogLevel::logERROR) << "Arduin Home Cockpit connect. Error during connect" << std::endl;
return EXIT_FAILURE;
}
}
else
{
if (UsbHidDevice::connect(_device_handle) != EXIT_SUCCESS)
{
Logger(TLogLevel::logERROR) << "Arduin Home Cockpit connect. Error during connect" << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}

int ArduinoHomeCockpit::connect()
{
return UsbHidDevice::connect();
Expand Down
1 change: 1 addition & 0 deletions src/devices/arduino-homecockpit/ArduinoHomeCockpit.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ArduinoHomeCockpit :public UsbHidDevice
const std::string TOKEN_DISPLAY = "display:id=\"([a-zA-Z0-9_-]+)\",width=([0-9]+)";
public:
ArduinoHomeCockpit(DeviceConfiguration& config);
int connect(hid_device* _device_handle);
int connect();
void start();
void stop(int timeout);
Expand Down
21 changes: 21 additions & 0 deletions src/devices/saitek-multi/SaitekMultiPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ SaitekMultiPanel::SaitekMultiPanel(DeviceConfiguration& config) :UsbHidDevice(co
}
}

int SaitekMultiPanel::connect(hid_device* _device_handle)
{
if (_device_handle == NULL)
{
if (UsbHidDevice::connect() != EXIT_SUCCESS)
{
Logger(TLogLevel::logERROR) << "SaitekMultiPanel connect. Error during connect" << std::endl;
return EXIT_FAILURE;
}
}
else
{
if (UsbHidDevice::connect(_device_handle) != EXIT_SUCCESS)
{
Logger(TLogLevel::logERROR) << "SaitekMultiPanel connect. Error during connect" << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}

int SaitekMultiPanel::connect()
{
unsigned char buff[WRITE_BUFFER_SIZE];
Expand Down
1 change: 1 addition & 0 deletions src/devices/saitek-multi/SaitekMultiPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SaitekMultiPanel : public UsbHidDevice
std::vector<PanelDisplay> multi_displays;
public:
SaitekMultiPanel(DeviceConfiguration &config);
int connect(hid_device* _device_handle);
int connect();
void start();
void stop(int timeout);
Expand Down
22 changes: 19 additions & 3 deletions src/devices/saitek-radio/SaitekRadioPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,26 @@ SaitekRadioPanel::SaitekRadioPanel(DeviceConfiguration& config) :UsbHidDevice(co

int SaitekRadioPanel::connect()
{
if (UsbHidDevice::connect() != EXIT_SUCCESS)
return connect(NULL);
}

int SaitekRadioPanel::connect(hid_device* _device_handle)
{
if (_device_handle == NULL)
{
Logger(TLogLevel::logERROR) << "SaitekRadioPanel connect. Error during connect" << std::endl;
return EXIT_FAILURE;
if (UsbHidDevice::connect() != EXIT_SUCCESS)
{
Logger(TLogLevel::logERROR) << "SaitekRadioPanel connect. Error during connect" << std::endl;
return EXIT_FAILURE;
}
}
else
{
if (UsbHidDevice::connect(_device_handle) != EXIT_SUCCESS)
{
Logger(TLogLevel::logERROR) << "SaitekRadioPanel connect. Error during connect" << std::endl;
return EXIT_FAILURE;
}
}

read_device(read_buffer, read_buffer_size);
Expand Down
1 change: 1 addition & 0 deletions src/devices/saitek-radio/SaitekRadioPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SaitekRadioPanel : public UsbHidDevice
public:
SaitekRadioPanel(DeviceConfiguration& config);
int connect();
int connect(hid_device* _device_handle);
void start();
void stop(int timeout);
};
21 changes: 21 additions & 0 deletions src/devices/saitek-switch/SaitekSwitchPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ SaitekSwitchPanel::SaitekSwitchPanel(DeviceConfiguration& config) :UsbHidDevice(
register_lights(switch_lights);
}

int SaitekSwitchPanel::connect(hid_device* _device_handle)
{
if (_device_handle == NULL)
{
if (UsbHidDevice::connect() != EXIT_SUCCESS)
{
Logger(TLogLevel::logERROR) << "SaitekSwitchPanel connect. Error during connect" << std::endl;
return EXIT_FAILURE;
}
}
else
{
if (UsbHidDevice::connect(_device_handle) != EXIT_SUCCESS)
{
Logger(TLogLevel::logERROR) << "SaitekSwitchPanel connect. Error during connect" << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}

int SaitekSwitchPanel::connect()
{
unsigned char buff[WRITE_BUFFER_SIZE];
Expand Down
1 change: 1 addition & 0 deletions src/devices/saitek-switch/SaitekSwitchPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SaitekSwitchPanel : public UsbHidDevice
std::vector<PanelLight> switch_lights;
public:
SaitekSwitchPanel(DeviceConfiguration& config);
int connect(hid_device* _device_handle);
int connect();
void start();
void stop(int timeout);
Expand Down
21 changes: 21 additions & 0 deletions src/devices/trc-1000/TRC1000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ void TRC1000::thread_func()
Logger(TLogLevel::logDEBUG) << "TRC1000 thread_func: exit vid=" << vid << " pid=" << pid << std::endl;
}

int TRC1000::connect(hid_device* _device_handle)
{
if (_device_handle == NULL)
{
if (UsbHidDevice::connect() != EXIT_SUCCESS)
{
Logger(TLogLevel::logERROR) << "TRC1000 connect. Error during connect" << std::endl;
return EXIT_FAILURE;
}
}
else
{
if (UsbHidDevice::connect(_device_handle) != EXIT_SUCCESS)
{
Logger(TLogLevel::logERROR) << "TRC1000 connect. Error during connect" << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}

int TRC1000::connect()
{
if (UsbHidDevice::connect() != EXIT_SUCCESS)
Expand Down
1 change: 1 addition & 0 deletions src/devices/trc-1000/TRC1000.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TRC1000 : public UsbHidDevice
TRC1000(DeviceConfiguration& config, int _read_buffer_size, int _write_buffer_size);
~TRC1000();
virtual void thread_func();
int connect(hid_device* _device_handle);
int connect();
void start();
void stop(int timeout);
Expand Down

0 comments on commit d5d639e

Please sign in to comment.