diff --git a/frameworks/native/include/sensor_agent_proxy.h b/frameworks/native/include/sensor_agent_proxy.h index 58e8bd1f5ff5d0d2be2c815839a6694491ccc6d2..83bea1a8c3b81d202be785ad2a1c363a5ccc6d29 100644 --- a/frameworks/native/include/sensor_agent_proxy.h +++ b/frameworks/native/include/sensor_agent_proxy.h @@ -66,6 +66,13 @@ private: static std::mutex createChannelMutex_; }; +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 2b10d9025f8c2cfc577b9b942084884bb9ee1067..c02e0f7e4e95d859b91df4d4cec359aa817b5048 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -26,7 +26,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; @@ -359,9 +359,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 @@ -381,10 +383,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); @@ -404,6 +418,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; @@ -415,16 +431,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 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