root / arduino-1.0 / libraries / Ethernet / Ethernet.cpp @ 58d82c77
History | View | Annotate | Download (2.46 KB)
1 | 58d82c77 | Tom Mullins | #include "w5100.h" |
---|---|---|---|
2 | #include "Ethernet.h" |
||
3 | #include "Dhcp.h" |
||
4 | |||
5 | // XXX: don't make assumptions about the value of MAX_SOCK_NUM.
|
||
6 | uint8_t EthernetClass::_state[MAX_SOCK_NUM] = { |
||
7 | 0, 0, 0, 0 }; |
||
8 | uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = { |
||
9 | 0, 0, 0, 0 }; |
||
10 | |||
11 | int EthernetClass::begin(uint8_t *mac_address)
|
||
12 | { |
||
13 | DhcpClass dhcp; |
||
14 | |||
15 | // Initialise the basic info
|
||
16 | W5100.init(); |
||
17 | W5100.setMACAddress(mac_address); |
||
18 | W5100.setIPAddress(IPAddress(0,0,0,0).raw_address()); |
||
19 | |||
20 | // Now try to get our config info from a DHCP server
|
||
21 | int ret = dhcp.beginWithDHCP(mac_address);
|
||
22 | if(ret == 1) |
||
23 | { |
||
24 | // We've successfully found a DHCP server and got our configuration info, so set things
|
||
25 | // accordingly
|
||
26 | W5100.setIPAddress(dhcp.getLocalIp().raw_address()); |
||
27 | W5100.setGatewayIp(dhcp.getGatewayIp().raw_address()); |
||
28 | W5100.setSubnetMask(dhcp.getSubnetMask().raw_address()); |
||
29 | _dnsServerAddress = dhcp.getDnsServerIp(); |
||
30 | } |
||
31 | |||
32 | return ret;
|
||
33 | } |
||
34 | |||
35 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
|
||
36 | { |
||
37 | // Assume the DNS server will be the machine on the same network as the local IP
|
||
38 | // but with last octet being '1'
|
||
39 | IPAddress dns_server = local_ip; |
||
40 | dns_server[3] = 1; |
||
41 | begin(mac_address, local_ip, dns_server); |
||
42 | } |
||
43 | |||
44 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
|
||
45 | { |
||
46 | // Assume the gateway will be the machine on the same network as the local IP
|
||
47 | // but with last octet being '1'
|
||
48 | IPAddress gateway = local_ip; |
||
49 | gateway[3] = 1; |
||
50 | begin(mac_address, local_ip, dns_server, gateway); |
||
51 | } |
||
52 | |||
53 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
|
||
54 | { |
||
55 | IPAddress subnet(255, 255, 255, 0); |
||
56 | begin(mac_address, local_ip, dns_server, gateway, subnet); |
||
57 | } |
||
58 | |||
59 | void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
|
||
60 | { |
||
61 | W5100.init(); |
||
62 | W5100.setMACAddress(mac); |
||
63 | W5100.setIPAddress(local_ip._address); |
||
64 | W5100.setGatewayIp(gateway._address); |
||
65 | W5100.setSubnetMask(subnet._address); |
||
66 | _dnsServerAddress = dns_server; |
||
67 | } |
||
68 | |||
69 | IPAddress EthernetClass::localIP() |
||
70 | { |
||
71 | IPAddress ret; |
||
72 | W5100.getIPAddress(ret.raw_address()); |
||
73 | return ret;
|
||
74 | } |
||
75 | |||
76 | IPAddress EthernetClass::subnetMask() |
||
77 | { |
||
78 | IPAddress ret; |
||
79 | W5100.getSubnetMask(ret.raw_address()); |
||
80 | return ret;
|
||
81 | } |
||
82 | |||
83 | IPAddress EthernetClass::gatewayIP() |
||
84 | { |
||
85 | IPAddress ret; |
||
86 | W5100.getGatewayIp(ret.raw_address()); |
||
87 | return ret;
|
||
88 | } |
||
89 | |||
90 | IPAddress EthernetClass::dnsServerIP() |
||
91 | { |
||
92 | return _dnsServerAddress;
|
||
93 | } |
||
94 | |||
95 | EthernetClass Ethernet; |