From b2696b796b73a63fbf59b59a973e60f245f3d9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B1=E6=98=AF=E9=93=B8=E5=B8=81=E6=8D=8F?= Date: Mon, 21 Apr 2025 15:53:58 +0800 Subject: [PATCH] fix acc light sensor Signed-off-by:echos2019 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 就是铸币捏 --- .../native/include/sensor_agent_proxy.h | 7 +++ frameworks/native/src/sensor_agent_proxy.cpp | 51 +++++++++++++------ utils/common/include/print_sensor_data.h | 1 + utils/common/src/print_sensor_data.cpp | 10 ++++ 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/frameworks/native/include/sensor_agent_proxy.h b/frameworks/native/include/sensor_agent_proxy.h index 5bac0b3b..22343996 100644 --- a/frameworks/native/include/sensor_agent_proxy.h +++ b/frameworks/native/include/sensor_agent_proxy.h @@ -69,6 +69,13 @@ private: std::map> unsubscribeMap_; }; +const int32_t CHECK_CODE = 0x00ABCDEF; + +struct SensorInfoCheck { + int32_t checkCode = CHECK_CODE; + SensorInfo *sensorInfos = nullptr; +}; + #define SENSOR_AGENT_IMPL OHOS::DelayedSingleton::GetInstance() } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index 188489b2..34df4706 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -29,7 +29,7 @@ namespace Sensors { namespace { constexpr uint32_t MAX_SENSOR_LIST_SIZE = 0Xffff; std::mutex sensorInfoMutex_; -SensorInfo *sensorInfos_ = nullptr; +SensorInfoCheck sensorInfoCheck_; std::mutex sensorActiveInfoMutex_; SensorActiveInfo *sensorActiveInfos_ = nullptr; int32_t sensorInfoCount_ = 0; @@ -334,9 +334,11 @@ void SensorAgentProxy::ClearSensorInfos() const free(sensorActiveInfos_); sensorActiveInfos_ = nullptr; } - CHKPV(sensorInfos_); - free(sensorInfos_); - sensorInfos_ = nullptr; + CHKPV(sensorInfoCheck_.sensorInfos); + free(sensorInfoCheck_.sensorInfos); + sensorInfoCheck_.sensorInfos = nullptr; + sensorInfoCheck_.checkCode = CHECK_CODE; + sensorInfoCount_ = 0; } int32_t SensorAgentProxy::ConvertSensorInfos() const @@ -352,10 +354,22 @@ int32_t SensorAgentProxy::ConvertSensorInfos() const SEN_HILOGE("The number of sensors exceeds the maximum value"); return ERROR; } - sensorInfos_ = (SensorInfo *)malloc(sizeof(SensorInfo) * count); - CHKPR(sensorInfos_, ERROR); + if (sensorInfoCount_ > 0 && sensorInfoCount_ == static_cast(count)) { + return SUCCESS; + } else if (sensorInfoCount_ > 0 && sensorInfoCount_ != static_cast(count) && + sensorInfoCheck_.checkCode == CHECK_CODE) { + SEN_HILOGW("sensorInfos_ error, sensorInfoCount_:%{public}d, sensorListCount:%{public}d", sensorInfoCount_, + static_cast(count)); + ClearSensorInfos(); + } else if (sensorInfoCheck_.checkCode != CHECK_CODE) { + SEN_HILOGE("CheckCode has been modified, %{public}d", sensorInfoCheck_.checkCode); + ClearSensorInfos(); + } + sensorInfoCheck_.sensorInfos = (SensorInfo *)malloc(sizeof(SensorInfo) * count); + CHKPR(sensorInfoCheck_.sensorInfos, ERROR); + SEN_HILOGI("Sensor count is %{public}zu", count); for (size_t i = 0; i < count; ++i) { - SensorInfo *sensorInfo = sensorInfos_ + i; + SensorInfo *sensorInfo = sensorInfoCheck_.sensorInfos + i; errno_t ret = strcpy_s(sensorInfo->sensorName, NAME_MAX_LEN, sensorList[i].GetSensorName().c_str()); CHKCR(ret == EOK, ERROR); @@ -375,6 +389,8 @@ int32_t SensorAgentProxy::ConvertSensorInfos() const sensorInfo->power = sensorList[i].GetPower(); sensorInfo->minSamplePeriod = sensorList[i].GetMinSamplePeriodNs(); sensorInfo->maxSamplePeriod = sensorList[i].GetMaxSamplePeriodNs(); + SEN_HILOGI("Sensor %{public}zu: sensorId is %{public}d, sensorTypeId is %{public}d", + i, sensorInfo->sensorId, sensorInfo->sensorTypeId); } sensorInfoCount_ = static_cast(count); return SUCCESS; @@ -386,16 +402,21 @@ int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) CHKPR(sensorInfo, OHOS::Sensors::ERROR); CHKPR(count, OHOS::Sensors::ERROR); std::lock_guard listLock(sensorInfoMutex_); - if (sensorInfos_ == nullptr) { - int32_t ret = ConvertSensorInfos(); - if (ret != SUCCESS) { - SEN_HILOGE("Convert sensor lists failed"); - ClearSensorInfos(); - return ERROR; - } + int32_t ret = ConvertSensorInfos(); + if (ret != SUCCESS) { + SEN_HILOGE("Convert sensor lists failed"); + ClearSensorInfos(); + return ERROR; + } + if (sensorInfoCheck_.checkCode != CHECK_CODE) { + SEN_HILOGE("CheckCode has been modified, %{public}d", sensorInfoCheck_.checkCode); + ClearSensorInfos(); + return ERROR; } - *sensorInfo = sensorInfos_; + CHKPR(sensorInfoCheck_.sensorInfos, OHOS::Sensors::ERROR); + *sensorInfo = sensorInfoCheck_.sensorInfos; *count = sensorInfoCount_; + PrintSensorData::GetInstance().PrintSensorInfo(sensorInfoCheck_.sensorInfos, sensorInfoCount_); return SUCCESS; } diff --git a/utils/common/include/print_sensor_data.h b/utils/common/include/print_sensor_data.h index 39970692..8f172d4c 100644 --- a/utils/common/include/print_sensor_data.h +++ b/utils/common/include/print_sensor_data.h @@ -37,6 +37,7 @@ public: bool IsContinuousType(int32_t sensorId); void SavePrintUserInfo(const RecordSensorCallback callback); void RemovePrintUserInfo(const RecordSensorCallback callback); + void PrintSensorInfo(SensorInfo *sensorInfos, int32_t sensorInfoCount); private: void PrintClientData(const SensorEvent &event); diff --git a/utils/common/src/print_sensor_data.cpp b/utils/common/src/print_sensor_data.cpp index 45881fdb..7023fef0 100644 --- a/utils/common/src/print_sensor_data.cpp +++ b/utils/common/src/print_sensor_data.cpp @@ -209,5 +209,15 @@ void PrintSensorData::ResetHdiCounter(int32_t sensorId) it->second.count = 0; it->second.lastTime = 0; } + +void PrintSensorData::PrintSensorInfo(SensorInfo *sensorInfos, int32_t sensorInfoCount) +{ + std::string combineSensorIds = ""; + for (int32_t i = 0; i < sensorInfoCount; ++i) { + combineSensorIds = combineSensorIds + std::to_string(sensorInfos[i].sensorTypeId) + " "; + } + SEN_HILOGI("PrintSensorInfo success, sensorIds:%{public}s, sensorInfoCount:%{public}d", combineSensorIds.c_str(), + sensorInfoCount); +} } // namespace Sensors } // namespace OHOS -- Gitee