From 99babb6430715a924057b335091cd12d651d57e2 Mon Sep 17 00:00:00 2001 From: li-tiangang4 Date: Sat, 30 Mar 2024 09:09:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=A6=82=E7=8E=87=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E8=8A=82=E7=82=B9=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li-tiangang4 --- services/source/inputinject/src/virtual_device.cpp | 4 +++- .../src/distributed_input_source_manager.cpp | 9 +++++++++ utils/include/dinput_utils_tool.h | 3 ++- utils/src/dinput_utils_tool.cpp | 8 ++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/services/source/inputinject/src/virtual_device.cpp b/services/source/inputinject/src/virtual_device.cpp index b34f12cc..3f7ad2a9 100644 --- a/services/source/inputinject/src/virtual_device.cpp +++ b/services/source/inputinject/src/virtual_device.cpp @@ -16,6 +16,7 @@ #include "virtual_device.h" #include +#include #include "constants_dinput.h" #include "dinput_log.h" @@ -30,6 +31,7 @@ namespace { constexpr uint32_t ABS_MAX_POS = 2; constexpr uint32_t ABS_FUZZ_POS = 3; constexpr uint32_t ABS_FLAT_POS = 4; + constexpr uint32_t SLEEP_TIME_US = 10 * 1000; } VirtualDevice::VirtualDevice(const InputDevice &event) : deviceName_(event.name), busType_(event.bus), vendorId_(event.vendor), productId_(event.product), version_(event.version), classes_(event.classes) @@ -163,7 +165,7 @@ bool VirtualDevice::SetUp(const InputDevice &inputDevice, const std::string &dev return false; } DHLOGI("create fd %{public}d", fd_); - + usleep(SLEEP_TIME_US); char sysfsDeviceName[16] = {0}; if (ioctl(fd_, UI_GET_SYSNAME(sizeof(sysfsDeviceName)), sysfsDeviceName) < 0) { DHLOGE("Unable to get input device name"); diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index 8d7f5fa9..713cbfc4 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -20,6 +20,7 @@ #include "nlohmann/json.hpp" #include "system_ability_definition.h" #include "string_ex.h" +#include #include "distributed_hardware_fwk_kit.h" #include "ipublisher_listener.h" @@ -44,6 +45,11 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { +namespace { + constexpr int32_t RAND_NUM_MIN = 0; + constexpr int32_t RAND_NUM_MAX = 20; + constexpr int32_t SLEEP_TIME_US = 1000; +} REGISTER_SYSTEM_ABILITY_BY_ID(DistributedInputSourceManager, DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID, true); DistributedInputSourceManager::DistributedInputSourceManager(int32_t saId, bool runOnCreate) @@ -265,6 +271,9 @@ int32_t DistributedInputSourceManager::RegisterDistributedHardware(const std::st } DHLOGI("RegisterDistributedHardware called, deviceId: %{public}s, dhId: %{public}s, parameters: %{public}s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), SetAnonyId(parameters).c_str()); + int32_t randNumber = GenRandInt(RAND_NUM_MIN, RAND_NUM_MAX); + usleep(randNumber * SLEEP_TIME_US); + std::lock_guard lock(regDisHardwareMutex_); DInputClientRegistInfo info {devId, dhId, callback}; regCallbacks_.push_back(info); diff --git a/utils/include/dinput_utils_tool.h b/utils/include/dinput_utils_tool.h index 8944eba9..2b51b723 100644 --- a/utils/include/dinput_utils_tool.h +++ b/utils/include/dinput_utils_tool.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 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 @@ -68,6 +68,7 @@ std::string GetString(const std::vector &vec); int32_t GetRandomInt32(); std::string JointDhIds(const std::vector &dhids); std::vector SplitDhIdString(const std::string &dhIdsString); +int32_t GenRandInt(int32_t randMin, int32_t randMax); } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index ee6a2b5b..06e3dc6b 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -464,6 +464,14 @@ std::vector SplitDhIdString(const std::string &dhIdsString) SplitStringToVector(dhIdsString, DHID_SPLIT, dhIdsVec); return dhIdsVec; } + +int32_t GenRandInt(int32_t randMin, int32_t randMax) +{ + std::random_device randDevice; + std::mt19937 genRand(randDevice()); + std::uniform_int_distribution disRand(randMin, randMax); + return disRand(genRand); +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file