aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2023-01-12 02:01:36 +0300
committerEvgeny Zinoviev <me@ch1p.io>2023-01-12 02:01:36 +0300
commit5d9e8289037675ee9f33ee62a3b9e2c25e33837a (patch)
treefe0ea9da36b42002fde03baab92ab1bbb6d61ff2
parentf95f3fe3d0923ace5991559a0e08885aad17b3ce (diff)
usb: support specifying device path (e.g. /dev/hidrawX)
-rw-r--r--src/common.h1
-rw-r--r--src/inverterctl.cc17
-rw-r--r--src/inverterd.cc18
-rw-r--r--src/voltronic/device.h3
-rw-r--r--src/voltronic/usb_device.cc16
5 files changed, 48 insertions, 7 deletions
diff --git a/src/common.h b/src/common.h
index c0b5a36..686893b 100644
--- a/src/common.h
+++ b/src/common.h
@@ -24,6 +24,7 @@ enum {
LO_DEVICE_ERROR_LIMIT,
LO_USB_VENDOR_ID,
LO_USB_DEVICE_ID,
+ LO_USB_PATH,
LO_SERIAL_NAME,
LO_SERIAL_BAUD_RATE,
LO_SERIAL_DATA_BITS,
diff --git a/src/inverterctl.cc b/src/inverterctl.cc
index f4eb51c..f87d6ea 100644
--- a/src/inverterctl.cc
+++ b/src/inverterctl.cc
@@ -78,6 +78,9 @@ static void usage(const char* progname) {
"USB device options:\n"
" --usb-vendor-id <ID>: Vendor ID (default: " << std::setw(4) << voltronic::USBDevice::VENDOR_ID << ")\n"
" --usb-device-id <ID>: Device ID (default: " << std::setw(4) << voltronic::USBDevice::PRODUCT_ID << ")\n"
+ "\n"
+ " Alternatively, you can specify device path (e.g., /dev/hidraw0):\n"
+ " --usb-path <PATH>: Device path\n"
"\n";
std::cout.flags(f);
std::cout <<
@@ -262,6 +265,7 @@ int main(int argc, char *argv[]) {
u16 usbVendorId = voltronic::USBDevice::VENDOR_ID;
u16 usbDeviceId = voltronic::USBDevice::PRODUCT_ID;
+ std::string usbDevicePath {};
std::string serialDeviceName(voltronic::SerialDevice::DEVICE_NAME);
voltronic::SerialBaudRate serialBaudRate = voltronic::SerialDevice::BAUD_RATE;
@@ -280,6 +284,7 @@ int main(int argc, char *argv[]) {
{"device", required_argument, nullptr, LO_DEVICE},
{"usb-vendor-id", required_argument, nullptr, LO_USB_VENDOR_ID},
{"usb-device-id", required_argument, nullptr, LO_USB_DEVICE_ID},
+ {"usb-path", required_argument, nullptr, LO_USB_PATH},
{"serial-name", required_argument, nullptr, LO_SERIAL_NAME},
{"serial-baud-rate", required_argument, nullptr, LO_SERIAL_BAUD_RATE},
{"serial-data-bits", required_argument, nullptr, LO_SERIAL_DATA_BITS},
@@ -357,6 +362,10 @@ int main(int argc, char *argv[]) {
}
break;
+ case LO_USB_PATH:
+ usbDevicePath = arg;
+ break;
+
case LO_SERIAL_NAME:
serialDeviceName = arg;
break;
@@ -442,8 +451,12 @@ int main(int argc, char *argv[]) {
std::shared_ptr<voltronic::Device> dev;
switch (deviceType) {
case DeviceType::USB:
- dev = std::shared_ptr<voltronic::Device>(new voltronic::USBDevice(usbVendorId,
- usbDeviceId));
+ if (usbDevicePath.empty()) {
+ dev = std::shared_ptr<voltronic::Device>(new voltronic::USBDevice(usbVendorId,
+ usbDeviceId));
+ } else {
+ dev = std::shared_ptr<voltronic::Device>(new voltronic::USBDevice(usbDevicePath));
+ }
break;
case DeviceType::Pseudo:
diff --git a/src/inverterd.cc b/src/inverterd.cc
index 3373c54..82a9717 100644
--- a/src/inverterd.cc
+++ b/src/inverterd.cc
@@ -42,7 +42,10 @@ static void usage(const char* progname) {
std::cout << std::hex << std::setfill('0') <<
"USB device options:\n"
" --usb-vendor-id <ID>: Vendor ID (default: " << std::setw(4) << voltronic::USBDevice::VENDOR_ID << ")\n"
- " --usb-device-id <ID>: Device ID (default: " << std::setw(4) << voltronic::USBDevice::PRODUCT_ID << ")\n";
+ " --usb-device-id <ID>: Device ID (default: " << std::setw(4) << voltronic::USBDevice::PRODUCT_ID << ")\n"
+ "\n"
+ " Alternatively, you can specify device path (e.g., /dev/hidraw0):\n"
+ " --usb-path <PATH>: Device path\n";
std::cout.flags(f);
std::cout << "\n"
@@ -73,6 +76,7 @@ int main(int argc, char *argv[]) {
unsigned short usbVendorId = voltronic::USBDevice::VENDOR_ID;
unsigned short usbDeviceId = voltronic::USBDevice::PRODUCT_ID;
+ std::string usbDevicePath {};
std::string serialDeviceName(voltronic::SerialDevice::DEVICE_NAME);
voltronic::SerialBaudRate serialBaudRate = voltronic::SerialDevice::BAUD_RATE;
@@ -92,6 +96,7 @@ int main(int argc, char *argv[]) {
{"device-error-limit", required_argument, nullptr, LO_DEVICE_ERROR_LIMIT},
{"usb-vendor-id", required_argument, nullptr, LO_USB_VENDOR_ID},
{"usb-device-id", required_argument, nullptr, LO_USB_DEVICE_ID},
+ {"usb-path", required_argument, nullptr, LO_USB_PATH},
{"serial-name", required_argument, nullptr, LO_SERIAL_NAME},
{"serial-baud-rate", required_argument, nullptr, LO_SERIAL_BAUD_RATE},
{"serial-data-bits", required_argument, nullptr, LO_SERIAL_DATA_BITS},
@@ -176,6 +181,10 @@ int main(int argc, char *argv[]) {
}
break;
+ case LO_USB_PATH:
+ usbDevicePath = arg;
+ break;
+
case LO_SERIAL_NAME:
serialDeviceName = arg;
break;
@@ -243,7 +252,12 @@ int main(int argc, char *argv[]) {
try {
switch (deviceType) {
case DeviceType::USB:
- dev = std::shared_ptr<voltronic::Device>(new voltronic::USBDevice(usbVendorId, usbDeviceId));
+ if (usbDevicePath.empty()) {
+ dev = std::shared_ptr<voltronic::Device>(new voltronic::USBDevice(usbVendorId,
+ usbDeviceId));
+ } else {
+ dev = std::shared_ptr<voltronic::Device>(new voltronic::USBDevice(usbDevicePath));
+ }
break;
case DeviceType::Pseudo:
diff --git a/src/voltronic/device.h b/src/voltronic/device.h
index 6584585..00c6628 100644
--- a/src/voltronic/device.h
+++ b/src/voltronic/device.h
@@ -72,8 +72,11 @@ public:
static u16 GET_HID_REPORT_SIZE(size_t size);
USBDevice(u16 vendorId, u16 productId);
+ explicit USBDevice(const std::string& path);
~USBDevice();
+ static inline void init();
+
size_t read(u8* buf, size_t bufSize) override;
size_t write(const u8* data, size_t dataSize) override;
};
diff --git a/src/voltronic/usb_device.cc b/src/voltronic/usb_device.cc
index ffb94d0..38fe379 100644
--- a/src/voltronic/usb_device.cc
+++ b/src/voltronic/usb_device.cc
@@ -12,14 +12,24 @@
namespace voltronic {
USBDevice::USBDevice(u16 vendorId, u16 productId) {
- if (hid_init() != 0)
- throw DeviceError("hidapi initialization failure");
-
+ init();
device_ = hid_open(vendorId, productId, nullptr);
if (!device_)
throw DeviceError("failed to create hidapi device");
}
+USBDevice::USBDevice(const std::string& path) {
+ init();
+ device_ = hid_open_path(path.c_str());
+ if (!device_)
+ throw DeviceError("failed to create hidapi device");
+}
+
+void USBDevice::init() {
+ if (hid_init() != 0)
+ throw DeviceError("hidapi initialization failure");
+}
+
USBDevice::~USBDevice() {
if (device_)
hid_close(device_);