1
1
#include " SerialTransceiver.h"
2
2
3
+ #include < algorithm>
4
+
3
5
namespace firebase {
4
6
namespace modem {
5
7
6
8
void SerialTransceiver::begin (Stream* serial) {
7
- std::unique_ptr<Firebase> fbase;
8
-
9
9
in_.reset (new ArduinoInputStream (serial));
10
10
out_.reset (new ArduinoOutputStream (serial));
11
11
}
12
12
13
+ void SerialTransceiver::RegisterProtocol (std::unique_ptr<SerialProtocol> protocol) {
14
+ protocols_.push_back (std::move (protocol));
15
+ }
16
+
13
17
void SerialTransceiver::loop () {
14
18
String command_name = in_->readStringUntil (' ' );
15
19
@@ -18,44 +22,23 @@ void SerialTransceiver::loop() {
18
22
return ;
19
23
}
20
24
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 ;
25
32
}
26
- return ;
27
- } else if (!fbase_) {
28
- in_->drain ();
29
- out_->println (" -FAIL Must call BEGIN before anything else." );
30
- return ;
31
33
}
32
34
33
- std::unique_ptr<Command> command = CreateCommand (command_name, fbase_.get ());
34
- if (!command) {
35
+ if (!command_found) {
35
36
in_->drain ();
36
37
out_->println (String (" -FAIL Invalid command '" ) + command_name + " '." );
37
38
return ;
38
39
}
39
-
40
- command->execute (command_name, in_.get (), out_.get ());
41
40
}
42
41
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
42
60
43
} // modem
61
44
} // firebase
0 commit comments