#include "wifi_shell.h"
#include "wifi.h"
#include "shell/sh.h"

#include <string.h>
#include "os/os.h"

#define CONFIG_SYS_LOG_LEVEL SYS_LOG_LEVEL_INF
#define SYS_LOG_DOMAIN "SH"
#include "sys_log.h"

SH_CMD_FN(_open_ap);
SH_CMD_FN(_open_sta);
SH_CMD_FN(_close);

SH_DEF_SUB_CMD(
    sub_wifi,
    SH_SETUP_CMD("oa", "打开 wifi ap 模式", _open_ap, NULL),   //
    SH_SETUP_CMD("os", "打开 wifi sta 模式", _open_sta, NULL), //
    SH_SETUP_CMD("close", "关闭 wifi", _close, NULL),          //
);

SH_DEF_CMD(
    _register_tcp_wifi,
    SH_SETUP_CMD("wifi", "操作 WIFI 接口", NULL, sub_wifi), //
);

void wifi_shell_register(void)
{
    sh_register_cmd(&_register_tcp_wifi);

    /* 初始化 WIFI */
    wifi_init_t init_struct = {
        .ap = {
            .ssid = "wifi-test",    /**< SSID of soft-AP. If ssid_len field is 0, this must be a Null terminated string. Otherwise, length is set according to ssid_len. */
            .password = "12345678", /**< Password of soft-AP. */
            .ip_v4 = {192, 168, 1, 1},
            .gw_v4 = {192, 168, 1, 1},
            .mask_v4 = {255, 255, 255, 0},
            .max_connection = 1,
            .connect_cb = NULL,
        },
        .sta = {
#if 0
            .ssid = "KFXJ",
            .password = "Kfdx201*",
#else
            .ssid = "SDWAN",
            .password = "Dxltkj201",
#endif
            .connect_cb = NULL,
        },
    };
    wifi_netif_init(&init_struct);

    wifi_set_mode(WIFI_NETIF_MODE_STA);
}

void wifi_shell_unregister(void)
{
    sh_unregister_cmd(&_register_tcp_wifi);
}

SH_CMD_FN(_open_ap)
{
    wifi_set_mode(WIFI_NETIF_MODE_AP);
    wifi_start();
    return 0;
}

SH_CMD_FN(_open_sta)
{
    wifi_set_mode(WIFI_NETIF_MODE_STA);
    wifi_start();
    return 0;
}

SH_CMD_FN(_close)
{
    wifi_stop();
    return 0;
}
