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

Commit 1b589b9

Browse files
committed
Switch to binary search
1 parent e765a31 commit 1b589b9

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class SerialProtocol {
2020

2121
/*
2222
* Returns all commands this protocol supports, commands are single words.
23+
* Returned vector MUST be sorted.
2324
*/
2425
virtual const std::vector<String>& commands() const = 0;
2526

src/modem/SerialTransceiver.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "SerialTransceiver.h"
22

3+
#include <algorithm>
4+
35
namespace firebase {
46
namespace modem {
57

@@ -9,10 +11,6 @@ void SerialTransceiver::begin(Stream* serial) {
911
}
1012

1113
void SerialTransceiver::RegisterProtocol(std::unique_ptr<SerialProtocol> protocol) {
12-
for (const String& command : protocol->commands()) {
13-
command_to_protocol_.insert({command, protocol.get()});
14-
}
15-
1614
protocols_.push_back(std::move(protocol));
1715
}
1816

@@ -24,13 +22,21 @@ void SerialTransceiver::loop() {
2422
return;
2523
}
2624

27-
auto itr = command_to_protocol_.find(command_name);
28-
if (itr == command_to_protocol_.end()) {
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;
32+
}
33+
}
34+
35+
if (!command_found) {
2936
in_->drain();
3037
out_->println(String("-FAIL Invalid command '") + command_name + "'." );
3138
return;
3239
}
33-
itr->second->Execute(command_name, in_.get(), out_.get());
3440
}
3541

3642

src/modem/SerialTransceiver.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class SerialTransceiver {
2626
std::unique_ptr<ArduinoInputStream> in_;
2727
std::unique_ptr<ArduinoOutputStream> out_;
2828
std::vector<std::unique_ptr<SerialProtocol>> protocols_;
29-
std::map<String, SerialProtocol*> command_to_protocol_;
3029
};
3130

3231
} // modem

0 commit comments

Comments
 (0)