3#include <zephyr/kernel.h>
4#include <zephyr/net/net_event.h>
5#include <zephyr/net/net_if.h>
6#include <zephyr/net/net_ip.h>
7#include <zephyr/net/wifi_mgmt.h>
8#include <zephyr/sys/util.h>
9#include <zephyr/version.h>
11#include "AudioLogger.h"
17 "WiFiZephyr requires CONFIG_NETWORKING=y");
19 "WiFiZephyr requires CONFIG_NET_L2_WIFI_MGMT=y");
20BUILD_ASSERT(IS_ENABLED(CONFIG_WIFI),
"WiFiZephyr requires CONFIG_WIFI=y");
22 "WiFiZephyr requires CONFIG_NET_DHCPV4=y");
51 bool begin(
const char* ssid,
const char* password) {
55 LOGE(
"setupWIFI failed");
65 struct net_if* iface = net_if_get_default();
67 net_mgmt(NET_REQUEST_WIFI_DISCONNECT, iface,
nullptr, 0);
70 net_mgmt_del_event_callback(&
_wifi_cb);
71 net_mgmt_del_event_callback(&
_ip_cb);
88 const struct in_addr&
ip()
const {
return _ip; }
91 static char address_str[NET_IPV4_ADDR_LEN];
92 net_addr_ntop(AF_INET, &
_ip, address_str,
sizeof(address_str));
98 struct in_addr
_ip = {};
109 bool setupWIFI(
const char* ssid,
const char* password) {
111 assert(password !=
nullptr);
112 LOGI(
"setupWIFI: %s", ssid);
114 struct net_if* iface = net_if_get_default();
116 LOGE(
"No default network interface");
121 net_mgmt_init_event_callback(
123 NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT);
124 net_mgmt_add_event_callback(&
_wifi_cb);
127 NET_EVENT_IPV4_ADDR_ADD);
128 net_mgmt_add_event_callback(&
_ip_cb);
134 struct wifi_ps_params ps_params = {};
136 net_mgmt(NET_REQUEST_WIFI_PS, iface, &ps_params,
sizeof(ps_params));
139 struct wifi_connect_req_params params = {};
140 params.ssid = (
const uint8_t*)ssid;
141 params.ssid_length = (uint8_t)strlen(ssid);
142 params.psk = (
const uint8_t*)password;
143 params.psk_length = (uint8_t)strlen(password);
144 params.channel = WIFI_CHANNEL_ANY;
145 params.security = WIFI_SECURITY_TYPE_PSK;
147 int rc = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, ¶ms,
sizeof(params));
149 LOGE(
"NET_REQUEST_WIFI_CONNECT failed: %d", rc);
162 uint64_t mgmt_event,
struct net_if* iface) {
166 if (mgmt_event == NET_EVENT_WIFI_CONNECT_RESULT) {
167 const struct wifi_status*
status = (
const struct wifi_status*)cb->info;
169 LOGI(
"WiFi connected — waiting for IP...");
172 LOGE(
"WiFi connection failed (status=%d)",
175 net_mgmt(NET_REQUEST_WIFI_CONNECT, iface,
nullptr, 0);
177 }
else if (mgmt_event == NET_EVENT_WIFI_DISCONNECT_RESULT) {
178 LOGI(
"WiFi disconnected — reconnecting...");
181 net_mgmt(NET_REQUEST_WIFI_CONNECT, iface,
nullptr, 0);
187 uint64_t mgmt_event,
struct net_if* iface) {
191 if (mgmt_event == NET_EVENT_IPV4_ADDR_ADD) {
193 struct net_if_ipv4* ipv4 = iface->config.
ip.ipv4;
196 for (
int i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
197#if KERNEL_VERSION_NUMBER >= ZEPHYR_VERSION(3,4,0)
201 struct net_if_addr_ipv4* entry = &ipv4->unicast[i];
202 struct net_if_addr* addr = &entry->ipv4;
205 struct net_if_addr* addr = &ipv4->unicast[i];
207 if (addr->is_used && addr->addr_type == NET_ADDR_DHCP &&
208 addr->address.family == AF_INET) {
209 self->
_ip = addr->address.in_addr;
212 char ip_str[NET_IPV4_ADDR_LEN];
213 net_addr_ntop(AF_INET, &self->
_ip, ip_str,
sizeof(ip_str));
214 LOGI(
"==> Station connected with IP: %s", ip_str);
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
BUILD_ASSERT(IS_ENABLED(CONFIG_NETWORKING), "WiFiZephyr requires CONFIG_NETWORKING=y")
#define assert(T)
Definition avr.h:10