1dfe32fa1Soh_ci/* 2dfe32fa1Soh_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3dfe32fa1Soh_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4dfe32fa1Soh_ci * you may not use this file except in compliance with the License. 5dfe32fa1Soh_ci * You may obtain a copy of the License at 6dfe32fa1Soh_ci * 7dfe32fa1Soh_ci * http://www.apache.org/licenses/LICENSE-2.0 8dfe32fa1Soh_ci * 9dfe32fa1Soh_ci * Unless required by applicable law or agreed to in writing, software 10dfe32fa1Soh_ci * distributed under the License is distributed on an "AS IS" BASIS, 11dfe32fa1Soh_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12dfe32fa1Soh_ci * See the License for the specific language governing permissions and 13dfe32fa1Soh_ci * limitations under the License. 14dfe32fa1Soh_ci */ 15dfe32fa1Soh_ci 16dfe32fa1Soh_ci#include "system_ability_wrapper.h" 17dfe32fa1Soh_ci 18dfe32fa1Soh_ci#include <unistd.h> 19dfe32fa1Soh_ci 20dfe32fa1Soh_ci#include "if_system_ability_manager.h" 21dfe32fa1Soh_ci#include "iservice_registry.h" 22dfe32fa1Soh_ci#include "system_ability_definition.h" 23dfe32fa1Soh_ci#include "system_ability_status_change_stub.h" 24dfe32fa1Soh_ci 25dfe32fa1Soh_ci#include "asset_log.h" 26dfe32fa1Soh_ci#include "system_event_wrapper.h" 27dfe32fa1Soh_ci 28dfe32fa1Soh_cinamespace { 29dfe32fa1Soh_ciconst int32_t RETRY_TIMES_FOR_SAMGR = 50; 30dfe32fa1Soh_ciconst int32_t RETRY_DURATION_US = 200 * 1000; 31dfe32fa1Soh_ci 32dfe32fa1Soh_ciclass SystemAbilityHandler : public OHOS::SystemAbilityStatusChangeStub { 33dfe32fa1Soh_cipublic: 34dfe32fa1Soh_ci explicit SystemAbilityHandler(const EventCallBack eventCallBack) : eventCallBack(eventCallBack) {}; 35dfe32fa1Soh_ci ~SystemAbilityHandler() = default; 36dfe32fa1Soh_ci void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override 37dfe32fa1Soh_ci { 38dfe32fa1Soh_ci if (systemAbilityId != OHOS::COMMON_EVENT_SERVICE_ID) { 39dfe32fa1Soh_ci return; 40dfe32fa1Soh_ci } 41dfe32fa1Soh_ci 42dfe32fa1Soh_ci if (SubscribeSystemEvent(eventCallBack)) { 43dfe32fa1Soh_ci LOGI("Subscribe system event success."); 44dfe32fa1Soh_ci } else { 45dfe32fa1Soh_ci LOGE("Subscribe system event failed."); 46dfe32fa1Soh_ci } 47dfe32fa1Soh_ci } 48dfe32fa1Soh_ci void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override 49dfe32fa1Soh_ci { 50dfe32fa1Soh_ci if (systemAbilityId != OHOS::COMMON_EVENT_SERVICE_ID) { 51dfe32fa1Soh_ci return; 52dfe32fa1Soh_ci } 53dfe32fa1Soh_ci if (UnSubscribeSystemEvent()) { 54dfe32fa1Soh_ci LOGI("UnSubscribe system event success."); 55dfe32fa1Soh_ci } else { 56dfe32fa1Soh_ci LOGE("UnSubscribe system event failed."); 57dfe32fa1Soh_ci } 58dfe32fa1Soh_ci } 59dfe32fa1Soh_ciprivate: 60dfe32fa1Soh_ci const EventCallBack eventCallBack; 61dfe32fa1Soh_ci}; 62dfe32fa1Soh_ci 63dfe32fa1Soh_ciOHOS::sptr<OHOS::ISystemAbilityManager> GetSystemAbility(void) 64dfe32fa1Soh_ci{ 65dfe32fa1Soh_ci int32_t retryCount = RETRY_TIMES_FOR_SAMGR; 66dfe32fa1Soh_ci OHOS::sptr<OHOS::ISystemAbilityManager> samgrProxy = 67dfe32fa1Soh_ci OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 68dfe32fa1Soh_ci while (samgrProxy == nullptr && retryCount > 0) { 69dfe32fa1Soh_ci usleep(RETRY_DURATION_US); 70dfe32fa1Soh_ci samgrProxy = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 71dfe32fa1Soh_ci retryCount--; 72dfe32fa1Soh_ci } 73dfe32fa1Soh_ci return samgrProxy; 74dfe32fa1Soh_ci} 75dfe32fa1Soh_ci 76dfe32fa1Soh_ciOHOS::sptr<SystemAbilityHandler> abilityHandler; 77dfe32fa1Soh_ci} // namespace 78dfe32fa1Soh_ci 79dfe32fa1Soh_cibool SubscribeSystemAbility(const EventCallBack eventCallBack) 80dfe32fa1Soh_ci{ 81dfe32fa1Soh_ci OHOS::sptr<OHOS::ISystemAbilityManager> samgrProxy = GetSystemAbility(); 82dfe32fa1Soh_ci if (samgrProxy == nullptr) { 83dfe32fa1Soh_ci LOGE("Get system ability failed"); 84dfe32fa1Soh_ci return false; 85dfe32fa1Soh_ci } 86dfe32fa1Soh_ci 87dfe32fa1Soh_ci abilityHandler = new (std::nothrow) SystemAbilityHandler(eventCallBack); 88dfe32fa1Soh_ci if (abilityHandler == nullptr) { 89dfe32fa1Soh_ci LOGE("Create system ability handler failed."); 90dfe32fa1Soh_ci return false; 91dfe32fa1Soh_ci } 92dfe32fa1Soh_ci 93dfe32fa1Soh_ci int32_t ret = samgrProxy->SubscribeSystemAbility(OHOS::COMMON_EVENT_SERVICE_ID, abilityHandler); 94dfe32fa1Soh_ci if (ret != OHOS::ERR_OK) { 95dfe32fa1Soh_ci LOGE("Subscribe system ability failed."); 96dfe32fa1Soh_ci return false; 97dfe32fa1Soh_ci } 98dfe32fa1Soh_ci return true; 99dfe32fa1Soh_ci} 100dfe32fa1Soh_ci 101dfe32fa1Soh_cibool UnSubscribeSystemAbility() 102dfe32fa1Soh_ci{ 103dfe32fa1Soh_ci OHOS::sptr<OHOS::ISystemAbilityManager> samgrProxy = GetSystemAbility(); 104dfe32fa1Soh_ci if (samgrProxy == nullptr || abilityHandler == nullptr) { 105dfe32fa1Soh_ci return false; 106dfe32fa1Soh_ci } 107dfe32fa1Soh_ci 108dfe32fa1Soh_ci if (samgrProxy->UnSubscribeSystemAbility(OHOS::COMMON_EVENT_SERVICE_ID, abilityHandler) != OHOS::ERR_OK || 109dfe32fa1Soh_ci !UnSubscribeSystemEvent()) { 110dfe32fa1Soh_ci LOGE("UnSubscribe system ability or system event failed."); 111dfe32fa1Soh_ci return false; 112dfe32fa1Soh_ci } 113dfe32fa1Soh_ci 114dfe32fa1Soh_ci return true; 115dfe32fa1Soh_ci}