Skip to content

Commit 08436bd

Browse files
move EthernetClientStream.cpp to .h file
Avoids linker issues with 3rd party board packages. Removed unnecessary includes.
1 parent 5d93938 commit 08436bd

File tree

2 files changed

+114
-136
lines changed

2 files changed

+114
-136
lines changed

utility/EthernetClientStream.cpp

Lines changed: 1 addition & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,3 @@
11
/*
2-
EthernetClientStream.cpp
3-
An Arduino-Stream that wraps an instance of Client reconnecting to
4-
the remote-ip in a transparent way. A disconnected client may be
5-
recognized by the returnvalues -1 from calls to peek or read and
6-
a 0 from calls to write.
7-
8-
Copyright (C) 2013 Norbert Truchsess. All rights reserved.
9-
10-
This library is free software; you can redistribute it and/or
11-
modify it under the terms of the GNU Lesser General Public
12-
License as published by the Free Software Foundation; either
13-
version 2.1 of the License, or (at your option) any later version.
14-
15-
See file LICENSE.txt for further informations on licensing terms.
16-
17-
formatted using the GNU C formatting and indenting
2+
* Implementation is in EthernetClientStream.h to avoid linker issues.
183
*/
19-
20-
#include "EthernetClientStream.h"
21-
#include <Arduino.h>
22-
23-
//#define SERIAL_DEBUG
24-
#include "firmataDebug.h"
25-
26-
#define MILLIS_RECONNECT 5000
27-
28-
EthernetClientStream::EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port)
29-
: client(client),
30-
localip(localip),
31-
ip(ip),
32-
host(host),
33-
port(port),
34-
connected(false)
35-
{
36-
}
37-
38-
int
39-
EthernetClientStream::available()
40-
{
41-
return maintain() ? client.available() : 0;
42-
}
43-
44-
int
45-
EthernetClientStream::read()
46-
{
47-
return maintain() ? client.read() : -1;
48-
}
49-
50-
int
51-
EthernetClientStream::peek()
52-
{
53-
return maintain() ? client.peek() : -1;
54-
}
55-
56-
void EthernetClientStream::flush()
57-
{
58-
if (maintain())
59-
client.flush();
60-
}
61-
62-
size_t
63-
EthernetClientStream::write(uint8_t c)
64-
{
65-
return maintain() ? client.write(c) : 0;
66-
}
67-
68-
void
69-
EthernetClientStream::maintain(IPAddress localip)
70-
{
71-
// temporary hack to Firmata to compile for Intel Galileo
72-
// the issue is documented here: https://github.com/firmata/arduino/issues/218
73-
#if !defined(ARDUINO_LINUX)
74-
// ensure the local IP is updated in the case that it is changed by the DHCP server
75-
if (this->localip != localip)
76-
{
77-
this->localip = localip;
78-
if (connected)
79-
stop();
80-
}
81-
#endif
82-
}
83-
84-
void
85-
EthernetClientStream::stop()
86-
{
87-
client.stop();
88-
connected = false;
89-
time_connect = millis();
90-
}
91-
92-
bool
93-
EthernetClientStream::maintain()
94-
{
95-
if (client && client.connected())
96-
return true;
97-
98-
if (connected)
99-
{
100-
stop();
101-
}
102-
// if the client is disconnected, attempt to reconnect every 5 seconds
103-
else if (millis()-time_connect >= MILLIS_RECONNECT)
104-
{
105-
connected = host ? client.connect(host, port) : client.connect(ip, port);
106-
if (!connected) {
107-
time_connect = millis();
108-
DEBUG_PRINTLN("connection failed. attempting to reconnect...");
109-
} else {
110-
DEBUG_PRINTLN("connected");
111-
}
112-
}
113-
return connected;
114-
}

utility/EthernetClientStream.h

Lines changed: 113 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,128 @@
1414
1515
See file LICENSE.txt for further informations on licensing terms.
1616
17-
formatted using the GNU C formatting and indenting
17+
Last updated June 18th, 2016
1818
*/
1919

2020
#ifndef ETHERNETCLIENTSTREAM_H
2121
#define ETHERNETCLIENTSTREAM_H
2222

2323
#include <inttypes.h>
24-
#include <stdio.h>
2524
#include <Stream.h>
26-
#include <Client.h>
27-
#include <IPAddress.h>
25+
26+
//#define SERIAL_DEBUG
27+
#include "firmataDebug.h"
28+
29+
#define MILLIS_RECONNECT 5000
2830

2931
class EthernetClientStream : public Stream
3032
{
31-
public:
32-
EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port);
33-
int available();
34-
int read();
35-
int peek();
36-
void flush();
37-
size_t write(uint8_t);
38-
void maintain(IPAddress localip);
39-
40-
private:
41-
Client &client;
42-
IPAddress localip;
43-
IPAddress ip;
44-
const char* host;
45-
uint16_t port;
46-
bool connected;
47-
uint32_t time_connect;
48-
bool maintain();
49-
void stop();
33+
public:
34+
EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port);
35+
int available();
36+
int read();
37+
int peek();
38+
void flush();
39+
size_t write(uint8_t);
40+
void maintain(IPAddress localip);
41+
42+
private:
43+
Client &client;
44+
IPAddress localip;
45+
IPAddress ip;
46+
const char* host;
47+
uint16_t port;
48+
bool connected;
49+
uint32_t time_connect;
50+
bool maintain();
51+
void stop();
5052
};
5153

52-
#endif
54+
55+
/*
56+
* EthernetClientStream.cpp
57+
* Copied here as a hack to linker issues with 3rd party board packages that don't properly
58+
* implement the Arduino network APIs.
59+
*/
60+
EthernetClientStream::EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port)
61+
: client(client),
62+
localip(localip),
63+
ip(ip),
64+
host(host),
65+
port(port),
66+
connected(false)
67+
{
68+
}
69+
70+
int
71+
EthernetClientStream::available()
72+
{
73+
return maintain() ? client.available() : 0;
74+
}
75+
76+
int
77+
EthernetClientStream::read()
78+
{
79+
return maintain() ? client.read() : -1;
80+
}
81+
82+
int
83+
EthernetClientStream::peek()
84+
{
85+
return maintain() ? client.peek() : -1;
86+
}
87+
88+
void EthernetClientStream::flush()
89+
{
90+
if (maintain())
91+
client.flush();
92+
}
93+
94+
size_t
95+
EthernetClientStream::write(uint8_t c)
96+
{
97+
return maintain() ? client.write(c) : 0;
98+
}
99+
100+
void
101+
EthernetClientStream::maintain(IPAddress localip)
102+
{
103+
// ensure the local IP is updated in the case that it is changed by the DHCP server
104+
if (this->localip != localip) {
105+
this->localip = localip;
106+
if (connected)
107+
stop();
108+
}
109+
}
110+
111+
void
112+
EthernetClientStream::stop()
113+
{
114+
client.stop();
115+
connected = false;
116+
time_connect = millis();
117+
}
118+
119+
bool
120+
EthernetClientStream::maintain()
121+
{
122+
if (client && client.connected())
123+
return true;
124+
125+
if (connected) {
126+
stop();
127+
}
128+
// if the client is disconnected, attempt to reconnect every 5 seconds
129+
else if (millis() - time_connect >= MILLIS_RECONNECT) {
130+
connected = host ? client.connect(host, port) : client.connect(ip, port);
131+
if (!connected) {
132+
time_connect = millis();
133+
DEBUG_PRINTLN("connection failed. attempting to reconnect...");
134+
} else {
135+
DEBUG_PRINTLN("connected");
136+
}
137+
}
138+
return connected;
139+
}
140+
141+
#endif /* ETHERNETCLIENTSTREAM_H */

0 commit comments

Comments
 (0)