new upgrade

This commit is contained in:
2025-12-21 23:28:26 +00:00
parent 82fa194bd8
commit 023644a887
99 changed files with 7457 additions and 7079 deletions

View File

@@ -1,114 +1,115 @@
#ifndef MQTT_H_
#define MQTT_H_
// === Início de: components/mqtt/include/mqtt.h ===
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
#include "esp_err.h"
// Tamanhos máximos esperados pelos getters (buffers do chamador)
#define MQTT_SERVER_MAX_LEN 64 ///< host ou URI do broker
#define MQTT_BASE_TOPIC_MAX_LEN 64 ///< tópico base (ex: "evse")
#define MQTT_USERNAME_MAX_LEN 32
#define MQTT_PASSWORD_MAX_LEN 64
/**
* @file mqtt.h
* @brief MQTT configuration and control interface.
* @brief Inicializa o módulo MQTT.
*
* This module provides initialization, configuration,
* and runtime access functions for the MQTT client.
* - Carrega configuração da NVS
* - Regista handlers de eventos (AUTH, SCHED, LOADBALANCER, METER, EVSE, NETWORK)
* - Cria a task de telemetria periódica
*
* Configuration is persisted in NVS under namespace "mqtt".
* Não inicia a ligação ao broker imediatamente; isso acontece quando existir IP
* (NETWORK_EVENT_STA_GOT_IP) e `enabled == true`.
*/
/* -------------------------------------------------------------------------- */
/* Definitions */
/* -------------------------------------------------------------------------- */
#define MQTT_MAX_SERVER_LEN 64 /**< Max length for MQTT server URI */
#define MQTT_MAX_USER_LEN 32 /**< Max length for MQTT username */
#define MQTT_MAX_PASSWORD_LEN 64 /**< Max length for MQTT password */
#define MQTT_MAX_BASE_TOPIC_LEN 64 /**< Max length for MQTT base topic */
/* -------------------------------------------------------------------------- */
/* Public Functions */
/* -------------------------------------------------------------------------- */
esp_err_t mqtt_init(void);
/**
* @brief Initialize MQTT subsystem.
* @brief Força tentativa de ligação ao broker (normalmente não é necessário chamar).
*
* Loads configuration from NVS and starts background publish task
* if MQTT is enabled. Must be called once during system startup.
* Útil apenas se quiseres forçar manualmente depois de mudares config.
*/
void mqtt_init(void);
esp_err_t mqtt_start(void);
/**
* @brief Restart the MQTT client safely.
*
* Stops the current MQTT client (if running) and starts a new one
* with the configuration currently stored in NVS.
*
* Useful when changing Wi-Fi networks, credentials, or broker settings.
*
* @return ESP_OK on success, or an ESP_ERR_* code otherwise.
* @brief Pára o cliente MQTT e destrói o handle.
*/
esp_err_t mqtt_restart(void);
void mqtt_stop(void);
// =============================
// Getters de configuração
// =============================
/**
* @brief Set and persist MQTT configuration parameters in NVS.
*
* Any NULL parameter will be skipped (the previous value remains stored).
*
* @param enabled Whether MQTT should be enabled (true/false).
* @param server Broker URI (e.g. "mqtt://192.168.1.10").
* @param base_topic Base topic prefix for publish/subscribe.
* @param user MQTT username (optional).
* @param password MQTT password (optional).
* @param periodicity Publish interval (in seconds). Must be >0 when enabled.
*
* @return ESP_OK on success, or an ESP_ERR_* code otherwise.
*/
esp_err_t mqtt_set_config(bool enabled,
const char *server,
const char *base_topic,
const char *user,
const char *password,
uint16_t periodicity);
/**
* @brief Get whether MQTT is enabled.
*
* @return true if enabled, false otherwise.
* @brief Indica se o MQTT está ativo (flag em config).
*/
bool mqtt_get_enabled(void);
/**
* @brief Get MQTT broker URI stored in NVS.
* @brief Obtém o servidor MQTT (host ou URI).
*
* @param[out] value Buffer to receive the URI (min length: MQTT_MAX_SERVER_LEN).
* @param server buffer de saída, com pelo menos MQTT_SERVER_MAX_LEN bytes.
*/
void mqtt_get_server(char *value);
void mqtt_get_server(char *server);
/**
* @brief Get MQTT base topic stored in NVS.
* @brief Obtém o tópico base (ex.: "evse").
*
* @param[out] value Buffer to receive the base topic (min length: MQTT_MAX_BASE_TOPIC_LEN).
* @param base_topic buffer de saída, com pelo menos MQTT_BASE_TOPIC_MAX_LEN bytes.
*/
void mqtt_get_base_topic(char *value);
void mqtt_get_base_topic(char *base_topic);
/**
* @brief Get MQTT username stored in NVS.
* @brief Obtém o username MQTT (se configurado).
*
* @param[out] value Buffer to receive the username (min length: MQTT_MAX_USER_LEN).
* @param username buffer de saída, com pelo menos MQTT_USERNAME_MAX_LEN bytes.
*/
void mqtt_get_user(char *value);
void mqtt_get_user(char *username);
/**
* @brief Get MQTT password stored in NVS.
* @brief Obtém a password MQTT (se configurada).
*
* @param[out] value Buffer to receive the password (min length: MQTT_MAX_PASSWORD_LEN).
* @param password buffer de saída, com pelo menos MQTT_PASSWORD_MAX_LEN bytes.
*/
void mqtt_get_password(char *value);
void mqtt_get_password(char *password);
/**
* @brief Get MQTT publish periodicity in seconds.
*
* @return Publish interval in seconds (default: 30).
* @brief Obtém a periodicidade da telemetria periódica (em segundos).
*/
uint16_t mqtt_get_periodicity(void);
#endif /* MQTT_H_ */
// =============================
// Setter de configuração
// =============================
/**
* @brief Atualiza a configuração MQTT (tipicamente chamado pelo endpoint REST).
*
* - Grava em NVS
* - Se já existir cliente ligado, pára e recria com a nova config
* - Se `enabled == true` e houver IP, tenta ligar ao broker
*
* @param enabled true para ativar MQTT
* @param host hostname ou URI (ex: "broker.hivemq.com" ou "mqtt://x.y.z")
* @param topic tópico base (ex: "evse")
* @param username username MQTT (ou NULL para manter/limpar)
* @param password password MQTT (ou NULL para manter/limpar)
* @param periodicity período da task de telemetria em segundos (ex: 30)
*
* @return ESP_OK em caso de sucesso, ou erro de NVS / MQTT.
*/
esp_err_t mqtt_set_config(bool enabled,
const char *host,
const char *topic,
const char *username,
const char *password,
int periodicity);
#ifdef __cplusplus
}
#endif
// === Fim de: components/mqtt/include/mqtt.h ===