Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
wip: wifi: initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mniestroj committed Oct 12, 2023
1 parent ad5dc6f commit aab276e
Show file tree
Hide file tree
Showing 3 changed files with 439 additions and 104 deletions.
12 changes: 12 additions & 0 deletions samples/common/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ config GOLIOTH_SAMPLE_WIFI
help
Enable utilities for easy WiFi setup, mainly for use inside samples/.

if GOLIOTH_SAMPLE_WIFI

config GOLIOTH_SAMPLE_WIFI_STACK_SIZE
int "WiFi manager stack size"
default 2048

config GOLIOTH_SAMPLE_WIFI_INIT_PRIORITY
int "WiFi manager init priority"
default 90

endif # GOLIOTH_SAMPLE_WIFI

config GOLIOTH_SAMPLE_WIFI_SETTINGS
bool "Load SSID and PSK from settigs subsystem"
depends on GOLIOTH_SAMPLE_WIFI
Expand Down
47 changes: 6 additions & 41 deletions samples/common/net_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,65 +28,30 @@ static void event_cb_handler(struct net_mgmt_event_callback *cb,
}
}

static void wait_for_iface_up_event(struct net_if *iface)
static void wait_for_net_event(struct net_if *iface, uint32_t event)
{
struct wait_data wait;

wait.cb.handler = event_cb_handler;
wait.cb.event_mask = NET_EVENT_IF_UP;
wait.cb.event_mask = event;

k_sem_init(&wait.sem, 0, 1);
net_mgmt_add_event_callback(&wait.cb);

if (!net_if_is_up(iface)) {
k_sem_take(&wait.sem, K_FOREVER);
}
k_sem_take(&wait.sem, K_FOREVER);

net_mgmt_del_event_callback(&wait.cb);
}

static void wait_for_iface_up_poll(struct net_if *iface)
{
while (!net_if_is_up(iface)) {
LOG_DBG("Interface is not up yet!");
k_sleep(K_MSEC(100));
}
}

static void wait_for_iface_up(struct net_if *iface)
{
if (IS_ENABLED(CONFIG_NET_MGMT_EVENT)) {
wait_for_iface_up_event(iface);
} else {
wait_for_iface_up_poll(iface);
}
}

static void wait_for_dhcp_bound(struct net_if *iface)
{
(void)net_mgmt_event_wait_on_iface(iface, NET_EVENT_IPV4_DHCP_BOUND,
NULL, NULL, NULL,
K_FOREVER);
}

void net_connect(void)
{
struct net_if *iface = net_if_get_default();

if (!(IS_ENABLED(CONFIG_GOLIOTH_SAMPLE_WIFI) &&
net_if_flag_is_set(iface, NET_IF_DORMANT))) {
LOG_INF("Waiting for interface to be up");
wait_for_iface_up(iface);
}

if (IS_ENABLED(CONFIG_GOLIOTH_SAMPLE_WIFI)) {
LOG_INF("Connecting to WiFi");
wifi_connect(iface);
}

if (IS_ENABLED(CONFIG_GOLIOTH_SAMPLE_DHCP_BIND)) {
LOG_INF("Starting DHCP to obtain IP address");
net_dhcpv4_start(iface);
wait_for_dhcp_bound(iface);
}

LOG_INF("Waiting to obtain IP address");
wait_for_net_event(iface, NET_EVENT_IPV4_ADDR_ADD);
}
Loading

0 comments on commit aab276e

Please sign in to comment.