/**
 * @file           bsp_boardcheck.h
 * @brief          
 * @author         plyang
 * @version        1.0
 * @date           20230427
 * 
 * @copyright Copyright (c) 2026 by OLE, All Rights Reserved.
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date        <th>Version         <th>Author          <th>Description
 * <tr><td>20230427    <td>2.0             <td>test            <td>内容
 * </table>
 */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bsp_boardcheck.h"
#include "gpio.h"
#include "debug_print.h"

#define BOARD_MAX           BOAED_NUM
#define BOARD_CHECK_TIME    100 //(单位ms)

static t_bsp_boardcheck m_board[BOARD_MAX];
static char *board_name[BOARD_MAX] = {"DI ", "AC ", "DO "};

static int bsp_read_board(uint8_t n)
{
    uint8_t ret = 0xff;

    switch (n)
    {
    case BOARD_DI:
        ret = gpioio_get_gpio_status(GPIO_INT_M1); /*M1-DI*/
        break;

    case BOARD_AC:
        ret = gpioio_get_gpio_status(GPIO_INT_M3); /*M3-ADC*/
        break;

    case BOARD_DO:
        ret = gpioio_get_gpio_status(GPIO_INT_M4); /*M4 DO*/
        break;

    default:
        ret = 0xff;
        break;
    }
    if (ret < 0)
    {
        return 0;
    }
    return (int)ret;
}

static int board_hot_swap_handl(int n)
{
    if (m_board[n].board_down && m_board[n].board_up)
    {
        m_board[n].time += BOARD_CHECK_TIME;
        m_board[n].setTime = 2000;
        if (m_board[n].time > m_board[n].setTime)
        {
            m_board[n].board_state = 1;
            switch (n)
            {
            case BOARD_DI:
                /* DI */
                break;

            case BOARD_AC:
                /*  key*/
                break;

            case BOARD_DO:
                /*AC  code */
                break;

            default:
                break;
            }

            m_board[n].board_down = 0;
            m_board[n].time = 0;
            dp_info_n_c("%s 板卡%d 插入", board_name[n], (n + 1));
        }
    }
    else
    {
        m_board[n].time = 0;
    }
    return 0;
}

int bsp_board_check(void)
{
    uint8_t i = 0;
    static uint8_t oldstate[BOARD_MAX] = {0};
    static uint8_t state[BOARD_MAX] = {0};
    static uint8_t sflg = 1;

    if (sflg)
    {
        sflg = 0;
        for (i = 0; i < BOARD_MAX; i++)
        {
            oldstate[i] = bsp_read_board(i) == 0 ? 1 : 0;
            state[i] = oldstate[i];
            m_board[i].board_state = oldstate[i];
            if (oldstate[i])
            {
                m_board[i].board_up = 1;
                m_board[i].board_down = 0;
                dp_info_n_c("%s 板卡%d 插入", board_name[i], i + 1);
            }
            else
            {
                m_board[i].board_down = 1;
                m_board[i].board_up = 0;
                dp_info_n_c("%s 板卡%d 拨出", board_name[i], i + 1);
            }
        }
    }
    else
    {
        for (i = 0; i < BOARD_MAX; i++)
        {
            state[i] = bsp_read_board(i) == 0 ? 1 : 0;
            if (state[i] == 1 && oldstate[i] == 0)
            {
                oldstate[i] = state[i];
                m_board[i].board_up = 1;
                m_board[i].board_state = 1;
            }
            else if (state[i] == 0 && oldstate[i] == 1)
            {
                oldstate[i] = state[i];
                m_board[i].board_down = 1;
                m_board[i].board_up = 0;
                m_board[i].board_state = 0;
                dp_info_n_c("%s 板卡%d 拨出", board_name[i], i + 1);
            }

            board_hot_swap_handl(i);
        }
    }

    return 0;
}

int bap_get_board_state(uint8_t n)
{
    uint8_t var = 0;

    if (n > BOARD_MAX)
    {
        return -1;
    }

    var = bsp_read_board(n) == 0 ? 1 : 0;

    return (m_board[n].board_state & var);
}

int bapbcio_get_board_state(uint8_t n)
{
    return (m_board[n].board_state);
}

int bsp_board_init(void)
{
    bsp_board_check();
}