To receive Wi-Fi events. you don't need to use the hotspot aggregator functionality, so you only need to be concerned with the first two events. For information on handling aggregator-related events, see Use the aggregator capability.
To retrieve the non-aggregator events:
#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;
}