
#include "uart_table.h"
#include "uart_interface.h"
#include "utils.h"

static void read_1000(uint8_t memoft, uint8_t *data, uint8_t *read_bytes)
{

    uint8_t size = 0;
    switch (memoft)
    {
    case 0:
        data[0] = 0x12;
        data[1] = 0x13;
        size    = 2;
        break;
    case 1:
        data[0] = 0x14;
        size    = 1;
        break;
    case 2:
        data[0] = 0x14;
        size    = 1;
        break;
    case 3:
        data[0] = 0x14;
        size    = 1;
        break;
    case 4:
        data[0] = 0x14;
        size    = 1;
        break;
    case 5:
        data[0] = 0x14;
        size    = 1;
        break;
    case 6:
        data[0] = 0x14;
        size    = 1;
        break;
    case 7:
        data[0] = 0x14;
        size    = 1;
        break;
    }

    *read_bytes += size;
}

static void read_1100(uint8_t memoft, uint8_t *data, uint8_t *read_bytes)
{

    uint8_t size = 0;
    switch (memoft)
    {
    case 0:
        data[0] = 0x12;
        data[1] = 0x13;
        size    = 2;
        break;
    case 1:
        data[0] = 0x14;
        size    = 1;
        break;
    case 2:
        data[0] = 0x14;
        size    = 1;
        break;
    case 3:
        data[0] = 0x14;
        size    = 1;
        break;
    case 4:
        data[0] = 0x14;
        size    = 1;
        break;
    case 5:
        data[0] = 0x14;
        size    = 1;
        break;
    case 6:
        data[0] = 0x14;
        size    = 1;
        break;
    case 7:
        data[0] = 0x14;
        size    = 1;
        break;
    }

    *read_bytes += size;
}

const regist_read_type mem_read_table[] = {
    {0x1000, read_1000},
    {0x1100, read_1100},
};

uint8_t     aa = 0;
uint8_t     bb = 0;
uint8_t     cc = 0;
static void write_1000(uint8_t memoft, uint8_t *data, uint8_t *read_bytes)
{

    uint8_t size = 0;
    switch (memoft)
    {
    case 0:
        aa   = data[0];
        bb   = data[1];
        size = 2;
        break;
    case 1:
        cc   = data[0];
        size = 1;
        break;
    }

    *read_bytes += size;
}

const regist_read_type mem_write_table[] = {
    {0x1000, write_1000},
};

uint8_t get_regist_value(uart_type *p_data, uint8_t mem_start_addr, uint8_t read_len)
{
    uint8_t  len, i;
    uint8_t  cur_len      = 0;
    uint8_t  mem_addr_oft = 0;
    uint16_t mem_addr     = 0;
    uint8_t *ptr          = 0;

    mem_addr_oft = (uint8_t)(mem_start_addr & 0x000F);
    mem_addr     = mem_start_addr & 0xFFF0;
    len          = read_len;
    ptr          = &p_data->tx[4];

    for (i = 0; i < ARR_SIZE(mem_read_table); i++)
    {
        if ((mem_addr = mem_read_table[i].addr) && mem_read_table[i].p_func != 0)
        {
            do
            {
                mem_read_table[i].p_func(mem_addr_oft++, &ptr[cur_len], &cur_len);
            } while ((cur_len < len) && (mem_addr_oft < 16));

            if (cur_len > len)
            {
                return 0;
            }
            else if (cur_len < len)
            {
                mem_addr     = mem_addr + (mem_addr_oft & 0xFFF0);
                mem_addr_oft = 0;
            }
            else
            {
                return 1;
            }
        }
    }

    return 0;
}

uint8_t set_regist_value(uart_type *p_data, uint8_t mem_start_addr, uint8_t read_len)
{
    uint8_t  len, i;
    uint8_t  cur_len      = 0;
    uint8_t  mem_addr_oft = 0;
    uint16_t mem_addr     = 0;
    uint8_t *ptr          = 0;

    mem_addr_oft = (uint8_t)(mem_start_addr & 0x000F);
    mem_addr     = mem_start_addr & 0xFFF0;
    len          = read_len;
    ptr          = &p_data->rx[5];

    for (i = 0; i < ARR_SIZE(mem_write_table); i++)
    {
        if ((mem_addr = mem_write_table[i].addr) && mem_write_table[i].p_func != 0)
        {
            do
            {
                mem_write_table[i].p_func(mem_addr_oft++, &ptr[cur_len], &cur_len);
            } while ((cur_len < len) && mem_addr_oft < 17);

            if ((mem_addr_oft >= 17) || (cur_len != len))
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
    }

    return 0;
}
