From 5c3b5fd8c0f2425ba5f5691be4993d3781d91ccc Mon Sep 17 00:00:00 2001 From: h00514358 Date: Thu, 21 Oct 2021 12:34:43 +0000 Subject: [PATCH 1/2] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- .../sensor/include/sensor_service_client.h | 1 + .../native/sensor/src/sensor_agent_proxy.cpp | 19 ++++++-------- .../sensor/src/sensor_service_client.cpp | 26 +++++++++++++++++++ interfaces/plugin/src/sensor_js.cpp | 8 +++--- services/sensor/src/sensor_service.cpp | 5 ++++ 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/frameworks/native/sensor/include/sensor_service_client.h b/frameworks/native/sensor/include/sensor_service_client.h index 2a2ac290..304f6637 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -44,6 +44,7 @@ private: int32_t InitServiceClient(); void UpdateSensorInfoMap(uint32_t sensorId, int64_t samplingPeroid, int64_t maxReportDelay); void DeleteSensorInfoItem(uint32_t sensorId); + bool IsValidSensorId(uint32_t sensorId); std::mutex clientMutex_; sptr serviceDeathObserver_; sptr sensorServer_; diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 5bfb89dc..d6219a7b 100755 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -203,22 +203,18 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u HiLog::Error(LABEL, "%{public}s user is null or sensorId is invalid", __func__); return OHOS::Sensors::ERROR; } - if (g_subscribeMap.find(sensorId) != g_subscribeMap.end()) { - HiLog::Error(LABEL, "%{public}s unsubscribe sensorId first", __func__); - return OHOS::Sensors::ERROR; - } - - if (g_unsubscribeMap.find(sensorId) == g_unsubscribeMap.end() || g_unsubscribeMap[sensorId] != user) { + if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { HiLog::Error(LABEL, "%{public}s unsubscribe sensorId first", __func__); return OHOS::Sensors::ERROR; } + g_subscribeMap.erase(sensorId); + g_unsubscribeMap[sensorId] = user; SensorServiceClient &client = SensorServiceClient::GetInstance(); int32_t ret = client.DisableSensor(sensorId); if (ret != 0) { HiLog::Error(LABEL, "%{public}s disable sensor failed, ret: %{public}d", __func__, ret); return OHOS::Sensors::ERROR; } - g_unsubscribeMap.erase(sensorId); return OHOS::Sensors::SUCCESS; } @@ -265,12 +261,13 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * HiLog::Error(LABEL, "%{public}s user is null or sensorId is invalid", __func__); return OHOS::Sensors::ERROR; } - if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { - HiLog::Error(LABEL, "%{public}s subscribe sensorId first", __func__); + + if (g_unsubscribeMap.find(sensorId) == g_unsubscribeMap.end() || g_unsubscribeMap[sensorId] != user) { + HiLog::Error(LABEL, "%{public}s unsubscribe sensorId first", __func__); return OHOS::Sensors::ERROR; } - g_subscribeMap.erase(sensorId); - g_unsubscribeMap[sensorId] = user; + + g_unsubscribeMap.erase(sensorId); if (g_subscribeMap.empty()) { DestroySensorDataChannel(); g_isChannelCreated = false; diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index 9460cba8..7eeb3f01 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -75,9 +75,27 @@ int32_t SensorServiceClient::InitServiceClient() return SENSOR_NATIVE_GET_SERVICE_ERR; } +bool SensorServiceClient::IsValidSensorId(uint32_t sensorId) +{ + if (sensorList_.empty()) { + HiLog::Error(LABEL, "%{public}s sensorList_ cannot be empty", __func__); + return false; + } + for (auto &sensor : sensorList_) { + if (sensor.GetSensorList() == sensorId) { + return true; + } + } + return false; +} + int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay) { HiLog::Debug(LABEL, "%{public}s begin", __func__); + if (!IsValidSensorId(sensorId)) { + HiLog::Error(LABEL, "%{public}s sensorList_ cannot be empty", __func__); + return SENSOR_NATIVE_SAM_ERR; + } int32_t ret = InitServiceClient(); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); @@ -93,6 +111,10 @@ int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPer int32_t SensorServiceClient::DisableSensor(uint32_t sensorId) { HiLog::Debug(LABEL, "%{public}s begin", __func__); + if (!IsValidSensorId(sensorId)) { + HiLog::Error(LABEL, "%{public}s sensorList_ cannot be empty", __func__); + return SENSOR_NATIVE_SAM_ERR; + } int32_t ret = InitServiceClient(); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); @@ -108,6 +130,10 @@ int32_t SensorServiceClient::DisableSensor(uint32_t sensorId) int32_t SensorServiceClient::RunCommand(uint32_t sensorId, int32_t cmdType, int32_t params) { HiLog::Debug(LABEL, "%{public}s begin", __func__); + if (!IsValidSensorId(sensorId)) { + HiLog::Error(LABEL, "%{public}s sensorList_ cannot be empty", __func__); + return SENSOR_NATIVE_SAM_ERR; + } int32_t ret = InitServiceClient(); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index ae12480c..0e51f8aa 100755 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -87,14 +87,14 @@ static const SensorUser user = { static int32_t UnsubscribeSensor(int32_t sensorTypeId) { HiLog::Info(LABEL, "%{public}s in", __func__); - int32_t ret = UnsubscribeSensor(sensorTypeId, &user); + int32_t ret = DeactivateSensor(sensorTypeId, &user); if (ret < 0) { - HiLog::Error(LABEL, "%{public}s UnsubscribeSensor failed", __func__); + HiLog::Error(LABEL, "%{public}s DeactivateSensor failed", __func__); return ret; } - ret = DeactivateSensor(sensorTypeId, &user); + ret = UnsubscribeSensor(sensorTypeId, &user); if (ret < 0) { - HiLog::Error(LABEL, "%{public}s DeactivateSensor failed", __func__); + HiLog::Error(LABEL, "%{public}s UnsubscribeSensor failed", __func__); return ret; } HiLog::Info(LABEL, "%{public}s left", __func__); diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index a715761c..f33cb447 100755 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -258,6 +258,7 @@ ErrCode SensorService::EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, auto ret = SaveSubscriber(sensorId, samplingPeriodNs, maxReportDelayNs); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s SaveSubscriber failed", __func__); + clientInfo_.RemoveSubscriber(sensorId, this->GetCallingPid()); return ret; } @@ -285,6 +286,10 @@ ErrCode SensorService::DisableSensor(uint32_t sensorId) HiLog::Error(LABEL, "%{public}s clientPid is invalid, clientPid : %{public}d", __func__, clientPid); return CLIENT_PID_INVALID_ERR; } + if (clientInfo_.GetSensorState(sensorId) != SENSOR_ENABLED) { + HiLog::Error(LABEL, "%{public}s sensor should be enabled first", __func__); + return DISABLE_SENSOR_ERR; + } if (sensorManager_.IsOtherClientUsingSensor(sensorId, clientPid)) { HiLog::Warn(LABEL, "%{public}s other client is using this sensor now, cannot disable", __func__); return ERR_OK; -- Gitee From 2e1628a13809dd59cb2e9219a1fcf3e26c6a877f Mon Sep 17 00:00:00 2001 From: h00514358 Date: Thu, 21 Oct 2021 12:41:32 +0000 Subject: [PATCH 2/2] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- frameworks/native/sensor/src/sensor_agent_proxy.cpp | 4 ++-- frameworks/native/sensor/src/sensor_data_channel.cpp | 6 +++++- frameworks/native/sensor/src/sensor_service_client.cpp | 8 ++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index d6219a7b..09adc437 100755 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -204,7 +204,7 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u return OHOS::Sensors::ERROR; } if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { - HiLog::Error(LABEL, "%{public}s unsubscribe sensorId first", __func__); + HiLog::Error(LABEL, "%{public}s subscribe sensorId first", __func__); return OHOS::Sensors::ERROR; } g_subscribeMap.erase(sensorId); @@ -263,7 +263,7 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * } if (g_unsubscribeMap.find(sensorId) == g_unsubscribeMap.end() || g_unsubscribeMap[sensorId] != user) { - HiLog::Error(LABEL, "%{public}s unsubscribe sensorId first", __func__); + HiLog::Error(LABEL, "%{public}s deactivate sensorId first", __func__); return OHOS::Sensors::ERROR; } diff --git a/frameworks/native/sensor/src/sensor_data_channel.cpp b/frameworks/native/sensor/src/sensor_data_channel.cpp index 38287de6..ca972392 100755 --- a/frameworks/native/sensor/src/sensor_data_channel.cpp +++ b/frameworks/native/sensor/src/sensor_data_channel.cpp @@ -142,7 +142,11 @@ int32_t SensorDataChannel::DestroySensorDataChannel() } int32_t fd = GetReceiveDataFd(); eventHandler_->RemoveFileDescriptorListener(fd); - + eventHandler_ = nullptr; + if (eventRunner_ != nullptr) { + eventRunner_->Stop(); + eventRunner_ = nullptr; + } threadStop_ = true; if (sensorDataThread_.joinable()) { diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index 7eeb3f01..68293575 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -82,7 +82,7 @@ bool SensorServiceClient::IsValidSensorId(uint32_t sensorId) return false; } for (auto &sensor : sensorList_) { - if (sensor.GetSensorList() == sensorId) { + if (sensor.GetSensorId() == sensorId) { return true; } } @@ -93,7 +93,7 @@ int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPer { HiLog::Debug(LABEL, "%{public}s begin", __func__); if (!IsValidSensorId(sensorId)) { - HiLog::Error(LABEL, "%{public}s sensorList_ cannot be empty", __func__); + HiLog::Error(LABEL, "%{public}s sensorId is invalid", __func__); return SENSOR_NATIVE_SAM_ERR; } int32_t ret = InitServiceClient(); @@ -112,7 +112,7 @@ int32_t SensorServiceClient::DisableSensor(uint32_t sensorId) { HiLog::Debug(LABEL, "%{public}s begin", __func__); if (!IsValidSensorId(sensorId)) { - HiLog::Error(LABEL, "%{public}s sensorList_ cannot be empty", __func__); + HiLog::Error(LABEL, "%{public}s sensorId is invalid", __func__); return SENSOR_NATIVE_SAM_ERR; } int32_t ret = InitServiceClient(); @@ -131,7 +131,7 @@ int32_t SensorServiceClient::RunCommand(uint32_t sensorId, int32_t cmdType, int3 { HiLog::Debug(LABEL, "%{public}s begin", __func__); if (!IsValidSensorId(sensorId)) { - HiLog::Error(LABEL, "%{public}s sensorList_ cannot be empty", __func__); + HiLog::Error(LABEL, "%{public}s sensorId is invalid", __func__); return SENSOR_NATIVE_SAM_ERR; } int32_t ret = InitServiceClient(); -- Gitee