Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Separating WiFiClient. #443

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Firebase.cpp
Original file line number Diff line number Diff line change
@@ -33,8 +33,8 @@ std::string makeFirebaseURL(const std::string& path, const std::string& auth) {

} // namespace

Firebase::Firebase(const std::string& host, const std::string& auth) : host_(host), auth_(auth) {
http_.reset(FirebaseHttpClient::create());
Firebase::Firebase(WiFiClient* client, const std::string& host, const std::string& auth) : host_(host), auth_(auth) {
http_.reset(FirebaseHttpClient::create(client));
http_->setReuseConnection(true);
}

2 changes: 1 addition & 1 deletion src/Firebase.h
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@
// Firebase REST API client.
class Firebase {
public:
Firebase(const std::string& host, const std::string& auth = "");
Firebase(WiFiClient* client, const std::string& host, const std::string& auth = "");

const std::string& auth() const;

12 changes: 7 additions & 5 deletions src/FirebaseArduino.cpp
Original file line number Diff line number Diff line change
@@ -19,22 +19,23 @@
// This is needed to compile std::string on esp8266.
template class std::basic_string<char>;

void FirebaseArduino::begin(const String& host, const String& auth) {
void FirebaseArduino::begin(WiFiClient* client, const String& host, const String& auth) {
host_ = host.c_str();
auth_ = auth.c_str();
client_ = client;
}

void FirebaseArduino::initStream() {
if (stream_http_.get() == nullptr) {
stream_http_.reset(FirebaseHttpClient::create());
stream_http_.reset(FirebaseHttpClient::create(client_));
stream_http_->setReuseConnection(true);
stream_.reset(new FirebaseStream(stream_http_));
}
}

void FirebaseArduino::initRequest() {
if (req_http_.get() == nullptr) {
req_http_.reset(FirebaseHttpClient::create());
req_http_.reset(FirebaseHttpClient::create(client_));
req_http_->setReuseConnection(true);
req_.reset(new FirebaseRequest(req_http_));
}
@@ -197,8 +198,9 @@ bool FirebaseArduino::failed() {
return error_.code() != 0;
}

const String& FirebaseArduino::error() {
return error_.message().c_str();
void FirebaseArduino::error(std::string* buf) {
std::string err = error_.message();
*buf = err;
}

FirebaseArduino Firebase;
6 changes: 4 additions & 2 deletions src/FirebaseArduino.h
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ class FirebaseArduino {
* \param host Your firebase db host, usually X.firebaseio.com.
* \param auth Optional credentials for the db, a secret or token.
*/
virtual void begin(const String& host, const String& auth = "");
virtual void begin(WiFiClient* client, const String& host, const String& auth = "");

/**
* Appends the integer value to the node at path.
@@ -221,7 +221,8 @@ class FirebaseArduino {
/**
* \return Error message from last command if failed() is true.
*/
virtual const String& error();
virtual void error(std::string* buf);

private:
std::string host_;
std::string auth_;
@@ -230,6 +231,7 @@ class FirebaseArduino {
std::shared_ptr<FirebaseRequest> req_;
std::shared_ptr<FirebaseHttpClient> stream_http_;
std::shared_ptr<FirebaseStream> stream_;
WiFiClient* client_;

void initStream();
void initRequest();
5 changes: 3 additions & 2 deletions src/FirebaseCloudMessaging.cpp
Original file line number Diff line number Diff line change
@@ -8,9 +8,10 @@ FirebaseCloudMessage FirebaseCloudMessage::SimpleNotification(
return message;
}

FirebaseCloudMessaging::FirebaseCloudMessaging(const std::string& server_key) {
FirebaseCloudMessaging::FirebaseCloudMessaging(WiFiClient* client, const std::string& server_key) {
auth_header_ = "key=";
auth_header_ += server_key;
client_ = client;
}

const FirebaseError FirebaseCloudMessaging::SendMessageToUser(
@@ -63,7 +64,7 @@ const FirebaseError FirebaseCloudMessaging::SendMessageToTopic(

const FirebaseError FirebaseCloudMessaging::SendPayload(
const char* payload) {
std::shared_ptr<FirebaseHttpClient> client(FirebaseHttpClient::create());
std::shared_ptr<FirebaseHttpClient> client(FirebaseHttpClient::create(client_));
client->begin("http://fcm.googleapis.com/fcm/send");
client->addHeader("Authorization", auth_header_.c_str());
client->addHeader("Content-Type", "application/json");
3 changes: 2 additions & 1 deletion src/FirebaseCloudMessaging.h
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ struct FirebaseCloudMessage {
// Firebase REST API client.
class FirebaseCloudMessaging {
public:
FirebaseCloudMessaging(const std::string& server_key);
FirebaseCloudMessaging(WiFiClient* client, const std::string& server_key);
const FirebaseError SendMessageToUser(const std::string& registration_id,
const FirebaseCloudMessage& message);
const FirebaseError SendMessageToUsers(const std::vector<std::string>& registration_ids,
@@ -82,5 +82,6 @@ class FirebaseCloudMessaging {
const void AddToJson(const FirebaseCloudMessage& message, JsonObject& json) const;

std::string auth_header_;
WiFiClient* client_;
};
#endif // firebase_cloud_messaging_h
6 changes: 2 additions & 4 deletions src/FirebaseHttpClient.h
Original file line number Diff line number Diff line change
@@ -5,14 +5,15 @@

#include "Arduino.h"
#include "Stream.h"
#include <WiFiClient.h>

struct HttpStatus {
static const int TEMPORARY_REDIRECT = 307;
};

class FirebaseHttpClient {
public:
static FirebaseHttpClient* create();
static FirebaseHttpClient* create(WiFiClient* client);

virtual void setReuseConnection(bool reuse) = 0;
virtual void begin(const std::string& url) = 0;
@@ -39,7 +40,4 @@ class FirebaseHttpClient {
static const uint16_t kFirebasePort = 443;
};

static const char kFirebaseFingerprint[] =
"B6 F5 80 C8 B1 DA 61 C1 07 9D 80 42 D8 A9 1F AF 9F C8 96 7D"; // 2019-04

#endif // FIREBASE_HTTP_CLIENT_H
13 changes: 8 additions & 5 deletions src/FirebaseHttpClient_Esp8266.cpp
Original file line number Diff line number Diff line change
@@ -36,19 +36,21 @@ class ForceReuseHTTPClient : public HTTPClient {

class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
public:
FirebaseHttpClientEsp8266() {}
FirebaseHttpClientEsp8266(WiFiClient* client) {
client_ = client;
}

void setReuseConnection(bool reuse) override {
http_.setReuse(reuse);
http_.forceReuse(reuse);
}

void begin(const std::string& url) override {
http_.begin(url.c_str(), kFirebaseFingerprint);
http_.begin(*client_, url.c_str());
}

void begin(const std::string& host, const std::string& path) override {
http_.begin(host.c_str(), kFirebasePort, path.c_str(), kFirebaseFingerprint);
http_.begin(*client_, host.c_str(), kFirebasePort, path.c_str());
}

void end() override {
@@ -89,9 +91,10 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {

private:
ForceReuseHTTPClient http_;
WiFiClient* client_;
};

FirebaseHttpClient* FirebaseHttpClient::create() {
return new FirebaseHttpClientEsp8266();
FirebaseHttpClient* FirebaseHttpClient::create(WiFiClient* client) {
return new FirebaseHttpClientEsp8266(client);
}