// wifi&ble模块发送命令模块
// 杭州秒寻科技有限公司
// 周涛
// 2024-9-22

#include "includes.h"

// 模块管理参数
WIFI_BLE_PARAM_T gWifiBle_stParam = {0};

// 模块初始化
uint8 wifi_ble_init(void)
{
    uint8 byFlag = 0;

    // 初始化参数
    memset((void *)&gWifiBle_stParam, 0, sizeof(WIFI_BLE_PARAM_T));
    gWifiBle_stParam.byInit = 1;

    // 创建定时器
    byFlag = timer_create_timer(&gWifiBle_stParam.byTimerID, ETIMER_ONCE);
    if (!byFlag)
    {
        return byFlag;
    }

    // 关闭回显
    wifi_ble_cmd_close_echo();

    // TODO
    // wifi_ble_cmd_get_version();

    return 1;
}

// 发送AT命令
// pbyAtCmd:AT命令
// pbyCmdValue:AT命令内容
// byWorkStatus:模块工作状态，1：初始状态，2：正常状态
uint8 wifi_ble_send_at_cmd(uint8 *pbyAtCmd, uint8 *pbyCmdValue, uint8 byWorkStatus)
{
    uint8 byFlag = 0;
    uint8 abyAtDataList[AT_WIFIBLE_CMD_VALUE_LEN] = {0};

    strcat((char *)abyAtDataList, (char *)pbyAtCmd);    // 拼接字符串
    strcat((char *)abyAtDataList, (char *)pbyCmdValue); // 拼接字符串

    timer_stop_timer(gWifiBle_stParam.byTimerID); // 关闭定时器
    if (gWifiBle_stParam.byMutex <= 0)
    {
        // 发送数据
        driver_delay_nxms(100);
        byFlag = driver_wifi_send_data(abyAtDataList, (uint16)strlen((char *)abyAtDataList));
        if (!byFlag)
        {
            return byFlag;
        }

        // 更新模块状态
        wifi_ble_update_module_status(pbyAtCmd, strlen(pbyAtCmd), byWorkStatus);
    }

    return 1;
}

// 更新模块状态
void wifi_ble_update_module_status(int8 *pbyAtCmd, uint8 byLen, uint8 byWorkStatus)
{
    // 设置模块工作模式
    memcpy(gWifiBle_stParam.abyAtCmd, pbyAtCmd, byLen);
    gWifiBle_stParam.byInit = byWorkStatus; // 模块工作状态
    gWifiBle_stParam.byMutex = 1;           // 发送加锁
    gWifiBle_stParam.byTimes++;             // 发送次数加1

    timer_start_timer(gWifiBle_stParam.byTimerID, 1000 * 5, (void *)ble_expire_no_param);

    return;
}

// 复位模块状态
void wifi_ble_reset_module_status()
{
    memset(gWifiBle_stParam.abyAtCmd, 0, AT_WIFIBLE_CMD_LEN);
    gWifiBle_stParam.byMutex = 0; // 发送去锁
    gWifiBle_stParam.byTimes = 0; // 发送次数清零

    return;
}

// 关闭回显功能
void wifi_ble_cmd_close_echo()
{
    uint8 abyAtValue[AT_WIFIBLE_CMD_VALUE_LEN] = {0};
    sprintf(abyAtValue, "%s", "\r\n"); // 拼接字符串

    wifi_ble_send_at_cmd(AT_WIFIBLE_ATE0, abyAtValue, 1);

    return;
}

// 查询版本信息
void wifi_ble_cmd_get_version()
{
    uint8 abyAtValue[AT_WIFIBLE_CMD_VALUE_LEN] = {0};
    sprintf(abyAtValue, "%s", "\r\n"); // 拼接字符串

    wifi_ble_send_at_cmd(AT_WIFIBLE_GMR, abyAtValue, 1);
}

// 模块复位
void wifi_ble_reset()
{
    // 模块状态保护
    uint8 byTimerId = gWifiBle_stParam.byTimerID;
    SYS_MEMSET(&gWifiBle_stParam, 0, sizeof(WIFI_BLE_PARAM_T));
    gWifiBle_stParam.byTimerID = byTimerId;
    gWifiBle_stParam.byInit = 1;

    // wifi模块重启
    driver_wifi_reset();

    // 10秒后重新配置
    timer_start_timer(gWifiBle_stParam.byTimerID, 1000 * 1, (void *)wifi_ble_cmd_close_echo);
}

// 获得模块socket透传模式
uint8 wifi_ble_get_transparent_mode()
{
    return gWifiBle_stParam.byTransparentFlag;
}

// 重新初始化wifi模块
void wifible_reinit()
{
    // 模块状态保护
    uint8 byTimerId = gWifiBle_stParam.byTimerID;
    uint8 byTransparentFlag = gWifiBle_stParam.byTransparentFlag;

    SYS_MEMSET(&gWifiBle_stParam, 0, sizeof(WIFI_BLE_PARAM_T));
    gWifiBle_stParam.byTimerID = byTimerId;
    gWifiBle_stParam.byInit = 1;

    // wifi模块重启
    driver_wifi_reset();

    // 1秒后重新配置
    if (byTransparentFlag)
    {
        gWifiBle_stParam.byTransparentFlag = 0;
        timer_start_timer(gWifiBle_stParam.byTimerID, 1000 * 1, (void *)wifi_cmd_exit_socket_transparent);
    }
    else
    {
        timer_start_timer(gWifiBle_stParam.byTimerID, 1000 * 1, (void *)wifi_ble_cmd_close_echo);
    }
}