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

Commit 7c7c57d

Browse files
committed
Merge pull request #86 from googlesamples/revert-83-modem-impl2
Revert "Implement modem (attempt 2)"
2 parents 6df8233 + 051d325 commit 7c7c57d

36 files changed

+55
-1659
lines changed

.gitmodules

-6
This file was deleted.

src/Firebase.cpp

+37-57
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@
1515
//
1616
#include "Firebase.h"
1717

18-
using std::unique_ptr;
18+
// Detect whether stable version of HTTP library is installed instead of
19+
// master branch and patch in missing status and methods.
20+
#ifndef HTTP_CODE_TEMPORARY_REDIRECT
21+
#define HTTP_CODE_TEMPORARY_REDIRECT 307
22+
#define USE_ESP_ARDUINO_CORE_2_0_0
23+
#endif
1924

2025
namespace {
26+
const char* kFirebaseFingerprint = "7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A";
27+
const uint16_t kFirebasePort = 443;
28+
2129
String makeFirebaseURL(const String& path, const String& auth) {
2230
String url;
2331
if (path[0] != '/') {
@@ -33,75 +41,45 @@ String makeFirebaseURL(const String& path, const String& auth) {
3341
} // namespace
3442

3543
Firebase::Firebase(const String& host) : host_(host) {
36-
http_.reset(FirebaseHttpClient::create());
37-
http_->setReuseConnection(true);
44+
http_.setReuse(true);
3845
}
3946

4047
Firebase& Firebase::auth(const String& auth) {
4148
auth_ = auth;
4249
return *this;
4350
}
4451

45-
const String& Firebase::auth() {
46-
return auth_;
47-
}
48-
4952
FirebaseGet Firebase::get(const String& path) {
50-
return FirebaseGet(host_, auth_, path, http_.get());
51-
}
52-
53-
unique_ptr<FirebaseGet> Firebase::getPtr(const String& path) {
54-
return unique_ptr<FirebaseGet>(new FirebaseGet(host_, auth_, path, http_.get()));
53+
return FirebaseGet(host_, auth_, path, &http_);
5554
}
5655

5756
FirebaseSet Firebase::set(const String& path, const String& value) {
58-
return FirebaseSet(host_, auth_, path, value, http_.get());
59-
}
60-
61-
unique_ptr<FirebaseSet> Firebase::setPtr(const String& path,
62-
const String& value) {
63-
return unique_ptr<FirebaseSet>(
64-
new FirebaseSet(host_, auth_, path, value, http_.get()));
57+
return FirebaseSet(host_, auth_, path, value, &http_);
6558
}
6659

6760
FirebasePush Firebase::push(const String& path, const String& value) {
68-
return FirebasePush(host_, auth_, path, value, http_.get());
69-
}
70-
unique_ptr<FirebasePush> Firebase::pushPtr(const String& path, const String& value) {
71-
return unique_ptr<FirebasePush>(
72-
new FirebasePush(host_, auth_, path, value, http_.get()));
61+
return FirebasePush(host_, auth_, path, value, &http_);
7362
}
7463

7564
FirebaseRemove Firebase::remove(const String& path) {
76-
return FirebaseRemove(host_, auth_, path, http_.get());
77-
}
78-
79-
unique_ptr<FirebaseRemove> Firebase::removePtr(const String& path) {
80-
return unique_ptr<FirebaseRemove>(
81-
new FirebaseRemove(host_, auth_, path, http_.get()));
65+
return FirebaseRemove(host_, auth_, path, &http_);
8266
}
8367

8468
FirebaseStream Firebase::stream(const String& path) {
8569
// TODO: create new client dedicated to stream.
86-
return FirebaseStream(host_, auth_, path, http_.get());
87-
}
88-
89-
unique_ptr<FirebaseStream> Firebase::streamPtr(const String& path) {
90-
// TODO: create new client dedicated to stream.
91-
return unique_ptr<FirebaseStream>(
92-
new FirebaseStream(host_, auth_, path, http_.get()));
70+
return FirebaseStream(host_, auth_, path, &http_);
9371
}
9472

9573
// FirebaseCall
9674
FirebaseCall::FirebaseCall(const String& host, const String& auth,
9775
const char* method, const String& path,
98-
const String& data, FirebaseHttpClient* http) : http_(http) {
99-
String path_with_auth = makeFirebaseURL(path, auth);
100-
http_->setReuseConnection(true);
101-
http_->begin(host, path_with_auth);
76+
const String& data, HTTPClient* http) : http_(http) {
77+
String url = makeFirebaseURL(path, auth);
78+
http_->setReuse(true);
79+
http_->begin(host, kFirebasePort, url, true, kFirebaseFingerprint);
10280

10381
bool followRedirect = false;
104-
if (String(method) == "STREAM") {
82+
if (method == "STREAM") {
10583
method = "GET";
10684
http_->addHeader("Accept", "text/event-stream");
10785
followRedirect = true;
@@ -112,24 +90,26 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,
11290
http_->collectHeaders(headers, 1);
11391
}
11492

115-
int status = http_->sendRequest(method, data);
93+
int status = http_->sendRequest(method, (uint8_t*)data.c_str(), data.length());
11694

11795
// TODO: Add a max redirect check
11896
if (followRedirect) {
119-
while (status == HttpStatus::TEMPORARY_REDIRECT) {
97+
while (status == HTTP_CODE_TEMPORARY_REDIRECT) {
12098
String location = http_->header("Location");
121-
http_->setReuseConnection(false);
99+
http_->setReuse(false);
122100
http_->end();
123-
http_->setReuseConnection(true);
124-
http_->begin(location);
125-
status = http_->sendRequest("GET", String());
101+
http_->setReuse(true);
102+
http_->begin(location, kFirebaseFingerprint);
103+
status = http_->sendRequest("GET", (uint8_t*)NULL, 0);
126104
}
127105
}
128106

129107
if (status != 200) {
130-
error_ = FirebaseError(status,
131-
String(method) + " " + path_with_auth +
132-
": " + http_->errorToString(status));
108+
#ifdef USE_ESP_ARDUINO_CORE_2_0_0
109+
error_ = FirebaseError(status, String(method) + " " + url + ": " + status);
110+
#else
111+
error_ = FirebaseError(status, String(method) + " " + url + ": " + HTTPClient::errorToString(status));
112+
#endif
133113
}
134114

135115
// if not streaming.
@@ -148,14 +128,14 @@ const JsonObject& FirebaseCall::json() {
148128
// FirebaseGet
149129
FirebaseGet::FirebaseGet(const String& host, const String& auth,
150130
const String& path,
151-
FirebaseHttpClient* http)
131+
HTTPClient* http)
152132
: FirebaseCall(host, auth, "GET", path, "", http) {
153133
}
154134

155135
// FirebaseSet
156136
FirebaseSet::FirebaseSet(const String& host, const String& auth,
157137
const String& path, const String& value,
158-
FirebaseHttpClient* http)
138+
HTTPClient* http)
159139
: FirebaseCall(host, auth, "PUT", path, value, http) {
160140
if (!error()) {
161141
// TODO: parse json
@@ -165,24 +145,24 @@ FirebaseSet::FirebaseSet(const String& host, const String& auth,
165145
// FirebasePush
166146
FirebasePush::FirebasePush(const String& host, const String& auth,
167147
const String& path, const String& value,
168-
FirebaseHttpClient* http)
148+
HTTPClient* http)
169149
: FirebaseCall(host, auth, "POST", path, value, http) {
170150
if (!error()) {
171-
//name_ = json()["name"].as<const char*>();
151+
name_ = json()["name"].as<const char*>();
172152
}
173153
}
174154

175155
// FirebasePush
176156
FirebaseRemove::FirebaseRemove(const String& host, const String& auth,
177157
const String& path,
178-
FirebaseHttpClient* http)
158+
HTTPClient* http)
179159
: FirebaseCall(host, auth, "DELETE", path, "", http) {
180160
}
181161

182162
// FirebaseStream
183163
FirebaseStream::FirebaseStream(const String& host, const String& auth,
184164
const String& path,
185-
FirebaseHttpClient* http)
165+
HTTPClient* http)
186166
: FirebaseCall(host, auth, "STREAM", path, "", http) {
187167
}
188168

src/Firebase.h

+18-49
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
#define firebase_h
2222

2323
#include <Arduino.h>
24-
#include <memory>
25-
#include "FirebaseHttpClient.h"
26-
// TODO(edcoyne): move this into our mock_arduino fork where we actually do the
27-
// override.
28-
#define ARDUINO_STRING_OVERRIDE
24+
#include <ESP8266WiFi.h>
25+
#include <WiFiClientSecure.h>
26+
#include <ESP8266HTTPClient.h>
2927
#include "third-party/arduino-json-5.1.1/include/ArduinoJson.h"
3028

3129
class FirebaseGet;
@@ -37,41 +35,26 @@ class FirebaseStream;
3735
// Firebase REST API client.
3836
class Firebase {
3937
public:
40-
explicit Firebase(const String& host);
38+
Firebase(const String& host);
4139
Firebase& auth(const String& auth);
42-
virtual ~Firebase() = default;
43-
44-
Firebase(const Firebase&) = delete;
45-
46-
// Fetch auth string back.
47-
const String& auth();
4840

4941
// Fetch json encoded `value` at `path`.
5042
FirebaseGet get(const String& path);
51-
virtual std::unique_ptr<FirebaseGet> getPtr(const String& path);
5243

5344
// Set json encoded `value` at `path`.
5445
FirebaseSet set(const String& path, const String& json);
55-
virtual std::unique_ptr<FirebaseSet> setPtr(const String& path, const String& json);
5646

5747
// Add new json encoded `value` to list at `path`.
5848
FirebasePush push(const String& path, const String& json);
59-
virtual std::unique_ptr<FirebasePush> pushPtr(const String& path, const String& json);
6049

6150
// Delete value at `path`.
6251
FirebaseRemove remove(const String& path);
63-
virtual std::unique_ptr<FirebaseRemove> removePtr(const String& path);
6452

6553
// Start a stream of events that affect value at `path`.
6654
FirebaseStream stream(const String& path);
67-
virtual std::unique_ptr<FirebaseStream> streamPtr(const String& path);
68-
69-
protected:
70-
// Used for testing.
71-
Firebase() {}
7255

7356
private:
74-
std::unique_ptr<FirebaseHttpClient> http_;
57+
HTTPClient http_;
7558
String host_;
7659
String auth_;
7760
};
@@ -94,20 +77,20 @@ class FirebaseCall {
9477
FirebaseCall() {}
9578
FirebaseCall(const String& host, const String& auth,
9679
const char* method, const String& path,
97-
const String& data = "",
98-
FirebaseHttpClient* http = NULL);
99-
virtual const FirebaseError& error() const {
80+
const String& data = "",
81+
HTTPClient* http = NULL);
82+
const FirebaseError& error() const {
10083
return error_;
10184
}
10285

103-
virtual const String& response() {
86+
const String& response() {
10487
return response_;
10588
}
10689

10790
const JsonObject& json();
10891

10992
protected:
110-
FirebaseHttpClient* http_;
93+
HTTPClient* http_;
11194
FirebaseError error_;
11295
String response_;
11396
DynamicJsonBuffer buffer_;
@@ -117,7 +100,7 @@ class FirebaseGet : public FirebaseCall {
117100
public:
118101
FirebaseGet() {}
119102
FirebaseGet(const String& host, const String& auth,
120-
const String& path, FirebaseHttpClient* http = NULL);
103+
const String& path, HTTPClient* http = NULL);
121104

122105
private:
123106
String json_;
@@ -127,8 +110,7 @@ class FirebaseSet: public FirebaseCall {
127110
public:
128111
FirebaseSet() {}
129112
FirebaseSet(const String& host, const String& auth,
130-
const String& path, const String& value, FirebaseHttpClient* http = NULL);
131-
113+
const String& path, const String& value, HTTPClient* http = NULL);
132114

133115
private:
134116
String json_;
@@ -138,9 +120,9 @@ class FirebasePush : public FirebaseCall {
138120
public:
139121
FirebasePush() {}
140122
FirebasePush(const String& host, const String& auth,
141-
const String& path, const String& value, FirebaseHttpClient* http = NULL);
123+
const String& path, const String& value, HTTPClient* http = NULL);
142124

143-
virtual const String& name() const {
125+
const String& name() const {
144126
return name_;
145127
}
146128

@@ -152,18 +134,18 @@ class FirebaseRemove : public FirebaseCall {
152134
public:
153135
FirebaseRemove() {}
154136
FirebaseRemove(const String& host, const String& auth,
155-
const String& path, FirebaseHttpClient* http = NULL);
137+
const String& path, HTTPClient* http = NULL);
156138
};
157139

158140

159141
class FirebaseStream : public FirebaseCall {
160142
public:
161143
FirebaseStream() {}
162144
FirebaseStream(const String& host, const String& auth,
163-
const String& path, FirebaseHttpClient* http = NULL);
145+
const String& path, HTTPClient* http = NULL);
164146

165147
// Return if there is any event available to read.
166-
virtual bool available();
148+
bool available();
167149

168150
// Event type.
169151
enum Event {
@@ -172,21 +154,8 @@ class FirebaseStream : public FirebaseCall {
172154
PATCH
173155
};
174156

175-
static inline String EventToName(Event event) {
176-
switch(event) {
177-
case UNKNOWN:
178-
return "UNKNOWN";
179-
case PUT:
180-
return "PUT";
181-
case PATCH:
182-
return "PATCH";
183-
default:
184-
return "INVALID_EVENT_" + event;
185-
}
186-
}
187-
188157
// Read next json encoded `event` from stream.
189-
virtual Event read(String& event);
158+
Event read(String& event);
190159

191160
const FirebaseError& error() const {
192161
return _error;

src/FirebaseHttpClient.h

-41
This file was deleted.

0 commit comments

Comments
 (0)