// 蓝牙超时处理模块
// 杭州秒寻科技有限公司
// 周涛
// 2024-5-3

#include "includes.h"

// 导入全局变量
extern WIFI_BLE_PARAM_T gWifiBle_stParam;
extern WIFI_PARAM_T gWifi_stParam;

// AT命令发送超时处理函数
void ble_expire_main(uint8 *pbyData)
{
    void (*pFuncPtrVoid)() = NULL;
    // void (*pFuncPtrOneInt)(int) = NULL;
    // void (*pFuncPtrOneChar)(char *) = NULL;
    void (*pFuncPtrTwoChar)(char *, char *) = NULL;
    void (*pFuncPtrTwoParam)(char *, uint16) = NULL;

    uint16 wPort = 0; // 服务器socket监听端口

    timer_stop_timer(gWifiBle_stParam.byTimerID); // 关闭定时器
    gWifiBle_stParam.byMutex = 0;                 // 发送解锁
    // gWifiBle_stParam.byInit = 0;                  // 模块进入初始化

    // 退出透传模式命令超时
    if (NULL != strstr(AT_WIFI_EXIT_TT, (char *)gWifiBle_stParam.abyAtCmd))
    {
        pFuncPtrVoid = wifi_cmd_exit_socket_transparent;
        ble_expire_resend_at_no_param(pFuncPtrVoid, 0);
    }
    // 关闭回显命令超时
    else if (NULL != strstr(AT_WIFIBLE_ATE0, (char *)gWifiBle_stParam.abyAtCmd))
    {
        pFuncPtrVoid = wifi_ble_cmd_close_echo;
        ble_expire_resend_at_no_param(pFuncPtrVoid, 1);
    }
    // 设置WiFi模块模式超时
    else if (NULL != strstr(AT_WIFI_WMODE, (char *)gWifiBle_stParam.abyAtCmd))
    {
        pFuncPtrVoid = wifi_cmd_set_wmode;
        ble_expire_resend_at_no_param(pFuncPtrVoid, 0);
    }
    // 设置WiFi模块DHCP超时
    else if (NULL != strstr(AT_WIFI_WSDHCP, (char *)gWifiBle_stParam.abyAtCmd))
    {
        pFuncPtrVoid = wifi_cmd_set_wsdhcp;
        ble_expire_resend_at_no_param(pFuncPtrVoid, 0);
    }
    // 连接AP超时
    else if (NULL != strstr(AT_WIFI_WJAP, (char *)gWifiBle_stParam.abyAtCmd))
    {
        pFuncPtrTwoChar = wifi_cmd_connect_ap;
        ble_expire_resend_at_two_char_param(pFuncPtrTwoChar, (char *)gWifi_stParam.abyApName, (char *)gWifi_stParam.abyApPassword, 0);
    }
    // 检查AP连接状态超时
    else if (NULL != strstr(AT_WIFI_WJAP_CHECK, (char *)gWifiBle_stParam.abyAtCmd))
    {
        pFuncPtrVoid = wifi_cmd_check_ap_connect_status;
        ble_expire_resend_at_no_param(pFuncPtrVoid, 0);
    }
    // 创建socket超时
    else if (NULL != strstr(AT_WIFI_SOCKET, (char *)gWifiBle_stParam.abyAtCmd))
    {
        pFuncPtrTwoParam = wifi_cmd_create_socket;
        SYS_MEMCPY((void *)&wPort, (void *)gWifi_stParam.abyMonitorPort, 2);
        ble_expire_resend_at_two_param(pFuncPtrTwoParam, (char *)gWifi_stParam.abyMonitorIP, wPort, 0);
    }
    // 检查socket连接状态超时
    else if (NULL != strstr(AT_WIFI_SOCKET_CHECK, (char *)gWifiBle_stParam.abyAtCmd))
    {
        pFuncPtrVoid = wifi_cmd_check_socket_connect_status;
        ble_expire_resend_at_no_param(pFuncPtrVoid, 0);
    }
    // socket进入透传超时
    else if (NULL != strstr(AT_WIFI_SOCKETTT, (char *)gWifiBle_stParam.abyAtCmd))
    {
        pFuncPtrVoid = wifi_cmd_socket_transparent;
        ble_expire_resend_at_no_param(pFuncPtrVoid, 0);
    }

    return;
}

// 超时重发函数，无参数
// byRstFlag:模块重启标识符，0：不重启，1：重启
void ble_expire_resend_at_no_param(void (*pFuncPtr)(), uint8 byRstFlag)
{
    memset(gWifiBle_stParam.abyAtCmd, 0, AT_WIFIBLE_CMD_LEN);

    // 已经超过最大次数
    if (gWifiBle_stParam.byTimes >= AT_WIFI_BLE_SEND_MAX_TIMES)
    {
        gWifiBle_stParam.byTimes = 0; // 发送次数置0

        // 重启模块
        if (byRstFlag)
        {
            // wifi模块重启
            driver_wifi_reset();
            // 10秒后重新配置
            timer_start_timer(gWifiBle_stParam.byTimerID, 1000 * 10, (void *)ble_cmd_module_init_config);
        }
    }
    else
    {
        // gWifiBle_stParam.byTimes++;
        pFuncPtr();
    }

    return;
}

// 超时重发函数，一个整数参数
// byRstFlag:模块重启标识符，0：不重启，1：重启
void ble_expire_resend_at_one_int_param(void (*pFuncPtr)(int), int dwData, uint8 byRstFlag)
{
    memset(gWifiBle_stParam.abyAtCmd, 0, AT_WIFIBLE_CMD_LEN);

    // 已经超过最大次数
    if (gWifiBle_stParam.byTimes >= AT_WIFI_BLE_SEND_MAX_TIMES)
    {
        gWifiBle_stParam.byTimes = 0; // 发送次数置0

        // 重启模块
        if (byRstFlag)
        {
            driver_wifi_reset();                                                                          // wifi模块重启
            timer_start_timer(gWifiBle_stParam.byTimerID, 1000 * 10, (void *)ble_cmd_module_init_config); // 10秒后重新配置
        }
    }
    else
    {
        // gWifiBle_stParam.byTimes++;
        pFuncPtr(dwData);
    }

    return;
}

// 超时重发函数，一个字符参数
void ble_expire_resend_at_one_char_param(void (*pFuncPtr)(char *), char *pbyData, uint8 byRstFlag)
{
    memset(gWifiBle_stParam.abyAtCmd, 0, AT_WIFIBLE_CMD_LEN);

    // 已经超过最大次数
    if (gWifiBle_stParam.byTimes >= AT_WIFI_BLE_SEND_MAX_TIMES)
    {
        gWifiBle_stParam.byTimes = 0; // 发送次数置0

        // 重启模块
        if (byRstFlag)
        {
            driver_wifi_reset();                                                                          // wifi模块重启
            timer_start_timer(gWifiBle_stParam.byTimerID, 1000 * 10, (void *)ble_cmd_module_init_config); // 10秒后重新配置
        }
    }
    else
    {
        // gWifiBle_stParam.byTimes++;
        pFuncPtr(pbyData);
    }

    return;
}

// 超时重发函数，2个字符参数
void ble_expire_resend_at_two_char_param(void (*pFuncPtr)(char *, char *), char *pbyData1, char *pbyData2, uint8 byRstFlag)
{
    memset(gWifiBle_stParam.abyAtCmd, 0, AT_WIFIBLE_CMD_LEN);

    // 已经超过最大次数
    if (gWifiBle_stParam.byTimes >= AT_WIFI_BLE_SEND_MAX_TIMES)
    {
        gWifiBle_stParam.byTimes = 0; // 发送次数置0

        // 重启模块
        if (byRstFlag)
        {
            driver_wifi_reset();                                                                          // wifi模块重启
            timer_start_timer(gWifiBle_stParam.byTimerID, 1000 * 10, (void *)ble_cmd_module_init_config); // 10秒后重新配置
        }
    }
    else
    {
        // gWifiBle_stParam.byTimes++;
        pFuncPtr(pbyData1, pbyData2);
    }

    return;
}

// 超时重发函数，2个参数，1个字符参数，1个整数参数
void ble_expire_resend_at_two_param(void (*pFuncPtr)(char *, uint16), char *pbyData, uint16 wParam, uint8 byRstFlag)
{
    memset(gWifiBle_stParam.abyAtCmd, 0, AT_WIFIBLE_CMD_LEN);

    // 已经超过最大次数
    if (gWifiBle_stParam.byTimes >= AT_WIFI_BLE_SEND_MAX_TIMES)
    {
        gWifiBle_stParam.byTimes = 0; // 发送次数置0

        // 重启模块
        if (byRstFlag)
        {
            driver_wifi_reset();                                                                         // wifi模块重启
            timer_start_timer(gWifiBle_stParam.byTimerID, 1000 * 1, (void *)ble_cmd_module_init_config); // 1秒后重新配置
        }
    }
    else
    {
        // gWifiBle_stParam.byTimes++;
        pFuncPtr(pbyData, wParam);
    }

    return;
}

// 超时处理函数(没有参数)，作为定时器超时响应函数
void ble_expire_no_param()
{
    uint8 *pbyData = "at no resp";
    ble_expire_main(pbyData);
}