From b423caffc3290c74b9a90392ac99fa00a0b9ff68 Mon Sep 17 00:00:00 2001 From: bailu1992 Date: Thu, 13 Mar 2025 21:14:45 +0800 Subject: [PATCH] sensor Adds interface monitoring Signed-off-by: bailu1992 --- bundle.json | 3 +- frameworks/native/BUILD.gn | 1 + frameworks/native/src/sensor_agent_proxy.cpp | 65 ++++++++++++++++---- utils/common/BUILD.gn | 2 + utils/common/include/sensor_xcollie.h | 41 ++++++++++++ utils/common/src/sensor_xcollie.cpp | 32 ++++++++++ 6 files changed, 132 insertions(+), 12 deletions(-) create mode 100644 utils/common/include/sensor_xcollie.h create mode 100644 utils/common/src/sensor_xcollie.cpp diff --git a/bundle.json b/bundle.json index b5207319..e3dd35de 100755 --- a/bundle.json +++ b/bundle.json @@ -32,7 +32,8 @@ "memmgr", "safwk", "samgr", - "eventhandler" + "eventhandler", + "hicollie" ], "third_party": [] }, diff --git a/frameworks/native/BUILD.gn b/frameworks/native/BUILD.gn index 5b048b1b..54f979cb 100644 --- a/frameworks/native/BUILD.gn +++ b/frameworks/native/BUILD.gn @@ -87,6 +87,7 @@ ohos_shared_library("libsensor_client") { external_deps = [ "c_utils:utils", "eventhandler:libeventhandler", + "hicollie:libhicollie", "hilog:libhilog", "ipc:ipc_single", "samgr:samgr_proxy", diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index caeb154d..2b10d902 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -17,6 +17,7 @@ #include "print_sensor_data.h" #include "sensor_service_client.h" +#include "sensor_xcollie.h" #undef LOG_TAG #define LOG_TAG "SensorAgentProxy" using namespace OHOS::HiviewDFX; @@ -110,7 +111,10 @@ int32_t SensorAgentProxy::CreateSensorDataChannel() SEN_HILOGE("Create data channel failed, ret:%{public}d", ret); return ret; } - ret = SEN_CLIENT.TransferDataChannel(dataChannel_); + { + SensorXcollie SensorXcollie("SensorAgentProxy:TransferDataChannel", XCOLLIE_TIMEOUT_5S); + ret = SEN_CLIENT.TransferDataChannel(dataChannel_); + } if (ret != ERR_OK) { auto destroyRet = dataChannel_->DestroySensorDataChannel(); SEN_HILOGE("Transfer data channel failed, ret:%{public}d, destroyRet:%{public}d", ret, destroyRet); @@ -135,7 +139,10 @@ int32_t SensorAgentProxy::DestroySensorDataChannel() SEN_HILOGE("Destroy data channel failed, ret:%{public}d", ret); return ret; } - ret = SEN_CLIENT.DestroyDataChannel(); + { + SensorXcollie SensorXcollie("SensorAgentProxy:DestroyDataChannel", XCOLLIE_TIMEOUT_5S); + ret = SEN_CLIENT.DestroyDataChannel(); + } if (ret != ERR_OK) { SEN_HILOGE("Destroy service data channel fail, ret:%{public}d", ret); return ret; @@ -168,7 +175,11 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use SEN_HILOGE("Subscribe user first"); return ERROR; } - int32_t ret = SEN_CLIENT.EnableSensor(sensorId, samplingInterval_, reportInterval_); + int32_t ret = 0; + { + SensorXcollie SensorXcollie("SensorAgentProxy:EnableSensor", XCOLLIE_TIMEOUT_15S); + ret = SEN_CLIENT.EnableSensor(sensorId, samplingInterval_, reportInterval_); + } if (ret != 0) { SEN_HILOGE("Enable sensor failed, ret:%{public}d", ret); subscribeSet.erase(user); @@ -207,7 +218,11 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u subscribeSet.erase(user); if (subscribeSet.empty()) { subscribeMap_.erase(sensorId); - int32_t ret = SEN_CLIENT.DisableSensor(sensorId); + int32_t ret = 0; + { + SensorXcollie SensorXcollie("SensorAgentProxy:DisableSensor", XCOLLIE_TIMEOUT_15S); + ret = SEN_CLIENT.DisableSensor(sensorId); + } if (ret != 0) { SEN_HILOGE("DisableSensor failed, ret:%{public}d", ret); return ret; @@ -352,7 +367,11 @@ void SensorAgentProxy::ClearSensorInfos() const int32_t SensorAgentProxy::ConvertSensorInfos() const { CALL_LOG_ENTER; - std::vector sensorList = SEN_CLIENT.GetSensorList(); + std::vector sensorList; + { + SensorXcollie SensorXcollie("SensorAgentProxy:GetSensorList", XCOLLIE_TIMEOUT_5S); + sensorList = SEN_CLIENT.GetSensorList(); + } if (sensorList.empty()) { SEN_HILOGE("Get sensor lists failed"); return ERROR; @@ -416,7 +435,11 @@ int32_t SensorAgentProxy::SuspendSensors(int32_t pid) SEN_HILOGE("Pid is invalid, pid:%{public}d", pid); return PARAMETER_ERROR; } - int32_t ret = SEN_CLIENT.SuspendSensors(pid); + int32_t ret = 0; + { + SensorXcollie SensorXcollie("SensorAgentProxy:SuspendSensors", XCOLLIE_TIMEOUT_5S); + ret = SEN_CLIENT.SuspendSensors(pid); + } if (ret != ERR_OK) { SEN_HILOGD("Suspend sensors failed, ret:%{public}d", ret); } @@ -430,7 +453,11 @@ int32_t SensorAgentProxy::ResumeSensors(int32_t pid) SEN_HILOGE("Pid is invalid, pid:%{public}d", pid); return PARAMETER_ERROR; } - int32_t ret = SEN_CLIENT.ResumeSensors(pid); + int32_t ret = 0; + { + SensorXcollie SensorXcollie("SensorAgentProxy:ResumeSensors", XCOLLIE_TIMEOUT_5S); + ret = SEN_CLIENT.ResumeSensors(pid); + } if (ret != ERR_OK) { SEN_HILOGD("Resume sensors failed, ret:%{public}d", ret); } @@ -453,7 +480,11 @@ int32_t SensorAgentProxy::GetSensorActiveInfos(int32_t pid, sensorActiveInfos_ = nullptr; } std::vector activeInfoList; - int32_t ret = SEN_CLIENT.GetActiveInfoList(pid, activeInfoList); + int32_t ret = 0; + { + SensorXcollie SensorXcollie("SensorAgentProxy:GetActiveInfoList", XCOLLIE_TIMEOUT_5S); + ret = SEN_CLIENT.GetActiveInfoList(pid, activeInfoList); + } if (ret != ERR_OK) { SEN_HILOGE("Get active info list failed, ret:%{public}d", ret); return ret; @@ -487,7 +518,11 @@ int32_t SensorAgentProxy::Register(SensorActiveInfoCB callback) { CHKPR(callback, OHOS::Sensors::ERROR); CHKPR(dataChannel_, INVALID_POINTER); - int32_t ret = SEN_CLIENT.Register(callback, dataChannel_); + int32_t ret = 0; + { + SensorXcollie SensorXcollie("SensorAgentProxy:Register", XCOLLIE_TIMEOUT_5S); + ret = SEN_CLIENT.Register(callback, dataChannel_); + } if (ret != ERR_OK) { SEN_HILOGE("Register sensor active info callback failed, ret:%{public}d", ret); } @@ -497,7 +532,11 @@ int32_t SensorAgentProxy::Register(SensorActiveInfoCB callback) int32_t SensorAgentProxy::Unregister(SensorActiveInfoCB callback) { CHKPR(callback, OHOS::Sensors::ERROR); - int32_t ret = SEN_CLIENT.Unregister(callback); + int32_t ret = 0; + { + SensorXcollie SensorXcollie("SensorAgentProxy:Unregister", XCOLLIE_TIMEOUT_5S); + ret = SEN_CLIENT.Unregister(callback); + } if (ret != ERR_OK) { SEN_HILOGE("Unregister sensor active info callback failed, ret:%{public}d", ret); } @@ -506,7 +545,11 @@ int32_t SensorAgentProxy::Unregister(SensorActiveInfoCB callback) int32_t SensorAgentProxy::ResetSensors() const { - int32_t ret = SEN_CLIENT.ResetSensors(); + int32_t ret = 0; + { + SensorXcollie SensorXcollie("SensorAgentProxy:ResetSensors", XCOLLIE_TIMEOUT_5S); + ret = SEN_CLIENT.ResetSensors(); + } if (ret != ERR_OK) { SEN_HILOGE("Reset sensors failed, ret:%{public}d", ret); } diff --git a/utils/common/BUILD.gn b/utils/common/BUILD.gn index e250a34d..84ff5e44 100644 --- a/utils/common/BUILD.gn +++ b/utils/common/BUILD.gn @@ -24,6 +24,7 @@ ohos_shared_library("libsensor_utils") { "src/sensor_basic_data_channel.cpp", "src/sensor_basic_info.cpp", "src/sensor_channel_info.cpp", + "src/sensor_xcollie.cpp", ] cflags = [ @@ -55,6 +56,7 @@ ohos_shared_library("libsensor_utils") { "access_token:libaccesstoken_sdk", "access_token:libprivacy_sdk", "c_utils:utils", + "hicollie:libhicollie", "hilog:libhilog", "ipc:ipc_single", ] diff --git a/utils/common/include/sensor_xcollie.h b/utils/common/include/sensor_xcollie.h new file mode 100644 index 00000000..34f54055 --- /dev/null +++ b/utils/common/include/sensor_xcollie.h @@ -0,0 +1,41 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef MISCDEVICE_XCOLLIE_H +#define MISCDEVICE_XCOLLIE_H + +#include + +#include "xcollie/xcollie.h" +#include "xcollie/xcollie_define.h" + +namespace OHOS { +namespace Sensors { +const unsigned int XCOLLIE_TIMEOUT_15S = 15; +const unsigned int XCOLLIE_TIMEOUT_5S = 5; + +class SensorXcollie { +public: + SensorXcollie(const std::string& tag, unsigned int timeoutSeconds = 10, + std::function func = nullptr, void *arg = nullptr, + unsigned int flag = HiviewDFX::XCOLLIE_FLAG_LOG); + ~SensorXcollie(); +private: + int id_; + std::string tag_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // MISCDEVICE_XCOLLIE_H diff --git a/utils/common/src/sensor_xcollie.cpp b/utils/common/src/sensor_xcollie.cpp new file mode 100644 index 00000000..527c774b --- /dev/null +++ b/utils/common/src/sensor_xcollie.cpp @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "sensor_xcollie.h" + +namespace OHOS { +namespace Sensors { +SensorXcollie::SensorXcollie(const std::string& tag, unsigned int timeoutSeconds, + std::function func, void *arg, unsigned int flag) +{ + id_ = HiviewDFX::XCollie::GetInstance().SetTimer(tag, timeoutSeconds, func, arg, flag); + tag_ = tag; +} + +SensorXcollie::~SensorXcollie() +{ + HiviewDFX::XCollie::GetInstance().CancelTimer(id_); +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file -- Gitee