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