Skip to content

Commit 99a005e

Browse files
committed
[nrf fromtree] samples: net: telnet: Remove redundant code
The sample uses net_config library, so there's no need for the sample to configure unicast IP addresses manually. Signed-off-by: Robert Lubos <[email protected]> (cherry picked from commit 7d91281)
1 parent 0ea156f commit 99a005e

File tree

1 file changed

+3
-109
lines changed

1 file changed

+3
-109
lines changed

samples/net/telnet/src/telnet.c

Lines changed: 3 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -18,111 +18,13 @@ LOG_MODULE_REGISTER(net_telnet_sample, LOG_LEVEL_DBG);
1818

1919
#include "net_sample_common.h"
2020

21-
#if defined(CONFIG_NET_DHCPV4)
22-
static struct net_mgmt_event_callback mgmt_cb;
23-
24-
static void ipv4_addr_add_handler(struct net_mgmt_event_callback *cb,
25-
uint32_t mgmt_event,
26-
struct net_if *iface)
27-
{
28-
char hr_addr[NET_IPV4_ADDR_LEN];
29-
int i = 0;
30-
31-
if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
32-
/* Spurious callback. */
33-
return;
34-
}
35-
36-
for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
37-
struct net_if_addr *if_addr =
38-
&iface->config.ip.ipv4->unicast[i];
39-
40-
if (if_addr->addr_type != NET_ADDR_DHCP || !if_addr->is_used) {
41-
continue;
42-
}
43-
44-
LOG_INF("IPv4 address: %s",
45-
net_addr_ntop(AF_INET,
46-
&if_addr->address.in_addr,
47-
hr_addr, NET_IPV4_ADDR_LEN));
48-
LOG_INF("Lease time: %u seconds",
49-
iface->config.dhcpv4.lease_time);
50-
LOG_INF("Subnet: %s",
51-
net_addr_ntop(AF_INET,
52-
&iface->config.ip.ipv4->netmask,
53-
hr_addr, NET_IPV4_ADDR_LEN));
54-
LOG_INF("Router: %s",
55-
net_addr_ntop(AF_INET,
56-
&iface->config.ip.ipv4->gw,
57-
hr_addr, NET_IPV4_ADDR_LEN));
58-
break;
59-
}
60-
}
61-
62-
static void setup_dhcpv4(struct net_if *iface)
63-
{
64-
LOG_INF("Running dhcpv4 client...");
65-
66-
net_mgmt_init_event_callback(&mgmt_cb, ipv4_addr_add_handler,
67-
NET_EVENT_IPV4_ADDR_ADD);
68-
net_mgmt_add_event_callback(&mgmt_cb);
69-
70-
net_dhcpv4_start(iface);
71-
}
72-
73-
#else
74-
#define setup_dhcpv4(...)
75-
#endif /* CONFIG_NET_DHCPV4 */
76-
77-
#if defined(CONFIG_NET_IPV4) && !defined(CONFIG_NET_DHCPV4)
78-
79-
#if !defined(CONFIG_NET_CONFIG_MY_IPV4_ADDR)
80-
#error "You need to define an IPv4 Address or enable DHCPv4!"
81-
#endif
82-
83-
static void setup_ipv4(struct net_if *iface)
84-
{
85-
char hr_addr[NET_IPV4_ADDR_LEN];
86-
struct in_addr addr;
87-
88-
if (net_addr_pton(AF_INET, CONFIG_NET_CONFIG_MY_IPV4_ADDR, &addr)) {
89-
LOG_ERR("Invalid address: %s", CONFIG_NET_CONFIG_MY_IPV4_ADDR);
90-
return;
91-
}
92-
93-
net_if_ipv4_addr_add(iface, &addr, NET_ADDR_MANUAL, 0);
94-
95-
LOG_INF("IPv4 address: %s",
96-
net_addr_ntop(AF_INET, &addr, hr_addr,
97-
NET_IPV4_ADDR_LEN));
98-
}
99-
100-
#else
101-
#define setup_ipv4(...)
102-
#endif /* CONFIG_NET_IPV4 && !CONFIG_NET_DHCPV4 */
103-
10421
#if defined(CONFIG_NET_IPV6)
105-
10622
#define MCAST_IP6ADDR "ff84::2"
10723

108-
#ifndef CONFIG_NET_CONFIG_MY_IPV6_ADDR
109-
#error "You need to define an IPv6 Address!"
110-
#endif
111-
112-
static void setup_ipv6(struct net_if *iface)
24+
static void setup_ipv6(void)
11325
{
114-
char hr_addr[NET_IPV6_ADDR_LEN];
11526
struct in6_addr addr;
116-
117-
if (net_addr_pton(AF_INET6, CONFIG_NET_CONFIG_MY_IPV6_ADDR, &addr)) {
118-
LOG_ERR("Invalid address: %s", CONFIG_NET_CONFIG_MY_IPV6_ADDR);
119-
return;
120-
}
121-
122-
net_if_ipv6_addr_add(iface, &addr, NET_ADDR_MANUAL, 0);
123-
124-
LOG_INF("IPv6 address: %s",
125-
net_addr_ntop(AF_INET6, &addr, hr_addr, NET_IPV6_ADDR_LEN));
27+
struct net_if *iface = net_if_get_default();
12628

12729
if (net_addr_pton(AF_INET6, MCAST_IP6ADDR, &addr)) {
12830
LOG_ERR("Invalid address: %s", MCAST_IP6ADDR);
@@ -131,23 +33,15 @@ static void setup_ipv6(struct net_if *iface)
13133

13234
net_if_ipv6_maddr_add(iface, &addr);
13335
}
134-
13536
#else
13637
#define setup_ipv6(...)
13738
#endif /* CONFIG_NET_IPV6 */
13839

13940
int main(void)
14041
{
141-
struct net_if *iface = net_if_get_default();
142-
14342
LOG_INF("Starting Telnet sample");
14443

14544
wait_for_network();
146-
147-
setup_ipv4(iface);
148-
149-
setup_dhcpv4(iface);
150-
151-
setup_ipv6(iface);
45+
setup_ipv6();
15246
return 0;
15347
}

0 commit comments

Comments
 (0)