Skip to content

Commit c11fa73

Browse files
committed
Separating EthernetConnectionManager definition/implementation in a h/cpp file
1 parent b141195 commit c11fa73

File tree

2 files changed

+217
-155
lines changed

2 files changed

+217
-155
lines changed

src/EthernetConnectionManager.cpp

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* This file is part of ArduinoIoTCloud.
3+
*
4+
* Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to [email protected].
16+
*/
17+
18+
/******************************************************************************
19+
* INCLUDE
20+
******************************************************************************/
21+
22+
#include "EthernetConnectionManager.h"
23+
24+
/******************************************************************************
25+
* CONSTANTS
26+
******************************************************************************/
27+
28+
static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000;
29+
30+
/******************************************************************************
31+
* CTOR
32+
******************************************************************************/
33+
34+
EthConnectionManager::EthConnectionManager(uint8_t * mac, int const ss_pin) :
35+
mac(mac),
36+
ss_pin(ss_pin),
37+
lastConnectionTickTime(millis()),
38+
connectionTickTimeInterval(CHECK_INTERVAL_IDLE) {
39+
}
40+
41+
/******************************************************************************
42+
* PUBLIC MEMBER FUNCTIONS
43+
******************************************************************************/
44+
45+
void EthConnectionManager::init() {
46+
}
47+
48+
unsigned long EthConnectionManager::getTime() {
49+
//handled by fallback manager
50+
return lastValidTimestamp + 1;
51+
}
52+
53+
void EthConnectionManager::check() {
54+
char msgBuffer[120];
55+
unsigned long const now = millis();
56+
int networkStatus = 0;
57+
if (now - lastConnectionTickTime > connectionTickTimeInterval) {
58+
switch (netConnectionState) {
59+
case CONNECTION_STATE_INIT:
60+
if (ss_pin == -1) {
61+
networkStatus = Ethernet.begin(mac);
62+
} else {
63+
networkStatus = Ethernet.begin(mac, ss_pin);
64+
}
65+
networkStatus = Ethernet.hardwareStatus();
66+
*msgBuffer = 0;
67+
sprintf(msgBuffer, "Eth hardware status(): %d", networkStatus);
68+
debugMessage(msgBuffer, 2);
69+
if (networkStatus == EthernetNoHardware) {
70+
debugMessage("No Ethernet chip connected", 0);
71+
// don't continue:
72+
changeConnectionState(CONNECTION_STATE_ERROR);
73+
lastConnectionTickTime = now;
74+
return;
75+
}
76+
networkStatus = Ethernet.linkStatus();
77+
*msgBuffer = 0;
78+
sprintf(msgBuffer, "Eth link status(): %d", networkStatus);
79+
debugMessage(msgBuffer, 2);
80+
if (networkStatus == LinkOFF) {
81+
debugMessage("Failed to configure Ethernet via dhcp", 0);
82+
// don't continue:
83+
changeConnectionState(CONNECTION_STATE_ERROR);
84+
lastConnectionTickTime = now;
85+
return;
86+
}
87+
*msgBuffer = 0;
88+
sprintf(msgBuffer, "Ethernet shield recognized: ID", Ethernet.hardwareStatus());
89+
debugMessage(msgBuffer, 0);
90+
changeConnectionState(CONNECTION_STATE_CONNECTING);
91+
break;
92+
case CONNECTION_STATE_CONNECTING:
93+
*msgBuffer = 0;
94+
sprintf(msgBuffer, "Connecting via dhcp");
95+
debugMessage(msgBuffer, 2);
96+
if (ss_pin == -1) {
97+
networkStatus = Ethernet.begin(mac);
98+
} else {
99+
networkStatus = Ethernet.begin(mac, ss_pin);
100+
}
101+
*msgBuffer = 0;
102+
sprintf(msgBuffer, "Ethernet.status(): %d", networkStatus);
103+
debugMessage(msgBuffer, 2);
104+
if (networkStatus == 0) {
105+
*msgBuffer = 0;
106+
sprintf(msgBuffer, "Connection failed");
107+
debugMessage(msgBuffer, 0);
108+
109+
*msgBuffer = 0;
110+
sprintf(msgBuffer, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval);
111+
debugMessage(msgBuffer, 2);
112+
//changeConnectionState(CONNECTION_STATE_CONNECTING);
113+
return;
114+
} else {
115+
*msgBuffer = 0;
116+
sprintf(msgBuffer, "Connected!");
117+
debugMessage(msgBuffer, 2);
118+
changeConnectionState(CONNECTION_STATE_GETTIME);
119+
return;
120+
}
121+
break;
122+
case CONNECTION_STATE_GETTIME:
123+
debugMessage("Acquiring Time from Network", 3);
124+
unsigned long networkTime;
125+
networkTime = getTime();
126+
*msgBuffer = 0;
127+
sprintf(msgBuffer, "Network Time: %u", networkTime);
128+
debugMessage(msgBuffer, 3);
129+
if(networkTime > lastValidTimestamp){
130+
lastValidTimestamp = networkTime;
131+
changeConnectionState(CONNECTION_STATE_CONNECTED);
132+
}
133+
break;
134+
case CONNECTION_STATE_CONNECTED:
135+
// keep testing connection
136+
Ethernet.maintain();
137+
networkStatus = Ethernet.linkStatus();
138+
*msgBuffer = 0;
139+
sprintf(msgBuffer, "Eth link status(): %d", networkStatus);
140+
debugMessage(msgBuffer, 4);
141+
if (networkStatus != LinkON) {
142+
changeConnectionState(CONNECTION_STATE_DISCONNECTED);
143+
return;
144+
}
145+
*msgBuffer = 0;
146+
sprintf(msgBuffer, "Connected");
147+
debugMessage(msgBuffer, 2);
148+
break;
149+
case CONNECTION_STATE_DISCONNECTED:
150+
*msgBuffer = 0;
151+
sprintf(msgBuffer, "Connection lost.");
152+
debugMessage(msgBuffer, 0);
153+
debugMessage("Attempting reconnection", 1);
154+
changeConnectionState(CONNECTION_STATE_CONNECTING);
155+
//wifiClient.stop();
156+
break;
157+
}
158+
lastConnectionTickTime = now;
159+
}
160+
}
161+
162+
/******************************************************************************
163+
* PRIVATE MEMBER FUNCTIONS
164+
******************************************************************************/
165+
166+
void EthConnectionManager::changeConnectionState(NetworkConnectionState _newState) {
167+
netConnectionState = _newState;
168+
int newInterval = CHECK_INTERVAL_IDLE;
169+
switch (_newState) {
170+
case CONNECTION_STATE_INIT:
171+
newInterval = CHECK_INTERVAL_INIT;
172+
break;
173+
case CONNECTION_STATE_CONNECTING:
174+
newInterval = CHECK_INTERVAL_CONNECTING;
175+
break;
176+
case CONNECTION_STATE_GETTIME:
177+
newInterval = CHECK_INTERVAL_GETTIME;
178+
break;
179+
case CONNECTION_STATE_CONNECTED:
180+
newInterval = CHECK_INTERVAL_CONNECTED;
181+
break;
182+
case CONNECTION_STATE_DISCONNECTED:
183+
newInterval = CHECK_INTERVAL_DISCONNECTED;
184+
185+
break;
186+
}
187+
connectionTickTimeInterval = newInterval;
188+
lastConnectionTickTime = millis();
189+
}

src/EthernetConnectionManager.h

Lines changed: 28 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
1+
/*
2+
* This file is part of ArduinoIoTCloud.
3+
*
4+
* Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to [email protected].
16+
*/
17+
18+
/******************************************************************************
19+
* INCLUDE
20+
******************************************************************************/
21+
122
#include "ConnectionManager.h"
223

324
#include <Ethernet.h>
425
#define BOARD_HAS_ETHERNET
526

27+
/******************************************************************************
28+
* CLASS DECLARATION
29+
******************************************************************************/
30+
631
class EthConnectionManager : public ConnectionManager {
732
public:
8-
EthConnectionManager(uint8_t *mac, int ss_pin);
33+
EthConnectionManager(uint8_t * mac, int const ss_pin = -1);
934

10-
virtual unsigned long getTime();
1135
virtual void init();
36+
virtual unsigned long getTime();
1237
virtual void check();
1338
virtual Client &getClient() { return ethClient; };
1439
virtual UDP &getUDP() { return udp; };
1540

1641
private:
1742

1843
void changeConnectionState(NetworkConnectionState _newState);
44+
1945
const int CHECK_INTERVAL_IDLE = 100;
2046
const int CHECK_INTERVAL_INIT = 100;
2147
const int CHECK_INTERVAL_CONNECTING = 500;
@@ -32,156 +58,3 @@ class EthConnectionManager : public ConnectionManager {
3258
EthernetUDP udp;
3359
int connectionTickTimeInterval;
3460
};
35-
36-
#if !defined(BOARD_HAS_WIFI) && !defined(BOARD_HAS_GSM)
37-
static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000;
38-
#endif
39-
40-
EthConnectionManager::EthConnectionManager(uint8_t *mac, int ss_pin = -1) :
41-
mac(mac),
42-
ss_pin(ss_pin),
43-
lastConnectionTickTime(millis()),
44-
connectionTickTimeInterval(CHECK_INTERVAL_IDLE) {
45-
}
46-
47-
unsigned long EthConnectionManager::getTime() {
48-
//handled by fallback manager
49-
return lastValidTimestamp + 1;
50-
}
51-
52-
void EthConnectionManager::init() {
53-
}
54-
55-
void EthConnectionManager::changeConnectionState(NetworkConnectionState _newState) {
56-
netConnectionState = _newState;
57-
int newInterval = CHECK_INTERVAL_IDLE;
58-
switch (_newState) {
59-
case CONNECTION_STATE_INIT:
60-
newInterval = CHECK_INTERVAL_INIT;
61-
break;
62-
case CONNECTION_STATE_CONNECTING:
63-
newInterval = CHECK_INTERVAL_CONNECTING;
64-
break;
65-
case CONNECTION_STATE_GETTIME:
66-
newInterval = CHECK_INTERVAL_GETTIME;
67-
break;
68-
case CONNECTION_STATE_CONNECTED:
69-
newInterval = CHECK_INTERVAL_CONNECTED;
70-
break;
71-
case CONNECTION_STATE_DISCONNECTED:
72-
newInterval = CHECK_INTERVAL_DISCONNECTED;
73-
74-
break;
75-
}
76-
connectionTickTimeInterval = newInterval;
77-
lastConnectionTickTime = millis();
78-
}
79-
80-
void EthConnectionManager::check() {
81-
char msgBuffer[120];
82-
unsigned long const now = millis();
83-
int networkStatus = 0;
84-
if (now - lastConnectionTickTime > connectionTickTimeInterval) {
85-
switch (netConnectionState) {
86-
case CONNECTION_STATE_INIT:
87-
if (ss_pin == -1) {
88-
networkStatus = Ethernet.begin(mac);
89-
} else {
90-
networkStatus = Ethernet.begin(mac, ss_pin);
91-
}
92-
networkStatus = Ethernet.hardwareStatus();
93-
*msgBuffer = 0;
94-
sprintf(msgBuffer, "Eth hardware status(): %d", networkStatus);
95-
debugMessage(msgBuffer, 2);
96-
if (networkStatus == EthernetNoHardware) {
97-
debugMessage("No Ethernet chip connected", 0);
98-
// don't continue:
99-
changeConnectionState(CONNECTION_STATE_ERROR);
100-
lastConnectionTickTime = now;
101-
return;
102-
}
103-
networkStatus = Ethernet.linkStatus();
104-
*msgBuffer = 0;
105-
sprintf(msgBuffer, "Eth link status(): %d", networkStatus);
106-
debugMessage(msgBuffer, 2);
107-
if (networkStatus == LinkOFF) {
108-
debugMessage("Failed to configure Ethernet via dhcp", 0);
109-
// don't continue:
110-
changeConnectionState(CONNECTION_STATE_ERROR);
111-
lastConnectionTickTime = now;
112-
return;
113-
}
114-
*msgBuffer = 0;
115-
sprintf(msgBuffer, "Ethernet shield recognized: ID", Ethernet.hardwareStatus());
116-
debugMessage(msgBuffer, 0);
117-
changeConnectionState(CONNECTION_STATE_CONNECTING);
118-
break;
119-
case CONNECTION_STATE_CONNECTING:
120-
*msgBuffer = 0;
121-
sprintf(msgBuffer, "Connecting via dhcp");
122-
debugMessage(msgBuffer, 2);
123-
if (ss_pin == -1) {
124-
networkStatus = Ethernet.begin(mac);
125-
} else {
126-
networkStatus = Ethernet.begin(mac, ss_pin);
127-
}
128-
*msgBuffer = 0;
129-
sprintf(msgBuffer, "Ethernet.status(): %d", networkStatus);
130-
debugMessage(msgBuffer, 2);
131-
if (networkStatus == 0) {
132-
*msgBuffer = 0;
133-
sprintf(msgBuffer, "Connection failed");
134-
debugMessage(msgBuffer, 0);
135-
136-
*msgBuffer = 0;
137-
sprintf(msgBuffer, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval);
138-
debugMessage(msgBuffer, 2);
139-
//changeConnectionState(CONNECTION_STATE_CONNECTING);
140-
return;
141-
} else {
142-
*msgBuffer = 0;
143-
sprintf(msgBuffer, "Connected!");
144-
debugMessage(msgBuffer, 2);
145-
changeConnectionState(CONNECTION_STATE_GETTIME);
146-
return;
147-
}
148-
break;
149-
case CONNECTION_STATE_GETTIME:
150-
debugMessage("Acquiring Time from Network", 3);
151-
unsigned long networkTime;
152-
networkTime = getTime();
153-
*msgBuffer = 0;
154-
sprintf(msgBuffer, "Network Time: %u", networkTime);
155-
debugMessage(msgBuffer, 3);
156-
if(networkTime > lastValidTimestamp){
157-
lastValidTimestamp = networkTime;
158-
changeConnectionState(CONNECTION_STATE_CONNECTED);
159-
}
160-
break;
161-
case CONNECTION_STATE_CONNECTED:
162-
// keep testing connection
163-
Ethernet.maintain();
164-
networkStatus = Ethernet.linkStatus();
165-
*msgBuffer = 0;
166-
sprintf(msgBuffer, "Eth link status(): %d", networkStatus);
167-
debugMessage(msgBuffer, 4);
168-
if (networkStatus != LinkON) {
169-
changeConnectionState(CONNECTION_STATE_DISCONNECTED);
170-
return;
171-
}
172-
*msgBuffer = 0;
173-
sprintf(msgBuffer, "Connected");
174-
debugMessage(msgBuffer, 2);
175-
break;
176-
case CONNECTION_STATE_DISCONNECTED:
177-
*msgBuffer = 0;
178-
sprintf(msgBuffer, "Connection lost.");
179-
debugMessage(msgBuffer, 0);
180-
debugMessage("Attempting reconnection", 1);
181-
changeConnectionState(CONNECTION_STATE_CONNECTING);
182-
//wifiClient.stop();
183-
break;
184-
}
185-
lastConnectionTickTime = now;
186-
}
187-
}

0 commit comments

Comments
 (0)