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

Commitdeaf832

Browse files
committed
Introduced module SerialProtocols, Moved current protocol to DatabaseProtocol, added unit test to SerialTransciever around delegation.
1 parenta072cb3 commitdeaf832

22 files changed

+299
-92
lines changed

‎src/modem/SerialProtocol.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef MODEM_SERIAL_PROTOCOL_H
2+
#defineMODEM_SERIAL_PROTOCOL_H
3+
4+
#include<string>
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+
*/
24+
virtualconst std::vector<std::string>&commands()const = 0;
25+
26+
/*
27+
* Execute command, takes over the serial line until execution is done.
28+
*/
29+
virtualvoidExecute(const std::string& command, InputStream* in, OutputStream* out) = 0;
30+
31+
};
32+
33+
}// modem
34+
}// firebase
35+
36+
#endif// MODEM_SERIAL_PROTOCOL_H

‎src/modem/SerialTransceiver.cpp

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ namespace firebase {
44
namespacemodem {
55

66
voidSerialTransceiver::begin(Stream* serial) {
7-
std::unique_ptr<Firebase> fbase;
8-
97
in_.reset(newArduinoInputStream(serial));
108
out_.reset(newArduinoOutputStream(serial));
119
}
1210

11+
voidSerialTransceiver::RegisterProtocol(std::unique_ptr<SerialProtocol> protocol) {
12+
for (const std::string& command : protocol->commands()) {
13+
command_to_protocol_.insert({command, protocol.get()});
14+
}
15+
16+
protocols_.push_back(std::move(protocol));
17+
}
18+
1319
voidSerialTransceiver::loop() {
1420
String command_name = in_->readStringUntil('');
1521

@@ -18,44 +24,15 @@ void SerialTransceiver::loop() {
1824
return;
1925
}
2026

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-
}elseif (!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) {
27+
auto itr = command_to_protocol_.find(command_name);
28+
if (itr == command_to_protocol_.end()) {
3529
in_->drain();
3630
out_->println(String("-FAIL Invalid command '") + command_name +"'." );
3731
return;
3832
}
39-
40-
command->execute(command_name, in_.get(), out_.get());
33+
itr->second->Execute(command_name, in_.get(), out_.get());
4134
}
4235

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-
}
5936

6037
}// modem
6138
}// firebase

‎src/modem/SerialTransceiver.h

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

44
#include<memory>
5+
#include<unordered_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_;
29+
std::unordered_map<std::string, SerialProtocol*> command_to_protocol_;
2330
};
2431

2532
}// 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<std::string> commands {
7+
"BEGIN_DB",
8+
"GET",
9+
"SET",
10+
"PUSH",
11+
"REMOVE",
12+
"BEGIN_STREAM"
13+
}
14+
}
15+
16+
const std::vector<std::string>&DatabaseProtocol::commands()const {
17+
return commands;
18+
}
19+
20+
voidDatabaseProtocol::Execute(const std::string& command, InputStream* in,
21+
OutputStream* out) {
22+
if (command_name =="BEGIN_DB") {
23+
BeginCommand command;
24+
if (command.execute(command_name, in_.get(), out_.get())) {
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 Invalid command '") + command_name +"'." );
38+
return;
39+
}
40+
41+
command->execute(command_name, in_.get(), out_.get());
42+
}
43+
44+
std::unique_ptr<Command>SerialTransceiver::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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
7+
namespacefirebase {
8+
namespacemodem {
9+
10+
classDatabaseProtocol :publicSerialProtocol {
11+
public:
12+
const std::vector<std::string>&commands()constoverride;
13+
voidExecute(const std::string& command, InputStream* in, OutputStream* out)override;
14+
private:
15+
std::unique_ptr<Command>CreateCommand(const String& text, Firebase* fbase);
16+
};
17+
18+
19+
}// modem
20+
}// firebase
21+
22+
23+
#endif// MODEM_DB_DATABASE_PROTOCOL_H

‎src/modem/begin-command.cpprenamed to‎src/modem/db/begin-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/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 {

‎test/dummies/Stream.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,35 @@
55

66
classStream {
77
public:
8-
intavailable() {
8+
virtualintavailable() {
99
return0;
1010
}
11-
StringreadStringUntil(constchar term__attribute__((unused))) {
11+
12+
virtual StringreadStringUntil(constchar term__attribute__((unused))) {
1213
returnString();
1314
}
14-
intprintln(const String&) {
15+
16+
virtualintprintln(const String&) {
1517
return0;
1618
}
17-
intprintln(constchar*) {
19+
20+
virtualintprintln(constchar*) {
1821
return0;
1922
}
20-
intprintln(int) {
23+
24+
virtualintprintln(int) {
2125
return0;
2226
}
23-
intprint(constchar*) {
27+
28+
virtualintprint(constchar*) {
2429
return0;
2530
}
26-
charpeek() {
31+
32+
virtualcharpeek() {
2733
return'\0';
2834
}
29-
charread() {
35+
36+
virtualcharread() {
3037
return'\0';
3138
}
3239
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp