From 65c8aa85993fe5eaf7219d1b704a16e92602f732 Mon Sep 17 00:00:00 2001 From: shenpeixing Date: Fri, 11 Apr 2025 18:17:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=A5=E8=BF=91=E5=85=89?= =?UTF-8?q?=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: shenpeixing --- frameworks/native/src/sensor_agent_proxy.cpp | 20 +++++++++++++------- utils/common/include/print_sensor_data.h | 1 + utils/common/src/print_sensor_data.cpp | 10 ++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index 2d023b4d..094696db 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -386,6 +386,13 @@ int32_t SensorAgentProxy::ConvertSensorInfos() const SEN_HILOGE("The number of sensors exceeds the maximum value"); return ERROR; } + if (sensorInfoCount_ > 0 && sensorInfoCount_ == static_cast(count)) { + return SUCCESS; + } else if (sensorInfoCount_ > 0 && sensorInfoCount_ != static_cast(count)) { + SEN_HILOGW("sensorInfos_ error, sensorInfoCount_:%{public}d, sensorListCount:%{public}d", sensorInfoCount_, + static_cast(count)); + ClearSensorInfos(); + } sensorInfos_ = (SensorInfo *)malloc(sizeof(SensorInfo) * count); CHKPR(sensorInfos_, ERROR); for (size_t i = 0; i < count; ++i) { @@ -420,16 +427,15 @@ 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; } *sensorInfo = sensorInfos_; *count = sensorInfoCount_; + PrintSensorData::GetInstance().PrintSensorInfo(sensorInfos_, sensorInfoCount_); return SUCCESS; } diff --git a/utils/common/include/print_sensor_data.h b/utils/common/include/print_sensor_data.h index 5d24b551..75550fc8 100644 --- a/utils/common/include/print_sensor_data.h +++ b/utils/common/include/print_sensor_data.h @@ -38,6 +38,7 @@ public: void SavePrintUserInfo(const RecordSensorCallback callback); void RemovePrintUserInfo(const RecordSensorCallback callback); void PrintSensorDataLog(const std::string &name, const SensorData &data); + 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 208472d9..cde86908 100644 --- a/utils/common/src/print_sensor_data.cpp +++ b/utils/common/src/print_sensor_data.cpp @@ -279,5 +279,15 @@ void PrintSensorData::PrintSensorDataLog(const std::string &name, const SensorDa str.append("\n"); SEN_HILOGI("%{public}s SensorData: %{public}s", name.c_str(), str.c_str()); } + +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