1 /*
2 * Copyright (c) 2023 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 "edm_load_manager.h"
17
18 #include <system_ability_definition.h>
19
20 #include "edm_load_callback.h"
21 #include "edm_log.h"
22 #include "edm_sys_manager.h"
23 #include "iremote_broker.h"
24 #include "iremote_object.h"
25 #include "iservice_registry.h"
26
27 namespace OHOS {
28 namespace EDM {
29 static constexpr int32_t EDM_LOADSA_TIMEOUT_MS = 1000;
30
GetInstance()31 EdmLoadManager& EdmLoadManager::GetInstance()
32 {
33 static auto instance = new EdmLoadManager();
34 return *instance;
35 }
36
LoadAndGetEdmService()37 sptr<IRemoteObject> EdmLoadManager::LoadAndGetEdmService()
38 {
39 if (SUCCEEDED(LoadEdmSa())) {
40 return EdmSysManager::GetRemoteObjectOfSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
41 }
42 return nullptr;
43 }
44
LoadEdmSa()45 ErrCode EdmLoadManager::LoadEdmSa()
46 {
47 EDMLOGI("%{public}s enter, systemAbilityId = [%{public}d] loading", __func__, ENTERPRISE_DEVICE_MANAGER_SA_ID);
48 InitLoadState();
49 sptr<ISystemAbilityManager> samgr =
50 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
51 if (samgr == nullptr) {
52 EDMLOGE("%{public}s: get system ability manager failed!", __func__);
53 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
54 }
55
56 auto edmLoadCallback = sptr<EdmLoadCallback>(new EdmLoadCallback());
57 int32_t ret = samgr->LoadSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID, edmLoadCallback);
58 if (ret != ERR_OK) {
59 EDMLOGE("%{public}s: Failed to load system ability, SA Id = [%{public}d], ret = [%{public}d].",
60 __func__, ENTERPRISE_DEVICE_MANAGER_SA_ID, ret);
61 return ERR_EDM_SA_LOAD_FAILED;
62 }
63 return WaitLoadStateChange();
64 }
65
InitLoadState()66 void EdmLoadManager::InitLoadState()
67 {
68 std::unique_lock<std::mutex> lock(locatorMutex_);
69 state_ = false;
70 }
71
WaitLoadStateChange()72 ErrCode EdmLoadManager::WaitLoadStateChange()
73 {
74 std::unique_lock<std::mutex> lock(locatorMutex_);
75 auto wait = locatorCon_.wait_for(lock, std::chrono::milliseconds(EDM_LOADSA_TIMEOUT_MS), [this] {
76 return state_;
77 });
78 if (!wait) {
79 EDMLOGE("locator sa edm start time out.");
80 return ERR_EDM_SA_LOAD_FAILED;
81 }
82 EDMLOGI("sa edm load success.");
83 return ERR_OK;
84 }
85
UnloadEdmSa()86 ErrCode EdmLoadManager::UnloadEdmSa()
87 {
88 EDMLOGI("%{public}s enter, systemAbilityId = [%{public}d] unloading", __func__, ENTERPRISE_DEVICE_MANAGER_SA_ID);
89 sptr<ISystemAbilityManager> samgr =
90 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
91 if (samgr == nullptr) {
92 EDMLOGE("%{public}s: get system ability manager failed!", __func__);
93 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
94 }
95 int32_t ret = samgr->UnloadSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
96 if (ret != ERR_OK) {
97 EDMLOGE("%{public}s: Failed to unload system ability, SA Id = [%{public}d], ret = [%{public}d].",
98 __func__, ENTERPRISE_DEVICE_MANAGER_SA_ID, ret);
99 return ERR_EDM_SA_LOAD_FAILED;
100 }
101 return ERR_OK;
102 }
103
LoadSystemAbilitySuccess()104 void EdmLoadManager::LoadSystemAbilitySuccess()
105 {
106 std::unique_lock<std::mutex> lock(locatorMutex_);
107 state_ = true;
108 locatorCon_.notify_one();
109 }
110
LoadSystemAbilityFail()111 void EdmLoadManager::LoadSystemAbilityFail()
112 {
113 std::unique_lock<std::mutex> lock(locatorMutex_);
114 state_ = false;
115 locatorCon_.notify_one();
116 }
117 }; // namespace EDM
118 }; // namespace OHOS