Skip to content

Commit edc329a

Browse files
committed
EthernetClientStream host connection callback
- added host connection callback hook to notfiy connect and disconnect (WiFiStream feature)
1 parent 41097ce commit edc329a

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

examples/StandardFirmataEthernet/StandardFirmataEthernet.ino

+25-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
See file LICENSE.txt for further informations on licensing terms.
2222
23-
Last updated August 17th, 2017
23+
Last updated March 10th, 2020
2424
*/
2525

2626
/*
@@ -832,6 +832,26 @@ void systemResetCallback()
832832
isResetting = false;
833833
}
834834

835+
#ifdef ETHERNETCLIENTSTREAM_H
836+
/*
837+
* Called when a TCP connection is either connected or disconnected.
838+
* TODO:
839+
* - report connected or reconnected state to host (to be added to protocol)
840+
* - report current state to host (to be added to protocol)
841+
*/
842+
void hostConnectionCallback(byte state)
843+
{
844+
switch (state) {
845+
case HOST_CONNECTION_CONNECTED:
846+
DEBUG_PRINTLN( "TCP connection established" );
847+
break;
848+
case HOST_CONNECTION_DISCONNECTED:
849+
DEBUG_PRINTLN( "TCP connection disconnected" );
850+
break;
851+
}
852+
}
853+
#endif
854+
835855
void printEthernetStatus()
836856
{
837857
DEBUG_PRINT("Local IP Address: ");
@@ -873,6 +893,10 @@ void ignorePins()
873893

874894
void initTransport()
875895
{
896+
#ifdef ETHERNETCLIENTSTREAM_H
897+
stream.attach(hostConnectionCallback);
898+
#endif
899+
876900
#ifdef YUN_ETHERNET
877901
Bridge.begin();
878902
#else

utility/EthernetClientStream.h

+26-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
See file LICENSE.txt for further informations on licensing terms.
1616
17-
Last updated June 18th, 2016
17+
Last updated March 10th, 2020
1818
*/
1919

2020
#ifndef ETHERNETCLIENTSTREAM_H
@@ -28,6 +28,14 @@
2828

2929
#define MILLIS_RECONNECT 5000
3030

31+
#define HOST_CONNECTION_DISCONNECTED 0
32+
#define HOST_CONNECTION_CONNECTED 1
33+
34+
extern "C" {
35+
// callback function types
36+
typedef void (*hostConnectionCallbackFunction)(byte);
37+
}
38+
3139
class EthernetClientStream : public Stream
3240
{
3341
public:
@@ -38,6 +46,7 @@ class EthernetClientStream : public Stream
3846
void flush();
3947
size_t write(uint8_t);
4048
void maintain(IPAddress localip);
49+
void attach(hostConnectionCallbackFunction newFunction);
4150

4251
private:
4352
Client &client;
@@ -47,6 +56,7 @@ class EthernetClientStream : public Stream
4756
uint16_t port;
4857
bool connected;
4958
uint32_t time_connect;
59+
hostConnectionCallbackFunction currentHostConnectionCallback;
5060
bool maintain();
5161
void stop();
5262
};
@@ -64,6 +74,7 @@ EthernetClientStream::EthernetClientStream(Client &client, IPAddress localip, IP
6474
host(host),
6575
port(port),
6676
connected(false)
77+
, currentHostConnectionCallback(nullptr)
6778
{
6879
}
6980

@@ -112,10 +123,20 @@ void
112123
EthernetClientStream::stop()
113124
{
114125
client.stop();
126+
if (currentHostConnectionCallback)
127+
{
128+
(*currentHostConnectionCallback)(HOST_CONNECTION_DISCONNECTED);
129+
}
115130
connected = false;
116131
time_connect = millis();
117132
}
118133

134+
void
135+
EthernetClientStream::attach(hostConnectionCallbackFunction newFunction)
136+
{
137+
currentHostConnectionCallback = newFunction;
138+
}
139+
119140
bool
120141
EthernetClientStream::maintain()
121142
{
@@ -133,6 +154,10 @@ EthernetClientStream::maintain()
133154
DEBUG_PRINTLN("Connection failed. Attempting to reconnect...");
134155
} else {
135156
DEBUG_PRINTLN("Connected");
157+
if (currentHostConnectionCallback)
158+
{
159+
(*currentHostConnectionCallback)(HOST_CONNECTION_CONNECTED);
160+
}
136161
}
137162
}
138163
return connected;

0 commit comments

Comments
 (0)