diff --git a/common/include/constants_dinput.h b/common/include/constants_dinput.h index 4c10da5..84c071e 100644 --- a/common/include/constants_dinput.h +++ b/common/include/constants_dinput.h @@ -41,6 +41,9 @@ namespace DistributedInput { const uint32_t READ_SLEEP_TIME_MS = 50; const uint32_t READ_RETRY_MAX = 5; const uint32_t DHID_LENGTH_MAX = 256; + const uint32_t DEVID_LENGTH_MAX = 256; + const uint32_t SCREEN_MSG_MAX = 40 * 1024 * 1024; + /* * Device Type definitions */ diff --git a/common/include/dinput_errcode.h b/common/include/dinput_errcode.h index b5ee118..8d0c911 100644 --- a/common/include/dinput_errcode.h +++ b/common/include/dinput_errcode.h @@ -106,6 +106,7 @@ namespace DistributedInput { constexpr int32_t ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REG_CALLBACK_ERR = -65036; constexpr int32_t ERR_DH_INPUT_SERVER_SOURCE_MANAGER_SIMULATION_EVENT_CALLBACK_ERR = -65037; constexpr int32_t ERR_DH_INPUT_SERVER_SOURCE_MANAGER_DH_FWK_KIT_IS_NULL = -65038; + constexpr int32_t ERR_DH_INPUT_SERVER_SOURCE_MANAGER_NODE_LISTENER_CALLBACK_ERR = -65039; // handler error code constexpr int32_t ERR_DH_INPUT_SINK_HANDLER_INIT_SINK_SA_FAIL = -66000; diff --git a/common/include/input_check_param.cpp b/common/include/input_check_param.cpp new file mode 100644 index 0000000..62cc46b --- /dev/null +++ b/common/include/input_check_param.cpp @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2021-2022 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 "constants_dinput.h" +#include "distributed_hardware_log.h" + +#include "input_check_param.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +DinputCheckParam &DinputCheckParam::GetInstance(void) +{ + static DinputCheckParam instance; + return instance; +} +bool DinputCheckParam::CheckParam(const std::string &deviceId, sptr callback) +{ + if (deviceId.empty() || deviceId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam deviceId is empty or deviceId size too long."); + return false; + } + if (callback == nullptr) { + DHLOGE("CheckParam callback is null."); + return false; + } + return true; +} + +bool DinputCheckParam::CheckParam(const std::string& deviceId, const uint32_t& inputTypes, + sptr callback) +{ + if (deviceId.empty() || deviceId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam deviceId is empty or deviceId size too long."); + return false; + } + if (inputTypes > static_cast(DInputDeviceType::ALL) || + inputTypes == static_cast(DInputDeviceType::NONE) || + !(inputTypes & static_cast(DInputDeviceType::ALL))) { + DHLOGE("CheckParam, inputTypes is invalids."); + return false; + } + if (callback == nullptr) { + DHLOGE("CheckParam callback is null."); + return false; + } + return true; +} + +bool DinputCheckParam::CheckParam(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes, + sptr callback) +{ + if (srcId.empty() || srcId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam srcId is empty or srcId size too long."); + return false; + } + if (sinkId.empty() || sinkId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam sinkId is empty or sinkId size too long."); + return false; + } + if (inputTypes > static_cast(DInputDeviceType::ALL) || + inputTypes == static_cast(DInputDeviceType::NONE) || + !(inputTypes & static_cast(DInputDeviceType::ALL))) { + DHLOGE("CheckParam, inputTypes is invalids."); + return false; + } + if (callback == nullptr) { + DHLOGE("CheckParam callback is null."); + return false; + } + return true; +} + +bool DinputCheckParam::CheckParam(const std::string &srcId, const std::string &sinkId, + sptr callback) +{ + if (srcId.empty() || srcId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam srcId is empty or srcId size too long."); + return false; + } + if (sinkId.empty() || sinkId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam sinkId is empty or sinkId size too long."); + return false; + } + if (callback == nullptr) { + DHLOGE("CheckParam callback is null."); + return false; + } + return true; +} + +bool DinputCheckParam::CheckParam(const std::string &sinkId, const std::vector &dhIds, + sptr callback) +{ + if (sinkId.empty() || sinkId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam sinkId is empty or sinkId size too long."); + return false; + } + if (dhIds.empty()) { + DHLOGE("CheckParam dhIds is empty."); + return false; + } + for (auto iter : dhIds) { + if (iter.size() > DHID_LENGTH_MAX) { + DHLOGE("CheckParam dhId size is too long."); + return false; + } + } + if (callback == nullptr) { + DHLOGE("CheckParam callback is null."); + return false; + } + return true; +} + +bool DinputCheckParam::CheckParam(const std::string &srcId, const std::string &sinkId, + const std::vector &dhIds, sptr callback) +{ + if (srcId.empty() || srcId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam srcId is empty or srcId size too long."); + return false; + } + if (sinkId.empty() || sinkId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam sinkId is empty or sinkId size too long."); + return false; + } + if (dhIds.empty()) { + DHLOGE("CheckParam dhIds is empty."); + return false; + } + for (auto iter : dhIds) { + if (iter.size() > DHID_LENGTH_MAX) { + DHLOGE("CheckParam dhId size is too long."); + return false; + } + } + if (callback == nullptr) { + DHLOGE("CheckParam callback is null."); + return false; + } + return true; +} + +bool DinputCheckParam::CheckRegisterParam(const std::string &devId, const std::string &dhId, + const std::string ¶meters, const std::shared_ptr &callback) +{ + if (devId.empty() || devId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam devId is empty or devId size too long."); + return false; + } + if (dhId.empty() || dhId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam dhId is empty or dhId size too long."); + return false; + } + if (parameters.empty()) { + DHLOGE("CheckParam parameters is empty."); + return false; + } + if (callback == nullptr) { + DHLOGE("CheckParam callback is null."); + return false; + } + return true; +} + +bool DinputCheckParam::CheckUnregisterParam(const std::string &devId, const std::string &dhId, + const std::shared_ptr &callback) +{ + if (devId.empty() || devId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam devId is empty or devId size too long."); + return false; + } + if (dhId.empty() || dhId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam dhId is empty or dhId size too long."); + return false; + } + if (callback == nullptr) { + DHLOGE("CheckParam callback is null."); + return false; + } + return true; +} +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/common/include/input_check_param.h b/common/include/input_check_param.h new file mode 100644 index 0000000..0665537 --- /dev/null +++ b/common/include/input_check_param.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021-2022 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. + */ + +#ifndef INPUT_CHECK_PARAM_H +#define INPUT_CHECK_PARAM_H + +#include + +#include "idistributed_hardware_source.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +class DinputCheckParam { +public: + static DinputCheckParam &GetInstance(void); + bool CheckParam(const std::string &deviceId, sptr callback); + bool CheckParam(const std::string &deviceId, const uint32_t &inputTypes, sptr callback); + bool CheckParam(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes, + sptr callback); + bool CheckParam(const std::string &srcId, const std::string &sinkId, sptr callback); + bool CheckParam(const std::string &sinkId, const std::vector &dhIds, + sptr callback); + bool CheckParam(const std::string &srcId, const std::string &sinkId, const std::vector &dhIds, + sptr callback); + bool CheckRegisterParam(const std::string &devId, const std::string &dhId, + const std::string ¶meters, const std::shared_ptr &callback); + bool CheckUnregisterParam(const std::string &devId, const std::string &dhId, + const std::shared_ptr &callback); + +private: + DinputCheckParam() = default; + ~DinputCheckParam() = default; +}; + +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS + +#endif // INPUT_CHECK_PARAM_H \ No newline at end of file diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index 1dee5b2..aade305 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -1102,11 +1102,11 @@ void InputHub::HandleTouchScreenEvent(struct input_event readBuffer[], const siz for (size_t j = iter.first; j <= iter.second; j++) { struct input_event &iev = readBuffer[j]; if (iev.code == ABS_X || iev.code == ABS_MT_POSITION_X) { - absInfo.absX = iev.value; + absInfo.absX = (uint32_t)iev.value; absInfo.absXIndex = (int32_t)j; } if (iev.code == ABS_Y || iev.code == ABS_MT_POSITION_Y) { - absInfo.absY = iev.value; + absInfo.absY = (uint32_t)iev.value; absInfo.absYIndex = (int32_t)j; } } diff --git a/common/include/input_hub.h b/common/include/input_hub.h index 34a9d9b..f8849b2 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -80,8 +80,8 @@ private: }; struct AbsInfo { - int32_t absX; - int32_t absY; + uint32_t absX; + uint32_t absY; int32_t absXIndex; int32_t absYIndex; }; diff --git a/common/include/white_list_util.cpp b/common/include/white_list_util.cpp index 671f7c7..73dfcb8 100644 --- a/common/include/white_list_util.cpp +++ b/common/include/white_list_util.cpp @@ -15,6 +15,7 @@ #include "white_list_util.h" +#include #include #include #include @@ -34,6 +35,11 @@ namespace { const int32_t COMB_KEY_VEC_MIN_LEN = 2; const int32_t LAST_KEY_ACTION_LEN = 1; const int32_t LAST_KEY_LEN = 1; + const int32_t MAX_LINE_NUM = 100; + const int32_t MAX_CHAR_PER_LINE_NUM = 100; + const int32_t MAX_SPLIT_COMMA_NUM = 4; + const int32_t MAX_SPLIT_LINE_NUM = 12; + const int32_t MAX_KEY_CODE_NUM = 4; } WhiteListUtil::WhiteListUtil() @@ -65,9 +71,13 @@ int32_t WhiteListUtil::Init() TYPE_KEY_CODE_VEC vecKeyCode; TYPE_COMBINATION_KEY_VEC vecCombinationKey; TYPE_WHITE_LIST_VEC vecWhiteList; - std::string line; + std::size_t lineNum = 0; while (getline(inFile, line)) { + if ((++lineNum > MAX_LINE_NUM) || CheckLine(line)) { + DHLOGE("whitelist cfg file has too many lines or too complicated. lineNum is %d", lineNum); + break; + } DHLOGI("read whitelist cfg, line=%s", line.c_str()); vecKeyCode.clear(); vecCombinationKey.clear(); @@ -81,12 +91,11 @@ int32_t WhiteListUtil::Init() ReadLineDataStepOne(column, vecKeyCode, vecCombinationKey); } - if (!line.empty()) { + if (CheckIsNumber(line)) { int32_t keyCode = std::stoi(line); if (keyCode != 0) { vecKeyCode.push_back(keyCode); } - if (!vecKeyCode.empty()) { vecCombinationKey.push_back(vecKeyCode); vecKeyCode.clear(); @@ -98,20 +107,47 @@ int32_t WhiteListUtil::Init() vecCombinationKey.clear(); } } - inFile.close(); std::string localNetworkId = GetLocalDeviceInfo().networkId; if (!localNetworkId.empty()) { SyncWhiteList(localNetworkId, vecWhiteList); - } else { - DHLOGE("query local network id from softbus failed"); } - - DHLOGI("Local WhiteListUtil init success"); return DH_SUCCESS; } +bool WhiteListUtil::CheckLine(const std::string &line) const +{ + if (line.size() > MAX_CHAR_PER_LINE_NUM) { + DHLOGE("This line is too long, size is %d", line.size()); + return false; + } + if (std::count(line.begin(), line.end(), SPLIT_COMMA[0]) > MAX_SPLIT_COMMA_NUM) { + DHLOGE("This line %s has too many SPLIT_COMMA", line.c_str()); + return false; + } + if (std::count(line.begin(), line.end(), SPLIT_LINE[0]) > MAX_SPLIT_LINE_NUM) { + DHLOGE("This line %s has too many SPLIT_LINE", line.c_str()); + return false; + } + return true; +} + +bool WhiteListUtil::CheckIsNumber(const std::string &str) const +{ + if (str.empty() || str.size() > MAX_KEY_CODE_NUM) { + DHLOGE("KeyCode size %d, is zero or too long.", str.size()); + return false; + } + for (char const &c : str) { + if (std::isdigit(c) == 0) { + DHLOGE("Check KeyCode format fail, %s.", str.c_str()); + return false; + } + } + return true; +} + void WhiteListUtil::ReadLineDataStepOne(std::string &column, TYPE_KEY_CODE_VEC &vecKeyCode, TYPE_COMBINATION_KEY_VEC &vecCombinationKey) const { @@ -121,7 +157,7 @@ void WhiteListUtil::ReadLineDataStepOne(std::string &column, TYPE_KEY_CODE_VEC & column = column.substr(pos2 + 1, column.size()); pos2 = column.find(SPLIT_LINE); - if (!single.empty()) { + if (CheckIsNumber(single)) { int32_t keyCode = std::stoi(single); if (keyCode != 0) { vecKeyCode.push_back(keyCode); @@ -129,7 +165,7 @@ void WhiteListUtil::ReadLineDataStepOne(std::string &column, TYPE_KEY_CODE_VEC & } } - if (!column.empty()) { + if (CheckIsNumber(column)) { int32_t keyCode = std::stoi(column); if (keyCode != 0) { vecKeyCode.push_back(keyCode); diff --git a/common/include/white_list_util.h b/common/include/white_list_util.h index 8771665..ea0e926 100644 --- a/common/include/white_list_util.h +++ b/common/include/white_list_util.h @@ -68,6 +68,8 @@ private: void GetAllComb(TYPE_COMBINATION_KEY_VEC vecs, WhiteListItemHash hash, int32_t targetLen, std::unordered_set &hashSets); std::string GetBusinessEventHash(const BusinessEvent &event); + bool CheckLine(const std::string &line) const; + bool CheckIsNumber(const std::string &str) const; private: TYPE_DEVICE_WHITE_LIST_MAP mapDeviceWhiteList_; std::map> combKeysHashMap_; diff --git a/interfaces/inner_kits/BUILD.gn b/interfaces/inner_kits/BUILD.gn index 95936c4..95d46c3 100644 --- a/interfaces/inner_kits/BUILD.gn +++ b/interfaces/inner_kits/BUILD.gn @@ -43,6 +43,7 @@ ohos_shared_library("libdinput_sdk") { sources = [ "${common_path}/include/white_list_util.cpp", + "${common_path}/include/input_check_param.cpp", "${innerkits_path}/src/distributed_input_kit.cpp", "${ipc_path}/src/add_white_list_infos_call_back_proxy.cpp", "${ipc_path}/src/add_white_list_infos_call_back_stub.cpp", diff --git a/interfaces/ipc/src/distributed_input_client.cpp b/interfaces/ipc/src/distributed_input_client.cpp index 2c068ec..ce32829 100644 --- a/interfaces/ipc/src/distributed_input_client.cpp +++ b/interfaces/ipc/src/distributed_input_client.cpp @@ -26,6 +26,7 @@ #include "dinput_errcode.h" #include "dinput_utils_tool.h" #include "distributed_input_source_proxy.h" +#include "input_check_param.h" #include "softbus_bus_center.h" #include "white_list_util.h" @@ -324,18 +325,15 @@ int32_t DistributedInputClient::ReleaseSink() int32_t DistributedInputClient::RegisterDistributedHardware(const std::string& devId, const std::string& dhId, const std::string& parameters, const std::shared_ptr& callback) { - DHLOGI("RegisterDistributedHardware called, deviceId: %s, dhId: %s, parameters: %s", + DHLOGI("DinputRegister called, deviceId: %s, dhId: %s, parameters: %s.", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), SetAnonyId(parameters).c_str()); - if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("RegisterDistributedHardware client fail"); + DHLOGE("DinputRegister client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (devId.empty() || dhId.empty() || parameters.empty() || !IsJsonData(parameters) || callback == nullptr) { + if (!DinputCheckParam::GetInstance().CheckRegisterParam(devId, dhId, parameters, callback)) { return ERR_DH_INPUT_CLIENT_REGISTER_FAIL; } - { std::lock_guard lock(DistributedInputClient::GetInstance().operationMutex_); for (auto iter : dHardWareFwkRstInfos) { @@ -343,11 +341,7 @@ int32_t DistributedInputClient::RegisterDistributedHardware(const std::string& d return ERR_DH_INPUT_CLIENT_REGISTER_FAIL; } } - - DHardWareFwkRegistInfo info; - info.devId = devId; - info.dhId = dhId; - info.callback = callback; + DHardWareFwkRegistInfo info {devId, dhId, callback}; dHardWareFwkRstInfos.push_back(info); } @@ -358,18 +352,15 @@ int32_t DistributedInputClient::RegisterDistributedHardware(const std::string& d int32_t DistributedInputClient::UnregisterDistributedHardware(const std::string& devId, const std::string& dhId, const std::shared_ptr& callback) { - DHLOGI("UnregisterDistributedHardware called, deviceId: %s, dhId: %s", + DHLOGI("DinputUnregister called, deviceId: %s, dhId: %s.", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); - if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("UnregisterDistributedHardware client fail"); + DHLOGE("DinputUnregister client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (devId.empty() || dhId.empty() || callback == nullptr) { + if (!DinputCheckParam::GetInstance().CheckUnregisterParam(devId, dhId, callback)) { return ERR_DH_INPUT_CLIENT_UNREGISTER_FAIL; } - { std::lock_guard lock(DistributedInputClient::GetInstance().operationMutex_); for (auto iter : dHardWareFwkUnRstInfos) { @@ -377,11 +368,7 @@ int32_t DistributedInputClient::UnregisterDistributedHardware(const std::string& return ERR_DH_INPUT_CLIENT_UNREGISTER_FAIL; } } - - DHardWareFwkUnRegistInfo info; - info.devId = devId; - info.dhId = dhId; - info.callback = callback; + DHardWareFwkUnRegistInfo info {devId, dhId, callback}; dHardWareFwkUnRstInfos.push_back(info); } @@ -389,56 +376,42 @@ int32_t DistributedInputClient::UnregisterDistributedHardware(const std::string& new(std::nothrow) UnregisterDInputCb()); } -int32_t DistributedInputClient::PrepareRemoteInput( - const std::string& deviceId, sptr callback) +int32_t DistributedInputClient::PrepareRemoteInput(const std::string& deviceId, sptr callback) { - DHLOGI("PrepareRemoteInput called, deviceId: %s", GetAnonyString(deviceId).c_str()); - + DHLOGI("DinputPrepare called, deviceId: %s.", GetAnonyString(deviceId).c_str()); if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("PrepareRemoteInput client fail"); + DHLOGE("DinputPrepare client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (deviceId.empty() || callback == nullptr) { + if (!DinputCheckParam::GetInstance().CheckParam(deviceId, callback)) { return ERR_DH_INPUT_CLIENT_PREPARE_FAIL; } - return DInputSAManager::GetInstance().dInputSourceProxy_->PrepareRemoteInput(deviceId, callback); } int32_t DistributedInputClient::UnprepareRemoteInput(const std::string& deviceId, sptr callback) { - DHLOGI("UnprepareRemoteInput called, deviceId: %s", GetAnonyString(deviceId).c_str()); - + DHLOGI("DinputUnprepare called, deviceId: %s.", GetAnonyString(deviceId).c_str()); if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("PrepareRemoteInput client fail"); + DHLOGE("DinputUnprepare client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (deviceId.empty() || callback == nullptr) { - DHLOGE("UnprepareRemoteInput param error, client fail"); + if (!DinputCheckParam::GetInstance().CheckParam(deviceId, callback)) { return ERR_DH_INPUT_CLIENT_UNPREPARE_FAIL; } - return DInputSAManager::GetInstance().dInputSourceProxy_->UnprepareRemoteInput(deviceId, callback); } int32_t DistributedInputClient::StartRemoteInput( const std::string& deviceId, const uint32_t& inputTypes, sptr callback) { - DHLOGI("StartRemoteInput called, deviceId: %s, inputTypes: %d", - GetAnonyString(deviceId).c_str(), inputTypes); + DHLOGI("DinputStart called, deviceId: %s, inputTypes: %d.", GetAnonyString(deviceId).c_str(), inputTypes); if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("StartRemoteInput client fail"); + DHLOGE("DinputStart client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (deviceId.empty() || callback == nullptr || - inputTypes > static_cast(DInputDeviceType::ALL) || - inputTypes == static_cast(DInputDeviceType::NONE) || - !(inputTypes & static_cast(DInputDeviceType::ALL))) { - DHLOGE("StartRemoteInput param error, client fail"); + if (!DinputCheckParam::GetInstance().CheckParam(deviceId, inputTypes, callback)) { return ERR_DH_INPUT_CLIENT_START_FAIL; } return DInputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(deviceId, inputTypes, callback); @@ -447,79 +420,58 @@ int32_t DistributedInputClient::StartRemoteInput( int32_t DistributedInputClient::StopRemoteInput(const std::string& deviceId, const uint32_t& inputTypes, sptr callback) { - DHLOGI("StopRemoteInput called, deviceId: %s, inputTypes: %d", - GetAnonyString(deviceId).c_str(), inputTypes); + DHLOGI("DinputStop called, deviceId: %s, inputTypes: %d.", GetAnonyString(deviceId).c_str(), inputTypes); if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("StopRemoteInput client fail"); + DHLOGE("DinputStop client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (deviceId.empty() || callback == nullptr || - inputTypes > static_cast(DInputDeviceType::ALL) || - inputTypes == static_cast(DInputDeviceType::NONE) || - !(inputTypes & static_cast(DInputDeviceType::ALL))) { - DHLOGE("StopRemoteInput param error, client fail"); + if (!DinputCheckParam::GetInstance().CheckParam(deviceId, inputTypes, callback)) { return ERR_DH_INPUT_CLIENT_STOP_FAIL; } - return DInputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(deviceId, inputTypes, callback); } int32_t DistributedInputClient::StartRemoteInput(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes, sptr callback) { - DHLOGI("StartRemoteInput relay by type called, srcId: %s, sinkId: %s, inputTypes: %d", - GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str(), inputTypes); + DHLOGI("DinputStart called, srcId: %s, sinkId: %s, inputTypes: %d.", GetAnonyString(srcId).c_str(), + GetAnonyString(sinkId).c_str(), inputTypes); if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("StartRemoteInput relay type client fail"); + DHLOGE("DinputStart relay type client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (srcId.empty() || sinkId.empty() || callback == nullptr || - inputTypes > static_cast(DInputDeviceType::ALL) || - inputTypes == static_cast(DInputDeviceType::NONE) || - !(inputTypes & static_cast(DInputDeviceType::ALL))) { - DHLOGE("StartRemoteInput relay type param error, client fail"); + if (!DinputCheckParam::GetInstance().CheckParam(srcId, sinkId, inputTypes, callback)) { return ERR_DH_INPUT_CLIENT_START_FAIL; } - return DInputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(srcId, sinkId, inputTypes, callback); } int32_t DistributedInputClient::StopRemoteInput(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes, sptr callback) { - DHLOGI("StopRemoteInput relay by type called, srcId: %s, sinkId: %s, inputTypes: %d", - GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str(), inputTypes); - + DHLOGI("DinputStop called, srcId: %s, sinkId: %s, inputTypes: %d.", GetAnonyString(srcId).c_str(), + GetAnonyString(sinkId).c_str(), inputTypes); if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("StopRemoteInput relay type client fail"); + DHLOGE("DinputStop relay type client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (srcId.empty() || sinkId.empty() || callback == nullptr || - inputTypes > static_cast(DInputDeviceType::ALL) || - inputTypes == static_cast(DInputDeviceType::NONE) || - !(inputTypes & static_cast(DInputDeviceType::ALL))) { - DHLOGE("StopRemoteInput relay type param error, client fail"); + if (!DinputCheckParam::GetInstance().CheckParam(srcId, sinkId, inputTypes, callback)) { return ERR_DH_INPUT_CLIENT_STOP_FAIL; } - return DInputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(srcId, sinkId, inputTypes, callback); } int32_t DistributedInputClient::PrepareRemoteInput(const std::string &srcId, const std::string &sinkId, sptr callback) { - DHLOGI("PrepareRemoteInput relay called, srcId: %s, sinkId: %s", - GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); + DHLOGI("DinputPrepare called, srcId: %s, sinkId: %s.", GetAnonyString(srcId).c_str(), + GetAnonyString(sinkId).c_str()); if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("PrepareRemoteInput relay proxy error, client fail"); + DHLOGE("DinputPrepare relay proxy error, client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - if (srcId.empty() || sinkId.empty() || callback == nullptr) { - DHLOGE("PrepareRemoteInput relay param error, client fail"); + if (!DinputCheckParam::GetInstance().CheckParam(srcId, sinkId, callback)) { return ERR_DH_INPUT_CLIENT_PREPARE_FAIL; } return DInputSAManager::GetInstance().dInputSourceProxy_->PrepareRemoteInput(srcId, sinkId, callback); @@ -528,16 +480,13 @@ int32_t DistributedInputClient::PrepareRemoteInput(const std::string &srcId, con int32_t DistributedInputClient::UnprepareRemoteInput(const std::string &srcId, const std::string &sinkId, sptr callback) { - DHLOGI("UnprepareRemoteInput relay called, srcId: %s, sinkId: %s", - GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); - + DHLOGI("DinputUnprepare called, srcId: %s, sinkId: %s.", GetAnonyString(srcId).c_str(), + GetAnonyString(sinkId).c_str()); if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("UnprepareRemoteInput relay proxy error, client fail"); + DHLOGE("DinputUnprepare relay proxy error, client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (srcId.empty() || sinkId.empty() || callback == nullptr) { - DHLOGE("UnprepareRemoteInput relay param error, client fail"); + if (!DinputCheckParam::GetInstance().CheckParam(srcId, sinkId, callback)) { return ERR_DH_INPUT_CLIENT_UNPREPARE_FAIL; } return DInputSAManager::GetInstance().dInputSourceProxy_->UnprepareRemoteInput(srcId, sinkId, callback); @@ -546,80 +495,66 @@ int32_t DistributedInputClient::UnprepareRemoteInput(const std::string &srcId, c int32_t DistributedInputClient::StartRemoteInput(const std::string &sinkId, const std::vector &dhIds, sptr callback) { - DHLOGI("StartRemoteInput relay by dhid called, sinkId: %s", GetAnonyString(sinkId).c_str()); - + DHLOGI("DinputStart called, sinkId: %s.", GetAnonyString(sinkId).c_str()); if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("StartRemoteInput dhid proxy error, client fail"); + DHLOGE("DinputStart dhid proxy error, client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (sinkId.empty() || callback == nullptr || (dhIds.size() <= 0)) { - DHLOGE("StartRemoteInput dhid param error, client fail"); + if (!DinputCheckParam::GetInstance().CheckParam(sinkId, dhIds, callback)) { return ERR_DH_INPUT_CLIENT_START_FAIL; } - return DInputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(sinkId, dhIds, callback); } int32_t DistributedInputClient::StopRemoteInput(const std::string &sinkId, const std::vector &dhIds, sptr callback) { - DHLOGI("StopRemoteInput relay by dhid called, sinkId: %s", GetAnonyString(sinkId).c_str()); - + DHLOGI("DinputStop called, sinkId: %s.", GetAnonyString(sinkId).c_str()); if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("StopRemoteInput dhid proxy error, client fail"); + DHLOGE("DinputStop dhid proxy error, client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (sinkId.empty() || callback == nullptr || (dhIds.size() <= 0)) { - DHLOGE("StopRemoteInput dhid param error, client fail"); + if (!DinputCheckParam::GetInstance().CheckParam(sinkId, dhIds, callback)) { return ERR_DH_INPUT_CLIENT_STOP_FAIL; } - return DInputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(sinkId, dhIds, callback); } int32_t DistributedInputClient::StartRemoteInput(const std::string &srcId, const std::string &sinkId, const std::vector &dhIds, sptr callback) { - DHLOGI("StartRemoteInput relay by dhid called, srcId: %s, sinkId: %s", - GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); - + DHLOGI("DinputStart called, srcId: %s, sinkId: %s.", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("StartRemoteInput proxy error, client fail"); + DHLOGE("DinputStart proxy error, client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (srcId.empty() || sinkId.empty() || callback == nullptr || (dhIds.size() <= 0)) { - DHLOGE("StartRemoteInput param error, client fail"); + if (!DinputCheckParam::GetInstance().CheckParam(srcId, sinkId, dhIds, callback)) { return ERR_DH_INPUT_CLIENT_START_FAIL; } - return DInputSAManager::GetInstance().dInputSourceProxy_->StartRemoteInput(srcId, sinkId, dhIds, callback); } int32_t DistributedInputClient::StopRemoteInput(const std::string &srcId, const std::string &sinkId, const std::vector &dhIds, sptr callback) { - DHLOGI("StopRemoteInput relay by dhid called, srcId: %s, sinkId: %s", - GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); - + DHLOGI("DinputStop called, srcId: %s, sinkId: %s.", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { - DHLOGE("StopRemoteInput proxy error, client fail"); + DHLOGE("DinputStop proxy error, client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } - - if (srcId.empty() || sinkId.empty() || callback == nullptr || (dhIds.size() <= 0)) { - DHLOGE("StopRemoteInput param error, client fail"); + if (!DinputCheckParam::GetInstance().CheckParam(srcId, sinkId, dhIds, callback)) { return ERR_DH_INPUT_CLIENT_STOP_FAIL; } - return DInputSAManager::GetInstance().dInputSourceProxy_->StopRemoteInput(srcId, sinkId, dhIds, callback); } bool DistributedInputClient::IsNeedFilterOut(const std::string& deviceId, const BusinessEvent& event) { DHLOGI("IsNeedFilterOut called, deviceId: %s", GetAnonyString(deviceId).c_str()); + if (deviceId.empty() || (deviceId.size() > DEVID_LENGTH_MAX)) { + DHLOGE("IsNeedFilterOut param deviceId is empty."); + return false; + } return WhiteListUtil::GetInstance().IsNeedFilterOut(deviceId, event); } @@ -639,6 +574,10 @@ bool DistributedInputClient::IsTouchEventNeedFilterOut(const TouchScreenEvent &e bool DistributedInputClient::IsStartDistributedInput(const std::string& dhId) { std::lock_guard lock(sharingDhIdsMtx_); + if (dhId.empty() || (dhId.size() > DHID_LENGTH_MAX)) { + DHLOGE("IsStartDistributedInput param dhid is error."); + return false; + } return sharingDhIds_.find(dhId) != sharingDhIds_.end(); } diff --git a/services/source/sourcemanager/BUILD.gn b/services/source/sourcemanager/BUILD.gn index cb9f0e3..e8080bc 100644 --- a/services/source/sourcemanager/BUILD.gn +++ b/services/source/sourcemanager/BUILD.gn @@ -49,6 +49,7 @@ ohos_shared_library("libdinput_source") { sources = [ "${common_path}/include/white_list_util.cpp", + "${common_path}/include/input_check_param.cpp", "${ipc_path}/src/add_white_list_infos_call_back_proxy.cpp", "${ipc_path}/src/add_white_list_infos_call_back_stub.cpp", "${ipc_path}/src/del_white_list_infos_call_back_proxy.cpp", diff --git a/services/source/sourcemanager/include/distributed_input_source_manager.h b/services/source/sourcemanager/include/distributed_input_source_manager.h index acd6b5f..ccc76fa 100644 --- a/services/source/sourcemanager/include/distributed_input_source_manager.h +++ b/services/source/sourcemanager/include/distributed_input_source_manager.h @@ -164,15 +164,10 @@ public: const std::string &nodeDesc) override; virtual int32_t RegisterSimulationEventListener(sptr listener) override; virtual int32_t UnregisterSimulationEventListener(sptr listener) override; - bool CheckParameters(const std::string &deviceId, sptr callback); - bool CheckParameters(const std::string& deviceId, const uint32_t& inputTypes, sptr callback); - bool CheckParameters(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes, - sptr callback); - bool CheckParameters(const std::string &srcId, const std::string &sinkId, sptr callback); - bool CheckParameters(const std::string &sinkId, const std::vector &dhIds, - sptr callback); - bool CheckParameters(const std::string &srcId, const std::string &sinkId, const std::vector &dhIds, - sptr callback); + bool CheckRegisterParam(const std::string &devId, const std::string &dhId, + const std::string ¶meters, sptr callback); + bool CheckUnregisterParam(const std::string &devId, const std::string &dhId, + sptr callback); int32_t Dump(int32_t fd, const std::vector& args) override; class DInputSourceListener : public DInputSourceTransCallback { diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index df98d4b..55a65e3 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -44,6 +44,7 @@ #include "distributed_input_source_transport.h" #include "hisysevent_util.h" #include "hidumper.h" +#include "input_check_param.h" #include "white_list_util.h" namespace OHOS { @@ -730,22 +731,40 @@ int32_t DistributedInputSourceManager::Release() return DH_SUCCESS; } +bool DistributedInputSourceManager::CheckRegisterParam(const std::string &devId, const std::string &dhId, + const std::string ¶meters, sptr callback) +{ + if (devId.empty() || devId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam devId is empty or devId size too long."); + return false; + } + if (dhId.empty() || dhId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam dhId is empty or dhId size too long."); + return false; + } + if (parameters.empty()) { + DHLOGE("CheckParam parameters is empty."); + return false; + } + if (callback == nullptr) { + DHLOGE("CheckParam callback is null."); + return false; + } + return true; +} + int32_t DistributedInputSourceManager::RegisterDistributedHardware(const std::string& devId, const std::string& dhId, const std::string& parameters, sptr callback) { HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_REGISTER, devId, dhId, "dinput register call."); DHLOGI("RegisterDistributedHardware called, deviceId: %s, dhId: %s, parameters: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), SetAnonyId(parameters).c_str()); - if (callback == nullptr) { - DHLOGE("RegisterDistributedHardware called, deviceId: %s callback is null.", GetAnonyString(devId).c_str()); + if (!CheckRegisterParam(devId, dhId, parameters, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_REGISTER_FAIL, devId, dhId, - ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REGISTER_FAIL, - "dinput register distributed hardware failed callback is nullptr."); + ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REGISTER_FAIL, "Dinputregister failed callback is nullptr."); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_REGISTER_FAIL; } - std::lock_guard lock(operationMutex_); - DInputClientRegistInfo info {devId, dhId, callback}; regCallbacks_.push_back(info); InputDeviceId inputDeviceId {devId, dhId, GetNodeDesc(parameters)}; @@ -844,6 +863,24 @@ int32_t DistributedInputSourceManager::UnregCallbackNotify(const std::string &de return DH_SUCCESS; } +bool DistributedInputSourceManager::CheckUnregisterParam(const std::string &devId, const std::string &dhId, + sptr callback) +{ + if (devId.empty() || devId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam devId is empty or devId size too long."); + return false; + } + if (dhId.empty() || dhId.size() > DEVID_LENGTH_MAX) { + DHLOGE("CheckParam dhId is empty or dhId size too long."); + return false; + } + if (callback == nullptr) { + DHLOGE("CheckParam callback is null."); + return false; + } + return true; +} + int32_t DistributedInputSourceManager::CheckDeviceIsExists(const std::string &devId, const std::string &dhId, const InputDeviceId &inputDeviceId, std::vector::iterator &it) { @@ -877,7 +914,6 @@ int32_t DistributedInputSourceManager::DeleteInputDeviceNodeInfo(const std::stri } inputDevice_.erase(it); - std::shared_ptr jsonArrayMsg = std::make_shared(); nlohmann::json tmpJson; tmpJson[INPUT_SOURCEMANAGER_KEY_DEVID] = devId; @@ -903,16 +939,13 @@ int32_t DistributedInputSourceManager::UnregisterDistributedHardware(const std:: { HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_UNREGISTER, devId, dhId, "dinput unregister call"); DHLOGI("Unregister called, deviceId: %s, dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); - - if (callback == nullptr) { - DHLOGE("Unregister called, deviceId: %s callback is null.", GetAnonyString(devId).c_str()); + if (!CheckUnregisterParam(devId, dhId, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_UNREGISTER_FAIL, devId, dhId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL, "dinput unregister failed in callback is nullptr"); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNREGISTER_FAIL; } std::lock_guard lock(operationMutex_); - DInputClientUnregistInfo info {devId, dhId, callback}; unregCallbacks_.push_back(info); @@ -941,30 +974,18 @@ int32_t DistributedInputSourceManager::UnregisterDistributedHardware(const std:: return DH_SUCCESS; } -bool DistributedInputSourceManager::CheckParameters(const std::string &deviceId, sptr callback) -{ - if (deviceId.empty()) { - DHLOGE("DeviceId is empty."); - return false; - } - if (callback == nullptr) { - DHLOGE("Callback is null."); - return false; - } - return true; -} - int32_t DistributedInputSourceManager::PrepareRemoteInput( const std::string &deviceId, sptr callback) { StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_PREPARE_START, DINPUT_PREPARE_TASK); HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_PREPARE, deviceId, "Dinput prepare call."); - if (!CheckParameters(deviceId, callback)) { + if (!DinputCheckParam::GetInstance().CheckParam(deviceId, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL, "Dinput prepare param is faild."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_PREPARE_START, DINPUT_PREPARE_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL; } + DHLOGI("Prepare called, deviceId: %s", GetAnonyString(deviceId).c_str()); for (auto iter : preCallbacks_) { if (iter.devId == deviceId) { @@ -1011,7 +1032,7 @@ int32_t DistributedInputSourceManager::UnprepareRemoteInput( { StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_UNPREPARE_START, DINPUT_UNPREPARE_TASK); HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_UNPREPARE, deviceId, "Dinput unprepare call."); - if (!CheckParameters(deviceId, callback)) { + if (!DinputCheckParam::GetInstance().CheckParam(deviceId, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL, "Dinput unprepare param is faild."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_UNPREPARE_START, DINPUT_UNPREPARE_TASK); @@ -1049,35 +1070,18 @@ int32_t DistributedInputSourceManager::UnprepareRemoteInput( return DH_SUCCESS; } -bool DistributedInputSourceManager::CheckParameters(const std::string &deviceId, const uint32_t& inputTypes, - sptr callback) -{ - if (deviceId.empty()) { - DHLOGE("DeviceId is empty."); - return false; - } - if (inputTypes > static_cast(DInputDeviceType::ALL)) { - DHLOGE("Inputtype is invalids."); - return false; - } - if (callback == nullptr) { - DHLOGE("Callback is null."); - return false; - } - return true; -} - int32_t DistributedInputSourceManager::StartRemoteInput( const std::string& deviceId, const uint32_t& inputTypes, sptr callback) { StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK); HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_START_USE, deviceId, "Dinput start use call."); - if (!CheckParameters(deviceId, inputTypes, callback)) { + if (!DinputCheckParam::GetInstance().CheckParam(deviceId, inputTypes, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start param is faild."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL; } + DHLOGI("Start called, deviceId: %s, inputTypes: %d", GetAnonyString(deviceId).c_str(), inputTypes); for (auto iter : staCallbacks_) { if (iter.devId == deviceId && iter.inputTypes == inputTypes) { @@ -1116,12 +1120,13 @@ int32_t DistributedInputSourceManager::StopRemoteInput( { StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_STOP_USE, deviceId, "Dinput stop use call"); - if (!CheckParameters(deviceId, inputTypes, callback)) { + if (!DinputCheckParam::GetInstance().CheckParam(deviceId, inputTypes, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, deviceId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop param is faild."); - FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); + FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL; } + DHLOGI("Stop called, deviceId: %s, inputTypes: %d", GetAnonyString(deviceId).c_str(), inputTypes); for (auto iter : stpCallbacks_) { if (iter.devId == deviceId && iter.inputTypes == inputTypes) { @@ -1154,45 +1159,27 @@ int32_t DistributedInputSourceManager::StopRemoteInput( return DH_SUCCESS; } -bool DistributedInputSourceManager::CheckParameters(const std::string &srcId, const std::string &sinkId, - const uint32_t &inputTypes, sptr callback) -{ - if (srcId.empty() || sinkId.empty()) { - DHLOGE("SrcId=%d or sinkId=%d is empty.", srcId.empty(), sinkId.empty()); - return false; - } - if (inputTypes > static_cast(DInputDeviceType::ALL)) { - DHLOGE("Inputtype is invalids."); - return false; - } - if (callback == nullptr) { - DHLOGE("Callback is null."); - return false; - } - return true; -} int32_t DistributedInputSourceManager::StartRemoteInput(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes, sptr callback) { StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK); HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_START_USE, sinkId, "Dinput start use call."); - if (!CheckParameters(srcId, sinkId, inputTypes, callback)) { + if (!DinputCheckParam::GetInstance().CheckParam(srcId, sinkId, inputTypes, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, - ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput stop param is faild."); + ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start param is faild."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL; } + DHLOGI("StartRemoteInput called, srcId: %s, sinkId: %s, inputTypes: %d", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str(), inputTypes); std::string localNetworkId = GetLocalNetworkId(); if (localNetworkId.empty()) { - DHLOGE("StartRemoteInput called, Could not get local device id."); HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start use failed in get local networkId error."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL; } - DHLOGI("StartRemoteInput called, device local networkId is %s", GetAnonyString(localNetworkId).c_str()); if (srcId != localNetworkId) { return RelayStartRemoteInputByType(srcId, sinkId, inputTypes, callback); } @@ -1231,7 +1218,7 @@ int32_t DistributedInputSourceManager::StopRemoteInput(const std::string &srcId, { StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_STOP_USE, sinkId, "Dinput stop use call."); - if (!CheckParameters(srcId, sinkId, inputTypes, callback)) { + if (!DinputCheckParam::GetInstance().CheckParam(srcId, sinkId, inputTypes, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop param is faild."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); @@ -1241,13 +1228,11 @@ int32_t DistributedInputSourceManager::StopRemoteInput(const std::string &srcId, GetAnonyString(sinkId).c_str(), inputTypes); std::string localNetworkId = GetLocalNetworkId(); if (localNetworkId.empty()) { - DHLOGE("StopRemoteInput called, Could not get local device id."); HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop use failed in get networkId."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL; } - DHLOGI("StopRemoteInput called, device local networkId is %s", GetAnonyString(localNetworkId).c_str()); if (srcId != localNetworkId) { return RelayStopRemoteInputByType(srcId, sinkId, inputTypes, callback); } @@ -1332,36 +1317,20 @@ int32_t DistributedInputSourceManager::RelayStopRemoteInputByType( return dInputSourceProxy->StopRemoteInput(srcId, sinkId, inputTypes, callback); } -bool DistributedInputSourceManager::CheckParameters(const std::string &srcId, const std::string &sinkId, - sptr callback) -{ - if (srcId.empty() || sinkId.empty()) { - DHLOGE("SrcId=%d or sinkId=%d is empty.", srcId.empty(), sinkId.empty()); - return false; - } - if (callback == nullptr) { - DHLOGE("Callback is null."); - return false; - } - return true; -} - int32_t DistributedInputSourceManager::PrepareRemoteInput(const std::string &srcId, const std::string &sinkId, sptr callback) { - if (!CheckParameters(srcId, sinkId, callback)) { + DHLOGI("Dinput prepare, srcId: %s, sinkId: %s", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); + if (!DinputCheckParam::GetInstance().CheckParam(srcId, sinkId, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL, "Dinput prepare param is faild."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL; } - DHLOGI("srcId: %s, sinkId: %s", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); std::string localNetworkId = GetLocalNetworkId(); if (localNetworkId.empty()) { - DHLOGE("Could not get local device id."); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_PREPARE_FAIL; } - DHLOGI("device local networkId is %s", GetAnonyString(localNetworkId).c_str()); if (srcId != localNetworkId) { return RelayPrepareRemoteInput(srcId, sinkId, callback); } @@ -1397,19 +1366,17 @@ int32_t DistributedInputSourceManager::PrepareRemoteInput(const std::string &src int32_t DistributedInputSourceManager::UnprepareRemoteInput(const std::string &srcId, const std::string &sinkId, sptr callback) { - if (!CheckParameters(srcId, sinkId, callback)) { + DHLOGI("Dinput unprepare, srcId: %s, sinkId: %s", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); + if (!DinputCheckParam::GetInstance().CheckParam(srcId, sinkId, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL, "Dinput unprepare param is faild."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL; } - DHLOGI("srcId: %s, sinkId: %s", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); std::string localNetworkId = GetLocalNetworkId(); if (localNetworkId.empty()) { - DHLOGE("Could not get local device id."); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_UNPREPARE_FAIL; } - DHLOGI("device local localNetworkId is %s", GetAnonyString(localNetworkId).c_str()); if (srcId != localNetworkId) { return RelayUnprepareRemoteInput(srcId, sinkId, callback); } @@ -1456,51 +1423,25 @@ bool DistributedInputSourceManager::IsStringDataSame(const std::vector &dhIds, - sptr callback) -{ - if (sinkId.empty()) { - DHLOGE("SinkId is empty."); - return false; - } - if (dhIds.empty()) { - DHLOGE("DhIds is empty."); - return false; - } - for (auto iter : dhIds) { - if (iter.size() > DHID_LENGTH_MAX) { - DHLOGE("DhId length is too long."); - return false; - } - } - if (callback == nullptr) { - DHLOGE("Callback is null."); - return false; - } - return true; -} - int32_t DistributedInputSourceManager::StartRemoteInput(const std::string &sinkId, const std::vector &dhIds, sptr callback) { StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK); HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_START_USE, sinkId, "dinput start use call"); - if (!CheckParameters(sinkId, dhIds, callback)) { + DHLOGI("Dinput start, sinkId: %s, vector.string.size: %d", GetAnonyString(sinkId).c_str(), dhIds.size()); + if (!DinputCheckParam::GetInstance().CheckParam(sinkId, dhIds, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start param is faild."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL; } - DHLOGI("sinkId: %s, vector.string.size: %d", GetAnonyString(sinkId).c_str(), dhIds.size()); std::string localNetworkId = GetLocalNetworkId(); if (localNetworkId.empty()) { - DHLOGE("Could not get local device id."); HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "dinput start use failed in get networkId"); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL; } - DHLOGI("device local networkId is %s", GetAnonyString(localNetworkId).c_str()); // current device is source device for (auto iter : staStringCallbacks_) { @@ -1538,22 +1479,20 @@ int32_t DistributedInputSourceManager::StopRemoteInput(const std::string &sinkId { StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_STOP_USE, sinkId, "dinput stop use call"); - if (!CheckParameters(sinkId, dhIds, callback)) { + DHLOGI("Dinput stop, sinkId: %s, vector.string.size: %d", GetAnonyString(sinkId).c_str(), dhIds.size()); + if (!DinputCheckParam::GetInstance().CheckParam(sinkId, dhIds, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop param is faild."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL; } - DHLOGI("sinkId: %s, vector.string.size: %d", GetAnonyString(sinkId).c_str(), dhIds.size()); std::string localNetworkId = GetLocalNetworkId(); if (localNetworkId.empty()) { - DHLOGE("Could not get local device id."); HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "dinput stop use failed in get networkId"); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL; } - DHLOGI("device local networkId is %s", GetAnonyString(localNetworkId).c_str()); for (auto iter : stpStringCallbacks_) { if (iter.sinkId == sinkId && IsStringDataSame(iter.dhIds, dhIds)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, @@ -1583,51 +1522,25 @@ int32_t DistributedInputSourceManager::StopRemoteInput(const std::string &sinkId return DH_SUCCESS; } -bool DistributedInputSourceManager::CheckParameters(const std::string &srcId, const std::string &sinkId, - const std::vector &dhIds, sptr callback) -{ - if (srcId.empty() || sinkId.empty()) { - DHLOGE("SrcId=%d or sinkId=%d is empty.", srcId.empty(), sinkId.empty()); - return false; - } - if (dhIds.empty()) { - DHLOGE("DhIds is empty."); - return false; - } - for (auto iter : dhIds) { - if (iter.size() > DHID_LENGTH_MAX) { - DHLOGE("DhId length is too long."); - return false; - } - } - if (callback == nullptr) { - DHLOGE("Callback is null."); - return false; - } - return true; -} - int32_t DistributedInputSourceManager::StartRemoteInput(const std::string &srcId, const std::string &sinkId, const std::vector &dhIds, sptr callback) { StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK); HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_START_USE, sinkId, "Dinput start use call."); - if (!CheckParameters(srcId, sinkId, dhIds, callback)) { + DHLOGI("Dinput start, srcId: %s, sinkId: %s", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); + if (!DinputCheckParam::GetInstance().CheckParam(srcId, sinkId, dhIds, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start param is faild."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL; } - DHLOGI("SrcId: %s, sinkId: %s", GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str()); std::string localNetworkId = GetLocalNetworkId(); if (localNetworkId.empty()) { - DHLOGE("Could not get local device id."); HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL, "Dinput start use failed in get networkId."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL; } - DHLOGI("device local networkId is %s", GetAnonyString(localNetworkId).c_str()); if (srcId != localNetworkId) { return RelayStartRemoteInputByDhid(srcId, sinkId, dhIds, callback); } @@ -1666,23 +1579,21 @@ int32_t DistributedInputSourceManager::StopRemoteInput(const std::string &srcId, { StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_STOP_USE, sinkId, "Dinput stop use call."); - if (!CheckParameters(srcId, sinkId, dhIds, callback)) { + DHLOGI("Dinput stop, srcId: %s, sinkId: %s, vector.string.size: %d", + GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str(), dhIds.size()); + if (!DinputCheckParam::GetInstance().CheckParam(srcId, sinkId, dhIds, callback)) { HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop param is faild."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL; } - DHLOGI("srcId: %s, sinkId: %s, vector.string.size: %d", - GetAnonyString(srcId).c_str(), GetAnonyString(sinkId).c_str(), dhIds.size()); std::string localNetworkId = GetLocalNetworkId(); if (localNetworkId.empty()) { - DHLOGE("Could not get local device id."); HisyseventUtil::GetInstance().SysEventWriteFault(DINPUT_OPT_FAIL, sinkId, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL, "Dinput stop use failed in get networkId."); FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK); return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL; } - DHLOGI("device local networkId is %s", GetAnonyString(localNetworkId).c_str()); if (srcId != localNetworkId) { return RelayStopRemoteInputByDhid(srcId, sinkId, dhIds, callback); } @@ -1742,6 +1653,10 @@ int32_t DistributedInputSourceManager::RegisterDelWhiteListCallback(sptr listener) { DHLOGI("RegisterInputNodeListener, addr: %p", &listener); + if (listener == nullptr) { + DHLOGE("RegisterInputNodeListener callback is null."); + return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_NODE_LISTENER_CALLBACK_ERR; + } DistributedInputInject::GetInstance().RegisterInputNodeListener(listener); SendExistVirNodeInfos(listener); return DH_SUCCESS; @@ -1750,6 +1665,10 @@ int32_t DistributedInputSourceManager::RegisterInputNodeListener(sptr listener) { DHLOGI("UnregisterInputNodeListener, addr: %p", &listener); + if (listener == nullptr) { + DHLOGE("UnregisterInputNodeListener callback is null."); + return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_NODE_LISTENER_CALLBACK_ERR; + } DistributedInputInject::GetInstance().UnregisterInputNodeListener(listener); return DH_SUCCESS; } @@ -2023,7 +1942,6 @@ void DistributedInputSourceManager::RunStartDhidCallback(const std::string &sink dhidsVec.size()); std::string localNetWorkId = GetLocalNetworkId(); if (localNetWorkId.empty()) { - DHLOGE("Could not get local device id."); return; } @@ -2044,7 +1962,6 @@ void DistributedInputSourceManager::RunStopDhidCallback(const std::string &sinkI StringSplitToVector(dhIds, INPUT_STRING_SPLIT_POINT, dhidsVec); std::string localNetworkId = GetLocalNetworkId(); if (localNetworkId.empty()) { - DHLOGE("Could not get local device id."); return; } @@ -2242,6 +2159,10 @@ void DistributedInputSourceManager::StartDScreenListener::OnMessage(const DHTopi DHLOGE("this topic is wrong, %d", static_cast(topic)); return; } + if (message.size() > SCREEN_MSG_MAX) { + DHLOGE("StartDScreenListener message size too long."); + return; + } std::string sinkDevId = ""; SrcScreenInfo srcScreenInfo = {}; int32_t parseRes = ParseMessage(message, sinkDevId, srcScreenInfo); diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index f221ead..531d68d 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -55,10 +55,12 @@ DevInfo GetLocalDeviceInfo() std::string GetLocalNetworkId() { - if (GetLocalDeviceInfo().networkId.empty()) { + std::string localNetworkId = GetLocalDeviceInfo().networkId; + if (localNetworkId.empty()) { DHLOGE("local networkId is empty!"); } - return GetLocalDeviceInfo().networkId; + DHLOGI("GetLocalNetworkId, device local networkId is %s", GetAnonyString(localNetworkId).c_str()); + return localNetworkId; } std::string GetUUIDBySoftBus(const std::string &networkId)