Skip to content

Commit 70ffad8

Browse files
jukkaryangbolu1991
authored andcommitted
samples: net: dsa: Enable DHCPv4 and start it when interface is up
Enable DHCPv4 when network interface goes up. This makes it easier to test IP connectivity with DSA. Signed-off-by: Jukka Rissanen <[email protected]> Signed-off-by: Yangbo Lu <[email protected]>
1 parent ec5a8a9 commit 70ffad8

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

samples/net/dsa/prj.conf

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CONFIG_NETWORKING=y
22
CONFIG_NET_LOG=y
33
CONFIG_NET_IPV6=y
44
CONFIG_NET_IPV4=y
5-
CONFIG_NET_DHCPV4=n
5+
CONFIG_NET_DHCPV4=y
66
CONFIG_NET_UDP=y
77
CONFIG_NET_TCP=y
88
CONFIG_NET_STATISTICS=y
@@ -18,6 +18,9 @@ CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=5
1818
CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=5
1919
CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT=1
2020
CONFIG_NET_MAX_CONTEXTS=4
21+
CONFIG_ZVFS_OPEN_MAX=4
22+
CONFIG_ZVFS_POLL_MAX=4
23+
CONFIG_NET_MAX_CONN=4
2124

2225
CONFIG_INIT_STACKS=y
2326
CONFIG_PRINTK=y
@@ -46,3 +49,10 @@ CONFIG_REQUIRES_FULL_LIBC=y
4649

4750
CONFIG_NET_IF_MAX_IPV4_COUNT=4
4851
CONFIG_NET_IF_MAX_IPV6_COUNT=4
52+
53+
CONFIG_NET_MGMT=y
54+
CONFIG_NET_MGMT_EVENT=y
55+
CONFIG_NET_MGMT_EVENT_STACK_SIZE=2048
56+
57+
CONFIG_NET_HOSTNAME_ENABLE=y
58+
CONFIG_NET_HOSTNAME="dsa"

samples/net/dsa/src/main.c

+30-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ static void dsa_iface_find_cb(struct net_if *iface, void *user_data)
3939
if (user == NULL) {
4040
continue;
4141
}
42-
LOG_INF("User interface %d found.", i);
42+
LOG_INF("[%d] User interface %d found.", i,
43+
net_if_get_by_iface(user));
4344

4445
ifaces->lan[i] = user;
4546
}
@@ -48,12 +49,40 @@ static void dsa_iface_find_cb(struct net_if *iface, void *user_data)
4849
}
4950
}
5051

52+
#if defined(CONFIG_NET_MGMT_EVENT)
53+
#define EVENT_MASK (NET_EVENT_IF_UP)
54+
static struct net_mgmt_event_callback mgmt_cb;
55+
56+
static void event_handler(struct net_mgmt_event_callback *cb,
57+
uint32_t mgmt_event, struct net_if *iface)
58+
{
59+
ARG_UNUSED(iface);
60+
ARG_UNUSED(cb);
61+
62+
if (mgmt_event == NET_EVENT_IF_UP) {
63+
LOG_INF("Port %d is up", net_if_get_by_iface(iface));
64+
65+
#if defined(CONFIG_NET_DHCPV4)
66+
net_dhcpv4_start(iface);
67+
#endif
68+
69+
return;
70+
}
71+
}
72+
#endif /* CONFIG_NET_MGMT_EVENT */
73+
5174
int main(void)
5275
{
5376
/* Initialize interfaces - read them to user_data */
5477
(void)memset(&user_data, 0, sizeof(user_data));
5578
net_if_foreach(dsa_iface_find_cb, &user_data);
5679

80+
#if defined(CONFIG_NET_MGMT_EVENT)
81+
net_mgmt_init_event_callback(&mgmt_cb,
82+
event_handler, EVENT_MASK);
83+
net_mgmt_add_event_callback(&mgmt_cb);
84+
#endif /* CONFIG_NET_MGMT_EVENT */
85+
5786
#if defined(CONFIG_NET_SAMPLE_DSA_LLDP)
5887
dsa_lldp(&user_data);
5988
#endif

0 commit comments

Comments
 (0)