1 /*
2 * Copyright (C) 2021-2022 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_hotspot_impl.h"
17 #include <new>
18 #include "wifi_hotspot_proxy.h"
19 #include "i_wifi_hotspot_mgr.h"
20 #include "iremote_broker.h"
21 #include "iremote_object.h"
22 #include "iservice_registry.h"
23 #include "wifi_hotspot_mgr_proxy.h"
24 #include "wifi_logger.h"
25 #include "wifi_sa_manager.h"
26
27 DEFINE_WIFILOG_HOTSPOT_LABEL("WifiHotspotImpl");
28
29 namespace OHOS {
30 namespace Wifi {
31 #define RETURN_IF_FAIL(cond) \
32 do { \
33 if (!(cond)) { \
34 WIFI_LOGI("'%{public}s' failed.", #cond); \
35 return WIFI_OPT_FAILED; \
36 } \
37 } while (0)
38
WifiHotspotImpl(int systemAbilityId)39 WifiHotspotImpl::WifiHotspotImpl(int systemAbilityId) : systemAbilityId_(systemAbilityId), instId(0)
40 {}
41
~WifiHotspotImpl()42 WifiHotspotImpl::~WifiHotspotImpl()
43 {}
44
Init(int id)45 bool WifiHotspotImpl::Init(int id)
46 {
47 instId = id;
48 return true;
49 }
50
GetWifiHotspotProxy()51 bool WifiHotspotImpl::GetWifiHotspotProxy()
52 {
53 WifiSaLoadManager::GetInstance().LoadWifiSa(systemAbilityId_);
54 if (IsRemoteDied() == false) {
55 return true;
56 }
57 sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58 if (sa_mgr == nullptr) {
59 WIFI_LOGE("failed to get SystemAbilityManager");
60 return false;
61 }
62 sptr<IRemoteObject> object = sa_mgr->GetSystemAbility(systemAbilityId_);
63 if (object == nullptr) {
64 WIFI_LOGE("failed to get hotspot mgr");
65 return false;
66 }
67
68 sptr<IWifiHotspotMgr> hotspotMgr = iface_cast<IWifiHotspotMgr>(object);
69 if (hotspotMgr == nullptr) {
70 hotspotMgr = new (std::nothrow) WifiHotspotMgrProxy(object);
71 }
72 if (hotspotMgr == nullptr) {
73 WIFI_LOGE("wifi hotspot init failed, %{public}d", systemAbilityId_);
74 return false;
75 }
76
77 sptr<IRemoteObject> service = hotspotMgr->GetWifiRemote(instId);
78 if (service == nullptr) {
79 WIFI_LOGE("wifi device remote obj is null, %{public}d", instId);
80 return false;
81 }
82
83 client_ = new (std::nothrow) WifiHotspotProxy(service);
84 if (client_ == nullptr) {
85 WIFI_LOGE("wifi device id init failed., %{public}d", systemAbilityId_);
86 return false;
87 }
88 return true;
89 }
90
IsHotspotActive(bool & isActive)91 ErrCode WifiHotspotImpl::IsHotspotActive(bool &isActive)
92 {
93 std::lock_guard<std::mutex> lock(mutex_);
94 RETURN_IF_FAIL(GetWifiHotspotProxy());
95 return client_->IsHotspotActive(isActive);
96 }
97
IsHotspotDualBandSupported(bool & isSupported)98 ErrCode WifiHotspotImpl::IsHotspotDualBandSupported(bool &isSupported)
99 {
100 std::lock_guard<std::mutex> lock(mutex_);
101 RETURN_IF_FAIL(GetWifiHotspotProxy());
102 return client_->IsHotspotDualBandSupported(isSupported);
103 }
104
GetHotspotState(int & state)105 ErrCode WifiHotspotImpl::GetHotspotState(int &state)
106 {
107 std::lock_guard<std::mutex> lock(mutex_);
108 RETURN_IF_FAIL(GetWifiHotspotProxy());
109 return client_->GetHotspotState(state);
110 }
111
GetHotspotConfig(HotspotConfig & config)112 ErrCode WifiHotspotImpl::GetHotspotConfig(HotspotConfig &config)
113 {
114 std::lock_guard<std::mutex> lock(mutex_);
115 RETURN_IF_FAIL(GetWifiHotspotProxy());
116 return client_->GetHotspotConfig(config);
117 }
118
SetHotspotConfig(const HotspotConfig & config)119 ErrCode WifiHotspotImpl::SetHotspotConfig(const HotspotConfig &config)
120 {
121 std::lock_guard<std::mutex> lock(mutex_);
122 RETURN_IF_FAIL(GetWifiHotspotProxy());
123 return client_->SetHotspotConfig(config);
124 }
125
SetHotspotIdleTimeout(int time)126 ErrCode WifiHotspotImpl::SetHotspotIdleTimeout(int time)
127 {
128 std::lock_guard<std::mutex> lock(mutex_);
129 RETURN_IF_FAIL(client_);
130 return client_->SetHotspotIdleTimeout(time);
131 }
132
GetStationList(std::vector<StationInfo> & result)133 ErrCode WifiHotspotImpl::GetStationList(std::vector<StationInfo> &result)
134 {
135 std::lock_guard<std::mutex> lock(mutex_);
136 RETURN_IF_FAIL(GetWifiHotspotProxy());
137 return client_->GetStationList(result);
138 }
139
DisassociateSta(const StationInfo & info)140 ErrCode WifiHotspotImpl::DisassociateSta(const StationInfo &info)
141 {
142 std::lock_guard<std::mutex> lock(mutex_);
143 RETURN_IF_FAIL(GetWifiHotspotProxy());
144 return client_->DisassociateSta(info);
145 }
146
EnableHotspot(const ServiceType type)147 ErrCode WifiHotspotImpl::EnableHotspot(const ServiceType type)
148 {
149 std::lock_guard<std::mutex> lock(mutex_);
150 RETURN_IF_FAIL(GetWifiHotspotProxy());
151 return client_->EnableHotspot(type);
152 }
153
DisableHotspot(const ServiceType type)154 ErrCode WifiHotspotImpl::DisableHotspot(const ServiceType type)
155 {
156 std::lock_guard<std::mutex> lock(mutex_);
157 RETURN_IF_FAIL(GetWifiHotspotProxy());
158 return client_->DisableHotspot(type);
159 }
160
GetBlockLists(std::vector<StationInfo> & infos)161 ErrCode WifiHotspotImpl::GetBlockLists(std::vector<StationInfo> &infos)
162 {
163 std::lock_guard<std::mutex> lock(mutex_);
164 RETURN_IF_FAIL(GetWifiHotspotProxy());
165 return client_->GetBlockLists(infos);
166 }
167
AddBlockList(const StationInfo & info)168 ErrCode WifiHotspotImpl::AddBlockList(const StationInfo &info)
169 {
170 std::lock_guard<std::mutex> lock(mutex_);
171 RETURN_IF_FAIL(GetWifiHotspotProxy());
172 return client_->AddBlockList(info);
173 }
174
DelBlockList(const StationInfo & info)175 ErrCode WifiHotspotImpl::DelBlockList(const StationInfo &info)
176 {
177 std::lock_guard<std::mutex> lock(mutex_);
178 RETURN_IF_FAIL(GetWifiHotspotProxy());
179 return client_->DelBlockList(info);
180 }
181
GetValidBands(std::vector<BandType> & bands)182 ErrCode WifiHotspotImpl::GetValidBands(std::vector<BandType> &bands)
183 {
184 std::lock_guard<std::mutex> lock(mutex_);
185 RETURN_IF_FAIL(GetWifiHotspotProxy());
186 return client_->GetValidBands(bands);
187 }
188
GetValidChannels(BandType band,std::vector<int32_t> & validchannels)189 ErrCode WifiHotspotImpl::GetValidChannels(BandType band, std::vector<int32_t> &validchannels)
190 {
191 std::lock_guard<std::mutex> lock(mutex_);
192 RETURN_IF_FAIL(GetWifiHotspotProxy());
193 return client_->GetValidChannels(band, validchannels);
194 }
195
RegisterCallBack(const sptr<IWifiHotspotCallback> & callback,const std::vector<std::string> & event)196 ErrCode WifiHotspotImpl::RegisterCallBack(const sptr<IWifiHotspotCallback> &callback,
197 const std::vector<std::string> &event)
198 {
199 std::lock_guard<std::mutex> lock(mutex_);
200 RETURN_IF_FAIL(GetWifiHotspotProxy());
201 return client_->RegisterCallBack(callback, event);
202 }
203
GetSupportedFeatures(long & features)204 ErrCode WifiHotspotImpl::GetSupportedFeatures(long &features)
205 {
206 std::lock_guard<std::mutex> lock(mutex_);
207 RETURN_IF_FAIL(GetWifiHotspotProxy());
208 return client_->GetSupportedFeatures(features);
209 }
210
IsFeatureSupported(long feature)211 bool WifiHotspotImpl::IsFeatureSupported(long feature)
212 {
213 std::lock_guard<std::mutex> lock(mutex_);
214 RETURN_IF_FAIL(GetWifiHotspotProxy());
215 long tmpFeatures = 0;
216 if (client_->GetSupportedFeatures(tmpFeatures) != WIFI_OPT_SUCCESS) {
217 return false;
218 }
219 return ((tmpFeatures & feature) == feature);
220 }
221
GetSupportedPowerModel(std::set<PowerModel> & setPowerModelList)222 ErrCode WifiHotspotImpl::GetSupportedPowerModel(std::set<PowerModel>& setPowerModelList)
223 {
224 std::lock_guard<std::mutex> lock(mutex_);
225 RETURN_IF_FAIL(GetWifiHotspotProxy());
226 return client_->GetSupportedPowerModel(setPowerModelList);
227 }
228
GetPowerModel(PowerModel & model)229 ErrCode WifiHotspotImpl::GetPowerModel(PowerModel& model)
230 {
231 std::lock_guard<std::mutex> lock(mutex_);
232 RETURN_IF_FAIL(GetWifiHotspotProxy());
233 return client_->GetPowerModel(model);
234 }
235
SetPowerModel(const PowerModel & model)236 ErrCode WifiHotspotImpl::SetPowerModel(const PowerModel& model)
237 {
238 std::lock_guard<std::mutex> lock(mutex_);
239 RETURN_IF_FAIL(GetWifiHotspotProxy());
240 return client_->SetPowerModel(model);
241 }
242
IsRemoteDied(void)243 bool WifiHotspotImpl::IsRemoteDied(void)
244 {
245 return (client_ == nullptr) ? true : client_->IsRemoteDied();
246 }
247 } // namespace Wifi
248 } // namespace OHOS
249