From ab533c3b71562e861b27cb7c97dcb07b10b6ff19 Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Thu, 26 May 2016 13:14:51 -0700 Subject: [PATCH 01/11] Switched Firebase to std::string and fixed all unit tests --- src/Firebase.cpp | 70 ++++++++++----------- src/Firebase.h | 76 +++++++++++------------ src/FirebaseObject.h | 99 ------------------------------ src/modem/begin-command.cpp | 2 +- src/modem/get-command.cpp | 4 +- src/modem/remove-command.cpp | 4 +- src/modem/set-command.cpp | 6 +- test/mock-firebase.h | 18 +++--- test/modem/get-command_test.cpp | 2 +- test/modem/remove-command_test.cpp | 2 +- test/modem/stream-command_test.cpp | 6 +- 11 files changed, 95 insertions(+), 194 deletions(-) delete mode 100644 src/FirebaseObject.h diff --git a/src/Firebase.cpp b/src/Firebase.cpp index bc392750..cece4312 100644 --- a/src/Firebase.cpp +++ b/src/Firebase.cpp @@ -18,8 +18,8 @@ using std::unique_ptr; namespace { -String makeFirebaseURL(const String& path, const String& auth) { - String url; +std::string makeFirebaseURL(const std::string& path, const std::string& auth) { + std::string url; if (path[0] != '/') { url = "/"; } @@ -32,71 +32,71 @@ String makeFirebaseURL(const String& path, const String& auth) { } // namespace -Firebase::Firebase(const String& host, const String& auth) : host_(host), auth_(auth) { +Firebase::Firebase(const std::string& host, const std::string& auth) : host_(host), auth_(auth) { http_.reset(FirebaseHttpClient::create()); http_->setReuseConnection(true); } -const String& Firebase::auth() const { +const std::string& Firebase::auth() const { return auth_; } -FirebaseGet Firebase::get(const String& path) { +FirebaseGet Firebase::get(const std::string& path) { return FirebaseGet(host_, auth_, path, http_.get()); } -unique_ptr Firebase::getPtr(const String& path) { +unique_ptr Firebase::getPtr(const std::string& path) { return unique_ptr(new FirebaseGet(host_, auth_, path, http_.get())); } -FirebaseSet Firebase::set(const String& path, const String& value) { +FirebaseSet Firebase::set(const std::string& path, const std::string& value) { return FirebaseSet(host_, auth_, path, value, http_.get()); } -unique_ptr Firebase::setPtr(const String& path, - const String& value) { +unique_ptr Firebase::setPtr(const std::string& path, + const std::string& value) { return unique_ptr( new FirebaseSet(host_, auth_, path, value, http_.get())); } -FirebasePush Firebase::push(const String& path, const String& value) { +FirebasePush Firebase::push(const std::string& path, const std::string& value) { return FirebasePush(host_, auth_, path, value, http_.get()); } -unique_ptr Firebase::pushPtr(const String& path, const String& value) { +unique_ptr Firebase::pushPtr(const std::string& path, const std::string& value) { return unique_ptr( new FirebasePush(host_, auth_, path, value, http_.get())); } -FirebaseRemove Firebase::remove(const String& path) { +FirebaseRemove Firebase::remove(const std::string& path) { return FirebaseRemove(host_, auth_, path, http_.get()); } -unique_ptr Firebase::removePtr(const String& path) { +unique_ptr Firebase::removePtr(const std::string& path) { return unique_ptr( new FirebaseRemove(host_, auth_, path, http_.get())); } -FirebaseStream Firebase::stream(const String& path) { +FirebaseStream Firebase::stream(const std::string& path) { // TODO: create new client dedicated to stream. return FirebaseStream(host_, auth_, path, http_.get()); } -unique_ptr Firebase::streamPtr(const String& path) { +unique_ptr Firebase::streamPtr(const std::string& path) { // TODO: create new client dedicated to stream. return unique_ptr( new FirebaseStream(host_, auth_, path, http_.get())); } // FirebaseCall -FirebaseCall::FirebaseCall(const String& host, const String& auth, - const char* method, const String& path, - const String& data, FirebaseHttpClient* http) : http_(http) { - String path_with_auth = makeFirebaseURL(path, auth); +FirebaseCall::FirebaseCall(const std::string& host, const std::string& auth, + const char* method, const std::string& path, + const std::string& data, FirebaseHttpClient* http) : http_(http) { + std::string path_with_auth = makeFirebaseURL(path, auth); http_->setReuseConnection(true); http_->begin(host, path_with_auth); bool followRedirect = false; - if (String(method) == "STREAM") { + if (std::string(method) == "STREAM") { method = "GET"; http_->addHeader("Accept", "text/event-stream"); followRedirect = true; @@ -112,18 +112,18 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth, // TODO: Add a max redirect check if (followRedirect) { while (status == HttpStatus::TEMPORARY_REDIRECT) { - String location = http_->header("Location"); + std::string location = http_->header("Location"); http_->setReuseConnection(false); http_->end(); http_->setReuseConnection(true); http_->begin(location); - status = http_->sendRequest("GET", String()); + status = http_->sendRequest("GET", std::string()); } } if (status != 200) { error_ = FirebaseError(status, - String(method) + " " + path_with_auth + + std::string(method) + " " + path_with_auth + ": " + http_->errorToString(status)); } @@ -141,15 +141,15 @@ const JsonObject& FirebaseCall::json() { } // FirebaseGet -FirebaseGet::FirebaseGet(const String& host, const String& auth, - const String& path, +FirebaseGet::FirebaseGet(const std::string& host, const std::string& auth, + const std::string& path, FirebaseHttpClient* http) : FirebaseCall(host, auth, "GET", path, "", http) { } // FirebaseSet -FirebaseSet::FirebaseSet(const String& host, const String& auth, - const String& path, const String& value, +FirebaseSet::FirebaseSet(const std::string& host, const std::string& auth, + const std::string& path, const std::string& value, FirebaseHttpClient* http) : FirebaseCall(host, auth, "PUT", path, value, http) { if (!error()) { @@ -158,8 +158,8 @@ FirebaseSet::FirebaseSet(const String& host, const String& auth, } } // FirebasePush -FirebasePush::FirebasePush(const String& host, const String& auth, - const String& path, const String& value, +FirebasePush::FirebasePush(const std::string& host, const std::string& auth, + const std::string& path, const std::string& value, FirebaseHttpClient* http) : FirebaseCall(host, auth, "POST", path, value, http) { if (!error()) { @@ -168,15 +168,15 @@ FirebasePush::FirebasePush(const String& host, const String& auth, } // FirebasePush -FirebaseRemove::FirebaseRemove(const String& host, const String& auth, - const String& path, +FirebaseRemove::FirebaseRemove(const std::string& host, const std::string& auth, + const std::string& path, FirebaseHttpClient* http) : FirebaseCall(host, auth, "DELETE", path, "", http) { } // FirebaseStream -FirebaseStream::FirebaseStream(const String& host, const String& auth, - const String& path, +FirebaseStream::FirebaseStream(const std::string& host, const std::string& auth, + const std::string& path, FirebaseHttpClient* http) : FirebaseCall(host, auth, "STREAM", path, "", http) { } @@ -185,10 +185,10 @@ bool FirebaseStream::available() { return http_->getStreamPtr()->available(); } -FirebaseStream::Event FirebaseStream::read(String& event) { +FirebaseStream::Event FirebaseStream::read(std::string& event) { auto client = http_->getStreamPtr(); Event type; - String typeStr = client->readStringUntil('\n').substring(7); + std::string typeStr = client->readStringUntil('\n').substring(7); if (typeStr == "put") { type = Event::PUT; } else if (typeStr == "patch") { diff --git a/src/Firebase.h b/src/Firebase.h index df214dd2..81875e23 100644 --- a/src/Firebase.h +++ b/src/Firebase.h @@ -37,29 +37,29 @@ class FirebaseStream; // Firebase REST API client. class Firebase { public: - Firebase(const String& host, const String& auth = ""); + Firebase(const std::string& host, const std::string& auth = ""); - const String& auth() const; + const std::string& auth() const; // Fetch json encoded `value` at `path`. - FirebaseGet get(const String& path); - virtual std::unique_ptr getPtr(const String& path); + FirebaseGet get(const std::string& path); + virtual std::unique_ptr getPtr(const std::string& path); // Set json encoded `value` at `path`. - FirebaseSet set(const String& path, const String& json); - virtual std::unique_ptr setPtr(const String& path, const String& json); + FirebaseSet set(const std::string& path, const std::string& json); + virtual std::unique_ptr setPtr(const std::string& path, const std::string& json); // Add new json encoded `value` to list at `path`. - FirebasePush push(const String& path, const String& json); - virtual std::unique_ptr pushPtr(const String& path, const String& json); + FirebasePush push(const std::string& path, const std::string& json); + virtual std::unique_ptr pushPtr(const std::string& path, const std::string& json); // Delete value at `path`. - FirebaseRemove remove(const String& path); - virtual std::unique_ptr removePtr(const String& path); + FirebaseRemove remove(const std::string& path); + virtual std::unique_ptr removePtr(const std::string& path); // Start a stream of events that affect value at `path`. - FirebaseStream stream(const String& path); - virtual std::unique_ptr streamPtr(const String& path); + FirebaseStream stream(const std::string& path); + virtual std::unique_ptr streamPtr(const std::string& path); protected: // Used for testing. @@ -67,29 +67,29 @@ class Firebase { private: std::unique_ptr http_; - String host_; - String auth_; + std::string host_; + std::string auth_; }; class FirebaseError { public: FirebaseError() {} - FirebaseError(int code, const String& message) : code_(code), message_(message) { + FirebaseError(int code, const std::string& message) : code_(code), message_(message) { } operator bool() const { return code_ != 0; } int code() const { return code_; } - const String& message() const { return message_; } + const std::string& message() const { return message_; } private: int code_ = 0; - String message_ = ""; + std::string message_ = ""; }; class FirebaseCall { public: FirebaseCall() {} - FirebaseCall(const String& host, const String& auth, - const char* method, const String& path, - const String& data = "", + FirebaseCall(const std::string& host, const std::string& auth, + const char* method, const std::string& path, + const std::string& data = "", FirebaseHttpClient* http = NULL); virtual ~FirebaseCall() {} @@ -97,7 +97,7 @@ class FirebaseCall { return error_; } - virtual const String& response() const { + virtual const std::string& response() const { return response_; } @@ -106,59 +106,59 @@ class FirebaseCall { protected: FirebaseHttpClient* http_; FirebaseError error_; - String response_; + std::string response_; DynamicJsonBuffer buffer_; }; class FirebaseGet : public FirebaseCall { public: FirebaseGet() {} - FirebaseGet(const String& host, const String& auth, - const String& path, FirebaseHttpClient* http = NULL); + FirebaseGet(const std::string& host, const std::string& auth, + const std::string& path, FirebaseHttpClient* http = NULL); private: - String json_; + std::string json_; }; class FirebaseSet: public FirebaseCall { public: FirebaseSet() {} - FirebaseSet(const String& host, const String& auth, - const String& path, const String& value, FirebaseHttpClient* http = NULL); + FirebaseSet(const std::string& host, const std::string& auth, + const std::string& path, const std::string& value, FirebaseHttpClient* http = NULL); private: - String json_; + std::string json_; }; class FirebasePush : public FirebaseCall { public: FirebasePush() {} - FirebasePush(const String& host, const String& auth, - const String& path, const String& value, FirebaseHttpClient* http = NULL); + FirebasePush(const std::string& host, const std::string& auth, + const std::string& path, const std::string& value, FirebaseHttpClient* http = NULL); virtual ~FirebasePush() {} - virtual const String& name() const { + virtual const std::string& name() const { return name_; } private: - String name_; + std::string name_; }; class FirebaseRemove : public FirebaseCall { public: FirebaseRemove() {} - FirebaseRemove(const String& host, const String& auth, - const String& path, FirebaseHttpClient* http = NULL); + FirebaseRemove(const std::string& host, const std::string& auth, + const std::string& path, FirebaseHttpClient* http = NULL); }; class FirebaseStream : public FirebaseCall { public: FirebaseStream() {} - FirebaseStream(const String& host, const String& auth, - const String& path, FirebaseHttpClient* http = NULL); + FirebaseStream(const std::string& host, const std::string& auth, + const std::string& path, FirebaseHttpClient* http = NULL); virtual ~FirebaseStream() {} // Return if there is any event available to read. @@ -171,7 +171,7 @@ class FirebaseStream : public FirebaseCall { PATCH }; - static inline String EventToName(Event event) { + static inline std::string EventToName(Event event) { switch(event) { case UNKNOWN: return "UNKNOWN"; @@ -185,7 +185,7 @@ class FirebaseStream : public FirebaseCall { } // Read next json encoded `event` from stream. - virtual Event read(String& event); + virtual Event read(std::string& event); const FirebaseError& error() const { return _error; diff --git a/src/FirebaseObject.h b/src/FirebaseObject.h deleted file mode 100644 index 8233bbde..00000000 --- a/src/FirebaseObject.h +++ /dev/null @@ -1,99 +0,0 @@ -// -// Copyright 2015 Google Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -#ifndef FIREBASE_OBJECT_H -#define FIREBASE_OBJECT_H - -#include "third-party/arduino-json-5.3/include/ArduinoJson.h" - -#ifndef FIREBASE_JSONBUFFER_SIZE -#define FIREBASE_JSONBUFFER_SIZE JSON_OBJECT_SIZE(32) -#endif // FIREBASE_JSONBUFFER_SIZE - -/** - * Represents value stored in firebase, may be a singular value (leaf node) or - * a tree structure. - */ -class FirebaseObject { - public: - /** - * Construct from json. - * \param data JSON formatted string. - */ - FirebaseObject(const String& data); - - /** - * Return the value as a boolean. - * \param optional path in the JSON object. - * \return result as a bool. - */ - bool getBool(const String& path = ""); - - /** - * Return the value as an int. - * \param optional path in the JSON object. - * \return result as an integer. - */ - int getInt(const String& path = ""); - - /** - * Return the value as a float. - * \param optional path in the JSON object. - * \return result as a float. - */ - float getFloat(const String& path = ""); - - /** - * Return the value as a String. - * \param optional path in the JSON object. - * \return result as a String. - */ - String getString(const String& path = ""); - - /** - * Return the value as a JsonVariant. - * \param optional path in the JSON object. - * \return result as a JsonVariant. - */ - JsonVariant getJsonVariant(const String& path = ""); - - - /** - * - * \return Whether there was an error decoding or accessing the JSON object. - */ - bool success() const; - - /** - * - * \return Whether there was an error decoding or accessing the JSON object. - */ - bool failed() const; - - /** - * - * \return Error message if failed() is true. - */ - const String& error() const; - - private: - String data_; - StaticJsonBuffer buffer_; - JsonVariant json_; - String error_; -}; - -#endif // FIREBASE_OBJECT_H diff --git a/src/modem/begin-command.cpp b/src/modem/begin-command.cpp index 97de7a09..ddcfb12c 100644 --- a/src/modem/begin-command.cpp +++ b/src/modem/begin-command.cpp @@ -33,7 +33,7 @@ bool BeginCommand::execute(const String& command, return false; } - new_firebase_.reset(new Firebase(host, auth)); + new_firebase_.reset(new Firebase(host.c_str(), auth.c_str())); out->println("+OK"); return true; diff --git a/src/modem/get-command.cpp b/src/modem/get-command.cpp index 0ee19299..62fd071e 100644 --- a/src/modem/get-command.cpp +++ b/src/modem/get-command.cpp @@ -18,11 +18,11 @@ bool GetCommand::execute(const String& command, if (get->error()) { out->print("-FAIL "); - out->println(get->error().message()); + out->println(get->error().message().c_str()); return false; } - String value(get->response()); + String value(get->response().c_str()); // TODO implement json parsing to pull and process value. out->print("+"); out->println(value); diff --git a/src/modem/remove-command.cpp b/src/modem/remove-command.cpp index 5026d25b..d8c4765c 100644 --- a/src/modem/remove-command.cpp +++ b/src/modem/remove-command.cpp @@ -14,11 +14,11 @@ bool RemoveCommand::execute(const String& command, } String path = in->readLine(); - std::unique_ptr get(fbase().removePtr(path)); + std::unique_ptr get(fbase().removePtr(path.c_str())); if (get->error()) { out->print("-FAIL "); - out->println(get->error().message()); + out->println(get->error().message().c_str()); return false; } diff --git a/src/modem/set-command.cpp b/src/modem/set-command.cpp index 35b53040..1fff899c 100644 --- a/src/modem/set-command.cpp +++ b/src/modem/set-command.cpp @@ -17,12 +17,12 @@ bool SetCommand::execute(const String& command, String path(in->readStringUntil(' ')); String data(in->readLine()); - std::unique_ptr set(fbase().setPtr(path, - EncodeForJson(data))); + std::unique_ptr set(fbase().setPtr(path.c_str(), + EncodeForJson(data).c_str())); if (set->error()) { out->print("-FAIL "); - out->println(set->error().message()); + out->println(set->error().message().c_str()); return false; } else { out->println("+OK"); diff --git a/test/mock-firebase.h b/test/mock-firebase.h index 3d788e69..92f1e9fa 100644 --- a/test/mock-firebase.h +++ b/test/mock-firebase.h @@ -10,28 +10,28 @@ namespace modem { class MockFirebase : public Firebase { public: - MOCK_METHOD1(getPtr, std::unique_ptr(const String&)); - MOCK_METHOD2(setPtr, std::unique_ptr(const String&, const String&)); - MOCK_METHOD2(pushPtr, std::unique_ptr(const String&, const String&)); - MOCK_METHOD1(removePtr, std::unique_ptr(const String&)); - MOCK_METHOD1(streamPtr, std::unique_ptr(const String&)); + MOCK_METHOD1(getPtr, std::unique_ptr(const std::string&)); + MOCK_METHOD2(setPtr, std::unique_ptr(const std::string&, const std::string&)); + MOCK_METHOD2(pushPtr, std::unique_ptr(const std::string&, const std::string&)); + MOCK_METHOD1(removePtr, std::unique_ptr(const std::string&)); + MOCK_METHOD1(streamPtr, std::unique_ptr(const std::string&)); }; class MockFirebaseGet : public FirebaseGet { public: - MOCK_CONST_METHOD0(response, const String&()); + MOCK_CONST_METHOD0(response, const std::string&()); MOCK_CONST_METHOD0(error, const FirebaseError&()); }; class MockFirebaseSet : public FirebaseSet { public: - MOCK_CONST_METHOD0(json, const String&()); + MOCK_CONST_METHOD0(json, const std::string&()); MOCK_CONST_METHOD0(error, const FirebaseError&()); }; class MockFirebasePush : public FirebasePush { public: - MOCK_CONST_METHOD0(name, const String&()); + MOCK_CONST_METHOD0(name, const std::string&()); MOCK_CONST_METHOD0(error, const FirebaseError&()); }; @@ -43,7 +43,7 @@ class MockFirebaseRemove : public FirebaseRemove { class MockFirebaseStream : public FirebaseStream { public: MOCK_METHOD0(available, bool()); - MOCK_METHOD1(read, Event(String& event)); + MOCK_METHOD1(read, Event(std::string& event)); MOCK_CONST_METHOD0(error, const FirebaseError&()); }; diff --git a/test/modem/get-command_test.cpp b/test/modem/get-command_test.cpp index 6c1981bb..ebdac3b5 100644 --- a/test/modem/get-command_test.cpp +++ b/test/modem/get-command_test.cpp @@ -67,7 +67,7 @@ TEST_F(GetCommandTest, handlesError) { EXPECT_CALL(out_, print(String("-FAIL "))) .WillOnce(Return(1)); - EXPECT_CALL(out_, println(error.message())) + EXPECT_CALL(out_, println(String(error.message().c_str()))) .WillOnce(Return(1)); ASSERT_FALSE(RunCommand(error)); diff --git a/test/modem/remove-command_test.cpp b/test/modem/remove-command_test.cpp index 5c361e7e..e6781745 100644 --- a/test/modem/remove-command_test.cpp +++ b/test/modem/remove-command_test.cpp @@ -60,7 +60,7 @@ TEST_F(RemoveCommandTest, handlesError) { EXPECT_CALL(out_, print(String("-FAIL "))) .WillOnce(Return(1)); - EXPECT_CALL(out_, println(error.message())) + EXPECT_CALL(out_, println(String(error.message().c_str()))) .WillOnce(Return(1)); ASSERT_FALSE(RunCommand(error)); } diff --git a/test/modem/stream-command_test.cpp b/test/modem/stream-command_test.cpp index 4cfc17be..277c7a23 100644 --- a/test/modem/stream-command_test.cpp +++ b/test/modem/stream-command_test.cpp @@ -52,8 +52,8 @@ TEST_F(StreamCommandTest, streams) { .WillRepeatedly(Return(false)); EXPECT_CALL(*stream_, read(_)) - .WillOnce(Invoke([&value](String& json) { - json = value; + .WillOnce(Invoke([&value](std::string& json) { + json = value.c_str(); return FirebaseStream::PUT; })); @@ -84,7 +84,7 @@ TEST_F(StreamCommandTest, handlesError) { EXPECT_CALL(out_, print(String("-FAIL "))) .WillOnce(Return(1)); - EXPECT_CALL(out_, println(error.message())) + EXPECT_CALL(out_, println(String(error.message().c_str()))) .WillOnce(Return(1)); ASSERT_FALSE(RunCommand(error)); } From 42c9ef2548a3547e040cd5b107007d8ee2f65e07 Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Thu, 26 May 2016 13:19:11 -0700 Subject: [PATCH 02/11] Restore FirebaseObject.h --- src/FirebaseObject.h | 99 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/FirebaseObject.h diff --git a/src/FirebaseObject.h b/src/FirebaseObject.h new file mode 100644 index 00000000..8233bbde --- /dev/null +++ b/src/FirebaseObject.h @@ -0,0 +1,99 @@ +// +// Copyright 2015 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef FIREBASE_OBJECT_H +#define FIREBASE_OBJECT_H + +#include "third-party/arduino-json-5.3/include/ArduinoJson.h" + +#ifndef FIREBASE_JSONBUFFER_SIZE +#define FIREBASE_JSONBUFFER_SIZE JSON_OBJECT_SIZE(32) +#endif // FIREBASE_JSONBUFFER_SIZE + +/** + * Represents value stored in firebase, may be a singular value (leaf node) or + * a tree structure. + */ +class FirebaseObject { + public: + /** + * Construct from json. + * \param data JSON formatted string. + */ + FirebaseObject(const String& data); + + /** + * Return the value as a boolean. + * \param optional path in the JSON object. + * \return result as a bool. + */ + bool getBool(const String& path = ""); + + /** + * Return the value as an int. + * \param optional path in the JSON object. + * \return result as an integer. + */ + int getInt(const String& path = ""); + + /** + * Return the value as a float. + * \param optional path in the JSON object. + * \return result as a float. + */ + float getFloat(const String& path = ""); + + /** + * Return the value as a String. + * \param optional path in the JSON object. + * \return result as a String. + */ + String getString(const String& path = ""); + + /** + * Return the value as a JsonVariant. + * \param optional path in the JSON object. + * \return result as a JsonVariant. + */ + JsonVariant getJsonVariant(const String& path = ""); + + + /** + * + * \return Whether there was an error decoding or accessing the JSON object. + */ + bool success() const; + + /** + * + * \return Whether there was an error decoding or accessing the JSON object. + */ + bool failed() const; + + /** + * + * \return Error message if failed() is true. + */ + const String& error() const; + + private: + String data_; + StaticJsonBuffer buffer_; + JsonVariant json_; + String error_; +}; + +#endif // FIREBASE_OBJECT_H From 7a7578b10aac6c9a57e60616e6632da499ec8710 Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Thu, 26 May 2016 14:15:11 -0700 Subject: [PATCH 03/11] Compiling examples and unit tests, still some linking issues --- src/Firebase.cpp | 6 ++-- src/FirebaseArduino.cpp | 38 +++++++++++------------ src/FirebaseArduino.h | 4 +-- src/FirebaseHttpClient.h | 18 ++++++----- src/FirebaseHttpClient_Esp8266.cpp | 33 +++++++++++--------- src/FirebaseObject.cpp | 4 ++- src/FirebaseObject.h | 2 +- src/modem/get-command.cpp | 2 +- src/modem/json_util.h | 16 ++++++++-- src/modem/push-command.cpp | 6 ++-- src/modem/set-command.cpp | 8 ++--- src/modem/stream-command.cpp | 11 ++++--- test/Makefile | 2 +- test/dummies/FirebaseHttpClient_dummy.cpp | 16 +++++----- test/modem/stream-command_test.cpp | 4 ++- 15 files changed, 95 insertions(+), 75 deletions(-) diff --git a/src/Firebase.cpp b/src/Firebase.cpp index cece4312..2e925067 100644 --- a/src/Firebase.cpp +++ b/src/Firebase.cpp @@ -137,7 +137,7 @@ const JsonObject& FirebaseCall::json() { //TODO(edcoyne): This is not efficient, we should do something smarter with //the buffers. buffer_ = DynamicJsonBuffer(); - return buffer_.parseObject(response()); + return buffer_.parseObject(response().c_str()); } // FirebaseGet @@ -188,7 +188,7 @@ bool FirebaseStream::available() { FirebaseStream::Event FirebaseStream::read(std::string& event) { auto client = http_->getStreamPtr(); Event type; - std::string typeStr = client->readStringUntil('\n').substring(7); + std::string typeStr = client->readStringUntil('\n').substring(7).c_str(); if (typeStr == "put") { type = Event::PUT; } else if (typeStr == "patch") { @@ -196,7 +196,7 @@ FirebaseStream::Event FirebaseStream::read(std::string& event) { } else { type = Event::UNKNOWN; } - event = client->readStringUntil('\n').substring(6); + event = client->readStringUntil('\n').substring(6).c_str(); client->readStringUntil('\n'); // consume separator return type; } diff --git a/src/FirebaseArduino.cpp b/src/FirebaseArduino.cpp index dfa782f7..a0300b8e 100644 --- a/src/FirebaseArduino.cpp +++ b/src/FirebaseArduino.cpp @@ -19,8 +19,8 @@ void FirebaseArduino::begin(const String& host, const String& auth) { http_.reset(FirebaseHttpClient::create()); http_->setReuseConnection(true); - host_ = host; - auth_ = auth; + host_ = host.c_str(); + auth_ = auth.c_str(); } String FirebaseArduino::pushInt(const String& path, int value) { @@ -43,9 +43,9 @@ String FirebaseArduino::pushString(const String& path, const String& value) { String FirebaseArduino::push(const String& path, const JsonVariant& value) { String buf; value.printTo(buf); - auto push = FirebasePush(host_, auth_, path, buf, http_.get()); + auto push = FirebasePush(host_, auth_, path.c_str(), buf.c_str(), http_.get()); error_ = push.error(); - return push.name(); + return push.name().c_str(); } void FirebaseArduino::setInt(const String& path, int value) { @@ -68,62 +68,62 @@ void FirebaseArduino::setString(const String& path, const String& value) { void FirebaseArduino::set(const String& path, const JsonVariant& value) { String buf; value.printTo(buf); - auto set = FirebaseSet(host_, auth_, path, buf, http_.get()); + auto set = FirebaseSet(host_, auth_, path.c_str(), buf.c_str(), http_.get()); error_ = set.error(); } FirebaseObject FirebaseArduino::get(const String& path) { - auto get = FirebaseGet(host_, auth_, path, http_.get()); + auto get = FirebaseGet(host_, auth_, path.c_str(), http_.get()); error_ = get.error(); if (failed()) { return FirebaseObject{""}; } - return FirebaseObject(get.response()); + return FirebaseObject(get.response().c_str()); } int FirebaseArduino::getInt(const String& path) { - auto get = FirebaseGet(host_, auth_, path, http_.get()); + auto get = FirebaseGet(host_, auth_, path.c_str(), http_.get()); error_ = get.error(); if (failed()) { return 0; } - return FirebaseObject(get.response()).getInt(); + return FirebaseObject(get.response().c_str()).getInt(); } float FirebaseArduino::getFloat(const String& path) { - auto get = FirebaseGet(host_, auth_, path, http_.get()); + auto get = FirebaseGet(host_, auth_, path.c_str(), http_.get()); error_ = get.error(); if (failed()) { return 0.0f; } - return FirebaseObject(get.response()).getFloat(); + return FirebaseObject(get.response().c_str()).getFloat(); } String FirebaseArduino::getString(const String& path) { - auto get = FirebaseGet(host_, auth_, path, http_.get()); + auto get = FirebaseGet(host_, auth_, path.c_str(), http_.get()); error_ = get.error(); if (failed()) { return ""; } - return FirebaseObject(get.response()).getString(); + return FirebaseObject(get.response().c_str()).getString(); } bool FirebaseArduino::getBool(const String& path) { - auto get = FirebaseGet(host_, auth_, path, http_.get()); + auto get = FirebaseGet(host_, auth_, path.c_str(), http_.get()); error_ = get.error(); if (failed()) { return ""; } - return FirebaseObject(get.response()).getBool(); + return FirebaseObject(get.response().c_str()).getBool(); } void FirebaseArduino::remove(const String& path) { - auto remove = FirebaseRemove(host_, auth_, path, http_.get()); + auto remove = FirebaseRemove(host_, auth_, path.c_str(), http_.get()); error_ = remove.error(); } void FirebaseArduino::stream(const String& path) { - auto stream = FirebaseStream(host_, auth_, path, http_.get()); + auto stream = FirebaseStream(host_, auth_, path.c_str(), http_.get()); error_ = stream.error(); } @@ -136,7 +136,7 @@ FirebaseObject FirebaseArduino::readEvent() { String type = client->readStringUntil('\n').substring(7);; String event = client->readStringUntil('\n').substring(6); client->readStringUntil('\n'); // consume separator - FirebaseObject obj = FirebaseObject(event); + FirebaseObject obj = FirebaseObject(event.c_str()); obj.getJsonVariant().asObject()["type"] = type; return obj; } @@ -150,7 +150,7 @@ bool FirebaseArduino::failed() { } const String& FirebaseArduino::error() { - return error_.message(); + return error_.message().c_str(); } FirebaseArduino Firebase; diff --git a/src/FirebaseArduino.h b/src/FirebaseArduino.h index 2b20db02..b780ee86 100644 --- a/src/FirebaseArduino.h +++ b/src/FirebaseArduino.h @@ -221,8 +221,8 @@ class FirebaseArduino { */ const String& error(); private: - String host_; - String auth_; + std::string host_; + std::string auth_; FirebaseError error_; std::unique_ptr http_; }; diff --git a/src/FirebaseHttpClient.h b/src/FirebaseHttpClient.h index 326c7410..106538e6 100644 --- a/src/FirebaseHttpClient.h +++ b/src/FirebaseHttpClient.h @@ -1,6 +1,8 @@ #ifndef FIREBASE_HTTP_CLIENT_H #define FIREBASE_HTTP_CLIENT_H +#include + #include "Arduino.h" #include "Stream.h" @@ -13,29 +15,29 @@ class FirebaseHttpClient { static FirebaseHttpClient* create(); virtual void setReuseConnection(bool reuse) = 0; - virtual void begin(const String& url) = 0; - virtual void begin(const String& host, const String& path) = 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 String& name, const String& value) = 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 String header(const String& name) = 0; + virtual std::string header(const std::string& name) = 0; - virtual int sendRequest(const String& method, const String& data) = 0; + virtual int sendRequest(const std::string& method, const std::string& data) = 0; - virtual String getString() = 0; + virtual std::string getString() = 0; virtual Stream* getStreamPtr() = 0; - virtual String errorToString(int error_code) = 0; + virtual std::string errorToString(int error_code) = 0; protected: static const uint16_t kFirebasePort = 443; }; -static const String kFirebaseFingerprint = +static const char kFirebaseFingerprint[] = "7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A"; #endif // FIREBASE_HTTP_CLIENT_H diff --git a/src/FirebaseHttpClient_Esp8266.cpp b/src/FirebaseHttpClient_Esp8266.cpp index 145afa9a..5afbbc12 100644 --- a/src/FirebaseHttpClient_Esp8266.cpp +++ b/src/FirebaseHttpClient_Esp8266.cpp @@ -1,3 +1,4 @@ +#include #include "FirebaseHttpClient.h" @@ -21,47 +22,49 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient { http_.setReuse(reuse); } - void begin(const String& url) override { - http_.begin(url, kFirebaseFingerprint); + void begin(const std::string& url) override { + http_.begin(url.c_str(), kFirebaseFingerprint); } - void begin(const String& host, const String& path) override { - http_.begin(host, kFirebasePort, path, true, kFirebaseFingerprint); + void begin(const std::string& host, const std::string& path) override { + http_.begin(host.c_str(), kFirebasePort, path.c_str(), true, kFirebaseFingerprint); } void end() override { http_.end(); } - void addHeader(const String& name, const String& value) override { - http_.addHeader(name, value); + 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); } - String header(const String& name) override { - return http_.header(name.c_str()); + std::string header(const std::string& name) override { + return http_.header(name.c_str()).c_str(); } - int sendRequest(const String& method, const String& data) override { + int sendRequest(const std::string& method, const std::string& data) override { return http_.sendRequest(method.c_str(), (uint8_t*)data.c_str(), data.length()); } - String getString() override { - return http_.getString(); + std::string getString() override { + return http_.getString().c_str(); } Stream* getStreamPtr() override { return http_.getStreamPtr(); } - String errorToString(int error_code) override { + std::string errorToString(int error_code) override { #ifdef USE_ESP_ARDUINO_CORE_2_0_0 - return String(error_code); + std::ostringstream ss; + ss << error_code; + return ss.str(); #else - return HTTPClient::errorToString(error_code); + return HTTPClient::errorToString(error_code).c_str(); #endif } @@ -72,4 +75,4 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient { FirebaseHttpClient* FirebaseHttpClient::create() { return new FirebaseHttpClientEsp8266(); } - + diff --git a/src/FirebaseObject.cpp b/src/FirebaseObject.cpp index dc1f1d87..6686aeb1 100644 --- a/src/FirebaseObject.cpp +++ b/src/FirebaseObject.cpp @@ -16,7 +16,9 @@ #include "FirebaseObject.h" -FirebaseObject::FirebaseObject(const String& data) : data_{data} { +// We need to make a copy of data here, even though it may be large. +// It will need to be long lived. +FirebaseObject::FirebaseObject(const char* data) : data_{data} { json_ = buffer_.parse(&data_[0]); // TODO(proppy): find a way to check decoding error, tricky because // ArduinoJson doesn't surface error for variant parsing. diff --git a/src/FirebaseObject.h b/src/FirebaseObject.h index 8233bbde..8755914f 100644 --- a/src/FirebaseObject.h +++ b/src/FirebaseObject.h @@ -33,7 +33,7 @@ class FirebaseObject { * Construct from json. * \param data JSON formatted string. */ - FirebaseObject(const String& data); + FirebaseObject(const char* data); /** * Return the value as a boolean. diff --git a/src/modem/get-command.cpp b/src/modem/get-command.cpp index 62fd071e..546775f8 100644 --- a/src/modem/get-command.cpp +++ b/src/modem/get-command.cpp @@ -13,7 +13,7 @@ bool GetCommand::execute(const String& command, return false; } - String path = in->readLine(); + std::string path = in->readLine().c_str(); std::unique_ptr get(fbase().getPtr(path)); if (get->error()) { diff --git a/src/modem/json_util.h b/src/modem/json_util.h index 544120a1..1275f855 100644 --- a/src/modem/json_util.h +++ b/src/modem/json_util.h @@ -3,11 +3,21 @@ namespace firebase { namespace modem { +namespace { +std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) { + size_t start_pos = 0; + while((start_pos = str.find(from, start_pos)) != std::string::npos) { + str.replace(start_pos, from.length(), to); + start_pos += to.length(); // Handles case where 'to' is a substring of 'from' + } + return str; +} +} // TODO(edcoyne): We should use a json library to escape. -inline String EncodeForJson(String input) { - input.replace("\\", "\\\\"); - input.replace("\"", "\\\""); +inline std::string EncodeForJson(std::string input) { + ReplaceAll(input, "\\", "\\\\"); + ReplaceAll(input, "\"", "\\\""); return "\"" + input + "\""; } diff --git a/src/modem/push-command.cpp b/src/modem/push-command.cpp index 73d0f7ef..2f73873c 100644 --- a/src/modem/push-command.cpp +++ b/src/modem/push-command.cpp @@ -14,15 +14,15 @@ bool PushCommand::execute(const String& command, return false; } - String path(in->readStringUntil(' ')); - String data(in->readLine()); + std::string path(in->readStringUntil(' ').c_str()); + std::string data(in->readLine().c_str()); std::unique_ptr push( fbase().pushPtr(path, EncodeForJson(data))); if (push->error()) { out->print("-FAIL "); - out->println(push->error().message()); + out->println(push->error().message().c_str()); return false; } else { out->println("+OK"); diff --git a/src/modem/set-command.cpp b/src/modem/set-command.cpp index 1fff899c..afa3c670 100644 --- a/src/modem/set-command.cpp +++ b/src/modem/set-command.cpp @@ -14,11 +14,11 @@ bool SetCommand::execute(const String& command, return false; } - String path(in->readStringUntil(' ')); - String data(in->readLine()); + std::string path(in->readStringUntil(' ').c_str()); + std::string data(in->readLine().c_str()); - std::unique_ptr set(fbase().setPtr(path.c_str(), - EncodeForJson(data).c_str())); + std::unique_ptr set(fbase().setPtr(path, + EncodeForJson(data))); if (set->error()) { out->print("-FAIL "); diff --git a/src/modem/stream-command.cpp b/src/modem/stream-command.cpp index b3b1c649..9a83d9e9 100644 --- a/src/modem/stream-command.cpp +++ b/src/modem/stream-command.cpp @@ -13,26 +13,27 @@ bool StreamCommand::execute(const String& command, return false; } - String path = in->readLine(); + std::string path = in->readLine().c_str(); std::unique_ptr stream(fbase().streamPtr(path)); if (stream->error()) { out->print("-FAIL "); - out->println(stream->error().message()); + out->println(stream->error().message().c_str()); return false; } bool running = true; while(running) { if (stream->available()) { - String json; + std::string json; FirebaseStream::Event event = stream->read(json); out->print("+"); - out->print(FirebaseStream::EventToName(event) + " "); + out->print(FirebaseStream::EventToName(event).c_str()); + out->print(" "); // TODO(edcoyne): add json parsing and get real path. out->println("/dummy/path"); out->println(json.length()); - out->println(json); + out->println(json.c_str()); } else if (in->available()) { String command = in->readLine(); if (command == "END_STREAM") { diff --git a/test/Makefile b/test/Makefile index 84ba5c35..e6d3628a 100644 --- a/test/Makefile +++ b/test/Makefile @@ -52,4 +52,4 @@ check: firebase-test ./firebase-test clean: - rm -f ${OBJS} firebase-test + echo rm -f ${OBJS} firebase-test diff --git a/test/dummies/FirebaseHttpClient_dummy.cpp b/test/dummies/FirebaseHttpClient_dummy.cpp index e8a746e9..571fa842 100644 --- a/test/dummies/FirebaseHttpClient_dummy.cpp +++ b/test/dummies/FirebaseHttpClient_dummy.cpp @@ -11,30 +11,30 @@ class FirebaseHttpClientDummy : public FirebaseHttpClient { void setReuseConnection(bool UNUSED_ARG(reuse)) override { } - void begin(const String& UNUSED_ARG(url)) override { + void begin(const std::string& UNUSED_ARG(url)) override { } - void begin(const String& UNUSED_ARG(host), const String& UNUSED_ARG(path)) override { + void begin(const std::string& UNUSED_ARG(host), const std::string& UNUSED_ARG(path)) override { } void end() override { } - void addHeader(const String& UNUSED_ARG(name), const String& UNUSED_ARG(value)) override { + void addHeader(const std::string& UNUSED_ARG(name), const std::string& UNUSED_ARG(value)) override { } void collectHeaders(const char* UNUSED_ARG(header_keys[]), const int UNUSED_ARG(count)) override { } - String header(const String& UNUSED_ARG(name)) override { + std::string header(const std::string& UNUSED_ARG(name)) override { return ""; } - int sendRequest(const String& UNUSED_ARG(method), const String& UNUSED_ARG(data)) override { + int sendRequest(const std::string& UNUSED_ARG(method), const std::string& UNUSED_ARG(data)) override { return 0; } - String getString() override { + std::string getString() override { return ""; } @@ -42,8 +42,8 @@ class FirebaseHttpClientDummy : public FirebaseHttpClient { return nullptr; } - String errorToString(int UNUSED_ARG(error_code)) override { - return String(); + std::string errorToString(int UNUSED_ARG(error_code)) override { + return std::string(); } }; diff --git a/test/modem/stream-command_test.cpp b/test/modem/stream-command_test.cpp index 277c7a23..4618a8e7 100644 --- a/test/modem/stream-command_test.cpp +++ b/test/modem/stream-command_test.cpp @@ -59,7 +59,9 @@ TEST_F(StreamCommandTest, streams) { EXPECT_CALL(out_, print(String("+"))) .WillOnce(Return(1)); - EXPECT_CALL(out_, print(String("PUT "))) + EXPECT_CALL(out_, print(String("PUT"))) + .WillOnce(Return(1)); + EXPECT_CALL(out_, print(String(" "))) .WillOnce(Return(1)); EXPECT_CALL(out_, println(String("/dummy/path"))) .WillOnce(Return(1)); From 0428c73ca1397686ed7b6a36317993059bbd6914 Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Thu, 26 May 2016 15:53:46 -0700 Subject: [PATCH 04/11] Bug fixes --- src/FirebaseArduino.cpp | 2 +- src/FirebaseArduino.h | 4 ++++ src/FirebaseHttpClient_Esp8266.cpp | 9 +++++---- test/Makefile | 1 + 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/FirebaseArduino.cpp b/src/FirebaseArduino.cpp index a0300b8e..86fe2ea2 100644 --- a/src/FirebaseArduino.cpp +++ b/src/FirebaseArduino.cpp @@ -61,7 +61,7 @@ void FirebaseArduino::setBool(const String& path, bool value) { } void FirebaseArduino::setString(const String& path, const String& value) { - JsonVariant json(value); + JsonVariant json(value.c_str()); set(path, json); } diff --git a/src/FirebaseArduino.h b/src/FirebaseArduino.h index b780ee86..14f62519 100644 --- a/src/FirebaseArduino.h +++ b/src/FirebaseArduino.h @@ -17,6 +17,10 @@ #ifndef FIREBASE_ARDUINO_H #define FIREBASE_ARDUINO_H +#include +// This is needed to compile std::string on esp8266. +template class std::basic_string; + #include "Firebase.h" #include "FirebaseObject.h" diff --git a/src/FirebaseHttpClient_Esp8266.cpp b/src/FirebaseHttpClient_Esp8266.cpp index 5afbbc12..4d65ac1f 100644 --- a/src/FirebaseHttpClient_Esp8266.cpp +++ b/src/FirebaseHttpClient_Esp8266.cpp @@ -1,7 +1,8 @@ -#include #include "FirebaseHttpClient.h" +#include + // The ordering of these includes matters greatly. #include #include @@ -60,9 +61,9 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient { std::string errorToString(int error_code) override { #ifdef USE_ESP_ARDUINO_CORE_2_0_0 - std::ostringstream ss; - ss << error_code; - return ss.str(); + char buff[11]; + itoa(error_code, buff, 10); + return buff; #else return HTTPClient::errorToString(error_code).c_str(); #endif diff --git a/test/Makefile b/test/Makefile index e6d3628a..403b0cbc 100644 --- a/test/Makefile +++ b/test/Makefile @@ -51,5 +51,6 @@ firebase-test: ${OBJS} check: firebase-test ./firebase-test +#TODO: had to echo this because it picks up FirebaseObject.h as well and deletes it :( clean: echo rm -f ${OBJS} firebase-test From 326518c1744b1fd0ac74c575421086ff1eeaf2c4 Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Thu, 26 May 2016 16:32:15 -0700 Subject: [PATCH 05/11] Fixs from testing streaming example --- examples/FirebaseSerialHost_ESP8266/push.txt | 2 +- src/SerialTransceiver.h | 4 ++++ src/modem/json_util.h | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/FirebaseSerialHost_ESP8266/push.txt b/examples/FirebaseSerialHost_ESP8266/push.txt index 9878dd3c..fc1ec2a1 100644 --- a/examples/FirebaseSerialHost_ESP8266/push.txt +++ b/examples/FirebaseSerialHost_ESP8266/push.txt @@ -1 +1 @@ -PUSH /seria/push_test "this is a test string \ " +PUSH /serial/push_test "this is a test string \ " diff --git a/src/SerialTransceiver.h b/src/SerialTransceiver.h index 45ae91cf..371fa4cc 100644 --- a/src/SerialTransceiver.h +++ b/src/SerialTransceiver.h @@ -1,3 +1,7 @@ +#include +// This is needed to compile std::string on esp8266. +template class std::basic_string; + #include "modem/SerialTransceiver.h" // Bring them into the base namespace for easier use in arduino ide. using firebase::modem::SerialTransceiver; diff --git a/src/modem/json_util.h b/src/modem/json_util.h index 1275f855..6917e291 100644 --- a/src/modem/json_util.h +++ b/src/modem/json_util.h @@ -16,8 +16,8 @@ std::string ReplaceAll(std::string str, const std::string& from, const std::stri // TODO(edcoyne): We should use a json library to escape. inline std::string EncodeForJson(std::string input) { - ReplaceAll(input, "\\", "\\\\"); - ReplaceAll(input, "\"", "\\\""); + input = ReplaceAll(input, "\\", "\\\\"); + input = ReplaceAll(input, "\"", "\\\""); return "\"" + input + "\""; } From 44601591ed915b3624c41b2664c5f74e3b1282be Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Thu, 26 May 2016 16:33:11 -0700 Subject: [PATCH 06/11] Add error handling to all calls in demo --- .../FirebaseDemo_ESP8266.ino | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino b/examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino index e4e3256b..e02968cf 100644 --- a/examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino +++ b/examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino @@ -58,6 +58,12 @@ void loop() { // update value Firebase.setFloat("number", 43.0); + // handle error + if (Firebase.failed()) { + Serial.print("setting /number failed:"); + Serial.println(Firebase.error()); + return; + } delay(1000); // get value @@ -71,13 +77,31 @@ void loop() { // set string value Firebase.setString("message", "hello world"); + // handle error + if (Firebase.failed()) { + Serial.print("setting /message failed:"); + Serial.println(Firebase.error()); + return; + } delay(1000); + // set bool value Firebase.setBool("truth", false); + if (Firebase.failed()) { + Serial.print("setting /truth failed:"); + Serial.println(Firebase.error()); + return; + } delay(1000); // append a new value to /logs String name = Firebase.pushInt("logs", n++); + // handle error + if (Firebase.failed()) { + Serial.print("pushing /logs failed:"); + Serial.println(Firebase.error()); + return; + } Serial.print("pushed: /logs/"); Serial.println(name); delay(1000); From 34f8f71279d3e9f9eb10292d3aecf3d921b3c521 Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Thu, 26 May 2016 16:44:04 -0700 Subject: [PATCH 07/11] Added code snippet that will allow strings to work for us until this is present upstream --- src/throw_out_of_range.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/throw_out_of_range.cpp diff --git a/src/throw_out_of_range.cpp b/src/throw_out_of_range.cpp new file mode 100644 index 00000000..50ceae37 --- /dev/null +++ b/src/throw_out_of_range.cpp @@ -0,0 +1,9 @@ +#include + +//TODO(edcoyne): remove this when it is present upstream. +// Currently this is needed to use std::string on Arduino. +namespace std{ +void __throw_out_of_range(const char* str) { + panic(); +} +} From 109463bf3edd341403633b85d60dfba53b582889 Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Thu, 26 May 2016 16:45:36 -0700 Subject: [PATCH 08/11] Added comment to demo --- examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino b/examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino index e02968cf..982fca1a 100644 --- a/examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino +++ b/examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino @@ -87,6 +87,7 @@ void loop() { // set bool value Firebase.setBool("truth", false); + // handle error if (Firebase.failed()) { Serial.print("setting /truth failed:"); Serial.println(Firebase.error()); From 0d2ca476250478d11a141e637886fb8021ba50f8 Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Tue, 31 May 2016 21:26:08 -0700 Subject: [PATCH 09/11] move template instatiation to cpp file --- src/FirebaseArduino.cpp | 3 +++ src/FirebaseArduino.h | 2 -- src/SerialTransceiver.h | 2 -- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/FirebaseArduino.cpp b/src/FirebaseArduino.cpp index 86fe2ea2..d2fc9c5e 100644 --- a/src/FirebaseArduino.cpp +++ b/src/FirebaseArduino.cpp @@ -16,6 +16,9 @@ #include "FirebaseArduino.h" +// This is needed to compile std::string on esp8266. +template class std::basic_string; + void FirebaseArduino::begin(const String& host, const String& auth) { http_.reset(FirebaseHttpClient::create()); http_->setReuseConnection(true); diff --git a/src/FirebaseArduino.h b/src/FirebaseArduino.h index 14f62519..a0bcca27 100644 --- a/src/FirebaseArduino.h +++ b/src/FirebaseArduino.h @@ -18,8 +18,6 @@ #define FIREBASE_ARDUINO_H #include -// This is needed to compile std::string on esp8266. -template class std::basic_string; #include "Firebase.h" #include "FirebaseObject.h" diff --git a/src/SerialTransceiver.h b/src/SerialTransceiver.h index 371fa4cc..dd0633a9 100644 --- a/src/SerialTransceiver.h +++ b/src/SerialTransceiver.h @@ -1,6 +1,4 @@ #include -// This is needed to compile std::string on esp8266. -template class std::basic_string; #include "modem/SerialTransceiver.h" // Bring them into the base namespace for easier use in arduino ide. From bfc4856d12013d4526101723bdbb008a41f4d276 Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Tue, 31 May 2016 21:29:59 -0700 Subject: [PATCH 10/11] Reverted Demo --- .../FirebaseDemo_ESP8266.ino | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino b/examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino index 982fca1a..e4e3256b 100644 --- a/examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino +++ b/examples/FirebaseDemo_ESP8266/FirebaseDemo_ESP8266.ino @@ -58,12 +58,6 @@ void loop() { // update value Firebase.setFloat("number", 43.0); - // handle error - if (Firebase.failed()) { - Serial.print("setting /number failed:"); - Serial.println(Firebase.error()); - return; - } delay(1000); // get value @@ -77,32 +71,13 @@ void loop() { // set string value Firebase.setString("message", "hello world"); - // handle error - if (Firebase.failed()) { - Serial.print("setting /message failed:"); - Serial.println(Firebase.error()); - return; - } delay(1000); - // set bool value Firebase.setBool("truth", false); - // handle error - if (Firebase.failed()) { - Serial.print("setting /truth failed:"); - Serial.println(Firebase.error()); - return; - } delay(1000); // append a new value to /logs String name = Firebase.pushInt("logs", n++); - // handle error - if (Firebase.failed()) { - Serial.print("pushing /logs failed:"); - Serial.println(Firebase.error()); - return; - } Serial.print("pushed: /logs/"); Serial.println(name); delay(1000); From 1b418d7c8f21c9b1c019f02e485b1c3c287dabca Mon Sep 17 00:00:00 2001 From: Ed Coyne Date: Tue, 31 May 2016 21:57:32 -0700 Subject: [PATCH 11/11] Change function to weak declaration --- src/throw_out_of_range.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/throw_out_of_range.cpp b/src/throw_out_of_range.cpp index 50ceae37..865b6ecf 100644 --- a/src/throw_out_of_range.cpp +++ b/src/throw_out_of_range.cpp @@ -3,7 +3,7 @@ //TODO(edcoyne): remove this when it is present upstream. // Currently this is needed to use std::string on Arduino. namespace std{ -void __throw_out_of_range(const char* str) { +void __attribute__((weak)) __throw_out_of_range(const char* str) { panic(); } }