From 26565852ce6decc7d8ed362915f843b364c2a3df Mon Sep 17 00:00:00 2001 From: maan4 Date: Tue, 5 Nov 2024 12:51:16 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9cleancode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maan4 --- .../native/include/sensor_agent_proxy.h | 3 +- frameworks/native/src/sensor_agent_proxy.cpp | 51 +++++++++++++++---- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/frameworks/native/include/sensor_agent_proxy.h b/frameworks/native/include/sensor_agent_proxy.h index 5d58e112..9300fbc5 100644 --- a/frameworks/native/include/sensor_agent_proxy.h +++ b/frameworks/native/include/sensor_agent_proxy.h @@ -63,8 +63,7 @@ private: static std::mutex chanelMutex_; OHOS::sptr dataChannel_ = nullptr; std::atomic_bool isChannelCreated_ = false; - int64_t samplingInterval_ = -1; - int64_t reportInterval_ = -1; + std::map>> intervalMap_; std::map> subscribeMap_; std::map> unsubscribeMap_; }; diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index 188489b2..e25bf5a0 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -16,6 +16,8 @@ #include "sensor_agent_proxy.h" #include +#include +#include #include "print_sensor_data.h" #include "securec.h" @@ -23,6 +25,7 @@ #include "sensor_service_client.h" #undef LOG_TAG #define LOG_TAG "SensorAgentProxy" +#define GETTID []()->int32_t { return static_cast(syscall(SYS_gettid)); } using namespace OHOS::HiviewDFX; namespace OHOS { namespace Sensors { @@ -32,7 +35,7 @@ std::mutex sensorInfoMutex_; SensorInfo *sensorInfos_ = nullptr; std::mutex sensorActiveInfoMutex_; SensorActiveInfo *sensorActiveInfos_ = nullptr; -int32_t sensorInfoCount_ = 0; +int32_t g_sensorInfoCount = 0; } // namespace #define SEN_CLIENT SensorServiceClient::GetInstance() @@ -147,10 +150,7 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use { CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); - if (samplingInterval_ < 0 || reportInterval_ < 0) { - SEN_HILOGE("SamplingPeriod or reportInterval_ is invalid"); - return ERROR; - } + if (!SEN_CLIENT.IsValid(sensorId)) { SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return PARAMETER_ERROR; @@ -160,12 +160,29 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use SEN_HILOGE("Subscribe sensorId first"); return ERROR; } + if (intervalMap_.find(sensorId) == intervalMap_.end()) { + SEN_HILOGE("Please set SamplingPeriod or ReportInterval first"); + return ERROR; + } auto& subscribeSet = subscribeMap_[sensorId]; if (subscribeSet.find(user) == subscribeSet.end()) { SEN_HILOGE("Subscribe user first"); return ERROR; } - int32_t ret = SEN_CLIENT.EnableSensor(sensorId, samplingInterval_, reportInterval_); + auto it = intervalMap_[sensorId]; + auto samplingNum = std::min_element(it.begin(), it.end(), + [](std::map>::const_reference a, + std::map>::const_reference b) { + return a.second.first < b.second.first; + }); + int64_t samplingInterval = samplingNum->second.first; + auto reportNum = std::min_element(it.begin(), it.end(), + [](std::map>::const_reference a, + std::map>::const_reference b) { + return a.second.first < b.second.first; + }); + int64_t reportInterval = reportNum->second.second; + int32_t ret = SEN_CLIENT.EnableSensor(sensorId, samplingInterval, reportInterval); if (ret != 0) { SEN_HILOGE("Enable sensor failed, ret:%{public}d", ret); subscribeSet.erase(user); @@ -190,6 +207,13 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u SEN_HILOGE("Subscribe sensorId first"); return OHOS::Sensors::ERROR; } + int32_t tid = GETTID(); + if (intervalMap_[sensorId].find(tid) != intervalMap_[sensorId].end()) { + intervalMap_[sensorId].erase(tid); + } + if (intervalMap_[sensorId].empty()) { + intervalMap_.erase(sensorId); + } auto& subscribeSet = subscribeMap_[sensorId]; if (subscribeSet.find(user) == subscribeSet.end()) { SEN_HILOGE("Subscribe user first"); @@ -233,8 +257,15 @@ int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int SEN_HILOGE("Subscribe user first"); return OHOS::Sensors::ERROR; } - samplingInterval_ = samplingInterval; - reportInterval_ = reportInterval; + int32_t tid = GETTID(); + if (intervalMap_.find(sensorId) == intervalMap_.end() || + intervalMap_[sensorId].find(tid) == intervalMap_[sensorId].end()) { + intervalMap_[sensorId][tid] = std::make_pair(samplingInterval, reportInterval); + } else { + intervalMap_[sensorId][tid].first = std::min(samplingInterval, intervalMap_[sensorId][tid].first); + intervalMap_[sensorId][tid].second = std::min(reportInterval, intervalMap_[sensorId][tid].second); + } + return OHOS::Sensors::SUCCESS; } @@ -376,7 +407,7 @@ int32_t SensorAgentProxy::ConvertSensorInfos() const sensorInfo->minSamplePeriod = sensorList[i].GetMinSamplePeriodNs(); sensorInfo->maxSamplePeriod = sensorList[i].GetMaxSamplePeriodNs(); } - sensorInfoCount_ = static_cast(count); + g_sensorInfoCount = static_cast(count); return SUCCESS; } @@ -395,7 +426,7 @@ int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) } } *sensorInfo = sensorInfos_; - *count = sensorInfoCount_; + *count = g_sensorInfoCount; return SUCCESS; } -- Gitee