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