Files
relay_chn/private_include/relay_chn_nvs.h
ismail 2c9ee40ff4 Enhance NVS module with a dedicated background task
- Implemented a dedicated background task to decouple long-running code from the main application task.
- Improved the NVS commit code logic, especially for batch writes to minimize flash wear.
- Updated NVS functions to support asynchronous writes and synchronous reads.
- Added default value parameters to `get` functions for better usability.
- Improved error handling and logging in NVS operations.
- Refactored related code in multiple source files to accommodate these changes.

Refs #1085, #1096 and closes #1098
2025-09-04 14:50:38 +03:00

143 lines
4.5 KiB
C

/*
* SPDX-FileCopyrightText: 2025 Kozmotronik Tech
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <stdint.h>
#include "esp_err.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "relay_chn_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Initialize NVS storage for relay channels.
*
* @attention Before calling this function, make sure the NVS flash is initialised
* using either the nvs_flash_init() function for the default NVS partition or the
* nvs_flash_init_partition() function for a custom partition.
*
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t relay_chn_nvs_init(void);
/**
* @brief Store relay channel direction in NVS.
*
* @param[in] ch Channel number.
* @param[in] direction Direction to store.
*
* @note This operation is asynchronous. The value is queued to be written
* by a background task. A subsequent `get` call may not immediately
* reflect the new value.
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t relay_chn_nvs_set_direction(uint8_t ch, relay_chn_direction_t direction);
/**
* @brief Retrieve relay channel direction from NVS.
*
* @param[in] ch Channel number.
* @param[out] direction Pointer to store retrieved direction.
* @param[in] default_val Default value to use if not found in NVS.
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t relay_chn_nvs_get_direction(uint8_t ch, relay_chn_direction_t *direction, relay_chn_direction_t default_val);
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
/**
* @brief Store relay channel run limit in NVS.
*
* @param[in] ch Channel number.
* @param[in] limit_sec Run limit value to store.
*
* @note This operation is asynchronous. The value is queued to be written
* by a background task. A subsequent `get` call may not immediately
* reflect the new value.
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t relay_chn_nvs_set_run_limit(uint8_t ch, uint16_t limit_sec);
/**
* @brief Retrieve relay channel run limit from NVS.
*
* @param[in] ch Channel number.
* @param[out] limit_sec Pointer to store retrieved run limit value.
* @param[in] default_val Default value to use if not found in NVS.
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t relay_chn_nvs_get_run_limit(uint8_t ch, uint16_t *limit_sec, uint16_t default_val);
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
#if CONFIG_RELAY_CHN_ENABLE_TILTING
/**
* @brief Store tilt sensitivity in NVS.
*
* @param[in] ch Channel number.
* @param[in] sensitivity Sensitivity value to store.
*
* @note This operation is asynchronous. The value is queued to be written
* by a background task. A subsequent `get` call may not immediately
* reflect the new value.
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t relay_chn_nvs_set_tilt_sensitivity(uint8_t ch, uint8_t sensitivity);
/**
* @brief Retrieve tilt sensitivity from NVS.
*
* @param[in] ch Channel number.
* @param[out] sensitivity Pointer to store retrieved sensitivity.
* @param[in] default_val Default value to use if not found in NVS.
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t relay_chn_nvs_get_tilt_sensitivity(uint8_t ch, uint8_t *sensitivity, uint8_t default_val);
/**
* @brief Store tilt counters in NVS.
*
* @param[in] ch Channel number.
* @param[in] tilt_count Tilt count value.
*
* @note This operation is asynchronous. The value is queued to be written
* by a background task. A subsequent `get` call may not immediately
* reflect the new value.
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t relay_chn_nvs_set_tilt_count(uint8_t ch, uint16_t tilt_count);
/**
* @brief Retrieve tilt counters from NVS.
*
* @param[in] ch Channel number.
* @param[out] tilt_count Pointer to store tilt count.
* @param[in] default_val Default value to use if not found in NVS.
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t relay_chn_nvs_get_tilt_count(uint8_t ch, uint16_t *tilt_count, uint16_t default_val);
#endif // CONFIG_RELAY_CHN_ENABLE_TILTING
/**
* @brief Erase all keys in the NVS namespace.
*
* This function will erase all key-value pairs in the NVS namespace used by relay channels.
* It will also flush all pending operations in the queue.
*
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t relay_chn_nvs_erase_all(void);
/**
* @brief Deinitialize NVS storage for relay channels.
*/
void relay_chn_nvs_deinit(void);
#ifdef __cplusplus
}
#endif