1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef COMMANDPARSER_H
17 #define COMMANDPARSER_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 class CommandInfo {
24 public:
25     std::string deviceType;
26     std::string pages;
27     std::string appResourcePath;
28     bool isCardDisplay;
29     std::string containerSdkPath;
30     bool isComponentMode;
31     std::string loaderJsonPath;
32     std::string abilityPath;
33     std::string abilityName;
34     std::string configPath;
35     std::string screenShape;
36     int32_t orignalResolutionWidth;
37     int32_t orignalResolutionHeight;
38     int32_t compressionResolutionWidth;
39     int32_t compressionResolutionHeight;
40 };
41 
42 class FoldInfo {
43 public:
44     bool foldable;
45     std::string foldStatus;
46     int32_t foldResolutionWidth;
47     int32_t foldResolutionHeight;
48 };
49 
50 class CommandParser {
51 public:
52     CommandParser(const CommandParser&) = delete;
53     CommandParser& operator=(const CommandParser&) = delete;
54     static CommandParser& GetInstance();
55     bool ProcessCommand(std::vector<std::string> strs);
56     bool IsCommandValid();
57     bool IsSet(std::string key);
58     std::string Value(std::string key);
59     std::vector<std::string> Values(std::string key);
60     void Register(std::string key, uint32_t argc, std::string help);
61     bool IsResolutionValid(int32_t resolution) const;
62     int32_t GetOrignalResolutionWidth() const;
63     int32_t GetOrignalResolutionHeight() const;
64     int32_t GetCompressionResolutionWidth() const;
65     int32_t GetCompressionResolutionHeight() const;
66     uint32_t GetJsHeapSize() const;
67     std::string GetAppName() const;
68     bool IsSendJSHeap() const;
69     std::string GetDeviceType() const;
70     bool IsRegionRefresh() const;
71     bool IsCardDisplay() const;
72     std::string GetConfigPath() const;
73     std::string GetProjectID() const;
74     enum class ScreenMode { DYNAMIC = 0, STATIC = 1};
75     CommandParser::ScreenMode GetScreenMode() const;
76     std::string GetConfigChanges() const;
77     std::string GetAppResourcePath() const;
78     std::string GetScreenShape() const;
79     std::string GetProjectModel() const;
80     int GetProjectModelEnumValue() const;
81     std::string GetProjectModelEnumName(int enumValue) const;
82     std::string GetPages() const;
83     std::string GetContainerSdkPath() const;
84     bool CheckParamInvalidity(std::string param, bool isNum);
85     bool IsComponentMode() const;
86     std::string GetAbilityPath() const;
87     std::string GetAbilityName() const;
88     bool IsStaticCard() const;
89     bool IsMainArgLengthInvalid(const char* str) const;
90     bool IsFoldable() const;
91     std::string GetFoldStatus() const;
92     int32_t GetFoldResolutionWidth() const;
93     int32_t GetFoldResolutionHeight() const;
94     std::string GetLoaderJsonPath() const;
95     int ParseArgs(int argc, char* argv[]);
96     void GetCommandInfo(CommandInfo& info) const;
97     void GetFoldInfo(FoldInfo& info) const;
98 #ifdef COMPONENT_TEST_ENABLED
99     std::string GetComponentTestConfig() const;
100 #endif // COMPONENT_TEST_ENABLED
101 
102 private:
103     CommandParser();
~CommandParser()104     ~CommandParser() {}
105     std::string errorInfo;
106     std::map<std::string, std::vector<std::string>> argsMap;
107     std::map<std::string, uint32_t> regsArgsCountMap;
108     std::map<std::string, std::string> regsHelpMap;
109     static CommandParser* example;
110     const std::vector<std::string> supportedDevices = {
111         "liteWearable",
112         "smartVision",
113         "wearable",
114         "tv",
115         "phone",
116         "tablet",
117         "car",
118         "2in1",
119         "default"
120     };
121     const std::vector<std::string> cardDisplayDevices = {"phone", "tablet", "wearable", "car", "tv", "2in1", "default"};
122     const std::vector<std::string> projectModels = {"FA", "Stage"};
123     const int MIN_PORT = 1024;
124     const int MAX_PORT = 65535;
125     const int32_t MIN_RESOLUTION = 1;
126     const int32_t MAX_RESOLUTION = 3840;
127     const int MAX_JSHEAPSIZE = 512 * 1024;
128     const int MIN_JSHEAPSIZE = 48 * 1024;
129     const size_t MAX_NAME_LENGTH = 256;
130     bool isSendJSHeap;
131     int32_t orignalResolutionWidth;
132     int32_t orignalResolutionHeight;
133     int32_t compressionResolutionWidth;
134     int32_t compressionResolutionHeight;
135     uint32_t jsHeapSize;
136     std::string deviceType;
137     std::string screenShape;
138     std::string appName;
139     std::string urlPath;
140     std::string configPath;
141     bool isRegionRefresh;
142     bool isCardDisplay;
143     std::string projectID;
144     CommandParser::ScreenMode screenMode;
145     std::string configChanges;
146     std::string appResourcePath;
147     std::string projectModel;
148     std::string pages;
149     std::string containerSdkPath;
150     std::string regex4Num = "^(0|[1-9][0-9]*)(\\.[0-9]+)?$";
151     std::string regex4Str = "^(?:[a-zA-Z0-9-_./\\s]+)$";
152     bool isComponentMode;
153     std::string abilityPath;
154     std::string abilityName;
155 #ifdef COMPONENT_TEST_ENABLED
156     std::string componentTestConfig;
157 #endif // COMPONENT_TEST_ENABLED
158     bool staticCard;
159     const size_t maxMainArgLength = 1024;
160     bool foldable;
161     std::string foldStatus;
162     int32_t foldResolutionWidth;
163     int32_t foldResolutionHeight;
164     std::string loaderJsonPath;
165 
166     bool IsDebugPortValid();
167     bool IsAppPathValid();
168     bool IsAppNameValid();
169     bool IsResolutionValid();
170     bool IsConfigPathValid();
171     bool IsAppResourcePathValid();
172     bool IsResolutionRangeValid(std::string value);
173     bool IsResolutionArgValid(std::string command);
174     bool IsJsHeapValid();
175     bool IsJsHeapFlagValid();
176     bool IsScreenShapeValid();
177     bool IsDeviceValid();
178     bool IsUrlValid();
179     bool IsRefreshValid();
180     bool IsCardValid();
181     bool IsProjectIDValid();
182     bool IsColorModeValid();
183     bool IsAceVersionValid();
184     bool IsOrientationValid();
185     bool IsWebSocketPortValid();
186     bool IsScreenModeValid();
187     bool IsProjectModelValid();
188     bool IsPagesValid();
189     bool IsLanguageValid();
190     bool IsTracePipeNameValid();
191     bool IsLocalSocketNameValid();
192     bool IsScreenDensityValid();
193     bool IsConfigChangesValid();
194     bool IsContainerSdkPathValid();
195     bool IsComponentModeValid();
196     bool IsAbilityPathValid();
197     bool IsAbilityNameValid();
198     bool IsStaticCardValid();
199     bool IsFoldableValid();
200     bool IsFoldStatusValid();
201     bool IsFoldResolutionValid();
202     bool IsLoaderJsonPathValid();
203     std::string HelpText();
204     void ProcessingCommand(const std::vector<std::string>& strs);
205 };
206 
207 #endif // COMMANDPARSER_H
208