1
1
#pragma once
2
2
3
3
#include " zephyr/net/dhcpv4.h"
4
+ #include < cstddef>
4
5
#include < zephyr/kernel.h>
5
6
#include < zephyr/linker/sections.h>
6
7
#include < errno.h>
@@ -38,18 +39,7 @@ class NetworkInterface {
38
39
static struct net_mgmt_event_callback mgmt_cb;
39
40
static struct net_dhcpv4_option_callback dhcp_cb;
40
41
41
- static void link_handler (struct net_mgmt_event_callback *cb,
42
- uint32_t mgmt_event,
43
- struct net_if *iface)
44
- {
45
- if (mgmt_event == NET_EVENT_IF_UP) {
46
- // printk("Interface %p is up\n", iface);
47
- } else {
48
- // printk("Interface %p is down\n", iface);
49
- }
50
- }
51
-
52
- static void addr_handler (struct net_mgmt_event_callback *cb,
42
+ static void event_handler (struct net_mgmt_event_callback *cb,
53
43
uint32_t mgmt_event,
54
44
struct net_if *iface)
55
45
{
@@ -99,10 +89,7 @@ class NetworkInterface {
99
89
100
90
int dhcp ()
101
91
{
102
- net_mgmt_init_event_callback (&mgmt_cb, addr_handler, NET_EVENT_IPV4_ADDR_ADD);
103
- net_mgmt_add_event_callback (&mgmt_cb);
104
-
105
- net_mgmt_init_event_callback (&mgmt_cb, link_handler, NET_EVENT_IF_UP | NET_EVENT_IF_DOWN);
92
+ net_mgmt_init_event_callback (&mgmt_cb, event_handler, NET_EVENT_IPV4_ADDR_ADD | NET_EVENT_IF_UP | NET_EVENT_IF_DOWN);
106
93
net_mgmt_add_event_callback (&mgmt_cb);
107
94
108
95
net_dhcpv4_init_option_callback (&dhcp_cb, option_handler,
@@ -117,8 +104,7 @@ class NetworkInterface {
117
104
}
118
105
119
106
public:
120
- NetworkInterface (int iface_index) : iface_index(iface_index) {
121
- }
107
+ NetworkInterface (int iface_index) : iface_index(iface_index) {}
122
108
~NetworkInterface () {}
123
109
IPAddress localIP () {
124
110
return IPAddress (net_if_get_by_index (iface_index)->config .ip .ipv4 ->unicast [0 ].ipv4 .address .in_addr .s_addr );
@@ -140,11 +126,7 @@ class NetworkInterface {
140
126
141
127
bool begin () {
142
128
dhcp ();
143
- // TODO: replace me with semaphore on the callback
144
- while (net_if_get_by_index (iface_index)->config .ip .ipv4 ->unicast [0 ].ipv4 .address .in_addr .s_addr == 0 ) {
145
- k_sleep (K_MSEC (100 ));
146
- }
147
- return 0 ;
129
+ net_mgmt_event_wait_on_iface (net_if_get_by_index (iface_index), NET_EVENT_IPV4_ADDR_ADD, NULL , NULL , NULL , K_FOREVER); return 0 ;
148
130
}
149
131
150
132
// Manual functions
0 commit comments