Android-cuttlefish cvd tool
host_server.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <stdint.h>
20
21#include <atomic>
22#include <memory>
23#include <mutex>
24#include <string>
25#include <thread>
26#include <unordered_map>
27#include <vector>
28
29#include "absl/log/log.h"
30#include "fruit/fruit.h"
31#include "teeui/utils.h"
32
44
45namespace cuttlefish {
46namespace confui {
50};
51
53 public:
54 INJECT(HostServer(HostModeCtrl& host_mode_ctrl, ConfUiRenderer& host_renderer,
55 const PipeConnectionPair& fd_pair));
56
57 void Start(); // start this server itself
58 virtual ~HostServer() {}
59
60 // implement input interfaces. called by webRTC
61 void TouchEvent(int x, int y, bool is_down);
62 void UserAbortEvent();
63
64 private:
65 HostServer() = delete;
66
114 [[noreturn]] void MainLoop();
115 void HalCmdFetcherLoop();
116
117 bool IsVirtioConsoleOpen() const;
118 // If !IsVirtioConsoleOpen(), LOG(FATAL) and return false
119 bool CheckVirtioConsole();
120 std::shared_ptr<Session> CreateSession(const std::string& session_name);
121 void SendUserSelection(std::unique_ptr<ConfUiMessage>& input);
122
123 void Transition(std::unique_ptr<ConfUiMessage>& input_ptr);
124 std::string GetCurrentSessionId() {
125 if (curr_session_) {
126 return curr_session_->GetId();
127 }
128 return SESSION_ANY;
129 }
130
131 std::string GetCurrentState() {
132 if (!curr_session_) {
133 return {"kInvalid"};
134 }
135 return ToString(curr_session_->GetState());
136 }
137
138 const uint32_t display_num_;
141
142 std::shared_ptr<Session> curr_session_;
143
146
150 /*
151 * Multiplexer has N queues. When pop(), it is going to sleep until
152 * there's at least one item in at least one queue. The lower the Q
153 * index is, the higher the priority is.
154 *
155 * For HostServer, we have a queue for the user input events, and
156 * another for hal cmd/msg queues
157 */
159 int hal_cmd_q_id_; // Q id in input_multiplexer_
160 int user_input_evt_q_id_; // Q id in input_multiplexer_
161
162 std::thread main_loop_thread_;
164};
165
166} // end of namespace confui
167} // end of namespace cuttlefish
Definition: host_mode_ctrl.h:39
Definition: shared_fd.h:124
Definition: thread_safe_queue.h:37
Definition: host_renderer.h:64
Definition: host_server.h:52
int hal_cmd_q_id_
Definition: host_server.h:159
void TouchEvent(int x, int y, bool is_down)
Definition: host_server.cc:157
std::shared_ptr< Session > curr_session_
Definition: host_server.h:142
std::shared_ptr< Session > CreateSession(const std::string &session_name)
Definition: host_server.cc:235
ConfUiRenderer & host_renderer_
Definition: host_server.h:139
std::string GetCurrentSessionId()
Definition: host_server.h:124
void HalCmdFetcherLoop()
Definition: host_server.cc:111
void Start()
Definition: host_server.cc:99
std::thread hal_input_fetcher_thread_
Definition: host_server.h:163
std::thread main_loop_thread_
Definition: host_server.h:162
SharedFD from_guest_fifo_fd_
Definition: host_server.h:144
SharedFD to_guest_fifo_fd_
Definition: host_server.h:145
int user_input_evt_q_id_
Definition: host_server.h:160
void Transition(std::unique_ptr< ConfUiMessage > &input_ptr)
Definition: host_server.cc:249
const uint32_t display_num_
Definition: host_server.h:138
Multiplexer< std::unique_ptr< ConfUiMessage >, ThreadSafeQueue< std::unique_ptr< ConfUiMessage > > > Multiplexer
Definition: host_server.h:149
bool IsVirtioConsoleOpen() const
Definition: host_server.cc:87
HostModeCtrl & host_mode_ctrl_
Definition: host_server.h:140
void UserAbortEvent()
Definition: host_server.cc:166
void MainLoop()
Definition: host_server.cc:177
virtual ~HostServer()
Definition: host_server.h:58
void SendUserSelection(std::unique_ptr< ConfUiMessage > &input)
Definition: host_server.cc:145
INJECT(HostServer(HostModeCtrl &host_mode_ctrl, ConfUiRenderer &host_renderer, const PipeConnectionPair &fd_pair))
HostServer()=delete
Definition: host_server.cc:67
std::string GetCurrentState()
Definition: host_server.h:131
bool CheckVirtioConsole()
Definition: host_server.cc:91
Multiplexer input_multiplexer_
Definition: host_server.h:158
std::string ToString(const ConfUiMessage &msg)
Definition: protocol.cpp:93
constexpr auto SESSION_ANY
Definition: protocol.h:33
Definition: alloc_driver.h:20
Definition: host_server.h:47
SharedFD from_guest_
Definition: host_server.h:48
SharedFD to_guest_
Definition: host_server.h:49