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

#include "includes.h"

// 定义wifi模块全局变量
WIFI_PARAM_T gWifi_stParam = {0}; // wifi模块参数

// 导出全局变量
extern WIFI_BLE_PARAM_T gWifiBle_stParam;

// 配置模块参数
// pstParam:模块参数
void wifi_cmd_set_param(WIFI_PARAM_T *pstParam)
{
    SYS_MEMCPY(&gWifi_stParam, pstParam, sizeof(WIFI_PARAM_T));
}

// 设置wifi工作模式（STA模式，不保存到flash）
void wifi_cmd_set_wmode()
{
    int8 abyAtValue[AT_WIFIBLE_CMD_VALUE_LEN] = {0};
    sprintf(abyAtValue, "%s", "=1,0\r\n"); // 拼接字符串

    wifi_ble_send_at_cmd(AT_WIFI_WMODE, abyAtValue, 2);

    return;
}

// 设置STA模式下DHCP参数(自动获得IP)
void wifi_cmd_set_wsdhcp()
{
    int8 abyAtValue[AT_WIFIBLE_CMD_VALUE_LEN] = {0};
    sprintf(abyAtValue, "%s", "=1\r\n"); // 拼接字符串

    wifi_ble_send_at_cmd(AT_WIFI_WSDHCP, abyAtValue, 2);

    return;
}

// 连接AP
// pbySsid:AP名称
// pbySsid:密码
void wifi_cmd_connect_ap(char *pbySsid, char *pbyPwd)
{
    int8 abyAtValue[AT_WIFIBLE_CMD_VALUE_LEN] = {0};

    sprintf(abyAtValue, "%s%s,%s%s", "=", pbySsid, pbyPwd, "\r\n"); // 拼接字符串
    wifi_ble_send_at_cmd(AT_WIFI_WJAP, abyAtValue, 2);

    return;
}

// 检查AP连接状态
void wifi_cmd_check_ap_connect_status()
{
    int8 abyAtValue[AT_WIFIBLE_CMD_VALUE_LEN] = {0};
    sprintf(abyAtValue, "%s", "\r\n"); // 拼接字符串

    wifi_ble_send_at_cmd(AT_WIFI_WJAP_CHECK, abyAtValue, 2);

    return;
}

// 创建tcp socket连接(客户端模式),2:UDP，4:TCP
// pbyIp:IP地址
// wPort:端口
void wifi_cmd_create_socket(char *pbyIp, uint16 wPort)
{
    int8 abyAtValue[AT_WIFIBLE_CMD_VALUE_LEN] = {0};
    sprintf(abyAtValue, "%s%d,%s,%d,%d%s", "=", 4, pbyIp, wPort, 90, "\r\n"); // 拼接字符串

    wifi_ble_send_at_cmd(AT_WIFI_SOCKET, abyAtValue, 2);

    return;
}

// 检查udp socket连接状态
void wifi_cmd_check_socket_connect_status()
{
    int8 abyAtValue[AT_WIFIBLE_CMD_VALUE_LEN] = {0};
    sprintf(abyAtValue, "%s", "\r\n"); // 拼接字符串

    wifi_ble_send_at_cmd(AT_WIFI_SOCKET_CHECK, abyAtValue, 2);

    return;
}

// 进入socket透传模式
void wifi_cmd_socket_transparent()
{
    int8 abyAtValue[AT_WIFIBLE_CMD_VALUE_LEN] = {0};
    sprintf(abyAtValue, "%s", "\r\n"); // 拼接字符串

    wifi_ble_send_at_cmd(AT_WIFI_SOCKETTT, abyAtValue, 2);

    return;
}

// 退出透传模式，进入AT命令模式
void wifi_cmd_exit_socket_transparent()
{
    uint8 *pbyData = AT_WIFI_EXIT_TT;

    driver_wifi_send_data(pbyData, 3);
    return;
}