1 /*
2 * Copyright (C) 2021 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 #include "wifi_hotspot_impl.h"
16 #include "iservice_registry.h"
17 #include "wifi_logger.h"
18
19 DEFINE_WIFILOG_HOTSPOT_LABEL("WifiHotspotImpl");
20
21 namespace OHOS {
22 namespace Wifi {
23 #define RETURN_IF_FAIL(cond) \
24 do { \
25 if (!(cond)) { \
26 WIFI_LOGI("'%{public}s' failed.", #cond); \
27 return WIFI_OPT_FAILED; \
28 } \
29 } while (0)
30
WifiHotspotImpl(int systemAbilityId)31 WifiHotspotImpl::WifiHotspotImpl(int systemAbilityId) : systemAbilityId_(systemAbilityId)
32 {}
33
~WifiHotspotImpl()34 WifiHotspotImpl::~WifiHotspotImpl()
35 {}
36
Init()37 bool WifiHotspotImpl::Init()
38 {
39 sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40 if (sa_mgr == nullptr) {
41 WIFI_LOGE("failed to get SystemAbilityManager");
42 return false;
43 }
44
45 sptr<IRemoteObject> object = sa_mgr->GetSystemAbility(systemAbilityId_);
46 if (object == nullptr) {
47 WIFI_LOGE("failed to get HOTSPOT_SERVICE");
48 return false;
49 }
50
51 client_ = iface_cast<IWifiHotspot>(object);
52 if (client_ == nullptr) {
53 client_ = new (std::nothrow) WifiHotspotProxy(object);
54 }
55
56 if (client_ == nullptr) {
57 WIFI_LOGE("wifi device init failed. %{public}d", systemAbilityId_);
58 return false;
59 }
60
61 return true;
62 }
63
IsHotspotActive(void)64 bool WifiHotspotImpl::IsHotspotActive(void)
65 {
66 if (!(client_)) {
67 WIFI_LOGI("get client failed.");
68 return false;
69 }
70 bool bActive = false;
71 client_->IsHotspotActive(bActive);
72 return bActive;
73 }
74
GetHotspotState(int & state)75 ErrCode WifiHotspotImpl::GetHotspotState(int &state)
76 {
77 RETURN_IF_FAIL(client_);
78 return client_->GetHotspotState(state);
79 }
GetHotspotConfig(HotspotConfig & config)80 ErrCode WifiHotspotImpl::GetHotspotConfig(HotspotConfig &config)
81 {
82 RETURN_IF_FAIL(client_);
83 return client_->GetHotspotConfig(config);
84 }
85
SetHotspotConfig(const HotspotConfig & config)86 ErrCode WifiHotspotImpl::SetHotspotConfig(const HotspotConfig &config)
87 {
88 RETURN_IF_FAIL(client_);
89 return client_->SetHotspotConfig(config);
90 }
91
GetStationList(std::vector<StationInfo> & result)92 ErrCode WifiHotspotImpl::GetStationList(std::vector<StationInfo> &result)
93 {
94 RETURN_IF_FAIL(client_);
95 return client_->GetStationList(result);
96 }
97
DisassociateSta(const StationInfo & info)98 ErrCode WifiHotspotImpl::DisassociateSta(const StationInfo &info)
99 {
100 RETURN_IF_FAIL(client_);
101 return client_->DisassociateSta(info);
102 }
103
EnableHotspot(void)104 ErrCode WifiHotspotImpl::EnableHotspot(void)
105 {
106 RETURN_IF_FAIL(client_);
107 return client_->EnableHotspot();
108 }
109
DisableHotspot(void)110 ErrCode WifiHotspotImpl::DisableHotspot(void)
111 {
112 RETURN_IF_FAIL(client_);
113 return client_->DisableHotspot();
114 }
115
GetBlockLists(std::vector<StationInfo> & infos)116 ErrCode WifiHotspotImpl::GetBlockLists(std::vector<StationInfo> &infos)
117 {
118 RETURN_IF_FAIL(client_);
119 return client_->GetBlockLists(infos);
120 }
121
AddBlockList(const StationInfo & info)122 ErrCode WifiHotspotImpl::AddBlockList(const StationInfo &info)
123 {
124 RETURN_IF_FAIL(client_);
125 return client_->AddBlockList(info);
126 }
127
DelBlockList(const StationInfo & info)128 ErrCode WifiHotspotImpl::DelBlockList(const StationInfo &info)
129 {
130 RETURN_IF_FAIL(client_);
131 return client_->DelBlockList(info);
132 }
133
GetValidBands(std::vector<BandType> & bands)134 ErrCode WifiHotspotImpl::GetValidBands(std::vector<BandType> &bands)
135 {
136 RETURN_IF_FAIL(client_);
137 return client_->GetValidBands(bands);
138 }
139
GetValidChannels(BandType band,std::vector<int32_t> & validchannels)140 ErrCode WifiHotspotImpl::GetValidChannels(BandType band, std::vector<int32_t> &validchannels)
141 {
142 RETURN_IF_FAIL(client_);
143 return client_->GetValidChannels(band, validchannels);
144 }
145
RegisterCallBack(const sptr<IWifiHotspotCallback> & callback)146 ErrCode WifiHotspotImpl::RegisterCallBack(const sptr<IWifiHotspotCallback> &callback)
147 {
148 RETURN_IF_FAIL(client_);
149 return client_->RegisterCallBack(callback);
150 }
151
GetSupportedFeatures(long & features)152 ErrCode WifiHotspotImpl::GetSupportedFeatures(long &features)
153 {
154 RETURN_IF_FAIL(client_);
155 return client_->GetSupportedFeatures(features);
156 }
157
IsFeatureSupported(long feature)158 bool WifiHotspotImpl::IsFeatureSupported(long feature)
159 {
160 RETURN_IF_FAIL(client_);
161 long tmpFeatures = 0;
162 if (client_->GetSupportedFeatures(tmpFeatures) != WIFI_OPT_SUCCESS) {
163 return false;
164 }
165 return ((tmpFeatures & feature) == feature);
166 }
167 } // namespace Wifi
168 } // namespace OHOS