[En-Nut-Discussion] NutLoadConfig/NutNetConfig/NutDhcpIfConfig
Hugo Simon
hugo.simon at gmx.de
Tue Sep 20 15:19:37 CEST 2005
Hi Bernhard
> I'm getting lost between all these functions. Basically I want an
As I at first. :-)
> 3) I flash httpserv.hex. It calls NutDhcpIfConfig() but starts by using
> 192.168.192.100 as ip address.
The flashing process normally erases the EEPROM, I am unsure, but I think
there is a fuse in the ATmega128 to protect the EEPROM.
But I do another thing in my application:
1.try to get network parameters via DHCP using EEPROMs MAC address
2.if this didn't work try again using a hardcoded MAC
3.if this also does not work use a standard hardcoded IP address like
192.168.1.100 or so.
But there is one problem which isn't solved that way: the MAC address is
also stored in EEPROM, and if it is gone you cannot use DHCP. So in step 2.
I use a hardcoded MAC, but beware using more than one Ethernut in the
network with that address.
It seems like the NutDhcpIfConfig function uses the EEPROM parameters if no
DHCP answer is received.
I give you a code snipped to see what I did:
---------------------------------
/* EThernut initialization */
puts_P(PSTR("Initialize TCP/IP..."));
printf_P(PSTR("Calling DHCP server..."));
/* first try, use EEPROMs MAC */
if (NutDhcpIfConfig("eth0", 0, 20000))
{
/* shit, it didn't work, so use hardcoded MAC (bad!) */
u_char my_mac[] = { 0x00,0x06,0x98,0x00,0x00,0x01 };
if (NutDhcpIfConfig("eth0", my_mac, 20000))
{
/* also didn't work, use default parameters */
printf_P(PSTR(" failed, using default settings"));
NutNetIfConfig2("eth0", my_mac, inet_addr("192.168.100.49"),
inet_addr("255.255.255.0"),
inet_addr("192.168.100.1"));
NutDnsConfig2("WOLPROXY", "subspace.de", inet_addr("192.168.100.1"),
0);
}
}
----------------------------------
via Telnet you get the following commandline interface to setup things
correctly:
/*
* set configuration value
*/
int set_config(char *varname, char *value, FILE *stream)
{
char *s;
if (!varname || !stricmp(varname,"help") || !stricmp(varname,"?"))
{
fputs_P(PSTR("SET ADDR <IPADDRES> set TCP/IP address (0.0.0.0 for
DHCP)\r\n"
"SET MASK <SUBNETMASK> set subnet mask\r\n"
"SET GATEWAY <IPADRESS> set default gateway\r\n"
"SET DNS1 <IPADRESS> set primary DNS server\r\n"
"SET DNS2 <IPADRESS> set secondary DNS server\r\n"
"SET HOST <HOST>[.<DOMAIN>] set hostname and domain\r\n"
"SET MAC <MACADDRES> set hardware address (use with
care)\r\n"),stream);
return 0;
}
if (!value) return 3;
if (!stricmp(varname,"addr"))
{
confnet.cdn_cip_addr=inet_addr(value);
return 0;
}
if (!stricmp(varname,"mask"))
{
confnet.cdn_ip_mask=inet_addr(value);
return 0;
}
if (!stricmp(varname,"gateway"))
{
confnet.cdn_gateway=inet_addr(value);
return 0;
}
if (!stricmp(varname,"dns1"))
{
doc.doc_ip1=inet_addr(value);
return 0;
}
if (!stricmp(varname,"dns2"))
{
doc.doc_ip2=inet_addr(value);
return 0;
}
if (!stricmp(varname,"host"))
{
if (doc.doc_hostname) free(doc.doc_hostname);
s=strchr(value,'.');
if (s)
{
*s++=0;
if (doc.doc_domain) free(doc.doc_domain);
doc.doc_domain=malloc(strlen(s)+1);
if (!doc.doc_domain)
{
fputs_P(PSTR("Internal Error setting hostname"),stream);
return 2;
}
strcpy(doc.doc_domain,s);
}
doc.doc_hostname=malloc(strlen(value)+1);
if (!doc.doc_hostname)
{
fputs_P(PSTR("Internal Error setting hostname"),stream);
return 2;
}
strcpy(doc.doc_hostname,value);
return 0;
}
return 4;
}
/*
* save configuration and reboot
*/
int save_config(FILE *stream)
{
char c;
fputs("About to save these settings:",stream);
showConfigInfo(stream);
fputs("Are you sure (y/n)",stream);
fflush(stream);
do
{
c=fgetc(stream);
} while (c!='y' && c!='Y' && c!='n' && c!='N');
if (c=='y' || c=='Y')
{
NutNetSaveConfig();
fputs("...saved, rebooting",stream);
fclose(stream);
NutSleep(1000);
cli();
wdt_reset();
wdt_enable(1);
while(1);
}
return 0;
}
More information about the En-Nut-Discussion
mailing list