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