/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "CommandLine.h" #include #include #include "CommandLineInterface.h" #include "CommandParser.h" #include "Interrupter.h" #include "JsApp.h" #include "JsAppImpl.h" #include "JsonReader.h" #include "LanguageManagerImpl.h" #include "ModelConfig.h" #include "ModelManager.h" #include "MouseInputImpl.h" #include "MouseWheelImpl.h" #include "KeyInputImpl.h" #include "PreviewerEngineLog.h" #include "SharedData.h" #include "VirtualMessageImpl.h" #include "VirtualScreenImpl.h" using namespace std; CommandLine::CommandLine(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : args(arg), cliSocket(socket), type(commandType), commandName("") { SetCommandResult("result", true); } CommandLine::~CommandLine() { args.clear(); } void CommandLine::CheckAndRun() { if (!IsArgValid()) { SetCommandResult("result", false); SendResult(); return; } Run(); SendResult(); } void CommandLine::SendResult() { if (commandResult.empty()) { return; } cliSocket << commandResult.toStyledString(); commandResult.clear(); } void CommandLine::RunAndSendResultToManager() { Run(); SendResultToManager(); } void CommandLine::SendResultToManager() { if (commandResultToManager.empty()) { return; } cliSocket << commandResultToManager.toStyledString(); commandResultToManager.clear(); } bool CommandLine::IsArgValid() const { if (type == CommandType::GET) { return IsGetArgValid(); } if (type == CommandType::SET) { return IsSetArgValid(); } if (type == CommandType::ACTION) { return IsActionArgValid(); } return true; } uint8_t CommandLine::ToUint8(std::string str) const { int value = atoi(str.data()); if (value > UINT8_MAX) { ELOG("CommandLine::ToUint8 value is overflow, value: %d", value); } return static_cast(value); } void CommandLine::SetCommandName(std::string command) { this->commandName = command; } void CommandLine::SetCommandResult(const std::string& resultType, const Json::Value& resultContent) { this->commandResult["version"] = CommandLineInterface::COMMAND_VERSION; this->commandResult["command"] = this->commandName; this->commandResult[resultType] = resultContent; } void CommandLine::SetResultToManager(const std::string& resultType, const Json::Value& resultContent, const std::string& messageType) { this->commandResultToManager["MessageType"] = messageType; this->commandResultToManager[resultType] = resultContent; } void CommandLine::Run() { if (type == CommandType::GET) { RunGet(); } else if (type == CommandType::SET) { RunSet(); } else if (type == CommandType::ACTION) { RunAction(); } } bool CommandLine::IsBoolType(std::string arg) const { regex tofrx("^(true)|(false)$"); if (regex_match(arg, tofrx)) { return true; } return false; } bool CommandLine::IsIntType(string arg) const { regex isInt("^\\d+$"); return regex_match(arg, isInt); } bool CommandLine::IsOneDigitFloatType(string arg) const { regex isFloat("^\\d(\\.\\d+)?$"); return regex_match(arg, isFloat); } bool TouchPressCommand::IsActionArgValid() const { if (args.isNull() || !args.isMember("x") || !args.isMember("y") || !args["x"].isInt() || !args["y"].isInt()) { return false; } int32_t pointX = args["x"].asInt(); int32_t pointY = args["y"].asInt(); if (pointX < 0 || pointX > VirtualScreenImpl::GetInstance().GetOrignalWidth()) { ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalWidth()); return false; } if (pointY < 0 || pointY > VirtualScreenImpl::GetInstance().GetOrignalHeight()) { ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalHeight()); return false; } return true; } TouchPressCommand::TouchPressCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void TouchPressCommand::RunAction() { if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) { return; } MouseInputImpl::GetInstance().SetMousePosition(atof(args["x"].asString().data()), atof(args["y"].asString().data())); MouseInputImpl::GetInstance().SetMouseStatus(MouseInputImpl::INDEV_STATE_PRESS); MouseInputImpl::GetInstance().DispatchOsTouchEvent(); SetCommandResult("result", true); ILOG("MousePress(%s,%s)", args["x"].asString().c_str(), args["y"].asString().c_str()); } bool MouseWheelCommand::IsActionArgValid() const { if (args.isNull() || !args.isMember("rotate") || !IsOneDigitFloatType(args["rotate"].asString())) { return false; } return true; } MouseWheelCommand::MouseWheelCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void MouseWheelCommand::RunAction() { if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) { return; } MouseWheelImpl::GetInstance().SetRotate(atof(args["rotate"].asString().data())); SetCommandResult("result", true); ILOG("CrownRotate (%s)", args["rotate"].asString().c_str()); } TouchReleaseCommand::TouchReleaseCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void TouchReleaseCommand::RunAction() { if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) { return; } MouseInputImpl::GetInstance().SetMouseStatus(MouseInputImpl::INDEV_STATE_RELEASE); MouseInputImpl::GetInstance().DispatchOsTouchEvent(); SetCommandResult("result", true); ILOG("MouseRelease run finished"); } bool TouchMoveCommand::IsActionArgValid() const { if (args.isNull() || !args.isMember("x") || !args.isMember("y") || !args["x"].isInt() || !args["y"].isInt()) { return false; } int32_t pX = args["x"].asInt(); int32_t pY = args["y"].asInt(); if (pX < 0 || pX > VirtualScreenImpl::GetInstance().GetOrignalWidth()) { ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalWidth()); return false; } if (pY < 0 || pY > VirtualScreenImpl::GetInstance().GetOrignalHeight()) { ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalHeight()); return false; } return true; } TouchMoveCommand::TouchMoveCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void TouchMoveCommand::RunAction() { if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) { return; } MouseInputImpl::GetInstance().SetMouseStatus(MouseInput::INDEV_STATE_MOVE); MouseInputImpl::GetInstance().SetMousePosition(atof(args["x"].asString().data()), atof(args["y"].asString().data())); MouseInputImpl::GetInstance().DispatchOsTouchEvent(); Json::Value res = true; SetCommandResult("result", res); ILOG("MouseMove run finished"); } PowerCommand::PowerCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool PowerCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("Power") || !IsOneDigitFloatType(args["Power"].asString())) { ELOG("Invalid number of arguments!"); return false; } float val = args["Power"].asFloat(); if (!SharedData::IsValid(SharedDataType::BATTERY_LEVEL, val)) { ELOG("PowerCommand invalid value: %f", val); return false; } return true; } void PowerCommand::RunGet() { double power = SharedData::GetData(SharedDataType::BATTERY_LEVEL); Json::Value resultContent; resultContent["Power"] = power; SetCommandResult("result", resultContent); ILOG("Get power run finished"); } void PowerCommand::RunSet() { string power(args["Power"].asString()); SharedData::SetData(SharedDataType::BATTERY_LEVEL, atof(power.data())); Json::Value resultContent = true; SetCommandResult("result", resultContent); ILOG("Set power run finished, the value is: %s", args["Power"].asString().c_str()); } VolumeCommand::VolumeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool VolumeCommand::IsSetArgValid() const { return true; } void VolumeCommand::RunGet() { Json::Value resultContent = "Command offline"; SetCommandResult("result", resultContent); ILOG("Command offline"); } void VolumeCommand::RunSet() { Json::Value resultContent = "Command offline"; SetCommandResult("result", resultContent); ILOG("Command offline"); } BarometerCommand::BarometerCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool BarometerCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("Barometer") || !args["Barometer"].isString()) { ELOG("Invalid number of arguments!"); return false; } string barometer(args["Barometer"].asString()); if (!IsIntType(barometer)) { ELOG("Invalid arguments!"); return false; } if (!SharedData::IsValid(SharedDataType::PRESSURE_VALUE, static_cast(atoi(barometer.data())))) { ELOG("Barometer invalid value: %d", atoi(barometer.data())); return false; } return true; } void BarometerCommand::RunGet() { int barometer = static_cast(SharedData::GetData(SharedDataType::PRESSURE_VALUE)); Json::Value resultContent; resultContent["Barometer"] = barometer; SetCommandResult("result", resultContent); ILOG("Get barometer run finished"); } void BarometerCommand::RunSet() { string barometer(args["Barometer"].asString()); SharedData::SetData(SharedDataType::PRESSURE_VALUE, static_cast(atoi(barometer.data()))); SetCommandResult("result", true); ILOG("Set barometer run finished, the value is: %s", args["Barometer"].asString().c_str()); } ResolutionSwitchCommand::ResolutionSwitchCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool ResolutionSwitchCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("originWidth") || !args.isMember("originHeight") || !args.isMember("width") || !args.isMember("height") || !args.isMember("screenDensity")) { ELOG("Invalid param of arguments!"); return false; } if (!args["originWidth"].isInt() || !args["originHeight"].isInt() || !args["screenDensity"].isInt() || !args["width"].isInt() || !args["height"].isInt()) { ELOG("Invalid number of arguments!"); return false; } if (!IsIntValValid(args)) { return false; } return true; } bool ResolutionSwitchCommand::IsIntValValid(const Json::Value& args) const { if (args["originWidth"].asInt() < minWidth || args["originWidth"].asInt() > maxWidth || args["originHeight"].asInt() < minWidth || args["originHeight"].asInt() > maxWidth || args["width"].asInt() < minWidth || args["width"].asInt() > maxWidth || args["height"].asInt() < minWidth || args["height"].asInt() > maxWidth) { ELOG("width or height is out of range %d-%d", minWidth, maxWidth); return false; } if (args["screenDensity"].asInt() < minDpi || args["screenDensity"].asInt() > maxDpi) { ELOG("screenDensity is out of range %d-%d", minDpi, maxDpi); return false; } return true; } void ResolutionSwitchCommand::RunSet() { int32_t originWidth = args["originWidth"].asInt(); int32_t originHeight = args["originHeight"].asInt(); int32_t width = args["width"].asInt(); int32_t height = args["height"].asInt(); int32_t screenDensity = args["screenDensity"].asInt(); JsAppImpl::GetInstance().ResolutionChanged(originWidth, originHeight, width, height, screenDensity); SetCommandResult("result", true); ILOG("ResolutionSwitch run finished."); } OrientationCommand::OrientationCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool OrientationCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("Orientation") || !args["Orientation"].isString()) { ELOG("Invalid number of arguments!"); return false; } if (args["Orientation"].asString() != "portrait" && args["Orientation"].asString() != "landscape") { ELOG("Orientation just support [portrait,landscape]."); return false; } return true; } void OrientationCommand::RunSet() { std::string commandOrientation = args["Orientation"].asString(); std::string curruntOrientation = JsAppImpl::GetInstance().GetOrientation(); if (commandOrientation != curruntOrientation) { JsAppImpl::GetInstance().OrientationChanged(commandOrientation); } SetCommandResult("result", true); ILOG("Set Orientation run finished, Orientation is: %s", args["Orientation"].asString().c_str()); } ColorModeCommand::ColorModeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool ColorModeCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("ColorMode") || !args["ColorMode"].isString()) { ELOG("Invalid number of arguments!"); return false; } if (args["ColorMode"].asString() != "light" && args["ColorMode"].asString() != "dark") { ELOG("ColorMode just support [light,dark]"); return false; } return true; } void ColorModeCommand::RunSet() { std::string commandColorMode = args["ColorMode"].asString(); std::string currentColorMode = JsAppImpl::GetInstance().GetColorMode(); if (commandColorMode != currentColorMode) { JsAppImpl::GetInstance().SetArgsColorMode(args["ColorMode"].asString()); JsAppImpl::GetInstance().ColorModeChanged(commandColorMode); } SetCommandResult("result", true); ILOG("Set ColorMode run finished, ColorMode is: %s", args["ColorMode"].asString().c_str()); } FontSelectCommand::FontSelectCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool FontSelectCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("FontSelect") || !args["FontSelect"].isBool()) { ELOG("Invalid number of arguments!"); return false; } return true; } void FontSelectCommand::RunSet() { SetCommandResult("result", true); ILOG("FontSelect finished, currentSelect is: %d", args["FontSelect"].asBool()); } MemoryRefreshCommand::MemoryRefreshCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool MemoryRefreshCommand::IsSetArgValid() const { if (args.isNull()) { ELOG("Invalid MemoryRefresh of arguments!"); return false; } return true; } void MemoryRefreshCommand::RunSet() { ILOG("MemoryRefreshCommand begin."); bool ret = JsAppImpl::GetInstance().MemoryRefresh(args.toStyledString()); SetCommandResult("result", ret); ILOG("MemoryRefresh finished."); } LoadDocumentCommand::LoadDocumentCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool LoadDocumentCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("url") || !args.isMember("className") || !args.isMember("previewParam") || !args["url"].isString() || !args["className"].isString() || !args["previewParam"].isObject()) { return false; } Json::Value previewParam = args["previewParam"]; if (!previewParam["width"].isInt() || !previewParam["height"].isInt() || !previewParam["dpi"].isInt() || !previewParam["locale"].isString() || !previewParam["colorMode"].isString() || !previewParam["orientation"].isString() || !previewParam["deviceType"].isString()) { return false; } if (!IsIntValValid(previewParam) || !IsStrValVailid(previewParam)) { return false; } return true; } bool LoadDocumentCommand::IsIntValValid(const Json::Value& previewParam) const { int width = previewParam["width"].asInt(); int height = previewParam["height"].asInt(); int dpi = previewParam["dpi"].asInt(); if (width < minLoadDocWidth || width > maxLoadDocWidth || height < minLoadDocWidth || height > maxLoadDocWidth || dpi < minDpi || dpi > maxDpi) { return false; } return true; } bool LoadDocumentCommand::IsStrValVailid(const Json::Value& previewParam) const { string locale = previewParam["locale"].asString(); bool isLiteDevice = JsApp::IsLiteDevice(CommandParser::GetInstance().GetDeviceType()); if (isLiteDevice) { if (std::find(liteSupportedLanguages.begin(), liteSupportedLanguages.end(), locale) == liteSupportedLanguages.end()) { return false; } } else { if (std::find(richSupportedLanguages.begin(), richSupportedLanguages.end(), locale) == richSupportedLanguages.end()) { return false; } } if (previewParam["colorMode"].asString() != "light" && previewParam["colorMode"].asString() != "dark") { return false; } if (previewParam["orientation"].asString() != "portrait" && previewParam["orientation"].asString() != "landscape") { return false; } if (std::find(LoadDocDevs.begin(), LoadDocDevs.end(), previewParam["deviceType"].asString()) == LoadDocDevs.end()) { return false; } return true; } void LoadDocumentCommand::RunSet() { VirtualScreenImpl::GetInstance().SetLoadDocFlag(VirtualScreen::LoadDocType::START); ILOG("LoadDocumentCommand begin."); std::string pageUrl = args["url"].asString(); std::string className = args["className"].asString(); Json::Value previewParam = Json::nullValue; if (args.isMember("previewParam")) { previewParam = args["previewParam"]; } JsAppImpl::GetInstance().LoadDocument(pageUrl, className, previewParam); VirtualScreenImpl::GetInstance().SetLoadDocFlag(VirtualScreen::LoadDocType::FINISHED); SetCommandResult("result", true); ILOG("LoadDocumentCommand finished."); } ReloadRuntimePageCommand::ReloadRuntimePageCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool ReloadRuntimePageCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("ReloadRuntimePage") || !args["ReloadRuntimePage"].isString()) { ELOG("Invalid number of arguments!"); return false; } return true; } void ReloadRuntimePageCommand::RunSet() { std::string currentPage = args["ReloadRuntimePage"].asString(); JsAppImpl::GetInstance().ReloadRuntimePage(currentPage); SetCommandResult("result", true); ILOG("ReloadRuntimePage finished, currentPage is: %s", args["ReloadRuntimePage"].asString().c_str()); } CurrentRouterCommand::CurrentRouterCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void CurrentRouterCommand::RunGet() { Json::Value resultContent; std::string currentRouter = VirtualScreenImpl::GetInstance().GetCurrentRouter(); resultContent["CurrentRouter"] = currentRouter; SetResultToManager("args", resultContent, "CurrentJsRouter"); ILOG("Get CurrentRouter run finished."); } LanguageCommand::LanguageCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool LanguageCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("Language") || !args["Language"].isString()) { ELOG("Invalid number of arguments!"); return false; } CommandParser& cmdParser = CommandParser::GetInstance(); string deviceType = cmdParser.GetDeviceType(); bool isLiteDevice = JsApp::IsLiteDevice(deviceType); if (isLiteDevice) { if (std::find(liteSupportedLanguages.begin(), liteSupportedLanguages.end(), args["Language"].asString()) == liteSupportedLanguages.end()) { ELOG("Language not support liteDevice : %s", args["Language"].asString().c_str()); return false; } } else { if (std::find(richSupportedLanguages.begin(), richSupportedLanguages.end(), args["Language"].asString()) == richSupportedLanguages.end()) { ELOG("Language not support richDevice : %s", args["Language"].asString().c_str()); return false; } } return true; } void LanguageCommand::RunGet() { std::string language = SharedData::GetData(SharedDataType::LANGUAGE); Json::Value resultContent; resultContent["Language"] = language; SetCommandResult("result", resultContent); ILOG("Get language run finished."); } void LanguageCommand::RunSet() { string language(args["Language"].asString()); SharedData::SetData(SharedDataType::LANGUAGE, language); SetCommandResult("result", true); ILOG("Set language run finished, language is: %s", language.c_str()); } SupportedLanguagesCommand::SupportedLanguagesCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void SupportedLanguagesCommand::RunGet() { Json::Value resultContent; Json::Value languageList; string deviceType = CommandParser::GetInstance().GetDeviceType(); bool isLiteDevice = JsApp::IsLiteDevice(deviceType); if (!deviceType.empty() && !isLiteDevice) { for (auto iter = richSupportedLanguages.begin(); iter != richSupportedLanguages.end(); iter++) { languageList.append(*iter); } } else { for (auto iter = liteSupportedLanguages.begin(); iter != liteSupportedLanguages.end(); iter++) { languageList.append(*iter); } } resultContent["SupportedLanguages"] = languageList; SetCommandResult("result", resultContent); ILOG("Get supportedLanguages run finished."); } LocationCommand::LocationCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool LocationCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("latitude") || !args.isMember("longitude")) { ELOG("Invalid number of arguments!"); return false; } string latitude(args["latitude"].asString()); string longitude(args["longitude"].asString()); regex isDob("^([\\-]*[0-9]{1,}[\\.][0-9]*)$"); if (!regex_match(latitude, isDob) || !regex_match(longitude, isDob)) { ELOG("Invalid arguments!"); return false; } if (!SharedData::IsValid(SharedDataType::LATITUDE, atof(latitude.data()))) { ELOG("LocationCommand invalid latitude value: %f", atof(latitude.data())); return false; } if (!SharedData::IsValid(SharedDataType::LONGITUDE, atof(longitude.data()))) { ELOG("LocationCommand invalid longitude value: %f", atof(longitude.data())); return false; } return true; } void LocationCommand::RunGet() { double longitude = SharedData::GetData(SharedDataType::LONGITUDE); double latitude = SharedData::GetData(SharedDataType::LATITUDE); Json::Value resultContent; resultContent["latitude"] = latitude; resultContent["longitude"] = longitude; SetCommandResult("result", resultContent); ILOG("Get location run finished"); } void LocationCommand::RunSet() { string latitude(args["latitude"].asString()); string longitude(args["longitude"].asString()); SharedData::SetData(SharedDataType::LONGITUDE, atof(longitude.data())); SharedData::SetData(SharedDataType::LATITUDE, atof(latitude.data())); Json::Value resultContent = true; SetCommandResult("result", resultContent); ILOG("Set location run finished, latitude: %s,longitude: %s", latitude.c_str(), longitude.c_str()); } DistributedCommunicationsCommand::DistributedCommunicationsCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void DistributedCommunicationsCommand::RunAction() { MessageInfo info; info.deviceID = args["DeviceId"].asString(); info.bundleName = args["bundleName"].asString(); info.abilityName = args["abilityName"].asString(); info.message = args["message"].asString(); VirtualMessageImpl::GetInstance().SendVirtualMessage(info); Json::Value resultContent = true; SetCommandResult("result", resultContent); ILOG("Send distributedCommunications run finished"); } bool DistributedCommunicationsCommand::IsActionArgValid() const { if (args.isNull() || !args.isMember("DeviceId") || !args.isMember("bundleName") || !args.isMember("abilityName") || !args.isMember("message")) { ELOG("Invalid number of arguments!"); return false; } if (args["DeviceId"].asString().empty() || args["bundleName"].asString().empty() || args["abilityName"].asString().empty() || args["message"].asString().empty()) { ELOG("Invalid arguments!"); return false; } return true; } std::vector DistributedCommunicationsCommand::StringToCharVector(string str) const { vector vec(str.begin(), str.end()); vec.push_back('\0'); return vec; } KeepScreenOnStateCommand::KeepScreenOnStateCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void KeepScreenOnStateCommand::RunGet() { Json::Value result; result["KeepScreenOnState"] = SharedData::GetData(SharedDataType::KEEP_SCREEN_ON); SetCommandResult("result", result); ILOG("Get keepScreenOnState run finished"); } void KeepScreenOnStateCommand::RunSet() { SharedData::SetData(SharedDataType::KEEP_SCREEN_ON, args["KeepScreenOnState"].asString() == "true"); Json::Value result = true; SetCommandResult("result", result); ILOG("Set keepScreenOnState run finished, the value is: %s", args["KeepScreenOnState"].asString().c_str()); } bool KeepScreenOnStateCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("KeepScreenOnState")) { ELOG("Invalid number of arguments!"); return false; } if (!IsBoolType(args["KeepScreenOnState"].asString())) { ELOG("arg 0: %s", args["KeepScreenOnState"].asString().c_str()); return false; } return true; } WearingStateCommand::WearingStateCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void WearingStateCommand::RunGet() { Json::Value result; result["WearingState"] = SharedData::GetData(SharedDataType::WEARING_STATE); SetCommandResult("result", result); ILOG("Get wearingState run finished"); } void WearingStateCommand::RunSet() { SharedData::SetData(SharedDataType::WEARING_STATE, args["WearingState"].asString() == "true"); Json::Value result = true; SetCommandResult("result", result); ILOG("Set wearingState run finished, the value is: %s", args["WearingState"].asString().c_str()); } bool WearingStateCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("WearingState")) { ELOG("Invalid number of arguments!"); return false; } if (!IsBoolType(args["WearingState"].asString())) { ILOG("arg 0: %s", args["WearingState"].asString().c_str()); return false; } return true; } BrightnessModeCommand::BrightnessModeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void BrightnessModeCommand::RunGet() { Json::Value result; result["BrightnessMode"] = SharedData::GetData(SharedDataType::BRIGHTNESS_MODE); SetCommandResult("result", result); ILOG("Get brightnessMode run finished"); } void BrightnessModeCommand::RunSet() { SharedData::SetData(SharedDataType::BRIGHTNESS_MODE, static_cast(atoi(args["BrightnessMode"].asString().data()))); Json::Value result = true; SetCommandResult("result", result); ILOG("Set brightnessMode run finished, the value is: %s", args["BrightnessMode"].asString().c_str()); } bool BrightnessModeCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("BrightnessMode")) { ELOG("Invalid number of arguments!"); return false; } if (!std::regex_match(args["BrightnessMode"].asString(), std::regex("^\\d$"))) { ELOG("regex match error"); return false; } uint8_t temp = ToUint8(args["BrightnessMode"].asString()); if (!SharedData::IsValid(SharedDataType::BRIGHTNESS_MODE, temp)) { ELOG("BrightnessModeCommand invalid value: %d", temp); return false; } return true; } ChargeModeCommand::ChargeModeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void ChargeModeCommand::RunGet() { Json::Value result; result["ChargeMode"] = SharedData::GetData(SharedDataType::BATTERY_STATUS); SetCommandResult("result", result); ILOG("Get chargeMode run finished"); } void ChargeModeCommand::RunSet() { SharedData::SetData(SharedDataType::BATTERY_STATUS, static_cast(atoi(args["ChargeMode"].asString().data()))); Json::Value result = true; SetCommandResult("result", result); ILOG("Set chargeMode run finished, the value is: %s", args["ChargeMode"].asString().c_str()); } bool ChargeModeCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("ChargeMode")) { ELOG("Invalid number of arguments!"); return false; } if (!std::regex_match(args["ChargeMode"].asString().data(), std::regex("\\d"))) { ELOG("Invalid arguments!"); return false; } uint8_t temp = ToUint8(args["ChargeMode"].asString()); if (!SharedData::IsValid(SharedDataType::BATTERY_STATUS, temp)) { ELOG("ChargeModeCommand invalid value: %d", temp); return false; } return true; } BrightnessCommand::BrightnessCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void BrightnessCommand::RunGet() { Json::Value result; result["Brightness"] = SharedData::GetData(SharedDataType::BRIGHTNESS_VALUE); SetCommandResult("result", result); ILOG("Get brightness run finished"); } void BrightnessCommand::RunSet() { SharedData::SetData(SharedDataType::BRIGHTNESS_VALUE, static_cast(atoi(args["Brightness"].asString().data()))); Json::Value result = true; SetCommandResult("result", result); ILOG("Set brightness run finished, the value is: %s", args["Brightness"].asString().c_str()); } bool BrightnessCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("Brightness")) { ELOG("Invalid number of arguments!"); return false; } if (!std::regex_match(args["Brightness"].asString().data(), std::regex("\\d+"))) { ELOG("Invalid arguments!"); return false; } uint8_t temp = ToUint8(args["Brightness"].asString()); if (!SharedData::IsValid(SharedDataType::BRIGHTNESS_VALUE, temp)) { ELOG("BrightnessCommand invalid value: ", temp); return false; } return true; } HeartRateCommand::HeartRateCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void HeartRateCommand::RunGet() { Json::Value result; result["HeartRate"] = SharedData::GetData(SharedDataType::HEARTBEAT_VALUE); SetCommandResult("result", result); ILOG("Get heartRate run finished"); } void HeartRateCommand::RunSet() { SharedData::SetData(SharedDataType::HEARTBEAT_VALUE, static_cast(atoi(args["HeartRate"].asString().data()))); Json::Value result = true; SetCommandResult("result", result); ILOG("Set heartRate run finished, the value is: %s", args["HeartRate"].asString().c_str()); } bool HeartRateCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("HeartRate")) { ELOG("Invalid number of arguments!"); return false; } if (!std::regex_match(args["HeartRate"].asString().data(), std::regex("\\d+"))) { ELOG("Invalid arguments!"); return false; } if (atoi(args["HeartRate"].asString().data()) > UINT8_MAX) { ELOG("Invalid arguments!"); return false; } uint8_t temp = ToUint8(args["HeartRate"].asString()); if (!SharedData::IsValid(SharedDataType::HEARTBEAT_VALUE, temp)) { ELOG("HeartRateCommand invalid value: %d", temp); return false; } return true; } StepCountCommand::StepCountCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void StepCountCommand::RunGet() { Json::Value result; result["StepCount"] = SharedData::GetData(SharedDataType::SUMSTEP_VALUE); SetCommandResult("result", result); ILOG("Get stepCount run finished"); } void StepCountCommand::RunSet() { SharedData::SetData(SharedDataType::SUMSTEP_VALUE, static_cast(atoi(args["StepCount"].asString().data()))); Json::Value result = true; SetCommandResult("result", result); ILOG("Set stepCount run finished, the value is: %s", args["StepCount"].asString().c_str()); } bool StepCountCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("StepCount")) { ELOG("Invalid number of arguments!"); return false; } if (!std::regex_match(args["StepCount"].asString().data(), std::regex("\\d+"))) { ELOG("Invalid arguments!"); return false; } uint32_t temp = ToUint8(args["StepCount"].asString()); if (!SharedData::IsValid(SharedDataType::SUMSTEP_VALUE, temp)) { ELOG("StepCountCommand invalid value: %d", temp); return false; } return true; } InspectorJSONTree::InspectorJSONTree(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void InspectorJSONTree::RunAction() { ILOG("GetJsonTree run!"); std::string str = JsAppImpl::GetInstance().GetJSONTree(); if (str == "null") { str = "{\"children\":\"empty json tree\"}"; } SetCommandResult("result", str); ILOG("SendJsonTree end!"); } InspectorDefault::InspectorDefault(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void InspectorDefault::RunAction() { ILOG("GetDefaultJsonTree run!"); std::string str = JsAppImpl::GetInstance().GetDefaultJSONTree(); SetCommandResult("result", str); ILOG("SendDefaultJsonTree end!"); } ExitCommand::ExitCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void ExitCommand::RunAction() { ILOG("ExitCommand run."); Json::Value res = true; SetCommandResult("result", res); SendResult(); Interrupter::Interrupt(); ILOG("Ready to exit"); } DeviceTypeCommand::DeviceTypeCommand(CommandLine::CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void DeviceTypeCommand::RunSet() {} ResolutionCommand::ResolutionCommand(CommandLine::CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void ResolutionCommand::RunSet() {} BackClickedCommand::BackClickedCommand(CommandLine::CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void BackClickedCommand::RunAction() { MouseInputImpl::GetInstance().DispatchOsBackEvent(); ILOG("BackClickCommand run"); Json::Value res = true; SetCommandResult("result", res); ILOG("BackClickCommand end"); } RestartCommand::RestartCommand(CommandLine::CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void RestartCommand::RunAction() { ILOG("RestartCommand start"); JsAppImpl::GetInstance().Restart(); Json::Value res = true; SetCommandResult("result", res); ILOG("RestartCommand end"); } FastPreviewMsgCommand::FastPreviewMsgCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void FastPreviewMsgCommand::RunGet() { Json::Value resultContent; std::string fastPreviewMsg = VirtualScreenImpl::GetInstance().GetFastPreviewMsg(); resultContent["FastPreviewMsg"] = fastPreviewMsg; SetResultToManager("args", resultContent, "MemoryRefresh"); ILOG("Get FastPreviewMsgCommand run finished."); } DropFrameCommand::DropFrameCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } bool DropFrameCommand::IsSetArgValid() const { if (args.isNull() || !args.isMember("frequency") || !args["frequency"].isInt()) { ELOG("Invalid DropFrame of arguments!"); return false; } if (args["frequency"].asInt() < 0) { ELOG("DropFrame param frequency must greater than or equal to 0"); return false; } return true; } void DropFrameCommand::RunSet() { ILOG("Set DropFrame frequency start."); int frequency = args["frequency"].asInt(); VirtualScreenImpl::GetInstance().SetDropFrameFrequency(frequency); SetCommandResult("result", true); ILOG("Set DropFrame frequency: %sms.", frequency); } bool KeyPressCommand::IsActionArgValid() const { if (args.isNull() || !args.isMember("isInputMethod") || !args["isInputMethod"].isBool()) { ELOG("Param isInputMethod's value is invalid."); return false; } bool isInputMethod = args["isInputMethod"].asBool(); if (isInputMethod) { return IsImeArgsValid(); } else { return IsKeyArgsValid(); } } bool KeyPressCommand::IsImeArgsValid() const { if (!args.isMember("codePoint") || !args["codePoint"].isInt()) { ELOG("Param codePoint's value is invalid."); return false; } return true; } bool KeyPressCommand::IsKeyArgsValid() const { if (!args.isMember("keyCode") || !args["keyCode"].isInt() || !args["keyAction"].isInt() || !args.isMember("keyAction") || !args["keyAction"].isInt() || !args.isMember("pressedCodes") || !args["pressedCodes"].isArray() || args["pressedCodes"].size() < 1 || (args.isMember("keyString") && !args["keyString"].isString())) { ELOG("Param keyEvent's value is invalid."); return false; } if (!args.isMember("keyString")) { ILOG("Param keyString is lost, it will be empty string."); } if (args["keyAction"].asInt() < minActionVal || args["keyAction"].asInt() > maxActionVal) { ELOG("Param keyAction's value is invalid,value range %d-%d.", minActionVal, maxActionVal); return false; } int keyCode = args["keyCode"].asInt(); if (keyCode > maxKeyVal || keyCode < minKeyVal) { ELOG("Param pressedCode value is invalid,value range %d-%d.", minKeyVal, maxKeyVal); return false; } Json::Value arrayNum = args["pressedCodes"]; for (unsigned int i = 0; i < arrayNum.size(); i++) { if (!arrayNum[i].isInt()) { ELOG("Param pressedCodes's value is invalid."); return false; } int pressedCode = arrayNum[i].asInt(); if (pressedCode > maxKeyVal || pressedCode < minKeyVal) { ELOG("Param pressedCode value is invalid,value range %d-%d.", minKeyVal, maxKeyVal); return false; } } return true; } KeyPressCommand::KeyPressCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket) : CommandLine(commandType, arg, socket) { } void KeyPressCommand::RunAction() { if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) { return; } bool isInputMethod = args["isInputMethod"].asBool(); if (isInputMethod) { VirtualScreen::inputMethodCountPerMinute++; unsigned int codePoint = args["codePoint"].asInt(); KeyInputImpl::GetInstance().SetCodePoint(codePoint); KeyInputImpl::GetInstance().DispatchOsInputMethodEvent(); } else { VirtualScreen::inputKeyCountPerMinute++; int32_t keyCode = args["keyCode"].asInt(); int32_t keyAction = args["keyAction"].asInt(); Json::Value pressedCodes = args["pressedCodes"]; vector pressedCodesVec; for (unsigned int i = 0; i < pressedCodes.size(); i++) { pressedCodesVec.push_back(pressedCodes[i].asInt()); } string keyString = ""; if (args.isMember("keyString") && args["keyString"].isString()) { keyString = args["keyString"].asString(); } KeyInputImpl::GetInstance().SetKeyEvent(keyCode, keyAction, pressedCodesVec, keyString); KeyInputImpl::GetInstance().DispatchOsKeyEvent(); } SetCommandResult("result", true); ILOG("KeyPressCommand run finished."); }