diff --git a/bundle.json b/bundle.json index 298936c9e5248730bbe8c609a8b2b4e330130329..73605b172d4ad00d765c186cb18f10e61b8b07ee 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/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 36b35e931e183c623beb30fe72148bc3c211f5ea..65c780571e5333d733db16d92c6e9113359f1a76 --- 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 0e767b1afcd2f274801011aaf5fae7959e589d83..e3b3a15ae9a8e9e3cb79acda88e9d7de417ce9d4 --- 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 46855753e4e684c80b5133c2f28525e44d855a80..15627d1aac375a534093292a3c32d71c80c887ab 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 8cbdd9f9885197af11d20863d8886ddff4d79cb9..ad45668b4ec4b9acde47e9958c24a6b8fd8fa763 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 c620828188254a782060915423453cd48ee5e1a2..0cfa02f02895437e0d296799add9a10621ee331f 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/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index c6298e47a42e2409c84f459187ea451e620557b8..8568cfbb71beffafb0de8805430a0871764028b9 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 a0a17892421b970b3bae7df7638be42d6371b388..dce8e0b5159e8c49c2928d7c98e345c93aa0925e 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/hisysevent.yaml b/hisysevent.yaml index fce6515b0bbc0dbb3667afefdba19f073de5226a..dba9b2a62a7236486012d7cd51948a01f0fed728 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: package 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: package 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: package 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: package 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: package name} ERROR_CODE: {type: INT32, desc: error code} \ No newline at end of file diff --git a/interfaces/native/include/geomagnetic_field.h b/interfaces/native/include/geomagnetic_field.h index 28a20a7252283d97141ce4c0924a801b170fdec1..1c3dd4c136427e05a7eda15da657b51398a26448 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 38c2e835432ff4cbeacd7391682ab8971d51eeaa..02637a32b1bfd2f1216987e5ffde40877ab4df75 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 53546d5de85e88f3644728978d29af3c898d16f0..42d10b575e70530653b84bc2e3e8c552dc99df27 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/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index c0d402a8945f4ba591426550d70c03f6eab9925a..851ad3a3cb5a72c4ada4ec0251aa75af1aa4b046 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 = [ + "$SUBSYSTEM_DIR/sensor/test/unittest/common/src/system_info.cpp", + "unittest/sensor_agent_test.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 = [ @@ -37,8 +41,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 ac31dec790abb75d5d9466b3be4170ba3281aca7..b566370d78a350ba574883411b388b77e9696439 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 { @@ -25,7 +26,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 +54,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 +275,38 @@ 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); +} + +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/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 12c22108c93e3da1bc97ef76dec1cc416f20d4db..a8720212dbe3328bfd7432ecf8ef4e86bd245465 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 0cb17ae156b1360f09aa8aa9734d6f9a6d0b02e2..fd479933a1dec5e7bb36189dc264108215c759c5 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 f2117c3256669bc2db75b5e98dfe2041db4195bf..5a7720262841c9c2e2bd9ba073e1dd48a9147c6e 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 86273d687574cbc328b8fb08cc254cb31429946d..292a2cc3dba2db3da0310472a5353e5b40ebeebe 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" @@ -138,6 +139,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/hdi_connection/adapter/src/hdi_connection.cpp b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp index 1161f5f168ce0dccb49c4e8b124b99265ed96186..4cdf260448c0fdb35d062143548af1919c7033ae 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/hdi_connection/adapter/src/sensor_event_callback.cpp b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp index 531b49cbca73bdbe36d47ab77bda253ceeda2b13..7359d1f52bd19c8c69f08fdb730ed344321c9aa6 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 7ebb018d40bf167b71662a3544c430689df1e1b5..0abc417235bb09a1a643035b83a6ad75d2050e0b 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 22d257988d1414b3e42b14123bdfe17009c008ac..c98a1c99523cbe9659d2ac96cd29e7cdafd4e169 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/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index be50d8c78b9094e6397815e1acbb5e29c4a04db1..61ec704a561903ffc2ad745e1c5a1ff14cdb1691 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; } @@ -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; diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 8185c8507e10f051f566506ad5938c6a06b738ce..39954600030b02f6e8a7e907d155f8234b248fc4 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/test/unittest/common/include/system_info.h b/test/unittest/common/include/system_info.h new file mode 100755 index 0000000000000000000000000000000000000000..a3ad5de74d5db04960fc44b3d1d99fb9a677c126 --- /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 0000000000000000000000000000000000000000..2fc16923641bcbbdb91d54b95e7cd0b80dad3d1d --- /dev/null +++ b/test/unittest/common/src/system_info.cpp @@ -0,0 +1,251 @@ +/* + * 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 "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/permission_util.h b/utils/include/permission_util.h index 648bc18746be16f8885d93e5e270cfc8715983c1..df4212272f6c25ded554f4780523a133d8bb8978 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 1bd9bb706ac461be6cd44dcabbfc2969618cbe75..356b4d4ce8e88cbab3d90613f57870c2a165abda 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/include/sensors_errors.h b/utils/include/sensors_errors.h index 42ce32db752550336c646d55b980926de599c072..754fc66182649d4182dc8ebfb0050e8940e877af 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 diff --git a/utils/src/permission_util.cpp b/utils/src/permission_util.cpp index 3ad352a8f2c84887b28301287bb7755cddb127b0..ff26d6484783fba2710890e87773a3547dccbd60 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 5b1e015103215a39ec7c5519cf8bf7e689fcd069..3e5cfc4f23b0a0ad2446ec6b3af8929d060d0c44 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 { diff --git a/utils/src/sensor_basic_data_channel.cpp b/utils/src/sensor_basic_data_channel.cpp index 25829ba1bd8be438b1c59bcd9f75672f01c78799..5dcb889aee472c8b25746aeb816053df1c348df5 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; }