1 /*
2  * Copyright (c) 2024 Nordic Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(net_samples_common, LOG_LEVEL_DBG);
9 
10 #include <zephyr/net/conn_mgr_connectivity.h>
11 
12 #if defined(CONFIG_NET_CONNECTION_MANAGER)
13 #define L4_EVENT_MASK (NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED)
14 
15 static struct net_mgmt_event_callback l4_cb;
16 static K_SEM_DEFINE(network_connected, 0, 1);
17 
l4_event_handler(struct net_mgmt_event_callback * cb,uint32_t event,struct net_if * iface)18 static void l4_event_handler(struct net_mgmt_event_callback *cb, uint32_t event,
19 			     struct net_if *iface)
20 {
21 	switch (event) {
22 	case NET_EVENT_L4_CONNECTED:
23 		LOG_INF("Network connectivity established and IP address assigned");
24 		k_sem_give(&network_connected);
25 		break;
26 	case NET_EVENT_L4_DISCONNECTED:
27 		break;
28 	default:
29 		break;
30 	}
31 }
32 
wait_for_network(void)33 void wait_for_network(void)
34 {
35 	net_mgmt_init_event_callback(&l4_cb, l4_event_handler, L4_EVENT_MASK);
36 	net_mgmt_add_event_callback(&l4_cb);
37 
38 	LOG_INF("Waiting for network...");
39 
40 	k_sem_take(&network_connected, K_FOREVER);
41 }
42 #endif /* CONFIG_NET_CONNECTION_MANAGER */
43