-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathEthernet.h
136 lines (109 loc) · 4.23 KB
/
Ethernet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Copyright 2018 Paul Stoffregen
* Copyright 2020-2021 Arduino SA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef ethernet_h_
#define ethernet_h_
#include "Arduino.h"
#include "SocketHelpers.h"
#include "api/IPAddress.h"
#include "netsocket/NetworkInterface.h"
#include "EthernetInterface.h"
enum EthernetLinkStatus {
Unknown,
LinkON,
LinkOFF
};
enum EthernetHardwareStatus {
EthernetNoHardware,
EthernetMbed = 6
};
namespace arduino {
enum { // compatibility with Arduino ::maintain()
DHCP_CHECK_NONE = 0,
DHCP_CHECK_RENEW_FAIL = 1,
DHCP_CHECK_RENEW_OK = 2,
DHCP_CHECK_REBIND_FAIL = 3,
DHCP_CHECK_REBIND_OK = 4
};
typedef void *(*voidPrtFuncPtr)(void);
class EthernetClass : public MbedSocketClass {
public:
// Initialise the Ethernet shield to use the provided MAC address and
// gain the rest of the configuration through DHCP.
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
EthernetClass(EthernetInterface *_if)
: eth_if(_if){};
EthernetClass(){};
EthernetClass(voidPrtFuncPtr _cb)
: _initializerCallback(_cb){};
int begin(uint8_t *mac = nullptr, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
EthernetLinkStatus linkStatus();
EthernetHardwareStatus hardwareStatus();
// Manual configuration
int begin(uint8_t *mac, IPAddress ip);
int begin(uint8_t *mac, IPAddress ip, IPAddress dns);
int begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway);
int begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
int begin(IPAddress ip) {
return begin(nullptr, ip);
}
int begin(IPAddress ip, IPAddress dns) {
return begin(nullptr, ip, dns);
}
int begin(IPAddress ip, IPAddress dns, IPAddress gateway) {
return begin(nullptr, ip, dns, gateway);
}
int begin(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {
return begin(nullptr, ip, dns, gateway, subnet);
}
void init(uint8_t sspin = 10);
void MACAddress(uint8_t *mac_address);
int disconnect(void);
void end(void);
uint8_t status();
unsigned long getTime();
void setMACAddress(const uint8_t *mac_address);
void setLocalIP(const IPAddress local_ip);
void setSubnetMask(const IPAddress subnet);
void setGatewayIP(const IPAddress gateway);
void setDnsServerIP(const IPAddress dns_server) {
_dnsServer1 = socketAddressFromIpAddress(dns_server, 0);
}
void setRetransmissionTimeout(uint16_t milliseconds);
void setRetransmissionCount(uint8_t num);
friend class EthernetClient;
friend class EthernetServer;
friend class EthernetUDP;
NetworkInterface *getNetwork();
constexpr static int maintain () { return DHCP_CHECK_NONE; }
private:
int _begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout);
volatile EthernetLinkStatus _currentNetworkStatus = Unknown;
EthernetInterface net;
EthernetInterface *eth_if = &net;
voidPrtFuncPtr _initializerCallback;
arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
};
}
extern arduino::EthernetClass Ethernet;
#include "EthernetClient.h"
#include "EthernetServer.h"
#include "EthernetUdp.h"
#endif