• 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().GetOrignalWidth()) {
202         ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalWidth());
203         return false;
204     }
205     if (pointY < 0 || pointY > VirtualScreenImpl::GetInstance().GetOrignalHeight()) {
206         ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalHeight());
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().GetOrignalWidth()) {
286         ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalWidth());
287         return false;
288     }
289     if (pY < 0 || pY > VirtualScreenImpl::GetInstance().GetOrignalHeight()) {
290         ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalHeight());
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     return true;
442 }
443 
IsIntValValid(const Json::Value & args) const444 bool ResolutionSwitchCommand::IsIntValValid(const Json::Value& args) const
445 {
446     if (args["originWidth"].asInt() < minWidth || args["originWidth"].asInt() > maxWidth ||
447         args["originHeight"].asInt() < minWidth || args["originHeight"].asInt() > maxWidth ||
448         args["width"].asInt() < minWidth || args["width"].asInt() > maxWidth ||
449         args["height"].asInt() < minWidth || args["height"].asInt() > maxWidth) {
450         ELOG("width or height is out of range %d-%d", minWidth, maxWidth);
451         return false;
452     }
453     if (args["screenDensity"].asInt() < minDpi || args["screenDensity"].asInt() > maxDpi) {
454         ELOG("screenDensity is out of range %d-%d", minDpi, maxDpi);
455         return false;
456     }
457     return true;
458 }
459 
RunSet()460 void ResolutionSwitchCommand::RunSet()
461 {
462     int32_t originWidth = args["originWidth"].asInt();
463     int32_t originHeight = args["originHeight"].asInt();
464     int32_t width = args["width"].asInt();
465     int32_t height = args["height"].asInt();
466     int32_t screenDensity = args["screenDensity"].asInt();
467     JsAppImpl::GetInstance().ResolutionChanged(originWidth, originHeight, width, height, screenDensity);
468     SetCommandResult("result", true);
469     ILOG("ResolutionSwitch run finished.");
470 }
471 
OrientationCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)472 OrientationCommand::OrientationCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
473     : CommandLine(commandType, arg, socket)
474 {
475 }
476 
IsSetArgValid() const477 bool OrientationCommand::IsSetArgValid() const
478 {
479     if (args.isNull() || !args.isMember("Orientation") || !args["Orientation"].isString()) {
480         ELOG("Invalid number of arguments!");
481         return false;
482     }
483     if (args["Orientation"].asString() != "portrait" && args["Orientation"].asString() != "landscape") {
484         ELOG("Orientation just support [portrait,landscape].");
485         return false;
486     }
487     return true;
488 }
489 
RunSet()490 void OrientationCommand::RunSet()
491 {
492     std::string commandOrientation = args["Orientation"].asString();
493     std::string curruntOrientation = JsAppImpl::GetInstance().GetOrientation();
494     if (commandOrientation != curruntOrientation) {
495         JsAppImpl::GetInstance().OrientationChanged(commandOrientation);
496     }
497     SetCommandResult("result", true);
498     ILOG("Set Orientation run finished, Orientation is: %s", args["Orientation"].asString().c_str());
499 }
500 
ColorModeCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)501 ColorModeCommand::ColorModeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
502     : CommandLine(commandType, arg, socket)
503 {
504 }
505 
IsSetArgValid() const506 bool ColorModeCommand::IsSetArgValid() const
507 {
508     if (args.isNull() || !args.isMember("ColorMode") || !args["ColorMode"].isString()) {
509         ELOG("Invalid number of arguments!");
510         return false;
511     }
512 
513     if (args["ColorMode"].asString() != "light" && args["ColorMode"].asString() != "dark") {
514         ELOG("ColorMode just support [light,dark]");
515         return false;
516     }
517     return true;
518 }
519 
RunSet()520 void ColorModeCommand::RunSet()
521 {
522     std::string commandColorMode = args["ColorMode"].asString();
523     std::string currentColorMode = JsAppImpl::GetInstance().GetColorMode();
524     if (commandColorMode != currentColorMode) {
525         JsAppImpl::GetInstance().SetArgsColorMode(args["ColorMode"].asString());
526         JsAppImpl::GetInstance().ColorModeChanged(commandColorMode);
527     }
528     SetCommandResult("result", true);
529     ILOG("Set ColorMode run finished, ColorMode is: %s", args["ColorMode"].asString().c_str());
530 }
531 
FontSelectCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)532 FontSelectCommand::FontSelectCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
533     : CommandLine(commandType, arg, socket)
534 {
535 }
536 
IsSetArgValid() const537 bool FontSelectCommand::IsSetArgValid() const
538 {
539     if (args.isNull() || !args.isMember("FontSelect") || !args["FontSelect"].isBool()) {
540         ELOG("Invalid number of arguments!");
541         return false;
542     }
543     return true;
544 }
545 
RunSet()546 void FontSelectCommand::RunSet()
547 {
548     SetCommandResult("result", true);
549     ILOG("FontSelect finished, currentSelect is: %d", args["FontSelect"].asBool());
550 }
551 
MemoryRefreshCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)552 MemoryRefreshCommand::MemoryRefreshCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
553     : CommandLine(commandType, arg, socket)
554 {
555 }
556 
IsSetArgValid() const557 bool MemoryRefreshCommand::IsSetArgValid() const
558 {
559     if (args.isNull()) {
560         ELOG("Invalid MemoryRefresh of arguments!");
561         return false;
562     }
563     return true;
564 }
565 
RunSet()566 void MemoryRefreshCommand::RunSet()
567 {
568     ILOG("MemoryRefreshCommand begin.");
569     bool ret = JsAppImpl::GetInstance().MemoryRefresh(args.toStyledString());
570     SetCommandResult("result", ret);
571     ILOG("MemoryRefresh finished.");
572 }
573 
LoadDocumentCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)574 LoadDocumentCommand::LoadDocumentCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
575     : CommandLine(commandType, arg, socket)
576 {
577 }
578 
IsSetArgValid() const579 bool LoadDocumentCommand::IsSetArgValid() const
580 {
581     if (args.isNull() || !args.isMember("url") || !args.isMember("className") || !args.isMember("previewParam") ||
582         !args["url"].isString() || !args["className"].isString() || !args["previewParam"].isObject()) {
583         return false;
584     }
585     Json::Value previewParam = args["previewParam"];
586     if (!previewParam["width"].isInt() || !previewParam["height"].isInt() || !previewParam["dpi"].isInt() ||
587         !previewParam["locale"].isString() || !previewParam["colorMode"].isString() ||
588         !previewParam["orientation"].isString() || !previewParam["deviceType"].isString()) {
589         return false;
590     }
591     if (!IsIntValValid(previewParam) || !IsStrValVailid(previewParam)) {
592         return false;
593     }
594     return true;
595 }
596 
IsIntValValid(const Json::Value & previewParam) const597 bool LoadDocumentCommand::IsIntValValid(const Json::Value& previewParam) const
598 {
599     int width = previewParam["width"].asInt();
600     int height = previewParam["height"].asInt();
601     int dpi = previewParam["dpi"].asInt();
602     if (width < minLoadDocWidth || width > maxLoadDocWidth || height < minLoadDocWidth ||
603         height > maxLoadDocWidth || dpi < minDpi || dpi > maxDpi) {
604         return false;
605     }
606     return true;
607 }
608 
IsStrValVailid(const Json::Value & previewParam) const609 bool LoadDocumentCommand::IsStrValVailid(const Json::Value& previewParam) const
610 {
611     string locale = previewParam["locale"].asString();
612     bool isLiteDevice = JsApp::IsLiteDevice(CommandParser::GetInstance().GetDeviceType());
613     if (isLiteDevice) {
614         if (std::find(liteSupportedLanguages.begin(), liteSupportedLanguages.end(), locale) ==
615             liteSupportedLanguages.end()) {
616             return false;
617         }
618     } else {
619         if (std::find(richSupportedLanguages.begin(), richSupportedLanguages.end(), locale) ==
620             richSupportedLanguages.end()) {
621             return false;
622         }
623     }
624     if (previewParam["colorMode"].asString() != "light" && previewParam["colorMode"].asString() != "dark") {
625         return false;
626     }
627     if (previewParam["orientation"].asString() != "portrait" &&
628         previewParam["orientation"].asString() != "landscape") {
629         return false;
630     }
631     if (std::find(LoadDocDevs.begin(), LoadDocDevs.end(), previewParam["deviceType"].asString()) ==
632         LoadDocDevs.end()) {
633         return false;
634     }
635     return true;
636 }
637 
RunSet()638 void LoadDocumentCommand::RunSet()
639 {
640     VirtualScreenImpl::GetInstance().SetLoadDocFlag(VirtualScreen::LoadDocType::START);
641     ILOG("LoadDocumentCommand begin.");
642     std::string pageUrl = args["url"].asString();
643     std::string className = args["className"].asString();
644     Json::Value previewParam = Json::nullValue;
645     if (args.isMember("previewParam")) {
646         previewParam = args["previewParam"];
647     }
648     JsAppImpl::GetInstance().LoadDocument(pageUrl, className, previewParam);
649     VirtualScreenImpl::GetInstance().SetLoadDocFlag(VirtualScreen::LoadDocType::FINISHED);
650     SetCommandResult("result", true);
651     ILOG("LoadDocumentCommand finished.");
652 }
653 
ReloadRuntimePageCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)654 ReloadRuntimePageCommand::ReloadRuntimePageCommand(CommandType commandType,
655                                                    const Json::Value& arg,
656                                                    const LocalSocket& socket)
657     : CommandLine(commandType, arg, socket)
658 {
659 }
660 
IsSetArgValid() const661 bool ReloadRuntimePageCommand::IsSetArgValid() const
662 {
663     if (args.isNull() || !args.isMember("ReloadRuntimePage") || !args["ReloadRuntimePage"].isString()) {
664         ELOG("Invalid number of arguments!");
665         return false;
666     }
667     return true;
668 }
669 
RunSet()670 void ReloadRuntimePageCommand::RunSet()
671 {
672     std::string currentPage = args["ReloadRuntimePage"].asString();
673     JsAppImpl::GetInstance().ReloadRuntimePage(currentPage);
674     SetCommandResult("result", true);
675     ILOG("ReloadRuntimePage finished, currentPage is: %s", args["ReloadRuntimePage"].asString().c_str());
676 }
677 
CurrentRouterCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)678 CurrentRouterCommand::CurrentRouterCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
679     : CommandLine(commandType, arg, socket)
680 {
681 }
682 
RunGet()683 void CurrentRouterCommand::RunGet()
684 {
685     Json::Value resultContent;
686     std::string currentRouter = VirtualScreenImpl::GetInstance().GetCurrentRouter();
687     resultContent["CurrentRouter"] = currentRouter;
688     SetResultToManager("args", resultContent, "CurrentJsRouter");
689     ILOG("Get CurrentRouter run finished.");
690 }
691 
LoadContentCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)692 LoadContentCommand::LoadContentCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
693     : CommandLine(commandType, arg, socket)
694 {
695 }
696 
RunGet()697 void LoadContentCommand::RunGet()
698 {
699     Json::Value resultContent;
700     std::string currentRouter = VirtualScreenImpl::GetInstance().GetAbilityCurrentRouter();
701     resultContent["AbilityCurrentRouter"] = currentRouter;
702     SetResultToManager("args", resultContent, "AbilityCurrentJsRouter");
703     ILOG("Get AbilityCurrentRouter run finished.");
704 }
705 
LanguageCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)706 LanguageCommand::LanguageCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
707     : CommandLine(commandType, arg, socket)
708 {
709 }
710 
IsSetArgValid() const711 bool LanguageCommand::IsSetArgValid() const
712 {
713     if (args.isNull() || !args.isMember("Language") || !args["Language"].isString()) {
714         ELOG("Invalid number of arguments!");
715         return false;
716     }
717 
718     CommandParser& cmdParser = CommandParser::GetInstance();
719     string deviceType = cmdParser.GetDeviceType();
720     bool isLiteDevice = JsApp::IsLiteDevice(deviceType);
721     if (isLiteDevice) {
722         if (std::find(liteSupportedLanguages.begin(), liteSupportedLanguages.end(), args["Language"].asString()) ==
723             liteSupportedLanguages.end()) {
724             ELOG("Language not support liteDevice : %s", args["Language"].asString().c_str());
725             return false;
726         }
727     } else {
728         if (std::find(richSupportedLanguages.begin(), richSupportedLanguages.end(), args["Language"].asString()) ==
729             richSupportedLanguages.end()) {
730             ELOG("Language not support richDevice : %s", args["Language"].asString().c_str());
731             return false;
732         }
733     }
734     return true;
735 }
736 
RunGet()737 void LanguageCommand::RunGet()
738 {
739     std::string language = SharedData<string>::GetData(SharedDataType::LANGUAGE);
740     Json::Value resultContent;
741     resultContent["Language"] = language;
742     SetCommandResult("result", resultContent);
743     ILOG("Get language run finished.");
744 }
745 
RunSet()746 void LanguageCommand::RunSet()
747 {
748     string language(args["Language"].asString());
749     SharedData<string>::SetData(SharedDataType::LANGUAGE, language);
750     SetCommandResult("result", true);
751     ILOG("Set language run finished, language is: %s", language.c_str());
752 }
753 
SupportedLanguagesCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)754 SupportedLanguagesCommand::SupportedLanguagesCommand(CommandType commandType,
755                                                      const Json::Value& arg,
756                                                      const LocalSocket& socket)
757     : CommandLine(commandType, arg, socket)
758 {
759 }
760 
RunGet()761 void SupportedLanguagesCommand::RunGet()
762 {
763     Json::Value resultContent;
764     Json::Value languageList;
765     string deviceType = CommandParser::GetInstance().GetDeviceType();
766     bool isLiteDevice = JsApp::IsLiteDevice(deviceType);
767     if (!deviceType.empty() && !isLiteDevice) {
768         for (auto iter = richSupportedLanguages.begin(); iter != richSupportedLanguages.end(); iter++) {
769             languageList.append(*iter);
770         }
771     } else {
772         for (auto iter = liteSupportedLanguages.begin(); iter != liteSupportedLanguages.end(); iter++) {
773             languageList.append(*iter);
774         }
775     }
776     resultContent["SupportedLanguages"] = languageList;
777     SetCommandResult("result", resultContent);
778     ILOG("Get supportedLanguages run finished.");
779 }
780 
LocationCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)781 LocationCommand::LocationCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
782     : CommandLine(commandType, arg, socket)
783 {
784 }
785 
IsSetArgValid() const786 bool LocationCommand::IsSetArgValid() const
787 {
788     if (args.isNull() || !args.isMember("latitude") || !args.isMember("longitude")) {
789         ELOG("Invalid number of arguments!");
790         return false;
791     }
792     string latitude(args["latitude"].asString());
793     string longitude(args["longitude"].asString());
794     regex isDob("^([\\-]*[0-9]{1,}[\\.][0-9]*)$");
795     if (!regex_match(latitude, isDob) || !regex_match(longitude, isDob)) {
796         ELOG("Invalid arguments!");
797         return false;
798     }
799 
800     if (!SharedData<double>::IsValid(SharedDataType::LATITUDE, atof(latitude.data()))) {
801         ELOG("LocationCommand invalid latitude value: %f", atof(latitude.data()));
802         return false;
803     }
804 
805     if (!SharedData<double>::IsValid(SharedDataType::LONGITUDE, atof(longitude.data()))) {
806         ELOG("LocationCommand invalid longitude value: %f", atof(longitude.data()));
807         return false;
808     }
809     return true;
810 }
811 
RunGet()812 void LocationCommand::RunGet()
813 {
814     double longitude = SharedData<double>::GetData(SharedDataType::LONGITUDE);
815     double latitude = SharedData<double>::GetData(SharedDataType::LATITUDE);
816     Json::Value resultContent;
817     resultContent["latitude"] = latitude;
818     resultContent["longitude"] = longitude;
819     SetCommandResult("result", resultContent);
820     ILOG("Get location run finished");
821 }
822 
RunSet()823 void LocationCommand::RunSet()
824 {
825     string latitude(args["latitude"].asString());
826     string longitude(args["longitude"].asString());
827     SharedData<double>::SetData(SharedDataType::LONGITUDE, atof(longitude.data()));
828     SharedData<double>::SetData(SharedDataType::LATITUDE, atof(latitude.data()));
829     Json::Value resultContent = true;
830     SetCommandResult("result", resultContent);
831     ILOG("Set location run finished, latitude: %s,longitude: %s", latitude.c_str(), longitude.c_str());
832 }
833 
DistributedCommunicationsCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)834 DistributedCommunicationsCommand::DistributedCommunicationsCommand(CommandType commandType,
835                                                                    const Json::Value& arg,
836                                                                    const LocalSocket& socket)
837     : CommandLine(commandType, arg, socket)
838 {
839 }
840 
RunAction()841 void DistributedCommunicationsCommand::RunAction()
842 {
843     MessageInfo info;
844     info.deviceID = args["DeviceId"].asString();
845     info.bundleName = args["bundleName"].asString();
846     info.abilityName = args["abilityName"].asString();
847     info.message = args["message"].asString();
848     VirtualMessageImpl::GetInstance().SendVirtualMessage(info);
849     Json::Value resultContent = true;
850     SetCommandResult("result", resultContent);
851     ILOG("Send distributedCommunications run finished");
852 }
853 
IsActionArgValid() const854 bool DistributedCommunicationsCommand::IsActionArgValid() const
855 {
856     if (args.isNull() || !args.isMember("DeviceId") || !args.isMember("bundleName") || !args.isMember("abilityName") ||
857         !args.isMember("message")) {
858         ELOG("Invalid number of arguments!");
859         return false;
860     }
861     if (args["DeviceId"].asString().empty() || args["bundleName"].asString().empty() ||
862         args["abilityName"].asString().empty() || args["message"].asString().empty()) {
863         ELOG("Invalid arguments!");
864         return false;
865     }
866     return true;
867 }
868 
StringToCharVector(string str) const869 std::vector<char> DistributedCommunicationsCommand::StringToCharVector(string str) const
870 {
871     vector<char> vec(str.begin(), str.end());
872     vec.push_back('\0');
873     return vec;
874 }
875 
KeepScreenOnStateCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)876 KeepScreenOnStateCommand::KeepScreenOnStateCommand(CommandType commandType,
877                                                    const Json::Value& arg,
878                                                    const LocalSocket& socket)
879     : CommandLine(commandType, arg, socket)
880 {
881 }
882 
RunGet()883 void KeepScreenOnStateCommand::RunGet()
884 {
885     Json::Value result;
886     result["KeepScreenOnState"] = SharedData<bool>::GetData(SharedDataType::KEEP_SCREEN_ON);
887     SetCommandResult("result", result);
888     ILOG("Get keepScreenOnState run finished");
889 }
890 
RunSet()891 void KeepScreenOnStateCommand::RunSet()
892 {
893     SharedData<bool>::SetData(SharedDataType::KEEP_SCREEN_ON, args["KeepScreenOnState"].asString() == "true");
894     Json::Value result = true;
895     SetCommandResult("result", result);
896     ILOG("Set keepScreenOnState run finished, the value is: %s", args["KeepScreenOnState"].asString().c_str());
897 }
898 
IsSetArgValid() const899 bool KeepScreenOnStateCommand::IsSetArgValid() const
900 {
901     if (args.isNull() || !args.isMember("KeepScreenOnState")) {
902         ELOG("Invalid number of arguments!");
903         return false;
904     }
905     if (!IsBoolType(args["KeepScreenOnState"].asString())) {
906         ELOG("arg 0: %s", args["KeepScreenOnState"].asString().c_str());
907         return false;
908     }
909     return true;
910 }
911 
WearingStateCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)912 WearingStateCommand::WearingStateCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
913     : CommandLine(commandType, arg, socket)
914 {
915 }
916 
RunGet()917 void WearingStateCommand::RunGet()
918 {
919     Json::Value result;
920     result["WearingState"] = SharedData<bool>::GetData(SharedDataType::WEARING_STATE);
921     SetCommandResult("result", result);
922     ILOG("Get wearingState run finished");
923 }
924 
RunSet()925 void WearingStateCommand::RunSet()
926 {
927     SharedData<bool>::SetData(SharedDataType::WEARING_STATE, args["WearingState"].asString() == "true");
928     Json::Value result = true;
929     SetCommandResult("result", result);
930     ILOG("Set wearingState run finished, the value is: %s", args["WearingState"].asString().c_str());
931 }
932 
IsSetArgValid() const933 bool WearingStateCommand::IsSetArgValid() const
934 {
935     if (args.isNull() || !args.isMember("WearingState")) {
936         ELOG("Invalid number of arguments!");
937         return false;
938     }
939     if (!IsBoolType(args["WearingState"].asString())) {
940         ILOG("arg 0: %s", args["WearingState"].asString().c_str());
941         return false;
942     }
943     return true;
944 }
945 
BrightnessModeCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)946 BrightnessModeCommand::BrightnessModeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
947     : CommandLine(commandType, arg, socket)
948 {
949 }
950 
RunGet()951 void BrightnessModeCommand::RunGet()
952 {
953     Json::Value result;
954     result["BrightnessMode"] = SharedData<uint8_t>::GetData(SharedDataType::BRIGHTNESS_MODE);
955     SetCommandResult("result", result);
956     ILOG("Get brightnessMode run finished");
957 }
958 
RunSet()959 void BrightnessModeCommand::RunSet()
960 {
961     SharedData<uint8_t>::SetData(SharedDataType::BRIGHTNESS_MODE,
962                                  static_cast<uint8_t>(atoi(args["BrightnessMode"].asString().data())));
963     Json::Value result = true;
964     SetCommandResult("result", result);
965     ILOG("Set brightnessMode run finished, the value is: %s", args["BrightnessMode"].asString().c_str());
966 }
967 
IsSetArgValid() const968 bool BrightnessModeCommand::IsSetArgValid() const
969 {
970     if (args.isNull() || !args.isMember("BrightnessMode")) {
971         ELOG("Invalid number of arguments!");
972         return false;
973     }
974     if (!std::regex_match(args["BrightnessMode"].asString(), std::regex("^\\d$"))) {
975         ELOG("regex match error");
976         return false;
977     }
978     uint8_t temp = ToUint8(args["BrightnessMode"].asString());
979     if (!SharedData<uint8_t>::IsValid(SharedDataType::BRIGHTNESS_MODE, temp)) {
980         ELOG("BrightnessModeCommand invalid value: %d", temp);
981         return false;
982     }
983     return true;
984 }
985 
ChargeModeCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)986 ChargeModeCommand::ChargeModeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
987     : CommandLine(commandType, arg, socket)
988 {
989 }
990 
RunGet()991 void ChargeModeCommand::RunGet()
992 {
993     Json::Value result;
994     result["ChargeMode"] = SharedData<uint8_t>::GetData(SharedDataType::BATTERY_STATUS);
995     SetCommandResult("result", result);
996     ILOG("Get chargeMode run finished");
997 }
998 
RunSet()999 void ChargeModeCommand::RunSet()
1000 {
1001     SharedData<uint8_t>::SetData(SharedDataType::BATTERY_STATUS,
1002                                  static_cast<uint8_t>(atoi(args["ChargeMode"].asString().data())));
1003     Json::Value result = true;
1004     SetCommandResult("result", result);
1005     ILOG("Set chargeMode run finished, the value is: %s", args["ChargeMode"].asString().c_str());
1006 }
1007 
IsSetArgValid() const1008 bool ChargeModeCommand::IsSetArgValid() const
1009 {
1010     if (args.isNull() || !args.isMember("ChargeMode")) {
1011         ELOG("Invalid number of arguments!");
1012         return false;
1013     }
1014     if (!std::regex_match(args["ChargeMode"].asString().data(), std::regex("\\d"))) {
1015         ELOG("Invalid arguments!");
1016         return false;
1017     }
1018     uint8_t temp = ToUint8(args["ChargeMode"].asString());
1019     if (!SharedData<uint8_t>::IsValid(SharedDataType::BATTERY_STATUS, temp)) {
1020         ELOG("ChargeModeCommand invalid value: %d", temp);
1021         return false;
1022     }
1023     return true;
1024 }
1025 
BrightnessCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1026 BrightnessCommand::BrightnessCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1027     : CommandLine(commandType, arg, socket)
1028 {
1029 }
1030 
RunGet()1031 void BrightnessCommand::RunGet()
1032 {
1033     Json::Value result;
1034     result["Brightness"] = SharedData<uint8_t>::GetData(SharedDataType::BRIGHTNESS_VALUE);
1035     SetCommandResult("result", result);
1036     ILOG("Get brightness run finished");
1037 }
1038 
RunSet()1039 void BrightnessCommand::RunSet()
1040 {
1041     SharedData<uint8_t>::SetData(SharedDataType::BRIGHTNESS_VALUE,
1042                                  static_cast<uint8_t>(atoi(args["Brightness"].asString().data())));
1043     Json::Value result = true;
1044     SetCommandResult("result", result);
1045     ILOG("Set brightness run finished, the value is: %s", args["Brightness"].asString().c_str());
1046 }
1047 
IsSetArgValid() const1048 bool BrightnessCommand::IsSetArgValid() const
1049 {
1050     if (args.isNull() || !args.isMember("Brightness")) {
1051         ELOG("Invalid number of arguments!");
1052         return false;
1053     }
1054     if (!std::regex_match(args["Brightness"].asString().data(), std::regex("\\d+"))) {
1055         ELOG("Invalid arguments!");
1056         return false;
1057     }
1058     uint8_t temp = ToUint8(args["Brightness"].asString());
1059     if (!SharedData<uint8_t>::IsValid(SharedDataType::BRIGHTNESS_VALUE, temp)) {
1060         ELOG("BrightnessCommand invalid value: ", temp);
1061         return false;
1062     }
1063     return true;
1064 }
1065 
HeartRateCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1066 HeartRateCommand::HeartRateCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1067     : CommandLine(commandType, arg, socket)
1068 {
1069 }
1070 
RunGet()1071 void HeartRateCommand::RunGet()
1072 {
1073     Json::Value result;
1074     result["HeartRate"] = SharedData<uint8_t>::GetData(SharedDataType::HEARTBEAT_VALUE);
1075     SetCommandResult("result", result);
1076     ILOG("Get heartRate run finished");
1077 }
1078 
RunSet()1079 void HeartRateCommand::RunSet()
1080 {
1081     SharedData<uint8_t>::SetData(SharedDataType::HEARTBEAT_VALUE,
1082                                  static_cast<uint8_t>(atoi(args["HeartRate"].asString().data())));
1083     Json::Value result = true;
1084     SetCommandResult("result", result);
1085     ILOG("Set heartRate run finished, the value is: %s", args["HeartRate"].asString().c_str());
1086 }
1087 
IsSetArgValid() const1088 bool HeartRateCommand::IsSetArgValid() const
1089 {
1090     if (args.isNull() || !args.isMember("HeartRate")) {
1091         ELOG("Invalid number of arguments!");
1092         return false;
1093     }
1094     if (!std::regex_match(args["HeartRate"].asString().data(), std::regex("\\d+"))) {
1095         ELOG("Invalid arguments!");
1096         return false;
1097     }
1098     if (atoi(args["HeartRate"].asString().data()) > UINT8_MAX) {
1099         ELOG("Invalid arguments!");
1100         return false;
1101     }
1102     uint8_t temp = ToUint8(args["HeartRate"].asString());
1103     if (!SharedData<uint8_t>::IsValid(SharedDataType::HEARTBEAT_VALUE, temp)) {
1104         ELOG("HeartRateCommand invalid value: %d", temp);
1105         return false;
1106     }
1107     return true;
1108 }
1109 
StepCountCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1110 StepCountCommand::StepCountCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1111     : CommandLine(commandType, arg, socket)
1112 {
1113 }
1114 
RunGet()1115 void StepCountCommand::RunGet()
1116 {
1117     Json::Value result;
1118     result["StepCount"] = SharedData<uint32_t>::GetData(SharedDataType::SUMSTEP_VALUE);
1119     SetCommandResult("result", result);
1120     ILOG("Get stepCount run finished");
1121 }
1122 
RunSet()1123 void StepCountCommand::RunSet()
1124 {
1125     SharedData<uint32_t>::SetData(SharedDataType::SUMSTEP_VALUE,
1126                                   static_cast<uint32_t>(atoi(args["StepCount"].asString().data())));
1127     Json::Value result = true;
1128     SetCommandResult("result", result);
1129     ILOG("Set stepCount run finished, the value is: %s", args["StepCount"].asString().c_str());
1130 }
1131 
IsSetArgValid() const1132 bool StepCountCommand::IsSetArgValid() const
1133 {
1134     if (args.isNull() || !args.isMember("StepCount")) {
1135         ELOG("Invalid number of arguments!");
1136         return false;
1137     }
1138     if (!std::regex_match(args["StepCount"].asString().data(), std::regex("\\d+"))) {
1139         ELOG("Invalid arguments!");
1140         return false;
1141     }
1142 
1143     uint32_t temp = ToUint8(args["StepCount"].asString());
1144     if (!SharedData<uint32_t>::IsValid(SharedDataType::SUMSTEP_VALUE, temp)) {
1145         ELOG("StepCountCommand invalid value: %d", temp);
1146         return false;
1147     }
1148     return true;
1149 }
1150 
InspectorJSONTree(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1151 InspectorJSONTree::InspectorJSONTree(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1152     : CommandLine(commandType, arg, socket)
1153 {
1154 }
1155 
RunAction()1156 void InspectorJSONTree::RunAction()
1157 {
1158     ILOG("GetJsonTree run!");
1159     std::string str = JsAppImpl::GetInstance().GetJSONTree();
1160     if (str == "null") {
1161         str = "{\"children\":\"empty json tree\"}";
1162     }
1163     SetCommandResult("result", str);
1164     ILOG("SendJsonTree end!");
1165 }
1166 
InspectorDefault(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1167 InspectorDefault::InspectorDefault(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1168     : CommandLine(commandType, arg, socket)
1169 {
1170 }
1171 
RunAction()1172 void InspectorDefault::RunAction()
1173 {
1174     ILOG("GetDefaultJsonTree run!");
1175     std::string str = JsAppImpl::GetInstance().GetDefaultJSONTree();
1176     SetCommandResult("result", str);
1177     ILOG("SendDefaultJsonTree end!");
1178 }
1179 
ExitCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1180 ExitCommand::ExitCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1181     : CommandLine(commandType, arg, socket)
1182 {
1183 }
1184 
RunAction()1185 void ExitCommand::RunAction()
1186 {
1187     ILOG("ExitCommand run.");
1188     Json::Value res = true;
1189     SetCommandResult("result", res);
1190     SendResult();
1191     Interrupter::Interrupt();
1192     ILOG("Ready to exit");
1193 }
1194 
DeviceTypeCommand(CommandLine::CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1195 DeviceTypeCommand::DeviceTypeCommand(CommandLine::CommandType commandType,
1196                                      const Json::Value& arg,
1197                                      const LocalSocket& socket)
1198     : CommandLine(commandType, arg, socket)
1199 {
1200 }
1201 
RunSet()1202 void DeviceTypeCommand::RunSet() {}
1203 
ResolutionCommand(CommandLine::CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1204 ResolutionCommand::ResolutionCommand(CommandLine::CommandType commandType,
1205                                      const Json::Value& arg,
1206                                      const LocalSocket& socket)
1207     : CommandLine(commandType, arg, socket)
1208 {
1209 }
1210 
RunSet()1211 void ResolutionCommand::RunSet() {}
1212 
BackClickedCommand(CommandLine::CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1213 BackClickedCommand::BackClickedCommand(CommandLine::CommandType commandType,
1214                                        const Json::Value& arg,
1215                                        const LocalSocket& socket)
1216     : CommandLine(commandType, arg, socket)
1217 {
1218 }
1219 
RunAction()1220 void BackClickedCommand::RunAction()
1221 {
1222     MouseInputImpl::GetInstance().DispatchOsBackEvent();
1223     ILOG("BackClickCommand run");
1224     Json::Value res = true;
1225     SetCommandResult("result", res);
1226     ILOG("BackClickCommand end");
1227 }
1228 
RestartCommand(CommandLine::CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1229 RestartCommand::RestartCommand(CommandLine::CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1230     : CommandLine(commandType, arg, socket)
1231 {
1232 }
1233 
RunAction()1234 void RestartCommand::RunAction()
1235 {
1236     ILOG("RestartCommand start");
1237     JsAppImpl::GetInstance().Restart();
1238     Json::Value res = true;
1239     SetCommandResult("result", res);
1240     ILOG("RestartCommand end");
1241 }
1242 
FastPreviewMsgCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1243 FastPreviewMsgCommand::FastPreviewMsgCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1244     : CommandLine(commandType, arg, socket)
1245 {
1246 }
1247 
RunGet()1248 void FastPreviewMsgCommand::RunGet()
1249 {
1250     Json::Value resultContent;
1251     std::string fastPreviewMsg = VirtualScreenImpl::GetInstance().GetFastPreviewMsg();
1252     resultContent["FastPreviewMsg"] = fastPreviewMsg;
1253     SetResultToManager("args", resultContent, "MemoryRefresh");
1254     ILOG("Get FastPreviewMsgCommand run finished.");
1255 }
1256 
DropFrameCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1257 DropFrameCommand::DropFrameCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1258     : CommandLine(commandType, arg, socket)
1259 {
1260 }
1261 
IsSetArgValid() const1262 bool DropFrameCommand::IsSetArgValid() const
1263 {
1264     if (args.isNull() || !args.isMember("frequency") || !args["frequency"].isInt()) {
1265         ELOG("Invalid DropFrame of arguments!");
1266         return false;
1267     }
1268     if (args["frequency"].asInt() < 0) {
1269         ELOG("DropFrame param frequency must greater than or equal to 0");
1270         return false;
1271     }
1272     return true;
1273 }
1274 
RunSet()1275 void DropFrameCommand::RunSet()
1276 {
1277     ILOG("Set DropFrame frequency start.");
1278     int frequency = args["frequency"].asInt();
1279     VirtualScreenImpl::GetInstance().SetDropFrameFrequency(frequency);
1280     SetCommandResult("result", true);
1281     ILOG("Set DropFrame frequency: %sms.", frequency);
1282 }
1283 
IsActionArgValid() const1284 bool KeyPressCommand::IsActionArgValid() const
1285 {
1286     if (args.isNull() || !args.isMember("isInputMethod") || !args["isInputMethod"].isBool()) {
1287         ELOG("Param isInputMethod's value is invalid.");
1288         return false;
1289     }
1290     bool isInputMethod = args["isInputMethod"].asBool();
1291     if (isInputMethod) {
1292         return IsImeArgsValid();
1293     } else {
1294         return IsKeyArgsValid();
1295     }
1296 }
1297 
IsImeArgsValid() const1298 bool KeyPressCommand::IsImeArgsValid() const
1299 {
1300     if (!args.isMember("codePoint") || !args["codePoint"].isInt()) {
1301         ELOG("Param codePoint's value is invalid.");
1302         return false;
1303     }
1304     return true;
1305 }
1306 
IsKeyArgsValid() const1307 bool KeyPressCommand::IsKeyArgsValid() const
1308 {
1309     if (!args.isMember("keyCode") || !args["keyCode"].isInt() || !args["keyAction"].isInt() ||
1310         !args.isMember("keyAction") || !args["keyAction"].isInt() ||
1311         !args.isMember("pressedCodes") || !args["pressedCodes"].isArray() ||
1312         args["pressedCodes"].size() < 1 || (args.isMember("keyString") && !args["keyString"].isString())) {
1313         ELOG("Param keyEvent's value is invalid.");
1314         return false;
1315     }
1316     if (!args.isMember("keyString")) {
1317         ILOG("Param keyString is lost, it will be empty string.");
1318     }
1319     if (args["keyAction"].asInt() < minActionVal || args["keyAction"].asInt() > maxActionVal) {
1320         ELOG("Param keyAction's value is invalid,value range %d-%d.", minActionVal, maxActionVal);
1321         return false;
1322     }
1323     int keyCode = args["keyCode"].asInt();
1324     if (keyCode > maxKeyVal || keyCode < minKeyVal) {
1325         ELOG("Param pressedCode value is invalid,value range %d-%d.", minKeyVal, maxKeyVal);
1326         return false;
1327     }
1328     Json::Value arrayNum = args["pressedCodes"];
1329     for (unsigned int i = 0; i < arrayNum.size(); i++) {
1330         if (!arrayNum[i].isInt()) {
1331             ELOG("Param pressedCodes's value is invalid.");
1332             return false;
1333         }
1334         int pressedCode = arrayNum[i].asInt();
1335         if (pressedCode > maxKeyVal || pressedCode < minKeyVal) {
1336             ELOG("Param pressedCode value is invalid,value range %d-%d.", minKeyVal, maxKeyVal);
1337             return false;
1338         }
1339     }
1340     return true;
1341 }
1342 
KeyPressCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1343 KeyPressCommand::KeyPressCommand(CommandType commandType, const Json::Value& arg,
1344                                  const LocalSocket& socket)
1345     : CommandLine(commandType, arg, socket)
1346 {
1347 }
1348 
RunAction()1349 void KeyPressCommand::RunAction()
1350 {
1351     if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) {
1352         return;
1353     }
1354     bool isInputMethod = args["isInputMethod"].asBool();
1355     if (isInputMethod) {
1356         VirtualScreen::inputMethodCountPerMinute++;
1357         unsigned int codePoint = args["codePoint"].asInt();
1358         KeyInputImpl::GetInstance().SetCodePoint(codePoint);
1359         KeyInputImpl::GetInstance().DispatchOsInputMethodEvent();
1360     } else {
1361         VirtualScreen::inputKeyCountPerMinute++;
1362         int32_t keyCode = args["keyCode"].asInt();
1363         int32_t keyAction = args["keyAction"].asInt();
1364         Json::Value pressedCodes = args["pressedCodes"];
1365         vector<int32_t> pressedCodesVec;
1366         for (unsigned int i = 0; i < pressedCodes.size(); i++) {
1367             pressedCodesVec.push_back(pressedCodes[i].asInt());
1368         }
1369         string keyString = "";
1370         if (args.isMember("keyString") && args["keyString"].isString()) {
1371             keyString = args["keyString"].asString();
1372         }
1373         KeyInputImpl::GetInstance().SetKeyEvent(keyCode, keyAction, pressedCodesVec, keyString);
1374         KeyInputImpl::GetInstance().DispatchOsKeyEvent();
1375     }
1376     SetCommandResult("result", true);
1377     ILOG("KeyPressCommand run finished.");
1378 }
1379 
IsActionArgValid() const1380 bool PointEventCommand::IsActionArgValid() const
1381 {
1382     return IsArgsExist() && IsArgsValid();
1383 }
1384 
IsArgsExist() const1385 bool PointEventCommand::IsArgsExist() const
1386 {
1387     if (args.isNull() || !args.isMember("x") || !args.isMember("y") ||
1388         !args["x"].isInt() || !args["y"].isInt()) {
1389         return false;
1390     }
1391     if (!args.isMember("button") || !args.isMember("action") ||
1392         !args["button"].isInt() || !args["action"].isInt()) {
1393         return false;
1394     }
1395     if (!args.isMember("sourceType") || !args.isMember("sourceTool") ||
1396         !args["sourceType"].isInt() || !args["sourceTool"].isInt()) {
1397         return false;
1398     }
1399     if (!args.isMember("axisValues") || !args["axisValues"].isArray()) {
1400         return false;
1401     }
1402     return true;
1403 }
1404 
IsArgsValid() const1405 bool PointEventCommand::IsArgsValid() const
1406 {
1407     int32_t pointX = args["x"].asInt();
1408     int32_t pointY = args["y"].asInt();
1409     int32_t button = args["button"].asInt();
1410     int32_t action = args["action"].asInt();
1411     int32_t sourceType = args["sourceType"].asInt();
1412     int32_t sourcceTool = args["sourcceTool"].asInt();
1413     if (pointX < 0 || pointX > VirtualScreenImpl::GetInstance().GetOrignalWidth()) {
1414         ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalWidth());
1415         return false;
1416     }
1417     if (pointY < 0 || pointY > VirtualScreenImpl::GetInstance().GetOrignalHeight()) {
1418         ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalHeight());
1419         return false;
1420     }
1421     if (button < -1 || action < 0 || sourceType < 0 || sourcceTool < 0) {
1422         ELOG("action,sourceType,sourcceTool must >= 0, button must >= -1");
1423         return false;
1424     }
1425     Json::Value axisArrayNum = args["axisValues"];
1426     for (unsigned int i = 0; i < axisArrayNum.size(); i++) {
1427         if (!axisArrayNum[i].isDouble()) {
1428             ELOG("Param axisValues's value is invalid.");
1429             return false;
1430         }
1431     }
1432     return true;
1433 }
1434 
PointEventCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1435 PointEventCommand::PointEventCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1436     : CommandLine(commandType, arg, socket)
1437 {
1438 }
1439 
RunAction()1440 void PointEventCommand::RunAction()
1441 {
1442     int type = 9;
1443     EventParams param;
1444     if (args.isMember("pressedButtons") && args["pressedButtons"].isArray()) {
1445         Json::Value pressedCodes = args["pressedCodes"];
1446         for (unsigned int i = 0; i < pressedCodes.size(); i++) {
1447             if (!pressedCodes[i].isInt() || pressedCodes[i].asInt() < -1) {
1448                 continue;
1449             }
1450             param.pressedBtnsVec.insert(pressedCodes[i].asInt());
1451         }
1452     }
1453     vector<double> axisVec; // 13 is array size
1454     Json::Value axisCodes = args["axisValues"];
1455     for (unsigned int i = 0; i < axisCodes.size(); i++) {
1456         param.axisVec.push_back(axisCodes[i].asDouble());
1457     }
1458     param.x = atof(args["x"].asString().data());
1459     param.y = atof(args["y"].asString().data());
1460     param.type = type;
1461     param.button = args["button"].asInt();
1462     param.action = args["action"].asInt();
1463     param.sourceType = args["sourceType"].asInt();
1464     param.sourceTool = args["sourceTool"].asInt();
1465     param.name = "PointEvent";
1466     SetEventParams(param);
1467     SetCommandResult("result", true);
1468 }