From 7ce10e13f289ce0412ef2c358713556b2edf1cb6 Mon Sep 17 00:00:00 2001 From: shaoziyun Date: Wed, 13 Mar 2024 10:01:06 +0000 Subject: [PATCH 1/3] 111 Signed-off-by: shaoziyun Change-Id: I9632b4ef82ce767eaa9bed435413afa25aa7b484 --- .../common/include/i_miscdevice_service.h | 1 + .../common/include/miscdevice_service_proxy.h | 1 + .../include/sensors_ipc_interface_code.h | 1 + .../common/src/miscdevice_service_proxy.cpp | 25 ++++ frameworks/native/vibrator/BUILD.gn | 1 + .../vibrator/include/i_vibrator_client.h | 31 +++++ .../vibrator/include/vibrator_client_stub.h | 34 ++++++ .../include/vibrator_service_client.h | 6 + .../vibrator/src/vibrator_client_stub.cpp | 39 +++++++ .../vibrator/src/vibrator_service_client.cpp | 16 +++ .../include/miscdevice_service.h | 9 ++ .../include/miscdevice_service_stub.h | 1 + .../src/miscdevice_service.cpp | 108 ++++++++++++++++++ .../src/miscdevice_service_stub.cpp | 13 +++ 14 files changed, 286 insertions(+) create mode 100644 frameworks/native/vibrator/include/i_vibrator_client.h create mode 100644 frameworks/native/vibrator/include/vibrator_client_stub.h create mode 100644 frameworks/native/vibrator/src/vibrator_client_stub.cpp diff --git a/frameworks/native/common/include/i_miscdevice_service.h b/frameworks/native/common/include/i_miscdevice_service.h index ab2acf9..1294f56 100644 --- a/frameworks/native/common/include/i_miscdevice_service.h +++ b/frameworks/native/common/include/i_miscdevice_service.h @@ -51,6 +51,7 @@ public: virtual int32_t TurnOff(int32_t lightId) = 0; virtual int32_t GetDelayTime(int32_t &delayTime) = 0; virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, const VibrateParameter ¶meter) = 0; + virtual int32_t SendClientRemoteObject(const sptr &vibratorClient) = 0; }; } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/common/include/miscdevice_service_proxy.h b/frameworks/native/common/include/miscdevice_service_proxy.h index bea6f97..a7c9838 100644 --- a/frameworks/native/common/include/miscdevice_service_proxy.h +++ b/frameworks/native/common/include/miscdevice_service_proxy.h @@ -43,6 +43,7 @@ public: virtual int32_t GetDelayTime(int32_t &delayTime) override; virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, const VibrateParameter ¶meter) override; + virtual int32_t SendClientRemoteObject(const sptr &vibratorClient) override; private: DISALLOW_COPY_AND_MOVE(MiscdeviceServiceProxy); diff --git a/frameworks/native/common/include/sensors_ipc_interface_code.h b/frameworks/native/common/include/sensors_ipc_interface_code.h index 5e68dbc..57544fc 100644 --- a/frameworks/native/common/include/sensors_ipc_interface_code.h +++ b/frameworks/native/common/include/sensors_ipc_interface_code.h @@ -33,6 +33,7 @@ enum class MiscdeviceInterfaceCode { TURN_OFF, PlAY_PATTERN, GET_DELAY_TIME, + SEND_CLIENT_REMOTE_OBJECT, }; } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/common/src/miscdevice_service_proxy.cpp b/frameworks/native/common/src/miscdevice_service_proxy.cpp index 275364b..e85add1 100644 --- a/frameworks/native/common/src/miscdevice_service_proxy.cpp +++ b/frameworks/native/common/src/miscdevice_service_proxy.cpp @@ -391,5 +391,30 @@ int32_t MiscdeviceServiceProxy::PlayPattern(const VibratePattern &pattern, int32 } return ret; } + +int32_t MiscdeviceServiceProxy::SendClientRemoteObject(const sptr &vibratorClient) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + MISC_HILOGD("Write descriptor failed"); + return WRITE_MSG_ERR; + } + if (!data.WriteRemoteObject(vibratorClient)) { + MISC_HILOGD("Parcel writeRemoteObject failed"); + return WRITE_MSG_ERR; + } + sptr remote = Remote(); + CHKPR(remote, ERROR); + int32_t ret = remote->SendRequest(static_cast(MiscdeviceInterfaceCode::SEND_CLIENT_REMOTE_OBJECT), + data, reply, option); + if (ret != NO_ERROR) { + HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION", + HiSysEvent::EventType::FAULT, "PKG_NAME", "SendClientRemoteObject", "ERROR_CODE", ret); + MISC_HILOGD("SendRequest failed, ret:%{public}d", ret); + } + return ret; +} } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/vibrator/BUILD.gn b/frameworks/native/vibrator/BUILD.gn index 1638002..9596f45 100644 --- a/frameworks/native/vibrator/BUILD.gn +++ b/frameworks/native/vibrator/BUILD.gn @@ -79,6 +79,7 @@ ohos_shared_library("libvibrator_native") { sources = [ "$SUBSYSTEM_DIR/frameworks/native/common/src/miscdevice_service_proxy.cpp", "src/vibrator_service_client.cpp", + "src/vibrator_client_stub.cpp" ] include_dirs = [ diff --git a/frameworks/native/vibrator/include/i_vibrator_client.h b/frameworks/native/vibrator/include/i_vibrator_client.h new file mode 100644 index 0000000..74ce62f --- /dev/null +++ b/frameworks/native/vibrator/include/i_vibrator_client.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021-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. + */ + +#ifndef I_VIBRATOR_CLIENT_H +#define I_VIBRATOR_CLIENT_H + +#include "iremote_broker.h" + +namespace OHOS { +namespace Sensors { +class IVibratorClient : public IRemoteBroker { +public: + IVibratorClient() = default; + virtual ~IVibratorClient() = default; + DECLARE_INTERFACE_DESCRIPTOR(u"IVibratorClient"); +}; +} // namespace Sensors +} // namespace OHOS +#endif // I_VIBRATOR_CLIENT_H diff --git a/frameworks/native/vibrator/include/vibrator_client_stub.h b/frameworks/native/vibrator/include/vibrator_client_stub.h new file mode 100644 index 0000000..9beba01 --- /dev/null +++ b/frameworks/native/vibrator/include/vibrator_client_stub.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 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 VIBRATOR_CLIENT_STUB_H +#define VIBRATOR_CLIENT_STUB_H + +#include "message_parcel.h" +#include "iremote_stub.h" +#include "i_vibrator_client.h" + +namespace OHOS { +namespace Sensors { +class VibratorClientStub : public IRemoteStub { +public: + VibratorClientStub() = default; + virtual ~VibratorClientStub() = default; + virtual int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) override; +}; +} // namespace Sensors +} // namespace OHOS +#endif // VIBRATOR_CLIENT_STUB_H diff --git a/frameworks/native/vibrator/include/vibrator_service_client.h b/frameworks/native/vibrator/include/vibrator_service_client.h index ecccb77..fc8f123 100644 --- a/frameworks/native/vibrator/include/vibrator_service_client.h +++ b/frameworks/native/vibrator/include/vibrator_service_client.h @@ -23,8 +23,12 @@ #include "singleton.h" #include "i_vibrator_decoder.h" +#include "i_vibrator_client.h" +#include "i_miscdevice_service.h" + #include "miscdevice_service_proxy.h" #include "vibrator_agent_type.h" +#include "vibrator_client_stub.h" namespace OHOS { namespace Sensors { @@ -67,6 +71,7 @@ public: int32_t GetDelayTime(int32_t &delayTime); int32_t PlayPattern(const VibratorPattern &pattern, int32_t usage, const VibratorParameter ¶meter); int32_t FreeVibratorPackage(VibratorPackage &package); + int32_t SendClientRemoteObject(); private: int32_t InitServiceClient(); @@ -74,6 +79,7 @@ private: int32_t ConvertVibratePackage(const VibratePackage& inPkg, VibratorPackage &outPkg); sptr serviceDeathObserver_ = nullptr; sptr miscdeviceProxy_ = nullptr; + sptr vibratorClient = nullptr; VibratorDecodeHandle decodeHandle_; std::mutex clientMutex_; std::mutex decodeMutex_; diff --git a/frameworks/native/vibrator/src/vibrator_client_stub.cpp b/frameworks/native/vibrator/src/vibrator_client_stub.cpp new file mode 100644 index 0000000..ef9460c --- /dev/null +++ b/frameworks/native/vibrator/src/vibrator_client_stub.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 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 "vibrator_client_stub.h" + +#include "message_parcel.h" +#include "miscdevice_log.h" + +#undef LOG_TAG +#define LOG_TAG "VibratorClientStub" +namespace OHOS { +namespace Sensors { + +int32_t VibratorClientStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + std::u16string descriptor = VibratorClientStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + MISC_HILOGD("Client and service descriptors are inconsistent"); + return OBJECT_NULL; + } + MISC_HILOGD("Begin, cmd:%{public}u", code); + return NO_ERROR; +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/vibrator/src/vibrator_service_client.cpp b/frameworks/native/vibrator/src/vibrator_service_client.cpp index a050694..377e731 100644 --- a/frameworks/native/vibrator/src/vibrator_service_client.cpp +++ b/frameworks/native/vibrator/src/vibrator_service_client.cpp @@ -68,6 +68,9 @@ int32_t VibratorServiceClient::InitServiceClient() MISC_HILOGD("miscdeviceProxy_ already init"); return ERR_OK; } + if (vibratorClient == nullptr) { + vibratorClient = new (std::nothrow) VibratorClientStub(); + } auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (sm == nullptr) { MISC_HILOGE("sm cannot be null"); @@ -84,6 +87,11 @@ int32_t VibratorServiceClient::InitServiceClient() auto remoteObject = miscdeviceProxy_->AsObject(); CHKPR(remoteObject, MISC_NATIVE_GET_SERVICE_ERR); remoteObject->AddDeathRecipient(serviceDeathObserver_); + int32_t ret = SendClientRemoteObject(); + if (ret != ERR_OK) { + MISC_HILOGE("SendClientRemoteObject failed, ret:%{public}d", ret); + return ERROR; + } return ERR_OK; } MISC_HILOGW("Get service failed, retry:%{public}d", retry); @@ -96,6 +104,14 @@ int32_t VibratorServiceClient::InitServiceClient() return MISC_NATIVE_GET_SERVICE_ERR; } +int32_t VibratorServiceClient::SendClientRemoteObject() +{ + auto remoteObject = vibratorClient->AsObject(); + CHKPR(remoteObject, MISC_NATIVE_GET_SERVICE_ERR); + int32_t ret = miscdeviceProxy_->SendClientRemoteObject(remoteObject); + return ret; +} + int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) { MISC_HILOGD("Vibrate begin, time:%{public}d, usage:%{public}d", timeOut, usage); diff --git a/services/miscdevice_service/include/miscdevice_service.h b/services/miscdevice_service/include/miscdevice_service.h index 98fc982..f5c7ba2 100644 --- a/services/miscdevice_service/include/miscdevice_service.h +++ b/services/miscdevice_service/include/miscdevice_service.h @@ -59,6 +59,7 @@ public: bool IsValid(int32_t lightId); bool IsLightAnimationValid(const LightAnimationIPC &animation); int32_t Dump(int32_t fd, const std::vector &args) override; + void ProcessDeathObserver(const wptr &object); virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) override; virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, int32_t loopCount, int32_t usage) override; @@ -75,6 +76,7 @@ public: virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, const VibrateParameter ¶meter) override; virtual int32_t GetDelayTime(int32_t &delayTime) override; + virtual int32_t SendClientRemoteObject(const sptr &vibratorServiceClient) override; private: DISALLOW_COPY_AND_MOVE(MiscdeviceService); @@ -87,6 +89,11 @@ private: void MergeVibratorParmeters(const VibrateParameter ¶meter, VibratePackage &package); bool CheckVibratorParmeters(const VibrateParameter ¶meter); bool InitLightList(); + void RegisterClientDeathRecipient(sptr vibratorServiceClient, int32_t pid); + void UnregisterClientDeathRecipient(sptr vibratorServiceClient); + bool SaveClientPid(const sptr &vibratorServiceClient, int32_t pid); + int32_t FindClientPid(const sptr &vibratorServiceClient); + void DestroyClientPid(const sptr &vibratorServiceClient); VibratorHdiConnection &vibratorHdiConnection_ = VibratorHdiConnection::GetInstance(); LightHdiConnection &lightHdiConnection_ = LightHdiConnection::GetInstance(); bool lightExist_; @@ -96,6 +103,8 @@ private: MiscdeviceServiceState state_; std::shared_ptr vibratorThread_ = nullptr; std::mutex vibratorThreadMutex_; + sptr clientDeathObserver_ = nullptr; + std::map, int32_t> clientPidMap_; }; } // namespace Sensors } // namespace OHOS diff --git a/services/miscdevice_service/include/miscdevice_service_stub.h b/services/miscdevice_service/include/miscdevice_service_stub.h index 2a62982..3ad771e 100644 --- a/services/miscdevice_service/include/miscdevice_service_stub.h +++ b/services/miscdevice_service/include/miscdevice_service_stub.h @@ -58,6 +58,7 @@ private: bool CheckVibratePermission(); int32_t PlayPatternStub(MessageParcel &data, MessageParcel &reply); int32_t GetDelayTimeStub(MessageParcel &data, MessageParcel &reply); + int32_t CreatClientRemoteObject(MessageParcel &data, MessageParcel &reply); std::map baseFuncs_; }; } // namespace Sensors diff --git a/services/miscdevice_service/src/miscdevice_service.cpp b/services/miscdevice_service/src/miscdevice_service.cpp index e36c464..172cfa8 100644 --- a/services/miscdevice_service/src/miscdevice_service.cpp +++ b/services/miscdevice_service/src/miscdevice_service.cpp @@ -20,9 +20,11 @@ #include +#include "death_recipient_template.h" #include "sensors_errors.h" #include "system_ability_definition.h" #include "vibration_priority_manager.h" +#include "vibrator_infos.h" #ifdef HDF_DRIVERS_INTERFACE_LIGHT #include "v1_0/light_interface_proxy.h" #endif // HDF_DRIVERS_INTERFACE_LIGHT @@ -52,6 +54,8 @@ constexpr int32_t INTENSITY_ADJUST_MIN = 0; constexpr int32_t INTENSITY_ADJUST_MAX = 100; constexpr int32_t FREQUENCY_ADJUST_MIN = -100; constexpr int32_t FREQUENCY_ADJUST_MAX = 100; +constexpr int32_t INVALID_PID = -1; +constexpr int32_t VIBRATOR_ID = 1; VibratorCapacity g_capacity; #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM const std::string PHONE_TYPE = "phone"; @@ -596,5 +600,109 @@ void MiscdeviceService::MergeVibratorParmeters(const VibrateParameter ¶meter } } } + +int32_t MiscdeviceService::SendClientRemoteObject(const sptr &vibratorServiceClient) +{ + auto clientPid = GetCallingPid(); + if (clientPid < 0) { + MISC_HILOGE("ClientPid is invalid, clientPid:%{public}d", clientPid); + return ERROR; + } + if (vibratorThread_ == nullptr) { + vibratorThread_ = std::make_shared(); + } + VibrateInfo info = vibratorThread_->GetCurrentVibrateInfo(); + RegisterClientDeathRecipient(vibratorServiceClient, clientPid); + return ERR_OK; +} + +void MiscdeviceService::ProcessDeathObserver(const wptr &object) +{ + sptr client = object.promote(); + int32_t clientPid = FindClientPid(client); + VibrateInfo info = vibratorThread_->GetCurrentVibrateInfo(); + int32_t VibratePid = info.pid; + MISC_HILOGD("ClientPid:%{public}d, VibratePid:%{public}d", clientPid, VibratePid); + if ((clientPid != INVALID_PID) && (clientPid == VibratePid)) { + StopVibrator(VIBRATOR_ID); + } + UnregisterClientDeathRecipient(client); + return; +} + +void MiscdeviceService::RegisterClientDeathRecipient(sptr vibratorServiceClient, int32_t pid) +{ + if (vibratorServiceClient == nullptr) { + MISC_HILOGE("VibratorServiceClient is nullptr"); + return; + } + if (clientDeathObserver_ == nullptr) { + clientDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); + if (clientDeathObserver_ == nullptr) { + MISC_HILOGE("ClientDeathObserver_ is nullptr"); + return; + } + } + vibratorServiceClient->AddDeathRecipient(clientDeathObserver_); + SaveClientPid(vibratorServiceClient, pid); +} + +void MiscdeviceService::UnregisterClientDeathRecipient(sptr vibratorServiceClient) +{ + if (vibratorServiceClient == nullptr) { + MISC_HILOGE("vibratorServiceClient is nullptr"); + return; + } + int32_t clientPid = FindClientPid(vibratorServiceClient); + if (clientPid == INVALID_PID) { + MISC_HILOGE("Pid is invalid"); + return; + } + vibratorServiceClient->RemoveDeathRecipient(clientDeathObserver_); + DestroyClientPid(vibratorServiceClient); +} + +bool MiscdeviceService::SaveClientPid(const sptr &vibratorServiceClient, int32_t pid) +{ + if (vibratorServiceClient == nullptr) { + MISC_HILOGE("VibratorServiceClient is nullptr"); + return false; + } + auto it = clientPidMap_.find(vibratorServiceClient); + if (it == clientPidMap_.end()) { + clientPidMap_.insert(std::make_pair(vibratorServiceClient, pid)); + return true; + } + clientPidMap_.insert(std::make_pair(vibratorServiceClient, pid)); + return true; +} + +int32_t MiscdeviceService::FindClientPid(const sptr &vibratorServiceClient) +{ + if (vibratorServiceClient == nullptr) { + MISC_HILOGE("VibratorServiceClient is nullptr"); + return false; + } + auto it = clientPidMap_.find(vibratorServiceClient); + if (it == clientPidMap_.end()) { + MISC_HILOGE("Cannot find client pid"); + return INVALID_PID; + } + return it->second; +} + +void MiscdeviceService::DestroyClientPid(const sptr &vibratorServiceClient) +{ + if (vibratorServiceClient == nullptr) { + MISC_HILOGD("VibratorServiceClient is nullptr"); + return; + } + auto it = clientPidMap_.find(vibratorServiceClient); + if (it == clientPidMap_.end()) { + MISC_HILOGD("Cannot find client pid"); + return; + } + clientPidMap_.erase(it); +} } // namespace Sensors } // namespace OHOS diff --git a/services/miscdevice_service/src/miscdevice_service_stub.cpp b/services/miscdevice_service/src/miscdevice_service_stub.cpp index 420e11d..8272f4f 100644 --- a/services/miscdevice_service/src/miscdevice_service_stub.cpp +++ b/services/miscdevice_service/src/miscdevice_service_stub.cpp @@ -64,6 +64,8 @@ MiscdeviceServiceStub::MiscdeviceServiceStub() &MiscdeviceServiceStub::PlayPatternStub; baseFuncs_[static_cast(MiscdeviceInterfaceCode::GET_DELAY_TIME)] = &MiscdeviceServiceStub::GetDelayTimeStub; + baseFuncs_[static_cast(MiscdeviceInterfaceCode::SEND_CLIENT_REMOTE_OBJECT)] = + &MiscdeviceServiceStub::CreatClientRemoteObject; } MiscdeviceServiceStub::~MiscdeviceServiceStub() @@ -338,5 +340,16 @@ int32_t MiscdeviceServiceStub::GetDelayTimeStub(MessageParcel &data, MessageParc } return NO_ERROR; } + +int32_t MiscdeviceServiceStub::CreatClientRemoteObject(MessageParcel &data, MessageParcel &reply) +{ + CALL_LOG_ENTER; + sptr vibratorServiceClient = data.ReadRemoteObject(); + if(vibratorServiceClient == nullptr) { + MISC_HILOGD("vibratorServiceClient is null"); + return ERROR; + } + return SendClientRemoteObject(vibratorServiceClient); +} } // namespace Sensors } // namespace OHOS -- Gitee From 8621103faef8815f18de670a0dd678a05707acab Mon Sep 17 00:00:00 2001 From: shaoziyun Date: Fri, 15 Mar 2024 01:32:20 +0000 Subject: [PATCH 2/3] 111 Signed-off-by: shaoziyun Change-Id: I7ddf04394ebb0c5a41df28836fa7d59633e839bf --- .../common/include/i_miscdevice_service.h | 2 +- .../common/include/miscdevice_service_proxy.h | 2 +- .../common/src/miscdevice_service_proxy.cpp | 8 +++--- .../vibrator/include/i_vibrator_client.h | 2 +- .../vibrator/include/vibrator_client_stub.h | 2 +- .../include/vibrator_service_client.h | 3 +-- .../vibrator/src/vibrator_client_stub.cpp | 2 +- .../vibrator/src/vibrator_service_client.cpp | 8 +++--- .../include/miscdevice_service.h | 5 ++-- .../include/miscdevice_service_stub.h | 2 +- .../src/miscdevice_service.cpp | 26 ++++++++----------- .../src/miscdevice_service_stub.cpp | 6 ++--- 12 files changed, 32 insertions(+), 36 deletions(-) diff --git a/frameworks/native/common/include/i_miscdevice_service.h b/frameworks/native/common/include/i_miscdevice_service.h index 1294f56..b0e9dec 100644 --- a/frameworks/native/common/include/i_miscdevice_service.h +++ b/frameworks/native/common/include/i_miscdevice_service.h @@ -51,7 +51,7 @@ public: virtual int32_t TurnOff(int32_t lightId) = 0; virtual int32_t GetDelayTime(int32_t &delayTime) = 0; virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, const VibrateParameter ¶meter) = 0; - virtual int32_t SendClientRemoteObject(const sptr &vibratorClient) = 0; + virtual int32_t TransferClientRemoteObject(const sptr &vibratorClient) = 0; }; } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/common/include/miscdevice_service_proxy.h b/frameworks/native/common/include/miscdevice_service_proxy.h index a7c9838..cf4729f 100644 --- a/frameworks/native/common/include/miscdevice_service_proxy.h +++ b/frameworks/native/common/include/miscdevice_service_proxy.h @@ -43,7 +43,7 @@ public: virtual int32_t GetDelayTime(int32_t &delayTime) override; virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, const VibrateParameter ¶meter) override; - virtual int32_t SendClientRemoteObject(const sptr &vibratorClient) override; + virtual int32_t TransferClientRemoteObject(const sptr &vibratorClient) override; private: DISALLOW_COPY_AND_MOVE(MiscdeviceServiceProxy); diff --git a/frameworks/native/common/src/miscdevice_service_proxy.cpp b/frameworks/native/common/src/miscdevice_service_proxy.cpp index e85add1..06c7aa9 100644 --- a/frameworks/native/common/src/miscdevice_service_proxy.cpp +++ b/frameworks/native/common/src/miscdevice_service_proxy.cpp @@ -392,17 +392,17 @@ int32_t MiscdeviceServiceProxy::PlayPattern(const VibratePattern &pattern, int32 return ret; } -int32_t MiscdeviceServiceProxy::SendClientRemoteObject(const sptr &vibratorClient) +int32_t MiscdeviceServiceProxy::TransferClientRemoteObject(const sptr &vibratorClient) { MessageParcel data; MessageParcel reply; MessageOption option; if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { - MISC_HILOGD("Write descriptor failed"); + MISC_HILOGE("Write descriptor failed"); return WRITE_MSG_ERR; } if (!data.WriteRemoteObject(vibratorClient)) { - MISC_HILOGD("Parcel writeRemoteObject failed"); + MISC_HILOGE("Parcel writeRemoteObject failed"); return WRITE_MSG_ERR; } sptr remote = Remote(); @@ -411,7 +411,7 @@ int32_t MiscdeviceServiceProxy::SendClientRemoteObject(const sptr data, reply, option); if (ret != NO_ERROR) { HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "PKG_NAME", "SendClientRemoteObject", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "TransferClientRemoteObject", "ERROR_CODE", ret); MISC_HILOGD("SendRequest failed, ret:%{public}d", ret); } return ret; diff --git a/frameworks/native/vibrator/include/i_vibrator_client.h b/frameworks/native/vibrator/include/i_vibrator_client.h index 74ce62f..57dbe0b 100644 --- a/frameworks/native/vibrator/include/i_vibrator_client.h +++ b/frameworks/native/vibrator/include/i_vibrator_client.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 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 diff --git a/frameworks/native/vibrator/include/vibrator_client_stub.h b/frameworks/native/vibrator/include/vibrator_client_stub.h index 9beba01..1c87669 100644 --- a/frameworks/native/vibrator/include/vibrator_client_stub.h +++ b/frameworks/native/vibrator/include/vibrator_client_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 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 diff --git a/frameworks/native/vibrator/include/vibrator_service_client.h b/frameworks/native/vibrator/include/vibrator_service_client.h index fc8f123..1e9eb65 100644 --- a/frameworks/native/vibrator/include/vibrator_service_client.h +++ b/frameworks/native/vibrator/include/vibrator_service_client.h @@ -24,7 +24,6 @@ #include "i_vibrator_decoder.h" #include "i_vibrator_client.h" -#include "i_miscdevice_service.h" #include "miscdevice_service_proxy.h" #include "vibrator_agent_type.h" @@ -71,7 +70,7 @@ public: int32_t GetDelayTime(int32_t &delayTime); int32_t PlayPattern(const VibratorPattern &pattern, int32_t usage, const VibratorParameter ¶meter); int32_t FreeVibratorPackage(VibratorPackage &package); - int32_t SendClientRemoteObject(); + int32_t TransferClientRemoteObject(); private: int32_t InitServiceClient(); diff --git a/frameworks/native/vibrator/src/vibrator_client_stub.cpp b/frameworks/native/vibrator/src/vibrator_client_stub.cpp index ef9460c..7aa8073 100644 --- a/frameworks/native/vibrator/src/vibrator_client_stub.cpp +++ b/frameworks/native/vibrator/src/vibrator_client_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 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 diff --git a/frameworks/native/vibrator/src/vibrator_service_client.cpp b/frameworks/native/vibrator/src/vibrator_service_client.cpp index 377e731..d2a2fb6 100644 --- a/frameworks/native/vibrator/src/vibrator_service_client.cpp +++ b/frameworks/native/vibrator/src/vibrator_service_client.cpp @@ -87,9 +87,9 @@ int32_t VibratorServiceClient::InitServiceClient() auto remoteObject = miscdeviceProxy_->AsObject(); CHKPR(remoteObject, MISC_NATIVE_GET_SERVICE_ERR); remoteObject->AddDeathRecipient(serviceDeathObserver_); - int32_t ret = SendClientRemoteObject(); + int32_t ret = TransferClientRemoteObject(); if (ret != ERR_OK) { - MISC_HILOGE("SendClientRemoteObject failed, ret:%{public}d", ret); + MISC_HILOGE("TransferClientRemoteObject failed, ret:%{public}d", ret); return ERROR; } return ERR_OK; @@ -104,11 +104,11 @@ int32_t VibratorServiceClient::InitServiceClient() return MISC_NATIVE_GET_SERVICE_ERR; } -int32_t VibratorServiceClient::SendClientRemoteObject() +int32_t VibratorServiceClient::TransferClientRemoteObject() { auto remoteObject = vibratorClient->AsObject(); CHKPR(remoteObject, MISC_NATIVE_GET_SERVICE_ERR); - int32_t ret = miscdeviceProxy_->SendClientRemoteObject(remoteObject); + int32_t ret = miscdeviceProxy_->TransferClientRemoteObject(remoteObject); return ret; } diff --git a/services/miscdevice_service/include/miscdevice_service.h b/services/miscdevice_service/include/miscdevice_service.h index f5c7ba2..b112aea 100644 --- a/services/miscdevice_service/include/miscdevice_service.h +++ b/services/miscdevice_service/include/miscdevice_service.h @@ -76,7 +76,7 @@ public: virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, const VibrateParameter ¶meter) override; virtual int32_t GetDelayTime(int32_t &delayTime) override; - virtual int32_t SendClientRemoteObject(const sptr &vibratorServiceClient) override; + virtual int32_t TransferClientRemoteObject(const sptr &vibratorServiceClient) override; private: DISALLOW_COPY_AND_MOVE(MiscdeviceService); @@ -91,7 +91,7 @@ private: bool InitLightList(); void RegisterClientDeathRecipient(sptr vibratorServiceClient, int32_t pid); void UnregisterClientDeathRecipient(sptr vibratorServiceClient); - bool SaveClientPid(const sptr &vibratorServiceClient, int32_t pid); + void SaveClientPid(const sptr &vibratorServiceClient, int32_t pid); int32_t FindClientPid(const sptr &vibratorServiceClient); void DestroyClientPid(const sptr &vibratorServiceClient); VibratorHdiConnection &vibratorHdiConnection_ = VibratorHdiConnection::GetInstance(); @@ -103,6 +103,7 @@ private: MiscdeviceServiceState state_; std::shared_ptr vibratorThread_ = nullptr; std::mutex vibratorThreadMutex_; + std::mutex clientPidMutex_; sptr clientDeathObserver_ = nullptr; std::map, int32_t> clientPidMap_; }; diff --git a/services/miscdevice_service/include/miscdevice_service_stub.h b/services/miscdevice_service/include/miscdevice_service_stub.h index 3ad771e..752b924 100644 --- a/services/miscdevice_service/include/miscdevice_service_stub.h +++ b/services/miscdevice_service/include/miscdevice_service_stub.h @@ -58,7 +58,7 @@ private: bool CheckVibratePermission(); int32_t PlayPatternStub(MessageParcel &data, MessageParcel &reply); int32_t GetDelayTimeStub(MessageParcel &data, MessageParcel &reply); - int32_t CreatClientRemoteObject(MessageParcel &data, MessageParcel &reply); + int32_t TransferClientRemoteObjectStub(MessageParcel &data, MessageParcel &reply); std::map baseFuncs_; }; } // namespace Sensors diff --git a/services/miscdevice_service/src/miscdevice_service.cpp b/services/miscdevice_service/src/miscdevice_service.cpp index 172cfa8..9a7b386 100644 --- a/services/miscdevice_service/src/miscdevice_service.cpp +++ b/services/miscdevice_service/src/miscdevice_service.cpp @@ -601,17 +601,13 @@ void MiscdeviceService::MergeVibratorParmeters(const VibrateParameter ¶meter } } -int32_t MiscdeviceService::SendClientRemoteObject(const sptr &vibratorServiceClient) +int32_t MiscdeviceService::TransferClientRemoteObject(const sptr &vibratorServiceClient) { auto clientPid = GetCallingPid(); if (clientPid < 0) { MISC_HILOGE("ClientPid is invalid, clientPid:%{public}d", clientPid); return ERROR; } - if (vibratorThread_ == nullptr) { - vibratorThread_ = std::make_shared(); - } - VibrateInfo info = vibratorThread_->GetCurrentVibrateInfo(); RegisterClientDeathRecipient(vibratorServiceClient, clientPid); return ERR_OK; } @@ -620,14 +616,16 @@ void MiscdeviceService::ProcessDeathObserver(const wptr &object) { sptr client = object.promote(); int32_t clientPid = FindClientPid(client); + if (vibratorThread_ == nullptr) { + vibratorThread_ = std::make_shared(); + } VibrateInfo info = vibratorThread_->GetCurrentVibrateInfo(); int32_t VibratePid = info.pid; - MISC_HILOGD("ClientPid:%{public}d, VibratePid:%{public}d", clientPid, VibratePid); + MISC_HILOGI("ClientPid:%{public}d, VibratePid:%{public}d", clientPid, VibratePid); if ((clientPid != INVALID_PID) && (clientPid == VibratePid)) { StopVibrator(VIBRATOR_ID); } UnregisterClientDeathRecipient(client); - return; } void MiscdeviceService::RegisterClientDeathRecipient(sptr vibratorServiceClient, int32_t pid) @@ -636,6 +634,7 @@ void MiscdeviceService::RegisterClientDeathRecipient(sptr vibrat MISC_HILOGE("VibratorServiceClient is nullptr"); return; } + std::lock_guard lock(vibratorThreadMutex_); if (clientDeathObserver_ == nullptr) { clientDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); if (clientDeathObserver_ == nullptr) { @@ -662,19 +661,14 @@ void MiscdeviceService::UnregisterClientDeathRecipient(sptr vibra DestroyClientPid(vibratorServiceClient); } -bool MiscdeviceService::SaveClientPid(const sptr &vibratorServiceClient, int32_t pid) +void MiscdeviceService::SaveClientPid(const sptr &vibratorServiceClient, int32_t pid) { if (vibratorServiceClient == nullptr) { MISC_HILOGE("VibratorServiceClient is nullptr"); - return false; - } - auto it = clientPidMap_.find(vibratorServiceClient); - if (it == clientPidMap_.end()) { - clientPidMap_.insert(std::make_pair(vibratorServiceClient, pid)); - return true; + return; } + std::lock_guard lock(clientPidMutex_); clientPidMap_.insert(std::make_pair(vibratorServiceClient, pid)); - return true; } int32_t MiscdeviceService::FindClientPid(const sptr &vibratorServiceClient) @@ -683,6 +677,7 @@ int32_t MiscdeviceService::FindClientPid(const sptr &vibratorServ MISC_HILOGE("VibratorServiceClient is nullptr"); return false; } + std::lock_guard lock(clientPidMutex_); auto it = clientPidMap_.find(vibratorServiceClient); if (it == clientPidMap_.end()) { MISC_HILOGE("Cannot find client pid"); @@ -697,6 +692,7 @@ void MiscdeviceService::DestroyClientPid(const sptr &vibratorServ MISC_HILOGD("VibratorServiceClient is nullptr"); return; } + std::lock_guard lock(clientPidMutex_); auto it = clientPidMap_.find(vibratorServiceClient); if (it == clientPidMap_.end()) { MISC_HILOGD("Cannot find client pid"); diff --git a/services/miscdevice_service/src/miscdevice_service_stub.cpp b/services/miscdevice_service/src/miscdevice_service_stub.cpp index 8272f4f..c66a167 100644 --- a/services/miscdevice_service/src/miscdevice_service_stub.cpp +++ b/services/miscdevice_service/src/miscdevice_service_stub.cpp @@ -65,7 +65,7 @@ MiscdeviceServiceStub::MiscdeviceServiceStub() baseFuncs_[static_cast(MiscdeviceInterfaceCode::GET_DELAY_TIME)] = &MiscdeviceServiceStub::GetDelayTimeStub; baseFuncs_[static_cast(MiscdeviceInterfaceCode::SEND_CLIENT_REMOTE_OBJECT)] = - &MiscdeviceServiceStub::CreatClientRemoteObject; + &MiscdeviceServiceStub::TransferClientRemoteObjectStub; } MiscdeviceServiceStub::~MiscdeviceServiceStub() @@ -341,7 +341,7 @@ int32_t MiscdeviceServiceStub::GetDelayTimeStub(MessageParcel &data, MessageParc return NO_ERROR; } -int32_t MiscdeviceServiceStub::CreatClientRemoteObject(MessageParcel &data, MessageParcel &reply) +int32_t MiscdeviceServiceStub::TransferClientRemoteObjectStub(MessageParcel &data, MessageParcel &reply) { CALL_LOG_ENTER; sptr vibratorServiceClient = data.ReadRemoteObject(); @@ -349,7 +349,7 @@ int32_t MiscdeviceServiceStub::CreatClientRemoteObject(MessageParcel &data, Mess MISC_HILOGD("vibratorServiceClient is null"); return ERROR; } - return SendClientRemoteObject(vibratorServiceClient); + return TransferClientRemoteObject(vibratorServiceClient); } } // namespace Sensors } // namespace OHOS -- Gitee From 3937c4be8a547ab8ddea19efc811ca076e43fc2e Mon Sep 17 00:00:00 2001 From: shaoziyun Date: Tue, 19 Mar 2024 02:13:02 +0000 Subject: [PATCH 3/3] 111 Signed-off-by: shaoziyun Change-Id: I6d24b7375c569677aec8139c03d864ba858c8dc8 --- frameworks/native/common/include/sensors_ipc_interface_code.h | 2 +- frameworks/native/common/src/miscdevice_service_proxy.cpp | 2 +- services/miscdevice_service/src/miscdevice_service_stub.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/native/common/include/sensors_ipc_interface_code.h b/frameworks/native/common/include/sensors_ipc_interface_code.h index 57544fc..e437d56 100644 --- a/frameworks/native/common/include/sensors_ipc_interface_code.h +++ b/frameworks/native/common/include/sensors_ipc_interface_code.h @@ -33,7 +33,7 @@ enum class MiscdeviceInterfaceCode { TURN_OFF, PlAY_PATTERN, GET_DELAY_TIME, - SEND_CLIENT_REMOTE_OBJECT, + TRANSFER_CLIENT_REMOTE_OBJECT, }; } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/common/src/miscdevice_service_proxy.cpp b/frameworks/native/common/src/miscdevice_service_proxy.cpp index 06c7aa9..3b87356 100644 --- a/frameworks/native/common/src/miscdevice_service_proxy.cpp +++ b/frameworks/native/common/src/miscdevice_service_proxy.cpp @@ -407,7 +407,7 @@ int32_t MiscdeviceServiceProxy::TransferClientRemoteObject(const sptr remote = Remote(); CHKPR(remote, ERROR); - int32_t ret = remote->SendRequest(static_cast(MiscdeviceInterfaceCode::SEND_CLIENT_REMOTE_OBJECT), + int32_t ret = remote->SendRequest(static_cast(MiscdeviceInterfaceCode::TRANSFER_CLIENT_REMOTE_OBJECT), data, reply, option); if (ret != NO_ERROR) { HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION", diff --git a/services/miscdevice_service/src/miscdevice_service_stub.cpp b/services/miscdevice_service/src/miscdevice_service_stub.cpp index c66a167..1157683 100644 --- a/services/miscdevice_service/src/miscdevice_service_stub.cpp +++ b/services/miscdevice_service/src/miscdevice_service_stub.cpp @@ -64,7 +64,7 @@ MiscdeviceServiceStub::MiscdeviceServiceStub() &MiscdeviceServiceStub::PlayPatternStub; baseFuncs_[static_cast(MiscdeviceInterfaceCode::GET_DELAY_TIME)] = &MiscdeviceServiceStub::GetDelayTimeStub; - baseFuncs_[static_cast(MiscdeviceInterfaceCode::SEND_CLIENT_REMOTE_OBJECT)] = + baseFuncs_[static_cast(MiscdeviceInterfaceCode::TRANSFER_CLIENT_REMOTE_OBJECT)] = &MiscdeviceServiceStub::TransferClientRemoteObjectStub; } -- Gitee