• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "CommandParser.h"
17 #include <cstring>
18 #include <algorithm>
19 #include <cstdlib>
20 #include <regex>
21 #include "FileSystem.h"
22 #include "PreviewerEngineLog.h"
23 #include "TraceTool.h"
24 
25 CommandParser* CommandParser::example = nullptr;
CommandParser()26 CommandParser::CommandParser()
27     : isSendJSHeap(true),
28       orignalResolutionWidth(0),
29       orignalResolutionHeight(0),
30       compressionResolutionWidth(0),
31       compressionResolutionHeight(0),
32       jsHeapSize(MAX_JSHEAPSIZE),
33       deviceType("liteWearable"),
34       screenShape("circle"),
35       appName("undefined"),
36       configPath(""),
37       isRegionRefresh(false),
38       isCardDisplay(false),
39       projectID(""),
40       screenMode(CommandParser::ScreenMode::DYNAMIC),
41       configChanges(""),
42       appResourcePath(""),
43       projectModel("FA"),
44       pages("main_pages"),
45       containerSdkPath(""),
46       isComponentMode(false),
47       abilityPath(""),
48 #ifdef COMPONENT_TEST_ENABLED
49       componentTestConfig(""),
50 #endif // COMPONENT_TEST_ENABLED
51       staticCard(false),
52       sid("")
53 {
54     Register("-j", 1, "Launch the js app in <directory>.");
55     Register("-n", 1, "Set the js app name show on <window title>.");
56     Register("-d", 0, "Run in debug mode and start debug server.");
57     Register("-p", 1, "Config debug server to listen <port>.");
58     Register("-s", 1, "Local socket name <socket-name> for command line interface.");
59     Register("-v", 0, "Print the periviewer engine version.");
60     Register("-h", 0, "Print the usage help.");
61     Register("-or", 2, "Original resolution <width> <height>"); // 2 arguments
62     Register("-cr", 2, "Compression resolution <width> <height>"); // 2 arguments
63     Register("-f", 1, "config path <path>");
64     Register("-hs", 1, "JS Heap <size>");
65     Register("-hf", 1, "JS Send Heap <flag>");
66     Register("-shape", 1, "Screen shape <shape>");
67     Register("-device", 1, "Device type <type>");
68     Register("-url", 1, "temp url");
69     Register("-refresh", 1, "Screen <refresh mode>, support region and full");
70     Register("-card", 1, "Controls the display <type> to switch between the app and card.");
71     Register("-projectID", 1, "the ID of current project.");
72     Register("-ts", 1, "Trace socket name");
73     Register("-cm", 1, "Set colormode for the theme.");
74     Register("-o", 1, "Set orientation for the display.");
75     Register("-lws", 1, "Listening port of WebSocket");
76     Register("-av", 1, "Set ace version.");
77     Register("-l", 1, "Set language for startParam.");
78     Register("-sd", 1, "Set screenDensity for Previewer.");
79     Register("-sm", 1, "Set Screen picture transport mode, support dynamic and static");
80     Register("-cc", 1, "Set Resource configChanges.");
81     Register("-arp", 1, "Set App ResourcePath.");
82     Register("-fs", 1, "Select Fonts sources.");
83     Register("-pm", 1, "Set project model type.");
84     Register("-pages", 1, "Set project's router config file path.");
85     Register("-hsp", 1, "Set container sdk path.");
86     Register("-cpm", 1, "Set previewer start mode.");
87     Register("-abp", 1, "Set abilityPath for debug.");
88     Register("-abn", 1, "Set abilityName for debug.");
89     Register("-staticCard", 1, "Set card mode.");
90     Register("-foldable", 1, "Set foldable for Previewer.");
91     Register("-foldStatus", 1, "Set fold status for Previewer.");
92     Register("-fr", 2, "Fold resolution <width> <height>"); // 2 arguments
93     Register("-ljPath", 1, "Set loader.json path for Previewer");
94     Register("-sid", 1, "Set sid for websocket");
95 #ifdef COMPONENT_TEST_ENABLED
96     Register("-componentTest", 1, "Set component test config");
97 #endif // COMPONENT_TEST_ENABLED
98 }
99 
GetInstance()100 CommandParser& CommandParser::GetInstance()
101 {
102     static CommandParser instance;
103     return instance;
104 }
105 
106 /*
107  * Parse user input and check parameter validity
108  */
ProcessCommand(std::vector<std::string> strs)109 bool CommandParser::ProcessCommand(std::vector<std::string> strs)
110 {
111     ProcessingCommand(strs);
112 
113     if (IsSet("v")) {
114         ELOG("ProcessCommand Set -v!");
115         return false;
116     }
117 
118     if (IsSet("h")) {
119         ELOG("ProcessCommand Set -h!");
120         ELOG(HelpText().c_str());
121         return false;
122     }
123 
124     return true;
125 }
126 
IsCommandValid()127 bool CommandParser::IsCommandValid()
128 {
129     bool partRet = IsDebugPortValid() && IsAppPathValid() && IsAppNameValid() && IsResolutionValid();
130     partRet = partRet && IsConfigPathValid() && IsJsHeapValid() && IsJsHeapFlagValid() && IsScreenShapeValid();
131     partRet = partRet && IsDeviceValid() && IsUrlValid() && IsRefreshValid() && IsCardValid() && IsProjectIDValid();
132     partRet = partRet && IsColorModeValid() && IsOrientationValid() && IsWebSocketPortValid() && IsAceVersionValid();
133     partRet = partRet && IsScreenModeValid() && IsAppResourcePathValid() && IsLoaderJsonPathValid();
134     partRet = partRet && IsProjectModelValid() && IsPagesValid() && IsContainerSdkPathValid();
135     partRet = partRet && IsComponentModeValid() && IsAbilityPathValid() && IsStaticCardValid();
136     partRet = partRet && IsFoldableValid() && IsFoldStatusValid() && IsFoldResolutionValid();
137     partRet = partRet && IsAbilityNameValid() && IsLanguageValid() && IsTracePipeNameValid();
138     partRet = partRet && IsLocalSocketNameValid() && IsConfigChangesValid() && IsScreenDensityValid();
139     partRet = partRet && IsSidValid();
140     if (partRet) {
141         return true;
142     }
143     ELOG(errorInfo.c_str());
144     ILOG(HelpText().c_str());
145     TraceTool::GetInstance().HandleTrace("Invalid startup parameters");
146     return false;
147 }
148 
IsSet(std::string key)149 bool CommandParser::IsSet(std::string key)
150 {
151     if (argsMap.find(std::string("-") + key) == argsMap.end()) {
152         return false;
153     }
154     return true;
155 }
156 
Value(std::string key)157 std::string CommandParser::Value(std::string key)
158 {
159     auto args = argsMap[std::string("-") + key];
160     if (args.size() > 0) {
161         return args[0];
162     }
163     return std::string();
164 }
165 
Values(std::string key)166 std::vector<std::string> CommandParser::Values(std::string key)
167 {
168     if (argsMap.find(key) == argsMap.end()) {
169         return std::vector<std::string>();
170     }
171     std::vector<std::string> args = argsMap[key];
172     return args;
173 }
174 
Register(std::string key,uint32_t argc,std::string help)175 void CommandParser::Register(std::string key, uint32_t argc, std::string help)
176 {
177     regsArgsCountMap[key] = argc;
178     regsHelpMap[key] = help;
179 }
180 
IsResolutionValid(int32_t resolution) const181 bool CommandParser::IsResolutionValid(int32_t resolution) const
182 {
183     if (resolution >= MIN_RESOLUTION && resolution <= MAX_RESOLUTION) {
184         return true;
185     }
186     return false;
187 }
188 
GetDeviceType() const189 std::string CommandParser::GetDeviceType() const
190 {
191     return deviceType;
192 }
193 
IsRegionRefresh() const194 bool CommandParser::IsRegionRefresh() const
195 {
196     return isRegionRefresh;
197 }
198 
IsCardDisplay() const199 bool CommandParser::IsCardDisplay() const
200 {
201     return isCardDisplay;
202 }
203 
GetConfigPath() const204 std::string CommandParser::GetConfigPath() const
205 {
206     return configPath;
207 }
208 
GetProjectID() const209 std::string CommandParser::GetProjectID() const
210 {
211     return projectID;
212 }
213 
GetAppResourcePath() const214 std::string CommandParser::GetAppResourcePath() const
215 {
216     return appResourcePath;
217 }
218 
GetScreenShape() const219 std::string CommandParser::GetScreenShape() const
220 {
221     return screenShape;
222 }
223 
GetProjectModel() const224 std::string CommandParser::GetProjectModel() const
225 {
226     return projectModel;
227 }
228 
GetPages() const229 std::string CommandParser::GetPages() const
230 {
231     return pages;
232 }
233 
GetContainerSdkPath() const234 std::string CommandParser::GetContainerSdkPath() const
235 {
236     return containerSdkPath;
237 }
238 
GetScreenMode() const239 CommandParser::ScreenMode CommandParser::GetScreenMode() const
240 {
241     return screenMode;
242 }
243 
GetConfigChanges() const244 std::string CommandParser::GetConfigChanges() const
245 {
246     return configChanges;
247 }
248 
GetOrignalResolutionWidth() const249 int32_t CommandParser::GetOrignalResolutionWidth() const
250 {
251     return orignalResolutionWidth;
252 }
253 
GetOrignalResolutionHeight() const254 int32_t CommandParser::GetOrignalResolutionHeight() const
255 {
256     return orignalResolutionHeight;
257 }
258 
GetCompressionResolutionWidth() const259 int32_t CommandParser::GetCompressionResolutionWidth() const
260 {
261     return compressionResolutionWidth;
262 }
263 
GetCompressionResolutionHeight() const264 int32_t CommandParser::GetCompressionResolutionHeight() const
265 {
266     return compressionResolutionHeight;
267 }
268 
GetJsHeapSize() const269 uint32_t CommandParser::GetJsHeapSize() const
270 {
271     return jsHeapSize;
272 }
273 
GetAppName() const274 std::string CommandParser::GetAppName() const
275 {
276     return appName;
277 }
278 
IsSendJSHeap() const279 bool CommandParser::IsSendJSHeap() const
280 {
281     return isSendJSHeap;
282 }
283 
IsComponentMode() const284 bool CommandParser::IsComponentMode() const
285 {
286     return isComponentMode;
287 }
288 
GetAbilityPath() const289 std::string CommandParser::GetAbilityPath() const
290 {
291     return abilityPath;
292 }
293 
GetAbilityName() const294 std::string CommandParser::GetAbilityName() const
295 {
296     return abilityName;
297 }
298 
IsStaticCard() const299 bool CommandParser::IsStaticCard() const
300 {
301     return staticCard;
302 }
303 
IsDebugPortValid()304 bool CommandParser::IsDebugPortValid()
305 {
306     if (IsSet("p")) {
307         if (CheckParamInvalidity(Value("p"), true)) {
308             errorInfo = "Launch -p parameters is not match regex.";
309             return false;
310         }
311         int port = atoi(Value("p").c_str());
312         if (port < MIN_PORT || port > MAX_PORT) {
313             errorInfo =
314                 std::string("Debug server port out of range: " + std::to_string(MIN_PORT) + "-" +
315                 std::to_string(MAX_PORT) + ".");
316             ELOG("Launch -p parameters abnormal!");
317             return false;
318         }
319     }
320     ILOG("CommandParser debug port: %s", Value("p").c_str());
321     return true;
322 }
323 
IsAppPathValid()324 bool CommandParser::IsAppPathValid()
325 {
326     if (!IsSet("j")) {
327         errorInfo = std::string("No app path specified.");
328         ELOG("Launch -j parameters abnormal!");
329         return false;
330     }
331     std::string path = Value("j");
332     if (!FileSystem::IsDirectoryExists(path)) {
333         errorInfo = std::string("Js app path not exist.");
334         ELOG("Launch -j parameters abnormal!");
335         return false;
336     }
337 
338     return true;
339 }
340 
IsAppNameValid()341 bool CommandParser::IsAppNameValid()
342 {
343     if (IsSet("n")) {
344         if (CheckParamInvalidity(Value("n"), false)) {
345             errorInfo = "Launch -n parameters is not match regex.";
346             return false;
347         }
348         size_t size = Value("n").size();
349         if (size > MAX_NAME_LENGTH) {
350             errorInfo = std::string("Js app name it too long, max: " + std::to_string(MAX_NAME_LENGTH) + ".");
351             return false;
352         }
353         appName = Value("n");
354     }
355     ILOG("CommandParser app name: %s", appName.c_str());
356     return true;
357 }
358 
IsResolutionValid()359 bool CommandParser::IsResolutionValid()
360 {
361     if (IsSet("or") && IsSet("cr")) {
362         if (IsResolutionArgValid(std::string("-or")) && IsResolutionArgValid(std::string("-cr"))) {
363             orignalResolutionWidth = atoi(Values("-or")[0].c_str());
364             orignalResolutionHeight = atoi(Values("-or")[1].c_str());
365             compressionResolutionWidth = atoi(Values("-cr")[0].c_str());
366             compressionResolutionHeight = atoi(Values("-cr")[1].c_str());
367             ILOG("CommandParser resolution: %d %d %d %d", orignalResolutionWidth, orignalResolutionHeight,
368                  compressionResolutionWidth, compressionResolutionHeight);
369             return true;
370         }
371         ELOG("Launch -cr/-or parameters abnormal!");
372         return false;
373     }
374     ELOG("Launch -cr/-or parameters abnormal!");
375     errorInfo = std::string("Origin resolution and compress resolution must be setted.");
376     return false;
377 }
378 
IsJsHeapValid()379 bool CommandParser::IsJsHeapValid()
380 {
381     if (IsSet("hs")) {
382         if (CheckParamInvalidity(Value("hs"), true)) {
383             errorInfo = "Launch -hs parameters is not match regex.";
384             return false;
385         }
386         int size = atoi(Value("hs").c_str());
387         if (size < MIN_JSHEAPSIZE || size > MAX_JSHEAPSIZE) {
388             errorInfo = std::string("JS heap size out of range: " + std::to_string(MIN_JSHEAPSIZE) + "-" +
389                 std::to_string(MAX_JSHEAPSIZE) + ".");
390             ELOG("Launch -hs parameters abnormal!");
391             return false;
392         }
393         jsHeapSize = static_cast<uint32_t>(size);
394     }
395     ILOG("CommandParser js heap: %d", jsHeapSize);
396     return true;
397 }
398 
IsJsHeapFlagValid()399 bool CommandParser::IsJsHeapFlagValid()
400 {
401     if (IsSet("hf")) {
402         std::string flag = Value("hf");
403         if (flag != "true" && flag != "false") {
404             errorInfo = std::string("JS heap flag suported: true or false");
405             ELOG("Launch -hs parameters abnormal!");
406             return false;
407         }
408         isSendJSHeap = (flag == "true");
409     }
410     ILOG("CommandParser is send JS heap: %d", isSendJSHeap);
411     return true;
412 }
413 
IsScreenShapeValid()414 bool CommandParser::IsScreenShapeValid()
415 {
416     if (IsSet("shape")) {
417         std::string shape = Value("shape");
418         if (shape != "rect" && shape != "circle") {
419             errorInfo = std::string("Screen shape suported: rect or circle");
420             ELOG("The current device does not support, please upgrade the SDK!");
421             return false;
422         }
423         screenShape = shape;
424     }
425     ILOG("CommandParser screen shape: %s", screenShape.c_str());
426     return true;
427 }
428 
IsDeviceValid()429 bool CommandParser::IsDeviceValid()
430 {
431     if (IsSet("device")) {
432         auto iter = find(supportedDevices.begin(), supportedDevices.end(), Value("device"));
433         if (iter == supportedDevices.end()) {
434             errorInfo += std::string("Device type unsupport, please upgrade the Previewer SDK!");
435             ELOG("Device type unsupport!");
436             return false;
437         }
438     }
439     deviceType = Value("device");
440     ILOG("CommandParser device: %s", deviceType.c_str());
441     return true;
442 }
443 
IsUrlValid()444 bool CommandParser::IsUrlValid()
445 {
446     urlPath = Value("url");
447     if (urlPath.empty()) {
448         errorInfo = "Launch -url parameters is empty.";
449         return false;
450     }
451     ILOG("CommandParser url: %s", urlPath.c_str());
452     return true;
453 }
454 
IsConfigPathValid()455 bool CommandParser::IsConfigPathValid()
456 {
457     if (!IsSet("f")) {
458         return true;
459     }
460 
461     std::string path = Value("f");
462     if (!FileSystem::IsFileExists(path)) {
463         errorInfo = std::string("The configuration file path does not exist.");
464         ELOG("Launch -f parameters abnormal!");
465         return false;
466     }
467     configPath = path;
468     return true;
469 }
470 
IsAppResourcePathValid()471 bool CommandParser::IsAppResourcePathValid()
472 {
473     if (!IsSet("arp")) {
474         return true;
475     }
476 
477     std::string path = Value("arp");
478     if (!FileSystem::IsDirectoryExists(path)) {
479         errorInfo = std::string("The configuration appResource path does not exist.");
480         ELOG("Launch -arp parameters abnormal!");
481         return false;
482     }
483     appResourcePath = path;
484     return true;
485 }
486 
IsProjectModelValid()487 bool CommandParser::IsProjectModelValid()
488 {
489     if (!IsSet("pm")) {
490         return true;
491     }
492 
493     std::string projectModelStr = Value("pm");
494     auto iter = find(projectModels.begin(), projectModels.end(), projectModelStr);
495     if (iter == projectModels.end()) {
496         errorInfo = std::string("The project model does not exist.");
497         ELOG("Launch -pm parameters abnormal!");
498         return false;
499     }
500 
501     projectModel = projectModelStr;
502     ILOG("CommandParser projectModel: %s", projectModelStr.c_str());
503     return true;
504 }
505 
IsPagesValid()506 bool CommandParser::IsPagesValid()
507 {
508     if (!IsSet("pages")) {
509         return true;
510     }
511     pages = Value("pages");
512     if (CheckParamInvalidity(pages, false)) {
513         errorInfo = "Launch -pages parameters is not match regex.";
514         return false;
515     }
516     ILOG("CommandParser pages: %s", pages.c_str());
517     return true;
518 }
519 
IsResolutionArgValid(std::string command)520 bool CommandParser::IsResolutionArgValid(std::string command)
521 {
522     std::vector<std::string> value = Values(command);
523     uint32_t size = regsArgsCountMap[command];
524     if (value.size() != size) {
525         errorInfo = std::string("Invalid argument's count.");
526         return false;
527     }
528     if (IsResolutionRangeValid(value[0]) && IsResolutionRangeValid(value[1])) {
529         return true;
530     }
531     return false;
532 }
533 
IsResolutionRangeValid(std::string value)534 bool CommandParser::IsResolutionRangeValid(std::string value)
535 {
536     if (CheckParamInvalidity(value, true)) {
537         errorInfo = "Launch -or/-cr or -fr parameters is not match regex.";
538         return false;
539     }
540     int32_t temp = atoi(value.c_str());
541     if (!IsResolutionValid(temp)) {
542         errorInfo = std::string("Resolution range " + std::to_string(MIN_RESOLUTION) + "-" +
543             std::to_string(MAX_RESOLUTION) + ".");
544         return false;
545     }
546     return true;
547 }
548 
IsRefreshValid()549 bool CommandParser::IsRefreshValid()
550 {
551     if (!IsSet("refresh")) {
552         return true;
553     }
554 
555     std::string refresh = Value("refresh");
556     if (refresh != "region" && refresh != "full") {
557         errorInfo = std::string("The refresh argument unsupported.");
558         ELOG("Launch -refresh parameters abnormal!");
559         return false;
560     }
561     if (refresh == "region") {
562         isRegionRefresh = true;
563     }
564     return true;
565 }
566 
IsCardValid()567 bool CommandParser::IsCardValid()
568 {
569     if (!IsSet("card")) {
570         return true;
571     }
572 
573     std::string card = Value("card");
574     if (card != "true" && card != "false") {
575         errorInfo = std::string("The card argument unsupported.");
576         ELOG("Launch -card parameters abnormal!");
577         return false;
578     }
579 
580     std::string devicetype = GetDeviceType();
581     auto iter = find(cardDisplayDevices.begin(), cardDisplayDevices.end(), devicetype);
582     if (iter != cardDisplayDevices.end() && card == "true") {
583         isCardDisplay = true;
584     }
585     return true;
586 }
587 
IsProjectIDValid()588 bool CommandParser::IsProjectIDValid()
589 {
590     if (IsSet("projectID")) {
591         projectID = Value("projectID");
592         if (CheckParamInvalidity(projectID, false)) {
593             errorInfo = "Launch -projectID parameters is not match regex.";
594             return false;
595         }
596     }
597     return true;
598 }
599 
IsColorModeValid()600 bool CommandParser::IsColorModeValid()
601 {
602     if (!IsSet("cm")) {
603         return true;
604     }
605 
606     std::string colorMode = Value("cm");
607     if (colorMode != "dark" && colorMode != "light") {
608         errorInfo = std::string("The colormode argument unsupported.");
609         ELOG("Launch -cm parameters abnormal!");
610         return false;
611     }
612     return true;
613 }
614 
IsAceVersionValid()615 bool CommandParser::IsAceVersionValid()
616 {
617     if (!IsSet("av")) {
618         return true;
619     }
620 
621     std::string aceVersion = Value("av");
622     if (aceVersion != "ACE_1_0" && aceVersion != "ACE_2_0") {
623         errorInfo = std::string("The aceVersion argument unsupported.");
624         ELOG("Launch -av parameters abnormal!");
625         return false;
626     }
627     return true;
628 }
629 
IsOrientationValid()630 bool CommandParser::IsOrientationValid()
631 {
632     if (!IsSet("o")) {
633         return true;
634     }
635 
636     std::string orientation = Value("o");
637     if (orientation != "portrait" && orientation != "landscape") {
638         errorInfo = std::string("The orientation argument unsupported.");
639         ELOG("Launch -o parameters abnormal!");
640         return false;
641     }
642     return true;
643 }
644 
IsWebSocketPortValid()645 bool CommandParser::IsWebSocketPortValid()
646 {
647     if (IsSet("lws")) {
648         if (CheckParamInvalidity(Value("lws"), true)) {
649             errorInfo = "Launch -lws parameters is not match regex.";
650             return false;
651         }
652         int port = atoi(Value("lws").c_str());
653         if (port < MIN_PORT || port > MAX_PORT) {
654             errorInfo = std::string("WebSocket listening port out of range: " + std::to_string(MIN_PORT) + "-" +
655                 std::to_string(MAX_PORT) + ".");
656             ELOG("Launch -lws parameters abnormal!");
657             return false;
658         }
659     }
660     ILOG("CommandParser WebSocket listening port: %s", Value("lws").c_str());
661     return true;
662 }
663 
IsScreenModeValid()664 bool CommandParser::IsScreenModeValid()
665 {
666     std::string mode("dynamic");
667     if (IsSet("sm")) {
668         mode = Value("sm");
669         if (mode != "dynamic" && mode != "static") {
670             errorInfo = std::string("Screen picture transport mode suported: dynamic or static");
671             ELOG("Launch -sm parameters abnormal!");
672             return false;
673         }
674         screenMode = (mode == "static" ? CommandParser::ScreenMode::STATIC :
675                       CommandParser::ScreenMode::DYNAMIC);
676     }
677     ILOG("CommandParser screen mode: %s", mode.c_str());
678     return true;
679 }
680 
IsLanguageValid()681 bool CommandParser::IsLanguageValid()
682 {
683     if (!IsSet("l")) {
684         return true;
685     }
686     std::string lan = Value("l");
687     if (CheckParamInvalidity(lan, false)) {
688         errorInfo = "Launch -l parameters is not match regex.";
689         return false;
690     }
691     ILOG("CommandParser l: %s", lan.c_str());
692     return true;
693 }
694 
IsTracePipeNameValid()695 bool CommandParser::IsTracePipeNameValid()
696 {
697     if (!IsSet("ts")) {
698         return true;
699     }
700     std::string tsName = Value("ts");
701     if (CheckParamInvalidity(tsName, false)) {
702         errorInfo = "Launch -ts parameters is not match regex.";
703         return false;
704     }
705     ILOG("CommandParser ts: %s", tsName.c_str());
706     return true;
707 }
708 
IsLocalSocketNameValid()709 bool CommandParser::IsLocalSocketNameValid()
710 {
711     if (!IsSet("s")) {
712         return true;
713     }
714     std::string socketName = Value("s");
715     std::string regexStr = "^(?:[a-zA-Z0-9-_./\\s*]+)$";
716     std::regex reg(regexStr);
717     if (!std::regex_match(socketName.cbegin(), socketName.cend(), reg)) {
718         errorInfo = "Launch -s parameters is not match regex.";
719         return false;
720     }
721     ILOG("CommandParser s: %s", socketName.c_str());
722     return true;
723 }
724 
IsConfigChangesValid()725 bool CommandParser::IsConfigChangesValid()
726 {
727     if (!IsSet("cc")) {
728         return true;
729     }
730     std::string configChange = Value("cc");
731     if (CheckParamInvalidity(configChange, false)) {
732         ELOG("Launch -cc parameters is not match regex.");
733         return false;
734     }
735     ILOG("CommandParser cc: %s", configChange.c_str());
736     return true;
737 }
738 
IsScreenDensityValid()739 bool CommandParser::IsScreenDensityValid()
740 {
741     if (!IsSet("sd")) {
742         return true;
743     }
744     std::string density = Value("sd");
745     if (CheckParamInvalidity(density, true)) {
746         errorInfo = "Launch -sd parameters is not match regex.";
747         return false;
748     }
749     ILOG("CommandParser sd: %s", density.c_str());
750     return true;
751 }
752 
IsContainerSdkPathValid()753 bool CommandParser::IsContainerSdkPathValid()
754 {
755     if (!IsSet("hsp")) {
756         return true;
757     }
758 
759     std::string path = Value("hsp");
760     if (!FileSystem::IsDirectoryExists(path)) {
761         errorInfo = std::string("The container sdk path does not exist.");
762         ELOG("Launch -hsp parameters abnormal!");
763         return false;
764     }
765     containerSdkPath = path;
766     return true;
767 }
768 
HelpText()769 std::string CommandParser::HelpText()
770 {
771     std::string helpText = "Usage:\n";
772     for (auto index = regsHelpMap.begin(); index != regsHelpMap.end(); index++) {
773         helpText += "-" + index->first + " ";
774         helpText += index->second + "\n";
775     }
776     return helpText;
777 }
778 
ProcessingCommand(const std::vector<std::string> & strs)779 void CommandParser::ProcessingCommand(const std::vector<std::string>& strs)
780 {
781     for (unsigned int i = 0; i < strs.size(); ++i) {
782         std::string index = strs[i];
783         auto regInfo = regsArgsCountMap.find(strs[i]);
784         if (regInfo == regsArgsCountMap.end()) {
785             continue;
786         }
787 
788         std::vector<std::string> args;
789         for (uint32_t j = 0; j < regInfo->second; ++j) {
790             if (i == strs.size() - 1  || strs[i + 1][0] == '-') {
791                 args.push_back("");
792                 break;
793             }
794             args.push_back(strs[++i]);
795         }
796         argsMap[index] = args;
797     }
798 }
799 
GetProjectModelEnumValue() const800 int CommandParser::GetProjectModelEnumValue() const
801 {
802     auto idxVal = std::distance(projectModels.begin(),
803                                 find(projectModels.begin(), projectModels.end(), projectModel));
804     idxVal = (idxVal >= projectModels.size()) ? 0 : idxVal;
805     return idxVal;
806 }
807 
GetProjectModelEnumName(int enumValue) const808 std::string CommandParser::GetProjectModelEnumName(int enumValue) const
809 {
810     if (enumValue < 0 || enumValue >= projectModels.size()) {
811         enumValue = 0;
812     }
813     return projectModels[enumValue];
814 }
815 
CheckParamInvalidity(std::string param,bool isNum=false)816 bool CommandParser::CheckParamInvalidity(std::string param, bool isNum = false)
817 {
818     std::regex reg(isNum ? regex4Num : regex4Str);
819     return !std::regex_match(param.cbegin(), param.cend(), reg);
820 }
821 
IsComponentModeValid()822 bool CommandParser::IsComponentModeValid()
823 {
824     if (!IsSet("cpm")) {
825         return true;
826     }
827 
828     std::string cpm = Value("cpm");
829     if (cpm != "true" && cpm != "false") {
830         errorInfo = std::string("The component mode argument unsupported.");
831         ELOG("Launch -cpm parameters abnormal!");
832         return false;
833     }
834 
835     isComponentMode = cpm == "true" ? true : false;
836     return true;
837 }
838 
IsAbilityPathValid()839 bool CommandParser::IsAbilityPathValid()
840 {
841     if (!IsSet("d")) {
842         return true;
843     }
844     if (deviceType == "liteWearable" || deviceType == "smartVision") {
845         return true;
846     }
847     if (!IsSet("abp")) {
848         errorInfo = "Launch -d parameters without -abp parameters.";
849         return false;
850     }
851     std::string path = Value("abp");
852     if (path.empty()) {
853         errorInfo = std::string("The ability path is empty.");
854         ELOG("Launch -abp parameters abnormal!");
855         return false;
856     }
857     abilityPath = path;
858     return true;
859 }
860 
IsAbilityNameValid()861 bool CommandParser::IsAbilityNameValid()
862 {
863     if (!IsSet("d")) {
864         return true;
865     }
866     if (deviceType == "liteWearable" || deviceType == "smartVision") {
867         return true;
868     }
869     if (!IsSet("abn")) {
870         ELOG("Launch -d parameters without -abn parameters.");
871         return true; // 兼容老版本IDE(沒有abn参数)
872     }
873     std::string name = Value("abn");
874     if (name.empty()) {
875         errorInfo = std::string("The ability name is empty.");
876         ELOG("Launch -abn parameters abnormal!");
877         return false;
878     }
879     abilityName = name;
880     return true;
881 }
882 
IsStaticCardValid()883 bool CommandParser::IsStaticCardValid()
884 {
885     if (!IsSet("staticCard")) {
886         return true;
887     }
888     std::string val = Value("staticCard");
889     if (val != "true" && val != "false") {
890         errorInfo = std::string("The staticCard argument unsupported.");
891         ELOG("Launch -staticCard parameters abnormal!");
892         return false;
893     }
894     if (val == "true") {
895         staticCard = true;
896     }
897     return true;
898 }
899 
IsMainArgLengthInvalid(const char * str) const900 bool CommandParser::IsMainArgLengthInvalid(const char* str) const
901 {
902     size_t argLength = strlen(str);
903     if (argLength > maxMainArgLength) {
904         ELOG("param size is more than %d", maxMainArgLength);
905         return true;
906     }
907     return false;
908 }
909 
IsFoldableValid()910 bool CommandParser::IsFoldableValid()
911 {
912     if (!IsSet("foldable")) {
913         return true;
914     }
915     std::string val = Value("foldable");
916     if (val != "true" && val != "false") {
917         errorInfo = std::string("The foldable argument unsupported.");
918         ELOG("Launch -foldable parameters abnormal!");
919         return false;
920     }
921     if (val == "true") {
922         foldable = true;
923     }
924     return true;
925 }
926 
IsFoldStatusValid()927 bool CommandParser::IsFoldStatusValid()
928 {
929     if ((!IsSet("foldable")) || Value("foldable") != "true") {
930         return true;
931     }
932     if (IsSet("foldStatus")) {
933         if (Value("foldStatus") == "fold" || Value("foldStatus") == "unfold" ||
934             Value("foldStatus") == "unknown" || Value("foldStatus") == "half_fold") {
935             foldStatus = Value("foldStatus");
936             return true;
937         }
938     }
939     ELOG("Launch -foldStatus parameters abnormal!");
940     return false;
941 }
942 
IsFoldResolutionValid()943 bool CommandParser::IsFoldResolutionValid()
944 {
945     if ((!IsSet("foldable")) || Value("foldable") != "true") {
946         return true;
947     }
948     if (IsSet("fr")) {
949         if (IsResolutionArgValid(std::string("-fr"))) {
950             foldResolutionWidth = atoi(Values("-fr")[0].c_str());
951             foldResolutionHeight = atoi(Values("-fr")[1].c_str());
952             ILOG("CommandParser fold resolution: %d %d", foldResolutionWidth, foldResolutionHeight);
953             return true;
954         }
955         ELOG("Launch -fr parameters abnormal!");
956         return false;
957     }
958     ELOG("Launch -fr parameters abnormal!");
959     errorInfo = std::string("Fold resolution must be setted.");
960     return false;
961 }
962 
IsFoldable() const963 bool CommandParser::IsFoldable() const
964 {
965     return foldable;
966 }
967 
GetFoldStatus() const968 std::string CommandParser::GetFoldStatus() const
969 {
970     return foldStatus;
971 }
972 
GetFoldResolutionWidth() const973 int32_t CommandParser::GetFoldResolutionWidth() const
974 {
975     return foldResolutionWidth;
976 }
977 
GetFoldResolutionHeight() const978 int32_t CommandParser::GetFoldResolutionHeight() const
979 {
980     return foldResolutionHeight;
981 }
982 
GetLoaderJsonPath() const983 std::string CommandParser::GetLoaderJsonPath() const
984 {
985     return loaderJsonPath;
986 }
987 
IsLoaderJsonPathValid()988 bool CommandParser::IsLoaderJsonPathValid()
989 {
990     if (!IsSet("ljPath")) {
991         return true;
992     }
993     std::string path = Value("ljPath");
994     if (!FileSystem::IsFileExists(path)) {
995         errorInfo = std::string("The configuration loader.json path does not exist.");
996         ELOG("Launch -ljPath parameters abnormal!");
997         return false;
998     }
999     loaderJsonPath = path;
1000     return true;
1001 }
1002 
ParseArgs(int argc,char * argv[])1003 int CommandParser::ParseArgs(int argc, char* argv[])
1004 {
1005     int startParamInvalidCode = 11;
1006     int defaultReturnVal = -1;
1007     std::vector<std::string> strs;
1008     for (int i = 1; i < argc; ++i) {
1009         if (IsMainArgLengthInvalid(argv[i])) {
1010             return startParamInvalidCode;
1011         }
1012         strs.push_back(argv[i]);
1013     }
1014     if (!ProcessCommand(strs)) {
1015         return 0;
1016     }
1017     if (!IsCommandValid()) {
1018         FLOG("Start args is invalid.");
1019         return startParamInvalidCode;
1020     }
1021     return defaultReturnVal;
1022 }
1023 
GetCommandInfo(CommandInfo & info) const1024 void CommandParser::GetCommandInfo(CommandInfo& info) const
1025 {
1026     info.deviceType = GetDeviceType();
1027     info.pages = GetPages();
1028     info.appResourcePath = GetAppResourcePath();
1029     info.isCardDisplay = IsCardDisplay();
1030     info.containerSdkPath = GetContainerSdkPath();
1031     info.isComponentMode = IsComponentMode();
1032     info.loaderJsonPath = GetLoaderJsonPath();
1033     info.abilityPath = GetAbilityPath();
1034     info.abilityName = GetAbilityName();
1035     info.configPath = GetConfigPath();
1036     info.screenShape = GetScreenShape();
1037     info.orignalResolutionWidth = GetOrignalResolutionWidth();
1038     info.orignalResolutionHeight = GetOrignalResolutionHeight();
1039     info.compressionResolutionWidth = GetCompressionResolutionWidth();
1040     info.compressionResolutionHeight = GetCompressionResolutionHeight();
1041 }
1042 
GetFoldInfo(FoldInfo & info) const1043 void CommandParser::GetFoldInfo(FoldInfo& info) const
1044 {
1045     info.foldable = IsFoldable();
1046     info.foldStatus = GetFoldStatus();
1047     info.foldResolutionWidth = GetFoldResolutionWidth();
1048     info.foldResolutionHeight = GetFoldResolutionHeight();
1049 }
1050 
GetSid() const1051 std::string CommandParser::GetSid() const
1052 {
1053     return sid;
1054 }
1055 
IsSidValid()1056 bool CommandParser::IsSidValid()
1057 {
1058     if (!IsSet("sid")) {
1059         return true;
1060     }
1061     std::string value = Value("sid");
1062     std::regex reg(regex4Sid);
1063     if (!std::regex_match(value.cbegin(), value.cend(), reg)) {
1064         errorInfo = "Launch -sid parameter is not match regex.";
1065         ELOG("Launch -sid parameter abnormal!");
1066         return false;
1067     }
1068     sid = value;
1069     return true;
1070 }
1071 
1072 #ifdef COMPONENT_TEST_ENABLED
GetComponentTestConfig() const1073 std::string CommandParser::GetComponentTestConfig() const
1074 {
1075     return componentTestConfig;
1076 }
1077 #endif // COMPONENT_TEST_ENABLED