11#include " SerialTransceiver.h"
22
3+ #include < algorithm>
4+
35namespace firebase {
46namespace modem {
57
68void SerialTransceiver::begin (Stream* serial) {
7- std::unique_ptr<Firebase> fbase;
8-
99 in_.reset (new ArduinoInputStream (serial));
1010 out_.reset (new ArduinoOutputStream (serial));
1111}
1212
13+ void SerialTransceiver::RegisterProtocol (std::unique_ptr<SerialProtocol> protocol) {
14+ protocols_.push_back (std::move (protocol));
15+ }
16+
1317void SerialTransceiver::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- } else if (!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 (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- }
5942
6043} // modem
6144} // firebase
0 commit comments