From 323b2d96c7b863f9967c6ef6274966a4d5e8536f Mon Sep 17 00:00:00 2001 From: HYH Date: Tue, 12 Jul 2022 13:16:58 +0000 Subject: [PATCH 01/10] Signed-off-by:hellohyh001 --- .../hdi_connection/adapter/src/compatible_connection.cpp | 1 + services/sensor/src/sensor_data_processer.cpp | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp index 86273d68..f4bf6f62 100644 --- a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp @@ -138,6 +138,7 @@ int32_t CompatibleConnection::SensorDataCallback(const struct SensorEvents *even .dataLen = event->dataLen }; sensorEvent.data = new uint8_t[SENSOR_DATA_LENGHT]; + CHKPR(sensorEvent.data, ERR_INVALID_VALUE); errno_t ret = memcpy_s(sensorEvent.data, event->dataLen, event->data, event->dataLen); if (ret != EOK) { SEN_HILOGE("copy data failed"); diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index be50d8c7..d88d4911 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -356,7 +356,9 @@ int32_t SensorDataProcesser::ProcessEvents(sptr dataCallback int32_t eventNum = eventsBuf.eventNum; for (int32_t i = 0; i < eventNum; i++) { EventFilter(eventsBuf); - delete[] eventsBuf.circularBuf[eventsBuf.readPos].data; + if (eventsBuf.circularBuf[eventsBuf.readPos].data != nullptr) { + delete[] eventsBuf.circularBuf[eventsBuf.readPos].data; + } eventsBuf.readPos++; if (eventsBuf.readPos == CIRCULAR_BUF_LEN) { eventsBuf.readPos = 0; -- Gitee From 1c94b891730536901bab81d7f3fbfd2555dcf647 Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Fri, 15 Jul 2022 06:30:45 +0000 Subject: [PATCH 02/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ut=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuzhihuitmac Change-Id: I38870e1fbdb4156e2a0525b754a6588ad5cbfad7 --- bundle.json | 3 +- interfaces/native/test/BUILD.gn | 2 - .../test/unittest/sensor_agent_test.cpp | 240 ++++++++++++++++-- 3 files changed, 224 insertions(+), 21 deletions(-) diff --git a/bundle.json b/bundle.json index 298936c9..73605b17 100644 --- a/bundle.json +++ b/bundle.json @@ -56,7 +56,8 @@ ], "test": [ "//base/sensors/sensor/interfaces/plugin/test/unittest:unittest", - "//base/sensors/sensor/interfaces/native/test/fuzztest:fuzztest" + "//base/sensors/sensor/interfaces/native/test/fuzztest:fuzztest", + "//base/sensors/sensor/interfaces/native/test:unittest" ] } } diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index c0d402a8..363c7398 100644 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -37,8 +37,6 @@ ohos_unittest("SensorAgentTest") { ] external_deps = [ "hiviewdfx_hilog_native:libhilog", - - #"communication_L2:ipc_core", "ipc:ipc_core", ] } diff --git a/interfaces/native/test/unittest/sensor_agent_test.cpp b/interfaces/native/test/unittest/sensor_agent_test.cpp index ac31dec7..66c262e8 100755 --- a/interfaces/native/test/unittest/sensor_agent_test.cpp +++ b/interfaces/native/test/unittest/sensor_agent_test.cpp @@ -25,7 +25,9 @@ using namespace testing::ext; using namespace OHOS::HiviewDFX; namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorAgentTest" }; +constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorAgentTest" }; +constexpr int32_t sensorId { 0 }; +constexpr int32_t invalidValue { -1 }; } // namespace class SensorAgentTest : public testing::Test { @@ -51,12 +53,216 @@ void SensorAgentTest::TearDown() void SensorDataCallbackImpl(SensorEvent *event) { if (event == nullptr) { - HiLog::Error(LABEL, "SensorDataCallbackImpl event is null"); + SEN_HILOGE("SensorEvent is null"); return; } float *sensorData = (float *)event[0].data; - HiLog::Info(LABEL, "SensorDataCallbackImpl sensorTypeId: %{public}d, version: %{public}d, dataLen: %{public}d, dataLen: %{public}f\n", - event[0].sensorTypeId, event[0].version, event[0].dataLen, *(sensorData)); + SEN_HILOGI("SensorId: %{public}d, version: %{public}d, dataLen: %{public}d," + "data: %{public}f\n", event[0].sensorTypeId, event[0].version, event[0].dataLen, *(sensorData)); +} + +HWTEST_F(SensorAgentTest, GetAllSensorsTest_001, TestSize.Level1) +{ + SEN_HILOGI("GetAllSensorsTest_001 in"); + SensorInfo *sensorInfos { nullptr }; + int32_t count { 0 }; + int32_t ret = GetAllSensors(&sensorInfos, &count); + ASSERT_EQ(ret, 0); + ASSERT_NE(count, 0); +} + +HWTEST_F(SensorAgentTest, GetAllSensorsTest_002, TestSize.Level1) +{ + SEN_HILOGI("GetAllSensorsTest_002 in"); + int32_t count = 0; + int32_t ret = GetAllSensors(nullptr, &count); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); + ASSERT_EQ(count, 0); +} + +HWTEST_F(SensorAgentTest, GetAllSensorsTest_003, TestSize.Level1) +{ + SEN_HILOGI("GetAllSensorsTest_003 in"); + SensorInfo *sensorInfos { nullptr }; + int32_t ret = GetAllSensors(&sensorInfos, nullptr); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, ActivateSensorTest_001, TestSize.Level1) +{ + SEN_HILOGI("ActivateSensorTest_001 in"); + SensorUser user; + user.callback = nullptr; + int32_t ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); + ret = SetBatch(sensorId, &user, 100000000, 100000000); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); + ret = ActivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); + ret = DeactivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); + ret = UnsubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, ActivateSensorTest_002, TestSize.Level1) +{ + SEN_HILOGI("ActivateSensorTest_002 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = ActivateSensor(invalidValue, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, ActivateSensorTest_003, TestSize.Level1) +{ + SEN_HILOGI("ActivateSensorTest_003 in"); + int32_t ret = ActivateSensor(sensorId, nullptr); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, DeactivateSensorTest_001, TestSize.Level1) +{ + SEN_HILOGI("DeactivateSensorTest_001 in"); + SensorUser user; + user.callback = nullptr; + int32_t ret = DeactivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, DeactivateSensorTest_002, TestSize.Level1) +{ + SEN_HILOGI("DeactivateSensorTest_002 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = DeactivateSensor(invalidValue, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, DeactivateSensorTest_003, TestSize.Level1) +{ + SEN_HILOGI("DeactivateSensorTest_003 in"); + int32_t ret = DeactivateSensor(sensorId, nullptr); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, DeactivateSensorTest_004, TestSize.Level1) +{ + SEN_HILOGI("DeactivateSensorTest_004 in"); + SensorUser user; + user.callback = nullptr; + int32_t ret = DeactivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, SetBatchTest_001, TestSize.Level1) +{ + SEN_HILOGI("SetBatchTest_001 in"); + SensorUser user; + user.callback = nullptr; + int32_t ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); + ret = SetBatch(sensorId, &user, 100000000, 100000000); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, SetBatchTest_002, TestSize.Level1) +{ + SEN_HILOGI("SetBatchTest_002 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = SetBatch(invalidValue, &user, 100000000, 100000000); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, SetBatchTest_003, TestSize.Level1) +{ + SEN_HILOGI("SetBatchTest_003 in"); + int32_t ret = SetBatch(sensorId, nullptr, invalidValue, invalidValue); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, SubscribeSensorTest_001, TestSize.Level1) +{ + SEN_HILOGI("SubscribeSensorTest_001 in"); + SensorUser user; + user.callback = nullptr; + int32_t ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, SubscribeSensorTest_002, TestSize.Level1) +{ + SEN_HILOGI("SubscribeSensorTest_002 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = SubscribeSensor(invalidValue, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, SubscribeSensorTest_003, TestSize.Level1) +{ + SEN_HILOGI("SubscribeSensorTest_003 in"); + int32_t ret = SubscribeSensor(sensorId, nullptr); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, UnsubscribeSensorTest_001, TestSize.Level1) +{ + SEN_HILOGI("UnsubscribeSensorTest_001 in"); + SensorUser user; + user.callback = nullptr; + int32_t ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); + ret = UnsubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, UnsubscribeSensorTest_002, TestSize.Level1) +{ + SEN_HILOGI("UnsubscribeSensorTest_002 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = UnsubscribeSensor(invalidValue, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, UnsubscribeSensorTest_003, TestSize.Level1) +{ + SEN_HILOGI("UnsubscribeSensorTest_003 in"); + int32_t ret = UnsubscribeSensor(sensorId, nullptr); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, SetModeTest_001, TestSize.Level1) +{ + SEN_HILOGI("SetModeTest_001 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + int32_t mode { 0 }; + ret = SetMode(sensorId, &user, mode); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = UnsubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, SetModeTest_002, TestSize.Level1) +{ + SEN_HILOGI("SetModeTest_002 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + int32_t ret = SetMode(invalidValue, &user, invalidValue); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); +} + +HWTEST_F(SensorAgentTest, SetModeTest_003, TestSize.Level1) +{ + SEN_HILOGI("SetModeTest_003 in"); + int32_t mode { 0 }; + int32_t ret = SetMode(sensorId, nullptr, mode); + ASSERT_EQ(ret, OHOS::Sensors::ERROR); } /* @@ -68,30 +274,28 @@ void SensorDataCallbackImpl(SensorEvent *event) */ HWTEST_F(SensorAgentTest, SensorNativeApiTest_001, TestSize.Level1) { - HiLog::Info(LABEL, "%{public}s begin", __func__); + SEN_HILOGI("SensorNativeApiTest_001 in"); - int32_t sensorTypeId = 0; SensorUser user; - user.callback = SensorDataCallbackImpl; - int32_t ret = SubscribeSensor(sensorTypeId, &user); - ASSERT_EQ(ret, 0); + int32_t ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); - ret = SetBatch(sensorTypeId, &user, 100000000, 100000000); - ASSERT_EQ(ret, 0); + ret = SetBatch(sensorId, &user, 100000000, 100000000); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); - ret = ActivateSensor(sensorTypeId, &user); - ASSERT_EQ(ret, 0); + ret = ActivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); std::this_thread::sleep_for(std::chrono::milliseconds(10000)); - ASSERT_EQ(ret, 0); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); - ret = DeactivateSensor(sensorTypeId, &user); - ASSERT_EQ(ret, 0); + ret = DeactivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); - ret = UnsubscribeSensor(sensorTypeId, &user); - ASSERT_EQ(ret, 0); + ret = UnsubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); } } // namespace Sensors } // namespace OHOS -- Gitee From 2ff7b9887c686067cd5795cb158477d929cb4553 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Fri, 15 Jul 2022 17:26:36 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9sensor=E6=89=93?= =?UTF-8?q?=E7=82=B9yaml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite --- hisysevent.yaml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/hisysevent.yaml b/hisysevent.yaml index fce6515b..da11daa1 100644 --- a/hisysevent.yaml +++ b/hisysevent.yaml @@ -26,26 +26,26 @@ DISABLE_SENSOR: TYPE: {type: INT32, desc: type} SENSOR_DATA_CHANNEL_EXCEPTION: - __BASE: {type: FAULT, level: MINOR, tag: PowerStats, desc: sensor data channel exception} - FUNC_NAME: {type: STRING, desc: function name} + __BASE: {type: FAULT, level: MINOR, desc: sensor data channel exception} + PKG_NAME: {type: STRING, desc: function name} ERROR_CODE: {type: INT32, desc: error code} SENSOR_VERIFY_ACCESS_TOKEN_FAIL: - __BASE: {type: SECURITY, level: MINOR, tag: PowerStats, desc: sensor verify access token fail} - FUNC_NAME: {type: STRING, desc: function name} + __BASE: {type: SECURITY, level: MINOR, desc: sensor verify access token fail} + PKG_NAME: {type: STRING, desc: function name} ERROR_CODE: {type: INT32, desc: error code} SENSOR_SERVICE_IPC_EXCEPTION: - __BASE: {type: FAULT, level: MINOR, tag: PowerStats, desc: sensor service ipc exception} - FUNC_NAME: {type: STRING, desc: function name} + __BASE: {type: FAULT, level: MINOR, desc: sensor service ipc exception} + PKG_NAME: {type: STRING, desc: function name} ERROR_CODE: {type: INT32, desc: error code} SENSOR_HDF_SERVICE_EXCEPTION: - __BASE: {type: FAULT, level: MINOR, tag: PowerStats, desc: sensor hdf service exception} - FUNC_NAME: {type: STRING, desc: function name} + __BASE: {type: FAULT, level: MINOR, desc: sensor hdf service exception} + PKG_NAME: {type: STRING, desc: function name} ERROR_CODE: {type: INT32, desc: error code} SENSOR_SERVICE_EXCEPTION: - __BASE: {type: FAULT, level: MINOR, tag: PowerStats, desc: sensor service exception} - FUNC_NAME: {type: STRING, desc: function name} + __BASE: {type: FAULT, level: MINOR, desc: sensor service exception} + PKG_NAME: {type: STRING, desc: function name} ERROR_CODE: {type: INT32, desc: error code} \ No newline at end of file -- Gitee From b536091d758237aa765f7f1711533341fae158af Mon Sep 17 00:00:00 2001 From: cff-gite Date: Mon, 18 Jul 2022 09:31:21 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9sensor=E6=89=93?= =?UTF-8?q?=E7=82=B9yaml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite --- .../sensor/src/sensor_service_client.cpp | 2 +- .../native/sensor/src/sensor_service_proxy.cpp | 14 +++++++------- .../adapter/src/hdi_connection.cpp | 18 +++++++++--------- services/sensor/src/sensor_data_processer.cpp | 2 +- services/sensor/src/sensor_service_stub.cpp | 8 ++++---- utils/src/sensor_basic_data_channel.cpp | 4 ++-- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index c6298e47..8568cfbb 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -68,7 +68,7 @@ int32_t SensorServiceClient::InitServiceClient() retry++; } HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "InitServiceClient", "ERROR_CODE", SENSOR_NATIVE_GET_SERVICE_ERR); + HiSysEvent::EventType::FAULT, "PKG_NAME", "InitServiceClient", "ERROR_CODE", SENSOR_NATIVE_GET_SERVICE_ERR); SEN_HILOGE("get service failed"); return SENSOR_NATIVE_GET_SERVICE_ERR; } diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index a0a17892..dce8e0b5 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -65,7 +65,7 @@ ErrCode SensorServiceProxy::EnableSensor(uint32_t sensorId, int64_t samplingPeri int32_t ret = remote->SendRequest(ISensorService::ENABLE_SENSOR, data, reply, option); if (ret != NO_ERROR) { HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "EnaleSensor", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "EnaleSensor", "ERROR_CODE", ret); SEN_HILOGE("failed, ret : %{public}d", ret); } return static_cast(ret); @@ -89,7 +89,7 @@ ErrCode SensorServiceProxy::DisableSensor(uint32_t sensorId) int32_t ret = remote->SendRequest(ISensorService::DISABLE_SENSOR, data, reply, option); if (ret != NO_ERROR) { HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "DisableSensor", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "DisableSensor", "ERROR_CODE", ret); SEN_HILOGE("failed, ret : %{public}d", ret); } return static_cast(ret); @@ -113,7 +113,7 @@ int32_t SensorServiceProxy::GetSensorState(uint32_t sensorId) 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, "FUNC_NAME", "GetSensorState", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "GetSensorState", "ERROR_CODE", ret); SEN_HILOGE("failed, ret : %{public}d", ret); } return static_cast(ret); @@ -149,7 +149,7 @@ ErrCode SensorServiceProxy::RunCommand(uint32_t sensorId, uint32_t cmdType, uint 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, "FUNC_NAME", "RunCommand", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "RunCommand", "ERROR_CODE", ret); SEN_HILOGE("failed, ret : %{public}d", ret); } return static_cast(ret); @@ -173,7 +173,7 @@ std::vector SensorServiceProxy::GetSensorList() int32_t ret = remote->SendRequest(ISensorService::GET_SENSOR_LIST, data, reply, option); if (ret != NO_ERROR) { HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "GetSensorList", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "GetSensorList", "ERROR_CODE", ret); SEN_HILOGE("failed, ret : %{public}d", ret); return sensors; } @@ -216,7 +216,7 @@ ErrCode SensorServiceProxy::TransferDataChannel(const sptrSendRequest(ISensorService::TRANSFER_DATA_CHANNEL, data, reply, option); if (ret != NO_ERROR) { HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "TransferDataChannel", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "TransferDataChannel", "ERROR_CODE", ret); SEN_HILOGE("failed, ret : %{public}d", ret); } sensorBasicDataChannel->CloseSendFd(); @@ -242,7 +242,7 @@ ErrCode SensorServiceProxy::DestroySensorChannel(sptr sensorClien int32_t ret = remote->SendRequest(ISensorService::DESTROY_SENSOR_CHANNEL, data, reply, option); if (ret != NO_ERROR) { HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "DestroySensorChannel", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "DestroySensorChannel", "ERROR_CODE", ret); SEN_HILOGE("failed, ret : %{public}d", ret); } return static_cast(ret); diff --git a/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp index 1161f5f1..4cdf2604 100644 --- a/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp @@ -60,7 +60,7 @@ int32_t HdiConnection::ConnectHdi() std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS)); } HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_HDF_SERVICE_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "ConnectHdi", "ERROR_CODE", CONNECT_SENSOR_HDF_ERR); + HiSysEvent::EventType::FAULT, "PKG_NAME", "ConnectHdi", "ERROR_CODE", CONNECT_SENSOR_HDF_ERR); SEN_HILOGE("connect v1_0 hdi failed"); return ERR_NO_INIT; } @@ -73,7 +73,7 @@ int32_t HdiConnection::GetSensorList(std::vector& sensorList) int32_t ret = sensorInterface_->GetAllSensorInfo(sensorInfos); if (ret != 0) { HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_HDF_SERVICE_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "GetSensorList", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "GetSensorList", "ERROR_CODE", ret); SEN_HILOGE("get sensor list failed"); return ret; } @@ -99,7 +99,7 @@ int32_t HdiConnection::EnableSensor(int32_t sensorId) int32_t ret = sensorInterface_->Enable(sensorId); if (ret != 0) { HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_HDF_SERVICE_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "EnableSensor", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "EnableSensor", "ERROR_CODE", ret); SEN_HILOGE("connect v1_0 hdi failed"); return ret; } @@ -113,7 +113,7 @@ int32_t HdiConnection::DisableSensor(int32_t sensorId) int32_t ret = sensorInterface_->Disable(sensorId); if (ret != 0) { HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_HDF_SERVICE_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "DisableSensor", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "DisableSensor", "ERROR_CODE", ret); SEN_HILOGE("Disable is failed"); return ret; } @@ -127,7 +127,7 @@ int32_t HdiConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int6 int32_t ret = sensorInterface_->SetBatch(sensorId, samplingInterval, reportInterval); if (ret != 0) { HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_HDF_SERVICE_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "SetBatch", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "SetBatch", "ERROR_CODE", ret); SEN_HILOGE("SetBatch is failed"); return ret; } @@ -142,7 +142,7 @@ int32_t HdiConnection::SetMode(int32_t sensorId, int32_t mode) int32_t ret = sensorInterface_->SetMode(sensorId, mode); if (ret != 0) { HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_HDF_SERVICE_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "SetMode", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "SetMode", "ERROR_CODE", ret); SEN_HILOGE("SetMode is failed"); return ret; } @@ -156,7 +156,7 @@ int32_t HdiConnection::SetOption(int32_t sensorId, int32_t option) int32_t ret = sensorInterface_->SetOption(sensorId, option); if (ret != 0) { HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_HDF_SERVICE_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "SetOption", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "SetOption", "ERROR_CODE", ret); SEN_HILOGE("SetOption is failed"); return ret; } @@ -171,7 +171,7 @@ int32_t HdiConnection::RegisteDataReport(ZReportDataCb cb, sptrRegister(0, eventCallback_); if (ret != 0) { HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_HDF_SERVICE_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "RegisteDataReport", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "RegisteDataReport", "ERROR_CODE", ret); SEN_HILOGE("Register is failed"); return ret; } @@ -187,7 +187,7 @@ int32_t HdiConnection::DestroyHdiConnection() int32_t ret = sensorInterface_->Unregister(0, eventCallback_); if (ret != 0) { HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_HDF_SERVICE_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "DestroyHdiConnection", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "DestroyHdiConnection", "ERROR_CODE", ret); SEN_HILOGE("Unregister is failed"); return ret; } diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index d88d4911..61ec704a 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -214,7 +214,7 @@ bool SensorDataProcesser::CheckSendDataPermission(sptr c int32_t ret = permissionUtil.CheckSensorPermission(appThreadInfo.callerToken, sensorId); if (ret != PERMISSION_GRANTED) { HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_VERIFY_ACCESS_TOKEN_FAIL", - HiSysEvent::EventType::SECURITY, "FUNC_NAME", "CheckSendDataPermission", "ERROR_CODE", ret); + HiSysEvent::EventType::SECURITY, "PKG_NAME", "CheckSendDataPermission", "ERROR_CODE", ret); SEN_HILOGE("sensorId: %{public}u grant failed, result: %{public}d", sensorId, ret); return false; } diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 8185c850..39954600 100644 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -82,7 +82,7 @@ ErrCode SensorServiceStub::SensorEnableInner(MessageParcel &data, MessageParcel int32_t ret = permissionUtil.CheckSensorPermission(this->GetCallingTokenID(), sensorId); if (ret != PERMISSION_GRANTED) { HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_VERIFY_ACCESS_TOKEN_FAIL", - HiSysEvent::EventType::SECURITY, "FUNC_NAME", "SensorEnableInner", "ERROR_CODE", ret); + HiSysEvent::EventType::SECURITY, "PKG_NAME", "SensorEnableInner", "ERROR_CODE", ret); SEN_HILOGE("sensorId: %{public}u grant failed, result: %{public}d", sensorId, ret); return ERR_PERMISSION_DENIED; } @@ -97,7 +97,7 @@ ErrCode SensorServiceStub::SensorDisableInner(MessageParcel &data, MessageParcel int32_t ret = permissionUtil.CheckSensorPermission(this->GetCallingTokenID(), sensorId); if (ret != PERMISSION_GRANTED) { HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_VERIFY_ACCESS_TOKEN_FAIL", - HiSysEvent::EventType::SECURITY, "FUNC_NAME", "SensorDisableInner", "ERROR_CODE", ret); + HiSysEvent::EventType::SECURITY, "PKG_NAME", "SensorDisableInner", "ERROR_CODE", ret); SEN_HILOGE("sensorId: %{public}u grant failed, result: %{public}d", sensorId, ret); return ERR_PERMISSION_DENIED; } @@ -112,7 +112,7 @@ ErrCode SensorServiceStub::GetSensorStateInner(MessageParcel &data, MessageParce int32_t ret = permissionUtil.CheckSensorPermission(this->GetCallingTokenID(), sensorId); if (ret != PERMISSION_GRANTED) { HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_VERIFY_ACCESS_TOKEN_FAIL", - HiSysEvent::EventType::SECURITY, "FUNC_NAME", "GetSensorStateInner", "ERROR_CODE", ret); + HiSysEvent::EventType::SECURITY, "PKG_NAME", "GetSensorStateInner", "ERROR_CODE", ret); SEN_HILOGE("sensorId: %{public}u grant failed, result: %{public}d", sensorId, ret); return ERR_PERMISSION_DENIED; } @@ -127,7 +127,7 @@ ErrCode SensorServiceStub::RunCommandInner(MessageParcel &data, MessageParcel &r int32_t ret = permissionUtil.CheckSensorPermission(this->GetCallingTokenID(), sensorId); if (ret != PERMISSION_GRANTED) { HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_VERIFY_ACCESS_TOKEN_FAIL", - HiSysEvent::EventType::SECURITY, "FUNC_NAME", "RunCommandInner", "ERROR_CODE", ret); + HiSysEvent::EventType::SECURITY, "PKG_NAME", "RunCommandInner", "ERROR_CODE", ret); SEN_HILOGE("sensorId: %{public}u grant failed, result: %{public}d", sensorId, ret); return ERR_PERMISSION_DENIED; } diff --git a/utils/src/sensor_basic_data_channel.cpp b/utils/src/sensor_basic_data_channel.cpp index 25829ba1..5dcb889a 100755 --- a/utils/src/sensor_basic_data_channel.cpp +++ b/utils/src/sensor_basic_data_channel.cpp @@ -48,7 +48,7 @@ int32_t SensorBasicDataChannel::CreateSensorBasicChannel() int32_t socketPair[SOCKET_PAIR_SIZE] = { 0 }; if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, socketPair) != 0) { HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_DATA_CHANNEL_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "CreateSensorBasicChannel", "ERROR_CODE", errno); + HiSysEvent::EventType::FAULT, "PKG_NAME", "CreateSensorBasicChannel", "ERROR_CODE", errno); SEN_HILOGE("create socketpair failed"); sendFd_ = -1; receiveFd_ = -1; @@ -158,7 +158,7 @@ int32_t SensorBasicDataChannel::SendData(const void *vaddr, size_t size) } while (errno == EINTR); if (length < 0) { HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_DATA_CHANNEL_EXCEPTION", - HiSysEvent::EventType::FAULT, "FUNC_NAME", "SendData", "ERROR_CODE", errno); + HiSysEvent::EventType::FAULT, "PKG_NAME", "SendData", "ERROR_CODE", errno); SEN_HILOGE("send fail : %{public}d, length = %{public}d", errno, (int32_t)length); return SENSOR_CHANNEL_SEND_DATA_ERR; } -- Gitee From d63423582fcb17a60108e4ed911239885934551f Mon Sep 17 00:00:00 2001 From: cff-gite Date: Mon, 18 Jul 2022 09:34:02 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9sensor=E6=89=93?= =?UTF-8?q?=E7=82=B9yaml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cff-gite --- hisysevent.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hisysevent.yaml b/hisysevent.yaml index da11daa1..dba9b2a6 100644 --- a/hisysevent.yaml +++ b/hisysevent.yaml @@ -27,25 +27,25 @@ DISABLE_SENSOR: SENSOR_DATA_CHANNEL_EXCEPTION: __BASE: {type: FAULT, level: MINOR, desc: sensor data channel exception} - PKG_NAME: {type: STRING, desc: function name} + PKG_NAME: {type: STRING, desc: package name} ERROR_CODE: {type: INT32, desc: error code} SENSOR_VERIFY_ACCESS_TOKEN_FAIL: __BASE: {type: SECURITY, level: MINOR, desc: sensor verify access token fail} - PKG_NAME: {type: STRING, desc: function name} + PKG_NAME: {type: STRING, desc: package name} ERROR_CODE: {type: INT32, desc: error code} SENSOR_SERVICE_IPC_EXCEPTION: __BASE: {type: FAULT, level: MINOR, desc: sensor service ipc exception} - PKG_NAME: {type: STRING, desc: function name} + PKG_NAME: {type: STRING, desc: package name} ERROR_CODE: {type: INT32, desc: error code} SENSOR_HDF_SERVICE_EXCEPTION: __BASE: {type: FAULT, level: MINOR, desc: sensor hdf service exception} - PKG_NAME: {type: STRING, desc: function name} + PKG_NAME: {type: STRING, desc: package name} ERROR_CODE: {type: INT32, desc: error code} SENSOR_SERVICE_EXCEPTION: __BASE: {type: FAULT, level: MINOR, desc: sensor service exception} - PKG_NAME: {type: STRING, desc: function name} + PKG_NAME: {type: STRING, desc: package name} ERROR_CODE: {type: INT32, desc: error code} \ No newline at end of file -- Gitee From 41826d8d2e6e3b8151780b0bcec94195cb06c6fc Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Mon, 18 Jul 2022 02:53:00 +0000 Subject: [PATCH 06/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuzhihuitmac Change-Id: Ief580fb537a484b8b77c368fa5dea52c791de1e9 --- interfaces/native/test/unittest/sensor_agent_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/native/test/unittest/sensor_agent_test.cpp b/interfaces/native/test/unittest/sensor_agent_test.cpp index 66c262e8..e84cc70f 100755 --- a/interfaces/native/test/unittest/sensor_agent_test.cpp +++ b/interfaces/native/test/unittest/sensor_agent_test.cpp @@ -53,7 +53,7 @@ void SensorAgentTest::TearDown() void SensorDataCallbackImpl(SensorEvent *event) { if (event == nullptr) { - SEN_HILOGE("SensorEvent is null"); + SEN_HILOGE("SensorEvent is null"); return; } float *sensorData = (float *)event[0].data; -- Gitee From b2ff7433d914a7a236f937f6c682c231017f4a91 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Wed, 20 Jul 2022 12:04:49 +0800 Subject: [PATCH 07/10] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- .../sensor/include/my_file_descriptor_listener.h | 9 +-------- .../native/sensor/include/sensor_data_channel.h | 5 ++--- .../sensor/src/my_file_descriptor_listener.cpp | 3 +++ frameworks/native/sensor/src/sensor_client_stub.cpp | 2 +- frameworks/native/sensor/src/sensor_data_channel.cpp | 12 +----------- interfaces/native/include/geomagnetic_field.h | 9 +-------- interfaces/native/src/geomagnetic_field.cpp | 3 +++ interfaces/native/src/sensor_algorithm.cpp | 3 ++- interfaces/plugin/src/sensor_js.cpp | 4 +--- interfaces/plugin/src/sensor_system_js.cpp | 3 ++- .../adapter/include/sensor_event_callback.h | 1 + .../adapter/src/compatible_connection.cpp | 1 + .../adapter/src/sensor_event_callback.cpp | 2 ++ .../hdi_connection/hardware/src/hdi_service_impl.cpp | 4 +++- services/sensor/include/fifo_cache_data.h | 3 +-- utils/include/permission_util.h | 2 +- utils/include/report_data_callback.h | 3 --- utils/src/permission_util.cpp | 4 ++-- utils/src/report_data_callback.cpp | 6 ------ 19 files changed, 28 insertions(+), 51 deletions(-) mode change 100755 => 100644 frameworks/native/sensor/include/my_file_descriptor_listener.h mode change 100755 => 100644 frameworks/native/sensor/include/sensor_data_channel.h diff --git a/frameworks/native/sensor/include/my_file_descriptor_listener.h b/frameworks/native/sensor/include/my_file_descriptor_listener.h old mode 100755 new mode 100644 index 36b35e93..65c78057 --- a/frameworks/native/sensor/include/my_file_descriptor_listener.h +++ b/frameworks/native/sensor/include/my_file_descriptor_listener.h @@ -16,16 +16,9 @@ #ifndef MY_FILE_DESCRIPTOR_LISTENER_H #define MY_FILE_DESCRIPTOR_LISTENER_H -#include +#include -#include -#include - -#include "event_handler.h" -#include "event_runner.h" #include "file_descriptor_listener.h" - -#include "sensor_agent_type.h" #include "sensor_data_channel.h" namespace OHOS { diff --git a/frameworks/native/sensor/include/sensor_data_channel.h b/frameworks/native/sensor/include/sensor_data_channel.h old mode 100755 new mode 100644 index 0e767b1a..e3b3a15a --- a/frameworks/native/sensor/include/sensor_data_channel.h +++ b/frameworks/native/sensor/include/sensor_data_channel.h @@ -17,11 +17,10 @@ #define SENSOR_DATA_CHANNEL_H #include -#include +#include -#include "sensor_basic_data_channel.h" -#include "sensor_agent_type.h" #include "my_event_handler.h" +#include "sensor_basic_data_channel.h" namespace OHOS { namespace Sensors { diff --git a/frameworks/native/sensor/src/my_file_descriptor_listener.cpp b/frameworks/native/sensor/src/my_file_descriptor_listener.cpp index 46855753..15627d1a 100644 --- a/frameworks/native/sensor/src/my_file_descriptor_listener.cpp +++ b/frameworks/native/sensor/src/my_file_descriptor_listener.cpp @@ -14,7 +14,10 @@ */ #include "my_file_descriptor_listener.h" +#include "sensor_agent_type.h" +#include "sensor_basic_data_channel.h" #include "sensors_errors.h" +#include "sys/socket.h" namespace OHOS { namespace Sensors { diff --git a/frameworks/native/sensor/src/sensor_client_stub.cpp b/frameworks/native/sensor/src/sensor_client_stub.cpp index 8cbdd9f9..ad45668b 100644 --- a/frameworks/native/sensor/src/sensor_client_stub.cpp +++ b/frameworks/native/sensor/src/sensor_client_stub.cpp @@ -16,7 +16,7 @@ #include "sensor_client_stub.h" #include "message_parcel.h" -#include "sensors_errors.h" +#include "sensor_log.h" namespace OHOS { namespace Sensors { diff --git a/frameworks/native/sensor/src/sensor_data_channel.cpp b/frameworks/native/sensor/src/sensor_data_channel.cpp index c6208281..0cfa02f0 100644 --- a/frameworks/native/sensor/src/sensor_data_channel.cpp +++ b/frameworks/native/sensor/src/sensor_data_channel.cpp @@ -15,19 +15,9 @@ #include "sensor_data_channel.h" -#include -#include - -#include -#include - +#include "errors.h" #include "my_file_descriptor_listener.h" #include "sensors_errors.h" -#include "string_ex.h" - -#ifndef O_NONBLOCK -# define O_NONBLOCK 04000 -#endif namespace OHOS { namespace Sensors { diff --git a/interfaces/native/include/geomagnetic_field.h b/interfaces/native/include/geomagnetic_field.h index 28a20a72..1c3dd4c1 100644 --- a/interfaces/native/include/geomagnetic_field.h +++ b/interfaces/native/include/geomagnetic_field.h @@ -16,16 +16,9 @@ #ifndef GEOMAGNETIC_FIELD_H #define GEOMAGNETIC_FIELD_H -#include -#include -#include -#include +#include #include -#include -#include -#include - class GeomagneticField { public: GeomagneticField(float latitude, float longitude, float altitude, int64_t timeMillis); diff --git a/interfaces/native/src/geomagnetic_field.cpp b/interfaces/native/src/geomagnetic_field.cpp index 38c2e835..02637a32 100644 --- a/interfaces/native/src/geomagnetic_field.cpp +++ b/interfaces/native/src/geomagnetic_field.cpp @@ -15,6 +15,9 @@ #include "geomagnetic_field.h" +#include +#include + using namespace std; namespace { constexpr float EARTH_MAJOR_AXIS_RADIUS = 6378.137f; diff --git a/interfaces/native/src/sensor_algorithm.cpp b/interfaces/native/src/sensor_algorithm.cpp index 53546d5d..42d10b57 100644 --- a/interfaces/native/src/sensor_algorithm.cpp +++ b/interfaces/native/src/sensor_algorithm.cpp @@ -15,7 +15,8 @@ #include "sensor_algorithm.h" #include -#include +#include + #include "sensors_errors.h" using OHOS::HiviewDFX::HiLog; diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 12c22108..a8720212 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -15,14 +15,12 @@ #include "sensor_js.h" #include -#include #include #include #include +#include #include -#include #include -#include #include #include "geomagnetic_field.h" diff --git a/interfaces/plugin/src/sensor_system_js.cpp b/interfaces/plugin/src/sensor_system_js.cpp index 0cb17ae1..fd479933 100644 --- a/interfaces/plugin/src/sensor_system_js.cpp +++ b/interfaces/plugin/src/sensor_system_js.cpp @@ -13,8 +13,9 @@ * limitations under the License. */ #include "sensor_system_js.h" + +#include "async_callback_info.h" #include "sensor_js.h" -#include "sensor_napi_utils.h" namespace OHOS { namespace Sensors { diff --git a/services/sensor/hdi_connection/adapter/include/sensor_event_callback.h b/services/sensor/hdi_connection/adapter/include/sensor_event_callback.h index f2117c32..5a772026 100644 --- a/services/sensor/hdi_connection/adapter/include/sensor_event_callback.h +++ b/services/sensor/hdi_connection/adapter/include/sensor_event_callback.h @@ -17,6 +17,7 @@ #define SENSOR_EVENT_CALLBACK_H #include "v1_0/isensor_callback.h" +#include "v1_0/sensor_types.h" using OHOS::HDI::Sensor::V1_0::HdfSensorEvents; using OHOS::HDI::Sensor::V1_0::ISensorCallback; diff --git a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp index f4bf6f62..292a2cc3 100644 --- a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp @@ -15,6 +15,7 @@ #include "compatible_connection.h" #include + #include "securec.h" #include "sensors_errors.h" diff --git a/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp index 531b49cb..7359d1f5 100644 --- a/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp +++ b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp @@ -14,7 +14,9 @@ */ #include "sensor_event_callback.h" + #include "hdi_connection.h" +#include "sensor_agent_type.h" #include "sensors_errors.h" namespace OHOS { 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 7ebb018d..0abc4172 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -14,8 +14,10 @@ */ #include "hdi_service_impl.h" +#include +#include + #include "sensors_errors.h" -#include "unistd.h" namespace OHOS { namespace Sensors { diff --git a/services/sensor/include/fifo_cache_data.h b/services/sensor/include/fifo_cache_data.h index 22d25798..c98a1c99 100644 --- a/services/sensor/include/fifo_cache_data.h +++ b/services/sensor/include/fifo_cache_data.h @@ -18,9 +18,8 @@ #include -#include "refbase.h" #include "nocopyable.h" - +#include "refbase.h" #include "sensor_agent_type.h" #include "sensor_basic_data_channel.h" diff --git a/utils/include/permission_util.h b/utils/include/permission_util.h index 648bc187..df421227 100644 --- a/utils/include/permission_util.h +++ b/utils/include/permission_util.h @@ -19,7 +19,7 @@ #include #include -#include "accesstoken_kit.h" +#include "access_token.h" #include "singleton.h" namespace OHOS { diff --git a/utils/include/report_data_callback.h b/utils/include/report_data_callback.h index 1bd9bb70..356b4d4c 100644 --- a/utils/include/report_data_callback.h +++ b/utils/include/report_data_callback.h @@ -16,10 +16,7 @@ #ifndef REPORT_DATA_CALLBACK_H #define REPORT_DATA_CALLBACK_H -#include - #include "refbase.h" - #include "sensor_agent_type.h" namespace OHOS { diff --git a/utils/src/permission_util.cpp b/utils/src/permission_util.cpp index 3ad352a8..ff26d648 100644 --- a/utils/src/permission_util.cpp +++ b/utils/src/permission_util.cpp @@ -15,10 +15,10 @@ #include "permission_util.h" -#include +#include "accesstoken_kit.h" #include "privacy_kit.h" #include "sensor_agent_type.h" -#include "sensors_errors.h" +#include "sensor_log.h" namespace OHOS { namespace Sensors { diff --git a/utils/src/report_data_callback.cpp b/utils/src/report_data_callback.cpp index 5b1e0151..3e5cfc4f 100644 --- a/utils/src/report_data_callback.cpp +++ b/utils/src/report_data_callback.cpp @@ -14,12 +14,6 @@ */ #include "report_data_callback.h" - -#include -#include - -#include "errors.h" -#include "securec.h" #include "sensors_errors.h" namespace OHOS { -- Gitee From 4fbe76814a0242e0d0dc814f01a2b6b0f7214bc4 Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Fri, 22 Jul 2022 08:18:43 +0000 Subject: [PATCH 08/10] add test case about ut Signed-off-by: wuzhihuitmac Change-Id: I00d043ee282d9014b92ca1ae4fbcb3f34dd22100 --- interfaces/native/test/BUILD.gn | 6 +- .../test/unittest/sensor_agent_test.cpp | 11 + test/unittest/common/include/system_info.h | 70 +++++ test/unittest/common/src/system_info.cpp | 252 ++++++++++++++++++ utils/include/sensors_errors.h | 2 + 5 files changed, 340 insertions(+), 1 deletion(-) create mode 100755 test/unittest/common/include/system_info.h create mode 100755 test/unittest/common/src/system_info.cpp diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index 363c7398..49354a5a 100644 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -20,12 +20,16 @@ module_output_path = "sensors/sensor/interfaces" ohos_unittest("SensorAgentTest") { module_out_path = module_output_path - sources = [ "unittest/sensor_agent_test.cpp" ] + sources = [ + "unittest/sensor_agent_test.cpp", + "$SUBSYSTEM_DIR/sensor/test/unittest/common/src/system_info.cpp", + ] include_dirs = [ "//utils/native/base/include", "$SUBSYSTEM_DIR/sensor/utils/include", "$SUBSYSTEM_DIR/sensor/interfaces/native/include", + "$SUBSYSTEM_DIR/sensor/test/unittest/common/include", ] deps = [ diff --git a/interfaces/native/test/unittest/sensor_agent_test.cpp b/interfaces/native/test/unittest/sensor_agent_test.cpp index e84cc70f..b566370d 100755 --- a/interfaces/native/test/unittest/sensor_agent_test.cpp +++ b/interfaces/native/test/unittest/sensor_agent_test.cpp @@ -18,6 +18,7 @@ #include "sensor_agent.h" #include "sensors_errors.h" +#include "system_info.h" namespace OHOS { namespace Sensors { @@ -297,5 +298,15 @@ HWTEST_F(SensorAgentTest, SensorNativeApiTest_001, TestSize.Level1) ret = UnsubscribeSensor(sensorId, &user); ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); } + +HWTEST_F(SensorAgentTest, GetProcCpuUsageTest_001, TestSize.Level1) +{ + SEN_HILOGI("GetProcCpuUsageTest_001 in"); + SYSTEM_INFO::CpuInfo cpuInfo; + const std::string process_name = "sensors"; + auto usage = cpuInfo.GetProcCpuUsage(process_name); + SEN_HILOGD("The CPU usage of the %{public}s process is %{public}.2f", process_name.c_str(), usage); + ASSERT_TRUE(usage < SYSTEM_INFO::CPU_USAGE_LOAD && usage != SYSTEM_INFO::CPU_USAGE_UNKONW); +} } // namespace Sensors } // namespace OHOS diff --git a/test/unittest/common/include/system_info.h b/test/unittest/common/include/system_info.h new file mode 100755 index 00000000..a3ad5de7 --- /dev/null +++ b/test/unittest/common/include/system_info.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SYSTEM_INFO_H +#define SYSTEM_INFO_H + +#include + +namespace OHOS { +namespace Sensors { +namespace SYSTEM_INFO { +static constexpr double CPU_USAGE_UNKONW = -1.0; +static constexpr double CPU_USAGE_LOAD = 20.0; +static constexpr double CPU_USAGE_MAX = 100.0; +class SystemInfo { +public: + SystemInfo() = default; + virtual ~SystemInfo() = default; + virtual double GetSystemCpuUsage(); + virtual double GetProcCpuUsage(const std::string& process_name); +}; + +class CpuInfo : public SystemInfo { +public: + double GetSystemCpuUsage(); + double GetProcCpuUsage(const std::string& process_name); +private: + struct Total_Cpu_Occupy { + char name[20] { 0 }; + int32_t user { 0 }; + int32_t nice { 0 }; + int32_t system { 0 }; + int32_t idle { 0 }; + int32_t lowait { 0 }; + int32_t irq { 0 }; + int32_t softirq { 0 }; + int32_t steal { 0 }; + int32_t guest { 0 }; + int32_t guest_nice { 0 }; + }; + struct Proc_Cpu_Occupy { + int32_t utime { 0 }; + int32_t stime { 0 }; + int32_t cutime { 0 }; + int32_t cstime { 0 }; + }; + int32_t GetTaskPidFile(const std::string& process_name); + int32_t GetTaskPidCmd(const std::string& process_name, int32_t flag = 0, std::string user = ""); + int32_t GetProcOccupy(int32_t pid); + + int32_t GetSystemCpuStatInfo(Total_Cpu_Occupy& info); + int64_t GetSystemTotalOccupy(); + double GetCpuUsage(const Total_Cpu_Occupy& first, const Total_Cpu_Occupy& second); +}; +} // namespace SYSTEM_INFO +} // namespace Sensors +} // namespace OHOS +#endif diff --git a/test/unittest/common/src/system_info.cpp b/test/unittest/common/src/system_info.cpp new file mode 100755 index 00000000..0bfed23d --- /dev/null +++ b/test/unittest/common/src/system_info.cpp @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2022 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 "system_info.h" + +#include +#include + +// #include +#include +#include +#include +#include + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +namespace SYSTEM_INFO { +using namespace OHOS::HiviewDFX; +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SYSTEM_INFO" }; +constexpr int32_t LOCATION = 14; +constexpr int32_t TIME_WAIT_FOR_OP = 1000; +constexpr int32_t DEFAULT_PID = -1; +} // namespace + +inline double CHK_RATE(double rate) +{ + return (rate > CPU_USAGE_MAX ? CPU_USAGE_MAX : rate); +} + +int32_t CpuInfo::GetTaskPidFile(const std::string& process_name) +{ + int32_t pid = DEFAULT_PID; + static const std::string procPath = "/proc"; + DIR* dir = ::opendir(procPath.c_str()); + if (dir == nullptr) { + SEN_HILOGE("Failed to open path: %{public}s", procPath.c_str()); + return DEFAULT_PID; + } + struct dirent* pidFile; + while ((pidFile = ::readdir(dir)) != nullptr) { + if ((::strcmp(pidFile->d_name, ".") == 0) || (::strcmp(pidFile->d_name, "..") == 0)) { + continue; + } + if (pidFile->d_type != DT_DIR) { + continue; + } + const std::string path = procPath + "/" + pidFile->d_name + "/status"; + std::ifstream filePath(path); + if (!filePath.is_open()) { + continue; + } + std::string strLine; + std::getline(filePath, strLine); + if (strLine.empty()) { + filePath.close(); + continue; + } + if ((strLine.find(process_name)) == std::string::npos) { + filePath.close(); + continue; + } + while (std::getline(filePath, strLine)) { + if ((strLine.find("Pid")) != std::string::npos) { + (void)::sscanf(strLine.c_str(), "%*s%d", &pid); + break; + } + } + filePath.close(); + break; + } + ::closedir(dir); + + return pid; +} + +int32_t CpuInfo::GetTaskPidCmd(const std::string& process_name, int32_t flag, std::string user) +{ + std::string command; + if (flag) { + if (user.empty()) { + user = ::getlogin(); + } + command = "pgrep " + process_name + " -u " + user; + } else { + command = "pidof -s " + process_name; + } + ::FILE *fp = nullptr; + if ((fp = ::popen(command.c_str(), "r")) == nullptr) { + SEN_HILOGE("Failed to open, cmd:%{public}s", command.c_str()); + fp = nullptr; + return DEFAULT_PID; + } + char buf[100] = { 0 }; + if (::fgets(buf, sizeof(buf), fp) == nullptr) { + SEN_HILOGE("Failed to read content"); + ::pclose(fp); + fp = nullptr; + return DEFAULT_PID; + } + ::pclose(fp); + return ::atoi(buf); +} + +int32_t CpuInfo::GetProcOccupy(int32_t pid) +{ + Proc_Cpu_Occupy info; + static const std::string procPath = "/proc/" + std::to_string(pid) + "/stat"; + std::ifstream file(procPath); + if (!file.is_open()) { + SEN_HILOGE("Failed to open path: %{public}s", procPath.c_str()); + return OHOS::Sensors::ERROR; + } + + std::string strLine; + std::getline(file, strLine); + if (strLine.empty()) { + SEN_HILOGE("Failed to read content"); + file.close(); + return OHOS::Sensors::ERROR; + } + file.close(); + + int pos = 1; + std::istringstream ss(strLine); + while (ss >> strLine) { + pos++; + if (pos >= LOCATION) { + break; + } + } + ss >> info.utime >> info.stime >> info.cutime >> info.cstime; + return (info.utime + info.stime + info.cutime + info.cstime); +} + +double CpuInfo::GetCpuUsage(const Total_Cpu_Occupy& first, const Total_Cpu_Occupy& second) +{ + unsigned long cpuTime2 = static_cast(second.user + second.nice + second.system + + second.idle + second.lowait + second.irq + second.softirq); + unsigned long cpuTime1 = static_cast(first.user + first.nice + first.system + + first.idle + first.lowait + first.irq + first.softirq); + + double cpu_use = (second.user - first.user) * CPU_USAGE_MAX / (cpuTime2 - cpuTime1); + double cpu_sys = (second.system - first.system) * CPU_USAGE_MAX / (cpuTime2 - cpuTime1); + + return CHK_RATE(cpu_use + cpu_sys); +} + +int32_t CpuInfo::GetSystemCpuStatInfo(Total_Cpu_Occupy& info) +{ + std::ifstream statFile("/proc/stat"); + if (!statFile.is_open()) { + SEN_HILOGE("Failed to open config file"); + return FILE_OPEN_FAIL; + } + std::string strLine; + std::getline(statFile, strLine); + if (strLine.empty()) { + SEN_HILOGE("No valid content was read"); + statFile.close(); + return STREAM_BUF_READ_FAIL; + } + if ((strLine.find("cpu")) == std::string::npos) { + SEN_HILOGE("The keyword was not matched. Procedure"); + statFile.close(); + return OHOS::Sensors::ERROR; + } + std::istringstream ss(strLine); + ss >> info.name >> info.user >> info.nice >> info.system >> info.idle >> info.lowait \ + >> info.irq >> info.softirq >> info.steal >> info.guest >> info.guest_nice; + + statFile.close(); + return OHOS::Sensors::SUCCESS; +} + +double CpuInfo::GetSystemCpuUsage() +{ + Total_Cpu_Occupy first {}; + int32_t ret = GetSystemCpuStatInfo(first); + if (ret != OHOS::Sensors::SUCCESS) { + SEN_HILOGE("Failed to obtain CPU information, errcode:%{public}d", ret); + return CPU_USAGE_UNKONW; + } + std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP)); + Total_Cpu_Occupy second {}; + ret = GetSystemCpuStatInfo(second); + if (ret != OHOS::Sensors::SUCCESS) { + SEN_HILOGE("Failed to obtain CPU information, errcode:%{public}d", ret); + return CPU_USAGE_UNKONW; + } + + return GetCpuUsage(first, second); +} + +int64_t CpuInfo::GetSystemTotalOccupy() +{ + int ret = -1; + Total_Cpu_Occupy occupy {}; + if ((ret = GetSystemCpuStatInfo(occupy)) != OHOS::Sensors::SUCCESS) { + SEN_HILOGE("Failed to obtain CPU information, errcode:%{public}d", ret); + return OHOS::Sensors::ERROR; + } + return (occupy.user + occupy.nice + occupy.system + occupy.idle); +} + +double CpuInfo::GetProcCpuUsage(const std::string& process_name) +{ + int64_t totalTime1 = 0; + int64_t totalTime2 = 0; + int64_t procTime1 = 0; + int64_t procTime2 = 0; + int32_t pid = GetTaskPidFile(process_name); + + if ((totalTime1 = GetSystemTotalOccupy()) == OHOS::Sensors::ERROR) { + SEN_HILOGE("Failed to obtain CPU occupy"); + return CPU_USAGE_UNKONW; + } + if ((procTime1 = GetProcOccupy(pid)) == OHOS::Sensors::ERROR) { + SEN_HILOGE("Failed to obtain process CPU information"); + return CPU_USAGE_UNKONW; + } + + std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP)); + + if ((totalTime2 = GetSystemTotalOccupy()) == OHOS::Sensors::ERROR) { + SEN_HILOGE("Failed to obtain CPU occupy"); + return CPU_USAGE_UNKONW; + } + if ((procTime2 = GetProcOccupy(pid)) == OHOS::Sensors::ERROR) { + SEN_HILOGE("Failed to obtain process CPU information"); + return CPU_USAGE_UNKONW; + } + + return CHK_RATE(CPU_USAGE_MAX * (procTime2 - procTime1) / (totalTime2 - totalTime1)); +} +} // namespace SYSTEM_INFO +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/utils/include/sensors_errors.h b/utils/include/sensors_errors.h index 42ce32db..754fc661 100644 --- a/utils/include/sensors_errors.h +++ b/utils/include/sensors_errors.h @@ -38,6 +38,8 @@ enum { ERROR = -1, SUCCESS = 0, COMMON_ERR = COMMON_ERR_OFFSET, + FILE_OPEN_FAIL = COMMON_ERR_OFFSET + 1, + STREAM_BUF_READ_FAIL = FILE_OPEN_FAIL + 1, }; // Error code for device -- Gitee From 881cc839dc5bc4308ec65441098f155ff2e4c1bc Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Fri, 22 Jul 2022 08:28:39 +0000 Subject: [PATCH 09/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuzhihuitmac Change-Id: I9bb0bfb5cbae314bcad169a39f987193900d0581 --- test/unittest/common/src/system_info.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unittest/common/src/system_info.cpp b/test/unittest/common/src/system_info.cpp index 0bfed23d..2fc16923 100755 --- a/test/unittest/common/src/system_info.cpp +++ b/test/unittest/common/src/system_info.cpp @@ -18,7 +18,6 @@ #include #include -// #include #include #include #include -- Gitee From 06b1e338eadfd3ca4c9525310c33bc4ce9d11b67 Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Fri, 22 Jul 2022 08:38:26 +0000 Subject: [PATCH 10/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9gn=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuzhihuitmac Change-Id: I1a83cc18bc8678b23fb372818dcdc1874cc338f3 --- interfaces/native/test/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index 49354a5a..851ad3a3 100644 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -21,8 +21,8 @@ ohos_unittest("SensorAgentTest") { module_out_path = module_output_path sources = [ - "unittest/sensor_agent_test.cpp", "$SUBSYSTEM_DIR/sensor/test/unittest/common/src/system_info.cpp", + "unittest/sensor_agent_test.cpp", ] include_dirs = [ -- Gitee