From 30ca7786e79bec9fb2969a811f50ee4411d2abbc Mon Sep 17 00:00:00 2001 From: Sergei Kotlyachkov Date: Wed, 20 Jun 2018 21:12:16 -0400 Subject: [PATCH 1/5] remove duplicates (#2) --- examples/FirebaseRoom_ESP8266/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/FirebaseRoom_ESP8266/README.md b/examples/FirebaseRoom_ESP8266/README.md index 7f1620db..37a38512 100644 --- a/examples/FirebaseRoom_ESP8266/README.md +++ b/examples/FirebaseRoom_ESP8266/README.md @@ -58,7 +58,7 @@ sensor data to Firebase, and trigger actuators from Firebase. - Add Grove modules to modules interactions. - Connect other Grove modules to the room and submit new [PRs](https://github.com/googlesamples/firebase-arduino/pulls) - Reduce the number of Firebase API calls using `FirebaseObject` or `FirebaseStream`. -- Watch or star the [GitHub repo repo](https://github.com/googlesamples/firebase-arduino) +- Watch or star the [GitHub repo](https://github.com/googlesamples/firebase-arduino) - Give [feedback](https://gitter.im/googlesamples/firebase-arduino) - Report [bugs](https://github.com/googlesamples/firebase-arduino/issues/new) - [Fork](https://github.com/googlesamples/firebase-arduino#fork-destination-box) and [contribute](https://github.com/googlesamples/firebase-arduino/blob/master/CONTRIBUTING.md) From 2347c48eddae069df4b43612c94dfdc17d736cbe Mon Sep 17 00:00:00 2001 From: Sergei Kotlyachkov Date: Wed, 20 Jun 2018 21:18:31 -0400 Subject: [PATCH 2/5] Update README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dc9420f8..b1d537ce 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,10 @@ The Arduino library is [under heavy development](https://github.com/googlesample - [FirebaseArduino API Reference](http://firebase-arduino.readthedocs.io/) ## Dependencies -- FirebaseArduino now depends on [ArduinoJson library](https://github.com/bblanchon/ArduinoJson) instead of containing it's own version of it. Please either use Library Manager or download specific version of the library from github. +- FirebaseArduino now depends on [ArduinoJson library](https://github.com/bblanchon/ArduinoJson) instead of containing it's own version of it. Please either use Library Manager or download specific version of the library from github. We recommend that ArduinoJson is at least version [5.13.1](https://github.com/bblanchon/ArduinoJson/tree/v5.13.1) + +- ESP8266 Core SDK. We recommend using officially tagged releases and it should be at least [2.4.1] +(https://github.com/esp8266/Arduino/tree/2.4.1) ## Disclaimer From 2f76fae7baf9b088fd931637ef604282c341ea43 Mon Sep 17 00:00:00 2001 From: Sergei Kotlyachkov Date: Wed, 20 Jun 2018 21:19:13 -0400 Subject: [PATCH 3/5] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index b1d537ce..827cf7ec 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,7 @@ The Arduino library is [under heavy development](https://github.com/googlesample ## Dependencies - FirebaseArduino now depends on [ArduinoJson library](https://github.com/bblanchon/ArduinoJson) instead of containing it's own version of it. Please either use Library Manager or download specific version of the library from github. We recommend that ArduinoJson is at least version [5.13.1](https://github.com/bblanchon/ArduinoJson/tree/v5.13.1) -- ESP8266 Core SDK. We recommend using officially tagged releases and it should be at least [2.4.1] -(https://github.com/esp8266/Arduino/tree/2.4.1) +- ESP8266 Core SDK. We recommend using officially tagged releases and it should be at least [2.4.1](https://github.com/esp8266/Arduino/tree/2.4.1) ## Disclaimer From 88170c8110c02ff4d12128ba09ec6351c19b93d1 Mon Sep 17 00:00:00 2001 From: Sergei Kotlyachkov Date: Wed, 20 Jun 2018 21:49:33 -0400 Subject: [PATCH 4/5] Manually merge: "Report error when streaming loses connection. #179" --- .../test/dummies/FirebaseHttpClient_dummy.cpp | 4 + src/FirebaseArduino.cpp | 5 + src/FirebaseError.h | 8 ++ src/FirebaseHttpClient.h | 2 + src/FirebaseHttpClient.h~ | 45 +++++++++ src/FirebaseHttpClient_Esp8266.cpp | 4 + src/FirebaseHttpClient_Esp8266.cpp~ | 97 +++++++++++++++++++ 7 files changed, 165 insertions(+) create mode 100644 src/FirebaseHttpClient.h~ create mode 100644 src/FirebaseHttpClient_Esp8266.cpp~ diff --git a/contrib/test/dummies/FirebaseHttpClient_dummy.cpp b/contrib/test/dummies/FirebaseHttpClient_dummy.cpp index 571fa842..2a89ff90 100644 --- a/contrib/test/dummies/FirebaseHttpClient_dummy.cpp +++ b/contrib/test/dummies/FirebaseHttpClient_dummy.cpp @@ -23,6 +23,10 @@ class FirebaseHttpClientDummy : public FirebaseHttpClient { void addHeader(const std::string& UNUSED_ARG(name), const std::string& UNUSED_ARG(value)) override { } + bool connected() override { + return true; + } + void collectHeaders(const char* UNUSED_ARG(header_keys[]), const int UNUSED_ARG(count)) override { } diff --git a/src/FirebaseArduino.cpp b/src/FirebaseArduino.cpp index 4ab93f0e..0e7ae87d 100644 --- a/src/FirebaseArduino.cpp +++ b/src/FirebaseArduino.cpp @@ -156,6 +156,11 @@ void FirebaseArduino::stream(const String& path) { bool FirebaseArduino::available() { if (stream_http_.get() == nullptr) { + error_ = FirebaseError(FIREBASE_ERROR_CODES::STREAM_NOT_INITIALIZED, "HTTP stream is not initialized"); + return 0; + } + if (!stream_http_.get()->connected()) { + error_ = FirebaseError(FIREBASE_ERROR_CODES::HTTP_CONNECTION_LOST, "Connection Lost"); return 0; } auto client = stream_http_.get()->getStreamPtr(); diff --git a/src/FirebaseError.h b/src/FirebaseError.h index 2a088aa9..422f9193 100644 --- a/src/FirebaseError.h +++ b/src/FirebaseError.h @@ -1,6 +1,14 @@ #ifndef firebase_error_h #define firebase_error_h + +// These error codes are used in addition to regular HTTP error codes. +// Same error space is shared between HTTP errors and these values. +enum FIREBASE_ERROR_CODES { + HTTP_CONNECTION_LOST = -5, + STREAM_NOT_INITIALIZED = -6 +}; + class FirebaseError { public: // Make it explicit that the empty constructor mean no error. diff --git a/src/FirebaseHttpClient.h b/src/FirebaseHttpClient.h index 055699c7..300c3d28 100644 --- a/src/FirebaseHttpClient.h +++ b/src/FirebaseHttpClient.h @@ -33,6 +33,8 @@ class FirebaseHttpClient { virtual std::string errorToString(int error_code) = 0; + virtual bool connected() = 0; + protected: static const uint16_t kFirebasePort = 443; }; diff --git a/src/FirebaseHttpClient.h~ b/src/FirebaseHttpClient.h~ new file mode 100644 index 00000000..1b8aed1f --- /dev/null +++ b/src/FirebaseHttpClient.h~ @@ -0,0 +1,45 @@ +#ifndef FIREBASE_HTTP_CLIENT_H +#define FIREBASE_HTTP_CLIENT_H + +#include + +#include "Arduino.h" +#include "Stream.h" + +struct HttpStatus { + static const int TEMPORARY_REDIRECT = 307; +}; + +class FirebaseHttpClient { + public: + static FirebaseHttpClient* create(); + + virtual void setReuseConnection(bool reuse) = 0; + virtual void begin(const std::string& url) = 0; + virtual void begin(const std::string& host, const std::string& path) = 0; + + virtual void end() = 0; + + virtual void addHeader(const std::string& name, const std::string& value) = 0; + virtual void collectHeaders(const char* header_keys[], + const int header_key_count) = 0; + virtual std::string header(const std::string& name) = 0; + + virtual int sendRequest(const std::string& method, const std::string& data) = 0; + + virtual std::string getString() = 0; + + virtual Stream* getStreamPtr() = 0; + + virtual std::string errorToString(int error_code) = 0; + + virtual bool connected() = 0; + + protected: + static const uint16_t kFirebasePort = 443; +}; + +static const char kFirebaseFingerprint[] = + "B8 4F 40 70 0C 63 90 E0 07 E8 7D BD B4 11 D0 4A EA 9C 90 F6"; + +#endif // FIREBASE_HTTP_CLIENT_H diff --git a/src/FirebaseHttpClient_Esp8266.cpp b/src/FirebaseHttpClient_Esp8266.cpp index 92dbe92d..c78590dc 100644 --- a/src/FirebaseHttpClient_Esp8266.cpp +++ b/src/FirebaseHttpClient_Esp8266.cpp @@ -83,6 +83,10 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient { return HTTPClient::errorToString(error_code).c_str(); } + bool connected() override { + return http_.connected(); + } + private: ForceReuseHTTPClient http_; }; diff --git a/src/FirebaseHttpClient_Esp8266.cpp~ b/src/FirebaseHttpClient_Esp8266.cpp~ new file mode 100644 index 00000000..b2e2362a --- /dev/null +++ b/src/FirebaseHttpClient_Esp8266.cpp~ @@ -0,0 +1,97 @@ + +#include "FirebaseHttpClient.h" + +#include + +// The ordering of these includes matters greatly. +#include +#include +#include + +// Detect whether stable version of HTTP library is installed instead of +// master branch and patch in missing status and methods. +#ifndef HTTP_CODE_TEMPORARY_REDIRECT +#define HTTP_CODE_TEMPORARY_REDIRECT 307 +#define USE_ESP_ARDUINO_CORE_2_0_0 +#endif + +// Firebase now returns `Connection: close` after REST streaming redirection. +// +// Override the built-in ESP8266HTTPClient to *not* close the +// connection if forceReuse it set to `true`. +class ForceReuseHTTPClient : public HTTPClient { +public: + void end() { + if (_forceReuse) { + _canReuse = true; + } + HTTPClient::end(); + } + void forceReuse(bool forceReuse) { + _forceReuse = forceReuse; + } +protected: + bool _forceReuse = false; +}; + +class FirebaseHttpClientEsp8266 : public FirebaseHttpClient { + public: + FirebaseHttpClientEsp8266() {} + + void setReuseConnection(bool reuse) override { + http_.setReuse(reuse); + http_.forceReuse(reuse); + } + + void begin(const std::string& url) override { + http_.begin(url.c_str(), kFirebaseFingerprint); + } + + void begin(const std::string& host, const std::string& path) override { + http_.begin(host.c_str(), kFirebasePort, path.c_str(), kFirebaseFingerprint); + } + + void end() override { + http_.end(); + } + + void addHeader(const std::string& name, const std::string& value) override { + http_.addHeader(name.c_str(), value.c_str()); + } + + void collectHeaders(const char* header_keys[], const int count) override { + http_.collectHeaders(header_keys, count); + } + + std::string header(const std::string& name) override { + return http_.header(name.c_str()).c_str(); + } + + int sendRequest(const std::string& method, const std::string& data) override { + return http_.sendRequest(method.c_str(), (uint8_t*)data.c_str(), data.length()); + } + + std::string getString() override { + return http_.getString().c_str(); + } + + Stream* getStreamPtr() override { + return http_.getStreamPtr(); + } + + std::string errorToString(int error_code) override { + return HTTPClient::errorToString(error_code).c_str(); + } + + bool connected() override { + return http_.connected(); + } + + private: + ForceReuseHTTPClient http_; +}; + +FirebaseHttpClient* FirebaseHttpClient::create() { + return new FirebaseHttpClientEsp8266(); +} + From 9dcf20586b00d44332bbbd59ff8cbf20826fb939 Mon Sep 17 00:00:00 2001 From: Sergei Kotlyachkov Date: Wed, 20 Jun 2018 21:50:22 -0400 Subject: [PATCH 5/5] Removed temp files --- src/FirebaseHttpClient.h~ | 45 ------------- src/FirebaseHttpClient_Esp8266.cpp~ | 97 ----------------------------- 2 files changed, 142 deletions(-) delete mode 100644 src/FirebaseHttpClient.h~ delete mode 100644 src/FirebaseHttpClient_Esp8266.cpp~ diff --git a/src/FirebaseHttpClient.h~ b/src/FirebaseHttpClient.h~ deleted file mode 100644 index 1b8aed1f..00000000 --- a/src/FirebaseHttpClient.h~ +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef FIREBASE_HTTP_CLIENT_H -#define FIREBASE_HTTP_CLIENT_H - -#include - -#include "Arduino.h" -#include "Stream.h" - -struct HttpStatus { - static const int TEMPORARY_REDIRECT = 307; -}; - -class FirebaseHttpClient { - public: - static FirebaseHttpClient* create(); - - virtual void setReuseConnection(bool reuse) = 0; - virtual void begin(const std::string& url) = 0; - virtual void begin(const std::string& host, const std::string& path) = 0; - - virtual void end() = 0; - - virtual void addHeader(const std::string& name, const std::string& value) = 0; - virtual void collectHeaders(const char* header_keys[], - const int header_key_count) = 0; - virtual std::string header(const std::string& name) = 0; - - virtual int sendRequest(const std::string& method, const std::string& data) = 0; - - virtual std::string getString() = 0; - - virtual Stream* getStreamPtr() = 0; - - virtual std::string errorToString(int error_code) = 0; - - virtual bool connected() = 0; - - protected: - static const uint16_t kFirebasePort = 443; -}; - -static const char kFirebaseFingerprint[] = - "B8 4F 40 70 0C 63 90 E0 07 E8 7D BD B4 11 D0 4A EA 9C 90 F6"; - -#endif // FIREBASE_HTTP_CLIENT_H diff --git a/src/FirebaseHttpClient_Esp8266.cpp~ b/src/FirebaseHttpClient_Esp8266.cpp~ deleted file mode 100644 index b2e2362a..00000000 --- a/src/FirebaseHttpClient_Esp8266.cpp~ +++ /dev/null @@ -1,97 +0,0 @@ - -#include "FirebaseHttpClient.h" - -#include - -// The ordering of these includes matters greatly. -#include -#include -#include - -// Detect whether stable version of HTTP library is installed instead of -// master branch and patch in missing status and methods. -#ifndef HTTP_CODE_TEMPORARY_REDIRECT -#define HTTP_CODE_TEMPORARY_REDIRECT 307 -#define USE_ESP_ARDUINO_CORE_2_0_0 -#endif - -// Firebase now returns `Connection: close` after REST streaming redirection. -// -// Override the built-in ESP8266HTTPClient to *not* close the -// connection if forceReuse it set to `true`. -class ForceReuseHTTPClient : public HTTPClient { -public: - void end() { - if (_forceReuse) { - _canReuse = true; - } - HTTPClient::end(); - } - void forceReuse(bool forceReuse) { - _forceReuse = forceReuse; - } -protected: - bool _forceReuse = false; -}; - -class FirebaseHttpClientEsp8266 : public FirebaseHttpClient { - public: - FirebaseHttpClientEsp8266() {} - - void setReuseConnection(bool reuse) override { - http_.setReuse(reuse); - http_.forceReuse(reuse); - } - - void begin(const std::string& url) override { - http_.begin(url.c_str(), kFirebaseFingerprint); - } - - void begin(const std::string& host, const std::string& path) override { - http_.begin(host.c_str(), kFirebasePort, path.c_str(), kFirebaseFingerprint); - } - - void end() override { - http_.end(); - } - - void addHeader(const std::string& name, const std::string& value) override { - http_.addHeader(name.c_str(), value.c_str()); - } - - void collectHeaders(const char* header_keys[], const int count) override { - http_.collectHeaders(header_keys, count); - } - - std::string header(const std::string& name) override { - return http_.header(name.c_str()).c_str(); - } - - int sendRequest(const std::string& method, const std::string& data) override { - return http_.sendRequest(method.c_str(), (uint8_t*)data.c_str(), data.length()); - } - - std::string getString() override { - return http_.getString().c_str(); - } - - Stream* getStreamPtr() override { - return http_.getStreamPtr(); - } - - std::string errorToString(int error_code) override { - return HTTPClient::errorToString(error_code).c_str(); - } - - bool connected() override { - return http_.connected(); - } - - private: - ForceReuseHTTPClient http_; -}; - -FirebaseHttpClient* FirebaseHttpClient::create() { - return new FirebaseHttpClientEsp8266(); -} -