• 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 "CommandLine.h"
17 
18 #include <algorithm>
19 #include <regex>
20 #include <sstream>
21 
22 #include "CommandLineInterface.h"
23 #include "CommandParser.h"
24 #include "Interrupter.h"
25 #include "JsApp.h"
26 #include "JsAppImpl.h"
27 #include "JsonReader.h"
28 #include "LanguageManagerImpl.h"
29 #include "ModelConfig.h"
30 #include "ModelManager.h"
31 #include "MouseInputImpl.h"
32 #include "MouseWheelImpl.h"
33 #include "KeyInputImpl.h"
34 #include "PreviewerEngineLog.h"
35 #include "SharedData.h"
36 #include "VirtualMessageImpl.h"
37 #include "VirtualScreenImpl.h"
38 
39 using namespace std;
40 
CommandLine(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)41 CommandLine::CommandLine(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
42     : args(arg), cliSocket(socket), type(commandType), commandName("")
43 {
44     SetCommandResult("result", true);
45 }
46 
~CommandLine()47 CommandLine::~CommandLine()
48 {
49     args.clear();
50 }
51 
CheckAndRun()52 void CommandLine::CheckAndRun()
53 {
54     if (!IsArgValid()) {
55         ELOG("CheckAndRun: invalid command params");
56         SetCommandResult("result", false);
57         SendResult();
58         return;
59     }
60     Run();
61     SendResult();
62 }
63 
SendResult()64 void CommandLine::SendResult()
65 {
66     if (commandResult.empty()) {
67         return;
68     }
69     cliSocket << commandResult.toStyledString();
70     commandResult.clear();
71 }
72 
RunAndSendResultToManager()73 void CommandLine::RunAndSendResultToManager()
74 {
75     Run();
76     SendResultToManager();
77 }
78 
SendResultToManager()79 void CommandLine::SendResultToManager()
80 {
81     if (commandResultToManager.empty()) {
82         return;
83     }
84     cliSocket << commandResultToManager.toStyledString();
85     commandResultToManager.clear();
86 }
87 
IsArgValid() const88 bool CommandLine::IsArgValid() const
89 {
90     if (type == CommandType::GET) {
91         return IsGetArgValid();
92     }
93     if (type == CommandType::SET) {
94         return IsSetArgValid();
95     }
96     if (type == CommandType::ACTION) {
97         return IsActionArgValid();
98     }
99     return true;
100 }
101 
ToUint8(std::string str) const102 uint8_t CommandLine::ToUint8(std::string str) const
103 {
104     int value = atoi(str.data());
105     if (value > UINT8_MAX) {
106         ELOG("CommandLine::ToUint8 value is overflow, value: %d", value);
107     }
108     return static_cast<uint8_t>(value);
109 }
110 
SetCommandName(std::string command)111 void CommandLine::SetCommandName(std::string command)
112 {
113     this->commandName = command;
114 }
115 
SetCommandResult(const std::string & resultType,const Json::Value & resultContent)116 void CommandLine::SetCommandResult(const std::string& resultType, const Json::Value& resultContent)
117 {
118     this->commandResult["version"] = CommandLineInterface::COMMAND_VERSION;
119     this->commandResult["command"] = this->commandName;
120     this->commandResult[resultType] = resultContent;
121 }
122 
SetResultToManager(const std::string & resultType,const Json::Value & resultContent,const std::string & messageType)123 void CommandLine::SetResultToManager(const std::string& resultType,
124                                      const Json::Value& resultContent,
125                                      const std::string& messageType)
126 {
127     this->commandResultToManager["MessageType"] = messageType;
128     this->commandResultToManager[resultType] = resultContent;
129 }
130 
Run()131 void CommandLine::Run()
132 {
133     if (type == CommandType::GET) {
134         RunGet();
135     } else if (type == CommandType::SET) {
136         RunSet();
137     } else if (type == CommandType::ACTION) {
138         RunAction();
139     }
140 }
141 
IsBoolType(std::string arg) const142 bool CommandLine::IsBoolType(std::string arg) const
143 {
144     regex tofrx("^(true)|(false)$");
145     if (regex_match(arg, tofrx)) {
146         return true;
147     }
148     return false;
149 }
150 
IsIntType(string arg) const151 bool CommandLine::IsIntType(string arg) const
152 {
153     regex isInt("^\\d+$");
154     return regex_match(arg, isInt);
155 }
156 
IsOneDigitFloatType(string arg,bool allowNegativeNumber) const157 bool CommandLine::IsOneDigitFloatType(string arg, bool allowNegativeNumber) const
158 {
159     if (allowNegativeNumber) {
160         regex isFloat("^-?\\d+(\\.\\d+)?$");
161         return regex_match(arg, isFloat);
162     } else {
163         regex isFloat("^\\d(\\.\\d+)?$");
164         return regex_match(arg, isFloat);
165     }
166 }
167 
SetEventParams(EventParams & params)168 void TouchAndMouseCommand::SetEventParams(EventParams& params)
169 {
170     if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) {
171         return;
172     }
173     MouseInputImpl::GetInstance().SetMousePosition(params.x, params.y);
174     MouseInputImpl::GetInstance().SetMouseStatus(params.type);
175     MouseInputImpl::GetInstance().SetMouseButton(params.button);
176     MouseInputImpl::GetInstance().SetMouseAction(params.action);
177     MouseInputImpl::GetInstance().SetSourceType(params.sourceType);
178     MouseInputImpl::GetInstance().SetSourceTool(params.sourceTool);
179     MouseInputImpl::GetInstance().SetPressedBtns(params.pressedBtnsVec);
180     MouseInputImpl::GetInstance().SetAxisValues(params.axisVec);
181     MouseInputImpl::GetInstance().DispatchOsTouchEvent();
182     std::stringstream ss;
183     ss << "[";
184     for (double val : params.axisVec) {
185         ss << " " << val << " ";
186     }
187     ss << "]" << std::endl;
188     ILOG("%s(%f,%f,%d,%d,%d,%d,%d,%d,%d,%s)", params.name.c_str(), params.x, params.y, params.type, params.button,
189         params.action, params.sourceType, params.sourceTool, params.pressedBtnsVec.size(), params.axisVec.size(),
190         ss.str().c_str());
191 }
192 
IsActionArgValid() const193 bool TouchPressCommand::IsActionArgValid() const
194 {
195     if (args.isNull() || !args.isMember("x") || !args.isMember("y") ||
196         !args["x"].isInt() || !args["y"].isInt()) {
197         return false;
198     }
199     int32_t pointX = args["x"].asInt();
200     int32_t pointY = args["y"].asInt();
201     if (pointX < 0 || pointX > VirtualScreenImpl::GetInstance().GetCurrentWidth()) {
202         ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentWidth());
203         return false;
204     }
205     if (pointY < 0 || pointY > VirtualScreenImpl::GetInstance().GetCurrentHeight()) {
206         ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentHeight());
207         return false;
208     }
209     return true;
210 }
211 
TouchPressCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)212 TouchPressCommand::TouchPressCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
213     : CommandLine(commandType, arg, socket)
214 {
215 }
216 
RunAction()217 void TouchPressCommand::RunAction()
218 {
219     int type = 0;
220     EventParams param;
221     param.x = atof(args["x"].asString().data());
222     param.y = atof(args["y"].asString().data());
223     param.type = type;
224     param.name = "TouchPress";
225     param.button = MouseInputImpl::GetInstance().defaultButton;
226     param.action = MouseInputImpl::GetInstance().defaultAction;
227     param.sourceType = MouseInputImpl::GetInstance().defaultSourceType;
228     param.sourceTool = MouseInputImpl::GetInstance().defaultSourceTool;
229     SetEventParams(param);
230     SetCommandResult("result", true);
231 }
232 
IsActionArgValid() const233 bool MouseWheelCommand::IsActionArgValid() const
234 {
235     if (args.isNull() || !args.isMember("rotate") || !IsOneDigitFloatType(args["rotate"].asString(), true)) {
236         return false;
237     }
238     return true;
239 }
240 
MouseWheelCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)241 MouseWheelCommand::MouseWheelCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
242     : CommandLine(commandType, arg, socket)
243 {
244 }
245 
RunAction()246 void MouseWheelCommand::RunAction()
247 {
248     if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) {
249         return;
250     }
251     MouseWheelImpl::GetInstance().SetRotate(atof(args["rotate"].asString().data()));
252     SetCommandResult("result", true);
253     ILOG("CrownRotate (%s)", args["rotate"].asString().c_str());
254 }
255 
TouchReleaseCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)256 TouchReleaseCommand::TouchReleaseCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
257     : CommandLine(commandType, arg, socket)
258 {
259 }
260 
RunAction()261 void TouchReleaseCommand::RunAction()
262 {
263     int type = 1;
264     EventParams param;
265     param.x = atof(args["x"].asString().data());
266     param.y = atof(args["y"].asString().data());
267     param.type = type;
268     param.name = "TouchRelease";
269     param.button = MouseInputImpl::GetInstance().defaultButton;
270     param.action = MouseInputImpl::GetInstance().defaultAction;
271     param.sourceType = MouseInputImpl::GetInstance().defaultSourceType;
272     param.sourceTool = MouseInputImpl::GetInstance().defaultSourceTool;
273     SetEventParams(param);
274     SetCommandResult("result", true);
275 }
276 
IsActionArgValid() const277 bool TouchMoveCommand::IsActionArgValid() const
278 {
279     if (args.isNull() || !args.isMember("x") || !args.isMember("y") ||
280         !args["x"].isInt() || !args["y"].isInt()) {
281         return false;
282     }
283     int32_t pX = args["x"].asInt();
284     int32_t pY = args["y"].asInt();
285     if (pX < 0 || pX > VirtualScreenImpl::GetInstance().GetCurrentWidth()) {
286         ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentWidth());
287         return false;
288     }
289     if (pY < 0 || pY > VirtualScreenImpl::GetInstance().GetCurrentHeight()) {
290         ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentHeight());
291         return false;
292     }
293     return true;
294 }
295 
TouchMoveCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)296 TouchMoveCommand::TouchMoveCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
297     : CommandLine(commandType, arg, socket)
298 {
299 }
300 
RunAction()301 void TouchMoveCommand::RunAction()
302 {
303     int type = 2;
304     EventParams param;
305     param.x = atof(args["x"].asString().data());
306     param.y = atof(args["y"].asString().data());
307     param.type = type;
308     param.name = "TouchMove";
309     param.button = MouseInputImpl::GetInstance().defaultButton;
310     param.action = MouseInputImpl::GetInstance().defaultAction;
311     param.sourceType = MouseInputImpl::GetInstance().defaultSourceType;
312     param.sourceTool = MouseInputImpl::GetInstance().defaultSourceTool;
313     SetEventParams(param);
314     SetCommandResult("result", true);
315 }
316 
PowerCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)317 PowerCommand::PowerCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
318     : CommandLine(commandType, arg, socket)
319 {
320 }
321 
IsSetArgValid() const322 bool PowerCommand::IsSetArgValid() const
323 {
324     if (args.isNull() || !args.isMember("Power") || !IsOneDigitFloatType(args["Power"].asString(), false)) {
325         ELOG("Invalid number of arguments!");
326         return false;
327     }
328     float val = args["Power"].asFloat();
329     if (!SharedData<double>::IsValid(SharedDataType::BATTERY_LEVEL, val)) {
330         ELOG("PowerCommand invalid value: %f", val);
331         return false;
332     }
333     return true;
334 }
335 
RunGet()336 void PowerCommand::RunGet()
337 {
338     double power = SharedData<double>::GetData(SharedDataType::BATTERY_LEVEL);
339     Json::Value resultContent;
340     resultContent["Power"] = power;
341     SetCommandResult("result", resultContent);
342     ILOG("Get power run finished");
343 }
344 
RunSet()345 void PowerCommand::RunSet()
346 {
347     string power(args["Power"].asString());
348     SharedData<double>::SetData(SharedDataType::BATTERY_LEVEL, atof(power.data()));
349     Json::Value resultContent = true;
350     SetCommandResult("result", resultContent);
351     ILOG("Set power run finished, the value is: %s", args["Power"].asString().c_str());
352 }
353 
VolumeCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)354 VolumeCommand::VolumeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
355     : CommandLine(commandType, arg, socket)
356 {
357 }
358 
IsSetArgValid() const359 bool VolumeCommand::IsSetArgValid() const
360 {
361     return true;
362 }
363 
RunGet()364 void VolumeCommand::RunGet()
365 {
366     Json::Value resultContent = "Command offline";
367     SetCommandResult("result", resultContent);
368     ILOG("Command offline");
369 }
370 
RunSet()371 void VolumeCommand::RunSet()
372 {
373     Json::Value resultContent = "Command offline";
374     SetCommandResult("result", resultContent);
375     ILOG("Command offline");
376 }
377 
BarometerCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)378 BarometerCommand::BarometerCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
379     : CommandLine(commandType, arg, socket)
380 {
381 }
382 
IsSetArgValid() const383 bool BarometerCommand::IsSetArgValid() const
384 {
385     if (args.isNull() || !args.isMember("Barometer") || !args["Barometer"].isString()) {
386         ELOG("Invalid number of arguments!");
387         return false;
388     }
389     string barometer(args["Barometer"].asString());
390     if (!IsIntType(barometer)) {
391         ELOG("Invalid arguments!");
392         return false;
393     }
394 
395     if (!SharedData<uint32_t>::IsValid(SharedDataType::PRESSURE_VALUE, static_cast<uint32_t>(atoi(barometer.data())))) {
396         ELOG("Barometer invalid value: %d", atoi(barometer.data()));
397         return false;
398     }
399     return true;
400 }
401 
RunGet()402 void BarometerCommand::RunGet()
403 {
404     int barometer = static_cast<int>(SharedData<uint32_t>::GetData(SharedDataType::PRESSURE_VALUE));
405     Json::Value resultContent;
406     resultContent["Barometer"] = barometer;
407     SetCommandResult("result", resultContent);
408     ILOG("Get barometer run finished");
409 }
410 
RunSet()411 void BarometerCommand::RunSet()
412 {
413     string barometer(args["Barometer"].asString());
414     SharedData<uint32_t>::SetData(SharedDataType::PRESSURE_VALUE, static_cast<uint32_t>(atoi(barometer.data())));
415     SetCommandResult("result", true);
416     ILOG("Set barometer run finished, the value is: %s", args["Barometer"].asString().c_str());
417 }
418 
ResolutionSwitchCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)419 ResolutionSwitchCommand::ResolutionSwitchCommand(CommandType commandType,
420                                                  const Json::Value& arg,
421                                                  const LocalSocket& socket)
422     : CommandLine(commandType, arg, socket)
423 {
424 }
425 
IsSetArgValid() const426 bool ResolutionSwitchCommand::IsSetArgValid() const
427 {
428     if (args.isNull() || !args.isMember("originWidth") || !args.isMember("originHeight") || !args.isMember("width") ||
429         !args.isMember("height") || !args.isMember("screenDensity")) {
430         ELOG("Invalid param of arguments!");
431         return false;
432     }
433     if (!args["originWidth"].isInt() || !args["originHeight"].isInt() ||
434         !args["screenDensity"].isInt() || !args["width"].isInt() || !args["height"].isInt()) {
435         ELOG("Invalid number of arguments!");
436         return false;
437     }
438     if (!IsIntValValid(args)) {
439         return false;
440     }
441     if (args.isMember("reason")) {
442         if (!args["reason"].isString()) {
443             return false;
444         }
445         string reason = args["reason"].asString();
446         if (reason != "rotation" && reason != "resize" && reason != "undefined") {
447             ELOG("Invalid value of reason!");
448             return false;
449         }
450     }
451     return true;
452 }
453 
IsIntValValid(const Json::Value & args) const454 bool ResolutionSwitchCommand::IsIntValValid(const Json::Value& args) const
455 {
456     if (args["originWidth"].asInt() < minWidth || args["originWidth"].asInt() > maxWidth ||
457         args["originHeight"].asInt() < minWidth || args["originHeight"].asInt() > maxWidth ||
458         args["width"].asInt() < minWidth || args["width"].asInt() > maxWidth ||
459         args["height"].asInt() < minWidth || args["height"].asInt() > maxWidth) {
460         ELOG("width or height is out of range %d-%d", minWidth, maxWidth);
461         return false;
462     }
463     if (args["screenDensity"].asInt() < minDpi || args["screenDensity"].asInt() > maxDpi) {
464         ELOG("screenDensity is out of range %d-%d", minDpi, maxDpi);
465         return false;
466     }
467     return true;
468 }
469 
RunSet()470 void ResolutionSwitchCommand::RunSet()
471 {
472     int32_t originWidth = args["originWidth"].asInt();
473     int32_t originHeight = args["originHeight"].asInt();
474     int32_t width = args["width"].asInt();
475     int32_t height = args["height"].asInt();
476     int32_t screenDensity = args["screenDensity"].asInt();
477     string reason = "undefined";
478     if (args.isMember("reason")) {
479         reason = args["reason"].asString();
480     }
481     ResolutionParam param(originWidth, originHeight, width, height);
482     JsAppImpl::GetInstance().ResolutionChanged(param, screenDensity, reason);
483     SetCommandResult("result", true);
484     ILOG("ResolutionSwitch run finished.");
485 }
486 
OrientationCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)487 OrientationCommand::OrientationCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
488     : CommandLine(commandType, arg, socket)
489 {
490 }
491 
IsSetArgValid() const492 bool OrientationCommand::IsSetArgValid() const
493 {
494     if (args.isNull() || !args.isMember("Orientation") || !args["Orientation"].isString()) {
495         ELOG("Invalid number of arguments!");
496         return false;
497     }
498     if (args["Orientation"].asString() != "portrait" && args["Orientation"].asString() != "landscape") {
499         ELOG("Orientation just support [portrait,landscape].");
500         return false;
501     }
502     return true;
503 }
504 
RunSet()505 void OrientationCommand::RunSet()
506 {
507     std::string commandOrientation = args["Orientation"].asString();
508     std::string currentOrientation = JsAppImpl::GetInstance().GetOrientation();
509     if (commandOrientation != currentOrientation) {
510         JsAppImpl::GetInstance().OrientationChanged(commandOrientation);
511     }
512     SetCommandResult("result", true);
513     ILOG("Set Orientation run finished, Orientation is: %s", args["Orientation"].asString().c_str());
514 }
515 
ColorModeCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)516 ColorModeCommand::ColorModeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
517     : CommandLine(commandType, arg, socket)
518 {
519 }
520 
IsSetArgValid() const521 bool ColorModeCommand::IsSetArgValid() const
522 {
523     if (args.isNull() || !args.isMember("ColorMode") || !args["ColorMode"].isString()) {
524         ELOG("Invalid number of arguments!");
525         return false;
526     }
527 
528     if (args["ColorMode"].asString() != "light" && args["ColorMode"].asString() != "dark") {
529         ELOG("ColorMode just support [light,dark]");
530         return false;
531     }
532     return true;
533 }
534 
RunSet()535 void ColorModeCommand::RunSet()
536 {
537     std::string commandColorMode = args["ColorMode"].asString();
538     std::string currentColorMode = JsAppImpl::GetInstance().GetColorMode();
539     if (commandColorMode != currentColorMode) {
540         JsAppImpl::GetInstance().SetArgsColorMode(args["ColorMode"].asString());
541         JsAppImpl::GetInstance().ColorModeChanged(commandColorMode);
542     }
543     SetCommandResult("result", true);
544     ILOG("Set ColorMode run finished, ColorMode is: %s", args["ColorMode"].asString().c_str());
545 }
546 
FontSelectCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)547 FontSelectCommand::FontSelectCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
548     : CommandLine(commandType, arg, socket)
549 {
550 }
551 
IsSetArgValid() const552 bool FontSelectCommand::IsSetArgValid() const
553 {
554     if (args.isNull() || !args.isMember("FontSelect") || !args["FontSelect"].isBool()) {
555         ELOG("Invalid number of arguments!");
556         return false;
557     }
558     return true;
559 }
560 
RunSet()561 void FontSelectCommand::RunSet()
562 {
563     SetCommandResult("result", true);
564     ILOG("FontSelect finished, currentSelect is: %d", args["FontSelect"].asBool());
565 }
566 
MemoryRefreshCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)567 MemoryRefreshCommand::MemoryRefreshCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
568     : CommandLine(commandType, arg, socket)
569 {
570 }
571 
IsSetArgValid() const572 bool MemoryRefreshCommand::IsSetArgValid() const
573 {
574     if (args.isNull()) {
575         ELOG("Invalid MemoryRefresh of arguments!");
576         return false;
577     }
578     return true;
579 }
580 
RunSet()581 void MemoryRefreshCommand::RunSet()
582 {
583     ILOG("MemoryRefreshCommand begin.");
584     bool ret = JsAppImpl::GetInstance().MemoryRefresh(args.toStyledString());
585     SetCommandResult("result", ret);
586     ILOG("MemoryRefresh finished.");
587 }
588 
LoadDocumentCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)589 LoadDocumentCommand::LoadDocumentCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
590     : CommandLine(commandType, arg, socket)
591 {
592 }
593 
IsSetArgValid() const594 bool LoadDocumentCommand::IsSetArgValid() const
595 {
596     if (args.isNull() || !args.isMember("url") || !args.isMember("className") || !args.isMember("previewParam") ||
597         !args["url"].isString() || !args["className"].isString() || !args["previewParam"].isObject()) {
598         return false;
599     }
600     Json::Value previewParam = args["previewParam"];
601     if (!previewParam["width"].isInt() || !previewParam["height"].isInt() || !previewParam["dpi"].isInt() ||
602         !previewParam["locale"].isString() || !previewParam["colorMode"].isString() ||
603         !previewParam["orientation"].isString() || !previewParam["deviceType"].isString()) {
604         return false;
605     }
606     if (!IsIntValValid(previewParam) || !IsStrValVailid(previewParam)) {
607         return false;
608     }
609     return true;
610 }
611 
IsIntValValid(const Json::Value & previewParam) const612 bool LoadDocumentCommand::IsIntValValid(const Json::Value& previewParam) const
613 {
614     int width = previewParam["width"].asInt();
615     int height = previewParam["height"].asInt();
616     int dpi = previewParam["dpi"].asInt();
617     if (width < minLoadDocWidth || width > maxLoadDocWidth || height < minLoadDocWidth ||
618         height > maxLoadDocWidth || dpi < minDpi || dpi > maxDpi) {
619         return false;
620     }
621     return true;
622 }
623 
IsStrValVailid(const Json::Value & previewParam) const624 bool LoadDocumentCommand::IsStrValVailid(const Json::Value& previewParam) const
625 {
626     string locale = previewParam["locale"].asString();
627     bool isLiteDevice = JsApp::IsLiteDevice(CommandParser::GetInstance().GetDeviceType());
628     if (isLiteDevice) {
629         if (std::find(liteSupportedLanguages.begin(), liteSupportedLanguages.end(), locale) ==
630             liteSupportedLanguages.end()) {
631             return false;
632         }
633     } else {
634         if (std::find(richSupportedLanguages.begin(), richSupportedLanguages.end(), locale) ==
635             richSupportedLanguages.end()) {
636             return false;
637         }
638     }
639     if (previewParam["colorMode"].asString() != "light" && previewParam["colorMode"].asString() != "dark") {
640         return false;
641     }
642     if (previewParam["orientation"].asString() != "portrait" &&
643         previewParam["orientation"].asString() != "landscape") {
644         return false;
645     }
646     if (std::find(LoadDocDevs.begin(), LoadDocDevs.end(), previewParam["deviceType"].asString()) ==
647         LoadDocDevs.end()) {
648         return false;
649     }
650     return true;
651 }
652 
RunSet()653 void LoadDocumentCommand::RunSet()
654 {
655     VirtualScreenImpl::GetInstance().SetLoadDocFlag(VirtualScreen::LoadDocType::START);
656     ILOG("LoadDocumentCommand begin.");
657     std::string pageUrl = args["url"].asString();
658     std::string className = args["className"].asString();
659     Json::Value previewParam = Json::nullValue;
660     if (args.isMember("previewParam")) {
661         previewParam = args["previewParam"];
662     }
663     JsAppImpl::GetInstance().LoadDocument(pageUrl, className, previewParam);
664     VirtualScreenImpl::GetInstance().SetLoadDocFlag(VirtualScreen::LoadDocType::FINISHED);
665     SetCommandResult("result", true);
666     ILOG("LoadDocumentCommand finished.");
667 }
668 
ReloadRuntimePageCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)669 ReloadRuntimePageCommand::ReloadRuntimePageCommand(CommandType commandType,
670                                                    const Json::Value& arg,
671                                                    const LocalSocket& socket)
672     : CommandLine(commandType, arg, socket)
673 {
674 }
675 
IsSetArgValid() const676 bool ReloadRuntimePageCommand::IsSetArgValid() const
677 {
678     if (args.isNull() || !args.isMember("ReloadRuntimePage") || !args["ReloadRuntimePage"].isString()) {
679         ELOG("Invalid number of arguments!");
680         return false;
681     }
682     return true;
683 }
684 
RunSet()685 void ReloadRuntimePageCommand::RunSet()
686 {
687     std::string currentPage = args["ReloadRuntimePage"].asString();
688     JsAppImpl::GetInstance().ReloadRuntimePage(currentPage);
689     SetCommandResult("result", true);
690     ILOG("ReloadRuntimePage finished, currentPage is: %s", args["ReloadRuntimePage"].asString().c_str());
691 }
692 
CurrentRouterCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)693 CurrentRouterCommand::CurrentRouterCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
694     : CommandLine(commandType, arg, socket)
695 {
696 }
697 
RunGet()698 void CurrentRouterCommand::RunGet()
699 {
700     Json::Value resultContent;
701     std::string currentRouter = VirtualScreenImpl::GetInstance().GetCurrentRouter();
702     resultContent["CurrentRouter"] = currentRouter;
703     SetResultToManager("args", resultContent, "CurrentJsRouter");
704     ILOG("Get CurrentRouter run finished.");
705 }
706 
LoadContentCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)707 LoadContentCommand::LoadContentCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
708     : CommandLine(commandType, arg, socket)
709 {
710 }
711 
RunGet()712 void LoadContentCommand::RunGet()
713 {
714     Json::Value resultContent;
715     std::string currentRouter = VirtualScreenImpl::GetInstance().GetAbilityCurrentRouter();
716     resultContent["AbilityCurrentRouter"] = currentRouter;
717     SetResultToManager("args", resultContent, "AbilityCurrentJsRouter");
718     ILOG("Get AbilityCurrentRouter run finished.");
719 }
720 
LanguageCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)721 LanguageCommand::LanguageCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
722     : CommandLine(commandType, arg, socket)
723 {
724 }
725 
IsSetArgValid() const726 bool LanguageCommand::IsSetArgValid() const
727 {
728     if (args.isNull() || !args.isMember("Language") || !args["Language"].isString()) {
729         ELOG("Invalid number of arguments!");
730         return false;
731     }
732 
733     CommandParser& cmdParser = CommandParser::GetInstance();
734     string deviceType = cmdParser.GetDeviceType();
735     bool isLiteDevice = JsApp::IsLiteDevice(deviceType);
736     if (isLiteDevice) {
737         if (std::find(liteSupportedLanguages.begin(), liteSupportedLanguages.end(), args["Language"].asString()) ==
738             liteSupportedLanguages.end()) {
739             ELOG("Language not support liteDevice : %s", args["Language"].asString().c_str());
740             return false;
741         }
742     } else {
743         if (std::find(richSupportedLanguages.begin(), richSupportedLanguages.end(), args["Language"].asString()) ==
744             richSupportedLanguages.end()) {
745             ELOG("Language not support richDevice : %s", args["Language"].asString().c_str());
746             return false;
747         }
748     }
749     return true;
750 }
751 
RunGet()752 void LanguageCommand::RunGet()
753 {
754     std::string language = SharedData<string>::GetData(SharedDataType::LANGUAGE);
755     Json::Value resultContent;
756     resultContent["Language"] = language;
757     SetCommandResult("result", resultContent);
758     ILOG("Get language run finished.");
759 }
760 
RunSet()761 void LanguageCommand::RunSet()
762 {
763     string language(args["Language"].asString());
764     SharedData<string>::SetData(SharedDataType::LANGUAGE, language);
765     SetCommandResult("result", true);
766     ILOG("Set language run finished, language is: %s", language.c_str());
767 }
768 
SupportedLanguagesCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)769 SupportedLanguagesCommand::SupportedLanguagesCommand(CommandType commandType,
770                                                      const Json::Value& arg,
771                                                      const LocalSocket& socket)
772     : CommandLine(commandType, arg, socket)
773 {
774 }
775 
RunGet()776 void SupportedLanguagesCommand::RunGet()
777 {
778     Json::Value resultContent;
779     Json::Value languageList;
780     string deviceType = CommandParser::GetInstance().GetDeviceType();
781     bool isLiteDevice = JsApp::IsLiteDevice(deviceType);
782     if (!deviceType.empty() && !isLiteDevice) {
783         for (auto iter = richSupportedLanguages.begin(); iter != richSupportedLanguages.end(); iter++) {
784             languageList.append(*iter);
785         }
786     } else {
787         for (auto iter = liteSupportedLanguages.begin(); iter != liteSupportedLanguages.end(); iter++) {
788             languageList.append(*iter);
789         }
790     }
791     resultContent["SupportedLanguages"] = languageList;
792     SetCommandResult("result", resultContent);
793     ILOG("Get supportedLanguages run finished.");
794 }
795 
LocationCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)796 LocationCommand::LocationCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
797     : CommandLine(commandType, arg, socket)
798 {
799 }
800 
IsSetArgValid() const801 bool LocationCommand::IsSetArgValid() const
802 {
803     if (args.isNull() || !args.isMember("latitude") || !args.isMember("longitude")) {
804         ELOG("Invalid number of arguments!");
805         return false;
806     }
807     string latitude(args["latitude"].asString());
808     string longitude(args["longitude"].asString());
809     regex isDob("^([\\-]*[0-9]{1,}[\\.][0-9]*)$");
810     if (!regex_match(latitude, isDob) || !regex_match(longitude, isDob)) {
811         ELOG("Invalid arguments!");
812         return false;
813     }
814 
815     if (!SharedData<double>::IsValid(SharedDataType::LATITUDE, atof(latitude.data()))) {
816         ELOG("LocationCommand invalid latitude value: %f", atof(latitude.data()));
817         return false;
818     }
819 
820     if (!SharedData<double>::IsValid(SharedDataType::LONGITUDE, atof(longitude.data()))) {
821         ELOG("LocationCommand invalid longitude value: %f", atof(longitude.data()));
822         return false;
823     }
824     return true;
825 }
826 
RunGet()827 void LocationCommand::RunGet()
828 {
829     double longitude = SharedData<double>::GetData(SharedDataType::LONGITUDE);
830     double latitude = SharedData<double>::GetData(SharedDataType::LATITUDE);
831     Json::Value resultContent;
832     resultContent["latitude"] = latitude;
833     resultContent["longitude"] = longitude;
834     SetCommandResult("result", resultContent);
835     ILOG("Get location run finished");
836 }
837 
RunSet()838 void LocationCommand::RunSet()
839 {
840     string latitude(args["latitude"].asString());
841     string longitude(args["longitude"].asString());
842     SharedData<double>::SetData(SharedDataType::LONGITUDE, atof(longitude.data()));
843     SharedData<double>::SetData(SharedDataType::LATITUDE, atof(latitude.data()));
844     Json::Value resultContent = true;
845     SetCommandResult("result", resultContent);
846     ILOG("Set location run finished, latitude: %s,longitude: %s", latitude.c_str(), longitude.c_str());
847 }
848 
DistributedCommunicationsCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)849 DistributedCommunicationsCommand::DistributedCommunicationsCommand(CommandType commandType,
850                                                                    const Json::Value& arg,
851                                                                    const LocalSocket& socket)
852     : CommandLine(commandType, arg, socket)
853 {
854 }
855 
RunAction()856 void DistributedCommunicationsCommand::RunAction()
857 {
858     MessageInfo info;
859     info.deviceID = args["DeviceId"].asString();
860     info.bundleName = args["bundleName"].asString();
861     info.abilityName = args["abilityName"].asString();
862     info.message = args["message"].asString();
863     VirtualMessageImpl::GetInstance().SendVirtualMessage(info);
864     Json::Value resultContent = true;
865     SetCommandResult("result", resultContent);
866     ILOG("Send distributedCommunications run finished");
867 }
868 
IsActionArgValid() const869 bool DistributedCommunicationsCommand::IsActionArgValid() const
870 {
871     if (args.isNull() || !args.isMember("DeviceId") || !args.isMember("bundleName") || !args.isMember("abilityName") ||
872         !args.isMember("message")) {
873         ELOG("Invalid number of arguments!");
874         return false;
875     }
876     if (args["DeviceId"].asString().empty() || args["bundleName"].asString().empty() ||
877         args["abilityName"].asString().empty() || args["message"].asString().empty()) {
878         ELOG("Invalid arguments!");
879         return false;
880     }
881     return true;
882 }
883 
StringToCharVector(string str) const884 std::vector<char> DistributedCommunicationsCommand::StringToCharVector(string str) const
885 {
886     vector<char> vec(str.begin(), str.end());
887     vec.push_back('\0');
888     return vec;
889 }
890 
KeepScreenOnStateCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)891 KeepScreenOnStateCommand::KeepScreenOnStateCommand(CommandType commandType,
892                                                    const Json::Value& arg,
893                                                    const LocalSocket& socket)
894     : CommandLine(commandType, arg, socket)
895 {
896 }
897 
RunGet()898 void KeepScreenOnStateCommand::RunGet()
899 {
900     Json::Value result;
901     result["KeepScreenOnState"] = SharedData<bool>::GetData(SharedDataType::KEEP_SCREEN_ON);
902     SetCommandResult("result", result);
903     ILOG("Get keepScreenOnState run finished");
904 }
905 
RunSet()906 void KeepScreenOnStateCommand::RunSet()
907 {
908     SharedData<bool>::SetData(SharedDataType::KEEP_SCREEN_ON, args["KeepScreenOnState"].asString() == "true");
909     Json::Value result = true;
910     SetCommandResult("result", result);
911     ILOG("Set keepScreenOnState run finished, the value is: %s", args["KeepScreenOnState"].asString().c_str());
912 }
913 
IsSetArgValid() const914 bool KeepScreenOnStateCommand::IsSetArgValid() const
915 {
916     if (args.isNull() || !args.isMember("KeepScreenOnState")) {
917         ELOG("Invalid number of arguments!");
918         return false;
919     }
920     if (!IsBoolType(args["KeepScreenOnState"].asString())) {
921         ELOG("arg 0: %s", args["KeepScreenOnState"].asString().c_str());
922         return false;
923     }
924     return true;
925 }
926 
WearingStateCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)927 WearingStateCommand::WearingStateCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
928     : CommandLine(commandType, arg, socket)
929 {
930 }
931 
RunGet()932 void WearingStateCommand::RunGet()
933 {
934     Json::Value result;
935     result["WearingState"] = SharedData<bool>::GetData(SharedDataType::WEARING_STATE);
936     SetCommandResult("result", result);
937     ILOG("Get wearingState run finished");
938 }
939 
RunSet()940 void WearingStateCommand::RunSet()
941 {
942     SharedData<bool>::SetData(SharedDataType::WEARING_STATE, args["WearingState"].asString() == "true");
943     Json::Value result = true;
944     SetCommandResult("result", result);
945     ILOG("Set wearingState run finished, the value is: %s", args["WearingState"].asString().c_str());
946 }
947 
IsSetArgValid() const948 bool WearingStateCommand::IsSetArgValid() const
949 {
950     if (args.isNull() || !args.isMember("WearingState")) {
951         ELOG("Invalid number of arguments!");
952         return false;
953     }
954     if (!IsBoolType(args["WearingState"].asString())) {
955         ILOG("arg 0: %s", args["WearingState"].asString().c_str());
956         return false;
957     }
958     return true;
959 }
960 
BrightnessModeCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)961 BrightnessModeCommand::BrightnessModeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
962     : CommandLine(commandType, arg, socket)
963 {
964 }
965 
RunGet()966 void BrightnessModeCommand::RunGet()
967 {
968     Json::Value result;
969     result["BrightnessMode"] = SharedData<uint8_t>::GetData(SharedDataType::BRIGHTNESS_MODE);
970     SetCommandResult("result", result);
971     ILOG("Get brightnessMode run finished");
972 }
973 
RunSet()974 void BrightnessModeCommand::RunSet()
975 {
976     SharedData<uint8_t>::SetData(SharedDataType::BRIGHTNESS_MODE,
977                                  static_cast<uint8_t>(atoi(args["BrightnessMode"].asString().data())));
978     Json::Value result = true;
979     SetCommandResult("result", result);
980     ILOG("Set brightnessMode run finished, the value is: %s", args["BrightnessMode"].asString().c_str());
981 }
982 
IsSetArgValid() const983 bool BrightnessModeCommand::IsSetArgValid() const
984 {
985     if (args.isNull() || !args.isMember("BrightnessMode")) {
986         ELOG("Invalid number of arguments!");
987         return false;
988     }
989     if (!std::regex_match(args["BrightnessMode"].asString(), std::regex("^\\d$"))) {
990         ELOG("regex match error");
991         return false;
992     }
993     uint8_t temp = ToUint8(args["BrightnessMode"].asString());
994     if (!SharedData<uint8_t>::IsValid(SharedDataType::BRIGHTNESS_MODE, temp)) {
995         ELOG("BrightnessModeCommand invalid value: %d", temp);
996         return false;
997     }
998     return true;
999 }
1000 
ChargeModeCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1001 ChargeModeCommand::ChargeModeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1002     : CommandLine(commandType, arg, socket)
1003 {
1004 }
1005 
RunGet()1006 void ChargeModeCommand::RunGet()
1007 {
1008     Json::Value result;
1009     result["ChargeMode"] = SharedData<uint8_t>::GetData(SharedDataType::BATTERY_STATUS);
1010     SetCommandResult("result", result);
1011     ILOG("Get chargeMode run finished");
1012 }
1013 
RunSet()1014 void ChargeModeCommand::RunSet()
1015 {
1016     SharedData<uint8_t>::SetData(SharedDataType::BATTERY_STATUS,
1017                                  static_cast<uint8_t>(atoi(args["ChargeMode"].asString().data())));
1018     Json::Value result = true;
1019     SetCommandResult("result", result);
1020     ILOG("Set chargeMode run finished, the value is: %s", args["ChargeMode"].asString().c_str());
1021 }
1022 
IsSetArgValid() const1023 bool ChargeModeCommand::IsSetArgValid() const
1024 {
1025     if (args.isNull() || !args.isMember("ChargeMode")) {
1026         ELOG("Invalid number of arguments!");
1027         return false;
1028     }
1029     if (!std::regex_match(args["ChargeMode"].asString().data(), std::regex("\\d"))) {
1030         ELOG("Invalid arguments!");
1031         return false;
1032     }
1033     uint8_t temp = ToUint8(args["ChargeMode"].asString());
1034     if (!SharedData<uint8_t>::IsValid(SharedDataType::BATTERY_STATUS, temp)) {
1035         ELOG("ChargeModeCommand invalid value: %d", temp);
1036         return false;
1037     }
1038     return true;
1039 }
1040 
BrightnessCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1041 BrightnessCommand::BrightnessCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1042     : CommandLine(commandType, arg, socket)
1043 {
1044 }
1045 
RunGet()1046 void BrightnessCommand::RunGet()
1047 {
1048     Json::Value result;
1049     result["Brightness"] = SharedData<uint8_t>::GetData(SharedDataType::BRIGHTNESS_VALUE);
1050     SetCommandResult("result", result);
1051     ILOG("Get brightness run finished");
1052 }
1053 
RunSet()1054 void BrightnessCommand::RunSet()
1055 {
1056     SharedData<uint8_t>::SetData(SharedDataType::BRIGHTNESS_VALUE,
1057                                  static_cast<uint8_t>(atoi(args["Brightness"].asString().data())));
1058     Json::Value result = true;
1059     SetCommandResult("result", result);
1060     ILOG("Set brightness run finished, the value is: %s", args["Brightness"].asString().c_str());
1061 }
1062 
IsSetArgValid() const1063 bool BrightnessCommand::IsSetArgValid() const
1064 {
1065     if (args.isNull() || !args.isMember("Brightness")) {
1066         ELOG("Invalid number of arguments!");
1067         return false;
1068     }
1069     if (!std::regex_match(args["Brightness"].asString().data(), std::regex("\\d+"))) {
1070         ELOG("Invalid arguments!");
1071         return false;
1072     }
1073     uint8_t temp = ToUint8(args["Brightness"].asString());
1074     if (!SharedData<uint8_t>::IsValid(SharedDataType::BRIGHTNESS_VALUE, temp)) {
1075         ELOG("BrightnessCommand invalid value: ", temp);
1076         return false;
1077     }
1078     return true;
1079 }
1080 
HeartRateCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1081 HeartRateCommand::HeartRateCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1082     : CommandLine(commandType, arg, socket)
1083 {
1084 }
1085 
RunGet()1086 void HeartRateCommand::RunGet()
1087 {
1088     Json::Value result;
1089     result["HeartRate"] = SharedData<uint8_t>::GetData(SharedDataType::HEARTBEAT_VALUE);
1090     SetCommandResult("result", result);
1091     ILOG("Get heartRate run finished");
1092 }
1093 
RunSet()1094 void HeartRateCommand::RunSet()
1095 {
1096     SharedData<uint8_t>::SetData(SharedDataType::HEARTBEAT_VALUE,
1097                                  static_cast<uint8_t>(atoi(args["HeartRate"].asString().data())));
1098     Json::Value result = true;
1099     SetCommandResult("result", result);
1100     ILOG("Set heartRate run finished, the value is: %s", args["HeartRate"].asString().c_str());
1101 }
1102 
IsSetArgValid() const1103 bool HeartRateCommand::IsSetArgValid() const
1104 {
1105     if (args.isNull() || !args.isMember("HeartRate")) {
1106         ELOG("Invalid number of arguments!");
1107         return false;
1108     }
1109     if (!std::regex_match(args["HeartRate"].asString().data(), std::regex("\\d+"))) {
1110         ELOG("Invalid arguments!");
1111         return false;
1112     }
1113     if (atoi(args["HeartRate"].asString().data()) > UINT8_MAX) {
1114         ELOG("Invalid arguments!");
1115         return false;
1116     }
1117     uint8_t temp = ToUint8(args["HeartRate"].asString());
1118     if (!SharedData<uint8_t>::IsValid(SharedDataType::HEARTBEAT_VALUE, temp)) {
1119         ELOG("HeartRateCommand invalid value: %d", temp);
1120         return false;
1121     }
1122     return true;
1123 }
1124 
StepCountCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1125 StepCountCommand::StepCountCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1126     : CommandLine(commandType, arg, socket)
1127 {
1128 }
1129 
RunGet()1130 void StepCountCommand::RunGet()
1131 {
1132     Json::Value result;
1133     result["StepCount"] = SharedData<uint32_t>::GetData(SharedDataType::SUMSTEP_VALUE);
1134     SetCommandResult("result", result);
1135     ILOG("Get stepCount run finished");
1136 }
1137 
RunSet()1138 void StepCountCommand::RunSet()
1139 {
1140     SharedData<uint32_t>::SetData(SharedDataType::SUMSTEP_VALUE,
1141                                   static_cast<uint32_t>(atoi(args["StepCount"].asString().data())));
1142     Json::Value result = true;
1143     SetCommandResult("result", result);
1144     ILOG("Set stepCount run finished, the value is: %s", args["StepCount"].asString().c_str());
1145 }
1146 
IsSetArgValid() const1147 bool StepCountCommand::IsSetArgValid() const
1148 {
1149     if (args.isNull() || !args.isMember("StepCount")) {
1150         ELOG("Invalid number of arguments!");
1151         return false;
1152     }
1153     if (!std::regex_match(args["StepCount"].asString().data(), std::regex("\\d+"))) {
1154         ELOG("Invalid arguments!");
1155         return false;
1156     }
1157 
1158     uint32_t temp = ToUint8(args["StepCount"].asString());
1159     if (!SharedData<uint32_t>::IsValid(SharedDataType::SUMSTEP_VALUE, temp)) {
1160         ELOG("StepCountCommand invalid value: %d", temp);
1161         return false;
1162     }
1163     return true;
1164 }
1165 
InspectorJSONTree(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1166 InspectorJSONTree::InspectorJSONTree(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1167     : CommandLine(commandType, arg, socket)
1168 {
1169 }
1170 
RunAction()1171 void InspectorJSONTree::RunAction()
1172 {
1173     ILOG("GetJsonTree run!");
1174     std::string str = JsAppImpl::GetInstance().GetJSONTree();
1175     if (str == "null") {
1176         str = "{\"children\":\"empty json tree\"}";
1177     }
1178     SetCommandResult("result", str);
1179     ILOG("SendJsonTree end!");
1180 }
1181 
InspectorDefault(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1182 InspectorDefault::InspectorDefault(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1183     : CommandLine(commandType, arg, socket)
1184 {
1185 }
1186 
RunAction()1187 void InspectorDefault::RunAction()
1188 {
1189     ILOG("GetDefaultJsonTree run!");
1190     std::string str = JsAppImpl::GetInstance().GetDefaultJSONTree();
1191     SetCommandResult("result", str);
1192     ILOG("SendDefaultJsonTree end!");
1193 }
1194 
ExitCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1195 ExitCommand::ExitCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1196     : CommandLine(commandType, arg, socket)
1197 {
1198 }
1199 
RunAction()1200 void ExitCommand::RunAction()
1201 {
1202     ILOG("ExitCommand run.");
1203     Json::Value res = true;
1204     SetCommandResult("result", res);
1205     SendResult();
1206     Interrupter::Interrupt();
1207     ILOG("Ready to exit");
1208 }
1209 
DeviceTypeCommand(CommandLine::CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1210 DeviceTypeCommand::DeviceTypeCommand(CommandLine::CommandType commandType,
1211                                      const Json::Value& arg,
1212                                      const LocalSocket& socket)
1213     : CommandLine(commandType, arg, socket)
1214 {
1215 }
1216 
RunSet()1217 void DeviceTypeCommand::RunSet() {}
1218 
ResolutionCommand(CommandLine::CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1219 ResolutionCommand::ResolutionCommand(CommandLine::CommandType commandType,
1220                                      const Json::Value& arg,
1221                                      const LocalSocket& socket)
1222     : CommandLine(commandType, arg, socket)
1223 {
1224 }
1225 
RunSet()1226 void ResolutionCommand::RunSet() {}
1227 
BackClickedCommand(CommandLine::CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1228 BackClickedCommand::BackClickedCommand(CommandLine::CommandType commandType,
1229                                        const Json::Value& arg,
1230                                        const LocalSocket& socket)
1231     : CommandLine(commandType, arg, socket)
1232 {
1233 }
1234 
RunAction()1235 void BackClickedCommand::RunAction()
1236 {
1237     MouseInputImpl::GetInstance().DispatchOsBackEvent();
1238     ILOG("BackClickCommand run");
1239     Json::Value res = true;
1240     SetCommandResult("result", res);
1241     ILOG("BackClickCommand end");
1242 }
1243 
RestartCommand(CommandLine::CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1244 RestartCommand::RestartCommand(CommandLine::CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1245     : CommandLine(commandType, arg, socket)
1246 {
1247 }
1248 
RunAction()1249 void RestartCommand::RunAction()
1250 {
1251     ILOG("RestartCommand start");
1252     JsAppImpl::GetInstance().Restart();
1253     Json::Value res = true;
1254     SetCommandResult("result", res);
1255     ILOG("RestartCommand end");
1256 }
1257 
FastPreviewMsgCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1258 FastPreviewMsgCommand::FastPreviewMsgCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1259     : CommandLine(commandType, arg, socket)
1260 {
1261 }
1262 
RunGet()1263 void FastPreviewMsgCommand::RunGet()
1264 {
1265     Json::Value resultContent;
1266     std::string fastPreviewMsg = VirtualScreenImpl::GetInstance().GetFastPreviewMsg();
1267     resultContent["FastPreviewMsg"] = fastPreviewMsg;
1268     SetResultToManager("args", resultContent, "MemoryRefresh");
1269     ILOG("Get FastPreviewMsgCommand run finished.");
1270 }
1271 
DropFrameCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1272 DropFrameCommand::DropFrameCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1273     : CommandLine(commandType, arg, socket)
1274 {
1275 }
1276 
IsSetArgValid() const1277 bool DropFrameCommand::IsSetArgValid() const
1278 {
1279     if (args.isNull() || !args.isMember("frequency") || !args["frequency"].isInt()) {
1280         ELOG("Invalid DropFrame of arguments!");
1281         return false;
1282     }
1283     if (args["frequency"].asInt() < 0) {
1284         ELOG("DropFrame param frequency must greater than or equal to 0");
1285         return false;
1286     }
1287     return true;
1288 }
1289 
RunSet()1290 void DropFrameCommand::RunSet()
1291 {
1292     ILOG("Set DropFrame frequency start.");
1293     int frequency = args["frequency"].asInt();
1294     VirtualScreenImpl::GetInstance().SetDropFrameFrequency(frequency);
1295     SetCommandResult("result", true);
1296     ILOG("Set DropFrame frequency: %sms.", frequency);
1297 }
1298 
IsActionArgValid() const1299 bool KeyPressCommand::IsActionArgValid() const
1300 {
1301     if (args.isNull() || !args.isMember("isInputMethod") || !args["isInputMethod"].isBool()) {
1302         ELOG("Param isInputMethod's value is invalid.");
1303         return false;
1304     }
1305     bool isInputMethod = args["isInputMethod"].asBool();
1306     if (isInputMethod) {
1307         return IsImeArgsValid();
1308     } else {
1309         return IsKeyArgsValid();
1310     }
1311 }
1312 
IsImeArgsValid() const1313 bool KeyPressCommand::IsImeArgsValid() const
1314 {
1315     if (!args.isMember("codePoint") || !args["codePoint"].isInt()) {
1316         ELOG("Param codePoint's value is invalid.");
1317         return false;
1318     }
1319     return true;
1320 }
1321 
IsKeyArgsValid() const1322 bool KeyPressCommand::IsKeyArgsValid() const
1323 {
1324     if (!args.isMember("keyCode") || !args["keyCode"].isInt() || !args["keyAction"].isInt() ||
1325         !args.isMember("keyAction") || !args["keyAction"].isInt() ||
1326         !args.isMember("pressedCodes") || !args["pressedCodes"].isArray() ||
1327         args["pressedCodes"].size() < 1 || (args.isMember("keyString") && !args["keyString"].isString())) {
1328         ELOG("Param keyEvent's value is invalid.");
1329         return false;
1330     }
1331     if (!args.isMember("keyString")) {
1332         ILOG("Param keyString is lost, it will be empty string.");
1333     }
1334     if (args["keyAction"].asInt() < minActionVal || args["keyAction"].asInt() > maxActionVal) {
1335         ELOG("Param keyAction's value is invalid,value range %d-%d.", minActionVal, maxActionVal);
1336         return false;
1337     }
1338     int keyCode = args["keyCode"].asInt();
1339     if (keyCode > maxKeyVal || keyCode < minKeyVal) {
1340         ELOG("Param pressedCode value is invalid,value range %d-%d.", minKeyVal, maxKeyVal);
1341         return false;
1342     }
1343     Json::Value arrayNum = args["pressedCodes"];
1344     for (unsigned int i = 0; i < arrayNum.size(); i++) {
1345         if (!arrayNum[i].isInt()) {
1346             ELOG("Param pressedCodes's value is invalid.");
1347             return false;
1348         }
1349         int pressedCode = arrayNum[i].asInt();
1350         if (pressedCode > maxKeyVal || pressedCode < minKeyVal) {
1351             ELOG("Param pressedCode value is invalid,value range %d-%d.", minKeyVal, maxKeyVal);
1352             return false;
1353         }
1354     }
1355     return true;
1356 }
1357 
KeyPressCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1358 KeyPressCommand::KeyPressCommand(CommandType commandType, const Json::Value& arg,
1359                                  const LocalSocket& socket)
1360     : CommandLine(commandType, arg, socket)
1361 {
1362 }
1363 
RunAction()1364 void KeyPressCommand::RunAction()
1365 {
1366     if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) {
1367         return;
1368     }
1369     bool isInputMethod = args["isInputMethod"].asBool();
1370     if (isInputMethod) {
1371         VirtualScreen::inputMethodCountPerMinute++;
1372         unsigned int codePoint = args["codePoint"].asInt();
1373         KeyInputImpl::GetInstance().SetCodePoint(codePoint);
1374         KeyInputImpl::GetInstance().DispatchOsInputMethodEvent();
1375     } else {
1376         VirtualScreen::inputKeyCountPerMinute++;
1377         int32_t keyCode = args["keyCode"].asInt();
1378         int32_t keyAction = args["keyAction"].asInt();
1379         Json::Value pressedCodes = args["pressedCodes"];
1380         vector<int32_t> pressedCodesVec;
1381         for (unsigned int i = 0; i < pressedCodes.size(); i++) {
1382             pressedCodesVec.push_back(pressedCodes[i].asInt());
1383         }
1384         string keyString = "";
1385         if (args.isMember("keyString") && args["keyString"].isString()) {
1386             keyString = args["keyString"].asString();
1387         }
1388         KeyInputImpl::GetInstance().SetKeyEvent(keyCode, keyAction, pressedCodesVec, keyString);
1389         KeyInputImpl::GetInstance().DispatchOsKeyEvent();
1390     }
1391     SetCommandResult("result", true);
1392     ILOG("KeyPressCommand run finished.");
1393 }
1394 
IsActionArgValid() const1395 bool PointEventCommand::IsActionArgValid() const
1396 {
1397     return IsArgsExist() && IsArgsValid();
1398 }
1399 
IsArgsExist() const1400 bool PointEventCommand::IsArgsExist() const
1401 {
1402     if (args.isNull() || !args.isMember("x") || !args.isMember("y") ||
1403         !args["x"].isInt() || !args["y"].isInt()) {
1404         return false;
1405     }
1406     if (!args.isMember("button") || !args.isMember("action") ||
1407         !args["button"].isInt() || !args["action"].isInt()) {
1408         return false;
1409     }
1410     if (!args.isMember("sourceType") || !args.isMember("sourceTool") ||
1411         !args["sourceType"].isInt() || !args["sourceTool"].isInt()) {
1412         return false;
1413     }
1414     if (!args.isMember("axisValues") || !args["axisValues"].isArray()) {
1415         return false;
1416     }
1417     return true;
1418 }
1419 
IsArgsValid() const1420 bool PointEventCommand::IsArgsValid() const
1421 {
1422     int32_t pointX = args["x"].asInt();
1423     int32_t pointY = args["y"].asInt();
1424     int32_t button = args["button"].asInt();
1425     int32_t action = args["action"].asInt();
1426     int32_t sourceType = args["sourceType"].asInt();
1427     int32_t sourcceTool = args["sourcceTool"].asInt();
1428     if (pointX < 0 || pointX > VirtualScreenImpl::GetInstance().GetCurrentWidth()) {
1429         ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentWidth());
1430         return false;
1431     }
1432     if (pointY < 0 || pointY > VirtualScreenImpl::GetInstance().GetCurrentHeight()) {
1433         ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentHeight());
1434         return false;
1435     }
1436     if (button < -1 || action < 0 || sourceType < 0 || sourcceTool < 0) {
1437         ELOG("action,sourceType,sourcceTool must >= 0, button must >= -1");
1438         return false;
1439     }
1440     Json::Value axisArrayNum = args["axisValues"];
1441     for (unsigned int i = 0; i < axisArrayNum.size(); i++) {
1442         if (!axisArrayNum[i].isDouble()) {
1443             ELOG("Param axisValues's value is invalid.");
1444             return false;
1445         }
1446     }
1447     return true;
1448 }
1449 
PointEventCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1450 PointEventCommand::PointEventCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1451     : CommandLine(commandType, arg, socket)
1452 {
1453 }
1454 
RunAction()1455 void PointEventCommand::RunAction()
1456 {
1457     int type = 9;
1458     EventParams param;
1459     if (args.isMember("pressedButtons") && args["pressedButtons"].isArray()) {
1460         Json::Value pressedCodes = args["pressedCodes"];
1461         for (unsigned int i = 0; i < pressedCodes.size(); i++) {
1462             if (!pressedCodes[i].isInt() || pressedCodes[i].asInt() < -1) {
1463                 continue;
1464             }
1465             param.pressedBtnsVec.insert(pressedCodes[i].asInt());
1466         }
1467     }
1468     vector<double> axisVec; // 13 is array size
1469     Json::Value axisCodes = args["axisValues"];
1470     for (unsigned int i = 0; i < axisCodes.size(); i++) {
1471         param.axisVec.push_back(axisCodes[i].asDouble());
1472     }
1473     param.x = atof(args["x"].asString().data());
1474     param.y = atof(args["y"].asString().data());
1475     param.type = type;
1476     param.button = args["button"].asInt();
1477     param.action = args["action"].asInt();
1478     param.sourceType = args["sourceType"].asInt();
1479     param.sourceTool = args["sourceTool"].asInt();
1480     param.name = "PointEvent";
1481     SetEventParams(param);
1482     SetCommandResult("result", true);
1483 }
1484 
FoldStatusCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1485 FoldStatusCommand::FoldStatusCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1486     : CommandLine(commandType, arg, socket)
1487 {
1488 }
1489 
IsSetArgValid() const1490 bool FoldStatusCommand::IsSetArgValid() const
1491 {
1492     if (args.isNull() || !args.isMember("FoldStatus") || !args["FoldStatus"].isString()) {
1493         ELOG("Invalid FoldStatus of arguments!");
1494         return false;
1495     }
1496     if (!args.isMember("width") || !args["width"].asInt() ||
1497         !args.isMember("height") || !args["height"].asInt()) {
1498         ELOG("Invalid width and height of arguments!");
1499         return false;
1500     }
1501     if (args["width"].asInt() < minWidth || args["width"].asInt() > maxWidth ||
1502         args["height"].asInt() < minWidth || args["height"].asInt() > maxWidth) {
1503         ELOG("width or height is out of range %d-%d", minWidth, maxWidth);
1504         return false;
1505     }
1506     if (args["FoldStatus"].asString() == "fold" || args["FoldStatus"].asString() == "unfold" ||
1507         args["FoldStatus"].asString() == "unknown" || args["FoldStatus"].asString() == "half_fold") {
1508         return true;
1509     }
1510     ELOG("FoldStatus param must be \"fold\" or \"unfold\" or \"unknown\" or \"half_fold\"");
1511     return false;
1512 }
1513 
RunSet()1514 void FoldStatusCommand::RunSet()
1515 {
1516     std::string commandStatus = args["FoldStatus"].asString();
1517     int32_t width = args["width"].asInt();
1518     int32_t height = args["height"].asInt();
1519     std::string currentStatus = VirtualScreenImpl::GetInstance().GetFoldStatus();
1520     if (commandStatus != currentStatus) {
1521         JsAppImpl::GetInstance().FoldStatusChanged(commandStatus, width, height);
1522     }
1523     SetCommandResult("result", true);
1524     ILOG("Set FoldStatus run finished, FoldStatus is: %s", args["FoldStatus"].asString().c_str());
1525 }