Receive Wi-Fi events

Wi-Fi events are defined in wifi_event_t and include the following event types:
  • WIFI_EVENT_STATION_CONNECTION_STATE
  • WIFI_EVENT_SCAN_RESULTS
  • WIFI_EVENT_AGGREGATOR_PROFILE_ENABLE_CHANGE
  • WIFI_EVENT_AGGREGATOR_LOGIN_TIMEOUT
  • WIFI_EVENT_AGGREGATOR_DEREGISTERED

To receive Wi-Fi events. you don't need to use the hotspot aggregator functionality, so you need to be concerned only with the first two events. For information on handling aggregator-related events, see Use the aggregator capability.

To retrieve the non-aggregator events:

  1. Initialize Wi-Fi service by calling wifi_service_initialize().
  2. Retrieve the Wi-Fi service file descriptor by calling wifi_service_get_fd().
  3. Poll the file descriptor for Wi-Fi events.
  4. Retrieve a Wi-Fi event by calling wifi_service_read_event().
  5. Retrieve the event type by calling wifi_service_get_event_type().
  6. You may receive the following events:
    • WIFI_EVENT_STATION_CONNECTION_STATE: This event indicates that the Wi-Fi connection state has changed. If the station is in the connected state, the app can retrieve the service set identifier (SSID), channel, and basic service set identifier (BSSID) of the connection. If the station is in the disconnected state, the app can check the reason for the disconnection.
      • To retrieve the SSID, call wifi_station_connection_get_ssid().
      • To retrieve the channel, call wifi_station_connection_get_channel().
      • To retrieve the BSSID, call wifi_station_connection_get_bssid().
      • To check the disconnection reason, call wifi_station_connection_get_disconnect_reason().
    • WIFI_EVENT_SCAN_RESULTS: This event indicates that new scan results are available. Scan results contain a list of detected access points (APs). The app can retrieve this list and loop through it to obtain information that it is interested in, such as the SSID and BSSID of each AP.
      • To retrieve the list of scan results, call wifi_get_scan_results().
      • To retrieve detailed information about each detected AP, such as the SSID, BSSID, frequency, network capabilities, and signal level, call the functions that are prefixed with wifi_get_scan_result_, such as wifi_get_scan_result_ssid().
      • When you are done with the scan results, free the memory allocated by calling wifi_free_scan_results().
  7. You may also retrieve the user configured profiles on the device, as well as information about each profile.
The sample code below demonstrates how to access Wi-Fi events.

Not applicable

Not applicable

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <sys/pps.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <string.h>
													
#include "wifi/wifi_service.h"

void check_scan_results(wifi_scan_results_t *results, int num)
{
    wifi_result_t ret = WIFI_SUCCESS;
    int option = 0;
    int index = 0;
    char ssid[WIFI_MAX_SSID_BUFFER_LEN];
    uint8_t bssid[WIFI_BSSID_ADDR_LEN];
    int freq = 0;
    int level = 0;
    wifi_key_management_t key;
    uint32_t flags;

    memset(ssid, 0x0, sizeof(ssid));
    memset(bssid, 0x0, sizeof(bssid));

    if ((results == NULL) || (num < 1)) {
        printf("Invalid results.");
        return;
    }

    for (index = 1; index <= num; index ++) {
        ret = wifi_get_scan_result_ssid(results, index, ssid);
        if (ret != WIFI_SUCCESS) {
            // handle error
        }

        ret = wifi_get_scan_result_bssid(results, index, bssid);
        if (ret != WIFI_SUCCESS) {
            // handle error
        }

        ret = wifi_get_scan_result_freq(results, index, &freq);
        if (ret != WIFI_SUCCESS) {
            // handle error
        }

        ret = wifi_get_scan_result_signal_level(results, 
                                                index, 
                                                &level);
        if (ret != WIFI_SUCCESS) {
            // handle error
        }

        ret = wifi_get_scan_result_flags(results, 
                                         index, 
                                         &key, 
                                         &flags);
        if (ret != WIFI_SUCCESS) {
            // handle error
        }
        printf("index #%d\tssid: [%s]\t"
               "bssid: [%2x:%2x:%2x:%2x:%2x:%2x]\t"
               "freq: %d\tlevel: %d\t"
               "key: %d\tflags: %x\n",
               index, ssid,
               bssid[0], bssid[1], bssid[2],
               bssid[3], bssid[4], bssid[5],
               freq, level,
               key, flags);
    }
}

void check_connection_state(wifi_service_event_t * event)
{
    wifi_result_t ret = WIFI_SUCCESS;
    wifi_station_connection_state_t connection_state;
    char ssid[WIFI_MAX_SSID_BUFFER_LEN];

    memset(ssid, 0x0, sizeof(ssid));

    connection_state = WIFI_STATION_RADIO_OFF;
    ret = wifi_service_event_get_station_connection_state(event, 
                                             &connection_state);
    if (ret != WIFI_SUCCESS) {
        // handle error
    }
    printf("WiFi state[%d]\n", connection_state);

    if (connection_state == WIFI_STATION_CONNECTED) {
        int channel = 0;
        uint8_t bssid[WIFI_BSSID_ADDR_LEN];

        ret = wifi_station_connection_get_ssid(ssid);
        if (ret != WIFI_SUCCESS) {
            // handle error
        }

        ret = wifi_station_connection_get_channel(&channel);
        if (ret != WIFI_SUCCESS) {
            // handle error
        }

        ret = wifi_station_connection_get_bssid(bssid);
        if (ret != WIFI_SUCCESS) {
            // handle error
        }

        printf("\nssid: [%s]\nchannel:%d\nbssid: [%2x:%2x:%2x:%2x:%2x:%2x]",
               ssid, channel,
               bssid[0], bssid[1], bssid[2],
               bssid[3], bssid[4], bssid[5]);
    } 
    if (connection_state == WIFI_STATION_DISCONNECTED) {
        wifi_station_disconnect_reason_t reason;
        ret = wifi_station_connection_get_disconnect_reason(&reason);
        if (ret != WIFI_SUCCESS) {
            // handle error
        }
        printf("disconnection reason: %d\n", reason);
    }
}

int read_events_without_aggregator_registration()
{
    wifi_result_t ret = WIFI_SUCCESS;
    wifi_service_event_t * event = NULL;
    wifi_event_t event_type;
    struct pollfd fds;
    int fd = -1;
    int rc = 0;
    int num_events = 0;
    int tmp_int = 0;

    ret = wifi_service_initialize(&wifi_service);
    if (ret != WIFI_SUCCESS) {
        // handle error
    }

    ret = wifi_service_get_fd(wifi_service, &fd);
    if (ret != WIFI_SUCCESS) {
        // handle error
    }

    fds.fd = fd;
    fds.events = POLLRDNORM;

    while (num_events < MAX_NUM_EVENTS_READ) {
        rc = poll(&fds, 1, -1);

        if ((rc < 1) ||
            ((fds.revents & POLLRDNORM) == 0)) {
            break;
        }

        ret = wifi_service_read_event(wifi_service, &event);
        if(ret != WIFI_SUCCESS) {
            // handle error
        }

        printf("\n# of events: %d\n", ++num_events);

        wifi_service_get_event_type(event, &event_type);

        switch(event_type) {
        case WIFI_EVENT_STATION_CONNECTION_STATE:
            printf("\nWIFI_STATION_CONNECTION_STATE_EVENT\n");
            check_connection_state(event);               
            break;
        case WIFI_EVENT_SCAN_RESULTS:
            printf("\nWIFI_SCAN_RESULTS_EVENT\n");

            wifi_scan_results_t *results = NULL;
            wifi_scan_report_t report_type;
            int num = 0;
            wifi_security_type_t security;
            bool enable = false;
            bool user_enable = false;
            
           /* Read user profiles if needed.
            * See code sample under "Check user profiles".
            */
            check_user_profiles();
            
            ret = wifi_get_scan_results(&results, 
                                        &report_type, 
                                        &num);
            if (ret != WIFI_SUCCESS) {
                // handle error
            }
            printf("Scan report type: %d, num: %d\n", 
                    report_type, num);
            
            /* Retrieve information about detected networks */
            check_scan_results(results, num);
            
            ret = wifi_free_scan_results(results);
            if (ret != WIFI_SUCCESS) {
                printf("wifi_free_scan_results() failed. [ret=%d]\n", ret);
                wifi_service_shutdown(wifi_service);
                return -1;
            }
            results = NULL;
            break;
            
        default:
            break;
        }
        
        wifi_service_free_event(wifi_service, event);
        event = NULL;
    }
        
    /* Shutdown and cleanup */
    wifi_service_shutdown(wifi_service);
    wifi_service = NULL;
        
    return 0;   
}
			

Last modified: 2015-05-07



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus