#ifndef MQTT_MANAGER_H #define MQTT_MANAGER_H #include "mqtt_client.h" #include "esp_err.h" #ifdef __cplusplus extern "C" { #endif /** * @brief MQTT connection status */ typedef enum { MQTT_STATUS_DISCONNECTED = 0, MQTT_STATUS_CONNECTING, MQTT_STATUS_CONNECTED, MQTT_STATUS_ERROR } mqtt_status_t; /** * @brief MQTT configuration structure */ typedef struct { const char *broker_url; /**< MQTT broker URL (e.g., "mqtt://192.168.1.100") */ int broker_port; /**< MQTT broker port (usually 1883 for non-secure) */ const char *client_id; /**< Unique client identifier */ const char *username; /**< Username (can be NULL if not required) */ const char *password; /**< Password (can be NULL if not required) */ int keepalive; /**< Keepalive interval in seconds (0 for default) */ int network_timeout_ms; /**< Network timeout in milliseconds (0 for default) */ } mqtt_config_t; /** * @brief MQTT event callback function type * * @param status Current MQTT status * @param topic Topic of received message (only valid for data events) * @param data Message data (only valid for data events) * @param data_len Length of message data */ typedef void (*mqtt_event_callback_t)(mqtt_status_t status, const char *topic, const char *data, int data_len); /** * @brief Initialize MQTT manager * * @param config MQTT configuration * @param event_callback Optional callback function for MQTT events * @return esp_err_t ESP_OK on success, error code on failure */ esp_err_t mqtt_manager_init(const mqtt_config_t *config, mqtt_event_callback_t event_callback); /** * @brief Start MQTT client * * @return esp_err_t ESP_OK on success, error code on failure */ esp_err_t mqtt_manager_start(void); /** * @brief Stop MQTT client * * @return esp_err_t ESP_OK on success, error code on failure */ esp_err_t mqtt_manager_stop(void); /** * @brief Get current MQTT connection status * * @return mqtt_status_t Current MQTT status */ mqtt_status_t mqtt_manager_get_status(void); /** * @brief Publish a message to a topic * * @param topic Topic to publish to * @param data Message data * @param len Length of data (0 for string length) * @param qos Quality of Service (0, 1, or 2) * @param retain Retain flag * @return int Message ID on success, -1 on error */ int mqtt_manager_publish(const char *topic, const char *data, int len, int qos, int retain); /** * @brief Publish a string message to a topic * * @param topic Topic to publish to * @param message String message to publish * @param qos Quality of Service (0, 1, or 2) * @param retain Retain flag * @return int Message ID on success, -1 on error */ int mqtt_manager_publish_string(const char *topic, const char *message, int qos, int retain); /** * @brief Publish sensor data as JSON * * @param topic Topic to publish to * @param temperature Temperature value * @param humidity Humidity value * @param device_id Device identifier * @param qos Quality of Service (0, 1, or 2) * @param retain Retain flag * @return int Message ID on success, -1 on error */ int mqtt_manager_publish_sensor_data(const char *topic, float temperature, float humidity, const char *device_id, int qos, int retain); /** * @brief Subscribe to a topic * * @param topic Topic to subscribe to * @param qos Quality of Service (0, 1, or 2) * @return int Message ID on success, -1 on error */ int mqtt_manager_subscribe(const char *topic, int qos); /** * @brief Unsubscribe from a topic * * @param topic Topic to unsubscribe from * @return int Message ID on success, -1 on error */ int mqtt_manager_unsubscribe(const char *topic); /** * @brief Check if MQTT is connected * * @return true if connected, false otherwise */ bool mqtt_manager_is_connected(void); /** * @brief Force reconnect to MQTT broker * * @return esp_err_t ESP_OK on success, error code on failure */ esp_err_t mqtt_manager_reconnect(void); /** * @brief Deinitialize MQTT manager * * @return esp_err_t ESP_OK on success, error code on failure */ esp_err_t mqtt_manager_deinit(void); #ifdef __cplusplus } #endif #endif // MQTT_MANAGER_H