From 0cd7aca2b13be8d98e40c687b16894a11999125d Mon Sep 17 00:00:00 2001 From: lixiangpeng5 Date: Fri, 20 Jun 2025 18:34:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E6=B8=85=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lixiangpeng5 --- .../native/include/sensor_agent_proxy.h | 6 ++--- .../native/include/sensor_client_proxy.h | 27 ++++++++++--------- .../native/include/sensor_service_client.h | 2 +- frameworks/native/src/sensor_agent_proxy.cpp | 16 ++++++----- frameworks/native/src/sensor_client_stub.cpp | 4 ++- .../native/src/sensor_service_client.cpp | 2 +- .../include/i_sensor_hdi_connection.h | 2 +- services/include/sensor_service.h | 2 +- services/src/sensor_service.cpp | 2 +- utils/common/src/sensor.cpp | 5 ++++ 10 files changed, 41 insertions(+), 27 deletions(-) diff --git a/frameworks/native/include/sensor_agent_proxy.h b/frameworks/native/include/sensor_agent_proxy.h index dd6198cc..9a4ab691 100644 --- a/frameworks/native/include/sensor_agent_proxy.h +++ b/frameworks/native/include/sensor_agent_proxy.h @@ -66,9 +66,9 @@ private: int32_t UpdateSensorInfo(SensorInfo* sensorInfo, const Sensor& sensor); int32_t UpdateSensorInfosCache(const std::vector& deviceSensorList); bool FindSensorInfo(int32_t deviceId, int32_t sensorIndex, int32_t sensorTypeId); - void UpdateSensorStatusEvent(SensorStatusEvent &event, SensorPlugData info); - bool UpdateSensorInfo(SensorPlugData info); - void EraseCacheSensorInfos(SensorPlugData info); + void UpdateSensorStatusEvent(SensorStatusEvent &event, const SensorPlugData &info); + bool UpdateSensorInfo(const SensorPlugData &info); + void EraseCacheSensorInfos(const SensorPlugData &info); static std::recursive_mutex subscribeMutex_; static std::recursive_mutex subscribePlugMutex_; static std::mutex chanelMutex_; diff --git a/frameworks/native/include/sensor_client_proxy.h b/frameworks/native/include/sensor_client_proxy.h index 8f7674c0..234a96dc 100755 --- a/frameworks/native/include/sensor_client_proxy.h +++ b/frameworks/native/include/sensor_client_proxy.h @@ -34,48 +34,51 @@ public: MessageParcel dataParcel; MessageParcel replyParcel; if (!dataParcel.WriteInterfaceToken(GetDescriptor())) { - SEN_HILOGD("Failed to write descriptor to parcelable"); + SEN_HILOGE("Failed to write descriptor to parcelable"); return PARAMETER_ERROR; } if (!dataParcel.WriteInt32(info.deviceId)) { - SEN_HILOGD("Failed to write deviceId to parcelable"); + SEN_HILOGE("Failed to write deviceId to parcelable"); return PARAMETER_ERROR; } if (!dataParcel.WriteInt32(info.sensorTypeId)) { - SEN_HILOGD("Failed to write sensorTypeId to parcelable"); + SEN_HILOGE("Failed to write sensorTypeId to parcelable"); return PARAMETER_ERROR; } if (!dataParcel.WriteInt32(info.sensorId)) { - SEN_HILOGD("Failed to write sensorId to parcelable"); + SEN_HILOGE("Failed to write sensorId to parcelable"); return PARAMETER_ERROR; } if (!dataParcel.WriteInt32(info.location)) { - SEN_HILOGD("Failed to write location to parcelable"); + SEN_HILOGE("Failed to write location to parcelable"); return PARAMETER_ERROR; } if (!dataParcel.WriteString(info.deviceName)) { - SEN_HILOGD("Failed to write deviceName to parcelable"); + SEN_HILOGE("Failed to write deviceName to parcelable"); return PARAMETER_ERROR; } if (!dataParcel.WriteInt32(info.status)) { - SEN_HILOGD("Failed to write status to parcelable"); + SEN_HILOGE("Failed to write status to parcelable"); return PARAMETER_ERROR; } if (!dataParcel.WriteInt32(info.reserved)) { - SEN_HILOGD("Failed to write reserved to parcelable"); + SEN_HILOGE("Failed to write reserved to parcelable"); return PARAMETER_ERROR; } if (!dataParcel.WriteInt64(info.timestamp)) { - SEN_HILOGD("Failed to write timestamp to parcelable"); + SEN_HILOGE("Failed to write timestamp to parcelable"); return PARAMETER_ERROR; } + if (Remote() == nullptr) { + SEN_HILOGE("Remote() is nullptr"); + return ERROR; + } int error = Remote()->SendRequest(PROCESS_PLUG_EVENT, dataParcel, replyParcel, option); if (error != ERR_NONE) { - SEN_HILOGD("failed, error code is: %{public}d", error); + SEN_HILOGE("failed, error code is: %{public}d", error); return PARAMETER_ERROR; } - int result = (error == ERR_NONE) ? replyParcel.ReadInt32() : -1; - return result; + return SUCCESS; } private: diff --git a/frameworks/native/include/sensor_service_client.h b/frameworks/native/include/sensor_service_client.h index 084c0797..fcacdb83 100755 --- a/frameworks/native/include/sensor_service_client.h +++ b/frameworks/native/include/sensor_service_client.h @@ -54,7 +54,7 @@ public: int32_t CreateClientRemoteObject(); int32_t TransferClientRemoteObject(); int32_t DestroyClientRemoteObject(); - bool EraseCacheSensorList(SensorPlugData info); + bool EraseCacheSensorList(const SensorPlugData &info); int32_t GetLocalDeviceId(int32_t &deviceId); private: diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index 162ef71d..af6b173e 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -565,7 +565,12 @@ int32_t SensorAgentProxy::UpdateSensorInfosCache(const std::vector& sing if (newSensorsCount == 0) { return SUCCESS; } - size_t newTotalCount = sensorInfoCount_ + newSensorsCount; + if (sensorInfoCount_ < 0 || sensorInfoCount_ > MAX_SENSOR_LIST_SIZE) { + SEN_HILOGE("sensorInfoCount_ invalid, sensorInfoCount_:%{public}d", sensorInfoCount_); + return ERROR; + } + size_t currentInfoCount = static_cast(sensorInfoCount_); + size_t newTotalCount = currentInfoCount + newSensorsCount; if (newTotalCount > MAX_SENSOR_LIST_SIZE) { SEN_HILOGE("The number of sensors exceeds the maximum value"); return ERROR; @@ -578,10 +583,9 @@ int32_t SensorAgentProxy::UpdateSensorInfosCache(const std::vector& sing if (sensorInfoCheck_.sensorInfos) { std::copy(sensorInfoCheck_.sensorInfos, sensorInfoCheck_.sensorInfos + sensorInfoCount_, newSensorInfos.get()); } - size_t currentIndex = sensorInfoCount_; for (const auto& sensor : singleDevSensors) { if (!FindSensorInfo(sensor.GetDeviceId(), sensor.GetSensorId(), sensor.GetSensorTypeId())) { - UpdateSensorInfo(&newSensorInfos[currentIndex++], sensor); + UpdateSensorInfo(&newSensorInfos[currentInfoCount++], sensor); } } sensorInfoCheck_.sensorInfos = newSensorInfos.release(); @@ -792,7 +796,7 @@ int32_t SensorAgentProxy::UnsubscribeSensorPlug(const SensorUser *user) return OHOS::Sensors::SUCCESS; } -void SensorAgentProxy::UpdateSensorStatusEvent(SensorStatusEvent &event, SensorPlugData info) +void SensorAgentProxy::UpdateSensorStatusEvent(SensorStatusEvent &event, const SensorPlugData &info) { event.sensorType = info.sensorTypeId; event.sensorId = info.sensorId; @@ -809,7 +813,7 @@ void SensorAgentProxy::UpdateSensorStatusEvent(SensorStatusEvent &event, SensorP event.timestamp = info.timestamp; } -bool SensorAgentProxy::UpdateSensorInfo(SensorPlugData info) +bool SensorAgentProxy::UpdateSensorInfo(const SensorPlugData &info) { CALL_LOG_ENTER; if (info.status == SENSOR_ONLINE) { @@ -844,7 +848,7 @@ bool SensorAgentProxy::UpdateSensorInfo(SensorPlugData info) return true; } -void SensorAgentProxy::EraseCacheSensorInfos(SensorPlugData info) +void SensorAgentProxy::EraseCacheSensorInfos(const SensorPlugData &info) { std::lock_guard listLock(sensorInfoMutex_); for (int32_t i = 0; i < sensorInfoCount_; ++i) { diff --git a/frameworks/native/src/sensor_client_stub.cpp b/frameworks/native/src/sensor_client_stub.cpp index 630aa717..5af01488 100644 --- a/frameworks/native/src/sensor_client_stub.cpp +++ b/frameworks/native/src/sensor_client_stub.cpp @@ -73,7 +73,9 @@ int32_t SensorClientStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Me return PARAMETER_ERROR; } int32_t result = ProcessPlugEvent(info); - reply.WriteInt32(result); + if (result != NO_ERROR) { + SEN_HILOGE("Process plug event failed"); + } return NO_ERROR; } diff --git a/frameworks/native/src/sensor_service_client.cpp b/frameworks/native/src/sensor_service_client.cpp index 4f2f29ca..ef54eb57 100644 --- a/frameworks/native/src/sensor_service_client.cpp +++ b/frameworks/native/src/sensor_service_client.cpp @@ -852,7 +852,7 @@ void SensorServiceClient::SetDeviceStatus(uint32_t deviceStatus) #endif // HIVIEWDFX_HITRACE_ENABLE } -bool SensorServiceClient::EraseCacheSensorList(SensorPlugData info) +bool SensorServiceClient::EraseCacheSensorList(const SensorPlugData &info) { CALL_LOG_ENTER; std::lock_guard clientLock(clientMutex_); diff --git a/services/hdi_connection/interface/include/i_sensor_hdi_connection.h b/services/hdi_connection/interface/include/i_sensor_hdi_connection.h index 2a45ac6e..76c51e0a 100644 --- a/services/hdi_connection/interface/include/i_sensor_hdi_connection.h +++ b/services/hdi_connection/interface/include/i_sensor_hdi_connection.h @@ -25,7 +25,7 @@ namespace OHOS { namespace Sensors { using OHOS::HDI::Sensor::V3_0::SensorPlugInfo; -using DevicePlugCallback = std::function; +using DevicePlugCallback = std::function; class ISensorHdiConnection { public: ISensorHdiConnection() = default; diff --git a/services/include/sensor_service.h b/services/include/sensor_service.h index 9cb8a7b3..59e002f2 100644 --- a/services/include/sensor_service.h +++ b/services/include/sensor_service.h @@ -71,7 +71,7 @@ private: void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; ErrCode CheckAuthAndParameter(const SensorDescription &sensorDesc, int64_t samplingPeriodNs, int64_t maxReportDelayNs); - void ReportPlugEventCallback(const SensorPlugInfo sensorPlugInfo); + void ReportPlugEventCallback(const SensorPlugInfo &sensorPlugInfo); ErrCode SensorReportEvent(const SensorDescription &sensorDesc, int64_t samplingPeriodNs, int64_t maxReportDelayNs, int32_t pid); diff --git a/services/src/sensor_service.cpp b/services/src/sensor_service.cpp index 23b9b5a3..ccc4a74b 100644 --- a/services/src/sensor_service.cpp +++ b/services/src/sensor_service.cpp @@ -1004,7 +1004,7 @@ ErrCode SensorService::DestroyClientRemoteObject(const sptr &sens return ERR_OK; } -void SensorService::ReportPlugEventCallback(const SensorPlugInfo info) +void SensorService::ReportPlugEventCallback(const SensorPlugInfo &info) { CALL_LOG_ENTER; if (info.status == SENSOR_ONLINE) { diff --git a/utils/common/src/sensor.cpp b/utils/common/src/sensor.cpp index f1091453..c78a9910 100644 --- a/utils/common/src/sensor.cpp +++ b/utils/common/src/sensor.cpp @@ -291,6 +291,7 @@ bool Sensor::ReadFromParcel(Parcel &parcel) } SensorDescriptionIPC::SensorDescriptionIPC() + :deviceId(0), sensorType(0), sensorId(0), location(0) {} SensorDescriptionIPC::SensorDescriptionIPC(int32_t deviceId, int32_t sensorTypeId, int32_t sensorId, int32_t location) @@ -327,21 +328,25 @@ SensorDescriptionIPC* SensorDescriptionIPC::Unmarshalling(Parcel &data) } if (!(data.ReadInt32(sensorDesc->deviceId))) { SEN_HILOGE("Read deviceId failed"); + delete sensorDesc; sensorDesc = nullptr; return sensorDesc; } if (!(data.ReadInt32(sensorDesc->sensorType))) { SEN_HILOGE("Read sensorTypeId failed"); + delete sensorDesc; sensorDesc = nullptr; return sensorDesc; } if (!(data.ReadInt32(sensorDesc->sensorId))) { SEN_HILOGE("Read sensorId failed"); + delete sensorDesc; sensorDesc = nullptr; return sensorDesc; } if (!(data.ReadInt32(sensorDesc->location))) { SEN_HILOGE("Read location_ failed"); + delete sensorDesc; sensorDesc = nullptr; return sensorDesc; } -- Gitee