source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/libraries/Ethernet/src/Ethernet.cpp @ 46

Last change on this file since 46 was 46, checked in by jrpelegrina, 4 years ago

First release to Xenial

File size: 3.8 KB
Line 
1#include "utility/w5100.h"
2#include "Ethernet.h"
3#include "Dhcp.h"
4
5// XXX: don't make assumptions about the value of MAX_SOCK_NUM.
6uint8_t EthernetClass::_state[MAX_SOCK_NUM] = { 
7  0, 0, 0, 0 };
8uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = { 
9  0, 0, 0, 0 };
10
11int EthernetClass::begin(uint8_t *mac_address, unsigned long timeout, unsigned long responseTimeout)
12{
13  static DhcpClass s_dhcp;
14  _dhcp = &s_dhcp;
15
16
17  // Initialise the basic info
18  W5100.init();
19  SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
20  W5100.setMACAddress(mac_address);
21  W5100.setIPAddress(IPAddress(0,0,0,0).raw_address());
22  SPI.endTransaction();
23
24  // Now try to get our config info from a DHCP server
25  int ret = _dhcp->beginWithDHCP(mac_address, timeout, responseTimeout);
26  if(ret == 1)
27  {
28    // We've successfully found a DHCP server and got our configuration info, so set things
29    // accordingly
30    SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
31    W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
32    W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
33    W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
34    SPI.endTransaction();
35    _dnsServerAddress = _dhcp->getDnsServerIp();
36  }
37
38  return ret;
39}
40
41void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
42{
43  // Assume the DNS server will be the machine on the same network as the local IP
44  // but with last octet being '1'
45  IPAddress dns_server = local_ip;
46  dns_server[3] = 1;
47  begin(mac_address, local_ip, dns_server);
48}
49
50void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
51{
52  // Assume the gateway will be the machine on the same network as the local IP
53  // but with last octet being '1'
54  IPAddress gateway = local_ip;
55  gateway[3] = 1;
56  begin(mac_address, local_ip, dns_server, gateway);
57}
58
59void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
60{
61  IPAddress subnet(255, 255, 255, 0);
62  begin(mac_address, local_ip, dns_server, gateway, subnet);
63}
64
65void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
66{
67  W5100.init();
68  SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
69  W5100.setMACAddress(mac);
70  W5100.setIPAddress(local_ip.raw_address());
71  W5100.setGatewayIp(gateway.raw_address());
72  W5100.setSubnetMask(subnet.raw_address());
73  SPI.endTransaction();
74  _dnsServerAddress = dns_server;
75}
76
77int EthernetClass::maintain(){
78  int rc = DHCP_CHECK_NONE;
79  if(_dhcp != NULL){
80    //we have a pointer to dhcp, use it
81    rc = _dhcp->checkLease();
82    switch ( rc ){
83      case DHCP_CHECK_NONE:
84        //nothing done
85        break;
86      case DHCP_CHECK_RENEW_OK:
87      case DHCP_CHECK_REBIND_OK:
88        //we might have got a new IP.
89        SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
90        W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
91        W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
92        W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
93        SPI.endTransaction();
94        _dnsServerAddress = _dhcp->getDnsServerIp();
95        break;
96      default:
97        //this is actually a error, it will retry though
98        break;
99    }
100  }
101  return rc;
102}
103
104IPAddress EthernetClass::localIP()
105{
106  IPAddress ret;
107  SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
108  W5100.getIPAddress(ret.raw_address());
109  SPI.endTransaction();
110  return ret;
111}
112
113IPAddress EthernetClass::subnetMask()
114{
115  IPAddress ret;
116  SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
117  W5100.getSubnetMask(ret.raw_address());
118  SPI.endTransaction();
119  return ret;
120}
121
122IPAddress EthernetClass::gatewayIP()
123{
124  IPAddress ret;
125  SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
126  W5100.getGatewayIp(ret.raw_address());
127  SPI.endTransaction();
128  return ret;
129}
130
131IPAddress EthernetClass::dnsServerIP()
132{
133  return _dnsServerAddress;
134}
135
136EthernetClass Ethernet;
Note: See TracBrowser for help on using the repository browser.