• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "location_sa_load_manager.h"
17 
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 
22 #include "common_utils.h"
23 #include "location_log.h"
24 
25 namespace OHOS {
26 namespace Location {
LocationSaLoadManager()27 LocationSaLoadManager::LocationSaLoadManager()
28 {
29 }
30 
~LocationSaLoadManager()31 LocationSaLoadManager::~LocationSaLoadManager()
32 {
33 }
34 
LoadLocationSa(int32_t systemAbilityId)35 LocationErrCode LocationSaLoadManager::LoadLocationSa(int32_t systemAbilityId)
36 {
37     LBSLOGI(LOCATOR, "%{public}s enter, systemAbilityId = [%{public}d] loading", __func__, systemAbilityId);
38     InitLoadState();
39     sptr<ISystemAbilityManager> samgr =
40         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41     if (samgr == nullptr) {
42         LBSLOGE(LOCATOR, "%{public}s: get system ability manager failed!", __func__);
43         return ERRCODE_SERVICE_UNAVAILABLE;
44     }
45 
46     auto locationSaLoadCallback = sptr<LocationSaLoadCallback>(new LocationSaLoadCallback());
47     int32_t ret = samgr->LoadSystemAbility(systemAbilityId, locationSaLoadCallback);
48     if (ret != ERR_OK) {
49         LBSLOGE(LOCATOR, "%{public}s: Failed to load system ability, SA Id = [%{public}d], ret = [%{public}d].",
50             __func__, systemAbilityId, ret);
51         return ERRCODE_SERVICE_UNAVAILABLE;
52     }
53     return WaitLoadStateChange(systemAbilityId);
54 }
55 
InitLoadState()56 void LocationSaLoadManager::InitLoadState()
57 {
58     std::unique_lock<std::mutex> lock(locatorMutex_);
59     state_ = false;
60 }
61 
WaitLoadStateChange(int32_t systemAbilityId)62 LocationErrCode LocationSaLoadManager::WaitLoadStateChange(int32_t systemAbilityId)
63 {
64     std::unique_lock<std::mutex> lock(locatorMutex_);
65     auto wait = locatorCon_.wait_for(lock, std::chrono::milliseconds(LOCATION_LOADSA_TIMEOUT_MS), [this] {
66         return state_ == true;
67     });
68     if (!wait) {
69         LBSLOGE(LOCATOR_STANDARD, "locator sa [%{public}d] start time out.", systemAbilityId);
70         return ERRCODE_SERVICE_UNAVAILABLE;
71     }
72     return ERRCODE_SUCCESS;
73 }
74 
UnloadLocationSa(int32_t systemAbilityId)75 LocationErrCode LocationSaLoadManager::UnloadLocationSa(int32_t systemAbilityId)
76 {
77     LBSLOGI(LOCATOR, "%{public}s enter, systemAbilityId = [%{public}d] unloading", __func__, systemAbilityId);
78     sptr<ISystemAbilityManager> samgr =
79         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
80     if (samgr == nullptr) {
81         LBSLOGE(LOCATOR, "%{public}s: get system ability manager failed!", __func__);
82         return ERRCODE_SERVICE_UNAVAILABLE;
83     }
84     int32_t ret = samgr->UnloadSystemAbility(systemAbilityId);
85     if (ret != ERR_OK) {
86         LBSLOGE(LOCATOR, "%{public}s: Failed to unload system ability, SA Id = [%{public}d], ret = [%{public}d].",
87             __func__, systemAbilityId, ret);
88         return ERRCODE_SERVICE_UNAVAILABLE;
89     }
90     return ERRCODE_SUCCESS;
91 }
92 
LoadSystemAbilitySuccess()93 void LocationSaLoadManager::LoadSystemAbilitySuccess()
94 {
95     std::unique_lock<std::mutex> lock(locatorMutex_);
96     state_ = true;
97     locatorCon_.notify_one();
98 }
99 
LoadSystemAbilityFail()100 void LocationSaLoadManager::LoadSystemAbilityFail()
101 {
102     std::unique_lock<std::mutex> lock(locatorMutex_);
103     state_ = false;
104     locatorCon_.notify_one();
105 }
106 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)107 void LocationSaLoadCallback::OnLoadSystemAbilitySuccess(
108     int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
109 {
110     LBSLOGI(LOCATOR, "LocationSaLoadManager Load SA success, systemAbilityId = [%{public}d]", systemAbilityId);
111     auto instance = DelayedSingleton<LocationSaLoadManager>::GetInstance();
112     if (instance == nullptr) {
113         LBSLOGE(LOCATOR, "LocationSaLoadManager GetInstance return null");
114         return;
115     }
116     instance->LoadSystemAbilitySuccess();
117 }
118 
OnLoadSystemAbilityFail(int32_t systemAbilityId)119 void LocationSaLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
120 {
121     LBSLOGI(LOCATOR, "LocationSaLoadManager Load SA failed, systemAbilityId = [%{public}d]", systemAbilityId);
122     auto instance = DelayedSingleton<LocationSaLoadManager>::GetInstance();
123     if (instance == nullptr) {
124         LBSLOGE(LOCATOR, "LocationSaLoadManager GetInstance return null");
125         return;
126     }
127     instance->LoadSystemAbilityFail();
128 }
129 }; // namespace Location
130 }; // namespace OHOS
131