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