@@ -237,8 +237,6 @@ struct dhcp_msg {
237
237
static uint32_t offer_xid ;
238
238
static uint32_t request_xid ;
239
239
240
- static struct k_sem test_lock ;
241
-
242
240
#define WAIT_TIME K_SECONDS(CONFIG_NET_DHCPV4_INITIAL_DELAY_MAX + 1)
243
241
244
242
struct net_dhcpv4_context {
@@ -248,9 +246,7 @@ struct net_dhcpv4_context {
248
246
249
247
static int net_dhcpv4_dev_init (const struct device * dev )
250
248
{
251
- struct net_dhcpv4_context * net_dhcpv4_context = dev -> data ;
252
-
253
- net_dhcpv4_context = net_dhcpv4_context ;
249
+ ARG_UNUSED (dev );
254
250
255
251
return 0 ;
256
252
}
@@ -475,9 +471,6 @@ NET_DEVICE_INIT(net_dhcpv4_test, "net_dhcpv4_test",
475
471
& net_dhcpv4_if_api , DUMMY_L2 ,
476
472
NET_L2_GET_CTX_TYPE (DUMMY_L2 ), 127 );
477
473
478
- static struct net_mgmt_event_callback rx_cb ;
479
- static struct net_mgmt_event_callback dns_cb ;
480
- static struct net_mgmt_event_callback dhcp_cb ;
481
474
#ifdef CONFIG_NET_DHCPV4_OPTION_CALLBACKS
482
475
static struct net_dhcpv4_option_callback opt_domain_cb ;
483
476
static struct net_dhcpv4_option_callback opt_pop3_cb ;
@@ -490,73 +483,117 @@ static struct net_dhcpv4_option_callback opt_vs_byte_cb;
490
483
static struct net_dhcpv4_option_callback opt_vs_empty_cb ;
491
484
static struct net_dhcpv4_option_callback opt_vs_invalid_cb ;
492
485
#endif
493
- static int event_count ;
494
486
495
- static void receiver_cb (struct net_mgmt_event_callback * cb ,
496
- uint64_t nm_event , struct net_if * iface )
487
+ #define EVT_ADDR_ADD BIT(0)
488
+ #define EVT_DNS_SERVER1_ADD BIT(1)
489
+ #define EVT_DNS_SERVER2_ADD BIT(2)
490
+ #define EVT_DNS_SERVER3_ADD BIT(3)
491
+ #define EVT_DHCP_START BIT(4)
492
+ #define EVT_DHCP_BOUND BIT(5)
493
+ #define EVT_OPTION_DOMAIN BIT(6)
494
+ #define EVT_OPTION_POP3 BIT(7)
495
+ #define EVT_VENDOR_STRING BIT(8)
496
+ #define EVT_VENDOR_BYTE BIT(9)
497
+ #define EVT_VENDOR_EMPTY BIT(10)
498
+
499
+ static K_EVENT_DEFINE (events );
500
+
501
+ static void receiver_cb (uint64_t nm_event , struct net_if * iface , void * info , size_t info_length ,
502
+ void * user_data )
497
503
{
498
- if (nm_event != NET_EVENT_IPV4_ADDR_ADD &&
499
- nm_event != NET_EVENT_DNS_SERVER_ADD &&
500
- nm_event != NET_EVENT_DNS_SERVER_DEL &&
501
- nm_event != NET_EVENT_IPV4_DHCP_START &&
502
- nm_event != NET_EVENT_IPV4_DHCP_BOUND ) {
503
- /* Spurious callback. */
504
- return ;
504
+ const struct in_addr ip_addr = { { { 10 , 237 , 72 , 158 } } };
505
+ const struct in_addr dns_addrs [3 ] = {
506
+ { { { 10 , 248 , 2 , 1 } } },
507
+ { { { 163 , 33 , 253 , 68 } } },
508
+ { { { 10 , 184 , 9 , 1 } } },
509
+ };
510
+
511
+ ARG_UNUSED (iface );
512
+ ARG_UNUSED (user_data );
513
+
514
+ switch (nm_event ) {
515
+ case NET_EVENT_IPV4_ADDR_ADD :
516
+ zassert_equal (info_length , sizeof (struct in_addr ));
517
+ zassert_mem_equal (info , & ip_addr , sizeof (struct in_addr ));
518
+ k_event_post (& events , EVT_ADDR_ADD );
519
+ break ;
520
+ case NET_EVENT_DNS_SERVER_ADD :
521
+ zassert_equal (info_length , sizeof (struct sockaddr ));
522
+ if (net_sin (info )-> sin_addr .s_addr == dns_addrs [0 ].s_addr ) {
523
+ k_event_post (& events , EVT_DNS_SERVER1_ADD );
524
+ } else if (net_sin (info )-> sin_addr .s_addr == dns_addrs [1 ].s_addr ) {
525
+ k_event_post (& events , EVT_DNS_SERVER2_ADD );
526
+ } else if (net_sin (info )-> sin_addr .s_addr == dns_addrs [2 ].s_addr ) {
527
+ k_event_post (& events , EVT_DNS_SERVER3_ADD );
528
+ } else {
529
+ zassert_unreachable ("Unknown DNS server" );
530
+ }
531
+ break ;
532
+ case NET_EVENT_IPV4_DHCP_START :
533
+ k_event_post (& events , EVT_DHCP_START );
534
+ break ;
535
+ case NET_EVENT_IPV4_DHCP_BOUND :
536
+ k_event_post (& events , EVT_DHCP_BOUND );
537
+ break ;
505
538
}
506
-
507
- event_count ++ ;
508
-
509
- k_sem_give (& test_lock );
510
539
}
511
540
541
+ NET_MGMT_REGISTER_EVENT_HANDLER (rx_cb , NET_EVENT_IPV4_ADDR_ADD , receiver_cb , NULL );
542
+ NET_MGMT_REGISTER_EVENT_HANDLER (dns_cb , NET_EVENT_DNS_SERVER_ADD | NET_EVENT_DNS_SERVER_DEL ,
543
+ receiver_cb , NULL );
544
+ NET_MGMT_REGISTER_EVENT_HANDLER (dhcp_cb , NET_EVENT_IPV4_DHCP_START | NET_EVENT_IPV4_DHCP_BOUND ,
545
+ receiver_cb , NULL );
546
+
512
547
#ifdef CONFIG_NET_DHCPV4_OPTION_CALLBACKS
513
548
514
549
static void option_domain_cb (struct net_dhcpv4_option_callback * cb ,
515
550
size_t length ,
516
551
enum net_dhcpv4_msg_type msg_type ,
517
552
struct net_if * iface )
518
553
{
519
- char expectation [] = "fi.intel.com" ;
554
+ static const char expectation [] = "fi.intel.com" ;
555
+
556
+ ARG_UNUSED (msg_type );
557
+ ARG_UNUSED (iface );
520
558
521
559
zassert_equal (cb -> option , OPTION_DOMAIN , "Unexpected option value" );
522
560
zassert_equal (length , sizeof (expectation ), "Incorrect data length" );
523
561
zassert_mem_equal (buffer , expectation , sizeof (expectation ),
524
562
"Incorrect buffer contents" );
525
563
526
- event_count ++ ;
527
-
528
- k_sem_give (& test_lock );
564
+ k_event_post (& events , EVT_OPTION_DOMAIN );
529
565
}
530
566
531
567
static void option_pop3_cb (struct net_dhcpv4_option_callback * cb ,
532
568
size_t length ,
533
569
enum net_dhcpv4_msg_type msg_type ,
534
570
struct net_if * iface )
535
571
{
536
- uint8_t expectation [4 ];
572
+ static const uint8_t expectation [4 ] = { 198 , 51 , 100 , 16 } ;
537
573
538
- expectation [0 ] = 198 ;
539
- expectation [1 ] = 51 ;
540
- expectation [2 ] = 100 ;
541
- expectation [3 ] = 16 ;
574
+ ARG_UNUSED (msg_type );
575
+ ARG_UNUSED (iface );
542
576
543
577
zassert_equal (cb -> option , OPTION_POP3 , "Unexpected option value" );
544
578
zassert_equal (length , sizeof (expectation ), "Incorrect data length" );
545
579
zassert_mem_equal (buffer , expectation , sizeof (expectation ),
546
580
"Incorrect buffer contents" );
547
581
548
- event_count ++ ;
549
-
550
- k_sem_give (& test_lock );
582
+ k_event_post (& events , EVT_OPTION_POP3 );
551
583
}
552
584
553
585
static void option_invalid_cb (struct net_dhcpv4_option_callback * cb ,
554
586
size_t length ,
555
587
enum net_dhcpv4_msg_type msg_type ,
556
588
struct net_if * iface )
557
589
{
590
+ ARG_UNUSED (cb );
591
+ ARG_UNUSED (length );
592
+ ARG_UNUSED (msg_type );
593
+ ARG_UNUSED (iface );
594
+
558
595
/* This function should never be called. If it is, the parser took a wrong turn. */
559
- zassert_true (false, "Unexpected callback - incorrect parsing of vendor sepcific options" );
596
+ zassert_unreachable ( "Unexpected callback - incorrect parsing of vendor sepcific options" );
560
597
}
561
598
562
599
#ifdef CONFIG_NET_DHCPV4_OPTION_CALLBACKS_VENDOR_SPECIFIC
@@ -566,45 +603,48 @@ static void vendor_specific_string_cb(struct net_dhcpv4_option_callback *cb,
566
603
enum net_dhcpv4_msg_type msg_type ,
567
604
struct net_if * iface )
568
605
{
569
- char expectation [] = "string" ;
606
+ static const char expectation [] = "string" ;
607
+
608
+ ARG_UNUSED (msg_type );
609
+ ARG_UNUSED (iface );
570
610
571
611
zassert_equal (cb -> option , OPTION_VENDOR_STRING ,
572
612
"Unexpected vendor specific option value" );
573
613
zassert_equal (length , sizeof (expectation ), "Incorrect data length" );
574
614
zassert_mem_equal (buffer , expectation , sizeof (expectation ), "Incorrect buffer contents" );
575
615
576
- event_count ++ ;
577
-
578
- k_sem_give (& test_lock );
616
+ k_event_post (& events , EVT_VENDOR_STRING );
579
617
}
580
618
581
619
static void vendor_specific_byte_cb (struct net_dhcpv4_option_callback * cb ,
582
620
size_t length ,
583
621
enum net_dhcpv4_msg_type msg_type ,
584
622
struct net_if * iface )
585
623
{
624
+ ARG_UNUSED (msg_type );
625
+ ARG_UNUSED (iface );
626
+
586
627
zassert_equal (cb -> option , OPTION_VENDOR_BYTE ,
587
628
"Unexpected vendor specific option value" );
588
629
zassert_equal (length , 1 , "Incorrect data length" );
589
630
zassert_equal (buffer [0 ], 1 , "Incorrect buffer contents" );
590
631
591
- event_count ++ ;
592
-
593
- k_sem_give (& test_lock );
632
+ k_event_post (& events , EVT_VENDOR_BYTE );
594
633
}
595
634
596
635
static void vendor_specific_empty_cb (struct net_dhcpv4_option_callback * cb ,
597
636
size_t length ,
598
637
enum net_dhcpv4_msg_type msg_type ,
599
638
struct net_if * iface )
600
639
{
640
+ ARG_UNUSED (msg_type );
641
+ ARG_UNUSED (iface );
642
+
601
643
zassert_equal (cb -> option , OPTION_VENDOR_EMPTY ,
602
644
"Unexpected vendor specific option value" );
603
645
zassert_equal (length , 0 , "Incorrect data length" );
604
646
605
- event_count ++ ;
606
-
607
- k_sem_give (& test_lock );
647
+ k_event_post (& events , EVT_VENDOR_EMPTY );
608
648
}
609
649
610
650
#endif /* CONFIG_NET_DHCPV4_OPTION_CALLBACKS_VENDOR_SPECIFIC */
@@ -614,25 +654,7 @@ static void vendor_specific_empty_cb(struct net_dhcpv4_option_callback *cb,
614
654
ZTEST (dhcpv4_tests , test_dhcp )
615
655
{
616
656
struct net_if * iface ;
617
-
618
- k_sem_init (& test_lock , 0 , UINT_MAX );
619
-
620
- net_mgmt_init_event_callback (& rx_cb , receiver_cb ,
621
- NET_EVENT_IPV4_ADDR_ADD );
622
-
623
- net_mgmt_add_event_callback (& rx_cb );
624
-
625
- net_mgmt_init_event_callback (& dns_cb , receiver_cb ,
626
- NET_EVENT_DNS_SERVER_ADD |
627
- NET_EVENT_DNS_SERVER_DEL );
628
-
629
- net_mgmt_add_event_callback (& dns_cb );
630
-
631
- net_mgmt_init_event_callback (& dhcp_cb , receiver_cb ,
632
- NET_EVENT_IPV4_DHCP_START |
633
- NET_EVENT_IPV4_DHCP_BOUND );
634
-
635
- net_mgmt_add_event_callback (& dhcp_cb );
657
+ uint32_t evt ;
636
658
637
659
#ifdef CONFIG_NET_DHCPV4_OPTION_CALLBACKS
638
660
net_dhcpv4_init_option_callback (& opt_domain_cb , option_domain_cb ,
@@ -689,19 +711,29 @@ ZTEST(dhcpv4_tests, test_dhcp)
689
711
690
712
net_dhcpv4_start (iface );
691
713
714
+ evt = k_event_wait (& events , EVT_DHCP_START , false, WAIT_TIME );
715
+ zassert_equal (evt , EVT_DHCP_START , "Missing DHCP start" );
716
+
717
+ #ifdef CONFIG_NET_DHCPV4_OPTION_CALLBACKS
718
+ evt = k_event_wait_all (& events , EVT_OPTION_DOMAIN | EVT_OPTION_POP3 , false, WAIT_TIME );
719
+ zassert_equal (evt , EVT_OPTION_DOMAIN | EVT_OPTION_POP3 , "Missing DHCP option(s) %08x" , evt );
720
+ #endif
721
+
692
722
#ifdef CONFIG_NET_DHCPV4_OPTION_CALLBACKS_VENDOR_SPECIFIC
693
- while (event_count < 16 ) {
694
- #elif defined(CONFIG_NET_DHCPV4_OPTION_CALLBACKS)
695
- while (event_count < 10 ) {
696
- #elif defined(CONFIG_NET_DHCPV4_OPTION_PRINT_IGNORED)
697
- while (event_count < 1 ) {
698
- #else
699
- while (event_count < 5 ) {
723
+ evt = k_event_wait_all (& events , EVT_VENDOR_STRING | EVT_VENDOR_BYTE | EVT_VENDOR_EMPTY ,
724
+ false, WAIT_TIME );
725
+ zassert_equal (evt , EVT_VENDOR_STRING | EVT_VENDOR_BYTE | EVT_VENDOR_EMPTY ,
726
+ "Missing DHCP vendor option(s) %08x" , evt );
700
727
#endif
701
- if (k_sem_take (& test_lock , WAIT_TIME )) {
702
- zassert_true (false, "Timeout while waiting" );
703
- }
704
- }
728
+
729
+ evt = k_event_wait_all (& events ,
730
+ EVT_DNS_SERVER1_ADD | EVT_DNS_SERVER2_ADD | EVT_DNS_SERVER3_ADD ,
731
+ false, WAIT_TIME );
732
+ zassert_equal (evt , EVT_DNS_SERVER1_ADD | EVT_DNS_SERVER2_ADD | EVT_DNS_SERVER3_ADD ,
733
+ "Missing DNS server(s) %08x" , evt );
734
+
735
+ evt = k_event_wait (& events , EVT_DHCP_BOUND , false, WAIT_TIME );
736
+ zassert_equal (evt , EVT_DHCP_BOUND , "Missing DHCP bound" );
705
737
706
738
/* Verify that Request xid matched Offer xid. */
707
739
zassert_equal (offer_xid , request_xid , "Offer/Request xid mismatch, "
0 commit comments