1 /*
2 * Copyright (c) 2020 DENX Software Engineering GmbH
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(net_dsa_sample, CONFIG_NET_DSA_LOG_LEVEL);
9
10 #include "dsa.h"
11
12 #if defined(CONFIG_NET_SAMPLE_DSA_LLDP)
13 #include "dsa_lldp.h"
14 #endif
15
16 struct ud g_user_data = {0};
17
dsa_iface_find_cb(struct net_if * iface,void * user_data)18 static void dsa_iface_find_cb(struct net_if *iface, void *user_data)
19 {
20
21 struct ud *ifaces = user_data;
22
23 if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) {
24 return;
25 }
26
27 if ((net_eth_get_hw_capabilities(iface) & ETHERNET_DSA_CONDUIT_PORT) &&
28 (ifaces->conduit == NULL)) {
29 ifaces->conduit = iface;
30
31 /* Get user interfaces */
32 for (int i = 0; i < ARRAY_SIZE(ifaces->lan); i++) {
33 #if defined(CONFIG_NET_DSA_DEPRECATED)
34 struct net_if *user = dsa_get_slave_port(iface, i);
35 #else
36 struct net_if *user = dsa_user_get_iface(iface, i);
37 #endif
38 if (user == NULL) {
39 continue;
40 }
41 LOG_INF("[%d] User interface %d found.", i, net_if_get_by_iface(user));
42
43 ifaces->lan[i] = user;
44 }
45 return;
46 }
47 }
48
49 #if defined(CONFIG_NET_MGMT_EVENT)
50 #define EVENT_MASK (NET_EVENT_IF_UP)
51 static struct net_mgmt_event_callback mgmt_cb;
52
event_handler(struct net_mgmt_event_callback * cb,uint64_t mgmt_event,struct net_if * iface)53 static void event_handler(struct net_mgmt_event_callback *cb,
54 uint64_t mgmt_event, struct net_if *iface)
55 {
56 ARG_UNUSED(iface);
57 ARG_UNUSED(cb);
58
59 if (mgmt_event == NET_EVENT_IF_UP) {
60 LOG_INF("Port %d is up", net_if_get_by_iface(iface));
61
62 #if defined(CONFIG_NET_DHCPV4)
63 net_dhcpv4_start(iface);
64 #endif
65
66 return;
67 }
68 }
69 #endif /* CONFIG_NET_MGMT_EVENT */
70
main(void)71 int main(void)
72 {
73 /* Initialize interfaces - read them to user_data */
74 net_if_foreach(dsa_iface_find_cb, &g_user_data);
75
76 #if defined(CONFIG_NET_MGMT_EVENT)
77 net_mgmt_init_event_callback(&mgmt_cb,
78 event_handler, EVENT_MASK);
79 net_mgmt_add_event_callback(&mgmt_cb);
80 #endif /* CONFIG_NET_MGMT_EVENT */
81
82 #if defined(CONFIG_NET_SAMPLE_DSA_LLDP)
83 dsa_lldp(&g_user_data);
84 #endif
85 LOG_INF("DSA ports init - OK");
86 return 0;
87 }
88