uint16_t WDRV_ScanStart(void)
{
    if (IS_SCAN_IN_PROGRESS(g_scanStatus.scanState))
        return 0;

    WDRV_EXT_CmdScanStart();

    SCAN_SET_IN_PROGRESS(g_scanStatus.scanState);
    SCAN_CLEAR_VALID(g_scanStatus.scanState);
    SCAN_CLEAR_DISPLAY(g_scanStatus.scanState);

    g_wdrv_priv.isScanDone = false;
    g_scanStatus.displayIdx = 0;

    return WDRV_SUCCESS;
}
void WDRV_ScanResultsDisplayManager(void)
{
    static bool displayHeader = true;
    uint8_t i;
    uint8_t ssid[32 + 1];

    if ((g_scanStatus.numberOfResults == 0) ||
        (!IS_SCAN_STATE_DISPLAY(g_scanStatus.scanState)) ||
        (IS_SCAN_IN_PROGRESS(g_scanStatus.scanState)) ||
        (!IS_SCAN_STATE_VALID(g_scanStatus.scanState)))
    {
       SCAN_CLEAR_DISPLAY(g_scanStatus.scanState);
       return;
    }

    if (displayHeader)
    {
        g_scanStatus.displayIdx = 0;
        SYS_CONSOLE_MESSAGE((const char *)"\nWi-Fi Scan Results:\r\n\n");
        if (g_scanStatus.numberOfResults == 0)
        {
            /* No AP found, re-initialize the scan context state. Scan is not allowed in Soft AP mode.
             * However, g_scanStatus.numberOfResults needs to be preserved for webpage using.
             */
            SYS_CONSOLE_MESSAGE((const char *)"No AP found\r\n ");
            SCAN_CLEAR_DISPLAY(g_scanStatus.scanState);
            if (WDRV_CONFIG_PARAMS(networkType) == WDRV_NETWORK_TYPE_SOFT_AP) {
                SCAN_CLEAR_VALID(g_scanStatus.scanState);
                return;
            } else {
                return;
            }
        }
        else
        {
            SYS_CONSOLE_MESSAGE((const char *)"    SSID                              RSSI  Channel\r\n");
            SYS_CONSOLE_MESSAGE((const char *)"    --------------------------------  ----  -------\r\n");
            displayHeader = false;
        }
    }

    // If the g_scanResults[] buffer is full, don't try to store more scan results to it.
    if (g_scanStatus.displayIdx > sizeof(g_scanResults) / sizeof(g_scanResults[0]) - 1)
    {
        SCAN_CLEAR_DISPLAY(g_scanStatus.scanState);
        g_scanStatus.displayIdx = 0;
        displayHeader = true;
        return;
    }
    WDRV_EXT_ScanResultGet(g_scanStatus.displayIdx, &(g_scanResults[g_scanStatus.displayIdx]));

    /* Display SSID */
    memset(ssid, ' ', sizeof(ssid));
    for (i = 0; i < g_scanResults[g_scanStatus.displayIdx].ssidLen; ++i)
    {
        if (!isprint(g_scanResults[g_scanStatus.displayIdx].ssid[i]))
            ssid[i] = '*';
        else
            ssid[i] = g_scanResults[g_scanStatus.displayIdx].ssid[i];
    }
    ssid[32] = 0;

    /* RSSI_MAX : 128, RSSI_MIN : 43 */
    SYS_CONSOLE_PRINT(" %2d)%s  %2u    %u\r\n",
            g_scanStatus.displayIdx + 1,
            ssid,
            g_scanResults[g_scanStatus.displayIdx].rssi,
            g_scanResults[g_scanStatus.displayIdx].channel);

    // If all the scan results have been displayed and stored, clear status and return.
    if (++g_scanStatus.displayIdx >= g_scanStatus.numberOfResults)
    {
        SCAN_CLEAR_DISPLAY(g_scanStatus.scanState);
        g_scanStatus.displayIdx = 0;
        displayHeader = true;
    }
}
void WFDisplayScanMgr(void)
{
    DRV_WIFI_SCAN_RESULT bssDesc;
    char ssid[32 + 1];
    int i;

    if ((SCANCXT.numScanResults == 0)               ||
            (!IS_SCAN_STATE_DISPLAY(SCANCXT.scanState)) ||
            (IS_SCAN_IN_PROGRESS(SCANCXT.scanState))    ||
            (!IS_SCAN_STATE_VALID(SCANCXT.scanState)))
    {
        SCAN_CLEAR_DISPLAY(SCANCXT.scanState);
        return;
    }

    WFRetrieveScanResult(SCANCXT.displayIdx, &bssDesc);

    memset(ssid, ' ', sizeof(ssid));

    /* Display SSID */
    for (i = 0; i < bssDesc.ssidLen; i++)
    {
        if (!isprint(bssDesc.ssid[i]))
        {
            ssid[i] = '*';
        }
        else
        {
            ssid[i] = bssDesc.ssid[i];
        }
    }

    ssid[32] = 0;

    /* Display Index & SSID */
    //SYS_CONSOLE_PRINT("%2d)  ",SCANCXT.displayIdx + 1);
    //SYS_CONSOLE_PRINT("%s", ssid);

    /* Display RSSI & Channel */
    /* RSSI_MAX : 128, RSSI_MIN : 43 */
    //SYS_CONSOLE_PRINT(" %2u    %u\r\n", bssDesc.rssi, bssDesc.channel);

    SYS_CONSOLE_PRINT(" %2d) %s %2u    %u\r\n", SCANCXT.displayIdx + 1, ssid, bssDesc.rssi, bssDesc.channel);

    if (p_wifi_ConfigData->networkType == DRV_WIFI_NETWORK_TYPE_SOFT_AP)
    {
        preScanResult[SCANCXT.displayIdx] = bssDesc; // WF_PRESCAN
        if (SCANCXT.displayIdx == sizeof(preScanResult) / sizeof(preScanResult[0]) - 1)
        {
            /* Prescan is finished, re-initialize the scan context state. Scan is not allowed in Soft AP mode.
             * However, SCANCXT.numScanResults needs to be preserved for webpage using.
             */
            SCAN_CLEAR_VALID(SCANCXT.scanState);
            SCAN_CLEAR_DISPLAY(SCANCXT.scanState);
            SCANCXT.displayIdx = 0;
        }
    }
    else
    {
#if (DRV_WIFI_DEFAULT_ADHOC_PRESCAN == DRV_WIFI_ENABLED)
        preScanResult[SCANCXT.displayIdx] = bssDesc; // WF_PRESCAN
        if (SCANCXT.displayIdx == sizeof(preScanResult) / sizeof(preScanResult[0]) - 1)
        {
            SCAN_CLEAR_DISPLAY(SCANCXT.scanState);
            SCANCXT.displayIdx = 0;
        }
#endif
    }

    if (++SCANCXT.displayIdx == SCANCXT.numScanResults)
    {
        if (p_wifi_ConfigData->networkType == DRV_WIFI_NETWORK_TYPE_SOFT_AP)
        {
            /* Prescan is finished, re-initialize the scan context state. Scan is not allowed in Soft AP mode.
             * However, SCANCXT.numScanResults needs to be preserved for webpage using.
             */
            SCAN_CLEAR_VALID(SCANCXT.scanState);
            SCAN_CLEAR_DISPLAY(SCANCXT.scanState);
            SCANCXT.displayIdx = 0;
        }
        else
        {
            SCAN_CLEAR_DISPLAY(SCANCXT.scanState);
            SCANCXT.displayIdx = 0;
        }
    }
}