From 07f8c0943da2071b021f9153274589608bce0d48 Mon Sep 17 00:00:00 2001 From: bullshit Date: Thu, 7 Aug 2025 12:07:16 +0200 Subject: [PATCH] fix(generic-hid): improve based on NUT reference Signed-off-by: bullshit --- components/nut_ups/generic_hid_protocol.cpp | 216 +++++++++++++++----- components/nut_ups/nut_ups.h | 3 + 2 files changed, 171 insertions(+), 48 deletions(-) diff --git a/components/nut_ups/generic_hid_protocol.cpp b/components/nut_ups/generic_hid_protocol.cpp index b915ebb..b3d29d9 100644 --- a/components/nut_ups/generic_hid_protocol.cpp +++ b/components/nut_ups/generic_hid_protocol.cpp @@ -6,19 +6,65 @@ namespace nut_ups { static const char *const GEN_TAG = "nut_ups.generic"; +// Standard HID Usage Pages (based on NUT usbhid-ups.c) +static const uint16_t HID_USAGE_PAGE_POWER_DEVICE = 0x84; +static const uint16_t HID_USAGE_PAGE_BATTERY = 0x85; +static const uint16_t HID_USAGE_PAGE_UPS = 0x84; + +// Common HID Power Device Class Usages +static const uint16_t HID_USAGE_UPS_BATTERY_CHARGE = 0x66; +static const uint16_t HID_USAGE_UPS_RUNTIME_TO_EMPTY = 0x68; +static const uint16_t HID_USAGE_UPS_AC_PRESENT = 0x5A; +static const uint16_t HID_USAGE_UPS_BATTERY_PRESENT = 0x5B; +static const uint16_t HID_USAGE_UPS_CHARGING = 0x44; +static const uint16_t HID_USAGE_UPS_DISCHARGING = 0x45; +static const uint16_t HID_USAGE_UPS_NEED_REPLACEMENT = 0x4B; +static const uint16_t HID_USAGE_UPS_FULLY_CHARGED = 0x49; +static const uint16_t HID_USAGE_UPS_INPUT_VOLTAGE = 0x30; +static const uint16_t HID_USAGE_UPS_OUTPUT_VOLTAGE = 0x31; +static const uint16_t HID_USAGE_UPS_PERCENT_LOAD = 0x35; + +// Common HID Report IDs found in UPS devices +static const uint8_t HID_REPORT_FEATURE = 0x03; +static const uint8_t HID_REPORT_INPUT = 0x01; + bool GenericHidProtocol::detect() { ESP_LOGD(GEN_TAG, "Detecting Generic HID Protocol..."); - // Generic HID detection - try basic USB HID communication - std::vector test_command = {0x01, 0x00}; // Basic HID report request - std::vector response; + // Try HID Get Feature Report (like NUT does) + std::vector get_feature_request = { + 0x21, // bmRequestType: Class, Interface, Host-to-device + 0x01, // bRequest: GET_REPORT + 0x00, HID_REPORT_FEATURE, // wValue: Report Type and Report ID + 0x00, 0x00, // wIndex: Interface + 0x08, 0x00 // wLength: 8 bytes + }; - if (send_command(test_command, response, 1000)) { - ESP_LOGD(GEN_TAG, "Generic HID response received, %d bytes", response.size()); - return true; + std::vector response; + if (send_command(get_feature_request, response, 2000)) { + if (!response.empty()) { + ESP_LOGD(GEN_TAG, "Generic HID Feature Report received, %zu bytes", response.size()); + return true; + } } - ESP_LOGD(GEN_TAG, "No Generic HID response"); + // Fallback: Try Input Report + std::vector get_input_request = { + 0x21, // bmRequestType + 0x01, // bRequest: GET_REPORT + 0x00, HID_REPORT_INPUT, // wValue: Input Report + 0x00, 0x00, // wIndex + 0x08, 0x00 // wLength + }; + + if (send_command(get_input_request, response, 2000)) { + if (!response.empty()) { + ESP_LOGD(GEN_TAG, "Generic HID Input Report received, %zu bytes", response.size()); + return true; + } + } + + ESP_LOGD(GEN_TAG, "No Generic HID response - device may not support standard HID reports"); return false; } @@ -41,52 +87,26 @@ bool GenericHidProtocol::initialize() { bool GenericHidProtocol::read_data(UpsData &data) { ESP_LOGV(GEN_TAG, "Reading Generic HID UPS data..."); - // Generic HID implementation - this provides basic functionality - // for UPS devices that don't match specific protocols - bool success = false; std::vector response; - // Try to read status information using common HID report IDs - for (uint8_t report_id = 1; report_id <= 4; report_id++) { - std::vector request = {report_id, 0x00}; - - if (send_command(request, response, 1000) && !response.empty()) { + // Try different HID report types (like NUT does) + std::vector>> report_requests = { + {"Feature", {0x21, 0x01, 0x00, HID_REPORT_FEATURE, 0x00, 0x00, 0x20, 0x00}}, + {"Input", {0x21, 0x01, 0x00, HID_REPORT_INPUT, 0x00, 0x00, 0x20, 0x00}}, + {"Report1", {0x21, 0x01, 0x01, HID_REPORT_INPUT, 0x00, 0x00, 0x08, 0x00}}, + {"Report2", {0x21, 0x01, 0x02, HID_REPORT_INPUT, 0x00, 0x00, 0x08, 0x00}} + }; + + for (const auto& req : report_requests) { + if (send_command(req.second, response, 1000) && response.size() >= 2) { success = true; + ESP_LOGV(GEN_TAG, "%s report received: %zu bytes", req.first.c_str(), response.size()); - // Parse generic HID response - if (response.size() >= 4) { - // Try to extract meaningful data from generic response - // This is a best-effort approach for unknown devices - - uint8_t status_byte = response[0]; - - // Basic status interpretation - data.status_flags = 0; - if (status_byte & 0x01) data.status_flags |= UPS_STATUS_ONLINE; - if (status_byte & 0x02) data.status_flags |= UPS_STATUS_ON_BATTERY; - if (status_byte & 0x04) data.status_flags |= UPS_STATUS_LOW_BATTERY; - - // Try to extract numeric values (very generic interpretation) - if (response.size() >= 6) { - uint16_t value1 = response[2] | (response[3] << 8); - uint16_t value2 = response[4] | (response[5] << 8); - - // Guess what these values might represent - if (value1 <= 100) { - data.battery_level = static_cast(value1); - } - - if (value2 >= 80 && value2 <= 300) { - data.input_voltage = static_cast(value2); - } - } - - ESP_LOGV(GEN_TAG, "Generic data parsed: Battery=%.1f%%, Status=0x%08X", - data.battery_level, data.status_flags); + if (parse_generic_report(response, data)) { + ESP_LOGV(GEN_TAG, "Successfully parsed %s report", req.first.c_str()); + break; } - - break; // Found some data, stop trying other report IDs } } @@ -95,7 +115,7 @@ bool GenericHidProtocol::read_data(UpsData &data) { data.manufacturer = "Generic"; } if (data.model.empty()) { - data.model = "HID UPS"; + data.model = "HID UPS"; } if (!success) { @@ -111,5 +131,105 @@ bool GenericHidProtocol::read_data(UpsData &data) { return success; } +bool GenericHidProtocol::parse_generic_report(const std::vector& response, UpsData& data) { + if (response.size() < 2) { + return false; + } + + bool found_data = false; + + // NUT-style heuristic parsing - try multiple approaches + + // Approach 1: Look for status bits in first bytes + for (size_t i = 0; i < std::min(response.size(), size_t(4)); ++i) { + uint8_t status_byte = response[i]; + + if (status_byte != 0x00 && status_byte != 0xFF) { + data.status_flags = 0; + + // Common status bit patterns found in various UPS devices + if (status_byte & 0x01) data.status_flags |= UPS_STATUS_ONLINE; + if (status_byte & 0x02) data.status_flags |= UPS_STATUS_ON_BATTERY; + if (status_byte & 0x04) data.status_flags |= UPS_STATUS_LOW_BATTERY; + if (status_byte & 0x08) data.status_flags |= UPS_STATUS_CHARGING; + if (status_byte & 0x10) data.status_flags |= UPS_STATUS_REPLACE_BATTERY; + if (status_byte & 0x20) data.status_flags |= UPS_STATUS_OVERLOAD; + + found_data = true; + ESP_LOGV(GEN_TAG, "Status bits found at offset %zu: 0x%02X -> flags 0x%08X", + i, status_byte, data.status_flags); + break; + } + } + + // Approach 2: Look for percentage values (0-100) in 16-bit words + for (size_t i = 0; i <= response.size() - 2; i += 2) { + uint16_t value_le = response[i] | (response[i+1] << 8); // Little-endian + uint16_t value_be = (response[i] << 8) | response[i+1]; // Big-endian + + // Check if either interpretation gives a valid percentage + if (value_le <= 100) { + data.battery_level = static_cast(value_le); + found_data = true; + ESP_LOGV(GEN_TAG, "Battery level found at offset %zu (LE): %.1f%%", i, data.battery_level); + } else if (value_be <= 100) { + data.battery_level = static_cast(value_be); + found_data = true; + ESP_LOGV(GEN_TAG, "Battery level found at offset %zu (BE): %.1f%%", i, data.battery_level); + } + + // Check for scaled percentages (0-1000 -> 0.0-100.0) + if (value_le <= 1000 && value_le > 100) { + float scaled_battery = static_cast(value_le) / 10.0f; + if (scaled_battery <= 100.0f) { + data.battery_level = scaled_battery; + found_data = true; + ESP_LOGV(GEN_TAG, "Scaled battery level found at offset %zu: %.1f%%", i, data.battery_level); + } + } + } + + // Approach 3: Look for voltage values (80-300V range) + for (size_t i = 0; i <= response.size() - 2; i += 2) { + uint16_t value_le = response[i] | (response[i+1] << 8); + uint16_t value_be = (response[i] << 8) | response[i+1]; + + // Check for voltage values (direct or scaled by 10) + std::vector> voltage_candidates = { + {value_le, 1.0f}, {value_be, 1.0f}, + {value_le, 0.1f}, {value_be, 0.1f} + }; + + for (const auto& candidate : voltage_candidates) { + float voltage = static_cast(candidate.first) * candidate.second; + if (voltage >= 80.0f && voltage <= 300.0f) { + if (std::isnan(data.input_voltage)) { + data.input_voltage = voltage; + found_data = true; + ESP_LOGV(GEN_TAG, "Input voltage found at offset %zu: %.1fV", i, voltage); + } else if (std::isnan(data.output_voltage) && std::abs(voltage - data.input_voltage) > 5.0f) { + data.output_voltage = voltage; + ESP_LOGV(GEN_TAG, "Output voltage found at offset %zu: %.1fV", i, voltage); + } + break; + } + } + } + + // Approach 4: Look for runtime values (reasonable minute ranges) + for (size_t i = 0; i <= response.size() - 2; i += 2) { + uint16_t value_le = response[i] | (response[i+1] << 8); + + // Runtime typically 0-999 minutes for most UPS devices + if (value_le > 0 && value_le <= 999) { + data.runtime_minutes = static_cast(value_le); + found_data = true; + ESP_LOGV(GEN_TAG, "Runtime found at offset %zu: %.1f min", i, data.runtime_minutes); + } + } + + return found_data; +} + } // namespace nut_ups } // namespace esphome \ No newline at end of file diff --git a/components/nut_ups/nut_ups.h b/components/nut_ups/nut_ups.h index ad478d3..5f0f287 100644 --- a/components/nut_ups/nut_ups.h +++ b/components/nut_ups/nut_ups.h @@ -303,6 +303,9 @@ namespace esphome bool read_data(UpsData &data) override; UpsProtocol get_protocol_type() const override { return PROTOCOL_GENERIC_HID; } std::string get_protocol_name() const override { return "Generic HID"; } + + private: + bool parse_generic_report(const std::vector& response, UpsData& data); }; } // namespace nut_ups