sa register SAmgr message receive

Match-id-7c7defcb9c685a174ca1062e95b86694d534f98a
This commit is contained in:
authName 2023-11-10 16:53:53 +08:00 committed by userName
parent aa657e9b15
commit 0385a71e44
4 changed files with 182 additions and 1 deletions

View File

@ -52,12 +52,21 @@ const RETRY_INTERVAL: u64 = 1000;
extern "C" {
fn SubscribeSystemEvent() -> bool;
fn UnSubscribeSystemEvent() -> bool;
fn RegisterCommonEventListener() -> bool;
fn DeregisterCommonEventListener() -> bool;
}
fn on_start<T: ISystemAbility + IMethod>(ability: &T) {
let service = AssetStub::new_remote_stub(AssetService).expect("create AssetService failed");
ability.publish(&service.as_object().expect("publish Asset service failed"), SA_ID);
logi!("[INFO]Asset service on_start");
thread::spawn(|| {
if unsafe { RegisterCommonEventListener() } {
logi!("comment event listener success.");
} else {
logi!("comment event listener failed.")
}
});
thread::spawn(|| {
for i in 0..MAX_RETRY_TIME {
if unsafe { SubscribeSystemEvent() } {
@ -74,6 +83,7 @@ fn on_stop<T: ISystemAbility + IMethod>(_ability: &T) {
logi!("[INFO]Asset service on_stop");
unsafe {
UnSubscribeSystemEvent();
DeregisterCommonEventListener();
}
}

View File

@ -21,7 +21,8 @@ ohos_static_library("asset_os_dependency") {
sources = [
"src/bms_wrapper.cpp",
"src/os_account_wrapper.cpp",
"src/system_event_wrapper.cpp",
"src/system_ability_wrapper.cpp",
"src/system_event_wrapper.cpp"
]
deps = [ ":asset_service_ffi" ]
external_deps = [
@ -34,6 +35,7 @@ ohos_static_library("asset_os_dependency") {
"hilog:libhilog",
"ipc:ipc_single",
"os_account:os_account_innerkits",
"samgr:samgr_proxy",
]
subsystem_name = "security"
part_name = "asset"

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2023 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_ABILITY_WRAPPER
#define SYSTEM_ABILITY_WRAPPER
#include "system_ability_manager_proxy.h"
#include "system_ability_status_change_stub.h"
#ifdef __cplusplus
extern "C" {
#endif
bool RegisterCommonEventListener(void);
bool DeregisterCommonEventListener(void);
class SystemAbilityHandler : public OHOS::SystemAbilityStatusChangeStub {
public:
SystemAbilityHandler();
~SystemAbilityHandler() = default;
void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
};
class SystemAbilityManager {
public:
static const int32_t LIBCESFWK_SERVICES_ID = 3299;
static bool RegisterCommonEventListener(void);
static bool DeregisterCommonEventListener(void);
private:
static OHOS::sptr<OHOS::ISystemAbilityManager> GetSystemAbility(void);
};
#ifdef __cplusplus
}
#endif
#endif // SYSTEM_ABILITY_WRAPPER_H

View File

@ -0,0 +1,119 @@
/*
* Copyright (c) 2023 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_ability_wrapper.h"
#include <unistd.h>
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
#include "system_ability_manager_proxy.h"
#include "system_ability_status_change_stub.h"
#include "asset_log.h"
#include "system_event_wrapper.h"
using namespace std;
using namespace OHOS;
static sptr<SystemAbilityHandler> abilityListener;
static constexpr int32_t RETRY_TIMES_FOR_SAMGR = 50;
static constexpr int32_t RETRY_DURATION_US = 200 * 1000;
bool SystemAbilityManager::RegisterCommonEventListener(void)
{
sptr<ISystemAbilityManager> samgrProxy = GetSystemAbility();
if (samgrProxy == nullptr) {
LOGE("wait for samgr time out (10s)");
return false;
}
abilityListener = new (std::nothrow) SystemAbilityHandler();
if (abilityListener == nullptr) {
LOGE("New ability listener failed.");
return false;
}
int32_t ret = samgrProxy->SubscribeSystemAbility(LIBCESFWK_SERVICES_ID, abilityListener);
if (ret != ERR_OK) {
LOGE("Subscribe common event systemAbility fail.");
return false;
}
return true;
}
bool SystemAbilityManager::DeregisterCommonEventListener(void)
{
sptr<ISystemAbilityManager> samgrProxy = GetSystemAbility();
if (samgrProxy == nullptr || abilityListener == nullptr) {
LOGE("Params is invalid.");
return false;
}
if (samgrProxy->UnSubscribeSystemAbility(LIBCESFWK_SERVICES_ID, abilityListener) != ERR_OK ||
!UnSubscribeSystemEvent()) {
LOGE("UnSubscribe common event systemAbility fail.");
return false;
}
return true;
}
OHOS::sptr<OHOS::ISystemAbilityManager> SystemAbilityManager::GetSystemAbility(void)
{
int32_t retryCount = RETRY_TIMES_FOR_SAMGR;
sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
while (samgrProxy == nullptr) {
LOGE("waiting for samgr...");
if (retryCount > 0) {
usleep(RETRY_DURATION_US);
samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
} else {
LOGE("wait for samgr time out (10s)");
return nullptr;
}
retryCount--;
}
return samgrProxy;
}
SystemAbilityHandler::SystemAbilityHandler() {}
void SystemAbilityHandler::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
{
if (systemAbilityId != SystemAbilityManager::LIBCESFWK_SERVICES_ID) {
LOGE("Current sa is invalid.");
return;
}
if (!SubscribeSystemEvent()) {
LOGE("Init comment event fail.");
}
}
void SystemAbilityHandler::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
{
LOGI("start OnRemoveSystemAbility.");
if (systemAbilityId != SystemAbilityManager::LIBCESFWK_SERVICES_ID) {
LOGE("Current sa is invalid.");
return;
}
if (!UnSubscribeSystemEvent()) {
LOGE("Destroy comment event fail.");
}
}
bool RegisterCommonEventListener() {
return SystemAbilityManager::RegisterCommonEventListener();
}
bool DeregisterCommonEventListener() {
return SystemAbilityManager::DeregisterCommonEventListener();
}