• 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.h"
17 
18 #include <map>
19 #include <sstream>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include <android-base/strings.h>
24 
25 #include "common/libs/confui/utils.h"
26 
27 namespace cuttlefish {
28 namespace confui {
ToDebugString(const ConfUiCmd & cmd,const bool is_debug)29 std::string ToDebugString(const ConfUiCmd& cmd, const bool is_debug) {
30   std::stringstream ss;
31   ss << "of " << Enum2Base(cmd);
32   std::string suffix = "";
33   if (is_debug) {
34     suffix.append(ss.str());
35   }
36   static std::unordered_map<ConfUiCmd, std::string> look_up_tab{
37       {ConfUiCmd::kUnknown, "kUnknown"},
38       {ConfUiCmd::kStart, "kStart"},
39       {ConfUiCmd::kStop, "kStop"},
40       {ConfUiCmd::kCliAck, "kCliAck"},
41       {ConfUiCmd::kCliRespond, "kCliRespond"},
42       {ConfUiCmd::kAbort, "kAbort"},
43       {ConfUiCmd::kSuspend, "kSuspend"},
44       {ConfUiCmd::kRestore, "kRestore"},
45       {ConfUiCmd::kUserInputEvent, "kUserInputEvent"}};
46   if (look_up_tab.find(cmd) != look_up_tab.end()) {
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,           ConfUiCmd::kCliAck,
57       ConfUiCmd::kCliRespond, ConfUiCmd::kAbort,          ConfUiCmd::kSuspend,
58       ConfUiCmd::kRestore,    ConfUiCmd::kUserInputEvent, ConfUiCmd::kUnknown};
59 
60   for (auto& cmd : all_cmds) {
61     if (i == Enum2Base(cmd)) {
62       return cmd;
63     }
64   }
65   return ConfUiCmd::kUnknown;
66 }
67 
ToCmd(const std::string & cmd_str)68 ConfUiCmd ToCmd(const std::string& cmd_str) {
69   static std::map<std::string, ConfUiCmd> cmds = {
70       {"kStart", ConfUiCmd::kStart},
71       {"kStop", ConfUiCmd::kStop},
72       {"kCliAck", ConfUiCmd::kCliAck},
73       {"kCliRespond", ConfUiCmd::kCliRespond},
74       {"kAbort", ConfUiCmd::kAbort},
75       {"kSuspend", ConfUiCmd::kSuspend},
76       {"kRestore", ConfUiCmd::kRestore},
77       {"kUserInputEvent", ConfUiCmd::kUserInputEvent},
78   };
79   if (cmds.find(cmd_str) != cmds.end()) {
80     return cmds[cmd_str];
81   }
82   return ConfUiCmd::kUnknown;
83 }
84 
ToCliAckMessage(const bool is_success,const std::string & message)85 std::string ToCliAckMessage(const bool is_success, const std::string& message) {
86   std::string header = "error:";
87   if (is_success) {
88     header = "success:";
89   }
90   return header + message;
91 }
92 
ToCliAckSuccessMsg(const std::string & message)93 std::string ToCliAckSuccessMsg(const std::string& message) {
94   return ToCliAckMessage(true, message);
95 }
96 
ToCliAckErrorMsg(const std::string & message)97 std::string ToCliAckErrorMsg(const std::string& message) {
98   return ToCliAckMessage(false, message);
99 }
100 
101 }  // end of namespace confui
102 }  // end of namespace cuttlefish
103