3.2.8.1 Wi-Fi Service

The MPLAB Code Configurator (MCC) allows Wi-Fi service configuration as mentioned below:
  1. Station mode
  2. Soft AP mode
Figure 3-68. RNWF WINCS Wi-Fi® Service

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
    • Web Server
  • 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.
  • Country code: Drop-down to select country code based on the region where product will be used. After selecting desired for country code setting, user needs to call API (SYS_RNWF_WIFI_SrvCtrl) with parameter (SYS_RNWF_WIFI_SET_REGULATORY_DOMAIN) from the application code.

    For example: SYS_RNWF_WIFI_SrvCtrl(SYS_RNWF_WIFI_SET_REGULATORY_DOMAIN, SYS_RNWF_COUNTRYCODE);

  • Certificates & Key Print: Select to print the certificates and keys present in the device. The user needs to call the API (SYS_RNWF_SYSTEM_SrvCtrl) with parameter (SYS_RNWF_SYSTEM_GET_CERT_LIST,buffer) for certificates and (SYS_RNWF_SYSTEM_GET_KEY_LIST,buffer) for keys from the application code.

  • Wi-Fi/BT Coexistence: Select to enable BT/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
  • 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.
  • Interface Debug logs : Select to get interface 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

    1. Figure 3-69. Wi-Fi Settings: StationMode
    2. Figure 3-70. Wi-Fi Settings: APmode
    3. Figure 3-71. Wi-Fi Settings: ProvisionMode
    4. The Wi-Fi Service API prototype is as follows:
      SYS_RNWF_RESULT_t SYS_RNWF_WIFI_SrvCtrl(SYS_RNWF_WIFI_SERVICE_t request, SYS_RNWF_WIFI_HANDLE_t);
      It handles following services and reports the result to application over the return code or through the registered callback.
      Table 3-17. Wi-Fi Services
      Option/CommandInputDescription
      SYS_RNWF_SET_WIFI_PARAMSMode, SSID, Passphrase, Security, AutoenableConfigures the provided Wi-Fi details and Triggers the connection based on auto enable flag
      SYS_RNWF_WIFI_STA_CONNECTNoneTriggers the Wi-Fi STA connection
      SYS_RNWF_WIFI_STA_DISCONNECTNoneDisconnects the connection
      SYS_RNWF_WIFI_AP_DISABLENoneDisables the SoftAP mode
      SYS_RNWF_WIFI_SET_WIFI_AP_CHANNELChannel numberConfigure the Wi-Fi channel
      SYS_RNWF_SET_WIFI_BSSIDBSSID of AP (String)Configure the Access point's BSSID to which RNWF needs to connect

      SYS_RNWF_SET_WIFI_TIMEOUT

      Seconds (int)Configure Wi-Fi connection timeout
      SYS_RNWF_SET_WIFI_HIDDEN,true or falseConfigure Hidden mode SSID in SoftAP mode
      SYS_RNWF_WIFI_PASSIVE_SCANNoneRequest/Trigger Wi-Fi passive scan
      SYS_RNWF_WIFI_ACTIVE_SCANNoneRequest/Trigger Wi-Fi active scan
      SYS_RNWF_GET_WIFI_CONF_INFONoneGet WiFi config information
      SYS_RNWF_WIFI_SET_SRVC_CALLBACKCallback Function handlerRegister a callback for async events
      SYS_RNWF_WIFI_GET_CALLBACKCallback Function handlerGet Callback function data
      SYS_RNWF_WIFI_SET_CALLBACKCallback Function handlerRegister the call back for async events
      SYS_RNWF_WIFI_SET_REGULATORY_DOMAINCountry codeSets the country code
      SYS_RNWF_WIFI_ENABLE_POWERSAVE_MODEtrue or falseEnable/disable the power save mode
      SYS_RNWF_WIFI_BT_COEX_ENABLE

      Enable/disable,Interface type.wlan_rx_priority, wlan_tx_priority,antenna_mode

      Configures the provided WiFi+BT details and enable
      SYS_RNWF_WIFI_PINGPing IPPing to given IP address
      SYS_RNWF_WIFI_DNSDNS URLDNS Resolve
The following list captures the Wi-Fi callback event codes and their arguments.
Table 3-18. Callback Event Codes
EventResponse ComponentsComments
SYS_RNWF_WIFI_CONNECTEDAssociation ID: Integer

Connected State: Integer

Wi-Fi connected event code. Reports the connection's Association ID and connected state
SYS_RNWF_WIFI_DISCONNECTEDAssociation ID: Integer

Connected State: Integer

Wi-Fi disconnected event code
SYS_RNWF_WIFI_CONNECT_FAILEDFail event code: IntegerWi-Fi connection failure event code
SYS_RNWF_WIFI_DHCP_IPV4_COMPLETEDHCP IPv4: StringWi-Fi DHCP IPv4 complete event code
SYS_RNWF_WIFI_DHCP_IPV6_LOCAL_COMPLETEDHCP IPv6 Local: StringWi-Fi DHCP IPv6 local complete event code
SYS_RNWF_WIFI_DHCP_IPV6_GLOBAL_COMPLETEDHCP IPv6 Global: StringWiFI DHCP IPv6 Global Complete event
SYS_RNWF_WIFI_PING_RESP

IP_ADDRESS : IP address of the target

RTT: Round trip time (in milliseconds)

Wi-Fi PING event code
SYS_RNWF_WIFI_SNTP_UP

ID : Parameter ID number

VAL: Parameter value

SNTP up event code
SYS_RNWF_WIFI_SCAN_INDICATIONRSSI: Received signal strength

Sec Type (Int): Recommended security type to use connecting to this AP (10 options)

Channel (Int): Channel # of device
BSSID (String): BSSID of detected device
SSID (String): SSID of detected device

Scan results to report each scan list
SYS_RNWF_WIFI_SCAN_DONENoneScan complete event code
SYS_RNWF_WIFI_DNS_RESPIP AddressDNS complete event code
The following figure illustrates the Station mode connection sequence.
Figure 3-72. Station Mode Connection Sequence
Figure 3-73. Process Flow for Creating a Soft AP
Figure 3-74. Scan Operation Sequence
Following is the example of establishing connection in the Station mode.
#include <string.h>
#include <stdio.h>
#include <stddef.h>                    
#include <stdbool.h>                  
#include <stdlib.h>  
 
/* This section lists the other files that are included in this file.*/
#include "app.h"
#include "user.h"
#include "definitions.h"
#include "configuration.h"
#include "system/debug/sys_debug.h"
#include "system/wifi/sys_rnwf_wifi_service.h"
#include "system/inf/sys_rnwf_interface.h"
 
/* Variable to check the UART transfer */
static volatile bool g_isUARTTxComplete = true,isUART0TxComplete = true;;
 
/*Shows the he application's current state*/
static APP_DATA g_appData;
 
 
/* DMAC Channel Handler Function */
static void APP_RNWF_usartDmaChannelHandler(DMAC_TRANSFER_EVENT event, uintptr_t contextHandle)
{
    if (event == DMAC_TRANSFER_EVENT_COMPLETE)
    {
        g_isUARTTxComplete = true;
    }
}
 
 
/* Application Wi-fi Callback Handler function */
void APP_WIFI_Callback(SYS_RNWF_WIFI_EVENT_t event, SYS_RNWF_WIFI_HANDLE_t wifiHandle)
{
    uint8_t *p_str =(uint8_t *)wifiHandle;
           
    switch(event)
    {
        /* SNTP UP event code*/
        case SYS_RNWF_WIFI_SNTP_UP:
        {            
               
                static uint8_t flag =1;
                if(flag==1)
                {
                    SYS_CONSOLE_PRINT("SNTP UP:%s\n", &p_str[0]);  
                    flag=0;
                }
                break;
        }
       
        /* Wi-Fi connected event code*/
        case SYS_RNWF_WIFI_CONNECTED:
        {
            SYS_CONSOLE_PRINT("Wi-Fi Connected\r\n");
            break;
        }
       
        /* Wi-Fi disconnected event code*/
        case SYS_RNWF_WIFI_DISCONNECTED:
        {
            SYS_CONSOLE_PRINT("Wi-Fi Disconnected\nReconnecting... \r\n");
            SYS_RNWF_WIFI_SrvCtrl(SYS_RNWF_WIFI_STA_CONNECT, NULL);
            break;
        }
       
        /* Wi-Fi DHCP complete event code*/
        case SYS_RNWF_WIFI_DHCP_IPV4_COMPLETE:
        {
            SYS_CONSOLE_PRINT("DHCP Done...%s \r\n",&p_str[2]);
            strncpy((char *)g_devIp,(const char *) &p_str[3], strlen((const char *)(&p_str[3]))-1);
            break;
        }
       
         /* Wi-Fi IPv6 DHCP complete event code*/
        case SYS_RNWF_WIFI_DHCP_IPV6_LOCAL_COMPLETE:
        {
            SYS_CONSOLE_PRINT("IPv6 Local DHCP Done...%s \r\n",&p_str[2]);
           
            /*Local IPv6 address code*/    
            break;
        }
        case SYS_RNWF_WIFI_DHCP_IPV6_GLOBAL_COMPLETE:
        {
            SYS_CONSOLE_PRINT("IPv6 Global DHCP Done...%s \r\n",&p_str[2]);
           
            /*Global IPv6 address code*/    
            break;
        }
       
        /* Wi-Fi scan indication event code*/
        case SYS_RNWF_WIFI_SCAN_INDICATION:
        {
            break;
        }
       
        /* Wi-Fi scan complete event code*/
        case SYS_RNWF_WIFI_SCAN_DONE:
        {
            break;
        }
       
        default:
        {
            break;
        }
                   
    }    
}
 
 
/* Application Initialization function */
void APP_Initialize ( void )
{
    /* Place the Application state machine in its initial state. */
    g_appData.state = APP_STATE_INITIALIZE;
}
 
/* Maintain the application's state machine.*/
void APP_Tasks ( void )
{
   
    switch(g_appData.state)
    {
        /* Application's state machine's initial state. */
        case APP_STATE_INITIALIZE:
        {
            DMAC_ChannelCallbackRegister(DMAC_CHANNEL_0, APP_RNWF_usartDmaChannelHandler, 0);
            SYS_RNWF_IF_Init();
           
            g_appData.state = APP_STATE_REGISTER_CALLBACK;
            SYS_CONSOLE_PRINT("Start Of Application\r\n");
            break;
        }
       
        /* Register the necessary callbacks */
        case APP_STATE_REGISTER_CALLBACK:
        {      
             
            /* RNWF Application Callback register */
            SYS_RNWF_WIFI_SrvCtrl(SYS_RNWF_WIFI_SET_CALLBACK, APP_WIFI_Callback);
         
            /* Set Regulatory domain/Country Code */
            const char *regDomain = SYS_RNWF_COUNTRYCODE;
            SYS_CONSOLE_PRINT("\r\nSetting regulatory domain : %s\r\n",regDomain);
            SYS_RNWF_WIFI_SrvCtrl(SYS_RNWF_WIFI_SET_REGULATORY_DOMAIN, (void *)regDomain);
           
            /* Wi-Fi Connectivity */
            SYS_CONSOLE_PRINT("Connecting to : %s\r\n",SYS_RNWF_WIFI_STA_SSID);
            SYS_RNWF_WIFI_PARAM_t wifi_sta_cfg = {SYS_RNWF_WIFI_MODE_STA, SYS_RNWF_WIFI_STA_SSID, SYS_RNWF_WIFI_STA_PWD, SYS_RNWF_STA_SECURITY, SYS_RNWF_WIFI_STA_AUTOCONNECT};        
            SYS_RNWF_WIFI_SrvCtrl(SYS_RNWF_SET_WIFI_PARAMS, &wifi_sta_cfg);
            g_appData.state = APP_STATE_TASK;
            break;
        }
       
        /* Run Event handler */
        case APP_STATE_TASK:
        {
            SYS_RNWF_IF_EventHandler();
            break;
        }
       
        default:
        {
            break;
        }
    }
}