diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index cadeabbcc79cc0558f83b7a8579af5a9f5418f73..162ef71d9ce3427be4cf0f948c1896106f711554 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; @@ -816,6 +823,18 @@ bool SensorAgentProxy::UpdateSensorInfo(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; @@ -844,10 +863,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 (!UpdateSensorInfo(info)) { return false; } + std::lock_guard subscribePlugLock(subscribePlugMutex_); SensorStatusEvent event; UpdateSensorStatusEvent(event, info); for (const auto &it : subscribeSet_) { diff --git a/services/src/sensor_service.cpp b/services/src/sensor_service.cpp index 2f4273cb39950a9ccdb26a3085263808ae62c0c3..f0099b94ebc41d8f9c1478b54124166af18e41a3 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);