33#include " utility/wl_definitions.h"
44#include < zephyr/net/wifi_mgmt.h>
55
6+ // Max number of scan results to store
7+ #define MAX_SCAN_RESULTS 20
8+
69#define NET_EVENT_WIFI_MASK \
710 (NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | \
811 NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \
@@ -31,6 +34,62 @@ class WiFiClass : public NetworkInterface {
3134
3235 String firmwareVersion ();
3336
37+ static void scanEventDispatcher (struct net_mgmt_event_callback *cb, uint64_t mgmt_event,
38+ struct net_if *iface) {
39+ if (instance != nullptr ) {
40+ instance->handleScanEvent (cb, mgmt_event, iface);
41+ }
42+ }
43+
44+ void handleScanEvent (struct net_mgmt_event_callback *cb, uint64_t mgmt_event,
45+ struct net_if *iface) {
46+ if (mgmt_event == NET_EVENT_WIFI_SCAN_RESULT) {
47+ const struct wifi_scan_result *entry =
48+ reinterpret_cast <const struct wifi_scan_result *>(cb->info );
49+ if (resultCount < MAX_SCAN_RESULTS) {
50+ memcpy (&scanResults[resultCount], entry, sizeof (struct wifi_scan_result ));
51+ resultCount++;
52+
53+ // for each new result found, compare network name with desired one
54+ if (!memcmp (entry->ssid , sta_config.ssid , entry->ssid_length )) {
55+ // if a match is found, add missing info to config before attempting to connect
56+ sta_config.security = entry->security ;
57+ sta_config.channel = entry->channel ;
58+ sta_config.band = entry->band ;
59+ sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
60+
61+ setSoughtNetworkFound (true );
62+ }
63+ }
64+ }
65+
66+ if (mgmt_event == NET_EVENT_WIFI_SCAN_DONE) {
67+ setScanSequenceFinished (true );
68+
69+ if (resultCount = 0 ) {
70+ printk (" No networks found.\n " );
71+ }
72+ }
73+ }
74+
75+ void setScanSequenceFinished (bool scanFinished) {
76+ scanSequenceFinished = scanFinished;
77+ }
78+
79+ void setSoughtNetworkFound (bool networkFound) {
80+ soughtNetworkFound = networkFound;
81+ }
82+
83+ bool getScanSequenceFinished (void ) {
84+ return scanSequenceFinished;
85+ }
86+
87+ bool getSoughtNetworkFound (void ) {
88+ return soughtNetworkFound;
89+ }
90+
91+ static WiFiClass *instance;
92+
3493private:
3594 struct net_if *sta_iface = nullptr ;
3695 struct net_if *ap_iface = nullptr ;
@@ -39,6 +98,13 @@ class WiFiClass : public NetworkInterface {
3998 struct wifi_connect_req_params sta_config;
4099
41100 struct wifi_iface_status sta_state = {0 };
101+
102+ struct wifi_scan_result scanResults[MAX_SCAN_RESULTS];
103+ uint8_t resultCount;
104+ struct net_mgmt_event_callback wifiCb;
105+
106+ bool soughtNetworkFound = false ;
107+ bool scanSequenceFinished = false ;
42108};
43109
44110extern WiFiClass WiFi;
0 commit comments