@@ -237,8 +237,6 @@ struct dhcp_msg {
237237static uint32_t offer_xid ;
238238static uint32_t request_xid ;
239239
240- static struct k_sem test_lock ;
241-
242240#define WAIT_TIME K_SECONDS(CONFIG_NET_DHCPV4_INITIAL_DELAY_MAX + 1)
243241
244242struct net_dhcpv4_context {
@@ -248,9 +246,7 @@ struct net_dhcpv4_context {
248246
249247static int net_dhcpv4_dev_init (const struct device * dev )
250248{
251- struct net_dhcpv4_context * net_dhcpv4_context = dev -> data ;
252-
253- net_dhcpv4_context = net_dhcpv4_context ;
249+ ARG_UNUSED (dev );
254250
255251 return 0 ;
256252}
@@ -475,9 +471,6 @@ NET_DEVICE_INIT(net_dhcpv4_test, "net_dhcpv4_test",
475471 & net_dhcpv4_if_api , DUMMY_L2 ,
476472 NET_L2_GET_CTX_TYPE (DUMMY_L2 ), 127 );
477473
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 ;
481474#ifdef CONFIG_NET_DHCPV4_OPTION_CALLBACKS
482475static struct net_dhcpv4_option_callback opt_domain_cb ;
483476static struct net_dhcpv4_option_callback opt_pop3_cb ;
@@ -490,73 +483,117 @@ static struct net_dhcpv4_option_callback opt_vs_byte_cb;
490483static struct net_dhcpv4_option_callback opt_vs_empty_cb ;
491484static struct net_dhcpv4_option_callback opt_vs_invalid_cb ;
492485#endif
493- static int event_count ;
494486
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 )
497503{
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 ;
505538 }
506-
507- event_count ++ ;
508-
509- k_sem_give (& test_lock );
510539}
511540
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+
512547#ifdef CONFIG_NET_DHCPV4_OPTION_CALLBACKS
513548
514549static void option_domain_cb (struct net_dhcpv4_option_callback * cb ,
515550 size_t length ,
516551 enum net_dhcpv4_msg_type msg_type ,
517552 struct net_if * iface )
518553{
519- char expectation [] = "fi.intel.com" ;
554+ static const char expectation [] = "fi.intel.com" ;
555+
556+ ARG_UNUSED (msg_type );
557+ ARG_UNUSED (iface );
520558
521559 zassert_equal (cb -> option , OPTION_DOMAIN , "Unexpected option value" );
522560 zassert_equal (length , sizeof (expectation ), "Incorrect data length" );
523561 zassert_mem_equal (buffer , expectation , sizeof (expectation ),
524562 "Incorrect buffer contents" );
525563
526- event_count ++ ;
527-
528- k_sem_give (& test_lock );
564+ k_event_post (& events , EVT_OPTION_DOMAIN );
529565}
530566
531567static void option_pop3_cb (struct net_dhcpv4_option_callback * cb ,
532568 size_t length ,
533569 enum net_dhcpv4_msg_type msg_type ,
534570 struct net_if * iface )
535571{
536- uint8_t expectation [4 ];
572+ static const uint8_t expectation [4 ] = { 198 , 51 , 100 , 16 } ;
537573
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 );
542576
543577 zassert_equal (cb -> option , OPTION_POP3 , "Unexpected option value" );
544578 zassert_equal (length , sizeof (expectation ), "Incorrect data length" );
545579 zassert_mem_equal (buffer , expectation , sizeof (expectation ),
546580 "Incorrect buffer contents" );
547581
548- event_count ++ ;
549-
550- k_sem_give (& test_lock );
582+ k_event_post (& events , EVT_OPTION_POP3 );
551583}
552584
553585static void option_invalid_cb (struct net_dhcpv4_option_callback * cb ,
554586 size_t length ,
555587 enum net_dhcpv4_msg_type msg_type ,
556588 struct net_if * iface )
557589{
590+ ARG_UNUSED (cb );
591+ ARG_UNUSED (length );
592+ ARG_UNUSED (msg_type );
593+ ARG_UNUSED (iface );
594+
558595 /* 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" );
560597}
561598
562599#ifdef CONFIG_NET_DHCPV4_OPTION_CALLBACKS_VENDOR_SPECIFIC
@@ -566,45 +603,48 @@ static void vendor_specific_string_cb(struct net_dhcpv4_option_callback *cb,
566603 enum net_dhcpv4_msg_type msg_type ,
567604 struct net_if * iface )
568605{
569- char expectation [] = "string" ;
606+ static const char expectation [] = "string" ;
607+
608+ ARG_UNUSED (msg_type );
609+ ARG_UNUSED (iface );
570610
571611 zassert_equal (cb -> option , OPTION_VENDOR_STRING ,
572612 "Unexpected vendor specific option value" );
573613 zassert_equal (length , sizeof (expectation ), "Incorrect data length" );
574614 zassert_mem_equal (buffer , expectation , sizeof (expectation ), "Incorrect buffer contents" );
575615
576- event_count ++ ;
577-
578- k_sem_give (& test_lock );
616+ k_event_post (& events , EVT_VENDOR_STRING );
579617}
580618
581619static void vendor_specific_byte_cb (struct net_dhcpv4_option_callback * cb ,
582620 size_t length ,
583621 enum net_dhcpv4_msg_type msg_type ,
584622 struct net_if * iface )
585623{
624+ ARG_UNUSED (msg_type );
625+ ARG_UNUSED (iface );
626+
586627 zassert_equal (cb -> option , OPTION_VENDOR_BYTE ,
587628 "Unexpected vendor specific option value" );
588629 zassert_equal (length , 1 , "Incorrect data length" );
589630 zassert_equal (buffer [0 ], 1 , "Incorrect buffer contents" );
590631
591- event_count ++ ;
592-
593- k_sem_give (& test_lock );
632+ k_event_post (& events , EVT_VENDOR_BYTE );
594633}
595634
596635static void vendor_specific_empty_cb (struct net_dhcpv4_option_callback * cb ,
597636 size_t length ,
598637 enum net_dhcpv4_msg_type msg_type ,
599638 struct net_if * iface )
600639{
640+ ARG_UNUSED (msg_type );
641+ ARG_UNUSED (iface );
642+
601643 zassert_equal (cb -> option , OPTION_VENDOR_EMPTY ,
602644 "Unexpected vendor specific option value" );
603645 zassert_equal (length , 0 , "Incorrect data length" );
604646
605- event_count ++ ;
606-
607- k_sem_give (& test_lock );
647+ k_event_post (& events , EVT_VENDOR_EMPTY );
608648}
609649
610650#endif /* CONFIG_NET_DHCPV4_OPTION_CALLBACKS_VENDOR_SPECIFIC */
@@ -614,25 +654,7 @@ static void vendor_specific_empty_cb(struct net_dhcpv4_option_callback *cb,
614654ZTEST (dhcpv4_tests , test_dhcp )
615655{
616656 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 ;
636658
637659#ifdef CONFIG_NET_DHCPV4_OPTION_CALLBACKS
638660 net_dhcpv4_init_option_callback (& opt_domain_cb , option_domain_cb ,
@@ -689,19 +711,29 @@ ZTEST(dhcpv4_tests, test_dhcp)
689711
690712 net_dhcpv4_start (iface );
691713
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+
692722#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 );
700727#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" );
705737
706738 /* Verify that Request xid matched Offer xid. */
707739 zassert_equal (offer_xid , request_xid , "Offer/Request xid mismatch, "
0 commit comments