Your third TCP/IP Library application

What would be great is if your evaluation board had a name, rather than an address, so we can simply ping the name of the board. Well, there is a way, and that is to register a name using DHCP.

Select and build the project

In the examples for your board, you'll find a Networking Projects solution, and within that a Ping by Name project. Double-click that project to make it active and press F7 to build.

Double-click the file example_ping_by_name.c in the Source Files folder and it will open in the code editor. In main you'll find a call to ctl_net_set_host_name, before the network is brought up, to set the host name of the evaluation board:

// Set our host name.
ctl_net_set_host_name("crossworks");

This registers the name of the host with the DHCP server and means that you can ping the board using a friendly name, whatever the assigned IP address is.

See if it works

Power up the board and run the code. In the CrossWorks Debug Terminal you should see the same as before:

DHCP: awaiting IP address assignment
DHCP: awaiting IP address assignment
DHCP: awaiting IP address assignment
DHCP: IP address is 10.0.0.44 and subnet mask is 255.255.255.0
DHCP: IP address is 10.0.0.44 and subnet mask is 255.255.255.0
DHCP: IP address is 10.0.0.44 and subnet mask is 255.255.255.0

Now you can ping the device by its assigned name, crossworks:

> ping crossworks

Pinging crossworks.rowley.co.uk [10.0.0.44] with 32 bytes of data:
Reply from 10.0.0.44: bytes=32 time<1ms TTL=64
Reply from 10.0.0.44: bytes=32 time<1ms TTL=64
Reply from 10.0.0.44: bytes=32 time<1ms TTL=64
Reply from 10.0.0.44: bytes=32 time<1ms TTL=64

Ping statistics for 10.0.0.44:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

>_ 

Notice that the full name of the host is crossworks.rowley.co.uk. This is because the LAN that the board is connected to has a domain name suffix. You might have noticed this in the output from ipconfig in the first example:

> ipconfig
Windows IP Configuration

Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . : rowley.co.uk
   Link-local IPv6 Address . . . . . : fe80::9c2d:e057:8641:2281%10
   IPv4 Address. . . . . . . . . . . : 10.0.0.58
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 10.0.0.3

> _

The full host name is the name that we assigned to the node, crossworks, with the suffix assigned by the network, rowley.co.uk.

Job done!

You now have a functioning Ethernet connection between your PC and your target board, using a dynamically-assigned IP address, and with a friendly name to contact the board.

The code
// Set up network using a DHCP-assigned IP address.

#include "libnet/ctl_net_private.h"
#include "libplatform/platform.h"
#include "libplatform/platform_network.h"
#include "example_support.h"

// Assign a fixed MAC address to the NIC.  Normally this will be blown into
// OTP or some other nonvolatile medium when the device is personalized as
// part of production.
#define FIXED_MAC_ADDRESS        "bc-28-d6-ff-ff-ff"

// Network task thread priority
#define NET_TASK_PRIORITY        200

// Network interface,
static CTL_NET_INTERFACE_t nic;

static void
bring_up_network(void)
{
  // Assign a fixed MAC address to the NIC.
  example_check_status(ctl_net_scan_mac_addr(&nic.mac.mac_addr, FIXED_MAC_ADDRESS));

  // Initialize MAC.
  example_check_status(ctl_mac_init(&nic));

  // Bring up network task and use DHCP to assign an IP address.
  example_check_status(ctl_net_init(NET_TASK_PRIORITY, 0));

  // Bring up UDP and ICMP: DHCP requires UDP, and ICMP will respond to pings.
  example_check_status(ctl_udp_init(0));
  example_check_status(ctl_icmp_init());

  // Start DHCP to assign us an IP address.
  example_check_status(ctl_dhcp_init());
}

int
main(void)
{
  char dot_ipaddr[16], dot_netmask[16];

  // Initialize platform.
  platform_initialize();

  // Initialize NIC for this platform.
  example_check_status(platform_configure_network(&nic));

  // Set our host name.
  ctl_net_set_host_name("crossworks");

  // Start network.
  bring_up_network();

  // Idle away; when we're configured, dump our network.
  for (;;)
    {
      // See if we've acquired an IP address yet...
      if (ctl_net_get_ip_address())
        {
          // Dump message inviting a ping.
          printf("DHCP: IP address is %s and subnet mask is %s\n",
                 ctl_ip_sprint_addr(dot_ipaddr, ctl_net_get_ip_address()),
                 ctl_ip_sprint_addr(dot_netmask, ctl_net_get_subnet_mask()));
        }
      else
        {
          // Can't ping me yet.
          printf("DHCP: awaiting IP address assignment\n");
        }

      // Don't be too enthusiastic with messages.
      ctl_delay(1000);
    }
}
See Also

ctl_net_set_host_name