diff --git a/frameworks/native/BUILD.gn b/frameworks/native/BUILD.gn index 74a921a99eb8732b54c568e69f64f24f839e5093..74f6b1bb70e9bff97629dae6d8917c0b781de764 100644 --- a/frameworks/native/BUILD.gn +++ b/frameworks/native/BUILD.gn @@ -54,6 +54,7 @@ ohos_shared_library("libsensor_client") { "src/sensor_event_handler.cpp", "src/sensor_file_descriptor_listener.cpp", "src/sensor_service_client.cpp", + "src/sensor_service_load.cpp", ] sources += filter_include(output_values, [ "*_proxy.cpp" ]) diff --git a/frameworks/native/include/sensor_service_load.h b/frameworks/native/include/sensor_service_load.h new file mode 100644 index 0000000000000000000000000000000000000000..4d1204449edbd799bc844dc5566679f9157564f7 --- /dev/null +++ b/frameworks/native/include/sensor_service_load.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 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 SENSOR_SERVICE_LOAD_H +#define SENSOR_SERVICE_LOAD_H + +#include "iremote_object.h" +#include "singleton.h" +#include "system_ability_load_callback_stub.h" + +namespace OHOS { +namespace Sensors { +class SensorLoadCallback : public SystemAbilityLoadCallbackStub { +public: + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr &remoteObject) override; + void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; +}; + +class SensorServiceLoad : public Singleton { +public: + ~SensorServiceLoad() override; + int32_t LoadSensorService(void); + void SetLoadFinish(void); +private: + std::atomic isSensorServiceLoading_ = false; +}; +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_SERVICE_LOAD_H diff --git a/frameworks/native/src/sensor_service_client.cpp b/frameworks/native/src/sensor_service_client.cpp index ec360296699efac15685ff69d96a0c8a6cb49e8b..4f2f29cad9e9137e8ff2a882289ffb4ec2483d9c 100644 --- a/frameworks/native/src/sensor_service_client.cpp +++ b/frameworks/native/src/sensor_service_client.cpp @@ -23,6 +23,7 @@ #include "hitrace_meter.h" #endif // HIVIEWDFX_HITRACE_ENABLE #include "sensor_agent_proxy.h" +#include "sensor_service_load.h" #include "system_ability_definition.h" #undef LOG_TAG @@ -47,6 +48,8 @@ extern "C" { #endif // OHOS_BUILD_ENABLE_RUST } // namespace +#define SEN_SERVICE_LOAD SensorServiceLoad::GetInstance() + SensorServiceClient::~SensorServiceClient() { if (sensorServer_ != nullptr && serviceDeathObserver_ != nullptr) { @@ -98,6 +101,9 @@ int32_t SensorServiceClient::InitServiceClient() } return ERR_OK; } + if (SEN_SERVICE_LOAD.LoadSensorService() != ERR_OK) { + SEN_HILOGE("LoadSensorService failed"); + } SEN_HILOGW("Get service failed"); #ifdef HIVIEWDFX_HISYSEVENT_ENABLE HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "SERVICE_EXCEPTION", diff --git a/frameworks/native/src/sensor_service_load.cpp b/frameworks/native/src/sensor_service_load.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bed68fcd28d90a99476a27cb6c33582ae92b40e --- /dev/null +++ b/frameworks/native/src/sensor_service_load.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2025 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 "sensor_service_load.h" + +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "sensor_errors.h" +#include "sensor_log.h" +#include "system_ability_definition.h" + +#undef LOG_TAG +#define LOG_TAG "SensorServiceLoad" + +namespace OHOS { +namespace Sensors { + +#define SEN_SERVICE_LOAD SensorServiceLoad::GetInstance() + +SensorServiceLoad::~SensorServiceLoad() {} + +int32_t SensorServiceLoad::LoadSensorService(void) +{ + if (isSensorServiceLoading_) { + SEN_HILOGI("Sensor service is loading."); + return ERR_OK; + } + SEN_HILOGI("start"); + isSensorServiceLoading_ = true; + sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + isSensorServiceLoading_ = false; + SEN_HILOGE("Failed to get system ability mgr"); + return SENSOR_NATIVE_SAM_ERR; + } + sptr sensorLoadCallback(new SensorLoadCallback()); + int32_t ret = samgr->LoadSystemAbility(SENSOR_SERVICE_ABILITY_ID, sensorLoadCallback); + if (ret != ERR_OK) { + isSensorServiceLoading_ = false; + SEN_HILOGE("Failed to load sensor service, ret code:%{public}d", ret); + return ret; + } + return ERR_OK; +} + +void SensorServiceLoad::SetLoadFinish(void) +{ + isSensorServiceLoading_ = false; +} + +void SensorLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const sptr &remoteObject) +{ + SEN_HILOGI("Load sensor service success remoteObject result:%{public}s", + (remoteObject != nullptr) ? "true" : "false"); + SEN_SERVICE_LOAD.SetLoadFinish(); + if (remoteObject == nullptr) { + SEN_HILOGE("remoteObject is nullptr"); + } +} + +void SensorLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + SEN_HILOGE("Load sensor service failed."); + SEN_SERVICE_LOAD.SetLoadFinish(); +} +} // namespace Sensors +} // namespace OHOS diff --git a/services/include/client_info.h b/services/include/client_info.h index d93d2f95e6626af3e8cb8fa2688d7fb26ef60978..95c7457ba6a81c95b0d5ccfeef8623c212cbb6dc 100644 --- a/services/include/client_info.h +++ b/services/include/client_info.h @@ -83,6 +83,7 @@ public: void SaveSensorClient(const sptr &sensorClient); void DestroySensorClient(const sptr &sensorClient); void SendMsgToClient(SensorPlugData info); + bool IsClientSubscribe(); private: DISALLOW_COPY_AND_MOVE(ClientInfo); diff --git a/services/include/sensor_service.h b/services/include/sensor_service.h index 6b3223c523218870ab266a7fed0d631d9ace8e07..9cb8a7b361b376b75fe92148c08268470d6c8110 100644 --- a/services/include/sensor_service.h +++ b/services/include/sensor_service.h @@ -99,6 +99,7 @@ private: bool IsSystemServiceCalling(); bool IsSystemCalling(); bool IsNeedLoadMotionLib(); + void SetCritical(); SensorServiceState state_; std::mutex serviceLock_; std::mutex sensorsMutex_; @@ -124,6 +125,7 @@ private: ErrCode SaveSubscriber(const SensorDescription &sensorDesc, int64_t samplingPeriodNs, int64_t maxReportDelayNs); std::atomic_bool isReportActiveInfo_ = false; static std::atomic_bool isAccessTokenServiceActive_; + static std::atomic_bool isCritical_; }; #define POWER_POLICY SensorPowerPolicy::GetInstance() diff --git a/services/src/client_info.cpp b/services/src/client_info.cpp index 9dc3e8fffe19b7a5c23ac27479484ffafa8d651c..921889ec8306fec0be3a78e226f97545d2b10b7a 100644 --- a/services/src/client_info.cpp +++ b/services/src/client_info.cpp @@ -863,5 +863,10 @@ void ClientInfo::SendMsgToClient(SensorPlugData info) } } } + +bool ClientInfo::IsClientSubscribe() +{ + return !clientMap_.empty(); +} } // namespace Sensors } // namespace OHOS diff --git a/services/src/sensor_service.cpp b/services/src/sensor_service.cpp index 0c9ebd77186ac9e5fe222d84c8edcd11fb299ce0..d6d4dc27efce6b4a3ae8355c83d9eecfc9969552 100644 --- a/services/src/sensor_service.cpp +++ b/services/src/sensor_service.cpp @@ -55,6 +55,7 @@ const std::string DEFAULTS_FOLD_TYPE = "0,0,0,0"; } // namespace std::atomic_bool SensorService::isAccessTokenServiceActive_ = false; +std::atomic_bool SensorService::isCritical_ = false; SensorService::SensorService() : SystemAbility(SENSOR_SERVICE_ABILITY_ID, true), state_(SensorServiceState::STATE_STOPPED) @@ -109,6 +110,7 @@ void SensorService::OnAddSystemAbility(int32_t systemAbilityId, const std::strin if (systemAbilityId == MEMORY_MANAGER_SA_ID) { Memory::MemMgrClient::GetInstance().NotifyProcessStatus(getpid(), PROCESS_TYPE_SA, PROCESS_STATUS_STARTED, SENSOR_SERVICE_ABILITY_ID); + SetCritical(); } #endif // MEMMGR_ENABLE #ifdef ACCESS_TOKEN_ENABLE @@ -450,6 +452,7 @@ ErrCode SensorService::EnableSensor(const SensorDescriptionIPC &SensorDescriptio return ENABLE_SENSOR_ERR; } #endif // HDF_DRIVERS_INTERFACE_SENSOR + SetCritical(); if ((!g_isRegister) && (RegisterPermCallback(sensorDesc.sensorType))) { g_isRegister = true; } @@ -509,7 +512,17 @@ ErrCode SensorService::DisableSensor(const SensorDescription &sensorDesc, int32_ int32_t uid = clientInfo_.GetUidByPid(pid); clientInfo_.DestroyCmd(uid); clientInfo_.ClearDataQueue(sensorDesc); - return sensorManager_.AfterDisableSensor(sensorDesc); + int32_t ret = sensorManager_.AfterDisableSensor(sensorDesc); +#ifdef MEMMGR_ENABLE + if (!clientInfo_.IsClientSubscribe() && isCritical_) { + if (Memory::MemMgrClient::GetInstance().SetCritical(getpid(), false, SENSOR_SERVICE_ABILITY_ID) != ERR_OK) { + SEN_HILOGE("setCritical failed"); + return ret; + } + isCritical_ = false; + } +#endif // MEMMGR_ENABLE + return ret; } ErrCode SensorService::DisableSensor(const SensorDescriptionIPC &SensorDescriptionIPC) @@ -1031,5 +1044,18 @@ void SensorService::ReportPlugEventCallback(const SensorPlugInfo info) }; clientInfo_.SendMsgToClient(sensorPlugData); } + +void SensorService::SetCritical() +{ +#ifdef MEMMGR_ENABLE + if (!isCritical_) { + if (Memory::MemMgrClient::GetInstance().SetCritical(getpid(), true, SENSOR_SERVICE_ABILITY_ID) != ERR_OK) { + SEN_HILOGE("setCritical failed"); + } else { + isCritical_ = true; + } + } +#endif // MEMMGR_ENABLE +} } // namespace Sensors } // namespace OHOS diff --git a/test/unittest/interfaces/inner_api/BUILD.gn b/test/unittest/interfaces/inner_api/BUILD.gn index e8799da2d0e036397aa76a56dddce43506ea5e95..b326fc8fba83a8715f3f122f54479c08dad1af6b 100644 --- a/test/unittest/interfaces/inner_api/BUILD.gn +++ b/test/unittest/interfaces/inner_api/BUILD.gn @@ -211,6 +211,33 @@ ohos_unittest("DropDetectionTest") { ] } +ohos_unittest("LoadSensorServiceTest") { + module_out_path = "sensor/sensor/interfaces/inner_api" + + sources = ["$SUBSYSTEM_DIR/test/unittest/interfaces/inner_api/load_sensor_service_test.cpp"] + + include_dirs = [ + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/interfaces/inner_api", + "$SUBSYSTEM_DIR/frameworks/native/include", + "$SUBSYSTEM_DIR/test/unittest/common/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/frameworks/native:sensor_target", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + ] + + external_deps = [ + "c_utils:utils", + "googletest:gmock", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_single", + "samgr:samgr_proxy", + ] +} + ohos_unittest("RPCSensorTest") { module_out_path = "sensor/sensor/interfaces/inner_api" @@ -271,6 +298,7 @@ group("unittest") { ":Ambientlight1Test", ":DropDetectionTest", ":HeadPostureTest", + ":LoadSensorServiceTest", ":PostureTest", ":Proximity1Test", ":RPCSensorTest", diff --git a/test/unittest/interfaces/inner_api/load_sensor_service_test.cpp b/test/unittest/interfaces/inner_api/load_sensor_service_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c24e0adc2828a17069a8103870886a28084201e --- /dev/null +++ b/test/unittest/interfaces/inner_api/load_sensor_service_test.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include + +#include "sensor_agent.h" +#include "sensor_agent_type.h" +#include "sensor_errors.h" +#include "sensor_service_load.h" +#include "system_ability_definition.h" + +#undef LOG_TAG +#define LOG_TAG "LoadSensorServiceTest" + +namespace OHOS { +namespace Sensors { +using namespace testing; +using namespace testing::ext; +using namespace OHOS::HiviewDFX; + +class LoadSensorServiceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + SensorServiceLoad *sensorServiceLoad; + SensorLoadCallback *callback; +}; + +void LoadSensorServiceTest::SetUpTestCase() +{ +} + +void LoadSensorServiceTest::TearDownTestCase() +{ +} + +void LoadSensorServiceTest::SetUp() +{ + sensorServiceLoad = new (std::nothrow) SensorServiceLoad(); + ASSERT_NE(sensorServiceLoad, nullptr); + callback = new (std::nothrow) SensorLoadCallback(); + ASSERT_NE(callback, nullptr); +} + +void LoadSensorServiceTest::TearDown() +{ + if (sensorServiceLoad != nullptr) { + delete sensorServiceLoad; + sensorServiceLoad = nullptr; + } + if (callback!= nullptr) { + delete callback; + callback = nullptr; + } +} + +HWTEST_F(LoadSensorServiceTest, SetLoadFinishTest_001, TestSize.Level1) +{ + SEN_HILOGI("SetLoadFinishTest_001 enter"); + ASSERT_NO_FATAL_FAILURE(sensorServiceLoad->SetLoadFinish()); +} + +HWTEST_F(LoadSensorServiceTest, OnLoadSystemAbilitySuccessTest_001, TestSize.Level1) +{ + SEN_HILOGI("OnLoadSystemAbilitySuccessTest_001 enter"); + int32_t systemAbilityId = 1; + sptr remoteObject = new (std::nothrow) IPCObjectStub(); + ASSERT_NO_FATAL_FAILURE(callback->OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject)); +} + +HWTEST_F(LoadSensorServiceTest, OnLoadSystemAbilitySuccessTest_002, TestSize.Level1) +{ + SEN_HILOGI("OnLoadSystemAbilitySuccessTest_002 enter"); + int32_t systemAbilityId = 1; + sptr remoteObject = new (std::nothrow) IPCObjectStub(); + ASSERT_NO_FATAL_FAILURE(callback->OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject)); +} + +HWTEST_F(LoadSensorServiceTest, OnLoadSystemAbilityFailTest_001, TestSize.Level1) +{ + SEN_HILOGI("OnLoadSystemAbilityFailTest_001 enter"); + int32_t systemAbilityId = 1; + ASSERT_NO_FATAL_FAILURE(callback->OnLoadSystemAbilityFail(systemAbilityId)); +} +} // namespace Sensors +} // namespace OHOS