|
1 | | -// DHCP Library v0.3 - April 25, 2009 |
2 | | -// Author: Jordan Terrell - blog.jordanterrell.com |
3 | | - |
4 | | -#include <string.h> |
5 | | -#include <stdlib.h> |
6 | | -#include "Dhcp.h" |
7 | | -#include "Arduino.h" |
8 | | -#include "utility/stm32_eth.h" |
9 | | - |
10 | | -int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) |
11 | | -{ |
12 | | - UNUSED(responseTimeout); |
13 | | - _timeout = timeout; |
14 | | - _dhcp_lease_state = DHCP_CHECK_NONE; |
15 | | - |
16 | | - // zero out _dhcpMacAddr |
17 | | - memset(_dhcpMacAddr, 0, 6); |
18 | | - reset_DHCP_lease(); |
19 | | - |
20 | | - memcpy((void*)_dhcpMacAddr, (void*)mac, 6); |
21 | | - _dhcp_state = STATE_DHCP_START; |
22 | | - stm32_set_DHCP_state(_dhcp_state); |
23 | | - return request_DHCP_lease(); |
24 | | -} |
25 | | - |
26 | | -void DhcpClass::reset_DHCP_lease(){ |
27 | | - _dhcp_state = STATE_DHCP_RELEASE; |
28 | | - stm32_set_DHCP_state(_dhcp_state); |
29 | | - stm32_eth_scheduler(); |
30 | | - _dhcp_state = stm32_get_DHCP_state(); |
31 | | -} |
32 | | - |
33 | | -//return:0 on error, 1 if request is sent and response is received |
34 | | -int DhcpClass::request_DHCP_lease(){ |
35 | | - |
36 | | - int result = 0; |
37 | | - unsigned long startTime = millis(); |
38 | | - |
39 | | - while(_dhcp_state != STATE_DHCP_LEASED) |
40 | | - { |
41 | | - stm32_eth_scheduler(); |
42 | | - _dhcp_state = stm32_get_DHCP_state(); |
43 | | - |
44 | | - if(result != 1 && ((millis() - startTime) > _timeout)) { |
45 | | - reset_DHCP_lease(); |
46 | | - break; |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - if(_dhcp_state == STATE_DHCP_LEASED) { |
51 | | - result = 1; |
52 | | - } |
53 | | - |
54 | | - return result; |
55 | | -} |
56 | | - |
57 | | -/* |
58 | | - returns: |
59 | | - 0/DHCP_CHECK_NONE: nothing happened |
60 | | - 1/DHCP_CHECK_RENEW_FAIL: renew failed |
61 | | - 2/DHCP_CHECK_RENEW_OK: renew success |
62 | | - 3/DHCP_CHECK_REBIND_FAIL: rebind fail |
63 | | - 4/DHCP_CHECK_REBIND_OK: rebind success |
64 | | -*/ |
65 | | -int DhcpClass::checkLease(){ |
66 | | - int rc = DHCP_CHECK_NONE; |
67 | | - |
68 | | - stm32_eth_scheduler(); |
69 | | - rc = stm32_get_DHCP_lease_state(); |
70 | | - |
71 | | - if(rc != _dhcp_lease_state) |
72 | | - { |
73 | | - switch(_dhcp_lease_state) { |
74 | | - case DHCP_CHECK_NONE: |
75 | | - _dhcp_lease_state = rc; |
76 | | - rc = DHCP_CHECK_NONE; |
77 | | - break; |
78 | | - |
79 | | - case DHCP_CHECK_RENEW_OK: |
80 | | - _dhcp_lease_state = rc; |
81 | | - if(rc == DHCP_CHECK_NONE) { |
82 | | - rc = DHCP_CHECK_RENEW_OK; |
83 | | - } else { |
84 | | - rc = DHCP_CHECK_RENEW_FAIL; |
85 | | - } |
86 | | - break; |
87 | | - |
88 | | - case DHCP_CHECK_REBIND_OK: |
89 | | - _dhcp_lease_state = rc; |
90 | | - if(rc == DHCP_CHECK_NONE) { |
91 | | - rc = DHCP_CHECK_REBIND_OK; |
92 | | - } else { |
93 | | - rc = DHCP_CHECK_REBIND_FAIL; |
94 | | - } |
95 | | - break; |
96 | | - |
97 | | - default: |
98 | | - _dhcp_lease_state = DHCP_CHECK_NONE; |
99 | | - break; |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - return rc; |
104 | | -} |
105 | | - |
106 | | -IPAddress DhcpClass::getLocalIp() |
107 | | -{ |
108 | | - return IPAddress(stm32_eth_get_ipaddr()); |
109 | | -} |
110 | | - |
111 | | -IPAddress DhcpClass::getSubnetMask() |
112 | | -{ |
113 | | - return IPAddress(stm32_eth_get_netmaskaddr()); |
114 | | -} |
115 | | - |
116 | | -IPAddress DhcpClass::getGatewayIp() |
117 | | -{ |
118 | | - return IPAddress(stm32_eth_get_gwaddr()); |
119 | | -} |
120 | | - |
121 | | -IPAddress DhcpClass::getDhcpServerIp() |
122 | | -{ |
123 | | - return IPAddress(stm32_eth_get_dhcpaddr()); |
124 | | -} |
125 | | - |
126 | | -IPAddress DhcpClass::getDnsServerIp() |
127 | | -{ |
128 | | - return IPAddress(stm32_eth_get_dnsaddr()); |
129 | | -} |
| 1 | +// DHCP Library v0.3 - April 25, 2009 |
| 2 | +// Author: Jordan Terrell - blog.jordanterrell.com |
| 3 | + |
| 4 | +#include <string.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include "Dhcp.h" |
| 7 | +#include "Arduino.h" |
| 8 | +#include "utility/stm32_eth.h" |
| 9 | + |
| 10 | +int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) |
| 11 | +{ |
| 12 | + UNUSED(responseTimeout); |
| 13 | + _timeout = timeout; |
| 14 | + _dhcp_lease_state = DHCP_CHECK_NONE; |
| 15 | + |
| 16 | + // zero out _dhcpMacAddr |
| 17 | + memset(_dhcpMacAddr, 0, 6); |
| 18 | + reset_DHCP_lease(); |
| 19 | + |
| 20 | + memcpy((void *)_dhcpMacAddr, (void *)mac, 6); |
| 21 | + _dhcp_state = STATE_DHCP_START; |
| 22 | + stm32_set_DHCP_state(_dhcp_state); |
| 23 | + return request_DHCP_lease(); |
| 24 | +} |
| 25 | + |
| 26 | +void DhcpClass::reset_DHCP_lease() |
| 27 | +{ |
| 28 | + _dhcp_state = STATE_DHCP_RELEASE; |
| 29 | + stm32_set_DHCP_state(_dhcp_state); |
| 30 | + stm32_eth_scheduler(); |
| 31 | + _dhcp_state = stm32_get_DHCP_state(); |
| 32 | +} |
| 33 | + |
| 34 | +//return:0 on error, 1 if request is sent and response is received |
| 35 | +int DhcpClass::request_DHCP_lease() |
| 36 | +{ |
| 37 | + |
| 38 | + int result = 0; |
| 39 | + unsigned long startTime = millis(); |
| 40 | + |
| 41 | + while (_dhcp_state != STATE_DHCP_LEASED) { |
| 42 | + stm32_eth_scheduler(); |
| 43 | + _dhcp_state = stm32_get_DHCP_state(); |
| 44 | + |
| 45 | + if (result != 1 && ((millis() - startTime) > _timeout)) { |
| 46 | + reset_DHCP_lease(); |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (_dhcp_state == STATE_DHCP_LEASED) { |
| 52 | + result = 1; |
| 53 | + } |
| 54 | + |
| 55 | + return result; |
| 56 | +} |
| 57 | + |
| 58 | +/* |
| 59 | + returns: |
| 60 | + 0/DHCP_CHECK_NONE: nothing happened |
| 61 | + 1/DHCP_CHECK_RENEW_FAIL: renew failed |
| 62 | + 2/DHCP_CHECK_RENEW_OK: renew success |
| 63 | + 3/DHCP_CHECK_REBIND_FAIL: rebind fail |
| 64 | + 4/DHCP_CHECK_REBIND_OK: rebind success |
| 65 | +*/ |
| 66 | +int DhcpClass::checkLease() |
| 67 | +{ |
| 68 | + int rc = DHCP_CHECK_NONE; |
| 69 | + |
| 70 | + stm32_eth_scheduler(); |
| 71 | + rc = stm32_get_DHCP_lease_state(); |
| 72 | + |
| 73 | + if (rc != _dhcp_lease_state) { |
| 74 | + switch (_dhcp_lease_state) { |
| 75 | + case DHCP_CHECK_NONE: |
| 76 | + _dhcp_lease_state = rc; |
| 77 | + rc = DHCP_CHECK_NONE; |
| 78 | + break; |
| 79 | + |
| 80 | + case DHCP_CHECK_RENEW_OK: |
| 81 | + _dhcp_lease_state = rc; |
| 82 | + if (rc == DHCP_CHECK_NONE) { |
| 83 | + rc = DHCP_CHECK_RENEW_OK; |
| 84 | + } else { |
| 85 | + rc = DHCP_CHECK_RENEW_FAIL; |
| 86 | + } |
| 87 | + break; |
| 88 | + |
| 89 | + case DHCP_CHECK_REBIND_OK: |
| 90 | + _dhcp_lease_state = rc; |
| 91 | + if (rc == DHCP_CHECK_NONE) { |
| 92 | + rc = DHCP_CHECK_REBIND_OK; |
| 93 | + } else { |
| 94 | + rc = DHCP_CHECK_REBIND_FAIL; |
| 95 | + } |
| 96 | + break; |
| 97 | + |
| 98 | + default: |
| 99 | + _dhcp_lease_state = DHCP_CHECK_NONE; |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return rc; |
| 105 | +} |
| 106 | + |
| 107 | +IPAddress DhcpClass::getLocalIp() |
| 108 | +{ |
| 109 | + return IPAddress(stm32_eth_get_ipaddr()); |
| 110 | +} |
| 111 | + |
| 112 | +IPAddress DhcpClass::getSubnetMask() |
| 113 | +{ |
| 114 | + return IPAddress(stm32_eth_get_netmaskaddr()); |
| 115 | +} |
| 116 | + |
| 117 | +IPAddress DhcpClass::getGatewayIp() |
| 118 | +{ |
| 119 | + return IPAddress(stm32_eth_get_gwaddr()); |
| 120 | +} |
| 121 | + |
| 122 | +IPAddress DhcpClass::getDhcpServerIp() |
| 123 | +{ |
| 124 | + return IPAddress(stm32_eth_get_dhcpaddr()); |
| 125 | +} |
| 126 | + |
| 127 | +IPAddress DhcpClass::getDnsServerIp() |
| 128 | +{ |
| 129 | + return IPAddress(stm32_eth_get_dnsaddr()); |
| 130 | +} |
0 commit comments