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