/************************************************************************************************
 *                                          Include                                              *
 ************************************************************************************************/

#include "ble.h"
#include "ble_cmd.h"
#include "ble_sn2model.h"
#include "stm32f4xx_gpio.h"
#include "systick.h"
#include "uart.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

/************************************************************************************************
 *                                          Config                                                *
 ************************************************************************************************/

uint8_t             zero_sn[];
extern BLE_CMD_ASYN g_ble_cmd_asyn_table[];

static uint8_t BUSY_IND                     = 0; // 0、空闲；1、繁忙
static uint8_t AUTH_IND                     = 0; // 0、未认证；1、认证
static uint8_t BLE_CONN_FLG                 = 0;
uint8_t        g_ble_mac[MAC_LEN]           = {0};
uint8_t        g_ble_name[BLE_NAME_LEN + 1] = {0};
uint8_t        g_ble_adv_data[28]           = {0};
uint8_t        g_ble_rst                    = 0; // 0、运行；1、复位
BLE_CMD_CACHE  g_ble_cmd_cache;                  // 命令缓存
/************************************************************************************************
 *                                          Data structs                                         *
 ************************************************************************************************/

void ble_reset(void)
{
    GPIO_ResetBits(BT_RST_PORT, BT_RST_PIN);
    ms_delay(1);
    GPIO_SetBits(BT_RST_PORT, BT_RST_PIN);
}

void ble_init(void)
{
    usart_config_init(&usart2_context, 9600);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

    GPIO_InitTypeDef GPIO_StructInit;
    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_OUT;
    GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
    GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_StructInit.GPIO_Pin   = BT_RST_PIN;
    GPIO_Init(BT_RST_PORT, &GPIO_StructInit);

    ble_reset();

    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_IN;
    GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
    GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_StructInit.GPIO_Pin   = BT_BUSY_IND_PIN;
    GPIO_Init(BT_BUSY_IND_PORT, &GPIO_StructInit);

    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_IN;
    GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
    GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_StructInit.GPIO_Pin   = BT_CONN_IND_PIN;
    GPIO_Init(BT_CONN_IND_PORT, &GPIO_StructInit);
}

// 蓝牙名称
int32_t ble_cmd08_action_asyn(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len)
{
    RSP_CMD08 *ptr;

    if (status != 0)
    {
        return status;
    }
    ptr = (RSP_CMD08 *)data;
    strcpy(g_ble_name, ptr->name);
    return 0;
}

// MAC地址
int32_t ble_cmd18_action_asyn(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len)
{
    RSP_CMD18 *ptr;

    if (status != 0)
    {
        return status;
    }
    ptr = (RSP_CMD18 *)data;
    if (ptr->item0.item == 3)
    {
        memcpy(g_ble_mac, ptr->item3.mac, sizeof(g_ble_mac));
        // OPP_SWAP_N(g_ble_mac, mac_len);
    }

    return 0;
}

// 广播数据
int32_t ble_cmd0e_action_asyn(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len)
{
    RSP_CMD0E *ptr;

    if (status != 0)
    {
        return status;
    }
    ptr = (RSP_CMD0E *)data;
    memcpy(g_ble_adv_data, ptr->adv_data, sizeof(g_ble_adv_data));
    return 0;
}

// 序列初始化
int32_t ble_cfg_init_asyn(EN_BLE_INIT_CMD cmd)
{
    int32_t ret;

    REQ_CMD07 cmd07;
    REQ_CMD08 cmd08;

    uint8_t SN[SN_LEN]           = {0};
    uint8_t UPP[SN_LEN]          = {0};
    uint8_t model[MODEL_LEN]     = {0};
    uint8_t upp_model[MODEL_LEN] = {0};

    uint8_t buff[64] = {0};

    if (g_ble_cmd_cache.is_used == 1)
    {
        return 1;
    }

    if (cmd == BLE_INIT_CMD_NAME_QRY)
    {
        g_ble_cmd_cache.is_used = 1;
        g_ble_cmd_cache.cmd     = 0x08;
        g_ble_cmd_cache.len     = sizeof(REQ_CMD08);
        memcpy(g_ble_cmd_cache.data, &cmd08, sizeof(REQ_CMD08));
        g_ble_cmd_cache.p_func = ble_cmd08_action_asyn;
        g_ble_cmd_cache.tick   = 0;
    }

    if (cmd == BLE_INIT_CMD_NAME_CFG)
    {
        // int32_t len = alientek_sn_get(SN);
        int32_t len = 12;
        ret         = sn_2_model(SN, 1, model);

        if (ret == 0)
        {
            len = sprintf(buff, "%s", model);
            // upp_sn_get(UPP);
            if (memcpy(UPP, zero_sn, SN_LEN) != 0)
            {
                ret = sn_2_model((uint8_t *)UPP, 1, upp_model);
                if (ret == 0)
                {
                    sprintf(buff + len, " %s", upp_model);
                }
            }
            if (strcpy(g_ble_name, buff) != 0)
            {
                strcpy(cmd07.name, buff);
                g_ble_cmd_cache.cmd = 0x07;
                g_ble_cmd_cache.len = strlen(buff);
                strcpy(&g_ble_cmd_cache.data, buff);
                g_ble_cmd_cache.p_func = NULL;
                g_ble_cmd_cache.tick   = 0;
            }
        }
    }

    if (cmd == BLE_INIT_CMD_TXWPR_CFG)
    {
        REQ_CMD0B req0b;
        req0b.tx_power = 8;
        ret            = ble_exec_cmd0b(&req0b);

        if (ret == 0)
        {
        }
    }
    return 0;
}

int32_t ble_cfg_init_do_asyn(void)
{
    static EN_BLE_INIT_CMD cmd = BLE_INIT_CMD_NAME_QRY;

    int32_t ret;

    ret = ble_cfg_init_asyn(cmd);

    if (ret == 0)
    {
        cmd++;
    }

    if (cmd == BLE_INIT_CMD_MAX)
    {
        cmd = BLE_INIT_CMD_NAME_QRY;
        return 0;
    }
    return 1;
}

int32_t ble_cfg_init(void)
{
    int32_t   ret;
    REQ_CMD07 cmd07;
    REQ_CMD08 cmd08;
    RSP_CMD08 rsp08;
    uint8_t   SN[SN_LEN]       = {0};
    uint8_t   model[MODEL_LEN] = {0};

    memset(&rsp08, 0, sizeof(RSP_CMD08));
    ret = ble_exec_cmd08(&cmd08, &rsp08);
    if (ret == 0)
    {
    }

    int32_t len = 12;
    // int32_t len = alientek_sn_get(SN);
    ret = sn_2_model(SN, 1, model);

    if (ret == 0)
    {
        if (strcmp(rsp08.name, model) != 0)
        {
            strcpy(cmd07.name, model);
            ret = ble_exec_cmd07(&cmd07, CMD_T0);
            if (ret != 0)
            {
            }
        }
    }
    else
    {
    }

    return 0;
}

int32_t ble_cfg_query_asyn(EN_BLE_INIT_CMD cmd)
{
    REQ_CMD18 cmd18;
    REQ_CMD0E cmd0e;
    uint8_t   buff[64] = {0};
    int32_t   len      = 0;

    if (g_ble_cmd_cache.is_used == 1)
    {
        return 1;
    }

    if (cmd == BLE_QRY_CMD_ADV_QRY)
    {
        g_ble_cmd_cache.is_used = 1;
        g_ble_cmd_cache.cmd     = 0x0e;
        g_ble_cmd_cache.len     = sizeof(REQ_CMD0E);
        memcpy(g_ble_cmd_cache.data, &cmd0e, sizeof(REQ_CMD0E));
        g_ble_cmd_cache.p_func = ble_cmd0e_action_asyn;
        g_ble_cmd_cache.tick   = 0;
    }

    if (cmd == BLE_QRY_CMD_MAC_QRY)
    {
        cmd18.item              = 3;
        g_ble_cmd_cache.is_used = 1;
        g_ble_cmd_cache.cmd     = 0x18;
        g_ble_cmd_cache.len     = sizeof(REQ_CMD18);
        memcpy(g_ble_cmd_cache.data, &cmd0e, sizeof(REQ_CMD18));
        g_ble_cmd_cache.p_func = ble_cmd18_action_asyn;
        g_ble_cmd_cache.tick   = 0;
    }

    if (cmd == BLE_QRY_CMD_ADV_CFG)
    {
        if (memcpy(g_ble_mac, g_ble_adv_data, MAC_LEN) != 0)
        {
            buff[len++] = 1;
            buff[len++] = 1 + MAC_LEN;
            buff[len++] = 0xFF;
            memcpy(&buff[len], g_ble_mac, MAC_LEN);
            len += MAC_LEN;
            g_ble_cmd_cache.is_used = 1;
            g_ble_cmd_cache.cmd     = 0x0d;
            g_ble_cmd_cache.len     = len;
            memcpy(g_ble_cmd_cache.data, buff, len);
            g_ble_cmd_cache.p_func = NULL;
            g_ble_cmd_cache.tick   = 0;
        }
    }
    return 0;
}

int32_t ble_cfg_query_do_asyn(void)
{
    static EN_BLE_QRY_CMD cmd = BLE_QRY_CMD_ADV_QRY;

    int32_t ret;

    ret = ble_cfg_query_asyn(cmd);

    if (ret == 0)
    {
        cmd++;
    }

    if (cmd == BLE_QRY_CMD_MAX)
    {
        cmd = BLE_QRY_CMD_ADV_QRY;
        return 0;
    }
    return 1;
}

int32_t ble_cfg_query(void)
{
    int32_t   ret;
    REQ_CMD18 cmd18;
    RSP_CMD18 rsp18;
    REQ_CMD0A cmd0a;
    RSP_CMD0A rsp0a;
    REQ_CMD0C cmd0c;
    RSP_CMD0C rsp0c;
    REQ_CMD0E cmd0e;
    RSP_CMD0E rsp0e;
    uint8_t   buff[64]      = {0};
    int32_t   len           = 0;
    int32_t   adv_data_len  = 0;
    uint8_t  *adv_data_ptr  = NULL;
    uint8_t   adv_data_uuid = 0;

    ret = ble_exec_cmd0e(&cmd0e, &rsp0e);
    if (ret == 0)
    {
        adv_data_len  = rsp0e.adv_data[0];
        adv_data_uuid = rsp0e.adv_data[1];
        adv_data_ptr  = &rsp0e.adv_data[2];

        // len += sprintf(buff+len)
    }

    cmd18.item = 3;
    ret        = ble_exec_cmd18(&cmd18, &rsp18);
    if (ret == 0)
    {
        memcpy(g_ble_mac, rsp18.item3.mac, len);
        // OPP_SWAP_N(g_ble_mac, mac_len);
        if (adv_data_len == 0 || adv_data_ptr == NULL || memcmp(rsp18.item3.mac, adv_data_ptr, MAC_LEN) != 0)
        {
            ret = ble_exec_cmd0d(1, rsp18.item3.mac, MAC_LEN);

            if (ret != 0)
            {
            }
        }
    }
    else
    {
    }

    cmd18.item = 5;
    ret        = ble_exec_cmd18(&cmd18, &rsp18);
    if (ret == 0)
    {
    }

    ret = ble_exec_cmd0a(&cmd0a, &rsp0a);
    if (ret == 0)
    {
    }

    ret = ble_exec_cmd0c(&cmd0c, &rsp0c);
    if (ret == 0)
    {
    }
}

int32_t BUSY_IND_LOOP(void)
{
    static uint32_t tick = 0;
    static uint32_t cnt  = 0;
    uint8_t         busy;

    if (tick == 0)
    {
        tick = get_systick_ms();
    }

    if (get_systick_ms() - tick > 50)
    {
        if (cnt++ < 2)
        {
            return 0;
        }
        else
        {
            cnt = 0;
        }

        if (GPIO_ReadInputDataBit(BT_BUSY_IND_PORT, BT_BUSY_IND_PIN) == 0)
        {
            busy = 1;
        }
        else
        {
            busy = 0;
        }

        if (BUSY_IND != busy)
        {
            BUSY_IND = busy;
        }
    }

    return 0;
}

int32_t CONN_IND_LOOP(void)
{
    static uint32_t tick = 0;
    static uint32_t cnt  = 0;
    uint8_t         conn;

    if (tick == 0)
    {
        tick = get_systick_ms();
    }

    if (get_systick_ms() - tick > 50)
    {
        if (cnt++ < 2)
        {
            return 0;
        }
        else
        {
            cnt = 0;
        }

        if (GPIO_ReadInputDataBit(BT_CONN_IND_PORT, BT_CONN_IND_PIN) == 0)
        {
            conn = 1;
        }
        else
        {
            conn = 0;
        }

        if (BLE_CONN_FLG != conn)
        {
            BLE_CONN_FLG = conn;
            if (BLE_CONN_FLG == 0)
            {
                AUTH_IND = 0;
            }
        }
    }

    return 0;
}

int32_t auth_ind_set(uint8_t auth)
{
    AUTH_IND = auth;
    return 0;
}

int32_t ble_is_conn(void)
{
    return BLE_CONN_FLG;
}

int32_t ble_get_busy_port_sts(void)
{
    return BUSY_IND;
}

int32_t ble_is_busy(void)
{
    if (BLE_CONN_FLG == 0 || BUSY_IND == 1 || g_ble_cmd_cache.is_used == 1)
    {
        return 1;
    }
    return 0;
}

int32_t ble_is_auth(void)
{
    return AUTH_IND;
}

int32_t ble_data_recv(uint8_t data[], uint8_t length, int32_t ms)
{
    static uint32_t tick = 0;
    uint8_t         rdata[BLE_MTU];
    int32_t         rlen;

    if (tick == 0)
    {
        tick = get_systick_ms();
    }

    do
    {
        rlen = usart_read_ble_format(&usart2_context);
        if (rlen > 0)
        {
            usart_read_update(&usart2_context, rlen, (uint8_t *)rdata);

            if (rdata[2] == g_ble_cmd_cache.cmd)
            {
                if (g_ble_cmd_cache.p_func != NULL)
                {
                    g_ble_cmd_cache.p_func(rdata[2], rdata[4], rdata, rlen);
                }

                memset(&g_ble_cmd_cache, 0, sizeof(BLE_CMD_CACHE));
            }

            if (rdata[2] == 0x42)
            {
                // ble_2_app((uint8_t *)&rdata[4], rlen - 4);
                tick = 0;
                return 0;
            }
            else
            {
                memcpy(data, rdata, rlen);
                tick = 0;
                return rlen;
            }
        }
        else
        {
        }
    } while (get_systick_ms() - tick < ms);
    tick = 0;
    return -1;
}

int32_t ble_send_data(uint8_t data[], int32_t length)
{
    if (length > BLE_MTU)
    {
        return 1;
    }
    g_ble_cmd_cache.is_used = 1;
    g_ble_cmd_cache.cmd     = 0x41;
    g_ble_cmd_cache.len     = length;
    memcpy(g_ble_cmd_cache.data, data, length);
    g_ble_cmd_cache.tick   = 0;
    g_ble_cmd_cache.p_func = NULL;
    return 0;
}

int32_t ble_rcv_loop(void)
{
    uint8_t rdata[BLE_MTU];
    ble_data_recv(rdata, sizeof(rdata), 0);
    return 0;
}

int32_t ble_name_modify(void)
{
    static uint8_t last_sn[SN_LEN] = {0};

    if (ble_is_conn())
    {
        return 0;
    }
}

int32_t ble_tx_loop(void)
{
    static uint8_t count = 0;

    if (g_ble_cmd_cache.is_used == 1)
    {
        if (g_ble_cmd_cache.tick == 0)
        {
            g_ble_cmd_cache.tick = get_systick_ms();
            count                = 0;
        }

        if (count == 0 || get_systick_ms() - g_ble_cmd_cache.tick > 3000)
        {
            for (int32_t i = 0; i < countof_asyn_cmd_table(); i++)
            {
                if (g_ble_cmd_asyn_table[i].cmd == g_ble_cmd_cache.cmd && g_ble_cmd_asyn_table[i].p_func != NULL)
                {
                    g_ble_cmd_asyn_table[i].p_func(g_ble_cmd_cache.cmd, g_ble_cmd_cache.data, g_ble_cmd_cache.len);
                }
            }

            g_ble_cmd_cache.tick = get_systick_ms();
            count++;
        }

        if (count > 10)
        {
            ble_reset();
            g_ble_rst = 1;
            memset(&g_ble_cmd_cache, 0, sizeof(BLE_CMD_CACHE));
            count = 0;
        }
    }

    return 0;
}

int32_t ble_loop(void)
{
    static uint8_t  init  = 0;
    static uint32_t tick  = 0;
    static uint8_t  init1 = 0;
    static uint32_t tick1 = 0;
    int32_t         ret;

    if (g_ble_rst == 1)
    {
        init      = 0;
        init1     = 0;
        g_ble_rst = 0;
    }

    if (!init)
    {
        if (tick == 0)
            tick = get_systick_ms();

        if (get_systick_ms() - tick > BLE_CFG_DELAY_MS)
        {
            ret = ble_cfg_init_do_asyn();
            if (ret == 0)
            {
                init = 1;
            }
        }
    }

    if (init && !init1)
    {
        if (tick1 == 0)
            tick1 = get_systick_ms();

        if (get_systick_ms() - tick1 > BLE_QRY_DELAY_MS)
        {
            ret = ble_cfg_query_do_asyn();
            if (ret == 0)
            {
                init1 = 1;
            }
        }
    }

    ble_rcv_loop();

    if (init && init1)
    {
        BUSY_IND_LOOP();
        CONN_IND_LOOP();
        ble_name_modify();
    }

    ble_tx_loop();

    return 0;
}
/************************************************************************************************
 *                                          implements                                           *
 ************************************************************************************************/
