2 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "anbox/qemu//at_parser.h"
19 #include "anbox/logger.h"
20 #include "anbox/utils.h"
24 AtParser::AtParser() {}
26 void AtParser::register_command(const std::string &command,
27 CommandHandler handler) {
28 handlers_.insert({command, handler});
31 void AtParser::process_data(std::vector<std::uint8_t> &data) {
32 size_t bytes_processed = 0;
33 for (size_t pos = 0; pos < data.size();) {
34 const auto byte = data.at(pos);
35 if (byte == '\n' || byte == '\r') {
38 0, reinterpret_cast<const char *>(data.data()) + bytes_processed,
39 pos - bytes_processed);
40 bytes_processed += (pos - bytes_processed) + 1;
41 processs_command(command);
46 data.erase(data.begin(), data.begin() + bytes_processed);
49 void AtParser::processs_command(const std::string &command) {
50 if (!utils::string_starts_with(command, "AT")) {
51 WARNING("Invalid AT command: '%s'", command);
55 // Strip AT prefix from command
56 auto real_command = command.substr(2, command.length() - 2);
58 DEBUG("command: %s", real_command);
60 CommandHandler handler = nullptr;
61 for (const auto &iter : handlers_) {
62 if (utils::string_starts_with(real_command, iter.first)) {
63 handler = iter.second;
69 WARNING("No handler for command '%s' available", real_command);
73 handler(real_command);