Per-pod NVS enable (default off) via UART/ESP-NOW/dashboard, and skip UV latch when LiPo ADC reads ~0 mV so USB/external supply still boots. Co-authored-by: Cursor <cursoragent@cursor.com>
119 lines
3.7 KiB
C
119 lines
3.7 KiB
C
#include "cmd_battery_uv.h"
|
|
#include "client_registry.h"
|
|
#include "esp_log.h"
|
|
#include "esp_now_comm.h"
|
|
#include "pod_settings.h"
|
|
#include "uart_cmd.h"
|
|
|
|
static const char *TAG = "[BATTERY_UV_CMD]";
|
|
|
|
static void reply(uint32_t client_id, bool success, bool enabled, bool latched,
|
|
uint32_t slaves_updated) {
|
|
alox_UartMessage response;
|
|
uart_cmd_init_response(&response, alox_MessageType_BATTERY_UV,
|
|
alox_UartMessage_battery_uv_response_tag);
|
|
response.payload.battery_uv_response.client_id = client_id;
|
|
response.payload.battery_uv_response.success = success;
|
|
response.payload.battery_uv_response.enabled = enabled;
|
|
response.payload.battery_uv_response.latched = latched;
|
|
response.payload.battery_uv_response.slaves_updated = slaves_updated;
|
|
uart_cmd_send(&response, TAG);
|
|
}
|
|
|
|
static esp_err_t apply_local_uv(bool enabled) {
|
|
esp_err_t err = pod_settings_set_uv_enabled(enabled);
|
|
if (err != ESP_OK) {
|
|
return err;
|
|
}
|
|
if (!enabled) {
|
|
(void)pod_settings_clear_uv_latched();
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
static esp_err_t push_uv_to_slave(const client_info_t *client, bool enabled) {
|
|
if (client == NULL) {
|
|
return ESP_ERR_INVALID_ARG;
|
|
}
|
|
esp_err_t err = client_registry_set_uv_protection(client->id, enabled);
|
|
if (err != ESP_OK) {
|
|
return err;
|
|
}
|
|
return esp_now_comm_send_battery_uv(client->mac, client->id, enabled);
|
|
}
|
|
|
|
static void handle_battery_uv(const uint8_t *data, size_t len) {
|
|
alox_UartMessage uart_msg;
|
|
alox_BatteryUvRequest req = alox_BatteryUvRequest_init_zero;
|
|
|
|
if (uart_cmd_decode(data, len, &uart_msg) != ESP_OK) {
|
|
ESP_LOGW(TAG, "decode failed");
|
|
reply(0, false, pod_settings_is_uv_enabled(), pod_settings_is_uv_latched(),
|
|
0);
|
|
return;
|
|
}
|
|
|
|
const alox_BatteryUvRequest *req_ptr = UART_CMD_REQ(
|
|
&uart_msg, alox_UartMessage_battery_uv_request_tag, battery_uv_request);
|
|
if (req_ptr != NULL) {
|
|
req = *req_ptr;
|
|
}
|
|
|
|
if (req.write) {
|
|
if (req.all_clients) {
|
|
(void)client_registry_set_uv_protection_all(req.enabled);
|
|
uint32_t sent = 0;
|
|
for (size_t i = 0; i < client_registry_count(); i++) {
|
|
const client_info_t *client = client_registry_at(i);
|
|
if (client == NULL) {
|
|
continue;
|
|
}
|
|
if (esp_now_comm_send_battery_uv(client->mac, client->id,
|
|
req.enabled) == ESP_OK) {
|
|
sent++;
|
|
}
|
|
}
|
|
|
|
bool local_ok = apply_local_uv(req.enabled) == ESP_OK;
|
|
ESP_LOGI(TAG, "UV protection %s local=%d slaves=%u",
|
|
req.enabled ? "on" : "off", local_ok, (unsigned)sent);
|
|
reply(0, local_ok || sent > 0, req.enabled,
|
|
pod_settings_is_uv_latched(), sent);
|
|
return;
|
|
}
|
|
|
|
if (req.client_id == 0) {
|
|
esp_err_t err = apply_local_uv(req.enabled);
|
|
reply(0, err == ESP_OK, pod_settings_is_uv_enabled(),
|
|
pod_settings_is_uv_latched(), 0);
|
|
return;
|
|
}
|
|
|
|
const client_info_t *client = client_registry_find_by_id(req.client_id);
|
|
if (client == NULL) {
|
|
ESP_LOGW(TAG, "client id %lu not found", (unsigned long)req.client_id);
|
|
reply(req.client_id, false, req.enabled, false, 0);
|
|
return;
|
|
}
|
|
|
|
esp_err_t err = push_uv_to_slave(client, req.enabled);
|
|
reply(req.client_id, err == ESP_OK, req.enabled, false,
|
|
err == ESP_OK ? 1 : 0);
|
|
return;
|
|
}
|
|
|
|
if (req.all_clients || req.client_id == 0) {
|
|
reply(0, true, pod_settings_is_uv_enabled(), pod_settings_is_uv_latched(),
|
|
0);
|
|
return;
|
|
}
|
|
|
|
bool enabled = true;
|
|
esp_err_t err = client_registry_get_uv_protection(req.client_id, &enabled);
|
|
reply(req.client_id, err == ESP_OK, enabled, false, 0);
|
|
}
|
|
|
|
void cmd_battery_uv_register(void) {
|
|
uart_cmd_register(alox_MessageType_BATTERY_UV, handle_battery_uv);
|
|
}
|