How to configure and use UDP or TCP interfaces in ncs2.5.2 after obtaining IP based on DHCPv4 client + Ethernet (W5500)

Hello everyone,

Here is the code I used to get my IP using DHCP,How do I use this IP to send and receive UDP or TCP? Using custom mac is this how I use it inside dts below?

#define DHCP_OPTION_NTP (42)

static uint8_t ntp_server[4];

static struct net_mgmt_event_callback mgmt_cb;

static struct net_dhcpv4_option_callback dhcp_cb;

static void handler(struct net_mgmt_event_callback *cb, uint32_t mgmt_event,
                    struct net_if *iface)
{
    int i = 0;

    if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
        return;
    }

    for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
        char buf[NET_IPV4_ADDR_LEN];

        if (iface->config.ip.ipv4->unicast[i].addr_type != NET_ADDR_DHCP) {
            continue;
        }

        LOG_INF("   Address[%d]: %s", net_if_get_by_iface(iface),
                net_addr_ntop(
                    AF_INET, &iface->config.ip.ipv4->unicast[i].address.in_addr,
                    buf, sizeof(buf)));
        client_addr = &iface->config.ip.ipv4->unicast[i].address.in_addr;
        LOG_INF("    Subnet[%d]: %s", net_if_get_by_iface(iface),
                net_addr_ntop(AF_INET, &iface->config.ip.ipv4->netmask, buf,
                              sizeof(buf)));
        LOG_INF("    Router[%d]: %s", net_if_get_by_iface(iface),
                net_addr_ntop(AF_INET, &iface->config.ip.ipv4->gw, buf,
                              sizeof(buf)));
        LOG_INF("Lease time[%d]: %u seconds", net_if_get_by_iface(iface),
                iface->config.dhcpv4.lease_time);
        k_sem_give(&m_udp);
    }
}

static void option_handler(struct net_dhcpv4_option_callback *cb, size_t length,
                           enum net_dhcpv4_msg_type msg_type,
                           struct net_if *iface)
{
    char buf[NET_IPV4_ADDR_LEN];

    LOG_INF("DHCP Option %d: %s", cb->option,
            net_addr_ntop(AF_INET, cb->data, buf, sizeof(buf)));
}

int main(void)
{

    iface = net_if_get_default();

    LOG_INF("Run dhcpv4 client");

    net_mgmt_init_event_callback(&mgmt_cb, handler, NET_EVENT_IPV4_ADDR_ADD);
    net_mgmt_add_event_callback(&mgmt_cb);

    net_dhcpv4_init_option_callback(&dhcp_cb, option_handler, DHCP_OPTION_NTP,
                                    ntp_server, sizeof(ntp_server));

    net_dhcpv4_add_option_callback(&dhcp_cb);

    LOG_INF("Start on %s: index=%d", net_if_get_device(iface)->name,
            net_if_get_by_iface(iface));
    net_dhcpv4_start(iface);

    return 0;
}

Prj.conf file

CONFIG_NET_L2_ETHERNET=y
CONFIG_NET_IPV6=n
CONFIG_NETWORKING=y
CONFIG_NET_IPV4=y
CONFIG_NET_DHCPV4=y
CONFIG_NET_DHCPV4_OPTION_CALLBACKS=y
CONFIG_NET_SHELL=y
CONFIG_NET_LOG=y

Custom board 

&spi1 {
	compatible = "nordic,nrf-spi";
	status = "okay";
	pinctrl-0 = < &spi1_default >;
	pinctrl-1 = < &spi1_sleep >;
	cs-gpios = <&gpio0 12 GPIO_ACTIVE_LOW>;
	pinctrl-names = "default", "sleep";

	spi_w5500: w5500@0 {
		compatible = "wiznet,w5500";
        local-mac-address = [02 08 dc 11 22 33];
		reg = <0x0>;
		spi-max-frequency = <80000000>;
		int-gpios = <&gpio0 4 GPIO_ACTIVE_LOW>;
		reset-gpios = <&gpio0 26 GPIO_ACTIVE_LOW>;
	};
};

Related