-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleNetManager.cpp
More file actions
209 lines (180 loc) · 6.06 KB
/
Copy pathSimpleNetManager.cpp
File metadata and controls
209 lines (180 loc) · 6.06 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "SimpleNetManager.h"
namespace SimpleNet {
/**
* @brief Constructor (Basic): Delegates to the full constructor with CS pin 10 and no debug stream.
*/
SimpleNetManager::SimpleNetManager(byte mac[])
: SimpleNetManager(mac, 10, nullptr) { // Default CS pin is 10
}
/**
* @brief Constructor (MAC + CS Pin): Delegates to the full constructor with no debug stream.
*/
SimpleNetManager::SimpleNetManager(byte mac[], uint8_t csPin)
: SimpleNetManager(mac, csPin, nullptr) {
}
/**
* @brief Constructor (MAC + Debug): Delegates to the full constructor with default CS pin 10.
*/
SimpleNetManager::SimpleNetManager(byte mac[], Stream* debugStream)
: SimpleNetManager(mac, 10, debugStream) { // Default CS pin is 10
}
/**
* @brief Constructor (Full): This is the main constructor that does all the setup work.
*/
SimpleNetManager::SimpleNetManager(byte mac[], uint8_t csPin, Stream* debugStream) {
memcpy(_mac, mac, 6);
_csPin = csPin;
_debugStream = debugStream;
_currentState = NET_DISCONNECTED;
_use_static_ip = false;
_lastConnectionAttempt = 0;
_onConnectCallback = nullptr;
_onDisconnectCallback = nullptr;
}
/**
* @brief Initializes for DHCP.
*/
void SimpleNetManager::begin() {
_use_static_ip = false;
// Always initialize the Ethernet CS pin based on the constructor used.
Ethernet.init(_csPin);
if (_debugStream) {
_debugStream->print(F("[NetManager] Using CS pin: "));
_debugStream->println(_csPin);
}
_lastConnectionAttempt = millis() - _connectionInterval;
if (_debugStream) {
_debugStream->println(F("[NetManager] Initialized for DHCP."));
}
}
/**
* @brief Initializes for Static IP.
*/
void SimpleNetManager::begin(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {
_use_static_ip = true;
_ip = ip;
_dns = dns;
_gateway = gateway;
_subnet = subnet;
// Always initialize the Ethernet CS pin based on the constructor used.
Ethernet.init(_csPin);
if (_debugStream) {
_debugStream->print(F("[NetManager] Using CS pin: "));
_debugStream->println(_csPin);
}
_lastConnectionAttempt = millis() - _connectionInterval;
if (_debugStream) {
_debugStream->println(F("[NetManager] Initialized for Static IP."));
}
}
/**
* @brief The main state machine loop to be called repeatedly.
*/
NetState SimpleNetManager::loop() {
NetState previousState = _currentState;
switch (_currentState) {
case NET_DISCONNECTED:
if (millis() - _lastConnectionAttempt >= _connectionInterval) {
_currentState = NET_CONNECTING;
connect();
}
break;
case NET_CONNECTING:
// This state is transient and handled within the connect() method.
break;
case NET_CONNECTED:
uint8_t maintain_status = Ethernet.maintain();
if (maintain_status == 1 || maintain_status == 3) {
if (_debugStream) _debugStream->println(F("[NetManager] DHCP lease lost."));
_currentState = NET_DISCONNECTED;
}
if (Ethernet.linkStatus() != LinkON) {
if (_debugStream) _debugStream->println(F("[NetManager] Physical link lost."));
_currentState = NET_DISCONNECTED;
}
break;
}
if (_currentState != previousState) {
if (_currentState == NET_CONNECTED) {
if (_onConnectCallback) {
_onConnectCallback();
}
} else if (_currentState == NET_DISCONNECTED && previousState == NET_CONNECTED) {
if (_onDisconnectCallback) {
_onDisconnectCallback();
}
_lastConnectionAttempt = millis();
}
}
return _currentState;
}
/**
* @brief Private method to handle the actual connection attempt.
*/
void SimpleNetManager::connect() {
_lastConnectionAttempt = millis();
if (_debugStream) {
_debugStream->print(F("[NetManager] Attempting connection... Mode: "));
_debugStream->println(_use_static_ip ? "Static" : "DHCP");
}
bool success = false;
if (_use_static_ip) {
Ethernet.begin(_mac, _ip, _dns, _gateway, _subnet);
// For static, success is assumed until link status check fails in loop().
// A short delay might be needed for link to establish, which the non-blocking
// loop naturally handles. We tentatively move to CONNECTED.
if (Ethernet.linkStatus() == LinkON) {
_currentState = NET_CONNECTED;
} else {
// If link is not on, we go back to disconnected to retry.
_currentState = NET_DISCONNECTED;
}
} else { // DHCP
if (Ethernet.begin(_mac) == 1) {
success = true;
}
}
if (!_use_static_ip) {
if (success && Ethernet.localIP() != IPAddress(0,0,0,0)) {
_currentState = NET_CONNECTED;
if (_debugStream) {
_debugStream->print(F("[NetManager] DHCP connection successful. IP: "));
_debugStream->println(Ethernet.localIP());
}
} else {
_currentState = NET_DISCONNECTED;
if (_debugStream) _debugStream->println(F("[NetManager] DHCP connection failed."));
}
}
}
/**
* @brief Returns true if the current state is CONNECTED.
*/
bool SimpleNetManager::isConnected() {
return _currentState == NET_CONNECTED;
}
/**
* @brief Returns a reference to the internal EthernetClient object.
*/
EthernetClient& SimpleNetManager::getClient() {
return _client;
}
/**
* @brief Sets the connection retry interval.
*/
void SimpleNetManager::setConnectionRetryInterval(long interval) {
_connectionInterval = interval;
}
/**
* @brief Registers the onConnect callback function.
*/
void SimpleNetManager::onConnect(void (*callback)()) {
_onConnectCallback = callback;
}
/**
* @brief Registers the onDisconnect callback function.
*/
void SimpleNetManager::onDisconnect(void (*callback)()) {
_onDisconnectCallback = callback;
}
} // namespace SimpleNet