/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : bsp_config.h
  * @brief          : Board support package configuration file header
  ******************************************************************************
  */
/* USER CODE END Header */

#ifndef BSP_CONFIG_H
#define BSP_CONFIG_H

#include <stdio.h>
#include "bsp_module.h"
#include "bsp_board.h"
#include "hal.h"

/**
 * @brief BSP configuration file format definitions
 */
#define BSP_CONFIG_FILE_VERSION "1.0.0"
#define BSP_CONFIG_MAX_LINE_LENGTH 256
#define BSP_CONFIG_MAX_SECTION_NAME_LENGTH 32
#define BSP_CONFIG_MAX_KEY_NAME_LENGTH 32
#define BSP_CONFIG_MAX_VALUE_LENGTH 128

/**
 * @brief BSP configuration section type
 */
typedef enum {
    BSP_CONFIG_SECTION_NONE = 0,
    BSP_CONFIG_SECTION_BOARD,
    BSP_CONFIG_SECTION_MODULE,
    BSP_CONFIG_SECTION_PERIPH,
    BSP_CONFIG_SECTION_MAX
} bsp_config_section_t;

/**
 * @brief BSP configuration entry
 */
typedef struct {
    const char* section;
    const char* key;
    const char* value;
} bsp_config_entry_t;

/**
 * @brief BSP configuration parser context
 */
typedef struct {
    FILE* file;
    char current_section[BSP_CONFIG_MAX_SECTION_NAME_LENGTH];
    char buffer[BSP_CONFIG_MAX_LINE_LENGTH];
    uint32_t line_number;
    uint8_t initialized;
} bsp_config_parser_t;

/**
 * @brief BSP configuration file operations
 */
typedef struct {
    hal_ret_t (*open)(const char* filename, bsp_config_parser_t* parser);
    hal_ret_t (*close)(bsp_config_parser_t* parser);
    hal_ret_t (*read_entry)(bsp_config_parser_t* parser, bsp_config_entry_t* entry);
    hal_ret_t (*parse_int)(const char* value, int* result);
    hal_ret_t (*parse_uint)(const char* value, uint32_t* result);
    hal_ret_t (*parse_bool)(const char* value, uint8_t* result);
    hal_ret_t (*parse_string)(const char* value, char* result, size_t max_length);
} bsp_config_ops_t;

/**
 * @brief Initialize BSP configuration system
 * @retval HAL status code
 */
hal_ret_t bsp_config_init(void);

/**
 * @brief Deinitialize BSP configuration system
 * @retval HAL status code
 */
hal_ret_t bsp_config_deinit(void);

/**
 * @brief Load BSP configuration from file
 * @param filename: Configuration file name
 * @retval HAL status code
 */
hal_ret_t bsp_config_load(const char* filename);

/**
 * @brief Save BSP configuration to file
 * @param filename: Configuration file name
 * @retval HAL status code
 */
hal_ret_t bsp_config_save(const char* filename);

/**
 * @brief Get configuration value
 * @param section: Configuration section name
 * @param key: Configuration key name
 * @param value: Pointer to store value
 * @param max_length: Maximum length of value buffer
 * @retval HAL status code
 */
hal_ret_t bsp_config_get_value(const char* section, const char* key, char* value, size_t max_length);

/**
 * @brief Set configuration value
 * @param section: Configuration section name
 * @param key: Configuration key name
 * @param value: Configuration value
 * @retval HAL status code
 */
hal_ret_t bsp_config_set_value(const char* section, const char* key, const char* value);

/**
 * @brief Get configuration value as integer
 * @param section: Configuration section name
 * @param key: Configuration key name
 * @param value: Pointer to store integer value
 * @retval HAL status code
 */
hal_ret_t bsp_config_get_int(const char* section, const char* key, int* value);

/**
 * @brief Set configuration value as integer
 * @param section: Configuration section name
 * @param key: Configuration key name
 * @param value: Integer value
 * @retval HAL status code
 */
hal_ret_t bsp_config_set_int(const char* section, const char* key, int value);

/**
 * @brief Get configuration value as unsigned integer
 * @param section: Configuration section name
 * @param key: Configuration key name
 * @param value: Pointer to store unsigned integer value
 * @retval HAL status code
 */
hal_ret_t bsp_config_get_uint(const char* section, const char* key, uint32_t* value);

/**
 * @brief Set configuration value as unsigned integer
 * @param section: Configuration section name
 * @param key: Configuration key name
 * @param value: Unsigned integer value
 * @retval HAL status code
 */
hal_ret_t bsp_config_set_uint(const char* section, const char* key, uint32_t value);

/**
 * @brief Get configuration value as boolean
 * @param section: Configuration section name
 * @param key: Configuration key name
 * @param value: Pointer to store boolean value
 * @retval HAL status code
 */
hal_ret_t bsp_config_get_bool(const char* section, const char* key, uint8_t* value);

/**
 * @brief Set configuration value as boolean
 * @param section: Configuration section name
 * @param key: Configuration key name
 * @param value: Boolean value
 * @retval HAL status code
 */
hal_ret_t bsp_config_set_bool(const char* section, const char* key, uint8_t value);

/**
 * @brief Parse BSP modules from configuration
 * @param config: Pointer to BSP board configuration structure to fill
 * @retval HAL status code
 */
hal_ret_t bsp_config_parse_modules(bsp_board_config_t* config);

/**
 * @brief Initialize BSP from configuration file
 * @param filename: Configuration file name
 * @retval HAL status code
 */
hal_ret_t bsp_init_from_config(const char* filename);

#endif /* BSP_CONFIG_H */
