15
15
16
16
#include <zephyr/drivers/cellular.h>
17
17
18
- #define SAMPLE_TEST_ENDPOINT_HOSTNAME CONFIG_SAMPLE_CELLULAR_MODEM_ENDPOINT_HOSTNAME
18
+ #define L4_EVENT_MASK \
19
+ (NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED | NET_EVENT_DNS_SERVER_ADD)
20
+ #define SAMPLE_TEST_ENDPOINT_HOSTNAME CONFIG_SAMPLE_CELLULAR_MODEM_ENDPOINT_HOSTNAME
19
21
#define SAMPLE_TEST_ENDPOINT_UDP_ECHO_PORT (7780)
20
22
#define SAMPLE_TEST_ENDPOINT_UDP_RECEIVE_PORT (7781)
21
23
#define SAMPLE_TEST_PACKET_SIZE (1024)
22
24
#define SAMPLE_TEST_ECHO_PACKETS (16)
23
25
#define SAMPLE_TEST_TRANSMIT_PACKETS (128)
26
+ #define L4_CONNECTED 1
27
+ #define L4_DNS_ADDED 2
24
28
25
29
const struct device * modem = DEVICE_DT_GET (DT_ALIAS (modem ));
26
30
27
31
static uint8_t sample_test_packet [SAMPLE_TEST_PACKET_SIZE ];
28
32
static uint8_t sample_recv_buffer [SAMPLE_TEST_PACKET_SIZE ];
29
33
static bool sample_test_dns_in_progress ;
30
34
static struct dns_addrinfo sample_test_dns_addrinfo ;
31
-
35
+ struct net_if * ppp_iface ;
36
+ K_EVENT_DEFINE (l4_event );
32
37
K_SEM_DEFINE (dns_query_sem , 0 , 1 );
33
38
34
39
static uint8_t sample_prng_random (void )
@@ -406,9 +411,32 @@ int sample_transmit_packets(struct sockaddr *ai_addr, socklen_t ai_addrlen, uint
406
411
return 0 ;
407
412
}
408
413
414
+ static void l4_event_handler (uint64_t event , struct net_if * iface , void * info , size_t info_length ,
415
+ void * user_data )
416
+ {
417
+ if (iface != ppp_iface ) {
418
+ return ;
419
+ }
420
+
421
+ switch (event ) {
422
+ case NET_EVENT_L4_CONNECTED :
423
+ k_event_post (& l4_event , L4_CONNECTED );
424
+ break ;
425
+ case NET_EVENT_DNS_SERVER_ADD :
426
+ k_event_post (& l4_event , L4_DNS_ADDED );
427
+ break ;
428
+ case NET_EVENT_L4_DISCONNECTED :
429
+ k_event_set (& l4_event , 0 );
430
+ break ;
431
+ default :
432
+ break ;
433
+ }
434
+ }
435
+
436
+ NET_MGMT_REGISTER_EVENT_HANDLER (l4_events , L4_EVENT_MASK , l4_event_handler , NULL );
437
+
409
438
int main (void )
410
439
{
411
- struct net_if * const iface = net_if_get_first_by_type (& NET_L2_GET_NAME (PPP ));
412
440
uint16_t * port ;
413
441
int ret ;
414
442
@@ -419,25 +447,33 @@ int main(void)
419
447
420
448
init_sample_test_packet ();
421
449
450
+ ppp_iface = net_if_get_first_by_type (& NET_L2_GET_NAME (PPP ));
451
+
422
452
printk ("Powering on modem\n" );
423
453
pm_device_action_run (modem , PM_DEVICE_ACTION_RESUME );
424
454
425
455
printk ("Bring up network interface\n" );
426
- ret = net_if_up (iface );
456
+ ret = net_if_up (ppp_iface );
427
457
if (ret < 0 ) {
428
458
printk ("Failed to bring up network interface\n" );
429
459
return -1 ;
430
460
}
431
461
432
- printk ("Waiting for L4 connected & DNS server added\n" );
433
- ret = net_mgmt_event_wait_on_iface (iface , NET_EVENT_L4_CONNECTED | NET_EVENT_DNS_SERVER_ADD ,
434
- NULL , NULL , NULL , K_SECONDS (120 ));
462
+ printk ("Waiting for L4 connected\n" );
463
+ ret = k_event_wait (& l4_event , L4_CONNECTED , false, K_SECONDS (120 ));
435
464
436
- if (ret != 0 ) {
465
+ if (ret != L4_CONNECTED ) {
437
466
printk ("L4 was not connected in time\n" );
438
467
return -1 ;
439
468
}
440
469
470
+ printk ("Waiting for DNS server added\n" );
471
+ ret = k_event_wait (& l4_event , L4_DNS_ADDED , false, K_SECONDS (10 ));
472
+ if (ret != L4_DNS_ADDED ) {
473
+ printk ("DNS server was not added in time\n" );
474
+ return -1 ;
475
+ }
476
+
441
477
printk ("Retrieving cellular info\n" );
442
478
print_cellular_info ();
443
479
@@ -495,9 +531,8 @@ int main(void)
495
531
pm_device_action_run (modem , PM_DEVICE_ACTION_RESUME );
496
532
497
533
printk ("Waiting for L4 connected\n" );
498
- ret = net_mgmt_event_wait_on_iface (iface , NET_EVENT_L4_CONNECTED , NULL , NULL , NULL ,
499
- K_SECONDS (60 ));
500
- if (ret != 0 ) {
534
+ ret = k_event_wait (& l4_event , L4_CONNECTED , false, K_SECONDS (120 ));
535
+ if (ret != L4_CONNECTED ) {
501
536
printk ("L4 was not connected in time\n" );
502
537
return -1 ;
503
538
}
@@ -514,7 +549,7 @@ int main(void)
514
549
return -1 ;
515
550
}
516
551
517
- ret = net_if_down (iface );
552
+ ret = net_if_down (ppp_iface );
518
553
if (ret < 0 ) {
519
554
printk ("Failed to bring down network interface\n" );
520
555
return -1 ;
0 commit comments