From 6050cc1ed23cef1db1b42aa78bcd3c28f8b07941 Mon Sep 17 00:00:00 2001 From: HYH Date: Tue, 15 Nov 2022 08:41:28 +0000 Subject: [PATCH 01/25] Modify test config Signed-off-by: HYH --- interfaces/plugin/test/unittest/config.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/interfaces/plugin/test/unittest/config.json b/interfaces/plugin/test/unittest/config.json index b4bc5993..026e069a 100755 --- a/interfaces/plugin/test/unittest/config.json +++ b/interfaces/plugin/test/unittest/config.json @@ -28,7 +28,8 @@ "package": "com.example.myapplication", "name": ".MyApplication", "deviceType": [ - "phone" + "default", + "tablet" ], "distro": { "deliveryWithInstall": true, -- Gitee From afb3ddea221508245c9a0029790a012fe8d3ac6c Mon Sep 17 00:00:00 2001 From: h00514358 Date: Fri, 18 Nov 2022 09:27:22 +0800 Subject: [PATCH 02/25] add test Signed-off-by: h00514358 Change-Id: I7892b5ad70ff89b6d4005f70c796653f784a126f --- interfaces/native/test/BUILD.gn | 30 +- .../test/unittest/sensor_algorithm_test.cpp | 342 ++++++++++++++++++ 2 files changed, 371 insertions(+), 1 deletion(-) create mode 100755 interfaces/native/test/unittest/sensor_algorithm_test.cpp diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index a9913aa7..5a175be2 100644 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -48,8 +48,36 @@ ohos_unittest("SensorAgentTest") { ] } +ohos_unittest("SensorAlgorithmTest") { + module_out_path = module_output_path + + sources = [ "unittest/sensor_algorithm_test.cpp" ] + + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "$SUBSYSTEM_DIR/sensor/utils/include", + "$SUBSYSTEM_DIR/sensor/interfaces/native/include", + "$SUBSYSTEM_DIR/sensor/test/unittest/common/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/sensor/interfaces/native:sensor_ndk_target", + "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} + ###########################end########################### group("unittest") { testonly = true - deps = [ ":SensorAgentTest" ] + deps = [ + ":SensorAgentTest", + ":SensorAlgorithmTest", + ] } diff --git a/interfaces/native/test/unittest/sensor_algorithm_test.cpp b/interfaces/native/test/unittest/sensor_algorithm_test.cpp new file mode 100755 index 00000000..f4004861 --- /dev/null +++ b/interfaces/native/test/unittest/sensor_algorithm_test.cpp @@ -0,0 +1,342 @@ +/* + * Copyright (c) 2021 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 + +#include "geomagnetic_field.h" +#include "sensor_algorithm.h" +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorAlgorithmTest" }; +constexpr int32_t QUATERNION_LENGTH = 4; +constexpr int32_t ROTATION_VECTOR_LENGTH = 3; +constexpr int32_t THREE_DIMENSIONAL_MATRIX_LENGTH = 9; +} // namespace + +class SensorAlgorithmTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void SensorAlgorithmTest::SetUpTestCase() {} + +void SensorAlgorithmTest::TearDownTestCase() {} + +void SensorAlgorithmTest::SetUp() {} + +void SensorAlgorithmTest::TearDown() {} + +SensorAlgorithm sensorAlgorithm; + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_001, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_001 in"); + std::vector rotationVector = {0.52, -0.336, -0.251}; + std::vector quaternion(QUATERNION_LENGTH); + int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ASSERT_EQ(quaternion.size(), QUATERNION_LENGTH); + std::vector result = {0.7441122531890869, 0.5199999809265137, -0.335999995470047, -0.25099998712539673}; + for (size_t i = 0; i < QUATERNION_LENGTH; ++i) { + ASSERT_EQ(quaternion[i], result[i]); + } +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_002, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_002 in"); + std::vector rotationVector = {}; + std::vector quaternion(QUATERNION_LENGTH); + int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_003, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_003 in"); + std::vector rotationVector = {0.52, -0.336, -0.251}; + std::vector quaternion(ROTATION_VECTOR_LENGTH - 1); + int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_004, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_004 in"); + std::vector inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; + std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, 1, 2, outRotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::vector result = + {1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000}; + ASSERT_EQ(outRotationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH); + for (size_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) { + ASSERT_EQ(outRotationMatrix[i], result[i]); + } +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_005, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_005 in"); + std::vector inRotationMatrix(3); + std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, 1, 2, outRotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_006, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_006 in"); + std::vector inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; + std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, 1, -1, outRotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_007, TestSize.Level1) +{ + SEN_HILOGI("SensorAlgorithmTest_007 in"); + std::vector inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; + std::vector outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, -1, 1, outRotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_008, TestSize.Level1) +{ + float altitude = -1.0; + int32_t ret = sensorAlgorithm.GetAltitude(5.0, 0.0, &altitude); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ASSERT_EQ(altitude, 44330.0); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_009, TestSize.Level1) +{ + int32_t ret = sensorAlgorithm.GetAltitude(5.0, 0.0, nullptr); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_010, TestSize.Level1) +{ + float geomagneticDip = -1.0; + std::vector inclinationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; + int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ASSERT_EQ(geomagneticDip, 0.8760581016540527); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_011, TestSize.Level1) +{ + std::vector inclinationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; + int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, nullptr); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_012, TestSize.Level1) +{ + std::vector inclinationMatrix(3); + float geomagneticDip = -1.0; + int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_013, TestSize.Level1) +{ + std::vector currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; + std::vector preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; + std::vector angleChange(ROTATION_VECTOR_LENGTH); + int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ASSERT_EQ(angleChange.size(), ROTATION_VECTOR_LENGTH); + std::vector result = {0.0, -0.0, -0.0}; + for (size_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) { + ASSERT_EQ(angleChange[i], result[i]); + } +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_014, TestSize.Level1) +{ + std::vector currotationMatrix(3); + std::vector preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; + std::vector angleChange(ROTATION_VECTOR_LENGTH); + int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_015, TestSize.Level1) +{ + std::vector currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; + std::vector preRotationMatrix(ROTATION_VECTOR_LENGTH); + std::vector angleChange(ROTATION_VECTOR_LENGTH); + int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_016, TestSize.Level1) +{ + std::vector currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; + std::vector preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38, + 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38}; + std::vector angleChange(ROTATION_VECTOR_LENGTH - 1); + int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_017, TestSize.Level1) +{ + std::vector rotationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; + std::vector rotationAngle(ROTATION_VECTOR_LENGTH); + int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::vector result = {0.38050639629364014, -0.9783217310905457, -0.6610431671142578}; + ASSERT_EQ(rotationAngle.size(), ROTATION_VECTOR_LENGTH); + for (size_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) { + ASSERT_EQ(rotationAngle[i], result[i]); + } +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_018, TestSize.Level1) +{ + std::vector rotationMatrix(5); + std::vector rotationAngle(ROTATION_VECTOR_LENGTH); + int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_019, TestSize.Level1) +{ + std::vector rotationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; + std::vector rotationAngle(ROTATION_VECTOR_LENGTH - 1); + int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_020, TestSize.Level1) +{ + std::vector rotationVector = {0.0, 0.0, 0.0}; + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::vector result = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; + ASSERT_EQ(rotationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH); + for (size_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) { + ASSERT_EQ(rotationMatrix[i], result[i]); + } +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_021, TestSize.Level1) +{ + std::vector rotationVector(ROTATION_VECTOR_LENGTH - 1); + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_022, TestSize.Level1) +{ + std::vector rotationVector = {0.0, 0.0, 0.0}; + std::vector rotationMatrix(ROTATION_VECTOR_LENGTH - 1); + int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_023, TestSize.Level1) +{ + std::vector gravity = {9.0, 9.0, 9.0}; + std::vector geomagnetic = {30.0, 25.0, 41.0}; + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ASSERT_EQ(rotationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH); + ASSERT_EQ(inclinationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector rotationMatrixResult = + {-0.7980074882507324, 0.5486301183700562, 0.24937734007835388, -0.17277367413043976, + -0.6047078967094421, 0.7774815559387207, 0.5773502588272095, 0.5773502588272095, 0.5773502588272095}; + std::vector inclinationMatrixResult = {1.0, 0.0, 0.0, 0.0, 0.20444221794605255, + 0.9788785576820374, 0, -0.9788785576820374, 0.20444221794605255}; + for (size_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) { + ASSERT_EQ(rotationMatrix[i], rotationMatrixResult[i]); + ASSERT_EQ(inclinationMatrix[i], inclinationMatrixResult[i]); + } +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_024, TestSize.Level1) +{ + std::vector gravity(ROTATION_VECTOR_LENGTH - 1); + std::vector geomagnetic = {30.0, 25.0, 41.0}; + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_025, TestSize.Level1) +{ + std::vector gravity = {9.0, 9.0, 9.0}; + std::vector geomagnetic(ROTATION_VECTOR_LENGTH - 1); + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_026, TestSize.Level1) +{ + std::vector gravity = {9.0, 9.0, 9.0}; + std::vector geomagnetic = {30.0, 25.0, 41.0}; + std::vector rotationMatrix(ROTATION_VECTOR_LENGTH - 1); + std::vector inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_027, TestSize.Level1) +{ + std::vector gravity = {9.0, 9.0, 9.0}; + std::vector geomagnetic = {30.0, 25.0, 41.0}; + std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); + std::vector inclinationMatrix(ROTATION_VECTOR_LENGTH - 1); + int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix); + ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR); +} + +HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_028, TestSize.Level1) +{ + GeomagneticField geomagneticField(80.0, 0.0, 0.0, 1580486400000); + ASSERT_EQ(geomagneticField.ObtainX(), 6570.3935546875); + ASSERT_EQ(geomagneticField.ObtainY(), -146.3289337158203); + ASSERT_EQ(geomagneticField.ObtainZ(), 54606.0078125); + ASSERT_EQ(geomagneticField.ObtainGeomagneticDip(), 83.13726043701172); + ASSERT_EQ(geomagneticField.ObtainDeflectionAngle(), -1.2758207321166992); + ASSERT_EQ(geomagneticField.ObtainLevelIntensity(), 6572.02294921875); + ASSERT_EQ(geomagneticField.ObtainTotalIntensity(), 55000.0703125); +} +} // namespace Sensors +} // namespace OHOS -- Gitee From d44554fbbcf223824a24dd4a710a6709d5c7081d Mon Sep 17 00:00:00 2001 From: h00514358 Date: Wed, 23 Nov 2022 10:34:22 +0800 Subject: [PATCH 03/25] update Signed-off-by: h00514358 Change-Id: Ia4c2ff18210067a84ce99150de77e85991ff0113 --- .../include/sensor_file_descriptor_listener.h | 5 ++--- .../src/sensor_file_descriptor_listener.cpp | 20 +++++++++---------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/frameworks/native/sensor/include/sensor_file_descriptor_listener.h b/frameworks/native/sensor/include/sensor_file_descriptor_listener.h index 165ba763..f403490b 100644 --- a/frameworks/native/sensor/include/sensor_file_descriptor_listener.h +++ b/frameworks/native/sensor/include/sensor_file_descriptor_listener.h @@ -25,16 +25,15 @@ namespace OHOS { namespace Sensors { class SensorFileDescriptorListener : public AppExecFwk::FileDescriptorListener { public: - explicit SensorFileDescriptorListener(); + SensorFileDescriptorListener(); ~SensorFileDescriptorListener(); void OnReadable(int32_t fileDescriptor) override; - void OnWritable(int32_t fileDescriptor) override; void OnShutdown(int32_t fileDescriptor) override; void OnException(int32_t fileDescriptor) override; void SetChannel(SensorDataChannel* channel); private: - SensorDataChannel* channel_; + SensorDataChannel* channel_ = nullptr; TransferSensorEvents *receiveDataBuff_ = nullptr; }; } // namespace Sensors diff --git a/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp b/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp index 8dc13614..9ae412c7 100644 --- a/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp +++ b/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp @@ -31,9 +31,7 @@ constexpr int32_t RECEIVE_DATA_SIZE = 100; SensorFileDescriptorListener::SensorFileDescriptorListener() { - channel_ = nullptr; - receiveDataBuff_ = - new (std::nothrow) TransferSensorEvents[sizeof(TransferSensorEvents) * RECEIVE_DATA_SIZE]; + receiveDataBuff_ = new (std::nothrow) TransferSensorEvents[RECEIVE_DATA_SIZE]; CHKPL(receiveDataBuff_); } @@ -52,8 +50,9 @@ void SensorFileDescriptorListener::OnReadable(int32_t fileDescriptor) SEN_HILOGE("fileDescriptor:%{public}d", fileDescriptor); return; } - FileDescriptorListener::OnReadable(fileDescriptor); + CHKPV(channel_); if (receiveDataBuff_ == nullptr) { + SEN_HILOGE("Receive data buff_ is null"); return; } int32_t len = @@ -77,8 +76,6 @@ void SensorFileDescriptorListener::OnReadable(int32_t fileDescriptor) } } -void SensorFileDescriptorListener::OnWritable(int32_t fileDescriptor) {} - void SensorFileDescriptorListener::SetChannel(SensorDataChannel* channel) { channel_ = channel; @@ -87,26 +84,27 @@ void SensorFileDescriptorListener::SetChannel(SensorDataChannel* channel) void SensorFileDescriptorListener::OnShutdown(int32_t fileDescriptor) { if (fileDescriptor < 0) { - SEN_HILOGE("param is error:%{public}d", fileDescriptor); + SEN_HILOGE("Invalid fd:%{public}d", fileDescriptor); } - FileDescriptorListener::OnShutdown(fileDescriptor); if (receiveDataBuff_ != nullptr) { delete[] receiveDataBuff_; receiveDataBuff_ = nullptr; } + CHKPV(channel_); + channel_->DestroySensorDataChannel(); } void SensorFileDescriptorListener::OnException(int32_t fileDescriptor) { if (fileDescriptor < 0) { - SEN_HILOGE("param is error:%{public}d", fileDescriptor); - return; + SEN_HILOGE("Invalid fd::%{public}d", fileDescriptor); } - FileDescriptorListener::OnException(fileDescriptor); if (receiveDataBuff_ != nullptr) { delete[] receiveDataBuff_; receiveDataBuff_ = nullptr; } + CHKPV(channel_); + channel_->DestroySensorDataChannel(); } } // namespace Sensors } // namespace OHOS \ No newline at end of file -- Gitee From 04fc006d1869f4fe5faccde100526b0053314b34 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Thu, 24 Nov 2022 14:58:28 +0800 Subject: [PATCH 04/25] fix data report problem Signed-off-by: h00514358 Change-Id: I28df51c15c2f94d940cf982f80ce33697705f003 --- frameworks/native/sensor/src/sensor_agent_proxy.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 7b28161c..ee76a2a7 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -73,12 +73,15 @@ void SensorAgentProxy::HandleSensorData(SensorEvent *events, int32_t num, void * SensorEvent eventStream; for (int32_t i = 0; i < num; ++i) { eventStream = events[i]; - if (g_subscribeMap.find(eventStream.sensorTypeId) == g_subscribeMap.end()) { - SEN_HILOGE("sensorTypeId not in g_subscribeMap"); + std::lock_guard subscribeLock(subscribeMutex_); + auto iter = g_subscribeMap.find(eventStream.sensorTypeId); + if (iter == g_subscribeMap.end()) { + SEN_HILOGE("sensor:%{public}d is not subscribed", iter->first); return; } - CHKPV(g_subscribeMap[eventStream.sensorTypeId]); - g_subscribeMap[eventStream.sensorTypeId]->callback(&eventStream); + const SensorUser *user = iter->second; + CHKPV(user); + user->callback(&eventStream); } } -- Gitee From f8a29d4148810aeed87f2a08ee393d5d9dcbed55 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Sat, 26 Nov 2022 16:47:15 +0800 Subject: [PATCH 05/25] update Signed-off-by: h00514358 Change-Id: I6aef98363f38e6816b1ab18bfc01c12b96a08eae --- .../native/sensor/include/sensor_agent_proxy.h | 2 +- .../native/sensor/src/sensor_agent_proxy.cpp | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/frameworks/native/sensor/include/sensor_agent_proxy.h b/frameworks/native/sensor/include/sensor_agent_proxy.h index 3fc3e014..a14ea60d 100644 --- a/frameworks/native/sensor/include/sensor_agent_proxy.h +++ b/frameworks/native/sensor/include/sensor_agent_proxy.h @@ -51,7 +51,7 @@ private: int32_t ConvertSensorInfos() const; void ClearSensorInfos() const; static OHOS::sptr sensorObj_; - static std::mutex subscribeMutex_; + static std::recursive_mutex subscribeMutex_; static std::mutex chanelMutex_; OHOS::sptr dataChannel_; static bool g_isChannelCreated; diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index ee76a2a7..16374f81 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -35,7 +35,7 @@ OHOS::sptr SensorAgentProxy::sensorObj_ = nullptr; bool SensorAgentProxy::g_isChannelCreated; int64_t SensorAgentProxy::g_samplingInterval; int64_t SensorAgentProxy::g_reportInterval; -std::mutex SensorAgentProxy::subscribeMutex_; +std::recursive_mutex SensorAgentProxy::subscribeMutex_; std::mutex SensorAgentProxy::chanelMutex_; std::mutex sensorInfoMutex_; SensorInfo *sensorInfos_ = nullptr; @@ -73,7 +73,7 @@ void SensorAgentProxy::HandleSensorData(SensorEvent *events, int32_t num, void * SensorEvent eventStream; for (int32_t i = 0; i < num; ++i) { eventStream = events[i]; - std::lock_guard subscribeLock(subscribeMutex_); + std::lock_guard subscribeLock(subscribeMutex_); auto iter = g_subscribeMap.find(eventStream.sensorTypeId); if (iter == g_subscribeMap.end()) { SEN_HILOGE("sensor:%{public}d is not subscribed", iter->first); @@ -136,7 +136,6 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use { CHKPR(user, OHOS::Sensors::ERROR); CHKPR(user->callback, OHOS::Sensors::ERROR); - std::lock_guard subscribeLock(subscribeMutex_); if (g_samplingInterval < 0 || g_reportInterval < 0) { SEN_HILOGE("samplingPeroid or g_reportInterval is invalid"); return ERROR; @@ -145,6 +144,7 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return PARAMETER_ERROR; } + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { SEN_HILOGE("subscribe sensorId first"); return ERROR; @@ -168,7 +168,7 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return PARAMETER_ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { SEN_HILOGE("subscribe sensorId first"); return OHOS::Sensors::ERROR; @@ -195,7 +195,7 @@ int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int SEN_HILOGE("samplingInterval or reportInterval is invalid"); return OHOS::Sensors::ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { SEN_HILOGE("subscribe sensorId first"); return OHOS::Sensors::ERROR; @@ -219,7 +219,7 @@ int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *us SEN_HILOGE("create sensor data chanel failed"); return OHOS::Sensors::ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); + std::lock_guard subscribeLock(subscribeMutex_); g_subscribeMap[sensorId] = user; return OHOS::Sensors::SUCCESS; } @@ -233,7 +233,7 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return PARAMETER_ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); + std::lock_guard subscribeLock(subscribeMutex_); if (g_unsubscribeMap.find(sensorId) == g_unsubscribeMap.end() || g_unsubscribeMap[sensorId] != user) { SEN_HILOGE("deactivate sensorId first"); return OHOS::Sensors::ERROR; @@ -257,7 +257,7 @@ int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int3 SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { SEN_HILOGE("subscribe sensorId first"); return OHOS::Sensors::ERROR; @@ -273,7 +273,7 @@ int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, in SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); + std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { SEN_HILOGE("subscribe sensorId first"); return OHOS::Sensors::ERROR; -- Gitee From fd7830b3043a10908c2dd4c24010e4a5de793462 Mon Sep 17 00:00:00 2001 From: lixiangpeng5 Date: Mon, 28 Nov 2022 15:03:46 +0800 Subject: [PATCH 06/25] =?UTF-8?q?=E4=BF=AE=E5=A4=8DEmitOnceCallback?= =?UTF-8?q?=E5=9C=A8native=E7=BA=BF=E7=A8=8B=E6=9E=90=E6=9E=84AsyncCallbac?= =?UTF-8?q?kInfo=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lixiangpeng5 Change-Id: Ie1ad7c321526f0140dd3a2fe892906aebb89c9cb --- interfaces/plugin/src/sensor_js.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index c87bb402..57ad79c2 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -137,14 +137,17 @@ static void EmitOnceCallback(SensorEvent *event) if (iter == g_onceCallbackInfos.end()) { return; } - for (auto &onceCallbackInfo : iter->second) { + auto& onceCallbackInfos = iter->second; + while (!onceCallbackInfos.empty()) { + auto onceCallbackInfo = onceCallbackInfos.front(); + auto beginIter = onceCallbackInfos.begin(); + onceCallbackInfos.erase(beginIter); if (!copySensorData(onceCallbackInfo, event)) { SEN_HILOGE("Copy sensor data failed"); continue; } - EmitUvEventLoop(onceCallbackInfo); + EmitUvEventLoop(std::move(onceCallbackInfo)); } - g_onceCallbackInfos[sensorTypeId].clear(); g_onceCallbackInfos.erase(sensorTypeId); CHKCV((!CheckSubscribe(sensorTypeId)), "Has client subscribe, not need cancel subscribe"); -- Gitee From 1cb0af03c2e84960ea3f2fe4bc891744109ddcf7 Mon Sep 17 00:00:00 2001 From: HYH Date: Tue, 29 Nov 2022 09:37:16 +0000 Subject: [PATCH 07/25] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: HYH --- frameworks/native/sensor/src/sensor_agent_proxy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 16374f81..1b09c58e 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -76,7 +76,7 @@ void SensorAgentProxy::HandleSensorData(SensorEvent *events, int32_t num, void * std::lock_guard subscribeLock(subscribeMutex_); auto iter = g_subscribeMap.find(eventStream.sensorTypeId); if (iter == g_subscribeMap.end()) { - SEN_HILOGE("sensor:%{public}d is not subscribed", iter->first); + SEN_HILOGE("sensor is not subscribed"); return; } const SensorUser *user = iter->second; -- Gitee From eed3fd5df39645863769dc1153846ab9920be918 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Thu, 1 Dec 2022 14:54:32 +0800 Subject: [PATCH 08/25] update Signed-off-by: h00514358 Change-Id: Ie0c9339a329ea2d42bb17b9bc00981689902bc9f --- interfaces/plugin/src/sensor_js.cpp | 31 ++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 57ad79c2..4341fe19 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -352,12 +352,26 @@ static napi_value On(napi_env env, napi_callback_info info) return nullptr; } -static void RemoveAllCallback(napi_env env, int32_t sensorTypeId) +static int32_t RemoveAllCallback(napi_env env, int32_t sensorTypeId) { CALL_LOG_ENTER; std::lock_guard onCallbackLock(onMutex_); - g_onCallbackInfos[sensorTypeId].clear(); - g_onCallbackInfos.erase(sensorTypeId); + std::vector> callbackInfos = g_onCallbackInfos[sensorTypeId]; + for (auto iter = callbackInfos.begin(); iter != callbackInfos.end();) { + CHKPC(*iter); + if ((*iter)->env != env) { + ++iter; + continue; + } + iter = callbackInfos.erase(iter); + } + if (callbackInfos.empty()) { + SEN_HILOGD("No subscription to change sensor data"); + g_onCallbackInfos.erase(sensorTypeId); + return 0; + } + g_onCallbackInfos[sensorTypeId] = callbackInfos; + return callbackInfos.size(); } static int32_t RemoveCallback(napi_env env, int32_t sensorTypeId, napi_value callback) @@ -367,6 +381,9 @@ static int32_t RemoveCallback(napi_env env, int32_t sensorTypeId, napi_value cal std::vector> callbackInfos = g_onCallbackInfos[sensorTypeId]; for (auto iter = callbackInfos.begin(); iter != callbackInfos.end(); ++iter) { CHKPC(*iter); + if ((*iter)->env != env) { + continue; + } napi_value sensorCallback = nullptr; if (napi_get_reference_value(env, (*iter)->callback[0], &sensorCallback) != napi_ok) { SEN_HILOGE("napi_get_reference_value fail"); @@ -403,17 +420,17 @@ static napi_value Off(napi_env env, napi_callback_info info) ThrowErr(env, PARAMETER_ERROR, "Wrong argument type or get number fail"); return nullptr; } + int32_t subscribeSize = -1; if (argc == 1) { - RemoveAllCallback(env, sensorTypeId); + subscribeSize = RemoveAllCallback(env, sensorTypeId); } else { if (!IsMatchType(env, args[1], napi_function)) { ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function"); return nullptr; } - CHKCP((RemoveCallback(env, sensorTypeId, args[1]) == 0), - "There are other client subscribe as well, not need unsubscribe"); + subscribeSize = RemoveCallback(env, sensorTypeId, args[1]); } - if (CheckSystemSubscribe(sensorTypeId)) { + if (CheckSystemSubscribe(sensorTypeId) || (subscribeSize > 0)) { SEN_HILOGW("There are other client subscribe system js api as well, not need unsubscribe"); return nullptr; } -- Gitee From a16cecd40338de5aac94a4d18bf69d2c44674eac Mon Sep 17 00:00:00 2001 From: h00514358 Date: Mon, 5 Dec 2022 20:55:37 +0800 Subject: [PATCH 09/25] update Signed-off-by: h00514358 Change-Id: Idbd9b2439d1aa449be708b2f8b6e3370d6fc68ea --- .../native/sensor/include/i_sensor_service.h | 2 - .../sensor/include/sensor_service_client.h | 1 - .../sensor/include/sensor_service_proxy.h | 2 - .../native/sensor/src/sensor_agent_proxy.cpp | 16 ----- .../sensor/src/sensor_service_client.cpp | 31 ---------- .../sensor/src/sensor_service_proxy.cpp | 60 ------------------- .../adapter/include/compatible_connection.h | 2 - .../adapter/include/hdi_connection.h | 2 - .../adapter/src/compatible_connection.cpp | 15 ----- .../adapter/src/hdi_connection.cpp | 19 ------ .../hardware/include/hdi_service_impl.h | 2 - .../hardware/src/hdi_service_impl.cpp | 10 ---- .../include/i_sensor_hdi_connection.h | 2 - .../interface/include/sensor_hdi_connection.h | 2 - .../interface/src/sensor_hdi_connection.cpp | 24 -------- services/sensor/include/flush_info_record.h | 1 - services/sensor/include/sensor_service.h | 2 - services/sensor/include/sensor_service_stub.h | 2 - services/sensor/src/flush_info_record.cpp | 7 +-- services/sensor/src/sensor_service.cpp | 37 ------------ services/sensor/src/sensor_service_stub.cpp | 32 ---------- 21 files changed, 1 insertion(+), 270 deletions(-) diff --git a/frameworks/native/sensor/include/i_sensor_service.h b/frameworks/native/sensor/include/i_sensor_service.h index e89fc433..e4400ba5 100755 --- a/frameworks/native/sensor/include/i_sensor_service.h +++ b/frameworks/native/sensor/include/i_sensor_service.h @@ -35,8 +35,6 @@ public: virtual ErrCode EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) = 0; virtual ErrCode DisableSensor(uint32_t sensorId) = 0; - virtual int32_t GetSensorState(uint32_t sensorId) = 0; - virtual ErrCode RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t params) = 0; virtual std::vector GetSensorList() = 0; virtual ErrCode TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) = 0; diff --git a/frameworks/native/sensor/include/sensor_service_client.h b/frameworks/native/sensor/include/sensor_service_client.h index 38c29496..586bb05d 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -38,7 +38,6 @@ public: std::vector GetSensorList(); int32_t EnableSensor(uint32_t sensorId, int64_t samplingPeroid, int64_t maxReportDelay); int32_t DisableSensor(uint32_t sensorId); - int32_t RunCommand(uint32_t sensorId, int32_t cmdType, int32_t parms); int32_t TransferDataChannel(sptr sensorDataChannel); int32_t DestroyDataChannel(); void ProcessDeathObserver(const wptr &object); diff --git a/frameworks/native/sensor/include/sensor_service_proxy.h b/frameworks/native/sensor/include/sensor_service_proxy.h index 2894fbc8..70a43eca 100755 --- a/frameworks/native/sensor/include/sensor_service_proxy.h +++ b/frameworks/native/sensor/include/sensor_service_proxy.h @@ -31,8 +31,6 @@ public: virtual ~SensorServiceProxy() = default; ErrCode EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) override; ErrCode DisableSensor(uint32_t sensorId) override; - int32_t GetSensorState(uint32_t sensorId) override; - ErrCode RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t params) override; std::vector GetSensorList() override; ErrCode TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) override; diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 16374f81..feb76b45 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -265,22 +265,6 @@ int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int3 return OHOS::Sensors::SUCCESS; } -int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, int32_t option) const -{ - CHKPR(user, OHOS::Sensors::ERROR); - CHKPR(user->callback, OHOS::Sensors::ERROR); - if (!SenClient.IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); - return ERROR; - } - std::lock_guard subscribeLock(subscribeMutex_); - if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { - SEN_HILOGE("subscribe sensorId first"); - return OHOS::Sensors::ERROR; - } - return OHOS::Sensors::SUCCESS; -} - void SensorAgentProxy::ClearSensorInfos() const { CHKPV(sensorInfos_); diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index df26d269..98e90ea9 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -95,10 +95,6 @@ bool SensorServiceClient::IsValid(uint32_t sensorId) int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay) { CALL_LOG_ENTER; - if (!IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid"); - return PARAMETER_ERROR; - } int32_t ret = InitServiceClient(); if (ret != ERR_OK) { SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); @@ -117,10 +113,6 @@ int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPer int32_t SensorServiceClient::DisableSensor(uint32_t sensorId) { CALL_LOG_ENTER; - if (!IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid"); - return PARAMETER_ERROR; - } int32_t ret = InitServiceClient(); if (ret != ERR_OK) { SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); @@ -136,29 +128,6 @@ int32_t SensorServiceClient::DisableSensor(uint32_t sensorId) return ret; } -int32_t SensorServiceClient::RunCommand(uint32_t sensorId, int32_t cmdType, int32_t params) -{ - CALL_LOG_ENTER; - if (!IsValid(sensorId)) { - SEN_HILOGE("sensorId is invalid"); - return PARAMETER_ERROR; - } - int32_t ret = InitServiceClient(); - if (ret != ERR_OK) { - SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); - return ret; - } - CHKPR(sensorServer_, ERROR); - StartTrace(HITRACE_TAG_SENSORS, "RunCommand"); - ret = sensorServer_->RunCommand(sensorId, cmdType, params); - FinishTrace(HITRACE_TAG_SENSORS); - if (ret != ERR_OK) { - SEN_HILOGE("RunCommand failed"); - return ret; - } - return ret; -} - std::vector SensorServiceClient::GetSensorList() { CALL_LOG_ENTER; diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index b0406b7a..fa826efd 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -95,66 +95,6 @@ ErrCode SensorServiceProxy::DisableSensor(uint32_t sensorId) return static_cast(ret); } -int32_t SensorServiceProxy::GetSensorState(uint32_t sensorId) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { - SEN_HILOGE("write descriptor failed"); - return WRITE_MSG_ERR; - } - if (!data.WriteUint32(sensorId)) { - SEN_HILOGE("write sensorId failed"); - return WRITE_MSG_ERR; - } - sptr remote = Remote(); - CHKPR(remote, ERROR); - int32_t ret = remote->SendRequest(ISensorService::GET_SENSOR_STATE, data, reply, option); - if (ret != NO_ERROR) { - HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "PKG_NAME", "GetSensorState", "ERROR_CODE", ret); - SEN_HILOGE("failed, ret:%{public}d", ret); - } - return static_cast(ret); -} - -ErrCode SensorServiceProxy::RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t params) -{ - if (cmdType > RESERVED) { - SEN_HILOGE("failed, cmdType:%{public}u", cmdType); - return CMD_TYPE_ERR; - } - MessageParcel data; - MessageParcel reply; - MessageOption option; - if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { - SEN_HILOGE("write descriptor failed"); - return WRITE_MSG_ERR; - } - if (!data.WriteUint32(sensorId)) { - SEN_HILOGE("write sensorId failed"); - return WRITE_MSG_ERR; - } - if (!data.WriteUint32(cmdType)) { - SEN_HILOGE("write cmdType failed"); - return WRITE_MSG_ERR; - } - if (!data.WriteUint32(params)) { - SEN_HILOGE("write params failed"); - return WRITE_MSG_ERR; - } - sptr remote = Remote(); - CHKPR(remote, ERROR); - int32_t ret = remote->SendRequest(ISensorService::RUN_COMMAND, data, reply, option); - if (ret != NO_ERROR) { - HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "PKG_NAME", "RunCommand", "ERROR_CODE", ret); - SEN_HILOGE("failed, ret:%{public}d", ret); - } - return static_cast(ret); -} - std::vector SensorServiceProxy::GetSensorList() { MessageParcel data; diff --git a/services/sensor/hdi_connection/adapter/include/compatible_connection.h b/services/sensor/hdi_connection/adapter/include/compatible_connection.h index 1dad247a..3ce78807 100644 --- a/services/sensor/hdi_connection/adapter/include/compatible_connection.h +++ b/services/sensor/hdi_connection/adapter/include/compatible_connection.h @@ -31,8 +31,6 @@ public: int32_t DisableSensor(int32_t sensorId) override; int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) override; int32_t SetMode(int32_t sensorId, int32_t mode) override; - int32_t SetOption(int32_t sensorId, int32_t option) override; - int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params) override; int32_t RegisteDataReport(ReportDataCb cb, sptr reportDataCallback) override; int32_t DestroyHdiConnection() override; diff --git a/services/sensor/hdi_connection/adapter/include/hdi_connection.h b/services/sensor/hdi_connection/adapter/include/hdi_connection.h index f93de5be..53436867 100644 --- a/services/sensor/hdi_connection/adapter/include/hdi_connection.h +++ b/services/sensor/hdi_connection/adapter/include/hdi_connection.h @@ -32,8 +32,6 @@ public: int32_t DisableSensor(int32_t sensorId) override; int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) override; int32_t SetMode(int32_t sensorId, int32_t mode) override; - int32_t SetOption(int32_t sensorId, int32_t option) override; - int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params) override; int32_t RegisteDataReport(ReportDataCb cb, sptr reportDataCallback) override; int32_t DestroyHdiConnection() override; ReportDataCb GetReportDataCb(); diff --git a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp index 1863ffd6..821504b0 100644 --- a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp @@ -109,21 +109,6 @@ int32_t CompatibleConnection::SetMode(int32_t sensorId, int32_t mode) return ERR_OK; } -int32_t CompatibleConnection::RunCommand(int32_t sensorId, int32_t cmd, int32_t params) -{ - return ERR_OK; -} - -int32_t CompatibleConnection::SetOption(int32_t sensorId, int32_t option) -{ - int32_t ret = hdiServiceImpl_.SetOption(sensorId, option); - if (ret != 0) { - SEN_HILOGI("set option failed, sensorId:%{public}d", sensorId); - return ret; - } - return ERR_OK; -} - int32_t CompatibleConnection::SensorDataCallback(const SensorEvents *event) { CHKPR(event, ERR_INVALID_VALUE); diff --git a/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp index 524d0e41..1727f296 100644 --- a/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp @@ -152,20 +152,6 @@ int32_t HdiConnection::SetMode(int32_t sensorId, int32_t mode) return ERR_OK; } -int32_t HdiConnection::SetOption(int32_t sensorId, int32_t option) -{ - CALL_LOG_ENTER; - CHKPR(sensorInterface_, ERR_NO_INIT); - int32_t ret = sensorInterface_->SetOption(sensorId, option); - if (ret != 0) { - HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_HDF_SERVICE_EXCEPTION", - HiSysEvent::EventType::FAULT, "PKG_NAME", "SetOption", "ERROR_CODE", ret); - SEN_HILOGE("SetOption is failed"); - return ret; - } - return ERR_OK; -} - int32_t HdiConnection::RegisteDataReport(ReportDataCb cb, sptr reportDataCallback) { CALL_LOG_ENTER; @@ -199,11 +185,6 @@ int32_t HdiConnection::DestroyHdiConnection() return ERR_OK; } -int32_t HdiConnection::RunCommand(int32_t sensorId, int32_t cmd, int32_t params) -{ - return 0; -} - ReportDataCb HdiConnection::GetReportDataCb() { if (reportDataCb_ == nullptr) { diff --git a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h index 8056a67c..26095217 100644 --- a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h +++ b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h @@ -34,8 +34,6 @@ public: int32_t DisableSensor(int32_t sensorId); int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval); int32_t SetMode(int32_t sensorId, int32_t mode); - int32_t SetOption(int32_t sensorId, int32_t option); - int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params); int32_t Register(RecordDataCallback cb); int32_t Unregister(); diff --git a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp index 6bec1fae..6a67a16c 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -129,16 +129,6 @@ int32_t HdiServiceImpl::SetMode(int32_t sensorId, int32_t mode) return ERR_OK; } -int32_t HdiServiceImpl::RunCommand(int32_t sensorId, int32_t cmd, int32_t params) -{ - return ERR_OK; -} - -int32_t HdiServiceImpl::SetOption(int32_t sensorId, int32_t option) -{ - return ERR_OK; -} - int32_t HdiServiceImpl::Register(RecordDataCallback cb) { CHKPR(cb, ERROR); diff --git a/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h b/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h index c0890f9c..c3b28f15 100644 --- a/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h +++ b/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h @@ -33,8 +33,6 @@ public: virtual int32_t DisableSensor(int32_t sensorId) = 0; virtual int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) = 0; virtual int32_t SetMode(int32_t sensorId, int32_t mode) = 0; - virtual int32_t SetOption(int32_t sensorId, int32_t option) = 0; - virtual int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params) = 0; virtual int32_t RegisteDataReport(ReportDataCb cb, sptr reportDataCallback) = 0; virtual int32_t DestroyHdiConnection() = 0; static std::mutex dataMutex_; diff --git a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h index 6c67144d..f027c460 100644 --- a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h +++ b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h @@ -31,8 +31,6 @@ public: int32_t DisableSensor(int32_t sensorId) override; int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) override; int32_t SetMode(int32_t sensorId, int32_t mode) override; - int32_t SetOption(int32_t sensorId, int32_t option) override; - int32_t RunCommand(int32_t sensorId, int32_t cmd, int32_t params) override; int32_t RegisteDataReport(ReportDataCb cb, sptr reportDataCallback) override; int32_t DestroyHdiConnection() override; diff --git a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp index 7f1ca5e6..9b59e9f7 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -111,30 +111,6 @@ int32_t SensorHdiConnection::SetMode(int32_t sensorId, int32_t mode) return ret; } -int32_t SensorHdiConnection::SetOption(int32_t sensorId, int32_t option) -{ - StartTrace(HITRACE_TAG_SENSORS, "SetOption"); - int32_t ret = iSensorHdiConnection_->SetOption(sensorId, option); - FinishTrace(HITRACE_TAG_SENSORS); - if (ret != 0) { - SEN_HILOGI("set option failed, sensorId:%{public}d", sensorId); - return SET_SENSOR_OPTION_ERR; - } - return ret; -} - -int32_t SensorHdiConnection::RunCommand(int32_t sensorId, int32_t cmd, int32_t params) -{ - StartTrace(HITRACE_TAG_SENSORS, "RunCommand"); - int32_t ret = iSensorHdiConnection_->RunCommand(sensorId, cmd, params); - FinishTrace(HITRACE_TAG_SENSORS); - if (ret != 0) { - SEN_HILOGI("run command failed, sensorId:%{public}d", sensorId); - return RUN_COMMAND_ERR; - } - return ret; -} - int32_t SensorHdiConnection::RegisteDataReport(ReportDataCb cb, sptr reportDataCallback) { StartTrace(HITRACE_TAG_SENSORS, "RegisteDataReport"); diff --git a/services/sensor/include/flush_info_record.h b/services/sensor/include/flush_info_record.h index fe100f7e..dfea8a6e 100644 --- a/services/sensor/include/flush_info_record.h +++ b/services/sensor/include/flush_info_record.h @@ -57,7 +57,6 @@ public: private: DISALLOW_COPY_AND_MOVE(FlushInfoRecord); - SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); // sensorId, channel pointer for pending flush. std::unordered_map> flushInfo_; diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index a436cf89..9c5893e9 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -49,8 +49,6 @@ public: int Dump(int fd, const std::vector &args) override; ErrCode EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) override; ErrCode DisableSensor(uint32_t sensorId) override; - int32_t GetSensorState(uint32_t sensorId) override; - ErrCode RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t params) override; std::vector GetSensorList() override; ErrCode TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) override; diff --git a/services/sensor/include/sensor_service_stub.h b/services/sensor/include/sensor_service_stub.h index c816b425..9259e793 100644 --- a/services/sensor/include/sensor_service_stub.h +++ b/services/sensor/include/sensor_service_stub.h @@ -36,8 +36,6 @@ private: using SensorBaseFunc = ErrCode (SensorServiceStub::*)(MessageParcel &data, MessageParcel &reply); ErrCode SensorEnableInner(MessageParcel &data, MessageParcel &reply); ErrCode SensorDisableInner(MessageParcel &data, MessageParcel &reply); - ErrCode GetSensorStateInner(MessageParcel &data, MessageParcel &reply); - ErrCode RunCommandInner(MessageParcel &data, MessageParcel &reply); ErrCode GetAllSensorsInner(MessageParcel &data, MessageParcel &reply); ErrCode CreateDataChannelInner(MessageParcel &data, MessageParcel &reply); ErrCode DestroyDataChannelInner(MessageParcel &data, MessageParcel &reply); diff --git a/services/sensor/src/flush_info_record.cpp b/services/sensor/src/flush_info_record.cpp index 6e1ea29d..fbdebc6b 100644 --- a/services/sensor/src/flush_info_record.cpp +++ b/services/sensor/src/flush_info_record.cpp @@ -91,14 +91,9 @@ int32_t FlushInfoRecord::GetFlushChannelIndex(const std::vector &flus ErrCode FlushInfoRecord::FlushProcess(const uint32_t sensorId, const uint32_t flag, const int32_t pid, const bool isEnableFlush) { - auto ret = sensorHdiConnection_.RunCommand(sensorId, FLUSH, 0); - if (ret != ERR_OK) { - SEN_HILOGE("flush command failed"); - return ret; - } sptr channel = clientInfo_.GetSensorChannelByPid(pid); CHKPR(channel, ERROR); - ret = SetFlushInfo(sensorId, channel, false); + int32_t ret = SetFlushInfo(sensorId, channel, false); if (ret != ERR_OK) { SEN_HILOGE("set flush info failed"); return ret; diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 563427c5..aecd057d 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -284,43 +284,6 @@ ErrCode SensorService::DisableSensor(uint32_t sensorId) return DisableSensor(sensorId, GetCallingPid()); } -int32_t SensorService::GetSensorState(uint32_t sensorId) -{ - if (sensorId == INVALID_SENSOR_ID) { - SEN_HILOGE("sensorId is 0"); - return ERR_NO_INIT; - } - auto state = clientInfo_.GetSensorState(sensorId); - return static_cast(state); -} - -ErrCode SensorService::RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t params) -{ - CALL_LOG_ENTER; - if (sensorId == INVALID_SENSOR_ID || ((cmdType != FLUSH) && (cmdType != SET_MODE))) { - SEN_HILOGE("sensorId or cmd is invalid"); - return ERR_NO_INIT; - } - std::lock_guard serviceLock(serviceLock_); - uint32_t flag = sensorManager_.GetSensorFlag(sensorId); - if (cmdType == FLUSH) { - int32_t pid = this->GetCallingPid(); - SEN_HILOGI("sensorId:%{public}u,flag:%{public}u", sensorId, flag); - auto retFlush = flushInfo_.FlushProcess(sensorId, flag, pid, false); - if (retFlush != ERR_OK) { - SEN_HILOGE("ret:%{public}d", retFlush); - } - return retFlush; - } - if (sensorHdiConnection_.RunCommand(sensorId, cmdType, params) != ERR_OK) { - SEN_HILOGE("RunCommand is failed"); - return RUN_COMMAND_ERR; - } - auto uid = GetCallingUid(); - clientInfo_.UpdateCmd(sensorId, uid, cmdType); - return ERR_OK; -} - std::vector SensorService::GetSensorList() { std::lock_guard sensorLock(sensorsMutex_); diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index bed7a011..167e8c4b 100644 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -40,8 +40,6 @@ SensorServiceStub::SensorServiceStub() CALL_LOG_ENTER; baseFuncs_[ENABLE_SENSOR] = &SensorServiceStub::SensorEnableInner; baseFuncs_[DISABLE_SENSOR] = &SensorServiceStub::SensorDisableInner; - baseFuncs_[GET_SENSOR_STATE] = &SensorServiceStub::GetSensorStateInner; - baseFuncs_[RUN_COMMAND] = &SensorServiceStub::RunCommandInner; baseFuncs_[GET_SENSOR_LIST] = &SensorServiceStub::GetAllSensorsInner; baseFuncs_[TRANSFER_DATA_CHANNEL] = &SensorServiceStub::CreateDataChannelInner; baseFuncs_[DESTROY_SENSOR_CHANNEL] = &SensorServiceStub::DestroyDataChannelInner; @@ -104,36 +102,6 @@ ErrCode SensorServiceStub::SensorDisableInner(MessageParcel &data, MessageParcel return DisableSensor(sensorId); } -ErrCode SensorServiceStub::GetSensorStateInner(MessageParcel &data, MessageParcel &reply) -{ - (void)reply; - uint32_t sensorId = data.ReadUint32(); - PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId); - if (ret != PERMISSION_GRANTED) { - HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_VERIFY_ACCESS_TOKEN_FAIL", - HiSysEvent::EventType::SECURITY, "PKG_NAME", "GetSensorStateInner", "ERROR_CODE", ret); - SEN_HILOGE("sensorId:%{public}u grant failed, result:%{public}d", sensorId, ret); - return PERMISSION_DENIED; - } - return GetSensorState(sensorId); -} - -ErrCode SensorServiceStub::RunCommandInner(MessageParcel &data, MessageParcel &reply) -{ - (void)reply; - uint32_t sensorId = data.ReadUint32(); - PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId); - if (ret != PERMISSION_GRANTED) { - HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_VERIFY_ACCESS_TOKEN_FAIL", - HiSysEvent::EventType::SECURITY, "PKG_NAME", "RunCommandInner", "ERROR_CODE", ret); - SEN_HILOGE("sensorId:%{public}u grant failed,result:%{public}d", sensorId, ret); - return PERMISSION_DENIED; - } - return RunCommand(sensorId, data.ReadUint32(), data.ReadUint32()); -} - ErrCode SensorServiceStub::GetAllSensorsInner(MessageParcel &data, MessageParcel &reply) { (void)data; -- Gitee From f8394a5d6aebc763a97ec61ecbd9817a27c86403 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Mon, 5 Dec 2022 20:58:07 +0800 Subject: [PATCH 10/25] update Signed-off-by: h00514358 Change-Id: I9578cde52725942b3d9666c8bcfd6305f027c3e8 --- interfaces/native/src/sensor_agent.cpp | 10 --- .../test/unittest/sensor_agent_test.cpp | 73 +++++++++++++++++++ 2 files changed, 73 insertions(+), 10 deletions(-) diff --git a/interfaces/native/src/sensor_agent.cpp b/interfaces/native/src/sensor_agent.cpp index c370db5c..b09a2531 100755 --- a/interfaces/native/src/sensor_agent.cpp +++ b/interfaces/native/src/sensor_agent.cpp @@ -147,13 +147,3 @@ int32_t SetMode(int32_t sensorId, const SensorUser *user, int32_t mode) } return proxy->SetMode(sensorId, user, mode); } - -int32_t SetOption(int32_t sensorId, const SensorUser *user, int32_t option) -{ - const SensorAgentProxy *proxy = GetInstance(); - if (proxy == nullptr) { - SEN_HILOGE("proxy is nullptr"); - return SERVICE_EXCEPTION; - } - return proxy->SetOption(sensorId, user, option); -} \ No newline at end of file diff --git a/interfaces/native/test/unittest/sensor_agent_test.cpp b/interfaces/native/test/unittest/sensor_agent_test.cpp index 4dc06b25..12271683 100755 --- a/interfaces/native/test/unittest/sensor_agent_test.cpp +++ b/interfaces/native/test/unittest/sensor_agent_test.cpp @@ -117,6 +117,17 @@ void SensorDataCallbackImpl(SensorEvent *event) event[0].sensorTypeId, event[0].version, event[0].dataLen, *(sensorData)); } +void SensorDataCallbackImpl2(SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("SensorEvent is null"); + return; + } + float *sensorData = (float *)event[0].data; + SEN_HILOGI("SensorId:%{public}d, version:%{public}d,dataLen:%{public}d,data:%{public}f", + event[0].sensorTypeId, event[0].version, event[0].dataLen, *(sensorData)); +} + HWTEST_F(SensorAgentTest, GetAllSensorsTest_001, TestSize.Level1) { SEN_HILOGI("GetAllSensorsTest_001 in"); @@ -385,5 +396,67 @@ HWTEST_F(SensorAgentTest, SensorListTest_001, TestSize.Level1) sensorInfo[i].minSamplePeriod, sensorInfo[i].maxSamplePeriod); } } + +/* + * Feature: sensor + * Function: SubscribeSensor + * FunctionPoints: Check the interface function + * EnvConditions: mobile that can run ohos test framework + * CaseDescription: Verify the sensor service framework process. + */ +HWTEST_F(SensorAgentTest, SensorNativeApiTest_002, TestSize.Level1) +{ + SEN_HILOGI("SensorNativeApiTest_002 in"); + + SensorUser user; + user.callback = SensorDataCallbackImpl; + + int32_t ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = SetBatch(sensorId, &user, 100000000, 100000000); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = ActivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + SensorUser user2; + user2.callback = SensorDataCallbackImpl2; + + ret = SubscribeSensor(sensorId, &user2); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = SetBatch(sensorId, &user2, 200000000, 100000000); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = ActivateSensor(sensorId, &user2); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + ret = DeactivateSensor(sensorId, &user2); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = UnsubscribeSensor(sensorId, &user2); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorAgentTest, SensorNativeApiTest_003, TestSize.Level1) +{ + SEN_HILOGI("SensorNativeApiTest_003 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = DeactivateSensor(sensorId, &user); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorAgentTest, SensorNativeApiTest_004, TestSize.Level1) +{ + SEN_HILOGI("SensorNativeApiTest_004 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = SetMode(sensorId, &user, SENSOR_DEFAULT_MODE); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} } // namespace Sensors } // namespace OHOS -- Gitee From c7d197cb6b44fe8b684248fb912c1132bcb503b3 Mon Sep 17 00:00:00 2001 From: lixiangpeng5 Date: Mon, 26 Dec 2022 15:13:49 +0800 Subject: [PATCH 11/25] =?UTF-8?q?=E4=BF=AE=E6=94=B9sensor=20name=E7=9A=84N?= =?UTF-8?q?AME=5FMAX=5FLEN:48=E6=94=B9=E4=B8=BA64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lixiangpeng5 Change-Id: I4e48f13d3046eebe1f011bb17c6ace8bbf3df557 --- interfaces/native/include/sensor_agent_type.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index 85531a58..e140e210 100644 --- a/interfaces/native/include/sensor_agent_type.h +++ b/interfaces/native/include/sensor_agent_type.h @@ -47,7 +47,7 @@ extern "C" { /** Maximum length of the sensor name */ #ifndef NAME_MAX_LEN -#define NAME_MAX_LEN 48 +#define NAME_MAX_LEN 64 #endif /* NAME_MAX_LEN */ /** Size of sensor data */ #ifndef SENSOR_USER_DATA_SIZE -- Gitee From 2ca5ec0f59fb50a8030714f8b5512be4283210df Mon Sep 17 00:00:00 2001 From: lixiangpeng5 Date: Mon, 26 Dec 2022 19:32:16 +0800 Subject: [PATCH 12/25] =?UTF-8?q?=E4=BF=AE=E6=94=B9sensor=20name=E7=9A=84N?= =?UTF-8?q?AME=5FMAX=5FLEN:=E6=94=B9=E4=B8=BA128?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lixiangpeng5 Change-Id: Ib8c00fb045584c0ca6c0c4cf23bc967cd5e555dc --- interfaces/native/include/sensor_agent_type.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index e140e210..96a24d6f 100644 --- a/interfaces/native/include/sensor_agent_type.h +++ b/interfaces/native/include/sensor_agent_type.h @@ -47,7 +47,7 @@ extern "C" { /** Maximum length of the sensor name */ #ifndef NAME_MAX_LEN -#define NAME_MAX_LEN 64 +#define NAME_MAX_LEN 128 #endif /* NAME_MAX_LEN */ /** Size of sensor data */ #ifndef SENSOR_USER_DATA_SIZE -- Gitee From 2252f15dd789bcdf9ee9118ea5c9e39b994db47c Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Wed, 8 Feb 2023 07:46:36 +0000 Subject: [PATCH 13/25] update Signed-off-by: hellohyh001 Change-Id: I787c8f768ec1af4ed5f52ee80eac72a7fb48c4dc --- interfaces/plugin/src/sensor_js.cpp | 24 ++++++++++++++++--- .../test/unittest/ExampleJsunit.test.js | 12 +++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 4341fe19..540b8563 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -521,6 +521,24 @@ static napi_value GetGeomagneticField(napi_env env, napi_callback_info info) return nullptr; } +static napi_value GetAxisX(napi_env env, napi_value value) +{ + napi_value napiAxisX = GetNamedProperty(env, value, "x"); + if (napiAxisX == nullptr) { + napiAxisX = GetNamedProperty(env, value, "axisX"); + } + return napiAxisX; +} + +static napi_value GetAxisY(napi_env env, napi_value value) +{ + napi_value napiAxisY = GetNamedProperty(env, value, "y"); + if (napiAxisY == nullptr) { + napiAxisY = GetNamedProperty(env, value, "axisY"); + } + return napiAxisY; +} + static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info info) { CALL_LOG_ENTER; @@ -546,7 +564,7 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf ThrowErr(env, PARAMETER_ERROR, "Wrong inRotationVector length"); return nullptr; } - napi_value napiAxisX = GetNamedProperty(env, args[1], "axisX"); + napi_value napiAxisX = GetAxisX(env, args[1]); if (napiAxisX == nullptr) { ThrowErr(env, PARAMETER_ERROR, "napiAxisX is null"); return nullptr; @@ -556,9 +574,9 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf ThrowErr(env, PARAMETER_ERROR, "Get axisY fail"); return nullptr; } - napi_value napiAxisY = GetNamedProperty(env, args[1], "axisY"); + napi_value napiAxisY = GetAxisY(env, args[1]); if (napiAxisY == nullptr) { - ThrowErr(env, PARAMETER_ERROR, "napiAxisX is null"); + ThrowErr(env, PARAMETER_ERROR, "napiAxisY is null"); return nullptr; } int32_t axisY = 0; diff --git a/interfaces/plugin/test/unittest/ExampleJsunit.test.js b/interfaces/plugin/test/unittest/ExampleJsunit.test.js index 3ab3807c..18dad8b9 100755 --- a/interfaces/plugin/test/unittest/ExampleJsunit.test.js +++ b/interfaces/plugin/test/unittest/ExampleJsunit.test.js @@ -3460,7 +3460,7 @@ describe("SensorJsTest", function () { */ it('Sensor_TransformCoordinateSystem_001', 0, async function (done) { console.info("---------------------------Sensor_TransformCoordinateSystem_001----------------------------------"); - sensor.transformRotationMatrix([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], {'axisX':1, 'axisY':2}, (error, data) => { + sensor.transformRotationMatrix([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], {'x':1, 'y':2}, (error, data) => { if (error) { console.info('Sensor_TransformCoordinateSystem_001 failed'); expect(false).assertTrue(); @@ -3480,7 +3480,7 @@ describe("SensorJsTest", function () { */ it('Sensor_TransformCoordinateSystem_002', 0, async function (done) { console.info("---------------------------Sensor_TransformCoordinateSystem_002----------------------------------"); - sensor.transformRotationMatrix([3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38], {'axisX':1, 'axisY':2}, (error, data) => { + sensor.transformRotationMatrix([3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38], {'x':1, 'y':2}, (error, data) => { if (error) { console.info('Sensor_TransformCoordinateSystem_002 failed'); expect(false).assertTrue(); @@ -3500,7 +3500,7 @@ describe("SensorJsTest", function () { */ it("Sensor_TransformCoordinateSystem_003", 0, async function (done) { console.info("---------------------------Sensor_TransformCoordinateSystem_003----------------------------------"); - sensor.transformRotationMatrix([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], {'axisX':1, 'axisY':2}).then((data) => { + sensor.transformRotationMatrix([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], {'x':1, 'y':2}).then((data) => { for (var i = 0; i < data.length; i++) { console.info("Sensor_TransformCoordinateSystem_003 data[ " + i + "] = " + data[i]); expect(data[i]).assertEqual(transformCoordinateSystemResult[0][i]); @@ -3521,7 +3521,7 @@ describe("SensorJsTest", function () { */ it("Sensor_TransformCoordinateSystem_004", 0, async function (done) { console.info("---------------------------Sensor_TransformCoordinateSystem_004----------------------------------"); - sensor.transformRotationMatrix([3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39], {'axisX':1, 'axisY':3}).then((data) => { + sensor.transformRotationMatrix([3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39], {'x':1, 'y':3}).then((data) => { for (var i = 0; i < data.length; i++) { console.info("Sensor_TransformCoordinateSystem_004 data[ " + i + "] = " + data[i]); expect(data[i]).assertEqual(transformCoordinateSystemResult[2][i]); @@ -3642,7 +3642,7 @@ describe("SensorJsTest", function () { it('Sensor_TransformCoordinateSystem_009', 0, async function (done) { console.info('Sensor_TransformCoordinateSystem_008 start') try { - sensor.transformRotationMatrix([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], {'axisX':1, 'axisY':1}).then((data)=>{ + sensor.transformRotationMatrix([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], {'x':1, 'y':1}).then((data)=>{ console.info("Sensor_TransformCoordinateSystem_009" + data) expect(true).assertfalse() done() @@ -3668,7 +3668,7 @@ describe("SensorJsTest", function () { it('Sensor_TransformCoordinateSystem_010', 0, async function (done) { console.info('Sensor_TransformCoordinateSystem_010 start') try { - sensor.transformRotationMatrix([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], {'axisX':1, 'axisY':1}, (error, data) => { + sensor.transformRotationMatrix([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], {'x':1, 'y':1}, (error, data) => { if (error) { console.info('Sensor_TransformCoordinateSystem_010 failed'); expect(false).assertTrue(); -- Gitee From 6b016614525d90eab5607f4e61ce655d375ca2ea Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Tue, 14 Feb 2023 07:57:46 +0000 Subject: [PATCH 14/25] update Signed-off-by: hellohyh001 Change-Id: If401d7256c5d59b8fbc27299d97894e6bb9a6aa0 --- interfaces/plugin/src/sensor_js.cpp | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 540b8563..d94619d0 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -523,20 +523,12 @@ static napi_value GetGeomagneticField(napi_env env, napi_callback_info info) static napi_value GetAxisX(napi_env env, napi_value value) { - napi_value napiAxisX = GetNamedProperty(env, value, "x"); - if (napiAxisX == nullptr) { - napiAxisX = GetNamedProperty(env, value, "axisX"); - } - return napiAxisX; + return GetNamedProperty(env, value, "x"); } static napi_value GetAxisY(napi_env env, napi_value value) { - napi_value napiAxisY = GetNamedProperty(env, value, "y"); - if (napiAxisY == nullptr) { - napiAxisY = GetNamedProperty(env, value, "axisY"); - } - return napiAxisY; + return GetNamedProperty(env, value, "y"); } static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info info) -- Gitee From d4332376039ef28fafbeae264d394bdafd054dc7 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Fri, 17 Feb 2023 09:41:08 +0000 Subject: [PATCH 15/25] update Signed-off-by: hellohyh001 --- services/sensor/src/flush_info_record.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/sensor/src/flush_info_record.cpp b/services/sensor/src/flush_info_record.cpp index fbdebc6b..dd42cfa5 100644 --- a/services/sensor/src/flush_info_record.cpp +++ b/services/sensor/src/flush_info_record.cpp @@ -69,7 +69,6 @@ bool FlushInfoRecord::IsFlushChannelValid(const std::vector(currChannelList.size())); for (const auto &channel : currChannelList) { - SEN_HILOGD("channel:%{public}p,flushchannel:%{public}p", channel.GetRefPtr(), flushChannel.GetRefPtr()); if (channel == flushChannel) { return true; } -- Gitee From ee22e2d2dcad8e328e88c90a7a64cfcd24ec5d2e Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Sat, 25 Feb 2023 08:19:41 +0000 Subject: [PATCH 16/25] fixed 724264c from https://gitee.com/hellohyh001/sensors_sensor/pulls/339 update Signed-off-by: hellohyh001 Change-Id: I83af3f57538d4b105476f47a408f8a88b7284b04 --- interfaces/plugin/src/sensor_napi_error.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/interfaces/plugin/src/sensor_napi_error.cpp b/interfaces/plugin/src/sensor_napi_error.cpp index 1b2cc5e8..9ff41bb7 100755 --- a/interfaces/plugin/src/sensor_napi_error.cpp +++ b/interfaces/plugin/src/sensor_napi_error.cpp @@ -51,8 +51,11 @@ void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &pri SEN_HILOGE("errCode: %{public}d is invalid", errCode); return; } + napi_handle_scope scope = nullptr; + napi_open_handle_scope(env, &scope); napi_value error = CreateBusinessError(env, errCode, msg.value()); napi_throw(env, error); + napi_close_handle_scope(env, scope); } } // namespace Sensors } // namespace OHOS \ No newline at end of file -- Gitee From 2f663b318e250ee9ed3335f58f21a89bb7c736e5 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 27 Feb 2023 03:01:52 +0000 Subject: [PATCH 17/25] =?UTF-8?q?=E4=BF=AE=E6=94=B9fd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I82e7c352e7617ae448a073742d58086c2f2f8881 --- utils/include/sensors_errors.h | 4 ++-- utils/src/sensor_basic_data_channel.cpp | 12 +++--------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/utils/include/sensors_errors.h b/utils/include/sensors_errors.h index 7c205893..e6a01dc1 100644 --- a/utils/include/sensors_errors.h +++ b/utils/include/sensors_errors.h @@ -99,8 +99,8 @@ enum { SENSOR_CHANNEL_SOCKET_CREATE_ERR = SENSOR_UTILS_ERR_OFFSET, SENSOR_CHANNEL_SENDFD_ERR = SENSOR_CHANNEL_SOCKET_CREATE_ERR + 1, SENSOR_CHANNEL_WRITE_DESCRIPTOR_ERR = SENSOR_CHANNEL_SENDFD_ERR + 1, - SENSOR_CHANNEL_DUP_ERR = SENSOR_CHANNEL_WRITE_DESCRIPTOR_ERR + 1, - SENSOR_CHANNEL_BASIC_CHANNEL_NOT_INIT = SENSOR_CHANNEL_DUP_ERR + 1, + SENSOR_CHANNEL_READ_DESCRIPTOR_ERR = SENSOR_CHANNEL_WRITE_DESCRIPTOR_ERR + 1, + SENSOR_CHANNEL_BASIC_CHANNEL_NOT_INIT = SENSOR_CHANNEL_READ_DESCRIPTOR_ERR + 1, SENSOR_CHANNEL_SEND_ADDR_ERR = SENSOR_CHANNEL_BASIC_CHANNEL_NOT_INIT + 1, SENSOR_CHANNEL_SEND_DATA_ERR = SENSOR_CHANNEL_SEND_ADDR_ERR + 1, SENSOR_CHANNEL_RECEIVE_DATA_ERR = SENSOR_CHANNEL_SEND_DATA_ERR + 1, diff --git a/utils/src/sensor_basic_data_channel.cpp b/utils/src/sensor_basic_data_channel.cpp index 0e7ef077..7e98aeef 100755 --- a/utils/src/sensor_basic_data_channel.cpp +++ b/utils/src/sensor_basic_data_channel.cpp @@ -100,17 +100,11 @@ int32_t SensorBasicDataChannel::CreateSensorBasicChannel(MessageParcel &data) SEN_HILOGD("already create socketpair"); return ERR_OK; } - int32_t tmpFd = data.ReadFileDescriptor(); - if (tmpFd < 0) { - SEN_HILOGE("ReadFileDescriptor is failed"); - sendFd_ = -1; - return SENSOR_CHANNEL_DUP_ERR; - } - sendFd_ = dup(tmpFd); + sendFd_ = data.ReadFileDescriptor(); if (sendFd_ < 0) { - SEN_HILOGE("dup FileDescriptor is failed"); + SEN_HILOGE("ReadFileDescriptor is failed"); sendFd_ = -1; - return SENSOR_CHANNEL_DUP_ERR; + return SENSOR_CHANNEL_READ_DESCRIPTOR_ERR; } return ERR_OK; } -- Gitee From 821b650f9e89acce3843bac3d7bd20cc685983be Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 9 Mar 2023 06:40:18 +0000 Subject: [PATCH 18/25] =?UTF-8?q?=E4=BF=AE=E5=A4=8Donce=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=B8=8A=E6=8A=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I9d4ca1e56194dfe83abfe9277ecc7a54e2044890 --- .../plugin/include/async_callback_info.h | 18 ++++++++++-------- interfaces/plugin/include/sensor_napi_utils.h | 2 ++ interfaces/plugin/src/sensor_napi_utils.cpp | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/interfaces/plugin/include/async_callback_info.h b/interfaces/plugin/include/async_callback_info.h index 2d515f21..a506bae7 100644 --- a/interfaces/plugin/include/async_callback_info.h +++ b/interfaces/plugin/include/async_callback_info.h @@ -111,14 +111,16 @@ public: ~AsyncCallbackInfo() { CALL_LOG_ENTER; - if (asyncWork != nullptr) { - SEN_HILOGD("Delete async work"); - napi_delete_async_work(env, asyncWork); - } - for (int32_t i = 0; i < CALLBACK_NUM; ++i) { - if (callback[i] != nullptr) { - SEN_HILOGD("Delete reference, i:%{public}d", i); - napi_delete_reference(env, callback[i]); + if (type != ONCE_CALLBACK) { + if (asyncWork != nullptr) { + SEN_HILOGD("Delete async work"); + napi_delete_async_work(env, asyncWork); + } + for (int32_t i = 0; i < CALLBACK_NUM; ++i) { + if (callback[i] != nullptr) { + SEN_HILOGD("Delete reference, i:%{public}d", i); + napi_delete_reference(env, callback[i]); + } } } if (work != nullptr) { diff --git a/interfaces/plugin/include/sensor_napi_utils.h b/interfaces/plugin/include/sensor_napi_utils.h index 6adfdb42..ca4549ea 100644 --- a/interfaces/plugin/include/sensor_napi_utils.h +++ b/interfaces/plugin/include/sensor_napi_utils.h @@ -57,6 +57,8 @@ bool CreateFailMessage(CallbackDataType type, int32_t code, string message, sptr &asyncCallbackInfo); bool ConvertToBodyData(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); bool ConvertToCompass(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); +void ReleaseCallback(sptr asyncCallbackInfo); + #define CHKNCF(env, cond, message) \ do { \ diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 4cfecb68..ab3e5fb8 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -418,6 +418,19 @@ bool CreateNapiArray(const napi_env &env, float data[], int32_t dataLength, napi return true; } +void ReleaseCallback(sptr asyncCallbackInfo) +{ + CHKPV(asyncCallbackInfo); + if (asyncCallbackInfo->type == ONCE_CALLBACK) { + napi_env env = asyncCallbackInfo->env; + CHKPV(env); + napi_ref callback = asyncCallbackInfo->callback[0]; + if (callback != nullptr) { + napi_delete_reference(env, callback); + } + } +} + void EmitAsyncCallbackWork(sptr asyncCallbackInfo) { CALL_LOG_ENTER; @@ -504,6 +517,7 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) napi_open_handle_scope(asyncCallbackInfo->env, &scope); if (scope == nullptr) { SEN_HILOGE("napi_handle_scope is nullptr"); + ReleaseCallback(asyncCallbackInfo); return; } napi_env env = asyncCallbackInfo->env; @@ -511,6 +525,7 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) if (napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback) != napi_ok) { SEN_HILOGE("napi_get_reference_value fail"); napi_throw_error(env, nullptr, "napi_get_reference_value fail"); + ReleaseCallback(asyncCallbackInfo); napi_close_handle_scope(asyncCallbackInfo->env, scope); return; } @@ -519,6 +534,7 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) if (!(g_convertfuncList.find(asyncCallbackInfo->type) != g_convertfuncList.end())) { SEN_HILOGE("asyncCallbackInfo type is invalid"); napi_throw_error(env, nullptr, "asyncCallbackInfo type is invalid"); + ReleaseCallback(asyncCallbackInfo); napi_close_handle_scope(asyncCallbackInfo->env, scope); return; } @@ -526,9 +542,11 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) if (napi_call_function(env, nullptr, callback, 1, &result[1], &callResult) != napi_ok) { SEN_HILOGE("napi_call_function callback fail"); napi_throw_error(env, nullptr, "napi_call_function callback fail"); + ReleaseCallback(asyncCallbackInfo); napi_close_handle_scope(asyncCallbackInfo->env, scope); return; } + ReleaseCallback(asyncCallbackInfo); napi_close_handle_scope(asyncCallbackInfo->env, scope); asyncCallbackInfo->work = nullptr; freeWork(work); -- Gitee From a96bf3cba6a5ba447442375b08ba0df42c8ecb05 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Fri, 10 Mar 2023 08:25:52 +0000 Subject: [PATCH 19/25] fixed 22223e3 from https://gitee.com/hellohyh001/sensors_sensor/pulls/348 update Signed-off-by: hellohyh001 Change-Id: I7094f9ad81241bffce06b318af049cd3d23346d9 --- services/sensor/include/fifo_cache_data.h | 4 ++-- services/sensor/src/fifo_cache_data.cpp | 16 ++++++++++++++-- services/sensor/src/sensor_data_processer.cpp | 4 ++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/services/sensor/include/fifo_cache_data.h b/services/sensor/include/fifo_cache_data.h index 7a1713b5..f82e6c7e 100644 --- a/services/sensor/include/fifo_cache_data.h +++ b/services/sensor/include/fifo_cache_data.h @@ -34,13 +34,13 @@ public: void SetFifoCacheData(const std::vector &fifoCacheData); std::vector GetFifoCacheData() const; void SetChannel(const sptr &channel); - sptr GetChannel() const; + bool IsSameChannel(const sptr &channel) const; void InitFifoCache(); private: DISALLOW_COPY_AND_MOVE(FifoCacheData); uint64_t periodCount_; - sptr channel_; + wptr channel_; std::vector fifoCacheData_; }; } // namespace Sensors diff --git a/services/sensor/src/fifo_cache_data.cpp b/services/sensor/src/fifo_cache_data.cpp index b3aa44c8..63dc1710 100644 --- a/services/sensor/src/fifo_cache_data.cpp +++ b/services/sensor/src/fifo_cache_data.cpp @@ -15,8 +15,16 @@ #include "fifo_cache_data.h" +#include "sensors_errors.h" + namespace OHOS { namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "FifoCacheData" }; +} // namespace + FifoCacheData::FifoCacheData() : periodCount_(0), channel_(nullptr) {} @@ -55,9 +63,13 @@ void FifoCacheData::SetChannel(const sptr &channel) { channel_ = channel; } -sptr FifoCacheData::GetChannel() const + +bool FifoCacheData::IsSameChannel(const sptr &channel) const { - return channel_; + CHKPF(channel); + sptr promoteChannel = channel_.promote(); + CHKPF(promoteChannel); + return (channel == promoteChannel); } } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index 5dc6a707..bf22b1de 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -84,7 +84,7 @@ void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_mapsecond) { - if (fifoCacheData->GetChannel() != channel) { + if (!fifoCacheData->IsSameChannel(channel)) { continue; } channelExist = true; @@ -130,7 +130,7 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_mapsecond) { - if (fifoData->GetChannel() != channel) { + if (!fifoData->IsSameChannel(channel)) { continue; } channelExist = true; -- Gitee From 07bd523893febc4afebb58a81ca0687454dc4ee7 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Sat, 18 Mar 2023 07:16:26 +0000 Subject: [PATCH 20/25] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: If77c64ecf5a6f963269ac123ef23c50c6b443cc0 --- services/sensor/include/fifo_cache_data.h | 2 +- services/sensor/include/flush_info_record.h | 2 +- services/sensor/include/sensor_service.h | 1 - services/sensor/src/fifo_cache_data.cpp | 15 ++---------- services/sensor/src/flush_info_record.cpp | 2 +- services/sensor/src/sensor_data_processer.cpp | 24 +++++++++++++++---- services/sensor/src/sensor_service.cpp | 2 -- 7 files changed, 25 insertions(+), 23 deletions(-) diff --git a/services/sensor/include/fifo_cache_data.h b/services/sensor/include/fifo_cache_data.h index f82e6c7e..6361e06f 100644 --- a/services/sensor/include/fifo_cache_data.h +++ b/services/sensor/include/fifo_cache_data.h @@ -34,7 +34,7 @@ public: void SetFifoCacheData(const std::vector &fifoCacheData); std::vector GetFifoCacheData() const; void SetChannel(const sptr &channel); - bool IsSameChannel(const sptr &channel) const; + sptr GetChannel() const; void InitFifoCache(); private: diff --git a/services/sensor/include/flush_info_record.h b/services/sensor/include/flush_info_record.h index dfea8a6e..d2c02a8a 100644 --- a/services/sensor/include/flush_info_record.h +++ b/services/sensor/include/flush_info_record.h @@ -33,7 +33,7 @@ namespace OHOS { namespace Sensors { struct FlushInfo { - sptr flushChannel; + wptr flushChannel; bool flushFromEnable; FlushInfo(const sptr &channel, bool enableFlush) : flushChannel(channel), flushFromEnable(enableFlush){}; diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 9c5893e9..d28a6681 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -75,7 +75,6 @@ private: SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); SensorManager &sensorManager_ = SensorManager::GetInstance(); - FlushInfoRecord &flushInfo_ = FlushInfoRecord::GetInstance(); sptr sensorDataProcesser_; sptr reportDataCallback_; std::mutex uidLock_; diff --git a/services/sensor/src/fifo_cache_data.cpp b/services/sensor/src/fifo_cache_data.cpp index 63dc1710..45bf5ef4 100644 --- a/services/sensor/src/fifo_cache_data.cpp +++ b/services/sensor/src/fifo_cache_data.cpp @@ -14,16 +14,8 @@ */ #include "fifo_cache_data.h" - -#include "sensors_errors.h" - namespace OHOS { namespace Sensors { -using namespace OHOS::HiviewDFX; - -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "FifoCacheData" }; -} // namespace FifoCacheData::FifoCacheData() : periodCount_(0), channel_(nullptr) {} @@ -64,12 +56,9 @@ void FifoCacheData::SetChannel(const sptr &channel) channel_ = channel; } -bool FifoCacheData::IsSameChannel(const sptr &channel) const +sptr FifoCacheData::GetChannel() const { - CHKPF(channel); - sptr promoteChannel = channel_.promote(); - CHKPF(promoteChannel); - return (channel == promoteChannel); + return channel_.promote(); } } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/flush_info_record.cpp b/services/sensor/src/flush_info_record.cpp index dd42cfa5..25e158e0 100644 --- a/services/sensor/src/flush_info_record.cpp +++ b/services/sensor/src/flush_info_record.cpp @@ -80,7 +80,7 @@ int32_t FlushInfoRecord::GetFlushChannelIndex(const std::vector &flus const sptr &channel) { for (size_t i = 0; i < flushInfoList.size(); i++) { - if (flushInfoList[i].flushChannel == channel) { + if (flushInfoList[i].flushChannel.promote() == channel) { return i; } } diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index bf22b1de..3053cf7e 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -83,8 +83,16 @@ void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_mapsecond) { - if (!fifoCacheData->IsSameChannel(channel)) { + for (auto fifoIt = dataCountIt->second.begin(); fifoIt != dataCountIt->second.end();) { + auto fifoCacheData = *fifoIt; + CHKPC(fifoCacheData); + auto fifoChannel = fifoCacheData->GetChannel(); + if (fifoChannel == nullptr) { + fifoIt = dataCountIt->second.erase(fifoIt); + continue; + } + ++fifoIt; + if (fifoChannel != channel) { continue; } channelExist = true; @@ -129,8 +137,16 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_mapsecond) { - if (!fifoData->IsSameChannel(channel)) { + for (auto fifoIt = dataCountIt->second.begin(); fifoIt != dataCountIt->second.end();) { + auto fifoData = *fifoIt; + CHKPC(fifoData); + auto fifoChannel = fifoData->GetChannel(); + if (fifoChannel == nullptr) { + fifoIt = dataCountIt->second.erase(fifoIt); + continue; + } + ++fifoIt; + if (fifoChannel != channel) { continue; } channelExist = true; diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index aecd057d..227044ed 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -227,8 +227,6 @@ ErrCode SensorService::EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, return ret; } ReportSensorSysEvent(sensorId, true, pid); - uint32_t flag = sensorManager_.GetSensorFlag(sensorId); - ret = flushInfo_.FlushProcess(sensorId, flag, pid, true); if (ret != ERR_OK) { SEN_HILOGE("ret : %{public}d", ret); } -- Gitee From 6d9f33f3fab397cd66ba338c1743878952ef813f Mon Sep 17 00:00:00 2001 From: hui1975 Date: Sat, 18 Mar 2023 07:41:57 +0000 Subject: [PATCH 21/25] update Signed-off-by: hui1975 Change-Id: Ibec6b972816bf62fba7798db028720e0f9b39c1a --- services/sensor/src/sensor_data_processer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index 3053cf7e..726e5e21 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -311,7 +311,7 @@ void SensorDataProcesser::EventFilter(CircularEventBuf &eventsBuf) if (it != flushInfo.end()) { flushVec = it->second; for (auto &channel : flushVec) { - if (flushInfo_.IsFlushChannelValid(channelList, channel.flushChannel)) { + if (flushInfo_.IsFlushChannelValid(channelList, channel.flushChannel.promote())) { SendEvents(channel.flushChannel, eventsBuf.circularBuf[eventsBuf.readPos]); flushInfo_.ClearFlushInfoItem(realSensorId); break; -- Gitee From ef70a6cf3d6946962ace57fea057ea058dcee301 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Sat, 18 Mar 2023 07:52:38 +0000 Subject: [PATCH 22/25] update Signed-off-by: hui1975 Change-Id: I6393ed4dc44a8995a95a43b14192d9ea1f780d86 --- services/sensor/src/sensor_data_processer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index 726e5e21..db9ba7ed 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -312,7 +312,7 @@ void SensorDataProcesser::EventFilter(CircularEventBuf &eventsBuf) flushVec = it->second; for (auto &channel : flushVec) { if (flushInfo_.IsFlushChannelValid(channelList, channel.flushChannel.promote())) { - SendEvents(channel.flushChannel, eventsBuf.circularBuf[eventsBuf.readPos]); + SendEvents(channel.flushChannel.promote(), eventsBuf.circularBuf[eventsBuf.readPos]); flushInfo_.ClearFlushInfoItem(realSensorId); break; } else { -- Gitee From 02286595461961e482a4cb9f08de3ff78b736f67 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Sat, 18 Mar 2023 08:07:19 +0000 Subject: [PATCH 23/25] update Signed-off-by: hui1975 Change-Id: If7c622897eb358da2afbfc80a2b7e207c8abe382 --- services/sensor/src/sensor_data_processer.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index db9ba7ed..44416d9e 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -311,8 +311,9 @@ void SensorDataProcesser::EventFilter(CircularEventBuf &eventsBuf) if (it != flushInfo.end()) { flushVec = it->second; for (auto &channel : flushVec) { - if (flushInfo_.IsFlushChannelValid(channelList, channel.flushChannel.promote())) { - SendEvents(channel.flushChannel.promote(), eventsBuf.circularBuf[eventsBuf.readPos]); + auto flushChannel = channel.flushChannel.promote(); + if (flushInfo_.IsFlushChannelValid(channelList, flushChannel)) { + SendEvents(flushChannel, eventsBuf.circularBuf[eventsBuf.readPos]); flushInfo_.ClearFlushInfoItem(realSensorId); break; } else { -- Gitee From 1c1c95d07ef30ca4dedb6b863f44c2b634d48eed Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 24 Mar 2023 07:40:43 +0000 Subject: [PATCH 24/25] =?UTF-8?q?=E5=87=8F=E5=B0=91=E8=A1=8C=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: Ib40b169150791ab62ea0ba54705bfb6ccb25f915 --- services/sensor/src/sensor_data_processer.cpp | 78 +------------------ 1 file changed, 4 insertions(+), 74 deletions(-) diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index 44416d9e..130e9d81 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -32,20 +32,6 @@ using namespace OHOS::HiviewDFX; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorDataProcesser" }; - -enum { - FIRST_INDEX = 1, - SECOND_INDEX = 2, - THIRD_INDEX = 3, -}; - -constexpr uint32_t SENSOR_INDEX_SHIFT = 8; -constexpr uint32_t SENSOR_TYPE_SHIFT = 16; -constexpr uint32_t SENSOR_CATAGORY_SHIFT = 24; - -constexpr uint32_t FLUSH_COMPLETE_ID = ((uint32_t)OTHER << SENSOR_CATAGORY_SHIFT) | - ((uint32_t)SENSOR_TYPE_FLUSH << SENSOR_TYPE_SHIFT) | - ((uint32_t)FIRST_INDEX << SENSOR_INDEX_SHIFT); } // namespace SensorDataProcesser::SensorDataProcesser(const std::unordered_map &sensorMap) @@ -68,9 +54,6 @@ void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map dataCountLock(dataCountMutex_); sendEvents.push_back(event); uint32_t sensorId = static_cast(event.sensorTypeId); - if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); - } auto dataCountIt = dataCountMap_.find(sensorId); if (dataCountIt == dataCountMap_.end()) { std::vector> channelFifoList; @@ -120,9 +103,6 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_map(event.sensorTypeId); - if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); - } std::lock_guard dataCountLock(dataCountMutex_); auto dataCountIt = dataCountMap_.find(sensorId); // there is no channelFifoList @@ -180,9 +160,6 @@ void SensorDataProcesser::ReportData(sptr &channel, Sens { CHKPV(channel); uint32_t sensorId = static_cast(event.sensorTypeId); - if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); - } auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); if (ReportNotContinuousData(cacheBuf, channel, event)) { return; @@ -203,9 +180,6 @@ bool SensorDataProcesser::ReportNotContinuousData(std::unordered_map &channel, SensorEvent &event) { uint32_t sensorId = static_cast(event.sensorTypeId); - if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); - } std::lock_guard sensorLock(sensorMutex_); auto sensor = sensorMap_.find(sensorId); if (sensor == sensorMap_.end()) { @@ -252,9 +226,6 @@ void SensorDataProcesser::SendRawData(std::unordered_map if (ret != ERR_OK) { SEN_HILOGE("send data failed, ret:%{public}d", ret); uint32_t sensorId = static_cast(event[eventSize - 1].sensorTypeId); - if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event[eventSize - 1].sensorTypeId); - } cacheBuf[sensorId] = event[eventSize - 1]; } } @@ -265,9 +236,6 @@ int32_t SensorDataProcesser::CacheSensorEvent(const SensorEvent &event, sptr &>(channel->GetDataCacheBuf()); uint32_t sensorId = static_cast(event.sensorTypeId); - if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); - } auto cacheEvent = cacheBuf.find(sensorId); if (cacheEvent != cacheBuf.end()) { // Try to send the last failed value, if it still fails, replace the previous cache directly @@ -294,49 +262,11 @@ int32_t SensorDataProcesser::CacheSensorEvent(const SensorEvent &event, sptr(eventsBuf.circularBuf[eventsBuf.readPos].sensorTypeId); - std::vector> channelList; - if (sensorId == FLUSH_COMPLETE_ID) { - realSensorId = static_cast(eventsBuf.circularBuf[eventsBuf.readPos].sensorTypeId); - channelList = clientInfo_.GetSensorChannel(realSensorId); - } else { - channelList = clientInfo_.GetSensorChannel(sensorId); - } - auto flushInfo = flushInfo_.GetFlushInfo(); - std::vector flushVec; - if (sensorId == FLUSH_COMPLETE_ID) { - SEN_HILOGD("sensorId:%{public}u", sensorId); - auto it = flushInfo.find(realSensorId); - if (it != flushInfo.end()) { - flushVec = it->second; - for (auto &channel : flushVec) { - auto flushChannel = channel.flushChannel.promote(); - if (flushInfo_.IsFlushChannelValid(channelList, flushChannel)) { - SendEvents(flushChannel, eventsBuf.circularBuf[eventsBuf.readPos]); - flushInfo_.ClearFlushInfoItem(realSensorId); - break; - } else { - // The channel that store in the flushVec has invalid, so erase this channel directly - SEN_HILOGD("clear flush info"); - flushInfo_.ClearFlushInfoItem(realSensorId); - } - } - } - } else { - for (auto &channel : channelList) { - int32_t index = flushInfo_.GetFlushChannelIndex(flushVec, channel); - if (index >= 0) { - if (flushVec[index].flushFromEnable) { - SEN_HILOGI("flushFromEnable"); - continue; - } - } - /* if has some suspend flush, but this flush come from the flush function rather than enable, - so we need to calling GetSensorStatus to decided whether send this event. */ - if (channel->GetSensorStatus()) { - SendEvents(channel, eventsBuf.circularBuf[eventsBuf.readPos]); - } + std::vector> channelList = clientInfo_.GetSensorChannel(sensorId); + for (auto &channel : channelList) { + if (channel->GetSensorStatus()) { + SendEvents(channel, eventsBuf.circularBuf[eventsBuf.readPos]); } } } -- Gitee From 3029cf23ae418df1347acae554f988fd17c8067d Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 24 Mar 2023 07:56:07 +0000 Subject: [PATCH 25/25] update Signed-off-by: hui1975 Change-Id: I8c4ceae19000554096075e8fb8146aa7fe2ca47f --- services/sensor/src/sensor_data_processer.cpp | 72 +++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index 130e9d81..967839b8 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -32,6 +32,20 @@ using namespace OHOS::HiviewDFX; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorDataProcesser" }; + +enum { + FIRST_INDEX = 1, + SECOND_INDEX = 2, + THIRD_INDEX = 3, +}; + +constexpr uint32_t SENSOR_INDEX_SHIFT = 8; +constexpr uint32_t SENSOR_TYPE_SHIFT = 16; +constexpr uint32_t SENSOR_CATAGORY_SHIFT = 24; + +constexpr uint32_t FLUSH_COMPLETE_ID = ((uint32_t)OTHER << SENSOR_CATAGORY_SHIFT) | + ((uint32_t)SENSOR_TYPE_FLUSH << SENSOR_TYPE_SHIFT) | + ((uint32_t)FIRST_INDEX << SENSOR_INDEX_SHIFT); } // namespace SensorDataProcesser::SensorDataProcesser(const std::unordered_map &sensorMap) @@ -160,6 +174,9 @@ void SensorDataProcesser::ReportData(sptr &channel, Sens { CHKPV(channel); uint32_t sensorId = static_cast(event.sensorTypeId); + if (sensorId == FLUSH_COMPLETE_ID) { + sensorId = static_cast(event.sensorTypeId); + } auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); if (ReportNotContinuousData(cacheBuf, channel, event)) { return; @@ -180,6 +197,9 @@ bool SensorDataProcesser::ReportNotContinuousData(std::unordered_map &channel, SensorEvent &event) { uint32_t sensorId = static_cast(event.sensorTypeId); + if (sensorId == FLUSH_COMPLETE_ID) { + sensorId = static_cast(event.sensorTypeId); + } std::lock_guard sensorLock(sensorMutex_); auto sensor = sensorMap_.find(sensorId); if (sensor == sensorMap_.end()) { @@ -226,6 +246,9 @@ void SensorDataProcesser::SendRawData(std::unordered_map if (ret != ERR_OK) { SEN_HILOGE("send data failed, ret:%{public}d", ret); uint32_t sensorId = static_cast(event[eventSize - 1].sensorTypeId); + if (sensorId == FLUSH_COMPLETE_ID) { + sensorId = static_cast(event[eventSize - 1].sensorTypeId); + } cacheBuf[sensorId] = event[eventSize - 1]; } } @@ -236,6 +259,9 @@ int32_t SensorDataProcesser::CacheSensorEvent(const SensorEvent &event, sptr &>(channel->GetDataCacheBuf()); uint32_t sensorId = static_cast(event.sensorTypeId); + if (sensorId == FLUSH_COMPLETE_ID) { + sensorId = static_cast(event.sensorTypeId); + } auto cacheEvent = cacheBuf.find(sensorId); if (cacheEvent != cacheBuf.end()) { // Try to send the last failed value, if it still fails, replace the previous cache directly @@ -262,11 +288,49 @@ int32_t SensorDataProcesser::CacheSensorEvent(const SensorEvent &event, sptr(eventsBuf.circularBuf[eventsBuf.readPos].sensorTypeId); - std::vector> channelList = clientInfo_.GetSensorChannel(sensorId); - for (auto &channel : channelList) { - if (channel->GetSensorStatus()) { - SendEvents(channel, eventsBuf.circularBuf[eventsBuf.readPos]); + std::vector> channelList; + if (sensorId == FLUSH_COMPLETE_ID) { + realSensorId = static_cast(eventsBuf.circularBuf[eventsBuf.readPos].sensorTypeId); + channelList = clientInfo_.GetSensorChannel(realSensorId); + } else { + channelList = clientInfo_.GetSensorChannel(sensorId); + } + auto flushInfo = flushInfo_.GetFlushInfo(); + std::vector flushVec; + if (sensorId == FLUSH_COMPLETE_ID) { + SEN_HILOGD("sensorId:%{public}u", sensorId); + auto it = flushInfo.find(realSensorId); + if (it != flushInfo.end()) { + flushVec = it->second; + for (auto &channel : flushVec) { + auto flushChannel = channel.flushChannel.promote(); + if (flushInfo_.IsFlushChannelValid(channelList, flushChannel)) { + SendEvents(flushChannel, eventsBuf.circularBuf[eventsBuf.readPos]); + flushInfo_.ClearFlushInfoItem(realSensorId); + break; + } else { + // The channel that store in the flushVec has invalid, so erase this channel directly + SEN_HILOGD("clear flush info"); + flushInfo_.ClearFlushInfoItem(realSensorId); + } + } + } + } else { + for (auto &channel : channelList) { + int32_t index = flushInfo_.GetFlushChannelIndex(flushVec, channel); + if (index >= 0) { + if (flushVec[index].flushFromEnable) { + SEN_HILOGI("flushFromEnable"); + continue; + } + } + /* if has some suspend flush, but this flush come from the flush function rather than enable, + so we need to calling GetSensorStatus to decided whether send this event. */ + if (channel->GetSensorStatus()) { + SendEvents(channel, eventsBuf.circularBuf[eventsBuf.readPos]); + } } } } -- Gitee