1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <android-base/logging.h>
18 
19 #include "hidl_return_util.h"
20 #include "hidl_struct_util.h"
21 #include "wifi_sta_iface.h"
22 #include "wifi_status_util.h"
23 
24 namespace android {
25 namespace hardware {
26 namespace wifi {
27 namespace V1_5 {
28 namespace implementation {
29 using hidl_return_util::validateAndCall;
30 
WifiStaIface(const std::string & ifname,const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util)31 WifiStaIface::WifiStaIface(
32     const std::string& ifname,
33     const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
34     const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util)
35     : ifname_(ifname),
36       legacy_hal_(legacy_hal),
37       iface_util_(iface_util),
38       is_valid_(true) {
39     // Turn on DFS channel usage for STA iface.
40     legacy_hal::wifi_error legacy_status =
41         legacy_hal_.lock()->setDfsFlag(ifname_, true);
42     if (legacy_status != legacy_hal::WIFI_SUCCESS) {
43         LOG(ERROR)
44             << "Failed to set DFS flag; DFS channels may be unavailable.";
45     }
46 }
47 
invalidate()48 void WifiStaIface::invalidate() {
49     legacy_hal_.reset();
50     event_cb_handler_.invalidate();
51     is_valid_ = false;
52 }
53 
isValid()54 bool WifiStaIface::isValid() { return is_valid_; }
55 
getName()56 std::string WifiStaIface::getName() { return ifname_; }
57 
getEventCallbacks()58 std::set<sp<IWifiStaIfaceEventCallback>> WifiStaIface::getEventCallbacks() {
59     return event_cb_handler_.getCallbacks();
60 }
61 
getName(getName_cb hidl_status_cb)62 Return<void> WifiStaIface::getName(getName_cb hidl_status_cb) {
63     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
64                            &WifiStaIface::getNameInternal, hidl_status_cb);
65 }
66 
getType(getType_cb hidl_status_cb)67 Return<void> WifiStaIface::getType(getType_cb hidl_status_cb) {
68     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
69                            &WifiStaIface::getTypeInternal, hidl_status_cb);
70 }
71 
registerEventCallback(const sp<IWifiStaIfaceEventCallback> & callback,registerEventCallback_cb hidl_status_cb)72 Return<void> WifiStaIface::registerEventCallback(
73     const sp<IWifiStaIfaceEventCallback>& callback,
74     registerEventCallback_cb hidl_status_cb) {
75     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
76                            &WifiStaIface::registerEventCallbackInternal,
77                            hidl_status_cb, callback);
78 }
79 
getCapabilities(getCapabilities_cb hidl_status_cb)80 Return<void> WifiStaIface::getCapabilities(getCapabilities_cb hidl_status_cb) {
81     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
82                            &WifiStaIface::getCapabilitiesInternal,
83                            hidl_status_cb);
84 }
85 
getApfPacketFilterCapabilities(getApfPacketFilterCapabilities_cb hidl_status_cb)86 Return<void> WifiStaIface::getApfPacketFilterCapabilities(
87     getApfPacketFilterCapabilities_cb hidl_status_cb) {
88     return validateAndCall(
89         this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
90         &WifiStaIface::getApfPacketFilterCapabilitiesInternal, hidl_status_cb);
91 }
92 
installApfPacketFilter(uint32_t cmd_id,const hidl_vec<uint8_t> & program,installApfPacketFilter_cb hidl_status_cb)93 Return<void> WifiStaIface::installApfPacketFilter(
94     uint32_t cmd_id, const hidl_vec<uint8_t>& program,
95     installApfPacketFilter_cb hidl_status_cb) {
96     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
97                            &WifiStaIface::installApfPacketFilterInternal,
98                            hidl_status_cb, cmd_id, program);
99 }
100 
readApfPacketFilterData(readApfPacketFilterData_cb hidl_status_cb)101 Return<void> WifiStaIface::readApfPacketFilterData(
102     readApfPacketFilterData_cb hidl_status_cb) {
103     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
104                            &WifiStaIface::readApfPacketFilterDataInternal,
105                            hidl_status_cb);
106 }
107 
getBackgroundScanCapabilities(getBackgroundScanCapabilities_cb hidl_status_cb)108 Return<void> WifiStaIface::getBackgroundScanCapabilities(
109     getBackgroundScanCapabilities_cb hidl_status_cb) {
110     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
111                            &WifiStaIface::getBackgroundScanCapabilitiesInternal,
112                            hidl_status_cb);
113 }
114 
getValidFrequenciesForBand(V1_0::WifiBand band,getValidFrequenciesForBand_cb hidl_status_cb)115 Return<void> WifiStaIface::getValidFrequenciesForBand(
116     V1_0::WifiBand band, getValidFrequenciesForBand_cb hidl_status_cb) {
117     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
118                            &WifiStaIface::getValidFrequenciesForBandInternal,
119                            hidl_status_cb, band);
120 }
121 
startBackgroundScan(uint32_t cmd_id,const StaBackgroundScanParameters & params,startBackgroundScan_cb hidl_status_cb)122 Return<void> WifiStaIface::startBackgroundScan(
123     uint32_t cmd_id, const StaBackgroundScanParameters& params,
124     startBackgroundScan_cb hidl_status_cb) {
125     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
126                            &WifiStaIface::startBackgroundScanInternal,
127                            hidl_status_cb, cmd_id, params);
128 }
129 
stopBackgroundScan(uint32_t cmd_id,stopBackgroundScan_cb hidl_status_cb)130 Return<void> WifiStaIface::stopBackgroundScan(
131     uint32_t cmd_id, stopBackgroundScan_cb hidl_status_cb) {
132     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
133                            &WifiStaIface::stopBackgroundScanInternal,
134                            hidl_status_cb, cmd_id);
135 }
136 
enableLinkLayerStatsCollection(bool debug,enableLinkLayerStatsCollection_cb hidl_status_cb)137 Return<void> WifiStaIface::enableLinkLayerStatsCollection(
138     bool debug, enableLinkLayerStatsCollection_cb hidl_status_cb) {
139     return validateAndCall(
140         this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
141         &WifiStaIface::enableLinkLayerStatsCollectionInternal, hidl_status_cb,
142         debug);
143 }
144 
disableLinkLayerStatsCollection(disableLinkLayerStatsCollection_cb hidl_status_cb)145 Return<void> WifiStaIface::disableLinkLayerStatsCollection(
146     disableLinkLayerStatsCollection_cb hidl_status_cb) {
147     return validateAndCall(
148         this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
149         &WifiStaIface::disableLinkLayerStatsCollectionInternal, hidl_status_cb);
150 }
151 
getLinkLayerStats(getLinkLayerStats_cb hidl_status_cb)152 Return<void> WifiStaIface::getLinkLayerStats(
153     getLinkLayerStats_cb hidl_status_cb) {
154     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
155                            &WifiStaIface::getLinkLayerStatsInternal,
156                            hidl_status_cb);
157 }
158 
getLinkLayerStats_1_3(getLinkLayerStats_1_3_cb hidl_status_cb)159 Return<void> WifiStaIface::getLinkLayerStats_1_3(
160     getLinkLayerStats_1_3_cb hidl_status_cb) {
161     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
162                            &WifiStaIface::getLinkLayerStatsInternal_1_3,
163                            hidl_status_cb);
164 }
165 
getLinkLayerStats_1_5(getLinkLayerStats_1_5_cb hidl_status_cb)166 Return<void> WifiStaIface::getLinkLayerStats_1_5(
167     getLinkLayerStats_1_5_cb hidl_status_cb) {
168     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
169                            &WifiStaIface::getLinkLayerStatsInternal_1_5,
170                            hidl_status_cb);
171 }
172 
startRssiMonitoring(uint32_t cmd_id,int32_t max_rssi,int32_t min_rssi,startRssiMonitoring_cb hidl_status_cb)173 Return<void> WifiStaIface::startRssiMonitoring(
174     uint32_t cmd_id, int32_t max_rssi, int32_t min_rssi,
175     startRssiMonitoring_cb hidl_status_cb) {
176     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
177                            &WifiStaIface::startRssiMonitoringInternal,
178                            hidl_status_cb, cmd_id, max_rssi, min_rssi);
179 }
180 
stopRssiMonitoring(uint32_t cmd_id,stopRssiMonitoring_cb hidl_status_cb)181 Return<void> WifiStaIface::stopRssiMonitoring(
182     uint32_t cmd_id, stopRssiMonitoring_cb hidl_status_cb) {
183     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
184                            &WifiStaIface::stopRssiMonitoringInternal,
185                            hidl_status_cb, cmd_id);
186 }
187 
getRoamingCapabilities(getRoamingCapabilities_cb hidl_status_cb)188 Return<void> WifiStaIface::getRoamingCapabilities(
189     getRoamingCapabilities_cb hidl_status_cb) {
190     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
191                            &WifiStaIface::getRoamingCapabilitiesInternal,
192                            hidl_status_cb);
193 }
194 
configureRoaming(const StaRoamingConfig & config,configureRoaming_cb hidl_status_cb)195 Return<void> WifiStaIface::configureRoaming(
196     const StaRoamingConfig& config, configureRoaming_cb hidl_status_cb) {
197     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
198                            &WifiStaIface::configureRoamingInternal,
199                            hidl_status_cb, config);
200 }
201 
setRoamingState(StaRoamingState state,setRoamingState_cb hidl_status_cb)202 Return<void> WifiStaIface::setRoamingState(StaRoamingState state,
203                                            setRoamingState_cb hidl_status_cb) {
204     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
205                            &WifiStaIface::setRoamingStateInternal,
206                            hidl_status_cb, state);
207 }
208 
enableNdOffload(bool enable,enableNdOffload_cb hidl_status_cb)209 Return<void> WifiStaIface::enableNdOffload(bool enable,
210                                            enableNdOffload_cb hidl_status_cb) {
211     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
212                            &WifiStaIface::enableNdOffloadInternal,
213                            hidl_status_cb, enable);
214 }
215 
startSendingKeepAlivePackets(uint32_t cmd_id,const hidl_vec<uint8_t> & ip_packet_data,uint16_t ether_type,const hidl_array<uint8_t,6> & src_address,const hidl_array<uint8_t,6> & dst_address,uint32_t period_in_ms,startSendingKeepAlivePackets_cb hidl_status_cb)216 Return<void> WifiStaIface::startSendingKeepAlivePackets(
217     uint32_t cmd_id, const hidl_vec<uint8_t>& ip_packet_data,
218     uint16_t ether_type, const hidl_array<uint8_t, 6>& src_address,
219     const hidl_array<uint8_t, 6>& dst_address, uint32_t period_in_ms,
220     startSendingKeepAlivePackets_cb hidl_status_cb) {
221     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
222                            &WifiStaIface::startSendingKeepAlivePacketsInternal,
223                            hidl_status_cb, cmd_id, ip_packet_data, ether_type,
224                            src_address, dst_address, period_in_ms);
225 }
226 
stopSendingKeepAlivePackets(uint32_t cmd_id,stopSendingKeepAlivePackets_cb hidl_status_cb)227 Return<void> WifiStaIface::stopSendingKeepAlivePackets(
228     uint32_t cmd_id, stopSendingKeepAlivePackets_cb hidl_status_cb) {
229     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
230                            &WifiStaIface::stopSendingKeepAlivePacketsInternal,
231                            hidl_status_cb, cmd_id);
232 }
233 
setScanningMacOui(const hidl_array<uint8_t,3> & oui,setScanningMacOui_cb hidl_status_cb)234 Return<void> WifiStaIface::setScanningMacOui(
235     const hidl_array<uint8_t, 3>& oui, setScanningMacOui_cb hidl_status_cb) {
236     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
237                            &WifiStaIface::setScanningMacOuiInternal,
238                            hidl_status_cb, oui);
239 }
240 
startDebugPacketFateMonitoring(startDebugPacketFateMonitoring_cb hidl_status_cb)241 Return<void> WifiStaIface::startDebugPacketFateMonitoring(
242     startDebugPacketFateMonitoring_cb hidl_status_cb) {
243     return validateAndCall(
244         this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
245         &WifiStaIface::startDebugPacketFateMonitoringInternal, hidl_status_cb);
246 }
247 
getDebugTxPacketFates(getDebugTxPacketFates_cb hidl_status_cb)248 Return<void> WifiStaIface::getDebugTxPacketFates(
249     getDebugTxPacketFates_cb hidl_status_cb) {
250     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
251                            &WifiStaIface::getDebugTxPacketFatesInternal,
252                            hidl_status_cb);
253 }
254 
getDebugRxPacketFates(getDebugRxPacketFates_cb hidl_status_cb)255 Return<void> WifiStaIface::getDebugRxPacketFates(
256     getDebugRxPacketFates_cb hidl_status_cb) {
257     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
258                            &WifiStaIface::getDebugRxPacketFatesInternal,
259                            hidl_status_cb);
260 }
261 
setMacAddress(const hidl_array<uint8_t,6> & mac,setMacAddress_cb hidl_status_cb)262 Return<void> WifiStaIface::setMacAddress(const hidl_array<uint8_t, 6>& mac,
263                                          setMacAddress_cb hidl_status_cb) {
264     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
265                            &WifiStaIface::setMacAddressInternal, hidl_status_cb,
266                            mac);
267 }
268 
getFactoryMacAddress(getFactoryMacAddress_cb hidl_status_cb)269 Return<void> WifiStaIface::getFactoryMacAddress(
270     getFactoryMacAddress_cb hidl_status_cb) {
271     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
272                            &WifiStaIface::getFactoryMacAddressInternal,
273                            hidl_status_cb);
274 }
275 
setScanMode(bool enable,setScanMode_cb hidl_status_cb)276 Return<void> WifiStaIface::setScanMode(bool enable,
277                                        setScanMode_cb hidl_status_cb) {
278     return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
279                            &WifiStaIface::setScanModeInternal, hidl_status_cb,
280                            enable);
281 }
282 
getNameInternal()283 std::pair<WifiStatus, std::string> WifiStaIface::getNameInternal() {
284     return {createWifiStatus(WifiStatusCode::SUCCESS), ifname_};
285 }
286 
getTypeInternal()287 std::pair<WifiStatus, IfaceType> WifiStaIface::getTypeInternal() {
288     return {createWifiStatus(WifiStatusCode::SUCCESS), IfaceType::STA};
289 }
290 
registerEventCallbackInternal(const sp<IWifiStaIfaceEventCallback> & callback)291 WifiStatus WifiStaIface::registerEventCallbackInternal(
292     const sp<IWifiStaIfaceEventCallback>& callback) {
293     if (!event_cb_handler_.addCallback(callback)) {
294         return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
295     }
296     return createWifiStatus(WifiStatusCode::SUCCESS);
297 }
298 
getCapabilitiesInternal()299 std::pair<WifiStatus, uint32_t> WifiStaIface::getCapabilitiesInternal() {
300     legacy_hal::wifi_error legacy_status;
301     uint64_t legacy_feature_set;
302     std::tie(legacy_status, legacy_feature_set) =
303         legacy_hal_.lock()->getSupportedFeatureSet(ifname_);
304     if (legacy_status != legacy_hal::WIFI_SUCCESS) {
305         return {createWifiStatusFromLegacyError(legacy_status), 0};
306     }
307     uint32_t legacy_logger_feature_set;
308     std::tie(legacy_status, legacy_logger_feature_set) =
309         legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname_);
310     if (legacy_status != legacy_hal::WIFI_SUCCESS) {
311         // some devices don't support querying logger feature set
312         legacy_logger_feature_set = 0;
313     }
314     uint32_t hidl_caps;
315     if (!hidl_struct_util::convertLegacyFeaturesToHidlStaCapabilities(
316             legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
317         return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
318     }
319     return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
320 }
321 
322 std::pair<WifiStatus, StaApfPacketFilterCapabilities>
getApfPacketFilterCapabilitiesInternal()323 WifiStaIface::getApfPacketFilterCapabilitiesInternal() {
324     legacy_hal::wifi_error legacy_status;
325     legacy_hal::PacketFilterCapabilities legacy_caps;
326     std::tie(legacy_status, legacy_caps) =
327         legacy_hal_.lock()->getPacketFilterCapabilities(ifname_);
328     if (legacy_status != legacy_hal::WIFI_SUCCESS) {
329         return {createWifiStatusFromLegacyError(legacy_status), {}};
330     }
331     StaApfPacketFilterCapabilities hidl_caps;
332     if (!hidl_struct_util::convertLegacyApfCapabilitiesToHidl(legacy_caps,
333                                                               &hidl_caps)) {
334         return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
335     }
336     return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
337 }
338 
installApfPacketFilterInternal(uint32_t,const std::vector<uint8_t> & program)339 WifiStatus WifiStaIface::installApfPacketFilterInternal(
340     uint32_t /* cmd_id */, const std::vector<uint8_t>& program) {
341     legacy_hal::wifi_error legacy_status =
342         legacy_hal_.lock()->setPacketFilter(ifname_, program);
343     return createWifiStatusFromLegacyError(legacy_status);
344 }
345 
346 std::pair<WifiStatus, std::vector<uint8_t>>
readApfPacketFilterDataInternal()347 WifiStaIface::readApfPacketFilterDataInternal() {
348     const std::pair<legacy_hal::wifi_error, std::vector<uint8_t>>
349         legacy_status_and_data =
350             legacy_hal_.lock()->readApfPacketFilterData(ifname_);
351     return {createWifiStatusFromLegacyError(legacy_status_and_data.first),
352             std::move(legacy_status_and_data.second)};
353 }
354 
355 std::pair<WifiStatus, StaBackgroundScanCapabilities>
getBackgroundScanCapabilitiesInternal()356 WifiStaIface::getBackgroundScanCapabilitiesInternal() {
357     legacy_hal::wifi_error legacy_status;
358     legacy_hal::wifi_gscan_capabilities legacy_caps;
359     std::tie(legacy_status, legacy_caps) =
360         legacy_hal_.lock()->getGscanCapabilities(ifname_);
361     if (legacy_status != legacy_hal::WIFI_SUCCESS) {
362         return {createWifiStatusFromLegacyError(legacy_status), {}};
363     }
364     StaBackgroundScanCapabilities hidl_caps;
365     if (!hidl_struct_util::convertLegacyGscanCapabilitiesToHidl(legacy_caps,
366                                                                 &hidl_caps)) {
367         return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
368     }
369     return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
370 }
371 
372 std::pair<WifiStatus, std::vector<WifiChannelInMhz>>
getValidFrequenciesForBandInternal(V1_0::WifiBand band)373 WifiStaIface::getValidFrequenciesForBandInternal(V1_0::WifiBand band) {
374     static_assert(sizeof(WifiChannelInMhz) == sizeof(uint32_t),
375                   "Size mismatch");
376     legacy_hal::wifi_error legacy_status;
377     std::vector<uint32_t> valid_frequencies;
378     std::tie(legacy_status, valid_frequencies) =
379         legacy_hal_.lock()->getValidFrequenciesForBand(
380             ifname_, hidl_struct_util::convertHidlWifiBandToLegacy(band));
381     return {createWifiStatusFromLegacyError(legacy_status), valid_frequencies};
382 }
383 
startBackgroundScanInternal(uint32_t cmd_id,const StaBackgroundScanParameters & params)384 WifiStatus WifiStaIface::startBackgroundScanInternal(
385     uint32_t cmd_id, const StaBackgroundScanParameters& params) {
386     legacy_hal::wifi_scan_cmd_params legacy_params;
387     if (!hidl_struct_util::convertHidlGscanParamsToLegacy(params,
388                                                           &legacy_params)) {
389         return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
390     }
391     android::wp<WifiStaIface> weak_ptr_this(this);
392     const auto& on_failure_callback =
393         [weak_ptr_this](legacy_hal::wifi_request_id id) {
394             const auto shared_ptr_this = weak_ptr_this.promote();
395             if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
396                 LOG(ERROR) << "Callback invoked on an invalid object";
397                 return;
398             }
399             for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
400                 if (!callback->onBackgroundScanFailure(id).isOk()) {
401                     LOG(ERROR)
402                         << "Failed to invoke onBackgroundScanFailure callback";
403                 }
404             }
405         };
406     const auto& on_results_callback =
407         [weak_ptr_this](
408             legacy_hal::wifi_request_id id,
409             const std::vector<legacy_hal::wifi_cached_scan_results>& results) {
410             const auto shared_ptr_this = weak_ptr_this.promote();
411             if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
412                 LOG(ERROR) << "Callback invoked on an invalid object";
413                 return;
414             }
415             std::vector<StaScanData> hidl_scan_datas;
416             if (!hidl_struct_util::
417                     convertLegacyVectorOfCachedGscanResultsToHidl(
418                         results, &hidl_scan_datas)) {
419                 LOG(ERROR) << "Failed to convert scan results to HIDL structs";
420                 return;
421             }
422             for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
423                 if (!callback->onBackgroundScanResults(id, hidl_scan_datas)
424                          .isOk()) {
425                     LOG(ERROR)
426                         << "Failed to invoke onBackgroundScanResults callback";
427                 }
428             }
429         };
430     const auto& on_full_result_callback = [weak_ptr_this](
431                                               legacy_hal::wifi_request_id id,
432                                               const legacy_hal::
433                                                   wifi_scan_result* result,
434                                               uint32_t buckets_scanned) {
435         const auto shared_ptr_this = weak_ptr_this.promote();
436         if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
437             LOG(ERROR) << "Callback invoked on an invalid object";
438             return;
439         }
440         StaScanResult hidl_scan_result;
441         if (!hidl_struct_util::convertLegacyGscanResultToHidl(
442                 *result, true, &hidl_scan_result)) {
443             LOG(ERROR) << "Failed to convert full scan results to HIDL structs";
444             return;
445         }
446         for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
447             if (!callback
448                      ->onBackgroundFullScanResult(id, buckets_scanned,
449                                                   hidl_scan_result)
450                      .isOk()) {
451                 LOG(ERROR)
452                     << "Failed to invoke onBackgroundFullScanResult callback";
453             }
454         }
455     };
456     legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startGscan(
457         ifname_, cmd_id, legacy_params, on_failure_callback,
458         on_results_callback, on_full_result_callback);
459     return createWifiStatusFromLegacyError(legacy_status);
460 }
461 
stopBackgroundScanInternal(uint32_t cmd_id)462 WifiStatus WifiStaIface::stopBackgroundScanInternal(uint32_t cmd_id) {
463     legacy_hal::wifi_error legacy_status =
464         legacy_hal_.lock()->stopGscan(ifname_, cmd_id);
465     return createWifiStatusFromLegacyError(legacy_status);
466 }
467 
enableLinkLayerStatsCollectionInternal(bool debug)468 WifiStatus WifiStaIface::enableLinkLayerStatsCollectionInternal(bool debug) {
469     legacy_hal::wifi_error legacy_status =
470         legacy_hal_.lock()->enableLinkLayerStats(ifname_, debug);
471     return createWifiStatusFromLegacyError(legacy_status);
472 }
473 
disableLinkLayerStatsCollectionInternal()474 WifiStatus WifiStaIface::disableLinkLayerStatsCollectionInternal() {
475     legacy_hal::wifi_error legacy_status =
476         legacy_hal_.lock()->disableLinkLayerStats(ifname_);
477     return createWifiStatusFromLegacyError(legacy_status);
478 }
479 
480 std::pair<WifiStatus, V1_0::StaLinkLayerStats>
getLinkLayerStatsInternal()481 WifiStaIface::getLinkLayerStatsInternal() {
482     return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
483 }
484 
485 std::pair<WifiStatus, V1_3::StaLinkLayerStats>
getLinkLayerStatsInternal_1_3()486 WifiStaIface::getLinkLayerStatsInternal_1_3() {
487     return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
488 }
489 
490 std::pair<WifiStatus, V1_5::StaLinkLayerStats>
getLinkLayerStatsInternal_1_5()491 WifiStaIface::getLinkLayerStatsInternal_1_5() {
492     legacy_hal::wifi_error legacy_status;
493     legacy_hal::LinkLayerStats legacy_stats;
494     std::tie(legacy_status, legacy_stats) =
495         legacy_hal_.lock()->getLinkLayerStats(ifname_);
496     if (legacy_status != legacy_hal::WIFI_SUCCESS) {
497         return {createWifiStatusFromLegacyError(legacy_status), {}};
498     }
499     V1_5::StaLinkLayerStats hidl_stats;
500     if (!hidl_struct_util::convertLegacyLinkLayerStatsToHidl(legacy_stats,
501                                                              &hidl_stats)) {
502         return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
503     }
504     return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
505 }
506 
startRssiMonitoringInternal(uint32_t cmd_id,int32_t max_rssi,int32_t min_rssi)507 WifiStatus WifiStaIface::startRssiMonitoringInternal(uint32_t cmd_id,
508                                                      int32_t max_rssi,
509                                                      int32_t min_rssi) {
510     android::wp<WifiStaIface> weak_ptr_this(this);
511     const auto& on_threshold_breached_callback =
512         [weak_ptr_this](legacy_hal::wifi_request_id id,
513                         std::array<uint8_t, 6> bssid, int8_t rssi) {
514             const auto shared_ptr_this = weak_ptr_this.promote();
515             if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
516                 LOG(ERROR) << "Callback invoked on an invalid object";
517                 return;
518             }
519             for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
520                 if (!callback->onRssiThresholdBreached(id, bssid, rssi)
521                          .isOk()) {
522                     LOG(ERROR)
523                         << "Failed to invoke onRssiThresholdBreached callback";
524                 }
525             }
526         };
527     legacy_hal::wifi_error legacy_status =
528         legacy_hal_.lock()->startRssiMonitoring(ifname_, cmd_id, max_rssi,
529                                                 min_rssi,
530                                                 on_threshold_breached_callback);
531     return createWifiStatusFromLegacyError(legacy_status);
532 }
533 
stopRssiMonitoringInternal(uint32_t cmd_id)534 WifiStatus WifiStaIface::stopRssiMonitoringInternal(uint32_t cmd_id) {
535     legacy_hal::wifi_error legacy_status =
536         legacy_hal_.lock()->stopRssiMonitoring(ifname_, cmd_id);
537     return createWifiStatusFromLegacyError(legacy_status);
538 }
539 
540 std::pair<WifiStatus, StaRoamingCapabilities>
getRoamingCapabilitiesInternal()541 WifiStaIface::getRoamingCapabilitiesInternal() {
542     legacy_hal::wifi_error legacy_status;
543     legacy_hal::wifi_roaming_capabilities legacy_caps;
544     std::tie(legacy_status, legacy_caps) =
545         legacy_hal_.lock()->getRoamingCapabilities(ifname_);
546     if (legacy_status != legacy_hal::WIFI_SUCCESS) {
547         return {createWifiStatusFromLegacyError(legacy_status), {}};
548     }
549     StaRoamingCapabilities hidl_caps;
550     if (!hidl_struct_util::convertLegacyRoamingCapabilitiesToHidl(legacy_caps,
551                                                                   &hidl_caps)) {
552         return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
553     }
554     return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
555 }
556 
configureRoamingInternal(const StaRoamingConfig & config)557 WifiStatus WifiStaIface::configureRoamingInternal(
558     const StaRoamingConfig& config) {
559     legacy_hal::wifi_roaming_config legacy_config;
560     if (!hidl_struct_util::convertHidlRoamingConfigToLegacy(config,
561                                                             &legacy_config)) {
562         return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
563     }
564     legacy_hal::wifi_error legacy_status =
565         legacy_hal_.lock()->configureRoaming(ifname_, legacy_config);
566     return createWifiStatusFromLegacyError(legacy_status);
567 }
568 
setRoamingStateInternal(StaRoamingState state)569 WifiStatus WifiStaIface::setRoamingStateInternal(StaRoamingState state) {
570     legacy_hal::wifi_error legacy_status =
571         legacy_hal_.lock()->enableFirmwareRoaming(
572             ifname_, hidl_struct_util::convertHidlRoamingStateToLegacy(state));
573     return createWifiStatusFromLegacyError(legacy_status);
574 }
575 
enableNdOffloadInternal(bool enable)576 WifiStatus WifiStaIface::enableNdOffloadInternal(bool enable) {
577     legacy_hal::wifi_error legacy_status =
578         legacy_hal_.lock()->configureNdOffload(ifname_, enable);
579     return createWifiStatusFromLegacyError(legacy_status);
580 }
581 
startSendingKeepAlivePacketsInternal(uint32_t cmd_id,const std::vector<uint8_t> & ip_packet_data,uint16_t ether_type,const std::array<uint8_t,6> & src_address,const std::array<uint8_t,6> & dst_address,uint32_t period_in_ms)582 WifiStatus WifiStaIface::startSendingKeepAlivePacketsInternal(
583     uint32_t cmd_id, const std::vector<uint8_t>& ip_packet_data,
584     uint16_t ether_type, const std::array<uint8_t, 6>& src_address,
585     const std::array<uint8_t, 6>& dst_address, uint32_t period_in_ms) {
586     legacy_hal::wifi_error legacy_status =
587         legacy_hal_.lock()->startSendingOffloadedPacket(
588             ifname_, cmd_id, ether_type, ip_packet_data, src_address,
589             dst_address, period_in_ms);
590     return createWifiStatusFromLegacyError(legacy_status);
591 }
592 
stopSendingKeepAlivePacketsInternal(uint32_t cmd_id)593 WifiStatus WifiStaIface::stopSendingKeepAlivePacketsInternal(uint32_t cmd_id) {
594     legacy_hal::wifi_error legacy_status =
595         legacy_hal_.lock()->stopSendingOffloadedPacket(ifname_, cmd_id);
596     return createWifiStatusFromLegacyError(legacy_status);
597 }
598 
setScanningMacOuiInternal(const std::array<uint8_t,3> &)599 WifiStatus WifiStaIface::setScanningMacOuiInternal(
600     const std::array<uint8_t, 3>& /* oui */) {
601     // deprecated.
602     return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
603 }
604 
startDebugPacketFateMonitoringInternal()605 WifiStatus WifiStaIface::startDebugPacketFateMonitoringInternal() {
606     legacy_hal::wifi_error legacy_status =
607         legacy_hal_.lock()->startPktFateMonitoring(ifname_);
608     return createWifiStatusFromLegacyError(legacy_status);
609 }
610 
611 std::pair<WifiStatus, std::vector<WifiDebugTxPacketFateReport>>
getDebugTxPacketFatesInternal()612 WifiStaIface::getDebugTxPacketFatesInternal() {
613     legacy_hal::wifi_error legacy_status;
614     std::vector<legacy_hal::wifi_tx_report> legacy_fates;
615     std::tie(legacy_status, legacy_fates) =
616         legacy_hal_.lock()->getTxPktFates(ifname_);
617     if (legacy_status != legacy_hal::WIFI_SUCCESS) {
618         return {createWifiStatusFromLegacyError(legacy_status), {}};
619     }
620     std::vector<WifiDebugTxPacketFateReport> hidl_fates;
621     if (!hidl_struct_util::convertLegacyVectorOfDebugTxPacketFateToHidl(
622             legacy_fates, &hidl_fates)) {
623         return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
624     }
625     return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_fates};
626 }
627 
628 std::pair<WifiStatus, std::vector<WifiDebugRxPacketFateReport>>
getDebugRxPacketFatesInternal()629 WifiStaIface::getDebugRxPacketFatesInternal() {
630     legacy_hal::wifi_error legacy_status;
631     std::vector<legacy_hal::wifi_rx_report> legacy_fates;
632     std::tie(legacy_status, legacy_fates) =
633         legacy_hal_.lock()->getRxPktFates(ifname_);
634     if (legacy_status != legacy_hal::WIFI_SUCCESS) {
635         return {createWifiStatusFromLegacyError(legacy_status), {}};
636     }
637     std::vector<WifiDebugRxPacketFateReport> hidl_fates;
638     if (!hidl_struct_util::convertLegacyVectorOfDebugRxPacketFateToHidl(
639             legacy_fates, &hidl_fates)) {
640         return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
641     }
642     return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_fates};
643 }
644 
setMacAddressInternal(const std::array<uint8_t,6> & mac)645 WifiStatus WifiStaIface::setMacAddressInternal(
646     const std::array<uint8_t, 6>& mac) {
647     bool status = iface_util_.lock()->setMacAddress(ifname_, mac);
648     if (!status) {
649         return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
650     }
651     return createWifiStatus(WifiStatusCode::SUCCESS);
652 }
653 
654 std::pair<WifiStatus, std::array<uint8_t, 6>>
getFactoryMacAddressInternal()655 WifiStaIface::getFactoryMacAddressInternal() {
656     std::array<uint8_t, 6> mac =
657         iface_util_.lock()->getFactoryMacAddress(ifname_);
658     if (mac[0] == 0 && mac[1] == 0 && mac[2] == 0 && mac[3] == 0 &&
659         mac[4] == 0 && mac[5] == 0) {
660         return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), mac};
661     }
662     return {createWifiStatus(WifiStatusCode::SUCCESS), mac};
663 }
664 
setScanModeInternal(bool enable)665 WifiStatus WifiStaIface::setScanModeInternal(bool enable) {
666     // OEM's need to implement this on their devices if needed.
667     LOG(WARNING) << "setScanModeInternal(" << enable << ") not supported";
668     return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
669 }
670 
671 }  // namespace implementation
672 }  // namespace V1_5
673 }  // namespace wifi
674 }  // namespace hardware
675 }  // namespace android
676