Android-cuttlefish cvd tool
session.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 <chrono>
21#include <memory>
22#include <string>
23
25
32
33namespace cuttlefish {
34namespace confui {
35
43class Session {
44 public:
45 Session(const std::string& session_name, const std::uint32_t display_num,
46 ConfUiRenderer& host_renderer, HostModeCtrl& host_mode_ctrl,
47 const std::string& locale = "en");
48
49 std::string GetId() { return session_id_; }
50
52
53 MainLoopState Transition(SharedFD& hal_cli, const FsmInput fsm_input,
54 const ConfUiMessage& conf_ui_message);
55
59 bool Suspend(SharedFD hal_cli);
60
64 bool Restore(SharedFD hal_cli);
65
66 // abort session
67 void Abort();
68
69 // client on the host wants to abort
70 // should let the guest know it
71 void UserAbort(SharedFD hal_cli);
72
73 bool IsSuspended() const;
74 void CleanUp();
75
76 bool IsConfirm(const int x, const int y) {
77 return renderer_.IsInConfirm(x, y);
78 }
79
80 bool IsCancel(const int x, const int y) { return renderer_.IsInCancel(x, y); }
81
82 // tell if grace period has passed
83 bool IsReadyForUserInput() const;
84
85 private:
86 bool IsUserInput(const FsmInput fsm_input) {
87 return fsm_input == FsmInput::kUserEvent;
88 }
89
94 bool RenderDialog();
95
96 // transition actions on each state per input
97 // the new state will be save to the state_ at the end of each call
98 //
99 // when false is returned, the FSM must terminate
100 // and, no need to let the guest know
101 bool HandleInit(SharedFD hal_cli, const FsmInput fsm_input,
102 const ConfUiMessage& conf_ui_message);
103
104 bool HandleWaitStop(SharedFD hal_cli, const FsmInput fsm_input);
105
106 bool HandleInSession(SharedFD hal_cli, const FsmInput fsm_input,
107 const ConfUiMessage& conf_ui_msg);
108
109 // report with an error ack to HAL, and reset the FSM
110 bool ReportErrorToHal(SharedFD hal_cli, const std::string& msg);
111
112 void ScheduleToTerminate();
113
114 const std::string session_id_;
115 const std::uint32_t display_num_;
118
119 // only context to save
120 std::string prompt_text_;
121 std::string locale_;
122 std::vector<teeui::UIOption> ui_options_;
123 std::vector<std::uint8_t> extra_data_;
124 // the second argument for resultCB of promptUserConfirmation
125 std::vector<std::uint8_t> signed_confirmation_;
126 std::vector<std::uint8_t> message_;
127
128 std::unique_ptr<Cbor> cbor_;
129
130 // effectively, this variables are shared with webRTC thread
131 // the input demuxer will check the confirmation UI mode based on this
132 std::atomic<MainLoopState> state_;
133 MainLoopState saved_state_; // for restore/suspend
134 using Clock = std::chrono::steady_clock;
135 using TimePoint = std::chrono::time_point<Clock>;
136 std::unique_ptr<TimePoint> start_time_;
137};
138} // end of namespace confui
139} // end of namespace cuttlefish
Definition: host_mode_ctrl.h:38
Definition: shared_fd.h:129
Definition: protocol_types.h:75
Definition: host_renderer.h:65
bool IsInCancel(const std::uint32_t x, const std::uint32_t y)
Definition: host_renderer.cc:435
bool IsInConfirm(const std::uint32_t x, const std::uint32_t y)
Definition: host_renderer.cc:429
Definition: session.h:43
std::vector< std::uint8_t > message_
Definition: session.h:126
std::string GetId()
Definition: session.h:49
bool IsUserInput(const FsmInput fsm_input)
Definition: session.h:86
std::chrono::time_point< Clock > TimePoint
Definition: session.h:135
void Abort()
Definition: session.cc:121
bool IsReadyForUserInput() const
Definition: session.cc:47
std::string prompt_text_
Definition: session.h:120
std::vector< std::uint8_t > extra_data_
Definition: session.h:123
bool Suspend(SharedFD hal_cli)
MainLoopState GetState()
Definition: session.h:51
bool IsCancel(const int x, const int y)
Definition: session.h:80
bool RenderDialog()
Definition: session.cc:55
bool HandleInit(SharedFD hal_cli, const FsmInput fsm_input, const ConfUiMessage &conf_ui_message)
Definition: session.cc:133
void ScheduleToTerminate()
Definition: session.cc:107
std::vector< teeui::UIOption > ui_options_
Definition: session.h:122
void UserAbort(SharedFD hal_cli)
Definition: session.cc:126
bool HandleWaitStop(SharedFD hal_cli, const FsmInput fsm_input)
Definition: session.cc:250
std::unique_ptr< TimePoint > start_time_
Definition: session.h:136
std::unique_ptr< Cbor > cbor_
Definition: session.h:128
HostModeCtrl & host_mode_ctrl_
Definition: session.h:117
const std::string session_id_
Definition: session.h:114
ConfUiRenderer & renderer_
Definition: session.h:116
void CleanUp()
Definition: session.cc:98
std::atomic< MainLoopState > state_
Definition: session.h:132
bool HandleInSession(SharedFD hal_cli, const FsmInput fsm_input, const ConfUiMessage &conf_ui_msg)
Definition: session.cc:198
MainLoopState Transition(SharedFD &hal_cli, const FsmInput fsm_input, const ConfUiMessage &conf_ui_message)
Definition: session.cc:65
MainLoopState saved_state_
Definition: session.h:133
Session(const std::string &session_name, const std::uint32_t display_num, ConfUiRenderer &host_renderer, HostModeCtrl &host_mode_ctrl, const std::string &locale="en")
Definition: session.cc:24
std::string locale_
Definition: session.h:121
std::vector< std::uint8_t > signed_confirmation_
Definition: session.h:125
bool IsConfirm(const int x, const int y)
Definition: session.h:76
bool ReportErrorToHal(SharedFD hal_cli, const std::string &msg)
Definition: session.cc:112
const std::uint32_t display_num_
Definition: session.h:115
bool Restore(SharedFD hal_cli)
std::chrono::steady_clock Clock
Definition: session.h:134
FsmInput
Definition: server_common.h:39
MainLoopState
Definition: server_common.h:27
Definition: alloc_utils.cpp:23