From 56cb4c1e2f6a19e6ad13b641a13c3303bfb1a680 Mon Sep 17 00:00:00 2001 From: lixiangpeng5 Date: Wed, 18 Jun 2025 18:34:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dsensor=E4=B8=8B=E7=BA=BF?= =?UTF-8?q?=E5=90=8E=E6=97=A0=E6=B3=95off=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lixiangpeng5 --- frameworks/native/src/sensor_agent_proxy.cpp | 33 +++++++++++++++----- services/src/sensor_service.cpp | 10 ++++-- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index e89c504d..d8e0e2af 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -210,12 +210,13 @@ int32_t SensorAgentProxy::DeactivateSensor(const SensorDescription &sensorDesc, sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId, sensorDesc.location); CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); - if (!SEN_CLIENT.IsValid(sensorDesc)) { + std::lock_guard subscribeLock(subscribeMutex_); + if (!(SEN_CLIENT.IsValid(sensorDesc) || + ((!SEN_CLIENT.IsValid(sensorDesc)) && subscribeMap_.find(sensorDesc) != subscribeMap_.end()))) { SEN_HILOGE("sensorDesc is invalid, deviceId:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d", sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId); return PARAMETER_ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); if (subscribeMap_.find(sensorDesc) == subscribeMap_.end()) { SEN_HILOGE("Subscribe sensorId first"); return OHOS::Sensors::ERROR; @@ -234,6 +235,10 @@ int32_t SensorAgentProxy::DeactivateSensor(const SensorDescription &sensorDesc, subscribeMap_.erase(sensorDesc); int32_t ret = 0; { + if (!SEN_CLIENT.IsValid(sensorDesc)) { + SEN_HILOGW("No need to call DisableSensor"); + return OHOS::Sensors::SUCCESS; + } SensorXcollie SensorXcollie("SensorAgentProxy:DisableSensor", XCOLLIE_TIMEOUT_15S); ret = SEN_CLIENT.DisableSensor(sensorDesc); } @@ -318,12 +323,14 @@ int32_t SensorAgentProxy::UnsubscribeSensor(const SensorDescription &sensorDesc, sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId); CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); - if (!SEN_CLIENT.IsValid(sensorDesc)) { - SEN_HILOGE("sensorId is invalid, %{public}d", sensorDesc.sensorType); - return PARAMETER_ERROR; - } { std::lock_guard subscribeLock(subscribeMutex_); + if (!(SEN_CLIENT.IsValid(sensorDesc) || + ((!SEN_CLIENT.IsValid(sensorDesc)) && unsubscribeMap_.find(sensorDesc) != unsubscribeMap_.end()))) { + SEN_HILOGE("sensorDesc is invalid, deviceId:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d", + sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId); + return PARAMETER_ERROR; + } if (unsubscribeMap_.find(sensorDesc) == unsubscribeMap_.end()) { SEN_HILOGE("Deactivate sensorId first"); return OHOS::Sensors::ERROR; @@ -817,6 +824,18 @@ bool SensorAgentProxy::UpdataSensorInfo(SensorPlugData info) free(sensorInfos); sensorInfos = nullptr; } else { + { + std::lock_guard subscribeLock(subscribeMutex_); + int32_t ret = 0; + if (subscribeMap_.find({info.deviceId, info.sensorTypeId, info.sensorId, info.location}) != + subscribeMap_.end()) { + SensorXcollie SensorXcollie("SensorAgentProxy:DisableSensor", XCOLLIE_TIMEOUT_15S); + ret = SEN_CLIENT.DisableSensor({info.deviceId, info.sensorTypeId, info.sensorId, info.location}); + } + if (ret != 0) { + SEN_HILOGE("DisableSensor failed, ret:%{public}d", ret); + } + } if (!(SEN_CLIENT.EraseCacheSensorList(info))) { SEN_HILOGE("EraseCacheSensorList failed"); return false; @@ -845,10 +864,10 @@ void SensorAgentProxy::EraseCacheSensorInfos(SensorPlugData info) bool SensorAgentProxy::HandlePlugSensorData(const SensorPlugData &info) __attribute__((no_sanitize("cfi"))) { CALL_LOG_ENTER; - std::lock_guard subscribePlugLock(subscribePlugMutex_); if (!UpdataSensorInfo(info)) { return false; } + std::lock_guard subscribePlugLock(subscribePlugMutex_); SensorStatusEvent event; UpdataSensorStatusEvent(event, info); for (const auto &it : subscribeSet_) { diff --git a/services/src/sensor_service.cpp b/services/src/sensor_service.cpp index 0c9ebd77..35774f43 100644 --- a/services/src/sensor_service.cpp +++ b/services/src/sensor_service.cpp @@ -486,7 +486,7 @@ ErrCode SensorService::SensorReportEvent(const SensorDescription &sensorDesc, in ErrCode SensorService::DisableSensor(const SensorDescription &sensorDesc, int32_t pid) { CALL_LOG_ENTER; - if (!CheckSensorId(sensorDesc)) { + if (!(CheckSensorId(sensorDesc) || ((!CheckSensorId(sensorDesc)) && clientInfo_.GetSensorState(sensorDesc)))) { SEN_HILOGE("sensorDesc is invalid"); return ERR_NO_INIT; } @@ -502,8 +502,12 @@ ErrCode SensorService::DisableSensor(const SensorDescription &sensorDesc, int32_ } #ifdef HDF_DRIVERS_INTERFACE_SENSOR if (sensorHdiConnection_.DisableSensor(sensorDesc) != ERR_OK) { - SEN_HILOGE("DisableSensor is failed"); - return DISABLE_SENSOR_ERR; + if (CheckSensorId(sensorDesc)) { + SEN_HILOGE("DisableSensor is failed"); + return DISABLE_SENSOR_ERR; + } + SEN_HILOGW("DisableSensor is failed, deviceId:%{public}d, sensorType:%{public}d, sensorId:%{public}d", + sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId); } #endif // HDF_DRIVERS_INTERFACE_SENSOR int32_t uid = clientInfo_.GetUidByPid(pid); -- Gitee