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