Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions libraries/SocketWrapper/SocketHelpers.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "SocketHelpers.h"
#include <WiFi.h>

#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(sketch, LOG_LEVEL_NONE);
Expand All @@ -8,14 +9,13 @@

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

if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
return;
}
// 1. Handle DHCP events first (e.g., NET_EVENT_IPV4_ADDR_ADD)
if (mgmt_event & NET_EVENT_IPV4_ADDR_ADD) {

for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
char buf[NET_IPV4_ADDR_LEN];
int i = 0;
for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
char buf[NET_IPV4_ADDR_LEN];

if (iface->config.ip.ipv4->unicast[i].ipv4.addr_type != NET_ADDR_DHCP) {
continue;
Expand All @@ -34,6 +34,24 @@
}
}

// 2. Handle WiFi events
if (mgmt_event == NET_EVENT_WIFI_SCAN_RESULT) {
const struct wifi_scan_result *entry = reinterpret_cast<const struct wifi_scan_result *>(cb->info);
// Call the registered callback for scan result, if it's set
if (scanResultCallback) {
scanResultCallback(entry);
}
} else if (mgmt_event == NET_EVENT_WIFI_SCAN_DONE) {
// Call the registered callback for scan done, if it's set
if (scanDoneCallback) {
scanDoneCallback();
}
}

// 3. Handle [placeholder] events
// ...
}

void NetworkInterface::option_handler(struct net_dhcpv4_option_callback *cb, size_t length,
enum net_dhcpv4_msg_type msg_type, struct net_if *iface) {
char buf[NET_IPV4_ADDR_LEN];
Expand All @@ -58,6 +76,14 @@
return 0;
}

int NetworkInterface::wifi() {
net_mgmt_init_event_callback(&mgmt_cb, event_handler,
NET_EVENT_WIFI_SCAN_RESULT | NET_EVENT_WIFI_SCAN_DONE);
net_mgmt_add_event_callback(&mgmt_cb);

return 0;
}

void NetworkInterface::enable_dhcpv4_server(struct net_if *netif, char *_netmask) {
static struct in_addr addr;
static struct in_addr netmaskAddr;
Expand Down Expand Up @@ -191,3 +217,15 @@
void NetworkInterface::setDnsServerIP(const IPAddress dns_server) {
return; // DNS server dynamic configuration is not supported
}

// Initialize static member variables
NetworkInterface::WiFiScanResultCallback NetworkInterface::scanResultCallback = nullptr;
NetworkInterface::WiFiScanDoneCallback NetworkInterface::scanDoneCallback = nullptr;

void NetworkInterface::registerWiFiScanResultCallback(WiFiScanResultCallback callback) {
scanResultCallback = callback;
}

void NetworkInterface::registerWiFiScanDoneCallback(WiFiScanDoneCallback callback) {
scanDoneCallback = callback;
}
43 changes: 27 additions & 16 deletions libraries/SocketWrapper/SocketHelpers.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

#include <zephyr/net/dhcpv4.h>
Expand All @@ -7,35 +7,22 @@
#include <zephyr/linker/sections.h>
#include <errno.h>
#include <stdio.h>
#include <functional>

#include <zephyr/net/net_if.h>
#include <zephyr/net/net_core.h>
#include <zephyr/net/net_context.h>
#include <zephyr/net/net_mgmt.h>
#include <zephyr/net/ethernet.h>
#include <zephyr/net/ethernet_mgmt.h>
#include <zephyr/net/wifi_mgmt.h>

#include <api/IPAddress.h>


#define DHCP_OPTION_NTP (42)

class NetworkInterface {
private:
uint8_t ntp_server[4];
static struct net_mgmt_event_callback mgmt_cb;
static struct net_dhcpv4_option_callback dhcp_cb;

static void event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_event,
struct net_if *iface);

static void option_handler(struct net_dhcpv4_option_callback *cb, size_t length,
enum net_dhcpv4_msg_type msg_type, struct net_if *iface);

protected:
struct net_if *netif = nullptr;
int dhcp();
void enable_dhcpv4_server(struct net_if *netif, char *_netmask = "255.255.255.0");

public:
NetworkInterface() {
}
Expand All @@ -60,4 +47,28 @@
int begin(bool blocking = true, uint32_t additional_event_mask = 0);

bool disconnect();
using WiFiScanResultCallback = std::function<void(const struct wifi_scan_result*)>;
using WiFiScanDoneCallback = std::function<void()>;

void registerWiFiScanResultCallback(WiFiScanResultCallback callback);
void registerWiFiScanDoneCallback(WiFiScanDoneCallback callback);
protected:
struct net_if *netif = nullptr;
int dhcp();
int wifi();
void enable_dhcpv4_server(struct net_if *netif, char *_netmask = "255.255.255.0");
private:
uint8_t ntp_server[4];
static struct net_mgmt_event_callback mgmt_cb;
static struct net_dhcpv4_option_callback dhcp_cb;


static void event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_event,
struct net_if *iface);

static void option_handler(struct net_dhcpv4_option_callback *cb, size_t length,
enum net_dhcpv4_msg_type msg_type, struct net_if *iface);

static WiFiScanResultCallback scanResultCallback;
static WiFiScanDoneCallback scanDoneCallback;
};
4 changes: 2 additions & 2 deletions libraries/WiFi/examples/WiFiWebClient/WiFiWebClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ void setup() {
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 3 seconds for connection:
delay(3000);
// wait 6 seconds for connection:
delay(6000);
}
Serial.println("Connected to wifi");
printWifiStatus();
Expand Down
Loading
Loading