diff --git a/frameworks/native/src/sensor_service_client.cpp b/frameworks/native/src/sensor_service_client.cpp index 5529438cf01bb6a6df8fdabd9c29e5ed0614aad2..e1c588ebc42450cfba021b47e490be8901dd86f0 100644 --- a/frameworks/native/src/sensor_service_client.cpp +++ b/frameworks/native/src/sensor_service_client.cpp @@ -38,8 +38,6 @@ namespace Sensors { using namespace OHOS::HiviewDFX; namespace { -constexpr int32_t GET_SERVICE_MAX_COUNT = 3; -constexpr uint32_t WAIT_MS = 200; #ifdef OHOS_BUILD_ENABLE_RUST extern "C" { void ReadClientPackets(RustStreamBuffer *, OHOS::Sensors::SensorServiceClient *, @@ -71,6 +69,10 @@ int32_t SensorServiceClient::InitServiceClient() std::lock_guard clientLock(clientMutex_); if (sensorServer_ != nullptr) { SEN_HILOGD("Already init"); + if (sensorList_.empty()) { + sensorList_ = sensorServer_->GetSensorList(); + SEN_HILOGW("sensorList is %{public}s", sensorList_.empty() ? "empty" : "not empty"); + } return ERR_OK; } if (sensorClientStub_ == nullptr) { @@ -78,25 +80,23 @@ int32_t SensorServiceClient::InitServiceClient() } auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); CHKPR(systemAbilityManager, SENSOR_NATIVE_SAM_ERR); - int32_t retry = 0; - while (retry < GET_SERVICE_MAX_COUNT) { - auto object = systemAbilityManager->GetSystemAbility(SENSOR_SERVICE_ABILITY_ID); - CHKPR(object, SENSOR_NATIVE_GET_SERVICE_ERR); - sensorServer_ = iface_cast(object); - if (sensorServer_ != nullptr) { - SEN_HILOGD("Get service success, retry:%{public}d", retry); - serviceDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); - CHKPR(serviceDeathObserver_, SENSOR_NATIVE_GET_SERVICE_ERR); - auto remoteObject = sensorServer_->AsObject(); - CHKPR(remoteObject, SENSOR_NATIVE_GET_SERVICE_ERR); - remoteObject->AddDeathRecipient(serviceDeathObserver_); - sensorList_ = sensorServer_->GetSensorList(); - return ERR_OK; + auto object = systemAbilityManager->GetSystemAbility(SENSOR_SERVICE_ABILITY_ID); + CHKPR(object, SENSOR_NATIVE_GET_SERVICE_ERR); + sensorServer_ = iface_cast(object); + if (sensorServer_ != nullptr) { + SEN_HILOGD("Get service success"); + serviceDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); + CHKPR(serviceDeathObserver_, SENSOR_NATIVE_GET_SERVICE_ERR); + auto remoteObject = sensorServer_->AsObject(); + CHKPR(remoteObject, SENSOR_NATIVE_GET_SERVICE_ERR); + remoteObject->AddDeathRecipient(serviceDeathObserver_); + sensorList_ = sensorServer_->GetSensorList(); + if (sensorList_.empty()) { + SEN_HILOGW("sensorList_ is empty when connecting to the service for the first time"); } - SEN_HILOGW("Get service failed, retry:%{public}d", retry); - std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS)); - retry++; + return ERR_OK; } + SEN_HILOGW("Get service failed"); HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "SERVICE_EXCEPTION", HiSysEvent::EventType::FAULT, "PKG_NAME", "InitServiceClient", "ERROR_CODE", SENSOR_NATIVE_GET_SERVICE_ERR); SEN_HILOGE("Get service failed"); @@ -222,11 +222,13 @@ int32_t SensorServiceClient::DestroyDataChannel() void SensorServiceClient::ReenableSensor() { CALL_LOG_ENTER; - std::lock_guard mapLock(mapMutex_); - for (const auto &it : sensorInfoMap_) { + { std::lock_guard clientLock(clientMutex_); - if (sensorServer_ != nullptr) { - sensorServer_->EnableSensor(it.first, it.second.GetSamplingPeriodNs(), it.second.GetMaxReportDelayNs()); + std::lock_guard mapLock(mapMutex_); + for (const auto &it : sensorInfoMap_) { + if (sensorServer_ != nullptr) { + sensorServer_->EnableSensor(it.first, it.second.GetSamplingPeriodNs(), it.second.GetMaxReportDelayNs()); + } } } if (!isConnected_) { @@ -256,6 +258,7 @@ void SensorServiceClient::ProcessDeathObserver(const wptr &object SEN_HILOGI("dataChannel_ is not nullptr"); dataChannel_->DestroySensorDataChannel(); dataChannel_->RestoreSensorDataChannel(); + SENSOR_AGENT_IMPL->SetIsChannelCreated(true); { std::lock_guard clientLock(clientMutex_); sensorServer_ = nullptr; diff --git a/hisysevent.yaml b/hisysevent.yaml index dacb428af770e7e4391327f5be0fed5f072da9bd..7cd09bad0515be301f104b054ca4595ee7598c2e 100755 --- a/hisysevent.yaml +++ b/hisysevent.yaml @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-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 @@ -48,4 +48,10 @@ HDF_SERVICE_EXCEPTION: SERVICE_EXCEPTION: __BASE: {type: FAULT, level: MINOR, desc: sensor service exception} PKG_NAME: {type: STRING, desc: package name} - ERROR_CODE: {type: INT32, desc: error code} \ No newline at end of file + ERROR_CODE: {type: INT32, desc: error code} + +EVENT_REPORT: + __BASE: {type: BEHAVIOR, level: CRITICAL, desc: Non consecutive and critical event reporting, preserve: true} + SENSOR_ID: {type: INT32, desc: Sensor Type} + TIMESTAMP: {type: STRING, desc: Timestamp} + DATA: {type: STRING, desc: Data Information} \ No newline at end of file diff --git a/services/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/hdi_connection/interface/src/sensor_hdi_connection.cpp index 7234e920cca84eef9dc33ba5ab3ce64d18686206..722476d32cffb9ef93adf18e5597647cc0deb352 100644 --- a/services/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -202,6 +202,11 @@ int32_t SensorHdiConnection::GetSensorList(std::vector &sensorList) { CHKPR(iSensorHdiConnection_, GET_SENSOR_LIST_ERR); std::lock_guard sensorLock(sensorMutex_); + if (sensorList_.empty()) { + if (iSensorHdiConnection_->GetSensorList(sensorList_) != ERR_OK) { + SEN_HILOGW("Get sensor list failed"); + } + } sensorList.assign(sensorList_.begin(), sensorList_.end()); #ifdef BUILD_VARIANT_ENG if (!hdiConnectionStatus_) { diff --git a/utils/common/include/print_sensor_data.h b/utils/common/include/print_sensor_data.h index d91a4db5c22372af6f9e2f8f65186bb10231eace..4c817967d34639be9daa934227308fde35cf1268 100644 --- a/utils/common/include/print_sensor_data.h +++ b/utils/common/include/print_sensor_data.h @@ -43,6 +43,8 @@ private: void PrintClientData(const SensorEvent &event); void PrintHdiData(const SensorData &sensorData); int32_t GetDataDimension(int32_t sensorId); + void ProcessHdiDFX(const SensorData &sensorData); + void ProcessClientDFX(const SensorEvent &event); struct LogPrintInfo { int32_t count { 0 }; int64_t lastTime { 0 }; @@ -62,4 +64,4 @@ private: }; } // namespace Sensors } // namespace OHOS -#endif // PRINT_SENSOR_DATA +#endif // PRINT_SENSOR_DATA \ No newline at end of file diff --git a/utils/common/src/print_sensor_data.cpp b/utils/common/src/print_sensor_data.cpp index 98feca686685a64dc747130154162e587d39919e..bd2900a099b21af17dc1f073887cb65870073a4f 100644 --- a/utils/common/src/print_sensor_data.cpp +++ b/utils/common/src/print_sensor_data.cpp @@ -18,6 +18,8 @@ #include #include +#include "hisysevent.h" + #include "sensor_errors.h" #undef LOG_TAG @@ -25,6 +27,7 @@ namespace OHOS { namespace Sensors { +using namespace OHOS::HiviewDFX; namespace { enum { ONE_DIMENSION = 1, @@ -42,6 +45,7 @@ const std::vector g_triggerSensorType = { SENSOR_TYPE_ID_PROXIMITY, SENSOR_TYPE_ID_HALL, SENSOR_TYPE_ID_WEAR_DETECTION, + SENSOR_TYPE_ID_PROXIMITY1, }; const std::vector g_continuousSensorType = { SENSOR_TYPE_ID_ACCELEROMETER, @@ -58,6 +62,10 @@ void PrintSensorData::ControlSensorHdiPrint(const SensorData &sensorData) auto triggerIt = std::find(g_triggerSensorType.begin(), g_triggerSensorType.end(), sensorData.sensorTypeId); if (triggerIt != g_triggerSensorType.end()) { PrintHdiData(sensorData); + if (sensorData.sensorTypeId != SENSOR_TYPE_ID_PROXIMITY && + sensorData.sensorTypeId != SENSOR_TYPE_ID_PROXIMITY1) { + ProcessHdiDFX(sensorData); + } } std::lock_guard hdiLoginfoLock(hdiLoginfoMutex_); auto it = hdiLoginfo_.find(sensorData.sensorTypeId); @@ -97,6 +105,24 @@ void PrintSensorData::PrintHdiData(const SensorData &sensorData) SEN_HILOGI("SensorData: %{public}s", str.c_str()); } +void PrintSensorData::ProcessHdiDFX(const SensorData &sensorData) +{ + std::string strData; + auto data = reinterpret_cast(sensorData.data); + int32_t dataDim = GetDataDimension(sensorData.sensorTypeId); + for (int32_t i = 0; i < dataDim; i++) { + CHKPV(data); + strData.append(std::to_string(*data)); + if (i != dataDim - 1) { + strData.append(", "); + } + ++data; + } + HiSysEventWrite(HiSysEvent::Domain::SENSOR, "EVENT_REPORT", + HiSysEvent::EventType::BEHAVIOR, "SENSOR_ID", sensorData.sensorTypeId, "TIMESTAMP", + std::to_string(sensorData.timestamp), "DATA", strData); +} + int32_t PrintSensorData::GetDataDimension(int32_t sensorId) { switch (sensorId) { @@ -125,6 +151,9 @@ void PrintSensorData::ControlSensorClientPrint(const RecordSensorCallback callba auto triggerIt = std::find(g_triggerSensorType.begin(), g_triggerSensorType.end(), event.sensorTypeId); if (triggerIt != g_triggerSensorType.end()) { PrintClientData(event); + if (event.sensorTypeId != SENSOR_TYPE_ID_PROXIMITY && event.sensorTypeId != SENSOR_TYPE_ID_PROXIMITY1) { + ProcessClientDFX(event); + } } auto continuosIt = std::find(g_continuousSensorType.begin(), g_continuousSensorType.end(), event.sensorTypeId); @@ -169,6 +198,24 @@ void PrintSensorData::PrintClientData(const SensorEvent &event) SEN_HILOGI("SensorData: %{public}s", str.c_str()); } +void PrintSensorData::ProcessClientDFX(const SensorEvent &event) +{ + std::string strData; + auto data = reinterpret_cast(event.data); + int32_t dataDim = GetDataDimension(event.sensorTypeId); + for (int32_t i = 0; i < dataDim; i++) { + CHKPV(data); + strData.append(std::to_string(*data)); + if (i != dataDim - 1) { + strData.append(", "); + } + ++data; + } + HiSysEventWrite(HiSysEvent::Domain::SENSOR, "EVENT_REPORT", + HiSysEvent::EventType::BEHAVIOR, "SENSOR_ID", event.sensorTypeId, "TIMESTAMP", + std::to_string(event.timestamp), "DATA", strData); +} + bool PrintSensorData::IsContinuousType(int32_t sensorId) { return std::find(g_continuousSensorType.begin(), g_continuousSensorType.end(),