Public Access
refactor(ups-hid): fix values in apc and cyberpower protocol
Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
4 files changed
+136
-42
No files matched your search
@@ -1235,35 +1235,34 @@ void ApcHidProtocol::parse_ups_delay_shutdown_report(const HidReport &report, Up
|
||||
}
|
||||
|
||||
std::string ApcHidProtocol::convert_apc_date(uint16_t date_value) {
|
||||
// Convert APC date format to "YYYY/MM/DD" string
|
||||
// Based on NUT libhid date conversion: 19257 → "2017/09/25"
|
||||
// This appears to be days since epoch (1970-01-01)
|
||||
ESP_LOGD(APC_HID_TAG, "Converting USB PDC date value: %u (0x%04X)", date_value, date_value);
|
||||
|
||||
if (date_value == 0) {
|
||||
return "not set";
|
||||
}
|
||||
|
||||
// Calculate date from days since epoch
|
||||
// 19257 days from 1970-01-01 = 2017-09-25
|
||||
const uint32_t days_since_epoch = date_value;
|
||||
const uint32_t seconds_per_day = 24 * 60 * 60;
|
||||
const uint32_t epoch_timestamp = days_since_epoch * seconds_per_day;
|
||||
// USB Power Device Class date format (per USB PDC spec v1.1, page 38):
|
||||
// ManufacturerDate: The date the pack was manufactured in a packed integer.
|
||||
// The date is packed as: (year – 1980)*512 + month*32 + day
|
||||
// This matches NUT's date_conversion_fun() in usbhid-ups.c
|
||||
|
||||
// Convert to calendar date
|
||||
time_t timestamp = epoch_timestamp;
|
||||
struct tm *date_tm = gmtime(×tamp);
|
||||
// Extract date components using bit operations (matching NUT implementation)
|
||||
int day = date_value & 0x1F; // Last 5 bits (0-31)
|
||||
int month = (date_value >> 5) & 0x0F; // Next 4 bits (0-15, but valid 1-12)
|
||||
int year = 1980 + (date_value >> 9); // Upper bits shifted right 9 positions + 1980
|
||||
|
||||
if (date_tm == nullptr) {
|
||||
ESP_LOGW(APC_HID_TAG, "Failed to convert APC date value %d", date_value);
|
||||
return "unknown";
|
||||
ESP_LOGD(APC_HID_TAG, "USB PDC date extraction: day=%d, month=%d, year=%d", day, month, year);
|
||||
|
||||
// Validate date components
|
||||
if (month < 1 || month > 12 || day < 1 || day > 31) {
|
||||
ESP_LOGW(APC_HID_TAG, "Invalid date components: year=%d, month=%d, day=%d", year, month, day);
|
||||
return "invalid date";
|
||||
}
|
||||
|
||||
char date_str[16];
|
||||
snprintf(date_str, sizeof(date_str), "%04d/%02d/%02d",
|
||||
1900 + date_tm->tm_year,
|
||||
1 + date_tm->tm_mon,
|
||||
date_tm->tm_mday);
|
||||
snprintf(date_str, sizeof(date_str), "%04d/%02d/%02d", year, month, day);
|
||||
|
||||
ESP_LOGD(APC_HID_TAG, "Converted USB PDC date %u to %s", date_value, date_str);
|
||||
return std::string(date_str);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include "freertos/task.h"
|
||||
#include "esp_err.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
namespace esphome {
|
||||
namespace ups_hid {
|
||||
@@ -61,6 +63,13 @@ bool CyberPowerProtocol::read_data(UpsData &data) {
|
||||
bool success = false;
|
||||
|
||||
// Core sensors (essential for operation)
|
||||
// Read battery capacity limits (Report 0x07) - includes FullChargeCapacity for battery.status
|
||||
HidReport battery_capacity_report;
|
||||
if (read_hid_report(BATTERY_CAPACITY_REPORT_ID, battery_capacity_report)) {
|
||||
parse_battery_capacity_report(battery_capacity_report, data);
|
||||
success = true;
|
||||
}
|
||||
|
||||
// Read battery level and runtime (Report 0x08)
|
||||
HidReport battery_runtime_report;
|
||||
if (read_hid_report(BATTERY_RUNTIME_REPORT_ID, battery_runtime_report)) {
|
||||
@@ -414,8 +423,9 @@ void CyberPowerProtocol::parse_battery_voltage_nominal_report(const HidReport &r
|
||||
}
|
||||
|
||||
// NUT debug shows: Report 0x09, Value: 24 (ConfigVoltage)
|
||||
// Some CyberPower models report in decivolts (240 = 24.0V)
|
||||
uint8_t voltage_raw = report.data[1];
|
||||
data.battery_voltage_nominal = static_cast<float>(voltage_raw);
|
||||
data.battery_voltage_nominal = static_cast<float>(voltage_raw) / 10.0f;
|
||||
|
||||
ESP_LOGD(CP_TAG, "Battery voltage nominal: %.0fV (raw: 0x%02X = %d)",
|
||||
data.battery_voltage_nominal, voltage_raw, voltage_raw);
|
||||
@@ -490,10 +500,17 @@ void CyberPowerProtocol::parse_delay_shutdown_report(const HidReport &report, Up
|
||||
}
|
||||
|
||||
// NUT debug shows: Report 0x15, Value: -60 (DelayBeforeShutdown)
|
||||
int16_t delay_raw = static_cast<int16_t>(report.data[1] | (report.data[2] << 8));
|
||||
data.ups_delay_shutdown = delay_raw;
|
||||
|
||||
ESP_LOGD(CP_TAG, "UPS delay shutdown: %d seconds", data.ups_delay_shutdown);
|
||||
// Handle 0xFFFF as "disabled" (not -1)
|
||||
uint16_t delay_raw_unsigned = report.data[1] | (report.data[2] << 8);
|
||||
if (delay_raw_unsigned == 0xFFFF) {
|
||||
// When disabled, use NUT default for CyberPower (DEFAULT_OFFDELAY_CPS = 60)
|
||||
data.ups_delay_shutdown = 60;
|
||||
ESP_LOGD(CP_TAG, "UPS delay shutdown: 60 seconds (default, raw: 0xFFFF)");
|
||||
} else {
|
||||
int16_t delay_raw = static_cast<int16_t>(delay_raw_unsigned);
|
||||
data.ups_delay_shutdown = delay_raw;
|
||||
ESP_LOGD(CP_TAG, "UPS delay shutdown: %d seconds", data.ups_delay_shutdown);
|
||||
}
|
||||
}
|
||||
|
||||
void CyberPowerProtocol::parse_delay_start_report(const HidReport &report, UpsData &data) {
|
||||
@@ -503,10 +520,17 @@ void CyberPowerProtocol::parse_delay_start_report(const HidReport &report, UpsDa
|
||||
}
|
||||
|
||||
// NUT debug shows: Report 0x16, Value: -60 (DelayBeforeStartup)
|
||||
int16_t delay_raw = static_cast<int16_t>(report.data[1] | (report.data[2] << 8));
|
||||
data.ups_delay_start = delay_raw;
|
||||
|
||||
ESP_LOGD(CP_TAG, "UPS delay start: %d seconds", data.ups_delay_start);
|
||||
// Handle 0xFFFF as "disabled" (not -1)
|
||||
uint16_t delay_raw_unsigned = report.data[1] | (report.data[2] << 8);
|
||||
if (delay_raw_unsigned == 0xFFFF) {
|
||||
// When disabled, use NUT default for CyberPower (DEFAULT_ONDELAY_CPS = 120)
|
||||
data.ups_delay_start = 120;
|
||||
ESP_LOGD(CP_TAG, "UPS delay start: 120 seconds (default, raw: 0xFFFF)");
|
||||
} else {
|
||||
int16_t delay_raw = static_cast<int16_t>(delay_raw_unsigned);
|
||||
data.ups_delay_start = delay_raw;
|
||||
ESP_LOGD(CP_TAG, "UPS delay start: %d seconds", data.ups_delay_start);
|
||||
}
|
||||
}
|
||||
|
||||
void CyberPowerProtocol::parse_realpower_nominal_report(const HidReport &report, UpsData &data) {
|
||||
@@ -607,9 +631,15 @@ void CyberPowerProtocol::parse_firmware_version_report(const HidReport &report,
|
||||
esp_err_t fw_ret = parent_->usb_get_string_descriptor(string_index, actual_firmware);
|
||||
|
||||
if (fw_ret == ESP_OK && !actual_firmware.empty()) {
|
||||
data.firmware_version = actual_firmware;
|
||||
// Clean up firmware string - remove invalid characters and trim
|
||||
std::string cleaned_firmware = clean_firmware_string(actual_firmware);
|
||||
data.firmware_version = cleaned_firmware;
|
||||
ESP_LOGI(CP_TAG, "Successfully read CyberPower firmware from USB string descriptor %d: \"%s\"",
|
||||
string_index, data.firmware_version.c_str());
|
||||
if (cleaned_firmware != actual_firmware) {
|
||||
ESP_LOGD(CP_TAG, "Cleaned firmware string from \"%s\" to \"%s\"",
|
||||
actual_firmware.c_str(), cleaned_firmware.c_str());
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
ESP_LOGW(CP_TAG, "Failed to read USB string descriptor %d for firmware: %s, trying fallbacks",
|
||||
@@ -644,14 +674,20 @@ void CyberPowerProtocol::parse_firmware_version_report(const HidReport &report,
|
||||
std::string fw_attempt;
|
||||
esp_err_t ret = parent_->usb_get_string_descriptor(idx, fw_attempt);
|
||||
|
||||
if (ret == ESP_OK && !fw_attempt.empty() &&
|
||||
(fw_attempt.find("CR") == 0 || fw_attempt.find("CP") == 0 ||
|
||||
fw_attempt.find("FW") != std::string::npos)) {
|
||||
// Looks like a valid CyberPower firmware string
|
||||
data.firmware_version = fw_attempt;
|
||||
ESP_LOGI(CP_TAG, "Found CyberPower firmware at alternative string descriptor %d: \"%s\"",
|
||||
idx, data.firmware_version.c_str());
|
||||
return;
|
||||
if (ret == ESP_OK && !fw_attempt.empty()) {
|
||||
std::string cleaned_fw = clean_firmware_string(fw_attempt);
|
||||
if ((cleaned_fw.find("CR") == 0 || cleaned_fw.find("CP") == 0 ||
|
||||
cleaned_fw.find("FW") != std::string::npos)) {
|
||||
// Looks like a valid CyberPower firmware string
|
||||
data.firmware_version = cleaned_fw;
|
||||
ESP_LOGI(CP_TAG, "Found CyberPower firmware at alternative string descriptor %d: \"%s\"",
|
||||
idx, data.firmware_version.c_str());
|
||||
if (cleaned_fw != fw_attempt) {
|
||||
ESP_LOGD(CP_TAG, "Cleaned alternative firmware string from \"%s\" to \"%s\"",
|
||||
fw_attempt.c_str(), cleaned_fw.c_str());
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -747,18 +783,25 @@ void CyberPowerProtocol::read_missing_dynamic_values(UpsData &data) {
|
||||
// It's at offset 24 and already parsed in parse_battery_runtime_report
|
||||
// Just need to extract it properly
|
||||
|
||||
// 4. Set static/derived values based on NUT behavior
|
||||
data.battery_status = "100%"; // CyberPower shows this as static in NUT
|
||||
// 4. Set static/derived values based on NUT behavior
|
||||
data.ups_test_result = "No test initiated"; // Default test result
|
||||
|
||||
// 5. Timer values are already implemented as ups_delay_shutdown/start
|
||||
// Map them to timer fields for consistency
|
||||
data.ups_timer_shutdown = data.ups_delay_shutdown;
|
||||
data.ups_timer_start = data.ups_delay_start;
|
||||
// NOTE: battery_status is now properly read from FullChargeCapacity in parse_battery_capacity_report
|
||||
|
||||
// 5. Timer values represent active countdown (negative when no countdown active)
|
||||
// NUT shows: ups.timer.shutdown: -60, ups.timer.start: -60
|
||||
data.ups_timer_shutdown = -data.ups_delay_shutdown; // Negative indicates no active countdown
|
||||
data.ups_timer_start = -data.ups_delay_start; // Negative indicates no active countdown
|
||||
data.ups_timer_reboot = -10; // CyberPower doesn't have separate reboot timer, use default
|
||||
|
||||
ESP_LOGD(CP_TAG, "Completed reading CyberPower missing dynamic values");
|
||||
}
|
||||
|
||||
void CyberPowerProtocol::parse_battery_capacity_report(const HidReport &report, UpsData &data) {
|
||||
// This is the same as the capacity limits report - just a cleaner interface
|
||||
parse_battery_capacity_limits_report(report, data);
|
||||
}
|
||||
|
||||
void CyberPowerProtocol::parse_battery_capacity_limits_report(const HidReport &report, UpsData &data) {
|
||||
if (report.data.size() < 6) {
|
||||
ESP_LOGW(CP_TAG, "Battery capacity limits report too short: %zu bytes", report.data.size());
|
||||
@@ -785,6 +828,17 @@ void CyberPowerProtocol::parse_battery_capacity_limits_report(const HidReport &r
|
||||
ESP_LOGI(CP_TAG, "CyberPower Battery charge low threshold: %.0f%% (raw: %d)",
|
||||
data.battery_charge_low, remaining_limit);
|
||||
}
|
||||
|
||||
// Extract full charge capacity (offset 40 = byte 6) - this is used for battery.status
|
||||
if (report.data.size() > 6) {
|
||||
uint8_t full_charge_capacity = report.data[6]; // Offset 40 bits = byte 5 + 1
|
||||
// Format as "XX%" like NUT cps_battstatus function does
|
||||
char battery_status_buf[8];
|
||||
snprintf(battery_status_buf, sizeof(battery_status_buf), "%.0f%%", static_cast<float>(full_charge_capacity));
|
||||
data.battery_status = std::string(battery_status_buf);
|
||||
ESP_LOGI(CP_TAG, "CyberPower Battery status (FullChargeCapacity): %s (raw: %d)",
|
||||
data.battery_status.c_str(), full_charge_capacity);
|
||||
}
|
||||
}
|
||||
|
||||
void CyberPowerProtocol::parse_battery_chemistry_report(const HidReport &report, UpsData &data) {
|
||||
@@ -826,5 +880,38 @@ void CyberPowerProtocol::parse_battery_chemistry_report(const HidReport &report,
|
||||
data.battery_type.c_str(), chemistry_raw);
|
||||
}
|
||||
|
||||
std::string CyberPowerProtocol::clean_firmware_string(const std::string &raw_firmware) {
|
||||
if (raw_firmware.empty()) {
|
||||
return raw_firmware;
|
||||
}
|
||||
|
||||
std::string cleaned = raw_firmware;
|
||||
|
||||
// Remove non-printable characters and common USB string descriptor artifacts
|
||||
cleaned.erase(std::remove_if(cleaned.begin(), cleaned.end(), [](unsigned char c) {
|
||||
// Keep alphanumeric, dots, dashes, and spaces
|
||||
return !(std::isalnum(c) || c == '.' || c == '-' || c == ' ');
|
||||
}), cleaned.end());
|
||||
|
||||
// Trim trailing and leading whitespace
|
||||
// Trim leading whitespace
|
||||
cleaned.erase(cleaned.begin(), std::find_if(cleaned.begin(), cleaned.end(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}));
|
||||
|
||||
// Trim trailing whitespace
|
||||
cleaned.erase(std::find_if(cleaned.rbegin(), cleaned.rend(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}).base(), cleaned.end());
|
||||
|
||||
// If the string is now empty, return the original
|
||||
if (cleaned.empty()) {
|
||||
ESP_LOGW(CP_TAG, "Firmware string cleaning resulted in empty string, keeping original");
|
||||
return raw_firmware;
|
||||
}
|
||||
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
} // namespace ups_hid
|
||||
} // namespace esphome
|
||||
@@ -29,6 +29,7 @@ class CyberPowerProtocol : public UpsProtocolBase {
|
||||
|
||||
private:
|
||||
// Report ID constants (based on NUT debug logs)
|
||||
static const uint8_t BATTERY_CAPACITY_REPORT_ID = 0x07; // Battery capacity limits
|
||||
static const uint8_t BATTERY_RUNTIME_REPORT_ID = 0x08; // Battery % + Runtime
|
||||
static const uint8_t BATTERY_VOLTAGE_NOMINAL_REPORT_ID = 0x09; // Battery voltage nominal
|
||||
static const uint8_t BATTERY_VOLTAGE_REPORT_ID = 0x0a; // Battery voltage
|
||||
@@ -63,6 +64,7 @@ class CyberPowerProtocol : public UpsProtocolBase {
|
||||
bool read_hid_report(uint8_t report_id, HidReport &report);
|
||||
|
||||
// Parser methods for different reports
|
||||
void parse_battery_capacity_report(const HidReport &report, UpsData &data);
|
||||
void parse_battery_runtime_report(const HidReport &report, UpsData &data);
|
||||
void parse_battery_voltage_report(const HidReport &report, UpsData &data);
|
||||
void parse_battery_voltage_nominal_report(const HidReport &report, UpsData &data);
|
||||
@@ -85,6 +87,9 @@ class CyberPowerProtocol : public UpsProtocolBase {
|
||||
void read_missing_dynamic_values(UpsData &data);
|
||||
void parse_battery_capacity_limits_report(const HidReport &report, UpsData &data);
|
||||
void parse_battery_chemistry_report(const HidReport &report, UpsData &data);
|
||||
|
||||
// String cleaning utilities
|
||||
std::string clean_firmware_string(const std::string &raw_firmware);
|
||||
|
||||
// CyberPower-specific scaling logic
|
||||
void check_battery_voltage_scaling(float battery_voltage, float nominal_voltage);
|
||||
|
||||
@@ -2,6 +2,7 @@ substitutions:
|
||||
name: ups-hid-monitor-full-test
|
||||
friendly_name: "UPS HID Monitor"
|
||||
timezone: "America/New_York"
|
||||
dns_suffix: !secret wifi_dns_domain
|
||||
|
||||
esphome:
|
||||
name: "${name}"
|
||||
@@ -46,6 +47,8 @@ ota:
|
||||
wifi:
|
||||
ssid: !secret wifi_ssid
|
||||
password: !secret wifi_password
|
||||
domain: "${dns_suffix}"
|
||||
use_address: "${name}${dns_suffix}"
|
||||
ap:
|
||||
ssid: "${friendly_name} Fallback"
|
||||
password: !secret ap_password
|
||||
|
||||
Reference in new issue
Block a user