77#include <zephyr/kernel.h>
88#include <zephyr/logging/log.h>
99#include <zephyr/zbus/zbus.h>
10- #include <zephyr/net/conn_mgr_connectivity.h>
11- #include <zephyr/net/conn_mgr_monitor.h>
10+ #include <modem/nrf_modem_lib.h>
1211#include <zephyr/task_wdt/task_wdt.h>
1312#include <date_time.h>
1413#include <zephyr/smf.h>
@@ -42,14 +41,6 @@ ZBUS_CHAN_ADD_OBS(NETWORK_CHAN, network, 0);
4241
4342#define MAX_MSG_SIZE sizeof(struct network_msg)
4443
45- /* Macros used to subscribe to specific Zephyr NET management events. */
46- #define L4_EVENT_MASK (NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED)
47- #define CONN_LAYER_EVENT_MASK (NET_EVENT_CONN_IF_FATAL_ERROR)
48-
49- /* Zephyr NET management event callback structures. */
50- static struct net_mgmt_event_callback l4_cb ;
51- static struct net_mgmt_event_callback conn_cb ;
52-
5344/* State machine */
5445
5546/* Network module states.
@@ -161,42 +152,6 @@ static void network_msg_send(const struct network_msg *msg)
161152 }
162153}
163154
164- static void l4_event_handler (struct net_mgmt_event_callback * cb ,
165- uint64_t event ,
166- struct net_if * iface )
167- {
168- ARG_UNUSED (cb );
169- ARG_UNUSED (iface );
170-
171- switch (event ) {
172- case NET_EVENT_L4_CONNECTED :
173- LOG_DBG ("Network connectivity established" );
174- network_status_notify (NETWORK_CONNECTED );
175- break ;
176- case NET_EVENT_L4_DISCONNECTED :
177- LOG_DBG ("Network connectivity lost" );
178- network_status_notify (NETWORK_DISCONNECTED );
179- break ;
180- default :
181- /* Don't care */
182- return ;
183- }
184- }
185-
186- static void connectivity_event_handler (struct net_mgmt_event_callback * cb ,
187- uint64_t event ,
188- struct net_if * iface )
189- {
190- ARG_UNUSED (cb );
191- ARG_UNUSED (iface );
192-
193- if (event == NET_EVENT_CONN_IF_FATAL_ERROR ) {
194- LOG_ERR ("NET_EVENT_CONN_IF_FATAL_ERROR" );
195- SEND_FATAL_ERROR ();
196- return ;
197- }
198- }
199-
200155static void lte_lc_evt_handler (const struct lte_lc_evt * const evt )
201156{
202157 switch (evt -> type ) {
@@ -208,6 +163,38 @@ static void lte_lc_evt_handler(const struct lte_lc_evt *const evt)
208163 LOG_WRN ("Not registered, check rejection cause" );
209164 network_status_notify (NETWORK_ATTACH_REJECTED );
210165 }
166+
167+ break ;
168+ case LTE_LC_EVT_PDN :
169+ switch (evt -> pdn .type ) {
170+ case LTE_LC_EVT_PDN_ACTIVATED :
171+ LOG_DBG ("PDN connection activated" );
172+ network_status_notify (NETWORK_CONNECTED );
173+
174+ break ;
175+ case LTE_LC_EVT_PDN_DEACTIVATED :
176+ LOG_DBG ("PDN connection deactivated" );
177+ network_status_notify (NETWORK_DISCONNECTED );
178+
179+ break ;
180+ case LTE_LC_EVT_PDN_NETWORK_DETACH :
181+ LOG_DBG ("PDN connection network detached" );
182+ network_status_notify (NETWORK_DISCONNECTED );
183+
184+ break ;
185+ case LTE_LC_EVT_PDN_SUSPENDED :
186+ LOG_DBG ("PDN connection suspended" );
187+ network_status_notify (NETWORK_DISCONNECTED );
188+
189+ break ;
190+ case LTE_LC_EVT_PDN_RESUMED :
191+ LOG_DBG ("PDN connection resumed" );
192+ network_status_notify (NETWORK_CONNECTED );
193+
194+ default :
195+ break ;
196+ }
197+
211198 break ;
212199 case LTE_LC_EVT_MODEM_EVENT :
213200 /* If a reset loop happens in the field, it should not be necessary
@@ -224,6 +211,7 @@ static void lte_lc_evt_handler(const struct lte_lc_evt *const evt)
224211 LOG_DBG ("LTE_LC_MODEM_EVT_SEARCH_DONE" );
225212 network_status_notify (NETWORK_SEARCH_DONE );
226213 }
214+
227215 break ;
228216 case LTE_LC_EVT_PSM_UPDATE : {
229217 struct network_msg msg = {
@@ -302,10 +290,10 @@ static int network_disconnect(void)
302290{
303291 int err ;
304292
305- err = conn_mgr_all_if_disconnect (true );
293+ err = lte_lc_offline ( );
306294 if (err ) {
307- LOG_ERR ("conn_mgr_all_if_down , error: %d" , err );
308- SEND_FATAL_ERROR ();
295+ LOG_ERR ("lte_lc_offline , error: %d" , err );
296+
309297 return err ;
310298 }
311299
@@ -322,26 +310,23 @@ static void state_running_entry(void *obj)
322310
323311 LOG_DBG ("state_running_entry" );
324312
325- /* Setup handler for Zephyr NET Connection Manager events. */
326- net_mgmt_init_event_callback ( & l4_cb , & l4_event_handler , L4_EVENT_MASK );
327- net_mgmt_add_event_callback ( & l4_cb );
313+ err = nrf_modem_lib_init ();
314+ if ( err ) {
315+ LOG_ERR ( "Failed to initialize the modem library, error: %d" , err );
328316
329- /* Setup handler for Zephyr NET Connection Manager Connectivity layer. */
330- net_mgmt_init_event_callback (& conn_cb , & connectivity_event_handler , CONN_LAYER_EVENT_MASK );
331- net_mgmt_add_event_callback (& conn_cb );
317+ return ;
318+ }
332319
333- /* Connecting to the configured connectivity layer. */
334- LOG_DBG ("Bringing network interface up and connecting to the network" );
320+ lte_lc_register_handler (lte_lc_evt_handler );
335321
336- err = conn_mgr_all_if_up (true);
322+ /* Register handler for default PDP context. */
323+ err = lte_lc_pdn_default_ctx_events_enable ();
337324 if (err ) {
338- LOG_ERR ("conn_mgr_all_if_up , error: %d" , err );
339- SEND_FATAL_ERROR ();
325+ LOG_ERR ("lte_lc_pdn_default_ctx_events_enable , error: %d" , err );
326+
340327 return ;
341328 }
342329
343- lte_lc_register_handler (lte_lc_evt_handler );
344-
345330 LOG_DBG ("Network module started" );
346331}
347332
@@ -382,16 +367,6 @@ static void state_disconnected_entry(void *obj)
382367 ARG_UNUSED (obj );
383368
384369 LOG_DBG ("state_disconnected_entry" );
385-
386- /* Resend connection status if the sample is built for Native Sim.
387- * This is necessary because the network interface is automatically brought up
388- * at SYS_INIT() before main() is called.
389- * This means that NET_EVENT_L4_CONNECTED fires before the
390- * appropriate handler l4_event_handler() is registered.
391- */
392- if (IS_ENABLED (CONFIG_BOARD_NATIVE_SIM )) {
393- conn_mgr_mon_resend_status ();
394- }
395370}
396371
397372static enum smf_state_result state_disconnected_run (void * obj )
@@ -424,21 +399,11 @@ static void state_disconnected_searching_entry(void *obj)
424399
425400 LOG_DBG ("state_disconnected_searching_entry" );
426401
427- err = conn_mgr_all_if_connect (true );
402+ err = lte_lc_connect_async ( lte_lc_evt_handler );
428403 if (err ) {
429- LOG_ERR ("conn_mgr_all_if_connect, error: %d" , err );
430- SEND_FATAL_ERROR ();
431- return ;
432- }
404+ LOG_ERR ("lte_lc_connect_async, error: %d" , err );
433405
434- /* Resend connection status if the sample is built for Native Sim.
435- * This is necessary because the network interface is automatically brought up
436- * at SYS_INIT() before main() is called.
437- * This means that NET_EVENT_L4_CONNECTED fires before the
438- * appropriate handler l4_event_handler() is registered.
439- */
440- if (IS_ENABLED (CONFIG_BOARD_NATIVE_SIM )) {
441- conn_mgr_mon_resend_status ();
406+ return ;
442407 }
443408}
444409
0 commit comments