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