1 /* Networking DHCPv4 client */
2 
3 /*
4  * Copyright (c) 2017 ARM Ltd.
5  * Copyright (c) 2016 Intel Corporation.
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <zephyr/logging/log.h>
11 LOG_MODULE_REGISTER(net_ipv4_autoconf_sample, LOG_LEVEL_DBG);
12 
13 #include <zephyr/kernel.h>
14 #include <zephyr/linker/sections.h>
15 #include <errno.h>
16 #include <stdio.h>
17 
18 #include <zephyr/net/net_if.h>
19 #include <zephyr/net/net_core.h>
20 #include <zephyr/net/net_context.h>
21 #include <zephyr/net/net_mgmt.h>
22 
23 static struct net_mgmt_event_callback mgmt_cb;
24 
handler(struct net_mgmt_event_callback * cb,uint32_t mgmt_event,struct net_if * iface)25 static void handler(struct net_mgmt_event_callback *cb,
26 		    uint32_t mgmt_event,
27 		    struct net_if *iface)
28 {
29 	int i = 0;
30 	struct net_if_config *cfg;
31 
32 	cfg = net_if_get_config(iface);
33 	if (!cfg) {
34 		return;
35 	}
36 
37 	if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
38 		return;
39 	}
40 
41 	for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
42 		char buf[NET_IPV4_ADDR_LEN];
43 
44 		if (cfg->ip.ipv4->unicast[i].addr_type != NET_ADDR_AUTOCONF) {
45 			continue;
46 		}
47 
48 		LOG_INF("Your address: %s",
49 			net_addr_ntop(AF_INET,
50 				    &cfg->ip.ipv4->unicast[i].address.in_addr,
51 				    buf, sizeof(buf)));
52 	}
53 }
54 
main(void)55 int main(void)
56 {
57 	LOG_INF("Run ipv4 autoconf client");
58 
59 	net_mgmt_init_event_callback(&mgmt_cb, handler,
60 				     NET_EVENT_IPV4_ADDR_ADD);
61 	net_mgmt_add_event_callback(&mgmt_cb);
62 	return 0;
63 }
64