Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit854bb0c

Browse files
authored
Merge pull requestFirebaseExtended#189 from ed7coyne/modular-protocols
Add concept of multiple protocols to SerialTransciever.
2 parentsb36352d +6b4c5ba commit854bb0c

28 files changed

+318
-95
lines changed

‎examples/FirebaseSerialHost_ESP8266/FirebaseSerialHost_ESP8266.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void setup() {
5555
delay(5000);
5656
}
5757

58+
transceiver.RegisterProtocol(newDatabaseProtocol());
5859
transceiver.begin(&data_serial);
5960
}
6061

‎examples/FirebaseSerialTerminal_ESP8266/FirebaseSerialTerminal_ESP8266.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void setup() {
5555
Serial.print("connected:");
5656
Serial.println(WiFi.localIP());
5757

58+
transceiver.RegisterProtocol(newDatabaseProtocol());
5859
transceiver.begin(&Serial);
5960
}
6061

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
BEGIN $YOUR_HOST $YOUR_SECRET
1+
BEGIN_DB $YOUR_HOST $YOUR_SECRET
22

‎src/SerialTransceiver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include<string>
22

33
#include"modem/SerialTransceiver.h"
4+
#include"modem/db/DatabaseProtocol.h"
5+
46
// Bring them into the base namespace for easier use in arduino ide.
57
using firebase::modem::SerialTransceiver;
8+
using firebase::modem::DatabaseProtocol;

‎src/modem/SerialProtocol.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef MODEM_SERIAL_PROTOCOL_H
2+
#defineMODEM_SERIAL_PROTOCOL_H
3+
4+
#include<WString.h>
5+
#include<vector>
6+
7+
namespacefirebase {
8+
namespacemodem {
9+
10+
classInputStream;
11+
classOutputStream;
12+
13+
/*
14+
* Define generic baseclass for all serial protocols that wish to share
15+
* the common commandspace.
16+
*/
17+
classSerialProtocol {
18+
public:
19+
virtual~SerialProtocol() =default;
20+
21+
/*
22+
* Returns all commands this protocol supports, commands are single words.
23+
* Returned vector MUST be sorted.
24+
*/
25+
virtualconst std::vector<String>&commands()const = 0;
26+
27+
/*
28+
* Execute command, takes over the serial line until execution is done.
29+
*/
30+
virtualvoidExecute(const String& command, InputStream* in, OutputStream* out) = 0;
31+
32+
};
33+
34+
}// modem
35+
}// firebase
36+
37+
#endif// MODEM_SERIAL_PROTOCOL_H

‎src/modem/SerialTransceiver.cpp

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#include"SerialTransceiver.h"
22

3+
#include<algorithm>
4+
35
namespacefirebase {
46
namespacemodem {
57

68
voidSerialTransceiver::begin(Stream* serial) {
7-
std::unique_ptr<Firebase> fbase;
8-
99
in_.reset(newArduinoInputStream(serial));
1010
out_.reset(newArduinoOutputStream(serial));
1111
}
1212

13+
voidSerialTransceiver::RegisterProtocol(std::unique_ptr<SerialProtocol> protocol) {
14+
protocols_.push_back(std::move(protocol));
15+
}
16+
1317
voidSerialTransceiver::loop() {
1418
String command_name = in_->readStringUntil('');
1519

@@ -18,44 +22,23 @@ void SerialTransceiver::loop() {
1822
return;
1923
}
2024

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+
bool command_found =false;
26+
for (auto& protocol : protocols_) {
27+
const std::vector<String>& commands = protocol->commands();
28+
if (std::binary_search(commands.begin(), commands.end(), command_name)) {
29+
protocol->Execute(command_name, in_.get(), out_.get());
30+
command_found =true;
31+
break;
2532
}
26-
return;
27-
}elseif (!fbase_) {
28-
in_->drain();
29-
out_->println("-FAIL Must call BEGIN before anything else.");
30-
return;
3133
}
3234

33-
std::unique_ptr<Command> command =CreateCommand(command_name, fbase_.get());
34-
if (!command) {
35+
if (!command_found) {
3536
in_->drain();
3637
out_->println(String("-FAIL Invalid command '") + command_name +"'." );
3738
return;
3839
}
39-
40-
command->execute(command_name, in_.get(), out_.get());
4140
}
4241

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(newGetCommand(fbase));
48-
}elseif (text =="SET") {
49-
command.reset(newSetCommand(fbase));
50-
}elseif (text =="PUSH") {
51-
command.reset(newPushCommand(fbase));
52-
}elseif (text =="REMOVE") {
53-
command.reset(newRemoveCommand(fbase));
54-
}elseif (text =="BEGIN_STREAM") {
55-
command.reset(newStreamCommand(fbase));
56-
}
57-
return command;
58-
}
5942

6043
}// modem
6144
}// firebase

‎src/modem/SerialTransceiver.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@
22
#defineMODEM_SERIAL_TRANSCIEVER_H
33

44
#include<memory>
5+
#include<map>
56

6-
#include"Firebase.h"
7-
#include"modem/commands.h"
7+
#include"modem/SerialProtocol.h"
8+
#include"modem/input-stream.h"
9+
#include"modem/output-stream.h"
810

911
namespacefirebase {
1012
namespacemodem {
1113

1214
classSerialTransceiver {
1315
public:
16+
voidRegisterProtocol(std::unique_ptr<SerialProtocol> protocol);
17+
// Also takes ownership as above but more arduino friendly.
18+
voidRegisterProtocol(SerialProtocol* protocol) {
19+
RegisterProtocol(std::unique_ptr<SerialProtocol>(protocol));
20+
}
21+
1422
voidbegin(Stream* serial);
1523
voidloop();
1624

1725
private:
18-
std::unique_ptr<Command>CreateCommand(const String& name, Firebase* fbase);
19-
20-
std::unique_ptr<Firebase> fbase_;
2126
std::unique_ptr<ArduinoInputStream> in_;
2227
std::unique_ptr<ArduinoOutputStream> out_;
28+
std::vector<std::unique_ptr<SerialProtocol>> protocols_;
2329
};
2430

2531
}// modem

‎src/modem/command.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef MODEM_COMMAND_H
2+
#defineMODEM_COMMAND_H
3+
4+
#include"Firebase.h"
5+
#include"modem/output-stream.h"
6+
#include"modem/input-stream.h"
7+
8+
namespacefirebase {
9+
namespacemodem {
10+
11+
classCommand {
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+
virtualboolexecute(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+
}// modem
29+
}// firebase
30+
31+
#endif//MODEM_COMMAND_H

‎src/modem/db/DatabaseProtocol.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include"modem/db/DatabaseProtocol.h"
2+
3+
namespacefirebase {
4+
namespacemodem {
5+
namespace {
6+
const std::vector<String>kCommands {
7+
"BEGIN_DB",
8+
"GET",
9+
"SET",
10+
"PUSH",
11+
"REMOVE",
12+
"BEGIN_STREAM"
13+
};
14+
}
15+
16+
const std::vector<String>&DatabaseProtocol::commands()const {
17+
returnkCommands;
18+
}
19+
20+
voidDatabaseProtocol::Execute(const String& command_name, InputStream* in,
21+
OutputStream* out) {
22+
if (command_name =="BEGIN_DB") {
23+
BeginCommand command;
24+
if (command.execute(command_name, in, out)) {
25+
fbase_ =std::move(command.firebase());
26+
}
27+
return;
28+
}elseif (!fbase_) {
29+
in->drain();
30+
out->println("-FAIL Must call BEGIN_DB before anything else.");
31+
return;
32+
}
33+
34+
std::unique_ptr<Command> command =CreateCommand(command_name, fbase_.get());
35+
if (!command) {
36+
in->drain();
37+
out->println(String("-FAIL unhandled command '") + command_name +"'." );
38+
return;
39+
}
40+
41+
command->execute(command_name, in, out);
42+
}
43+
44+
std::unique_ptr<Command>DatabaseProtocol::CreateCommand(const String& text,
45+
Firebase* fbase) {
46+
std::unique_ptr<Command> command;
47+
if (text =="GET") {
48+
command.reset(newGetCommand(fbase));
49+
}elseif (text =="SET") {
50+
command.reset(newSetCommand(fbase));
51+
}elseif (text =="PUSH") {
52+
command.reset(newPushCommand(fbase));
53+
}elseif (text =="REMOVE") {
54+
command.reset(newRemoveCommand(fbase));
55+
}elseif (text =="BEGIN_STREAM") {
56+
command.reset(newStreamCommand(fbase));
57+
}
58+
return command;
59+
}
60+
}// modem
61+
}// firebase

‎src/modem/db/DatabaseProtocol.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef MDOEM_DB_DATABASE_PROTOCOL_H
2+
#defineMODEM_DB_DATABASE_PROTOCOL_H
3+
4+
#include"modem/SerialProtocol.h"
5+
#include"modem/db/commands.h"
6+
#include"Firebase.h"
7+
8+
namespacefirebase {
9+
namespacemodem {
10+
11+
classDatabaseProtocol :publicSerialProtocol {
12+
public:
13+
const std::vector<String>&commands()constoverride;
14+
voidExecute(const String& command, InputStream* in, OutputStream* out)override;
15+
private:
16+
std::unique_ptr<Command>CreateCommand(const String& text, Firebase* fbase);
17+
18+
std::unique_ptr<Firebase> fbase_;
19+
};
20+
21+
22+
}// modem
23+
}// firebase
24+
25+
26+
#endif// MODEM_DB_DATABASE_PROTOCOL_H

‎src/modem/begin-command.cpprenamed to‎src/modem/db/begin-command.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include"modem/commands.h"
1+
#include"modem/db/commands.h"
22

33
namespacefirebase {
44
namespacemodem {
@@ -9,7 +9,7 @@ bool BeginCommand::execute(const String& command,
99
returnfalse;
1010
}
1111

12-
if (command !="BEGIN") {
12+
if (command !="BEGIN_DB") {
1313
returnfalse;
1414
}
1515

‎src/modem/commands.hrenamed to‎src/modem/db/commands.h

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
1-
#ifndefMODEM_COMMAND_H
2-
#defineMODEM_COMMAND_H
1+
#ifndefMODEM_DB_COMMANDS_H
2+
#defineMODEM_DB_COMMANDS_H
33

44
#include"Firebase.h"
5+
#include"modem/command.h"
56
#include"modem/output-stream.h"
67
#include"modem/input-stream.h"
78

89
namespacefirebase {
910
namespacemodem {
1011

11-
classCommand {
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-
virtualboolexecute(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-
2812
classGetCommand :publicCommand {
2913
public:
3014
GetCommand(Firebase* fbase) : Command(fbase) {}
@@ -76,4 +60,4 @@ class StreamCommand : public Command {
7660
}// modem
7761
}// firebase
7862

79-
#endif//MODEM_COMMAND_H
63+
#endif//MODEM_DB_COMMANDS_H

‎src/modem/get-command.cpprenamed to‎src/modem/db/get-command.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include"modem/commands.h"
1+
#include"modem/db/commands.h"
22

33
namespacefirebase {
44
namespacemodem {

‎src/modem/push-command.cpprenamed to‎src/modem/db/push-command.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include"modem/commands.h"
1+
#include"modem/db/commands.h"
22
#include"modem/json_util.h"
33

44
namespacefirebase {

‎src/modem/remove-command.cpprenamed to‎src/modem/db/remove-command.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include"modem/commands.h"
1+
#include"modem/db/commands.h"
22

33
namespacefirebase {
44
namespacemodem {

‎src/modem/set-command.cpprenamed to‎src/modem/db/set-command.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include"modem/commands.h"
1+
#include"modem/db/commands.h"
22
#include"modem/json_util.h"
33

44
namespacefirebase {

‎src/modem/stream-command.cpprenamed to‎src/modem/db/stream-command.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include"modem/commands.h"
1+
#include"modem/db/commands.h"
22

33
namespacefirebase {
44
namespacemodem {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp