diff --git a/examples/StandardFirmataEthernet/StandardFirmataEthernet.ino b/examples/StandardFirmataEthernet/StandardFirmataEthernet.ino index f512ccea..ff1409dd 100644 --- a/examples/StandardFirmataEthernet/StandardFirmataEthernet.ino +++ b/examples/StandardFirmataEthernet/StandardFirmataEthernet.ino @@ -20,7 +20,7 @@ See file LICENSE.txt for further informations on licensing terms. - Last updated August 17th, 2017 + Last updated March 10th, 2020 */ /* @@ -832,6 +832,26 @@ void systemResetCallback() isResetting = false; } +#ifdef ETHERNETCLIENTSTREAM_H +/* + * Called when a TCP connection is either connected or disconnected. + * TODO: + * - report connected or reconnected state to host (to be added to protocol) + * - report current state to host (to be added to protocol) + */ +void hostConnectionCallback(byte state) +{ + switch (state) { + case HOST_CONNECTION_CONNECTED: + DEBUG_PRINTLN( "TCP connection established" ); + break; + case HOST_CONNECTION_DISCONNECTED: + DEBUG_PRINTLN( "TCP connection disconnected" ); + break; + } +} +#endif + void printEthernetStatus() { DEBUG_PRINT("Local IP Address: "); @@ -873,6 +893,10 @@ void ignorePins() void initTransport() { +#ifdef ETHERNETCLIENTSTREAM_H + stream.attach(hostConnectionCallback); +#endif + #ifdef YUN_ETHERNET Bridge.begin(); #else diff --git a/utility/EthernetClientStream.h b/utility/EthernetClientStream.h index 1b7d2e29..73ed5ca4 100644 --- a/utility/EthernetClientStream.h +++ b/utility/EthernetClientStream.h @@ -14,7 +14,7 @@ See file LICENSE.txt for further informations on licensing terms. - Last updated June 18th, 2016 + Last updated March 10th, 2020 */ #ifndef ETHERNETCLIENTSTREAM_H @@ -28,6 +28,14 @@ #define MILLIS_RECONNECT 5000 +#define HOST_CONNECTION_DISCONNECTED 0 +#define HOST_CONNECTION_CONNECTED 1 + +extern "C" { + // callback function types + typedef void (*hostConnectionCallbackFunction)(byte); +} + class EthernetClientStream : public Stream { public: @@ -38,6 +46,7 @@ class EthernetClientStream : public Stream void flush(); size_t write(uint8_t); void maintain(IPAddress localip); + void attach(hostConnectionCallbackFunction newFunction); private: Client &client; @@ -47,6 +56,7 @@ class EthernetClientStream : public Stream uint16_t port; bool connected; uint32_t time_connect; + hostConnectionCallbackFunction currentHostConnectionCallback; bool maintain(); void stop(); }; @@ -64,6 +74,7 @@ EthernetClientStream::EthernetClientStream(Client &client, IPAddress localip, IP host(host), port(port), connected(false) + , currentHostConnectionCallback(nullptr) { } @@ -112,10 +123,20 @@ void EthernetClientStream::stop() { client.stop(); + if (currentHostConnectionCallback) + { + (*currentHostConnectionCallback)(HOST_CONNECTION_DISCONNECTED); + } connected = false; time_connect = millis(); } +void +EthernetClientStream::attach(hostConnectionCallbackFunction newFunction) +{ + currentHostConnectionCallback = newFunction; +} + bool EthernetClientStream::maintain() { @@ -133,6 +154,10 @@ EthernetClientStream::maintain() DEBUG_PRINTLN("Connection failed. Attempting to reconnect..."); } else { DEBUG_PRINTLN("Connected"); + if (currentHostConnectionCallback) + { + (*currentHostConnectionCallback)(HOST_CONNECTION_CONNECTED); + } } } return connected;