From 5d09b0f97a3d3541b0e1b94481f0fdf02ea739db Mon Sep 17 00:00:00 2001 From: bullshit Date: Thu, 7 Aug 2025 11:04:02 +0200 Subject: [PATCH] feat: APC HID implementation Signed-off-by: bullshit --- components/nut_ups/__init__.py | 10 +- components/nut_ups/apc_hid_protocol.cpp | 325 ++++++++++++++++++++++++ components/nut_ups/apc_hid_protocol.h | 40 +++ components/nut_ups/nut_ups.cpp | 16 +- components/nut_ups/nut_ups.h | 2 + components/nut_ups/ups_vendors.h | 19 +- 6 files changed, 400 insertions(+), 12 deletions(-) create mode 100644 components/nut_ups/apc_hid_protocol.cpp create mode 100644 components/nut_ups/apc_hid_protocol.h diff --git a/components/nut_ups/__init__.py b/components/nut_ups/__init__.py index 4ee1695..3299132 100644 --- a/components/nut_ups/__init__.py +++ b/components/nut_ups/__init__.py @@ -19,13 +19,17 @@ CONF_NUT_UPS_ID = "nut_ups_id" # Known UPS vendor IDs for validation KNOWN_VENDOR_IDS = { - 0x0001: "Fiskars", + 0x0001: "Generic/Other", 0x0463: "MGE Office Protection Systems", + 0x050D: "Belkin", 0x051D: "APC", - 0x0665: "Cypress/Other", - 0x06DA: "Phoenixtec Power", + 0x05B8: "SVEN", + 0x0665: "Cypress/Belkin", + 0x06DA: "MGE UPS Systems", 0x0764: "CyberPower", + 0x0925: "Richcomm", 0x09AE: "Tripp Lite", + 0x09D6: "Micropower", } nut_ups_ns = cg.esphome_ns.namespace("nut_ups") diff --git a/components/nut_ups/apc_hid_protocol.cpp b/components/nut_ups/apc_hid_protocol.cpp new file mode 100644 index 0000000..5a4cece --- /dev/null +++ b/components/nut_ups/apc_hid_protocol.cpp @@ -0,0 +1,325 @@ +#include "apc_hid_protocol.h" +#include "esphome/core/log.h" +#include "esphome/core/helpers.h" +#include + +namespace esphome { +namespace nut_ups { + +static const char *const APC_HID_TAG = "nut_ups.apc_hid"; + +// APC HID Usage Pages and Usage IDs (based on NUT project research) +static const uint16_t APC_USAGE_PAGE_UPS = 0x84; +static const uint16_t APC_USAGE_PAGE_BATTERY = 0x85; +static const uint16_t APC_USAGE_PAGE_POWER = 0x80; + +// HID Report IDs commonly used by APC UPS devices +static const uint8_t APC_REPORT_ID_STATUS = 0x01; +static const uint8_t APC_REPORT_ID_BATTERY = 0x02; +static const uint8_t APC_REPORT_ID_VOLTAGE = 0x03; +static const uint8_t APC_REPORT_ID_POWER = 0x04; +static const uint8_t APC_REPORT_ID_CONFIG = 0x05; + +// APC-specific date conversion (hex-as-decimal format) +static std::string convert_apc_date(uint32_t apc_date) { + if (apc_date == 0) return "Unknown"; + + // APC uses hex-as-decimal format, e.g., 0x102202 = 10/22/02 + uint8_t month = (apc_date >> 16) & 0xFF; + uint8_t day = (apc_date >> 8) & 0xFF; + uint8_t year = apc_date & 0xFF; + + // Convert 2-digit year to 4-digit (Y2K handling) + uint16_t full_year = (year <= 69) ? (2000 + year) : (1900 + year); + + char date_str[16]; + snprintf(date_str, sizeof(date_str), "%02d/%02d/%04d", month, day, full_year); + return std::string(date_str); +} + +// APC HID Protocol implementation +ApcHidProtocol::ApcHidProtocol(NutUpsComponent *parent) : UpsProtocolBase(parent) {} + +bool ApcHidProtocol::detect() { + ESP_LOGD(APC_HID_TAG, "Detecting APC HID Protocol..."); + + // Try to read device descriptor or status report + HidReport status_report; + if (read_hid_report(APC_REPORT_ID_STATUS, status_report)) { + ESP_LOGD(APC_HID_TAG, "APC HID response received, report ID: 0x%02X", status_report.report_id); + return true; + } + + ESP_LOGD(APC_HID_TAG, "No APC HID Protocol response"); + return false; +} + +bool ApcHidProtocol::initialize() { + ESP_LOGD(APC_HID_TAG, "Initializing APC HID Protocol..."); + + // Initialize HID communication + if (!init_hid_communication()) { + ESP_LOGE(APC_HID_TAG, "Failed to initialize HID communication"); + return false; + } + + // Read basic device information + read_device_info(); + + ESP_LOGI(APC_HID_TAG, "APC HID Protocol initialized successfully"); + return true; +} + +bool ApcHidProtocol::read_data(UpsData &data) { + ESP_LOGV(APC_HID_TAG, "Reading APC HID UPS data..."); + + bool success = true; + + // Read status report + HidReport status_report; + if (read_hid_report(APC_REPORT_ID_STATUS, status_report)) { + parse_status_report(status_report, data); + } else { + ESP_LOGW(APC_HID_TAG, "Failed to read status report"); + success = false; + } + + // Read battery report + HidReport battery_report; + if (read_hid_report(APC_REPORT_ID_BATTERY, battery_report)) { + parse_battery_report(battery_report, data); + } else { + ESP_LOGW(APC_HID_TAG, "Failed to read battery report"); + } + + // Read voltage/power report + HidReport voltage_report; + if (read_hid_report(APC_REPORT_ID_VOLTAGE, voltage_report)) { + parse_voltage_report(voltage_report, data); + } else { + ESP_LOGW(APC_HID_TAG, "Failed to read voltage report"); + } + + HidReport power_report; + if (read_hid_report(APC_REPORT_ID_POWER, power_report)) { + parse_power_report(power_report, data); + } else { + ESP_LOGV(APC_HID_TAG, "No power report available"); + } + + return success; +} + +bool ApcHidProtocol::init_hid_communication() { + // Set up HID communication parameters + // For APC devices, we typically use standard HID interrupt transfers + return true; +} + +bool ApcHidProtocol::read_hid_report(uint8_t report_id, HidReport &report) { + // Prepare HID Get Report request + std::vector request = { + 0x21, // bmRequestType: Class, Interface, Host-to-device + 0x01, // bRequest: GET_REPORT + report_id, 0x03, // wValue: Report Type (Input=0x01, Output=0x02, Feature=0x03) and Report ID + 0x00, 0x00, // wIndex: Interface + 0x40, 0x00 // wLength: Maximum report length (64 bytes) + }; + + std::vector response; + if (!send_command(request, response, 2000)) { + return false; + } + + if (response.empty()) { + return false; + } + + report.report_id = report_id; + report.data = response; + + return true; +} + +bool ApcHidProtocol::write_hid_report(const HidReport &report) { + std::vector request = {report.report_id}; + request.insert(request.end(), report.data.begin(), report.data.end()); + + std::vector response; + return send_command(request, response, 1000); +} + +void ApcHidProtocol::parse_status_report(const HidReport &report, UpsData &data) { + if (report.data.size() < 4) { + ESP_LOGW(APC_HID_TAG, "Status report too short: %zu bytes", report.data.size()); + return; + } + + uint32_t status_flags = 0; + + // Parse common APC HID status bits + uint8_t status_byte_1 = report.data[0]; + uint8_t status_byte_2 = report.data[1]; + + // Online/AC Present + if (status_byte_1 & 0x04) { + status_flags |= UPS_STATUS_ONLINE; + } + + // On Battery + if (status_byte_1 & 0x10) { + status_flags |= UPS_STATUS_ON_BATTERY; + } + + // Low Battery + if (status_byte_1 & 0x02) { + status_flags |= UPS_STATUS_LOW_BATTERY; + } + + // Replace Battery + if (status_byte_1 & 0x08) { + status_flags |= UPS_STATUS_REPLACE_BATTERY; + } + + // Charging + if (status_byte_2 & 0x01) { + status_flags |= UPS_STATUS_CHARGING; + } + + // Overload + if (status_byte_2 & 0x20) { + status_flags |= UPS_STATUS_OVERLOAD; + } + + // Fault/Shutdown + if (status_byte_2 & 0x40) { + status_flags |= UPS_STATUS_FAULT; + } + + data.status_flags = status_flags; + + ESP_LOGV(APC_HID_TAG, "Status flags: 0x%08X (bytes: 0x%02X 0x%02X)", + status_flags, status_byte_1, status_byte_2); +} + +void ApcHidProtocol::parse_battery_report(const HidReport &report, UpsData &data) { + if (report.data.size() < 8) { + ESP_LOGW(APC_HID_TAG, "Battery report too short: %zu bytes", report.data.size()); + return; + } + + // Battery charge level (typically at offset 0-1, 16-bit value, percentage) + uint16_t battery_raw = (report.data[1] << 8) | report.data[0]; + if (battery_raw <= 100) { + data.battery_level = static_cast(battery_raw); + ESP_LOGV(APC_HID_TAG, "Battery Level: %.1f%%", data.battery_level); + } + + // Runtime remaining (typically at offset 2-3, 16-bit value, minutes) + uint16_t runtime_raw = (report.data[3] << 8) | report.data[2]; + if (runtime_raw > 0 && runtime_raw < 65535) { + data.runtime_minutes = static_cast(runtime_raw); + ESP_LOGV(APC_HID_TAG, "Runtime: %.1f min", data.runtime_minutes); + } + + // Battery voltage (if available, typically at offset 4-5) + if (report.data.size() >= 6) { + uint16_t battery_voltage_raw = (report.data[5] << 8) | report.data[4]; + if (battery_voltage_raw > 0) { + // Convert from raw value to voltage (scaling depends on UPS model) + float battery_voltage = static_cast(battery_voltage_raw) / 100.0f; + ESP_LOGV(APC_HID_TAG, "Battery Voltage: %.2fV", battery_voltage); + } + } +} + +void ApcHidProtocol::parse_voltage_report(const HidReport &report, UpsData &data) { + if (report.data.size() < 8) { + ESP_LOGW(APC_HID_TAG, "Voltage report too short: %zu bytes", report.data.size()); + return; + } + + // Input voltage (typically at offset 0-1, 16-bit value) + uint16_t input_voltage_raw = (report.data[1] << 8) | report.data[0]; + if (input_voltage_raw > 0) { + // Most APC devices report voltage in tenths of volts + data.input_voltage = static_cast(input_voltage_raw) / 10.0f; + ESP_LOGV(APC_HID_TAG, "Input Voltage: %.1fV", data.input_voltage); + } + + // Output voltage (typically at offset 2-3, 16-bit value) + uint16_t output_voltage_raw = (report.data[3] << 8) | report.data[2]; + if (output_voltage_raw > 0) { + data.output_voltage = static_cast(output_voltage_raw) / 10.0f; + ESP_LOGV(APC_HID_TAG, "Output Voltage: %.1fV", data.output_voltage); + } + + // Line frequency (if available, typically at offset 4-5) + if (report.data.size() >= 6) { + uint16_t frequency_raw = (report.data[5] << 8) | report.data[4]; + if (frequency_raw > 0) { + data.frequency = static_cast(frequency_raw) / 10.0f; + ESP_LOGV(APC_HID_TAG, "Frequency: %.1f Hz", data.frequency); + } + } +} + +void ApcHidProtocol::parse_power_report(const HidReport &report, UpsData &data) { + if (report.data.size() < 4) { + ESP_LOGW(APC_HID_TAG, "Power report too short: %zu bytes", report.data.size()); + return; + } + + // Load percentage (typically at offset 0-1, 16-bit value) + uint16_t load_raw = (report.data[1] << 8) | report.data[0]; + if (load_raw <= 100) { + data.load_percent = static_cast(load_raw); + ESP_LOGV(APC_HID_TAG, "Load: %.1f%%", data.load_percent); + } + + // Apparent power (if available, typically at offset 2-3) + if (report.data.size() >= 4) { + uint16_t apparent_power_raw = (report.data[3] << 8) | report.data[2]; + if (apparent_power_raw > 0) { + ESP_LOGV(APC_HID_TAG, "Apparent Power: %u VA", apparent_power_raw); + } + } +} + +void ApcHidProtocol::read_device_info() { + // Read configuration report for device information + HidReport config_report; + if (read_hid_report(APC_REPORT_ID_CONFIG, config_report)) { + parse_device_info_report(config_report); + } +} + +void ApcHidProtocol::parse_device_info_report(const HidReport &report) { + if (report.data.size() < 16) { + ESP_LOGW(APC_HID_TAG, "Device info report too short: %zu bytes", report.data.size()); + return; + } + + // Manufacturer date (APC hex-as-decimal format, typically at offset 8-11) + if (report.data.size() >= 12) { + uint32_t mfr_date = (report.data[11] << 24) | (report.data[10] << 16) | + (report.data[9] << 8) | report.data[8]; + if (mfr_date != 0) { + std::string date_str = convert_apc_date(mfr_date); + ESP_LOGD(APC_HID_TAG, "Manufacture Date: %s", date_str.c_str()); + } + } + + // Battery replacement date (if available, typically at offset 12-15) + if (report.data.size() >= 16) { + uint32_t battery_date = (report.data[15] << 24) | (report.data[14] << 16) | + (report.data[13] << 8) | report.data[12]; + if (battery_date != 0) { + std::string date_str = convert_apc_date(battery_date); + ESP_LOGD(APC_HID_TAG, "Battery Date: %s", date_str.c_str()); + } + } +} + +} // namespace nut_ups +} // namespace esphome \ No newline at end of file diff --git a/components/nut_ups/apc_hid_protocol.h b/components/nut_ups/apc_hid_protocol.h new file mode 100644 index 0000000..97a803d --- /dev/null +++ b/components/nut_ups/apc_hid_protocol.h @@ -0,0 +1,40 @@ +#pragma once + +#include "nut_ups.h" + +namespace esphome { +namespace nut_ups { + +// APC HID Protocol implementation for modern APC UPS devices +class ApcHidProtocol : public UpsProtocolBase { +public: + explicit ApcHidProtocol(NutUpsComponent *parent); + + bool detect() override; + bool initialize() override; + bool read_data(UpsData &data) override; + UpsProtocol get_protocol_type() const override { return PROTOCOL_APC_HID; } + std::string get_protocol_name() const override { return "APC HID Protocol"; } + +private: + struct HidReport { + uint8_t report_id; + std::vector data; + + HidReport() : report_id(0) {} + }; + + bool init_hid_communication(); + bool read_hid_report(uint8_t report_id, HidReport &report); + bool write_hid_report(const HidReport &report); + + void parse_status_report(const HidReport &report, UpsData &data); + void parse_battery_report(const HidReport &report, UpsData &data); + void parse_voltage_report(const HidReport &report, UpsData &data); + void parse_power_report(const HidReport &report, UpsData &data); + void read_device_info(); + void parse_device_info_report(const HidReport &report); +}; + +} // namespace nut_ups +} // namespace esphome \ No newline at end of file diff --git a/components/nut_ups/nut_ups.cpp b/components/nut_ups/nut_ups.cpp index c9949f9..78562b4 100644 --- a/components/nut_ups/nut_ups.cpp +++ b/components/nut_ups/nut_ups.cpp @@ -3,6 +3,9 @@ #include "esphome/core/log.h" #include "esphome/core/application.h" +// Include protocol implementations +#include "apc_hid_protocol.h" + #ifdef USE_ESP32 #include "driver/gpio.h" #include "esp_log.h" @@ -161,7 +164,18 @@ bool NutUpsComponent::detect_ups_protocol() { return false; } - // Try APC Smart Protocol first + // For APC devices (VID 0x051D), try HID protocol first (modern devices) + if (usb_vendor_id_ == 0x051D) { + auto apc_hid_protocol = std::make_unique(this); + if (apc_hid_protocol->detect() && apc_hid_protocol->initialize()) { + active_protocol_ = std::move(apc_hid_protocol); + ups_data_.detected_protocol = PROTOCOL_APC_HID; + ESP_LOGI(TAG, "Detected APC HID Protocol"); + return true; + } + } + + // Try APC Smart Protocol (legacy devices) auto apc_protocol = std::make_unique(this); if (apc_protocol->detect() && apc_protocol->initialize()) { active_protocol_ = std::move(apc_protocol); diff --git a/components/nut_ups/nut_ups.h b/components/nut_ups/nut_ups.h index 2077fa9..74887aa 100644 --- a/components/nut_ups/nut_ups.h +++ b/components/nut_ups/nut_ups.h @@ -56,6 +56,7 @@ namespace esphome { PROTOCOL_UNKNOWN = 0, PROTOCOL_APC_SMART, + PROTOCOL_APC_HID, PROTOCOL_CYBERPOWER_HID, PROTOCOL_GENERIC_HID }; @@ -102,6 +103,7 @@ namespace esphome // Forward declarations class UpsProtocolBase; class ApcSmartProtocol; + class ApcHidProtocol; class CyberPowerProtocol; class GenericHidProtocol; diff --git a/components/nut_ups/ups_vendors.h b/components/nut_ups/ups_vendors.h index cc8f037..8fd8b74 100644 --- a/components/nut_ups/ups_vendors.h +++ b/components/nut_ups/ups_vendors.h @@ -18,16 +18,19 @@ struct UpsVendor { const char* description; }; -// Definitive list of known UPS vendors +// Definitive list of known UPS vendors (based on NUT project driver.list) static constexpr UpsVendor KNOWN_UPS_VENDORS[] = { - {0x051D, "APC", "American Power Conversion"}, - {0x0764, "CyberPower", "CyberPower Systems"}, - {0x09AE, "Tripp Lite", "Tripp Lite"}, - {0x06DA, "MGE UPS Systems", "MGE UPS Systems (now Eaton)"}, - {0x0665, "Cypress/Other", "Cypress or other UPS devices"}, + {0x0001, "Generic/Other", "Generic UPS vendors (multiple brands)"}, {0x0463, "MGE Office Protection Systems", "MGE Office Protection Systems"}, - {0x06da, "Phoenixtec Power", "Phoenixtec Power Co., Ltd."}, - {0x0001, "Fiskars", "Fiskars Corporation (some UPS models)"}, + {0x050D, "Belkin", "Belkin (older USB UPS models)"}, + {0x051D, "APC", "American Power Conversion"}, + {0x05B8, "SVEN", "SVEN UPS devices"}, + {0x0665, "Cypress/Belkin", "Cypress or Belkin UPS devices"}, + {0x06DA, "MGE UPS Systems", "MGE UPS Systems (now Eaton)"}, + {0x0764, "CyberPower", "CyberPower Systems"}, + {0x0925, "Richcomm", "Richcomm Technologies (Digitus, Sweex)"}, + {0x09AE, "Tripp Lite", "Tripp Lite"}, + {0x09D6, "Micropower", "Micropower UPS devices"}, }; static constexpr size_t KNOWN_UPS_VENDORS_COUNT = sizeof(KNOWN_UPS_VENDORS) / sizeof(KNOWN_UPS_VENDORS[0]);