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 "wifi_sa_manager.h"
17 
18 #include "iremote_broker.h"
19 #include "iremote_object.h"
20 #include "iservice_registry.h"
21 #include "wifi_logger.h"
22 #include "wifi_errcode.h"
23 DEFINE_WIFILOG_LABEL("WifiSaLoadManager");
24 
25 namespace OHOS {
26 namespace Wifi {
27 static constexpr int32_t WIFI_LOADSA_TIMEOUT_MS = 1000;
28 
GetInstance()29 WifiSaLoadManager& WifiSaLoadManager::GetInstance()
30 {
31     static auto instance = new WifiSaLoadManager();
32     return * instance;
33 }
34 
LoadWifiSa(int32_t systemAbilityId)35 ErrCode WifiSaLoadManager::LoadWifiSa(int32_t systemAbilityId)
36 {
37     WIFI_LOGD("%{public}s enter, systemAbilityId = [%{public}d] loading", __func__, systemAbilityId);
38     sptr<ISystemAbilityManager> samgr =
39         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40     if (samgr == nullptr) {
41         WIFI_LOGE("%{public}s: get system ability manager failed!", __func__);
42         return WIFI_OPT_FAILED;
43     }
44     auto object = samgr->CheckSystemAbility(systemAbilityId);
45     if (object != nullptr) {
46         return WIFI_OPT_SUCCESS;
47     }
48     InitLoadState();
49     sptr<WifiSaLoadCallback> loadCallback = new (std::nothrow) WifiSaLoadCallback();
50     if (loadCallback == nullptr) {
51         WIFI_LOGE("%{public}s: wifi sa load callback failed!", __func__);
52         return WIFI_OPT_FAILED;
53     }
54     int32_t ret = samgr->LoadSystemAbility(systemAbilityId, loadCallback);
55     if (ret != WIFI_OPT_SUCCESS) {
56         WIFI_LOGE("%{public}s: Failed to load system ability, SA Id = [%{public}d], ret = [%{public}d].",
57             __func__, systemAbilityId, ret);
58         return WIFI_OPT_FAILED;
59     }
60     return WaitLoadStateChange(systemAbilityId);
61 }
62 
InitLoadState()63 void WifiSaLoadManager::InitLoadState()
64 {
65     std::unique_lock<std::mutex> lock(locatorMutex_);
66     state_ = false;
67 }
68 
WaitLoadStateChange(int32_t systemAbilityId)69 ErrCode WifiSaLoadManager::WaitLoadStateChange(int32_t systemAbilityId)
70 {
71     std::unique_lock<std::mutex> lock(locatorMutex_);
72     auto wait = locatorCon_.wait_for(lock, std::chrono::milliseconds(WIFI_LOADSA_TIMEOUT_MS), [this] {
73         return state_ == true;
74     });
75     if (!wait) {
76         WIFI_LOGE("locator sa [%{public}d] start time out.", systemAbilityId);
77         return WIFI_OPT_FAILED;
78     }
79     return WIFI_OPT_SUCCESS;
80 }
81 
UnloadWifiSa(int32_t systemAbilityId)82 ErrCode WifiSaLoadManager::UnloadWifiSa(int32_t systemAbilityId)
83 {
84     WIFI_LOGI("%{public}s enter, systemAbilityId = [%{public}d] unloading", __func__, systemAbilityId);
85     sptr<ISystemAbilityManager> samgr =
86         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
87     if (samgr == nullptr) {
88         WIFI_LOGE("%{public}s: get system ability manager failed!", __func__);
89         return WIFI_OPT_FAILED;
90     }
91     int32_t ret = samgr->UnloadSystemAbility(systemAbilityId);
92     if (ret != WIFI_OPT_SUCCESS) {
93         WIFI_LOGE("%{public}s: Failed to unload system ability, SA Id = [%{public}d], ret = [%{public}d].",
94             __func__, systemAbilityId, ret);
95         return WIFI_OPT_FAILED;
96     }
97     return WIFI_OPT_SUCCESS;
98 }
99 
LoadSystemAbilitySuccess()100 void WifiSaLoadManager::LoadSystemAbilitySuccess()
101 {
102     std::unique_lock<std::mutex> lock(locatorMutex_);
103     state_ = true;
104     locatorCon_.notify_one();
105 }
106 
LoadSystemAbilityFail()107 void WifiSaLoadManager::LoadSystemAbilityFail()
108 {
109     std::unique_lock<std::mutex> lock(locatorMutex_);
110     state_ = false;
111     locatorCon_.notify_one();
112 }
113 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)114 void WifiSaLoadCallback::OnLoadSystemAbilitySuccess(
115     int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
116 {
117     WIFI_LOGD("WifiSaLoadManager Load SA success, systemAbilityId = [%{public}d]", systemAbilityId);
118     WifiSaLoadManager::GetInstance().LoadSystemAbilitySuccess();
119 }
120 
OnLoadSystemAbilityFail(int32_t systemAbilityId)121 void WifiSaLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
122 {
123     WIFI_LOGI("WifiSaLoadManager Load SA failed, systemAbilityId = [%{public}d]", systemAbilityId);
124     WifiSaLoadManager::GetInstance().LoadSystemAbilityFail();
125 }
126 }; // namespace Wifi
127 }; // namespace OHOS