[HID] Portal: Simplified device searching procedure

This commit is contained in:
Gliniak
2026-04-26 13:26:30 +02:00
parent dc561e4df5
commit cff0773e00
4 changed files with 15 additions and 43 deletions

View File

@@ -26,53 +26,31 @@ HardwarePortal::~HardwarePortal() {
libusb_exit(context_); libusb_exit(context_);
} }
bool HardwarePortal::IsConnected() { return handle_ != nullptr; }
void HardwarePortal::OpenDevice() { void HardwarePortal::OpenDevice() {
if (!context_ || connected_) { if (!context_ || handle_) {
return; return;
} }
libusb_device** devs; // Allow only one portal device at the time.
const ssize_t cnt = libusb_get_device_list(context_, &devs); for (const auto& entry : kPortalVendorProductIdList) {
if (cnt < 0) { handle_ =
// No device available... It might appear later. libusb_open_device_with_vid_pid(context_, entry.first, entry.second);
return; if (handle_) {
} libusb_claim_interface(handle_, 0);
for (ssize_t i = 0; i < cnt; ++i) {
libusb_device* dev = devs[i];
libusb_device_descriptor desc;
if (libusb_get_device_descriptor(dev, &desc) == 0) {
// Check if this device matches the target Vendor ID and Product ID.
// Small limitation. We're only supporting one device being connected at
// the time.
for (const auto& entry : kPortalVendorProductIdList) {
if (desc.idVendor == entry.first && desc.idProduct == entry.second) {
if (libusb_open(dev, &handle_) == 0) {
libusb_claim_interface(handle_, 0);
connected_ = true;
// We found device. No need to go thorugh remaining IDs.
break;
}
}
}
}
// We found device. No need to go thorugh remaining devices.
if (connected_) {
break; break;
} }
} }
libusb_free_device_list(devs, 0);
} }
void HardwarePortal::CloseDevice() { void HardwarePortal::CloseDevice() {
if (!connected_) { if (!handle_) {
return; return;
} }
libusb_release_interface(handle_, 0); libusb_release_interface(handle_, 0);
libusb_close(handle_); libusb_close(handle_);
connected_ = false;
handle_ = nullptr; handle_ = nullptr;
} }

View File

@@ -28,6 +28,8 @@ class HardwarePortal final : public Portal {
HardwarePortal(); HardwarePortal();
~HardwarePortal() override; ~HardwarePortal() override;
virtual bool IsConnected() override;
virtual void OnDeviceArrival() override; virtual void OnDeviceArrival() override;
virtual void OnDeviceRemoval() override; virtual void OnDeviceRemoval() override;

View File

@@ -15,16 +15,11 @@ namespace hid {
Portal::Portal() {} Portal::Portal() {}
Portal::~Portal() {} Portal::~Portal() {}
bool Portal::IsConnected() {
std::lock_guard<xe_mutex> guard(lock_);
return connected_;
}
X_STATUS Portal::Read(std::span<uint8_t> data, uint32_t& bytes_read, X_STATUS Portal::Read(std::span<uint8_t> data, uint32_t& bytes_read,
uint16_t& state) { uint16_t& state) {
std::lock_guard<xe_mutex> guard(lock_); std::lock_guard<xe_mutex> guard(lock_);
if (!connected_) { if (!IsConnected()) {
return X_ERROR_DEVICE_NOT_CONNECTED; return X_ERROR_DEVICE_NOT_CONNECTED;
} }
@@ -55,7 +50,7 @@ X_STATUS Portal::Read(std::span<uint8_t> data, uint32_t& bytes_read,
X_STATUS Portal::Write(std::span<uint8_t> data) { X_STATUS Portal::Write(std::span<uint8_t> data) {
std::lock_guard<xe_mutex> guard(lock_); std::lock_guard<xe_mutex> guard(lock_);
if (!connected_) { if (!IsConnected()) {
return X_ERROR_DEVICE_NOT_CONNECTED; return X_ERROR_DEVICE_NOT_CONNECTED;
} }

View File

@@ -25,7 +25,7 @@ class Portal {
Portal(); Portal();
virtual ~Portal(); virtual ~Portal();
bool IsConnected(); virtual bool IsConnected() = 0;
virtual X_STATUS Read(std::span<uint8_t> data, uint32_t& bytes_read, virtual X_STATUS Read(std::span<uint8_t> data, uint32_t& bytes_read,
uint16_t& state); uint16_t& state);
@@ -34,9 +34,6 @@ class Portal {
virtual void OnDeviceArrival() = 0; virtual void OnDeviceArrival() = 0;
virtual void OnDeviceRemoval() = 0; virtual void OnDeviceRemoval() = 0;
protected:
bool connected_ = false;
private: private:
virtual void OpenDevice() = 0; virtual void OpenDevice() = 0;
virtual void CloseDevice() = 0; virtual void CloseDevice() = 0;