Wi-Fi Open / WPA

The Wi-Fi API provides several interfaces to manage the Wi-Fi functionality on the XDK. XDK applications can implement this API to communicate over the Simplelink stack with surrounding Wi-Fi networks. All network information such as IP-address, connection state, gateway or subnet mask are available. There are API features for scanning and initiating several connections.

This section describes the different functions which can be used to establish a Wi-Fi connection. For a simple introduction to initializing and connecting, please refer to the section Getting Started / Simple Wi-Fi Connection.

  1. API Overview
  2. Implementation
  3. Initializing Wi Fi with the simplified Wi Fi Implementation
  4. Enabling the simplified Wi Fi Implementation
  5. Full Code Example
  6. Implementing the Wi Fi functionality by using the advanced Wi-Fi implementation
    1. Preparation
    2. Scanning for Wi Fi Networks
    3. IP Settings
      1. DNS
      2. Gateway
      3. Subnet mask
      4. DHCP
    4. Static IP settings
    5. DHCP without callback
    6. DHCP with callback
    7. Get IP settings
    8. Connecting
    9. To an Open Access Point
    10. To WPA
    11. Disconnecting
  7. Minimal Network Setup
  8. Full Code Example 2
  9. Appendix
    1. XDK Console Output example

API Overview

In general, it is recommended to develop an application based on the highest API level the XDK framework supports. If this functionality is not enough for the distinct purpose, it is possible to access deeper API levels.

The XDK Wi-Fi API is a part of the XDK platform section and accesses the Texas Instruments’ Simplelink library that is part of the library section. The Simplelink library provides a low level access to the CC3100 Wi-Fi chip of the XDK-Hardware.

Image

Implementation

In the following, a simple Wi-Fi implementation with the interface XDK_WLAN.h will be described and how to establish a Wi-Fi connection with only necessary configurations. Additionally, the underlying advanced Wi-Fi implementation with the interfaces BCDS_WlanConnect.h and BCDS_NetworkConfig.h will be described in detail if more configuration is required to suit the use case best.

Initializing Wi Fi with the simplified Wi Fi Implementation

Note: The code snippets in this subchapter are intended for XDK-Workbench versions 3.4.0 and higher.

The interface XDK_WLAN.h utilizes a new kind of initialization, while using a configuration struct of the type WLAN_Setup_T, which includes the following configuration elements:

Structure elementDescription
IsEnterpriseA boolean representing if the connection will be Enterprise WPA2 Wi-Fi if enabled or Personal WPA Wi-Fi if disabled.
IsHostPgmEnabledA boolean representing if a dummy certificate needs to be flashed into the Wi-Fi chips memory for the Enterprise WPA2 Wi-Fi connection.
SSIDA pointer holding the Wi-Fi SSID.
UsernameA pointer holding the username for the Enterprise WPA2 Wi-Fi connection.
PasswordA pointer holding Wi-Fi password.
IsStaticA boolean representing if static IPv4 configuration is made while enabled or DHCP while disabled.
IpAddrA variable holding the static IP address for the static IPv4 configuration.
GwAddrA variable holding the gateway address for the static IPv4 configuration.
DnsAddrA variable holding the DNS server address for the static IPv4 configuration.
MaskA variable holding the subnet mask for the static IPv4 configuration.

Please note that the configuration elements, such as the SSID and the IP settings are only described in short detail. If more information regarding them are required, it is recommended to take a look at the corresponding chapter for the advanced Wi-Fi implementation, where these are described in greater detail.

An example of how to configure and initialize the simplified Wi-Fi functionality is shown in the code below.

Furthermore, please note that the example code for the XDK_WLAN.h interface is following the conventions of the new XdkApplicationtemplate example from the XDK-Workbench 3.4.0.

#include "XDK_WLAN.h"
// other interfaces here

static WLAN_Setup_T WLANSetupInfo = {
                .IsEnterprise = false,
                .IsHostPgmEnabled = false,
                .SSID = "SSID",
                .Password = "PASSPHRASE",
                .IsStatic = false,
        };/**< WLAN setup parameters */

static void AppControllerSetup(void * param1, uint32_t param2) {
    // Other XdkApplicationTemplate code here

    WLAN_Setup(&WLANSetupInfo);

    // Other XdkApplicationTemplate code here
}

The example code shows first the inclusion of the interface XDK_WLAN.h. Afterwards, a struct called WLANSetupInfo of the type WLAN_Setup_T is declared and filled with the parameters described in the previously listed table. The configuration is made for a Personal connection to a WPA Wi-Fi hotspot. In that case only the SSID, the passphrase and DHCP are configured. All other configuration items are left out.

Afterwards, the function WLAN_Setup() passed by reference WLANSetupInfo struct is called within the context of the function AppControllerSetup to initialize the Wi-Fi module.

Enabling the simplified Wi Fi Implementation

Since all setup configurations are made, it is time to enable the Wi-Fi module. This means in detail that the Wi-Fi chip CC3100 on the XDK is ready to connect to the configured Wi-Fi hotspot.

For that, the function WLAN_Enable() needs to be called in the context of the function AppControllerEnable() as shown in the code below.

static void AppControllerEnable(void * param1, uint32_t param2) {
  // Other XdkApplicationTemplate code here

    WLAN_Enable();
}

This function will then connect once to a Personal WPA Wi-Fi network or to an Enterprise WPA2 Wi-Fi network, depending on the previously made configuration.

Please note that this will only be a one time established connection. It will not integrate a reconnect routine, which is capable to monitor the network connection and reconnect if the connection gets lost. For that, an implementation with the advanced Wi-Fi API, which is explained after the full code example below, is required.

Full Code Example

Note: The full code example is intended for XDK-Workbench versions 3.4.0 and higher.

/*----------------------------------------------------------------------------*

/* --------------------------------------------------------------------------- |
 * INCLUDES & DEFINES ******************************************************** |
 * -------------------------------------------------------------------------- */

/* own header files */
#include "XdkAppInfo.h"
#undef BCDS_MODULE_ID  /* Module ID define before including Basics package*/
#define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER

/* own header files */

/* system header files */
#include <stdio.h>

/* additional interface header files */
#include "BCDS_CmdProcessor.h"
#include "FreeRTOS.h"

#include "XDK_WLAN.h"

/* --------------------------------------------------------------------------- |
 * HANDLES ******************************************************************* |
 * -------------------------------------------------------------------------- */

static CmdProcessor_T * AppCmdProcessor;/**< Handle to store the main Command processor handle to be used by run-time event driven threads */

static WLAN_Setup_T WLANSetupInfo = {
                .IsEnterprise = false,
                .IsHostPgmEnabled = false,
                .SSID = "yourWifiNetworkSSID",
                .Password = "yourWifiNetworkPW",
                .IsStatic = false,
        };/**< WLAN setup parameters */

/* --------------------------------------------------------------------------- |
* EXECUTING FUNCTIONS ******************************************************** |
* -------------------------------------------------------------------------- */

/* --------------------------------------------------------------------------- |
 * BOOTING- AND SETUP FUNCTIONS ********************************************** |
 * -------------------------------------------------------------------------- */

static void AppControllerEnable(void * param1, uint32_t param2) {

    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);

    /* Enable necessary modules for the application and check their return values */

    WLAN_Enable();
}

static void AppControllerSetup(void * param1, uint32_t param2) {

    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);

    /* Setup the necessary modules required for the application */
    Retcode_T retcode = RETCODE_OK;

    WLAN_Setup(&WLANSetupInfo);

    retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));
    if (RETCODE_OK != retcode)
    {
        printf("AppControllerSetup : Failed \r\n");
        Retcode_RaiseError(retcode);
        assert(0); /* To provide LED indication for the user */
    }
}

void AppController_Init(void * cmdProcessorHandle, uint32_t param2) {

    BCDS_UNUSED(param2);

    Retcode_T retcode = RETCODE_OK;

    if (cmdProcessorHandle == NULL)
    {
        printf("AppController_Init : Command processor handle is NULL \r\n");
        retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_NULL_POINTER);
    }
    else
    {
        AppCmdProcessor = (CmdProcessor_T *) cmdProcessorHandle;
        retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerSetup, NULL, UINT32_C(0));
    }

    if (RETCODE_OK != retcode)
    {
        Retcode_RaiseError(retcode);
        assert(0); /* To provide LED indication for the user */
    }
}

/** ************************************************************************* */

Implementing the Wi Fi functionality by using the advanced Wi Fi implementation

This section will demonstrate some functions of the underlying two main APIs for Wi-Fi. Those are

  • BCDS_WlanConnect.h for the Wi-Fi connection interface
  • BCDS_NetworkConfig.h for the Wi-Fi configuration interface

The BCDS_WlanConnect.h interface provides several functions to make the Wi-Fi Hardware available and to do things like searching for networks or initiating a connection to them. In general, this interface provides the connection functions of the API.

FunctionDescription
WlanConnect_init()Initialize the Wi-Fi driver and Wi-Fi stack. This function must be called before calling any other Wi-Fi API function.
WlanConnect_DeInit()Deinitialize the Wi-Fi driver and the Wi-Fi stack.
WlanConnect_ScanNetworks()Scan for surrounded Wi-Fi networks.
WlanConnect_Open()Connect to a network without security permissions.
WlanConnect_WPA()Connect to a network with a WPA passphrase.
WlanConnect_GetCurrentNwStatus()Delivers the current status of the Wi-Fi network connection.

The BCDS_NetworkConfig.h interface provides basic functions to make general connection settings and to get information about the current network connection.

FunctionDescription
NetworkConfig_Ipv4Value()Convert a given IP in a hexadecimal state.
NetworkConfig_Ipv4Byte()Extract a byte from an acquired IP address.
NetworkConfig_SetIpStatic()Set the configuration to a static IP.
NetworkConfig_SetIpDhcp()Set the configuration to dynamic IP by using DHCP.
NetworkConfig_GetIpSettings()Handle Wi-Fi IP settings. Return a structure that includes the connection information.

Preparation

This chapter’s code snippets can be used in any project and do not require any other preparation other than the necessary includes. The API is included as follows.

#include "stdio.h" // If not already included in system header files
#include "BCDS_WlanConnect.h"
#include "BCDS_NetworkConfig.h"

To use any of the functions, which are presented here in this section, it is necessary to initialize the Wi-Fi stack of the XDK by using the following function.

// call this before any other function from the WlanConnect API
WlanConnect_Init();

Scanning for Wi Fi Networks

In order to understand how scanning for Wi-Fi networks and connecting to Wi-Fi networks is done, it is important to understand what the SSID network parameter is.

The SSID (Service Set Identifier) describes the name of a Wi-Fi network. It is a case-sensitive string with a byte length up to 32 bytes. Devices are able to identify different access points (AP) after boot up and establish a Wi-Fi connection to them. Similar to this the SSID can be used to search for a specific network.

If the Wi-Fi is initialized, it is possible to scan for surrounding Wi-Fi networks before connecting to them. In order to do that, three variable types are of particular interest. They are described in the table below.

Variable TypeDescription
Retcode_TReturn state for programmable request logics.
WlanConnect_ScanInterval_TDefined time period in which intervals the scan occurs.
WlanConnect_ScanList_TA struct which contains the scanned network information.

The following code-snippet will scan the environment for Wi-Fi networks:

Retcode_T retScanStatus;
WlanConnect_ScanInterval_T scanInterval = 5;
WlanConnect_ScanList_T scanList;
portTickType delay500msec = 500;
retScanStatus = WlanConnect_ScanNetworks(scanInterval, &scanList);
if (retScanStatus == RETCODE_OK) {
// Print SSID networks
  for (int i = 0; i < scanList.NumOfScanEntries; i++) {
    if (0 != scanList.ScanData[i].SsidLength) {
      printf("Found SSID number %d is : %s\n\r", i,
      scanList.ScanData[i].Ssid);
      vTaskDelay( delay500msec / portTICK_RATE_MS);
    }
  }
}
else if (retScanStatus == RETCODE_NO_NW_AVAILABLE) { /* No networks found */ }
else {
  // Scan failed
}

The WlanConnect_ScanNetworks() function scans for surrounding networks in the committed time intervals. Results of the scan are saved in a scanList variable. The return state retScanStatus provides the result of the scan request. There are three listed request states used for the Wi-Fi network scan: RETCODE_OK, RETCODE_NO_NW_AVAILABLE and an else case if the scan failed.

IP Settings

To better understand IP settings, some explanations are important first. If the IP is set to static, it is important to modify the parameters to the respective access point configuration.

DNS

Stands for “Domain Name System.” Domain names serve as memorizable names for websites and other services on the Internet. However, computers access Internet devices by their IP addresses. DNS translates domain names into IP addresses, allowing you to access an Internet location by its domain name.

Gateway

A gateway is a hardware device that acts as a “gate” between two networks. It may be a router, firewall, server, or other device that enables traffic to flow in and out of the network. While a gateway protects the nodes within network, it is also a node itself. The gateway node is considered to be on the “edge” of the network as all data must flow through it before coming in or going out of the network. It may also translate data received from outside networks into a format or protocol recognized by devices within the internal network. A router is a common type of gateway used in home networks.

Subnet mask

A subnet mask is a number that defines a range of IP addresses that can be used in a network. Subnet masks are used to designate subnetworks, or subnets, which are typically local networks LANs that are connected to the Internet. Systems within the same subnet can communicate directly with each other, while systems on different subnets must communicate through a router. Therefore, subnetworks can be used to partition multiple networks and limit the traffic between them.

DHCP

The Dynamic Host Configuration Protocol (DHCP) is a standardized network protocol for dynamically distributing network configuration parameters like IP addresses. With the XDK it is possible to implement a connection based on DHCP too. There are two different DHCP modes, with and without a callback. It is possible to turn DHCP off and set the network parameters manually.

The possible IP modes are

  • static IP application
  • DHCP IP application with callback
  • DHCP IP application without callback

Every IP mode will be explained in this chapter, to give the user all the information necessary to setup a mode for his own use case.

Static IP settings

The manual implementation of the IP settings is the configuration below with static network parameters. This configuration allows the setup of the network parameters like DNS, Gateway or Subnet Mask. Therefore, it is necessary to disable the introduced DHCP and set the network parameters manually in the code. The following table describes the structure of NetworkConfig_IpSettings_T. The fields of this variable represent the network parameters that have to be set for the static IP.

Struct FieldDescription
ipV432 Bit Internet Protocol Version 4 Address.
ipV4DnsServer32 Bit Address of the local DNS Server.
ipV4Gateway32 Bit Address of the local Router/Gateway.
ipV4Mask32 Bit Subnetmask of the local Network.

The following code shows an example of how the network parameters can be set for a static IP configuration. The function NetworkConfig_Ipv4Value() converts the committed decimal IP parameters to hexadecimal IP parameters.

NetworkConfig_IpSettings_T myIpSet;
myIpSet.isDHCP = (uint8_t) NETWORKCONFIG_DHCP_DISABLED;
myIpSet.ipV4 = NetworkConfig_Ipv4Value(192, 168, 0, 2);
myIpSet.ipV4DnsServer = NetworkConfig_Ipv4Value(192, 168, 0, 1);
myIpSet.ipV4Gateway = NetworkConfig_Ipv4Value(192, 168, 0, 1);
myIpSet.ipV4Mask = NetworkConfig_Ipv4Value(255, 255, 255, 0);

In the following code, the previously configured static IP is set. A request state retStatusSetIp is returned and it can be determined if the IP setup was configured successfully or not. The NetworkConfig_SetIpStatic() function sets the committed IP parameters in the interface.

Retcode_T retStatusSetIp;
retStatusSetIp = NetworkConfig_SetIpStatic(myIpSet);
if (retStatusSetIp == RETCODE_OK) {
  // Set IP succeed
}
else {
  // Set IP static failed
}

DHCP without callback

A dynamic implementation of the IP setup can be done with an IP setup using DHCP without a callback. In comparison to the static IP setup, the DHCP setup only requires one function call to acquire the IP parameters.

The code below shows the DHCP implementation and uses similar to the static IP setup a request state retStatusSetIp if the IP setup was successfully configured. Similar to the static IP configuration the NetworkConfig_SetIpDhcp() function provides the dynamic setting of the IP parameters.

// Set the DHCP without callback
Retcode_T retStatusSetIp;
retStatusSetIp = NetworkConfig_SetIpDhcp(0);
if (retStatusSetIp == RETCODE_OK) {
  // Waiting for IP
}
else {
  // Setting Ip over DHCP failed
}

DHCP with callback

Sometimes it is useful to execute a piece of code directly after setting the IP parameters. As such, this implementation provides a dynamic IP setup using DHCP with callback. This implementation provides a callback for whether the dynamic IP setup was configured or not. An if-condition is not necessary for this case. To implement an IP setup using DHCP with callback, a callback function has to be implemented. A callback is simply a reference to a function, which is given to an interface and which later will be called automatically. The following code-snippet shows an implementation of a simple callback function. An if-condition verifies, whether the IP was successfully acquired.

void myDhcpIpCallbackFunc(NetworkConfig_IpStatus_T returnStatus) {
  if ( returnStatus == NETWORKCONFIG_IPV4_ACQUIRED) {
    printf("Callback Function : IP was acquired using DHCP\n\r");
  }
  else {
    // DHCP Request failed
  }
}

The dynamic IP setup needs to be configured as before with the call of the NetworkConfig_SetIpDhcp() function. Instead of 0 as the input, the callback function is provided. Similar to the other examples, there is a request state retStatusSetIP, to check if the setup execution was successful. Note that this additional request isn’t necessary. The request status can be verified using the callback. The following code demonstrates how to do the setup, using the previously defined callback myDhcpIpCallbackFunc.

Retcode_T retStatusSetIp;
NetworkConfig_IpCallback_T myIpCallback;
// Set the IP callback
myIpCallback = myDhcpIpCallbackFunc;
// Set the DHCP with callback
retStatusSetIp = NetworkConfig_SetIpDhcp(myIpCallback);
if (retStatusSetIp == RETCODE_OK) {
  // Waiting for IP
  }
else {
  // Set DHCP with Callback failed
}

Note: If no IP settings are made, the Wi-Fi API automatically uses the dynamic IP setup using DHCP without callback.

Get IP settings

After the IP settings are set, they can be retrieved as well. This might not be necessary for static IP settings, but is sometimes crucial to know after an IP setup with DHCP. The following code accesses a previously acquired IP and prints it to the console.

NetworkConfig_IpSettings_T myIpGet;
Retcode_T retStatusGetIp;
retStatusGetIp = NetworkConfig_GetIpSettings(&myIpGet);
if (retStatusGetIp == RETCODE_OK) {
  printf("The static IP was retrieved : %u.%u.%u.%u \n\r",
              (unsigned int) (NetworkConfig_Ipv4Byte(myIpGet.ipV4, 3)),
              (unsigned int) (NetworkConfig_Ipv4Byte(myIpGet.ipV4, 2)),
              (unsigned int) (NetworkConfig_Ipv4Byte(myIpGet.ipV4, 1)),
              (unsigned int) (NetworkConfig_Ipv4Byte(myIpGet.ipV4, 0)));
}
else {
  // Get IP settings failed
}

The NetworkConfig_IpSettings_T struct is needed to save the acquired IP. Afterwards, the NetworkConfig_GetIpSettings() function accesses the IP settings and stores the acquired IP in the myIPGet variable. As before, the retStatusGetIp return state indicates, whether the accessing the acquired IP was successful or not. The NetworkConfig_Ipv4Byte() function extracts a byte from the acquired IP address in the myIPGet variable. This function call can be used to print the acquired IP to the console.

Connecting

For a connection, the credentials of the Wi-Fi network can be stored in variables of types provided by the API.

  • WlanConnect_SSID_T will contain the SSID of the Wi-Fi network
  • WlanConnect_PassPhrase_T will contain the password of the Wi-Fi network

To an Open Access Point

An open Access Point is a Wi-Fi network without any security permissions and therefore no authentication is necessary. If the Wi-Fi stack is initizialized, the IP can be set and a connection can be established. The chapter IP Settings shows how to configure the necessary networks IP settings and access the acquired IP.

The following code-snippet shows how to connect to a Wi-Fi network using the WlanConnect_Open() function. The input parameters are the network SSID and a connection callback. In this particular example the function was called without the connection callback function. An example for a callback-function can be found in chapter DHCP with callback. Please note that the callback-function has to be adapted. Similar to the other codes, the return code retStatusConnect indicates whether the connection was successfully established or not.

Retcode_T retStatusConnect;
WlanConnect_SSID_T connectSSID = "yourWifiNetworkSSID";
retStatusConnect = (Retcode_T) WlanConnect_Open(connectSSID, 0);
if (retStatusConnect == RETCODE_OK) {
printf("Connected successfully.\n\r");
}
else {
// Connection failed
}

To WPA

WPA stands for Wi-Fi Protected Access. WPA is a security protocol designed to create secure wireless (Wi-Fi) networks. It is similar to the WEP protocol, but offers improvements in the way it handles security keys and the way users are authorized.

Similar to the implementation with an open Access Point, the network IP settings have to be configured to establish a connection beforehand. A password is also needed to connect to this network. The following code shows an implementation for connecting to a WPA Wi-Fi network. The difference to the open Access Point configuration is the password, which is committed to the WlanConnect_WPA() function. Similar to the implementation of an open Access Point, the function also contains the option to use a callback if the connection is established. The example similarly returns a code that indicates, whether the connection was successful or not.

WlanConnect_SSID_T connectSSID = "yourWifiNetworkSSID";
WlanConnect_PassPhrase_T connectPassPhrase = "yourWifiNetworkPW";
Retcode_T retStatusConnect;
retStatusConnect = (Retcode_T) WlanConnect_WPA(connectSSID,
            connectPassPhrase,0);
if (retStatusConnect == RETCODE_OK) {
  printf("Connected successfully.\n\r");
}
else {
  // Connection failed
}

Disconnecting

Finally, if a connection should be closed, the following code shows an implementation that disconnects from a connected Wi-Fi network. As with other functions, a return code indicates whether the connection was sucessfully terminated or not. The WlanConnect_Disconnect() function call disconnects from a connected Wi-Fi network.

Minimal Network Setup

For most applications of the XDK, a network connection is required to send or receive data. One minimal implementation of a network setup is provided by the following code. Please note, that this code does not implement error handling. In a real world application, error handling is crucial and should not be ignored.

For the following code snippets to work, the following header files must be included:

#include "BCDS_NetworkConfig.h"
#include "BCDS_WlanConnect.h"
#include "PAL_initialize_ih.h"
#include "PAL_socketMonitor_ih.h"

In the following code, replace the strings “yourWifiNetworkSSID” and “yourWifiNetworkPW” with your respective SSID and PW.

WlanConnect_SSID_T connectSSID = (WlanConnect_SSID_T) "yourWifiNetworkSSID";
WlanConnect_PassPhrase_T connectPassPhrase = (WlanConnect_PassPhrase_T) "yourWifiNetworkPW";
WlanConnect_Init();
NetworkConfig_SetIpDhcp(0);
WlanConnect_WPA(connectSSID, connectPassPhrase, NULL);

PAL_initialize();
PAL_socketMonitorInit();

Note that we include and initialize the PAL module here. While this module isn’t required to establish a Wi-Fi connection, it provides several useful networking functions that will be used by most Wi-Fi-based applications (such as resolving domain names to IP addresses). For this reason, we include the module here and recommend to keep this setup as the foundation of more advanced examples.

Additionally, the following code will print the XDK’s IP after it has been set.

NetworkConfig_IpSettings_T myIp;
NetworkConfig_GetIpSettings(&myIp);

// insert a delay here, if the IP is not properly printed
printf("The IP was retrieved: %u.%u.%u.%u \n\r",
(unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 3)),
(unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 2)),
(unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 1)),
(unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 0)));

Full Code Example 2

Note: The full code example is intended for XDK-Workbench versions 3.4.0 and higher.

/*----------------------------------------------------------------------------*

/* --------------------------------------------------------------------------- |
 * INCLUDES & DEFINES ******************************************************** |
 * -------------------------------------------------------------------------- */
#include "XdkAppInfo.h"
#undef BCDS_MODULE_ID  // [i] Module ID define before including Basics package
#define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER

#include <stdio.h>
#include "BCDS_NetworkConfig.h"
#include "BCDS_WlanConnect.h"
#include "BCDS_CmdProcessor.h"
#include "FreeRTOS.h"

/* --------------------------------------------------------------------------- |
 * MACROS ******************************************************************** |
 * -------------------------------------------------------------------------- */

#warning Set these Macros accordingly, before running this application
#define USE_DHCP    1   // 1: DHCP; 0: Static IP
#define USE_WPA     1   // 1: WPA; 0: Open Access point
#define SSID        "yourWifiNetworkSSID"
#define PW          "yourWifiNetworkPW"

/* --------------------------------------------------------------------------- |
 * HANDLES ******************************************************************* |
 * -------------------------------------------------------------------------- */
static CmdProcessor_T * AppCmdProcessor;

/* --------------------------------------------------------------------------- |
 * EXECUTING FUNCTIONS ******************************************************* |
 * -------------------------------------------------------------------------- */

static void initWifi(void)
{
  /* initialize the Wi-Fi module */
  WlanConnect_Init();
}

static void setIPSettings(void) {
#if USE_DHCP
    NetworkConfig_SetIpDhcp(NULL);
#else
    NetworkConfig_IpSettings_T myIpSet;
    myIpSet.isDHCP = (uint8_t) NETWORKCONFIG_DHCP_DISABLED;
    myIpSet.ipV4 = NetworkConfig_Ipv4Value(192, 168, 0, 2);
    myIpSet.ipV4DnsServer = NetworkConfig_Ipv4Value(192, 168, 0, 1);
    myIpSet.ipV4Gateway = NetworkConfig_Ipv4Value(192, 168, 0, 1);
    myIpSet.ipV4Mask = NetworkConfig_Ipv4Value(255, 255, 255, 0);
    NetworkConfig_SetIpStatic(myIpSet);
#endif
}

static void connectWiFi(void){
    WlanConnect_SSID_T connectSSID = (WlanConnect_SSID_T) SSID;
#if USE_WPA
    WlanConnect_PassPhrase_T connectPassPhrase = (WlanConnect_PassPhrase_T) PW;
    WlanConnect_WPA(connectSSID, connectPassPhrase, NULL);
#else
    status = WlanConnect_Open(connectSSID, NULL);
#endif // USE_WPA
}

static void printIP(void){
    NetworkConfig_IpSettings_T myIp;
    NetworkConfig_GetIpSettings(&myIp);

    // insert a delay here, if the IP is not properly printed
    printf("The IP was retrieved: %u.%u.%u.%u \n\r",
    (unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 3)),
    (unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 2)),
    (unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 1)),
    (unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 0)));
}

/* --------------------------------------------------------------------------- |
 * BOOTING- AND SETUP FUNCTIONS ********************************************** |
 * -------------------------------------------------------------------------- */

static void AppControllerSetup(void * param1, uint32_t param2)
{
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);

    Retcode_T retcode = RETCODE_OK;

    // Setup of the necessary module
    vTaskDelay(2500);
    initWifi();
    setIPSettings();
    connectWiFi();
    printIP();

    if (RETCODE_OK != retcode)
    {
        printf("AppControllerSetup : Failed \r\n");
        Retcode_RaiseError(retcode);
        assert(0);
    }
}

void AppController_Init(void * cmdProcessorHandle, uint32_t param2)
{
    BCDS_UNUSED(param2);

    Retcode_T retcode = RETCODE_OK;

    if (cmdProcessorHandle == NULL)
    {
        printf("AppController_Init : Command processor handle is NULL \r\n");
        retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_NULL_POINTER);
    }
    else
    {
        AppCmdProcessor = (CmdProcessor_T *) cmdProcessorHandle;
        retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerSetup, NULL, UINT32_C(0));
    }
    if (RETCODE_OK != retcode)
    {
        Retcode_RaiseError(retcode);
        assert(0);
    }
}

Appendix

XDK Console Output example

The following console log is an example output of the code that has been implemented in the Connectivity WiFi example guide:

Image