4.1.8.1 Wi-Fi® Service
The MPLAB Code Configurator (MCC) allows Wi-Fi® service
configuration as mentioned below
The following list captures the Wi-Fi callback event codes and their
arguments
- Station mode
- Soft AP mode
This section allows Wi-Fi service configuration as mentioned below:
- Wi-Fi Modes: Drop-down to select Wi-Fi modes.
Available options are:
- StationMode
- ProvisionMode
- SoftAPmode
- Provision Method: Drop-down to select Wi-Fi Provisioning
method.
Available options are:
- Mobile App
- SSID: Wi-Fi Access Point/Network Name
- Passphrase: Wi-Fi Access point/Network password
- Security Type: Wi-Fi security protocol
- Auto Connect : Enable to automatically connect to the AP when the device is in station mode.\
- Provision Callback Handler: Configure callback function name for Wi-Fi Provisioning states (Applicable only if selected Wi-Fi Mode is ProvisionMode)
- Country code :
Drop-down to select Country code based on the region where product will be
used.
- GEN - World-wide(channels 1-11)
- USA - North America(channels 1-11)
- EMEA -
Europe(channels 1-13)Note: After selecting desired for country code setting, user need to call API (SYS_WINCS_WIFI_SrvCtrl) with parameter (SYS_WINCS_WIFI_SET_REG_DOMAIN) from the application code.
Example: SYS_WINCS_WIFI_SrvCtrl(SYS_WINCS_WIFI_SET_REG_DOMAIN, SYS_WINCS_WIFI_COUNTRYCODE)
- Certificates & Key Print: Select to print the Certificates & Keys present in device. User need to call API (SYS_WINCS_SYSTEM_SrvCtrl) with parameter (SYS_WINCS_SYSTEM_GET_CERT_LIST,NULL) for certificate and (SYS_WINCS_SYSTEM_GET_KEY_LIST,NULL) for keys from the application code.
- Wi-Fi BT Coexistence:
Select to enableBT/Wi-Fi coexistence arbiter
- Interface Type :
Drop-down to select Interface type
- 3-wire interface (BT_Act, BT_Prio, WLAN_Act)
- 2-wire interface (BT_Prio, WLAN_Act)
- WLAN Rx priority higher than BT Low Priority : Select to give WLAN Rx higher priority.
- WLAN Tx priority higher than BT Low Priority : Select to give WLAN Tx higher priority.
- Antenna type :
Drop-down to select antenna type
- Dedicated antenna
- Shared antenna
- Interface Type :
Drop-down to select Interface type
- Power save mode : Select to enable power save mode.
- SNTP Server address : SNTP server IP address or URL.
- Ping : Select to
enable ping functionality.
- Ping Address : Provide IPv4 or IPv6 Ping address.
- Resolve DNS: Select to enable the DNS resolve functionality.
- Wi-Fi Debug logs : Enable to get Wi-Fi debug logs.
- WiFi-Callback Handler: Configure callback function name to handle Wi-Fi service specific events (for example, Wi-Fi STA connection and disconnection, DHCP resolution, Wi-Fi Scan indication)
Wi-Fi System Service MCC Configuration
The Wi-Fi Service API prototype is as
follows:
SYS_WINCS_RESULT_t SYS_WINCS_WIFI_SrvCtrl( SYS_WINCS_WIFI_SERVICE_t request, SYS_WINCS_WIFI_HANDLE_t wifiHandle);It handles following services and reports the result to application over the return code or through the registered callback.
Option/Command | Input | Description |
---|---|---|
SYS_WINCS_WIFI_SET_PARAMS | Configures the provided Wi-Fi details and Triggers the connection based on auto enable flag | |
SYS_WINCS_WIFI_STA_CONNECT | None | Triggers the Wi-Fi STA connection |
SYS_WINCS_WIFI_STA_DISCONNECT | None | Disconnects the connection |
SYS_WINCS_WIFI_AP_DISABLE | None | Disables the SoftAP mode |
SYS_WINCS_WIFI_SET_CHANNEL_AP | Channel number | Configure the Wi-Fi channel |
SYS_WINCS_WIFI_SET_BSSID | BSSID of AP (String) | Configure the Access point's BSSID to which WINCS needs to connect |
SYS_WINCS_WIFI_GET_DRV_STATUS | None | Request Driver status |
SYS_WINCS_WIFI_OPEN_DRIVER | Empty DRV_HANDLE | Request to open wincs02 |
SYS_WINCS_WIFI_GET_DRV_HANDLE | Empty DRV_HANDLE | Request Driver Handle |
SYS_WINCS_WIFI_GET_TIME | None | Get Time through callback event SYS_WINCS_WIFI_SNTP_UP |
SYS_WINCS_WIFI_SET_SNTP | URL/ IP address | Set SNTP Configuration |
SYS_WINCS_WIFI_BT_COEX_CONFG | SYS_WINCS_WIFI_COEX_CFG_t structure | Set Wi-Fi BT Configuration |
SYS_WINCS_WIFI_BT_COEX_ENABLE | true/false | Enable/Disable Wi-Fi BT Configuration |
SYS_WINCS_WIFI_SET_HIDDEN, | true or false | Configure Hidden mode SSID in SoftAP mode |
SYS_WINCS_WIFI_PASSIVE_SCAN | SYS_WINCS_WIFI_SCAN_PARAM_t structure | Request/Trigger Wi-Fi passive scan |
SYS_WINCS_WIFI_ACTIVE_SCAN | SYS_WINCS_WIFI_SCAN_PARAM_t structure | Request/Trigger Wi-Fi active scan |
SYS_WINCS_WIFI_SET_REG_DOMAIN | Country code string Ex: “GEN” | Set the country code |
SYS_WINCS_WIFI_SET_SRVC_CALLBACK | Callback function handler | Register a callback for async events |
SYS_WINCS_WIFI_GET_CALLBACK | Callback function handler | Get Callback function data |
SYS_WINCS_WIFI_PING | Ping IP | Ping to given IP address |
SYS_WINCS_WIFI_DNS_RESOLVE | URL | DNS Resolve |
SYS_WINCS_WIFI_SET_CALLBACK | Callback Function handler | Register the call back for async events |
Event | Response Components | Comments |
---|---|---|
SYS_WINCS_WIFI_CONNECTED | Association ID: Integer Connected State: Integer | Wi-Fi connected event code. Reports the connection's Association ID and connected state |
SYS_WINCS_WIFI_DISCONNECTED | Association ID: Integer Connected State: Integer | Wi-Fi disconnected event code |
SYS_WINCS_WIFI_CONNECT_FAILED | NULL | Wi-Fi connection failure event |
SYS_WINCS_WIFI_DHCP_IPV4_COMPLETE | DHCP IPv4: String | Wi-Fi DHCP complete event code |
SYS_WINCS_WIFI_DHCP_IPV6_LOCAL_COMPLETE | DHCP IPv6 local: String | Wi-Fi local DHCP complete event code |
SYS_WINCS_WIFI_DHCP_IPV6_GLOBAL_COMPLETE | DHCP IPv6 global: String | Wi-Fi global DHCP complete event code |
SYS_WINCS_SNTP_UP | Time | SNTP UTC time received event code |
SYS_WINCS_DNS_RESOLVED | IP address | DNS Resolve event code |
SYS_WINCS_WIFI_REG_DOMAIN_SET_ACK | NULL | Set regulatory domain Acknowledge |
The following figure illustrates the Station mode connection sequence
Following is the example of provision mode,
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
/* This section lists the other files that are included in this file.
*/
#include "configuration.h"
#include "driver/driver_common.h"
#include "app_wincs02.h"
#include "system/system_module.h"
#include "system/console/sys_console.h"
#include "system/wifi/sys_wincs_wifi_service.h"
#include "system/sys_wincs_system_service.h"
#include "system/net/sys_wincs_net_service.h"
#include "system/wifiprov/sys_wincs_provision_service.h"
// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************
// *****************************************************************************
/* Application Data
Summary:
Holds application data
Description:
This structure holds the application's data.
Remarks:
This structure should be initialized by the APP_Initialize function.
Application strings and buffers are be defined outside this structure.
*/
APP_DATA g_appData;
// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************
/* TODO: Add any necessary callback functions.
*/
// *****************************************************************************
// Application Wi-Fi Callback Handler
//
// Summary:
// Handles Wi-Fi events.
//
// Description:
// This function handles various Wi-Fi events and performs appropriate actions.
//
// Parameters:
// event - The type of Wi-Fi event
// wifiHandle - Handle to the Wi-Fi event data
//
// Returns:
// None.
//
// Remarks:
// None.
// *****************************************************************************
void SYS_WINCS_WIFI_CallbackHandler
(
SYS_WINCS_WIFI_EVENT_t event, // The type of Wi-Fi event
SYS_WINCS_WIFI_HANDLE_t wifiHandle // Handle to the Wi-Fi event data
)
{
switch(event)
{
/* Set regulatory domain Acknowledgment */
case SYS_WINCS_WIFI_REG_DOMAIN_SET_ACK:
{
// The driver generates this event callback twice, hence the if condition
// to ignore one more callback. This will be resolved in the next release.
static bool domainFlag = false;
if( domainFlag == false)
{
SYS_CONSOLE_PRINT("Set Reg Domain -> SUCCESS\r\n");
g_appData.state = APP_STATE_WINCS_ENABLE_PROV;
domainFlag = true;
}
break;
}
/* SNTP UP event code*/
case SYS_WINCS_WIFI_SNTP_UP:
{
SYS_CONSOLE_PRINT("[APP] : SNTP UP \r\n");
break;
}
break;
/* Wi-Fi connected event code*/
case SYS_WINCS_WIFI_CONNECTED:
{
SYS_CONSOLE_PRINT(TERM_GREEN"[APP] : Wi-Fi Connected \r\n"TERM_RESET);
break;
}
/* Wi-Fi disconnected event code*/
case SYS_WINCS_WIFI_DISCONNECTED:
{
SYS_CONSOLE_PRINT(TERM_RED"[APP] : Wi-Fi Disconnected\nReconnecting... \r\n"TERM_RESET);
break;
}
/* Wi-Fi DHCP complete event code*/
case SYS_WINCS_WIFI_DHCP_IPV4_COMPLETE:
{
SYS_CONSOLE_PRINT("[APP] : DHCP IPv4 : %s\r\n", (uint8_t *)wifiHandle);
break;
}
case SYS_WINCS_WIFI_DHCP_IPV6_LOCAL_COMPLETE:
{
SYS_CONSOLE_PRINT("[APP] : DHCP IPv6 Local : %s\r\n", (uint8_t *)wifiHandle);
break;
}
case SYS_WINCS_WIFI_DHCP_IPV6_GLOBAL_COMPLETE:
{
SYS_CONSOLE_PRINT("[APP] : DHCP IPv6 Global: %s\r\n", (uint8_t *)wifiHandle);
break;
}
default:
{
break;
}
}
}
// *****************************************************************************
/**
* @brief Callback handler for WiFi provisioning events.
*
* This function is called whenever a WiFi provisioning event occurs. It handles
* the event based on the type of event received and the provisioning handle.
*
* @param event The WiFi provisioning event that occurred. This is of type
* SYS_WINCS_PROV_EVENT_t and indicates the specific event.
* @param provHandle The handle associated with the provisioning event. This is
* of type SYS_WINCS_PROV_HANDLE_t and is used to identify
* the specific provisioning instance.
*/
// *****************************************************************************
static void SYS_WINCS_WIFIPROV_CallbackHandler
(
SYS_WINCS_PROV_EVENT_t event,
SYS_WINCS_PROV_HANDLE_t provHandle
)
{
switch(event)
{
/**<Provisionging complete*/
case SYS_WINCS_PROV_COMPLETE:
{
SYS_WINCS_PROV_SrvCtrl(SYS_WINCS_PROV_DISABLE, NULL);
// SYS_WINCS_WIFI_SrvCtrl(SYS_WINCS_WIFI_SET_CALLBACK, SYS_WINCS_WIFI_CallbackHandler);
// Connect to the received AP configurations
SYS_WINCS_WIFI_SrvCtrl(SYS_WINCS_WIFI_SET_PARAMS, (SYS_WINCS_WIFI_HANDLE_t)provHandle);
//If autoConnect is false
//SYS_WINCS_WIFI_SrvCtrl(SYS_WINCS_WIFI_STA_CONNECT, NULL);
break;
}
/**<Provisionging Failure*/
case SYS_WINCS_PROV_FAILURE:
{
break;
}
default:
{
break;
}
}
}
// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************
/* TODO: Add any necessary local functions.
*/
// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************
// *****************************************************************************
// Application Initialization Function
//
// Summary:
// Initializes the application.
//
// Description:
// This function initializes the application's state machine and other
// parameters.
//
// Parameters:
// None.
//
// Returns:
// None.
//
// Remarks:
// None.
// *****************************************************************************
void APP_WINCS02_Initialize ( void )
{
/* Place the App state machine in its initial state. */
g_appData.state = APP_STATE_WINCS_PRINT;
/* TODO: Initialize your application's state machine and other
* parameters.
*/
}
// *****************************************************************************
// Application Tasks Function
//
// Summary:
// Executes the application's tasks.
//
// Description:
// This function implements the application's state machine and performs
// the necessary actions based on the current state.
//
// Parameters:
// None.
//
// Returns:
// None.
//
// Remarks:
// None.
// *****************************************************************************
void APP_WINCS02_Tasks ( void )
{
/* Check the application's current state. */
switch ( g_appData.state )
{
// State to print Message
case APP_STATE_WINCS_PRINT:
{
SYS_CONSOLE_PRINT(TERM_YELLOW"########################################\r\n"TERM_RESET);
SYS_CONSOLE_PRINT(TERM_CYAN" WINCS02 Wi-Fi Easy Config demo\r\n"TERM_RESET);
SYS_CONSOLE_PRINT(TERM_YELLOW"########################################\r\n"TERM_RESET);
g_appData.state = APP_STATE_WINCS_INIT;
break;
}
/* Application's initial state. */
case APP_STATE_WINCS_INIT:
{
SYS_STATUS status;
SYS_WINCS_WIFI_SrvCtrl(SYS_WINCS_WIFI_GET_DRV_STATUS, &status);
if (SYS_STATUS_READY == status)
{
g_appData.state = APP_STATE_WINCS_OPEN_DRIVER;
}
break;
}
case APP_STATE_WINCS_OPEN_DRIVER:
{
DRV_HANDLE wdrvHandle = DRV_HANDLE_INVALID;
// Open the Wi-Fi driver
if (SYS_WINCS_FAIL == SYS_WINCS_WIFI_SrvCtrl(SYS_WINCS_WIFI_OPEN_DRIVER, &wdrvHandle))
{
g_appData.state = APP_STATE_WINCS_ERROR;
break;
}
// Get the driver handle
SYS_WINCS_WIFI_SrvCtrl(SYS_WINCS_WIFI_GET_DRV_HANDLE, &wdrvHandle);
g_appData.state = APP_STATE_WINCS_DEVICE_INFO;
break;
}
case APP_STATE_WINCS_DEVICE_INFO:
{
APP_DRIVER_VERSION_INFO drvVersion;
APP_FIRMWARE_VERSION_INFO fwVersion;
APP_DEVICE_INFO devInfo;
SYS_WINCS_RESULT_t status = SYS_WINCS_BUSY;
// Get the firmware version
status = SYS_WINCS_SYSTEM_SrvCtrl(SYS_WINCS_SYSTEM_SW_REV, &fwVersion);
if(status == SYS_WINCS_PASS)
{
// Get the device information
status = SYS_WINCS_SYSTEM_SrvCtrl(SYS_WINCS_SYSTEM_DEV_INFO, &devInfo);
}
if(status == SYS_WINCS_PASS)
{
// Get the driver version
status = SYS_WINCS_SYSTEM_SrvCtrl(SYS_WINCS_SYSTEM_DRIVER_VER, &drvVersion);
}
if(status == SYS_WINCS_PASS)
{
char buff[30];
// Print device information
SYS_CONSOLE_PRINT("WINC: Device ID = %08x\r\n", devInfo.id);
for (int i = 0; i < devInfo.numImages; i++)
{
SYS_CONSOLE_PRINT("%d: Seq No = %08x, Version = %08x, Source Address = %08x\r\n",
i, devInfo.image[i].seqNum, devInfo.image[i].version, devInfo.image[i].srcAddr);
}
// Print firmware version
SYS_CONSOLE_PRINT(TERM_CYAN "Firmware Version: %d.%d.%d ", fwVersion.version.major,
fwVersion.version.minor, fwVersion.version.patch);
strftime(buff, sizeof(buff), "%X %b %d %Y", localtime((time_t*)&fwVersion.build.timeUTC));
SYS_CONSOLE_PRINT(" [%s]\r\n", buff);
// Print driver version
SYS_CONSOLE_PRINT("Driver Version: %d.%d.%d\r\n"TERM_RESET, drvVersion.version.major,
drvVersion.version.minor, drvVersion.version.patch);
g_appData.state = APP_STATE_WINCS_SET_REG_DOMAIN;
}
break;
}
case APP_STATE_WINCS_SET_REG_DOMAIN:
{
// Set the callback handler for Wi-Fi events
SYS_WINCS_WIFI_SrvCtrl(SYS_WINCS_WIFI_SET_CALLBACK, SYS_WINCS_WIFI_CallbackHandler);
SYS_CONSOLE_PRINT(TERM_YELLOW"[APP] : Setting REG domain to " TERM_UL "%s\r\n"TERM_RESET ,SYS_WINCS_WIFI_COUNTRYCODE);
// Set the regulatory domain
if (SYS_WINCS_FAIL == SYS_WINCS_WIFI_SrvCtrl(SYS_WINCS_WIFI_SET_REG_DOMAIN, SYS_WINCS_WIFI_COUNTRYCODE))
{
g_appData.state = APP_STATE_WINCS_ERROR;
break;
}
g_appData.state = APP_STATE_WINCS_SERVICE_TASKS;
break;
}
case APP_STATE_WINCS_ENABLE_PROV:
{
// Enable Provisioning Mode
SYS_WINCS_PROV_SrvCtrl(SYS_WINCS_PROV_SET_CALLBACK, (void *)SYS_WINCS_WIFIPROV_CallbackHandler);
if (SYS_WINCS_FAIL == SYS_WINCS_PROV_SrvCtrl(SYS_WINCS_PROV_ENABLE, NULL))
{
g_appData.state = APP_STATE_WINCS_ERROR;
break;
}
g_appData.state = APP_STATE_WINCS_SERVICE_TASKS;
break;
}
case APP_STATE_WINCS_SERVICE_TASKS:
{
break;
}
case APP_STATE_WINCS_ERROR:
{
SYS_CONSOLE_PRINT(TERM_RED"[APP_ERROR] : ERROR in Application "TERM_RESET);
g_appData.state = APP_STATE_WINCS_SERVICE_TASKS;
break;
}
/* The default state should never be executed. */
default:
{
/* TODO: Handle error in application's state machine. */
break;
}
}
}
/*******************************************************************************
End of File
*/