diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 9dd4e8bb7ffc530b5efc1d3d30aff41b67685760..dbbfda132ef0b13ef30b25228a55e07123133df8 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 fe9b012a30b9b16cc65d1a5acf79805695df7c86..00de47486980571e8ccd7675bf0118f43b80991d 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 0cdfbe2d2f4b387943ab5374da6324dd4287ade8..267b3255b0f5d70794ec1fb8e31825fce771f8c0 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" @@ -76,6 +77,7 @@ void SensorAgentProxy::HandleSensorData(SensorEvent *events, RecordSensorCallback fun = user->callback; CHKPV(fun); fun(&eventStream); + PrintSensorData::GetInstance().ControlSensorClientPrint(user, eventStream); } } } @@ -245,6 +247,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; } @@ -278,6 +283,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/services/hdi_connection/adapter/include/sensor_event_callback.h b/services/hdi_connection/adapter/include/sensor_event_callback.h index f1c62ff6733f7e42c441f660c70a824c516a8edd..2d81cb858aec85f741810e9361766b1c84ee8a09 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 19c830f8c0206f3d5f00877d8be807e7711e785a..93a021f84eb8f94e9dedabd3b054563ce5a7ecd7 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, @@ -87,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/include/sensor_service.h b/services/include/sensor_service.h index 67d9b4b0bcc613c17d5e60368254c82a3ed4e738..0e3ab6cda4424e4a12511a8f34ccaf68d13650c4 100644 --- a/services/include/sensor_service.h +++ b/services/include/sensor_service.h @@ -70,6 +70,7 @@ public: private: DISALLOW_COPY_AND_MOVE(SensorService); void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + bool CheckParameter(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); class PermStateChangeCb : public Security::AccessToken::PermStateChangeCallbackCustomize { public: diff --git a/services/src/sensor_service.cpp b/services/src/sensor_service.cpp index eef9a2f37e133c116e5b7e2fdeb20aa22a8193f0..7031992ac9ba98790eba474e26d35d5d47be6dcb 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" @@ -254,12 +255,21 @@ bool SensorService::CheckSensorId(int32_t sensorId) return true; } -ErrCode SensorService::EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) +bool SensorService::CheckParameter(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) { - CALL_LOG_ENTER; if ((!CheckSensorId(sensorId)) || ((samplingPeriodNs != 0L) && ((maxReportDelayNs / samplingPeriodNs) > MAX_EVENT_COUNT))) { SEN_HILOGE("sensorId is invalid or maxReportDelayNs exceeded the maximum value"); + return false; + } + return true; +} + +ErrCode SensorService::EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) +{ + CALL_LOG_ENTER; + if (!CheckParameter(sensorId, samplingPeriodNs, maxReportDelayNs)) { + SEN_HILOGE("sensorId, samplingPeriodNs or maxReportDelayNs is invalid"); return ERR_NO_INIT; } int32_t pid = GetCallingPid(); @@ -279,6 +289,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 +313,7 @@ ErrCode SensorService::EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, if (isReportActiveInfo_) { ReportActiveInfo(sensorId, pid); } + PrintSensorData::GetInstance().ResetHdiCounter(sensorId); return ret; } diff --git a/utils/common/BUILD.gn b/utils/common/BUILD.gn index 48bce26e0b22d0f7f21321a598905ed917336150..0d522496a5c8929deed3cd9e944219f72686bf9f 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 0000000000000000000000000000000000000000..db5d36b6f2da551d05a58e4a361dd721987f5511 --- /dev/null +++ b/utils/common/include/print_sensor_data.h @@ -0,0 +1,61 @@ +/* + * 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 }; + }; + std::mutex hdiLoginfoMutex_; + std::mutex clientLoginfoMutex_; + LogPrintInfo info_; + std::map hdiLoginfo_ = { + {SENSOR_TYPE_ID_POSTURE, info_}, + {SENSOR_TYPE_ID_AMBIENT_LIGHT, info_}, + {SENSOR_TYPE_ID_MAGNETIC_FIELD, info_}, + }; + std::map clientLoginfo_; +}; +} // 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 0000000000000000000000000000000000000000..4f635d7d437320b934bf5bb9ecf186a8c186c18c --- /dev/null +++ b/utils/common/src/print_sensor_data.cpp @@ -0,0 +1,205 @@ +/* + * 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 = 60000000000L; +constexpr int32_t FIRST_PRINT_TIMES = 20; +constexpr float LOG_FORMAT_DIVIDER = 1e9f; + +const std::vector g_triggerSensorType = { + SENSOR_TYPE_ID_HALL_EXT, + SENSOR_TYPE_ID_PROXIMITY, + SENSOR_TYPE_ID_HALL, +}; +const std::vector g_continuousSensorType = { + SENSOR_TYPE_ID_POSTURE, + SENSOR_TYPE_ID_AMBIENT_LIGHT, + SENSOR_TYPE_ID_MAGNETIC_FIELD, +}; +} + +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); + } + std::lock_guard hdiLoginfoLock(hdiLoginfoMutex_); + auto it = hdiLoginfo_.find(sensorData.sensorTypeId); + if (it == hdiLoginfo_.end()) { + return; + } + if (it->second.count < FIRST_PRINT_TIMES) { + PrintHdiData(sensorData); + if (it->second.count == FIRST_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 / LOG_FORMAT_DIVIDER) + ", "; + int32_t dataDim = GetDataDimension(sensorData.sensorTypeId); + auto data = reinterpret_cast(sensorData.data); + CHKPV(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) +{ + auto triggerIt = std::find(g_triggerSensorType.begin(), g_triggerSensorType.end(), event.sensorTypeId); + if (triggerIt != g_triggerSensorType.end()) { + PrintClientData(event); + } + + auto continuosIt = std::find(g_continuousSensorType.begin(), g_continuousSensorType.end(), event.sensorTypeId); + if (continuosIt == g_continuousSensorType.end()) { + return; + } + std::lock_guard clientLoginfoLock(clientLoginfoMutex_); + auto it = clientLoginfo_.find(user); + if (it == clientLoginfo_.end()) { + return; + } + if (it->second.count < FIRST_PRINT_TIMES) { + PrintClientData(event); + if (it->second.count == FIRST_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 / LOG_FORMAT_DIVIDER) + ", "; + int32_t dataDim = GetDataDimension(event.sensorTypeId); + auto data = reinterpret_cast(event.data); + CHKPV(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 std::find(g_continuousSensorType.begin(), g_continuousSensorType.end(), + sensorId) != g_continuousSensorType.end(); +} + +void PrintSensorData::SavePrintUserInfo(const SensorUser *user) +{ + CHKPV(user); + std::lock_guard clientLoginfoLock(clientLoginfoMutex_); + if (clientLoginfo_.find(user) != clientLoginfo_.end()) { + return; + } + LogPrintInfo info; + auto status = clientLoginfo_.insert(std::make_pair(user, info)); + if (!status.second) { + SEN_HILOGD("User has been subscribed"); + } +} + +void PrintSensorData::RemovePrintUserInfo(const SensorUser *user) +{ + CHKPV(user); + std::lock_guard clientLoginfoLock(clientLoginfoMutex_); + if (clientLoginfo_.find(user) == clientLoginfo_.end()) { + return; + } + clientLoginfo_.erase(user); +} + +void PrintSensorData::ResetHdiCounter(int32_t sensorId) +{ + std::lock_guard hdiLoginfoLock(hdiLoginfoMutex_); + auto it = hdiLoginfo_.find(sensorId); + if (it == hdiLoginfo_.end()) { + return; + } + it->second.count = 0; + it->second.lastTime = 0; +} +} // namespace Sensors +} // namespace OHOS