From 87fc2bd3fb364f52e49fd5f8d271b014066e0f12 Mon Sep 17 00:00:00 2001 From: wangyeyu Date: Fri, 6 Jun 2025 13:40:11 +0800 Subject: [PATCH] =?UTF-8?q?1.=20on=20=E4=B9=8B=E5=90=8E=E5=8F=AA=E4=B8=8A?= =?UTF-8?q?=E6=8A=A5=E5=8F=98=E5=8C=96=E7=9A=84=E8=AE=BE=E5=A4=87=EF=BC=9B?= =?UTF-8?q?2.=E6=9F=A5=E8=AF=A2=E6=8E=A5=E5=8F=A3=E4=B8=8D=E5=8F=97?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E7=BA=BF=E5=BD=B1=E5=93=8D=EF=BC=9B3.?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E8=A1=8C=E4=B8=BA=E4=BF=AE=E6=94=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyeyu --- .../include/miscdevice_service.h | 9 +- .../src/miscdevice_service.cpp | 247 ++++++++++-------- 2 files changed, 143 insertions(+), 113 deletions(-) diff --git a/services/miscdevice_service/include/miscdevice_service.h b/services/miscdevice_service/include/miscdevice_service.h index 148b25c..4905e30 100644 --- a/services/miscdevice_service/include/miscdevice_service.h +++ b/services/miscdevice_service/include/miscdevice_service.h @@ -143,11 +143,14 @@ private: int32_t GetHapticCapacityInfo(const VibratorIdentifierIPC& identifier, VibratorCapacity& capacityInfo); int32_t GetAllWaveInfo(const VibratorIdentifierIPC& identifier, std::vector& waveInfo); int32_t GetHapticStartUpTime(const VibratorIdentifierIPC& identifier, int32_t mode, int32_t &startUpTime); - void GetLocalVibratorInfo(); + void GetOnlineVibratorInfo(); std::vector CheckDeviceIdIsValid(const VibratorIdentifierIPC& identifier); int32_t StartVibrateThreadControl(const VibratorIdentifierIPC& identifier, VibrateInfo& info); - bool UpdateVibratorAllInfo(const VibratorIdentifierIPC &identifier, const HdfVibratorPlugInfo &info, - VibratorAllInfos &vibratorAllInfos); + int32_t InsertVibratorInfo(int deviceId, const std::string &deviceName, + const std::vector &vibratorInfo); + int32_t GetLocalDeviceId(int32_t &deviceId); + int32_t GetOneVibrator(const VibratorIdentifierIPC& actIdentifier, + std::vector& vibratorInfoIPC); void ConvertToServerInfos(const std::vector &baseVibratorInfo, const VibratorCapacity &vibratorCapacity, const std::vector &waveInfomation, const HdfVibratorPlugInfo &info, VibratorAllInfos &vibratorAllInfos); diff --git a/services/miscdevice_service/src/miscdevice_service.cpp b/services/miscdevice_service/src/miscdevice_service.cpp index b836376..5c52788 100644 --- a/services/miscdevice_service/src/miscdevice_service.cpp +++ b/services/miscdevice_service/src/miscdevice_service.cpp @@ -293,7 +293,7 @@ bool MiscdeviceService::InitInterface() MISC_HILOGE("InitVibratorServiceImpl failed"); return false; } - (void)GetLocalVibratorInfo(); + GetOnlineVibratorInfo(); return true; } @@ -1033,7 +1033,36 @@ void MiscdeviceService::MergeVibratorParmeters(const VibrateParameter ¶meter void MiscdeviceService::SendMsgToClient(HdfVibratorPlugInfo info) { - MISC_HILOGI("Device:%{public}d state change,state:%{public}d", info.deviceId, info.status); + MISC_HILOGI("Device:%{public}d state change, state:%{public}d, deviceName:%{public}s", info.deviceId, info.status, + info.deviceName.c_str()); + std::lock_guard lockManage(devicesManageMutex_); + if (info.status == 0) { + auto it = devicesManageMap_.find(info.deviceId); + if (it == devicesManageMap_.end()) { + MISC_HILOGE("Device %{public}d is not in the map, so no action taken.", info.deviceId); + return; + } + VibratorIdentifierIPC identifier; + for (auto &value : it->second.baseInfo) { + identifier.deviceId = info.deviceId; + identifier.vibratorId = value.vibratorId; + StopVibratorService(identifier); + } + devicesManageMap_.erase(it); + MISC_HILOGI("Device %{public}d is offline and removed from the map.", info.deviceId); + } else { + std::vector vibratorInfo; + auto ret = vibratorHdiConnection_.GetVibratorInfo(vibratorInfo); + if (ret != NO_ERROR || vibratorInfo.empty()) { + MISC_HILOGE("Device not contain the local vibrator"); + return; + } + if (InsertVibratorInfo(info.deviceId, info.deviceName, vibratorInfo) != NO_ERROR) { + MISC_HILOGE("Insert vibrator of device %{public}d fail", info.deviceId); + return; + } + } + std::lock_guard lock(clientPidMapMutex_); MISC_HILOGI("Device:%{public}d state change,state:%{public}d, clientPidMap_.size::%{public}zu", info.deviceId, info.status, clientPidMap_.size()); @@ -1047,28 +1076,6 @@ void MiscdeviceService::SendMsgToClient(HdfVibratorPlugInfo info) clientProxy->ProcessPlugEvent(info.status, info.deviceId, info.vibratorCnt); } } - std::lock_guard lockManage(devicesManageMutex_); - if (info.status == 0) { - auto it = devicesManageMap_.find(info.deviceId); - if (it != devicesManageMap_.end()) { - VibratorIdentifierIPC identifier; - identifier.deviceId = info.deviceId; - identifier.vibratorId = -1; - StopVibratorService(identifier); - devicesManageMap_.erase(it); - MISC_HILOGI("Device %{public}d is offline and removed from the map.", info.deviceId); - } else { - MISC_HILOGI("Device %{public}d is not in the map, so no action taken.", info.deviceId); - } - } else { - VibratorIdentifierIPC identifier; - identifier.deviceId = info.deviceId; - VibratorAllInfos newAllInfo({}); - if (!UpdateVibratorAllInfo(identifier, info, newAllInfo)) { - MISC_HILOGE("Update new vibrator info failed"); - return; - } - } } int32_t MiscdeviceService::TransferClientRemoteObject(const sptr &vibratorServiceClient) @@ -1274,6 +1281,42 @@ int32_t MiscdeviceService::PlayPrimitiveEffectCheckAuthAndParam(int32_t intensit return ERR_OK; } +int32_t MiscdeviceService::GetLocalDeviceId(int32_t &deviceId) +{ + for (const auto& device : devicesManageMap_) { + for (const auto& info : device.second.baseInfo) { + if (info.isLocalVibrator) { + deviceId = info.deviceId; + return NO_ERROR; + } + } + } + + return ERROR; +} + +int32_t MiscdeviceService::GetOneVibrator(const VibratorIdentifierIPC& actIdentifier, + std::vector& vibratorInfoIPC) +{ + if (devicesManageMap_.empty()) { + MISC_HILOGE("No vibrator device online"); + return ERROR; + } + + auto it = devicesManageMap_.find(actIdentifier.deviceId); + if (it == devicesManageMap_.end()) { + MISC_HILOGE("Device manager map has no vibrator info"); + return ERROR; + } + for (auto &value : it->second.baseInfo) { + if (actIdentifier.vibratorId == value.vibratorId) { + vibratorInfoIPC.emplace_back(value); + break; + } + } + return NO_ERROR; +} + int32_t MiscdeviceService::GetVibratorList(const VibratorIdentifierIPC& identifier, std::vector& vibratorInfoIPC) { @@ -1290,36 +1333,38 @@ int32_t MiscdeviceService::GetVibratorList(const VibratorIdentifierIPC& identifi } identifier.Dump(); if (devicesManageMap_.empty()) { - MISC_HILOGI("No vibrator device online"); - return NO_ERROR; + GetOnlineVibratorInfo(); } - if ((identifier.deviceId == -1 && identifier.vibratorId == -1) || - (identifier.deviceId == -1 && identifier.vibratorId != -1)) { + std::lock_guard lockManage(devicesManageMutex_); + if ((identifier.deviceId == -1) && (identifier.vibratorId == -1)) { for (auto &value : devicesManageMap_) { vibratorInfoIPC.insert(vibratorInfoIPC.end(), value.second.baseInfo.begin(), value.second.baseInfo.end()); } - return NO_ERROR; - } else if (identifier.deviceId != -1 && identifier.vibratorId == -1) { - auto it = devicesManageMap_.find(identifier.deviceId); - if (it == devicesManageMap_.end()) { - MISC_HILOGD("Device manager map has no vibrator info"); - return NO_ERROR; + } else if ((identifier.deviceId == -1) && (identifier.vibratorId != -1)) { + VibratorIdentifierIPC actIdentifier; + actIdentifier.vibratorId = identifier.vibratorId; + ret = GetLocalDeviceId(actIdentifier.deviceId); + if (ret == NO_ERROR) { + ret = GetOneVibrator(actIdentifier, vibratorInfoIPC); + if (ret != NO_ERROR) { + MISC_HILOGI("Local deviceId %{public}d has no vibratorId %{public}d info", + actIdentifier.deviceId, actIdentifier.vibratorId); + } } - vibratorInfoIPC = it->second.baseInfo; - return NO_ERROR; - } else { + } else if ((identifier.deviceId != -1) && (identifier.vibratorId == -1)) { auto it = devicesManageMap_.find(identifier.deviceId); if (it == devicesManageMap_.end()) { MISC_HILOGD("Device manager map has no vibrator info"); return NO_ERROR; } - for (auto &value : it->second.baseInfo) { - if (identifier.vibratorId == value.vibratorId) { - vibratorInfoIPC.emplace_back(value); - } + vibratorInfoIPC = it->second.baseInfo; + } else { // ((identifier.deviceId != -1) && (identifier.vibratorId != -1)) + ret = GetOneVibrator(identifier, vibratorInfoIPC); + if (ret != NO_ERROR) { + MISC_HILOGI("DeviceId %{public}d has no vibratorId %{public}d info", + identifier.deviceId, identifier.vibratorId); } - return NO_ERROR; } return NO_ERROR; } @@ -1459,7 +1504,7 @@ int32_t MiscdeviceService::GetHapticCapacityInfo(const VibratorIdentifierIPC& id } } } - MISC_HILOGW("No local vibrator capacity information found for device ID: %{public}d", identifier.deviceId); + MISC_HILOGW("No vibrator capacity information found for device ID: %{public}d", identifier.deviceId); return ERR_OK; } @@ -1500,49 +1545,6 @@ int32_t MiscdeviceService::GetHapticStartUpTime(const VibratorIdentifierIPC& ide return NO_ERROR; } -bool MiscdeviceService::UpdateVibratorAllInfo(const VibratorIdentifierIPC &identifier, - const HdfVibratorPlugInfo &info, VibratorAllInfos &vibratorAllInfos) -{ - CALL_LOG_ENTER; - std::vector baseInfo; - std::vector vibratorIdList; - int32_t ret = -1; - const auto ite = devicesManageMap_.find(identifier.deviceId); - if (ite != devicesManageMap_.end()) { - for (auto& value : ite->second.baseInfo) { - (value.deviceName == "") ? (value.deviceName = info.deviceName) : 0; - } - return true; - } - if (identifier.deviceId < 0 || (identifier.deviceId < 0 && identifier.vibratorId < 0)) { - ret = vibratorHdiConnection_.GetVibratorInfo(baseInfo); - } else { - ret = vibratorHdiConnection_.GetVibratorList(identifier, baseInfo); - } - if (ret != NO_ERROR || baseInfo.empty()) { - MISC_HILOGE("HDI::GetVibratorInfoList return error"); - return false; - } - for (auto& value: baseInfo) { - vibratorIdList.push_back(value.vibratorId); - } - VibratorAllInfos tVibratorInfo(vibratorIdList); - vibratorAllInfos = tVibratorInfo; - VibratorCapacity capacity; - ret = vibratorHdiConnection_.GetVibratorCapacity(identifier, capacity); - if (ret != NO_ERROR) { - MISC_HILOGW("HDI::GetHapticCapacity return error, then fill the default capacity"); - } - std::vector waveInfo; - ret = vibratorHdiConnection_.GetAllWaveInfo(identifier, waveInfo); - if (ret != NO_ERROR) { - MISC_HILOGW("HDI::GetAllWaveInfo return error"); - } - (void)ConvertToServerInfos(baseInfo, capacity, waveInfo, info, vibratorAllInfos); - devicesManageMap_.insert(std::make_pair(identifier.deviceId, vibratorAllInfos)); - return true; -} - void MiscdeviceService::ConvertToServerInfos(const std::vector &baseVibratorInfo, const VibratorCapacity &vibratorCapacity, const std::vector &waveInfomation, const HdfVibratorPlugInfo &info, VibratorAllInfos& vibratorAllInfos) @@ -1556,55 +1558,78 @@ void MiscdeviceService::ConvertToServerInfos(const std::vector vibratorInfo.isSupportHdHaptic = vibratorCapacity.isSupportHdHaptic; vibratorInfo.isLocalVibrator = infos.isLocal; vibratorAllInfos.baseInfo.emplace_back(vibratorInfo); - MISC_HILOGI("HDI::HdfVibratorInfo deviceId:%{public}d, vibratorId:%{public}d, isLocalVibrator:%{public}d", - vibratorInfo.deviceId, vibratorInfo.vibratorId, vibratorInfo.isLocalVibrator); + MISC_HILOGI("HDI::HdfVibratorInfo deviceName:%{public}s, deviceId:%{public}d, vibratorId:%{public}d," + " isLocalVibrator:%{public}d", info.deviceName.c_str(), vibratorInfo.deviceId, vibratorInfo.vibratorId, + vibratorInfo.isLocalVibrator); } vibratorAllInfos.capacityInfo = vibratorCapacity; vibratorAllInfos.waveInfo = waveInfomation; } -void MiscdeviceService::GetLocalVibratorInfo() +void MiscdeviceService::GetOnlineVibratorInfo() { CALL_LOG_ENTER; std::vector vibratorInfo; auto ret = vibratorHdiConnection_.GetVibratorInfo(vibratorInfo); if (ret != NO_ERROR || vibratorInfo.empty()) { - MISC_HILOGW("Device not contain the local vibrator"); + MISC_HILOGW("Device does not contain any vibrators"); return; } - std::vector localInfo; - HdfVibratorPlugInfo mockInfo; - mockInfo.deviceName = ""; + + std::lock_guard lockManage(devicesManageMutex_); + const std::string deviceName = ""; for (auto &info : vibratorInfo) { - if (info.isLocal == 1) { - localInfo.emplace_back(info); + const auto it = devicesManageMap_.find(info.deviceId); + if (it != devicesManageMap_.end()) { + continue; + } + if (InsertVibratorInfo(info.deviceId, deviceName, vibratorInfo) != NO_ERROR) { + MISC_HILOGW("Insert vibrator of device %{public}d fail", info.deviceId); } } - VibratorIdentifierIPC param; +} + +int32_t MiscdeviceService::InsertVibratorInfo(int deviceId, const std::string &deviceName, + const std::vector &vibratorInfo) +{ + CALL_LOG_ENTER; + std::vector infos; + std::vector vibratorIdList; for (auto &info : vibratorInfo) { - if (info.isLocal == 1) { - param.deviceId = info.deviceId; - param.vibratorId = info.vibratorId; - break; + const auto it = devicesManageMap_.find(info.deviceId); + if (it != devicesManageMap_.end()) { + continue; } + if (info.deviceId == deviceId) { + infos.emplace_back(info); + vibratorIdList.push_back(info.vibratorId); + } + } + if (infos.empty()) { + MISC_HILOGE("Device %{public}d does not contain any vibrators", deviceId); + return ERROR; } + + VibratorIdentifierIPC param; + param.deviceId = infos[0].deviceId; + param.vibratorId = infos[0].vibratorId; VibratorCapacity capacity; - ret = vibratorHdiConnection_.GetVibratorCapacity(param, capacity); + int32_t ret = vibratorHdiConnection_.GetVibratorCapacity(param, capacity); if (ret != NO_ERROR) { - MISC_HILOGW("HDI::Get local hapticCapacity fail, then fill the default capacity"); + MISC_HILOGW("Get capacity fail from HDI, then use the default capacity, deviceId: %{public}d", param.deviceId); } std::vector waveInfo; ret = vibratorHdiConnection_.GetAllWaveInfo(param, waveInfo); if (ret != NO_ERROR) { - MISC_HILOGW("Get local waveInfo fail"); - } - std::vector vibratorIdList; - for (auto &value : localInfo) { - vibratorIdList.push_back(value.vibratorId); + MISC_HILOGW("Get waveInfo fail from HDI, deviceId: %{public}d", param.deviceId); } + + HdfVibratorPlugInfo mockInfo; + mockInfo.deviceName = deviceName; VibratorAllInfos localVibratorInfo(vibratorIdList); - (void)ConvertToServerInfos(localInfo, capacity, waveInfo, mockInfo, localVibratorInfo); + (void)ConvertToServerInfos(infos, capacity, waveInfo, mockInfo, localVibratorInfo); devicesManageMap_.insert(std::make_pair(param.deviceId, localVibratorInfo)); + return NO_ERROR; } int32_t MiscdeviceService::StartVibrateThreadControl(const VibratorIdentifierIPC& identifier, VibrateInfo& info) @@ -1661,7 +1686,7 @@ std::vector MiscdeviceService::CheckDeviceIdIsValid(const auto addToResult = [&](const auto& info) { VibratorIdentifierIPC newIdentifier; - newIdentifier.deviceId = identifier.deviceId == -1 ? info.deviceId : identifier.deviceId; + newIdentifier.deviceId = info.deviceId; newIdentifier.vibratorId = info.vibratorId; newIdentifier.position = info.position; newIdentifier.isLocalVibrator = info.isLocalVibrator; @@ -1671,13 +1696,15 @@ std::vector MiscdeviceService::CheckDeviceIdIsValid(const auto processDevice = [&](const auto& device) { for (const auto& info : device.second.baseInfo) { - bool shouldAdd = true; + bool shouldAdd = false; if (identifier.deviceId == -1 && identifier.vibratorId != -1) { shouldAdd = info.isLocalVibrator && info.vibratorId == identifier.vibratorId; } else if (identifier.deviceId != -1 && identifier.vibratorId != -1) { shouldAdd = info.vibratorId == identifier.vibratorId; } else if (identifier.deviceId == -1 && identifier.vibratorId == -1) { shouldAdd = info.isLocalVibrator; + } else { // (identifier.deviceId != -1 && identifier.vibratorId == -1) + shouldAdd = true; } if (shouldAdd) { -- Gitee