basic/scan/scan.c

See examples/basic/scan

/*
* ZentriOS SDK LICENSE AGREEMENT | Zentri.com, 2015.
*
* Use of source code and/or libraries contained in the ZentriOS SDK is
* subject to the Zentri Operating System SDK license agreement and
* applicable open source license agreements.
*
*/
/* Documentation for this app is available online.
* See https://docs.zentri.com/wifi/sdk/latest/examples/basic/scan
*/
#include "zos.h"
#define SCAN_PERIOD 15000 // ms
/*************************************************************************************************/
void zn_app_init(void)
{
zn_event_register_periodic(scan_event_handler, NULL, SCAN_PERIOD, EVENT_FLAGS1(RUN_NOW));
}
/*************************************************************************************************/
zos_bool_t zn_app_idle(void)
{
// return TRUE so the event loop idles
return ZOS_TRUE;
}
/*************************************************************************************************/
static void scan_event_handler(void *arg)
{
zos_scan_result_t *scan_results;
const uint32_t start_time = zn_rtos_get_time();
int i = 0;
ZOS_LOG( "Scanning for Wi-Fi networks ..." );
if(ZOS_FAILED(ret, zn_network_scan(&scan_results, 0, NULL)))
{
ZOS_LOG("Failed to issue scan: %d", ret);
return;
}
ZOS_LOG(" # Type BSSID RSSI Rate Chan Security SSID" );
ZOS_LOG("--------------------------------------------------------------------" );
for(const zos_scan_result_t *result = scan_results; result != NULL; result = result->next, ++i)
{
print_scan_result(i, result);
}
ZOS_LOG("Scan complete in %lu milliseconds\r\n", zn_rtos_get_time() - start_time);
}
/*************************************************************************************************/
static void print_scan_result(int index, const zos_scan_result_t* record )
{
char buffer[128], mac[20];
fpi_str_buffer_t data_rate_str;
char *ptr = buffer;
fpi_int_to_str(data_rate_str, record->max_data_rate, 1000.0, 3);
ptr += sprintf(ptr, " %2d", index);
ptr += sprintf(ptr, " %5s", ( record->bss_type == ZOS_BSS_TYPE_ADHOC ) ? "Adhoc" : "Infra");
ptr += sprintf(ptr, " %s", mac_to_str(&record->BSSID, mac));
ptr += sprintf(ptr, " %3d", record->signal_strength);
ptr += sprintf(ptr, " %-5s", data_rate_str);
ptr += sprintf(ptr, " %3d", record->channel);
ptr += sprintf(ptr, " %-10s", ( record->security == ZOS_SECURITY_OPEN ) ? "Open" :
( record->security == ZOS_SECURITY_WEP_PSK ) ? "WEP" :
( record->security == ZOS_SECURITY_WPA_TKIP_PSK ) ? "WPA TKIP" :
( record->security == ZOS_SECURITY_WPA_AES_PSK ) ? "WPA AES" :
( record->security == ZOS_SECURITY_WPA2_AES_PSK ) ? "WPA2 AES" :
( record->security == ZOS_SECURITY_WPA2_TKIP_PSK ) ? "WPA2 TKIP" :
( record->security == ZOS_SECURITY_WPA2_MIXED_PSK ) ? "WPA2 Mixed" :
"Unknown" );
ptr += sprintf(ptr, " ");
if(record->SSID.len > 0)
{
ssid_to_str(ptr, &record->SSID);
}
else
{
strcpy(ptr, "<ssid hidden>");
}
ZOS_LOG("%s", buffer);
}