From d85e6aa6ba286f80b9bcdb689dccac6e02a27b4d Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Fri, 2 Aug 2024 11:18:44 +0800 Subject: [PATCH 1/2] Added trigger sensor type filtering Signed-off-by: wuzhihuitmac Change-Id: I536623ea60b6b3818a9ead659921fd9426257a93 --- .../adapter/src/sensor_event_callback.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/services/hdi_connection/adapter/src/sensor_event_callback.cpp b/services/hdi_connection/adapter/src/sensor_event_callback.cpp index 85b8a466..29449815 100644 --- a/services/hdi_connection/adapter/src/sensor_event_callback.cpp +++ b/services/hdi_connection/adapter/src/sensor_event_callback.cpp @@ -14,6 +14,8 @@ */ #include "sensor_event_callback.h" +#include + #include "hdi_connection.h" #include "sensor_agent_type.h" #include "sensor_errors.h" @@ -35,6 +37,13 @@ enum { SEVEN_DIMENSION = 7, DEFAULT_DIMENSION = 16 }; +const std::set g_sensorTypeTrigger = { + SENSOR_TYPE_ID_PROXIMITY, + SENSOR_TYPE_ID_DROP_DETECTION, + SENSOR_TYPE_ID_HALL, + SENSOR_TYPE_ID_HALL_EXT, + SENSOR_TYPE_ID_PROXIMITY1 +}; } // namespace int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents &event) @@ -60,6 +69,9 @@ int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents &event) sensorData.sensorTypeId == SENSOR_TYPE_ID_DROP_DETECTION) { sensorData.mode = SENSOR_ON_CHANGE; } + if (g_sensorTypeTrigger.find(sensorData.sensorTypeId) != g_sensorTypeTrigger.end()) { + sensorData.mode = SENSOR_ON_CHANGE; + } CHKPR(sensorData.data, ERR_NO_INIT); if (sensorData.sensorTypeId == SENSOR_TYPE_ID_HEADPOSTURE) { sensorData.dataLen = HEADPOSTURE_DATA_SIZE; -- Gitee From 32118d12306309f665fe60b32c05759337e15e21 Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Tue, 6 Aug 2024 21:13:01 +0800 Subject: [PATCH 2/2] add log print about sensor data report Signed-off-by: wuzhihuitmac Change-Id: Ibae1b1fd8faab12cab1e9d1d542f80ebf4eec810 --- frameworks/js/napi/BUILD.gn | 5 +- .../js/napi/include/async_callback_info.h | 4 +- frameworks/native/src/sensor_agent_proxy.cpp | 8 + interfaces/inner_api/sensor_agent_type.h | 4 + .../adapter/include/sensor_event_callback.h | 7 - .../adapter/src/sensor_event_callback.cpp | 74 +------ services/src/sensor_service.cpp | 4 + utils/common/BUILD.gn | 1 + utils/common/include/print_sensor_data.h | 59 ++++++ utils/common/src/print_sensor_data.cpp | 185 ++++++++++++++++++ 10 files changed, 269 insertions(+), 82 deletions(-) create mode 100644 utils/common/include/print_sensor_data.h create mode 100644 utils/common/src/print_sensor_data.cpp diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 9dd4e8bb..51d5dc43 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -37,7 +37,10 @@ ohos_shared_library("libsensor") { cfi_cross_dso = true debug = false } - deps = [ "$SUBSYSTEM_DIR/frameworks/native:sensor_interface_native" ] + deps = [ + "$SUBSYSTEM_DIR/frameworks/native:sensor_interface_native", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils" + ] external_deps = [ "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", diff --git a/frameworks/js/napi/include/async_callback_info.h b/frameworks/js/napi/include/async_callback_info.h index fe9b012a..00de4748 100644 --- a/frameworks/js/napi/include/async_callback_info.h +++ b/frameworks/js/napi/include/async_callback_info.h @@ -72,7 +72,7 @@ struct RationMatrixData { float inclinationMatrix[THREE_DIMENSIONAL_MATRIX_LENGTH]; }; -struct SensorData { +struct CallbackSensorData { int32_t sensorTypeId; uint32_t dataLength; float data[DATA_LENGTH]; @@ -86,7 +86,7 @@ struct ReserveData { }; union CallbackData { - SensorData sensorData; + CallbackSensorData sensorData; GeomagneticData geomagneticData; RationMatrixData rationMatrixData; ReserveData reserveData; diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index a714a3cb..bb2e7fd7 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -17,6 +17,7 @@ #include +#include "print_sensor_data.h" #include "securec.h" #include "sensor_errors.h" #include "sensor_service_client.h" @@ -75,6 +76,7 @@ void SensorAgentProxy::HandleSensorData(SensorEvent *events, int32_t num, void * RecordSensorCallback fun = user->callback; CHKPV(fun); fun(&eventStream); + PrintSensorData::GetInstance().ControlSensorClientPrint(user, eventStream); } } } @@ -244,6 +246,9 @@ int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *us if (!status.second) { SEN_HILOGD("User has been subscribed"); } + if (PrintSensorData::GetInstance().IsContinuousType(sensorId)) { + PrintSensorData::GetInstance().SavePrintUserInfo(user); + } return OHOS::Sensors::SUCCESS; } @@ -277,6 +282,9 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * if (unsubscribeSet.empty()) { unsubscribeMap_.erase(sensorId); } + if (PrintSensorData::GetInstance().IsContinuousType(sensorId)) { + PrintSensorData::GetInstance().RemovePrintUserInfo(user); + } return OHOS::Sensors::SUCCESS; } diff --git a/interfaces/inner_api/sensor_agent_type.h b/interfaces/inner_api/sensor_agent_type.h index 22ceecb4..d7b45cff 100644 --- a/interfaces/inner_api/sensor_agent_type.h +++ b/interfaces/inner_api/sensor_agent_type.h @@ -184,6 +184,10 @@ typedef struct SensorUser { char name[NAME_MAX_LEN]; /**< Name of the sensor data subscriber */ RecordSensorCallback callback; /**< Callback for reporting sensor data */ UserData *userData = nullptr; /**< Reserved field for the sensor data subscriber */ + bool operator==( const SensorUser& user) const + { + return callback == user.callback; + } } SensorUser; /** diff --git a/services/hdi_connection/adapter/include/sensor_event_callback.h b/services/hdi_connection/adapter/include/sensor_event_callback.h index f1c62ff6..2d81cb85 100644 --- a/services/hdi_connection/adapter/include/sensor_event_callback.h +++ b/services/hdi_connection/adapter/include/sensor_event_callback.h @@ -30,13 +30,6 @@ class SensorEventCallback : public ISensorCallback { public: virtual ~SensorEventCallback() {} int32_t OnDataEvent(const HdfSensorEvents &event) override; -private: - void ControlSensorPrint(const SensorData &sensorData); - void PrintSensorData(const SensorData &sensorData); - int32_t GetDataDimension(int32_t sensorId); - int64_t postureLastTs_ = 0; - int64_t ambientLightLastTs_ = 0; - int64_t magneticFieldLastTs_ = 0; }; } // namespace Sensors } // namespace OHOS diff --git a/services/hdi_connection/adapter/src/sensor_event_callback.cpp b/services/hdi_connection/adapter/src/sensor_event_callback.cpp index 0f90a59f..93a021f8 100644 --- a/services/hdi_connection/adapter/src/sensor_event_callback.cpp +++ b/services/hdi_connection/adapter/src/sensor_event_callback.cpp @@ -17,6 +17,7 @@ #include #include "hdi_connection.h" +#include "print_sensor_data.h" #include "sensor_agent_type.h" #include "sensor_errors.h" @@ -29,14 +30,6 @@ using namespace OHOS::HiviewDFX; namespace { std::unique_ptr HdiConnection_ = std::make_unique(); constexpr int32_t HEADPOSTURE_DATA_SIZE = 20; -constexpr int64_t LOG_INTERVAL = 60000000000; -enum { - ONE_DIMENSION = 1, - TWO_DIMENSION = 2, - THREE_DIMENSION = 3, - SEVEN_DIMENSION = 7, - DEFAULT_DIMENSION = 16 -}; const std::set g_sensorTypeTrigger = { SENSOR_TYPE_ID_PROXIMITY, SENSOR_TYPE_ID_DROP_DETECTION, @@ -68,9 +61,6 @@ int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents &event) if (g_sensorTypeTrigger.find(sensorData.sensorTypeId) != g_sensorTypeTrigger.end()) { sensorData.mode = SENSOR_ON_CHANGE; } - if (g_sensorTypeTrigger.find(sensorData.sensorTypeId) != g_sensorTypeTrigger.end()) { - sensorData.mode = SENSOR_ON_CHANGE; - } CHKPR(sensorData.data, ERR_NO_INIT); if (sensorData.sensorTypeId == SENSOR_TYPE_ID_HEADPOSTURE) { sensorData.dataLen = HEADPOSTURE_DATA_SIZE; @@ -90,71 +80,11 @@ int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents &event) sensorData.data[i] = event.data[i]; } } - ControlSensorPrint(sensorData); + PrintSensorData::GetInstance().ControlSensorHdiPrint(sensorData); std::unique_lock lk(ISensorHdiConnection::dataMutex_); (void)(reportDataCallback_->*(reportDataCb_))(&sensorData, reportDataCallback_); ISensorHdiConnection::dataCondition_.notify_one(); return ERR_OK; } - -void SensorEventCallback::ControlSensorPrint(const SensorData &sensorData) -{ - if (sensorData.sensorTypeId == SENSOR_TYPE_ID_HALL_EXT || sensorData.sensorTypeId == SENSOR_TYPE_ID_PROXIMITY - || sensorData.sensorTypeId == SENSOR_TYPE_ID_HALL) { - PrintSensorData(sensorData); - } - if ((sensorData.sensorTypeId == SENSOR_TYPE_ID_POSTURE) - && ((postureLastTs_ == 0) || (sensorData.timestamp - postureLastTs_ >= LOG_INTERVAL))) { - PrintSensorData(sensorData); - postureLastTs_ = sensorData.timestamp; - } - if ((sensorData.sensorTypeId == SENSOR_TYPE_ID_AMBIENT_LIGHT) - && ((ambientLightLastTs_ == 0) || (sensorData.timestamp - ambientLightLastTs_ >= LOG_INTERVAL))) { - PrintSensorData(sensorData); - ambientLightLastTs_ = sensorData.timestamp; - } - if ((sensorData.sensorTypeId == SENSOR_TYPE_ID_MAGNETIC_FIELD) - && ((magneticFieldLastTs_ == 0) || (sensorData.timestamp - magneticFieldLastTs_ >= LOG_INTERVAL))) { - PrintSensorData(sensorData); - magneticFieldLastTs_ = sensorData.timestamp; - } -} - -void SensorEventCallback::PrintSensorData(const SensorData &sensorData) -{ - std::string str; - str += "sensorId: " + std::to_string(sensorData.sensorTypeId) + ", "; - str += "timestamp: " + std::to_string(sensorData.timestamp) + ", "; - int32_t dataDim = GetDataDimension(sensorData.sensorTypeId); - auto data = reinterpret_cast(sensorData.data); - for (int32_t i = 0; i < dataDim; ++i) { - str.append(std::to_string(*data)); - if (i != dataDim - 1) { - str.append(", "); - } - ++data; - } - str.append("\n"); - SEN_HILOGI("SensorData: %{public}s", str.c_str()); -} - -int32_t SensorEventCallback::GetDataDimension(int32_t sensorId) -{ - switch (sensorId) { - case SENSOR_TYPE_ID_HALL: - case SENSOR_TYPE_ID_PROXIMITY: - return ONE_DIMENSION; - case SENSOR_TYPE_ID_HALL_EXT: - return TWO_DIMENSION; - case SENSOR_TYPE_ID_POSTURE: - return SEVEN_DIMENSION; - case SENSOR_TYPE_ID_AMBIENT_LIGHT: - case SENSOR_TYPE_ID_MAGNETIC_FIELD: - return THREE_DIMENSION; - default: - SEN_HILOGW("Unknown sensorId:%{public}d, size:%{public}d", sensorId, DEFAULT_DIMENSION); - return DEFAULT_DIMENSION; - } -} } // namespace Sensors } // namespace OHOS \ No newline at end of file diff --git a/services/src/sensor_service.cpp b/services/src/sensor_service.cpp index eef9a2f3..f72faf6c 100644 --- a/services/src/sensor_service.cpp +++ b/services/src/sensor_service.cpp @@ -27,6 +27,7 @@ #endif // MEMMGR_ENABLE #include "permission_util.h" +#include "print_sensor_data.h" #include "securec.h" #include "sensor.h" #include "sensor_dump.h" @@ -279,6 +280,7 @@ ErrCode SensorService::EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, if (isReportActiveInfo_) { ReportActiveInfo(sensorId, pid); } + PrintSensorData::GetInstance().ResetHdiCounter(sensorId); return ERR_OK; } auto ret = SaveSubscriber(sensorId, samplingPeriodNs, maxReportDelayNs); @@ -302,6 +304,7 @@ ErrCode SensorService::EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, if (isReportActiveInfo_) { ReportActiveInfo(sensorId, pid); } + PrintSensorData::GetInstance().ResetHdiCounter(sensorId); return ret; } @@ -331,6 +334,7 @@ ErrCode SensorService::DisableSensor(int32_t sensorId, int32_t pid) int32_t uid = clientInfo_.GetUidByPid(pid); clientInfo_.DestroyCmd(uid); clientInfo_.ClearDataQueue(sensorId); + PrintSensorData::GetInstance().ResetHdiCounter(sensorId); return sensorManager_.AfterDisableSensor(sensorId); } diff --git a/utils/common/BUILD.gn b/utils/common/BUILD.gn index 48bce26e..0d522496 100644 --- a/utils/common/BUILD.gn +++ b/utils/common/BUILD.gn @@ -18,6 +18,7 @@ ohos_shared_library("libsensor_utils") { sources = [ "src/active_info.cpp", "src/permission_util.cpp", + "src/print_sensor_data.cpp", "src/report_data_callback.cpp", "src/sensor.cpp", "src/sensor_basic_data_channel.cpp", diff --git a/utils/common/include/print_sensor_data.h b/utils/common/include/print_sensor_data.h new file mode 100644 index 00000000..6de8bddc --- /dev/null +++ b/utils/common/include/print_sensor_data.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2024 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 PRINT_SENSOR_DATA +#define PRINT_SENSOR_DATA + +#include + +#include "singleton.h" + +#include "sensor_agent_type.h" +#include "sensor_data_event.h" + +namespace OHOS { +namespace Sensors { + + +class PrintSensorData : public Singleton { +public: + PrintSensorData() = default; + virtual ~PrintSensorData() {}; + void ControlSensorClientPrint(const SensorUser *user, const SensorEvent &event); + void ControlSensorHdiPrint(const SensorData &sensorData); + void ResetHdiCounter(int32_t sensorId); + bool IsContinuousType(int32_t sensorId); + void SavePrintUserInfo(const SensorUser *user); + void RemovePrintUserInfo(const SensorUser *user); + +private: + void PrintClientData(const SensorEvent &event); + void PrintHdiData(const SensorData &sensorData); + int32_t GetDataDimension(int32_t sensorId); + struct LogPrintInfo { + int32_t count { 0 }; + int64_t lastTime { 0 }; + }; + LogPrintInfo info_; + std::map hdiLoginfos_ = { + {SENSOR_TYPE_ID_POSTURE, info_}, + {SENSOR_TYPE_ID_AMBIENT_LIGHT, info_}, + {SENSOR_TYPE_ID_MAGNETIC_FIELD, info_}, + }; + std::map clientLoginfos_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // PRINT_SENSOR_DATA diff --git a/utils/common/src/print_sensor_data.cpp b/utils/common/src/print_sensor_data.cpp new file mode 100644 index 00000000..195ace40 --- /dev/null +++ b/utils/common/src/print_sensor_data.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2024 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 "print_sensor_data.h" + +#include +#include + +#include "sensor_errors.h" + +#undef LOG_TAG +#define LOG_TAG "PrintSensorData" + +namespace OHOS { +namespace Sensors { +namespace { +enum { + ONE_DIMENSION = 1, + TWO_DIMENSION = 2, + THREE_DIMENSION = 3, + SEVEN_DIMENSION = 7, + DEFAULT_DIMENSION = 16 +}; +constexpr int64_t LOG_INTERVAL = 60000000000; +constexpr int32_t PRINT_TIMES = 20; + +const std::set g_triggerSensorType = { + SENSOR_TYPE_ID_HALL_EXT, + SENSOR_TYPE_ID_PROXIMITY, + SENSOR_TYPE_ID_HALL, +}; +const std::set g_continuousSensorType = { + SENSOR_TYPE_ID_POSTURE, + SENSOR_TYPE_ID_AMBIENT_LIGHT, + SENSOR_TYPE_ID_MAGNETIC_FIELD, +}; +} + +void PrintSensorData::ControlSensorHdiPrint(const SensorData &sensorData) +{ + if (g_triggerSensorType.find(sensorData.sensorTypeId) != g_triggerSensorType.end()) { + PrintHdiData(sensorData); + } + auto it = hdiLoginfos_.find(sensorData.sensorTypeId); + if (it != hdiLoginfos_.end()) { + if (it->second.count < PRINT_TIMES) { + PrintHdiData(sensorData); + if (it->second.count == PRINT_TIMES - 1) { + it->second.lastTime = sensorData.timestamp; + } + it->second.count++; + } else { + if (sensorData.timestamp - it->second.lastTime >= LOG_INTERVAL) { + PrintHdiData(sensorData); + it->second.lastTime = sensorData.timestamp; + } + } + } +} + +void PrintSensorData::PrintHdiData(const SensorData &sensorData) +{ + std::string str; + str += "sensorId: " + std::to_string(sensorData.sensorTypeId) + ", "; + str += "timestamp: " + std::to_string(sensorData.timestamp / 1e9) + ", "; + int32_t dataDim = GetDataDimension(sensorData.sensorTypeId); + auto data = reinterpret_cast(sensorData.data); + for (int32_t i = 0; i < dataDim; ++i) { + str.append(std::to_string(*data)); + if (i != dataDim - 1) { + str.append(", "); + } + ++data; + } + str.append("\n"); + SEN_HILOGI("SensorData: %{public}s", str.c_str()); +} + +int32_t PrintSensorData::GetDataDimension(int32_t sensorId) +{ + switch (sensorId) { + case SENSOR_TYPE_ID_HALL: + case SENSOR_TYPE_ID_PROXIMITY: + return ONE_DIMENSION; + case SENSOR_TYPE_ID_HALL_EXT: + return TWO_DIMENSION; + case SENSOR_TYPE_ID_POSTURE: + return SEVEN_DIMENSION; + case SENSOR_TYPE_ID_AMBIENT_LIGHT: + case SENSOR_TYPE_ID_MAGNETIC_FIELD: + return THREE_DIMENSION; + default: + SEN_HILOGW("Unknown sensorId:%{public}d, size:%{public}d", sensorId, DEFAULT_DIMENSION); + return DEFAULT_DIMENSION; + } +} + +void PrintSensorData::ControlSensorClientPrint(const SensorUser *user, const SensorEvent &event) +{ + if (g_triggerSensorType.find(event.sensorTypeId) != g_triggerSensorType.end()) { + PrintClientData(event); + } + if (g_continuousSensorType.find(event.sensorTypeId) != g_continuousSensorType.end()) { + auto it = clientLoginfos_.find(user); + if (it != clientLoginfos_.end()) { + if (it->second.count < PRINT_TIMES) { + PrintClientData(event); + if (it->second.count == PRINT_TIMES - 1) { + it->second.lastTime = event.timestamp; + } + it->second.count++; + } else { + if (event.timestamp - it->second.lastTime >= LOG_INTERVAL) { + PrintClientData(event); + it->second.lastTime = event.timestamp; + } + } + } + } +} + +void PrintSensorData::PrintClientData(const SensorEvent &event) +{ + std::string str; + str += "sensorId: " + std::to_string(event.sensorTypeId) + ", "; + str += "timestamp: " + std::to_string(event.timestamp / 1e9) + ", "; + int32_t dataDim = GetDataDimension(event.sensorTypeId); + auto data = reinterpret_cast(event.data); + for (int32_t i = 0; i < dataDim; ++i) { + str.append(std::to_string(*data)); + if (i != dataDim - 1) { + str.append(", "); + } + ++data; + } + str.append("\n"); + SEN_HILOGI("SensorData: %{public}s", str.c_str()); +} + +bool PrintSensorData::IsContinuousType(int32_t sensorId) +{ + return g_continuousSensorType.find(sensorId) != g_continuousSensorType.end(); +} + +void PrintSensorData::SavePrintUserInfo(const SensorUser *user) +{ + if (clientLoginfos_.find(user) == clientLoginfos_.end()) { + LogPrintInfo info; + auto status = clientLoginfos_.insert(std::make_pair(user, info)); + if (!status.second) { + SEN_HILOGD("User has been subscribed"); + } + } +} + +void PrintSensorData::RemovePrintUserInfo(const SensorUser *user) +{ + if (clientLoginfos_.find(user) != clientLoginfos_.end()) { + clientLoginfos_.erase(user); + } +} + +void PrintSensorData::ResetHdiCounter(int32_t sensorId) +{ + auto hdiIt = hdiLoginfos_.find(sensorId); + if (hdiIt != hdiLoginfos_.end()) { + SEN_HILOGE("tmac ResetCounter in 111, id:%{public}d", sensorId); + hdiIt->second.count = 0; + hdiIt->second.lastTime = 0; + } +} +} // namespace Sensors +} // namespace OHOS -- Gitee