Files
chargeflow/components/protocols/include/mqtt.h
2025-11-20 07:45:00 +00:00

115 lines
3.5 KiB
C
Executable File

#ifndef MQTT_H_
#define MQTT_H_
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
/**
* @file mqtt.h
* @brief MQTT configuration and control interface.
*
* This module provides initialization, configuration,
* and runtime access functions for the MQTT client.
*
* Configuration is persisted in NVS under namespace "mqtt".
*/
/* -------------------------------------------------------------------------- */
/* 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 */
/* -------------------------------------------------------------------------- */
/**
* @brief Initialize MQTT subsystem.
*
* Loads configuration from NVS and starts background publish task
* if MQTT is enabled. Must be called once during system startup.
*/
void mqtt_init(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.
*/
esp_err_t mqtt_restart(void);
/**
* @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.
*/
bool mqtt_get_enabled(void);
/**
* @brief Get MQTT broker URI stored in NVS.
*
* @param[out] value Buffer to receive the URI (min length: MQTT_MAX_SERVER_LEN).
*/
void mqtt_get_server(char *value);
/**
* @brief Get MQTT base topic stored in NVS.
*
* @param[out] value Buffer to receive the base topic (min length: MQTT_MAX_BASE_TOPIC_LEN).
*/
void mqtt_get_base_topic(char *value);
/**
* @brief Get MQTT username stored in NVS.
*
* @param[out] value Buffer to receive the username (min length: MQTT_MAX_USER_LEN).
*/
void mqtt_get_user(char *value);
/**
* @brief Get MQTT password stored in NVS.
*
* @param[out] value Buffer to receive the password (min length: MQTT_MAX_PASSWORD_LEN).
*/
void mqtt_get_password(char *value);
/**
* @brief Get MQTT publish periodicity in seconds.
*
* @return Publish interval in seconds (default: 30).
*/
uint16_t mqtt_get_periodicity(void);
#endif /* MQTT_H_ */