Skip to content

Commit 2116455

Browse files
committed
WiFi: Addressed code review findings.
1 parent f769b0c commit 2116455

File tree

4 files changed

+284
-219
lines changed

4 files changed

+284
-219
lines changed

libraries/SocketWrapper/SocketHelpers.cpp

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "SocketHelpers.h"
2+
#include <WiFi.h>
23

34
#include <zephyr/logging/log.h>
45
LOG_MODULE_DECLARE(sketch, LOG_LEVEL_NONE);
@@ -8,14 +9,13 @@ struct net_dhcpv4_option_callback NetworkInterface::dhcp_cb;
89

910
void NetworkInterface::event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_event,
1011
struct net_if *iface) {
11-
int i = 0;
1212

13-
if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
14-
return;
15-
}
13+
// 1. Handle DHCP events first (e.g., NET_EVENT_IPV4_ADDR_ADD)
14+
if (mgmt_event & NET_EVENT_IPV4_ADDR_ADD) {
1615

17-
for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
18-
char buf[NET_IPV4_ADDR_LEN];
16+
int i = 0;
17+
for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
18+
char buf[NET_IPV4_ADDR_LEN];
1919

2020
if (iface->config.ip.ipv4->unicast[i].ipv4.addr_type != NET_ADDR_DHCP) {
2121
continue;
@@ -34,6 +34,24 @@ void NetworkInterface::event_handler(struct net_mgmt_event_callback *cb, uint64_
3434
}
3535
}
3636

37+
// 2. Handle WiFi events
38+
if (mgmt_event == NET_EVENT_WIFI_SCAN_RESULT) {
39+
const struct wifi_scan_result *entry = reinterpret_cast<const struct wifi_scan_result *>(cb->info);
40+
// Call the registered callback for scan result, if it's set
41+
if (scanResultCallback) {
42+
scanResultCallback(entry);
43+
}
44+
} else if (mgmt_event == NET_EVENT_WIFI_SCAN_DONE) {
45+
// Call the registered callback for scan done, if it's set
46+
if (scanDoneCallback) {
47+
scanDoneCallback();
48+
}
49+
}
50+
51+
// 3. Handle [placeholder] events
52+
// ...
53+
}
54+
3755
void NetworkInterface::option_handler(struct net_dhcpv4_option_callback *cb, size_t length,
3856
enum net_dhcpv4_msg_type msg_type, struct net_if *iface) {
3957
char buf[NET_IPV4_ADDR_LEN];
@@ -58,6 +76,14 @@ int NetworkInterface::dhcp() {
5876
return 0;
5977
}
6078

79+
int NetworkInterface::wifi() {
80+
net_mgmt_init_event_callback(&mgmt_cb, event_handler,
81+
NET_EVENT_WIFI_SCAN_RESULT | NET_EVENT_WIFI_SCAN_DONE);
82+
net_mgmt_add_event_callback(&mgmt_cb);
83+
84+
return 0;
85+
}
86+
6187
void NetworkInterface::enable_dhcpv4_server(struct net_if *netif, char *_netmask) {
6288
static struct in_addr addr;
6389
static struct in_addr netmaskAddr;
@@ -191,3 +217,15 @@ void NetworkInterface::setGatewayIP(const IPAddress gateway) {
191217
void NetworkInterface::setDnsServerIP(const IPAddress dns_server) {
192218
return; // DNS server dynamic configuration is not supported
193219
}
220+
221+
// Initialize static member variables
222+
NetworkInterface::WiFiScanResultCallback NetworkInterface::scanResultCallback = nullptr;
223+
NetworkInterface::WiFiScanDoneCallback NetworkInterface::scanDoneCallback = nullptr;
224+
225+
void NetworkInterface::registerWiFiScanResultCallback(WiFiScanResultCallback callback) {
226+
scanResultCallback = callback;
227+
}
228+
229+
void NetworkInterface::registerWiFiScanDoneCallback(WiFiScanDoneCallback callback) {
230+
scanDoneCallback = callback;
231+
}

libraries/SocketWrapper/SocketHelpers.h

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,22 @@
77
#include <zephyr/linker/sections.h>
88
#include <errno.h>
99
#include <stdio.h>
10+
#include <functional>
1011

1112
#include <zephyr/net/net_if.h>
1213
#include <zephyr/net/net_core.h>
1314
#include <zephyr/net/net_context.h>
1415
#include <zephyr/net/net_mgmt.h>
1516
#include <zephyr/net/ethernet.h>
1617
#include <zephyr/net/ethernet_mgmt.h>
18+
#include <zephyr/net/wifi_mgmt.h>
1719

1820
#include <api/IPAddress.h>
1921

22+
2023
#define DHCP_OPTION_NTP (42)
2124

2225
class NetworkInterface {
23-
private:
24-
uint8_t ntp_server[4];
25-
static struct net_mgmt_event_callback mgmt_cb;
26-
static struct net_dhcpv4_option_callback dhcp_cb;
27-
28-
static void event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_event,
29-
struct net_if *iface);
30-
31-
static void option_handler(struct net_dhcpv4_option_callback *cb, size_t length,
32-
enum net_dhcpv4_msg_type msg_type, struct net_if *iface);
33-
34-
protected:
35-
struct net_if *netif = nullptr;
36-
int dhcp();
37-
void enable_dhcpv4_server(struct net_if *netif, char *_netmask = "255.255.255.0");
38-
3926
public:
4027
NetworkInterface() {
4128
}
@@ -60,4 +47,28 @@ class NetworkInterface {
6047
int begin(bool blocking = true, uint32_t additional_event_mask = 0);
6148

6249
bool disconnect();
50+
using WiFiScanResultCallback = std::function<void(const struct wifi_scan_result*)>;
51+
using WiFiScanDoneCallback = std::function<void()>;
52+
53+
void registerWiFiScanResultCallback(WiFiScanResultCallback callback);
54+
void registerWiFiScanDoneCallback(WiFiScanDoneCallback callback);
55+
protected:
56+
struct net_if *netif = nullptr;
57+
int dhcp();
58+
int wifi();
59+
void enable_dhcpv4_server(struct net_if *netif, char *_netmask = "255.255.255.0");
60+
private:
61+
uint8_t ntp_server[4];
62+
static struct net_mgmt_event_callback mgmt_cb;
63+
static struct net_dhcpv4_option_callback dhcp_cb;
64+
65+
66+
static void event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_event,
67+
struct net_if *iface);
68+
69+
static void option_handler(struct net_dhcpv4_option_callback *cb, size_t length,
70+
enum net_dhcpv4_msg_type msg_type, struct net_if *iface);
71+
72+
static WiFiScanResultCallback scanResultCallback;
73+
static WiFiScanDoneCallback scanDoneCallback;
6374
};

0 commit comments

Comments
 (0)