Network Configuration Using Stored Configuration
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">
- include <dev/board.h>
- include <sys/timer.h>
- include <sys/confnet.h>
- include <stdio.h>
- include <io.h>
- include <arpa/inet.h>
- 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
- Network Configuration Overview
- Basic TCP Server
- Basic TCP Client
- More Nut/OS Examples
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