1.4.4 Wi-Fi System Service Developer's Guide

Code modification/implementation guide for common use cases

Wi-Fi System Service Auto Connect

When user enables the auto connect features using Wi-Fi service system MHC menu, the default auto connect retry count is configured to 5. User can modify the macro MAX_AUTO_CONNECT_RETRY in generated file sys_wifi.c as per their choice. PIC32MZW1 device stops the connection request to AP after retry count reaches to zero and successful connections with AP is not established.

/* Wi-Fi STA Mode, maximum auto connect retry */
#define MAX_AUTO_CONNECT_RETRY                5

Wi-Fi System Service Scan Example

Wi-Fi System Service support scan functinality both in STA and AP mode. In case, user want to overwrite MHC configuration of the scan parameter,please find the below snippet code that need to be added in the application.

#include "wdrv_pic32mzw_bssfind.h"

APP_DATA             appData;
SYS_WIFI_SCAN_CONFIG scanConfig;

/* Wi-Fi driver triggers a callback to update each Scan result one-by-one*/
bool APP_ScanHandler (DRV_HANDLE handle, uint8_t index, uint8_t ofTotal, WDRV_PIC32MZW_BSS_INFO *pBSSInfo)
{
    if (0 == ofTotal) 
    {
        SYS_CONSOLE_MESSAGE("No AP Found... Rescan\r\n");
    } 
    else 
    {
        if (index == 1)
        {
            SYS_CONSOLE_PRINT("Scan Results: #%02d\r\n", ofTotal);
        }
        SYS_CONSOLE_PRINT("[%02d] %s\r\n", index, pBSSInfo->ctx.ssid.name);
    }
    // return true to receive further results; otherwise return false if desired
    return true;
}

void APP_Initialize(void) {
    appData.state = APP_STATE_INIT;
}

void APP_Tasks(void) {
    
    SYS_WIFI_RESULT res;
    SYS_WIFI_STATUS wifiStatus;
    
    switch (appData.state) 
    {
        case APP_STATE_INIT:
        {
            appData.state = APP_STATE_TRIGGER_SCAN;
            break;
        }
        
        case APP_STATE_TRIGGER_SCAN:
        {
            wifiStatus = SYS_WIFI_GetStatus (sysObj.syswifi);
            if (wifiStatus > SYS_WIFI_STATUS_WDRV_OPEN_REQ)
            {
                memset(&scanConfig, 0, sizeof(scanConfig));
                res = SYS_WIFI_CtrlMsg(sysObj.syswifi, SYS_WIFI_GETSCANCONFIG, &scanConfig, sizeof(SYS_WIFI_SCAN_CONFIG));
                if(SYS_WIFI_SUCCESS == res)
                {
                    //Received the wifiSrvcScanConfig data
                    char myAPlist[] = ""; // e.g. "myAP*OPENAP*Hello World!"
                    char delimiter  = '*';
                    scanConfig.channel         = 0;
                    scanConfig.mode            = SYS_WIFI_SCAN_MODE_ACTIVE;
                    scanConfig.pSsidList       = myAPlist;
                    scanConfig.delimChar       = delimiter;
                    scanConfig.pNotifyCallback = (void *)APP_ScanHandler;
                    scanConfig.matchMode       = WDRV_PIC32MZW_SCAN_MATCH_MODE_FIND_ALL;
                    
                    SYS_CONSOLE_PRINT("\r\nStarting Custom Scan ...\r\n");
                    
                    res = SYS_WIFI_CtrlMsg(sysObj.syswifi,SYS_WIFI_SCANREQ,&scanConfig,sizeof(SYS_WIFI_SCAN_CONFIG));
                    if(SYS_WIFI_SUCCESS != res)
                    {
                        SYS_CONSOLE_PRINT("Error Starting scan: %d\r\n", res);
                        appData.state = APP_STATE_ERROR;
                        break;
                    }
                    appData.state = APP_STATE_DONE;
                }
                else
                {
                    SYS_CONSOLE_PRINT("Error retrieving scan config: %d\r\n", res);
                    appData.state = APP_STATE_ERROR;
                }
            }
            
            break;
        }
        
        case APP_STATE_DONE:
        {
            // wait for custom scan to complete and do nothing later
            break;
        }
        
        case APP_STATE_ERROR:
        {
            break;
        }
        default:
        {
            break;
        }
    }
}

User can initiate the Scan request with default Wi-Fi System service MHC menu configuration with below code snippet.

Wi-Fi_service_SCAN_MHC

SYS_WIFI_CtrlMsg(sysObj.syswifi, SYS_WIFI_SCANREQ, NULL, 0);

How to Disconnect Connected STA in AP Mode Using Wi-Fi System Service

Please see the code snippet below for reference.

#include "app.h"
#include "definitions.h"

APP_DATA appData;
static SYS_WIFI_CONFIG wificonfig;

void WiFiServCallback(uint32_t event, void * data, void *cookie) 
{
    IPV4_ADDR *IPAddr;
    switch (event) 
    {
        case SYS_WIFI_CONNECT:
        {
            IPAddr = (IPV4_ADDR *) data;
            if (SYS_WIFI_STA == wificonfig.mode) 
            {
                SYS_CONSOLE_PRINT("Connected to AP. Got IP address = %d.%d.%d.%d \r\n", IPAddr->v[0], IPAddr->v[1], IPAddr->v[2], IPAddr->v[3]);
            } 
            else 
            {
                SYS_WIFI_STA_APP_INFO    *psStaConnInfo = (SYS_WIFI_STA_APP_INFO *)data;
                SYS_CONSOLE_PRINT("STA Connected to AP. Got IP address = %d.%d.%d.%d \r\n", 
                        psStaConnInfo->ipAddr.v[0], psStaConnInfo->ipAddr.v[1], psStaConnInfo->ipAddr.v[2], psStaConnInfo->ipAddr.v[3]);                
                SYS_CONSOLE_PRINT("STA Connected to AP. Got MAC address = %x:%x:%x:%x:%x:%x \r\n", 
                        psStaConnInfo->macAddr[0], psStaConnInfo->macAddr[1], psStaConnInfo->macAddr[2],
                        psStaConnInfo->macAddr[3], psStaConnInfo->macAddr[4], psStaConnInfo->macAddr[5]);
                SYS_WIFI_CtrlMsg(sysObj.syswifi, SYS_WIFI_DISCONNECT, psStaConnInfo->macAddr, 6);
            }
            break;
        }
        case SYS_WIFI_DISCONNECT:
        {
            SYS_CONSOLE_PRINT("Device DISCONNECTED \r\n");
            break;
        }
        case SYS_WIFI_PROVCONFIG:
        {
            memcpy(&wificonfig,data,sizeof(SYS_WIFI_CONFIG));
            SYS_CONSOLE_PRINT("%s:%d Received Provisioning Data : \r\n Device mode=%s \r\n", __func__, __LINE__, (wificonfig.mode == SYS_WIFI_STA) ? "STA" : "AP");
            if (SYS_WIFI_STA == wificonfig.mode) 
            {
                SYS_CONSOLE_PRINT(" ssid=%s password=%s \r\n",wificonfig.staConfig.ssid,wificonfig.staConfig.psk);
            } 
            else if (SYS_WIFI_AP == wificonfig.mode) 
            {
                SYS_CONSOLE_PRINT(" ssid=%s password=%s \r\n",wificonfig.apConfig.ssid,wificonfig.apConfig.psk);
            }            
            break;
        }
    }
}

void APP_Initialize(void) 
{
    appData.state = APP_STATE_INIT;
}

void APP_Tasks(void) 
{

    switch (appData.state) 
    {
        case APP_STATE_INIT:
        {
            SYS_CONSOLE_PRINT("Application: wifi_easy_config \r\n");
            SYS_WIFI_CtrlMsg(sysObj.syswifi, SYS_WIFI_REGCALLBACK, WiFiServCallback, sizeof (uint8_t *));

            appData.state = APP_STATE_GETCONFIG;
            break;
        }
        case APP_STATE_GETCONFIG:
        {
            if(SYS_WIFI_SUCCESS == SYS_WIFI_CtrlMsg(sysObj.syswifi, SYS_WIFI_GETWIFICONFIG, &wificonfig, sizeof(SYS_WIFI_CONFIG)))
            {
                SYS_CONSOLE_PRINT("%s:%d Device mode=%s \r\n", __func__, __LINE__, (wificonfig.mode == SYS_WIFI_STA) ? "STA" : "AP");
                appData.state = APP_STATE_SERVICE_TASKS;

            }            
            break;
        }
        case APP_STATE_SERVICE_TASKS:
        {
            break;
        }
        default:
        {
            break;
        }
    }
}