fix(ups-hid): handle wonky esp32-idf disconnect/reconnect issues with v5.3

Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
bullshit committed 2025-08-18 14:53:45 +02:00
1 parent bbacfcb5c8
commit 0c1e24b525
5 files changed
+179 -9

No files matched your search

@@ -15,6 +15,12 @@ static const char *const CP_TAG = "ups_hid.cyberpower_hid";
bool CyberPowerProtocol::detect() {
ESP_LOGD(CP_TAG, "Detecting CyberPower HID protocol");
// Check device connection status first
if (!parent_->is_device_connected()) {
ESP_LOGD(CP_TAG, "Device not connected, skipping protocol detection");
return false;
}
// Give device time to initialize after connection (same as APC)
vTaskDelay(pdMS_TO_TICKS(100));
@@ -31,6 +37,12 @@ bool CyberPowerProtocol::detect() {
HidReport test_report;
for (uint8_t report_id : test_report_ids) {
// Check device connection before each report attempt
if (!parent_->is_device_connected()) {
ESP_LOGD(CP_TAG, "Device disconnected during protocol detection");
return false;
}
ESP_LOGD(CP_TAG, "Testing report ID 0x%02X...", report_id);
if (read_hid_report(report_id, test_report)) {
@@ -225,6 +237,12 @@ bool CyberPowerProtocol::read_data(UpsData &data) {
}
bool CyberPowerProtocol::read_hid_report(uint8_t report_id, HidReport &report) {
// Check device connection before any HID communication
if (!parent_->is_device_connected()) {
ESP_LOGV(CP_TAG, "Device not connected, skipping HID report 0x%02X", report_id);
return false;
}
uint8_t buffer[64]; // Maximum HID report size
size_t buffer_len = sizeof(buffer);
esp_err_t ret;
@@ -244,6 +262,12 @@ bool CyberPowerProtocol::read_hid_report(uint8_t report_id, HidReport &report) {
// Log the specific error for Feature Report
ESP_LOGD(CP_TAG, "Feature Report 0x%02X failed: %s", report_id, esp_err_to_name(ret));
// Check connection again before trying Input report
if (!parent_->is_device_connected()) {
ESP_LOGV(CP_TAG, "Device disconnected during HID communication for report 0x%02X", report_id);
return false;
}
// Fallback: try Input Report (0x01) for real-time data
buffer_len = sizeof(buffer);
ret = parent_->hid_get_report(0x01, report_id, buffer, &buffer_len);