Network Configuration Using Stored Configuration

From Nutwiki
Revision as of 11:13, 14 April 2010 by Harald (Talk | contribs) (See also)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Test Environments

Hardware
Comments
Nut/OS
4.6.4
Nut/OS
4.7.4
Nut/OS
4.8.0
Ethernut 1.3 H OK
Binaries
OK
Binaries
OK
Binaries
Ethernut 2.1 B OK
Binaries
OK
Binaries
OK
Binaries
Ethernut 3.0 E OK
Binaries
OK
Binaries
OK
Binaries
EIR 1.0 C Configuration
Error.
Configuration
Error.
OK
Binaries
Compiler: ARM-GCC 4.2.2 ; AVR-GCC 4.3.0

Description

In most cases the network configuration is stored in non-volatile memory and optionally uses DHCP.

Source Code

<source lang="c">

  1. include <dev/board.h>
  2. include <sys/timer.h>
  3. include <sys/confnet.h>
  1. include <stdio.h>
  2. include <io.h>
  3. include <arpa/inet.h>
  4. include <pro/dhcp.h>

int main(void) {

   u_long baud = 115200;
   /* Assign stdout to the DEBUG device. */
   NutRegisterDevice(&DEV_DEBUG, 0, 0);
   freopen(DEV_DEBUG_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   puts("Network Configuration Sample 2");
   /* Register Ethernet controller. */
   if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
       puts("Registering " DEV_ETHER_NAME " failed.");
   }
   /* Configure network. */
   else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
       puts("Configuring " DEV_ETHER_NAME " failed.");
   }
   /* Done. */
   else {
       printf("Now try 'ping %s' on your PC.\n", inet_ntoa(confnet.cdn_ip_addr));
   }
   for (;;) {
       NutSleep(1000);
   }
   return 0;

} </source>

Details

<source lang="c"> if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {

   puts("Configuring " DEV_ETHER_NAME " failed.");

} </source>

Instead of using NutNetIfConfig we call NutDhcpIfConfig here. This will check the configuration stored in non-volatile memory first. If the IP address is 0.0.0.0, then a DHCP request will be sent out.

If no valid configuration has been previously stored, then this call will fail because of the missing MAC address. As an alternative the MAC address may be provided by the application.

<source lang="c"> uint8_t mac[6] = { 0x00, 0x06, 0x98, 0x30, 0x02, 0x76 };

if (NutDhcpIfConfig(DEV_ETHER_NAME, mac, 0)) {

   puts("Configuring " DEV_ETHER_NAME " failed.");

} </source>

However, the hard coded MAC address may provide problems when running more than one Nut/OS device. A much better solution would be to use a different piece of software, which allows to store a network configuration in non-volatile memory on a virgin device first (see next sample).

See also

External Links

MAC address A Media Access Control address (MAC address) is a unique identifier assigned to network adapters for identification.

IP address A numerical identification that is assigned to devices participating in a computer network utilizing the Internet Protocol.

Dynamic Host Configuration Protocol A protocol used by networked devices to obtain the parameters necessary for operation in an Internet Protocol network.Template:Languages