1 /*
2  * Copyright (c) 2024 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_sta_iface.h"
17 #include <hdf_log.h>
18 
19 namespace OHOS {
20 namespace HDI {
21 namespace Wlan {
22 namespace Chip {
23 namespace V1_0 {
24 
WifiStaIface(const std::string & ifname,const std::weak_ptr<WifiVendorHal> vendorHal,const std::weak_ptr<IfaceUtil> ifaceUtil)25 WifiStaIface::WifiStaIface(
26     const std::string& ifname,
27     const std::weak_ptr<WifiVendorHal> vendorHal,
28     const std::weak_ptr<IfaceUtil> ifaceUtil)
29     : ifname_(ifname),
30       vendorHal_(vendorHal),
31       ifaceUtil_(ifaceUtil),
32       isValid_(true)
33 {}
34 
Invalidate()35 void WifiStaIface::Invalidate()
36 {
37     vendorHal_.reset();
38     isValid_ = false;
39 }
40 
IsValid()41 bool WifiStaIface::IsValid()
42 {
43     return isValid_;
44 }
45 
GetName()46 std::string WifiStaIface::GetName()
47 {
48     return ifname_;
49 }
50 
GetIfaceType(IfaceType & type)51 int32_t WifiStaIface::GetIfaceType(IfaceType& type)
52 {
53     type = IfaceType::STA;
54     return HDF_SUCCESS;
55 }
56 
GetIfaceName(std::string & name)57 int32_t WifiStaIface::GetIfaceName(std::string& name)
58 {
59     name = ifname_;
60     return HDF_SUCCESS;
61 }
62 
GetSupportFreqs(int band,std::vector<uint32_t> & frequencies)63 int32_t WifiStaIface::GetSupportFreqs(int band, std::vector<uint32_t>& frequencies)
64 {
65     WifiError status;
66     std::vector<uint32_t> validFrequencies;
67     std::tie(status, validFrequencies) = vendorHal_.lock()->GetValidFrequenciesForBand(
68         ifname_, band);
69     frequencies = validFrequencies;
70     if (status == HAL_SUCCESS) {
71         return HDF_SUCCESS;
72     }
73     return HDF_FAILURE;
74 }
75 
GetIfaceCap(uint32_t & capabilities)76 int32_t WifiStaIface::GetIfaceCap(uint32_t& capabilities)
77 {
78     WifiError status = vendorHal_.lock()->GetSupportedFeatureSet(ifname_, capabilities);
79     if (status != HAL_SUCCESS) {
80         return HDF_FAILURE;
81     }
82     return HDF_SUCCESS;
83 }
84 
SetMacAddress(const std::string & mac)85 int32_t WifiStaIface::SetMacAddress(const std::string& mac)
86 {
87     bool status = ifaceUtil_.lock()->SetMacAddress(ifname_, mac);
88     if (!status) {
89         return HDF_FAILURE;
90     }
91     return HDF_SUCCESS;
92 }
93 
SetCountryCode(const std::string & code)94 int32_t WifiStaIface::SetCountryCode(const std::string& code)
95 {
96     WifiError status = vendorHal_.lock()->SetCountryCode(ifname_, code);
97     if (status == HAL_SUCCESS) {
98         return HDF_SUCCESS;
99     }
100     return HDF_FAILURE;
101 }
102 
GetPowerMode(int32_t & powerMode)103 int32_t WifiStaIface::GetPowerMode(int32_t& powerMode)
104 {
105     return HDF_ERR_NOT_SUPPORT;
106 }
107 
SetPowerMode(int32_t powerMode)108 int32_t WifiStaIface::SetPowerMode(int32_t powerMode)
109 {
110     return HDF_ERR_NOT_SUPPORT;
111 }
112 
RegisterChipIfaceCallBack(const sptr<IChipIfaceCallback> & chipIfaceCallback)113 int32_t WifiStaIface::RegisterChipIfaceCallBack(const sptr<IChipIfaceCallback>& chipIfaceCallback)
114 {
115     if (chipIfaceCallback == nullptr) {
116         HDF_LOGE("chipIfaceCallback is null");
117         return HDF_FAILURE;
118     }
119     HDF_LOGI("register sta callback");
120     vendorHal_.lock()->RegisterIfaceCallBack(ifname_, chipIfaceCallback);
121     return HDF_SUCCESS;
122 }
123 
UnRegisterChipIfaceCallBack(const sptr<IChipIfaceCallback> & chipIfaceCallback)124 int32_t WifiStaIface::UnRegisterChipIfaceCallBack(const sptr<IChipIfaceCallback>& chipIfaceCallback)
125 {
126     if (chipIfaceCallback == nullptr) {
127         HDF_LOGE("chipIfaceCallback is null");
128         return HDF_FAILURE;
129     }
130     HDF_LOGI("unregister sta callback");
131     vendorHal_.lock()->UnRegisterIfaceCallBack(ifname_, chipIfaceCallback);
132     return HDF_SUCCESS;
133 }
134 
StartScan(const ScanParams & scanParam)135 int32_t WifiStaIface::StartScan(const ScanParams& scanParam)
136 {
137     HDF_LOGD("StartScan");
138     WifiError status = vendorHal_.lock()->StartScan(ifname_, scanParam);
139     if (status == HAL_SUCCESS) {
140         return HDF_SUCCESS;
141     }
142     return HDF_FAILURE;
143 }
144 
GetScanInfos(std::vector<ScanResultsInfo> & scanResultsInfo)145 int32_t WifiStaIface::GetScanInfos(std::vector<ScanResultsInfo>& scanResultsInfo)
146 {
147     HDF_LOGD("GetScanInfos");
148     WifiError status = vendorHal_.lock()->GetScanInfos(ifname_, scanResultsInfo);
149     if (status == HAL_SUCCESS) {
150         return HDF_SUCCESS;
151     }
152     return HDF_FAILURE;
153 }
154 
StartPnoScan(const PnoScanParams & pnoParams)155 int32_t WifiStaIface::StartPnoScan(const PnoScanParams& pnoParams)
156 {
157     HDF_LOGD("StartPnoScan");
158     WifiError status = vendorHal_.lock()->StartPnoScan(ifname_, pnoParams);
159     if (status == HAL_SUCCESS) {
160         return HDF_SUCCESS;
161     }
162     return HDF_FAILURE;
163 }
164 
StopPnoScan()165 int32_t WifiStaIface::StopPnoScan()
166 {
167     HDF_LOGD("StopPnoScan");
168     WifiError status = vendorHal_.lock()->StopPnoScan(ifname_);
169     if (status == HAL_SUCCESS) {
170         return HDF_SUCCESS;
171     }
172     return HDF_FAILURE;
173 }
174 
GetSignalPollInfo(SignalPollResult & signalPollResult)175 int32_t WifiStaIface::GetSignalPollInfo(SignalPollResult& signalPollResult)
176 {
177     WifiError status = vendorHal_.lock()->GetSignalPollInfo(ifname_, signalPollResult);
178     if (status == HAL_SUCCESS) {
179         return HDF_SUCCESS;
180     }
181     return HDF_FAILURE;
182 }
183 
EnablePowerMode(int32_t mode)184 int32_t WifiStaIface::EnablePowerMode(int32_t mode)
185 {
186     HDF_LOGD("EnablePowerMode");
187     if (ifaceUtil_.lock()->GetUpState(ifname_)) {
188         HDF_LOGE("EnablePowerMode interface state is not OK.");
189         return HDF_FAILURE;
190     }
191     WifiError status = vendorHal_.lock()->EnablePowerMode(ifname_, mode);
192     if (status == HAL_SUCCESS) {
193         return HDF_SUCCESS;
194     }
195     return HDF_FAILURE;
196 }
197 
SetDpiMarkRule(int32_t uid,int32_t protocol,int32_t enable)198 int32_t WifiStaIface::SetDpiMarkRule(int32_t uid, int32_t protocol, int32_t enable)
199 {
200     WifiError status = vendorHal_.lock()->SetDpiMarkRule(uid, protocol, enable);
201     if (status == HAL_SUCCESS) {
202         return HDF_SUCCESS;
203     }
204     return HDF_FAILURE;
205 }
206 
SetTxPower(int32_t power)207 int32_t WifiStaIface::SetTxPower(int32_t power)
208 {
209     WifiError status = vendorHal_.lock()->SetTxPower(ifname_, power);
210     if (status == HAL_SUCCESS) {
211         return HDF_SUCCESS;
212     }
213     return HDF_FAILURE;
214 }
215 
SetIfaceState(bool state)216 int32_t WifiStaIface::SetIfaceState(bool state)
217 {
218     if (ifaceUtil_.lock()->SetUpState(ifname_, state)) {
219         return HDF_SUCCESS;
220     }
221     return HDF_FAILURE;
222 }
223 }
224 }
225 }
226 }
227 }