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 user_data;
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_MASTER_PORT) {
28 		if (ifaces->master == NULL) {
29 			ifaces->master = iface;
30 
31 			/* Get slave interfaces */
32 			for (int i = 0; i < ARRAY_SIZE(ifaces->lan); i++) {
33 				struct net_if *slave = dsa_get_slave_port(iface, i);
34 
35 				if (slave == NULL) {
36 					continue;
37 				}
38 				LOG_INF("Slave interface %d found.", i);
39 
40 				ifaces->lan[i] = slave;
41 			}
42 			return;
43 		}
44 	}
45 }
46 
main(void)47 int main(void)
48 {
49 	/* Initialize interfaces - read them to user_data */
50 	(void)memset(&user_data, 0, sizeof(user_data));
51 	net_if_foreach(dsa_iface_find_cb, &user_data);
52 
53 #if defined(CONFIG_NET_SAMPLE_DSA_LLDP)
54 	dsa_lldp(&user_data);
55 #endif
56 	LOG_INF("DSA ports init - OK");
57 	return 0;
58 }
59