2010-08-02 20:59:44 +02:00
|
|
|
#include "w5100.h"
|
2010-08-03 15:36:47 +02:00
|
|
|
#include "Ethernet.h"
|
2011-01-16 21:11:50 +01:00
|
|
|
#include "Dhcp.h"
|
2008-07-30 16:47:36 +02:00
|
|
|
|
|
|
|
// XXX: don't make assumptions about the value of MAX_SOCK_NUM.
|
2010-08-02 20:59:44 +02:00
|
|
|
uint8_t EthernetClass::_state[MAX_SOCK_NUM] = {
|
|
|
|
0, 0, 0, 0 };
|
|
|
|
uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = {
|
|
|
|
0, 0, 0, 0 };
|
2008-07-30 16:47:36 +02:00
|
|
|
|
2011-01-16 21:11:50 +01:00
|
|
|
int EthernetClass::begin(uint8_t *mac_address)
|
2008-07-30 16:47:36 +02:00
|
|
|
{
|
2011-01-16 21:11:50 +01:00
|
|
|
DhcpClass dhcp;
|
|
|
|
|
|
|
|
// Initialise the basic info
|
|
|
|
W5100.init();
|
|
|
|
W5100.setMACAddress(mac_address);
|
|
|
|
W5100.setIPAddress(IPAddress(0,0,0,0).raw_address());
|
|
|
|
|
|
|
|
// Now try to get our config info from a DHCP server
|
|
|
|
int ret = dhcp.beginWithDHCP(mac_address);
|
|
|
|
if(ret == 1)
|
|
|
|
{
|
|
|
|
// We've successfully found a DHCP server and got our configuration info, so set things
|
|
|
|
// accordingly
|
|
|
|
W5100.setIPAddress(dhcp.getLocalIp().raw_address());
|
|
|
|
W5100.setGatewayIp(dhcp.getGatewayIp().raw_address());
|
|
|
|
W5100.setSubnetMask(dhcp.getSubnetMask().raw_address());
|
|
|
|
_dnsServerAddress = dhcp.getDnsServerIp();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
|
2011-06-12 23:02:25 +02:00
|
|
|
{
|
|
|
|
// Assume the DNS server will be the machine on the same network as the local IP
|
|
|
|
// but with last octet being '1'
|
|
|
|
IPAddress dns_server = local_ip;
|
|
|
|
dns_server[3] = 1;
|
|
|
|
begin(mac_address, local_ip, dns_server);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
|
2011-01-16 21:11:50 +01:00
|
|
|
{
|
|
|
|
// Assume the gateway will be the machine on the same network as the local IP
|
|
|
|
// but with last octet being '1'
|
|
|
|
IPAddress gateway = local_ip;
|
2008-07-30 16:47:36 +02:00
|
|
|
gateway[3] = 1;
|
2011-06-12 23:02:25 +02:00
|
|
|
begin(mac_address, local_ip, dns_server, gateway);
|
2008-07-30 16:47:36 +02:00
|
|
|
}
|
|
|
|
|
2011-06-12 23:02:25 +02:00
|
|
|
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
|
2008-07-30 16:47:36 +02:00
|
|
|
{
|
2011-01-16 21:11:50 +01:00
|
|
|
IPAddress subnet(255, 255, 255, 0);
|
2011-06-12 23:02:25 +02:00
|
|
|
begin(mac_address, local_ip, dns_server, gateway, subnet);
|
2008-07-30 16:47:36 +02:00
|
|
|
}
|
|
|
|
|
2011-06-12 23:02:25 +02:00
|
|
|
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
|
2008-07-30 16:47:36 +02:00
|
|
|
{
|
2010-08-02 20:59:44 +02:00
|
|
|
W5100.init();
|
|
|
|
W5100.setMACAddress(mac);
|
2011-01-16 21:11:50 +01:00
|
|
|
W5100.setIPAddress(local_ip._address);
|
|
|
|
W5100.setGatewayIp(gateway._address);
|
|
|
|
W5100.setSubnetMask(subnet._address);
|
2011-06-12 23:02:25 +02:00
|
|
|
_dnsServerAddress = dns_server;
|
2011-01-16 21:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress EthernetClass::localIP()
|
|
|
|
{
|
|
|
|
IPAddress ret;
|
|
|
|
W5100.getIPAddress(ret.raw_address());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress EthernetClass::subnetMask()
|
|
|
|
{
|
|
|
|
IPAddress ret;
|
|
|
|
W5100.getSubnetMask(ret.raw_address());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress EthernetClass::gatewayIP()
|
|
|
|
{
|
|
|
|
IPAddress ret;
|
|
|
|
W5100.getGatewayIp(ret.raw_address());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress EthernetClass::dnsServerIP()
|
|
|
|
{
|
|
|
|
return _dnsServerAddress;
|
2008-07-30 16:47:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EthernetClass Ethernet;
|