740fde01aeea03f0b5158d443040a747885b8c9b
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / qemu / camera_message_processor.cpp
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
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.
7  *
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.
12  *
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/>.
15  *
16  */
17
18 #include "anbox/qemu/camera_message_processor.h"
19 #include "anbox/logger.h"
20
21 namespace anbox {
22 namespace qemu {
23 CameraMessageProcessor::CameraMessageProcessor(
24     const std::shared_ptr<network::SocketMessenger> &messenger)
25     : messenger_(messenger) {}
26
27 CameraMessageProcessor::~CameraMessageProcessor() {}
28
29 bool CameraMessageProcessor::process_data(
30     const std::vector<std::uint8_t> &data) {
31   for (const auto &byte : data) buffer_.push_back(byte);
32
33   process_commands();
34
35   return true;
36 }
37
38 void CameraMessageProcessor::process_commands() {
39   while (buffer_.size() > 0) {
40     size_t size = 0;
41     while (size < buffer_.size()) {
42       if (buffer_.at(size) == 0x0) break;
43       size++;
44     }
45
46     std::string command;
47     command.insert(0, reinterpret_cast<const char *>(buffer_.data()), size);
48     buffer_.erase(buffer_.begin(), buffer_.begin() + size + 1);
49
50     handle_command(command);
51   }
52 }
53
54 void CameraMessageProcessor::handle_command(const std::string &command) {
55   if (command == "list") list();
56 }
57
58 void CameraMessageProcessor::list() {
59   char buf[5];
60   snprintf(buf, 5, "\n");
61   messenger_->send(buf, strlen(buf));
62 }
63 }  // namespace qemu
64 }  // namespace anbox