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

Commit 8e81a0e

Browse files
committed
Imported all from modem-impl2
1 parent e1bc354 commit 8e81a0e

33 files changed

+1550
-0
lines changed

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "test/googletest"]
2+
path = test/googletest
3+
url = https://github.com/google/googletest.git
4+
[submodule "test/arduino-mock"]
5+
path = test/arduino-mock
6+
url = https://github.com/ed7coyne/arduino-mock.git

src/FirebaseHttpClient.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef FIREBASE_HTTP_CLIENT_H
2+
#define FIREBASE_HTTP_CLIENT_H
3+
4+
#include "Arduino.h"
5+
#include "Stream.h"
6+
7+
struct HttpStatus {
8+
static const int TEMPORARY_REDIRECT = 307;
9+
};
10+
11+
class FirebaseHttpClient {
12+
public:
13+
static FirebaseHttpClient* create();
14+
15+
virtual void setReuseConnection(bool reuse) = 0;
16+
virtual void begin(const String& url) = 0;
17+
virtual void begin(const String& host, const String& path) = 0;
18+
19+
virtual void end() = 0;
20+
21+
virtual void addHeader(const String& name, const String& value) = 0;
22+
virtual void collectHeaders(const char* header_keys[],
23+
const int header_key_count) = 0;
24+
virtual String header(const String& name) = 0;
25+
26+
virtual int sendRequest(const String& method, const String& data) = 0;
27+
28+
virtual String getString() = 0;
29+
30+
virtual Stream* getStreamPtr() = 0;
31+
32+
virtual String errorToString(int error_code) = 0;
33+
34+
protected:
35+
static const uint16_t kFirebasePort = 443;
36+
};
37+
38+
static const String kFirebaseFingerprint =
39+
"7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A";
40+
41+
#endif // FIREBASE_HTTP_CLIENT_H

src/FirebaseHttpClient_Esp8266.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
#include "FirebaseHttpClient.h"
3+
4+
// The ordering of these includes matters greatly.
5+
#include <WiFiClientSecure.h>
6+
#include <ESP8266WiFi.h>
7+
#include <ESP8266HTTPClient.h>
8+
9+
// Detect whether stable version of HTTP library is installed instead of
10+
// master branch and patch in missing status and methods.
11+
#ifndef HTTP_CODE_TEMPORARY_REDIRECT
12+
#define HTTP_CODE_TEMPORARY_REDIRECT 307
13+
#define USE_ESP_ARDUINO_CORE_2_0_0
14+
#endif
15+
16+
class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
17+
public:
18+
FirebaseHttpClientEsp8266() {}
19+
20+
void setReuseConnection(bool reuse) override {
21+
http_.setReuse(reuse);
22+
}
23+
24+
void begin(const String& url) override {
25+
http_.begin(url, kFirebaseFingerprint);
26+
}
27+
28+
void begin(const String& host, const String& path) override {
29+
http_.begin(host, kFirebasePort, path, true, kFirebaseFingerprint);
30+
}
31+
32+
void end() override {
33+
http_.end();
34+
}
35+
36+
void addHeader(const String& name, const String& value) override {
37+
http_.addHeader(name, value);
38+
}
39+
40+
void collectHeaders(const char* header_keys[], const int count) override {
41+
http_.collectHeaders(header_keys, count);
42+
}
43+
44+
String header(const String& name) override {
45+
return http_.header(name.c_str());
46+
}
47+
48+
int sendRequest(const String& method, const String& data) override {
49+
return http_.sendRequest(method.c_str(), (uint8_t*)data.c_str(), data.length());
50+
}
51+
52+
String getString() override {
53+
return http_.getString();
54+
}
55+
56+
Stream* getStreamPtr() override {
57+
return http_.getStreamPtr();
58+
}
59+
60+
String errorToString(int error_code) override {
61+
#ifdef USE_ESP_ARDUINO_CORE_2_0_0
62+
return String(error_code);
63+
#else
64+
return HTTPClient::errorToString(error_code);
65+
#endif
66+
}
67+
68+
private:
69+
HTTPClient http_;
70+
};
71+
72+
FirebaseHttpClient* FirebaseHttpClient::create() {
73+
return new FirebaseHttpClientEsp8266();
74+
}
75+

src/SerialTransceiver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "modem/SerialTransceiver.h"
2+
// Bring them into the base namespace for easier use in arduino ide.
3+
using firebase::modem::SerialTransceiver;

src/modem/SerialTransceiver.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "SerialTransceiver.h"
2+
3+
namespace firebase {
4+
namespace modem {
5+
6+
void SerialTransceiver::begin(Stream* serial) {
7+
std::unique_ptr<Firebase> fbase;
8+
9+
in_.reset(new ArduinoInputStream(serial));
10+
out_.reset(new ArduinoOutputStream(serial));
11+
}
12+
13+
void SerialTransceiver::loop() {
14+
String command_name = in_->readStringUntil(' ');
15+
16+
if (command_name.length() == 0 // Generally means a timeout has occured.
17+
|| command_name == "\n") {
18+
return;
19+
}
20+
21+
if (command_name == "BEGIN") {
22+
BeginCommand command;
23+
if (command.execute(command_name, in_.get(), out_.get())) {
24+
fbase_ = std::move(command.firebase());
25+
}
26+
return;
27+
} else if (!fbase_) {
28+
in_->drain();
29+
out_->println("-FAIL Must call BEGIN before anything else.");
30+
return;
31+
}
32+
33+
std::unique_ptr<Command> command = CreateCommand(command_name, fbase_.get());
34+
if (!command) {
35+
in_->drain();
36+
out_->println(String("-FAIL Invalid command '") + command_name + "'." );
37+
return;
38+
}
39+
40+
command->execute(command_name, in_.get(), out_.get());
41+
}
42+
43+
std::unique_ptr<Command> SerialTransceiver::CreateCommand(const String& text,
44+
Firebase* fbase) {
45+
std::unique_ptr<Command> command;
46+
if (text == "GET") {
47+
command.reset(new GetCommand(fbase));
48+
} else if (text == "SET") {
49+
command.reset(new SetCommand(fbase));
50+
} else if (text == "PUSH") {
51+
command.reset(new PushCommand(fbase));
52+
} else if (text == "REMOVE") {
53+
command.reset(new RemoveCommand(fbase));
54+
} else if (text == "BEGIN_STREAM") {
55+
command.reset(new StreamCommand(fbase));
56+
}
57+
return command;
58+
}
59+
60+
} // modem
61+
} // firebase

src/modem/SerialTransceiver.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef MODEM_SERIAL_TRANSCIEVER_H
2+
#define MODEM_SERIAL_TRANSCIEVER_H
3+
4+
#include <memory>
5+
6+
#include "Firebase.h"
7+
#include "modem/commands.h"
8+
9+
namespace firebase {
10+
namespace modem {
11+
12+
class SerialTransceiver {
13+
public:
14+
void begin(Stream* serial);
15+
void loop();
16+
17+
private:
18+
std::unique_ptr<Command> CreateCommand(const String& name, Firebase* fbase);
19+
20+
std::unique_ptr<Firebase> fbase_;
21+
std::unique_ptr<ArduinoInputStream> in_;
22+
std::unique_ptr<ArduinoOutputStream> out_;
23+
};
24+
25+
} // modem
26+
} // firebase
27+
28+
#endif // MODEM_SERIAL_TRANSCIEVER_H

src/modem/begin-command.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "modem/commands.h"
2+
3+
namespace firebase {
4+
namespace modem {
5+
6+
bool BeginCommand::execute(const String& command,
7+
InputStream* in, OutputStream* out) {
8+
if (in == nullptr || out == nullptr) {
9+
return false;
10+
}
11+
12+
if (command != "BEGIN") {
13+
return false;
14+
}
15+
16+
String host;
17+
String auth;
18+
19+
String data(in->readLine());
20+
21+
int space_index = data.indexOf(' ');
22+
if (space_index == -1) {
23+
// host only.
24+
host = data;
25+
} else {
26+
// host and auth.
27+
host = data.substring(0, space_index);
28+
auth = data.substring(space_index + 1);
29+
}
30+
31+
if (host.length() == 0) {
32+
out->println("-FAIL Missing host");
33+
return false;
34+
}
35+
36+
new_firebase_.reset(new Firebase(host));
37+
if (auth.length() != 0) {
38+
new_firebase_->auth(auth);
39+
}
40+
41+
out->println("+OK");
42+
return true;
43+
}
44+
45+
std::unique_ptr<Firebase> BeginCommand::firebase() {
46+
return std::move(new_firebase_);
47+
}
48+
49+
} // modem
50+
} // firebase

src/modem/commands.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#ifndef MODEM_COMMAND_H
2+
#define MODEM_COMMAND_H
3+
4+
#include "Firebase.h"
5+
#include "modem/output-stream.h"
6+
#include "modem/input-stream.h"
7+
8+
namespace firebase {
9+
namespace modem {
10+
11+
class Command {
12+
public:
13+
Command(Firebase* fbase) : fbase_(fbase) {}
14+
15+
// Execute command, reading any additional data needed from stream.
16+
// Return false if execution failed.
17+
virtual bool execute(const String& command,
18+
InputStream* in, OutputStream* out) = 0;
19+
protected:
20+
Firebase& fbase() {
21+
return *fbase_;
22+
}
23+
24+
private:
25+
Firebase* fbase_;
26+
};
27+
28+
class GetCommand : public Command {
29+
public:
30+
GetCommand(Firebase* fbase) : Command(fbase) {}
31+
32+
bool execute(const String& command, InputStream* in, OutputStream* out);
33+
};
34+
35+
class SetCommand : public Command {
36+
public:
37+
SetCommand(Firebase* fbase) : Command(fbase) {}
38+
39+
bool execute(const String& command, InputStream* in, OutputStream* out);
40+
};
41+
42+
class RemoveCommand : public Command {
43+
public:
44+
RemoveCommand(Firebase* fbase) : Command(fbase) {}
45+
46+
bool execute(const String& command, InputStream* in, OutputStream* out);
47+
};
48+
49+
class PushCommand : public Command {
50+
public:
51+
PushCommand(Firebase* fbase) : Command(fbase) {}
52+
53+
bool execute(const String& command, InputStream* in, OutputStream* out);
54+
};
55+
56+
class BeginCommand : public Command {
57+
public:
58+
BeginCommand() : Command(nullptr) {}
59+
60+
bool execute(const String& command, InputStream* in, OutputStream* out);
61+
62+
// This can only be called once.
63+
std::unique_ptr<Firebase> firebase();
64+
65+
private:
66+
std::unique_ptr<Firebase> new_firebase_;
67+
};
68+
69+
class StreamCommand : public Command {
70+
public:
71+
StreamCommand(Firebase* fbase) : Command(fbase) {}
72+
73+
bool execute(const String& command, InputStream* in, OutputStream* out);
74+
};
75+
76+
} // modem
77+
} // firebase
78+
79+
#endif //MODEM_COMMAND_H

src/modem/design.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Overview:
2+
3+
![Design diagram](diagram.png)
4+
5+
We create a SerialTransceiver object that will manage the serial connection. It will read the first word on a line to determine which command is being called and construct the appropriate Command subclass to handle the interaction. Commands will be short lived objects with no state between calls, they will own the serial connection until they hand it back and are responsible for handling all necessary input and leaving the input line in a clean state for the next command.
6+
7+
We will aim to keep the Transceiver/Command interaction generic enough to enable creating additional Protocol Transceivers in the future.
8+
9+
The Transceiver object will also own the Firebase object and present it to each command during construction. The Firebase object is our link to the firebase backend.
10+

src/modem/diagram.png

15.6 KB
Loading

0 commit comments

Comments
 (0)