1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License. 5d9f0492fSopenharmony_ci * You may obtain a copy of the License at 6d9f0492fSopenharmony_ci * 7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8d9f0492fSopenharmony_ci * 9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and 13d9f0492fSopenharmony_ci * limitations under the License. 14d9f0492fSopenharmony_ci */ 15d9f0492fSopenharmony_ci 16d9f0492fSopenharmony_ci#include <cstdlib> 17d9f0492fSopenharmony_ci#include <iostream> 18d9f0492fSopenharmony_ci#include <string> 19d9f0492fSopenharmony_ci 20d9f0492fSopenharmony_ci#include "iservice_registry.h" 21d9f0492fSopenharmony_ci#include "system_ability_definition.h" 22d9f0492fSopenharmony_ci#include "system_ability_load_callback_stub.h" 23d9f0492fSopenharmony_ci 24d9f0492fSopenharmony_ciusing namespace OHOS; 25d9f0492fSopenharmony_ciusing namespace std; 26d9f0492fSopenharmony_ci 27d9f0492fSopenharmony_ciclass OnDemandLoadCallback : public SystemAbilityLoadCallbackStub { 28d9f0492fSopenharmony_cipublic: 29d9f0492fSopenharmony_ci void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject) override; 30d9f0492fSopenharmony_ci void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; 31d9f0492fSopenharmony_ci}; 32d9f0492fSopenharmony_ci 33d9f0492fSopenharmony_civoid OnDemandLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, 34d9f0492fSopenharmony_ci const sptr<IRemoteObject>& remoteObject) 35d9f0492fSopenharmony_ci{ 36d9f0492fSopenharmony_ci cout << "OnLoadSystemAbilitySuccess systemAbilityId:" << systemAbilityId << " IRemoteObject result:" << 37d9f0492fSopenharmony_ci ((remoteObject != nullptr) ? "succeed" : "failed") << endl; 38d9f0492fSopenharmony_ci} 39d9f0492fSopenharmony_ci 40d9f0492fSopenharmony_civoid OnDemandLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) 41d9f0492fSopenharmony_ci{ 42d9f0492fSopenharmony_ci cout << "OnLoadSystemAbilityFail systemAbilityId:" << systemAbilityId << endl; 43d9f0492fSopenharmony_ci} 44d9f0492fSopenharmony_ci 45d9f0492fSopenharmony_ciint main(int argc, char * const argv[]) 46d9f0492fSopenharmony_ci{ 47d9f0492fSopenharmony_ci std::map<string, int> saService = { 48d9f0492fSopenharmony_ci {"updater_sa", UPDATE_DISTRIBUTED_SERVICE_ID}, 49d9f0492fSopenharmony_ci {"softbus_server", SOFTBUS_SERVER_SA_ID}, 50d9f0492fSopenharmony_ci }; 51d9f0492fSopenharmony_ci int parameterNum = 2; 52d9f0492fSopenharmony_ci if ((argc != parameterNum) || (argv[1] == nullptr)) { 53d9f0492fSopenharmony_ci cout << "Invalid parameter" << endl; 54d9f0492fSopenharmony_ci return 0; 55d9f0492fSopenharmony_ci } 56d9f0492fSopenharmony_ci const string name = argv[1]; 57d9f0492fSopenharmony_ci int abilityId = 0; 58d9f0492fSopenharmony_ci std::map<string, int>::iterator item = saService.find(name); 59d9f0492fSopenharmony_ci if (item != saService.end()) { 60d9f0492fSopenharmony_ci cout << "sa service name " << item->first << "ability id " << item->second << endl; 61d9f0492fSopenharmony_ci abilityId = item->second; 62d9f0492fSopenharmony_ci } else { 63d9f0492fSopenharmony_ci cout << "Invalid sa service name" << endl; 64d9f0492fSopenharmony_ci return 0; 65d9f0492fSopenharmony_ci } 66d9f0492fSopenharmony_ci 67d9f0492fSopenharmony_ci sptr<OnDemandLoadCallback> loadCallback_ = new OnDemandLoadCallback(); 68d9f0492fSopenharmony_ci sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 69d9f0492fSopenharmony_ci if (sm == nullptr) { 70d9f0492fSopenharmony_ci cout << "GetSystemAbilityManager samgr object null!" << endl; 71d9f0492fSopenharmony_ci return 0; 72d9f0492fSopenharmony_ci } 73d9f0492fSopenharmony_ci int32_t result = sm->LoadSystemAbility(abilityId, loadCallback_); 74d9f0492fSopenharmony_ci if (result != ERR_OK) { 75d9f0492fSopenharmony_ci cout << "systemAbilityId:" << abilityId << " load failed, result code:" << result << endl; 76d9f0492fSopenharmony_ci return 0; 77d9f0492fSopenharmony_ci } 78d9f0492fSopenharmony_ci return 0; 79d9f0492fSopenharmony_ci} 80d9f0492fSopenharmony_ci 81