• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "common/libs/confui/protocol_types.h"
17 
18 #include <map>
19 #include <sstream>
20 #include <unordered_map>
21 
22 #include "common/libs/confui/packet.h"
23 #include "common/libs/confui/utils.h"
24 
25 namespace cuttlefish {
26 namespace confui {
ToDebugString(const ConfUiCmd & cmd,const bool is_verbose)27 std::string ToDebugString(const ConfUiCmd& cmd, const bool is_verbose) {
28   std::stringstream ss;
29   ss << " of " << Enum2Base(cmd);
30   std::string suffix = "";
31   if (is_verbose) {
32     suffix.append(ss.str());
33   }
34   static std::unordered_map<ConfUiCmd, std::string> look_up_tab{
35       {ConfUiCmd::kUnknown, "kUnknown"},
36       {ConfUiCmd::kStart, "kStart"},
37       {ConfUiCmd::kStop, "kStop"},
38       {ConfUiCmd::kCliAck, "kCliAck"},
39       {ConfUiCmd::kCliRespond, "kCliRespond"},
40       {ConfUiCmd::kAbort, "kAbort"},
41       {ConfUiCmd::kUserInputEvent, "kUserInputEvent"},
42       {ConfUiCmd::kUserInputEvent, "kUserTouchEvent"}};
43   if (look_up_tab.find(cmd) != look_up_tab.end()) {
44     return look_up_tab[cmd] + suffix;
45   }
46   return "kUnknown" + suffix;
47 }
48 
ToString(const ConfUiCmd & cmd)49 std::string ToString(const ConfUiCmd& cmd) { return ToDebugString(cmd, false); }
50 
ToCmd(std::uint32_t i)51 ConfUiCmd ToCmd(std::uint32_t i) {
52   std::vector<ConfUiCmd> all_cmds{
53       ConfUiCmd::kStart,          ConfUiCmd::kStop,
54       ConfUiCmd::kCliAck,         ConfUiCmd::kCliRespond,
55       ConfUiCmd::kAbort,          ConfUiCmd::kUserInputEvent,
56       ConfUiCmd::kUserTouchEvent, ConfUiCmd::kUnknown};
57 
58   for (auto& cmd : all_cmds) {
59     if (i == Enum2Base(cmd)) {
60       return cmd;
61     }
62   }
63   return ConfUiCmd::kUnknown;
64 }
65 
ToCmd(const std::string & cmd_str)66 ConfUiCmd ToCmd(const std::string& cmd_str) {
67   static std::map<std::string, ConfUiCmd> cmds = {
68       {"kStart", ConfUiCmd::kStart},
69       {"kStop", ConfUiCmd::kStop},
70       {"kCliAck", ConfUiCmd::kCliAck},
71       {"kCliRespond", ConfUiCmd::kCliRespond},
72       {"kAbort", ConfUiCmd::kAbort},
73       {"kUserInputEvent", ConfUiCmd::kUserInputEvent},
74       {"kUserTouchEvent", ConfUiCmd::kUserTouchEvent},
75   };
76   if (cmds.find(cmd_str) != cmds.end()) {
77     return cmds[cmd_str];
78   }
79   return ConfUiCmd::kUnknown;
80 }
81 
ToString(const teeui::UIOption ui_opt)82 std::string ToString(const teeui::UIOption ui_opt) {
83   return std::to_string(static_cast<int>(ui_opt));
84 }
85 
ToUiOption(const std::string & src)86 std::optional<teeui::UIOption> ToUiOption(const std::string& src) {
87   if (!IsOnlyDigits(src)) {
88     return std::nullopt;
89   }
90   return {static_cast<teeui::UIOption>(std::stoi(src))};
91 }
92 
93 template <typename T>
ByteVecToString(const std::vector<T> & v)94 static std::string ByteVecToString(const std::vector<T>& v) {
95   static_assert(sizeof(T) == 1);
96   std::string result{v.begin(), v.end()};
97   return result;
98 }
99 
IsUserInput() const100 bool ConfUiMessage::IsUserInput() const {
101   switch (GetType()) {
102     case ConfUiCmd::kUserInputEvent:
103     case ConfUiCmd::kUserTouchEvent:
104       return true;
105     default:
106       return false;
107   }
108 }
109 
ToString() const110 std::string ConfUiAckMessage::ToString() const {
111   return CreateString(session_id_, confui::ToString(GetType()),
112                       (is_success_ ? "success" : "fail"), status_message_);
113 }
114 
SendOver(SharedFD fd)115 bool ConfUiAckMessage::SendOver(SharedFD fd) {
116   return Send_(fd, GetType(), session_id_,
117                std::string(is_success_ ? "success" : "fail"), status_message_);
118 }
119 
ToString() const120 std::string ConfUiCliResponseMessage::ToString() const {
121   return CreateString(session_id_, confui::ToString(GetType()), response_,
122                       ByteVecToString(sign_), ByteVecToString(message_));
123 }
124 
SendOver(SharedFD fd)125 bool ConfUiCliResponseMessage::SendOver(SharedFD fd) {
126   return Send_(fd, GetType(), session_id_, response_, sign_, message_);
127 }
128 
UiOptsToString() const129 std::string ConfUiStartMessage::UiOptsToString() const {
130   std::stringstream ss;
131   for (const auto& ui_opt : ui_opts_) {
132     ss << cuttlefish::confui::ToString(ui_opt) << ",";
133   }
134   auto ui_opt_str = ss.str();
135   if (!ui_opt_str.empty()) {
136     ui_opt_str.pop_back();
137   }
138   return ui_opt_str;
139 }
140 
ToString() const141 std::string ConfUiStartMessage::ToString() const {
142   auto ui_opts_str = UiOptsToString();
143   return CreateString(
144       session_id_, confui::ToString(GetType()), prompt_text_, locale_,
145       std::string(extra_data_.begin(), extra_data_.end()), ui_opts_str);
146 }
147 
SendOver(SharedFD fd)148 bool ConfUiStartMessage::SendOver(SharedFD fd) {
149   return Send_(fd, GetType(), session_id_, prompt_text_, extra_data_, locale_,
150                UiOptsToString());
151 }
152 
ToString() const153 std::string ConfUiUserSelectionMessage::ToString() const {
154   return CreateString(session_id_, confui::ToString(GetType()), response_);
155 }
156 
SendOver(SharedFD fd)157 bool ConfUiUserSelectionMessage::SendOver(SharedFD fd) {
158   return Send_(fd, GetType(), session_id_, response_);
159 }
160 
ToString() const161 std::string ConfUiUserTouchMessage::ToString() const {
162   std::stringstream ss;
163   ss << "(" << x_ << "," << y_ << ")";
164   auto pos = ss.str();
165   return CreateString(session_id_, confui::ToString(GetType()), response_, pos);
166 }
167 
SendOver(SharedFD fd)168 bool ConfUiUserTouchMessage::SendOver(SharedFD fd) {
169   return Send_(fd, GetType(), session_id_, std::to_string(x_),
170                std::to_string(y_));
171 }
172 
173 }  // end of namespace confui
174 }  // end of namespace cuttlefish
175