/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : bsp_board_manager.c
  * @brief          : 板级支持包管理器源文件
  ******************************************************************************
  */
/* USER CODE END Header */

#include "bsp_board_manager.h"
#include "bsp_board.h"

/* Forward declarations for board configurations */
extern const bsp_board_config_t stm32f407vet6_board_config;

/**
 * @brief 支持的板卡配置列表
 */
static const bsp_board_config_t* supported_boards[] = {
    &stm32f407vet6_board_config,
    /* Add more board configurations here */
};

/**
 * @brief 当前板卡配置索引
 */
static uint8_t current_board_index = 0;

/**
 * @brief 获取支持的板卡数量
 * @retval 支持的板卡数量
 */
uint8_t bsp_board_get_count(void) {
    return sizeof(supported_boards) / sizeof(supported_boards[0]);
}

/**
 * @brief 通过索引获取板卡配置
 * @param index: 板卡配置索引
 * @retval 指向板卡配置结构体的指针，无效索引返回 NULL
 */
const bsp_board_config_t* bsp_board_get_by_index(uint8_t index) {
    if (index < bsp_board_get_count()) {
        return supported_boards[index];
    }
    return NULL;
}

/**
 * @brief 通过名称获取板卡配置
 * @param name: 板卡名称字符串
 * @retval 指向板卡配置结构体的指针，未找到返回 NULL
 */
const bsp_board_config_t* bsp_board_get_by_name(const char* name) {
    for (uint8_t i = 0; i < bsp_board_get_count(); i++) {
        if (supported_boards[i]->name != NULL && 
            strcmp(supported_boards[i]->name, name) == 0) {
            return supported_boards[i];
        }
    }
    return NULL;
}

/**
 * @brief 通过索引设置当前板卡配置
 * @param index: 板卡配置索引
 * @retval HAL 状态码
 */
hal_ret_t bsp_board_set_by_index(uint8_t index) {
    if (index < bsp_board_get_count()) {
        current_board_index = index;
        return HAL_RET_OK;
    }
    return HAL_RET_INVALID_PARAM;
}

/**
 * @brief 通过名称设置当前板卡配置
 * @param name: 板卡名称字符串
 * @retval HAL 状态码
 */
hal_ret_t bsp_board_set_by_name(const char* name) {
    if (name == NULL) {
        return HAL_RET_INVALID_PARAM;
    }
    
    for (uint8_t i = 0; i < bsp_board_get_count(); i++) {
        if (supported_boards[i]->name != NULL && 
            strcmp(supported_boards[i]->name, name) == 0) {
            current_board_index = i;
            return HAL_RET_OK;
        }
    }
    return HAL_RET_ERROR;
}

/**
 * @brief 获取当前板卡配置
 * @retval 指向当前板卡配置结构体的指针
 */
const bsp_board_config_t* bsp_board_get_config(void) {
    return supported_boards[current_board_index];
}

/**
 * @brief 获取当前板卡索引
 * @retval 当前板卡索引
 */
uint8_t bsp_board_get_current_index(void) {
    return current_board_index;
}
