diff --git a/services/source/inputinject/src/virtual_device.cpp b/services/source/inputinject/src/virtual_device.cpp index b34f12ccde81dd110296859d2ee00ae03035ee48..3f7ad2a9381b05fc49d1b6dc5df466e437bd220d 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 8d7f5fa95a159d64716cd3be6b967a261046ec22..713cbfc49090635c1383ba80fab95cc9cb00f456 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 8944eba995677779464037527c42e83d99672cc3..2b51b7231cd8cb1bc8a8304ba8311840ca831655 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 ee6a2b5bc2eebd88402f3ccfd82ba918fa5b47e1..06e3dc6b6222923b2555a68157bf6c384e88fac1 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