diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index 2d023b4d0eef11e2ccd2c755ccf232d6c9f80411..094696db0fc61868f8505285f3362ea3ca5c366b 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 5d24b551065af2f6d019c7e522b7efc358e134ff..75550fc8ddec8f6348f628448917f3f5d9d3b106 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 208472d9e209e632b01afe064eacd20f065bb42e..cde8690873286480643ca94e7006c7f398ffb189 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