76 lines
2.3 KiB
C
76 lines
2.3 KiB
C
// === Início de: components/mqtt/include/mqtt.h ===
|
|
#pragma once
|
|
|
|
#ifdef __cplusplus
|
|
extern "C"
|
|
{
|
|
#endif
|
|
|
|
#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
|
|
|
|
/**
|
|
* @brief Inicializa o módulo MQTT.
|
|
*
|
|
* - Carrega configuração da NVS
|
|
* - Regista handlers de eventos (AUTH, SCHED, LOADBALANCER, METER, EVSE, NETWORK)
|
|
* - Cria a task de telemetria periódica
|
|
* Não inicia a ligação ao broker imediatamente; isso acontece quando existir IP
|
|
* (NETWORK_EVENT_STA_GOT_IP) e `enabled == true`.
|
|
*
|
|
* IMPORTANTE: chamar DEPOIS de loadbalancer_init() para o estado publicado
|
|
* refletir a política local já restaurada.
|
|
*/
|
|
esp_err_t mqtt_init(void);
|
|
|
|
/**
|
|
* @brief Força tentativa de ligação ao broker (normalmente não é necessário chamar).
|
|
*/
|
|
esp_err_t mqtt_start(void);
|
|
|
|
/**
|
|
* @brief Pára o cliente MQTT e destrói o handle.
|
|
*/
|
|
void mqtt_stop(void);
|
|
|
|
// =============================
|
|
// Getters de configuração
|
|
// =============================
|
|
|
|
bool mqtt_get_enabled(void);
|
|
void mqtt_get_server(char *server);
|
|
void mqtt_get_base_topic(char *base_topic);
|
|
void mqtt_get_user(char *username);
|
|
void mqtt_get_password(char *password);
|
|
uint16_t mqtt_get_periodicity(void);
|
|
|
|
// =============================
|
|
// 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
|
|
*/
|
|
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 ===
|