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 <atomic>
20#include <cstdint>
21#include <memory>
22#include <mutex>
23#include <string>
24#include <thread>
25#include <unordered_map>
26#include <vector>
27
29#include <fruit/fruit.h>
30#include <teeui/utils.h>
31
43
44namespace cuttlefish {
45namespace confui {
49};
50
52 public:
53 INJECT(HostServer(HostModeCtrl& host_mode_ctrl, ConfUiRenderer& host_renderer,
54 const PipeConnectionPair& fd_pair));
55
56 void Start(); // start this server itself
57 virtual ~HostServer() {}
58
59 // implement input interfaces. called by webRTC
60 void TouchEvent(const int x, const int y, const bool is_down);
61 void UserAbortEvent();
62
63 private:
64 HostServer() = delete;
65
113 [[noreturn]] void MainLoop();
114 void HalCmdFetcherLoop();
115
116 bool IsVirtioConsoleOpen() const;
117 // If !IsVirtioConsoleOpen(), LOG(FATAL) and return false
118 bool CheckVirtioConsole();
119 std::shared_ptr<Session> CreateSession(const std::string& session_name);
120 void SendUserSelection(std::unique_ptr<ConfUiMessage>& input);
121
122 void Transition(std::unique_ptr<ConfUiMessage>& input_ptr);
123 std::string GetCurrentSessionId() {
124 if (curr_session_) {
125 return curr_session_->GetId();
126 }
127 return SESSION_ANY;
128 }
129
130 std::string GetCurrentState() {
131 if (!curr_session_) {
132 return {"kInvalid"};
133 }
134 return ToString(curr_session_->GetState());
135 }
136
137 const std::uint32_t display_num_;
140
141 std::shared_ptr<Session> curr_session_;
142
145
149 /*
150 * Multiplexer has N queues. When pop(), it is going to sleep until
151 * there's at least one item in at least one queue. The lower the Q
152 * index is, the higher the priority is.
153 *
154 * For HostServer, we have a queue for the user input events, and
155 * another for hal cmd/msg queues
156 */
158 int hal_cmd_q_id_; // Q id in input_multiplexer_
159 int user_input_evt_q_id_; // Q id in input_multiplexer_
160
161 std::thread main_loop_thread_;
163};
164
165} // end of namespace confui
166} // end of namespace cuttlefish
Definition: host_mode_ctrl.h:38
Definition: shared_fd.h:129
Definition: thread_safe_queue.h:35
Definition: host_renderer.h:65
Definition: host_server.h:51
const std::uint32_t display_num_
Definition: host_server.h:137
int hal_cmd_q_id_
Definition: host_server.h:158
std::shared_ptr< Session > curr_session_
Definition: host_server.h:141
std::shared_ptr< Session > CreateSession(const std::string &session_name)
Definition: host_server.cc:234
ConfUiRenderer & host_renderer_
Definition: host_server.h:138
std::string GetCurrentSessionId()
Definition: host_server.h:123
void HalCmdFetcherLoop()
Definition: host_server.cc:110
void Start()
Definition: host_server.cc:98
std::thread hal_input_fetcher_thread_
Definition: host_server.h:162
std::thread main_loop_thread_
Definition: host_server.h:161
SharedFD from_guest_fifo_fd_
Definition: host_server.h:143
SharedFD to_guest_fifo_fd_
Definition: host_server.h:144
void TouchEvent(const int x, const int y, const bool is_down)
Definition: host_server.cc:156
int user_input_evt_q_id_
Definition: host_server.h:159
void Transition(std::unique_ptr< ConfUiMessage > &input_ptr)
Definition: host_server.cc:248
Multiplexer< std::unique_ptr< ConfUiMessage >, ThreadSafeQueue< std::unique_ptr< ConfUiMessage > > > Multiplexer
Definition: host_server.h:148
bool IsVirtioConsoleOpen() const
Definition: host_server.cc:86
HostModeCtrl & host_mode_ctrl_
Definition: host_server.h:139
void UserAbortEvent()
Definition: host_server.cc:165
void MainLoop()
Definition: host_server.cc:176
virtual ~HostServer()
Definition: host_server.h:57
void SendUserSelection(std::unique_ptr< ConfUiMessage > &input)
Definition: host_server.cc:144
INJECT(HostServer(HostModeCtrl &host_mode_ctrl, ConfUiRenderer &host_renderer, const PipeConnectionPair &fd_pair))
HostServer()=delete
Definition: host_server.cc:66
std::string GetCurrentState()
Definition: host_server.h:130
bool CheckVirtioConsole()
Definition: host_server.cc:90
Multiplexer input_multiplexer_
Definition: host_server.h:157
std::string ToString(const ConfUiMessage &msg)
Definition: protocol.cpp:85
constexpr auto SESSION_ANY
Definition: protocol.h:34
Definition: alloc_utils.cpp:23
Definition: host_server.h:46
SharedFD from_guest_
Definition: host_server.h:47
SharedFD to_guest_
Definition: host_server.h:48