aboutsummaryrefslogtreecommitdiff
path: root/host/libs/confui/session.cc
blob: 3d365c61764def7c04e87d41faac0cc11389159e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "host/libs/confui/session.h"

namespace cuttlefish {
namespace confui {

Session::Session(const std::string& session_id, const std::uint32_t display_num,
                 ConfUiRenderer& host_renderer, HostModeCtrl& host_mode_ctrl,
                 ScreenConnectorFrameRenderer& screen_connector,
                 const std::string& locale)
    : session_id_{session_id},
      display_num_{display_num},
      renderer_{host_renderer},
      host_mode_ctrl_{host_mode_ctrl},
      screen_connector_{screen_connector},
      locale_{locale},
      state_{MainLoopState::kInit},
      saved_state_{MainLoopState::kInit} {}

bool Session::IsConfUiActive() const {
  if (state_ == MainLoopState::kInSession ||
      state_ == MainLoopState::kWaitStop) {
    return true;
  }
  return false;
}

bool Session::RenderDialog(const std::string& msg, const std::string& locale) {
  auto [teeui_frame, is_success] = renderer_.RenderRawFrame(msg, locale);
  if (!is_success) {
    return false;
  }
  prompt_ = msg;
  locale_ = locale;

  ConfUiLog(DEBUG) << "actually trying to render the frame"
                   << thread::GetName();
  auto raw_frame = reinterpret_cast<std::uint8_t*>(teeui_frame.data());
  return screen_connector_.RenderConfirmationUi(display_num_, raw_frame);
}

bool Session::IsSuspended() const {
  return (state_ == MainLoopState::kSuspended);
}

MainLoopState Session::Transition(const bool is_user_input, SharedFD& hal_cli,
                                  const FsmInput fsm_input,
                                  const std::string& additional_info) {
  switch (state_) {
    case MainLoopState::kInit: {
      HandleInit(is_user_input, hal_cli, fsm_input, additional_info);
    } break;
    case MainLoopState::kInSession: {
      HandleInSession(is_user_input, hal_cli, fsm_input);
    } break;
    case MainLoopState::kWaitStop: {
      if (is_user_input) {
        ConfUiLog(DEBUG) << "User input ignored" << ToString(fsm_input) << " : "
                         << additional_info << "at state" << ToString(state_);
      }
      HandleWaitStop(is_user_input, hal_cli, fsm_input);
    } break;
    default:
      // host service explicitly calls restore and suspend
      ConfUiLog(FATAL) << "Must not be in the state of" << ToString(state_);
      break;
  }
  return state_;
};

bool Session::Suspend(SharedFD hal_cli) {
  if (state_ == MainLoopState::kInit) {
    // HAL sent wrong command
    ConfUiLog(FATAL)
        << "HAL sent wrong command, suspend, when the session is in kIinit";
    return false;
  }
  if (state_ == MainLoopState::kSuspended) {
    ConfUiLog(DEBUG) << "Already kSuspended state";
    return false;
  }
  saved_state_ = state_;
  state_ = MainLoopState::kSuspended;
  host_mode_ctrl_.SetMode(HostModeCtrl::ModeType::kAndroidMode);
  if (!packet::SendAck(hal_cli, session_id_, /*is success*/ true,
                       "suspended")) {
    ConfUiLog(FATAL) << "I/O error";
    return false;
  }
  return true;
}

bool Session::Restore(SharedFD hal_cli) {
  if (state_ == MainLoopState::kInit) {
    // HAL sent wrong command
    ConfUiLog(FATAL)
        << "HAL sent wrong command, restore, when the session is in kIinit";
    return false;
  }

  if (state_ != MainLoopState::kSuspended) {
    ConfUiLog(DEBUG) << "Already Restored to state " + ToString(state_);
    return false;
  }
  host_mode_ctrl_.SetMode(HostModeCtrl::ModeType::kConfUI_Mode);
  if (!RenderDialog(prompt_, locale_)) {
    // the confirmation UI is driven by a user app, not running from the start
    // automatically so that means webRTC/vnc should have been set up
    ConfUiLog(ERROR) << "Dialog is not rendered. However, it should."
                     << "No webRTC can't initiate any confirmation UI.";
    if (!packet::SendAck(hal_cli, session_id_, false,
                         "render failed in restore")) {
      ConfUiLog(FATAL) << "Rendering failed in restore, and ack failed in I/O";
    }
    state_ = MainLoopState::kInit;
    return false;
  }
  if (!packet::SendAck(hal_cli, session_id_, true, "restored")) {
    ConfUiLog(FATAL) << "Ack to restore failed in I/O";
  }
  state_ = saved_state_;
  saved_state_ = MainLoopState::kInit;
  return true;
}

bool Session::Kill(SharedFD hal_cli, const std::string& response_msg) {
  state_ = MainLoopState::kAwaitCleanup;
  saved_state_ = MainLoopState::kInvalid;
  if (!packet::SendAck(hal_cli, session_id_, true, response_msg)) {
    ConfUiLog(FATAL) << "I/O error in ack to Abort";
    return false;
  }
  return true;
}

void Session::CleanUp() {
  if (state_ != MainLoopState::kAwaitCleanup) {
    ConfUiLog(FATAL) << "Clean up a session only when in kAwaitCleanup";
  }
  // common action done when the state is back to init state
  host_mode_ctrl_.SetMode(HostModeCtrl::ModeType::kAndroidMode);
}

void Session::ReportErrorToHal(SharedFD hal_cli, const std::string& msg) {
  // reset the session -- destroy it & recreate it with the same
  // session id
  state_ = MainLoopState::kAwaitCleanup;
  if (!packet::SendAck(hal_cli, session_id_, false, msg)) {
    ConfUiLog(FATAL) << "I/O error in sending ack to report rendering failure";
  }
  return;
}

bool Session::Abort(SharedFD hal_cli) { return Kill(hal_cli, "aborted"); }

void Session::HandleInit(const bool is_user_input, SharedFD hal_cli,
                         const FsmInput fsm_input,
                         const std::string& additional_info) {
  using namespace cuttlefish::confui::packet;
  if (is_user_input) {
    // ignore user input
    state_ = MainLoopState::kInit;
    return;
  }

  ConfUiLog(DEBUG) << ToString(fsm_input) << "is handled in HandleInit";
  if (fsm_input != FsmInput::kHalStart) {
    ConfUiLog(ERROR) << "invalid cmd for Init State:" << ToString(fsm_input);
    // reset the session -- destroy it & recreate it with the same
    // session id
    ReportErrorToHal(hal_cli, "wrong hal command");
    return;
  }

  // Start Session
  ConfUiLog(DEBUG) << "Sending ack to hal_cli: "
                   << Enum2Base(ConfUiCmd::kCliAck);
  host_mode_ctrl_.SetMode(HostModeCtrl::ModeType::kConfUI_Mode);
  auto confirmation_msg = additional_info;
  if (!RenderDialog(confirmation_msg, locale_)) {
    // the confirmation UI is driven by a user app, not running from the start
    // automatically so that means webRTC/vnc should have been set up
    ConfUiLog(ERROR) << "Dialog is not rendered. However, it should."
                     << "No webRTC can't initiate any confirmation UI.";
    ReportErrorToHal(hal_cli, "rendering failed");
    return;
  }
  if (!packet::SendAck(hal_cli, session_id_, true, "started")) {
    ConfUiLog(FATAL) << "Ack to kStart failed in I/O";
  }
  state_ = MainLoopState::kInSession;
  return;
}

void Session::HandleInSession(const bool is_user_input, SharedFD hal_cli,
                              const FsmInput fsm_input) {
  if (!is_user_input) {
    ConfUiLog(FATAL) << "cmd" << ToString(fsm_input)
                     << "should not be handled in HandleInSession";
    ReportErrorToHal(hal_cli, "wrong hal command");
    return;
  }

  // send to hal_cli either confirm or cancel
  if (fsm_input != FsmInput::kUserConfirm &&
      fsm_input != FsmInput::kUserCancel) {
    /*
     * TODO(kwstephenkim@google.com): change here when other user inputs must
     * be handled
     *
     */
    if (!packet::SendAck(hal_cli, session_id_, true,
                         "invalid user input error")) {
      // note that input is what we control in memory
      ConfUiCheck(false) << "Input must be either confirm or cancel for now.";
    }
    return;
  }

  ConfUiLog(DEBUG) << "In HandlieInSession, session" << session_id_
                   << "is sending the user input" << ToString(fsm_input);
  auto selection = UserResponse::kConfirm;
  if (fsm_input == FsmInput::kUserCancel) {
    selection = UserResponse::kCancel;
  }
  if (!packet::SendResponse(hal_cli, session_id_, selection)) {
    ConfUiLog(FATAL) << "I/O error in sending user response to HAL";
  }
  state_ = MainLoopState::kWaitStop;
  return;
}

void Session::HandleWaitStop(const bool is_user_input, SharedFD hal_cli,
                             const FsmInput fsm_input) {
  using namespace cuttlefish::confui::packet;

  if (is_user_input) {
    // ignore user input
    state_ = MainLoopState::kWaitStop;
    return;
  }
  if (fsm_input == FsmInput::kHalStop) {
    ConfUiLog(DEBUG) << "Handling Abort in kWaitStop.";
    Kill(hal_cli, "stopped");
    return;
  }
  ConfUiLog(FATAL) << "In WaitStop, received wrong HAL command "
                   << ToString(fsm_input);
  state_ = MainLoopState::kAwaitCleanup;
  return;
}

}  // end of namespace confui
}  // end of namespace cuttlefish